Vectors and tensors
Tolosa-lib contains different structures for vectors and tensors in order to enhance numerical implementation and lisibility. Therefore, it enables one to implement numerical schemes easily.
Vectors¶
Two structures for 2D and 3D vectors are implemented. The implementation of various operations is enhanced. One can easily add and substract vectors, multiply and divide vectors by a scalar, compute the euclidean norm and its square, and the product and cross products of two vectors.
2D¶
The structure of a 2D vector contains two components x
and y
and several vector procedures. This structure overloads the assignment operator and other operators by creating a pointer component for each of them.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
|
The assignement operator =
points to the equal_vec2d
and equal_vec2d_scal
procedures. This operation assigns a value to each component of a vector.
Assignement =
a = b
(wherea
andb
are twovec2d
objects) assignsb%x
toa%x
andb%y
toa%y
a = r
(wherea
is avec2d
object andr
is a scalar) assignsr
toa%x
anda%y
The +
operator points to the add_vec2d
procedure. This operation adds the components of each vector.
Operation +
c = a + b
(where a
, b
, c
are vec2d
objects) assigns a%x + b%x
to c%x
and a%y + b%y
to c%y
.
The -
operator points to the sub_vec2d
and sub_vec2d_unary
procedures. This operation substract the components of one vector to another, or substract the components of a vector by a scalar.
Operation -
c = a - b
(wherea
,b
,c
arevec2d
objects) assignsa%x - b%x
toc%x
anda%y - b%y
toc%y
.c = - a
(wherea
,c
arevec2d
objects) assigns-a%x
toc%x
and-a%y
toc%y
.
The *
operator points to the scalar_vec2d
and vec2d_scalar
procedures. This operation multiply each component of a vector by a scalar.
Operation *
c = r * a
or c = a * r
(where a
and c
are vec2d
objects, and r
a scalar) assign r * a%x
to c%x
and r * a%y
to c%y
.
The /
operator points to the vec2d_div_scalar
procedure. This operation divides each component by a scalar.
Operation /
c = a / r
(where a
and c
are vec2d
objects and r
a scalar) assigns a%x / r
to c%x
and a%y / r
to c%y
.
The .norm.
operator points to the norm_vec2d
procedure. This operation computes the euclidean norm of a vector.
Operation .norm.
r = .norm. a
(where a
is a vec2d
object and r
a scalar) assigns sqrt( a%x**2 + a%y**2 )
to r
.
The .square.
operator points to the square_vec2d
procedure. This operatio computes the square of the euclidean norm of a vector.
Operation .square.
r = .square. a
(where a
is a vec2d
object and r
a scalar) assigns a%x**2 + a%y**2
to r
.
The .dotprod.
operator points to the dot_product_vec2d
procedure. This operation computes the dot product of two vectors.
Operation .dotprod.
r = a .dotprod. b
(where a
and b
are vec2d
objects and r
a scalar) assigns a%x * b%x + a%y * b%y
to r
.
The .tensprod.
operator points to the tens_prod2d
procedure. This operation computes the tensor product of two vectors.
Operation .tensprod.
c = a .tensprod. b
(where a
, b
are vec2d
objects and c
a tens2d
object) :
c%xx = a%x * b%x
c%xy = a%x * b%y
c%yx = a%y * b%x
c%yy = a%y * b%y
The .rot.
operator points to the rot_vec2d
procedure. This operation applies a rotation operation on a vector.
Operation .rot.
c = a .rot. b
(where a
, b
, c
are vec2d
objects) :
c%x = b%x * a%x + b%y * a%y
c%y = b%x * a%y - b%y * a%x
The .invrot.
operator points to the invrot_vec2d
procedure. This operation unapplies the previously defined rotation operation.
Operation .invrot.
c = a .invrot. b
(where a
, b
, c
are vec2d
objects) :
c%x = b%x * a%x - b%y * a%y
c%y = b%x * a%y + b%y * a%x
3D¶
The structure of a 3D vector contains three components x
, y
and z
and several vector procedures. This structure overloads the assignment operator and other operators by creating a pointer component for each of them.
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
The assignement operator =
points to the equal_vec3d
and equal_vec3d_scal
procedures.
The +
operator points to the add_vec3d
procedure.
The -
operator points to the sub_vec3d
and sub_vec3d_unary
procedures.
The *
operator points to the scalar_vec3d
and vec3d_scalar
procedures.
The /
operator points to the vec3d_div_scalar
procedure.
The .norm.
operator points to the norm_vec3d
procedure.
The .square.
operator points to the square_vec3d
procedure.
The .dotprod.
operator points to the dot_product_vec3d
procedure.
Assignment =
and +
, -
, *
, /
, .norm.
, .square.
, .dotprod.
operators
These operators are similar to the 2D operators, see Vectors - 2D to get the detailed operations and significations.
The .tensprod.
operator points to the tens_prod3d
procedure. This operation computes the tensor product of two vectors.
Operation .tensprod.
The .tensprod.
operator is pretty similar to the 2D operator, still for c = a .tensprod. b
(where a
, b
are vec3d
objects and c
a tens3d
object) :
c%xx = a%x * b%x
c%xy = a%x * b%y
c%xz = a%x * b%z
c%yx = a%y * b%x
c%yy = a%y * b%y
c%yz = a%y * b%z
c%zx = a%z * b%x
c%zy = a%z * b%y
c%zz = a%z * b%z
The .rot.
operator points to the rot_vec3d
procedure. This operation applies a rotation operation on a vector.
Operation .rot.
c = a .rot. b
(where a
, b
, c
are vec3d
objects) :
d = sqrt( b%x**2 + b%y**2 )
if ( d < epsilon(1._rp) ) then
c%x = a%z
c%y = a%y
c%z = - a%x
else
dinv = - 1._rp / d
c%x = b%x * a%x + b%y * a%y + b%z * a%z
c%y = b%y * dinv * a%x - b%x * dinv * a%y
c%z = b%x * b%z * dinv * a%x + b%y * b%z * dinv * a%y + d * a%z
end if
The .invrot.
operator points to the invrot_vec3d
procedure. This operation unapplies the previously defined rotation operation.
Operation .invrot.
c = a .invrot. b
(where a
, b
, c
are vec3d
objects) :
d = sqrt( b%x**2 + b%y**2 )
if ( d < epsilon(1._rp) ) then
c%x = - a%z
c%y = a%y
c%z = a%x
else
dinv = - 1._rp / d
c%x = b%x * a%x + b%y * dinv * a%y + b%x * b%z * dinv * a%z
c%y = b%y * a%x - b%x * dinv * a%y + b%y * b%z * dinv * a%z
c%z = b%z * a%x + d * a%z
end if
Tensor¶
Two structures for 2D and 3D tensors are implemented. The implementation of various operations is enhanced. One can easily add and substract tensors, multiply a tensor by a scalar or a vector, divide a tensor by a scalar, and transpose a tensor.
2D¶
The structure of a 2D tensor contains four components xx
, xy
, yx
and yy
and several tensor procedures. This structure overloads the assignment operator and other operators by creating a pointer component for each of them.
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
|
The assignement operator =
points to the equal_tens2d
and equal_tens2d_scal
procedures. This operation assigns a value to each tensor component.
Assignement =
a = b
(wherea
andb
are twotens2d
objects) assigns all theb
components to the correspondinga
components.a = r
(wherea
is atens2d
object andr
is a scalar) assignsr
to all thea
components.
The +
operator points to the add_tens2d
procedure. This operation sums the components of each tensor.
Operation +
c = a + b
(where a
, b
, c
are tens2d
objects) :
c%xx = a%xx + b%xx
c%xy = a%xy + b%xy
c%yx = a%yx + b%yx
c%yy = a%yy + b%yy
The -
operator points to the sub_tens2d
procedure. This operation substract each component of one tensor to another.
Operation -
c = a - b
(where a
, b
, c
are tens2d
objects) :
c%xx = a%xx - b%xx
c%xy = a%xy - b%xy
c%yx = a%yx - b%yx
c%yy = a%yy - b%yy
The *
operator points to the scalar_tens2d
, tens2d_scalar
, tens2d_vec2d
procedures. This operation multiplies each tensor's component by a scalar, or multiplies a tensor to a vector.
Operation *
c = r * a
orc = a * r
(wherea
andc
aretens2d
objects, andr
a scalar) :c%xx = r * a%xx
c%xy = r * a%xy
c%yx = r * a%yx
c%yy = r * a%yy
c = A * b
(whereA
is atens2d
object andc
andb
vec2d
objects) :c%x = A%xx * b%x + A%xy * b%y
c%y = A%yx * b%x + A%yy * b%y
The /
operator points to the tens2d_div_scalar
procedure. This operation divides each tensor's component by a scalar.
Operation /
c = a / r
(where a
and c
are tens2d
objects and r
a scalar) :
c%xx = a%xx / r
c%xy = a%xy / r
c%yx = a%yx / r
c%yy = a%yy / r
The .t.
operator points to the transpose_tens2d
procedure. This operation computes the tensor's transposition.
Operation .t.
c = .t. a
(where a
and c
are tens2d
objects) :
c%xx = a%xx
c%xy = a%yx
c%yx = a%xy
c%yy = a%yy
3D¶
The structure of a 3D tensor contains nine components xx
, xy
, xz
, yx
, yy
, yz
, zx
, zy
and zz
and several tensor procedures. This structure overloads the assignment operator and other operators by creating a pointer component for each of them.
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 |
|
The assignement operator =
points to the equal_tens3d
and equal_tens3d_scal
procedures.
The +
operator points to the add_tens3d
procedure.
The -
operator points to the sub_tens3d
procedure.
Assignment =
and +
, -
operators
These operators are similar to the 2D operators, see Tensors - 2D to get the detailed operations and significations.
The *
operator points to the scalar_tens3d
, tens3d_scalar
, tens3d_vec3d
procedures. This operation multiplies each tensor's component by a scalar, or multiplies a tensor to a vector.
Operation *
-
c = r * a
orc = a * r
: If a scalar is involved in the operation, the multiplication is similar to the 2D multiplication (see Tensors - 2D). -
c = A * b
(whereA
is atens3d
object andc
andb
vec3d
objects) :c%x = A%xx * b%x + A%xy * b%y + A%xz * b%z
c%y = A%yx * b%x + A%yy * b%y + A%yz * b%z
c%z = A%zx * b%x + A%zy * b%y + A%zz * b%z
The /
operator points to the tens3d_div_scalar
procedure.
Operation /
This operator is similar to the 2D operator, see Tensors - 2D to get the detailed operation and signification.
The .t.
operator points to the transpose_tens3d
procedure. This operation computes the tensor's transposition.
Operation .t.
c = .t. a
(where a
and c
are tens3d
objects) :
c%xx = a%xx
c%xy = a%yx
c%xz = a%zx
c%yx = a%xy
c%yy = a%yy
c%yz = a%zy
c%zx = a%xz
c%zy = a%yz
c%zz = a%zz