FreeFEM Documentation on GitHub

stars - forks

# Operators

1 real a = 1. + 2.;


Works for int, real, complex, string, mesh, mesh3, array.

## Increment operator ++

Pre-increment:

1 int i = 0;
2 ++i;


Post-increment:

1 int i = 0;
2 i++;


## Substraction operator -

1 real a = 1. - 2.;


Works for int, real, complex, array.

## Decrement operator –

Pre-decrement:

1 int i = 0;
2 --i;


Post-decrement:

1 int i = 0;
2 i--;


## Multiplication operator *

1 real[int] b;
2 matrix A
3 real[int] x = A^-1*b;


Works for int, real, complex, array, matrix.

## Equal operator =

1 real a = 1.;


## Comparison operator ==

1 real a = 1.;
2 real b = 1.;
3
4 cout << (a == b) << endl;


## Comparison operator !=

1 real a = 1.;
2 real b = 2.;
3
4 cout << (a != b) << endl;


## Comparison operator <, <=

1 real a = 1.;
2 real b = 2.;
3
4 cout << (a < b) << endl;
5 cout << (a <= b) << endl;


## Comparison operator >, >=

1 real a = 3.;
2 real b = 2.;
3
4 cout << (a > b) << endl;
5 cout << (a >= b) << endl;


## Compound operator +=, -=, *=, /=

1 real a = 1;
2 a += 2.;
3 a -= 1.;
4 a *= 3.;
5 a /= 2.;


## 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.

## Inverse of a matrix ^-1

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 ::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

1 int a = 12; int b = 5;
2
3 cout << a << " + " << b << " = " << a + b << endl;
4 cout << a << " - " << b << " = " << a - b << endl;
5 cout << a << " * " << b << " = " << a * b << endl;
6 cout << a << " / " << b << " = " << a / b << endl;
7 cout << a << " % " << b << " = " << a % b << endl;
8 cout << a << " ^ " << b << " = " << a ^ b << endl;
9 cout << "( " << 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

1 real a = qsrt(2.); real b = pi;
2
3 cout << a << " + " << b << " = " << a + b << endl;
4 cout << a << " - " << b << " = " << a - b << endl;
5 cout << a << " * " << b << " = " << a * b << endl;
6 cout << a << " / " << b << " = " << a / b << endl;
7 cout << a << " % " << b << " = " << a % b << endl;
8 cout << a << " ^ " << b << " = " << a ^ b << endl;
9 cout << "( " << 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