Library of Assembled Shared Sources
matrix_expressions.h
Go to the documentation of this file.
1/** @file
2 * @author Bram de Greve (bram@cocamware.com)
3 * @author Tom De Muer (tom@cocamware.com)
4 *
5 * *** BEGIN LICENSE INFORMATION ***
6 *
7 * The contents of this file are subject to the Common Public Attribution License
8 * Version 1.0 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://lass.sourceforge.net/cpal-license. The License is based on the
11 * Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover
12 * use of software over a computer network and provide for limited attribution for
13 * the Original Developer. In addition, Exhibit A has been modified to be consistent
14 * with Exhibit B.
15 *
16 * Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
17 * WARRANTY OF ANY KIND, either express or implied. See the License for the specific
18 * language governing rights and limitations under the License.
19 *
20 * The Original Code is LASS - Library of Assembled Shared Sources.
21 *
22 * The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
23 * The Original Developer is the Initial Developer.
24 *
25 * All portions of the code written by the Initial Developer are:
26 * Copyright (C) 2004-2011 the Initial Developer.
27 * All Rights Reserved.
28 *
29 * Contributor(s):
30 *
31 * Alternatively, the contents of this file may be used under the terms of the
32 * GNU General Public License Version 2 or later (the GPL), in which case the
33 * provisions of GPL are applicable instead of those above. If you wish to allow use
34 * of your version of this file only under the terms of the GPL and not to allow
35 * others to use your version of this file under the CPAL, indicate your decision by
36 * deleting the provisions above and replace them with the notice and other
37 * provisions required by the GPL License. If you do not delete the provisions above,
38 * a recipient may use your version of this file under either the CPAL or the GPL.
39 *
40 * *** END LICENSE INFORMATION ***
41 */
42
43
44
45#ifndef LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_MATRIX_EXPRESSIONS_H
46#define LASS_GUARDIAN_OF_INCLUSION_NUM_IMPL_MATRIX_EXPRESSIONS_H
47
48#include "../num_common.h"
49
50namespace lass
51{
52namespace num
53{
54namespace impl
55{
56
57/** @internal
58*/
59template <typename T>
60class MStorage
61{
62public:
63 enum { lvalue = true };
64 typedef typename util::CallTraits<T>::TValue TValue;
65 typedef typename util::CallTraits<T>::TParam TParam;
66 typedef typename util::CallTraits<T>::TReference TReference;
67 typedef typename util::CallTraits<T>::TConstReference TConstReference;
68 typedef size_t TSize;
69
70 MStorage(): storage_(), rows_(0), cols_(0) {}
71 MStorage(TSize iRows, TSize iCols): storage_(iRows * iCols, T()), rows_(iRows), cols_(iCols) {}
72 TConstReference operator()(TSize iI, TSize iJ) const { return storage_[iI * cols_ + iJ]; }
73 TReference operator()(TSize iI, TSize iJ) { return storage_[iI * cols_ + iJ]; }
74 TSize rows() const { return rows_; }
75 TSize columns() const { return cols_; }
76
77 // special MStorage members:
78 //
79 void resize(TSize iRows, TSize iCols)
80 {
81 storage_.resize(iRows * iCols, T());
82 rows_ = iRows;
83 cols_ = iCols;
84 }
85 void swap(MStorage<T>& iOther)
86 {
87 storage_.swap(iOther.storage_);
88 std::swap(rows_, iOther.rows_);
89 std::swap(cols_, iOther.cols_);
90 }
91
92 typename std::vector<T>::iterator rowMajor() { return storage_.begin(); }
93 typename std::vector<T>::const_iterator rowMajor() const { return storage_.begin(); }
94
95private:
96 std::vector<T> storage_;
97 TSize rows_;
98 TSize cols_;
99};
100
101/** @internal
102*/
103template <typename T>
104class MScalar
105{
106public:
107 enum { lvalue = false };
108 typedef typename util::CallTraits<T>::TValue TValue;
109 typedef typename util::CallTraits<T>::TParam TParam;
110 typedef size_t TSize;
111
112 MScalar(TParam iValue, TSize iRows, TSize iCols): value_(iValue), rows_(iRows), cols_(iCols) {}
113 TParam operator()(TSize /*iI*/, TSize /*iJ*/) const
114 {
115 return value_;
116 }
117 TSize rows() const { return rows_; }
118 TSize columns() const { return cols_; }
119private:
120 TValue value_;
121 TSize rows_;
122 TSize cols_;
123};
124
125
126
127template <typename ExpressionType>
128struct MatrixExpressionTraits
129{
130 typedef const ExpressionType& TStorage;
131};
132
133template <typename T>
134struct MatrixExpressionTraits<MScalar<T> >
135{
136 typedef MScalar<T> TStorage;
137};
138
139
140
141#define LASS_NUM_MATRIX_BINARY_EXPRESSION(i_name, c_operator)\
142template <typename T, typename Operand1, typename Operand2>\
143class LASS_CONCATENATE(M, i_name)\
144{\
145public:\
146 enum { lvalue = false };\
147 typedef typename util::CallTraits<T>::TValue TValue;\
148 typedef size_t TSize;\
149 LASS_CONCATENATE(M, i_name)(const Operand1& iA, const Operand2& iB):\
150 operand1_(iA), operand2_(iB)\
151 {\
152 LASS_ASSERT(operand1_.rows() == operand2_.rows() &&\
153 operand1_.columns() == operand2_.columns());\
154 }\
155 TValue operator()(TSize iI, TSize iJ) const\
156 {\
157 return operand1_(iI, iJ) c_operator operand2_(iI, iJ);\
158 }\
159 TSize rows() const { return operand1_.rows(); }\
160 TSize columns() const { return operand1_.columns(); }\
161private:\
162 typename MatrixExpressionTraits<Operand1>::TStorage operand1_;\
163 typename MatrixExpressionTraits<Operand2>::TStorage operand2_;\
164}
165
166LASS_NUM_MATRIX_BINARY_EXPRESSION(Add, +);
167LASS_NUM_MATRIX_BINARY_EXPRESSION(Sub, -);
168LASS_NUM_MATRIX_BINARY_EXPRESSION(Mul, *);
169
170
171
172#define LASS_NUM_MATRIX_UNARY_EXPRESSION(i_name, c_operator)\
173template <typename T, typename Operand1>\
174class LASS_CONCATENATE(M, i_name)\
175{\
176public:\
177 enum { lvalue = false };\
178 typedef typename util::CallTraits<T>::TValue TValue;\
179 typedef size_t TSize;\
180 LASS_CONCATENATE(M, i_name)(const Operand1& iA):\
181 operand1_(iA)\
182 {\
183 }\
184 TValue operator()(TSize iRow, TSize iCol) const\
185 {\
186 return c_operator operand1_(iRow, iCol);\
187 }\
188 TSize rows() const { return operand1_.rows(); }\
189 TSize columns() const { return operand1_.columns(); }\
190private:\
191 typename MatrixExpressionTraits<Operand1>::TStorage operand1_;\
192}
193
194LASS_NUM_MATRIX_UNARY_EXPRESSION(Neg, -);
195LASS_NUM_MATRIX_UNARY_EXPRESSION(Rec, ::lass::num::NumTraits<T>::one /);
196
197
198
199/** @internal
200* matrix product
201*/
202template <typename T, typename Operand1, typename Operand2>
203class MProd
204{
205public:
206 enum { lvalue = false };
207 typedef typename util::CallTraits<T>::TValue TValue;
208 typedef size_t TSize;
209 MProd(const Operand1& iA, const Operand2& iB):
210 operand1_(iA), operand2_(iB), loopSize_(iA.columns())
211 {
212 LASS_ASSERT(operand1_.columns() == operand2_.rows());
213 }
214 TValue operator()(TSize iI, TSize iJ) const
215 {
216 TValue result = TValue();
217 for (TSize k = 0; k < loopSize_; ++k)
218 {
219 result += operand1_(iI, k) * operand2_(k, iJ);
220 }
221 return result;
222 }
223 TSize rows() const { return operand1_.rows(); }
224 TSize columns() const { return operand2_.columns(); }
225private:
226 typename MatrixExpressionTraits<Operand1>::TStorage operand1_;
227 typename MatrixExpressionTraits<Operand2>::TStorage operand2_;
228 TSize loopSize_;
229};
230
231
232/** @internal
233* transpose matrix
234*/
235template <typename T, typename Operand1>
236class MTrans
237{
238public:
239 enum { lvalue = false };
240 typedef typename util::CallTraits<T>::TParam TParam;
241 typedef size_t TSize;
242 MTrans(const Operand1& iA): operand1_(iA) {}
243 TParam operator()(TSize iI, TSize iJ) const { return operand1_(iJ, iI);
244}
245 TSize rows() const { return operand1_.columns(); }
246 TSize columns() const { return operand1_.rows(); }
247private:
248 typename MatrixExpressionTraits<Operand1>::TStorage operand1_;
249};
250
251
252
253/** @internal
254* apply function
255*/
256template <typename T, typename Operand1>
257class MFun
258{
259public:
260 enum { lvalue = false };
261 typedef T (*TOperator)(T);
262 typedef typename util::CallTraits<T>::TValue TValue;
263 typedef size_t TSize;
264 MFun(const Operand1& iA, TOperator iOperator): operand1_(iA),
265operator_(iOperator) {}
266 TValue operator()(TSize iI, TSize iJ) const { return
267operator_(operand1_(iI, iJ)); }
268 TSize rows() const { return operand1_.rows(); }\
269 TSize columns() const { return operand1_.columns(); }\
270private:
271 typename MatrixExpressionTraits<Operand1>::TStorage operand1_;
272 TOperator operator_;
273};
274
275
276
277}
278
279}
280
281}
282
283#endif
284
285// EOF
numeric types and traits.
Definition basic_ops.h:70
Library for Assembled Shared Sources.
Definition config.h:53