Subversion Repositories gelsvn

Rev

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

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