1 #-*- coding: cp936 -*-
2 '''
3 使用单纯形法时,采用现在的这种头文件,不能出现from __future__ import division4 使用计算器或画图时,务必把from __future__ import division 前面的#拿掉5 如果不想用复数计算,可以载入math,屏蔽cmath,此时plot里面的sin()不用写出m.sin()6 如果要复数计算,和前一行所说的相反7 屏蔽头文件的方法,加#;载入,则取消#8 ''' #calculator plot SM
9 from __future__ import division #1 1 0
10 from math import* #0(推荐) 1 x
11 #from cmath import* # 1(推荐) 0 x
12
13 from graphics import *
14 from math importfabs,ceil,floor15 importmath as m16 from random importrandom17 from copy importdeepcopy18 from time importsleep19
20
21 defstat(ilist):22 ave=023 s=024 s2=025 n=len(ilist)26 for i inilist:27 ave=ave+i/n28 for i inilist:29 s2=(i-ave)**2/(n-1)30 s=sqrt(s2)31 return 'ave='+str(ave)+'\n'+'s='+str(s)32
33
34 '''________________________________class Rational___________________________'''
35 classRational:36 def __init__(self,num,den):37 if num%1!=0 or den%1!=0:38 exit(1)39 self.num=num40 self.den=den41 self.__RF()42 def __RF(self):43 if self.num==0:44 self.den=1
45 temp=min(abs(self.num),abs(self.den))46 while temp>1:47 if self.num%temp==0 and self.den%temp==0:48 self.num=self.num/temp49 self.den=self.den/temp50 temp=temp-1
51 if self.den<0:52 self.num=-self.num53 self.den=-self.den54 defshow(self):55 if self.den!=1:56 return str(self.num)+'/'+str(self.den)57 else:58 returnstr(self.num)59 def __add__(self,other):60 return Rational(self.num*other.den+self.den*other.num,self.den*other.den)61 def __sub__(self,other):62 return Rational(self.num*other.den-self.den*other.num,self.den*other.den)63 def __mul__(self,other):64 return Rational(self.num*other.num,self.den*other.den)65 def __div__(self,other):66 if other==Rational(0,1):67 exit(1)68 else:69 return Rational(self.num*other.den,self.den*other.num)70 def __pow__(self,n):71 if n%1==0:72 return Rational(self.num**n,self.den**n)73 else:74 print "Number Error"
75 def floatify(self): #for compare
76 return 1.0*self.num/self.den77 def __gt__(self,other):78 return self.floatify() >other.floatify()79 def __lt__(self,other):80 return self.floatify() =other.floatify()83 def __le__(self,other):84 return self.floatify() <=other.floatify()85 def __eq__(self,other):86 return self.floatify() ==other.floatify()87 def __ne__(self,other):88 return not self ==other89 def __iaddr__(self,other):90 self=self+other91 def __isubr__(self,other):92 self=self-other93 def __imulr__(self,other):94 self=self*other95 def __idivr__(self,other):96 self=self/other97 def __ipowr__(self,other):98 self=self**other99 '''_________________________________class iMatrix___________________________'''
100 classiMatrix:101 def __init__(self):102 self.win=GraphWin('simplex method',1095,410) #1095 410
103 self.win.setBackground('#8497b0')104 self.win.setCoords(0,0,61,28)105 self.__createEntrys()106 self.__createButton()107 self.__createList()108 self.__createInfoBar()109 self.__createInfoEntrys()110 self.t=Text(Point(28,26),'')111 self.t.setTextColor('red')112 self.t.setSize(20)113 def __createInfoEntrys(self):114 self.InfoEntrys=[]115 for i in range(1,11,1):116 self.InfoEntrys.append(Entry(Point(-2+5*i,23.5),2))117 for i in range(10):118 self.InfoEntrys[i].draw(self.win)119 def __createInfoBar(self):120 self.InfoBox=Rectangle(Point(1,25),Point(5,27))121 self.InfoBox.setFill('red')122 self.InfoBox.draw(self.win)123 self.InfoBoxText=Text(Point(3,26),'Info')124 self.InfoBoxText.draw(self.win)125 self.InfoBar=Rectangle(Point(6,25),Point(50,27))126 self.InfoBar.setFill('white')127 self.Info=Text(Point(28,26),'Information')128 self.Info.setTextColor('#d0cece')129 self.Info.setSize(25)130 self.InfoBar.draw(self.win)131 self.Info.draw(self.win)132 def __createEntrys(self):133 self.entrys=[]134 for i in range(11):135 self.entrys.append([])136 for j in range(11):137 self.entrys[i].append(Entry(Point(3+5*j,1.5+2*i),8))138 for i in range(10):139 self.entrys[0][i].setTextColor('red')140 for i in range(10):141 self.entrys[i+1][10].setTextColor('purple')142 self.entrys[0][10].setTextColor('blue')143 for i in range(11):144 for j in range(11):145 self.entrys[i][j].setText('0')146 self.entrys[i][j].draw(self.win)147 def __createButton(self):148 self.button=Rectangle(Point(56,1),Point(60,22))149 self.button.setFill('green')150 self.button.draw(self.win)151 def __createList(self):152 self.List=[]153 for i in range(11):154 self.List.append([])155 for j in range(11):156 self.List[i].append(0)157 defgetInfo(self):158 self.InfoList=[]159 for i in range(10):160 if self.InfoEntrys[i].getText()!='':161 self.InfoList.append(eval(self.InfoEntrys[i].getText()))162 else:163 self.InfoList.append(0)164 defprocessInfo(self):165 for i in range(10):166 if self.InfoList[i]>0:167 if self.List[0][i]!=Rational(0,1):168 n_1=0169 n_0=0170 for j in range(1,self.row+1,1):171 if self.List[j][i]==Rational(0,1):172 n_0=n_0+1
173 if self.List[j][i]==Rational(1,1):174 n_1=n_1+1
175 if n_1==1 and n_0==self.row-1:176 self.t.setText('No solution!')177 self.t.draw(self.win)178 sleep(2)179 self.t.undraw(win)180 break
181 flag=0182 for i in range(10):183 if self.List[0][i]==Rational(0,1) and (not(i inself.BlankColumn)):184 n_1=0185 n_0=0186 for j in range(1,self.row+1,1):187 if self.List[j][i]==Rational(0,1):188 n_0=n_0+1
189 if self.List[j][i]==Rational(1,1):190 n_1=n_1+1
191 if n_1!=1 or n_0!=self.row-1:192 self.t.setText('Infinity solutions!')193 self.t.draw(self.win)194 sleep(2)195 self.t.undraw()196 flag=1
197 break
198 if flag==0:199 self.t.setText('One solution')200 self.t.draw(self.win)201 sleep(2)202 self.t.undraw()203 defgetNum(self):204 for i in range(11):205 for j in range(11):206 temp=self.entrys[i][j].getText()207 if temp=='':208 self.List[i][j]=Rational(0,1)209 elif '/' intemp:210 n=temp.index('/')211 a=eval(temp[:n])212 b=eval(temp[n+1:])213 self.List[i][j]=Rational(a,b)214 else:215 self.List[i][j]=Rational(eval(temp),1)216 self.row=10
217 for i in range(10,0,-1):218 if max(self.List[i])==Rational(0,1) and min(self.List[i])==Rational(0,1):219 self.row=i-1
220 else:221 break
222 if self.row==0:223 exit(1)224 self.BlankColumn=[]225 for j in range(10):226 flag=0227 for i in range(11):228 if self.List[i][j]!=Rational(0,1):229 flag=flag+1
230 if flag==0:231 self.BlankColumn.append(j)232 #self.column=len(self.BlankColumn)
233 defprintList(self):234 for i in range(self.row,-1,-1):235 print '\n'
236 for j in range(11):237 if not (j inself.BlankColumn):238 print '%8s' %(self.List[i][j].show()),239
240 defwhether_0(self):241 temp=max(self.List[0][0:10])242 self.centerX=self.List[0].index(temp)243 return(temp)244 deffindXY(self,delta):245 self.centerX=self.List[0].index(delta)246 for i in range(1,self.row+1,1):247 if self.List[i][self.centerX]>Rational(0,1):248 self.centerY=i249 temp1=self.List[i][10]/self.List[i][self.centerX]250 break
251 for i in range(1,self.row+1,1):252 if self.List[i][self.centerX]>Rational(0,1):253 temp2=self.List[i][10]/self.List[i][self.centerX]254 if temp2
<<<<