fmatvec  0.0.0
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 "diagonal_matrix.h"
39 #include "sparse_matrix.h"
40 #include "symmetric_sparse_matrix.h"
41 #include <cmath>
42 
43 namespace fmatvec {
44 
45  // template declaration for complex type defined in linear_algebra_complex.h
46  template<class T> std::complex<T> operator+(const std::complex<T> &x, int y);
47  template<class T> std::complex<T> operator-(const std::complex<T> &x, int y);
48  template<class T> std::complex<T> operator*(const std::complex<T> &x, int y);
49  template<class T> std::complex<T> operator/(const std::complex<T> &x, int y);
50  template<class T> std::complex<T> operator+(int x, const std::complex<T> &y);
51  template<class T> std::complex<T> operator-(int x, const std::complex<T> &y);
52  template<class T> std::complex<T> operator*(int x, const std::complex<T> &y);
53  template<class T> std::complex<T> operator/(int x, const std::complex<T> &y);
54 
56 
57  // Vector-Vector
58  template <class Row1, class Row2, class Row3, class AT1, class AT2, class AT3>
59  inline void add(const Vector<Row1, AT1> &a1, const Vector<Row2, AT2> &a2, Vector<Row3, AT3> &a3) {
60  FMATVEC_ASSERT(a1.size() == a2.size(), AT3);
61  for (int i = 0; i < a1.size(); i++)
62  a3.e(i) = a1.e(i) + a2.e(i);
63  }
64 
65  template <class Row1, class Row2, class AT>
66  inline void add(Vector<Row1, AT> &a1, const Vector<Row2, AT> &a2) {
67  FMATVEC_ASSERT(a1.size() == a2.size(), AT);
68  for (int i = 0; i < a1.size(); i++)
69  a1.e(i) += a2.e(i);
70  }
71 
72  template <class Row1, class Row2, class Row3, class AT1, class AT2>
73  inline void sub(const Vector<Row1, AT1> &a1, const Vector<Row2, AT2> &a2, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &a3) {
74  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
75  for (int i = 0; i < a1.size(); i++)
76  a3.e(i) = a1.e(i) - a2.e(i);
77  }
78 
79  template <class Row1, class Row2, class AT1, class AT2>
80  inline void sub(Vector<Row1, AT1> &a1, const Vector<Row2, AT2> &a2) {
81  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
82  for (int i = 0; i < a1.size(); i++)
83  a1.e(i) -= a2.e(i);
84  }
85 
86  // Addition
87  template <class AT1, class AT2, class Row1, class Row2>
88  inline Vector<Var, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Row1, AT1> &a, const Vector<Row2, AT2> &b) {
89  Vector<Var, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
90  add(a, b, c);
91  return c;
92  }
93  template <class AT1, class AT2, class Row>
94  inline Vector<Row, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Row, AT1> &a, const Vector<Row, AT2> &b) {
95  Vector<Row, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
96  add(a, b, c);
97  return c;
98  }
99  template <class AT1, class AT2, int M, class Row>
100  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Fixed<M>, AT1> &a, const Vector<Row, AT2> &b) {
101  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
102  add(a, b, c);
103  return c;
104  }
105  template <class AT1, class AT2, int M>
106  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator+(const Vector<Fixed<M>, AT1> &a, const Vector<Fixed<M>, AT2> &b) {
107  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
108  add(a, b, c);
109  return c;
110  }
111  // move
112  template <class Row1, class Row2>
113  inline Vector<Row2, double> operator+(Vector<Row1, double> &&a, Vector<Row2, double> &&b) {
114  add(a, b);
115  return std::move(a);
116  }
117  template <class Row1, class Row2>
118  inline Vector<Row1, double> operator+(Vector<Row1, double> &&a, const Vector<Row2, double> &b) {
119  add(a, b);
120  return std::move(a);
121  }
122  template <class Row1, class Row2>
123  inline Vector<Row2, double> operator+(const Vector<Row1, double> &a, Vector<Row2, double> &&b) {
124  add(b, a);
125  return std::move(b);
126  }
127  template <class AT, class Row1, class Row2>
128  inline Vector<Row1, AT> operator+=(Vector<Row1, AT> &a, const Vector<Row2, AT> &b) {
129  add(a, b);
130  return a;
131  }
132 
133  // Subtraction
134  template <class AT1, class AT2, class Row1, class Row2>
135  inline Vector<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Row1, AT1> &a, const Vector<Row2, AT2> &b) {
136  Vector<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
137  sub(a, b, c);
138  return c;
139  }
140  template <class AT1, class AT2, class Row>
141  inline Vector<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Row, AT1> &a, const Vector<Row, AT2> &b) {
142  Vector<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
143  sub(a, b, c);
144  return c;
145  }
146  template <class AT1, class AT2, int M, class Row2>
147  inline Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Fixed<M>, AT1> &a, const Vector<Row2, AT2> &b) {
148  Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
149  sub(a, b, c);
150  return c;
151  }
152  template <class AT1, class AT2, int M>
153  inline Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const Vector<Fixed<M>, AT1> &a, const Vector<Fixed<M>, AT2> &b) {
154  Vector<Fixed<M>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
155  sub(a, b, c);
156  return c;
157  }
158  // move
159  template <class Row1, class Row2>
160  inline Vector<Row2, double> operator-(Vector<Row1, double> &&a, Vector<Row2, double> &&b) {
161  sub(a, b);
162  return std::move(a);
163  }
164  template <class Row1, class Row2>
165  inline Vector<Row1, double> operator-(Vector<Row1, double> &&a, const Vector<Row2, double> &b) {
166  sub(a, b);
167  return std::move(a);
168  }
169  template <class Row1, class Row2>
170  inline Vector<Row2, double> operator-(const Vector<Row1, double> &a, Vector<Row2, double> &&b) {
171  sub(a, b, b);
172  return std::move(b);
173  }
174  template <class AT, class Row1, class Row2>
175  inline Vector<Row1, AT> operator-=(Vector<Row1, AT> &a, const Vector<Row2, AT> &b) {
176  sub(a, b);
177  return a;
178  }
179 
180  // RowVector-Vector
181  template <class Col1, class Col2, class Col3, class AT>
182  inline void add(const RowVector<Col1, AT> &a1, const RowVector<Col2, AT> &a2, RowVector<Col3, AT> &a3) {
183  FMATVEC_ASSERT(a1.size() == a2.size(), AT);
184  for (int i = 0; i < a1.size(); i++)
185  a3.e(i) = a1.e(i) + a2.e(i);
186  }
187 
188  template <class Col1, class Col2, class AT>
189  inline void add(RowVector<Col1, AT> &a1, const RowVector<Col2, AT> &a2) {
190  FMATVEC_ASSERT(a1.size() == a2.size(), AT);
191  for (int i = 0; i < a1.size(); i++)
192  a1.e(i) += a2.e(i);
193  }
194 
195  template <class Col1, class Col2, class Col3, class AT1, class AT2>
196  inline void sub(const RowVector<Col1, AT1> &a1, const RowVector<Col2, AT2> &a2, RowVector<Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &a3) {
197  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
198  for (int i = 0; i < a1.size(); i++)
199  a3.e(i) = a1.e(i) - a2.e(i);
200  }
201 
202  template <class Col1, class Col2, class AT1, class AT2>
203  inline void sub(RowVector<Col1, AT1> &a1, const RowVector<Col2, AT2> &a2) {
204  FMATVEC_ASSERT(a1.size() == a2.size(), AT2);
205  for (int i = 0; i < a1.size(); i++)
206  a1.e(i) -= a2.e(i);
207  }
208 
209  // Addition
210  template <class AT1, class AT2, class Col1, class Col2>
211  inline RowVector<Var, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Col1, AT1> &a, const RowVector<Col2, AT2> &b) {
212  RowVector<Var, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
213  add(a, b, c);
214  return c;
215  }
216  template <class AT1, class AT2, class Col>
217  inline RowVector<Col, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Col, AT1> &a, const RowVector<Col, AT2> &b) {
218  RowVector<Col, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
219  add(a, b, c);
220  return c;
221  }
222  template <class AT1, class AT2, int N, class Col2>
223  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Fixed<N>, AT1> &a, const RowVector<Col2, AT2> &b) {
224  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
225  add(a, b, c);
226  return c;
227  }
228  template <class AT1, class AT2, int N>
229  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator+(const RowVector<Fixed<N>, AT1> &a, const RowVector<Fixed<N>, AT2> &b) {
230  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
231  add(a, b, c);
232  return c;
233  }
234  // move
235  template <class Row1, class Row2>
236  inline RowVector<Row2, double> operator+(RowVector<Row1, double> &&a, RowVector<Row2, double> &&b) {
237  add(a, b);
238  return std::move(a);
239  }
240  template <class Row1, class Row2>
241  inline RowVector<Row1, double> operator+(RowVector<Row1, double> &&a, const RowVector<Row2, double> &b) {
242  add(a, b);
243  return std::move(a);
244  }
245  template <class Row1, class Row2>
246  inline RowVector<Row2, double> operator+(const RowVector<Row1, double> &a, RowVector<Row2, double> &&b) {
247  add(b, a);
248  return std::move(b);
249  }
250  template <class AT, class Col1, class Col2>
251  inline RowVector<Col1, AT> operator+=(RowVector<Col1, AT> &a, const RowVector<Col2, AT> &b) {
252  add(a, b);
253  return a;
254  }
255 
256  // Subtraction
257  template <class AT1, class AT2, class Col1, class Col2>
258  inline RowVector<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Col1, AT1> &a, const RowVector<Col2, AT2> &b) {
259  RowVector<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
260  sub(a, b, c);
261  return c;
262  }
263  template <class AT1, class AT2, class Col>
264  inline RowVector<Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Col, AT1> &a, const RowVector<Col, AT2> &b) {
265  RowVector<Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
266  sub(a, b, c);
267  return c;
268  }
269  template <class AT1, class AT2, int N, class Col2>
270  inline RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Fixed<N>, AT1> &a, const RowVector<Col2, AT2> &b) {
271  RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
272  sub(a, b, c);
273  return c;
274  }
275  template <class AT1, class AT2, int N>
276  inline RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const RowVector<Fixed<N>, AT1> &a, const RowVector<Fixed<N>, AT2> &b) {
277  RowVector<Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> c(a.size(), NONINIT);
278  sub(a, b, c);
279  return c;
280  }
281  // move
282  template <class Row1, class Row2>
283  inline RowVector<Row2, double> operator-(RowVector<Row1, double> &&a, RowVector<Row2, double> &&b) {
284  sub(a, b);
285  return std::move(a);
286  }
287  template <class Row1, class Row2>
288  inline RowVector<Row1, double> operator-(RowVector<Row1, double> &&a, const RowVector<Row2, double> &b) {
289  sub(a, b);
290  return std::move(a);
291  }
292  template <class Row1, class Row2>
293  inline RowVector<Row2, double> operator-(const RowVector<Row1, double> &a, RowVector<Row2, double> &&b) {
294  sub(a, b, b);
295  return std::move(b);
296  }
297  template <class AT, class Col1, class Col2>
298  inline RowVector<Col1, AT> operator-=(RowVector<Col1, AT> &a, const RowVector<Col2, AT> &b) {
299  sub(a, b);
300  return a;
301  }
302 
303  // Matrix-Matrix
304  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>
305  inline void add(const Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, AT3> &A3) {
306  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT3);
307  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT3);
308  for (int i = 0; i < A1.rows(); i++)
309  for (int j = 0; j < A2.cols(); j++)
310  A3.e(i, j) = A1.e(i, j) + A2.e(i, j);
311  }
312 
313  template <class Row1, class Row2, class Row3, class AT>
314  inline void add(const Matrix<Symmetric, Row1, Row1, AT> &A1, const Matrix<Symmetric, Row2, Row2, AT> &A2, Matrix<Symmetric, Row3, Row3, AT> &A3) {
315  FMATVEC_ASSERT(A1.size() == A2.size(), AT);
316  for (int i = 0; i < A1.size(); i++)
317  for (int j = i; j < A2.size(); j++)
318  A3.ej(i, j) = A1.ej(i, j) + A2.ej(i, j);
319  }
320 
321  template <class Row1, class Row2, class Row3, class AT>
322  inline void add(const Matrix<Diagonal, Row1, Row1, AT> &A1, const Matrix<Diagonal, Row2, Row2, AT> &A2, Matrix<Diagonal, Row3, Row3, AT> &A3) {
323  FMATVEC_ASSERT(A1.size() == A2.size(), AT);
324  for (int i = 0; i < A3.size(); i++)
325  A3.e(i) = A1.e(i) + A2.e(i);
326  }
327 
328  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class AT>
329  inline void add(Matrix<Type1, Row1, Col1, AT> &A1, const Matrix<Type2, Row2, Col2, AT> &A2) {
330  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT);
331  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT);
332  for (int i = 0; i < A1.rows(); i++)
333  for (int j = 0; j < A2.cols(); j++)
334  A1.e(i, j) += A2.e(i, j);
335  }
336 
337  template <class Row1, class Row2, class AT>
338  inline void add(Matrix<Symmetric, Row1, Row1, AT> &A1, const Matrix<Symmetric, Row2, Row2, AT> &A2) {
339  FMATVEC_ASSERT(A1.size() == A2.size(), AT);
340  for (int i = 0; i < A1.size(); i++)
341  for (int j = i; j < A2.size(); j++)
342  A1.ej(i, j) += A2.ej(i, j);
343  }
344 
345  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
346  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) {
347  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT2);
348  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT2);
349  for (int i = 0; i < A1.rows(); i++)
350  for (int j = 0; j < A2.cols(); j++)
351  A3.e(i, j) = A1.e(i, j) - A2.e(i, j);
352  }
353 
354  template <class Row1, class Row2, class Row3, class AT1, class AT2>
355  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) {
356  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
357  for (int i = 0; i < A1.size(); i++)
358  for (int j = i; j < A2.size(); j++)
359  A3.ej(i, j) = A1.ej(i, j) - A2.ej(i, j);
360  }
361 
362  template <class Row1, class Row2, class Row3, class AT1, class AT2>
363  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) {
364  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
365  for (int i = 0; i < A3.size(); i++)
366  A3.e(i) = A1.e(i) - A2.e(i);
367  }
368 
369  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class AT1, class AT2>
370  inline void sub(Matrix<Type1, Row1, Col1, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2) {
371  FMATVEC_ASSERT(A1.rows() == A2.rows(), AT2);
372  FMATVEC_ASSERT(A1.cols() == A2.cols(), AT2);
373  for (int i = 0; i < A1.rows(); i++)
374  for (int j = 0; j < A2.cols(); j++)
375  A1.e(i, j) -= A2.e(i, j);
376  }
377 
378  template <class Row1, class Row2, class AT1, class AT2>
379  inline void sub(Matrix<Symmetric, Row1, Row1, AT1> &A1, const Matrix<Symmetric, Row2, Row2, AT2> &A2) {
380  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
381  for (int i = 0; i < A1.size(); i++)
382  for (int j = i; j < A2.size(); j++)
383  A1.ej(i, j) -= A2.ej(i, j);
384  }
385 
386  // Addition
387  // Type1 Type2
388  template <class AT1, class AT2, class Type1, class Type2, class Row1, class Col1, class Row2, class Col2>
389  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) {
390  Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
391  add(A, B, C);
392  return C;
393  }
394  template <class AT1, class AT2, class Type1, class Type2, class Row, class Col>
395  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) {
396  Matrix<General, Row, Col, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
397  add(A, B, C);
398  return C;
399  }
400  template <class AT1, class AT2, int M, class Type1, class Type2, class Row, class Col>
401  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) {
402  Matrix<General, Fixed<M>, 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, int M, class Type1, class Type2>
407  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) {
408  Matrix<General, Fixed<M>, Var, 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 N, class Type1, class Type2, class Row, class Col>
413  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) {
414  Matrix<General, Var, Fixed<N>, 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 N, class Type1, class Type2>
419  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) {
420  Matrix<General, Var, Fixed<N>, 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 M, int N, class Type1, class Type2, class Row, class Col>
425  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) {
426  Matrix<General, Fixed<M>, 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 M, int N, class Type1, class Type2>
431  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) {
432  Matrix<General, Fixed<M>, 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 Type1, class Type2>
437  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) {
438  Matrix<General, 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 Type1, class Type2>
443  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) {
444  Matrix<General, 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 Type1, class Type2, class Row, class Col>
449  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) {
450  Matrix<General, 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 Type1, class Type2>
455  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) {
456  Matrix<General, 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 Type1, class Type2>
461  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) {
462  Matrix<General, 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  // Type
467  template <class AT1, class AT2, class Type, class Row1, class Col1, class Row2, class Col2>
468  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) {
469  Matrix<Type, Var, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
470  add(A, B, C);
471  return C;
472  }
473  template <class AT1, class AT2, class Type, class Row, class Col>
474  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) {
475  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
476  add(A, B, C);
477  return C;
478  }
479  template <class AT1, class AT2, int M, class Type, class Row, class Col>
480  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) {
481  Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
482  add(A, B, C);
483  return C;
484  }
485  template <class AT1, class AT2, int M, class Type>
486  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) {
487  Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
488  add(A, B, C);
489  return C;
490  }
491  template <class AT1, class AT2, int N, class Type, class Row, class Col>
492  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) {
493  Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
494  add(A, B, C);
495  return C;
496  }
497  template <class AT1, class AT2, int N, class Type>
498  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) {
499  Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
500  add(A, B, C);
501  return C;
502  }
503  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
504  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) {
505  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
506  add(A, B, C);
507  return C;
508  }
509  template <class AT1, class AT2, int M, int N, class Type>
510  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) {
511  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
512  add(A, B, C);
513  return C;
514  }
515  template <class AT1, class AT2, int M, int N, class Type>
516  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) {
517  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
518  add(A, B, C);
519  return C;
520  }
521  template <class AT1, class AT2, int M, int N, class Type>
522  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) {
523  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
524  add(A, B, C);
525  return C;
526  }
527  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
528  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) {
529  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
530  add(A, B, C);
531  return C;
532  }
533  template <class AT1, class AT2, int M, int N, class Type>
534  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) {
535  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
536  add(A, B, C);
537  return C;
538  }
539  template <class AT1, class AT2, int M, int N, class Type>
540  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) {
541  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
542  add(A, B, C);
543  return C;
544  }
545  // move
546  template <class Type1, class Type2, class Row1, class Row2, class Col1, class Col2>
547  inline Matrix<Type2, Row2, Col2, double> operator+(Matrix<Type1, Row1, Col1, double> &&a, Matrix<Type2, Row2, Col2, double> &&b) {
548  add(a, b);
549  return std::move(a);
550  }
551  template <class Type1, class Type2, class Row1, class Row2, class Col1, class Col2>
552  inline Matrix<Type1, Row1, Col1, double> operator+(Matrix<Type1, Row1, Col1, double> &&a, const Matrix<Type2, Row2, Col2, double> &b) {
553  add(a, b);
554  return std::move(a);
555  }
556  template <class Type1, class Type2, class Row1, class Row2, class Col1, class Col2>
557  inline Matrix<Type2, Row2, Col2, double> operator+(const Matrix<Type1, Row1, Col1, double> &a, Matrix<Type2, Row2, Col2, double> &&b) {
558  add(b, a);
559  return std::move(b);
560  }
561  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
562  inline Matrix<Type1, Row1, Col1, AT>& operator+=(Matrix<Type1, Row1, Col1, AT> &A, const Matrix<Type2, Row2, Col2, AT> &B) {
563  add(A, B);
564  return A;
565  }
566 
567  // Subtraction
568  // Type1 Type2
569  template <class AT1, class AT2, class Type1, class Type2, class Row1, class Col1, class Row2, class Col2>
570  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) {
571  Matrix<General, Var, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
572  sub(A, B, C);
573  return C;
574  }
575  template <class AT1, class AT2, class Type1, class Type2, class Row, class Col>
576  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) {
577  Matrix<General, Row, Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
578  sub(A, B, C);
579  return C;
580  }
581  template <class AT1, class AT2, int M, class Type1, class Type2, class Row, class Col>
582  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) {
583  Matrix<General, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
584  sub(A, B, C);
585  return C;
586  }
587  template <class AT1, class AT2, int M, class Type1, class Type2>
588  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) {
589  Matrix<General, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
590  sub(A, B, C);
591  return C;
592  }
593  template <class AT1, class AT2, int N, class Type1, class Type2, class Row, class Col>
594  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) {
595  Matrix<General, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
596  sub(A, B, C);
597  return C;
598  }
599  template <class AT1, class AT2, int N, class Type1, class Type2>
600  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) {
601  Matrix<General, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
602  sub(A, B, C);
603  return C;
604  }
605  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Row, class Col>
606  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) {
607  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
608  sub(A, B, C);
609  return C;
610  }
611  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
612  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) {
613  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
614  sub(A, B, C);
615  return C;
616  }
617  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
618  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) {
619  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
620  sub(A, B, C);
621  return C;
622  }
623  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
624  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) {
625  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
626  sub(A, B, C);
627  return C;
628  }
629  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Row, class Col>
630  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) {
631  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
632  sub(A, B, C);
633  return C;
634  }
635  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
636  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) {
637  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
638  sub(A, B, C);
639  return C;
640  }
641  template <class AT1, class AT2, int M, int N, class Type1, class Type2>
642  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) {
643  Matrix<General, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
644  sub(A, B, C);
645  return C;
646  }
647  // Type
648  template <class AT1, class AT2, class Type, class Row1, class Col1, class Row2, class Col2>
649  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) {
650  Matrix<Type, Var, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
651  sub(A, B, C);
652  return C;
653  }
654  template <class AT1, class AT2, class Type, class Row, class Col>
655  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) {
656  Matrix<Type, Row, Col, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
657  sub(A, B, C);
658  return C;
659  }
660  template <class AT1, class AT2, int M, class Type, class Row, class Col>
661  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) {
662  Matrix<Type, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
663  sub(A, B, C);
664  return C;
665  }
666  template <class AT1, class AT2, int M, class Type>
667  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) {
668  Matrix<Type, Fixed<M>, Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
669  sub(A, B, C);
670  return C;
671  }
672  template <class AT1, class AT2, int N, class Type, class Row, class Col>
673  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) {
674  Matrix<Type, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
675  sub(A, B, C);
676  return C;
677  }
678  template <class AT1, class AT2, int N, class Type>
679  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) {
680  Matrix<Type, Var, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
681  sub(A, B, C);
682  return C;
683  }
684  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
685  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) {
686  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
687  sub(A, B, C);
688  return C;
689  }
690  template <class AT1, class AT2, int M, int N, class Type>
691  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) {
692  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
693  sub(A, B, C);
694  return C;
695  }
696  template <class AT1, class AT2, int M, int N, class Type>
697  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) {
698  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
699  sub(A, B, C);
700  return C;
701  }
702  template <class AT1, class AT2, int M, int N, class Type>
703  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) {
704  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
705  sub(A, B, C);
706  return C;
707  }
708  template <class AT1, class AT2, int M, int N, class Type, class Row, class Col>
709  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) {
710  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
711  sub(A, B, C);
712  return C;
713  }
714  template <class AT1, class AT2, int M, int N, class Type>
715  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) {
716  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
717  sub(A, B, C);
718  return C;
719  }
720  template <class AT1, class AT2, int M, int N, class Type>
721  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) {
722  Matrix<Type, Fixed<M>, Fixed<N>, typename fmatvec::OperatorResult<AT1, AT2>::Type> C(A.rows(), A.cols(), NONINIT);
723  sub(A, B, C);
724  return C;
725  }
726  // move
727  template <class Type1, class Type2, class Row1, class Row2, class Col1, class Col2>
728  inline Matrix<Type2, Row2, Col2, double> operator-(Matrix<Type1, Row1, Col1, double> &&a, Matrix<Type2, Row2, Col2, double> &&b) {
729  sub(a, b);
730  return std::move(a);
731  }
732  template <class Type1, class Type2, class Row1, class Row2, class Col1, class Col2>
733  inline Matrix<Type1, Row1, Col1, double> operator-(Matrix<Type1, Row1, Col1, double> &&a, const Matrix<Type2, Row2, Col2, double> &b) {
734  sub(a, b);
735  return std::move(a);
736  }
737  template <class Type1, class Type2, class Row1, class Row2, class Col1, class Col2>
738  inline Matrix<Type2, Row2, Col2, double> operator-(const Matrix<Type1, Row1, Col1, double> &a, Matrix<Type2, Row2, Col2, double> &&b) {
739  sub(a, b, b);
740  return std::move(b);
741  }
742  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
743  inline Matrix<Type1, Row1, Col1, AT>& operator-=(Matrix<Type1, Row1, Col1, AT> &A, const Matrix<Type2, Row2, Col2, AT> &B) {
744  sub(A, B);
745  return A;
746  }
747 
748 // SquareMatrix-SquareMatrix
749  template <class AT1, class AT2, class Row1, class Row2>
750  inline SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> operator+(const SquareMatrix<Row1, AT1> &A1, const SquareMatrix<Row2, AT2> &A2) {
751  SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
752  add(A1, A2, A3);
753  return A3;
754  }
755  template <class AT1, class AT2, class Row>
756  inline SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator+(const SquareMatrix<Row, AT1> &A1, const SquareMatrix<Row, AT2> &A2) {
757  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
758  add(A1, A2, A3);
759  return A3;
760  }
761  // move
762  template <class Row1, class Row2>
763  inline SquareMatrix<Row1, double> operator+(SquareMatrix<Row1, double> &&A1, SquareMatrix<Row2, double> &&A2) {
764  add(A1, A2);
765  return std::move(A1);;
766  }
767  template <class Row1, class Row2>
768  inline SquareMatrix<Row1, double> operator+(SquareMatrix<Row1, double> &&A1, const SquareMatrix<Row2, double> &A2) {
769  add(A1, A2);
770  return std::move(A1);;
771  }
772  template <class Row1, class Row2>
773  inline SquareMatrix<Row2, double> operator+(const SquareMatrix<Row1, double> &A1, SquareMatrix<Row2, double> &&A2) {
774  add(A2, A1);
775  return std::move(A2);
776  }
777 
778 // SquareMatrix-SquareMatrix
779  template <class AT1, class AT2, class Row1, class Row2>
780  inline SquareMatrix<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const SquareMatrix<Row1, AT1> &A1, const SquareMatrix<Row2, AT2> &A2) {
781  SquareMatrix<Var, typename fmatvec::OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
782  sub(A1, A2, A3);
783  return A3;
784  }
785  template <class AT1, class AT2, class Row>
786  inline SquareMatrix<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> operator-(const SquareMatrix<Row, AT1> &A1, const SquareMatrix<Row, AT2> &A2) {
787  SquareMatrix<Row, typename fmatvec::OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
788  sub(A1, A2, A3);
789  return A3;
790  }
791  // move
792  template <class Row1, class Row2>
793  inline SquareMatrix<Row1, double> operator-(SquareMatrix<Row1, double> &&A1, SquareMatrix<Row2, double> &&A2) {
794  sub(A1, A2);
795  return std::move(A1);;
796  }
797  template <class Row1, class Row2>
798  inline SquareMatrix<Row1, double> operator-(SquareMatrix<Row1, double> &&A1, const SquareMatrix<Row2, double> &A2) {
799  sub(A1, A2);
800  return std::move(A1);;
801  }
802  template <class Row1, class Row2>
803  inline SquareMatrix<Row2, double> operator-(const SquareMatrix<Row1, double> &A1, SquareMatrix<Row2, double> &&A2) {
804  sub(A1, A2, A2);
805  return std::move(A2);
806  }
807 
813  template <class Type1, class Row1, class Col1, class Row2, class Row3, class AT1, class AT2>
814  inline void mult(const Matrix<Type1, Row1, Col1, AT1> &A, const Vector<Row2, AT2> &x, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
815  FMATVEC_ASSERT(A.cols() == x.size(), AT2);
816  for (int i = 0; i < y.size(); i++) {
817  y.e(i) = 0;
818  for (int j = 0; j < x.size(); j++)
819  y.e(i) += A.e(i, j) * x.e(j);
820  }
821  }
822 
823  template <class Row1, class Row2, class Row3, class AT1, class AT2>
824  inline void mult(const Matrix<Symmetric, Row1, Row1, AT1> &A, const Vector<Row2, AT2> &x, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
825  FMATVEC_ASSERT(A.cols() == x.size(), AT2);
826  for (int i = 0; i < y.size(); i++) {
827  y.e(i) = 0;
828  for (int j = 0; j < i; j++)
829  y.e(i) += A.ei(i, j) * x.e(j);
830  for (int j = i; j < x.size(); j++)
831  y.e(i) += A.ej(i, j) * x.e(j);
832  }
833  }
834 
835  template <class Row2, class Row3, class AT1, class AT2>
836  inline void mult(const Matrix<Sparse, Ref, Ref, AT1> &A, const Vector<Row2, AT2> &x, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
837  FMATVEC_ASSERT(A.cols() == x.size(), AT2);
838  for (int i = 0; i < y.size(); i++) {
839  y.e(i) = 0;
840  for (int j = A.Ip()[i]; j < A.Ip()[i+1]; j++)
841  y.e(i) += A()[j]*x.e(A.Jp()[j]);
842  }
843  }
844 
845  template <class Row2, class Row3, class AT1, class AT2>
846  inline void mult(const Matrix<SymmetricSparse, Ref, Ref, AT1> &A, const Vector<Row2, AT2> &x, Vector<Row3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
847  FMATVEC_ASSERT(A.cols() == x.size(), AT2);
848  for (int i = 0; i < y.size(); i++)
849  y.e(i) = A()[A.Ip()[i]]*x.e(A.Jp()[A.Ip()[i]]);
850  for (int i = 0; i < y.size(); i++) {
851  for (int j = A.Ip()[i]+1; j < A.Ip()[i+1]; j++) {
852  y.e(i) += A()[j]*x.e(A.Jp()[j]);
853  y.e(A.Jp()[j]) += A()[j]*x.e(i);
854  }
855  }
856  }
857 
858  // Matrix-Vector
859  template <class AT1, class AT2, class Type1, class Row1, class Col1, class Row2>
860  inline Vector<Var, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row1, Col1, AT1> &A, const Vector<Row2, AT2> &x) {
861  Vector<Var, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
862  mult(A, x, y);
863  return y;
864  }
865  template <class AT1, class AT2, class Type1, class Row, class Col>
866  inline Vector<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Row, Col, AT1> &A, const Vector<Row, AT2> &x) {
867  Vector<Row, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
868  mult(A, x, y);
869  return y;
870  }
871  template <class AT1, class AT2, int M, class Type1, class Col1, class Row2>
872  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type1, Fixed<M>, Col1, AT1> &A, const Vector<Row2, AT2> &x) {
873  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
874  mult(A, x, y);
875  return y;
876  }
877  template <class AT1, class AT2, int M, class Type1>
878  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) {
879  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
880  mult(A, x, y);
881  return y;
882  }
883  template <class AT1, class AT2, int M, int N, class Type, class Row2>
884  inline Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Type, Fixed<M>, Fixed<N>, AT1> &A, const Vector<Row2, AT2> &x) {
885  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
886  mult(A, x, y);
887  return y;
888  }
889  template <class AT1, class AT2, int M, int N, class Type>
890  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) {
891  Vector<Fixed<M>, typename OperatorResult<AT1, AT2>::Type> y(A.rows(), NONINIT);
892  mult(A, x, y);
893  return y;
894  }
895 
896  // RowVector-Matrix
897  template <class Col1, class Type2, class Row2, class Col2, class Type3, class Col3, class AT1, class AT2>
898  inline void mult(const RowVector<Col1, AT1> &x, const Matrix<Type2, Row2, Col2, AT2> &A, RowVector<Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
899  FMATVEC_ASSERT(x.size() == A.rows(), AT2);
900  for (int i = 0; i < y.size(); i++) {
901  y.e(i) = 0;
902  for (int j = 0; j < x.size(); j++)
903  y.e(i) += x.e(j) * A.e(j, i);
904  }
905  }
906 
907  template <class Col1, class Row2, class Col3, class AT1, class AT2>
908  inline void mult(const RowVector<Col1, AT1> &x, const Matrix<Symmetric, Row2, Row2, AT2> &A, RowVector<Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &y) {
909  FMATVEC_ASSERT(x.size() == A.rows(), AT2);
910  for (int i = 0; i < y.size(); i++) {
911  y.e(i) = 0;
912  for (int j = 0; j < i; j++)
913  y.e(i) += x.e(j) * A.ej(j, i);
914  for (int j = i; j < x.size(); j++)
915  y.e(i) += x.e(j) * A.ei(j, i);
916  }
917  }
918 
919  template <class AT1, class AT2, class Type2, class Col2, class Row1, class Col1>
920  inline RowVector<Var, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Col2, AT1> &x, const Matrix<Type2, Row1, Col1, AT2> &A) {
921  RowVector<Var, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
922  mult(x, A, y);
923  return y;
924  }
925  template <class AT1, class AT2, class Type2, class Row, class Col>
926  inline RowVector<Col, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Col, AT1> &x, const Matrix<Type2, Row, Col, AT2> &A) {
927  RowVector<Col, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
928  mult(x, A, y);
929  return y;
930  }
931  template <class AT1, class AT2, int N, class Type2, class Col1, class Row2>
932  inline RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> operator*(const RowVector<Col1, AT1> &x, const Matrix<Type2, Row2, Fixed<N>, AT2> &A) {
933  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
934  mult(x, A, y);
935  return y;
936  }
937  template <class AT1, class AT2, int N, class Type2>
938  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) {
939  RowVector<Fixed<N>, typename OperatorResult<AT1, AT2>::Type> y(A.cols(), NONINIT);
940  mult(x, A, y);
941  return y;
942  }
943 
944  // Matrix-Matrix
945  template <class Type1, class Row1, class Col1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
946  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) {
947  FMATVEC_ASSERT(A1.cols() == A2.rows(), AT2);
948  for (int i = 0; i < A3.rows(); i++) {
949  for (int k = 0; k < A3.cols(); k++) {
950  A3.e(i, k) = 0;
951  for (int j = 0; j < A1.cols(); j++)
952  A3.e(i, k) += A1.e(i, j) * A2.e(j, k);
953  }
954  }
955  }
956  template <class Row1, class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
957  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) {
958  FMATVEC_ASSERT(A1.size() == A2.rows(), AT2);
959  for (int i = 0; i < A3.rows(); i++) {
960  for (int k = 0; k < A3.cols(); k++) {
961  A3.e(i, k) = 0;
962  for (int j = 0; j < i; j++)
963  A3.e(i, k) += A1.ei(i, j) * A2.e(j, k);
964  for (int j = i; j < A1.cols(); j++)
965  A3.e(i, k) += A1.ej(i, j) * A2.e(j, k);
966  }
967  }
968  }
969  template <class Row1, class Row2, class Row3, class AT1, class AT2>
970  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) {
971  FMATVEC_ASSERT(A1.size() == A2.size(), AT2);
972  for (int i = 0; i < A3.size(); i++)
973  A3.e(i) = A1.e(i) * A2.e(i);
974  }
975 
976  template <class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
977  inline void mult(const Matrix<Sparse, Ref, Ref, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
978  FMATVEC_ASSERT(A1.cols() == A2.rows(), AT2);
979  for (int i = 0; i < A3.rows(); i++) {
980  for (int k = 0; k < A3.cols(); k++) {
981  A3.e(i, k) = 0;
982  for (int j = A1.Ip()[i]; j < A1.Ip()[i+1]; j++)
983  A3.e(i, k) += A1()[j]*A2.e(A1.Jp()[j], k);
984  }
985  }
986  }
987 
988  template <class Type2, class Row2, class Col2, class Type3, class Row3, class Col3, class AT1, class AT2>
989  inline void mult(const Matrix<SymmetricSparse, Ref, Ref, AT1> &A1, const Matrix<Type2, Row2, Col2, AT2> &A2, Matrix<Type3, Row3, Col3, typename fmatvec::OperatorResult<AT1, AT2>::Type> &A3) {
990  FMATVEC_ASSERT(A1.size() == A2.rows(), AT2);
991  for (int i = 0; i < A3.rows(); i++) {
992  for (int k = 0; k < A3.cols(); k++)
993  A3.e(i, k) = A1()[A1.Ip()[i]]*A2.e(A1.Jp()[A1.Ip()[i]], k);
994  }
995  for (int i = 0; i < A3.rows(); i++) {
996  for (int k = 0; k < A3.cols(); k++) {
997  for (int j = A1.Ip()[i]+1; j < A1.Ip()[i+1]; j++) {
998  A3.e(i, k) += A1()[j]*A2.e(A1.Jp()[j], k);
999  A3.e(A1.Jp()[j], k) += A1()[j]*A2.e(i, k);
1000  }
1001  }
1002  }
1003  }
1004 
1005  template <class AT1, class AT2, class Type1, class Type2, class Row1, class Col1, class Row2, class Col2>
1006  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) {
1007  Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1008  mult(A1, A2, A3);
1009  return A3;
1010  }
1011  template <class AT1, class AT2, class Type1, class Type2, class Row, class Col>
1012  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) {
1013  Matrix<General, Row, Col, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1014  mult(A1, A2, A3);
1015  return A3;
1016  }
1017  template <class AT1, class AT2, int M, class Type1, class Type2, class Col1, class Row2, class Col2>
1018  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) {
1019  Matrix<General, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1020  mult(A1, A2, A3);
1021  return A3;
1022  }
1023  template <class AT1, class AT2, int N, class Type1, class Row1, class Col1, class Type2, class Row2>
1024  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) {
1025  Matrix<General, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1026  mult(A1, A2, A3);
1027  return A3;
1028  }
1029  template <class AT1, class AT2, int M, int N, class Type1, class Type2, class Col1, class Row2>
1030  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) {
1031  Matrix<General, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1032  mult(A1, A2, A3);
1033  return A3;
1034  }
1035  template <class AT1, class AT2, int M, class Type1, class Type2>
1036  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) {
1037  Matrix<General, Fixed<M>, Fixed<M>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1038  mult(A1, A2, A3);
1039  return A3;
1040  }
1041  template <class AT1, class AT2, class Type, class Row1, class Col1, class Row2, class Col2>
1042  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) {
1043  Matrix<Type, Var, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1044  mult(A1, A2, A3);
1045  return A3;
1046  }
1047  template <class AT1, class AT2, class Type, class Row, class Col>
1048  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) {
1049  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1050  mult(A1, A2, A3);
1051  return A3;
1052  }
1053  template <class AT1, class AT2, int M, class Type, class Col1, class Row2, class Col2>
1054  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) {
1055  Matrix<Type, Fixed<M>, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1056  mult(A1, A2, A3);
1057  return A3;
1058  }
1059  template <class AT1, class AT2, int N, class Type, class Row1, class Col1, class Row2>
1060  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) {
1061  Matrix<Type, Var, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1062  mult(A1, A2, A3);
1063  return A3;
1064  }
1065  template <class AT1, class AT2, int M, int N, class Type, class Col1, class Row2>
1066  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) {
1067  Matrix<Type, Fixed<M>, Fixed<N>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1068  mult(A1, A2, A3);
1069  return A3;
1070  }
1071  template <class AT1, class AT2, int M, class Type>
1072  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) {
1073  Matrix<Type, Fixed<M>, Fixed<M>, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1074  mult(A1, A2, A3);
1075  return A3;
1076  }
1077 
1078 // SquareMatrix-SquareMatrix
1079  template <class AT1, class AT2, class Row1, class Row2>
1080  inline SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> operator*(const SquareMatrix<Row1, AT1> &A1, const SquareMatrix<Row2, AT2> &A2) {
1081  SquareMatrix<Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
1082  mult(A1, A2, A3);
1083  return A3;
1084  }
1085  template <class AT1, class AT2, class Row>
1086  inline SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const SquareMatrix<Row, AT1> &A1, const SquareMatrix<Row, AT2> &A2) {
1087  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> A3(A1.size(), NONINIT);
1088  mult(A1, A2, A3);
1089  return A3;
1090  }
1091 
1092  // SymmetricMatrix-SymmetricMatrix
1093  template <class AT1, class AT2, class Row1, class Row2>
1094  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) {
1095  Matrix<General, Var, Var, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1096  mult(A1, A2, A3);
1097  return A3;
1098  }
1099  template <class AT1, class AT2, class Row>
1100  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) {
1101  Matrix<General, Row, Row, typename OperatorResult<AT1, AT2>::Type> A3(A1.rows(), A2.cols(), NONINIT);
1102  mult(A1, A2, A3);
1103  return A3;
1104  }
1105 
1107 
1114  template <class Row, class AT1, class AT2>
1116 
1118 
1119  for (int i = 0; i < x.size(); i++)
1120  y.e(i) = x.e(i) * alpha;
1121 
1122  return y;
1123  }
1124  // move
1125  template <class Row>
1126  Vector<Row, double> operator*(Vector<Row, double> &&x, const double& alpha) {
1127  for (int i = 0; i < x.size(); i++)
1128  x.e(i) *= alpha;
1129  return std::move(x);
1130  }
1131 
1136  template <class Row, class AT1, class AT2>
1138 
1140 
1141  for (int i = 0; i < x.size(); i++)
1142  y.e(i) = x.e(i) * alpha;
1143 
1144  return y;
1145  }
1146  // move
1147  template <class Row>
1148  Vector<Row, double> operator*(const double& alpha, Vector<Row, double> &&x) {
1149  for (int i = 0; i < x.size(); i++)
1150  x.e(i) *= alpha;
1151  return std::move(x);
1152  }
1153 
1154  template <class Row, class AT1, class AT2>
1155  inline Vector<Row, AT1> operator*=(Vector<Row, AT1> &x, const AT2 &alpha) {
1156  for (int i = 0; i < x.size(); i++)
1157  x.e(i) *= alpha;
1158  return x;
1159  }
1160 
1161  template <class Row, class AT1, class AT2>
1162  inline Vector<Row, typename OperatorResult<AT1, AT2>::Type> operator/(const Vector<Row, AT1> &x, const AT2 &alpha) {
1163  Vector<Row, typename OperatorResult<AT1, AT2>::Type> y(x.size(), NONINIT);
1164  for (int i = 0; i < x.size(); i++)
1165  y.e(i) = x.e(i) / alpha;
1166  return y;
1167  }
1168  // move
1169  template <class Row>
1170  inline Vector<Row, double> operator/(Vector<Row, double> &&x, const double &alpha) {
1171  for (int i = 0; i < x.size(); i++)
1172  x.e(i) /= alpha;
1173  return std::move(x);
1174  }
1175 
1176  template <class Row, class AT1, class AT2>
1177  inline Vector<Row, AT1> operator/=(Vector<Row, AT1> &x, const AT2 &alpha) {
1178  for (int i = 0; i < x.size(); i++)
1179  x.e(i) /= alpha;
1180  return x;
1181  }
1188  template <class Col, class AT1, class AT2>
1190 
1192 
1193  for (int i = 0; i < x.size(); i++)
1194  y.e(i) = x.e(i) * alpha;
1195 
1196  return y;
1197  }
1198  // move
1199  template <class Col>
1200  RowVector<Col, double> operator*(RowVector<Col, double> &&x, const double& alpha) {
1201  for (int i = 0; i < x.size(); i++)
1202  x.e(i) *= alpha;
1203  return std::move(x);
1204  }
1205 
1210  template <class Col, class AT1, class AT2>
1212 
1214 
1215  for (int i = 0; i < x.size(); i++)
1216  y.e(i) = x.e(i) * alpha;
1217 
1218  return y;
1219  }
1220  // move
1221  template <class Col>
1222  RowVector<Col, double> operator*(const double &alpha, RowVector<Col, double> &&x) {
1223  for (int i = 0; i < x.size(); i++)
1224  x.e(i) *= alpha;
1225  return std::move(x);
1226  }
1227 
1228  template <class Col, class AT1, class AT2>
1229  inline RowVector<Col, AT1> operator*=(RowVector<Col, AT1> &x, const AT2 &alpha) {
1230  for (int i = 0; i < x.size(); i++)
1231  x.e(i) *= alpha;
1232  return x;
1233  }
1234 
1235  template <class Col, class AT1, class AT2>
1236  inline RowVector<Col, typename OperatorResult<AT1, AT2>::Type> operator/(const RowVector<Col, AT1> &x, const AT2 &a) {
1237  RowVector<Col, typename OperatorResult<AT1, AT2>::Type> y(x.size(), NONINIT);
1238  for (int i = 0; i < x.size(); i++)
1239  y.e(i) = x.e(i) / a;
1240  return y;
1241  }
1242  // move
1243  template <class Col>
1244  inline RowVector<Col, double> operator/(RowVector<Col, double> &&x, const double &a) {
1245  for (int i = 0; i < x.size(); i++)
1246  x.e(i) /= a;
1247  return std::move(x);
1248  }
1249 
1250  template <class Col, class AT1, class AT2>
1251  inline RowVector<Col, AT1> operator/=(RowVector<Col, AT1> &x, const AT2 &a) {
1252  for (int i = 0; i < x.size(); i++)
1253  x.e(i) /= a;
1254  return x;
1255  }
1256 
1258 
1260 
1267  template <class Col, class Row, class AT1, class AT2>
1269 
1270  FMATVEC_ASSERT(x.size() == y.size(), AT2);
1271 
1272  typename OperatorResult<AT1, AT2>::Type res = 0;
1273 
1274  for (int i = 0; i < x.size(); i++)
1275  res += x.e(i) * y.e(i);
1276 
1277  return res;
1278  }
1279 
1285  template <class Row, class AT>
1287 
1288  FMATVEC_ASSERT(x.size() == y.size(), AT);
1289 
1290  AT res = 0;
1291 
1292  for (int i = 0; i < x.size(); i++)
1293  res += x.e(i) * y.e(i);
1294 
1295  return res;
1296  }
1297 
1303  template <class Row1, class Row2, class AT1, class AT2>
1305 
1306  FMATVEC_ASSERT(x.size() == 3, AT2);
1307  FMATVEC_ASSERT(y.size() == 3, AT2);
1308 
1309  Vector<Fixed<3>, typename OperatorResult<AT1, AT2>::Type> z(3, NONINIT);
1310 
1311  z.e(0) = x.e(1) * y.e(2) - x.e(2) * y.e(1);
1312  z.e(1) = x.e(2) * y.e(0) - x.e(0) * y.e(2);
1313  z.e(2) = x.e(0) * y.e(1) - x.e(1) * y.e(0);
1314 
1315  return z;
1316  }
1317 
1323  template <class Row, class AT>
1324  double tripleProduct(const Vector<Row, AT> &a, const Vector<Row, AT> &x, const Vector<Row, AT> &y) {
1325 
1326  FMATVEC_ASSERT(a.size() == 3, AT);
1327  FMATVEC_ASSERT(x.size() == 3, AT);
1328  FMATVEC_ASSERT(y.size() == 3, AT);
1329 
1330  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));
1331  }
1332 
1334  //
1336 
1345  template <class Type, class Row, class Col, class AT1, class AT2>
1347 
1349 
1350  for (int i = 0; i < A.rows(); i++)
1351  for (int j = 0; j < A.cols(); j++)
1352  B.e(i, j) = A.e(i, j) * alpha;
1353 
1354  return B;
1355  }
1356  // move
1357  template <class Type, class Row, class Col>
1358  Matrix<Type, Row, Col, double> operator*(Matrix<Type, Row, Col, double> &&A, const double &alpha) {
1359  for (int i = 0; i < A.rows(); i++)
1360  for (int j = 0; j < A.cols(); j++)
1361  A.e(i, j) *= alpha;
1362  return std::move(A);
1363  }
1364 
1365  template <class Type, class Row, class Col, class AT1, class AT2>
1366  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const Matrix<Type, Row, Col, AT2> &A) {
1367 
1368  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1369 
1370  for (int i = 0; i < A.rows(); i++)
1371  for (int j = 0; j < A.cols(); j++)
1372  B.e(i, j) = A.e(i, j) * alpha;
1373 
1374  return B;
1375  }
1376  // move
1377  template <class Type, class Row, class Col>
1378  Matrix<Type, Row, Col, double> operator*(const double &alpha, Matrix<Type, Row, Col, double> &&A) {
1379  for (int i = 0; i < A.rows(); i++)
1380  for (int j = 0; j < A.cols(); j++)
1381  A.e(i, j) *= alpha;
1382  return std::move(A);
1383  }
1384 
1385  template <class Row, class AT1, class AT2>
1386  inline Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const Matrix<Symmetric, Row, Row, AT2> &A) {
1387  Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1388  for (int i = 0; i < A.size(); i++)
1389  for (int j = i; j < A.size(); j++)
1390  B.ej(i, j) = A.ej(i, j) * alpha;
1391  return B;
1392  }
1393  // move
1394  template <class Row>
1395  inline Matrix<Symmetric, Row, Row, double> operator*(const double &alpha, Matrix<Symmetric, Row, Row, double> &&A) {
1396  for (int i = 0; i < A.size(); i++)
1397  for (int j = i; j < A.size(); j++)
1398  A.ej(i, j) *= alpha;
1399  return std::move(A);
1400  }
1401 
1402  template <class Row, class AT1, class AT2>
1403  inline Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1404  Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1405  for (int i = 0; i < A.size(); i++)
1406  for (int j = i; j < A.size(); j++)
1407  B.ej(i, j) = A.ej(i, j) * alpha;
1408  return B;
1409  }
1410  // move
1411  template <class Row>
1412  inline Matrix<Symmetric, Row, Row, double> operator*(Matrix<Symmetric, Row, Row, double> &&A, const double &alpha) {
1413  for (int i = 0; i < A.size(); i++)
1414  for (int j = i; j < A.size(); j++)
1415  A.ej(i, j) *= alpha;
1416  return std::move(A);
1417  }
1418 
1419  template <class AT1, class AT2>
1420  inline Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const Matrix<Diagonal, Ref, Ref, AT2> &A) {
1421  Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1422  for (int i = 0; i < A.rows(); i++)
1423  B.e(i) = A.e(i) * alpha;
1424  return B;
1425  }
1426  // move
1427  inline Matrix<Diagonal, Ref, Ref, double> operator*(const double &alpha, Matrix<Diagonal, Ref, Ref, double> &&A) {
1428  for (int i = 0; i < A.rows(); i++)
1429  A.e(i) *= alpha;
1430  return A;
1431  }
1432 
1433  template <class AT1, class AT2>
1434  inline Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> operator*(const Matrix<Diagonal, Ref, Ref, AT1> &A, const AT2 &alpha) {
1435  Matrix<Diagonal, Ref, Ref, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1436  for (int i = 0; i < A.size(); i++)
1437  B.e(i) = A.e(i) * alpha;
1438  return B;
1439  }
1440  // move
1441  inline Matrix<Diagonal, Ref, Ref, double> operator*(Matrix<Diagonal, Ref, Ref, double> &&A, const double &alpha) {
1442  for (int i = 0; i < A.size(); i++)
1443  A.e(i) *= alpha;
1444  return A;
1445  }
1446 
1447  template <class Type, class Row, class Col, class AT1, class AT2>
1448  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator/(const Matrix<Type, Row, Col, AT1> &A, const AT2 &alpha) {
1449 
1450  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> B(A.rows(), A.cols(), NONINIT);
1451 
1452  for (int i = 0; i < A.rows(); i++)
1453  for (int j = 0; j < A.cols(); j++)
1454  B.e(i, j) = A.e(i, j) / alpha;
1455 
1456  return B;
1457  }
1458  // move
1459  template <class Type, class Row, class Col>
1460  Matrix<Type, Row, Col, double> operator/(Matrix<Type, Row, Col, double> &&A, const double &alpha) {
1461  for (int i = 0; i < A.rows(); i++)
1462  for (int j = 0; j < A.cols(); j++)
1463  A.e(i, j) /= alpha;
1464  return std::move(A);
1465  }
1466 
1467  template <class Row, class AT1, class AT2>
1468  inline Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator/(const Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1469  Matrix<Symmetric, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1470  for (int i = 0; i < A.size(); i++)
1471  for (int j = i; j < A.size(); j++)
1472  B.ej(i, j) = A.ej(i, j) / alpha;
1473  return B;
1474  }
1475  // move
1476  template <class Row>
1477  inline Matrix<Symmetric, Row, Row, double> operator/(Matrix<Symmetric, Row, Row, double> &&A, const double &alpha) {
1478  for (int i = 0; i < A.size(); i++)
1479  for (int j = i; j < A.size(); j++)
1480  A.ej(i, j) /= alpha;
1481  return std::move(A);
1482  }
1483 
1484  template <class Row, class AT1, class AT2>
1485  inline Matrix<Diagonal, Row, Row, typename OperatorResult<AT1, AT2>::Type> operator/(const Matrix<Diagonal, Row, Row, AT1> &A, const AT2 &alpha) {
1486  Matrix<Diagonal, Row, Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), A.size(), NONINIT);
1487  for (int i = 0; i < A.size(); i++)
1488  B.e(i) = A.e(i) / alpha;
1489  return B;
1490  }
1491  // move
1492  template <class Row>
1493  inline Matrix<Diagonal, Row, Row, double> operator/(Matrix<Diagonal, Row, Row, double> &&A, const double &alpha) {
1494  for (int i = 0; i < A.size(); i++)
1495  A.e(i) /= alpha;
1496  return std::move(A);
1497  }
1498 
1499  template <class Row, class AT1, class AT2>
1500  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const SquareMatrix<Row, AT1> &A, const AT2 &alpha) {
1501 
1502  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), NONINIT);
1503 
1504  for (int i = 0; i < A.size(); i++)
1505  for (int j = 0; j < A.size(); j++)
1506  B.e(i, j) = A.e(i, j) * alpha;
1507 
1508  return B;
1509  }
1510  // move
1511  template <class Row>
1512  SquareMatrix<Row, double> operator*(SquareMatrix<Row, double> &&A, const double &alpha) {
1513  for (int i = 0; i < A.size(); i++)
1514  for (int j = 0; j < A.size(); j++)
1515  A.e(i, j) *= alpha;
1516  return std::move(A);
1517  }
1518 
1519  template <class Row, class AT1, class AT2>
1520  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator*(const AT1 &alpha, const SquareMatrix<Row, AT2> &A) {
1521 
1522  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), NONINIT);
1523 
1524  for (int i = 0; i < A.size(); i++)
1525  for (int j = 0; j < A.size(); j++)
1526  B.e(i, j) = A.e(i, j) * alpha;
1527 
1528  return B;
1529  }
1530  // move
1531  template <class Row>
1532  SquareMatrix<Row, double> operator*(const double &alpha, SquareMatrix<Row, double> &&A) {
1533  for (int i = 0; i < A.size(); i++)
1534  for (int j = 0; j < A.size(); j++)
1535  A.e(i, j) *= alpha;
1536  return std::move(A);
1537  }
1538 
1539  template <class Row, class AT1, class AT2>
1540  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> operator/(const SquareMatrix<Row, AT1> &A, const AT2 &alpha) {
1541 
1542  SquareMatrix<Row, typename OperatorResult<AT1, AT2>::Type> B(A.size(), NONINIT);
1543 
1544  for (int i = 0; i < A.size(); i++)
1545  for (int j = 0; j < A.size(); j++)
1546  B.e(i, j) = A.e(i, j) / alpha;
1547 
1548  return B;
1549  }
1550  // move
1551  template <class Row>
1552  SquareMatrix<Row, double> operator/(SquareMatrix<Row, double> &&A, const double &alpha) {
1553  for (int i = 0; i < A.size(); i++)
1554  for (int j = 0; j < A.size(); j++)
1555  A.e(i, j) /= alpha;
1556  return std::move(A);
1557  }
1558 
1559  template <class Type, class Row, class Col, class AT1, class AT2>
1560  Matrix<Type, Row, Col, typename OperatorResult<AT1, AT2>::Type> operator*=(Matrix<Type, Row, Col, AT1> &A, const AT2 &alpha) {
1561  for (int i = 0; i < A.rows(); i++)
1562  for (int j = 0; j < A.cols(); j++)
1563  A.e(i, j) *= alpha;
1564 
1565  return A;
1566  }
1567 
1568  template <class Row, class AT1, class AT2>
1569  inline Matrix<Symmetric, Row, Row, AT1> operator*=(Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1570  for (int i = 0; i < A.size(); i++)
1571  for (int j = i; j < A.size(); j++)
1572  A.ej(i, j) *= alpha;
1573  return A;
1574  }
1575 
1576  template <class Row, class AT1, class AT2>
1577  inline Matrix<Diagonal, Row, Row, AT1> operator*=(Matrix<Diagonal, Row, Row, AT1> &A, const AT2 &alpha) {
1578  for (int i = 0; i < A.size(); i++)
1579  A.e(i) *= alpha;
1580  return A;
1581  }
1582 
1583  template <class Type, class Row, class Col, class AT1, class AT2>
1584  Matrix<Type, Row, Col, AT1> operator/=(Matrix<Type, Row, Col, AT1> &A, const AT2 &alpha) {
1585  for (int i = 0; i < A.rows(); i++)
1586  for (int j = 0; j < A.cols(); j++)
1587  A.e(i, j) /= alpha;
1588 
1589  return A;
1590  }
1591 
1592  template <class Row, class AT1, class AT2>
1593  inline Matrix<Symmetric, Row, Row, AT1> operator/=(Matrix<Symmetric, Row, Row, AT1> &A, const AT2 &alpha) {
1594  for (int i = 0; i < A.size(); i++)
1595  for (int j = i; j < A.size(); j++)
1596  A.ej(i, j) /= alpha;
1597  return A;
1598  }
1599 
1600  template <class Row, class AT1, class AT2>
1601  inline Matrix<Diagonal, Row, Row, AT1> operator/=(Matrix<Diagonal, Row, Row, AT1> &A, const AT2 &alpha) {
1602  for (int i = 0; i < A.size(); i++)
1603  A.e(i) /= alpha;
1604  return A;
1605  }
1606 
1608 
1610 
1616  template <class Row, class AT>
1618 
1620 
1621  for (int i = 0; i < x.size(); i++)
1622  y.e(i) = -x.e(i);
1623 
1624  return y;
1625  }
1626  // move
1627  template <class Row>
1628  Vector<Row, double> operator-(Vector<Row, double> &&x) {
1629  for (int i = 0; i < x.size(); i++)
1630  x.e(i) = -x.e(i);
1631  return std::move(x);
1632  }
1633 
1639  template <class Col, class AT>
1641 
1643 
1644  for (int i = 0; i < a.size(); i++)
1645  c.e(i) = -a.e(i);
1646 
1647  return c;
1648  }
1649  // move
1650  template <class Col>
1651  RowVector<Col, double> operator-(RowVector<Col, double> &&a) {
1652  for (int i = 0; i < a.size(); i++)
1653  a.e(i) = -a.e(i);
1654  return std::move(a);
1655  }
1656 
1662  template <class Row, class AT>
1664 
1666 
1667  for (int i = 0; i < A.size(); i++)
1668  for (int j = 0; j < A.size(); j++)
1669  B.e(i, j) = -A.e(i, j);
1670 
1671  return B;
1672  }
1673  // move
1674  template <class Row>
1675  SquareMatrix<Row, double> operator-(SquareMatrix<Row, double> &&A) {
1676  for (int i = 0; i < A.size(); i++)
1677  for (int j = 0; j < A.size(); j++)
1678  A.e(i, j) = -A.e(i, j);
1679  return std::move(A);
1680  }
1681 
1687  template <class Type, class Row, class Col, class AT>
1689 
1691 
1692  for (int i = 0; i < A.rows(); i++)
1693  for (int j = 0; j < A.cols(); j++)
1694  B.e(i, j) = -A.e(i, j);
1695 
1696  return B;
1697  }
1698  // move
1699  template <class Type, class Row, class Col>
1700  Matrix<Type, Row, Col, double> operator-(Matrix<Type, Row, Col, double> &&A) {
1701  for (int i = 0; i < A.rows(); i++)
1702  for (int j = 0; j < A.cols(); j++)
1703  A.e(i, j) = -A.e(i, j);
1704  return std::move(A);
1705  }
1706 
1709 
1715  template <class Row, class AT>
1716  RowVector<Row,AT> trans(const Vector<Row,AT> &x) { return x.T(); }
1717  // move
1718  inline RowVector<Var,double> trans(Vector<Var,double> &&x) { return std::move(x).T(); }
1719 
1727  template <class Col, class AT>
1728  Vector<Col,AT> trans(const RowVector<Col,AT> &x) { return x.T(); }
1729  // move
1730  inline Vector<Var,double> trans(RowVector<Var,double> &&x) { return std::move(x).T(); }
1731 
1740  template <class Type, class Row, class Col, class AT>
1742 
1751  template <class Row, class AT>
1753 
1754 // template <class Type, class Row, class Col, class AT>
1755 // inline Matrix<Type, Col, Row, AT> trans(const Matrix<Type, Row, Col, AT> &A) {
1756 // Matrix<Type, Col, Row, AT> B(A.cols(), A.rows(), NONINIT);
1757 // for (int i = 0; i < B.rows(); i++)
1758 // for (int j = 0; j < B.cols(); j++)
1759 // B.e(i, j) = A.e(j, i);
1760 // return B;
1761 // }
1762 //
1763 // template <class Row, class AT>
1764 // inline SquareMatrix<Row, AT> trans(const SquareMatrix<Row, AT> &A) {
1765 // SquareMatrix<Row, AT> B(A.size(), NONINIT);
1766 // for (int i = 0; i < B.size(); i++)
1767 // for (int j = 0; j < B.size(); j++)
1768 // B.e(i, j) = A.e(j, i);
1769 // return B;
1770 // }
1771 //
1772 // template <class Row, class AT>
1773 // inline RowVector<Row, AT> trans(const Vector<Row, AT> &x) {
1774 // RowVector<Row, AT> y(x.size(), NONINIT);
1775 // for (int i = 0; i < y.size(); i++)
1776 // y.e(i) = x.e(i);
1777 // return y;
1778 // }
1779 //
1780 // template <class Row, class AT>
1781 // inline Vector<Row, AT> trans(const RowVector<Row, AT> &x) {
1782 // Vector<Row, AT> y(x.size(), NONINIT);
1783 // for (int i = 0; i < y.size(); i++)
1784 // y.e(i) = x.e(i);
1785 // return y;
1786 // }
1787 
1789 
1791  template <class Row, class AT>
1792  inline AT nrmInf(const Vector<Row, AT> &x) {
1793  AT c = 0;
1794  for (int i = 0; i < x.size(); i++)
1795  if (c < fabs(x.e(i)))
1796  c = fabs(x.e(i));
1797  return c;
1798  }
1799 
1800  template <class Row, class AT>
1801  inline AT nrmInf(const RowVector<Row, AT> &x) {
1802  AT c = 0;
1803  for (int i = 0; i < x.size(); i++)
1804  if (c < fabs(x.e(i)))
1805  c = fabs(x.e(i));
1806  return c;
1807  }
1808 
1809  template <class Row, class AT>
1810  inline typename OperatorResult<AT, AT>::Type nrm2(const Vector<Row, AT> &x) {
1811  typename OperatorResult<AT, AT>::Type c=typename OperatorResult<AT, AT>::Type();
1812  for (int i = 0; i < x.size(); i++)
1813  c += pow(x.e(i), 2);
1814  return sqrt(c);
1815  }
1816 
1817  template <class Col, class AT>
1818  inline typename OperatorResult<AT, AT>::Type nrm2(const RowVector<Col, AT> &x) {
1819  typename OperatorResult<AT, AT>::Type c=typename OperatorResult<AT, AT>::Type();
1820  for (int i = 0; i < x.size(); i++)
1821  c += pow(x.e(i), 2);
1822  return sqrt(c);
1823  }
1824 
1826 
1838  template <class Row, class AT>
1840 
1841  SquareMatrix<Row, AT> B(x.size(), NONINIT);
1842 
1843  B.e(0, 0) = 0.0;
1844  B.e(1, 1) = 0.0;
1845  B.e(2, 2) = 0.0;
1846  B.e(0, 1) = -x.e(2);
1847  B.e(0, 2) = x.e(1);
1848  B.e(1, 0) = x.e(2);
1849  B.e(1, 2) = -x.e(0);
1850  B.e(2, 0) = -x.e(1);
1851  B.e(2, 1) = x.e(0);
1852 
1853  return B;
1854  }
1855 
1861  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
1863 
1864  if (&A == &B)
1865  return true;
1866 
1867  if (A.rows() != B.rows() || A.cols() != B.cols())
1868  return false;
1869 
1870  for (int i = 0; i < A.rows(); i++)
1871  for (int j = 0; j < A.cols(); j++)
1872  if (A(i, j) != B(i, j))
1873  return false;
1874 
1875  return true;
1876  }
1877 
1883  template <class AT, class Type1, class Row1, class Col1, class Type2, class Row2, class Col2>
1885 
1886  if (A.rows() != B.rows() || A.cols() != B.cols())
1887  return true;
1888 
1889  for (int i = 0; i < A.rows(); i++)
1890  for (int j = 0; j < A.cols(); j++)
1891  if (A(i, j) != B(i, j))
1892  return true;
1893 
1894  return false;
1895  }
1896 
1902  template <class Row, class AT>
1903  AT max(const Vector<Row, AT> &x) {
1904 
1905  FMATVEC_ASSERT(x.size() > 0, AT);
1906  AT maximum = x.e(0);
1907  for (int i = 1; i < x.size(); i++) {
1908  if (x.e(i) > maximum)
1909  maximum = x.e(i);
1910  }
1911  return maximum;
1912  }
1913 
1919  template <class Row, class AT>
1920  int maxIndex(const Vector<Row, AT> &x) {
1921 
1922  FMATVEC_ASSERT(x.size() > 0, AT);
1923  AT maximum = x.e(0);
1924  int index = 0;
1925  for (int i = 1; i < x.size(); i++) {
1926  if (x.e(i) > maximum) {
1927  maximum = x.e(i);
1928  index = i;
1929  }
1930  }
1931  return index;
1932  }
1933 
1939  template <class Row, class AT>
1940  AT min(const Vector<Row, AT> &x) {
1941 
1942  FMATVEC_ASSERT(x.size() > 0, AT);
1943  AT minimum = x.e(0);
1944  for (int i = 1; i < x.size(); i++) {
1945  if (x.e(i) < minimum)
1946  minimum = x.e(i);
1947  }
1948  return minimum;
1949  }
1950 
1956  template <class Row, class AT>
1957  int minIndex(const Vector<Row, AT> &x) {
1958 
1959  FMATVEC_ASSERT(x.size() > 0, AT);
1960  AT minimum = x.e(0);
1961  int index = 0;
1962  for (int i = 1; i < x.size(); i++) {
1963  if (x.e(i) < minimum) {
1964  minimum = x.e(i);
1965  index = i;
1966  }
1967  }
1968  return index;
1969  }
1970 
1971  // HR 28.09.2006
1978  template <class AT>
1980 
1981  FMATVEC_ASSERT(A_.rows() > 0, AT);
1982  FMATVEC_ASSERT(A_.cols() > 0, AT);
1983  FMATVEC_ASSERT(A_.cols() > PivotCol, AT);
1985  int i, j, N;
1986  RowVector<Ref, AT> tmp(A.cols(), NONINIT);
1987  N = A.rows();
1988  for (i = 1; i <= N - 1; i++) {
1989  for (j = 0; j < N - 1; j++) {
1990  if (A(j, PivotCol) > A(j + 1, PivotCol)) {
1991  tmp = A.row(j);
1992  A.set(j, A.row(j + 1));
1993  A.set(j + 1, tmp);
1994  }
1995  }
1996  }
1997  return A;
1998  }
1999 
2000  // HR 28.09.2006
2002  template <class AT>
2003  void quicksortmedian_intern(Matrix<General, Ref, Ref, AT> &A, int PivotCol, RowVector<Ref, AT> &tmp, int l, int r) {
2004 
2005  if (r > l) {
2006  int i = l - 1, j = r;
2007  //RowVec tmp;
2008  if (r - l > 3) { //Median of three
2009  int m = l + (r - l) / 2;
2010  if (A(l, PivotCol) > A(m, PivotCol)) {
2011  tmp = A.row(l);
2012  A.set(l, A.row(m));
2013  A.set(m, tmp);
2014  }
2015  if (A(l, PivotCol) > A(r, PivotCol)) {
2016  tmp = A.row(l);
2017  A.set(l, A.row(r));
2018  A.set(r, tmp);
2019  }
2020  else if (A(r, PivotCol) > A(m, PivotCol)) {
2021  tmp = A.row(r);
2022  A.set(r, A.row(m));
2023  A.set(m, tmp);
2024  }
2025  }
2026 
2027  for (;;) {
2028  while(A(++i,PivotCol) < A(r,PivotCol));
2029  while(A(--j,PivotCol) > A(r,PivotCol) && j>i);
2030  if(i>=j) break;
2031  tmp = A.row(i);
2032  A.set(i, A.row(j));
2033  A.set(j, tmp);
2034  }
2035  tmp = A.row(i);
2036  A.set(i, A.row(r));
2037  A.set(r, tmp);
2038  quicksortmedian_intern(A, PivotCol, tmp, l, i - 1);
2039  quicksortmedian_intern(A, PivotCol, tmp, i + 1, r);
2040  }
2041  }
2042 
2043  // HR 28.09.2006
2052  template <class AT>
2055  int N = A.rows();
2056  RowVector<Ref, AT> tmp(A.cols(), NONINIT);
2057  quicksortmedian_intern(A, PivotCol, tmp, 0, N - 1);
2058  return A;
2059  }
2060 
2061  template <class Row, class Col, class AT>
2062  inline Matrix<Symmetric, Col, Col, AT> JTJ(const Matrix<General, Row, Col, AT> &A) {
2063  Matrix<Symmetric, Col, Col, AT> S(A.cols(), A.cols(), NONINIT);
2064  for (int i = 0; i < A.cols(); i++) {
2065  for (int k = i; k < A.cols(); k++) {
2066  S.ej(i, k) = 0;
2067  for (int j = 0; j < A.rows(); j++)
2068  S.ej(i, k) += A.e(j, i) * A.e(j, k);
2069  }
2070  }
2071  return S;
2072  }
2073 
2074  template <class Row1, class Row2, class Col, class AT>
2075  inline Matrix<Symmetric, Col, Col, AT> JTMJ(const Matrix<Symmetric, Row2, Row2, AT> &B, const Matrix<General, Row1, Col, AT> &A) {
2076 
2077  Matrix<Symmetric, Col, Col, AT> S(A.cols(), A.cols(), NONINIT);
2078  Matrix<General, Row1, Col, AT> C = B * A;
2079 
2080  for (int i = 0; i < A.cols(); i++) {
2081  for (int k = i; k < A.cols(); k++) {
2082  S.ej(i, k) = 0;
2083  for (int j = 0; j < A.rows(); j++)
2084  S.ej(i, k) += A.e(j, i) * C.e(j, k);
2085  }
2086  }
2087  return S;
2088  }
2089 
2090  template <class Row, class Col, class AT>
2091  inline Matrix<Symmetric, Col, Col, AT> JTMJ(const Matrix<SymmetricSparse, Ref, Ref, AT> &B, const Matrix<General, Row, Col, AT> &A) {
2092 
2093  Matrix<Symmetric, Col, Col, AT> S(A.cols(), A.cols(), NONINIT);
2094  Matrix<General, Row, Col, AT> C = B * A;
2095 
2096  for (int i = 0; i < A.cols(); i++) {
2097  for (int k = i; k < A.cols(); k++) {
2098  S.ej(i, k) = 0;
2099  for (int j = 0; j < A.rows(); j++)
2100  S.ej(i, k) += A.e(j, i) * C.e(j, k);
2101  }
2102  }
2103  return S;
2104  }
2105 
2106  template <class Row, class Col1, class Col2, class AT>
2107  inline Matrix<Symmetric, Row, Row, AT> JMJT(const Matrix<General, Row, Col1, AT> &A, const Matrix<Symmetric, Col2, Col2, AT> &B) {
2108 
2109  Matrix<Symmetric, Row, Row, AT> S(A.rows(), A.rows(), NONINIT);
2110  Matrix<General, Row, Col1, AT> C = A * B;
2111 
2112  for (int i = 0; i < S.size(); i++) {
2113  for (int k = i; k < S.size(); k++) {
2114  S.ej(i, k) = 0;
2115  for (int j = 0; j < A.cols(); j++)
2116  S.ej(i, k) += C.e(k, j) * A.e(i, j);
2117  }
2118  }
2119  return S;
2120  }
2121 
2122 }
2123 
2124 #endif
RowVector< Row, AT > trans(const Vector< Row, AT > &x)
Transpose of a vector.
Definition: linear_algebra.h:1716
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:2053
This is the basic matrix class for arbitrary matrices.
Definition: fmatvec.h:42
int rows() const
Number of rows.
Definition: general_matrix.h:307
void quicksortmedian_intern(Matrix< General, Ref, Ref, AT > &A, int PivotCol, RowVector< Ref, AT > &tmp, int l, int r)
Definition: linear_algebra.h:2003
int minIndex(const Vector< Row, AT > &x)
Index of minimum value.
Definition: linear_algebra.h:1957
Definition: fmatvec.h:51
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:1304
This is a rowvector class of general shape in dense storage format.
Definition: row_vector.h:36
Definition: fmatvec.h:45
Definition: fmatvec.h:48
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:1979
int maxIndex(const Vector< Row, AT > &x)
Index of maximum value.
Definition: linear_algebra.h:1920
int cols() const
Number of columns.
This is a matrix class for general matrices.
Definition: general_matrix.h:42
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:1862
int cols() const
Number of columns.
Definition: general_matrix.h:313
const RowVector< Ref, AT > row(int i) const
Row operator.
Definition: general_matrix.h:525
bool operator!=(const Matrix< Type1, Row1, Col1, AT > &A, const Matrix< Type2, Row2, Col2, AT > &B)
Matrix-matrix comparison.
Definition: linear_algebra.h:1884
Definition: types.h:194
double tripleProduct(const Vector< Row, AT > &a, const Vector< Row, AT > &x, const Vector< Row, AT > &y)
Triple product.
Definition: linear_algebra.h:1324
SquareMatrix< Row, AT > tilde(const Vector< Row, AT > &x)
Tilde operator.
Definition: linear_algebra.h:1839
int rows() const
Number of rows.
AT scalarProduct(const Vector< Row, AT > &x, const Vector< Row, AT > &y)
Scalar product.
Definition: linear_algebra.h:1286