1
# ifndef _RHEO_TENSOR_H
2
# define _RHEO_TENSOR_H
4
/// This file is part of Rheolef.
6
/// Copyright (C) 2000-2009 Pierre Saramito <Pierre.Saramito@imag.fr>
8
/// Rheolef is free software; you can redistribute it and/or modify
9
/// it under the terms of the GNU General Public License as published by
10
/// the Free Software Foundation; either version 2 of the License, or
11
/// (at your option) any later version.
13
/// Rheolef is distributed in the hope that it will be useful,
14
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
15
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
/// GNU General Public License for more details.
18
/// You should have received a copy of the GNU General Public License
19
/// along with Rheolef; if not, write to the Free Software
20
/// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22
/// =========================================================================
25
NAME: @code{tensor} - a N*N tensor, N=1,2,3
32
The @code{tensor} class defines a 3*3 tensor, as the value of
33
a tensorial valued field. Basic algebra with scalars, vectors
34
of R^3 (i.e. the @code{point} class) and @code{tensor} objects
36
AUTHOR: Pierre.Saramito@imag.fr
41
#include "rheolef/point.h"
48
typedef size_t size_type;
49
typedef Float element_type;
53
tensor (const Float& init_val = 0);
54
tensor (Float x[3][3]);
55
tensor (const tensor& a);
59
tensor& operator = (const tensor& a);
60
tensor& operator = (const Float& val);
64
void fill (const Float& init_val);
66
void set_row (const point& r, size_t i, size_t d = 3);
67
void set_column (const point& c, size_t j, size_t d = 3);
71
Float& operator()(size_type i, size_type j);
72
Float operator()(size_type i, size_type j) const;
73
point row(size_type i) const;
74
point col(size_type i) const;
75
size_t nrow() const; // = 3, for template matrix compatibility
80
friend std::istream& operator >> (std::istream& in, tensor& a);
81
friend std::ostream& operator << (std::ostream& out, const tensor& a);
82
std::ostream& put (std::ostream& s, size_type d = 3) const;
86
friend bool operator == (const tensor&, const tensor&);
87
friend tensor operator - (const tensor&);
88
friend tensor operator + (const tensor&, const tensor&);
89
friend tensor operator - (const tensor&, const tensor&);
90
friend tensor operator * (Float, const tensor&);
91
friend tensor operator * (const tensor& a, Float k);
92
friend tensor operator / (const tensor& a, Float k);
93
friend point operator * (const tensor& a, const point& x);
94
friend point operator * (const point& yt, const tensor& a);
95
point trans_mult (const point& x) const;
96
friend tensor trans (const tensor& a, size_type d = 3);
97
friend tensor operator * (const tensor& a, const tensor& b);
98
friend tensor inv (const tensor& a, size_type d = 3);
99
friend tensor diag (const point& d);
101
// metric and geometric transformations:
103
friend Float dotdot (const tensor&, const tensor&);
104
friend Float norm2 (const tensor& a) { return dotdot(a,a); }
105
friend Float dist2 (const tensor& a, const tensor& b) { return norm2(a-b); }
106
friend Float norm (const tensor& a) { return ::sqrt(norm2(a)); }
107
friend Float dist (const tensor& a, const tensor& b) { return norm(a-b); }
108
Float determinant (size_type d = 3) const;
109
friend Float determinant (const tensor& A, size_t d = 3);
110
friend bool invert_3x3 (const tensor& A, tensor& result);
113
// eigenvalues & eigenvectors:
115
// a may be symmetric
116
// where q=(q1,q2,q3) are eigenvectors in rows (othonormal matrix)
117
// and d=(d1,d2,d3) are eigenvalues, sorted in decreasing order d1 >= d2 >= d3
119
point eig (tensor& q, size_t dim = 3) const;
120
point eig (size_t dim = 3) const;
122
// singular value decomposition:
124
// a can be unsymmetric
125
// where u=(u1,u2,u3) are left pseudo-eigenvectors in rows (othonormal matrix)
126
// v=(v1,v2,v3) are right pseudo-eigenvectors in rows (othonormal matrix)
127
// and s=(s1,s2,s3) are eigenvalues, sorted in decreasing order s1 >= s2 >= s3
129
point svd (tensor& u, tensor& v, size_t dim = 3) const;
134
std::istream& get (std::istream&);
137
tensor otimes (const point& a, const point& b, size_t na = 3);
139
void cumul_otimes (tensor& t, const point& a, const point& b, size_t na = 3);
140
void cumul_otimes (tensor& t, const point& a, const point& b, size_t na, size_t nb);
143
// -----------------------------------------------------------------------
145
// -----------------------------------------------------------------------
149
tensor::fill (const Float& init_val)
151
for (size_type i = 0; i < 3; i++) for (size_type j = 0; j < 3; j++)
161
tensor::tensor (const Float& init_val)
166
tensor::tensor (Float x[3][3])
168
for (size_type i = 0; i < 3; i++) for (size_type j = 0; j < 3; j++)
172
tensor::tensor (const tensor& a)
174
for (size_type i = 0; i < 3; i++) for (size_type j = 0; j < 3; j++)
175
_x[i][j] = a._x[i][j];
179
tensor::operator = (const tensor& a)
181
for (size_type i = 0; i < 3; i++) for (size_type j = 0; j < 3; j++)
182
_x[i][j] = a._x[i][j];
187
tensor::operator = (const Float& val)
189
for (size_type i = 0; i < 3; i++) for (size_type j = 0; j < 3; j++)
207
tensor::operator()(size_type i, size_type j)
213
tensor::operator()(size_type i, size_type j) const
218
std::istream& operator >> (std::istream& in, tensor& a)
223
std::ostream& operator << (std::ostream& out, const tensor& a)
229
operator * (const tensor& a, Float k)
235
operator / (const tensor& a, Float k)
241
tensor::trans_mult (const point& x) const
247
cumul_otimes (tensor& t, const point& a, const point& b, size_t n)
249
cumul_otimes (t, a, b, n, n);
253
otimes (const point& a, const point& b, size_t n)
256
cumul_otimes (t, a, b, n, n);
261
determinant (const tensor& A, size_t d)
263
return A.determinant (d);
267
diag (const point& d)
277
tensor::set_column (const point& c, size_t j, size_t d)
279
for (size_t i = 0; i < d; i++)
280
operator()(i,j) = c[i];
284
tensor::set_row (const point& r, size_t i, size_t d)
286
for (size_t j = 0; j < d; j++)
287
operator()(i,j) = r[j];
289
}// namespace rheolef
290
# endif /* _RHEO_TENSOR_H */