# MathScript Syntax Suitable for Real-Time Applications (MathScript RT Module)

LabVIEW 2012 MathScript RT Module Help

Edition Date: June 2012

Part Number: 373123C-01

»View Product Info

National Instruments has tested a subset of syntax possible in LabVIEW MathScript and found that this syntax typically executes in a bounded amount of time when used according to the examples and requirements in this topic and certain other guidelines. When determinism in your application is important, use this syntax to write scripts and user-defined functions for use in MathScript Nodes that you deploy in a real-time application.

## General Operations

The following table provides recommended syntax for commonly used operations in MathScript.

Construct Grammar Requirements Example
Binary Operations a operator b operator is a numeric operator, such as +, -, .*, ./, .\, .^, <=, >=, <, >, ==, and ~=.

a and b are vectors of the same orientation and number of elements, or matrices of the same dimensions. Or, if one input is a scalar, the other input is a scalar, vector, or matrix.

a and b must be numeric data types only.
[1, 2] + [3, 4];
a operator b operator is a logical operator, such as ==, ~=, &&, ||, &, and |.

a and b are vectors of the same orientation and number of elements, or matrices of the same dimensions. Or, if one input is a scalar, the other input is a scalar, vector, or matrix.

a and b must be Boolean values only.
[2, 3; 2, 2] ~= [3, 3; 2, 3];
a * b a and b must be double-precision, floating-point numeric or complex double-precision, floating-point numeric values. One input is a scalar or vector and the other input is a scalar, vector, or matrix. 2 * [2; 3];
One input is an n-by-m matrix and the other input is an m-by-1 column vector. [1, 4; 2, 3; 0, 3] * [1; 2];
One input is a 1-by-n row vector and the other input is an n-by-m matrix. [1, 2] * [1, 3, 5; 2, 4, 6];
One input is an m-by-n matrix and the other input is an n-by-r matrix [0, 2; -1, 6; 3, 1; -2, 5] * [1, 3, 5; 2, 4, 6];
Unary Operations -a, ~a, a', a.' a is a scalar, vector, or matrix value.

For -a, a must be a numeric value or values. For ~a, a must be a Boolean value or values. For a' and a.', a must be either a Boolean or numeric value or values.
[1, 2]';

~[1, 0; 0, 1];
Constant Values constant constant is a MathScript function that produces constants, such as zeros, ones, true, false, and functions of the constants class. pi;
constant(n) n is a constant integer number and constant is a MathScript function that produces constants. a = true(3);
constant(n, m) n and m are constant integer numbers and constant is a MathScript function that produces constants. z = zeros(2, 3);
Literals literal literal is a string constant or a constant of any numeric data type. a = 4;

b = 'string';
Matrix Definition [a1, a2, ..., an] Values of a must be scalar values of any numeric data type, or constants such as NaN or Inf. If a is a vector or matrix, index scalar values from a for use in the matrix. a = [1, 2, 0];

b = [0; Inf];

Values of a must be scalar values of any numeric data type, or constants such as NaN or Inf. a = [1, 2, 0; 6, 1.5, 3];
Range Definition start:stop start and stop are constant scalar numbers of either integer or floating-point data types, or non-constant integer or floating-point numbers with the length of start:stop fixed over successive executions of the line of script. b = 2:20;
start:step:stop start, step, and stop are constant scalar numbers of either integer or floating-point data types, or non-constant integer or floating-point numbers with the length of (start:step:stop) fixed over successive executions of the line of script. b = 2:2:20;

## Loops and Statements

The following table provides recommended syntax for loops and statements you can use in MathScript.

Construct Grammar Requirements Example
For Loop for iter = expression
statement_list
end
statement_list and expression, if applicable, are composed of operations, syntax, or built-in functions recommended by National Instruments in this topic and as described in other guidelines. expression must result in a scalar, vector, or matrix of numeric or Boolean values. for i = [3, 6, 9]
a = sin(2*pi*i/10);
end
for iter = start:stop
statement_list
end
start and stop are scalar numbers of either integer or floating-point data types. for i = 1:10
a = sin(2*pi*i/10);
end
for iter = start:step:stop
statement_list
end
start, step, and stop are scalar numbers of either integer or floating-point data types. for i = 1:2:10
a = sin(2*pi*i/10);
end
If-Else Statement if expression
statement_list
[elseif expression
statement-list]
...
[else
statement_list]
end
expression must result in a scalar Boolean value. if b == 1
c = 3;
else
c = 4;
end
While Loop while expression
statement_list
end
expression must result in a scalar Boolean value. while i < 10
a = cos(2*pi*i/10);
i = i + 1;
end

## Matrix Operations

The following table provides recommended syntax for indexing and replacing values in vectors and matrices, where values in italics indicate syntax for replacement operations only. Always specify indexes as 32-bit signed integers. When you specify a start:stop range, explicitly define the range rather than defining the range and assigning the value to a variable on a separate line of the script.

Vectors and matrices in MathScript are one-based, not zero-based as in LabVIEW. Therefore, the index of the first element in a vector or matrix is one. Also, when you specify a single index for a matrix element rather than both a row and column index, the elements are ordered linearly and column-wise. For example, given a 2-by-2 matrix, the index of the element in the first row and second column is 3.

Operation Grammar Example
Return or replace a single element in a vector vector(index) = scalar Return the second element of b:

b = [4, -7, 0];
b(2);
Return or replace a range of elements in a vector vector(start_index:stop_index) = vector Replace the second and third elements of b:

a = zeros(1, 2);
b = ones(1, 4);
b(2:3) = a;
Return a column vector of all elements in a row vector vector(:) a = ones(1, 4);
a(:);
Return a single element in a matrix matrix(index) Return the element in the second column and first row of b:

b = [4, -2; 9, 5];
b(3);
Return or replace a single element in a matrix matrix(column_index, row_index) = scalar Replace the element in the second column and second row of b:

a = 0;
b = ones(2);
b(2, 2) = a;
Return or replace a single column in a matrix matrix(:, column_index) = vector Return the second column of b:

b = ones(2);
b(:, 2);
Return or replace a single row in a matrix matrix(row_index, :) = vector Replace the second row of b:

a = zeros(1, 2);
b = ones(2);
b(2, :) = a;
Return a range of elements in a matrix matrix(start_index:stop_index) Return the third through fifth elements of b:

b = [3, 7, -1; 4, 5, 0];
b(3:5);
Return a range of elements in a matrix column matrix(start_index:stop_index, column_index) Return the second through fourth elements of the second column in b:

b = [3, 7; -1, 4; 4, 5; 0, 9];
a = b(2:4, 2);
Return a range of elements in a matrix row matrix(row_index, start_index:stop_index) Return the second through fourth elements of the second row in b:

b = [3, 7, -1, 4; 4, 5, 0, 9];
a = b(2, 2:4);
Return or replace a range of columns in a matrix matrix(:, start_column_index:stop_column_index) = matrix Replace the first and second columns in b:

a = zeros(3, 2);
b = ones(3);
b(:, 1:2) = a;
Return or replace a range of rows in a matrix matrix(start_row_index:stop_row_index, :) = matrix Replace the first and second rows in b:

a = zeros(2, 3);
b = ones(3);
b(1:2, :) = a;
Return a column vector of all elements in a matrix matrix(:) a = ones(2);
a(:);