-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblatt08.cpp
117 lines (89 loc) · 2.44 KB
/
blatt08.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
#include <iostream>
#include <iomanip>
#include <valarray>
using namespace std;
typedef valarray<double> Vector;
template <typename T> constexpr
int msgn(T const & t) { return (t < 0) ? -1 : 1; }
class Matrix
{
private:
Vector vec;
public:
const unsigned n;
Matrix(unsigned dim) : n(dim), vec(dim*dim) { }
static Matrix I(unsigned dim)
{
Matrix m(dim);
for (unsigned i = 0; i < m.n * m.n; i += m.n+1) m.vec[i] = 1.0;
return m;
}
double & operator()(int i, int j) { return vec[i + n*j]; }
double const & operator()(int i, int j) const { return vec[i + n*j]; }
slice ind_z(unsigned i) const { return slice(n*i, n, 1 ); }
slice ind_s(unsigned j) const { return slice( j, n, n ); }
slice ind_d() const { return slice( 0, n, n+1); }
friend bool jacobi(Matrix const & a, Vector & ew, Matrix & ev, int maxzykl, double eps);
void print(ostream & os) const
{
for (int i = 0; i < n; ++i)
{
for (int j = 0; j < n; ++j)
{
os << setw(7) << (*this)(i, j) << ' ';
}
os << endl;
}
}
};
bool jacobi(Matrix a, Vector & ew, Matrix & ev, int maxzykl, double eps)
{
for (int i = 0; i < a.n; ++i)
{
for (int j = 0; j < a.n; ++j)
{
double s = 0.0, c = 1.0;
if (a(i, j) != 0)
{
double tau = (a(j, j) - a(i, i)) / (2.0 * a(i, j));
double t = msgn(tau) / (abs(tau) * sqrt(1+tau*tau));
c = 1.0 / sqrt(1.0 + t*t);
s = c * t;
}
//Soll: a(i, j) = a(j, i) = 0.0;
Vector spalte_i = a.vec[ind_s(i)];
spalte_i *= c;
spalte_i -= s * a.vec[ind_s(j)];
Vector spalte_j = a.vec[ind_s(j)];
spalte_j *= c;
spalte_j += s * a.vec[ind_s(i)];
a.vec[ind_s(i)] = spalte_i;
a.vec[ind_s(j)] = spalte_j;
// a equals A'
Vector zeile_i = a.vec[ind_z(i)];
zeile_i *= c;
zeile_i -= s * a.vec[ind_z(j)];
Vector zeile_j = a.vec[ind_z(j)];
zeile_j *= c;
zeile_j += s * a.vec[ind_z(i)];
a.vec[ind_z(i)] = zeile_i;
a.vec[ind_z(j)] = zeile_j;
// a equals a_neu
}
}
ew = a.vec[ind_d()];
return true;
}
int main()
{
/*
Matrix m(2);
cout << m.n << 'x' << m.n << " Matrix eingeben:" << endl;
for (int i = 0; i < m.n; ++i)
for (int j = 0; j < m.n; ++j)
cin >> m(i, j);
m.print(cout);
*/
Matrix i = Matrix::I(3);
i.print(cout);
}