示例#1
0
def solveLinear (poly): # solving via -b/m
  m = Tree(poly.coeffs[1])
  b = Tree(poly.coeffs[0])
  answer = Tree("=", OP, None, None)
  answer.left = Tree(Polynomial(poly.var))
  temp = Tree("-", OP, 0, b)
  answer.right = Tree("/", OP, temp, m)
  if INTERMEDIATE: print str(answer), "\t(Solving linear equation)"
  answer.right = evalTree(answer.right)
  return answer
示例#2
0
def solveQuad(poly): # Solve via quadratic formula
  a = Tree(poly.coeffs[2])
  b = Tree(poly.coeffs[1])
  c = Tree(poly.coeffs[0])
  discriminant = Tree(
    "sqrt",
    FUNCTION, 
    Tree("-", OP,
       Tree( "**", OP, b, 2),
      Tree("*", OP, 
        4, 
        Tree( "*", OP, a, c)),
	 ),
      None, 
  )
  answerLeft = Tree(Polynomial(poly.var))
  answer1 = Tree( "=", OP , answerLeft, None)  #may need Copy here
  answer1.right = Tree("/", OP,
    Tree( "+",  OP,
      	Tree( "-", OP, 0, b ), 
      discriminant,
		),
    Tree("*", OP, 2, a )
 )
  
  if INTERMEDIATE: print str(answer1), "\t(Solving quadratic equation; root 1)"
  answer1.right = evalTree(answer1.right)

  answer2 = Tree("=", OP, answerLeft, None)
  answer2.left = Tree(Polynomial(poly.var))
  answer2.right = Tree("/", OP,
    Tree( "-",  OP,
      	Tree( "-", OP, 0, b ), 
      discriminant,
		),
    Tree("*", OP, 2, a))
  
  if INTERMEDIATE: print str(answer2), "\t(Solving quadratic equation; root 2)"
  
  answer2.right = evalTree(answer2.right)
  return [answer1, answer2]