-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMatrix.cpp
More file actions
195 lines (157 loc) · 3.41 KB
/
Matrix.cpp
File metadata and controls
195 lines (157 loc) · 3.41 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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "Matrix.h"
#include <fstream>
#include <iostream>
using namespace std;
//the indices are 1-based!!
Matrix::Matrix(int r, int c)
{
rows = r;
cols = c;
if (r < 1)
{
rows = 1;
}
if (c < 1)
{
cols = 1;
}
int num_elements = rows*cols;
mat = new double[rows*cols];
//initialize the matrix to contain all zeroes (might not be square in general)
int count = 0;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
mat[count] = 0.0;
count++;
}
}
}
Matrix::~Matrix()
{
delete[] mat;
}
void Matrix::setElement(int row, int col, double val)
{
int index = (row - 1) * cols + col - 1;
mat[index] = val;
}
double Matrix::getElement(int row, int col)
{
int index = (row - 1) * cols + col - 1;
return mat[index];
}
void Matrix::displayMatrix()
{
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
cout << this->getElement(i, j) << " ";
}
cout << endl;
}
cout << endl;
}
Matrix* Matrix::add(Matrix* other)
{
int rows1 = rows;
int cols1 = cols;
int rows2 = other->rows; //getNumRows()
int cols2 = other->cols; //getNumCols()
if (cols1 != rows1 || cols2 != rows2)
{
cout << "Cannot add matrices." << endl;
return NULL;
}
Matrix* result = new Matrix(rows1, cols1);
int rows = rows1;
int cols = cols1;
//loop over all elements of resulting matrix
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
double x = this->getElement(i, j) + other->getElement(i, j);
result->setElement(i, j, x);
}
}
return result;
}
Matrix* Matrix::multiply(Matrix* other)
{
int rows1 = rows;
int cols1 = cols;
int rows2 = other->rows;
int cols2 = other->cols;
if (cols1 != rows2)
{
cout << "Cannot multiply matrices." << endl;
return NULL;
}
Matrix* result = new Matrix(rows1, cols2);
int rows = rows1;
int cols = cols2;
double x;
//loop over all elements of resulting matrix
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
x = 0;
//sum up multiplying matrices to obtain value placed in new matrix
for (int k = 1; k <= cols1; k++)
{
x = x + this->getElement(i, k) * other->getElement(k, j);
}
result->setElement(i, j, x);
}
}
return result;
}
//assumes a specific format for the file
Matrix* Matrix::readMatrix(const char* file_name)
{
ifstream matrix_file;
matrix_file.open(file_name);
int rows = 1;
int cols = 1;
matrix_file >> rows >> cols;
if (rows < 1)
{
rows = 1;
}
if (cols < 1)
{
cols = 1;
}
Matrix* matrix = new Matrix(rows, cols);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
double val = 0.0;
matrix_file >> val;
matrix->setElement(i, j, val);
}
}
matrix_file.close();
return matrix;
}
//assumes a specific format for the file
void Matrix::writeMatrix(const char* file_name)
{
ofstream matrix_file;
matrix_file.open(file_name);
matrix_file << rows << " " << cols << endl;
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= cols; j++)
{
matrix_file << this->getElement(i, j) << " ";
}
matrix_file << endl;
}
matrix_file.close();
}