Subversion Repositories gelsvn

Rev

Rev 12 | Rev 45 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 12 Rev 43
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
 
6
 
7
#include "CGLA.h"
7
#include "CGLA.h"
8
 
8
 
9
 
9
 
10
namespace CGLA 
10
namespace CGLA 
11
{
11
{
12
 
12
 
13
  /** Basic class template for matrices.
13
  /** Basic class template for matrices.
14
		
14
		
15
  In this template a matrix is defined as an array of vectors. This may
15
  In this template a matrix is defined as an array of vectors. This may
16
  not in all cases be the most efficient but it has the advantage that 
16
  not in all cases be the most efficient but it has the advantage that 
17
  it is possible to use the double subscripting notation:
17
  it is possible to use the double subscripting notation:
18
		
18
		
19
  T x = m[i][j]
19
  T x = m[i][j]
20
 
20
 
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
 
33
      /// Vertical vector type
31
      /// Vertical vector type
34
      typedef VVT VVectorType;
32
      typedef VVT VVectorType;
35
 
33
 
36
      /// The type of a matrix element
34
      /// The type of a matrix element
37
      typedef typename HVT::ScalarType ScalarType;
35
      typedef typename HVT::ScalarType ScalarType;
38
		
36
		
39
    protected:
37
    protected:
40
 
38
 
41
      /// The actual contents of the matrix.
39
      /// The actual contents of the matrix.
42
      HVT data[ROWS];
40
      HVT data[ROWS];
43
 
41
 
44
    protected:
42
    protected:
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;
72
	  data[1] = _b;
69
	  data[1] = _b;
73
	}
70
	}
74
 
71
 
75
      /// Construct a matrix with three rows.
72
      /// Construct a matrix with three rows.
76
      ArithMatFloat(HVT _a, HVT _b, HVT _c)
73
      ArithMatFloat(HVT _a, HVT _b, HVT _c)
77
	{
74
	{
78
	  assert(ROWS==3);
75
	  assert(ROWS==3);
79
	  data[0] = _a;
76
	  data[0] = _a;
80
	  data[1] = _b;
77
	  data[1] = _b;
81
	  data[2] = _c;
78
	  data[2] = _c;
82
	}
79
	}
83
 
80
 
84
      /// Construct a matrix with four rows.
81
      /// Construct a matrix with four rows.
85
      ArithMatFloat(HVT _a, HVT _b, HVT _c, HVT _d)
82
      ArithMatFloat(HVT _a, HVT _b, HVT _c, HVT _d)
86
	{
83
	{
87
	  assert(ROWS==4);
84
	  assert(ROWS==4);
88
	  data[0] = _a;
85
	  data[0] = _a;
89
	  data[1] = _b;
86
	  data[1] = _b;
90
	  data[2] = _c;
87
	  data[2] = _c;
91
	  data[3] = _d;
88
	  data[3] = _d;
92
	}
89
	}
93
		
90
		
94
    public:
91
    public:
95
 
92
 
96
      /// Get vertical dimension of matrix 
93
      /// Get vertical dimension of matrix 
97
      static int get_v_dim() {return VVT::get_dim();}
94
      static int get_v_dim() {return VVT::get_dim();}
98
 
95
 
99
      /// Get horizontal dimension of matrix
96
      /// Get horizontal dimension of matrix
100
      static int get_h_dim() {return HVT::get_dim();}
97
      static int get_h_dim() {return HVT::get_dim();}
101
 
98
 
102
 
99
 
103
      /** Get const pointer to data array.
100
      /** Get const pointer to data array.
104
	  This function may be useful when interfacing with some other API 
101
	  This function may be useful when interfacing with some other API 
105
	  such as OpenGL (TM). */
102
	  such as OpenGL (TM). */
106
      const ScalarType* get() const 
103
      const ScalarType* get() const 
107
	{
104
	{
108
	  return data[0].get();
105
	  return data[0].get();
109
	}
106
	}
110
 
107
 
111
      /** Get pointer to data array.
108
      /** Get pointer to data array.
112
	  This function may be useful when interfacing with some other API 
109
	  This function may be useful when interfacing with some other API 
113
	  such as OpenGL (TM). */
110
	  such as OpenGL (TM). */
114
      ScalarType* get()
111
      ScalarType* get()
115
	{
112
	{
116
	  return data[0].get();
113
	  return data[0].get();
117
	}
114
	}
118
 
115
 
119
      //----------------------------------------------------------------------
116
      //----------------------------------------------------------------------
120
      // index operators
117
      // index operators
121
      //----------------------------------------------------------------------
118
      //----------------------------------------------------------------------
122
 
119
 
123
      /// Const index operator. Returns i'th row of matrix.
120
      /// Const index operator. Returns i'th row of matrix.
124
      const HVT& operator [] ( int i ) const
121
      const HVT& operator [] ( int i ) const
125
	{
122
	{
126
	  assert(i<ROWS);
123
	  assert(i<ROWS);
127
	  return data[i];
124
	  return data[i];
128
	}
125
	}
129
 
126
 
130
      /// Non-const index operator. Returns i'th row of matrix.
127
      /// Non-const index operator. Returns i'th row of matrix.
131
      HVT& operator [] ( int i ) 
128
      HVT& operator [] ( int i ) 
132
	{
129
	{
133
	  assert(i<ROWS);
130
	  assert(i<ROWS);
134
	  return data[i];
131
	  return data[i];
135
	}
132
	}
136
 
133
 
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
	{
149
	  return !(*this==v);
146
	  return !(*this==v);
150
	}
147
	}
151
 
148
 
152
      //----------------------------------------------------------------------
149
      //----------------------------------------------------------------------
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) 
233
    {
227
    {
234
      return v * k;
228
      return v * k;
235
    }
229
    }
