GL Studio C++ Runtime API
gls_matrix.h
Go to the documentation of this file.
1/// \file gls_matrix.h
2/// \brief The GlsMatrix class.
3///
4/// \par Copyright Information
5///
6/// Copyright (c) 2017 by The DiSTI Corporation.<br>
7/// 12249 Science Drive; Suite 300<br>
8/// Orlando, Florida 32826<br>
9/// USA<br>
10/// <br>
11/// All rights reserved.<br>
12///
13/// This Software contains proprietary trade secrets of DiSTI and may not be
14/// reproduced, in whole or part, in any form, or by any means of electronic,
15/// mechanical, or otherwise, without the written permission of DiSTI. Said
16/// permission may be derived through the purchase of applicable DiSTI product
17/// licenses which detail the distribution rights of this content and any
18/// Derivative Works based on this or other copyrighted DiSTI Software.
19///
20/// NO WARRANTY. THE SOFTWARE IS PROVIDED "AS-IS," WITHOUT WARRANTY OF ANY KIND,
21/// AND ANY USE OF THIS SOFTWARE PRODUCT IS AT YOUR OWN RISK. TO THE MAXIMUM EXTENT
22/// PERMITTED BY APPLICABLE LAW, DISTI AND ITS SUPPLIERS DISCLAIM ALL WARRANTIES
23/// AND CONDITIONS, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
24/// IMPLIED WARRANTIES AND CONDITIONS OF MERCHANTABILITY AND/OR FITNESS FOR A
25/// PARTICULAR PURPOSE, TITLE, AND NON-INFRINGEMENT, WITH REGARD TO THE SOFTWARE.
26///
27/// LIMITATION OF LIABILITY. TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW,
28/// IN NO EVENT SHALL DISTI OR ITS SUPPLIERS BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
29/// INDIRECT, OR CONSEQUENTIAL DAMAGES WHATSOEVER (INCLUDING, WITHOUT LIMITATION,
30/// DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION, LOSS OF BUSINESS
31/// INFORMATION, OR ANY OTHER PECUNIARY LOSS) ARISING OUT OF THE USE OF OR
32/// INABILITY TO USE THE SOFTWARE, EVEN IF DISTI HAS BEEN ADVISED OF THE POSSIBILITY
33/// OF SUCH DAMAGES. DISTI'S ENTIRE LIABILITY AND YOUR EXCLUSIVE REMEDY SHALL NOT
34/// EXCEED FIVE DOLLARS (US$5.00).
35///
36/// The aforementioned terms and restrictions are governed by the laws of the
37/// State of Florida and the United States of America.
38
39#ifndef GLS_MATRIX_H
40#define GLS_MATRIX_H
41
42#include <cstring>
43#include <iostream>
44#include <math.h>
45#include <memory>
46
47namespace disti
48{
49//----------------------------------------------------------------------------
50/**
51* The GlsMatrix class provides support for an n x n square matrix of any
52* numeric type. Most basic matrix operations are supported. Internally,
53* the matrix terms are stored contiguously in column-major order so that the
54* matrix data can be passed to OpenGL matrix operations. However, instances
55* of this class can be treated as row-major matrices since the API performs
56* the conversion to column-major ordering for the user.
57*/
58//----------------------------------------------------------------------------
59template<class Type, int DIM = 4>
61{
62public:
63 typedef Type CStyleMatrix[ DIM ][ DIM ]; ///< Typedef for a 4x4 matrix stored as a 2D array.
64
65 //------------------------------------------------------------------------
66 /**
67 * Default constructor. The new matrix will be the identity matrix.
68 */
69 //------------------------------------------------------------------------
71
72 //------------------------------------------------------------------------
73 /**
74 * Constructor. This will transpose m so that the matrix is stored in
75 * column-major order, but a caller can pass one in that is in row-major
76 * order, which is more intuitive.
77 *
78 * \param m a row-major, two-dimensional array to initialize the matrix
79 * with.
80 */
81 //------------------------------------------------------------------------
83
84 //------------------------------------------------------------------------
85 /**
86 * Constructor.
87 *
88 * \param m a column-major, one-dimensional array containing all values
89 * to initialize the matrix with.
90 */
91 //------------------------------------------------------------------------
92 GlsMatrix( const Type* m );
93
94 //------------------------------------------------------------------------
95 /**
96 * Copy Constructor.
97 *
98 * \param m matrix to copy.
99 */
100 //------------------------------------------------------------------------
102
103 //------------------------------------------------------------------------
104 /**
105 * Destructor.
106 */
107 //------------------------------------------------------------------------
108 virtual ~GlsMatrix();
109
110 //------------------------------------------------------------------------
111 /**
112 * Dump the contents of the matrix to standard output.
113 */
114 //------------------------------------------------------------------------
115 void Dump() const;
116
117 //------------------------------------------------------------------------
118 /**
119 * Zero out the matrix so that all terms are equal to zero.
120 */
121 //------------------------------------------------------------------------
122 void Clear() { memset( _data, 0, sizeof( Type ) * DIM * DIM ); }
123
124 /// Provides access to the raw column-major matrix data as a one dimensional
125 /// array in a format suitable for Open GL matrix operations.
126 /// \return A pointer to the underlying matrix array.
127 Type* Data() { return _data; }
128
129 /// Provides access to the raw column-major matrix data as a one dimensional
130 /// array in a format suitable for Open GL matrix operations.
131 /// \return A const pointer to the underlying matrix array.
132 const Type* Data() const { return _data; }
133
134 //------------------------------------------------------------------------
135 /**
136 * Set this matrix to the identity matrix. The identity matrix is
137 * I = [Aij], where Aii = 1 and Aij = 0 for i != j. Thus, for the identity
138 * matrix, the terms off the main diagonal are all zero, and the terms on
139 * the main diagonal are all one.
140 */
141 //------------------------------------------------------------------------
143
144 //------------------------------------------------------------------------
145 /**
146 * Calculates the determinant of the matrix.
147 *
148 * \return the calculated value of the determinant.
149 */
150 //------------------------------------------------------------------------
151 Type Determinant() const;
152
153 //------------------------------------------------------------------------
154 /**
155 * Calculates the inverse of this matrix. The inverse of matrix A is B if
156 * AB = BA = I, where I is the identity matrix. The inverse of a matrix is
157 * the notion corresponding to the reciprocal of a nonzero real number.
158 *
159 * \return the inverse of this matrix.
160 */
161 //------------------------------------------------------------------------
163
164 //------------------------------------------------------------------------
165 /**
166 * Sets this matrix equal to its inverse.
167 */
168 //------------------------------------------------------------------------
169 void Invert();
170
171 //------------------------------------------------------------------------
172 /**
173 * Calculates the transpose of this matrix. The transpose of a matrix
174 * is obtained by interchanging its rows and columns.
175 *
176 * \return the transpose of this matrix.
177 */
178 //------------------------------------------------------------------------
180
181 //------------------------------------------------------------------------
182 /**
183 * Sets this matrix equal to its transpose.
184 */
185 //------------------------------------------------------------------------
186 void Transpose();
187
188 //------------------------------------------------------------------------
189 /**
190 * Determines if this is a diagonal matrix. An n x n matrix A = [Aij] is
191 * called a diagonal matrix if Aij = 0 for i != j. Thus, for a diagonal
192 * matrix, the terms off the main diagonal are all zero.
193 *
194 * \return true if the matrix is a diagonal matrix, false otherwise.
195 */
196 //------------------------------------------------------------------------
197 bool IsDiagonal() const;
198
199 //------------------------------------------------------------------------
200 /**
201 * Determines if this matrix is equal to the identity matrix. The
202 * identity matrix is I = [Aij], where Aii = 1 and Aij = 0 for i != j.
203 * Thus, for the identity matrix, the terms off the main diagonal are all
204 * zero, and the terms on the main diagonal are all one.
205 *
206 * \return true if the matrix is the identity matrix, false otherwise.
207 */
208 //------------------------------------------------------------------------
209 bool IsIdentity() const;
210
211 //------------------------------------------------------------------------
212 /**
213 * Determines if this is a scalar matrix. A scalar matrix is a diagonal
214 * matrix whose diagonal terms are equal.
215 *
216 * \return true if the matrix is a scalar matrix, false otherwise.
217 */
218 //------------------------------------------------------------------------
219 bool IsScalar() const;
220
221 //------------------------------------------------------------------------
222 /**
223 * Determines if this matrix is singular, or non-invertible. A singular
224 * matrix has no inverse.
225 *
226 * \return true if the matrix is singular, false otherwise.
227 */
228 //------------------------------------------------------------------------
229 bool IsSingular() const;
230
231 //------------------------------------------------------------------------
232 /**
233 * Determines if this matrix is symmetric. A matrix is symmetric if it is
234 * equal to its transpose, i.e. A = A'.
235 *
236 * \return true if the matrix is symmetric, false otherwise.
237 */
238 //------------------------------------------------------------------------
239 bool IsSymmetric() const;
240
241 //------------------------------------------------------------------------
242 /**
243 * Determines if this matrix is skew symmetric. A matrix is skew symmetric
244 * if its transpose is equal to its negative, i.e. -A = A'.
245 *
246 * \return true if the matrix is skew symmetric, false otherwise.
247 */
248 //------------------------------------------------------------------------
249 bool IsSkewSymmetric() const;
250
251 //------------------------------------------------------------------------
252 /**
253 * Determines if this matrix is lower triangular. An n x n matrix A = [Aij]
254 * is lower triangular if Aij = 0 for i < j.
255 *
256 * \return true if the matrix is lower triangular, false otherwise.
257 */
258 //------------------------------------------------------------------------
259 bool IsLowerTriangular() const;
260
261 //------------------------------------------------------------------------
262 /**
263 * Determines if this matrix is upper triangular. An n x n matrix A = [Aij]
264 * is upper triangular if Aij = 0 for i > j.
265 *
266 * \return true if the matrix is upper triangular, false otherwise.
267 */
268 //------------------------------------------------------------------------
269 bool IsUpperTriangular() const;
270
271 //------------------------------------------------------------------------
272 // Operators
273 //------------------------------------------------------------------------
274
275 /// Subscript operator (zero-based, no bounds checking).
276 /// \param row The row of the element to retrieve.
277 /// \param col The column of the element to retrieve.
278 /// \return A reference to the element at that position.
279 Type& operator()( unsigned row, unsigned col );
280
281 /// Subscript operator (zero-based, no bounds checking).
282 /// \param row The row of the element to retrieve.
283 /// \param col The column of the element to retrieve.
284 /// \return The value of the element at that position.
285 Type operator()( unsigned row, unsigned col ) const;
286
287 // Equality operator
288 /// \param rhs The matrix to compare.
289 /// \return Whether or not the matrices are equal.
290 bool operator==( const GlsMatrix<Type, DIM>& rhs ) const;
291
292 // Inequality operator
293 /// \param rhs The matrix to compare.
294 /// \return whether or not the matrices are not equal.
295 bool operator!=( const GlsMatrix<Type, DIM>& rhs ) const;
296
297 /// Assignment operator
298 /// \param m The matrix to copy from.
299 /// \return The resulting matrix (this).
301
302 /// Assignment operator
303 /// \param m The matrix to copy from.
304 /// \return The resulting matrix (this).
306
307 /// Unary + operator
308 /// \return This matrix.
309 GlsMatrix<Type, DIM> operator+() const { return *this; }
310
311 /// Unary - operator
312 /// \return Matrix with all elements multiplied by -1.
314
315 /// Combined assignment += operator
316 /// \param rhs Matrix to add.
317 /// \return The resulting matrix (this).
319
320 /// Combined assignment -= operator
321 /// \param rhs Matrix to subtract.
322 /// \return The resulting matrix (this).
324
325 /// Combined assignment *= operator
326 /// \param rhs Matrix to multiply by.
327 /// \return The resulting matrix (this).
329
330 /// Combined assignment *= operator
331 /// \param scalar Value to multiply by.
332 /// \return The resulting matrix (this).
334
335 /// Combined assignment /= operator
336 /// \param scalar Value to divide by.
337 /// \return The resulting matrix (this).
339
340 // Mathematical + operator
341 /// \param rhs The matrix to add.
342 /// \return The resulting matrix.
344
345 // Mathematical - operator
346 /// \param rhs The matrix to subtract.
347 /// \return The resulting matrix.
349
350 // Mathematical * operator
351 /// \param rhs The matrix to multiply by.
352 /// \return The resulting matrix.
354
355 // Mathematical * operator
356 /// \param scalar The value to multiply by.
357 /// \return The resulting matrix.
358 GlsMatrix<Type, DIM> operator*( Type scalar ) const;
359
360 // Mathematical / operator
361 /// \param scalar The value to divide by.
362 /// \return The resulting matrix.
363 GlsMatrix<Type, DIM> operator/( Type scalar ) const;
364
365protected:
366 /** A very tiny number, close to zero. Used to compare floats */
367 static const double PRECISION;
368
369 /**
370 * This is the actual matrix data stored in column-major ordering in
371 * a one-dimensional array of the appropriate size to store all matrix
372 * terms. The data is stored in this manner so that it can be passed
373 * to OpenGL matrix operations, which expect matrices to be ordered
374 * this way.
375 */
376 Type _data[ DIM * DIM ];
377
378 /**
379 * A column index array. Each element in this array "points"
380 * to a column of the matrix within the above data. By using this index,
381 * we can address matrix terms using two dimensions, column and row,
382 * without incurring a multiplication. Eg. matrix[0] points to a column
383 * within data, which is the address of the first term in the column.
384 * We can further address into the column and access a particular row
385 * by adding another dimension, eg. matrix[0][1]. The indices must
386 * be set up at construction time.
387 */
388 Type* _matrix[ DIM ];
389
390 //------------------------------------------------------------------------
391 /**
392 * This should be called from all constructors, as it initializes the
393 * matrix column index so that matrix elements can be addressed in two
394 * dimensions, but a multiplication is not incurred when doing so.
395 */
396 //------------------------------------------------------------------------
398
399 /// Pivoting is used in conjunction with Gauss-Jordan reduction to find
400 /// the inverse of a matrix and in Gaussian Elimination to find the
401 /// determinant of a matrix. It involves the elementary row operation of
402 /// interchanging rows (or columns in our column-major matrix). Only the
403 /// row passed in and rows below it are considered. The row passed in
404 /// will be swapped with another row below it by selecting the desired
405 /// pivot element. The pivot is the largest term (in magnitude) in
406 /// the column equal to row for any of the considered rows. This will
407 /// always put the largest (in magnitude) term from the column on the
408 /// diagonal of the matrix.
409 /// \param row is the current row to pivot about. Rows above this are not
410 /// considered during pivoting, as these rows should already be in
411 /// their proper form.
412 /// \return the row number index that was interchanged, if the pivot is different to the incoming row
413 /// 0 if the pivot was successful on the incoming row
414 /// -1 if no pivot is possible
415 int Pivot( unsigned row );
416
417 /// Decides if the two values are close together determined by the constant PRECISION.
418 /// The formula is: lv - PRECISION < rv < lv + PRECISION
419 /// \param lv The left value.
420 /// \param rv The right value.
421 /// \return Whether or not the two values are close together.
422 bool closeValues( Type lv, Type rv ) const;
423};
424
425//============================================================================
426//
427// CLASS IMPLEMENTATION
428//
429//============================================================================
430
431//----------------------------------------------------------------------------
432template<class Type, int DIM>
433const double GlsMatrix<Type, DIM>::PRECISION = 1e-7;
434
435//----------------------------------------------------------------------------
436template<class Type, int DIM>
438{
439 Initialize();
440 MakeIdentity();
441}
442//----------------------------------------------------------------------------
443template<class Type, int DIM>
444GlsMatrix<Type, DIM>::GlsMatrix( const CStyleMatrix m )
445{
446 Initialize();
447 for( int row = 0; row < DIM; ++row )
448 {
449 for( int col = 0; col < DIM; ++col )
450 {
451 _matrix[ col ][ row ] = m[ row ][ col ];
452 }
453 }
454}
455//----------------------------------------------------------------------------
456template<class Type, int DIM>
458{
459 Initialize();
460 memcpy( _data, m, sizeof( Type ) * DIM * DIM );
461}
462//----------------------------------------------------------------------------
463template<class Type, int DIM>
465{
466 Initialize();
467 memcpy( _data, m._data, sizeof( Type ) * DIM * DIM );
468}
469//----------------------------------------------------------------------------
470template<class Type, int DIM>
472{
473}
474
475/// Mathematical * operator
476/// \param rhs The matrix to multiply by.
477/// \return The resulting matrix.
478template<>
481{
483
484 // Same as regular, just unroll the loop
485 result._matrix[ 0 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 0 ][ 3 ];
486 result._matrix[ 1 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 1 ][ 3 ];
487 result._matrix[ 2 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 2 ][ 3 ];
488 result._matrix[ 3 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 3 ][ 3 ];
489 result._matrix[ 0 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 0 ][ 3 ];
490 result._matrix[ 1 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 1 ][ 3 ];
491 result._matrix[ 2 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 2 ][ 3 ];
492 result._matrix[ 3 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 3 ][ 3 ];
493 result._matrix[ 0 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 0 ][ 3 ];
494 result._matrix[ 1 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 1 ][ 3 ];
495 result._matrix[ 2 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 2 ][ 3 ];
496 result._matrix[ 3 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 3 ][ 3 ];
497 result._matrix[ 0 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 0 ][ 3 ];
498 result._matrix[ 1 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 1 ][ 3 ];
499 result._matrix[ 2 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 2 ][ 3 ];
500 result._matrix[ 3 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 3 ][ 3 ];
501
502 return result;
503}
504
505/// Mathematical * operator
506/// \param rhs The matrix to multiply by.
507/// \return The resulting matrix.
508template<>
511{
512 GlsMatrix<float, 4> result;
513
514 // Same as regular, just unroll the loop
515 result._matrix[ 0 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 0 ][ 3 ];
516 result._matrix[ 1 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 1 ][ 3 ];
517 result._matrix[ 2 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 2 ][ 3 ];
518 result._matrix[ 3 ][ 0 ] = _matrix[ 0 ][ 0 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 0 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 0 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 0 ] * rhs._matrix[ 3 ][ 3 ];
519 result._matrix[ 0 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 0 ][ 3 ];
520 result._matrix[ 1 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 1 ][ 3 ];
521 result._matrix[ 2 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 2 ][ 3 ];
522 result._matrix[ 3 ][ 1 ] = _matrix[ 0 ][ 1 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 1 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 1 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 1 ] * rhs._matrix[ 3 ][ 3 ];
523 result._matrix[ 0 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 0 ][ 3 ];
524 result._matrix[ 1 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 1 ][ 3 ];
525 result._matrix[ 2 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 2 ][ 3 ];
526 result._matrix[ 3 ][ 2 ] = _matrix[ 0 ][ 2 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 2 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 2 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 2 ] * rhs._matrix[ 3 ][ 3 ];
527 result._matrix[ 0 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 0 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 0 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 0 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 0 ][ 3 ];
528 result._matrix[ 1 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 1 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 1 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 1 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 1 ][ 3 ];
529 result._matrix[ 2 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 2 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 2 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 2 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 2 ][ 3 ];
530 result._matrix[ 3 ][ 3 ] = _matrix[ 0 ][ 3 ] * rhs._matrix[ 3 ][ 0 ] + _matrix[ 1 ][ 3 ] * rhs._matrix[ 3 ][ 1 ] + _matrix[ 2 ][ 3 ] * rhs._matrix[ 3 ][ 2 ] + _matrix[ 3 ][ 3 ] * rhs._matrix[ 3 ][ 3 ];
531
532 return result;
533}
534//----------------------------------------------------------------------------
535template<class Type, int DIM>
538{
540 memset( result._data, 0, sizeof( Type ) * DIM * DIM );
541 for( int row = 0; row < DIM; ++row )
542 {
543 for( int col = 0; col < DIM; ++col )
544 {
545 for( int i = 0; i < DIM; ++i )
546 {
547 result._matrix[ col ][ row ] += _matrix[ i ][ row ] * rhs._matrix[ col ][ i ];
548 }
549 }
550 }
551 return result;
552}
553//----------------------------------------------------------------------------
554template<class Type, int DIM>
556{
558 for( int row = 0; row < DIM; ++row )
559 {
560 for( int col = 0; col < DIM; ++col )
561 {
562 result._matrix[ col ][ row ] = _matrix[ col ][ row ] * scalar;
563 }
564 }
565 return result;
566}
567//----------------------------------------------------------------------------
568template<class Type, int DIM>
570{
571 for( int row = 0; row < DIM; ++row )
572 {
573 for( int col = 0; col < DIM; ++col )
574 {
575 std::cout.width( 11 );
576 std::cout << _matrix[ col ][ row ] << ' ';
577 }
578 std::cout << std::endl;
579 }
580 std::cout << std::endl;
581}
582//----------------------------------------------------------------------------
583template<class Type, int DIM>
585{
586 Clear();
587 for( int i = 0; i < DIM; ++i )
588 {
589 _matrix[ i ][ i ] = Type( 1 );
590 }
591}
592//----------------------------------------------------------------------------
593template<class Type, int DIM>
595{
596 // In this algorithm we use gaussian elimination with pivoting to reduce
597 // the matrix to an upper triangular form. The determinant of an
598 // upper triangular matrix is the product of the terms on the diagonal.
599 // To perform the elimination, we will use two types of elementary row
600 // operations on the matrix.
601 // 1) Interchanging rows of the matrix.
602 // 2) Add c times row i of the matrix to row j where i != j.
603 //
604 // The first operation of interchanging rows prevents numerical
605 // instability, and the second allows us to zero out terms below
606 // the diagonal by choosing c appropriately.
607
608 Type pivotFactor;
609 Type det = Type( 1 );
610
611 // Make a temporary copy of the matrix so the original is not destroyed
612 // during gaussian elimination and pivoting
613 GlsMatrix<Type, DIM> temp( *this );
614
615 // Loop through each row in the matrix
616 for( int k = 0; k < DIM; ++k )
617 {
618 // Perform pivoting
619 int index = temp.Pivot( k );
620
621 // If pivot returns a -1, then a zero exists on the diagonal
622 // so the determinant will be zero.
623 if( index == -1 )
624 return 0;
625 // If pivot returns a number other than zero, rows have been
626 // interchanged, so we need to change the sign of the determinant
627 // due to the theorem that states "If matrix B results from A by
628 // interchanging two rows (columns) of A, then det(B) = - det(A).
629 if( index != 0 )
630 det = -det;
631
632 // The determinant will be the product of the diagonal terms. The
633 // row that the [k][k] term is in already has the proper form
634 // for an upper triangular matrix, so go ahead and use it in the det.
635 det *= temp._matrix[ k ][ k ];
636
637 // Since we are using the diagonal element from row k, column k, we
638 // need all other terms below this element, in column k, to be
639 // zero. We do this by an elementary operation (2) above to each
640 // row below this diagonal element.
641 for( int i = k + 1; i < DIM; ++i )
642 {
643 // Choose the pivot factor that would zero out the [i][k]term
644 // when multiplied by the kth row and subtracted from the
645 // ith row.
646 pivotFactor = temp._matrix[ i ][ k ] / temp._matrix[ k ][ k ];
647
648 // Perform the operation on each term in the row to the right
649 // of the pivot. The terms to the left are assumed to be zero
650 // and we never reference them again so no need to waste time
651 // processing them.
652 for( int j = k + 1; j < DIM; ++j )
653 {
654 temp._matrix[ i ][ j ] -= pivotFactor * temp._matrix[ k ][ j ];
655 }
656 }
657 }
658 return det;
659}
660//----------------------------------------------------------------------------
661template<class Type, int DIM>
663{
664 // In this algorithm we use Gauss-Jordan reduction with pivoting to
665 // transform the original matrix to the identity matrix, all the while
666 // performing the same operations on what started out as the identity
667 // matrix. What started out as the identity matrix will become the
668 // inverse of the original matrix. This is because A*Inv(A) = I and
669 // performing the same operations on either side of the equation to
670 // reduce A to the identity matrix, the equation becomes I*Inv(A) = Inv(A).
671 // To perform the elimination, we will use two types of elementary row
672 // operations on the matrix.
673 // 1) Interchanging rows of the matrix.
674 // 2) Multiply row i of the matrix by c, where c != 0.
675 // 2) Add c times row i of the matrix to row j where i != j.
676 //
677 // The first operation of interchanging rows prevents numerical
678 // instability. The second allows us to put 1's on the diagonal, by
679 // dividing the row by the value of the term on the diagonal.
680 // and the third to zero out terms that are not on the diagonal by
681 // choosing c appropriately.
682
683 Type a1_inv, a2;
684
685 // Make a temporary copy of the matrix so the original is not destroyed
686 // during Gauss-Jordan reduction and pivoting
687 GlsMatrix<Type, DIM> temp( *this );
688
689 // This starts out as the identity matrix. Every operation we perform
690 // on temp, we will also do to this matrix to maintain the equality in
691 // the equation we are starting out with: A*Inv(A) = I
692 GlsMatrix<Type, DIM> inverse;
693
694 // Loop through each row in the matrix
695 for( int k = 0; k < DIM; ++k )
696 {
697 // Perform pivoting
698 int index = temp.Pivot( k );
699
700 // If pivot returns a -1, then the matrix is singular or
701 // non-invertible. TBD: WHAT SHOULD WE DO HERE?
702 if( index == -1 )
703 return inverse;
704
705 // If pivoting resulted in the swapping of rows, then
706 // we need to do the same thing to the inverse matrix
707 if( index != 0 )
708 {
709 // Swap rows
710 Type rowptr[ DIM ];
711 size_t byteCount = sizeof( Type ) * DIM;
712 memcpy( rowptr, inverse._matrix[ k ], byteCount );
713 memcpy( inverse._matrix[ k ], inverse._matrix[ index ], byteCount );
714 memcpy( inverse._matrix[ index ], rowptr, byteCount );
715 }
716
717 // Divide the current row by the term on the diagonal so we
718 // get a 1 on the diagonal. Remember, do the same operation
719 // to the inverse matrix.
720 a1_inv = ( (Type)1.0 ) / temp._matrix[ k ][ k ];
721 for( int j = 0; j < DIM; ++j )
722 {
723 temp._matrix[ k ][ j ] *= a1_inv;
724 inverse._matrix[ k ][ j ] *= a1_inv;
725 }
726
727 // Now, loop through each row, skipping the current
728 // one (k).
729 // Choose the pivot factor (a2) that would zero out the [i][k]term
730 // when multiplied by the kth row and subtracted from the
731 // ith row. Since the kth row has a 1 in the diagonal term,
732 // the factor should be the [i][k] term itself. This loop results
733 // in zeros in the kth column, except for the diagonal term [k][k].
734 for( int i = 0; i < DIM; ++i )
735 {
736 if( i != k )
737 {
738 a2 = temp._matrix[ i ][ k ];
739 for( int j = 0; j < DIM; ++j )
740 {
741 temp._matrix[ i ][ j ] -= a2 * temp._matrix[ k ][ j ];
742 inverse._matrix[ i ][ j ] -= a2 * inverse._matrix[ k ][ j ];
743 }
744 }
745 }
746 }
747 return inverse;
748}
749//----------------------------------------------------------------------------
750template<class Type, int DIM>
752{
753 *this = Inverse();
754}
755//----------------------------------------------------------------------------
756template<class Type, int DIM>
758{
759 // Interchange rows with columns
761 for( int row = 0; row < DIM; ++row )
762 {
763 for( int col = 0; col < DIM; ++col )
764 {
765 result._matrix[ col ][ row ] = _matrix[ row ][ col ];
766 }
767 }
768 return result;
769}
770//----------------------------------------------------------------------------
771template<class Type, int DIM>
773{
775 result = Transposition();
776 *this = result;
777}
778//----------------------------------------------------------------------------
779template<class Type, int DIM>
781{
782 // Look for the first term off the diagonal (where row != column) that
783 // is not equal to zero. If we find one, the matrix is not a diagonal.
784 for( int row = 0; row < DIM; ++row )
785 {
786 for( int col = 0; col < DIM; ++col )
787 {
788 if( col != row && !closeValues( _matrix[ col ][ row ], Type( 0 ) ) )
789 return false;
790 }
791 }
792 return true;
793}
794//----------------------------------------------------------------------------
795template<class Type, int DIM>
797{
798 // First do a quick check to see if it is an un-touched identity matrix
799 // This allows for zero tolerences, but should be much faster than
800 // doing all the tolerance checking every time.
801 static const GlsMatrix referenceIdentity; // Defaults to identity.
802 if( 0 == memcmp( _data, referenceIdentity._data, sizeof( Type ) * DIM * DIM ) )
803 return true;
804
805 // If the matrix is scalar, it is diagonal and all terms on the diagonal
806 // are equal. Make sure they are equal to 1 to be the identity matrix.
807 if( IsScalar() && closeValues( _matrix[ 0 ][ 0 ], Type( 1 ) ) )
808 return true;
809 else
810 return false;
811}
812//----------------------------------------------------------------------------
813template<class Type, int DIM>
815{
816 // If the matrix is scalar, it is diagonal and all terms on the diagonal
817 // are equal.
818 if( !IsDiagonal() )
819 return false;
820
821 Type val = _matrix[ 0 ][ 0 ];
822 for( int i = 0; i < DIM; ++i )
823 {
824 if( !closeValues( _matrix[ i ][ i ], val ) )
825 return false;
826 }
827 return true;
828}
829//----------------------------------------------------------------------------
830template<class Type, int DIM>
832{
833 return Determinant() == Type( 0 );
834}
835//----------------------------------------------------------------------------
836template<class Type, int DIM>
838{
839 return Transposition() == *this;
840}
841//----------------------------------------------------------------------------
842template<class Type, int DIM>
844{
845 return Transposition() == -( *this );
846}
847//----------------------------------------------------------------------------
848template<class Type, int DIM>
850{
851 // An n x n matrix A = [Aij] is lower triangular if Aij = 0 for i < j.
852 for( int row = 0; row < DIM - 1; ++row )
853 {
854 for( int col = row + 1; col < DIM; ++col )
855 {
856 if( !closeValues( _matrix[ col ][ row ], Type( 0 ) ) )
857 return false;
858 }
859 }
860 return true;
861}
862//----------------------------------------------------------------------------
863template<class Type, int DIM>
865{
866 // An n x n matrix A = [Aij] is upper triangular if Aij = 0 for i > j.
867 for( int row = 1; row < DIM; ++row )
868 {
869 for( int col = 0; col < row; ++col )
870 {
871 if( !closeValues( _matrix[ col ][ row ], Type( 0 ) ) )
872 return false;
873 }
874 }
875 return true;
876}
877//----------------------------------------------------------------------------
878template<class Type, int DIM>
879Type& GlsMatrix<Type, DIM>::operator()( unsigned row, unsigned col )
880{
881 // transpose the row and column since the matrix is in column
882 // major order but we want users to be able to use row-major ordering.
883 return _matrix[ col ][ row ];
884}
885//----------------------------------------------------------------------------
886template<class Type, int DIM>
887Type GlsMatrix<Type, DIM>::operator()( unsigned row, unsigned col ) const
888{
889 // transpose the row and column since the matrix is in column
890 // major order but we want users to be able to use row-major ordering.
891 return _matrix[ col ][ row ];
892}
893//----------------------------------------------------------------------------
894template<class Type, int DIM>
896{
897 static const int SIZE = DIM * DIM;
898 for( int i = 0; i < SIZE; ++i )
899 {
900 if( !closeValues( _data[ i ], rhs._data[ i ] ) )
901 return false;
902 }
903 return true;
904}
905//----------------------------------------------------------------------------
906template<class Type, int DIM>
908{
909 return !( *this == rhs );
910}
911//----------------------------------------------------------------------------
912template<class Type, int DIM>
915{
916 if( this == &m )
917 return *this;
918
919 memcpy( _data, m._data, sizeof( Type ) * DIM * DIM );
920
921 return *this;
922}
923//----------------------------------------------------------------------------
924template<class Type, int DIM>
926GlsMatrix<Type, DIM>::operator=( const CStyleMatrix m )
927{
928 for( int row = 0; row < DIM; ++row )
929 {
930 for( int col = 0; col < DIM; ++col )
931 {
932 _matrix[ col ][ row ] = m[ row ][ col ];
933 }
934 }
935
936 return *this;
937}
938//----------------------------------------------------------------------------
939template<class Type, int DIM>
941{
943
944 for( int row = 0; row < DIM; ++row )
945 {
946 for( int col = 0; col < DIM; ++col )
947 {
948 temp._matrix[ col ][ row ] = -_matrix[ col ][ row ];
949 }
950 }
951
952 return temp;
953}
954//----------------------------------------------------------------------------
955template<class Type, int DIM>
958{
959 for( int row = 0; row < DIM; ++row )
960 {
961 for( int col = 0; col < DIM; ++col )
962 {
963 _matrix[ col ][ row ] += rhs._matrix[ col ][ row ];
964 }
965 }
966 return *this;
967}
968//----------------------------------------------------------------------------
969template<class Type, int DIM>
972{
973 for( int row = 0; row < DIM; ++row )
974 {
975 for( int col = 0; col < DIM; ++col )
976 {
977 _matrix[ col ][ row ] -= rhs._matrix[ col ][ row ];
978 }
979 }
980 return *this;
981}
982
983/// Combined assignment *= operator
984/// \param rhs The matrix to multiply by.
985/// \return The resulting matrix (this).
986template<>
989{
990 return *this = *this * rhs; // faster because it calls the specialized operator*
991}
992
993/// Combined assignment *= operator
994/// \param rhs The matrix to multiply by.
995/// \return The resulting matrix (this).
996template<>
999{
1000 return *this = *this * rhs; // faster because it calls the specialized operator*
1001}
1002//----------------------------------------------------------------------------
1003template<class Type, int DIM>
1006{
1007 GlsMatrix<Type, DIM> result;
1008 memset( result._data, 0, sizeof( Type ) * DIM * DIM );
1009 for( int row = 0; row < DIM; ++row )
1010 {
1011 for( int col = 0; col < DIM; ++col )
1012 {
1013 for( int i = 0; i < DIM; ++i )
1014 {
1015 result._matrix[ col ][ row ] += _matrix[ i ][ row ] * rhs._matrix[ col ][ i ];
1016 }
1017 }
1018 }
1019 *this = result;
1020 return *this;
1021}
1022//----------------------------------------------------------------------------
1023template<class Type, int DIM>
1026{
1027 for( int row = 0; row < DIM; ++row )
1028 {
1029 for( int col = 0; col < DIM; ++col )
1030 {
1031 _matrix[ col ][ row ] *= scalar;
1032 }
1033 }
1034 return *this;
1035}
1036//----------------------------------------------------------------------------
1037template<class Type, int DIM>
1040{
1041 for( int row = 0; row < DIM; ++row )
1042 {
1043 for( int col = 0; col < DIM; ++col )
1044 {
1045 _matrix[ col ][ row ] /= scalar;
1046 }
1047 }
1048 return *this;
1049}
1050//----------------------------------------------------------------------------
1051template<class Type, int DIM>
1054{
1055 GlsMatrix<Type, DIM> result;
1056 for( int row = 0; row < DIM; ++row )
1057 {
1058 for( int col = 0; col < DIM; ++col )
1059 {
1060 result._matrix[ col ][ row ] = _matrix[ col ][ row ] + rhs._matrix[ col ][ row ];
1061 }
1062 }
1063 return result;
1064}
1065//----------------------------------------------------------------------------
1066template<class Type, int DIM>
1069{
1070 GlsMatrix<Type, DIM> result;
1071 for( int row = 0; row < DIM; ++row )
1072 {
1073 for( int col = 0; col < DIM; ++col )
1074 {
1075 result._matrix[ col ][ row ] = _matrix[ col ][ row ] - rhs._matrix[ col ][ row ];
1076 }
1077 }
1078 return result;
1079}
1080//----------------------------------------------------------------------------
1081template<class Type, int DIM>
1083{
1084 GlsMatrix<Type, DIM> result;
1085 for( int row = 0; row < DIM; ++row )
1086 {
1087 for( int col = 0; col < DIM; ++col )
1088 {
1089 result._matrix[ col ][ row ] = _matrix[ col ][ row ] / scalar;
1090 }
1091 }
1092 return result;
1093}
1094
1095/// Stream out operator
1096/// \param ostrm The stream to write to.
1097/// \param m The value to write to the stream.
1098/// \return The stream in its current state.
1099template<class Type, int DIM>
1100std::ostream& operator<<( std::ostream& ostrm, const GlsMatrix<Type, DIM>& m )
1101{
1102 for( int row = 0; row < DIM; ++row )
1103 {
1104 for( int col = 0; col < DIM; ++col )
1105 {
1106 ostrm.width( 11 );
1107 ostrm << m( row, col ) << ' ';
1108 }
1109 }
1110 return ostrm;
1111}
1112
1113/// Stream in operator
1114/// \param istrm The stream to read from.
1115/// \param m The returned value read from the stream.
1116/// \return The stream in its current state.
1117template<class Type, int DIM>
1118std::istream& operator>>( std::istream& istrm, GlsMatrix<Type, DIM>& m )
1119{
1120 Type x;
1121 for( int row = 0; row < DIM; ++row )
1122 {
1123 for( int col = 0; col < DIM; ++col )
1124 {
1125 istrm >> x;
1126 m( row, col ) = x;
1127 }
1128 }
1129 return istrm;
1130}
1131//----------------------------------------------------------------------------
1132template<class Type, int DIM>
1134{
1135 // Set each element in the matrix index to point to a column of the
1136 // _matrix data.
1137 for( int i = 0, j = 0; i < DIM; ++i, j += DIM )
1138 {
1139 _matrix[ i ] = &( _data[ j ] );
1140 }
1141}
1142//----------------------------------------------------------------------------
1143template<class Type, int DIM>
1145{
1146 int pivotRow;
1147 int k = int( row );
1148 double amax = -1;
1149 double temp;
1150
1151 // Go through each element in the column below the diagonal element
1152 // [row][row] to find the largest term (in magnitude). Save which row
1153 // has the largest term in the column.
1154 for( unsigned i = row; i < unsigned( DIM ); ++i )
1155 {
1156 if( ( temp = fabs( _matrix[ i ][ row ] ) ) > amax && temp != 0.0 )
1157 {
1158 amax = temp;
1159 k = i;
1160 }
1161 }
1162
1163 if( _matrix[ k ][ row ] == Type( 0 ) )
1164 {
1165 // If the largest term is zero, then return a -1 so the caller knows
1166 // we cannot pivot.
1167 pivotRow = -1;
1168 }
1169 else if( k != int( row ) )
1170 {
1171 // If k is not equal to the passed in row, it means another row
1172 // has the largest term in it, so lets interchange rows.
1173 // Return the row that was interchanged to let the caller know.
1174 Type rowptr[ DIM ];
1175 size_t byteCount = sizeof( Type ) * DIM;
1176 memcpy( rowptr, _matrix[ k ], byteCount );
1177 memcpy( _matrix[ k ], _matrix[ row ], byteCount );
1178 memcpy( _matrix[ row ], rowptr, byteCount );
1179 pivotRow = k;
1180 }
1181 else
1182 {
1183 // If k is equal to the passed in row, it means current row
1184 // has the largest term in it, so lets not interchange any rows.
1185 // return 0 to let the caller know.
1186 pivotRow = 0;
1187 }
1188
1189 return pivotRow;
1190}
1191//----------------------------------------------------------------------------
1192template<class Type, int DIM>
1193bool GlsMatrix<Type, DIM>::closeValues( Type lv, Type rv ) const
1194{
1195 return ( lv - PRECISION ) <= rv && rv <= ( lv + PRECISION );
1196}
1197
1198} // namespace disti
1199
1200#endif
Definition: gls_matrix.h:61
bool IsSkewSymmetric() const
Definition: gls_matrix.h:843
bool operator!=(const GlsMatrix< Type, DIM > &rhs) const
Definition: gls_matrix.h:907
Type CStyleMatrix[DIM][DIM]
Typedef for a 4x4 matrix stored as a 2D array.
Definition: gls_matrix.h:63
bool IsIdentity() const
Definition: gls_matrix.h:796
void Transpose()
Definition: gls_matrix.h:772
GlsMatrix< Type, DIM > operator+(const GlsMatrix< Type, DIM > &rhs) const
Definition: gls_matrix.h:1053
GlsMatrix< Type, DIM > & operator+=(const GlsMatrix< Type, DIM > &rhs)
Definition: gls_matrix.h:957
Type & operator()(unsigned row, unsigned col)
Definition: gls_matrix.h:879
GlsMatrix< Type, DIM > operator-() const
Definition: gls_matrix.h:940
GlsMatrix< Type, DIM > operator+() const
Definition: gls_matrix.h:309
GlsMatrix< Type, DIM > Inverse() const
Definition: gls_matrix.h:662
GlsMatrix()
Definition: gls_matrix.h:437
virtual ~GlsMatrix()
Definition: gls_matrix.h:471
Type Determinant() const
Definition: gls_matrix.h:594
bool IsLowerTriangular() const
Definition: gls_matrix.h:849
GlsMatrix< Type, DIM > & operator*=(const GlsMatrix< Type, DIM > &rhs)
Definition: gls_matrix.h:1005
bool operator==(const GlsMatrix< Type, DIM > &rhs) const
Definition: gls_matrix.h:895
void MakeIdentity()
Definition: gls_matrix.h:584
GlsMatrix< Type, DIM > operator/(Type scalar) const
Definition: gls_matrix.h:1082
GlsMatrix< Type, DIM > & operator*=(Type scalar)
Definition: gls_matrix.h:1025
Type * Data()
Definition: gls_matrix.h:127
bool IsDiagonal() const
Definition: gls_matrix.h:780
int Pivot(unsigned row)
Definition: gls_matrix.h:1144
GlsMatrix< Type, DIM > & operator=(const GlsMatrix< Type, DIM > &m)
Definition: gls_matrix.h:914
const Type * Data() const
Definition: gls_matrix.h:132
GlsMatrix< Type, DIM > & operator=(const CStyleMatrix m)
Definition: gls_matrix.h:926
GlsMatrix< Type, DIM > Transposition() const
Definition: gls_matrix.h:757
bool IsSingular() const
Definition: gls_matrix.h:831
void Dump() const
Definition: gls_matrix.h:569
bool IsUpperTriangular() const
Definition: gls_matrix.h:864
static const double PRECISION
Definition: gls_matrix.h:367
bool closeValues(Type lv, Type rv) const
Definition: gls_matrix.h:1193
void Initialize()
Definition: gls_matrix.h:1133
Type * _matrix[DIM]
Definition: gls_matrix.h:388
void Clear()
Definition: gls_matrix.h:122
GlsMatrix< Type, DIM > operator*(const GlsMatrix< Type, DIM > &rhs) const
Definition: gls_matrix.h:537
Type operator()(unsigned row, unsigned col) const
Definition: gls_matrix.h:887
GlsMatrix< Type, DIM > & operator/=(Type scalar)
Definition: gls_matrix.h:1039
GlsMatrix< Type, DIM > operator-(const GlsMatrix< Type, DIM > &rhs) const
Definition: gls_matrix.h:1068
GlsMatrix(const CStyleMatrix m)
Definition: gls_matrix.h:444
void Invert()
Definition: gls_matrix.h:751
GlsMatrix(const GlsMatrix< Type, DIM > &m)
Definition: gls_matrix.h:464
Type _data[DIM *DIM]
Definition: gls_matrix.h:376
GlsMatrix< Type, DIM > operator*(Type scalar) const
Definition: gls_matrix.h:555
GlsMatrix< Type, DIM > & operator-=(const GlsMatrix< Type, DIM > &rhs)
Definition: gls_matrix.h:971
GlsMatrix(const Type *m)
Definition: gls_matrix.h:457
bool IsSymmetric() const
Definition: gls_matrix.h:837
bool IsScalar() const
Definition: gls_matrix.h:814
Definition: bmpimage.h:46
bool operator!=(const AttributeName &attr1, const AttributeName &attr2)
Definition: disti_metadata.h:165
std::istream & operator>>(std::istream &istrm, GlsMatrix< Type, DIM > &m)
Definition: gls_matrix.h:1118
std::ostream & operator<<(std::ostream &ostrm, const GlsMatrix< Type, DIM > &m)
Definition: gls_matrix.h:1100
GlsPropString operator+(const GlsPropString &a, GlsPropString &b)
Definition: util.h:1788
bool operator==(const AttributeName &attr1, const AttributeName &attr2)
Definition: disti_metadata.h:154
GlsPropString & operator+=(GlsPropString &a, const GlsPropString &b)
Definition: util.h:1842