-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathSparseMatrix.h
More file actions
97 lines (59 loc) · 2.37 KB
/
SparseMatrix.h
File metadata and controls
97 lines (59 loc) · 2.37 KB
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
/**
* This file is part of the SparseMatrix library
*
* @license MIT
* @author Petr Kessler (https://kesspess.cz)
* @link https://github.com/uestla/Sparse-Matrix
*/
#ifndef __SPARSEMATRIX_H__
#define __SPARSEMATRIX_H__
#include <vector>
#include <iostream>
namespace SparseMatrix
{
template<typename T>
class SparseMatrix
{
public:
// === CREATION ==============================================
SparseMatrix(int n); // square matrix n×n
SparseMatrix(int rows, int columns); // general matrix
SparseMatrix(const SparseMatrix<T> & m); // copy constructor
SparseMatrix<T> & operator = (const SparseMatrix<T> & m);
~SparseMatrix(void);
// === GETTERS / SETTERS ==============================================
int getRowCount(void) const;
int getColumnCount(void) const;
// === VALUES ==============================================
T get(int row, int col) const;
SparseMatrix & set(T val, int row, int col);
// === OPERATIONS ==============================================
std::vector<T> multiply(const std::vector<T> & x) const;
std::vector<T> operator * (const std::vector<T> & x) const;
SparseMatrix<T> multiply(const SparseMatrix<T> & m) const;
SparseMatrix<T> operator * (const SparseMatrix<T> & m) const;
SparseMatrix<T> add(const SparseMatrix<T> & m) const;
SparseMatrix<T> operator + (const SparseMatrix<T> & m) const;
SparseMatrix<T> subtract(const SparseMatrix<T> & m) const;
SparseMatrix<T> operator - (const SparseMatrix<T> & m) const;
// === FRIEND FUNCTIONS =========================================
template<typename X>
friend bool operator == (const SparseMatrix<X> & a, const SparseMatrix<X> & b);
template<typename X>
friend bool operator != (const SparseMatrix<X> & a, const SparseMatrix<X> & b);
template<typename X>
friend std::ostream & operator << (std::ostream & os, const SparseMatrix<X> & matrix);
protected:
int m, n;
std::vector<T> * vals;
std::vector<int> * rows, * cols;
// === HELPERS / VALIDATORS ==============================================
void construct(int m, int n);
void destruct(void);
void deepCopy(const SparseMatrix<T> & m);
void validateCoordinates(int row, int col) const;
void insert(int index, int row, int col, T val);
void remove(int index, int row);
};
}
#endif