Subversion Repositories gelsvn

Rev

Rev 382 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 382 Rev 393
1
#ifndef __CGLA_ARITHMATFLOAT_H__
1
#ifndef __CGLA_ARITHMATFLOAT_H__
2
#define __CGLA_ARITHMATFLOAT_H__
2
#define __CGLA_ARITHMATFLOAT_H__
3
 
3
 
4
#include <vector>
4
#include <vector>
5
#include <iostream>
5
#include <iostream>
6
#include <numeric>
6
#include <numeric>
7
 
7
 
8
#include "CGLA.h"
8
#include "CGLA.h"
9
 
9
 
10
 
10
 
11
namespace CGLA 
11
namespace CGLA 
12
{
12
{
13
 
13
 
14
  /** \brief Basic class template for matrices.
14
  /** \brief Basic class template for matrices.
15
		
15
		
16
  In this template a matrix is defined as an array of vectors. This may
16
  In this template a matrix is defined as an array of vectors. This may
17
  not in all cases be the most efficient but it has the advantage that 
17
  not in all cases be the most efficient but it has the advantage that 
18
  it is possible to use the double subscripting notation:
18
  it is possible to use the double subscripting notation:
19
		
19
		
20
  T x = m[i][j]
20
  T x = m[i][j]
21
 
21
 
22
  This template should be used through inheritance just like the 
22
  This template should be used through inheritance just like the 
23
  vector template */
23
  vector template */
24
  template <class VVT, class HVT, class MT, unsigned int ROWS>
24
  template <class VVT, class HVT, class MT, unsigned int ROWS>
25
    class ArithMatFloat
25
    class ArithMatFloat
26
    { 
26
    { 
27
    public:
27
    public:
28
 
28
 
29
      /// Horizontal vector type
29
      /// Horizontal vector type
30
      typedef HVT HVectorType;
30
      typedef HVT HVectorType;
31
 
31
 
32
      /// Vertical vector type
32
      /// Vertical vector type
33
      typedef VVT VVectorType;
33
      typedef VVT VVectorType;
34
 
34
 
35
      /// The type of a matrix element
35
      /// The type of a matrix element
36
      typedef typename HVT::ScalarType ScalarType;
36
      typedef typename HVT::ScalarType ScalarType;
37
		
37
		
38
    protected:
38
    protected:
39
 
39
 
40
      /// The actual contents of the matrix.
40
      /// The actual contents of the matrix.
41
      HVT data[ROWS];
41
      HVT data[ROWS];
42
 
42
 
43
    protected:
43
    protected:
44
 
44
 
45
      /// Construct 0 matrix
45
      /// Construct 0 matrix
46
      ArithMatFloat() 
46
      ArithMatFloat() 
47
	{
47
	{
48
#ifndef NDEBUG
48
#ifndef NDEBUG
49
	  std::fill_n(data, ROWS, HVT(CGLA_INIT_VALUE));
49
	  std::fill_n(data, ROWS, HVT(CGLA_INIT_VALUE));
50
#endif
50
#endif
51
	}
51
	}
52
 
52
 
53
      /// Construct a matrix where all entries are the same.
53
      /// Construct a matrix where all entries are the same.
54
      explicit ArithMatFloat(ScalarType x)
54
      explicit ArithMatFloat(ScalarType x)
55
	{
55
	{
56
	  std::fill_n(data, ROWS, HVT(x));
56
	  std::fill_n(data, ROWS, HVT(x));
57
	}
57
	}
58
 
58
 
59
      /// Construct a matrix where all rows are the same.
59
      /// Construct a matrix where all rows are the same.
60
      explicit ArithMatFloat(HVT _a)
60
      explicit ArithMatFloat(HVT _a)
61
	{
61
	{
62
	  std::fill_n(data, ROWS, _a);
62
	  std::fill_n(data, ROWS, _a);
63
	}
63
	}
64
 
64
 
65
      /// Construct a matrix with two rows.
65
      /// Construct a matrix with two rows.
66
      ArithMatFloat(HVT _a, HVT _b)
66
      ArithMatFloat(HVT _a, HVT _b)
67
	{
67
	{
68
	  assert(ROWS==2);
68
	  assert(ROWS==2);
69
	  data[0] = _a;
69
	  data[0] = _a;
70
	  data[1] = _b;
70
	  data[1] = _b;
71
	}
71
	}
72
 
72
 
73
      /// Construct a matrix with three rows.
73
      /// Construct a matrix with three rows.
74
      ArithMatFloat(HVT _a, HVT _b, HVT _c)
74
      ArithMatFloat(HVT _a, HVT _b, HVT _c)
75
	{
75
	{
76
	  assert(ROWS==3);
76
	  assert(ROWS==3);
77
	  data[0] = _a;
77
	  data[0] = _a;
78
	  data[1] = _b;
78
	  data[1] = _b;
79
	  data[2] = _c;
79
	  data[2] = _c;
80
	}
80
	}
81
 
81
 
82
      /// Construct a matrix with four rows.
82
      /// Construct a matrix with four rows.
83
      ArithMatFloat(HVT _a, HVT _b, HVT _c, HVT _d)
83
      ArithMatFloat(HVT _a, HVT _b, HVT _c, HVT _d)
84
	{
84
	{
85
	  assert(ROWS==4);
85
	  assert(ROWS==4);
86
	  data[0] = _a;
86
	  data[0] = _a;
87
	  data[1] = _b;
87
	  data[1] = _b;
88
	  data[2] = _c;
88
	  data[2] = _c;
89
	  data[3] = _d;
89
	  data[3] = _d;
90
	}
90
	}
91
		
91
		
92
    public:
92
    public:
93
 
93
 
94
      /// Get vertical dimension of matrix 
94
      /// Get vertical dimension of matrix 
95
      static unsigned int get_v_dim() {return VVT::get_dim();}
95
      static unsigned int get_v_dim() {return VVT::get_dim();}
96
 
96
 
97
      /// Get horizontal dimension of matrix
97
      /// Get horizontal dimension of matrix
98
      static unsigned int get_h_dim() {return HVT::get_dim();}
98
      static unsigned int get_h_dim() {return HVT::get_dim();}
99
 
99
 
100
 
100
 
101
      /** Get const pointer to data array.
101
      /** Get const pointer to data array.
102
	  This function may be useful when interfacing with some other API 
102
	  This function may be useful when interfacing with some other API 
103
	  such as OpenGL (TM). */
103
	  such as OpenGL (TM). */
104
      const ScalarType* get() const 
104
      const ScalarType* get() const 
105
	{
105
	{
106
	  return data[0].get();
106
	  return data[0].get();
107
	}
107
	}
108
 
108
 
109
      /** Get pointer to data array.
109
      /** Get pointer to data array.
110
	  This function may be useful when interfacing with some other API 
110
	  This function may be useful when interfacing with some other API 
111
	  such as OpenGL (TM). */
111
	  such as OpenGL (TM). */
112
      ScalarType* get()
112
      ScalarType* get()
113
	{
113
	{
114
	  return data[0].get();
114
	  return data[0].get();
115
	}
115
	}
116
 
116
 
117
      //----------------------------------------------------------------------
117
      //----------------------------------------------------------------------
118
      // index operators
118
      // index operators
119
      //----------------------------------------------------------------------
119
      //----------------------------------------------------------------------
120
 
120
 
121
      /// Const index operator. Returns i'th row of matrix.
121
      /// Const index operator. Returns i'th row of matrix.
122
      const HVT& operator [] ( unsigned int i ) const
122
      const HVT& operator [] ( unsigned int i ) const
123
	{
123
	{
124
	  assert(i<ROWS);
124
	  assert(i<ROWS);
125
	  return data[i];
125
	  return data[i];
126
	}
126
	}
127
 
127
 
128
      /// Non-const index operator. Returns i'th row of matrix.
128
      /// Non-const index operator. Returns i'th row of matrix.
129
      HVT& operator [] ( unsigned int i ) 
129
      HVT& operator [] ( unsigned int i ) 
130
	{
130
	{
131
	  assert(i<ROWS);
131
	  assert(i<ROWS);
132
	  return data[i];
132
	  return data[i];
133
	}
133
	}
134
 
134
 
135
      //----------------------------------------------------------------------
135
      //----------------------------------------------------------------------
136
 
136
 
137
      /// Equality operator. 
137
      /// Equality operator. 
138
      bool operator==(const MT& v) const 
138
      bool operator==(const MT& v) const 
139
	{
139
	{
140
	  return std::inner_product(data, &data[ROWS], &v[0], true,
140
	  return std::inner_product(data, &data[ROWS], &v[0], true,
141
				    std::logical_and<bool>(), std::equal_to<HVT>());
141
				    std::logical_and<bool>(), std::equal_to<HVT>());
142
	}
142
	}
143
 
143
 
144
      /// Inequality operator.
144
      /// Inequality operator.
145
      bool operator!=(const MT& v) const 
145
      bool operator!=(const MT& v) const 
146
	{
146
	{
147
	  return !(*this==v);
147
	  return !(*this==v);
148
	}
148
	}
149
 
149
 
150
      //----------------------------------------------------------------------
150
      //----------------------------------------------------------------------
151
 
151
 
152
      /// Multiply scalar onto matrix. All entries are multiplied by scalar.
152
      /// Multiply scalar onto matrix. All entries are multiplied by scalar.
153
      const MT operator * (ScalarType k) const
153
      const MT operator * (ScalarType k) const
154
	{
154
	{
155
	  MT v_new;
155
	  MT v_new;
156
	  std::transform(data, &data[ROWS], &v_new[0], std::bind2nd(std::multiplies<HVT>(), k));
156
	  std::transform(data, &data[ROWS], &v_new[0], std::bind2nd(std::multiplies<HVT>(), k));
157
	  return v_new;
157
	  return v_new;
158
	}
158
	}
159
 
159
 
160
      /// Divide all entries in matrix by scalar.
160
      /// Divide all entries in matrix by scalar.
161
      const MT operator / (ScalarType k) const
161
      const MT operator / (ScalarType k) const
162
	{
162
	{
163
	  MT v_new;
163
	  MT v_new;
164
	  std::transform(data, &data[ROWS], &v_new[0], std::bind2nd(std::divides<HVT>(), k));
164
	  std::transform(data, &data[ROWS], &v_new[0], std::bind2nd(std::divides<HVT>(), k));
165
	  return v_new;      
165
	  return v_new;      
166
	}
166
	}
167
 
167
 
168
      /// Assignment multiplication of matrix by scalar.
168
      /// Assignment multiplication of matrix by scalar.
169
      const MT& operator *=(ScalarType k) 
169
      const MT& operator *=(ScalarType k) 
170
	{
170
	{
171
	  std::transform(data, &data[ROWS], data, std::bind2nd(std::multiplies<HVT>(), k));
171
	  std::transform(data, &data[ROWS], data, std::bind2nd(std::multiplies<HVT>(), k));
172
	  return static_cast<const MT&>(*this);
172
	  return static_cast<const MT&>(*this);
173
	}
173
	}
174
 
174
 
175
      /// Assignment division of matrix by scalar.
175
      /// Assignment division of matrix by scalar.
176
      const MT& operator /=(ScalarType k) 
176
      const MT& operator /=(ScalarType k) 
177
	{ 
177
	{ 
178
	  std::transform(data, &data[ROWS], data, std::bind2nd(std::divides<HVT>(), k));
178
	  std::transform(data, &data[ROWS], data, std::bind2nd(std::divides<HVT>(), k));
179
	  return static_cast<const MT&>(*this);
179
	  return static_cast<const MT&>(*this);
180
	}
180
	}
181
 
181
 
182
      //----------------------------------------------------------------------
182
      //----------------------------------------------------------------------
183
 
183
 
184
      /// Add two matrices. 
184
      /// Add two matrices. 
185
      const MT operator + (const MT& m1) const
185
      const MT operator + (const MT& m1) const
186
	{
186
	{
187
	  MT v_new;
187
	  MT v_new;
188
	  std::transform(data, &data[ROWS], &m1[0], &v_new[0], std::plus<HVT>());
188
	  std::transform(data, &data[ROWS], &m1[0], &v_new[0], std::plus<HVT>());
189
	  return v_new;
189
	  return v_new;
190
	}
190
	}
191
 
191
 
192
      /// Subtract two matrices.
192
      /// Subtract two matrices.
193
      const MT operator - (const MT& m1) const
193
      const MT operator - (const MT& m1) const
194
	{
194
	{
195
	  MT v_new;
195
	  MT v_new;
196
	  std::transform(data, &data[ROWS], &m1[0], &v_new[0], std::minus<HVT>());
196
	  std::transform(data, &data[ROWS], &m1[0], &v_new[0], std::minus<HVT>());
197
	  return v_new;
197
	  return v_new;
198
	}
198
	}
199
 
199
 
200
      /// Assigment addition of matrices.
200
      /// Assigment addition of matrices.
201
      const MT& operator +=(const MT& v) 
201
      const MT& operator +=(const MT& v) 
202
	{
202
	{
203
	  std::transform(data, &data[ROWS], &v[0], data, std::plus<HVT>());
203
	  std::transform(data, &data[ROWS], &v[0], data, std::plus<HVT>());
204
	  return static_cast<const MT&>(*this);
204
	  return static_cast<const MT&>(*this);
205
	}
205
	}
206
 
206
 
207
      /// Assigment subtraction of matrices.
207
      /// Assigment subtraction of matrices.
208
      const MT& operator -=(const MT& v) 
208
      const MT& operator -=(const MT& v) 
209
	{
209
	{
210
	  std::transform(data, &data[ROWS], &v[0], data, std::minus<HVT>());
210
	  std::transform(data, &data[ROWS], &v[0], data, std::minus<HVT>());
211
	  return static_cast<const MT&>(*this);
211
	  return static_cast<const MT&>(*this);
212
	}
212
	}
213
 
213
 
214
      //----------------------------------------------------------------------
214
      //----------------------------------------------------------------------
215
 
215
 
216
      /// Negate matrix.
216
      /// Negate matrix.
217
      const MT operator - () const
217
      const MT operator - () const
218
	{
218
	{
219
	  MT v_new;
219
	  MT v_new;
220
	  std::transform(data, &data[ROWS], &v_new[0], std::negate<HVT>());
220
	  std::transform(data, &data[ROWS], &v_new[0], std::negate<HVT>());
221
	  return v_new;
221
	  return v_new;
222
	}
222
	}
223
    };
223
    };
224
 
224
 
225
  /// Multiply scalar onto matrix
225
  /// Multiply scalar onto matrix
226
  template <class VVT, class HVT, class MT, unsigned int ROWS>
226
  template <class VVT, class HVT, class MT, unsigned int ROWS>
227
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
227
    inline const MT operator * (double k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
228
    {
228
    {
229
      return v * k;
229
      return v * k;
230
    }
230
    }
231
 
231
 
232
  /// Multiply scalar onto matrix
232
  /// Multiply scalar onto matrix
233
  template <class VVT, class HVT, class MT, unsigned int ROWS>
233
  template <class VVT, class HVT, class MT, unsigned int ROWS>
234
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
234
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
235
    {
235
    {
236
      return v * k;
236
      return v * k;
237
    }
237
    }
238
 
238
 
239
  /// Multiply scalar onto matrix
239
  /// Multiply scalar onto matrix
240
  template <class VVT, class HVT, class MT, unsigned int ROWS>
240
  template <class VVT, class HVT, class MT, unsigned int ROWS>
241
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
241
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
242
    {
242
    {
243
      return v * k;
243
      return v * k;
244
    }
244
    }
245
 
245
 
246
  /// Multiply vector onto matrix 
246
  /// Multiply vector onto matrix 
247
  template <class VVT, class HVT, class MT, unsigned int ROWS>
247
  template <class VVT, class HVT, class MT, unsigned int ROWS>
248
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
248
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
249
    {
249
    {
250
      VVT v2;
250
      VVT v2;
251
      for(unsigned int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
251
      for(unsigned int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
252
      return v2;
252
      return v2;
253
    }
253
    }
254
 
254
 
255
 
255
 
256
#ifndef WIN32
256
#ifndef WIN32
257
  /** Multiply two arbitrary matrices. 
257
  /** Multiply two arbitrary matrices. 
258
      In principle, this function could return a matrix, but in general
258
      In principle, this function could return a matrix, but in general
259
      the new matrix will be of a type that is different from either of
259
      the new matrix will be of a type that is different from either of
260
      the two matrices that are multiplied together. We do not want to 
260
      the two matrices that are multiplied together. We do not want to 
261
      return an ArithMatFloat - so it seems best to let the return value be
261
      return an ArithMatFloat - so it seems best to let the return value be
262
      a reference arg.
262
      a reference arg.
263
		
263
		
264
      This template can only be instantiated if the dimensions of the
264
      This template can only be instantiated if the dimensions of the
265
      matrices match -- i.e. if the multiplication can actually be
265
      matrices match -- i.e. if the multiplication can actually be
266
      carried out. This is more type safe than the win32 version below.
266
      carried out. This is more type safe than the win32 version below.
267
  */
267
  */
268
 
268
 
269
  template <class VVT, class HVT, 
269
  template <class VVT, class HVT, 
270
    class HV1T, class VV2T,
270
    class HV1T, class VV2T,
271
    class MT1, class MT2, class MT,
271
    class MT1, class MT2, class MT,
272
    unsigned int ROWS1, unsigned int ROWS2>
272
    unsigned int ROWS1, unsigned int ROWS2>
273
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
273
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
274
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
274
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
275
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
275
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
276
    {
276
    {
277
      unsigned int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
277
      unsigned int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
278
      for(unsigned int i=0;i<ROWS1;i++)
278
      for(unsigned int i=0;i<ROWS1;i++)
279
	for(unsigned int j=0;j<cols;j++)
279
	for(unsigned int j=0;j<cols;j++)
280
	  {
280
	  {
281
	    m[i][j] = 0;
281
	    m[i][j] = 0;
282
	    for(unsigned int k=0;k<ROWS2;k++)
282
	    for(unsigned int k=0;k<ROWS2;k++)
283
	      m[i][j] += m1[i][k] * m2[k][j]; 
283
	      m[i][j] += m1[i][k] * m2[k][j]; 
284
	  }
284
	  }
285
    }
285
    }
286
 
286
 
287
 
287
 
288
  /** Transpose. See the discussion on mul if you are curious as to why
288
  /** Transpose. See the discussion on mul if you are curious as to why
289
      I don't simply return the transpose. */
289
      I don't simply return the transpose. */
290
  template <class VVT, class HVT, class M1T, class M2T, unsigned int ROWS, unsigned int COLS>
290
  template <class VVT, class HVT, class M1T, class M2T, unsigned int ROWS, unsigned int COLS>
291
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
291
    inline void transpose(const ArithMatFloat<VVT,HVT,M1T,ROWS>& m,
292
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
292
			  ArithMatFloat<HVT,VVT,M2T,COLS>& m_new)
293
    {
293
    {
294
      for(unsigned int i=0;i<M2T::get_v_dim();++i)
294
      for(unsigned int i=0;i<M2T::get_v_dim();++i)
295
	for(unsigned int j=0;j<M2T::get_h_dim();++j)
295
	for(unsigned int j=0;j<M2T::get_h_dim();++j)
296
	  m_new[i][j] = m[j][i];
296
	  m_new[i][j] = m[j][i];
297
    }
297
    }
298
 
298
 
299
#else
299
#else
300
 
300
 
301
  //----------------- win32 -------------------------------
301
  //----------------- win32 -------------------------------
302
  // Visual studio is not good at deducing the args. to these template functions.
302
  // Visual studio is not good at deducing the args. to these template functions.
303
  // This means that you can call the two functions below with 
303
  // This means that you can call the two functions below with 
304
  // matrices of wrong dimension.
304
  // matrices of wrong dimension.
305
 
305
 
306
  template <class M1, class M2, class M>
306
  template <class M1, class M2, class M>
307
    inline void mul(const M1& m1, const M2& m2, M& m)
307
    inline void mul(const M1& m1, const M2& m2, M& m)
308
    {
308
    {
309
      unsigned int cols = M::get_h_dim();
309
      unsigned int cols = M::get_h_dim();
310
      unsigned int rows1 = M1::get_v_dim();
310
      unsigned int rows1 = M1::get_v_dim();
311
      unsigned int rows2 = M2::get_v_dim();
311
      unsigned int rows2 = M2::get_v_dim();
312
 
312
 
313
      for(unsigned int i=0;i<rows1;++i)
313
      for(unsigned int i=0;i<rows1;++i)
314
	for(unsigned int j=0;j<cols;++j)
314
	for(unsigned int j=0;j<cols;++j)
315
	  {
315
	  {
316
	    m[i][j] = 0;
316
	    m[i][j] = 0;
317
	    for(unsigned int k=0;k<rows2;++k)
317
	    for(unsigned int k=0;k<rows2;++k)
318
	      m[i][j] += m1[i][k] * m2[k][j];
318
	      m[i][j] += m1[i][k] * m2[k][j];
319
	  }
319
	  }
320
    }
320
    }
321
 
321
 
322
 
322
 
323
  /** Transpose. See the discussion on mul if you are curious as to why
323
  /** Transpose. See the discussion on mul if you are curious as to why
324
      I don't simply return the transpose. */
324
      I don't simply return the transpose. */
325
  template <class M1, class M2>
325
  template <class M1, class M2>
326
    inline void transpose(const M1& m1, M2& m2)
326
    inline void transpose(const M1& m1, M2& m2)
327
    {
327
    {
328
      for(unsigned int i=0;i<M2::get_v_dim();++i)
328
      for(unsigned int i=0;i<M2::get_v_dim();++i)
329
	for(unsigned int j=0;j<M2::get_h_dim();++j)
329
	for(unsigned int j=0;j<M2::get_h_dim();++j)
330
	  m2[i][j] = m1[j][i];
330
	  m2[i][j] = m1[j][i];
331
    }
331
    }
332
 
332
 
333
#endif
333
#endif
334
 
334
 
335
  /** Compute the outer product of a and b: a * transpose(b). This is 
335
  /** Compute the outer product of a and b: a * transpose(b). This is 
336
      a matrix with a::rows and b::columns. */
336
      a matrix with a::rows and b::columns. */
337
  template <class VVT, class HVT, class MT, unsigned int ROWS>
337
  template <class VVT, class HVT, class MT, unsigned int ROWS>
338
    void outer_product(const VVT& a, const HVT& b, 
338
    void outer_product(const VVT& a, const HVT& b, 
339
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
339
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
340
    {
340
    {
341
      unsigned int R = VVT::get_dim();
341
      unsigned int R = VVT::get_dim();
342
      unsigned int C = HVT::get_dim();
342
      unsigned int C = HVT::get_dim();
343
      for(unsigned int i=0;i<R;++i)
343
      for(unsigned int i=0;i<R;++i)
344
	for(unsigned int j=0;j<C;++j)
344
	for(unsigned int j=0;j<C;++j)
345
	  {
345
	  {
346
	    m[i][j] = a[i] * b[j];
346
	    m[i][j] = a[i] * b[j];
347
	  }
347
	  }
348
    }
348
    }
349
 
349
 
350
  /** Compute the outer product of a and b using an arbitrary 
350
  /** Compute the outer product of a and b using an arbitrary 
351
      binary operation: op(a, transpose(b)). This is 
351
      binary operation: op(a, transpose(b)). This is 
352
      a matrix with a::rows and b::columns. */
352
      a matrix with a::rows and b::columns. */
353
  template <class VVT, class HVT, class MT, int ROWS, class BinOp>
353
  template <class VVT, class HVT, class MT, int ROWS, class BinOp>
354
    void outer_product(const VVT& a, const HVT& b, 
354
    void outer_product(const VVT& a, const HVT& b, 
355
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m, BinOp op)
355
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m, BinOp op)
356
    {
356
    {
357
      int R = VVT::get_dim();
357
      int R = VVT::get_dim();
358
      int C = HVT::get_dim();
358
      int C = HVT::get_dim();
359
      for(int i=0;i<R;++i)
359
      for(int i=0;i<R;++i)
360
	for(int j=0;j<C;++j)
360
	for(int j=0;j<C;++j)
361
	  {
361
	  {
362
	    m[i][j] = op(a[i], b[j]);
362
	    m[i][j] = op(a[i], b[j]);
363
	  }
363
	  }
364
    }
364
    }
365
 
365
 
366
  /** Copy a matrix to another matrix, cell by cell.
366
  /** Copy a matrix to another matrix, cell by cell.
367
      This conversion that takes a const matrix as first argument
367
      This conversion that takes a const matrix as first argument
368
      (source) and a non-const matrix as second argument
368
      (source) and a non-const matrix as second argument
369
      (destination). The contents of the first matrix is simply copied
369
      (destination). The contents of the first matrix is simply copied
370
      to the second matrix. 
370
      to the second matrix. 
371
			
371
			
372
      However, if the first matrix is	larger than the second,
372
      However, if the first matrix is	larger than the second,
373
      the cells outside the range of the destination are simply not
373
      the cells outside the range of the destination are simply not
374
      copied. If the destination is larger, the cells outside the 
374
      copied. If the destination is larger, the cells outside the 
375
      range of the source matrix are not touched.
375
      range of the source matrix are not touched.
376
 
376
 
377
      An obvious use of this function is to copy a 3x3 rotation matrix
377
      An obvious use of this function is to copy a 3x3 rotation matrix
378
      into a 4x4 transformation matrix.
378
      into a 4x4 transformation matrix.
379
  */
379
  */
380
 
380
 
381
  template <class M1, class M2>
381
  template <class M1, class M2>
382
    void copy_matrix(const M1& inmat, M2& outmat)
382
    void copy_matrix(const M1& inmat, M2& outmat)
383
    {
383
    {
384
      const unsigned int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
384
      const unsigned int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
385
      const unsigned int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
385
      const unsigned int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
386
      for(unsigned int i=0;i<R;++i)
386
      for(unsigned int i=0;i<R;++i)
387
	for(unsigned int j=0;j<C;++j)
387
	for(unsigned int j=0;j<C;++j)
388
	  outmat[i][j] = inmat[i][j];
388
	  outmat[i][j] = inmat[i][j];
389
    }
389
    }
390
 
390
 
391
  /** Put to operator */
391
  /** Put to operator */
392
  template <class VVT, class HVT, class MT, unsigned int ROWS>
392
  template <class VVT, class HVT, class MT, unsigned int ROWS>
393
    inline std::ostream& 
393
    inline std::ostream& 
394
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
394
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
395
    {
395
    {
396
      os << "[\n";
396
      os << "[\n";
397
      for(unsigned int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
397
      for(unsigned int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
398
      os << "]\n";
398
      os << "]\n";
399
      return os;
399
      return os;
400
    }
400
    }
401
 
401
 
402
  /** Get from operator */
402
  /** Get from operator */
403
  template <class VVT, class HVT, class MT, unsigned int ROWS>
403
  template <class VVT, class HVT, class MT, unsigned int ROWS>
404
    inline std::istream& operator>>(std::istream&is, 
404
    inline std::istream& operator>>(std::istream&is, 
405
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
405
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
406
    {
406
    {
407
      for(unsigned int i=0;i<ROWS;i++) is>>m[i];
407
      for(unsigned int i=0;i<ROWS;i++) is>>m[i];
408
      return is;
408
      return is;
409
    }
409
    }
410
}
410
}
411
#endif
411
#endif
412
 
412