- 論壇徽章:
- 0
|
不需要化成一般形式,也不需要在系數(shù)和字母之間加入*號,只需要輸入未知數(shù)和方程即可,已經(jīng)打包,代碼也在包里
解方程.zip
(2.06 MB, 下載次數(shù): 947)
2013-09-14 13:24 上傳
點(diǎn)擊文件名下載附件
一下是核心代碼(已經(jīng)先通過另一個(gè)函數(shù)化成PYTHON的表達(dá)式才用這個(gè)計(jì)算)
一元一次方程:- def solve(eq,var='x'): #利用虛數(shù)型解方程
- eq1 = eq.replace("=","-(")+")"
- c = eval(eq1,{var:1j})
- return -c.real/c.imag
復(fù)制代碼 二元一次方程:- def solve(eqa,eqb,x,y): #利用虛數(shù)型解方程
- #兩方程整理后分別為ax+by+c=0,dx+ey+f=0 把用來帶入方程2的x值放在put,x的終值為root1,y為root2
- eq1 = eqa.replace("=","-(")+")" #方程1移項(xiàng)
- #化簡方程1
- eq1=cheaker1(x,y,eq1)
- a = (eval(eq1,{x:1j,y:0})).imag
- b = (eval(eq1,{x:0,y:1j})).imag
- c = (eval(eq1,{x:0,y:1j})).real
- #帶入準(zhǔn)備
- put = "(-("+str(b)+"y+"+str(c)+")/"+str(a)+")"
- eq2 = eqb.replace("=","-(")+")" #方程2移項(xiàng)
- #將x帶入
- two = eq2.replace(x,put)
- #整理y
- two=cheaker1(x,y,two)
- #求y
- two1 = eval(two,{y:1j})
- root2=(-two1.real/two1.imag)
- #將y帶入,求x
- one = eq1.replace(y,str(root2))
- one1 = eval(one,{x:1j})
- root1=(-one1.real/one1.imag)
-
- return (root1,root2)
復(fù)制代碼 一元二次方程:- def solve(eqa,x): #利用虛數(shù)型解方程
- #方程整理后為ax^2+bx+c=0
-
- eq1 = eqa.replace("=","-(")+")" #方程移項(xiàng)
- #化簡方程1
- c = eval(eq1,{(str(x)+"**2"):0,x:0})
- b =(eval(eq1,{(x+"**2"):0,x:1j})).imag
- a1=c-(eval(eq1,{(x+"**2"):0,x:1j})).real
- a =(eval(eq1,{(x+"**2"):1j,x:0})).imag+a1
- #代入公式
- d=b**2-4*a*c
- if d<=0 : return (None,None) #無實(shí)數(shù)解
- root1=(-b+sqrt(d))/(2*a)
- root2=(-b-sqrt(d))/(2*a)
-
- return (root1,root2)
復(fù)制代碼 |
|