| Bsoft 2.0.5 Bernard's software package

Matrix.h
Go to the documentation of this file.
1
9 #include "random_numbers.h"
10 #include <iostream>
11 #include <fstream>
12
13 using namespace std;
14
15 // Declaration of global variables
16 extern int verbose; // Level of output to the screen
17
18 #ifndef _Matrix_
19 #define _Matrix_
20
21 class Matrix
22 {
23 private:
24 long m, n, len;
25 double* d;
26 double** p;
27 void init() {
28 len = m*n;
29 d = new double[len];
30 p = new double*[m];
31 for ( long i=0; i<m; i++ ) p[i] = d + i*n;
32 for ( long i=0; i<len; i++ ) d[i] = 0;
33 // cout << d << "\t" << &d[8] << "\t" << p[2] << endl;
34 }
35 void clear() {
36 delete[] d; delete[] p;
37 d = NULL; p = NULL;
38 }
39 public:
40 Matrix() : m(0), n(0) { d = NULL; p = NULL; }
41 Matrix(const Matrix& mat) : m(mat.m), n(mat.n) {
42 init();
43 for ( long i=0; i<len; i++ ) d[i] = mat.d[i];
44 }
45 Matrix(long rows, long cols) : m(rows), n(cols) {
46 init();
47 }
48 Matrix(Bstring& filename) {
49 m = n = 0;
50 ifstream fmat(filename.c_str());
51 if ( fmat.fail() ) return;
52 char buf[10000];
53 long i(0), j;
54 Bstring sline, *tokens, *token;
55 while ( fmat.getline(buf, 1024) && strncmp(buf, "matrix_", 7) ) ;
56 if ( verbose & VERB_DEBUG )
57 cout << buf << endl;
58 while ( fmat.getline(buf, 10000) ) {
59 sline = buf;
60 tokens = sline.split();
61 for ( j=0, token = tokens; token; token = token->next ) ++j;
62 // cout << j << tab << sline << endl;
63 if ( tokens ) {
64 if ( *tokens == "_rows" ) {
65 m = tokens->next->integer();
66 if ( m && n ) { init(); }
67 // cout << "Rows: " << m << endl;
68 } else if ( *tokens == "_columns" ) {
69 n = tokens->next->integer();
70 if ( m && n ) { init(); }
71 // cout << "Columns: " << n << endl;
72 } else {
73 for ( j=0, token = tokens; token; token = token->next ) ++j;
74 if ( verbose & VERB_DEBUG )
75 cout << "Row " << i+1 << ": " << j << endl;
76 if ( i < m && j >= n ) {
77 for ( j=0, token = tokens; j<n; j++, token = token->next )
78 (*this)[i][j] = token->real();
79 i++;
80 }
81 }
82 string_kill(tokens);
83 }
84 }
85 fmat.close();
86 if ( m < 1 || n < 1 ) {
87 cerr << "Error: The matrix size is zero!" << endl;
88 bexit(-1);
89 }
90 if ( verbose & VERB_DEBUG )
91 cout << filename << " read" << endl;
92 }
93 ~Matrix() { clear(); }
94
95 void write(Bstring& filename) {
96 ofstream fmat(filename.c_str());
97 if ( fmat.fail() ) return;
98 long i, j;
99 fmat << "# Matrix written by Bsoft\n\ndata_\n\nmatrix_" << endl;
100 fmat << "_rows" << tab << m << endl;
101 fmat << "_columns" << tab << n << endl;
102 for ( i=0; i<m; i++ ) {
103 fmat << (*this)[i][0];
104 for ( j=1; j<n; j++ ) fmat << " " << (*this)[i][j];
105 fmat << endl;
106 }
107 fmat << endl;
108 fmat.close();
109 }
110
111 double* data() { return d; }
112 // double* row(long i) { while ( i>=m ) i -= m; return p; }
113
114 class Row {
115 private:
116 long n;
117 double* p;
118 public:
119 Row(long len, double* row) : n(len), p(row) { }
120 double& operator[](long i) {
121 while ( i>=n ) i -= n;
122 return p[i];
123 }
124 };
125
126 Row operator[](long i) {
127 while ( i>=m ) i -= m;
128 return Row(n, p[i]);
129 }
130
131 Matrix operator=(const Matrix mat) {
132 clear();
133 m = mat.m; n = mat.n;
134 init();
135 for ( long i=0; i<len; i++ ) d[i] = mat.d[i];
136 return *this;
137 }
138 Matrix operator-() {
139 Matrix mat(*this);
140 for ( long i=0; i<len; i++ ) mat.d[i] = -mat.d[i];
141 return mat;
142 }
143 Matrix operator*(Matrix& mat) {
144 long i, j, k;
145 Matrix numat(mat.m, n);
146 if ( m != mat.n ) return numat;
147 for ( i=0; i<n; i++ )
148 for ( j=0; j<mat.m; j++ )
149 for ( k=0; k<m; k++ ) numat[i][j] += (*this)[i][k]*mat[k][j];
150 return numat;
151 }
152
153 long rows() { return m; }
154 long columns() { return n; }
155 long size() { return len; }
156
157 void show_below_cutoff(double d) {
158 for ( long i=0; i<m; ++i ) {
159 cout << i;
160 for ( long j=0; j<n; ++j )
161 if ( (*this)[i][j] <= d )
162 cout << tab << j << "," << (*this)[i][j];
163 cout << endl;
164 }
165 }
166
167 void swap_rows_columns(long rc1, long rc2) {
168 long i;
169 for ( i=0; i<m; i++ ) swap((*this)[i][rc1], (*this)[i][rc2]);
170 for ( i=0; i<n; i++ ) swap((*this)[rc1][i], (*this)[rc2][i]);
171 }
172
173 Matrix delete_row_column(long rc) {
174 long i, j, k, l;
175 Matrix mat(m-1,n-1);
176 for ( i=k=0; i<m; i++ ) {
177 for ( j=l=0; j<n; j++ ) {
178 if ( i != rc && j != rc ) mat[k][l] = p[i][j];
179 if ( j != rc ) l++;
180 }
181 if ( i != rc ) k++;
182 }
183 return mat;
184 }
185
186 void fill(double v) { for ( long i=0; i<len; i++ ) d[i] = v; }
187
188 int check_for_singularity() {
189 int c(0);
190 long i, j, k;
191 double max;
192 for ( i=k=0; i<m; i++ ) {
193 for ( j=0, max=0; j<n; j++, k++ ) if ( max < fabs(d[k]) ) max = fabs(d[k]);
194 if ( max < 1e-37 ) c |= 1 << i;
195 }
196 return c;
197 }
202 void normalize()
203 {
204 long i, j, r, c;
205 double err(1);
206 vector<double> rw(m);
207 vector<double> cw(n);
208
209 if ( verbose & VERB_FULL )
210 cout << "Cycle\tError" << endl;
211 for ( i=0; i<100 && err > 1e-20; i++ ) {
212 for ( r=0; r<m; r++ ) rw[r] = 0; // Row scaling
213 for ( j=r=0; r<m; r++ )
214 for ( c=0; c<n; c++, j++ )
215 rw[r] += d[j];
216 for ( r=0; r<m; r++ ) rw[r] = 1/rw[r];
217 for ( j=r=0; r<m; r++ )
218 for ( c=0; c<n; c++, j++ )
219 d[j] *= rw[r];
220
221 for ( c=0; c<n; c++ ) cw[c] = 0; // Column scaling
222 for ( j=r=0; r<m; r++ )
223 for ( c=0; c<n; c++, j++ )
224 cw[c] += d[j];
225 for ( c=0; c<n; c++ ) cw[c] = 1/cw[c];
226 for ( j=r=0; r<m; r++ )
227 for ( c=0; c<n; c++, j++ )
228 d[j] *= cw[c];
229
230 for ( err=0, r=0; r<m; r++ ) err += rw[r];
231 for ( c=0; c<n; c++ ) err += cw[c];
232 err = fabs(err/(m + n) - 1);
233 if ( verbose & VERB_FULL )
234 cout << i+1 << "\t" << err << endl;
235 }
236 }
237 void randomize() {
238 random_seed();
239 long i, j;
240 double rm = INT_MAX/4, irm = 10.0L/INT_MAX;
241 for ( i=0; i<m; i++ )
242 for ( j=0; j<n; j++ )
243 p[i][j] = irm*(random() - rm);
244 }
245 // int multiply_in_place(double* vec);
246 int multiply_in_place(vector<double>& vec);
247 double determinant() { return LU_decomposition(); }
248 double LU_decomposition();
249 /* double LU_decomposition(double* b) {
250 double det = LU_decomposition();
251 if ( b ) multiply_in_place(b);
252 return det;
253 }*/
254 double LU_decomposition(vector<double>& b) {
255 double det = LU_decomposition();
256 multiply_in_place(b);
257 return det;
258 }
259 double singular_value_decomposition();
260 /* double singular_value_decomposition(double* b) {
261 singular_value_decomposition();
262 if ( b ) multiply_in_place(b);
263 return 0;
264 }*/
265 double singular_value_decomposition(vector<double>& b) {
266 singular_value_decomposition();
267 multiply_in_place(b);
268 return 0;
269 }
270 int jrotate(double s, double tau, long i, long j, long k, long l);
271 // double* jacobi_rotation_old();
272 vector<double> jacobi_rotation();
273 // long jacobi_rotation_row(long ip, long n, long i, double* val, double* z, Matrix* vec);
274 // double* jacobi_rotation_parallel();
275 // void eigen_sort(double* val);
276 void eigen_sort(vector<double>& val);
277 };
278
279 ostream& operator<<(ostream& output, Matrix& mat);
280
281 #endif
282
283 // Function prototypes
284 Vector3<double> principal_axes(Vector3<double> avg, Vector3<double> avg2, Vector3<double> avgx, Vector3<double>* eigenvec);
285 vector<double> dsyevq3(Matrix& A);
286 void dsytrd3(Matrix& A, double d[3], double e[2]);
Matrix::Matrix
Matrix()
Definition: Matrix.h:40
Matrix::show_below_cutoff
void show_below_cutoff(double d)
Definition: Matrix.h:157
Matrix::operator[]
Row operator[](long i)
Definition: Matrix.h:126
random_seed
long random_seed()
Gets a random seed and sets the flag.
Definition: random_numbers.cpp:41
Matrix::rows
long rows()
Definition: Matrix.h:153
Matrix::delete_row_column
Matrix delete_row_column(long rc)
Definition: Matrix.h:173
operator<<
ostream & operator<<(ostream &output, Matrix &mat)
Definition: matrix.cpp:18
Matrix::fill
void fill(double v)
Definition: Matrix.h:186
Matrix::LU_decomposition
double LU_decomposition(vector< double > &b)
Definition: Matrix.h:254
VERB_DEBUG
#define VERB_DEBUG
Definition: utilities.h:112
Matrix::check_for_singularity
int check_for_singularity()
Definition: Matrix.h:188
Matrix::singular_value_decomposition
double singular_value_decomposition(vector< double > &b)
Definition: Matrix.h:265
Matrix::Row::Row
Row(long len, double *row)
Definition: Matrix.h:119
principal_axes
Vector3< double > principal_axes(Vector3< double > avg, Vector3< double > avg2, Vector3< double > avgx, Vector3< double > *eigenvec)
Calculates the principal axes of 3D coordinates.
Definition: matrix.cpp:706
Matrix::columns
long columns()
Definition: Matrix.h:154
dsyevq3
vector< double > dsyevq3(Matrix &A)
Calculates the eigenvalues and normalized eigenvectors of a symmetric 3x3 matrix.
Definition: matrix.cpp:850
dsytrd3
void dsytrd3(Matrix &A, double d[3], double e[2])
Matrix::determinant
double determinant()
Definition: Matrix.h:247
Matrix::Matrix
Matrix(long rows, long cols)
Definition: Matrix.h:45
operator *
Complex< Type > operator *(const double d, Complex< Type > &c)
Definition: Complex.h:130
Matrix::operator=
Matrix operator=(const Matrix mat)
Definition: Matrix.h:131
Bstring::next
Bstring * next
Definition: Bstring.h:31
verbose
int verbose
Matrix::data
double * data()
Definition: Matrix.h:111
Matrix::Matrix
Matrix(Bstring &filename)
Definition: Matrix.h:48
Matrix::size
long size()
Definition: Matrix.h:155
irm
double irm(1.0L/get_rand_max())
Matrix::~Matrix
~Matrix()
Definition: Matrix.h:93
Matrix::Row::operator[]
double & operator[](long i)
Definition: Matrix.h:120
Bstring::c_str
const char * c_str()
Definition: Bstring.h:70
random_numbers.h
Header file for functions for creating random images.
Matrix
Definition: Matrix.h:21
Bstring
Definition: Bstring.h:29
Matrix::write
void write(Bstring &filename)
Definition: Matrix.h:95
Matrix::Row
Definition: Matrix.h:114
Vector3< double >
Matrix::randomize
void randomize()
Definition: Matrix.h:237
Bstring::integer
long integer()
Definition: Bstring.cpp:539
Matrix::normalize
void normalize()
Definition: Matrix.h:202
Matrix::swap_rows_columns
void swap_rows_columns(long rc1, long rc2)
Definition: Matrix.h:167
Matrix::operator-
Matrix operator-()
Definition: Matrix.h:138
Matrix::Matrix
Matrix(const Matrix &mat)
Definition: Matrix.h:41
VERB_FULL
#define VERB_FULL
Definition: utilities.h:109
bexit
int bexit(int value)
Exit function for cleanup and debugging.
Definition: utilities.cpp:273
tab
ostream & tab(ostream &out)
Definition: utilities.cpp:21
Bstring::split
Bstring * split()
Definition: Bstring.cpp:597
string_kill
int string_kill(Bstring *list)
Kills a string linked list.
Definition: Bstring.cpp:760