-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlotri_matrix.h
144 lines (131 loc) · 3.92 KB
/
lotri_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
/********************************************/
/* lotri_matrix.h 23rd February 2005 */
/* (c) Danny Wilson. */
/* www.danielwilson.me.uk */
/********************************************/
#ifndef _LOWER_TRIANGULAR_MATRIX_H_
#define _LOWER_TRIANGULAR_MATRIX_H_
#include <stdlib.h>
#include <stdio.h>
/****************************************************************/
/* myutils::Matrix */
/* */
/* Matrix is a C++ style container whose memory storage is */
/* designed so that elements can easily be viewed at debug */
/* time in MSVC++ and to be compatible with some C code in */
/* which matrices are stored as one-dimensional arrays, where */
/* element (i,j) would be accessed as M[i*n+j]. */
/* */
/* Element (i,j) can be accessed in one of three ways: */
/* M[i][j] clearest syntax */
/* M.element[i][j] useful for viewing during debug */
/* M.array[i*n+j] compatible with C arrays */
/* */
/****************************************************************/
namespace myutils
{
template <typename T>
class LowerTriangularMatrix
{
public:
/*Preserve public access for back-compatibility*/
T *array;
T **element;
protected:
int _n; /* dimension of the lower triangular square matrix */
int _size; /* number of elements of the matrix */
// int protected_ncols;
int initialized;
public:
/*Default constructor*/ LowerTriangularMatrix()
{
initialized=0;
initialize(0);
}
/*Constructor*/ LowerTriangularMatrix(int n)
{
initialize(n);
}
/*Constructor*/ LowerTriangularMatrix(int n, T value)
{
initialize(n);
int i,j;
for(i=0;i<n;i++)
for(j=0;j<=i;j++)
element[i][j]=value;
}
/*Destructor*/ ~LowerTriangularMatrix()
{
delete[] array;
delete[] element;
}
LowerTriangularMatrix<T>& initialize(int n)
{
int i;
int size = n*(n+1)/2;
array = new T[size];
if (!array) error("array allocation failure in LowerTriangularMatrix::initialize()");
element = new T*[n];
if (!element) error("element allocation failure in LowerTriangularMatrix::initialize()");
for(i=0;i<n;i++) element[i] = &(array[i*(i+1)/2+0]);
_n = n;
_size = size;
initialized=1;
return *this;
}
/*All current data is lost when the LowerTriangularMatrix is resized*/
LowerTriangularMatrix<T>& resize(int n)
{
int i;
int size = n*(n+1)/2;
if (!initialized) return initialize(n);
if(n==_n)return *this;
delete[] array;
delete[] element;
array = new T[size];
if (!array) error("array allocation failure in LowerTriangularMatrix::resize()");
element = new T*[n];
if (!element) error("element allocation failure in LowerTriangularMatrix::resize()");
for(i=0;i<n;i++) element[i] = &(array[i*(i+1)/2+0]);
_n = n;
_size = size;
return *this;
}
int n(){return _n;}
int size(){return _size;}
int n() const {return _n;}
int size() const {return _size;}
void error(char* error_text)
{
printf("Run-time error in LowerTriangularMatrix::");
printf("%s%\n", error_text);
printf("Exiting to system...\n");
exit(13);
}
/*Copy constructor*/ LowerTriangularMatrix(const LowerTriangularMatrix<T> &mat)
/* Copy constructor for the following cases:
LowerTriangularMatrix mat2(mat);
LowerTriangularMatrix mat2=mat;
and when LowerTriangularMatrix is returned from a function */
{
initialize(mat._n);
int i;
for(i=0;i<_size;i++)
array[i] = mat.array[i];
}
/*Assignment operator*/ LowerTriangularMatrix<T>& operator=(const LowerTriangularMatrix<T>& mat)
{
//if(this==mat)return *this;
resize(mat._n);
int i;
for(i=0;i<_size;i++)
array[i] = mat.array[i];
return *this;
}
/*Subscript operator*/inline T* operator[](int pos){return element[pos];};
inline T& safe(int i, int j) {
return (j<=i) ? element[i][j] : element[j][i];
}
};
};
#endif // _LOWER_TRIANGULAR_MATRIX_H_