Page logoMath sketchbook

Examples

Basics

Number Formatting

The decimal point is represented by the '.' character.

The comma is reserved for separating elements in vectors and operands in functions.

Addition and Subtraction

Parentheses are optional in an expression involving multiple operations. The evaluation order follows the mathematical standards in either case.

Multiplication and Division

Use * to denote multiplication. Omitting this is not supported currently.

Exponentiation

You can either use the function pow(base, exponent) or the operator ^ to calculate the power of a number.

Constants

Below are all the built-in constants.

They can be used with either uppercase or lowercase letters for convenience.

Defining Constants

Custom constants can be accessed anywhere in the document, regardless of where they are defined.

Use the following format to define a constant:
x = 123

Valid names consist of letters from the english alphabet, numbers and '_', but cannot begin with a number.

Complex Numbers

Complex numbers are supported as well and they can be used in many functions.

Comments

Single-line and multiline comments are supported:

  • // starts a single-line comments
  • /* and */ enclose a comment block spanning several lines

Functions

Function Usage

Check the Functions page for the complete list of functions. If you need something specific, use the function search panel.

The usage of functions is simple and follows the syntax used in most programming languages:

Scalar Functions

Here are a few of the (likely) most commonly used scalar functions:

Trigonometry

Many trigonometric functions are supported, and most of them even work with complex numbers.

Here are a few examples:

Linear Algebra

Construction

To create a vector, enclose comma-separated numbers with [ and ]:
vector = [1,2,3]

To create a matrix, enclose comma-separated vectors with [ and ]:
matrix = [[1,2],[3,4]]

Multiplications

For vectors, use either dot(v1,v2) or cross(v1,v2) to compute the dot and cross products, respectively.

Matrices support the usual operator (*) as long as the dimensions are compatible.

Both vectors and matrices support scalar multiplication.

Transformations

In graphics programming, there are many important functions to generate transformations:

  • lookAt
  • rotate
  • translate
  • scale
  • identity

For a complete list, check the Matrix construction section on the functions page.

Multiplication Order

Some people might be used to multiplying their vectors from the left, while others prefer it from the right. This can cause confusion.

To address this, I have added a suffix to matrix constructor functions that indicate what convention it follows. This is checked when the expression is evaluated. If a matrix is misused, a warning will be issued (though the evaluation will still be completed).

lookAtMV(...) implies that it will be used as lookAtMV(...) * vector. The MV suffix refers to Matrix * vector. The other variant is the VM suffix, which means Vector * Matrix, as it can be expected. The difference between the matrices is a simple transposition.

Equations

Linear Equations

To solve an equation, use a variable that hasn't been defined previously.

The equations will be automatically evaluated, and the result will be displayed below the equation.

Non-linear Equations

Non-linear equations are supported up to any level of complexity, although a solution is not guaranteed. Depending on the equation, it might be solved using a formula or numerically.

Linear equation systems

Equations that use any common variable will be considered a system of equations, even if the equations are located further apart in the document.

The solutions will be displayed below the last equation that belongs to the system.

Non-linear Equation Systems

Non-linear equation systems are always evaluated using numerical methods. In certain cases, this process can be time-consuming and the solution may not always be found.

Assistance

If there is an error in an equation, it will be indicated with a red background. This can be used to verify assumed equalities. This functionality also applies to custom-defined constants.

Use Cases

Help with Solving Equations

Here is an example (taken from a real use case), where I try to detect the time of collision of two objects.

While working through the equation, I made a small mistake. This resulted in a significant amount of time spent trying to identify the error.

With this tool, mistakes are detected as soon as they appear.

Verify Multiplication Order

In this example, a modelToWorld transform is constructed along with a viewProj matrix. The two matrices are constructed under different multiplication conventions. Detecting and debugging this kind of error can be particularly challenging.

The following example identifies and issues a warning about inconsistent multiplication order.