AUTHOR: Timothy Clemans, [email protected]

Support for elementary mathematics in SAGE is currently very limited. I have done little bit of work on supporting equations for Python. I want to create a pure Python library of classes around expressions and equations. This way it could be included in SAGE, [ Sympy], and a standalone web server package just for elementary mathematics education.

The lead implementer of basic symbolic computation in SAGE is Bobby Moretti. He will be giving a short [ talk] on work in this area at SAGE Days 3. Neither SAGE nor Sympy support equations.

I started trying to support equations in SAGE using strings. That is a very bad idea and string input should be parsed at the interface level. Personally I think a Python Elementary Algebra package should be composed of several hundred classes. Examples of these classes could be variable, independent variable, dependent variable, coefficient, term, expression, equation, linear equation, equation_of_line, equation_of_line_in_slope_intercept_form, or quadratic equation class.

For example lets say you have the following system of linear equations that you want to solve using elimination:

class line_equation_in_general_form(object):
   def __init__(self,A,B,C):
       self.A = A
       self.B = B
       self.C = C

   def __str__(self):
       return '%dx + %dy == %d' % (self.A,self.B,self.C)

   __repr__ = __str__

   def __add__(self,other):
       return self.__class__(self.A+other.A,self.B+other.B,self.C+other.C)

   def __mul__(self,n):
       return self.__class__(self.A*n,self.B*n,self.C*n)

equation1 = line_equation_in_general_form(2,3,1)
equation2 = line_equation_in_general_form(-1,1,-3)
equation1 + equation2*2

0x + 5y == -5

def solve_system_of_two_linear_equations_in_general_form(equation1,equation2):
   equation3 = equation1*(lcm(equation1.A,equation2.A)/equation1.A)+equation2*-(lcm(equation1.A,equation2.A)/equation2.A)
   return equation3, "x == %d,y == %d" % ((equation1.C-equation1.B*(equation3.C/equation3.B))/equation1.A,(equation3.C/equation3.B))

equation1 = line_equation_in_general_form(2,3,1)
equation2 = line_equation_in_general_form(-1,1,-3)
print solve_system_of_two_linear_equations_in_general_form(equation1,equation2)

equation1 = line_equation_in_general_form(4,3,10)
equation2 = line_equation_in_general_form(2,1,4)
print solve_system_of_two_linear_equations_in_general_form(equation1,equation2)

(0x + 5y == -5, 'x == 2,y == -1') (0x + 1y == 2, 'x == 1,y == 2')

Another example is about building terms, which could lead to expressions and in turn equations.

class Term(object):
    def __init__(self,coefficient,variable_degree_dictionary):
        self.coefficient = coefficient
        self.vdd_input = variable_degree_dictionary
        self.variable_degree_pairs = []
        for each_variable_key in sorted(self.vdd_input):
            if self.vdd_input[each_variable_key] != 0:

    def __str__(self):
        string = str(self.coefficient)
        for i in range(len(self.variable_degree_pairs)):
            if self.variable_degree_pairs[i][1] == 1:
                string += '*%s' % (str(self.variable_degree_pairs[i][0]))
                string += '*%s**%s' % (str(self.variable_degree_pairs[i][0]),str(self.variable_degree_pairs[i][1]))
        return string

    __repr__ = __str__   

    def __add__(self,other):
        if self.variable_degree_pairs == other.variable_degree_pairs:
            if type(self.coefficient) == int or type(self.coefficient) == float:
                if type(other.coefficient) == int or type(other.coefficient) == float:
                    return Term(self.coefficient + other.coefficient,self.vdd_input)

term1 = Term(3,{'y':3,'x':1}); term1


term2 = Term(7,{'x':1,'y':3}); term2


term1 + term2


So I'm trying to build a Python elementary mathematics package not a SAGE package. I will probably look at supporting classification of numbers by natural, integer, rational, real, and complex in a subset setup allowing the teacher to restrict her students by grade level.

Chat with Ondrej Certik, author of Sympy

but the support you are talking about is already there you just create the left hand side as an expression and the right hand side and that's all. then you just multiply LHS and RHS with a number, or add something to both sides etc.

Are your equations going to be implemented using strings?

right that's it.

no you need to write code to solve that. no->now

and implement some methods. that's the way to do that but I would suggest you to check ginac, giac and other projects to see how they did it. so that you can just copy the ideas. Sent at 1:36 AM on Tuesday

        symbol a("a"), b("b"), x("x"), y("y");
        lst eqns, vars;
        eqns = a*x+b*y==3, x-y==b;
        vars = x, y;
        cout << lsolve(eqns, vars) << endl;
         // -> {x=[equal sad]3+b^2)/(b+a),y=[equal sad]3-b*a)/(b+a)}

that's how ginac is doing it

In my system 'a' and 'b' would be coefficients

where eq is the equation but in sympy it could be just expression (equal to 0) and vars are the independent variables all the rest are coefficients what remains to be done is to implement the function lsolve.