-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLinearAlgebra.h
308 lines (189 loc) · 6.37 KB
/
LinearAlgebra.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
//
// Created by m7md_nor on 9/21/2021.
//
/* missing 1- clearing memory function based on readerStack
2- inverse matrix sorting algorithm before inverting*/
#pragma once
#ifndef DATA_STRUCTURES_H_LINEARALGEBRA_H
#define DATA_STRUCTURES_H_LINEARALGEBRA_H
#include "DataStructures.h"
#include <math.h>
#undef node_ptr
#define node_ptr ABST::tree_node *
typedef double TYPE ;
class DimensionError : std::exception {
};
class RedundantSystem : public std::exception {
};
class UnsolvableSystem : public std::exception {
};
namespace short_vector {
class Vector;
class Matrix;
static SingleLinkedList::stack<Vector *> delete_list;
static SingleLinkedList::stack<Matrix *> mat_delete_list;
void clear_memory();
class Vector {
private:
TYPE *vector;
int dimension;
public:
explicit Vector(int dimension);
Vector(int dimension, TYPE vector_[]);
Vector(const Vector &other);
~Vector();
void print();
int get_dimension() const;
TYPE get_item(int i);
TYPE *get();
void set_item(int i, TYPE value);
TYPE& operator[](int i);
TYPE Norm();
TYPE modulus();
// copying involved functions-----------------------------
void set_vector(Vector &v);
Vector &operator=(Vector &v2);
Vector &operator=(double *);
Vector &operator=(Vector &&v2);
Vector &operator+(Vector &v2);
Vector &operator-(Vector &v2);
Vector &operator*(TYPE scalar);
TYPE operator*(Vector &v);
bool operator==(Vector &v);
};
Matrix *inverse(Matrix *matrix,Matrix* result = nullptr);
Matrix identity(int m);
Matrix *mull(Matrix *mat1, Matrix *mat, Matrix* result= nullptr);
Matrix *Transpose_mull(Matrix *mat1, Matrix *mat, Matrix* result= nullptr);
Vector *mull(Matrix *mat1, Vector *v,Vector* result= nullptr);
class Matrix {
private:
int degree, span;
TYPE *_matrix;
TYPE det_;
Matrix *pow_(int p, Matrix &mat);
public:
Matrix(int degree, int span);
Matrix(int degree, int span, TYPE matrix_list[]); // it does not copy data
// the given location will be treated as the data buffer
~Matrix();
int get_n();
int get_m();
TYPE& get_item(int i, int j);
void set_item(int row, int column, TYPE value);
void print();
bool operator==(Matrix &mat);
Matrix &operator=(const Matrix &mat);
Matrix &operator+(Matrix &mat);
Matrix &operator+(Matrix &&mat);
Matrix &operator-(Matrix &mat);
Matrix &operator*(TYPE scalar);
Vector &operator*(Vector &v);
Matrix &operator*(Matrix &mat);
// Matrix &operator*=(Matrix &mat);
Matrix &inverse();
TYPE det();
Matrix &pow(int p);
Matrix &transpose();
friend Matrix *mull(Matrix *mat1, Matrix *mat, Matrix* result);
friend Vector *mull(Matrix *mat1, Vector *v, Vector* result);
friend Matrix *Transpose_mull(Matrix *mat1, Matrix *mat, Matrix* result);
friend Matrix *inverse(Matrix *matrix, Matrix* result);
friend class supMatrix;
};
class supMatrix{
Matrix* baseMatrix;
int rs,re,cs,ce;
int R,C;
public:
supMatrix(Matrix*matrix,int rs, int re,int cs,int ce);
TYPE &item(int x, int y);
supMatrix* mull(supMatrix* other, supMatrix* result);
supMatrix* add(supMatrix* other, supMatrix* result);
};
void Recursive_Matrix_Mull(Matrix* matrix1, Matrix* matrix2, Matrix* result);
}
namespace sparse_system {
class Matrix;
class AgMatrix;
static SingleLinkedList::stack<Matrix *> mat_delete_list;
static SingleLinkedList::stack<short_vector::Vector *> delete_list;
void clear_memory();
typedef struct _Item_ {
int key;
TYPE value;
_Item_ *next;
}item;
void delete_row(item *row);
item *q_sort(item *head, item *end);
item *add_in_place(item *x, item *y);
item *_add_(item *x, item *y, TYPE , TYPE);
item *row_matrix_mul(item *row, item *arr[]);
class Matrix {
private:
int span;
int degree;
item **array;
double determinant = 1;
protected:
Matrix ©();
public:
Matrix(int span, int degree);
Matrix(Matrix& matrix);
void set_item(int row, int column, TYPE value);
void sort();
void print();
Matrix &operator+(Matrix &other);
Matrix &operator-(Matrix &other);
Matrix &operator*(TYPE scalar);
Matrix &operator*(Matrix &other);
short_vector::Vector & operator*(short_vector::Vector &vector);
Matrix &inverse();
~Matrix();
Matrix& pow(int p) ;
Matrix& transpose();
Matrix& diagonalize_trianglur_matrix(TYPE*& eigenvalues);
friend Matrix &operator*(TYPE scalar, Matrix &matrix);
friend Matrix* pow_(int p, Matrix &mat, Matrix* ) ;
friend sparse_system::AgMatrix;
};
Matrix &operator*(TYPE scalar, Matrix &matrix);
class AgMatrix:public Matrix{
public:
inline AgMatrix(int span , int degree):Matrix(span,degree)
{
}
void solveSystem(short_vector::Vector& resultVector);
inline void setDegree(int i) {
degree = i;
}
};
}
namespace TreeBasedMatrix{
typedef float type;
typedef struct node{
int key;
type data[5];
node* left;
node* right;
short height;
}node;
class Vector{
node * root;
int span,counter;
public:SingleLinkedList::stack<node*> readerStack;
type* reader;
public:
Vector();
type& operator[](int i);
void pushItem(type value);
void print();
type read();
void start();
void operator++();
int getCounter();
int getSpan();
Vector& operator+=(Vector&);
};
}
#endif //DATA_STRUCTURES_H_LINEARALGEBRA_H