Difference between revisions of "Introduction to MathObjects"

From WeBWorK_wiki
Jump to navigation Jump to search
Line 3: Line 3:
 
== Why use MathObjects? ==
 
== Why use MathObjects? ==
   
MathObjects are designed to be used in two ways. First, you can use it within your perl code when writing problems as a means of making it easier to handle formulas, and in particular,
+
MathObjects are designed to be used in two ways. First, you can use it within your perl code
  +
when writing problems as a means of making it easier to handle formulas, and in particular,
 
to be able to use a single object to produce numeric values, TeX output and answer strings from a single
 
to be able to use a single object to produce numeric values, TeX output and answer strings from a single
 
formula entry. This avoids having to type a function three different ways
 
formula entry. This avoids having to type a function three different ways
Line 12: Line 12:
   
 
Checkers are available for each of the types of values that the parser can produce (numbers, complex numbers, infinities, points, vectors, intervals, unions, formulas, lists of numbers, lists of points, lists of intervals, lists of formulas returning numbers, lists of formulas returning points, and so on). They are typically invoked using the notation <code>$mathObject->cmp()</code>
 
Checkers are available for each of the types of values that the parser can produce (numbers, complex numbers, infinities, points, vectors, intervals, unions, formulas, lists of numbers, lists of points, lists of intervals, lists of formulas returning numbers, lists of formulas returning points, and so on). They are typically invoked using the notation <code>$mathObject->cmp()</code>
  +
  +
MathObjects are useful (and powerful) because they "know" information about themselves:
  +
thus, we can add formulas to get new formulas, plug real objects
  +
into formulas to get formulas evaluated at those values, and calculate derivatives of formulas.
   
 
== How to create a MathObject ==
 
== How to create a MathObject ==
   
$a = Real(3.5);
+
$a = Real(3.5); or
$b = Complex(3, 4);
+
$a = Compute("3.5");
$b = Complex("3 +4i");
+
  +
$b = Complex(3, 4); or
  +
$b = Complex("3 +4i"); or
  +
$b = Compute("3+4i");
  +
  +
$v = Vector(4,5,8);
  +
$v = Compute("&lt;4,5,8&gt;");
  +
  +
$f = Formula("sin(x^2) + 6");
   
 
<code>$a</code> represents a real number 3.5 and <code>$b</code> (defined by either method) represents a complex number.
 
<code>$a</code> represents a real number 3.5 and <code>$b</code> (defined by either method) represents a complex number.
  +
In general using the <code>Compute</code> variant is preferred because the input string also serves as a model for the
  +
correct answer expected from the student.
   
 
== Which MathObject types (classes) can be created? ==
 
== Which MathObject types (classes) can be created? ==
Line 46: Line 52:
 
