-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
1187 lines (1027 loc) · 30.3 KB
/
matrix.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// ==========================================================================
// Copyright (C) 2008 Aline Normoyle
// ==========================================================================
////////////////////////////////
// Extensions for creating rotation, scale, and translation matrices
// Extensions for multiplying vec3
////////////////////////////////
// Matrix TCL Lite v1.13
// Copyright (c) 1997-2002 Techsoft Pvt. Ltd. (See License.Txt file.)
//
// Matrix.h: Matrix C++ template class include file
// Web: http://www.techsoftpl.com/matrix/
// Email: matrix@techsoftpl.com
//
//////////////////////////////
// Note: This matrix template class defines majority of the matrix
// operations as overloaded operators or methods. It is assumed that
// users of this class is familiar with matrix algebra. We have not
// defined any specialization of this template here, so all the instances
// of matrix will be created implicitly by the compiler. The data types
// tested with this class are float, double, long double, complex<float>,
// complex<double> and complex<long double>. Note that this class is not
// optimized for performance.
//
// Since implementation of exception, namespace and template are still
// not standardized among the various (mainly old) compilers, you may
// encounter compilation error with some compilers. In that case remove
// any of the above three features by defining the following macros:
//
// _NO_NAMESPACE: Define this macro to remove namespace support.
//
// _NO_EXCEPTION: Define this macro to remove exception handling
// and use old style of error handling using function.
//
// _NO_TEMPLATE: If this macro is defined matrix class of double
// type will be generated by default. You can also
// generate a different type of matrix like float.
//
// _SGI_BROKEN_STL: For SGI C++ v.7.2.1 compiler.
//
// Since all the definitions are also included in this header file as
// inline function, some compiler may give warning "inline function
// can't be expanded". You may ignore/disable this warning using compiler
// switches. All the operators/methods defined in this class have their
// natural meaning except the followings:
//
// Operator/Method Description
// --------------- -----------
// operator () : This function operator can be used as a
// two-dimensional subscript operator to get/set
// individual matrix elements.
//
// operator ! : This operator has been used to calculate inversion
// of matrix.
//
// operator ~ : This operator has been used to return transpose of
// a matrix.
//
// operator ^ : It is used calculate power (by a scalar) of a matrix.
// When using this operator in a matrix equation, care
// must be taken by parenthesizing it because it has
// lower precedence than addition, subtraction,
// multiplication and division operators.
//
// operator >> : It is used to read matrix from input stream as per
// standard C++ stream operators.
//
// operator << : It is used to write matrix to output stream as per
// standard C++ stream operators.
//
// Note that professional version of this package, Matrix TCL Pro 2.11
// is optimized for performance and supports many more matrix operations.
// It is available from our web site at <http://www.techsoftpl.com/matrix/>.
//
#ifndef __cplusplus
#error Must use C++ for the type matrix.
#endif
#if !defined(__STD_MATRIX_H)
#define __STD_MATRIX_H
//////////////////////////////
// First deal with various shortcomings and incompatibilities of
// various (mainly old) versions of popular compilers available.
//
#if defined(__BORLANDC__)
#pragma option -w-inl -w-pch
#endif
#if ( defined(__BORLANDC__) || _MSC_VER <= 1000 ) && !defined( __GNUG__ )
# include <stdio.h>
# include <stdlib.h>
# include <math.h>
# include <iostream.h>
# include <string.h>
#else
# include <cmath>
# include <cstdio>
# include <cstdlib>
# include <string>
# include <iostream>
#endif
#include "vec.h"
#if defined(_MSC_VER) && _MSC_VER <= 1000
# define _NO_EXCEPTION // stdexception is not fully supported in MSVC++ 4.0
typedef int bool;
# if !defined(false)
# define false 0
# endif
# if !defined(true)
# define true 1
# endif
#endif
#if defined(__BORLANDC__) && !defined(__WIN32__)
# define _NO_EXCEPTION // std exception and namespace are not fully
# define _NO_NAMESPACE // supported in 16-bit compiler
#endif
#if defined(_MSC_VER) && !defined(_WIN32)
# define _NO_EXCEPTION
#endif
#if defined(_NO_EXCEPTION)
# define _NO_THROW
# define _THROW_MATRIX_ERROR
#else
# if defined(_MSC_VER)
# if _MSC_VER >= 1020
# include <stdexcept>
# else
# include <stdexcpt.h>
# endif
# elif defined(__MWERKS__)
# include <stdexcept>
# elif (__GNUC__ >= 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 8))
# include <stdexcept>
# else
# include <stdexcep>
# endif
# define _NO_THROW throw ()
# define _THROW_MATRIX_ERROR throw (matrix_error)
#endif
#ifndef __MINMAX_DEFINED
# define max(a,b) (((a) > (b)) ? (a) : (b))
# define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
#if defined(_MSC_VER)
#undef _MSC_EXTENSIONS // To include overloaded abs function definitions!
#endif
//#if ( defined(__BORLANDC__) || _MSC_VER ) && !defined( __GNUG__ )
//inline float abs (float v) { return (float)fabs( v); }
//inline double abs (double v) { return fabs( v); }
//inline long double abs (long double v) { return fabsl( v); }
//#endif
#if defined(__GNUG__) || defined(__MWERKS__) || (defined(__BORLANDC__) && (__BORLANDC__ >= 0x540))
#define FRIEND_FUN_TEMPLATE <>
#else
#define FRIEND_FUN_TEMPLATE
#endif
#if defined(_MSC_VER) && _MSC_VER <= 1020 // MSVC++ 4.0/4.2 does not
# define _NO_NAMESPACE // support "std" namespace
#endif
#if !defined(_NO_NAMESPACE)
#if defined( _SGI_BROKEN_STL ) // For SGI C++ v.7.2.1 compiler
namespace std { }
#endif
using namespace std;
#endif
#ifndef _NO_NAMESPACE
namespace math {
#endif
#if !defined(_NO_EXCEPTION)
class matrix_error : public logic_error
{
public:
matrix_error (const string& what_arg) : logic_error( what_arg) {}
};
#define REPORT_ERROR(ErrormMsg) throw matrix_error( ErrormMsg);
#else
inline void _matrix_error (const char* pErrMsg)
{
cout << pErrMsg << endl;
exit(1);
}
#define REPORT_ERROR(ErrormMsg) _matrix_error( ErrormMsg);
#endif
#if !defined(_NO_TEMPLATE)
# define MAT_TEMPLATE template <class T>
# define matrixT matrix<T>
#else
# define MAT_TEMPLATE
# define matrixT matrix
# ifdef MATRIX_TYPE
typedef MATRIX_TYPE T;
# else
typedef double T;
# endif
#endif
MAT_TEMPLATE
class matrix
{
public:
// Constructors
matrix (const matrixT& m);
matrix (size_t row = 6, size_t col = 6);
// Destructor
~matrix ();
// Assignment operators
matrixT& operator = (const matrixT& m) _NO_THROW;
// Value extraction method
size_t RowNo () const { return _m->Row; }
size_t ColNo () const { return _m->Col; }
// Subscript operator
T& operator () (size_t row, size_t col) _THROW_MATRIX_ERROR;
T operator () (size_t row, size_t col) const _THROW_MATRIX_ERROR;
// Unary operators
matrixT operator + () _NO_THROW { return *this; }
matrixT operator - () _NO_THROW;
// Combined assignment - calculation operators
matrixT& operator += (const matrixT& m) _THROW_MATRIX_ERROR;
matrixT& operator -= (const matrixT& m) _THROW_MATRIX_ERROR;
matrixT& operator *= (const matrixT& m) _THROW_MATRIX_ERROR;
matrixT& operator *= (const T& c) _NO_THROW;
matrixT& operator /= (const T& c) _NO_THROW;
matrixT& operator ^= (const size_t& pow) _THROW_MATRIX_ERROR;
// Miscellaneous -methods
void Null (const size_t& row, const size_t& col) _NO_THROW;
void Null () _NO_THROW;
void Unit (const size_t& row) _NO_THROW;
void Unit () _NO_THROW;
void SetSize (size_t row, size_t col) _NO_THROW;
void Set (size_t row, size_t col, const T* data) _NO_THROW;
// Utility methods
matrixT Solve (const matrixT& v) const _THROW_MATRIX_ERROR;
matrixT Adj () _THROW_MATRIX_ERROR;
matrixT Inv () _THROW_MATRIX_ERROR;
matrixT Transpose () _THROW_MATRIX_ERROR;
T Det () const _THROW_MATRIX_ERROR;
T Norm () _NO_THROW;
T Cofact (size_t row, size_t col) _THROW_MATRIX_ERROR;
T Cond () _NO_THROW;
// Type of matrices
bool IsSquare () _NO_THROW { return (_m->Row == _m->Col); }
bool IsSingular () _NO_THROW;
bool IsDiagonal () _NO_THROW;
bool IsScalar () _NO_THROW;
bool IsUnit () _NO_THROW;
bool IsNull () _NO_THROW;
bool IsSymmetric () _NO_THROW;
bool IsSkewSymmetric () _NO_THROW;
bool IsUpperTriangular () _NO_THROW;
bool IsLowerTriangular () _NO_THROW;
protected:
struct base_mat
{
T **Val;
size_t Row, Col, RowSiz, ColSiz;
int Refcnt;
base_mat (size_t row, size_t col, T** v)
{
Row = row; RowSiz = row;
Col = col; ColSiz = col;
Refcnt = 1;
Val = new T* [row];
size_t rowlen = col * sizeof(T);
for (size_t i=0; i < row; i++)
{
Val[i] = new T [col];
if (v) memcpy( Val[i], v[i], rowlen);
}
}
~base_mat ()
{
for (size_t i=0; i < RowSiz; i++)
delete [] Val[i];
delete [] Val;
}
};
base_mat *_m;
void clone ();
void realloc (size_t row, size_t col);
int pivot (size_t row);
};
MAT_TEMPLATE class TranslationMatrix : public matrixT
{
public:
TranslationMatrix(T x, T y, T z);
};
MAT_TEMPLATE class RotationMatrix : public matrixT
{
public:
RotationMatrix(int axis, double angle); // angle in radians
RotationMatrix (const vec3& c1, const vec3& c2, const vec3& c3);
};
MAT_TEMPLATE class ScaleMatrix : public matrixT
{
public:
ScaleMatrix(T x, T y, T z); // angle in radians
};
#if defined(_MSC_VER) && _MSC_VER <= 1020
# undef _NO_THROW // MSVC++ 4.0/4.2 does not support
# undef _THROW_MATRIX_ERROR // exception specification in definition
# define _NO_THROW
# define _THROW_MATRIX_ERROR
#endif
// constructor
MAT_TEMPLATE inline
matrixT::matrix (size_t row, size_t col)
{
_m = new base_mat( row, col, 0);
}
// copy constructor
MAT_TEMPLATE inline
matrixT::matrix (const matrixT& m)
{
_m = m._m;
_m->Refcnt++;
}
// Internal copy constructor
MAT_TEMPLATE inline void
matrixT::clone ()
{
_m->Refcnt--;
_m = new base_mat( _m->Row, _m->Col, _m->Val);
}
// destructor
MAT_TEMPLATE inline
matrixT::~matrix ()
{
if (--_m->Refcnt == 0) delete _m;
}
// assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator = (const matrixT& m) _NO_THROW
{
m._m->Refcnt++;
if (--_m->Refcnt == 0) delete _m;
_m = m._m;
return *this;
}
// reallocation method
MAT_TEMPLATE inline void
matrixT::realloc (size_t row, size_t col)
{
if (row == _m->RowSiz && col == _m->ColSiz)
{
_m->Row = _m->RowSiz;
_m->Col = _m->ColSiz;
return;
}
base_mat *m1 = new base_mat( row, col, NULL);
size_t colSize = min(_m->Col,col) * sizeof(T);
size_t minRow = min(_m->Row,row);
for (size_t i=0; i < minRow; i++)
memcpy( m1->Val[i], _m->Val[i], colSize);
if (--_m->Refcnt == 0)
delete _m;
_m = m1;
return;
}
// public method for resizing matrix
MAT_TEMPLATE inline void
matrixT::SetSize (size_t row, size_t col) _NO_THROW
{
size_t i,j;
size_t oldRow = _m->Row;
size_t oldCol = _m->Col;
if (row != _m->RowSiz || col != _m->ColSiz)
realloc( row, col);
for (i=oldRow; i < row; i++)
for (j=0; j < col; j++)
_m->Val[i][j] = T(0);
for (i=0; i < row; i++)
for (j=oldCol; j < col; j++)
_m->Val[i][j] = T(0);
return;
}
MAT_TEMPLATE inline void matrixT::Set(size_t row, size_t col, const T* data) _NO_THROW
{
SetSize(row, col);
for (size_t i = 0; i < row; i++)
{
for (size_t j = 0; j < col; j++)
{
_m->Val[i][j] = data[j*col+i];
}
}
}
// subscript operator to get/set individual elements
MAT_TEMPLATE inline T&
matrixT::operator () (size_t row, size_t col) _THROW_MATRIX_ERROR
{
if (row >= _m->Row || col >= _m->Col)
REPORT_ERROR( "matrixT::operator(): Index out of range!");
if (_m->Refcnt > 1) clone();
return _m->Val[row][col];
}
// subscript operator to get/set individual elements
MAT_TEMPLATE inline T
matrixT::operator () (size_t row, size_t col) const _THROW_MATRIX_ERROR
{
if (row >= _m->Row || col >= _m->Col)
REPORT_ERROR( "matrixT::operator(): Index out of range!");
return _m->Val[row][col];
}
// input stream function
MAT_TEMPLATE inline istream&
operator >> (istream& istrm, matrixT& m)
{
for (size_t i=0; i < m.RowNo(); i++)
for (size_t j=0; j < m.ColNo(); j++)
{
T x;
istrm >> x;
m(i,j) = x;
}
return istrm;
}
// output stream function
MAT_TEMPLATE inline ostream&
operator << (ostream& ostrm, const matrixT& m)
{
for (size_t i=0; i < m.RowNo(); i++)
{
for (size_t j=0; j < m.ColNo(); j++)
{
T x = m(i,j);
ostrm << x << '\t';
}
ostrm << endl;
}
return ostrm;
}
// logical equal-to operator
MAT_TEMPLATE inline bool
operator == (const matrixT& m1, const matrixT& m2) _NO_THROW
{
if (m1.RowNo() != m2.RowNo() || m1.ColNo() != m2.ColNo())
return false;
for (size_t i=0; i < m1.RowNo(); i++)
for (size_t j=0; j < m1.ColNo(); j++)
if (m1(i,j) != m2(i,j))
return false;
return true;
}
// logical no-equal-to operator
MAT_TEMPLATE inline bool
operator != (const matrixT& m1, const matrixT& m2) _NO_THROW
{
return (m1 == m2) ? false : true;
}
// combined addition and assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator += (const matrixT& m) _THROW_MATRIX_ERROR
{
if (_m->Row != m._m->Row || _m->Col != m._m->Col)
REPORT_ERROR( "matrixT::operator+= : Inconsistent matrix sizes in addition!");
if (_m->Refcnt > 1) clone();
for (size_t i=0; i < m._m->Row; i++)
for (size_t j=0; j < m._m->Col; j++)
_m->Val[i][j] += m._m->Val[i][j];
return *this;
}
// combined subtraction and assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator -= (const matrixT& m) _THROW_MATRIX_ERROR
{
if (_m->Row != m._m->Row || _m->Col != m._m->Col)
REPORT_ERROR( "matrixT::operator-= : Inconsistent matrix sizes in subtraction!");
if (_m->Refcnt > 1) clone();
for (size_t i=0; i < m._m->Row; i++)
for (size_t j=0; j < m._m->Col; j++)
_m->Val[i][j] -= m._m->Val[i][j];
return *this;
}
// combined scalar multiplication and assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator *= (const T& c) _NO_THROW
{
if (_m->Refcnt > 1) clone();
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
_m->Val[i][j] *= c;
return *this;
}
// combined matrix multiplication and assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator *= (const matrixT& m) _THROW_MATRIX_ERROR
{
if (_m->Col != m._m->Row)
REPORT_ERROR( "matrixT::operator*= : Inconsistent matrix sizes in multiplication!");
matrixT temp(_m->Row,m._m->Col);
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < m._m->Col; j++)
{
temp._m->Val[i][j] = T(0);
for (size_t k=0; k < _m->Col; k++)
temp._m->Val[i][j] += _m->Val[i][k] * m._m->Val[k][j];
}
*this = temp;
return *this;
}
// combined scalar division and assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator /= (const T& c) _NO_THROW
{
if (_m->Refcnt > 1) clone();
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
_m->Val[i][j] /= c;
return *this;
}
// combined power and assignment operator
MAT_TEMPLATE inline matrixT&
matrixT::operator ^= (const size_t& pow) _THROW_MATRIX_ERROR
{
matrixT temp(*this);
for (size_t i=2; i <= pow; i++)
*this = *this * temp;
return *this;
}
// unary negation operator
MAT_TEMPLATE inline matrixT
matrixT::operator - () _NO_THROW
{
matrixT temp(_m->Row,_m->Col);
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
temp._m->Val[i][j] = - _m->Val[i][j];
return temp;
}
// binary addition operator
MAT_TEMPLATE inline matrixT
operator + (const matrixT& m1, const matrixT& m2) _THROW_MATRIX_ERROR
{
matrixT temp = m1;
temp += m2;
return temp;
}
// binary subtraction operator
MAT_TEMPLATE inline matrixT
operator - (const matrixT& m1, const matrixT& m2) _THROW_MATRIX_ERROR
{
matrixT temp = m1;
temp -= m2;
return temp;
}
// binary scalar multiplication operator
MAT_TEMPLATE inline matrixT
operator * (const matrixT& m, const T& no) _NO_THROW
{
matrixT temp = m;
temp *= no;
return temp;
}
// binary scalar multiplication operator
MAT_TEMPLATE inline matrixT
operator * (const T& no, const matrixT& m) _NO_THROW
{
return (m * no);
}
// binary matrix multiplication operator
MAT_TEMPLATE inline matrixT
operator * (const matrixT& m1, const matrixT& m2) _THROW_MATRIX_ERROR
{
matrixT temp = m1;
temp *= m2;
return temp;
}
// binary scalar division operator
MAT_TEMPLATE inline matrixT
operator / (const matrixT& m, const T& no) _NO_THROW
{
return (m * (T(1) / no));
}
// binary scalar division operator
MAT_TEMPLATE inline matrixT
operator / (const T& no, const matrixT& m) _THROW_MATRIX_ERROR
{
return (!m * no);
}
// binary matrix division operator
MAT_TEMPLATE inline matrixT
operator / (const matrixT& m1, const matrixT& m2) _THROW_MATRIX_ERROR
{
return (m1 * !m2);
}
// binary power operator
MAT_TEMPLATE inline matrixT
operator ^ (const matrixT& m, const size_t& pow) _THROW_MATRIX_ERROR
{
matrixT temp = m;
temp ^= pow;
return temp;
}
// unary transpose operator
MAT_TEMPLATE inline matrixT
operator ~ (const matrixT& m) _NO_THROW
{
matrixT temp(m.ColNo(),m.RowNo());
for (size_t i=0; i < m.RowNo(); i++)
for (size_t j=0; j < m.ColNo(); j++)
{
T x = m(i,j);
temp(j,i) = x;
}
return temp;
}
// unary inversion operator
MAT_TEMPLATE inline matrixT
operator ! (const matrixT m) _THROW_MATRIX_ERROR
{
matrixT temp = m;
return temp.Inv();
}
// inversion function
MAT_TEMPLATE inline matrixT
matrixT::Inv () _THROW_MATRIX_ERROR
{
size_t i,j,k;
T a1,a2,*rowptr;
if (_m->Row != _m->Col)
REPORT_ERROR( "matrixT::operator!: Inversion of a non-square matrix");
matrixT temp(_m->Row,_m->Col);
if (_m->Refcnt > 1) clone();
temp.Unit();
for (k=0; k < _m->Row; k++)
{
int indx = pivot(k);
if (indx == -1)
REPORT_ERROR( "matrixT::operator!: Inversion of a singular matrix");
if (indx != 0)
{
rowptr = temp._m->Val[k];
temp._m->Val[k] = temp._m->Val[indx];
temp._m->Val[indx] = rowptr;
}
a1 = _m->Val[k][k];
for (j=0; j < _m->Row; j++)
{
_m->Val[k][j] /= a1;
temp._m->Val[k][j] /= a1;
}
for (i=0; i < _m->Row; i++)
if (i != k)
{
a2 = _m->Val[i][k];
for (j=0; j < _m->Row; j++)
{
_m->Val[i][j] -= a2 * _m->Val[k][j];
temp._m->Val[i][j] -= a2 * temp._m->Val[k][j];
}
}
}
return temp;
}
// solve simultaneous equation
MAT_TEMPLATE inline matrixT
matrixT::Solve (const matrixT& v) const _THROW_MATRIX_ERROR
{
size_t i,j,k;
T a1;
if (!(_m->Row == _m->Col && _m->Col == v._m->Row))
REPORT_ERROR( "matrixT::Solve():Inconsistent matrices!");
matrixT temp(_m->Row,_m->Col+v._m->Col);
for (i=0; i < _m->Row; i++)
{
for (j=0; j < _m->Col; j++)
temp._m->Val[i][j] = _m->Val[i][j];
for (k=0; k < v._m->Col; k++)
temp._m->Val[i][_m->Col+k] = v._m->Val[i][k];
}
for (k=0; k < _m->Row; k++)
{
int indx = temp.pivot(k);
if (indx == -1)
REPORT_ERROR( "matrixT::Solve(): Singular matrix!");
a1 = temp._m->Val[k][k];
for (j=k; j < temp._m->Col; j++)
temp._m->Val[k][j] /= a1;
for (i=k+1; i < _m->Row; i++)
{
a1 = temp._m->Val[i][k];
for (j=k; j < temp._m->Col; j++)
temp._m->Val[i][j] -= a1 * temp._m->Val[k][j];
}
}
matrixT s(v._m->Row,v._m->Col);
for (k=0; k < v._m->Col; k++)
for (int m=int(_m->Row)-1; m >= 0; m--)
{
s._m->Val[m][k] = temp._m->Val[m][_m->Col+k];
for (j=m+1; j < _m->Col; j++)
s._m->Val[m][k] -= temp._m->Val[m][j] * s._m->Val[j][k];
}
return s;
}
// set zero to all elements of this matrix
MAT_TEMPLATE inline void
matrixT::Null (const size_t& row, const size_t& col) _NO_THROW
{
if (row != _m->Row || col != _m->Col)
realloc( row,col);
if (_m->Refcnt > 1)
clone();
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
_m->Val[i][j] = T(0);
return;
}
// set zero to all elements of this matrix
MAT_TEMPLATE inline void
matrixT::Null() _NO_THROW
{
if (_m->Refcnt > 1) clone();
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
_m->Val[i][j] = T(0);
return;
}
// set this matrix to unity
MAT_TEMPLATE inline void
matrixT::Unit (const size_t& row) _NO_THROW
{
if (row != _m->Row || row != _m->Col)
realloc( row, row);
if (_m->Refcnt > 1)
clone();
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
_m->Val[i][j] = i == j ? T(1) : T(0);
return;
}
// set this matrix to unity
MAT_TEMPLATE inline void
matrixT::Unit () _NO_THROW
{
if (_m->Refcnt > 1) clone();
size_t row = min(_m->Row,_m->Col);
_m->Row = _m->Col = row;
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
_m->Val[i][j] = i == j ? T(1) : T(0);
return;
}
// private partial pivoting method
MAT_TEMPLATE inline int
matrixT::pivot (size_t row)
{
int k = int(row);
double amax,temp;
amax = -1;
for (size_t i=row; i < _m->Row; i++)
if ( (temp = abs( _m->Val[i][row])) > amax && temp != 0.0)
{
amax = temp;
k = i;
}
if (_m->Val[k][row] == T(0))
return -1;
if (k != int(row))
{
T* rowptr = _m->Val[k];
_m->Val[k] = _m->Val[row];
_m->Val[row] = rowptr;
return k;
}
return 0;
}
// calculate the determinant of a matrix
MAT_TEMPLATE inline T
matrixT::Det () const _THROW_MATRIX_ERROR
{
size_t i,j,k;
T piv,detVal = T(1);
if (_m->Row != _m->Col)
REPORT_ERROR( "matrixT::Det(): Determinant a non-square matrix!");
matrixT temp(*this);
if (temp._m->Refcnt > 1) temp.clone();
for (k=0; k < _m->Row; k++)
{
int indx = temp.pivot(k);
if (indx == -1)
return 0;
if (indx != 0)
detVal = - detVal;
detVal = detVal * temp._m->Val[k][k];
for (i=k+1; i < _m->Row; i++)
{
piv = temp._m->Val[i][k] / temp._m->Val[k][k];
for (j=k+1; j < _m->Row; j++)
temp._m->Val[i][j] -= piv * temp._m->Val[k][j];
}
}
return detVal;
}
// calculate the norm of a matrix
MAT_TEMPLATE inline T
matrixT::Norm () _NO_THROW
{
T retVal = T(0);
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
retVal += _m->Val[i][j] * _m->Val[i][j];
retVal = sqrt( retVal);
return retVal;
}
// calculate the condition number of a matrix
MAT_TEMPLATE inline T
matrixT::Cond () _NO_THROW
{
matrixT inv = ! (*this);
return (Norm() * inv.Norm());
}
// calculate the cofactor of a matrix for a given element
MAT_TEMPLATE inline T
matrixT::Cofact (size_t row, size_t col) _THROW_MATRIX_ERROR
{
size_t i,i1,j,j1;
if (_m->Row != _m->Col)
REPORT_ERROR( "matrixT::Cofact(): Cofactor of a non-square matrix!");
if (row > _m->Row || col > _m->Col)
REPORT_ERROR( "matrixT::Cofact(): Index out of range!");
matrixT temp (_m->Row-1,_m->Col-1);
for (i=i1=0; i < _m->Row; i++)
{
if (i == row)
continue;
for (j=j1=0; j < _m->Col; j++)
{
if (j == col)
continue;
temp._m->Val[i1][j1] = _m->Val[i][j];
j1++;
}
i1++;
}
T cof = temp.Det();
if ((row+col)%2 == 1)
cof = -cof;
return cof;
}
// calculate adjoin of a matrix
MAT_TEMPLATE inline matrixT
matrixT::Transpose () _THROW_MATRIX_ERROR
{
/*
matrixT temp(_m->Col,_m->Row);
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
temp._m->Val[j][i] = _m->Val[i][j];
*/
return ~(*this);
}
// calculate adjoin of a matrix
MAT_TEMPLATE inline matrixT
matrixT::Adj () _THROW_MATRIX_ERROR
{
if (_m->Row != _m->Col)
REPORT_ERROR( "matrixT::Adj(): Adjoin of a non-square matrix.");
matrixT temp(_m->Row,_m->Col);
for (size_t i=0; i < _m->Row; i++)
for (size_t j=0; j < _m->Col; j++)
temp._m->Val[j][i] = Cofact(i,j);
return temp;