-
Notifications
You must be signed in to change notification settings - Fork 1
/
trimatrix.h
executable file
·93 lines (81 loc) · 1.96 KB
/
trimatrix.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
#ifndef _TRI_MATRIX_
#define _TRI_MATRIX_
#include <cassert>
#include <cstdio>
template<class T> class TriMatrix {
public:
TriMatrix() {
matrix = NULL;
size = 0;
}
TriMatrix(int n) {
matrix = NULL;
size = 0;
init(n);
}
void init(int n) {
if (n == 0)
return;
if (n == size) {
for (int i = 0; i < size - 1; i++)
for (int j = 0; j <= i; j++)
matrix[i][j] = T(0.0);
} else {
release();
size = n;
matrix = new T*[size - 1];
for (int i = 0; i < size - 1; i++)
matrix[i] = new T[i + 1];
for (int i = 0; i < size - 1; i++)
for (int j = 0; j <= i; j++)
matrix[i][j] = T(0.0);
}
}
void release() {
if (matrix == NULL)
return;
for (int i = 0; i < size - 1; i++)
delete[] (matrix[i]);
delete[] matrix;
size = 0;
}
~TriMatrix() {
release();
}
void dec(int i, int j, int step=1) {
if (i==j) return;
if (i < j)
matrix[j-1][i]-=step;
else
matrix[i-1][j]-=step;
}
void inc(int i, int j, int step=1) {
if (i==j) return;
dec(i, j, -step);
}
void write(int i, int j, T val) {
assert(i < size && j < size);
if (i == j) return;
if (i < j) {
int temp = i;
i = j;
j = temp;
}
matrix[i - 1][j] = val;
}
T operator()(int i, int j) const {
if (i == j)
return T(1.0);
assert(i < size && j < size);
if (i < j) {
int temp = i;
i = j;
j = temp;
}
return matrix[i - 1][j];
}
private:
T** matrix;
int size;
};
#endif