How do I make this app factor a polynomial with quadratic formula or some other trick? - polynomials

How do I make this app factor a polynomial with quadratic formula or some other trick?.
The polynomial 3x^2 +10x -8 can be factored to (x +4)(3x -2) with the use of
grouping or the X trick. I would like my code to do this
but I have no clue how to accomplish it. My code factors the polynomial to (x -0.6666666666666666)(x +4.0)
which is not what I want from it.
This code snippet
require "option_parser"
puts "Please enter your variable values assuming the form ax**2 + bx + c ."
puts "a: "
a = gets
exit if a.nil?
a = a.to_i
puts "b: "
b = gets
exit if b.nil?
b = b.to_i
puts "c: "
c = gets
exit if c.nil?
c = c.to_i
the_gcd = a.gcd(b).gcd(a.gcd(c))
if the_gcd != 1
x1 = (-1*b + (b**2 - 4*a*c) ** 0.5)/(2 * a)
x2 = (-1*b - (b**2 - 4*a*c) ** 0.5)/(2 * a)
if x1.to_i - x1 != 0 || x2.to_i - x2 != 0
puts "The root is not a whole number. Consider grouping for factoring the polynomial."
if x1 < 0 && x2 < 0
puts "#{the_gcd}(x +#{-x1})(x +#{-x2})"
elsif x1 < 0 && x2 > 0
puts "#{the_gcd}(x +#{-x1})(x #{-x2})"
elsif x1 > 0 && x2 < 0
puts "#{the_gcd}(x #{-x1})(x +#{-x2})"
elsif x1 > 0 && x2 > 0
puts "#{the_gcd}(x #{-x1})(x #{-x2})"
end
exit
else
if x1 < 0 && x2 < 0
puts "#{the_gcd}(x +#{-x1.to_i})(x +#{-x2.to_i})"
exit
elsif x1 < 0 && x2 > 0
puts "#{the_gcd}(x +#{-x1.to_i})(x #{-x2.to_i})"
exit
elsif x1 > 0 && x2 < 0
puts "#{the_gcd}(x #{-x1.to_i})(x +#{-x2.to_i})"
exit
elsif x1 > 0 && x2 > 0
puts "#{the_gcd}(x #{-x1.to_i})(x #{-x2.to_i})"
exit
end
end
if (b**2 - 4*a*c) < 0
puts "No real solution. Imaginary numbers involved in the solution."
exit
end
end
#The part below does not utilize GCD
x1 = (-1*b + (b**2 - 4*a*c) ** 0.5)/(2 * a)
x2 = (-1*b - (b**2 - 4*a*c) ** 0.5)/(2 * a)
if x1.to_i - x1 != 0 || x2.to_i - x2 != 0
puts "The root is not a whole number. Consider grouping for factoring the polynomial."
if x1 < 0 && x2 < 0
puts "(x +#{-x1})(x +#{-x2})"
elsif x1 < 0 && x2 > 0
puts "(x +#{-x1})(x #{-x2})"
elsif x1 > 0 && x2 < 0
puts "(x #{-x1})(x +#{-x2})"
elsif x1 > 0 && x2 > 0
puts "(x #{-x1})(x #{-x2})"
end
exit
else
if x1 < 0 && x2 < 0
puts "(x +#{-x1.to_i})(x +#{-x2.to_i})"
elsif x1 < 0 && x2 > 0
puts "(x +#{-x1.to_i})(x #{-x2.to_i})"
elsif x1 > 0 && x2 < 0
puts "(x #{-x1.to_i})(x +#{-x2.to_i})"
elsif x1 > 0 && x2 > 0
puts "(x #{-x1.to_i})(x #{-x2.to_i})"
end
end
if (b**2 - 4*a*c) < 0
puts "No real solution. Imaginary numbers involved in the solution."
exit
end
is part of a larger app that deals with other methods of factoring quadratic polynomials as well.
Let these other methods of factoring, such as factoring perfect square trinomials or difference of perfect squares, be
outside of the scope of this question.
I came across a similar problem in this Python code, that apparently was solved with the use of fractions.
Might one utilize fractions in Crystal in order to solve my problem?

Related

How should I implement the Grassfire Algorithm in C++