236
 
230
 
237
  /// Multiply scalar onto matrix
231
  /// Multiply scalar onto matrix
238
  template <class VVT, class HVT, class MT, int ROWS>
232
  template <class VVT, class HVT, class MT, int ROWS>
239
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
233
    inline const MT operator * (float k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
240
    {
234
    {
241
      return v * k;
235
      return v * k;
242
    }
236
    }
243
 
237
 
244
  /// Multiply scalar onto matrix
238
  /// Multiply scalar onto matrix
245
  template <class VVT, class HVT, class MT, int ROWS>
239
  template <class VVT, class HVT, class MT, int ROWS>
246
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
240
    inline const MT operator * (int k, const ArithMatFloat<VVT,HVT,MT,ROWS>& v) 
247
    {
241
    {
248
      return v * k;
242
      return v * k;
249
    }
243
    }
250
 
244
 
251
  /// Multiply vector onto matrix 
245
  /// Multiply vector onto matrix 
252
  template <class VVT, class HVT, class MT, int ROWS>
246
  template <class VVT, class HVT, class MT, int ROWS>
253
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
247
    inline VVT operator*(const ArithMatFloat<VVT,HVT,MT,ROWS>& m,const HVT& v) 
254
    {
248
    {
255
      VVT v2;
249
      VVT v2;
256
      for(int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
250
      for(int i=0;i<ROWS;i++) v2[i] = dot(m[i], v);
257
      return v2;
251
      return v2;
258
    }
252
    }
259
 
253
 
260
 
254
 
261
#ifndef WIN32
255
#ifndef WIN32
262
  /** Multiply two arbitrary matrices. 
256
  /** Multiply two arbitrary matrices. 
263
      In principle, this function could return a matrix, but in general
257
      In principle, this function could return a matrix, but in general
264
      the new matrix will be of a type that is different from either of
258
      the new matrix will be of a type that is different from either of
265
      the two matrices that are multiplied together. We do not want to 
259
      the two matrices that are multiplied together. We do not want to 
266
      return an ArithMatFloat - so it seems best to let the return value be
260
      return an ArithMatFloat - so it seems best to let the return value be
267
      a reference arg.
261
      a reference arg.
268
		
262
		
269
      This template can only be instantiated if the dimensions of the
263
      This template can only be instantiated if the dimensions of the
270
      matrices match -- i.e. if the multiplication can actually be
264
      matrices match -- i.e. if the multiplication can actually be
271
      carried out. This is more type safe than the win32 version below.
265
      carried out. This is more type safe than the win32 version below.
272
  */
266
  */
273
 
267
 
274
  template <class VVT, class HVT, 
268
  template <class VVT, class HVT, 
275
    class HV1T, class VV2T,
269
    class HV1T, class VV2T,
276
    class MT1, class MT2, class MT,
270
    class MT1, class MT2, class MT,
277
    int ROWS1, int ROWS2>
271
    int ROWS1, int ROWS2>
278
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
272
    inline void mul(const ArithMatFloat<VVT,HV1T,MT1,ROWS1>& m1,
279
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
273
		    const ArithMatFloat<VV2T,HVT,MT2,ROWS2>& m2,
280
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
274
		    ArithMatFloat<VVT,HVT,MT,ROWS1>& m)
281
    {
275
    {
282
      int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
276
      int cols = ArithMatFloat<VVT,HVT,MT,ROWS1>::get_h_dim();
283
      for(int i=0;i<ROWS1;i++)
277
      for(int i=0;i<ROWS1;i++)
284
	for(int j=0;j<cols;j++)
278
	for(int j=0;j<cols;j++)
285
	  {
279
	  {
286
	    m[i][j] = 0;
280
	    m[i][j] = 0;
287
	    for(int k=0;k<ROWS2;k++)
281
	    for(int k=0;k<ROWS2;k++)
288
	      m[i][j] += m1[i][k] * m2[k][j]; 
282
	      m[i][j] += m1[i][k] * m2[k][j]; 
289
	  }
283
	  }
290
    }
284
    }
291
 
285
 
292
 
286
 
293
  /** Transpose. See the discussion on mul if you are curious as to why
287
  /** Transpose. See the discussion on mul if you are curious as to why
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
 
306
  //----------------- win32 -------------------------------
300
  //----------------- win32 -------------------------------
307
  // Visual studio is not good at deducing the args. to these template functions.
301
  // Visual studio is not good at deducing the args. to these template functions.
308
  // This means that you can call the two functions below with 
302
  // This means that you can call the two functions below with 
309
  // matrices of wrong dimension.
303
  // matrices of wrong dimension.
310
 
304
 
311
  template <class M1, class M2, class M>
305
  template <class M1, class M2, class M>
312
    inline void mul(const M1& m1, const M2& m2, M& m)
306
    inline void mul(const M1& m1, const M2& m2, M& m)
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
 
340
  /** Compute the outer product of a and b: a * transpose(b). This is 
334
  /** Compute the outer product of a and b: a * transpose(b). This is 
341
      a matrix with a::rows and b::columns. */
335
      a matrix with a::rows and b::columns. */
342
  template <class VVT, class HVT, class MT, int ROWS>
336
  template <class VVT, class HVT, class MT, int ROWS>
343
    void outer_product(const VVT& a, const HVT& b, 
337
    void outer_product(const VVT& a, const HVT& b, 
344
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
338
		       ArithMatFloat<VVT,HVT,MT,ROWS>& m)
345
    {
339
    {
346
      int R = VVT::get_dim();
340
      int R = VVT::get_dim();
347
      int C = HVT::get_dim();
341
      int C = HVT::get_dim();
348
      for(int i=0;i<R;++i)
342
      for(int i=0;i<R;++i)
349
	for(int j=0;j<C;++j)
343
	for(int j=0;j<C;++j)
350
	  {
344
	  {
351
	    m[i][j] = a[i] * b[j];
345
	    m[i][j] = a[i] * b[j];
352
	  }
346
	  }
353
    }
347
    }
354
 
348
 
355
  /** Copy a matrix to another matrix, cell by cell.
349
  /** Copy a matrix to another matrix, cell by cell.
356
      This conversion that takes a const matrix as first argument
350
      This conversion that takes a const matrix as first argument
357
      (source) and a non-const matrix as second argument
351
      (source) and a non-const matrix as second argument
358
      (destination). The contents of the first matrix is simply copied
352
      (destination). The contents of the first matrix is simply copied
359
      to the second matrix. 
353
      to the second matrix. 
360
			
354
			
361
      However, if the first matrix is	larger than the second,
355
      However, if the first matrix is	larger than the second,
362
      the cells outside the range of the destination are simply not
356
      the cells outside the range of the destination are simply not
363
      copied. If the destination is larger, the cells outside the 
357
      copied. If the destination is larger, the cells outside the 
364
      range of the source matrix are not touched.
358
      range of the source matrix are not touched.
365
 
359
 
366
      An obvious use of this function is to copy a 3x3 rotation matrix
360
      An obvious use of this function is to copy a 3x3 rotation matrix
367
      into a 4x4 transformation matrix.
361
      into a 4x4 transformation matrix.
368
  */
362
  */
369
 
363
 
370
  template <class M1, class M2>
364
  template <class M1, class M2>
371
    void copy_matrix(const M1& inmat, M2& outmat)
365
    void copy_matrix(const M1& inmat, M2& outmat)
372
    {
366
    {
373
      const int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
367
      const int R = s_min(inmat.get_v_dim(), outmat.get_v_dim());
374
      const int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
368
      const int C = s_min(inmat.get_h_dim(), outmat.get_h_dim());
375
      for(int i=0;i<R;++i)
369
      for(int i=0;i<R;++i)
376
	for(int j=0;j<C;++j)
370
	for(int j=0;j<C;++j)
377
	  outmat[i][j] = inmat[i][j];
371
	  outmat[i][j] = inmat[i][j];
378
    }
372
    }
379
 
373
 
380
  /** Put to operator */
374
  /** Put to operator */
381
  template <class VVT, class HVT, class MT, int ROWS>
375
  template <class VVT, class HVT, class MT, int ROWS>
382
    inline std::ostream& 
376
    inline std::ostream& 
383
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
377
    operator<<(std::ostream&os, const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
384
    {
378
    {
385
      os << "[\n";
379
      os << "[\n";
386
      for(int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
380
      for(int i=0;i<ROWS;i++) os << "  " << m[i] << "\n";
387
      os << "]\n";
381
      os << "]\n";
388
      return os;
382
      return os;
389
    }
383
    }
390
 
384
 
391
  /** Get from operator */
385
  /** Get from operator */
392
  template <class VVT, class HVT, class MT, int ROWS>
386
  template <class VVT, class HVT, class MT, int ROWS>
393
    inline std::istream& operator>>(std::istream&is, 
387
    inline std::istream& operator>>(std::istream&is, 
394
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
388
				    const ArithMatFloat<VVT,HVT,MT,ROWS>& m)
395
    {
389
    {
396
      for(int i=0;i<ROWS;i++) is>>m[i];
390
      for(int i=0;i<ROWS;i++) is>>m[i];
397
      return is;
391
      return is;
398
    }
392
    }
399
}
393
}
400
#endif
394
#endif
401
 
395