Basic Expressions

You can use MP's command line to solve basic mathematical expressions. The basic operations consist of operands and operators. An operand may be a number, variable or constant or an expressions that produces a number.

The following operators are available:

= Assignment. It is used to assign values to variables. For equatlity, use '==' operator.
() For changing priority of operations as well as to enclose arguments passed to the functions
+ With one operand specifies that the number is positive (default). With two operands performs addition
- With one operand specifies that the number is negative. With two operands performs subtraction
* Multiplication
/ Division
% Modulus operator. Obtains the 'remainder' after dividing first operand by the second (cf. rem and IEEERem)
^ Power. Raises the first operand to the power of the second
, Separator

For details of how operators work with Matrices see the section Matrix Support. Priority of the operators from highest to lowest is specified as:

  • ()
  • unary + and - (to specify that the associated number is either positive (default) or negative.
  • ^
  • * / %
  • + - (addition and subtraction)
  • = (assignment)
  • , (comma)

We recommend using parentheses to avoid any confusion with the operators' priority in complex operations

All the above operators work not only with numbers but also with arrays.

For any of binary left-to-right operators (except =), if both the operands are singe values, operation is performed as in ordinary practice; and if they are both arrays, they must be of equal size. In that case respective members of the arrays are used as operands. If one of the operands is an array and the other is a single value, the operation is performed using each member of the array as one operand and the single value as the second operand.

Operator '=' is used for assignment. It means, the LHS (left-hand side) can only be a variable. It can be used for a single assignment like this

a = 3

Or in a series like this:

a = b = c = d = 3

In the second case, all the variables will be given the same value 3. Assignment will be done from right to left i.e. first d will be assigned, then c and so on.

Note: Certain directives are helpful for working with the variables. See the Directives section for further details.