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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 60 by aw0a, Sat Nov 13 16:45:56 2004 UTC revision 61 by jds, Mon Nov 14 02:37:20 2005 UTC
# Line 1  Line 1 
1  #include "base.h"  #include "utilities/ascConfig.h"
2  #include "linsol.h"  #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;  static linsol_system_t sys;
13  static mtx_matrix_t A;  static mtx_matrix_t A;
14  static mtx_number_t b[10];  static real64 b[10];
15    
16  static make_coefficient_matrix()  static make_coefficient_matrix()
17  {  {
18     mtx_coord_t coord;     mtx_coord_t coord;
19    
20     printf("Create A\n");     printf("Create A\n");
21     A = mtx_create();     A = mtx_create();
22     mtx_set_order(A,10);     mtx_set_order(A,10);
23     printf("A[0][0]\n");     printf("\tA[0][0] = 2.0\n");
24     coord.row = 0;     coord.row = 0;
25     coord.col = 0;     coord.col = 0;
26     mtx_set_value(A,&coord,2.);     mtx_set_value(A,&coord,2.);
27     printf("A[0][1]\n");     printf("\tA[0][1] = 4.0\n");
28     coord.row = 0;     coord.row = 0;
29     coord.col = 1;     coord.col = 1;
30     mtx_set_value(A,&coord,4.);     mtx_set_value(A,&coord,4.);
31     printf("A[1][0]\n");     printf("\tA[1][0] = 1.0\n");
32     coord.row = 1;     coord.row = 1;
33     coord.col = 0;     coord.col = 0;
34     mtx_set_value(A,&coord,1.);     mtx_set_value(A,&coord,1.);
35     printf("A[1][1]\n");     printf("\tA[1][1] = 4.0\n");
36     coord.row = 1;     coord.row = 1;
37     coord.col = 1;     coord.col = 1;
38     mtx_set_value(A,&coord,4.);     mtx_set_value(A,&coord,4.);
39       printf("\tA[2][1] = 1.0\n");
40     coord.row = 2;     coord.row = 2;
41     coord.col = 1;     coord.col = 1;
42     mtx_set_value(A,&coord,1.);     mtx_set_value(A,&coord,1.);
43       printf("\tA[2][2] = 1.0\n");
44     coord.row = 2;     coord.row = 2;
45     coord.col = 2;     coord.col = 2;
46     mtx_set_value(A,&coord,1.);     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()  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;     b[0] = 8.0;
58     b[1] = 6.0;     b[1] = 6.0;
59     b[2] = 2.5;     b[2] = 2.5;
60    
61     linsol_add_rhs(sys,b);     linsol_add_rhs(sys,b,FALSE);
62  }  }
63    
64  static print_solution()  static print_solution()
65  {  {
66     printf("x[0] = %f\n",linsol_var_value(sys,b,0));     printf("x[0] = %f\n",linsol_var_value(sys,b,0));
67     printf("x[1] = %f\n",linsol_var_value(sys,b,1));     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()  static print_residuals()
# Line 59  static print_residuals() Line 75  static print_residuals()
75     printf("Residual[2] = %f\n",linsol_eqn_residual(sys,b,2));     printf("Residual[2] = %f\n",linsol_eqn_residual(sys,b,2));
76  }  }
77    
78  main()  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();     sys = linsol_create();
87    
88     printf("Make coefficient matrix\n");     printf("Make coefficient matrix\n");
# Line 73  main() Line 95  main()
95     printf("Number of right hand sides = %d\n"     printf("Number of right hand sides = %d\n"
96           ,linsol_number_of_rhs(sys));           ,linsol_number_of_rhs(sys));
97     printf("Set coefficient matrix\n");     printf("Set coefficient matrix\n");
98     linsol_set_coef_matrix(sys,A);     linsol_set_matrix(sys,A);
99     printf("Reorder equations\n");     printf("Reorder equations\n");
100     linsol_reorder(sys);     linsol_reorder(sys,&region);
101     printf("Solve without inverse\n");     printf("Invert matrix\n");
102     linsol_solve_wo_inverse(sys,b);     linsol_invert(sys,&region);
103       printf("Solve system\n");
104       linsol_solve(sys,b);
105    
106     printf("Rank = %d\n",linsol_rank(sys));     printf("Rank = %d\n",linsol_rank(sys));
107        
108     print_residuals();     print_residuals();
109    
110     print_solution();     print_solution();
 }  
   
   
111    
112    #ifdef __WIN32__
113       Asc_PrintRemoveVTable(f_vtable_name);
114    #endif
115    
116      return 0;
117    }

Legend:
Removed from v.60  
changed lines
  Added in v.61

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