Subversion Repositories gelsvn

Rev

Rev 67 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 bj 1
#ifndef __CGLA_ARITHSQMATFLOAT_H__
2
#define __CGLA_ARITHSQMATFLOAT_H__
3
 
4
#include "ArithMatFloat.h"
5
 
12 jab 6
namespace CGLA 
7
{
2 bj 8
 
12 jab 9
  /** Template for square matrices.
94 bj 10
 
12 jab 11
      Some functions like trace and determinant work only on
12
      square matrices. To express this in the class hierarchy,
13
      ArithSqMatFloat was created. ArithSqMatFloat is derived from ArithMat
14
      and contains a few extra facilities applicable only to
15
      square matrices.
16
  */
45 jab 17
  template <class VT, class MT, unsigned int ROWS>
12 jab 18
    class ArithSqMatFloat: public ArithMatFloat<VT,VT,MT,ROWS> 
19
    { 
20
    public:
2 bj 21
 
12 jab 22
      /// Vector type
23
      typedef VT VectorType;
2 bj 24
 
12 jab 25
      /// The type of a matrix element
26
      typedef typename VT::ScalarType ScalarType;
2 bj 27
 
12 jab 28
    protected:
2 bj 29
 
12 jab 30
      /// Construct 0 matrix
31
      ArithSqMatFloat() {}
2 bj 32
 
12 jab 33
      /// Construct matrix where all values are equal to constructor argument.
34
      explicit ArithSqMatFloat(ScalarType _a):
35
	ArithMatFloat<VT,VT,MT,ROWS>(_a) {}
2 bj 36
 
12 jab 37
      /// Construct 2x2 Matrix from two vectors
38
      ArithSqMatFloat(VT _a, VT _b): 
39
	ArithMatFloat<VT,VT,MT,ROWS>(_a,_b) {}
2 bj 40
 
12 jab 41
      /// Construct 3x3 Matrix from three vectors
42
      ArithSqMatFloat(VT _a, VT _b, VT _c): 
43
	ArithMatFloat<VT,VT,MT,ROWS>(_a,_b,_c) {}
2 bj 44
 
12 jab 45
      /// Construct 4x4 Matrix from four vectors
46
      ArithSqMatFloat(VT _a, VT _b, VT _c, VT _d): 
47
	ArithMatFloat<VT,VT,MT,ROWS>(_a,_b,_c,_d) {}
2 bj 48
 
12 jab 49
    public:
2 bj 50
 
12 jab 51
      /** Assignment multiplication of matrices. 
52
	  This function is not very efficient. This because we need a temporary
53
	  matrix anyway, so it can't really be made efficient. */
54
      const MT& operator*=(const MT& m2)
55
	{
56
	  (*this) = (*this) * m2;
57
	  return static_cast<const MT&>(*this);
58
	}
2 bj 59
 
44 jrf 60
      const MT& operator *=(ScalarType k) 
61
	{
62
	  return ArithMatFloat<VT,VT,MT,ROWS>::operator*=(k);
63
	}
64
 
12 jab 65
      void identity()
66
	{
45 jab 67
	  for(unsigned int i=0;i<ROWS;++i)
12 jab 68
	    {
45 jab 69
	      for(unsigned int j=0;j<ROWS;++j)
12 jab 70
		(*this)[i][j] = ScalarType(0);
71
	      (*this)[i][i] = ScalarType(1);
72
	    }
73
	}
2 bj 74
 
75
 
12 jab 76
    };
2 bj 77
 
12 jab 78
  /** Multiply two matrices derived from same type, 
79
      producing a new of same type */
45 jab 80
  template <class VT, class MT, unsigned int ROWS>
12 jab 81
    inline MT operator*(const ArithSqMatFloat<VT,MT,ROWS>& m1,
82
			const ArithSqMatFloat<VT,MT,ROWS>& m2) 
83
    {
84
      MT n;
45 jab 85
      for(unsigned int i=0;i<ROWS;i++)
86
	for(unsigned int j=0;j<ROWS;j++)
12 jab 87
	  {
88
	    n[i][j] = 0;
45 jab 89
	    for(unsigned int k=0;k<ROWS;k++)
12 jab 90
	      n[i][j] += m1[i][k] * m2[k][j]; 
91
	  }
92
      return n;
93
    }
2 bj 94
 
12 jab 95
  /** Compute the transpose of a square matrix. This function returns
96
      the transpose of its argument. */
45 jab 97
  template <class VT, class MT, unsigned int ROWS>
12 jab 98
    inline MT transpose(const ArithSqMatFloat<VT,MT,ROWS>& m) 
99
    {
100
      MT m_new;
45 jab 101
      for(unsigned int i=0;i<MT::get_v_dim();i++)
102
	for(unsigned int j=0;j<MT::get_h_dim();j++)
12 jab 103
	  m_new[i][j] = m[j][i];
104
      return m_new;
105
    }
2 bj 106
 
12 jab 107
  /// Compute trace. Works only for sq. matrices.
45 jab 108
  template <class VT, class MT, unsigned int ROWS>
12 jab 109
    inline typename MT::ScalarType trace(const ArithSqMatFloat<VT,MT,ROWS>& M)
110
    {
111
      typename ArithSqMatFloat<VT,MT,ROWS>::ScalarType s=0;
45 jab 112
      for(unsigned int i=0;i<ROWS;i++)
12 jab 113
	s += M[i][i];
114
      return s;
115
    }
2 bj 116
 
117
}
118
#endif