Operators are special functions with a syntax which mimics mathematical arithmetic operations like the addition and the multiplication. They can be infix (such as x+y), separating their two arguments (called operands); prefix (such as -x), placed before their unique operand; or postfix (such as M'), placed after their unique operand. In SysQuake, their arguments are always evaluated from left to right. Since they do not require parenthesis or comma, their priority matters. Priority specifies when subexpressions are considered as a whole, as the argument of some operator. For instance, in the expression a+b*c, where * denotes the multiplication, the evaluation could result in (a+b)*c or a+(b*c); however, since operator *'s priority is higher than operator +'s, the expression yields a+(b*c) without ambiguity.
Here is the list of operators, from higher to lower priority:
' .' |
^ .^ |
- (unary) |
* .* / ./ \ .\ |
+ - |
== ~= < > <= >= === |
~ |
& |
| |
&& |
| |
: ? |
, |
; |
Most operators have also a functional syntax; for instance, a+b can also be written plus(a,b). This enables their overriding with new definitions and their use in functions such as feval which take the name of a function as an argument.
Here is the correspondence between operators and functions:
a;b | vertcat(a,b) |
a,b | horzcat(a,b) |
a:b | colon(a,b) |
a:b:c | colon(a,b,c) |
a|b | or(a,b) |
a&b | and(a,b) |
a<=b | le(a,b) |
a<b | lt(a,b) |
a>=b | ge(a,b) |
a>b | gt(a,b) |
a==b | eq(a,b) |
a~=b | ne(a,b) |
a===b | same(a,b) |
a~==b | unsame(a,b) |
a+b | plus(a,b) |
a-b | minus(a,b) |
a*b | mtimes(a,b) |
a/b | mrdivide(a,b) |
a\b | mldivide(a,b) |
a.*b | times(a,b) |
a./b | rdivide(a,b) |
a.\b | ldivide(a,b) |
a^b | mpower(a,b) |
a.^b | power(a,b) |
~a | not(a) |
-a | uminus(a) |
+a | uplus(a) |
a' | ctranspose(a) |
a.' | transpose(a) |
Operator which do not have a corresponding function are ?:, && and || because unlike functions, they do not always evaluate all of their operands.
Parenthesis.
(expr) v(:) v(index) v(index1,index2) v(:,index) v(index,:) v(predicate) v(predicate,:) v(:,predicate) v(:,:)
A pair of parenthesis can be used to change the order of evaluation. The subexpression it encloses is evaluated as a whole and used as if it was a single object. Parenthesis serve also to indicate a list of input or output parameters; see the description of the function keyword.
The last use of parenthesis is for specifying some elements of a matrix or list variable.
Matrices: In LME, any numerical object is considered as a two-dimension matrix. Therefore, two indices are required to specify a single element; the first index specifies the row, and the second the column. In some circumstances, however, it is convenient to consider a matrix as a vector, be it a column vector, a row vector, or even a matrix whose elements are indexed row-wise. For this way of handling matrices, a single index is specified. In all indexing operations, several indices can be specified simultaneously to extract more than one element. A single colon means all the elements in that dimension. The first valid value of an index is always 1. The matrix whose elements are extracted must be the content of a variable (an expression like [1,2;3,4](1,1) is invalid).
Instead of indices, the elements to be extracted can be selected by the true values in an array of the same size as the variable, or in a vector of the adequate size if some elements of a vector or some rows or columns of a matrix are extracted. Calculating a boolean expression based on the variable itself used as a whole is the easiest way to get such a logical array. LME knows whether to consider the expression between the parenthesis as an index or a predicate by looking at the logical flag of the object, which is set by all comparison operators and test functions and reset by arithmetic operators and mathematical functions.
Variable indexing can be used in an expression or in the left hand side of an assignment. In this latter case, the right hand size can be one of the following:
When indices are larger than the dimensions of the variable, the variable is expanded; new elements are set to 0.
Lists: In LME, lists have one dimension; thus a single index is required. Be it with a single index or a vector of indices, indexed elements are grouped in a list. New elements, also provided in a list, can be assigned to indexed elements; if the list to be assigned has a single element, the element is assigned to every indexed element of the variable. Predicate are not supported with lists.
(1+2)*3 9 a = [1,2,3;4,5,6;7,8,9]; a(2,3) 6 a(2,:) 4 5 6 a(:,3) 3 6 9 a(1:2,[1,3]) 1 3 4 6 a(3:5) 3 4 5 a(:) 1 2 3 4 5 6 7 8 9 a(a>=8) 8 9 a(sum(a,2) > 8, :) 4 5 6 7 8 9 a(1,5) = 99 a = 1 2 3 0 99 4 5 6 0 0 7 8 9 0 0 a = {1,[2,7,3],'abc',magic(3),'x'}; a([2,5]) {[2,7,3],'x'} a([2,5]) = {'ab','cde'} a = {1,'ab','abc',[8,1,6;3,5,7;4,9,2],'cde'} a([2,5]) = {[3,9]} a = {1,[3,9],'abc',[8,1,6;3,5,7;4,9,2],[3,9]} a(4) = {} a = {1,[3,9],'abc',[3,9]}
Operator {}, end, reshape, variable assignment, operator []
Brackets.
[matrix_elements]
A pair of brackets is used to define a matrix given by its elements or by submatrices. The operator , (or spaces) is used to separate elements on the same row, and the operator ; is used to separate rows. Since the space is considered as a separator when it is in the direct scope of brackets, it should not be used at the top level of expressions; as long as this rule is observed, each element can be given by an expression.
[1, 2, 3+5] 1 2 8 [1:3; 2 5 , 9 ] 1 2 3 2 5 9 [5-2, 3] 3 3 [5 -2, 3] 5 -2 3 [(5 -2), 3] 3 3
Operator {}, operator (), operator ,, operator ;
Braces.
{list_elements} v{index} v{index} = expr
A pair of braces is used to define a list given by its elements. The operator , (or spaces) is used to separate elements. Since the space is considered as a separator when it is in the direct scope of braces, it should not be used at the top level of expressions; as long as this rule is observed, each element can be given by an expression whose result is of any type and size.
v{index} is the element of list variable v whose index is given. index must be an integer between 1 (for the first element) and length(v) (for the last element). v{index} may be used in an expression to extract an element, or on the left hand-side of the equal sign to assign a new value to an element.
x = {1, 'abc', [3,5;7,1]} x = {1,string,real 2x2} x{3} 3 5 7 1 x{2} = 2+3j x = {1,2+3j,real 2x2} x{3} = {2} x = {1,2+3j,list}
operator ,, operator [], operator (), operator ;
Addition.
x + y M1 + M2 M + x +x +M
With two operands, both operands are added together. If both operands are matrices with a size different from 1-by-1, their size must be equal; the addition is performed element-wise. If one operand is a scalar, it is added to each element of the other operand.
With one operand, no operation is performed, except that the result is converted to a number if it was a string or a logical value, like with all mathematical operators and functions. For strings, each character is replaced with its numerical encoding. The prefix + is actually a synonym of double.
2 + 3 5 [1 2] + [3 5] 4 7 [3 4] + 2 5 6
operator -, sum, addpol, double
Subtraction or negation.
x - y M1 - M2 M - x -x -M
With two operands, the second operand is subtracted from the first operand. If both operands are matrices with a size different from 1-by-1, their size must be equal; the subtraction is performed element-wise. If one operand is a scalar, it is repeated to match the size of the other operand.
With one operand, the sign of each element is changed.
2 - 3 -1 [1 2] - [3 5] -2 -3 [3 4] - 2 1 2 -[2 2-3j] -2 -2+3j
Multiplication.
x * y M1 * M2 M * x
x*y multiplies the operands together. Operands can be scalars (plain arithmetic product), matrices (matrix product), or mixed scalar and matrix.
2 * 3 6 [1,2;3,4] * [3;5] 13 29 [3 4] * 2 6 8
Scalar multiplication.
x .* y M1 .* M2 M .* x
x.*y is the element-wise multiplication. If both operands are matrices with a size different from 1-by-1, their size must be equal; the multiplication is performed element-wise. If one operand is a scalar, it multiplies each element of the other operand.
[1 2] .* [3 5] 3 10 [3 4] .* 2 6 8
operator *, operator ./, operator .^
Matrix right division.
x / y M1 / M2 M / x
x/y divides the first operand by the second operand. If the second operand is a scalar, it divides each element of the first operand. Otherwise, it must be a square matrix; M1/M2 is equivalent to M1*inv(M2).
9 / 3 3 [2,6] / [1,2;3,4] 5 -1 [4 10] / 2 2 5
Scalar right division.
x ./ y M1 ./ M2 M ./ x x ./ M
The first operand is divided by the second operand. If both operands are matrices with a size different from 1-by-1, their size must be equal; the division is performed element-wise. If one operand is a scalar, it is repeated to match the size of the other operand.
[3 10] ./ [3 5] 1 2 [4 8] ./ 2 2 4 10 ./ [5 2] 2 5
operator /, operator .*, operator .^
Matrix left division.
x \ y M1 \ M2 x \ M
x\y divides the second operand by the first operand. If the first operand is a scalar, it divides each element of the second operand. Otherwise, it must be a square matrix; M1\M2 is equivalent to inv(M1)*M2.
3 \ 9 3 [1,2;3,4] \ [2;6] 2 0 2 \ [4 10] 2 5
Scalar left division.
M1 .\ M2 M1 .\ x
The second operand is divided by the first operand. If both operands are matrices with a size different from 1-by-1, their size must be equal; the division is performed element-wise. If one operand is a scalar, it is repeated to match the size of the other operand.
[1 2 3] .\ [10 11 12] 10 5.5 4
Matrix power.
x ^ y M ^ k x ^ M
x^y calculates x to the y power, provided that either
Other cases yield an error.
2 ^ 3 8 [1,2;3,4] ^ 2 7 10 15 22 2 ^ [1,2;3,4] 10.4827 14.1519 21.2278 31.7106
Scalar power.
M1 .^ M2 x .^ M M .^ x
M1.^M2 calculates M1 to the M2 power, element-wise. Both arguments must have the same size, unless one of them is a scalar.
[1,2;3,4].^2 1 4 9 16 [1,2,3].^[5,4,3] 1 16 27
Complex conjugate transpose.
M2 = M1'
M1' is the transpose of the real matrix M1, i.e. columns and rows are permuted. If M1 is complex, the result is the complex conjugate transpose of M1.
[1,2;3,4]' 1 3 2 4 [1+2j, 3-4j]' 1-2j 3+4j
Transpose.
M2 = M1'
M1.' is the transpose of the matrix M1, i.e. columns and rows are permuted. M1 can be real or complex.
[1,2;3,4].' 1 3 2 4 [1+2j, 3-4j].' 1+2j 3-4j
operator ', fliplr, flipud, rot90
Equality.
x == y
x == y is true if x is equal to y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is a matrix, the comparison is performed element-wise and the result has the same size.
1 == 1 true 1 == 1 + eps false 1 == 1 + eps / 2 true inf == inf true nan == nan false [1,2,3] == [1,3,3] T F T
operator ~=, operator <, operator <=, operator >, operator >=, operator ===, operator ~==, strcmp
Object equality.
a === b same(a, b)
a === b is true if a is the same as b, and false otherwise. Contrary to the equality operator, same returns a single boolean even if its operands are arrays. In addition, numbers, logical values and strings are not considered as equivalent.
(1:5) === (1:5) true (1:5) == (1:5) T T T T T [1,2,3] === [4,5] false [1,2,3] == [4,5] Incompatible size nan === nan true nan == nan false
operator ~==, operator ==, operator ~=, operator <, operator <=, operator >, operator >=, operator ==, operator ~=, strcmp
Inequality.
x ~= y
x ~= y is true if x is not equal to y, and false otherwise. Comparing NaN (not a number) to any number yields true, including to NaN. If x and/or y is a matrix, the comparison is performed element-wise and the result has the same size.
1 ~= 1 false inf ~= inf false nan ~= nan true [1,2,3] ~= [1,3,3] F T F
operator ==, operator <, operator <=, operator >, operator >=, operator ===, operator ~==, strcmp
Object inequality.
a ~== b unsame(a, b)
a ~== b is true if a is not the same as b, and false otherwise. Contrary to the inequality operator, ~== yields a single boolean even if its operands are arrays. In addition, numbers, logical values and strings are not considered as equivalent.
(1:5) ~== (1:5) false (1:5) ~= (1:5) F F F F F [1,2,3] ~== [4,5] true [1,2,3] ~= [4,5] Incompatible size nan ~== nan false nan ~= nan true
operator ===, operator ==, operator ~=, operator <, operator <=, operator >, operator >=, strcmp
Less than.
x < y
x < y is true if x is less than y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is a matrix, the comparison is performed element-wise and the result has the same size.
[2,3,4] < [2,4,2] F T F
operator ==, operator ~=, operator <=, operator >, operator >=
Greater than.
x > y
x > y is true if x is greater than y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is a matrix, the comparison is performed element-wise and the result has the same size.
[2,3,4] > [2,4,2] F F T
operator ==, operator ~=, operator <, operator <=, operator >=
Less or equal to.
x <= y
x <= y is true if x is less than or equal to y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is a matrix, the comparison is performed element-wise and the result has the same size.
[2,3,4] <= [2,4,2] T T F
operator ==, operator ~=, operator <, operator >, operator >=
Greater or equal to.
x >= y
x >= y is true if x is greater than or equal to y, and false otherwise. Comparing NaN (not a number) to any number yields false, including to NaN. If x and/or y is a matrix, the comparison is performed element-wise and the result has the same size.
[2,3,4] >= [2,4,2] T F T
operator ==, operator ~=, operator <, operator <=, operator >
Not.
~b
~b is false (logical 0) if b is different from 0 or false, and true otherwise. If b is a matrix, the operation is performed on each element.
~true false ~[1,0,3,false] F T F T
And.
b1 & b2
b1&b2 performs the logical AND operation between the corresponding elements of b1 and b2; the result is true (logical 1) if both operands are different from false or 0, and false (logical 0) otherwise.
[false, false, true, true] & [false, true, false, true] F F F T
operator |, xor, all
And with lazy evaluation.
b1 && b2
b1&&b2 is b1 if b1 is false, and b2 otherwise. Like with if and while statements, b1 is true if it is a nonempty array with only non-zero elements. b2 is evaluated only if b1 is true.
b1&&b2&&...&&bn returns the last operand which is false (remaining operands are not evaluated), or the last one.
Boolean value which is true if the vector v is made of pairs of equal values:
mod(length(v),2) == 0 && v(1:2:end) == v(2:2:end)
The second operand of && is evaluated only if the length is even.
operator ||, operator ?, operator &, if
Or.
b1 | b2
b1|b2 performs the logical OR operation between the corresponding elements of b1 and b2; the result is false (logical 0) if both operands are false or 0, and true (logical 1) otherwise.
[false, false, true, true] | [false, true, false, true] F T T T
operator &, xor, any
Or with lazy evaluation.
b1 || b2
b1||b2 is b1 if b1 is true, and b2 otherwise. Like with if and while statements, b1 is true if it is a nonempty array with only non-zero elements. b2 is evaluated only if b1 is false.
b1||b2||...||bn returns the last operand which is true (remaining operands are not evaluated), or the last one.
Boolean value which is true if the vector v is empty or if its first element is NaN:
isempty(v) || isnan(v(1))
operator &&, operator ?, operator |, if
Alternative with lazy evaluation.
b ? x : y
b?x:y is x if b is true, and y otherwise. Like with if and while statements, b is true if it is a nonempty array with only non-zero elements. Only one of x and y is evaluated depending on b.
Operators ? and : have the same priority; parenthesis or brackets should be used if e.g. x or y is a range.
Element of a vector v, or default value 5 if the index ind is out of range:
ind < 1 || ind > length(v) ? 5 : v(ind)
Horizontal matrix concatenation.
[M1, M2] [M1 M2]
Between brackets, the comma is used to separate elements on the same row in a matrix. Elements can be scalars, vector or matrices; their number of rows must be the same, unless one of them is an empty matrix.
Outside brackets or between parenthesis, the comma is used to separate statements or the arguments of functions.
[1,2] 1 2 [[3;5],ones(2)] 3 1 1 5 1 1 ['abc','def'] abcdef
Vertical matrix concatenation.
[M1; M2]
Between brackets, the semicolon is used to separate rows in a matrix. Rows can be scalars, vector or matrices; their number of columns must be the same, unless one of them is an empty matrix.
Outside brackets, the comma is used to separate statements; they loose any meaning between parenthesis and give a syntax error.
[1;2] 1 2 [1:5;3,2,4,5,1] 1 2 3 4 5 3 2 4 5 1 ['abc';'def'] abc def
Range.
x1:x2 x1:step:x2
x1:x2 gives a row vector with the elements x1, x1+1, x1+2, etc. until x2. The last element is equal to x2 only if x2-x1 is an integer, and smaller otherwise. If x2<x1, the result is an empty matrix.
x1:step:x2 gives a row vector with the elements x1, x1+step, x1+2*step, etc. until x2. The last element is equal to x2 only if (x2-x1)/step is an integer. With fractional numbers, rounding errors may cause x2 to be discarded even if (x2-x1)/step is "almost" an integer. If x2*sign(step)<x1*sign(step), the result is an empty matrix.
If x1 or step is complex, a complex vector is produced, with the expected content. The following algorithm is used to generate each element:
z = x1 while real((z - x1)/(x2 - x1)) <= 1 add z to the vector z = z + step end
This algorithm is robust enough to stop even if x2 is not on the complex straight line defined by x1 and step. If x2-x1 and step are orthogonal, it is attempted to produce an infinite number of elements, which will obviously trigger an out of memory error. This is similar to having a null step in the real case.
Note that the default step value is always 1 for consistency with real values. Choosing for instance sign(x2-x1) would have made the generation of lists of indices more difficult. Hence for a vector of purely imaginary numbers, always specify a step.
2:5 2 3 4 5 2:5.3 2 3 4 5 3:3 3 3:2 [] 2:2:8 2 4 6 8 5:-1:2 5 4 3 2 0:1j:10j 0 1j 2j 3j 4j 5j 6j 7j 8j 9j 10j 1:1+1j:5+4j 1 2+1j 3+2j 4+3j 5+4j 0:1+1j:5 0 1+1j 2+2j 3+3j 4+4j 5+5j
Function reference.
@fun
@fun gives a reference to function fun which can be used wherever an inline function can. Its main use is as the argument of functions like feval or quad, but it may also be stored in lists or structures. A reference cannot be cast to a double (unlike characters or logical values), nor can it be stored in an array.
quad(@sin, 0, pi) 2