181 lines
5.1 KiB
Python
181 lines
5.1 KiB
Python
|
#!/usr/bin/env python
|
||
|
# encoding: utf-8
|
||
|
|
||
|
|
||
|
from .expression import Expression
|
||
|
from .polynom import Polynom
|
||
|
from .fraction import Fraction
|
||
|
|
||
|
from .random_expression import RdExpression
|
||
|
|
||
|
__all__ = ['Equation']
|
||
|
|
||
|
|
||
|
class Equation(object):
|
||
|
"""A calculus expression. Today it can andle only expression with numbers later it will be able to manipulate unknown"""
|
||
|
|
||
|
# @classmethod
|
||
|
# def random(self, form="", conditions=[], val_min=-10, val_max=10):
|
||
|
# """Create a random expression from form and with conditions
|
||
|
#
|
||
|
# :param form: the form of the expression (/!\ variables need to be in brackets {})
|
||
|
# :param conditions: condition on variables (/!\ variables need to be in brackets {})
|
||
|
# :param val_min: min value for generate variables
|
||
|
# :param val_max: max value for generate variables
|
||
|
#
|
||
|
# """
|
||
|
# random_generator = RdExpression(form, conditions)
|
||
|
# return Expression(random_generator(val_min, val_max))
|
||
|
#
|
||
|
|
||
|
def __init__(self, exp_str = "", left_poly = Expression([0]), right_poly = Expression([0])):
|
||
|
"""Create the equation
|
||
|
|
||
|
:param exp_str: the equality string which represent the equation
|
||
|
:param left_poly: the left polynom of the equation
|
||
|
:param right_poly: the right polynom of the equation
|
||
|
|
||
|
>>> e = Equation("2x+3 = 4x+5")
|
||
|
>>> e
|
||
|
< Equation [2 x + 3, 4 x + 5]>
|
||
|
>>> Pl = Polynom([1, 2])
|
||
|
>>> Pr = Polynom([3, 4])
|
||
|
>>> e = Equation(left_poly = Pl)
|
||
|
>>> e
|
||
|
< Equation [2 x + 1, 0]>
|
||
|
>>> e = Equation(right_poly = Pr)
|
||
|
>>> e
|
||
|
< Equation [0, 4 x + 3]>
|
||
|
>>> e = Equation(left_poly = Pl, right_poly = Pr)
|
||
|
>>> e
|
||
|
< Equation [2 x + 1, 4 x + 3]>
|
||
|
|
||
|
"""
|
||
|
if exp_str:
|
||
|
l_part, r_part = exp_str.split("=")
|
||
|
self.l_exp = Expression(l_part)
|
||
|
self.r_exp = Expression(r_part)
|
||
|
else:
|
||
|
self.l_exp = left_poly
|
||
|
self.r_exp = right_poly
|
||
|
|
||
|
self.smpl_each_part()
|
||
|
|
||
|
def smpl_each_part(self):
|
||
|
""" Simplify left and right part, transform them into polynom and stock them in smpl_*_exp
|
||
|
"""
|
||
|
self.smpl_l_exp = self.l_exp.simplify()
|
||
|
self.smpl_r_exp = self.r_exp.simplify()
|
||
|
|
||
|
try:
|
||
|
self.smpl_r_exp = self.smpl_l_exp.conv2poly(self.smpl_r_exp)
|
||
|
except AttributeError:
|
||
|
pass
|
||
|
|
||
|
try:
|
||
|
self.smpl_l_exp = self.smpl_r_exp.conv2poly(self.smpl_l_exp)
|
||
|
except AttributeError:
|
||
|
raise EquationError("None of left and right parts are polynoms. \
|
||
|
Can't use it to make an equation.")
|
||
|
|
||
|
# TODO: On pourrait rajouter des tests sur les inconnues |mar. mars 22 10:17:12 EAT 2016
|
||
|
|
||
|
def __str__(self):
|
||
|
return str(self.l_exp) + " = " + str(self.r_exp)
|
||
|
|
||
|
def __repr__(self):
|
||
|
return "< {cls} [{l}, {r}]>".format(
|
||
|
cls = str(self.__class__).split('.')[-1][:-2],
|
||
|
l = self.l_exp,
|
||
|
r = self.r_exp,
|
||
|
)
|
||
|
|
||
|
def solve(self):
|
||
|
"""Solve the equation but yielding each steps
|
||
|
|
||
|
"""
|
||
|
pass
|
||
|
|
||
|
def solution(self):
|
||
|
"""Return the solution of the equation
|
||
|
|
||
|
:returns: the solution
|
||
|
|
||
|
>>> e = Equation("2x + 1 = x + 2")
|
||
|
>>> e.solution()
|
||
|
1
|
||
|
>>> e = Equation("2x + 1 = 1")
|
||
|
>>> e.solution()
|
||
|
0
|
||
|
>>> e = Equation("1 = 2x + 1")
|
||
|
>>> e.solution()
|
||
|
0
|
||
|
>>> e = Equation("3x = 2x")
|
||
|
>>> e.solution()
|
||
|
0
|
||
|
>>> e = Equation("3x + 1 = 0")
|
||
|
>>> e.solution()
|
||
|
< Fraction -1 / 3>
|
||
|
>>> e = Equation("6x + 2 = 0")
|
||
|
>>> e.solution()
|
||
|
< Fraction -1 / 3>
|
||
|
|
||
|
"""
|
||
|
num = self.smpl_r_exp._coef[0] - self.smpl_l_exp._coef[0]
|
||
|
|
||
|
try:
|
||
|
denom = self.smpl_l_exp._coef[1]
|
||
|
except IndexError:
|
||
|
denom = 0
|
||
|
try:
|
||
|
denom -= self.smpl_r_exp._coef[1]
|
||
|
except IndexError:
|
||
|
pass
|
||
|
|
||
|
if denom == 0 and num == 0:
|
||
|
raise EquationError("All number are solution")
|
||
|
elif denom == 0:
|
||
|
raise NoSolutionError("This equation has no solution")
|
||
|
|
||
|
return Fraction(num, denom).simplify()
|
||
|
|
||
|
def is_solution(self, num):
|
||
|
""" Tell if a number is a solution.
|
||
|
|
||
|
:param num: the number to test
|
||
|
|
||
|
>>> e = Equation("2x + 1 = x + 2")
|
||
|
>>> e.is_solution(2)
|
||
|
False
|
||
|
>>> e.is_solution(1)
|
||
|
True
|
||
|
>>> e = Equation("3x = 2x")
|
||
|
>>> e.is_solution(1)
|
||
|
False
|
||
|
>>> e.is_solution(0)
|
||
|
True
|
||
|
>>> e = Equation("3x + 1 = 0")
|
||
|
>>> e.is_solution(0)
|
||
|
False
|
||
|
>>> e.is_solution(Fraction(-1, 3))
|
||
|
True
|
||
|
>>> e.is_solution(Fraction(-2, 6))
|
||
|
True
|
||
|
|
||
|
"""
|
||
|
l_part = self.smpl_l_exp.replace_letter(num).simplify()
|
||
|
r_part = self.smpl_r_exp.replace_letter(num).simplify()
|
||
|
return l_part == r_part
|
||
|
|
||
|
class EquationError(Exception):
|
||
|
pass
|
||
|
|
||
|
|
||
|
class NoSolutionError(EquationError):
|
||
|
pass
|
||
|
|
||
|
# -----------------------------
|
||
|
# Reglages pour 'vim'
|
||
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
||
|
# cursor: 16 del
|