/[ascend]/trunk/base/generic/solver/example/example1.c
ViewVC logotype

Contents of /trunk/base/generic/solver/example/example1.c

Parent Directory Parent Directory | Revision Log Revision Log


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

john.pye@anu.edu.au
ViewVC Help
Powered by ViewVC 1.1.22