GOAL: I want to turn the following sequence of operations into Python:
- Isolate a square root term on the LHS.
- Square both sides of the equation.
- Add and subtract terms accordingly to get 0 on LHS.
- Isolate another square root term on the LHS. [ i.e. step 1) ]
- Repeat step 2).
- Repeat step 3).
- Repeat step 1).
ETC...
to de-radicalize equations like this one and get them into forms like this i.e. a polynomial p(x) with integer powers of x.
WHAT I HAVE TRIED: I have tried writing an outline for this, but I don't know how to formalize this for Python. Here is my work:
Notation:
- LHS: Left hand side (of the equation).
- TBS: To both sides (of the equation).
- FBS: From both sides (of the equation).
- BS: Both sides (of the equation).
- Semicolons are used to denote the desired value of the LHS at each stage.
- By "X has a term to the power of 1/2" I mean the element of the equation X has coefficient/variable raised to that fractional power.
EQUATION={ 0 = A B C }
If A has a term to the power if 1/2 then
If sign of A is then
Subtract A FBS ; LHS=A
Square BS ; LHS=A^2
Subtract A^2 FBS ; LHS=0
Else
Add A TBS ; LHS=A
Square BS ; LHS=A^2
Subtract A^2 FBS ; LHS=0
Elif B has a term to the power of 1/2 then
If sign of B is then
Subtract B FBS ; LHS=B
Square BS ; LHS=B^2
Subtract B^2 FBS ; LHS=0
Else
Add B TBS ; LHS=B
Square BS ; LHS=B^2
Subtract B^2 FBS ; LHS=0
Elif C has a term to the power of 1/2 then
If sign of C is then
Subtract C FBS ; LHS=C
Square BS ; LHS=C^2
Subtract C^2 FBS ; LHS=0
Else
Add C TBS ; LHS=C
Square BS ; LHS=C^2
Subtract C^2 FBS ; LHS=0
Elif A nor B nor C have terms to the power 1/2 then
Stop the program
CodePudding user response:
The sign of the terms, A
, B
, C
are not to be considered. In any case you have to substract the term in order to that term vanish from the RHS
.
So in the first step you always get -X
in the LHS
(you put LHS=X
, but I suppose was a mistake)
Then you square both sides and get X^2
in LHS
.
The last Elif
can be replaced by just an Else
.
So this is the code a bit simplified
EQUATION={ 0 = A B C }
If A has a term to the power of 1/2 then
Subtract A FBS ; LHS=-A
Square BS ; LHS=A^2
Subtract A^2 FBS ; LHS=0
Elif B has a term to the power of 1/2 then
Subtract B FBS ; LHS=-B
Square BS ; LHS=B^2
Subtract B^2 FBS ; LHS=0
Elif C has a term to the power of 1/2 then
Subtract C FBS ; LHS=C
Square BS ; LHS=C^2
Subtract C^2 FBS ; LHS=0
Else
Stop the program
Now you could generalize for any number of terms while simplifying the code.
Assume the equation has the form { 0 = SUM }
and consider SUM as a list of terms.
Then you can loop over the terms until you find a square root:
EQUATION = { 0 = SUM }
For X in SUM:
if X has square root then
Subtract X FBS ; LHS=-X
Square BS ; LHS=X^2
Subtract X^2 FBS ; LHS=0
break
Else:
Stop the program
Note the break
statement. When you found the square root and do the corresponding operations, you have to simplify RHS and start over.
The Else block inside the for loop is executed when the loop finishes without break. This is as python works also.
So the overall program would be:
EQUATION = { 0 = SUM }
While True:
For X in SUM:
if X has square root then
Subtract X FBS ; LHS=-X
Square BS ; LHS=X^2
Subtract X^2 FBS ; LHS=0
break
Else:
Stop the program
# after break continue here
Simplify RHS
I suppose that the terms are of the form x^n * p(x)^0.5
where p(x)
is a polynomial in x
.
So for example a term would be x^3 * (x^2 x)^0.5
In order to do operations with this expressions you have to define how to represent the different parts of the equation.
An equation is a list of (2) sums
A sum is a list of terms.
A term is a list of (2) factors:
A polynomial factor
A square root of a polynomial
A polynomial is list of coefficients
In order to represent all of this you could use just plain lists or you could use objects derived from custom classes.
But in any of the two approaches the logic is the same.
Ill try to represent the equation 0 = (x 1)^0.5 x^2 4
I said an equation is a list of sums and a sum is a list of terms. So as a first approximations could do:
0 = A B C
eq = [ ['0'] , ['(x 1)^0.5' , 'x^2' , '4' ] ]
We have a list (equation) of two lists (sums) Now we have to represent the individual terms in terms of lists.
I said a term is a list of 2 factors: a polynomial and a square root of a polynomial.
The square root can be represented just as the polynomial inside it. If there is no square root, the polynomial is 1. (square root of 1 is 1, and multiplying by 1 does not change anything).
In order to improve readability I will write terms as tuples instead of lists. So:
0 = A B C
eq = [ [ ('0', '1') ], [('1' ,'x 1'), ('x^2', '1'), ('4', '1')] ]
I use the quotes to show that the elements inside the tuples (monomials and square roots) has yet to be converted to other representation. We don't know yet how to do that so I put it in quotes as a label.
In order to recover the original representation of a term we can use this formula:
Original_term = term[0] * term[1]^0.5
Now, a polynomial can be represented by its coeficients listed from C0
to CN
, where N is the degree of the polynomial and Cn is the coefficient which multiply x^n
. So for example:
x^3 3x^2 := [ 0, 0, 3, 1]
0 =: []
1 = [1]
x = [0 , 1]
Now, we can represent equations using just numbers, list and tuples.
Remember that a term is a tuple which first element is a polynomial and second term is a square root, represented as a polynomial.
Some examples of representations of terms:
'0' =: ([], [1])
'1' =: ([1], [1])
'x' =: ([0,1], [1])
'2x^3 (x^2 1)^0.5' =: ([0,0,0,2], [1, 0, 1])
Going back to the equation 0 = (x 1)^0.5 x^2 4
:
We have one term in LHS and 3 terms in RHS so the representation has the form:
eq = [ [()] , [(), (), ()] ]
Now fill the terms:
eq = [ [([], [1])] , [([1,0], [1,1]), ([1,0], [1]), ([4,0], [1])]
Yet the representation can be simplified: If there are no square root, then we could represent the term by a tuple of just one element. So, the pseudocode:
If A has a term to the power of 1/2 then
could be translated to python in:
if len(term) == 2:
Now in order to implement the algorithm we have to define functions to substract and multiply terms.
Note also that after you square the RHS, you have to simplify the result. That part could be tricky.