-
Notifications
You must be signed in to change notification settings - Fork 0
/
Matrix.cpp
182 lines (123 loc) · 3.67 KB
/
Matrix.cpp
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
// Matrix.cpp
//
// Matrix class. It uses an MatrixStructure for the actual storage.
// This class acts like a kind of adapter class since it defines a common interface
// for different matrix structures like normal matrices and sparse matrices.
// The matrix structure to use is given as template argument.
//
// 1 februari 1999 RD Started
// 2002-4-9 DD small changes
// 2005-12-17 DD sameShape() function; ALSO size_t --> I
//
// (C) Datasim Component Technology 1999-2006
#ifndef Matrix_cpp
#define Matrix_cpp
#include "Matrix.hpp"
// Constructors & destructor
template <class V, class I, class S>
Matrix<V, I, S>::Matrix()
{ // Default constructor
m_structure=S();
m_rowstart=1;
m_columnstart=1;
nr = nc = 1;
}
template <class V, class I, class S>
Matrix<V, I, S>::Matrix(I rows, I columns)
{ // Constructor with size. Start index=1.
m_structure=S(rows, columns);
m_rowstart=1;
m_columnstart=1;
nr = rows; nc = columns;
}
template <class V, class I, class S>
Matrix<V, I, S>::Matrix(I rows, I columns, I rowStart, I columnStart)
{ // Constructor with size & start index
m_structure=S(rows, columns);
m_rowstart=rowStart;
m_columnstart=columnStart;
nr = rows; nc = columns;
}
template <class V, class I, class S>
Matrix<V, I, S>::Matrix(const Matrix<V, I, S>& source)
{ // Copy constructor
m_structure=source.m_structure;
m_rowstart=source.m_rowstart;
m_columnstart=source.m_columnstart;
nr = source.nr; nc = source.nc;
}
template <class V, class I, class S>
Matrix<V, I, S>::~Matrix()
{ // Destructor
}
// Selectors
template <class V, class I, class S>
I Matrix<V, I, S>::MinRowIndex() const
{ // Return the minimum row index
return m_rowstart;
}
template <class V, class I, class S>
I Matrix<V, I, S>::MaxRowIndex() const
{ // Return the maximum row index
return m_rowstart + Rows() - 1;
}
template <class V, class I, class S>
I Matrix<V, I, S>::MinColumnIndex() const
{ // Return the minimum column index
return m_columnstart;
}
template <class V, class I, class S>
I Matrix<V, I, S>::MaxColumnIndex() const
{ // Return the maximum column index
return m_columnstart + Columns() - 1;
}
template <class V, class I, class S>
I Matrix<V, I, S>::Rows() const
{ // The number of rows
return nr;
}
template <class V, class I, class S>
I Matrix<V, I, S>::Columns() const
{ // The number of columns
return nc;
}
template <class V, class I, class S>
void Matrix<V, I, S>::Row(I row, const Array<V, I>& val)
{ // Replace row
I i, j;
cout << "mod row";
print(val);
int y; cin >> y;
// Replace the row elements
for (i=val.MinIndex(), j = MinColumnIndex(); i<=val.MaxIndex(); i++, j++) m_structure[row][j]=val[i];
}
template <class V, class I, class S>
void Matrix<V, I, S>::Column(I column, const Array<V, I>& val)
{ // Replace column
I i, j;
// Replace the column elements
for (i=val.MinIndex(), j=MinRowIndex(); i<=val.MaxIndex(); i++, j++) m_structure[j][column]=val[i];
}
// Operators
template <class V, class I, class S>
inline const V& Matrix<V, I, S>::operator () (I row, I column) const
{ // Get the element at position
return m_structure[row - m_rowstart + 1][column - m_columnstart + 1];
}
template <class V, class I, class S>
V& Matrix<V, I, S>::operator () (I row, I column)
{ // Get the element at position
return m_structure[row - m_rowstart + 1][column - m_columnstart + 1];
}
template <class V, class I, class S>
inline Matrix<V, I, S>& Matrix<V, I, S>::operator = (const Matrix<V, I, S>& source)
{ // Assignment operator
// Exit if same object
if (this==&source) return *this;
m_structure=source.m_structure;
m_rowstart=source.m_rowstart;
m_columnstart=source.m_columnstart;
nr = source.nr; nc = source.nc;
return *this;
}
#endif // Matrix_cpp