# Compute Engine Forms

A given mathematical expression can be represented in multiple equivalent ways as a MathJSON expression. A **form** is used to specify a representation:

: only transformations applied are those necessary to make it valid JSON (for example making sure that`'full'`

`Infinity`

and`NaN`

are represented as strings): associative functions are combined, e.g. f(f(a, b), c) -> f(a, b, c)`'flatten'`

: the arguments of commutative functions are sorted such that: - numbers are first, sorted numerically - complex numbers are next, sorted numerically by imaginary value - symbols are next, sorted lexicographically -`'sorted'`

`add`

functions are next -`multiply`

functions are next -`power`

functions are next, sorted by their first argument, then by their second argument - other functions follow, sorted lexicographically: any metadata associated with elements of the expression is removed.`'stripped-metadata'`

: each term of an expression is expressed as an object literal: no shorthand representation is used.`'object-literal'`

: `addition of 0 is simplified, associativity rules are applied, unnecessary groups are moved, single argument ‘add’ are simplified`'canonical-add'`

:`'canonical-divide'`

`divide`

is replaced with`multiply`

and `power’, division by 1 is simplified,:`'canonical-exp'`

`exp`

is replaced with`power`

: multiplication by 1 or -1 is simplified`'canonical-multiply'`

:`'canonical-power'`

`power`

with a first or second argument of 1 is simplified: real or complex number is replaced by the negative of that number. Negation of negation is simplified.`'canonical-negate'`

: complex numbers with no imaginary compnents are simplified`'canonical-number'`

:`'canonical-root'`

`root`

is replaced with`power`

:`'canonical-subtract'`

`subtract`

is replaced with`add`

and`negate`

: the following transformations are performed, in this order:`'canonical'`

`'canonical-number'`

, -> simplify number`'canonical-exp'`

, -> power`'canonical-root'`

, -> power, divide`'canonical-subtract'`

, -> add, negate, multiply,`'canonical-divide'`

, -> multiply, power`'canonical-power'`

, -> simplify power`'canonical-multiply'`

, -> multiply, power`'canonical-negate'`

, -> simplify negate`'canonical-add'`

, -> simplify add`'flatten'`

, simplify associative, idempotent, involution and groups`'sorted'`

,`'full'`

,

To transform an expression using the rules for a particular form, use the `format()`

function.

```
import { format } from 'math-json';
console.log(format(["Add", 2, "x", 3], 'canonical');
// ➔ ["Add", 2, 3, "x"]
```

`BaseForm`

`["BaseForm", _value_, _base_]`

Format a *value* in a specific *base*, such as hexadecimal or binary.

*value*should be an integer.*base*should be an integer from 2 to 36.

```
["Latex", ["BaseForm", 42, 16]]
// ➔ (\mathtt(2a))_{16}
```

```
Latex(BaseForm(42, 16))
// ➔ (\mathtt(2a))_{16}
BaseForm(42, 16)
// ➔ 0x2a
```

`Derivative`

`["Derivative", _expression_, _order_]`

*order*: default value is 1.

MathJSON | Latex |
---|---|

`["Derivative", "f"]` | `f^\prime` |

`["Derivative", "f", 2]` | `f^\doubleprime` |

`["Derivative", "f", "n"]` | `f^{(n)}` |