/[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 61 - (show annotations) (download) (as text)
Mon Nov 14 02:37:20 2005 UTC (18 years, 10 months ago) by jds
File MIME type: text/x-csrc
File size: 2657 byte(s)
Minor bug fixes, extend unit tests to solver:

minor doc changes - compiler/func.h, general/list.h, solver/mtx.h, utilities/mem.h
solver/example - upgraded examples so they run under current system
solver/slv_common.[ch] - added unit tests, minor bug fixes, extended vector_data functions
utilities/ascDynaLoad.c - bug fix on *nix so dlopen, dlsym not called with NULL arguments
1 #include "utilities/ascConfig.h"
2 #include "solver/mtx.h"
3 #include "solver/linsol.h"
4
5 #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 static linsol_system_t sys;
13 static mtx_matrix_t A;
14 static real64 b[10];
15
16 static make_coefficient_matrix()
17 {
18 mtx_coord_t coord;
19
20 printf("Create A\n");
21 A = mtx_create();
22 mtx_set_order(A,10);
23 printf("\tA[0][0] = 2.0\n");
24 coord.row = 0;
25 coord.col = 0;
26 mtx_set_value(A,&coord,2.);
27 printf("\tA[0][1] = 4.0\n");
28 coord.row = 0;
29 coord.col = 1;
30 mtx_set_value(A,&coord,4.);
31 printf("\tA[1][0] = 1.0\n");
32 coord.row = 1;
33 coord.col = 0;
34 mtx_set_value(A,&coord,1.);
35 printf("\tA[1][1] = 4.0\n");
36 coord.row = 1;
37 coord.col = 1;
38 mtx_set_value(A,&coord,4.);
39 printf("\tA[2][1] = 1.0\n");
40 coord.row = 2;
41 coord.col = 1;
42 mtx_set_value(A,&coord,1.);
43 printf("\tA[2][2] = 1.0\n");
44 coord.row = 2;
45 coord.col = 2;
46 mtx_set_value(A,&coord,1.);
47 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 }
51
52 static make_rhs()
53 {
54 printf("\tb[0] = 8.0\n");
55 printf("\tb[1] = 6.0\n");
56 printf("\tb[2] = 2.5\n");
57 b[0] = 8.0;
58 b[1] = 6.0;
59 b[2] = 2.5;
60
61 linsol_add_rhs(sys,b,FALSE);
62 }
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 printf("x[2] = %f\n",linsol_var_value(sys,b,2));
69 }
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 int main(void)
79 {
80 mtx_region_t region = {0,2,0,2};
81
82 #ifdef __WIN32__
83 Asc_PrintPushVTable(&f_vtable);
84 #endif
85
86 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 linsol_set_matrix(sys,A);
99 printf("Reorder equations\n");
100 linsol_reorder(sys,&region);
101 printf("Invert matrix\n");
102 linsol_invert(sys,&region);
103 printf("Solve system\n");
104 linsol_solve(sys,b);
105
106 printf("Rank = %d\n",linsol_rank(sys));
107
108 print_residuals();
109
110 print_solution();
111
112 #ifdef __WIN32__
113 Asc_PrintRemoveVTable(f_vtable_name);
114 #endif
115
116 return 0;
117 }

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