-
Notifications
You must be signed in to change notification settings - Fork 0
/
matrix.cpp
105 lines (89 loc) · 2.04 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
#include "matrix.hpp"
#include "vector.hpp"
#include <cassert>
template<class T>
// Standardkonstruktor
Matrix<T>::Matrix():
zeilen(0u), spalten(0u), data(nullptr) { };
template<class T>
// Benutzerdefinierter Konstruktor
Matrix<T>::Matrix(unsigned long zei, unsigned long spa) :
zeilen(zei), spalten(spa), data(nullptr)
{
if(zei > 0u && spa >0u)
data = new T[zei*spa];
};
template<class T>
// Kopier-Konstruktor
Matrix<T>::Matrix(const Matrix& other)
{
data = new T[spalten*zeilen];
};
template<class T>
// Destruktor
Matrix<T>::~Matrix()
{
if(data != nullptr)
delete [] data;
};
template<class T>
// Zeilen
unsigned int Matrix<T>::get_zei()
{
return zeilen;
}
template<class T>
// Spalten
unsigned int Matrix<T>::get_spa()
{
return spalten;
}
template<class T>
// Überladung (), Matrixeinträge
T& Matrix<T>::operator()(unsigned int i, unsigned int j)
{
assert(i < zeilen);
assert(j < spalten);
assert(data != nullptr);
return data[i*spalten+j];
}
template<class T>
// Kopier-Funktion
void Matrix<T>::copy(const Matrix<T>& other)
{
if(zeilen > 0u && spalten > 0u)
{
assert(other.data != nullptr);
//kopiere Array-Inhalte
for(unsigned int i(0); i <= spalten*zeilen; i++)
this->data[i] = other.data[i];
}
}
template<class T>
// Überladung =
Matrix<T>& Matrix<T> :: operator=(const Matrix<T>& other)
{
assert(this->get_zei() == other.zeilen && this->get_spa() == other.spalten);
this->copy(other);
return *this;
}
template<class T>
// Matrix-Vektor-Multiplikation
Vector<T> Matrix<T>::apply(Vector<T>& other)
{
assert(this->get_spa() == other.get_size());
assert(data != nullptr);
Vector<T> result(zeilen);
for(unsigned int i(0); i < zeilen; i++)
{T k(0);
for(unsigned int j(0); j < spalten; j++)
{
k += (this->data[(i)*spalten+j]*other[j]);
};
result[i] = k;
};
return result;
}
template class Matrix<double>;
template class Matrix<float>;
template class Matrix<int>;