-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmatrix.h
102 lines (87 loc) · 2.69 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
#ifndef MATRIX_H
#define MATRIX_H
#include <iostream>
#include <vector>
#include <fstream>
#include <cassert>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <array>
#include <climits>
using namespace std;
class Matrix {
private:
double ** m;
int rows;
int cols;
void clearMatrix();
public:
// constructors
Matrix();
Matrix(int rows, int cols, double num=0);
Matrix(double ** lhs, int r, int c);
Matrix(const Matrix & lhs);
Matrix(vector<vector<double>> & lhs);
Matrix(double * vec, int n, string type = "column", bool remove_vec = true);
Matrix(string fname);
// getters
int getRows()const { return rows; };
int getCols()const { return cols; };
double* return_col(int i)const;
double* return_row(int i)const;
// operators
Matrix operator+(const Matrix & rhs)const;
Matrix operator-(const Matrix & rhs)const;
Matrix operator*(const Matrix & rhs)const;
Matrix operator*(double rhs)const;
Matrix operator+(double rhs)const;
Matrix operator-(double rhs)const;
Matrix operator/(double rhs)const;
void operator*=(double rhs);
void operator+=(double rhs);
void operator-=(double rhs);
void operator/=(double rhs);
void operator*=(const Matrix & rhs);
void operator+=(const Matrix & rhs);
void operator-=(const Matrix & rhs);
bool operator==(const Matrix & rhs)const;
bool operator!=(const Matrix & rhs)const;
const Matrix & operator=(const Matrix & rhs);
double* operator[](int i)const { return m[i]; }
// member utils
void swap_rows(int index_1, int index_2);
void swap_cols(int index_1, int index_2);
Matrix remove_row_col(int r, int c, bool self = false);
Matrix cofactor_matrix();
Matrix elewiseSquare(bool self = false);
Matrix sum(int axis = 0, bool self = false);
Matrix clipCols(int start, int end, bool self = false);
Matrix clipRows(int start, int end, bool self = false);
double frobeniusNorm();
void assignCol(Matrix m, int col);
void assignRow(Matrix mat, int row);
void trimMatrix();
void printMatrix()const;
Matrix apply(double(*function)(double));
// matrix functions
Matrix T(bool self = false);
Matrix gaussianElimination(bool self = false);
Matrix inverse();
double determinant();
pair<Matrix, Matrix> qr_decomposition(); // analytical gram-schmidt solution (fails when matrix is singular)
array<Matrix, 3> singular_value_decomposition(); // numerical approximation with jacobi eigenvalue algorithm
vector<double> eigs();
~Matrix(); // destructor
};
// free utils
Matrix eye(int n);
Matrix zeros(int n);
Matrix ones(int n);
Matrix concatCols(Matrix a, Matrix b);
Matrix concatRows(Matrix a, Matrix b);
Matrix diag(Matrix & s);
Matrix diag(vector<double> & s);
vector<vector<double>> matrixtoVector(Matrix & m);
double max(Matrix m);
# endif