-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix.cpp
116 lines (96 loc) · 2.81 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
#include <string>
#include <sstream>
#include <iostream>
#include <complex>
#include "Matrix.h"
using std::string;
using std::complex;
const Matrix Matrix::identity_matrix = Matrix(1.0, 0.0, 0.0, 1.0);
Matrix Matrix::Inverted() const
{
Matrix inverted(d, -b, -c, a);
return inverted / CalculateDeterminant();
}
complex <double> Matrix::CalculateDeterminant() const
{
return (a * d) - (b * c);
}
string Matrix::ComplexToString(const complex <double> & value)
{
std::stringstream out;
out << value;
return out.str();
}
int MaxLength(const string & a, const string & b)
{
return (a.length() > b.length()) ? a.length() : b.length();
}
string PadStringToLength(const string & input_string, unsigned int final_length)
{
string result = input_string;
while (result.length() < final_length) result += " ";
return result;
}
string Matrix::ToString() const
{
string string_a = ComplexToString(a);
string string_b = ComplexToString(b);
string string_c = ComplexToString(c);
string string_d = ComplexToString(d);
unsigned int max_width_left = MaxLength(string_a, string_c);
unsigned int max_width_right = MaxLength(string_b, string_d);
std::stringstream result;
result << "| " << PadStringToLength(string_a, max_width_left) << " " << PadStringToLength(string_b, max_width_right) << " |" << std::endl;
result << "| " << PadStringToLength(string_c, max_width_left) << " " << PadStringToLength(string_d, max_width_right) << " |" << std::endl;
return result.str();
}
bool operator == (const Matrix & lhs, const Matrix & rhs)
{
return (lhs.a == rhs.a &&
lhs.b == rhs.b &&
lhs.c == rhs.c &&
lhs.d == rhs.d);
}
bool operator != (const Matrix & lhs, const Matrix & rhs)
{
return (lhs.a != rhs.a ||
lhs.b != rhs.b ||
lhs.c != rhs.c ||
lhs.d != rhs.d);
}
Matrix operator + (const Matrix & lhs, const Matrix & rhs)
{
return Matrix(lhs.a + rhs.a, lhs.b + rhs.b, lhs.c + rhs.c, lhs.d + rhs.d);
}
Matrix operator - (const Matrix & lhs, const Matrix & rhs)
{
return lhs + (rhs * -1.0);
}
Matrix operator * (const Matrix & lhs, const Matrix & rhs)
{
return Matrix(lhs.a * rhs.a + lhs.b * rhs.c,
lhs.a * rhs.b + lhs.b * rhs.d,
lhs.c * rhs.a + lhs.d * rhs.c,
lhs.c * rhs.b + lhs.d * rhs.d);
}
Matrix operator * (const Matrix & lhs, const complex <double> & rhs)
{
return Matrix(lhs.a * rhs, lhs.b * rhs, lhs.c * rhs, lhs.d * rhs);
}
Matrix operator / (const Matrix & lhs, const complex <double> & rhs)
{
return Matrix(lhs.a / rhs, lhs.b / rhs, lhs.c / rhs, lhs.d / rhs);
}
Matrix operator * (const complex <double> & lhs, const Matrix & rhs)
{
return Matrix(rhs.a * lhs, rhs.b * lhs, rhs.c * lhs, rhs.d * lhs);
}
Matrix operator - (const Matrix & rhs)
{
return rhs * -1.0;
}
std::ostream & operator << (std::ostream & lhs, const Matrix & rhs)
{
lhs << std::endl << rhs.ToString();
return lhs;
}