Introduction to MathObjects

Perl has very limited data types: scalars (i.e., numbers and strings), and arrays and hashes of scalars. To Perl, a string like "sin(x^2+6)" is just a string, not a function. MathObjects were created by Davide Cervone at Union College to address this and other problems. MathObjects are a set of formal objects introduced to make manipulation of mathematical objects in WeBWorK problems more intuitive. For instance, Formula("sin(x^2+6)") is a MathObject that takes the Perl string "sin(x^2+6)" and makes it a function that can be evaluated, differentiated, can produce a TeX representation, can produce a Perl function (i.e., a subroutine that returns the value of this function), etc.

MathObjects are useful and powerful because they are much more than just a number or a string --- they know what context they live in, mathematical operations can be done with them, and they have methods defined on them.

  1. MathObjects know what context they live in:
    Context("Numeric");
    $f = Formula("sin(x^2/3)");
    $a = Real("sqrt(pi/2)");
    
    Context("Vector");
    $v = Vector("<1,2,3>");
    
    Context("Inequalities");
    $domain = Inequality("-16 < x < 9");
    
    In fact, we could replace Formula, Real, Vector, and Inequality all by Compute, in which case MathObjects would discern the type of object from the context. Compute is usually the preferred way to convert a string to a MathObject. It preserves the original string and uses it to display the correct answer expected of the student in the most useful form. The angle brackets in the vector and the inequality are interpreted differently (and correctly) because these MathObjects know which context they live in.

  2. Mathematical operations can be done with MathObjects:
    Context("Numeric");
    $f = Compute("sin(x^2/3)");
    $a = Compute("sqrt(pi/2)");
    $g = $a * $f;
    
    Note that $g is a MathObject, as it is the product of two MathObjects that live in the same context. Mathematical operations cannot be done with MathObjects that live in different contexts.

  3. MathObjects have methods defined on them:
    Context("Numeric");
    $f = Compute("sin(x^2/3)");
    $a = Compute("sqrt(pi/2)");
    
    # evaluate f(a)
    $b = $f->eval(x=>$a);
    
    # partial derivative df/dx
    $fx = $f->D('x');
    
    # basic algebraic simplification to x+4
    $c = -4;
    $g = Compute("x - $c")->reduce();
    
    # get a string of TeX code for f
    # returns "\sin\!\left(\frac{x^{2}}{3}\right)"
    $h = $f->TeX();
    
    We have called the evaluate ->eval(), partial differentiation ->D('x'), very basic algebraic simplification ->reduce(), and TeX string ->TeX() methods on MathObjects.

Why Use MathObjects?

  1. MathObjects convert ordinary strings into a rich variety of data types such as points, vectors, matrices, real numbers, complex numbers, inequalities, etc. (Before MathObjects, Perl strings were the best game in town.)
  2. You can use a single MathObject, such as a formula, to produce numeric values, other formulas, TeX output, and answer strings. This avoids having to type the function a multitude of different ways, which would make maintaining the problem harder. (Before MathObjects, you often had to produce each of these separately and manually.)
  3. The answer checkers for MathObjects are better at giving students feedback on syntax errors and are more versatile. (Before MathObjects, very little feedback was given.)
  4. The basic syntax $f->cmp() for comparing a student's answer to the correct answer $f is the same no matter what type of MathObject $f is. (Before MathObjects, you had to know the name of the answer checker you wanted to apply to a Perl string.)

Online References