目標:我想將以下操作序列轉換為 Python:
- 隔離 LHS 上的平方根項。
- 等式兩邊平方。
- 相應地添加和減去項以在 LHS 上獲得 0。
- 在 LHS 上隔離另一個平方根項。[即步驟1)]
- 重復步驟 2)。
- 重復步驟 3)。
- 重復步驟 1)。
等等...
消除過激方程像這一個,并讓他們到像表單這樣即多項式P(X)與x的整數次冪。
我的嘗試:我曾嘗試為此撰寫一個大綱,但我不知道如何為 Python 將其形式化。這是我的作業:
符號:
- LHS:左側(等式的)。
- TBS:兩邊(等式)。
- FBS:從兩邊(等式)。
- BS:(等式的)兩邊。
- 分號用于表示每個階段所需的 LHS 值。
- “X 有一個 1/2 次方的項”我的意思是等式 X 的元素具有提高到那個分數次方的系數/變數。
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
uj5u.com熱心網友回復:
條款的符號,A,B,C不被考慮。在任何情況下,您都必須減去該術語才能使該術語從RHS.
因此,在第一步中,您總是-X在LHS(你放LHS=X,但我想是一個錯誤)
然后,你方雙方并獲得X^2在LHS。最后一個Elif可以僅替換為Else. 所以這是稍微簡化的代碼
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
現在您可以在簡化代碼的同時概括任意數量的術語。假設方程具有以下形式,{ 0 = SUM }并將 SUM 視為項串列。
然后你可以遍歷這些項,直到找到一個平方根:
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
注意break宣告。當你找到平方根并做相應的運算時,你必須簡化RHS并重新開始。
當回圈結束且沒有中斷時執行 for 回圈內的 Else 塊。這也是python的作業原理。
所以整個程式將是:
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
我想這些項的形式是x^n * p(x)^0.5where p(x)is a polynomial in x。
例如,一個術語是 x^3 * (x^2 x)^0.5
為了使用這個運算式進行運算,您必須定義如何表示方程的不同部分。
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
為了表示所有這些,您可以只使用普通串列,也可以使用從自定義類派生的物件。
但是在這兩種方法中的任何一種中,邏輯都是相同的。
我會嘗試表示方程 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.
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/321835.html
下一篇:為兩個單獨的排序函式輸出相同的值
