-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray2d.edp
198 lines (194 loc) · 3.68 KB
/
array2d.edp
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
196
197
198
// array2d.edp
//
// Discussion:
//
// Demonstrate how 2D matrices can be defined and manipulated.
//
// Location:
//
// http://people.sc.fsu.edu/~jburkardt/freefem++/array2d/array2d.edp
//
// Modified:
//
// 02 July 2015
//
// Author:
//
// John Burkardt
//
cout << "\n";
cout << "array2d:\n";
cout << " FreeFem++ version\n";
cout << " Demonstrate how 2D matrices can be defined and manipulated.\n";
load "lapack"
//
// Define a 3x3 matrix A
//
// [ 0 1 0 ]
// [ 0 0 1 ]
// [ 1 1 1 ]
//
real[int,int] A(3,3);
A = 1.0;
A(0,0) = 0.0;
A(0,2) = 0.0;
A(1,0) = 0.0;
A(1,1) = 0.0;
cout << "\n";
cout << " Matrix A = " << A << endl;
//
// Define a 3x3 matrix B:
//
// [ 3 1 1 ]
// [ 1 1 3 ]
// [ 3 3 3 ]
//
real[int,int] B(3,3);
//B = 3.0;
//B(0,1) = 1.0;
//B(0,2) = 1.0;
//B(1,0) = 1.0;
//B(1,0) = 1.0;
B = [ [ 3.0, 1.0, 1.0 ], [ 1.0, 1.0, 3.0 ], [ 3.0, 3.0, 3.0 ] ];
cout << "\n";
cout << " Matrix B = " << B << endl;
//
// Compute A*B.
// This operation requires the "load lapack" statement above.
//
real[int,int] AxB(3,3);
AxB = A * B;
cout << "\n";
cout << " Matrix AxB = product A*B = " << AxB << endl;
//
// Compute A transpose.
//
real[int,int] AT(3,3);
AT = A';
cout << "\n";
cout << " Matrix AT = A' = transpose A' " << AT << endl;
//
// Define a matrix C.
//
// C = 1 1 0 1
// 1 1 1 1
// 1 0 1 1
//
real[int,int] C(3,4);
C = 1.0;
C(2,1) = 0.0;
C(0,2) = 0.0;
cout << "\n";
cout << " Matrix C = " << C << endl;
cout << " First row of C = C(0,:) = " << C(0,:) << endl;
//
// Define a vector d, and copy row 0 of C into it.
//
real[int] d(4);
d = C(0,:);
cout << "\n";
cout << " d = C(0,:)\n";
cout << " Size of d = d.n = " << d.n << "\n";
cout << " Entries of d = " << d << "\n";
//
// Define vectors e and f, and create a matrix G as their outer product.
//
real[int] e(3);
e=[1,2,3];
real[int] f(4);
f=[4,5,6,7];
real[int,int] G(3,4);
G = e * f';
cout << "\n";
cout << " Vector e = " << e << "\n";
cout << " Vector f = " << f << "\n";
cout << " Matrix G = e * f' = " << G << "\n";
//
// Define vectors h and i, and use them to pre- and post-multiply
// the 3x4 matrix C.
//
real[int] h(3);
h = [ 1.0, 1.0, 1.0 ];
real[int] i(4);
i = [ 1.0, 1.0, 1.0, 1.0 ];
real[int] j(4);
j = h' * C;
real[int] k(3);
k = C * i;
cout << "\n";
cout << " Premultiply j = h' * C = " << j << "\n";
cout << " Postmultiply k = C * i = " << k << "\n";
//
// (Rectangular) Matrix multiplication.
//
real[int,int] L(3,4);
L = A * G;
cout << "\n";
cout << " L = A * G = " << L << endl;
//
// Matrix addition.
//
real[int,int] M(3,3);
M = A + B;
cout << "\n";
cout << " M = A + B = " << M << endl;
//
// Scalar multiplication.
//
M = 2 * M;
cout << " M = 2 * M = " << M << endl;
//
// Scalar addition.
//
//M = M .+ 10;
//cout << " M = M + 10 = " << M << endl;
//
// Elementwise multiplication.
//
M = A .* B;
cout << " M = A .* B = " << M << endl;
//
// Elementwise division
//
M = A ./ B;
cout << " M = A ./ B = " << M << endl;
//
// Inverse.
//
M = A ^ -1;
cout << " M = A ^ -1 = " << M << endl;
//
// Dimensions.
//
cout << " Dimensions of A = (A.m,A.n) = " << A.m << "," << A.n << "\n";
//
// Solution operator is multiplication by inverse:
//
real[int] x1(3);
real[int] x2(3);
real[int] y2(3);
real[int,int] Ainv(3,3);
x1 = [ 1, 2, 3];
cout << "\n";
cout << " Solution X1 = " << x1 << "\n";
y2 = A * x1;
cout << " Y2 = A * X1 = " << y2 << "\n";
//x2 = ( A ^ -1 ) * y2;
Ainv = ( A ^ - 1 );
x2 = Ainv * y2;
cout << " X2 = A^-1 * Y2 = " << x2 << "\n";
//
// Maximum, doesn't work!
//
//cout << " max ( M ) = " << max ( M ) << "\n";
//
// Norms? Doesn't work.
//
//cout << || M ||_1 << "\n";
//
// Terminate.
//
cout << "\n";
cout << "array2d:\n";
cout << " Normal end of execution.\n";
exit ( 0 );