-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdemo_bla.cpp
More file actions
137 lines (96 loc) · 2.92 KB
/
Copy pathdemo_bla.cpp
File metadata and controls
137 lines (96 loc) · 2.92 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
/*
Basic Linear Algebra demos
*/
//g++ demo_bla.cpp bla/calcinverse.cpp bla/exception.cpp bla/localheap.cpp -std=c++11 -o demo_bla -fmax-errors=2 -fdiagnostics-color=auto
// ng-soft header files
#include "bla/bla.hpp"
//using namespace std;
using namespace ngbla;
int main ()
{
Vector<double> u(3), v(5);
Matrix<double> m(5,3);
u = 1.0;
for (int i = 0; i < Height(m); i++)
for (int j = 0; j < Width(m); j++)
m(i,j) = i+j;
// do some math:
v = m * u;
cout << "u = " << endl << u << endl;
cout << "v = " << endl << v << endl;
cout << "v.range(1,3) = " << endl << v.Range(1, 3) << endl;
cout << "m = " << endl << m << endl;
// use result directly
cout << "Trans(m) * v = " << endl << Trans(m) * v << endl;
// fix-size objects:
Vec<3,double> u3;
Mat<3,3,double> m3;
u3 = 2.7;
m3 = 1.0;
cout << "m3 * u3 = " << m3 * u3 << endl;
{
// C++11 : initializer lists
Vector<> x = { 1, 2, 3 };
Matrix<> m = { { 2, 2, 2 }, { 5, 5, 5 } };
Vector<> y = m * x;
cout << "x = " << x << endl;
cout << "m = " << m << endl;
cout << "y = " << y << endl;
}
// own memory management:
double data[1000];
FlatVector<double> fu(100, data);
FlatVector<double> fv(100, data+100);
// overlay
FlatVector<double> fw(200, data);
fu = 1.0;
fv = fu;
cout << "(u,v) = " << InnerProduct (fu, fv) << endl;
cout << "(w,w) = " << InnerProduct (fw, fw) << endl;
// access row/col of matrix:
cout << "Row 2 of m is " << endl << m.Row(2) << endl;
cout << "type is " << typeid(m.Row(2)).name() << endl;
cout << "Col 1 of m is " << endl << m.Col(1) << endl;
cout << "type is " << typeid(m.Col(1)).name() << endl;
// more complicated vectors
Vector<Vec<2,Complex> > sysu(4);
Vector<Vec<3,Complex> > sysv(3);
Matrix<Mat<3,2,Complex> > sysm(3,4);
for (int i = 0; i < sysu.Size(); i++)
{
sysu(i)(0) = Complex (1,0);
sysu(i)(1) = Complex (0,1);
}
cout << "sysu = " << sysu << endl;
for (int i = 0; i < sysm.Height(); i++)
for (int j = 0; j < sysm.Width(); j++)
{
sysm(i,j) = 0.0;
sysm(i,j)(1,0) = 2.5;
}
cout << "sysm = " << sysm << endl;
sysv = sysm * sysu;
cout << "sysv = " << sysv << endl;
Vector<double> a(1000);
Vector<double> b(1000);
a = 1.0;
b = 1.0;
double sum = 0;
for (int i = 0; i < 1000000; i++)
sum += InnerProduct (a, b);
cout << "sum = " << sum << endl;
// Vector<Complex> ac(1000);
// Vector<Complex> bc(1000);
Vector<complex<double> > ac(1000);
Vector<complex<double> > bc(1000);
// Vector<Vec<2> > ac(1000);
// Vector<Vec<2> > bc(1000);
ac = 1.0;
bc = 1.0;
Complex sumc = 0;
for (int i = 0; i < 100000; i++)
sumc += InnerProduct (ac, bc);
cout << "sum = " << sumc << endl;
//cout << "n! = " << math.<< endl;
return 0;
}