fmatvec  0.0.0
Fast Matrix/Vector Library
linear_algebra.h
1 /* Copyright (C) 2003-2005 Martin Förg, Rober Huber
2 
3  * This library is free software; you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public
5  * License as published by the Free Software Foundation; either
6  * version 2.1 of the License, or (at your option) any later version.
7  *
8  * This library is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  * Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with this library; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16  *
17  * Contact:
18  * martin.o.foerg@googlemail.com
19  *
20  */
21 
22 #ifndef linear_algebra_h
23 #define linear_algebra_h
24 
25 #include "square_matrix.h"
26 #include "vector.h"
27 #include "row_vector.h"
28 #include "fixed_square_matrix.h"
29 #include "fixed_vector.h"
30 #include "fixed_row_vector.h"
31 #include "fixed_symmetric_matrix.h"
32 #include "var_square_matrix.h"
33 #include "var_symmetric_matrix.h"
34 #include "var_vector.h"
35 #include "var_row_vector.h"
36 #include "var_fixed_general_matrix.h"
37 #include "fixed_var_general_matrix.h"
38 #include <cmath>
39 
40 namespace fmatvec {
41 
42  // template declaration for complex type defined in linear_algebra_complex.h
43  template<class T> std::complex<T> operator+(const std::complex<T> &x, int y);
44  template<class T> std::complex<T> operator-(const std::complex<T> &x, int y);
45  template<class T> std::complex<T> operator*(const std::complex<T> &x, int y);
46  template<class T> std::complex<T> operator/(const std::complex<T> &x, int y);
47  template<class T> std::complex<T> operator+(int x, const std::complex<T> &y);
48  template<class T> std::complex<T> operator-(int x, const std::complex<T> &y);
49  template<class T> std::complex<T> operator*(int x, const std::complex<T> &y);
50  template<class T> std::complex<T> operator/(int x, const std::complex<T> &y);
51 
53 
54  // Vector-Vector
55  template <class Row1, class Row2, class Row3, class AT1, class AT2, class AT3>
56  inline void add(const Vector<Row1, AT1> &a1, const Vector<Row2, AT2> &a2, Vector<Row3, AT3> &a3) {
57  FMATVEC_ASSERT(a1.size() == a2.size(), AT3);
58  for (int i = 0; i < a1.size(); i++)
59  a3.e(i) = a1.e(i) + a2.e(i);
60  }
61 
62  template <class Row1, class Row2, class AT>
63  inline void add(Vector<Row1, AT> &a1, const Vector<Row2, AT> &a2) {
64  FMATVEC_ASSERT(a1.size() == a2.size(), AT);
65  for (int i = 0; i < a1.size(); i++)
66  a1.e(i) += a2.e(i);
67  }
68 
69  template <class Row1, class Row2, class Row3, class AT1, class AT2>
70  inline void sub(const Vector<Row1, AT1> &a1, const Vector<Row2, AT2> &a2, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &a3) {
71  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
72  for (int i = 0; i < a1.size(); i++)
73  a3.e(i) = a1.e(i) - a2.e(i);
74  }
75 
76  template <class Row1, class Row2, class AT1, class AT2>
77  inline void sub(Vector<Row1, AT1> &a1, const Vector<Row2, AT2> &a2) {
78  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
79  for (int i = 0; i < a1.size(); i++)
80  a1.e(i) -= a2.e(i);
81  }
82 
83  // Addition
84  template <class AT1, class AT2, class Row1, class Row2>
85  inline Vector<Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Row1, AT1> &a, const Vector<Row2, AT2> &b) {
86  Vector<Var, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
87  add(a, b, c);
88  return c;
89  }
90  template <class AT1, class AT2, class Row>
91  inline Vector<Row, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Row, AT1> &a, const Vector<Row, AT2> &b) {
92  Vector<Row, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
93  add(a, b, c);
94  return c;
95  }
96  template <class AT1, class AT2, int M, class Row>
97  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Fixed<M>, AT1> &a, const Vector<Row, AT2> &b) {
98  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
99  add(a, b, c);
100  return c;
101  }
102  template <class AT1, class AT2, int M>
103  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Fixed<M>, AT1> &a, const Vector<Fixed<M>, AT2> &b) {
104  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
105  add(a, b, c);
106  return c;
107  }
108  template <class AT, class Row1, class Row2>
109  inline Vector<Row1, AT> operator+=(Vector<Row1, AT> &a, const Vector<Row2, AT> &b) {
110  add(a, b);
111  return a;
112  }
113 
115  template <class AT1, class AT2, class Row1, class Row2>
118  sub(a, b, c);
119  return c;
120  }
121  template <class AT1, class AT2, class Row>
122  inline Vector<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Row, AT1> &a, const Vector<Row, AT2> &b) {
123  Vector<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
124  sub(a, b, c);
125  return c;
126  }
127  template <class AT1, class AT2, int M, class Row2>
128  inline Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Fixed<M>, AT1> &a, const Vector<Row2, AT2> &b) {
129  Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
130  sub(a, b, c);
131  return c;
132  }
133  template <class AT1, class AT2, int M>
134  inline Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Fixed<M>, AT1> &a, const Vector<Fixed<M>, AT2> &b) {
135  Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
136  sub(a, b, c);
137  return c;
138  }
139  template <class AT, class Row1, class Row2>
140  inline Vector<Row1, AT> operator-=(Vector<Row1, AT> &a, const Vector<Row2, AT> &b) {
141  sub(a, b);
142  return a;
143  }
144 
145  // RowVector-Vector
146  template <class Col1, class Col2, class Col3, class AT>
147  inline void add(const RowVector<Col1, AT> &a1, const RowVector<Col2, AT> &a2, RowVector<Col3, AT> &a3) {
148  FMATVEC_ASSERT(a1.size() == a2.size(), AT);
149  for (int i = 0; i < a1.size(); i++)
150  a3.e(i) = a1.e(i) + a2.e(i);
151  }
152 
153  template <class Col1, class Col2, class AT>
154  inline void add(RowVector<Col1, AT> &a1, const RowVector<Col2, AT> &a2) {
155  FMATVEC_ASSERT(a1.size() == a2.size(), AT);
156  for (int i = 0; i < a1.size(); i++)
157  a1.e(i) += a2.e(i);
158  }
159 
160  template <class Col1, class Col2, class Col3, class AT1, class AT2>
161  inline void sub(const RowVector<Col1, AT1> &a1, const RowVector<Col2, AT2> &a2, RowVector<Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &a3) {
162  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
163  for (int i = 0; i < a1.size(); i++)
164  a3.e(i) = a1.e(i) - a2.e(i);
165  }
166 
167  template <class Col1, class Col2, class AT1, class AT2>
168  inline void sub(RowVector<Col1, AT1> &a1, const RowVector<Col2, AT2> &a2) {
169  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
170  for (int i = 0; i < a1.size(); i++)
171  a1.e(i) -= a2.e(i);
172  }
173 
174  // Addition
175  template <class AT1, class AT2, class Col1, class Col2>
176  inline RowVector<Var, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Col1, AT1> &a, const RowVector<Col2, AT2> &b) {
177  RowVector<Var, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
178  add(a, b, c);
179  return c;
180  }
181  template <class AT1, class AT2, class Col>
182  inline RowVector<Col, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Col, AT1> &a, const RowVector<Col, AT2> &b) {
183  RowVector<Col, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
184  add(a, b, c);
185  return c;
186  }
187  template <class AT1, class AT2, int N, class Col2>
188  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Fixed<N>, AT1> &a, const RowVector<Col2, AT2> &b) {
189  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
190  add(a, b, c);
191  return c;
192  }
193  template <class AT1, class AT2, int N>
194  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Fixed<N>, AT1> &a, const RowVector<Fixed<N>, AT2> &b) {
195  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
196  add(a, b, c);
197  return c;
198  }
199  template <class AT, class Col1, class Col2>
200  inline RowVector<Col1, AT> operator+=(RowVector<Col1, AT> &a, const RowVector<Col2, AT> &b) {
201  add(a, b);
202  return a;
203  }
204 
206  template <class AT1, class AT2, class Col1, class Col2>
209  sub(a, b, c);
210  return c;
211  }
212  template <class AT1, class AT2, class Col>
213  inline RowVector<Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Col, AT1> &a, const RowVector<Col, AT2> &b) {
214  RowVector<Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
215  sub(a, b, c);
216  return c;
217  }
218  template <class AT1, class AT2, int N, class Col2>
219  inline RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Fixed<N>, AT1> &a, const RowVector<Col2, AT2> &b) {
220  RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
221  sub(a, b, c);
222  return c;
223  }
224  template <class AT1, class AT2, int N>
225  inline RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Fixed<N>, AT1> &a, const RowVector<Fixed<N>, AT2> &b) {
226  RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
227  sub(a, b, c);
228  return c;
229  }
230  template <class AT, class Col1, class Col2>
231  inline RowVector<Col1, AT> operator-=(RowVector<Col1, AT> &a, const RowVector<Col2, AT> &b) {
232  sub(a, b);
233  return a;
234  }
235 
236  // Matrix-Matrix
237  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2, class AT3>
238  inline void add(const Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, AT3> &A3) {
239  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT3);
240  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT3);
241  for (int i = 0; i < A1.rows(); i++)
242  for (int j = 0; j < A2.cols(); j++)
243  A3.e(i, j) = A1.e(i, j) + A2.e(i, j);
244  }
245 
246  template <class Row1, class Row2, class Row3, class AT>
247  inline void add(const Matrix<Symmetric, Row1, Row1, AT> &A1, const Matrix<Symmetric, Row2, Row2, AT> &A2, Matrix<Symmetric, Row3, Row3, AT> &A3) {
248  FMATVEC_ASSERT(A1.size() == A2.size(), AT);
249  for (int i = 0; i < A1.size(); i++)
250  for (int j = i; j < A2.size(); j++)
251  A3.ej(i, j) = A1.ej(i, j) + A2.ej(i, j);
252  }
253 
254  template <class Row1, class Row2, class Row3, class AT>
255  inline void add(const Matrix<Diagonal, Row1, Row1, AT> &A1, const Matrix<Diagonal, Row2, Row2, AT> &A2, Matrix<Diagonal, Row3, Row3, AT> &A3) {
256  FMATVEC_ASSERT(A1.size() == A2.size(), AT);
257  for (int i = 0; i < A3.size(); i++)
258  A3.e(i) = A1.e(i) + A2.e(i);
259  }
260 
261  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class AT>
262  inline void add(Matrix<Type1, Row1, Col1, AT> &A1, const Matrix<Type2, Row2, Col2, AT> &A2) {
263  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT);
264  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT);
265  for (int i = 0; i < A1.rows(); i++)
266  for (int j = 0; j < A2.cols(); j++)
267  A1.e(i, j) += A2.e(i, j);
268  }
269 
270  template <class Row1, class Row2, class AT>
271  inline void add(Matrix<Symmetric, Row1, Row1, AT> &A1, const Matrix<Symmetric, Row2, Row2, AT> &A2) {
272  FMATVEC_ASSERT(A1.size() == A2.size(), AT);
273  for (int i = 0; i < A1.size(); i++)
274  for (int j = i; j < A2.size(); j++)
275  A1.ej(i, j) += A2.ej(i, j);
276  }
277 
278  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
279  inline void sub(const Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
280  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT2);
281  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT2);
282  for (int i = 0; i < A1.rows(); i++)
283  for (int j = 0; j < A2.cols(); j++)
284  A3.e(i, j) = A1.e(i, j) - A2.e(i, j);
285  }
286 
287  template <class Row1, class Row2, class Row3, class AT1, class AT2>
288  inline void sub(const Matrix<Symmetric, Row1, Row1, AT1> &A1, const Matrix<Symmetric, Row2, Row2, AT2> &A2, Matrix<Symmetric, Row3, Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
289  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
290  for (int i = 0; i < A1.size(); i++)
291  for (int j = i; j < A2.size(); j++)
292  A3.ej(i, j) = A1.ej(i, j) - A2.ej(i, j);
293  }
294 
295  template <class Row1, class Row2, class Row3, class AT1, class AT2>
296  inline void sub(const Matrix<Diagonal, Row1, Row1, AT1> &A1, const Matrix<Diagonal, Row2, Row2, AT2> &A2, Matrix<Diagonal, Row3, Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
297  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
298  for (int i = 0; i < A3.size(); i++)
299  A3.e(i) = A1.e(i) - A2.e(i);
300  }
301 
302  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class AT1, class AT2>
303  inline void sub(Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2) {
304  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT2);
305  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT2);
306  for (int i = 0; i < A1.rows(); i++)
307  for (int j = 0; j < A2.cols(); j++)
308  A1.e(i, j) -= A2.e(i, j);
309  }
310 
311  template <class Row1, class Row2, class AT1, class AT2>
312  inline void sub(Matrix<Symmetric, Row1, Row1, AT1> &A1, const Matrix<Symmetric, Row2, Row2, AT2> &A2) {
313  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
314  for (int i = 0; i < A1.size(); i++)
315  for (int j = i; j < A2.size(); j++)
316  A1.ej(i, j) -= A2.ej(i, j);
317  }
318 
319  // Addition
320  // Type1 Type2
321  template <class AT1, class AT2, class Type1, class Type2, class Row1, class Col1, class Row2, class Col2>
322  inline Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Row1, Col1, AT1> &A, const Matrix<Type2, Row2, Col2, AT2> &B) {
323  Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
324  add(A, B, C);
325  return C;
326  }
327  template <class AT1, class AT2, class Type1, class Type2, class Row, class Col>
328  inline Matrix<General, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Row, Col, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
329  Matrix<General, Row, Col, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
330  add(A, B, C);
331  return C;
332  }
333  template <class AT1, class AT2, int M, class Type1, class Type2, class Row, class Col>
334  inline Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Fixed<M>, Var, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
335  Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
336  add(A, B, C);
337  return C;
338  }
339  template <class AT1, class AT2, int M, class Type1, class Type2>
340  inline Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Fixed<M>, Var, AT1> &A, const Matrix<Type2, Fixed<M>, Var, AT2> &B) {
341  Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
342  add(A, B, C);
343  return C;
344  }
345  template <class AT1, class AT2, int N, class Type1, class Type2, class Row, class Col>
346  inline Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Var, Fixed<N>, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
347  Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
348  add(A, B, C);
349  return C;
350  }
351  template <class AT1, class AT2, int N, class Type1, class Type2>
352  inline Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Var, Fixed<N>, AT1> &A, const Matrix<Type2, Var, Fixed<N>, AT2> &B) {
353  Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
354  add(A, B, C);
355  return C;
356  }
357  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Row, class Col>
358  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
359  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
360  add(A, B, C);
361  return C;
362  }
363  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
364  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Fixed<M>, Var, AT2> &B) {
365  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
366  add(A, B, C);
367  return C;
368  }
369  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
370  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Var, Fixed<N>, AT2> &B) {
371  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
372  add(A, B, C);
373  return C;
374  }
375  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
376  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Fixed<M>, Fixed<N>, AT2> &B) {
377  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
378  add(A, B, C);
379  return C;
380  }
381  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Row, class Col>
382  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type2, Row, Col, AT1> &A, const Matrix<Type1, Fixed<M>, Fixed<N>, AT2> &B) {
383  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
384  add(A, B, C);
385  return C;
386  }
387  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
388  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type2, Fixed<M>, Var, AT1> &A, const Matrix<Type1, Fixed<M>, Fixed<N>, AT2> &B) {
389  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
390  add(A, B, C);
391  return C;
392  }
393  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
394  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type2, Var, Fixed<N>, AT1> &A, const Matrix<Type1, Fixed<M>, Fixed<N>, AT2> &B) {
395  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
396  add(A, B, C);
397  return C;
398  }
399  // Type
400  template <class AT1, class AT2, class Type, class Row1, class Col1, class Row2, class Col2>
401  inline Matrix<Type, Var, Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Row1, Col1, AT1> &A, const Matrix<Type, Row2, Col2, AT2> &B) {
402  Matrix<Type, Var, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
403  add(A, B, C);
404  return C;
405  }
406  template <class AT1, class AT2, class Type, class Row, class Col>
407  inline Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Row, Col, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
408  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
409  add(A, B, C);
410  return C;
411  }
412  template <class AT1, class AT2, int M, class Type, class Row, class Col>
413  inline Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Var, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
414  Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
415  add(A, B, C);
416  return C;
417  }
418  template <class AT1, class AT2, int M, class Type>
419  inline Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Var, AT1> &A, const Matrix<Type, Fixed<M>, Var, AT2> &B) {
420  Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
421  add(A, B, C);
422  return C;
423  }
424  template <class AT1, class AT2, int N, class Type, class Row, class Col>
425  inline Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Var, Fixed<N>, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
426  Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
427  add(A, B, C);
428  return C;
429  }
430  template <class AT1, class AT2, int N, class Type>
431  inline Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Var, Fixed<N>, AT1> &A, const Matrix<Type, Var, Fixed<N>, AT2> &B) {
432  Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
433  add(A, B, C);
434  return C;
435  }
436  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
437  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
438  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
439  add(A, B, C);
440  return C;
441  }
442  template <class AT1, class AT2, int M, int N, class Type>
443  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Fixed<M>, Var, AT2> &B) {
444  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
445  add(A, B, C);
446  return C;
447  }
448  template <class AT1, class AT2, int M, int N, class Type>
449  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Var, Fixed<N>, AT2> &B) {
450  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
451  add(A, B, C);
452  return C;
453  }
454  template <class AT1, class AT2, int M, int N, class Type>
455  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
456  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
457  add(A, B, C);
458  return C;
459  }
460  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
461  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Row, Col, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
462  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
463  add(A, B, C);
464  return C;
465  }
466  template <class AT1, class AT2, int M, int N, class Type>
467  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Fixed<M>, Var, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
468  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
469  add(A, B, C);
470  return C;
471  }
472  template <class AT1, class AT2, int M, int N, class Type>
473  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const Matrix<Type, Var, Fixed<N>, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
474  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
475  add(A, B, C);
476  return C;
477  }
478  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
479  inline Matrix<Type1, Row1, Col1, AT>& operator+=(Matrix<Type1, Row1, Col1, AT> &A, const Matrix<Type2, Row2, Col2, AT> &B) {
480  add(A, B);
481  return A;
482  }
483 
484  // Subtraction
485  // Type1 Type2
486  template <class AT1, class AT2, class Type1, class Type2, class Row1, class Col1, class Row2, class Col2>
487  inline Matrix<General, Var, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Row1, Col1, AT1> &A, const Matrix<Type2, Row2, Col2, AT2> &B) {
488  Matrix<General, Var, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
489  sub(A, B, C);
490  return C;
491  }
492  template <class AT1, class AT2, class Type1, class Type2, class Row, class Col>
493  inline Matrix<General, Row, Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Row, Col, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
494  Matrix<General, Row, Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
495  sub(A, B, C);
496  return C;
497  }
498  template <class AT1, class AT2, int M, class Type1, class Type2, class Row, class Col>
499  inline Matrix<General, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Fixed<M>, Var, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
500  Matrix<General, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
501  sub(A, B, C);
502  return C;
503  }
504  template <class AT1, class AT2, int M, class Type1, class Type2>
505  inline Matrix<General, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Fixed<M>, Var, AT1> &A, const Matrix<Type2, Fixed<M>, Var, AT2> &B) {
506  Matrix<General, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
507  sub(A, B, C);
508  return C;
509  }
510  template <class AT1, class AT2, int N, class Type1, class Type2, class Row, class Col>
511  inline Matrix<General, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Var, Fixed<N>, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
512  Matrix<General, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
513  sub(A, B, C);
514  return C;
515  }
516  template <class AT1, class AT2, int N, class Type1, class Type2>
517  inline Matrix<General, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Var, Fixed<N>, AT1> &A, const Matrix<Type2, Var, Fixed<N>, AT2> &B) {
518  Matrix<General, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
519  sub(A, B, C);
520  return C;
521  }
522  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Row, class Col>
523  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Row, Col, AT2> &B) {
524  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
525  sub(A, B, C);
526  return C;
527  }
528  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
529  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Fixed<M>, Var, AT2> &B) {
530  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
531  sub(A, B, C);
532  return C;
533  }
534  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
535  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Var, Fixed<N>, AT2> &B) {
536  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
537  sub(A, B, C);
538  return C;
539  }
540  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
541  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type1, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type2, Fixed<M>, Fixed<N>, AT2> &B) {
542  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
543  sub(A, B, C);
544  return C;
545  }
546  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Row, class Col>
547  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type2, Row, Col, AT1> &A, const Matrix<Type1, Fixed<M>, Fixed<N>, AT2> &B) {
548  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
549  sub(A, B, C);
550  return C;
551  }
552  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
553  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type2, Fixed<M>, Var, AT1> &A, const Matrix<Type1, Fixed<M>, Fixed<N>, AT2> &B) {
554  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
555  sub(A, B, C);
556  return C;
557  }
558  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
559  inline Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type2, Var, Fixed<N>, AT1> &A, const Matrix<Type1, Fixed<M>, Fixed<N>, AT2> &B) {
560  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
561  sub(A, B, C);
562  return C;
563  }
564  // Type
565  template <class AT1, class AT2, class Type, class Row1, class Col1, class Row2, class Col2>
566  inline Matrix<Type, Var, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Row1, Col1, AT1> &A, const Matrix<Type, Row2, Col2, AT2> &B) {
567  Matrix<Type, Var, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
568  sub(A, B, C);
569  return C;
570  }
571  template <class AT1, class AT2, class Type, class Row, class Col>
572  inline Matrix<Type, Row, Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Row, Col, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
573  Matrix<Type, Row, Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
574  sub(A, B, C);
575  return C;
576  }
577  template <class AT1, class AT2, int M, class Type, class Row, class Col>
578  inline Matrix<Type, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Var, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
579  Matrix<Type, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
580  sub(A, B, C);
581  return C;
582  }
583  template <class AT1, class AT2, int M, class Type>
584  inline Matrix<Type, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Var, AT1> &A, const Matrix<Type, Fixed<M>, Var, AT2> &B) {
585  Matrix<Type, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
586  sub(A, B, C);
587  return C;
588  }
589  template <class AT1, class AT2, int N, class Type, class Row, class Col>
590  inline Matrix<Type, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Var, Fixed<N>, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
591  Matrix<Type, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
592  sub(A, B, C);
593  return C;
594  }
595  template <class AT1, class AT2, int N, class Type>
596  inline Matrix<Type, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Var, Fixed<N>, AT1> &A, const Matrix<Type, Var, Fixed<N>, AT2> &B) {
597  Matrix<Type, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
598  sub(A, B, C);
599  return C;
600  }
601  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
602  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Row, Col, AT2> &B) {
603  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
604  sub(A, B, C);
605  return C;
606  }
607  template <class AT1, class AT2, int M, int N, class Type>
608  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Fixed<M>, Var, AT2> &B) {
609  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
610  sub(A, B, C);
611  return C;
612  }
613  template <class AT1, class AT2, int M, int N, class Type>
614  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Var, Fixed<N>, AT2> &B) {
615  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
616  sub(A, B, C);
617  return C;
618  }
619  template <class AT1, class AT2, int M, int N, class Type>
620  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
621  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
622  sub(A, B, C);
623  return C;
624  }
625  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
626  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Row, Col, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
627  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
628  sub(A, B, C);
629  return C;
630  }
631  template <class AT1, class AT2, int M, int N, class Type>
632  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Fixed<M>, Var, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
633  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
634  sub(A, B, C);
635  return C;
636  }
637  template <class AT1, class AT2, int M, int N, class Type>
638  inline Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Matrix<Type, Var, Fixed<N>, AT1> &A, const Matrix<Type, Fixed<M>, Fixed<N>, AT2> &B) {
639  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
640  sub(A, B, C);
641  return C;
642  }
643  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
644  inline Matrix<Type1, Row1, Col1, AT>& operator-=(Matrix<Type1, Row1, Col1, AT> &A, const Matrix<Type2, Row2, Col2, AT> &B) {
645  sub(A, B);
646  return A;
647  }
648 
649 // SquareMatrix-SquareMatrix
650  template <class AT1, class AT2, class Row1, class Row2>
651  inline SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> operator+(const SquareMatrix<Row1, AT1> &A1, const SquareMatrix<Row2, AT2> &A2) {
652  SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
653  add(A1, A2, A3);
654  return A3;
655  }
656  template <class AT1, class AT2, class Row>
657  inline SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator+(const SquareMatrix<Row, AT1> &A1, const SquareMatrix<Row, AT2> &A2) {
658  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
659  add(A1, A2, A3);
660  return A3;
661  }
662 
663 // SquareMatrix-SquareMatrix
664  template <class AT1, class AT2, class Row1, class Row2>
665  inline SquareMatrix<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const SquareMatrix<Row1, AT1> &A1, const SquareMatrix<Row2, AT2> &A2) {
666  SquareMatrix<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
667  sub(A1, A2, A3);
668  return A3;
669  }
670  template <class AT1, class AT2, class Row>
671  inline SquareMatrix<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const SquareMatrix<Row, AT1> &A1, const SquareMatrix<Row, AT2> &A2) {
672  SquareMatrix<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
673  sub(A1, A2, A3);
674  return A3;
675  }
676 
682  template <class Type1, class Row1, class Col1, class Row2, class Row3, class AT1, class AT2>
683  inline void mult(const Matrix<Type1, Row1, Col1, AT1> &A, const Vector<Row2, AT2> &x, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
684  FMATVEC_ASSERT(A.cols() == x.size(), AT2);
685  for (int i = 0; i < y.size(); i++) {
686  y.e(i) = 0;
687  for (int j = 0; j < x.size(); j++)
688  y.e(i) += A.e(i, j) * x.e(j);
689  }
690  }
691 
692  template <class Row1, class Row2, class Row3, class AT1, class AT2>
693  inline void mult(const Matrix<Symmetric, Row1, Row1, AT1> &A, const Vector<Row2, AT2> &x, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
694  FMATVEC_ASSERT(A.cols() == x.size(), AT2);
695  for (int i = 0; i < y.size(); i++) {
696  y.e(i) = 0;
697  for (int j = 0; j < i; j++)
698  y.e(i) += A.ei(i, j) * x.e(j);
699  for (int j = i; j < x.size(); j++)
700  y.e(i) += A.ej(i, j) * x.e(j);
701  }
702  }
703 
704  // Matrix-Vector
705  template <class AT1, class AT2, class Type1, class Row1, class Col1, class Row2>
706  inline Vector<Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row1, Col1, AT1> &A, const Vector<Row2, AT2> &x) {
707  Vector<Var, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
708  mult(A, x, y);
709  return y;
710  }
711  template <class AT1, class AT2, class Type1, class Row, class Col>
712  inline Vector<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row, Col, AT1> &A, const Vector<Row, AT2> &x) {
713  Vector<Row, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
714  mult(A, x, y);
715  return y;
716  }
717  template <class AT1, class AT2, int M, class Type1, class Col1, class Row2>
718  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Fixed<M>, Col1, AT1> &A, const Vector<Row2, AT2> &x) {
719  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
720  mult(A, x, y);
721  return y;
722  }
723  template <class AT1, class AT2, int M, class Type1>
724  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Fixed<M>, Fixed<M>, AT1> &A, const Vector<Fixed<M>, AT2> &x) {
725  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
726  mult(A, x, y);
727  return y;
728  }
729  template <class AT1, class AT2, int M, int N, class Type, class Row2>
730  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Vector<Row2, AT2> &x) {
731  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
732  mult(A, x, y);
733  return y;
734  }
735  template <class AT1, class AT2, int M, int N, class Type>
736  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Vector<Fixed<N>, AT2> &x) {
737  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
738  mult(A, x, y);
739  return y;
740  }
741 
742  // RowVector-Matrix
743  template <class Col1, class Type2, class Row2, class Col2, class Type3, class Col3, class AT1, class AT2>
744  inline void mult(const RowVector<Col1, AT1> &x, const Matrix<Type2, Row2, Col2, AT2> &A, RowVector<Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
745  FMATVEC_ASSERT(x.size() == A.rows(), AT2);
746  for (int i = 0; i < y.size(); i++) {
747  y.e(i) = 0;
748  for (int j = 0; j < x.size(); j++)
749  y.e(i) += x.e(j) * A.e(j, i);
750  }
751  }
752 
753  template <class Col1, class Row2, class Col3, class AT1, class AT2>
754  inline void mult(const RowVector<Col1, AT1> &x, const Matrix<Symmetric, Row2, Row2, AT2> &A, RowVector<Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
755  FMATVEC_ASSERT(x.size() == A.rows(), AT2);
756  for (int i = 0; i < y.size(); i++) {
757  y.e(i) = 0;
758  for (int j = 0; j < i; j++)
759  y.e(i) += x.e(j) * A.ej(j, i);
760  for (int j = i; j < x.size(); j++)
761  y.e(i) += x.e(j) * A.ei(j, i);
762  }
763  }
764 
765  template <class AT1, class AT2, class Type2, class Col2, class Row1, class Col1>
766  inline RowVector<Var, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Col2, AT1> &x, const Matrix<Type2, Row1, Col1, AT2> &A) {
767  RowVector<Var, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
768  mult(x, A, y);
769  return y;
770  }
771  template <class AT1, class AT2, class Type2, class Row, class Col>
772  inline RowVector<Col, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Col, AT1> &x, const Matrix<Type2, Row, Col, AT2> &A) {
773  RowVector<Col, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
774  mult(x, A, y);
775  return y;
776  }
777  template <class AT1, class AT2, int N, class Type2, class Col1, class Row2>
778  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Col1, AT1> &x, const Matrix<Type2, Row2, Fixed<N>, AT2> &A) {
779  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
780  mult(x, A, y);
781  return y;
782  }
783  template <class AT1, class AT2, int N, class Type2>
784  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Fixed<N>, AT1> &x, const Matrix<Type2, Fixed<N>, Fixed<N>, AT2> &A) {
785  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
786  mult(x, A, y);
787  return y;
788  }
789 
790  // Matrix-Matrix
791  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
792  inline void mult(const Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
793  FMATVEC_ASSERT(A1.cols() == A2.rows(), AT2);
794  for (int i = 0; i < A3.rows(); i++) {
795  for (int k = 0; k < A3.cols(); k++) {
796  A3.e(i, k) = 0;
797  for (int j = 0; j < A1.cols(); j++)
798  A3.e(i, k) += A1.e(i, j) * A2.e(j, k);
799  }
800  }
801  }
802  template <class Row1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
803  inline void mult(const Matrix<Symmetric, Row1, Row1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
804  FMATVEC_ASSERT(A1.size() == A2.rows(), AT2);
805  for (int i = 0; i < A3.rows(); i++) {
806  for (int k = 0; k < A3.cols(); k++) {
807  A3.e(i, k) = 0;
808  for (int j = 0; j < i; j++)
809  A3.e(i, k) += A1.ei(i, j) * A2.e(j, k);
810  for (int j = i; j < A1.cols(); j++)
811  A3.e(i, k) += A1.ej(i, j) * A2.e(j, k);
812  }
813  }
814  }
815  template <class Row1, class Row2, class Row3, class AT1, class AT2>
816  inline void mult(const Matrix<Diagonal, Row1, Row1, AT1> &A1, const Matrix<Diagonal, Row2, Row2, AT2> &A2, Matrix<Diagonal, Row3, Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
817  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
818  for (int i = 0; i < A3.size(); i++)
819  A3.e(i) = A1.e(i) * A2.e(i);
820  }
821 
822  template <class AT1, class AT2, class Type1, class Type2, class Row1, class Col1, class Row2, class Col2>
823  inline Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2) {
824  Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
825  mult(A1, A2, A3);
826  return A3;
827  }
828  template <class AT1, class AT2, class Type1, class Type2, class Row, class Col>
829  inline Matrix<General, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row, Col, AT1> &A1, const Matrix<Type2, Row, Col, AT2> &A2) {
830  Matrix<General, Row, Col, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
831  mult(A1, A2, A3);
832  return A3;
833  }
834  template <class AT1, class AT2, int M, class Type1, class Type2, class Col1, class Row2, class Col2>
835  inline Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Fixed<M>, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2) {
836  Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
837  mult(A1, A2, A3);
838  return A3;
839  }
840  template <class AT1, class AT2, int N, class Type1, class Row1, class Col1, class Type2, class Row2>
841  inline Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Fixed<N>, AT2> &A2) {
842  Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
843  mult(A1, A2, A3);
844  return A3;
845  }
846  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Col1, class Row2>
847  inline Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Fixed<M>, Col1, AT1> &A1, const Matrix<Type2, Row2, Fixed<N>, AT2> &A2) {
848  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
849  mult(A1, A2, A3);
850  return A3;
851  }
852  template <class AT1, class AT2, int M, class Type1, class Type2>
853  inline Matrix<General, Fixed<M>, Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Fixed<M>, Fixed<M>, AT1> &A1, const Matrix<Type2, Fixed<M>, Fixed<M>, AT2> &A2) {
854  Matrix<General, Fixed<M>, Fixed<M>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
855  mult(A1, A2, A3);
856  return A3;
857  }
858  template <class AT1, class AT2, class Type, class Row1, class Col1, class Row2, class Col2>
859  inline Matrix<Type, Var, Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Row1, Col1, AT1> &A1, const Matrix<Type, Row2, Col2, AT2> &A2) {
860  Matrix<Type, Var, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
861  mult(A1, A2, A3);
862  return A3;
863  }
864  template <class AT1, class AT2, class Type, class Row, class Col>
865  inline Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Row, Col, AT1> &A1, const Matrix<Type, Row, Col, AT2> &A2) {
866  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
867  mult(A1, A2, A3);
868  return A3;
869  }
870  template <class AT1, class AT2, int M, class Type, class Col1, class Row2, class Col2>
871  inline Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Fixed<M>, Col1, AT1> &A1, const Matrix<Type, Row2, Col2, AT2> &A2) {
872  Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
873  mult(A1, A2, A3);
874  return A3;
875  }
876  template <class AT1, class AT2, int N, class Type, class Row1, class Col1, class Row2>
877  inline Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Row1, Col1, AT1> &A1, const Matrix<Type, Row2, Fixed<N>, AT2> &A2) {
878  Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
879  mult(A1, A2, A3);
880  return A3;
881  }
882  template <class AT1, class AT2, int M, int N, class Type, class Col1, class Row2>
883  inline Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Fixed<M>, Col1, AT1> &A1, const Matrix<Type, Row2, Fixed<N>, AT2> &A2) {
884  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
885  mult(A1, A2, A3);
886  return A3;
887  }
888  template <class AT1, class AT2, int M, class Type>
889  inline Matrix<Type, Fixed<M>, Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Fixed<M>, Fixed<M>, AT1> &A1, const Matrix<Type, Fixed<M>, Fixed<M>, AT2> &A2) {
890  Matrix<Type, Fixed<M>, Fixed<M>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
891  mult(A1, A2, A3);
892  return A3;
893  }
894 
895 // SquareMatrix-SquareMatrix
896  template <class AT1, class AT2, class Row1, class Row2>
897  inline SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> operator*(const SquareMatrix<Row1, AT1> &A1, const SquareMatrix<Row2, AT2> &A2) {
898  SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
899  mult(A1, A2, A3);
900  return A3;
901  }
902  template <class AT1, class AT2, class Row>
903  inline SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const SquareMatrix<Row, AT1> &A1, const SquareMatrix<Row, AT2> &A2) {
904  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
905  mult(A1, A2, A3);
906  return A3;
907  }
908 
909  // SymmetricMatrix-SymmetricMatrix
910  template <class AT1, class AT2, class Row1, class Row2>
911  inline Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Symmetric, Row1, Row1, AT1> &A1, const Matrix<Symmetric, Row2, Row2, AT2> &A2) {
912  Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
913  mult(A1, A2, A3);
914  return A3;
915  }
916  template <class AT1, class AT2, class Row>
917  inline Matrix<General, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Symmetric, Row, Row, AT1> &A1, const Matrix<Symmetric, Row, Row, AT2> &A2) {
918  Matrix<General, Row, Row, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
919  mult(A1, A2, A3);
920  return A3;
921  }
922 
924 
931  template <class Row, class AT1, class AT2>
933 
935 
936  for (int i = 0; i < x.size(); i++)
937  y.e(i) = x.e(i) * alpha;
938 
939  return y;
940  }
941 
946  template <class Row, class AT1, class AT2>
948 
950 
951  for (int i = 0; i < x.size(); i++)
952  y.e(i) = x.e(i) * alpha;
953 
954  return y;
955  }
956 
957  template <class Row, class AT1, class AT2>
958  inline Vector<Row, AT1> operator*=(Vector<Row, AT1> &x, const AT2 &alpha) {
959  for (int i = 0; i < x.size(); i++)
960  x.e(i) *= alpha;
961  return x;
962  }
963 
964  template <class Row, class AT1, class AT2>
965  inline Vector<Row, typename OperatorResult<AT1, AT2>::Type> operator/(const Vector<Row, AT1> &x, const AT2 &alpha) {
966  Vector<Row, typename OperatorResult<AT1, AT2>::Type> y(x.size(), NONINIT);
967  for (int i = 0; i < x.size(); i++)
968  y.e(i) = x.e(i) / alpha;
969  return y;
970  }
971 
972  template <class Row, class AT1, class AT2>
973  inline Vector<Row, AT1> operator/=(Vector<Row, AT1> &x, const AT2 &alpha) {
974  for (int i = 0; i < x.size(); i++)
975  x.e(i) /= alpha;
976  return x;
977  }
984  template <class Col, class AT1, class AT2>
986 
988 
989  for (int i = 0; i < x.size(); i++)
990  y.e(i) = x.e(i) * alpha;
991 
992  return y;
993  }
994 
999  template <class Col, class AT1, class AT2>
1001 
1003 
1004  for (int i = 0; i < x.size(); i++)
1005  y.e(i) = x.e(i) * alpha;
1006 
1007  return y;
1008  }
1009 
1010  template <class Col, class AT1, class AT2>
1011  inline RowVector<Col, AT1> operator*=(RowVector<Col, AT1> &x, const AT2 &alpha) {
1012  for (int i = 0; i < x.size(); i++)
1013  x.e(i) *= alpha;
1014  return x;
1015  }
1016 
1017  template <class Col, class AT1, class AT2>
1018  inline RowVector<Col, typename OperatorResult<AT1, AT2>::Type> operator/(const RowVector<Col, AT1> &x, const AT2 &a) {
1019  RowVector<Col, typename OperatorResult<AT1, AT2>::Type> y(x.size(), NONINIT);
1020  for (int i = 0; i < x.size(); i++)
1021  y.e(i) = x.e(i) / a;
1022  return y;
1023  }
1024 
1025  template <class Col, class AT1, class AT2>
1026  inline RowVector<Col, AT1> operator/=(RowVector<Col, AT1> &x, const AT2 &a) {
1027  for (int i = 0; i < x.size(); i++)
1028  x.e(i) /= a;
1029  return x;
1030  }
1031 
1033 
1035 
1042  template <class Col, class Row, class AT1, class AT2>
1044 
1045  FMATVEC_ASSERT(x.size() == y.size(), AT2);
1046 
1047  typename OperatorResult<AT1, AT2>::Type res = 0;
1048 
1049  for (int i = 0; i < x.size(); i++)
1050  res += x.e(i) * y.e(i);
1051 
1052  return res;
1053  }
1054 
1060  template <class Row, class AT>
1062 
1063  FMATVEC_ASSERT(x.size() == y.size(), AT);
1064 
1065  AT res = 0;
1066 
1067  for (int i = 0; i < x.size(); i++)
1068  res += x.e(i) * y.e(i);
1069 
1070  return res;
1071  }
1072 
1078  template <class Row1, class Row2, class AT1, class AT2>
1080 
1081  FMATVEC_ASSERT(x.size() == 3, AT2);
1082  FMATVEC_ASSERT(y.size() == 3, AT2);
1083 
1084  Vector<Fixed<3>, typename OperatorResult<AT1, AT2>::Type> z(3, NONINIT);
1085 
1086  z.e(0) = x.e(1) * y.e(2) - x.e(2) * y.e(1);
1087  z.e(1) = x.e(2) * y.e(0) - x.e(0) * y.e(2);
1088  z.e(2) = x.e(0) * y.e(1) - x.e(1) * y.e(0);
1089 
1090  return z;
1091  }
1092 
1098  template <class Row, class AT>
1099  double tripleProduct(const Vector<Row, AT> &a, const Vector<Row, AT> &x, const Vector<Row, AT> &y) {
1100 
1101  FMATVEC_ASSERT(a.size() == 3, AT);
1102  FMATVEC_ASSERT(x.size() == 3, AT);
1103  FMATVEC_ASSERT(y.size() == 3, AT);
1104 
1105  return a.e(0) * (x.e(1) * y.e(2) - x.e(2) * y.e(1)) + a.e(1) * (x.e(2) * y.e(0) - x.e(0) * y.e(2)) + a.e(2) * (x.e(0) * y.e(1) - x.e(1) * y.e(0));
1106  }
1107 
1109  //
1111 
1120  template <class Type, class Row, class Col, class AT1, class AT2>
1122 
1124 
1125  for (int i = 0; i < A.rows(); i++)
1126  for (int j = 0; j < A.cols(); j++)
1127  B.e(i, j) = A.e(i, j) * alpha;
1128 
1129  return B;
1130  }
1131 
1132  template <class Type, class Row, class Col, class AT1, class AT2>
1133  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const Matrix<Type, Row, Col, AT2> &A) {
1134 
1135  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1136 
1137  for (int i = 0; i < A.rows(); i++)
1138  for (int j = 0; j < A.cols(); j++)
1139  B.e(i, j) = A.e(i, j) * alpha;
1140 
1141  return B;
1142  }
1143 
1144  template <class Row, class AT1, class AT2>
1145  inline Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const Matrix<Symmetric, Row, Row, AT2> &A) {
1146  Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1147  for (int i = 0; i < A.size(); i++)
1148  for (int j = i; j < A.size(); j++)
1149  B.ej(i, j) = A.ej(i, j) * alpha;
1150  return B;
1151  }
1152 
1153  template <class Row, class AT1, class AT2>
1154  inline Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1155  Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1156  for (int i = 0; i < A.size(); i++)
1157  for (int j = i; j < A.size(); j++)
1158  B.ej(i, j) = A.ej(i, j) * alpha;
1159  return B;
1160  }
1161 
1162  template <class AT1, class AT2>
1163  inline Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const Matrix<Diagonal, Ref, Ref, AT2> &A) {
1164  Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1165  for (int i = 0; i < A.rows(); i++)
1166  B.e(i) = A.e(i) * alpha;
1167  return B;
1168  }
1169 
1170  template <class AT1, class AT2>
1171  inline Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Diagonal, Ref, Ref, AT1> &A, const AT2 &alpha) {
1172  Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1173  for (int i = 0; i < A.size(); i++)
1174  B.e(i) = A.e(i) * alpha;
1175  return B;
1176  }
1177 
1178  template <class Type, class Row, class Col, class AT1, class AT2>
1179  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator/(const Matrix<Type, Row, Col, AT1> &A, const AT2 &alpha) {
1180 
1181  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1182 
1183  for (int i = 0; i < A.rows(); i++)
1184  for (int j = 0; j < A.cols(); j++)
1185  B.e(i, j) = A.e(i, j) / alpha;
1186 
1187  return B;
1188  }
1189 
1190  template <class Row, class AT1, class AT2>
1191  inline Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator/(const Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1192  Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1193  for (int i = 0; i < A.size(); i++)
1194  for (int j = i; j < A.size(); j++)
1195  B.ej(i, j) = A.ej(i, j) / alpha;
1196  return B;
1197  }
1198 
1199  template <class Row, class AT1, class AT2>
1200  inline Matrix<Diagonal, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator/(const Matrix<Diagonal, Row, Row, AT1> &A, const AT2 &alpha) {
1201  Matrix<Diagonal, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1202  for (int i = 0; i < A.size(); i++)
1203  B.e(i) = A.e(i) / alpha;
1204  return B;
1205  }
1206 
1207  template <class Row, class AT1, class AT2>
1208  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const SquareMatrix<Row, AT1> &A, const AT2 &alpha) {
1209 
1210  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), NONINIT);
1211 
1212  for (int i = 0; i < A.size(); i++)
1213  for (int j = 0; j < A.size(); j++)
1214  B.e(i, j) = A.e(i, j) * alpha;
1215 
1216  return B;
1217  }
1218 
1219  template <class Row, class AT1, class AT2>
1220  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const SquareMatrix<Row, AT2> &A) {
1221 
1222  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), NONINIT);
1223 
1224  for (int i = 0; i < A.size(); i++)
1225  for (int j = 0; j < A.size(); j++)
1226  B.e(i, j) = A.e(i, j) * alpha;
1227 
1228  return B;
1229  }
1230 
1231  template <class Row, class AT1, class AT2>
1232  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator/(const SquareMatrix<Row, AT1> &A, const AT2 &alpha) {
1233 
1234  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), NONINIT);
1235 
1236  for (int i = 0; i < A.size(); i++)
1237  for (int j = 0; j < A.size(); j++)
1238  B.e(i, j) = A.e(i, j) / alpha;
1239 
1240  return B;
1241  }
1242 
1243  template <class Type, class Row, class Col, class AT1, class AT2>
1244  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator*=(Matrix<Type, Row, Col, AT1> &A, const AT2 &alpha) {
1245  for (int i = 0; i < A.rows(); i++)
1246  for (int j = 0; j < A.cols(); j++)
1247  A.e(i, j) *= alpha;
1248 
1249  return A;
1250  }
1251 
1252  template <class Row, class AT1, class AT2>
1253  inline Matrix<Symmetric, Row, Row, AT1> operator*=(Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1254  for (int i = 0; i < A.size(); i++)
1255  for (int j = i; j < A.size(); j++)
1256  A.ej(i, j) *= alpha;
1257  return A;
1258  }
1259 
1260  template <class Row, class AT1, class AT2>
1261  inline Matrix<Diagonal, Row, Row, AT1> operator*=(Matrix<Diagonal, Row, Row, AT1> &A, const AT2 &alpha) {
1262  for (int i = 0; i < A.size(); i++)
1263  A.e(i) *= alpha;
1264  return A;
1265  }
1266 
1267  template <class Type, class Row, class Col, class AT1, class AT2>
1268  Matrix<Type, Row, Col, AT1> operator/=(Matrix<Type, Row, Col, AT1> &A, const AT2 &alpha) {
1269  for (int i = 0; i < A.rows(); i++)
1270  for (int j = 0; j < A.cols(); j++)
1271  A.e(i, j) /= alpha;
1272 
1273  return A;
1274  }
1275 
1276  template <class Row, class AT1, class AT2>
1277  inline Matrix<Symmetric, Row, Row, AT1> operator/=(Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1278  for (int i = 0; i < A.size(); i++)
1279  for (int j = i; j < A.size(); j++)
1280  A.ej(i, j) /= alpha;
1281  return A;
1282  }
1283 
1284  template <class Row, class AT1, class AT2>
1285  inline Matrix<Diagonal, Row, Row, AT1> operator/=(Matrix<Diagonal, Row, Row, AT1> &A, const AT2 &alpha) {
1286  for (int i = 0; i < A.size(); i++)
1287  A.e(i) /= alpha;
1288  return A;
1289  }
1290 
1292 
1294 
1300  template <class Row, class AT>
1302 
1304 
1305  for (int i = 0; i < x.size(); i++)
1306  y.e(i) = -x.e(i);
1307 
1308  return y;
1309  }
1310 
1316  template <class Col, class AT>
1318 
1320 
1321  for (int i = 0; i < a.size(); i++)
1322  c.e(i) = -a.e(i);
1323 
1324  return c;
1325  }
1326 
1332  template <class Row, class AT>
1334 
1336 
1337  for (int i = 0; i < A.size(); i++)
1338  for (int j = 0; j < A.size(); j++)
1339  B.e(i, j) = -A.e(i, j);
1340 
1341  return B;
1342  }
1343 
1349  template <class Type, class Row, class Col, class AT>
1351 
1353 
1354  for (int i = 0; i < A.rows(); i++)
1355  for (int j = 0; j < A.cols(); j++)
1356  B.e(i, j) = -A.e(i, j);
1357 
1358  return B;
1359  }
1360 
1363 
1369  template <class Row, class AT>
1370  RowVector<Row,AT> trans(const Vector<Row,AT> &x) { return x.T(); }
1371 
1379  template <class Col, class AT>
1380  Vector<Col,AT> trans(const RowVector<Col,AT> &x) { return x.T(); }
1381 
1390  template <class Type, class Row, class Col, class AT>
1392 
1401  template <class Row, class AT>
1403 
1404 // template <class Type, class Row, class Col, class AT>
1405 // inline Matrix<Type, Col, Row, AT> trans(const Matrix<Type, Row, Col, AT> &A) {
1406 // Matrix<Type, Col, Row, AT> B(A.cols(), A.rows(), NONINIT);
1407 // for (int i = 0; i < B.rows(); i++)
1408 // for (int j = 0; j < B.cols(); j++)
1409 // B.e(i, j) = A.e(j, i);
1410 // return B;
1411 // }
1412 //
1413 // template <class Row, class AT>
1414 // inline SquareMatrix<Row, AT> trans(const SquareMatrix<Row, AT> &A) {
1415 // SquareMatrix<Row, AT> B(A.size(), NONINIT);
1416 // for (int i = 0; i < B.size(); i++)
1417 // for (int j = 0; j < B.size(); j++)
1418 // B.e(i, j) = A.e(j, i);
1419 // return B;
1420 // }
1421 //
1422 // template <class Row, class AT>
1423 // inline RowVector<Row, AT> trans(const Vector<Row, AT> &x) {
1424 // RowVector<Row, AT> y(x.size(), NONINIT);
1425 // for (int i = 0; i < y.size(); i++)
1426 // y.e(i) = x.e(i);
1427 // return y;
1428 // }
1429 //
1430 // template <class Row, class AT>
1431 // inline Vector<Row, AT> trans(const RowVector<Row, AT> &x) {
1432 // Vector<Row, AT> y(x.size(), NONINIT);
1433 // for (int i = 0; i < y.size(); i++)
1434 // y.e(i) = x.e(i);
1435 // return y;
1436 // }
1437 
1439 
1441  template <class Row, class AT>
1442  inline AT nrmInf(const Vector<Row, AT> &x) {
1443  AT c = 0;
1444  for (int i = 0; i < x.size(); i++)
1445  if (c < fabs(x.e(i)))
1446  c = fabs(x.e(i));
1447  return c;
1448  }
1449 
1450  template <class Row, class AT>
1451  inline AT nrmInf(const RowVector<Row, AT> &x) {
1452  AT c = 0;
1453  for (int i = 0; i < x.size(); i++)
1454  if (c < fabs(x.e(i)))
1455  c = fabs(x.e(i));
1456  return c;
1457  }
1458 
1459  template <class Row, class AT>
1460  inline typename OperatorResult<AT, AT>::Type nrm2(const Vector<Row, AT> &x) {
1461  typename OperatorResult<AT, AT>::Type c=typename OperatorResult<AT, AT>::Type();
1462  for (int i = 0; i < x.size(); i++)
1463  c += pow(x.e(i), 2);
1464  return sqrt(c);
1465  }
1466 
1467  template <class Col, class AT>
1468  inline typename OperatorResult<AT, AT>::Type nrm2(const RowVector<Col, AT> &x) {
1469  typename OperatorResult<AT, AT>::Type c=typename OperatorResult<AT, AT>::Type();
1470  for (int i = 0; i < x.size(); i++)
1471  c += pow(x.e(i), 2);
1472  return sqrt(c);
1473  }
1474 
1476 
1488  template <class Row, class AT>
1490 
1491  SquareMatrix<Row, AT> B(x.size(), NONINIT);
1492 
1493  B.e(0, 0) = 0.0;
1494  B.e(1, 1) = 0.0;
1495  B.e(2, 2) = 0.0;
1496  B.e(0, 1) = -x.e(2);
1497  B.e(0, 2) = x.e(1);
1498  B.e(1, 0) = x.e(2);
1499  B.e(1, 2) = -x.e(0);
1500  B.e(2, 0) = -x.e(1);
1501  B.e(2, 1) = x.e(0);
1502 
1503  return B;
1504  }
1505 
1511  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
1513 
1514  if (&A == &B)
1515  return true;
1516 
1517  if (A.rows() != B.rows() || A.cols() != B.cols())
1518  return false;
1519 
1520  for (int i = 0; i < A.rows(); i++)
1521  for (int j = 0; j < A.cols(); j++)
1522  if (A(i, j) != B(i, j))
1523  return false;
1524 
1525  return true;
1526  }
1527 
1533  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
1535 
1536  if (A.rows() != B.rows() || A.cols() != B.cols())
1537  return true;
1538 
1539  for (int i = 0; i < A.rows(); i++)
1540  for (int j = 0; j < A.cols(); j++)
1541  if (A(i, j) != B(i, j))
1542  return true;
1543 
1544  return false;
1545  }
1546 
1552  template <class Row, class AT>
1553  AT max(const Vector<Row, AT> &x) {
1554 
1555  FMATVEC_ASSERT(x.size() > 0, AT);
1556  AT maximum = x.e(0);
1557  for (int i = 1; i < x.size(); i++) {
1558  if (x.e(i) > maximum)
1559  maximum = x.e(i);
1560  }
1561  return maximum;
1562  }
1563 
1569  template <class Row, class AT>
1570  int maxIndex(const Vector<Row, AT> &x) {
1571 
1572  FMATVEC_ASSERT(x.size() > 0, AT);
1573  AT maximum = x.e(0);
1574  int index = 0;
1575  for (int i = 1; i < x.size(); i++) {
1576  if (x.e(i) > maximum) {
1577  maximum = x.e(i);
1578  index = i;
1579  }
1580  }
1581  return index;
1582  }
1583 
1589  template <class Row, class AT>
1590  AT min(const Vector<Row, AT> &x) {
1591 
1592  FMATVEC_ASSERT(x.size() > 0, AT);
1593  AT minimum = x.e(0);
1594  for (int i = 1; i < x.size(); i++) {
1595  if (x.e(i) < minimum)
1596  minimum = x.e(i);
1597  }
1598  return minimum;
1599  }
1600 
1606  template <class Row, class AT>
1607  int minIndex(const Vector<Row, AT> &x) {
1608 
1609  FMATVEC_ASSERT(x.size() > 0, AT);
1610  AT minimum = x.e(0);
1611  int index = 0;
1612  for (int i = 1; i < x.size(); i++) {
1613  if (x.e(i) < minimum) {
1614  minimum = x.e(i);
1615  index = i;
1616  }
1617  }
1618  return index;
1619  }
1620 
1621  // HR 28.09.2006
1628  template <class AT>
1630 
1631  FMATVEC_ASSERT(A_.rows() > 0, AT);
1632  FMATVEC_ASSERT(A_.cols() > 0, AT);
1633  FMATVEC_ASSERT(A_.cols() > PivotCol, AT);
1635  int i, j, N;
1636  RowVector<Ref, AT> tmp(A.cols(), NONINIT);
1637  N = A.rows();
1638  for (i = 1; i <= N - 1; i++) {
1639  for (j = 0; j < N - 1; j++) {
1640  if (A(j, PivotCol) > A(j + 1, PivotCol)) {
1641  tmp = A.row(j);
1642  A.set(j, A.row(j + 1));
1643  A.set(j + 1, tmp);
1644  }
1645  }
1646  }
1647  return A;
1648  }
1649 
1650  // HR 28.09.2006
1652  template <class AT>
1653  void quicksortmedian_intern(Matrix<General, Ref, Ref, AT> &A, int PivotCol, RowVector<Ref, AT> &tmp, int l, int r) {
1654 
1655  if (r > l) {
1656  int i = l - 1, j = r;
1657  //RowVec tmp;
1658  if (r - l > 3) { //Median of three
1659  int m = l + (r - l) / 2;
1660  if (A(l, PivotCol) > A(m, PivotCol)) {
1661  tmp = A.row(l);
1662  A.set(l, A.row(m));
1663  A.set(m, tmp);
1664  }
1665  if (A(l, PivotCol) > A(r, PivotCol)) {
1666  tmp = A.row(l);
1667  A.set(l, A.row(r));
1668  A.set(r, tmp);
1669  }
1670  else if (A(r, PivotCol) > A(m, PivotCol)) {
1671  tmp = A.row(r);
1672  A.set(r, A.row(m));
1673  A.set(m, tmp);
1674  }
1675  }
1676 
1677  for (;;) {
1678  while(A(++i,PivotCol) < A(r,PivotCol));
1679  while(A(--j,PivotCol) > A(r,PivotCol) && j>i);
1680  if(i>=j) break;
1681  tmp = A.row(i);
1682  A.set(i, A.row(j));
1683  A.set(j, tmp);
1684  }
1685  tmp = A.row(i);
1686  A.set(i, A.row(r));
1687  A.set(r, tmp);
1688  quicksortmedian_intern(A, PivotCol, tmp, l, i - 1);
1689  quicksortmedian_intern(A, PivotCol, tmp, i + 1, r);
1690  }
1691  }
1692 
1693  // HR 28.09.2006
1702  template <class AT>
1705  int N = A.rows();
1706  RowVector<Ref, AT> tmp(A.cols(), NONINIT);
1707  quicksortmedian_intern(A, PivotCol, tmp, 0, N - 1);
1708  return A;
1709  }
1710 
1711  template <class Row, class Col, class AT>
1712  inline Matrix<Symmetric, Col, Col, AT> JTJ(const Matrix<General, Row, Col, AT> &A) {
1713  Matrix<Symmetric, Col, Col, AT> S(A.cols(), A.cols(), NONINIT);
1714  for (int i = 0; i < A.cols(); i++) {
1715  for (int k = i; k < A.cols(); k++) {
1716  S.ej(i, k) = 0;
1717  for (int j = 0; j < A.rows(); j++)
1718  S.ej(i, k) += A.e(j, i) * A.e(j, k);
1719  }
1720  }
1721  return S;
1722  }
1723 
1724  template <class Row, class Col, class AT>
1725  inline Matrix<Symmetric, Col, Col, AT> JTMJ(const Matrix<Symmetric, Row, Row, AT> &B, const Matrix<General, Row, Col, AT> &A) {
1726 
1727  Matrix<Symmetric, Col, Col, AT> S(A.cols(), A.cols(), NONINIT);
1728  Matrix<General, Row, Col, AT> C = B * A;
1729 
1730  for (int i = 0; i < A.cols(); i++) {
1731  for (int k = i; k < A.cols(); k++) {
1732  S.ej(i, k) = 0;
1733  for (int j = 0; j < A.rows(); j++)
1734  S.ej(i, k) += A.e(j, i) * C.e(j, k);
1735  }
1736  }
1737  return S;
1738  }
1739 
1740  template <class Row, class Col, class AT>
1741  inline Matrix<Symmetric, Row, Row, AT> JMJT(const Matrix<General, Row, Col, AT> &A, const Matrix<Symmetric, Col, Col, AT> &B) {
1742 
1743  Matrix<Symmetric, Row, Row, AT> S(A.rows(), A.rows(), NONINIT);
1744  Matrix<General, Row, Col, AT> C = A * B;
1745 
1746  for (int i = 0; i < S.size(); i++) {
1747  for (int k = i; k < S.size(); k++) {
1748  S.ej(i, k) = 0;
1749  for (int j = 0; j < A.cols(); j++)
1750  S.ej(i, k) += C.e(k, j) * A.e(i, j);
1751  }
1752  }
1753  return S;
1754  }
1755 
1756 }
1757 
1758 #endif
RowVector< Row, AT > trans(const Vector< Row, AT > &x)
Transpose of a vector.
Definition: linear_algebra.h:1370
Matrix< General, Ref, Ref, AT > quickSortMedian(const Matrix< General, Ref, Ref, AT > &A_, int PivotCol)
Modified QuickSort Algorithm (unstable sorting Algorithm )
Definition: linear_algebra.h:1703
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:41
int rows() const
Number of rows.
Definition: general_matrix.h:304
void quicksortmedian_intern(Matrix< General, Ref, Ref, AT > &A, int PivotCol, RowVector< Ref, AT > &tmp, int l, int r)
Definition: linear_algebra.h:1653
int minIndex(const Vector< Row, AT > &x)
Index of minimum value.
Definition: linear_algebra.h:1607
Definition: fmatvec.h:50
Vector< Fixed< 3 >, typename OperatorResult< AT1, AT2 >::Type > crossProduct(const Vector< Row1, AT1 > &x, const Vector< Row2, AT2 > &y)
Cross product.
Definition: linear_algebra.h:1079
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: fmatvec.h:44
AT min(const Vector< Row, AT > &x)
Minimum value.
Definition: linear_algebra.h:1590
Definition: fmatvec.h:47
Matrix< General, Ref, Ref, AT > bubbleSort(const Matrix< General, Ref, Ref, AT > &A_, int PivotCol)
Bubble Sort Algorithm (stable sorting Algorithm )
Definition: linear_algebra.h:1629
int maxIndex(const Vector< Row, AT > &x)
Index of maximum value.
Definition: linear_algebra.h:1570
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:41
Namespace fmatvec.
Definition: _memory.cc:28
bool operator==(const Matrix< Type1, Row1, Col1, AT > &A, const Matrix< Type2, Row2, Col2, AT > &B)
Matrix-matrix comparison.
Definition: linear_algebra.h:1512
int cols() const
Number of columns.
Definition: general_matrix.h:310
const RowVector< Ref, AT > row(int i) const
Row operator.
Definition: general_matrix.h:516
bool operator!=(const Matrix< Type1, Row1, Col1, AT > &A, const Matrix< Type2, Row2, Col2, AT > &B)
Matrix-matrix comparison.
Definition: linear_algebra.h:1534
Definition: types.h:206
AT max(const Vector< Row, AT > &x)
Maximum value.
Definition: linear_algebra.h:1553
double tripleProduct(const Vector< Row, AT > &a, const Vector< Row, AT > &x, const Vector< Row, AT > &y)
Triple product.
Definition: linear_algebra.h:1099
SquareMatrix< Row, AT > tilde(const Vector< Row, AT > &x)
Tilde operator.
Definition: linear_algebra.h:1489
int rows() const
Number of rows.
AT scalarProduct(const Vector< Row, AT > &x, const Vector< Row, AT > &y)
Scalar product.
Definition: linear_algebra.h:1061