Tuesday, 24 November 2009

The Unified Representation of Simple Mathematics

Someone has told me the following words of wisdom: "Use inheritance only where it makes sense to do so". When it comes to mathematical problems, we think it does make sense to use inheritance, and so we have a base class called somewhat surprisingly "the exercise class" that contains some properties common to all exercises/problems. One such subcategory of problems are textual problems, which I am currently working to implement. These will consist of: the problem text containing variable names, a formula that is used to compute the answer, a list of variables that will take bounded random values, and an answer comment string, so that the pupils can learn something too, which is quite important!
While working on this I realised that quite a bit more can be moved out into our basic math representation; A variable taking on a bounded random value for instance, is something that we will have a use for in many problems. Also, I needed a simple mathematical expressions parser so the computer can calculate the problem result. This would not strictly be necessary, but is done in this way because then it will be very easy for a teacher to create new text problems that the system can use, simply by entering a problem string with variable names isntead of values, entering a formual such as (x1+2*x2)/2, specifying variable bounds and also supply a nice solution comment string.
This was an excellent reason to visit one of my favourite branches of computer science, namely the theory of formal languages. Simple mathematical expressions are ... simple, and can be parsed by a push-down automaton, like many programming languages. In my current implementation I lex and parse expressions involving addition, subtraction, multiplication, division, and parenthesis, by the following grammar (Backus Naur Form):
Expr ::= Term | Expr "+" Term | Expr "-" Term
Term ::= Atom | Term "*" Atom | Term "/" Atom
Atom ::= int | "(" Expr ")"

Special consideration must be taken for the case of division, something which I am working on right now.

No comments:

Post a Comment