Operators
Addition operator +
1real a = 1. + 2.;
Works for int
, real
, complex
, string
, mesh
, mesh3
, array.
Increment operator ++
Pre-increment:
1int i = 0;
2++i;
Post-increment:
1int i = 0;
2i++;
Substraction operator -
1real a = 1. - 2.;
Works for int
, real
, complex
, array.
Decrement operator –
Pre-decrement:
1int i = 0;
2--i;
Post-decrement:
1int i = 0;
2i--;
Multiplication operator *
1real[int] b;
2matrix A
3real[int] x = A^-1*b;
Works for int
, real
, complex
, array, matrix
.
Equal operator =
1real a = 1.;
Comparison operator ==
1real a = 1.;
2real b = 1.;
3
4cout << (a == b) << endl;
Comparison operator !=
1real a = 1.;
2real b = 2.;
3
4cout << (a != b) << endl;
Comparison operator <, <=
1real a = 1.;
2real b = 2.;
3
4cout << (a < b) << endl;
5cout << (a <= b) << endl;
Comparison operator >, >=
1real a = 3.;
2real b = 2.;
3
4cout << (a > b) << endl;
5cout << (a >= b) << endl;
Compound operator +=, -=, *=, /=
1real a = 1;
2a += 2.;
3a -= 1.;
4a *= 3.;
5a /= 2.;
Term by term multiplication .*
1matrix A = B .* C;
Division operator /
1real a = 1. / 2.;
Works for int
, real
, complex
.
Term by term division ./
1matrix A = B ./ C;
Remainder from the division %
1int a = 1 % 2;
Works for int
, real
.
Power operator ^
1real a = 2.^2;
Works for int
, real
, complex
, matrix
.
Inverse of a matrix ^-1
1real[int] Res = A^-1 * b;
Warning
This operator can not be used to directly create a matrix, see Matrix inversion.
Transpose operator ‘
1real[int] a = b';
Works for array and matrix
.
Note
For matrix<complex>
, the ::freefem`’` operator return the Hermitian tranpose.
Tensor scalar product :
\[A:B = \sum_{i,j}{A_{ij}B_{ij}}\]
C++ arithmetical if expression ? :
a ? b : c
is equal to b
if the a
is true, c
otherwise.
Tip
Example with int
1int a = 12; int b = 5;
2
3cout << a << " + " << b << " = " << a + b << endl;
4cout << a << " - " << b << " = " << a - b << endl;
5cout << a << " * " << b << " = " << a * b << endl;
6cout << a << " / " << b << " = " << a / b << endl;
7cout << a << " % " << b << " = " << a % b << endl;
8cout << a << " ^ " << b << " = " << a ^ b << endl;
9cout << "( " << a << " < " << b << " ? " << a << " : " << b << ") = " << (a < b ? a : b) << endl;
The output of this script is:
12 + 5 = 17
12 - 5 = 7
12 * 5 = 60
12 / 5 = 2
12 % 5 = 2
12 ^ 5 = 248832
( 12 < 5 ? 12 : 5) = 5
Tip
Example with real
1real a = qsrt(2.); real b = pi;
2
3cout << a << " + " << b << " = " << a + b << endl;
4cout << a << " - " << b << " = " << a - b << endl;
5cout << a << " * " << b << " = " << a * b << endl;
6cout << a << " / " << b << " = " << a / b << endl;
7cout << a << " % " << b << " = " << a % b << endl;
8cout << a << " ^ " << b << " = " << a ^ b << endl;
9cout << "( " << a << " < " << b << " ? " << a << " : " << b << ") = " << (a < b ? a : b) << endl;
The output of this script is:
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