-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix.cpp
171 lines (143 loc) · 3.07 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
#include <iostream>
#include <fstream>
#include <iomanip>
#include "matrix.h"
CMatrix::CMatrix(int h, int w, double v, double f)
{
if(h <= 0 || w <= 0)
{
throw WrongDim();
}
try
{
this->array = new CArray(h, w, v, f);
}catch(...)
{
throw bad_alloc();
}
}
CMatrix::CMatrix(const CMatrix& m)
{
m.array->n++;
this->array = m.array;
}
CMatrix::CMatrix(std::fstream &f)
{
array = new CArray(f);
}
CMatrix::CMatrix()
{
array = new CArray();
}
CMatrix::~CMatrix()
{
if(--array->n==0)
delete array;
}
void CMatrix::check(int i, int j)
{
if(i < 0 || i >= array->m_row || j < 0 || j >= array->m_col)
throw WrongDim();
}
int CMatrix::getRows() const
{
return this->array->m_row;
}
int CMatrix::getColumns() const
{
return this->array->m_col;
}
double CMatrix::read(int col, int row) const
{
return this->array->A[col][row];
}
void CMatrix::write(int col, int row, double val)
{
array = array->detach();
this->array->A[col][row] = val;
}
std::ostream& operator<<(std::ostream &os, CMatrix &matrix)
{
for (int i = 0; i < matrix.array->m_row; i++)
{
os << "[ ";
for (int j = 0; j < matrix.array->m_col; j++)
{
os << std::fixed << std::setprecision(1) << matrix.array->A[i][j] << " ";
}
os << "]" << std::endl;
}
return os;
}
CMatrix CMatrix::operator=(CMatrix mat)
{
mat.array->n++;
if(--array->n == 0)
delete array;
array = mat.array;
return *this;
}
/*
CMatrix CMatrix::operator=(double num)
{
if(array->n > 1)
{
CArray *arr = new CArray(1, 1, num);
this->array->n--;
this->array = arr;
return *this;
}
//
}*/
CMatrix CMatrix::operator*(CMatrix mat)
{
if(mat.array == NULL)
throw null_as_argument();
if(this->getColumns() != mat.getRows())
throw WrongDim();
CMatrix newMat(this->getRows(), mat.getColumns());
for(int i = 0; i < newMat.getRows(); i++)
{
for(int j = 0; j < newMat.getColumns(); j++)
{
double tmp = 0.0f;
for(int k = 0; k < this->getColumns(); k++)
{
tmp += read(i,k) * mat.read(k,j);
}
newMat.write(i,j,tmp);
}
}
return newMat;
}
double* CMatrix::operator[](int i)
{
check(i, getRows());
array = array->detach();
return array->A[i];
//return CMatrix(*this);
}
/*
CMatrix::Cref CMatrix::operator[](int i)
{
check(i, getRows());
//return array->A[i];
return Cref(*this,i);
}
CMatrix::Cref::Cref(CMatrix& matrix, int col): mat(matrix), m_col(col)
{
}
CMatrix::Cref::operator double()
{
return mat.read(m_col, m_row);
}
CMatrix::Cref& CMatrix::Cref::operator=(int num)
{
mat.write(m_col, m_row, num);
return *this;
}
CMatrix::Cref& CMatrix::Cref::operator=(Cref& ref)
{
return operator=((double)ref);
}
*/