NCBI C++ ToolKit
matrix3.hpp
Go to the documentation of this file.

Go to the SVN repository for this file.

1 #ifndef GUI_MATH___MATRIX3___HPP
2 #define GUI_MATH___MATRIX3___HPP
3 
4 /* $Id: matrix3.hpp 27135 2012-12-27 16:45:51Z falkrb $
5  * ===========================================================================
6  *
7  * PUBLIC DOMAIN NOTICE
8  * National Center for Biotechnology Information
9  *
10  * This software / database is a "United States Government Work" under the
11  * terms of the United States Copyright Act. It was written as part of
12  * the author's official duties as a United States Government employee and
13  * thus cannot be copyrighted. This software / database is freely available
14  * to the public for use. The National Library of Medicine and the U.S.
15  * Government have not placed any restriction on its use or reproduction.
16  *
17  * Although all reasonable efforts have been taken to ensure the accuracy
18  * and reliability of the software and data, the NLM and the U.S.
19  * Government do not and cannot warrant the performance or results that
20  * may be obtained by using this software or data. The NLM and the U.S.
21  * Government disclaim all warranties, express or implied, including
22  * warranties of performance, merchantability or fitness for any particular
23  * purpose.
24  *
25  * Please cite the author in any work or product based on this material.
26  *
27  * ===========================================================================
28  *
29  * Authors: Mike DiCuccio
30  *
31  * File Description:
32  *
33  */
34 
35 #include <corelib/ncbistd.hpp>
36 
37 /** @addtogroup GUI_MATH
38  *
39  * @{
40  */
41 
42 #include <gui/utils/vect3.hpp>
43 
45 
46 
47 template <class T>
48 class CMatrix3
49 {
50 public:
51  // ctors
53  CMatrix3 (T,T,T, T,T,T, T,T,T);
54  CMatrix3 (const T[9]);
55  CMatrix3 (const T[3][3]);
56 
57  //
58  // operators
59  //
60 
61  // operator: index with bounds checking
62  T operator() (int i, int j) const { return m_Data[i * 3+j]; }
63  T& operator() (int i, int j) { return m_Data[i * 3+j]; }
64 
65  // operator: index with no bounds checking
66  T operator[] (int i) const { return m_Data[i]; }
67  T& operator[] (int i) { return m_Data[i]; }
68 
69  // operator: addition
72 
73  // operator: subtraction
76 
77  // operator: multiplication
80 
81  // operator: division
83 
84  //
85  // named functions
86 
87  // transpose the current matrix
88  void Transpose();
89 
90  // make the current matrix an identity matrix
91  void Identity();
92 
93  // clear the current matrix to a given value
94  void Clear(T x = (T)0);
95 
96  // return the determinant of the current matrix
97  T Determinant() const;
98 
99  // data accessor
100  const T* GetData() const { return m_Data; }
101  T* GetData() { return m_Data; }
102 
103  // return a vector representing a row
104  CVect3<T> Row(int r) const;
105 
106  // return a vector representing a column
107  CVect3<T> Column(int c) const;
108 
109 private:
110  T m_Data[9];
111 
112 };
113 
115 
116 //
117 // global operations
118 // this is included after the class declaration
119 #include <gui/utils/globals.hpp>
120 
122 
123 
124 //
125 // default ctor
126 //
127 template <class T> inline
129 {
130  Clear(T(0));
131 }
132 
133 //
134 // conversion ctors
135 //
136 template <class T> inline
137 CMatrix3<T>::CMatrix3 (T m1, T m2, T m3, T m4, T m5, T m6, T m7, T m8, T m9)
138 {
139  m_Data[0] = m1; m_Data[1] = m2; m_Data[2] = m3;
140  m_Data[3] = m4; m_Data[4] = m5; m_Data[5] = m6;
141  m_Data[6] = m7; m_Data[7] = m8; m_Data[8] = m9;
142 }
143 
144 
145 template <class T> inline
147 {
148  for (int i = 0; i < 9; ++i) {
149  m_Data[i] = m[i];
150  }
151 }
152 
153 
154 template <class T> inline
155 CMatrix3<T>::CMatrix3(const T m[3][3])
156 {
157  m_Data[0] = m[0][0];
158  m_Data[1] = m[0][1];
159  m_Data[2] = m[0][2];
160  m_Data[3] = m[1][0];
161  m_Data[4] = m[1][1];
162  m_Data[5] = m[1][2];
163  m_Data[6] = m[2][0];
164  m_Data[7] = m[2][1];
165  m_Data[8] = m[2][2];
166 }
167 
168 
169 //
170 // addition: matrix
171 template <class T> inline CMatrix3<T>&
173 {
174  for (int i = 0; i < 9; ++i) {
175  m_Data[i] += m[i];
176  }
177 
178  return *this;
179 }
180 
181 //
182 // Addition: scalar
183 template <class T> inline CMatrix3<T>&
185 {
186  for (int i = 0; i < 9; ++i) {
187  m_Data[i] += scalar;
188  }
189 
190  return *this;
191 }
192 
193 //
194 // subtraction: matrix
195 template <class T> inline CMatrix3<T>&
197 {
198  for (int i = 0; i < 9; ++i) {
199  m_Data[i] -= m[i];
200  }
201 
202  return *this;
203 }
204 
205 //
206 // Subtraction: scalar
207 template <class T> inline CMatrix3<T>&
209 {
210  for (int i = 0; i < 9; ++i) {
211  m_Data[i] -= scalar;
212  }
213 
214  return *this;
215 }
216 
217 
218 //
219 // multiplication: scalar
220 template <class T> inline CMatrix3<T>&
222 {
223  for (int i = 0; i < 9; ++i) {
224  m_Data[i] *= scalar;
225  }
226 
227  return *this;
228 }
229 
230 //
231 // multiplication: matrix
232 template <class T> inline CMatrix3<T>&
234 {
235  // given
236  //
237  // a b c 1 2 3
238  // d e f 4 5 6
239  // g h i 7 8 9
240  //
241  // result is:
242  // (a1 + b4 + c7) (a2 + b5 + c8) (a3 + b6 + c9)
243  // (d1 + e4 + f7) (d2 + e5 + f8) (d3 + e6 + f9)
244  // (g1 + h4 + i7) (g2 + h5 + i8) (g3 + h6 + i9)
245  //
246  T f1, f2, f3;
247 
248  f1 = m_Data[0]*m[0] + m_Data[1]*m[3] + m_Data[2]*m[6];
249  f2 = m_Data[0]*m[1] + m_Data[1]*m[4] + m_Data[2]*m[7];
250  f3 = m_Data[0]*m[2] + m_Data[1]*m[5] + m_Data[2]*m[8];
251  m_Data[0] = f1;
252  m_Data[1] = f2;
253  m_Data[2] = f3;
254 
255  f1 = m_Data[3]*m[0] + m_Data[4]*m[3] + m_Data[5]*m[6];
256  f2 = m_Data[3]*m[1] + m_Data[4]*m[4] + m_Data[5]*m[7];
257  f3 = m_Data[3]*m[2] + m_Data[4]*m[5] + m_Data[5]*m[8];
258  m_Data[3] = f1;
259  m_Data[4] = f2;
260  m_Data[5] = f3;
261 
262  f1 = m_Data[6]*m[0] + m_Data[7]*m[3] + m_Data[8]*m[6];
263  f2 = m_Data[6]*m[1] + m_Data[7]*m[4] + m_Data[8]*m[7];
264  f3 = m_Data[6]*m[2] + m_Data[7]*m[5] + m_Data[8]*m[8];
265  m_Data[6] = f1;
266  m_Data[7] = f2;
267  m_Data[8] = f3;
268 
269  return *this;
270 }
271 
272 //
273 // division: scalar
274 //
275 template <class T> inline CMatrix3<T>&
277 {
278  scalar = T(1) / scalar;
279  for (int i = 0; i < 9; ++i) {
280  m_Data[i] *= scalar;
281  }
282 
283  return *this;
284 }
285 
286 
287 //
288 // transpose a matrix
289 //
290 template <class T> inline void
292 {
293  // given
294  // a b c
295  // d e f
296  // g h i
297  //
298  // result is
299  // a d g
300  // b e h
301  // c f i
302  //
303 
304  std::swap(m_Data[3], m_Data[1]);
305  std::swap(m_Data[6], m_Data[2]);
306  std::swap(m_Data[7], m_Data[5]);
307 }
308 
309 //
310 // create an identity matrix
311 //
312 template <class T> inline void
314 {
315  m_Data[0] = (T)1;
316  m_Data[1] = (T)0;
317  m_Data[2] = (T)0;
318 
319  m_Data[3] = (T)0;
320  m_Data[4] = (T)1;
321  m_Data[5] = (T)0;
322 
323  m_Data[6] = (T)0;
324  m_Data[7] = (T)0;
325  m_Data[8] = (T)1;
326 }
327 
328 //
329 // clear a matrix so that all its values are the passed value
330 //
331 template <class T> inline void
333 {
334  for (int i = 0; i < 9; ++i) {
335  m_Data[i] = value;
336  }
337 }
338 
339 
340 //
341 // return a row as a vector
342 //
343 template <class T> inline CVect3<T>
344 CMatrix3<T>::Row(int i) const
345 {
346  return CVect3<T> (m_Data[i * 3], m_Data[i * 3 + 1], m_Data[i * 3 + 2]);
347 }
348 
349 
350 //
351 // return a column as a vector
352 //
353 template <class T> inline CVect3<T>
355 {
356  return CVect3<T> (m_Data[j], m_Data[3 + j], m_Data[6 + j]);
357 }
358 
359 
360 //
361 // return the determinant of a 3x3 matrix
362 //
363 template <class T> inline T
365 {
366  // given
367  // a b c
368  // d e f
369  // g h i
370  //
371  // the determinant is
372  // a(ei - hf) + b(di - fg) + c(dh - eg)
373  //
374  return (m_Data[0] * (m_Data[4]*m_Data[8] - m_Data[5]*m_Data[7]) +
375  m_Data[1] * (m_Data[3]*m_Data[8] - m_Data[5]*m_Data[6]) +
376  m_Data[2] * (m_Data[3]*m_Data[7] - m_Data[4]*m_Data[6]));
377 }
378 
379 
381 
382 /* @} */
383 
384 #endif // GUI_MATH___MATRIX3___HPP
Definition: vect3.hpp:48
char value[7]
Definition: config.c:431
Include a standard set of the NCBI C++ Toolkit most basic headers.
#define T(s)
Definition: common.h:230
void swap(NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair1, NCBI_NS_NCBI::pair_base_member< T1, T2 > &pair2)
Definition: ncbimisc.hpp:1508
void Identity()
Definition: matrix3.hpp:313
CMatrix3(const T[3][3])
Definition: matrix3.hpp:155
CMatrix3< T > & operator/=(T)
Definition: matrix3.hpp:276
T operator[](int i) const
Definition: matrix3.hpp:66
T operator()(int i, int j) const
Definition: matrix3.hpp:62
CVect3< T > Column(int c) const
Definition: matrix3.hpp:354
CVect3< T > Row(int r) const
Definition: matrix3.hpp:344
void Transpose()
Definition: matrix3.hpp:291
CMatrix3(T, T, T, T, T, T, T, T, T)
Definition: matrix3.hpp:137
void Clear(T x=(T) 0)
Definition: matrix3.hpp:332
T Determinant() const
Definition: matrix3.hpp:364
const T * GetData() const
Definition: matrix3.hpp:100
CMatrix3()
Definition: matrix3.hpp:128
CMatrix3< T > & operator+=(T)
Definition: matrix3.hpp:184
CMatrix3< T > & operator*=(T)
Definition: matrix3.hpp:221
T * GetData()
Definition: matrix3.hpp:101
CMatrix3(const T[9])
Definition: matrix3.hpp:146
CMatrix3< T > & operator-=(T)
Definition: matrix3.hpp:208
T m_Data[9]
Definition: matrix3.hpp:110
#define END_NCBI_SCOPE
End previously defined NCBI scope.
Definition: ncbistl.hpp:103
#define BEGIN_NCBI_SCOPE
Define ncbi namespace.
Definition: ncbistl.hpp:100
int i
double r(size_t dimension_, const Int4 *score_, const double *prob_, double theta_)
Modified on Sat Feb 24 07:48:39 2024 by modify_doxy.py rev. 669887