Differences between revisions 4 and 5
Revision 4 as of 2007-02-20 22:05:00
Size: 7211
Editor: anonymous
Revision 5 as of 2007-02-20 22:07:27
Size: 7437
Editor: anonymous
Deletions are marked like this. Additions are marked like this.
Line 44: Line 44:
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)
Line 46: Line 50:
solve_system_of_two_linear_equations_in_general_form(equation1,equation2) print solve_system_of_two_linear_equations_in_general_form(equation1,equation2)
Line 48: Line 52:
(0x + 5y == -5, 'x == 2,y == -1')

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, [http://code.google.com/p/sympy/ 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 [http://sage.math.washington.edu/home/moretti/days3/talks/calculus.pdf 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:

  • (equation1) 2*x + 3*y = 1
  • (equation2) -x + y = -3

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

  • me: Your symbol system does not use a dictionary.
  • Ondrej: no
  • me: I want to make it easy to deal ith both sides
  • Ondrej: with boths sides of what
  • me: equation
  • me: I want the equation to be very modular. Made up of terms, made up of coefficient, variables, and degrees with variables.
  • me: Why do not you not support equations in Sympy?
  • Ondrej: because I didn't yet have time to write it

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.

  • me: Yeah

Are your equations going to be implemented using strings?

  • Ondrej: strings? no. just as sympy expressions.
  • me: So how would I in the future define an equation in Sympy and assign say equation1 to it?
  • Ondrej: there are many ways to do that. the simplest is to express every eq. in the from something=0
  • me: a=x+1=0 nice one
  • Ondrej: so, you would use equation1=3*x**2-x-5

right that's it.

  • me: no thats an expression
  • Ondrej: every expression is an equation as well

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

  • me: Nah I'm interested in creating an equation data type
  • Ondrej: do what you want. I was just telling that you don't have to reinvent everything again.
  • me: You don't have a bunch of blocks of the components of expressions than the components of equations. I want it to be very easy to say equation1.left_side etc and identify all kinds of things.
  • Ondrej: right. just create a class equation

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

  • me:

        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)}
  • Ondrej: yes

that's how ginac is doing it

  • me: In that system I would have no idea what 'a','b','x','y' were

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

  • 'x' and 'y' would be independent variables
  • Ondrej: in ginac you just use the lsolve(eq,vars) function

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.

  • me: I'm not interested in not being able to cheat on math homework with say Mathematica which only solves an equation.
  • Ondrej: I don't know what you want - but it seems to me you just need to implement lsolve(). either to show the steps in the solving process, or not.