-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.h
107 lines (82 loc) · 3.62 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
/******************************************************************************
Vector, Matrix, and Permutation classes, and related fcns
For example:
Vector v(5) gives avector v of 5 doubles, indexed from 0 to 4.
Matrix m(5,12) gives a matrix of size 5X12 doubles,
indexed from 0 to 4 and 0 to 11.
******************************************************************************/
#ifndef Matrix_Included
#define Matrix_Included
#include <iostream>
#include <vector>
class Vector {
private:
int size;
double* array;
public:
Vector(int n) { size = n; array = new double[size]; };
Vector() { size = 1, array = new double[size]; }; // class members initialization
Vector(const Vector&);
~Vector() { delete[] array; }
int n(int=0) const { return size; } // return size of the vector
double operator() (int i) const { return array[i]; }
// access to return element of vector
double& operator() (int i) { return array[i]; }
// access to modify element of vector
Vector& operator=(const Vector&); // equate two vectors
Vector& operator=(double); // fill value into vector
Vector& operator=(double*); // copy array to vector. NOTE: unsafe
Vector& operator=(std::vector<double>&); // copy std::vector into vector. NOTE: safe
Vector& operator+=(const Vector&); // increment vector by another vector
Vector& operator-=(const Vector&); // decrement vector by another vector
Vector& operator*=(double); // multiplication by a scalar
Vector& operator/=(double); // division by a scalar
double operator*(const Vector& v) const; // scalar dot product
};
class Matrix {
private:
int size[2];
double* array;
public:
Matrix(int n0, int n1) { size[0] = n0; size[1] = n1;
array = new double[size[0]*size[1]]; };
Matrix(const Matrix&);
~Matrix() { delete[] array; }
int n(int i) const { return size[i]; }
double operator() (int i, int j) const { return array[i + size[0]*j]; }
// access to return element of matrix
double& operator() (int i, int j) { return array[i + size[0]*j]; }
// access to modify element of matrix
Matrix& operator=(const Matrix&); // equate two matrices
Matrix& operator=(double); // fill value into matrix
Matrix& operator+=(const Matrix&); // increment matrix by another matrix
Matrix& operator-=(const Matrix&); // decrement matrix by another matrix
Matrix& operator*=(double); // multiplication by a scalar
Matrix& operator/=(double); // division by a scalar
};
class Permutation {
private:
int size;
int* array;
int my_parity;
public:
Permutation(int n) { size = n; array = new int[size]; identity(); };
~Permutation() { delete[] array; }
int n(int=0) const { return size; }
int operator() (int i) const { return array[i]; }
void identity();
void swap(int i, int j);
double parity() const { return my_parity; }
void permute(Vector& b) const;
};
std::ostream& operator<< (std::ostream&, const Vector&); // output vector
std::ostream& operator<< (std::ostream&, const Matrix&); // output matrix by rows
std::istream& operator>> (std::istream&, Vector&); // input vector
std::istream& operator>> (std::istream&, Matrix&); // input matrix by rows
double l2norm(const Vector&); // L2-norm of the vector
double maxNorm(const Vector&); // L-infinity (max) norm of the vector
double maxNorm(const Matrix&); // L-infinity (max) norm of the matrix
double scDot(const Vector&, const Vector&); // Scalar dot product of vectors
int matVecMult(const Matrix& M, const Vector& x, Vector& y); // y = Mx
int matMatMult(const Matrix& A, const Matrix& B, Matrix& C); // C = AB
#endif