1 |
jds |
61 |
#include "utilities/ascConfig.h" |
2 |
|
|
#include "solver/mtx.h" |
3 |
|
|
#include "solver/linsol.h" |
4 |
aw0a |
1 |
|
5 |
jds |
61 |
#ifdef __WIN32__ |
6 |
|
|
#include "utilities/ascPrintType.h" |
7 |
|
|
#include "utilities/ascPrint.h" |
8 |
|
|
#define f_vtable_name "example_vtable" |
9 |
|
|
static struct Asc_PrintVTable f_vtable = {f_vtable_name, vfprintf, fflush, NULL}; |
10 |
|
|
#endif |
11 |
|
|
|
12 |
aw0a |
1 |
static linsol_system_t sys; |
13 |
|
|
static mtx_matrix_t A; |
14 |
jds |
61 |
static real64 b[10]; |
15 |
aw0a |
1 |
|
16 |
|
|
static make_coefficient_matrix() |
17 |
|
|
{ |
18 |
|
|
mtx_coord_t coord; |
19 |
jds |
61 |
|
20 |
aw0a |
1 |
printf("Create A\n"); |
21 |
|
|
A = mtx_create(); |
22 |
|
|
mtx_set_order(A,10); |
23 |
jds |
61 |
printf("\tA[0][0] = 2.0\n"); |
24 |
aw0a |
1 |
coord.row = 0; |
25 |
|
|
coord.col = 0; |
26 |
|
|
mtx_set_value(A,&coord,2.); |
27 |
jds |
61 |
printf("\tA[0][1] = 4.0\n"); |
28 |
aw0a |
1 |
coord.row = 0; |
29 |
|
|
coord.col = 1; |
30 |
|
|
mtx_set_value(A,&coord,4.); |
31 |
jds |
61 |
printf("\tA[1][0] = 1.0\n"); |
32 |
aw0a |
1 |
coord.row = 1; |
33 |
|
|
coord.col = 0; |
34 |
|
|
mtx_set_value(A,&coord,1.); |
35 |
jds |
61 |
printf("\tA[1][1] = 4.0\n"); |
36 |
aw0a |
1 |
coord.row = 1; |
37 |
|
|
coord.col = 1; |
38 |
|
|
mtx_set_value(A,&coord,4.); |
39 |
jds |
61 |
printf("\tA[2][1] = 1.0\n"); |
40 |
aw0a |
1 |
coord.row = 2; |
41 |
|
|
coord.col = 1; |
42 |
|
|
mtx_set_value(A,&coord,1.); |
43 |
jds |
61 |
printf("\tA[2][2] = 1.0\n"); |
44 |
aw0a |
1 |
coord.row = 2; |
45 |
|
|
coord.col = 2; |
46 |
|
|
mtx_set_value(A,&coord,1.); |
47 |
jds |
61 |
printf("\t{ 2.0 4.0 0.0 }\n"); |
48 |
|
|
printf("\t{ 1.0 4.0 0.0 }\n"); |
49 |
|
|
printf("\t{ 0.0 1.0 1.0 }\n"); |
50 |
aw0a |
1 |
} |
51 |
|
|
|
52 |
|
|
static make_rhs() |
53 |
|
|
{ |
54 |
jds |
61 |
printf("\tb[0] = 8.0\n"); |
55 |
|
|
printf("\tb[1] = 6.0\n"); |
56 |
|
|
printf("\tb[2] = 2.5\n"); |
57 |
aw0a |
1 |
b[0] = 8.0; |
58 |
|
|
b[1] = 6.0; |
59 |
|
|
b[2] = 2.5; |
60 |
|
|
|
61 |
jds |
61 |
linsol_add_rhs(sys,b,FALSE); |
62 |
aw0a |
1 |
} |
63 |
|
|
|
64 |
|
|
static print_solution() |
65 |
|
|
{ |
66 |
|
|
printf("x[0] = %f\n",linsol_var_value(sys,b,0)); |
67 |
|
|
printf("x[1] = %f\n",linsol_var_value(sys,b,1)); |
68 |
jds |
61 |
printf("x[2] = %f\n",linsol_var_value(sys,b,2)); |
69 |
aw0a |
1 |
} |
70 |
|
|
|
71 |
|
|
static print_residuals() |
72 |
|
|
{ |
73 |
|
|
printf("Residual[0] = %f\n",linsol_eqn_residual(sys,b,0)); |
74 |
|
|
printf("Residual[1] = %f\n",linsol_eqn_residual(sys,b,1)); |
75 |
|
|
printf("Residual[2] = %f\n",linsol_eqn_residual(sys,b,2)); |
76 |
|
|
} |
77 |
|
|
|
78 |
jds |
61 |
int main(void) |
79 |
aw0a |
1 |
{ |
80 |
jds |
61 |
mtx_region_t region = {0,2,0,2}; |
81 |
|
|
|
82 |
|
|
#ifdef __WIN32__ |
83 |
|
|
Asc_PrintPushVTable(&f_vtable); |
84 |
|
|
#endif |
85 |
|
|
|
86 |
aw0a |
1 |
sys = linsol_create(); |
87 |
|
|
|
88 |
|
|
printf("Make coefficient matrix\n"); |
89 |
|
|
make_coefficient_matrix(); |
90 |
|
|
printf("Non-zeros in row 0 = %d\n", |
91 |
|
|
mtx_nonzeros_in_row(A,0,mtx_ALL_COLS)); |
92 |
|
|
printf("Make right hand side\n"); |
93 |
|
|
make_rhs(); |
94 |
|
|
|
95 |
|
|
printf("Number of right hand sides = %d\n" |
96 |
|
|
,linsol_number_of_rhs(sys)); |
97 |
|
|
printf("Set coefficient matrix\n"); |
98 |
jds |
61 |
linsol_set_matrix(sys,A); |
99 |
aw0a |
1 |
printf("Reorder equations\n"); |
100 |
jds |
61 |
linsol_reorder(sys,®ion); |
101 |
|
|
printf("Invert matrix\n"); |
102 |
|
|
linsol_invert(sys,®ion); |
103 |
|
|
printf("Solve system\n"); |
104 |
|
|
linsol_solve(sys,b); |
105 |
aw0a |
1 |
|
106 |
|
|
printf("Rank = %d\n",linsol_rank(sys)); |
107 |
|
|
|
108 |
|
|
print_residuals(); |
109 |
|
|
|
110 |
|
|
print_solution(); |
111 |
|
|
|
112 |
jds |
61 |
#ifdef __WIN32__ |
113 |
|
|
Asc_PrintRemoveVTable(f_vtable_name); |
114 |
|
|
#endif |
115 |
aw0a |
1 |
|
116 |
jds |
61 |
return 0; |
117 |
|
|
} |