Skip to content

Operators

+#

Addition operator.

1
real a = 1. + 2.;
Works for int, real, complex, string, mesh, mesh3, array.

-#

Substraction operator.

1
real a = 1. - 2.;
Works for int, real, complex, array.

*#

Multiplication operator.

1
2
3
real[int] b;
matrix A
real[int] x = A^-1*b;
Works for int, real, complex, array, matrix.

.*#

Term by term multiplication.

1
matrix A = B .* C;

/#

Division operator.

1
real a = 1. / 2.;
Works for int, real, complex.

./#

Term by term division.

1
matrix A = B ./ C;

%#

Remainder from the division.

1
int a = 1 % 2;
Works for int, real.

^#

Power operator.

1
real a = 2.^2;
Works for int, real, complex, matrix.

^-1#

Inverse of a matrix.

1
real[int] Res = A^-1 * b;

Warning

This operator can not be used to directly create a matrix, see Matrix inversion.

'#

Transpose operator.

1
real[int] a = b';
Works for array and matrix.

Note

For matrix<complex>, the '' operator return the Hermitian tranpose.

:#

Tensor scalar product.

? :#

C++ arithmetical if expression.

a ? b : c is equal to b if the a is true, c otherwise.

Example with int

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
int a = 12;
int b = 5;

cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << a << " % " << b << " = " << a % b << endl;
cout << a << " ^ " << b << " = " << a ^ b << endl;
cout << "( " << a << " < " << b << " ? " << a << " : " << b << ") = " << (a < b ? a : b) << endl;

The output of this script is:

1
2
3
4
5
6
7
12 + 5 = 17
12 - 5 = 7
12 * 5 = 60
12 / 5 = 2
12 % 5 = 2
12 ^ 5 = 248832
( 12 < 5 ? 12 : 5) = 5

Example with real

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
real a = qsrt(2.);
real b = pi;

cout << a << " + " << b << " = " << a + b << endl;
cout << a << " - " << b << " = " << a - b << endl;
cout << a << " * " << b << " = " << a * b << endl;
cout << a << " / " << b << " = " << a / b << endl;
cout << a << " % " << b << " = " << a % b << endl;
cout << a << " ^ " << b << " = " << a ^ b << endl;
cout << "( " << a << " < " << b << " ? " << a << " : " << b << ") = " << (a < b ? a : b) << endl;

The output of this script is:

1
2
3
4
5
6
7
1.41421 + 3.14159 = 4.55581
1.41421 - 3.14159 = -1.72738
1.41421 * 3.14159 = 4.44288
1.41421 / 3.14159 = 0.450158
1.41421 % 3.14159 = 1
1.41421 ^ 3.14159 = 2.97069
( 1.41421 < 3.14159 ? 1.41421 : 3.14159) = 1.41421