Subversion Repositories gelsvn

Rev

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

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