-
Notifications
You must be signed in to change notification settings - Fork 9
/
CMatrix3.h
180 lines (158 loc) · 5.37 KB
/
CMatrix3.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
#ifndef __CMATRIX__
#define __CMATRIX__
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <iomanip.h>
#include <string.h>
#include <math.h>
#include <malloc.h>
void test_mat (); // Function to test matrix class
typedef class CMatrix Matrix;
typedef class CMatrix ColumnVector;
typedef class CMatrix DiagonalMatrix;
typedef class CMatrix SymmetricMatrix;
struct matrep
{
double **Mat;
int r;
int c;
};
typedef double p_D;
//*****************************************************************************
// Alberto N. Perez
//
// CMatrix class
// A basic matrix class that can be embedded into a real time
// system such as navigation or flight control.
// All the source is available with in one file, and it can
// be review for a flight system verification.
// This class will be use as basic building block for a vector
// and a quaternion class
//
// members
// matrix and scalar addition
// matrix and scalar subtraction
// matrix and scalar multiplication
// inverse
// determinant
// max and min element
// variance and mean of elements
// Notes
// The matrix storage and handling and inverse matrix member are mainly
// derived from reference #1.
// Reference (2) was used to verify reference (1).
//
//
//
// References
// (1) "C++ Inside & Out", by Bruce Eckel, Chapter #9
// (2) "Numerical Recepies" in C, Second Edition, Chapter #2
//
//
//*****************************************************************************
typedef double Real;
class CMatrix
{
private:
matrep * p;
double init_val;
public:
CMatrix (int r = 1, int c = 1, double init = 0);
CMatrix (int r, int c, double *initval);
CMatrix (char *flag, int dimension, double init);
CMatrix (char *flag, int dimension);
CMatrix (char *initfile);
CMatrix::CMatrix (int mrows, double *initvalues);
~CMatrix (void);
//************************************************
// void ReSize(int r,int c,double init);
// void ReSize(int r,int c);
// void ReSize(int r);
void CMatrix::Create_Mem (int r, int c);
void Release_Mem (void);
void Release (void)
{
Release_Mem ();
};
CMatrix (CMatrix & cm); // Copy constructor
CMatrix operator + (const CMatrix & M); // Matrix addition
CMatrix operator + (const double rval); // Scalar addition
CMatrix operator - (const CMatrix & M); // Matrix subtraction
CMatrix operator - (const double rval); // Scalar subtraction
CMatrix operator * (const CMatrix & M); // Matrix multiplication
CMatrix operator * (double d); // Scalar multiplication
CMatrix operator / (CMatrix M); // Matrix Division
CMatrix operator / (double d); // Scalar Division
// Assign a value to an array element //
CMatrix operator = (const CMatrix & M);
CMatrix operator = (const double d);
double &operator () (int m, int n);
double &operator () (int m);
// operator double() const;
//***************************************//
// Read the values from an array element //
// operator double();
//***************************************//
int rows () const
{
return p->r;
}; // rows in matrix
int cols () const
{
return p->c;
}; // cols in matrix
int Nrows () const
{
return p->r;
}; // rows in matrix
int Ncols () const
{
return p->c;
}; // cols in matrix
//****************************************************
CMatrix transpose (); // transpose a square matrix
CMatrix t ()
{
return transpose ();
}; // Transpose shell for newmat
double determinant (); // Determinant
CMatrix inverse (); // Inverse
CMatrix i ()
{
return inverse ();
} // Inverse shell for newmat
//****************************************************
private:
double &val (int row, int col);
double &mval (int row, int col)
{
return (p->Mat[row][col]);
};
double &mval (int row)
{
return (p->Mat[row][0]);
};
CMatrix scale (void);
void copy_column (CMatrix & mm, int from_col, int to_col);
void deepcopy (CMatrix & from, CMatrix & to);
CMatrix lu_decompose (CMatrix & indx, int &d);
void switch_columns (int col1, int col2);
void lu_back_subst (CMatrix & indx, CMatrix & b);
public:
void display (void);
void error (char *c)
{
printf ("%s", c);
getchar ();
exit (1);
};
void error (char *msg1, char *msg2)
{
cerr << "matrix error: " << msg1 << " " << msg2 << endl;
exit (1);
}
void write_standard (char *filename, char *msg);
};
#endif