Subversion Repositories gelsvn

Rev

Rev 12 | Rev 45 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12 Rev 43
Line 21... Line 21...
21
  This template should be used through inheritance just like the 
21
  This template should be used through inheritance just like the 
22
  vector template */
22
  vector template */
23
  template <class VVT, class HVT, class MT, int ROWS>
23
  template <class VVT, class HVT, class MT, int ROWS>
24
    class ArithMatFloat
24
    class ArithMatFloat
25
    { 
25
    { 
26
#define for_all_i(expr) for(int i=0;i<ROWS;i++) {expr;}
-
 
27
 
-
 
28
    public:
26
    public:
29
 
27
 
30
      /// Horizontal vector type
28
      /// Horizontal vector type
31
      typedef HVT HVectorType;
29
      typedef HVT HVectorType;
32
 
30
 
Line 45... Line 43...
45
 
43
 
46
      /// Construct 0 matrix
44
      /// Construct 0 matrix
47
      ArithMatFloat() 
45
      ArithMatFloat() 
48
	{
46
	{
49
#ifndef NDEBUG
47
#ifndef NDEBUG
50
	  for_all_i(data[i]=HVT(CGLA_INIT_VALUE));
48
	  std::fill_n(data, ROWS, HVT(CGLA_INIT_VALUE));
51
#endif
49
#endif
52
	}
50
	}
53
 
51
 
54
      /// Construct a matrix where all entries are the same.
52
      /// Construct a matrix where all entries are the same.
55
      explicit ArithMatFloat(ScalarType x)
53
      explicit ArithMatFloat(ScalarType x)
56
	{
54
	{
57
	  for_all_i(data[i] = HVT(x));
55
	  std::fill_n(data, ROWS, HVT(x));
58
	}
56
	}
59
 
57
 
60
      /// Construct a matrix where all rows are the same.
58
      /// Construct a matrix where all rows are the same.
61
      explicit ArithMatFloat(HVT _a)
59
      explicit ArithMatFloat(HVT _a)
62
	{
60
	{
63
	  for_all_i(data[i] = _a);
61
	  std::fill_n(data, ROWS, _a);
64
	}
62
	}
65
 
63
 
66
 
-
 
67
      /// Construct a matrix with two rows.
64
      /// Construct a matrix with two rows.
68
      ArithMatFloat(HVT _a, HVT _b)
65
      ArithMatFloat(HVT _a, HVT _b)
69
	{
66
	{
70
	  assert(ROWS==2);
67
	  assert(ROWS==2);
71
	  data[0] = _a;
68
	  data[0] = _a;
Line 137... Line 134...
137
      //----------------------------------------------------------------------
134
      //----------------------------------------------------------------------
138
 
135
 
139
      /// Equality operator. 
136
      /// Equality operator. 
140
      bool operator==(const MT& v) const 
137
      bool operator==(const MT& v) const 
141
	{
138
	{
142
	  for_all_i(if (data[i] != v[i]) return false)
139
	  return std::inner_product(data, &data[ROWS], &v[0], true,
143
	    return true;
140
				    std::logical_and<bool>(), std::equal_to<HVT>());
144
	}
141
	}
145
 
142
 
146
      /// Inequality operator.
143
      /// Inequality operator.
147
      bool operator!=(const MT& v) const 
144
      bool operator!=(const MT& v) const 
148
	{
145
	{
Line 153... Line 150...
153
 
150
 
154
      /// Multiply scalar onto matrix. All entries are multiplied by scalar.
151
      /// Multiply scalar onto matrix. All entries are multiplied by scalar.
155
      const MT operator * (ScalarType k) const
152
      const MT operator * (ScalarType k) const
156
	{
153
	{
157
	  MT v_new;
154
	  MT v_new;
158
	  for_all_i(v_new[i] = data[i] * k);
155
	  std::transform(data, &data[ROWS], &v_new[0], std::bind2nd(std::multiplies<HVT>(), k));
159
	  return v_new;
156
	  return v_new;
160
	}
157
	}
161
 
158
 
162
      /// Divide all entries in matrix by scalar.
159
      /// Divide all entries in matrix by scalar.
163
      const MT operator / (ScalarType k) const
160
      const MT operator / (ScalarType k) const
164
	{
161
	{
165
	  MT v_new;
162
	  MT v_new;
166
	  for_all_i(v_new[i] = data[i] / k);
163
	  std::transform(data, &data[ROWS], &v_new[0], std::bind2nd(std::divides<HVT>(), k));
167
	  return v_new;      
164
	  return v_new;      
168
	}
165
	}
169
 
166
 
170
      /// Assignment multiplication of matrix by scalar.
167
      /// Assignment multiplication of matrix by scalar.
171
      const MT& operator *=(ScalarType k) 
168
      const MT& operator *=(ScalarType k) 
172
	{
169
	{
173
	  for_all_i(data[i] *= k); 
170
	  std::transform(data, &data[ROWS], data, std::bind2nd(std::multiplies<HVT>(), k));
174
	  return static_cast<const MT&>(*this);
171
	  return static_cast<const MT&>(*this);
175
	}
172
	}
176
 
173
 
177
      /// Assignment division of matrix by scalar.
174
      /// Assignment division of matrix by scalar.
178
      const MT& operator /=(ScalarType k) 
175
      const MT& operator /=(ScalarType k) 
179
	{ 
176
	{ 
180
	  for_all_i(data[i] /= k); 
177
	  std::transform(data, &data[ROWS], data, std::bind2nd(std::divides<HVT>(), k));
181
	  return static_cast<const MT&>(*this);
178
	  return static_cast<const MT&>(*this);
182
	}
179
	}
183
 
180
 
184
      //----------------------------------------------------------------------
181
      //----------------------------------------------------------------------
185
 
182
 
186
      /// Add two matrices. 
183
      /// Add two matrices. 
187
      const MT operator + (const MT& m1) const
184
      const MT operator + (const MT& m1) const
188
	{
185
	{
189
	  MT v_new;
186
	  MT v_new;
190
	  for_all_i(v_new[i] = data[i] + m1[i]);
187
	  std::transform(data, &data[ROWS], &m1[0], &v_new[0], std::plus<HVT>());
191
	  return v_new;
188
	  return v_new;
192
	}
189
	}
193
 
190
 
194
      /// Subtract two matrices.
191
      /// Subtract two matrices.
195
      const MT operator - (const MT& m1) const
192
      const MT operator - (const MT& m1) const
196
	{
193
	{
197
	  MT v_new;
194
	  MT v_new;
198
	  for_all_i(v_new[i] = data[i] - m1[i]);
195
	  std::transform(data, &data[ROWS], &m1[0], &v_new[0], std::minus<HVT>());
199
	  return v_new;
196
	  return v_new;
200
	}
197
	}
201
 
198
 
202
      /// Assigment addition of matrices.
199
      /// Assigment addition of matrices.
203
      const MT& operator +=(const MT& v) 
200
      const MT& operator +=(const MT& v) 
204
	{
201
	{
205
	  for_all_i(data[i] += v[i]); 
202
	  std::transform(data, &data[ROWS], &v[0], data, std::plus<HVT>());
206
	  return static_cast<const MT&>(*this);
203
	  return static_cast<const MT&>(*this);
207
	}
204
	}
208
 
205
 
209
      /// Assigment subtraction of matrices.
206
      /// Assigment subtraction of matrices.
210
      const MT& operator -=(const MT& v) 
207
      const MT& operator -=(const MT& v) 
211
	{
208
	{
212
	  for_all_i(data[i] -= v[i]); 
209
	  std::transform(data, &data[ROWS], &v[0], data, std::minus<HVT>());
213
	  return static_cast<const MT&>(*this);
210
	  return static_cast<const MT&>(*this);
214
	}
211
	}
215
 
212
 
216
      //----------------------------------------------------------------------
213
      //----------------------------------------------------------------------
217
 
214
 
218
      /// Negate matrix.
215
      /// Negate matrix.
219
      const MT operator - () const
216
      const MT operator - () const
220
	{
217
	{
221
	  MT v_new;
218
	  MT v_new;
222
	  for_all_i(v_new[i] = - data[i]);
219
	  std::transform(data, &data[ROWS], &v_new[0], std::negate<HVT>());
223
	  return v_new;
220
	  return v_new;
224
	}
221
	}
225
 
-
 
226
#undef for_all_i  
-
 
227
 
-
 
228
    };
222
    };
229
 
223
 
230
  /// Multiply scalar onto matrix
224
  /// Multiply scalar onto matrix
231
  template <class VVT, class HVT, class MT, int ROWS>
225
  template <class VVT, class HVT, class MT, int ROWS>
232
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
226
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
Line 294... Line 288...
294
      I don't simply return the transpose. */
288
      I don't simply return the transpose. */
295
  template <class VVT, class HVT, class M1T, class M2T, int ROWS, int COLS>
289
  template <class VVT, class HVT, class M1T, class M2T, int ROWS, int COLS>
296
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
290
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
297
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
291
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
298
    {
292
    {
299
      for(int i=0;i<M2T::get_v_dim();i++)
293
      for(int i=0;i<M2T::get_v_dim();++i)
300
	for(int j=0;j<M2T::get_h_dim();j++)
294
	for(int j=0;j<M2T::get_h_dim();++j)
301
	  m_new[i][j] = m[j][i];
295
	  m_new[i][j] = m[j][i];
302
    }
296
    }
303
 
297
 
304
#else
298
#else
305
 
299
 
Line 313... Line 307...
313
    {
307
    {
314
      int cols = M::get_h_dim();
308
      int cols = M::get_h_dim();
315
      int rows1 = M1::get_v_dim();
309
      int rows1 = M1::get_v_dim();
316
      int rows2 = M2::get_v_dim();
310
      int rows2 = M2::get_v_dim();
317
 
311
 
318
      for(int i=0;i<rows1;i++)
312
      for(int i=0;i<rows1;++i)
319
	for(int j=0;j<cols;j++)
313
	for(int j=0;j<cols;++j)
320
	  {
314
	  {
321
	    m[i][j] = 0;
315
	    m[i][j] = 0;
322
	    for(int k=0;k<rows2;k++)
316
	    for(int k=0;k<rows2;++k)
323
	      m[i][j] += m1[i][k] * m2[k][j];
317
	      m[i][j] += m1[i][k] * m2[k][j];
324
	  }
318
	  }
325
    }
319
    }
326
 
320
 
327
 
321
 
328
  /** Transpose. See the discussion on mul if you are curious as to why
322
  /** Transpose. See the discussion on mul if you are curious as to why
329
      I don't simply return the transpose. */
323
      I don't simply return the transpose. */
330
  template <class M1, class M2>
324
  template <class M1, class M2>
331
    inline void transpose(const M1& m1, M2& m2)
325
    inline void transpose(const M1& m1, M2& m2)
332
    {
326
    {
333
      for(int i=0;i<M2::get_v_dim();i++)
327
      for(int i=0;i<M2::get_v_dim();++i)
334
	for(int j=0;j<M2::get_h_dim();j++)
328
	for(int j=0;j<M2::get_h_dim();++j)
335
	  m2[i][j] = m1[j][i];
329
	  m2[i][j] = m1[j][i];
336
    }
330
    }
337
 
331
 
338
#endif
332
#endif
339
 
333