So in my program, I generate a random grid using 2D Arrays where all indexes are initialized to 0. Now, a certain percentage of random indexes are filled with -1 which means that they are impassable/ act like a wall. The user also inputs a certain target index say (i,j) from where he starts and his goal is to reach index (0,0) by taking the shortest path possible.
To find the shortest path, I have to check for the neighbours of each cell, starting from the target location. If they have neighbours, I increment the neighbour value by 1. Refer to my figure for more details. I got the code on how to calculate the shortest path, but I'm stuck with this incrementation part. I tried writing a code but it doesn't seem to work. Any help would be appreciated:-
GRID is generated in the following way:
1 is the user input location, and the goal is to reach X i.e 0,0
-X 0 0 0 0 0 0 0 0 -1
-0 0 0 -1 -1 0 0 0 0 0
-0 0 0 0 -1 0 0 0 0 0
-0 0 0 0 0 0 0 0 0 -1
-0 0 0 0 0 0 0 1 0 0
Starting by incrementing
-X 0 0 0 0 0 0 0 0 -1
-0 0 0 -1 -1 0 0 0 0 0
-0 0 0 0 -1 3 3 3 3 3
-0 0 0 0 0 3 2 2 2 -1
-0 0 0 0 0 3 2 1 2 3
I have only showed it till 3, but it keeps on going until index 0,0 is reached.
void waveAlgorithm(int *array, int height, int width, int x, int y)
{
while (array != NULL)
{
// Assume that index 0 0 is never 1
if (currX == 0 && currY == 0){
break;
}
// Check South
int currX = x;
int currY = y + 1;
if (currX < width && currX > 0 && currY < height && currY >= 0)
{
if (*(array + currX * width + currY) == 0)
{
(*(array + currX * width + currY))++;
}
}
// Check North
currX = x;
currY = y - 1;
if (currX < width && currX > 0 && currY < height && currY >= 0)
{
if (*(array + currX * width + currY) != -1)
{
(*(array + currX * width + currY))++;
}
}
// Check West
currX = x - 1;
currY = y;
if (currX < width && currX > 0 && currY < height && currY >= 0)
{
if (*(array + currX * width + currY) != -1)
{
(*(array + currX * width + currY))++;
}
}
// Check East
currX = x + 1;
currY = y;
if (currX < width && currX > 0 && currY < height && currY >= 0)
{
if (*(array + currX * width + currY) != -1)
{
(*(array + currX * width + currY))++;
}
}
}
}
I am kinda stuck while implementing this program, especially for the the directions that are combinational i.e North East, South East, etc. I tried writing a recursive program but couldn't figure out how to increment the cells
waveAlgorithm(int *arr)
{
if(index is 0,0)
return;
waveAlgorithm(int[i+1][j]);
waveAlgorithm(int[i][j+1]);
waveAlgorithm(int[i-1][j]);
waveAlgorithm(int[i][j-1]);
}

How to make lemmas about changes made to heap objects?

