667 |
khor |
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 |
|
|
|
12 |
#ifndef __CGLA_ARITHSQMATFLOAT_H__
|
|
|
13 |
#define __CGLA_ARITHSQMATFLOAT_H__
|
|
|
14 |
|
|
|
15 |
#include "ArithMatFloat.h"
|
|
|
16 |
|
|
|
17 |
namespace CGLA
|
|
|
18 |
{
|
|
|
19 |
|
|
|
20 |
/** Template for square matrices.
|
|
|
21 |
|
|
|
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 |
*/
|
|
|
28 |
template <class VT, class MT, unsigned int ROWS>
|
|
|
29 |
class ArithSqMatFloat: public ArithMatFloat<VT,VT,MT,ROWS>
|
|
|
30 |
{
|
|
|
31 |
public:
|
|
|
32 |
|
|
|
33 |
/// Vector type
|
|
|
34 |
typedef VT VectorType;
|
|
|
35 |
|
|
|
36 |
/// The type of a matrix element
|
|
|
37 |
typedef typename VT::ScalarType ScalarType;
|
|
|
38 |
|
|
|
39 |
protected:
|
|
|
40 |
|
|
|
41 |
/// Construct 0 matrix
|
|
|
42 |
ArithSqMatFloat() {}
|
|
|
43 |
|
|
|
44 |
/// Construct matrix where all values are equal to constructor argument.
|
|
|
45 |
explicit ArithSqMatFloat(ScalarType _a):
|
|
|
46 |
ArithMatFloat<VT,VT,MT,ROWS>(_a) {}
|
|
|
47 |
|
|
|
48 |
/// Construct 2x2 Matrix from two vectors
|
|
|
49 |
ArithSqMatFloat(VT _a, VT _b):
|
|
|
50 |
ArithMatFloat<VT,VT,MT,ROWS>(_a,_b) {}
|
|
|
51 |
|
|
|
52 |
/// Construct 3x3 Matrix from three vectors
|
|
|
53 |
ArithSqMatFloat(VT _a, VT _b, VT _c):
|
|
|
54 |
ArithMatFloat<VT,VT,MT,ROWS>(_a,_b,_c) {}
|
|
|
55 |
|
|
|
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) {}
|
|
|
59 |
|
|
|
60 |
public:
|
|
|
61 |
|
|
|
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 |
}
|
|
|
70 |
|
|
|
71 |
const MT& operator *=(ScalarType k)
|
|
|
72 |
{
|
|
|
73 |
return ArithMatFloat<VT,VT,MT,ROWS>::operator*=(k);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
void identity()
|
|
|
77 |
{
|
|
|
78 |
for(unsigned int i=0;i<ROWS;++i)
|
|
|
79 |
{
|
|
|
80 |
for(unsigned int j=0;j<ROWS;++j)
|
|
|
81 |
(*this)[i][j] = ScalarType(0);
|
|
|
82 |
(*this)[i][i] = ScalarType(1);
|
|
|
83 |
}
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
};
|
|
|
88 |
|
|
|
89 |
/** Multiply two matrices derived from same type,
|
|
|
90 |
producing a new of same type */
|
|
|
91 |
template <class VT, class MT, unsigned int ROWS>
|
|
|
92 |
inline MT operator*(const ArithSqMatFloat<VT,MT,ROWS>& m1,
|
|
|
93 |
const ArithSqMatFloat<VT,MT,ROWS>& m2)
|
|
|
94 |
{
|
|
|
95 |
MT n;
|
|
|
96 |
for(unsigned int i=0;i<ROWS;i++)
|
|
|
97 |
for(unsigned int j=0;j<ROWS;j++)
|
|
|
98 |
{
|
|
|
99 |
n[i][j] = 0;
|
|
|
100 |
for(unsigned int k=0;k<ROWS;k++)
|
|
|
101 |
n[i][j] += m1[i][k] * m2[k][j];
|
|
|
102 |
}
|
|
|
103 |
return n;
|
|
|
104 |
}
|
|
|
105 |
|
|
|
106 |
/** Compute the transpose of a square matrix. This function returns
|
|
|
107 |
the transpose of its argument. */
|
|
|
108 |
template <class VT, class MT, unsigned int ROWS>
|
|
|
109 |
inline MT transpose(const ArithSqMatFloat<VT,MT,ROWS>& m)
|
|
|
110 |
{
|
|
|
111 |
MT m_new;
|
|
|
112 |
for(unsigned int i=0;i<MT::get_v_dim();i++)
|
|
|
113 |
for(unsigned int j=0;j<MT::get_h_dim();j++)
|
|
|
114 |
m_new[i][j] = m[j][i];
|
|
|
115 |
return m_new;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
/// Compute trace. Works only for sq. matrices.
|
|
|
119 |
template <class VT, class MT, unsigned int ROWS>
|
|
|
120 |
inline typename MT::ScalarType trace(const ArithSqMatFloat<VT,MT,ROWS>& M)
|
|
|
121 |
{
|
|
|
122 |
typename ArithSqMatFloat<VT,MT,ROWS>::ScalarType s=0;
|
|
|
123 |
for(unsigned int i=0;i<ROWS;i++)
|
|
|
124 |
s += M[i][i];
|
|
|
125 |
return s;
|
|
|
126 |
}
|
|
|
127 |
|
|
|
128 |
}
|
|
|
129 |
#endif
|