2 // Copyright (c) 2000-2002
3 // Joerg Walter, Mathias Koch
5 // Distributed under the Boost Software License, Version 1.0. (See
6 // accompanying file LICENSE_1_0.txt or copy at
7 // http://www.boost.org/LICENSE_1_0.txt)
9 // The authors gratefully acknowledge the support of
10 // GeNeSys mbH & Co. KG in producing this work.
13 #ifndef _BOOST_UBLAS_BLAS_
14 #define _BOOST_UBLAS_BLAS_
16 #include <boost/numeric/ublas/traits.hpp>
18 namespace boost { namespace numeric { namespace ublas {
21 /** Interface and implementation of BLAS level 1
22 * This includes functions which perform \b vector-vector operations.
23 * More information about BLAS can be found at
24 * <a href="http://en.wikipedia.org/wiki/BLAS">http://en.wikipedia.org/wiki/BLAS</a>
28 /** 1-Norm: \f$\sum_i |x_i|\f$ (also called \f$\mathcal{L}_1\f$ or Manhattan norm)
30 * \param v a vector or vector expression
31 * \return the 1-Norm with type of the vector's type
33 * \tparam V type of the vector (not needed by default)
36 typename type_traits<typename V::value_type>::real_type
41 /** 2-Norm: \f$\sum_i |x_i|^2\f$ (also called \f$\mathcal{L}_2\f$ or Euclidean norm)
43 * \param v a vector or vector expression
44 * \return the 2-Norm with type of the vector's type
46 * \tparam V type of the vector (not needed by default)
49 typename type_traits<typename V::value_type>::real_type
54 /** Infinite-norm: \f$\max_i |x_i|\f$ (also called \f$\mathcal{L}_\infty\f$ norm)
56 * \param v a vector or vector expression
57 * \return the Infinite-Norm with type of the vector's type
59 * \tparam V type of the vector (not needed by default)
62 typename type_traits<typename V::value_type>::real_type
67 /** Inner product of vectors \f$v_1\f$ and \f$v_2\f$
69 * \param v1 first vector of the inner product
70 * \param v2 second vector of the inner product
71 * \return the inner product of the type of the most generic type of the 2 vectors
73 * \tparam V1 type of first vector (not needed by default)
74 * \tparam V2 type of second vector (not needed by default)
76 template<class V1, class V2>
77 typename promote_traits<typename V1::value_type, typename V2::value_type>::promote_type
78 dot (const V1 &v1, const V2 &v2) {
79 return inner_prod (v1, v2);
82 /** Copy vector \f$v_2\f$ to \f$v_1\f$
84 * \param v1 target vector
85 * \param v2 source vector
86 * \return a reference to the target vector
88 * \tparam V1 type of first vector (not needed by default)
89 * \tparam V2 type of second vector (not needed by default)
91 template<class V1, class V2>
92 V1 & copy (V1 &v1, const V2 &v2)
94 return v1.assign (v2);
97 /** Swap vectors \f$v_1\f$ and \f$v_2\f$
99 * \param v1 first vector
100 * \param v2 second vector
102 * \tparam V1 type of first vector (not needed by default)
103 * \tparam V2 type of second vector (not needed by default)
105 template<class V1, class V2>
106 void swap (V1 &v1, V2 &v2)
111 /** scale vector \f$v\f$ with scalar \f$t\f$
113 * \param v vector to be scaled
114 * \param t the scalar
117 * \tparam V type of the vector (not needed by default)
118 * \tparam T type of the scalar (not needed by default)
120 template<class V, class T>
121 V & scal (V &v, const T &t)
126 /** Compute \f$v_1= v_1 + t.v_2\f$
128 * \param v1 target and first vector
129 * \param t the scalar
130 * \param v2 second vector
131 * \return a reference to the first and target vector
133 * \tparam V1 type of the first vector (not needed by default)
134 * \tparam T type of the scalar (not needed by default)
135 * \tparam V2 type of the second vector (not needed by default)
137 template<class V1, class T, class V2>
138 V1 & axpy (V1 &v1, const T &t, const V2 &v2)
140 return v1.plus_assign (t * v2);
143 /** Performs rotation of points in the plane and assign the result to the first vector
145 * Each point is defined as a pair \c v1(i) and \c v2(i), being respectively
146 * the \f$x\f$ and \f$y\f$ coordinates. The parameters \c t1 and \t2 are respectively
147 * the cosine and sine of the angle of the rotation.
148 * Results are not returned but directly written into \c v1.
150 * \param t1 cosine of the rotation
151 * \param v1 vector of \f$x\f$ values
152 * \param t2 sine of the rotation
153 * \param v2 vector of \f$y\f$ values
155 * \tparam T1 type of the cosine value (not needed by default)
156 * \tparam V1 type of the \f$x\f$ vector (not needed by default)
157 * \tparam T2 type of the sine value (not needed by default)
158 * \tparam V2 type of the \f$y\f$ vector (not needed by default)
160 template<class T1, class V1, class T2, class V2>
161 void rot (const T1 &t1, V1 &v1, const T2 &t2, V2 &v2)
163 typedef typename promote_traits<typename V1::value_type, typename V2::value_type>::promote_type promote_type;
164 vector<promote_type> vt (t1 * v1 + t2 * v2);
165 v2.assign (- t2 * v1 + t1 * v2);
171 /** \brief Interface and implementation of BLAS level 2
172 * This includes functions which perform \b matrix-vector operations.
173 * More information about BLAS can be found at
174 * <a href="http://en.wikipedia.org/wiki/BLAS">http://en.wikipedia.org/wiki/BLAS</a>
178 /** \brief multiply vector \c v with triangular matrix \c m
181 * \param m a triangular matrix
182 * \return the result of the product
184 * \tparam V type of the vector (not needed by default)
185 * \tparam M type of the matrix (not needed by default)
187 template<class V, class M>
188 V & tmv (V &v, const M &m)
190 return v = prod (m, v);
193 /** \brief solve \f$m.x = v\f$ in place, where \c m is a triangular matrix
197 * \param C (this parameter is not needed)
198 * \return a result vector from the above operation
200 * \tparam V type of the vector (not needed by default)
201 * \tparam M type of the matrix (not needed by default)
204 template<class V, class M, class C>
205 V & tsv (V &v, const M &m, C)
207 return v = solve (m, v, C ());
210 /** \brief compute \f$ v_1 = t_1.v_1 + t_2.(m.v_2)\f$, a general matrix-vector product
214 * \param t2 another scalar
216 * \param v2 another vector
217 * \return the vector \c v1 with the result from the above operation
219 * \tparam V1 type of first vector (not needed by default)
220 * \tparam T1 type of first scalar (not needed by default)
221 * \tparam T2 type of second scalar (not needed by default)
222 * \tparam M type of matrix (not needed by default)
223 * \tparam V2 type of second vector (not needed by default)
225 template<class V1, class T1, class T2, class M, class V2>
226 V1 & gmv (V1 &v1, const T1 &t1, const T2 &t2, const M &m, const V2 &v2)
228 return v1 = t1 * v1 + t2 * prod (m, v2);
231 /** \brief Rank 1 update: \f$ m = m + t.(v_1.v_2^T)\f$
236 * \param v2 another vector
237 * \return a matrix with the result from the above operation
239 * \tparam M type of matrix (not needed by default)
240 * \tparam T type of scalar (not needed by default)
241 * \tparam V1 type of first vector (not needed by default)
242 * \tparam V2type of second vector (not needed by default)
244 template<class M, class T, class V1, class V2>
245 M & gr (M &m, const T &t, const V1 &v1, const V2 &v2)
247 #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
248 return m += t * outer_prod (v1, v2);
250 return m = m + t * outer_prod (v1, v2);
254 /** \brief symmetric rank 1 update: \f$m = m + t.(v.v^T)\f$
259 * \return a matrix with the result from the above operation
261 * \tparam M type of matrix (not needed by default)
262 * \tparam T type of scalar (not needed by default)
263 * \tparam V type of vector (not needed by default)
265 template<class M, class T, class V>
266 M & sr (M &m, const T &t, const V &v)
268 #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
269 return m += t * outer_prod (v, v);
271 return m = m + t * outer_prod (v, v);
275 /** \brief hermitian rank 1 update: \f$m = m + t.(v.v^H)\f$
280 * \return a matrix with the result from the above operation
282 * \tparam M type of matrix (not needed by default)
283 * \tparam T type of scalar (not needed by default)
284 * \tparam V type of vector (not needed by default)
286 template<class M, class T, class V>
287 M & hr (M &m, const T &t, const V &v)
289 #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
290 return m += t * outer_prod (v, conj (v));
292 return m = m + t * outer_prod (v, conj (v));
296 /** \brief symmetric rank 2 update: \f$ m=m+ t.(v_1.v_2^T + v_2.v_1^T)\f$
301 * \param v2 another vector
302 * \return a matrix with the result from the above operation
304 * \tparam M type of matrix (not needed by default)
305 * \tparam T type of scalar (not needed by default)
306 * \tparam V1 type of first vector (not needed by default)
307 * \tparam V2type of second vector (not needed by default)
309 template<class M, class T, class V1, class V2>
310 M & sr2 (M &m, const T &t, const V1 &v1, const V2 &v2)
312 #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
313 return m += t * (outer_prod (v1, v2) + outer_prod (v2, v1));
315 return m = m + t * (outer_prod (v1, v2) + outer_prod (v2, v1));
319 /** \brief hermitian rank 2 update: \f$m=m+t.(v_1.v_2^H) + v_2.(t.v_1)^H)\f$
324 * \param v2 another vector
325 * \return a matrix with the result from the above operation
327 * \tparam M type of matrix (not needed by default)
328 * \tparam T type of scalar (not needed by default)
329 * \tparam V1 type of first vector (not needed by default)
330 * \tparam V2type of second vector (not needed by default)
332 template<class M, class T, class V1, class V2>
333 M & hr2 (M &m, const T &t, const V1 &v1, const V2 &v2)
335 #ifndef BOOST_UBLAS_SIMPLE_ET_DEBUG
336 return m += t * outer_prod (v1, conj (v2)) + type_traits<T>::conj (t) * outer_prod (v2, conj (v1));
338 return m = m + t * outer_prod (v1, conj (v2)) + type_traits<T>::conj (t) * outer_prod (v2, conj (v1));
344 /** \brief Interface and implementation of BLAS level 3
345 * This includes functions which perform \b matrix-matrix operations.
346 * More information about BLAS can be found at
347 * <a href="http://en.wikipedia.org/wiki/BLAS">http://en.wikipedia.org/wiki/BLAS</a>
351 /** \brief triangular matrix multiplication \f$m_1=t.m_2.m_3\f$ where \f$m_2\f$ and \f$m_3\f$ are triangular
353 * \param m1 a matrix for storing result
355 * \param m2 a triangular matrix
356 * \param m3 a triangular matrix
357 * \return the matrix \c m1
359 * \tparam M1 type of the result matrix (not needed by default)
360 * \tparam T type of the scalar (not needed by default)
361 * \tparam M2 type of the first triangular matrix (not needed by default)
362 * \tparam M3 type of the second triangular matrix (not needed by default)
365 template<class M1, class T, class M2, class M3>
366 M1 & tmm (M1 &m1, const T &t, const M2 &m2, const M3 &m3)
368 return m1 = t * prod (m2, m3);
371 /** \brief triangular solve \f$ m_2.x = t.m_1\f$ in place, \f$m_2\f$ is a triangular matrix
375 * \param m2 a triangular matrix
376 * \param C (not used)
377 * \return the \f$m_1\f$ matrix
379 * \tparam M1 type of the first matrix (not needed by default)
380 * \tparam T type of the scalar (not needed by default)
381 * \tparam M2 type of the triangular matrix (not needed by default)
384 template<class M1, class T, class M2, class C>
385 M1 & tsm (M1 &m1, const T &t, const M2 &m2, C)
387 return m1 = solve (m2, t * m1, C ());
390 /** \brief general matrix multiplication \f$m_1=t_1.m_1 + t_2.m_2.m_3\f$
392 * \param m1 first matrix
393 * \param t1 first scalar
394 * \param t2 second scalar
395 * \param m2 second matrix
396 * \param m3 third matrix
397 * \return the matrix \c m1
399 * \tparam M1 type of the first matrix (not needed by default)
400 * \tparam T1 type of the first scalar (not needed by default)
401 * \tparam T2 type of the second scalar (not needed by default)
402 * \tparam M2 type of the second matrix (not needed by default)
403 * \tparam M3 type of the third matrix (not needed by default)
405 template<class M1, class T1, class T2, class M2, class M3>
406 M1 & gmm (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2, const M3 &m3)
408 return m1 = t1 * m1 + t2 * prod (m2, m3);
411 /** \brief symmetric rank \a k update: \f$m_1=t.m_1+t_2.(m_2.m_2^T)\f$
413 * \param m1 first matrix
414 * \param t1 first scalar
415 * \param t2 second scalar
416 * \param m2 second matrix
417 * \return matrix \c m1
419 * \tparam M1 type of the first matrix (not needed by default)
420 * \tparam T1 type of the first scalar (not needed by default)
421 * \tparam T2 type of the second scalar (not needed by default)
422 * \tparam M2 type of the second matrix (not needed by default)
423 * \todo use opb_prod()
425 template<class M1, class T1, class T2, class M2>
426 M1 & srk (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2)
428 return m1 = t1 * m1 + t2 * prod (m2, trans (m2));
431 /** \brief hermitian rank \a k update: \f$m_1=t.m_1+t_2.(m_2.m2^H)\f$
433 * \param m1 first matrix
434 * \param t1 first scalar
435 * \param t2 second scalar
436 * \param m2 second matrix
437 * \return matrix \c m1
439 * \tparam M1 type of the first matrix (not needed by default)
440 * \tparam T1 type of the first scalar (not needed by default)
441 * \tparam T2 type of the second scalar (not needed by default)
442 * \tparam M2 type of the second matrix (not needed by default)
443 * \todo use opb_prod()
445 template<class M1, class T1, class T2, class M2>
446 M1 & hrk (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2)
448 return m1 = t1 * m1 + t2 * prod (m2, herm (m2));
451 /** \brief generalized symmetric rank \a k update: \f$m_1=t_1.m_1+t_2.(m_2.m3^T)+t_2.(m_3.m2^T)\f$
453 * \param m1 first matrix
454 * \param t1 first scalar
455 * \param t2 second scalar
456 * \param m2 second matrix
457 * \param m3 third matrix
458 * \return matrix \c m1
460 * \tparam M1 type of the first matrix (not needed by default)
461 * \tparam T1 type of the first scalar (not needed by default)
462 * \tparam T2 type of the second scalar (not needed by default)
463 * \tparam M2 type of the second matrix (not needed by default)
464 * \tparam M3 type of the third matrix (not needed by default)
465 * \todo use opb_prod()
467 template<class M1, class T1, class T2, class M2, class M3>
468 M1 & sr2k (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2, const M3 &m3)
470 return m1 = t1 * m1 + t2 * (prod (m2, trans (m3)) + prod (m3, trans (m2)));
473 /** \brief generalized hermitian rank \a k update: * \f$m_1=t_1.m_1+t_2.(m_2.m_3^H)+(m_3.(t_2.m_2)^H)\f$
475 * \param m1 first matrix
476 * \param t1 first scalar
477 * \param t2 second scalar
478 * \param m2 second matrix
479 * \param m3 third matrix
480 * \return matrix \c m1
482 * \tparam M1 type of the first matrix (not needed by default)
483 * \tparam T1 type of the first scalar (not needed by default)
484 * \tparam T2 type of the second scalar (not needed by default)
485 * \tparam M2 type of the second matrix (not needed by default)
486 * \tparam M3 type of the third matrix (not needed by default)
487 * \todo use opb_prod()
489 template<class M1, class T1, class T2, class M2, class M3>
490 M1 & hr2k (M1 &m1, const T1 &t1, const T2 &t2, const M2 &m2, const M3 &m3)
494 + t2 * prod (m2, herm (m3))
495 + type_traits<T2>::conj (t2) * prod (m3, herm (m2));