This repository has been archived by the owner on Nov 23, 2019. It is now read-only.
forked from yayahjb/ncdist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mat66.cpp
executable file
·233 lines (202 loc) · 5.65 KB
/
Mat66.cpp
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
#include <cstdlib>
#include <cstdio>
#include <iostream>
#include <cmath>
#include <sstream>
#include "Mat66.h"
#include "Mat7.h"
#include "MatMN.h"
#include "G6.h"
#include "VectorTools.h"
Mat66::Mat66( )
: m_mat( Default36 ) {
for( size_t i=0; i<Default36; ++i )
m_mat[i] = 19191.0;
}
Mat66::Mat66( const Mat66& m )
: m_mat(Default36)
{
for( size_t i=0; i<Default36; ++i ) m_mat[i] = m.m_mat[i];
}
Mat66::Mat66( const Mat7& m7 )
{
MatMN m77( 7,7 );
for ( size_t i=0; i<49; ++i ) m77[i] = m7[i];
const MatMN mn = VectorTools::g_m7To6 * m77 * VectorTools::g_m6To7;
m_mat.resize(36);
for( size_t i=0; i<36; ++i )
m_mat[i] = mn[i];
}
/*
Mat7::Mat7( const Mat66& m6 )
: MatN( Default49 ) {
MatMN m66( 6,6 );
for( size_t i=0; i<36; ++i ) m66[i] = m6[i];
MatMN m67 = m66 * g_m7To6;
MatMN mupTo7Rows(7,6);
*this = g_m6To7 * m66 * g_m7To6;
*/
Mat66::Mat66( const MatN& m ) {
if ( m.GetDim()!=36 || m.GetRowDim() != 6 ) throw;
m_mat = m.GetMatrix();
}
Mat66::Mat66( const double m[36] )
: m_mat()
{
for( size_t i=0; i<Default36; ++i ) {
m_mat.push_back( m[i] );
}
}
Mat66::Mat66( const std::string& s )
: m_mat()
{
std::istringstream istr(s);
double d;
for( size_t i=0; i<Default36; ++i ) {
istr >> d;
m_mat.push_back( d );
}
}
Mat66 Mat66::operator* ( const Mat66& m2 ) const {
Mat66 m;
m.zeros();
const Mat66& m1(*this);
for ( int count=0; count<Default36; ++count ){
for (int i=0; i<Default6; ++i ) {
m[count] += m1[Default6*(count/Default6)+i] *m2[(count%Default6)+Default6*i];
}
}
return m;
}
Mat66 Mat66::operator+ ( const Mat66& m2 ) const {
Mat66 m;
for( size_t i=0; i<Default36; ++i )
m[i] = (*this)[i]+m2[i];
return m;
}
Mat66 Mat66::operator- ( const Mat66& m2 ) const {
Mat66 m(*this);
for( size_t i=0; i<Default36; ++i )
m[i] -= m2[i];
return m;
}
Mat66 Mat66::transpose(const Mat66& m2) const {
// transpose a symmetrical matrix
const int size = Default6;
Mat66 m;
for(int count=0; count<size*size; ++count) {
const int transposeIndex = count/size + size*(count%size); //'=int(rowindex/6) +6*colIndex)
if(count >= transposeIndex) {
m[transposeIndex] = m2[count];
m[count] = m2[transposeIndex];
}
}
return m;
}
bool Mat66::IsUnit( void ) const {
const Mat66& m(*this);
for( size_t i=0; i<36; i+=7)
if ( m[i] !=1.0 || m[7]!=1.0) return false;
double sum = 0.0;
for( size_t i=0; i<36; ++i ) sum += std::fabs(m[i]);
return sum == 6.0;
}
inline size_t LinearIndex(const size_t row,const size_t col,const size_t sizeOfRow) {
return row*sizeOfRow + col;
}
Mat66 Mat66::inverse ( const Mat66& m2 ) const {
Mat66 m( (*this) );
Mat66 I = m.Eye();
const size_t rowsize = Default6;
for( size_t k=0;k<rowsize;k++) {
const double temp=m[LinearIndex(k,k,rowsize)];
for( size_t j=0;j<rowsize;j++) {
const size_t index = LinearIndex(k,j,rowsize);
m[index]/=temp;
I[index]/=temp;
}
for( size_t i=0;i<rowsize;i++) {
if(i==k) continue;
const double temp2=m[LinearIndex(i,k,rowsize)];
for( size_t j=0;j<rowsize;j++) {
const size_t indexij = LinearIndex(i,j,rowsize);
const size_t indexkj = LinearIndex(k,j,rowsize);
m[indexij]-=m[indexkj]*temp2;
I[indexij]-=m[indexkj]*temp2;
}
}
}
return I;
}
G6 Mat66::operator* ( const G6& v ) const {
G6 vout;
for( size_t i=0; i<Default6; ++i ) vout[i] =0.0;
int count = 0;
for( size_t im=0; im<Default36; im+=Default6) {
double d = 0.0;
for( size_t j=0; j<Default6; ++j) {
d += (*this)[im+j]* v[j];
}
// std::cout << "d " << d << std::endl << std::endl;
vout[count] = d ;
++count;
}
return vout;
}
double Mat66::norm( void ) const {
double sum = 0.0;
for( size_t i=0; i<Default36; ++i )
sum += std::pow(m_mat[i], 2);
return std::sqrt(sum);
}
Mat66 Mat66::operator*(const double d) const {
Mat66 m2;
for( size_t i=0; i<Default36; ++i )
m2.m_mat[i] = d*m_mat[i];
return m2;
}
Mat66 Mat66::operator/(const double d) const {
Mat66 m2;
for( size_t i=0; i<Default36; ++i )
m2.m_mat[i] = m_mat[i]/d;
return m2;
}
Mat66 operator*(const double d, const Mat66& m) {
return m*d;
}
Mat66 operator/(const double d, const Mat66& m) {
return m/d;
}
Mat66 Mat66::Eye( ) {
Mat66 m;
for( size_t i=0; i<Default36; ++i )
m.m_mat[i] = 0.0;
for( size_t i=0; i<Default36; i+=7 )
m.m_mat[i] = 1.0;
return m;
}
double Mat66::operator[]( const size_t i ) const{
return m_mat[i];
}
double& Mat66::operator[]( const size_t i ){
return m_mat[i];
}
Mat66& Mat66::operator= (const Mat66& m2 ) {
for( size_t i=0; i<Default36; ++i ) m_mat[i] = m2.m_mat[i];
return *this;
}
void Mat66::zeros( ) {
for( size_t i=0; i<Default36; ++i ) m_mat[i] = 0.0;
}
bool Mat66::operator== (const Mat66& m2 ) const {
bool b = true;
for( size_t i=0; i<Default36; ++i) b = b && m2[i]==(*this)[i];
return b;
}
std::ostream& operator<< ( std::ostream& o, const Mat66& v ) {
for( size_t i=0; i<36; ++i ) {
if ( i > 0 && i%6==0 ) o << std::endl;
o << v[i] << " ";
}
return o;
}