コード例 #1
0
ファイル: equationsolver.py プロジェクト: Englebabz/CasPyTeX
	def movesolvenumstoleftside(self,solvesideinput,constantsideinput,solvenum,firstrun=True):
		"""
		Returns to new expressions that are just as equal as the one you put in.
		Should have put the solvenum on the solveside (the first index in the return value)

		"""
		solvenumstring=solvenum.num
		if solvesideinput.contains(solvenumstring) and not constantsideinput.contains(solvenumstring):
			return [solvesideinput,constantsideinput]
		elif constantsideinput.contains(solvenumstring) and not solvesideinput.contains(solvenumstring):
			return [constantsideinput,solvesideinput]
		elif not constantsideinput.contains(solvenumstring) and not solvesideinput.contains(solvenumstring):
			return [constantsideinput,solvesideinput]

		solveside=solvesideinput.simplify(solvenum)
		constantside=constantsideinput.simplify(solvenum)
		if constantside.type()=="addition": #
			newconstantsideaddends=[]
			newsolvesideaddends=[solveside]
			for addend in constantside.addends:
				if addend.contains(solvenumstring):
					newsolvesideaddends.append( ent.maybeclass([ent.number(["-1"]),addend],ent.product)   )
				else:
					newconstantsideaddends.append(addend)
			newsolveside=ent.addition(newsolvesideaddends)
			newconstantside=ent.addition(newconstantsideaddends)
			return self.movesolvenumstoleftside(newsolveside,newconstantside,solvenum,False)
		elif constantside.type()=="product":
			newsolvedividors=[]
			newconstantsidefacts=[]
			for factor in constantside.factors:
				if factor.contains(solvenumstring):
					newsolvedividors.append(factor)
				else:
					newconstantsidefacts.append(factor)
			newsolveside=ent.division([solveside,ent.maybeclass(newsolvedividors,ent.product)])
			newconstantside=ent.maybeclass(newconstantsidefacts,ent.product)
			return self.movesolvenumstoleftside(newsolveside,newconstantside,solvenum,False)
		elif constantside.type()=="division":
			num=constantside.numerator
			denom=constantside.denominator
			if num.contains(solvenumstring):
				newsolveside=ent.addition([solveside,ent.product([ent.number(["-1"]),constantside])])
				newconstantside=ent.number(["0"])
			elif denom.contains(solvenumstring):
				newsolveside=ent.product([denom,solveside])
				newconstantside=num
			return self.movesolvenumstoleftside(newsolveside,newconstantside,solvenum,False)
		elif constantside.type()=="potens":
			root=constantside.root
			exponent=constantside.exponent
			newsolveside=ent.addition([solveside,ent.product([ent.number(["-1"]),constantside])])
			newconstantside=ent.number(["0"])
			return self.movesolvenumstoleftside(newsolveside,newconstantside,solvenum,False)
		elif constantside.type() in ["number","sine","cosine","tangent","arcsine","arccosine","arctangent","natlogarithm","comlogarithm","squareroot"]:
			newsolveside=ent.addition([solveside,ent.product([ent.number(["-1"]),constantside])])
			newconstantside=ent.number(["0"])	
			return self.movesolvenumstoleftside(newsolveside,newconstantside,solvenum,False)
コード例 #2
0
ファイル: equationsolver.py プロジェクト: Englebabz/CasPyTeX
	def solveproduct(self,solveside,constantside,solvenum):
		"""
		Is a solving method
		Will move constantfactors to the other side
		"""
		newsolvefactors=[]
		newconstdividors=[]
		for factor in solveside.factors:
			if factor.contains(solvenum.num):
				newsolvefactors.append(factor)
				
			else:
				newconstdividors.append(factor)
		returnsolveside=ent.maybeclass(newsolvefactors,ent.product)
		returnconstant=ent.division([constantside,ent.maybeclass(newconstdividors,ent.product)])
		if len(newsolvefactors)!=1:
			return None
		return [[returnsolveside,returnconstant]]
コード例 #3
0
ファイル: equationsolver.py プロジェクト: Englebabz/CasPyTeX
	def solveaddition(self,solveside,constantside,solvenum):
		"""
		The solving method if the solveside is an instance of addition
		will try to put terms on the constantside such that there's 
		only one term with solvenum in it.
		If that fails, it will try to solve it as a polynomial (but
			that function is not finished)
		"""
		newaddends=[]
		newconstandsideaddends=[constantside]
		for addend in solveside.addends:
			if addend.contains(solvenum.num):
				newaddends.append(addend)
			else:
				newconstandsideaddends.append(ent.product([ent.number(["-1"]),addend]))
		returnsolveside=ent.maybeclass(newaddends,ent.addition)
		returnconstantside=ent.maybeclass(newconstandsideaddends,ent.addition)
		if len(newaddends)!=1:
			degrees=False
			ispol=True
			poladdends=[[ent.product([ent.number(["-1"]),returnconstantside]).simplify(solvenum),0]]# [coeff,potens]
			subvalue=False #the part that contains x
			for addend in newaddends:
				coeffadd=False
				if addend.type()=="product":
					for index,fact in enumerate(addend.factors):
						if fact.contains(solvenum.num):
							if coeffadd==False:
								if fact.type()=="potens":
									if fact.exponent.evaluable(True) and eval(fact.exponent.tostring().replace("^","**"))%1==0 and (subvalue==False or fact.root==subvalue) and coeffadd==False:
										subvalue=fact.root
										coeffadd=[addend.delfactor(index),int(eval(fact.exponent.tostring().replace("^","**")))]
									else:
										ispol=False
										break
								elif fact.type()=="product": #damned negative number
									if fact.factors[1].type()=="potens":
										exponent=fact.factors[1].exponent
										if exponent.evaluable(True) and eval(exponent.tostring().replace("^","**"))%1==0 and (subvalue==False or fact1.factors[1].root==subvalue) and coeffadd==False:
											subvalue=fact.factors[1].root
											coeffadd=[ent.product([ent.number(["-1"]),addend.delfactor(index)]),int(eval(exponent.tostring().replace("^","**")))]
										else:
											ispol=False
											break
									else:
										thisval=fact.factors[1]
										if coeffadd==False and (subvalue==False or thisval==subvalue):
											subvalue=thisval
											coeffadd[ent.product([ent.number(["-1"]),addend.delfactor(index)]),1]
										else:
											ispol=False
											break
								else:
									if coeffadd==False and (subvalue==False or subvalue==fact):			
										subvalue=fact
										coeffadd=[addend.delfactor(index),1]
									else:
										ispol=False
										break
							else:
								ispol=False
								break
				elif addend.type()=="potens":
					if coeffadd==False and (addend.exponent.evaluable(True) and eval(addend.exponent.tostring().replace("^","**"))%1==0 and (subvalue==False or addend.root==subvalue)):
						subvalue=addend.root
						coeffadd=[ent.number(["1"]),int(eval(addend.exponent.tostring().replace("^","**")))]
					else:
						ispol=False
						break
				else:
					if coeffadd==False and (subvalue==False or addend==subvalue):
						subvalue=addend
						coeffadd=[ent.number(["1"]),1]

					else:
						ispol=False
						break
				if coeffadd!=False:
					poladdends.append(coeffadd)
			if subvalue==False or not ispol:
				return None
			polsolveresult=self.polsolve(poladdends)
			retvar=[]
			for solution in polsolveresult:
				retvar.append([subvalue,solution])
			return retvar
		return [[returnsolveside,returnconstantside]]