Newer
Older
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
/* Sparse_Matrix.h
Mark Woolrich, FMRIB Image Analysis Group
Copyright (C) 1999-2000 University of Oxford */
/* CCOPYRIGHT */
#if !defined(Sparse_Matrix_h)
#define Sparse_Matrix_h
#include <iostream>
#include "newmat.h"
#include <math.h>
#include <map>
#include <vector>
#include "newmatio.h"
using namespace NEWMAT;
using namespace std;
namespace MISCMATHS {
class SparseMatrix
{
public:
typedef map<int,double> Row;
SparseMatrix() : nrows(0), ncols(0) {}
SparseMatrix(int pnrows, int pncols);
SparseMatrix(const SparseMatrix& psm)
{
operator=(psm);
}
const SparseMatrix& operator=(const SparseMatrix& psm)
{
nrows = psm.nrows;
ncols = psm.ncols;
data = psm.data;
return *this;
}
SparseMatrix(const Matrix& pmatin)
{
operator=(pmatin);
}
const SparseMatrix& operator=(const Matrix& pmatin);
// void ReSize(int pnrows, int pncols)
void ReSize(int pnrows, int pncols);
void clear()
{
ReSize(0,0);
}
void transpose(SparseMatrix& ret);
ReturnMatrix RowAsColumn(int r) const;
int maxnonzerosinrow() const;
void permute(const ColumnVector& p, SparseMatrix& pA);
const double operator()(int x, int y) const
{
double ret = 0.0;
map<int,double>::const_iterator it=data[x-1].find(y-1);
if(it != data[x-1].end())
ret = (*it).second;
return ret;
}
void set(int x, int y, double val)
{
data[x-1][y-1] = val;
}
void update(int x, int y, double val)
{
data[x-1][y-1] = val;
}
void insert(int x, int y, double val)
{
data[x-1].insert(Row::value_type(y-1,val));
}
void addto(int x, int y, double val)
{
if(val!=0)
data[x-1][y-1] += val;
}
void multiplyby(int x, int y, double val)
{
if((*this)(x,y)!=0)
data[x-1][y-1] *= val;
}
float trace() const;
Row& row(int r) { return data[r-1]; }
const Row& row(int r) const { return data[r-1]; }
ReturnMatrix AsMatrix() const;
int Nrows() const { return nrows; }
int Ncols() const { return ncols; }
void multiplyby(float S);
private:
int nrows;
int ncols;
vector<map<int,double>,single_client_alloc> data;
};
void multiply(const SparseMatrix& lm, const SparseMatrix& rm, SparseMatrix& ret);
void multiply(const SparseMatrix& lm, const ColumnVector& rm, ColumnVector& ret);
void multiply(const SparseMatrix& lm, const SparseMatrix::Row& rm, ColumnVector& ret);
void colvectosparserow(const ColumnVector& col, SparseMatrix::Row& row);
}
#endif