I'm trying to implement a MaxHeap using Dafny based on the code from Intro. to Algorithms, CLRS 3rd edition, section 6.1, page 153 or the Max-Heapify function here. I switched from using recursion to a while loop because that seemed a bit easier to handle in Dafny.
Trying to prove the heap property on an array after calling the heapify function. In particular I was hoping to be able to use the require statement on heapify to assert that triples which didn't change in the heap which were satisfying the heap property before an update, are satisfying the heap property after the update.
However, after making any changes to the array it seems like it forgets all about the require/invariant statement. Even if I show that the value are the same before and after the update it still no longer passes the assertion. I pulled out the update into the swap method.
I was hoping I could write a lemma asserting this fact but it seems like lemmas don't allow modifying the heap or using old() or calling a method. Is there a way to write a lemma for this?
function method parent(i: int): int
{
i/2
}
function method left(i: int): int
{
2*i
}
function method right(i: int): int
{
2*i+1
}
class MaxHeap {
var data: array<int>
ghost var repr: set<object>
constructor(data: array<int>)
ensures this.data == data
ensures this in repr
{
this.data := data;
this.repr := {this};
}
predicate method MaxHeapChildren(i: int)
reads this, this.data
requires 1 <= i
{
(left(i) > this.data.Length || this.data[i-1] >= this.data[left(i)-1]) && (right(i) > this.data.Length || this.data[i-1] >= this.data[right(i)-1])
}
method heapify(i: int) returns (largest: int)
modifies this.data
requires 1 <= i <= this.data.Length
requires forall x :: i < x <= this.data.Length ==> MaxHeapChildren(x)
// ensures multiset(this.data[..]) == multiset(old(this.data[..]))
ensures forall x :: i <= x <= this.data.Length ==> MaxHeapChildren(x)
decreases this.data.Length - i
{
var i' := i;
ghost var oldi := i;
largest := i;
var l := left(i);
var r := right(i);
ghost var count := 0;
ghost var count' := 1;
while !MaxHeapChildren(i')
invariant count' == count + 1;
invariant 1 <= largest <= this.data.Length
invariant l == left(i')
invariant r == right(i')
invariant 1 <= i' <= this.data.Length
invariant i' == i || i' == left(oldi) || i' == right(oldi)
invariant largest == i'
invariant count == 0 ==> oldi == i
invariant oldi > 0
invariant count > 0 ==> oldi == parent(i')
invariant count > 0 ==> MaxHeapChildren(oldi)
invariant count > 0 ==> forall x :: i <= x < i' ==> old(this.data)[x] == this.data[x]
invariant count > 0 ==> forall x :: i <= x < i' && left(x+1) < this.data.Length ==> old(this.data)[left(x+1)] == this.data[left(x+1)]
invariant count > 0 ==> forall x :: i <= x < i' && right(x+1) < this.data.Length ==> old(this.data)[right(x+1)] == this.data[right(x+1)]
// invariant count > 0 ==> forall x :: i <= x <= i' && left(x+1) ==> MaxHeapChildren(left(x+1))
invariant forall x :: i <= x <= this.data.Length && x != i' ==> MaxHeapChildren(x)
decreases this.data.Length-i';
{
if l <= this.data.Length && this.data[l-1] > this.data[i'-1] {
largest := l;
}
if r <= this.data.Length && this.data[r-1] > this.data[largest-1] {
largest := r;
}
if largest != i' {
assert forall x :: i < x <= this.data.Length && x != i' ==> MaxHeapChildren(x);
swap(this, i', largest);
label AfterChange:
oldi := i';
assert MaxHeapChildren(oldi);
i' := largest;
assert forall x :: largest < x <= this.data.Length && x != i' ==> MaxHeapChildren(x);
l := left(i');
r := right(i');
assert forall x :: i <= x < i' ==> old#AfterChange(this.data[x]) == this.data[x] && left(x+1) < this.data.Length ==> old(this.data)[left(x+1)] == this.data[left(x+1)] && right(x+1) < this.data.Length ==> old(this.data)[right(x+1)] == this.data[right(x+1)];
}else{
assert MaxHeapChildren(i');
assert MaxHeapChildren(oldi);
}
count := count + 1;
count' := count' + 1;
}
}
}
method swap(heap: MaxHeap, i: int, largest: int)
modifies heap.data
requires 1 <= i < largest <= heap.data.Length
requires heap.data[largest-1] > heap.data[i-1]
requires left(i) <= heap.data.Length ==> heap.data[largest-1] >= heap.data[left(i)-1]
requires right(i) <= heap.data.Length ==> heap.data[largest-1] >= heap.data[right(i)-1]
requires forall x :: i <= x <= heap.data.Length && x != i ==> heap.MaxHeapChildren(x)
ensures heap.data[i-1] == old(heap.data[largest-1])
ensures heap.data[largest-1] == old(heap.data[i-1])
ensures heap.MaxHeapChildren(i)
ensures forall x :: 1 <= x <= heap.data.Length && x != i && x != largest ==> heap.data[x-1] == old(heap.data[x-1])
ensures forall x :: i <= x <= heap.data.Length && x != largest ==> heap.MaxHeapChildren(x)
{
ghost var oldData := heap.data[..];
var temp := heap.data[i-1];
heap.data[i-1] := heap.data[largest-1];
heap.data[largest-1] := temp;
var z:int :| assume i < z <= heap.data.Length && z != largest;
var lz: int := left(z);
var rz: int := right(z);
assert heap.data[z-1] == old(heap.data[z-1]);
assert lz != i && lz != largest && lz <= heap.data.Length ==> heap.data[lz-1] == old(heap.data[lz-1]);
assert rz != i && rz != largest && rz <= heap.data.Length ==> heap.data[rz-1] == old(heap.data[rz-1]);
assert heap.MaxHeapChildren(z);
}
/**
heapify(4)
length = 17
i = 4
left = 8, 7 (0based)
right = 9, 8 (0based)
x in 5 .. 17 :: MaxHeapChildren(x) (i+1)..17
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[20,18,16,3,14,12,10, 8, 6, 4, 2, 0, 1,-2, 4, 4,-5]
i = 8
left = 16
right = 17
x in i' .. i-1 :: MaxHeapChildren (4..15)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[20,18,16,8,14,12,10, 3, 6, 4, 2, 0, 1,-2, 4, 4,-5]
i = 16
left = 32
right = 33
x in i' .. i-1 :: MaxHeapChildren (4..16) + 17.. MaxHeapChildren
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
[20,18,16,8,14,12,10, 4, 6, 4, 2, 0, 1,-2, 4, 3,-5]
*/
Yes, I think what you are looking for is called a twostate lemma
Basically, you can use old() in their specification, and at the call site, you can specify which heap to consider for calls to old() by suffixing the lemma's name by #a, if label a: existed before that method's call.

Setting up an Minimizing the sum of absolute deviation linear programing problem in CPlex

I am new to CPLEx and trying to set up my first problem. What I want to do set up a LP to minimise the sum of absolute deviations. I have set up the below as a start (based on googling possibilities). This is only a single deviation. I thought I would get this to work and then add to ti. It loads ok but won't solve. Can anyone shed some light on where I need to go next?
Minimize
obj: y1pos + y1neg
Subject To
c1: x0 + x1 + x2 + x3 = 1
c2: y1pos - y1neg + 451320 x0 + 500870 x1 + 483425 x2 + 447330 x3 = 58999
Bounds
0 <= x0 <= 1
0 <= x1 <= 1
0 <= x2 <= 1
0 <= x3 <= 1
y1pos >= 0
y1neg <= 0
End
As Erwin Kalvelagen suggested, changing y1neg <= 0 to y1neg >= 0 was the answer since our our error factor in our constraint is y1pos - y1neg which we want to minimise.

Having negative value for non basic variable gives a non feasible solution in simplex method?

Objective function => x1 - 2x2
Subject to =>
x2 <= 5
x1 - x2 >= 2
x1 ,x2, x3 >= 0
Maximize?
convert to standard form :
Maximize -> -x1 + 2x2
Subject to ->
x2 <= 5
-x1 + x2 <= -2
convert to slack form :
Z = -x1 + 2x2
x3 = 5 - x2
x4 = -2 +x1 -x2
Basic solution (0,0,5,-2)
Can I found optimal solution in here? If not why?

Constraints - linear programming- CPLEX

How do I write such constraints in CPLEX (linear-programming) ?
forall(p in P, x in X, y in Y)
if ((remx[p,x] <= 0) OR (remy[p,y] <= 0)) then
pbl[p,x,y] == 0 // MUST be 0
else
pbl[p,x,y] == 1 OR == 0 // can be 0 or 1
where pbl is a decision variable (matrix), remx and remy is a normal matrix variable and p,x,y are indices.
I can not use if-then
Thanks,
I believe this cannot be done using (continuous) linear programming, but using mixed-integer programming we can use binary variables.
One way to attack this is using a bunch of inequalities, something like:
remx[p,x] <= 0 + bx[p,x]*M
remx[p,x] >= 0 - (1-bx[p,x])*M
remy[p,y] <= 0 + by[p,y]*M
remy[p,y] >= 0 - (1-by[p,y])*M
pbl[p,x,y] >= bx[p,x]+by[p,y]-1
pbl[p,x,y] <= bx[p,x]
pbl[p,x,y] <= bx[p,x]
bx[p,x],bx[p,x] in {0,1}
where M is indicating a sufficiently large number (they form a bound on remx and remy). Alternatively you can use the indicator constraints in Cplex to model implications:
bx[p,x]=0 => remx[p,x] <= 0
bx[p,x]=1 => remx[p,x] >= 0
by[p,y]=0 => remy[p,y] <= 0
by[p,y]=1 => remy[p,y] >= 0
pbl[p,x,y] = 1 => bx[p,x]+by[p,y] = 2
pbl[p,x,y] = 0 => bx[p,x]+by[p,y] <= 1
(Note: the question has changed, so these fragments are no longer 100% correct).