IMathAS Language Overview

Purpose of this Document

This document provides an overview of the IMathAS Question Language, including variable definition, basic operators, control structures, etc. It does not include a list of all IMathAS functions; please refer to the help documentation section on writing questions for function reference

Overview

The IMathAS language is built on top of PHP, and many of PHP's properties apply. However, the IMathAS language was written to allow ASCIIMath operator use, simplify coding for non-coders, and provide some level of security.

Variables

Variables are represented by a dollar sign followed by the name of the variable. The first character of the name must be a letter, then the remainder of the name can contain numbers and the underscore character.

Variables do not have to be declared or typed; you can just go ahead and define them. Variables have type (number,string,array,etc.) automatically determined.

Example:

$a = 5
$b = "linear"
$c = array(2,3,4)

Advanced: PHP and IMathAS also support "variable variables". An example, if you wrote:

$name = "linear"
${$name} = "y=mx+b"

The second line creates the variable $linear, with value "y=mx+b"

Arrays

Arrays are similar to lists, allowing you to store several values in one variable. The elements of the array are accessed by an index, in the form $variable[index]. Arrays are 0-indexed, meaning the first element of the array has an index of 0

Example

$a = array(2,4,6)
$b = array("me","you","other")
$c[0] = 5
$c[1] = 6

In this example, $a is defined to be an array of numbers. $a[0] would be 2. $b is an array of strings; $b[1] = "you". The last two lines show that variables can be defined element-by-element, rather than defining the entire array at once

Constants

The following constants are defined in IMathAS:

SymbolMeaningExample
pipi (3.14159...)$a = pi*5^2
ee (2.71828...)$a = 20*e^(.05*10)

Operators

The following are basic operators (plus a couple other useful ones)

SymbolMeaningExample
+, -Add, Subtract2 + 3 = 5, 2 - 3 = -1
*, /Multiply, Divide2 * 3 = 6, 8/4 = 2
%Modulus (Remainder after division)7 % 3 = 1, 6 % 2 = 0,
^Power2^3 = 8
.String Concatenation"one "."two" = "one two"
sqrtSquare rootsqrt(9) = 3
absAbsolute valuesabs(-9) = 9
!Factorial5! = 120

Control Structures

There are two control structures in IMathAS:

if

The "if" statement allows you to conditionally make variable assignments. Its syntax takes the form:

$var = $toassign if (condition)

Some examples:

$a = rand(0,1)
$b = "even" if ($a==0)
$b = "odd" if ($a==1)

$c = 3 if ($a > 0)

where

The "where" statement allows you to repeat a randomization statement until some condition is met. Its syntax is:

$var = randomizer where (condition)

Note that the condition is what we want. For system stability, the system gives up on the where condition if it cannot be met in 10 iterations. A warning message will be displayed if this happens.

An example, to find two relatively prime numbers:

$a = rand(2,10)
$b = rand(2,10) where (gcd($a,$b)==1)

Comparison Operators

These comparison operators are available:

SymbolMeaningExample
<Less thanwhere ($a < 1)
>Greater thanwhere ($a > 1)
<=Less than or equal towhere ($a <= 1)
>=Greater than or equal towhere ($a >= 1)
==Equal to (note it's two equal signs)where ($a%$b == 0)
!=Not equal towhere ($a%$b != 0)
&&Andwhere (($a>5) && ($a<10))
||Orwhere (($a<5) || ($a>10))

for / foreach

There is no official "for" or "foreach" control structure in IMathAS. However, there is a function called "calconarray" which can achieve similar results. An example:

$a = array(1,2,3,4)
$b = calconarray($a,"x^2")
$c = calconarray($a,"'y='.x")

$b is now the array (1,4,9,16). $c is the array ("y=1","y=2","y=3","y=4")

Functions

IMathAS provides a library of macros (functions) which provide the core functionality of IMathAS, including randomization, working with variables, creating graphs, etc. See the help section on writing questions for a full list of these functions.

In addition to built-in functions, additional functions can be loaded using the loadlibrary command. In the question editor, click the "Macro Library Help" link to view what libraries are available on your system. Macro libraries have to be installed by your IMathAS administrator before they can be used in questions. Macro libraries allow users to extend the functionality of IMathAS by defining functions using direct PHP code.


© 2006 David Lippman
This guide was written with development grant support from the WA State Distance Learning Council