-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmatrix.cpp
114 lines (100 loc) · 2.73 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
/**
* This file is part of MT-BA.
*
* Copyright 2018 Cao Lin.
* Developed by Cao Lin ,
* If you use this code, please cite the respective publications as
* listed on the above website.
*
* MT-BA is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* MT-BA is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MT-BA. If not, see <http://www.gnu.org/licenses/>.
*/
#include "matrix.h"
#include <iomanip>
#include <exception>
clMat::clMat(int _row, int _col) : row(_row), col(_col)
{
if (row < 1) {
std::cerr << "row is lower than 1 in " << __FILE__ << " line : " << __LINE__ << endl;
throw std::runtime_error("row is lower than 1");
}
if (col < 1) {
std::cerr << "col is lower than 1 in " << __FILE__ << " line : " << __LINE__ << endl;
throw std::runtime_error("col is lower than 1");
}
data = new double*[row];
data[0] = new double[row * col]();
for (int i = 1; i < row; ++i)
{
data[i] = data[0] + i * col;
}
}
clMat::~clMat()
{
delete[]data[0];
for (int i = 0; i < row; ++i)
data[i] = nullptr;
delete[]data;
data = nullptr;
}
void clMat::setData(double* _data)
{
#ifdef _MSC_VER
memcpy_s(data[0], sizeof(double) * row * col, _data, sizeof(double) * row * col);
#else
memcpy(data[0], _data, sizeof(double) * row * col);
#endif // check the IDE
}
void clMat::resize(int _row, int _col)
{
delete[]data[0];
delete[]data;
data = nullptr;
row = _row;
col = _col;
if (row < 1) {
std::cerr << "row is lower than 1 in " << __FILE__ << " line : " << __LINE__ << endl;
throw std::runtime_error("row is lower than 1");
}
if (col < 1) {
std::cerr << "col is lower than 1 in " << __FILE__ << " line : " << __LINE__ << endl;
throw std::runtime_error("col is lower than 1");
}
data = new double*[row];
data[0] = new double[row * col]();
for (int i = 1; i < row; ++i)
{
data[i] = data[0] + i * col;
}
}
void clMat::output(std::ostream& out)const
{
for (int i = 0; i < row; ++i) {
for (int j = 0; j < col; ++j) {
out << std::setw(7) << data[i][j] << " ";
}
out << endl;
}
}
std::ostream& operator << (std::ostream& out, const clMat& x)
{
x.output(out);
return out;
}
clMat clMat::Identity(int _size)
{
clMat* I = new clMat(_size, _size);
for (int i = 0; i < _size; ++i)
I->data[i][i] = 1;
return *I;
}