This repository has been archived by the owner on May 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
matrix.h
153 lines (114 loc) · 3.59 KB
/
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
145
146
147
148
149
150
151
152
153
#include <stdexcept>
#include <cmath>
#include "complex.h"
template <typename T>
class Matrix{
public:
virtual T get(int x, int y) const = 0;
virtual void set(T item, int x, int y) = 0;
virtual int getWidth() const = 0;
virtual int getHeight() const = 0;
bool operator==(const Matrix<T> &matrix) const {
if(this->width != matrix.width || this->height != matrix.height) return false;
for(int i = 0; i < this->width; i++)
for(int j = 0; j < this->height; j++)
if(this->get(i, j) != matrix.get(i, j)) return false;
return true;
}
template <typename U>
U getNorm() const {
U sum = U();
for(int x = 0; x < this->getWidth(); x++)
for(int y = 0; y < this->getHeight(); y++)
sum += this->get(x, y) * this->get(x, y);
return static_cast<U>(std::sqrt(sum));
}
};
template<>
template<>
float Matrix<complexf>::getNorm<float>() const {
complexf sum = 0;
for(int x = 0; x < this->getWidth(); x++)
for(int y = 0; y < this->getHeight(); y++){
sum += this->get(x, y) * this->get(x, y).conj();
}
return std::sqrt(sum.real());
}
template <typename T>
class DiagonalMatrix : public Matrix<T>{
private:
Sequence<Sequence<T>*> *data;
int diag = 0; //diagonality
int width = 0, height = 0;
int getDiagLength(int x, int y) const {
return getDiagLength(getDiagNumber(x,y));
}
int getDiagLength(int diagNum) const {
diagNum = this->diag / 2 - diagNum;
int x = 0, y = 0;
if(diagNum >= 0){
x = diagNum;
y = 0;
}else{
x = 0;
y = -diagNum;
}
int t1 = this->width - x;
int t2 = this->height - y;
return (t1 < t2 ? t1 : t2);
}
int getDiagNumber(int x, int y) const {
return y - x + this->diag/2;
}
public:
DiagonalMatrix(int width, int height, int diag) : width(width), height(height), diag(diag) {
if(width < 0 || height < 0 || diag < 0) throw std::length_error(NEGATIVE_SIZE_MESSAGE);
data = (Sequence<Sequence<T>*>*) new ListSequence<ListSequence<T>*>;
for(int i = 0; i < diag; i++){
data->append(new ListSequence<T>(getDiagLength(i)));
}
}
T get(int x, int y) const override {
if(x < 0 || x >= this->width || y < 0 || y >= this->height) throw std::out_of_range(INDEX_OUT_OF_RANGE_MESSAGE);
int num = this->getDiagNumber(x, y);
if(num < 0 || num >= this->diag) return T();
return data->get(getDiagNumber(x,y))->get( (x < y) ? x : y );
}
void set(T item, int x, int y) override {
if(x < 0 || x >= this->width || y < 0 || y >= this->height) throw std::out_of_range(INDEX_OUT_OF_RANGE_MESSAGE);
int num = this->getDiagNumber(x, y);
if(num < 0 || num >= this->diag){
if(item != T())
throw std::domain_error("trying to set non-diagonal element");
return;
}
data->get(getDiagNumber(x,y))->set(item, (x < y) ? x : y );
}
int getDiag() const {
return this->diag;
}
template <typename U>
DiagonalMatrix<T>* operator+(const DiagonalMatrix<U> &matrix) const {
DiagonalMatrix<T> *newMatrix = new DiagonalMatrix<T>(this->width, this->height, (this->diag > matrix.getDiag() ? this->diag : matrix.getDiag()));
for(int i = 0; i < this->width; i++)
for(int j = 0; j < this->height; j++){
newMatrix->set(this->get(i, j) + matrix.get(i, j), i, j);
}
return newMatrix;
}
template <typename U>
DiagonalMatrix<T>* operator*(U k) const {
DiagonalMatrix<T> *newMatrix = new DiagonalMatrix<T>(this->width, this->height, this->diag);
for(int i = 0; i < this->width; i++)
for(int j = 0; j < this->height; j++){
newMatrix->set(this->get(i, j) * k, i, j);
}
return newMatrix;
}
virtual int getWidth() const override {
return this->width;
}
virtual int getHeight() const override {
return this->height;
}
};