* Union (of intervals and sets): <code>$U = Union(""I U J");</code> (I union J)
 
* Union (of intervals and sets): <code>$U = Union(""I U J");</code> (I union J)
   
  +
=== The <code>Compute</code> command ===
  +
* You can use the <code>Compute</code> command to create any of the objects above from a string
  +
Compute("3,7,3+2i")
  +
Compute( "[[1,0],[0,1]]" );
 
=== The String type ===
 
=== The String type ===
   

Revision as of 12:22, 13 June 2008

MathObjects are programing objects which behave much as you would expect their true mathematical counterparts to behave. For example a+b means one thing if a and b are vectors or matrices, another if they are real numbers and a third if a and b are complex numbers. Likewise multiplication: a b or a*b mean different things depending on the mathematical object (although of course all of the versions of multiplication have certain similarities.)

Why use MathObjects?

MathObjects are designed to be used in two ways. First, you can use it within your perl code when writing problems as a means of making it easier to handle formulas, and in particular, to be able to use a single object to produce numeric values, TeX output and answer strings from a single formula entry. This avoids having to type a function three different ways (which makes maintaining a problem much harder). Since MathObjects also included vector and complex arthimetic, it is easier to work with these types of values as well.

Secondly using MathObjects improves the processing of student input. This is accomplished through special answer checkers that are part of the Parser package (rather than the traditional WeBWorK answer checkers). Each of these checkers has error checking customized to the type of input expected from the student and can provide helpful feedback if the syntax of the student's entry is incorrect.

Checkers are available for each of the types of values that the parser can produce (numbers, complex numbers, infinities, points, vectors, intervals, unions, formulas, lists of numbers, lists of points, lists of intervals, lists of formulas returning numbers, lists of formulas returning points, and so on). They are typically invoked using the notation $mathObject->cmp()

MathObjects are useful (and powerful) because they "know" information about themselves: thus, we can add formulas to get new formulas, plug real objects into formulas to get formulas evaluated at those values, and calculate derivatives of formulas.

How to create a MathObject

$a = Real(3.5);   or
$a = Compute("3.5");
$b = Complex(3, 4); or
$b = Complex("3 +4i"); or 
$b = Compute("3+4i");
$v = Vector(4,5,8);
$v = Compute("<4,5,8>");
$f = Formula("sin(x^2) + 6");

$a represents a real number 3.5 and $b (defined by either method) represents a complex number. In general using the Compute variant is preferred because the input string also serves as a model for the correct answer expected from the student.

Which MathObject types (classes) can be created?

These classes are listed and made available for writing problems in pg/macros/Value.pl. It is loaded automatically when you load MathObjects.pl.

Standard types

  • Real: Behave like real numbers
  • Infinity: The positive infinity of the extended reals. Can be negated, but can't be added to real numbers.
  • Complex: Behave like complex numbers. The interpretations of + and * are those standardly used for mathematical complex numbers.

List types

List objects are math objects whose description involves delimiters (parentheses) of some type. For example points (4, 5) or vectors <2,5> . Here are examples of the construction of the List Objects.

  • Point: $a = Point("(4,5)");
  • Vector: $b = Vector("<3,5,6!>");
  • Matrix: $c = Matrix("[[1,0],[0,1]]");
  • List: $d = List("3, 7, 3+2i");

Types that represent some subset of the real numbers

  • Interval: $I = Interval("[0,1)");
  • Set (a finite collections of points): $S = Set("{3,5,6,8}");
  • Union (of intervals and sets): $U = Union(""I U J"); (I union J)

The Compute command

  • You can use the Compute command to create any of the objects above from a string
Compute("3,7,3+2i")
Compute( "[[1,0],[0,1]]" );

The String type

String is a special purpose type which allows comparison to an arbitrary string.

String("DNE")

The Formula type

A Formula object represents a functions whose output is one of the MathObject types defined above. Every Formula contains a parse tree which allows you to calculate output values from given input values.

$f = Formula('2x^2+3x-5');

How to invoke a method of a MathObject

Use the standard Perl method call syntax:

$obj->method;
$obj->method($arg1,$arg2);

For example:

ANS($a->cmp);

This compares the student's answer with $a. If $a is Real then this comparison will be "fuzzy" which means that equality is checked to a tolerance defined by the current Context.

Methods shared by all MathObjects

  • cmp: Returns an answer checker for the Value. All of the answer checkers are defined in the file lib/Value/AnswerChecker.pm.
  • perl: Returns a string which represents the object as Perl source code.
  • perlFunction: Returns a Perl subroutine which represents the object. (Only available for Formula objects.)
  • value: Returns the value of the object.
  • TeX: Returns a string which represents the object as a TeX math expression.
  • string: Returns a string similar to that used to create the object. May include extra parentheses.
  • stringify: Produces the output of the object when inside quotes. Depending on context this is either a TeX string or a regular string. (This is called automatically by Perl when when an object is used in string context, and should not need to be called explicitly by the problem author.)
  • getFlag("flag name"): Returns the value of one of the object's internal flags. For example: $a->getFlag("tolerance");

The MathObjects Parser

The parser works "behind the scenes" to create formula. It's purpose is to parse a string representing a formula and turn it into a parse tree. Objects containing a parse tree are of the Formula class and have these additional methods.

Parser methods include:

  • eval
  • reduce
  • perl
  • TeX

The parser is defined in the file pg/lib/Parser.pm and the files in the pg/lib/Parser directory. Even though the subdirectory names under pg/lib/Parser are similar to those under pg/lib/Value they refer to different although related concepts. Under pg/lib/Parser the files refer to tokens in a string that is to be parsed, while the files under pg/lib/Value refer to MathObjects.

The Context

This is essentially a table of values that provides default values for the MathObjects and for the Parser. As a quick example: in Numeric context the answer (4,5) is interpreted as a point in the two dimensional plane. in Interval context it is interpreted as the real values x satisfying 4 < x < 5.

  • Define context using: Context("Numeric");
  • To obtain the current context: $context = Context();
  • Context names: defined in pg/lib/Parser/Context/Default.pm
    • Numeric: no Matrix, Complex or Vectors (or intervals) are allowed.
    • Complex: no Matrix or Vector, can't use "less than".
    • Point: really the same as the Vector context below
    • Vector: i, j, and k are defined as unit Vectors, no Complex numbers are allowed.
    • Vector2D: i and j are defined as unit Vectors, no Complex numbers are allowed.
    • Matrix: square brackets define Matrix instead of Point or Interval
    • Interval: similar to Numeric context, but (,) and [,] create Real Intervals rather than Lists. {,} creates finite sets of Reals.
    • Full: For internal use. This context is used to seed the others.
      • pi is defined
      • i is square root of minus one, but j and k are unit Vectors
      • Matrix, Vector and Complex are all defined.
      • x is a variable

Modifying Contexts

ModifyingContexts