File: examples.c

package info (click to toggle)
libwn6 6.0-3
  • links: PTS
  • area: main
  • in suites: potato
  • size: 5,996 kB
  • ctags: 3,938
  • sloc: ansic: 45,083; makefile: 926; csh: 274; sh: 12
file content (268 lines) | stat: -rw-r--r-- 4,804 bytes parent folder | download | duplicates (4)
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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/****************************************************************************

COPYRIGHT NOTICE:

  The source code in this file is provided free of charge
  to the author's consulting clients.  It is in the
  public domain and therefore may be used by anybody for
  any purpose.

AUTHOR:

  Will Naylor

****************************************************************************/
#include "wnlib.h"
#include "wnmat.h"
#include "wncplx.h"



main()
{
  /*
  test_inverse();
  test_ls_inverse();
  test_simplex_method();
  */
  test_fft();
}



local test_inverse()
{
  wn_matrix mat,mati,prod;
  int code;

  wn_gpmake("no_free");    /* temp memory group */
  wn_gplabel("main group");

  create_matrix1(&mat);
  printf("matrix made\n");

  wn_print_matrix(mat);
  wn_invert_matrix(&code,&mati,mat);

  printf("code = %d\n",code);
  wn_print_matrix(mati);

  wn_matrix_multiply_matrix(&prod,mat,mati);
  printf("product\n");
  wn_print_matrix(prod);

  wn_gpfree();
}



local create_matrix1(pmat)

wn_matrix *pmat;

{
  wn_enter_matrix(pmat);
  /*
  random_matrix(pmat,4,4,-10.0,10.0);
  random_matrix(pmat,200,200,-10.0,10.0);
  random_matrix(pmat,100,100,-10.0,10.0);
  random_matrix(pmat,400,400,-10.0,10.0);
  */
}



local test_ls_inverse()
{
  wn_matrix mat,mati,prod;
  wn_vector vect,coefs;
  int code;

  wn_gpmake("no_free");    /* temp memory group */
  wn_gplabel("main group");

  printf("mat:\n");
  wn_enter_matrix(&mat);
  printf("vect:\n");
  wn_enter_vector(&vect);

  printf("mat:\n");
  wn_print_matrix(mat);
  printf("vect:\n");
  wn_print_vector(vect);

  wn_ls_invert_matrix(&code,&mati,mat);

  printf("code = %d\n",code);
  wn_print_matrix(mati);

  wn_matrix_multiply_matrix(&prod,mati,mat);
  printf("product\n");
  wn_print_matrix(prod);

  wn_matrix_multiply_vector(&coefs,mati,vect);
  printf("coefs\n");
  wn_print_vector(coefs);

  wn_gpfree();
}



local test_simplex_method()
{
  double objective;
  wn_vector objective_vect,right_side,solution,shadow_prices;
  wn_matrix mat;
  int code;

  wn_gpmake("no_free");    /* temp memory group */
  wn_gplabel("main group");

  create_problem(&objective_vect,&right_side,&mat);

  printf("objective_vect:\n");
  wn_print_vector(objective_vect);
  printf("matrix:\n");
  wn_print_matrix(mat);
  printf("right_side:\n");
  wn_print_vector(right_side);

  wn_simplex_method(&code,&objective,&shadow_prices,&solution,
                    objective_vect,mat,right_side);

  printf("code = %d\n",code);

  printf("objective = %f\n",objective);

  printf("solution:\n");
  wn_print_vector(solution);

  printf("shadow_prices:\n");
  wn_print_vector(shadow_prices);

  wn_gpfree();
}



local create_problem(pobjective_vect,pright_side,pmat)

wn_vector *pobjective_vect,*pright_side;
wn_matrix *pmat;

{
  random_simplex_problem(pobjective_vect,pright_side,pmat,8,17);
  /*
  enter_problem(pobjective_vect,pright_side,pmat);
  random_simplex_problem(pobjective_vect,pright_side,pmat,50,100);
  random_simplex_problem(pobjective_vect,pright_side,pmat,50,100);
  random_simplex_problem(pobjective_vect,pright_side,pmat,100,200);
  create_assignment_problem(pobjective_vect,pright_side,pmat);
  */
}



local enter_problem(pobjective_vect,pright_side,pmat)

wn_vector *pobjective_vect,*pright_side;
wn_matrix *pmat;

{
  printf("objective:\n");
  wn_enter_vector(pobjective_vect);
  printf("matrix:\n");
  wn_enter_matrix(pmat);
  printf("right_side:\n");
  wn_enter_vector(pright_side);
}



local create_assignment_problem(pobjective_vect,pright_side,pmat)

wn_vector *pobjective_vect,*pright_side;
wn_matrix *pmat;

{
  wn_matrix assignment_mat;

  wn_enter_matrix(&assignment_mat);
  /*
  random_matrix(&assignment_mat,10,10,0.0,10.0);
  random_matrix(&assignment_mat,4,4,0.0,10.0);
  */

  printf("assignment_mat:\n");
  wn_print_matrix(assignment_mat);

  make_assignment_problem(pobjective_vect,pright_side,pmat,assignment_mat);
}



local test_fft()
{
  extern double wn_flat_distribution();
  wn_cplx *vector;
  int i,len_i;

  wn_gpmake("no_free");    /* temp memory group */

  len_i = 1<<18;
  /*
  len_i = 1<<3;
  */

  wn_cplx_make_raw_vector(&vector,len_i);

  for(i=0;i<len_i;++i)
  {
    /*
    printf("i=%d\n",i);
    wn_cplx_enter(vector[i]);
    vector[i]->real = (i < 5);
    */
    vector[i]->real = wn_flat_distribution();
    vector[i]->imag = 0.0;
  }

  /*
  */
  wn_fft_raw_vector(vector,len_i);

  /*
  cplx_print_vector(vector,len_i);

  wn_fft_raw_vector(vector,len_i);

#if 0
  cplx_print_vector(vector,len_i);
#endif

  wn_inverse_sft_raw_vector(vector,len_i);

  cplx_print_vector(vector,len_i);
  */
}



local cplx_print_vector(vector,len_i)

wn_cplx vector[];
int len_i;

{
  int i;

  for(i=0;i<len_i;++i)
  {
    printf("vector[%d] = ",i);
    wn_cplx_print(vector[i]);
    printf("\n");
  }
}