-
Notifications
You must be signed in to change notification settings - Fork 0
/
mls.c
2186 lines (1800 loc) · 69.1 KB
/
mls.c
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "linalg.h"
#include "combinadic.h"
#include "trees.h"
#include "math_config.h"
typedef struct
{
/**
* The number of points used in the interpolation;
* this is also the number of interpolating basis
* functions used in the reconstruction
*/
int np;
/**
* The dimension of the points given as input
*/
int dim;
/**
* The degree of interpolating polynomial to seek as
* the weighted reconstruction
*/
int deg;
/**
* The list of points used in the interpolation
*/
double *pts;
/**
* The dilation parameters for each point in pts
*/
double *dlts;
/**
* The list of function values at their corresponding
* point in pts; these can be changed arbitrarily during
* the process of evaluating the interpolant; this is a
* single real value corresponding to each dim-dimensional
* point in pts
*/
double *vals;
/**
* Function pointer to the weight function; this function
* is shifted to properly center in about each interpolating
* point stored in pts; the first argument is the dimension;
* the second is the actual point, which must be centered on
* the appropriate point by the user, at which to evaluate
*/
double (*wfs)(int,double,double*); /* Add another double to the arg list for setting the local radius of the basis function */
} mls_t;
typedef struct
{
/**
* The number of interpolation points, i.e. number of
* basis functions used to approximate a function
*/
int np;
/**
* The dimension of the domain
*/
int dim;
/**
* The degree of the local basis to generate
*/
int deg;
/**
* Pointer to the list of vertices
*/
double *pts;
/**
* List of indexes into pts corresponding to boundary
* particles; not yet used, but implement somehow soon
*/
int *bpts;
/**
* The dilation parameters for each point in pts
*/
double *dlts;
/**
* Values of the interpolant at the desired function
* points stored in pts
*/
double *vals;
/**
* Global quadrature weights used for evaluating the
* interpolant; there are as many values in gqw as
* there are points in pts
*/
double *gqw;
/**
* Pointer to the weight functions prototype to use
*/
double (*wfs)(int,double,double*);
/**
* Pointer to function which returns the gradient of
* the weight functions; just for testing purposes
*/
void (*wfsg)(int,double,double*,double*);
/**
* Radius of the function wfs when a_in is unity
*/
double wrad;
/**
* The Gram matrix defining the reproducing kernel of
* the given desired order using the window function
* given as wfs_in
*/
double *grm;
/**
* Use this space to stored the LU-decomposed grm matrix
* internally; using this space so as not to disturb the
* original undilated Gram matrix
*/
double *grmlu;
/**
* Store the coefficients here for the correction function;
* this is currently used for only one correction function,
* but must store np * pdim coefficients; assumes all particle
* basis functions have the same polynomial order
*/
double *coeffs; /* Adjust this to be used as the storage for the INTERNAL basis polynomial coefficients */
/**
* Wavelet coefficients generated by wavelet generator
*/
double *wcoeffs; /* Do something similar here */
/**
* A multi-use mode specifier
*/
int mode;
} rkp_t;
/**
* Set up the data set and parameters for use in building the interpolant;
* right now this just copies the input parameters into the mls_t struct;
* maybe we can do without the mls_t struct all together and just use arrays
* directly
*/
int mls_init( mls_t *obj_in, int np_in, int dim_in, int deg_in, double *pts_in, double *dlts_in, double *vals_in, double (*wfs_in)(int,double,double*) )
{
obj_in->np = np_in;
obj_in->dim = dim_in;
obj_in->deg = deg_in;
obj_in->pts = pts_in; /* Passing by reference here; do not free pts_in until finished */
obj_in->dlts = dlts_in; /* Passing the dilation parameters */
obj_in->vals = vals_in; /* Same here */
obj_in->wfs = wfs_in;
return 0;
}
/**
* This function initializes the system by doing the integrals using Monte Carlo
* averaging; this is not the best way to do this, but it will work as
* an optional method; this is why this function ends in mc, indicating the
* initialization using Monte Carlo instead of a systematic quadrature
* @param obj_in The reproducing kernel particle object
* @param np_in The number of points about which basis particles are placed
* @param dim_in The dimension of the vertices
* @param deg_in The order of the polynomial space to use to generate the moment matrix
* @param pts_in The set of interpolation points at which to place the RKPs
* @param vals_in The values of the interpolant at each entry in pts_in
* @param wfs_in The pointer to the window function to use for the system
* @param rad_in This is the radius of the window function defining the support of the local function
* @return Returns 0 if all is well or -1 if some error
*/
int rkp_init( rkp_t *obj_in, int np_in, int dim_in, int deg_in, double *pts_in, double *dlts_in, double *gqw_in, double *vals_in, double (*wfs_in)(int,double,double*), double wrad_in )
{
int i;
obj_in->np = np_in;
obj_in->dim = dim_in;
obj_in->deg = deg_in;
obj_in->pts = (double*) malloc( dim_in * np_in * sizeof(double) );
if( obj_in->pts == NULL )
return -1;
for(i=0;i<dim_in*np_in;i++)
obj_in->pts[i] = pts_in[i];
obj_in->dlts = (double*) malloc( np_in * sizeof(double) );
if( obj_in->dlts == NULL )
return -1;
for(i=0;i<np_in;i++)
obj_in->dlts[i] = dlts_in[i];
obj_in->vals = (double*) malloc( np_in * sizeof(double) );
if( obj_in->vals == NULL )
return -1;
for(i=0;i<np_in;i++)
obj_in->vals[i] = vals_in[i];
obj_in->gqw = (double*) malloc( np_in * sizeof(double) );
if( obj_in->gqw == NULL )
return -1;
for(i=0;i<np_in;i++)
obj_in->gqw[i] = gqw_in[i];
obj_in->wfs = wfs_in;
obj_in->wrad = wrad_in;
obj_in->mode = 0;
int n = binomial( dim_in + deg_in, dim_in );
obj_in->grm = (double*) malloc( n * n * sizeof(double) ); /* Stores a matrix */
obj_in->grmlu = (double*) malloc( n * n * sizeof(double) ); /* Stores a matrix */
obj_in->coeffs = (double*) malloc( np_in * n * sizeof(double) ); /* Stores a vector */
obj_in->wcoeffs = (double*) malloc( np_in * n * ( n - 1 ) * sizeof(double) ); /* Stores higher order wavelet coefficients */
if( obj_in->grm == NULL || obj_in->grmlu == NULL || obj_in->coeffs == NULL || obj_in->wcoeffs == NULL )
return -1;
return 0;
}
int mls_free( mls_t *obj_in )
{
return 0;
}
int rkp_free( rkp_t *obj_in )
{
if( obj_in->vals != NULL )
free( obj_in->vals );
if( obj_in->pts != NULL )
free( obj_in->pts );
if( obj_in->dlts != NULL )
free( obj_in->dlts );
if( obj_in->gqw != NULL )
free( obj_in->gqw );
if( obj_in->grm != NULL )
free( obj_in->grm );
if( obj_in->grmlu != NULL )
free( obj_in->grmlu );
if( obj_in->coeffs != NULL )
free( obj_in->coeffs );
if( obj_in->wcoeffs != NULL )
free( obj_in->wcoeffs );
return 0;
}
/**
* This function evaluate the polynomial basis vector at the specified
* point given in x_in and outputs the elements to vec_out, which is
* presumed to have enough space already allocated.
* @param obj_in The MLS object
* @param x_in The point at which to evaluate the basis; should have same dimension as mls_t
* @param vec_out The space to which the p-vector is output; presumed allocated; will be assumed to have C(deg+dim,dim) entries
* @return Returns 0 if all is well otherwise < 0
*/
int mls_basis_evaluate( mls_t *obj_in, double *x_in, double *vec_out ) /* TODO: Pre-allocate cmb and occ so we don't have to do it repeatedly */
{
int i,j,k,m,n,*cmb,*occ;
int pdim = binomial( obj_in->deg + obj_in->dim, obj_in->dim ); /* Make sure vec_out points to at least this many doubles */
double prd;
/* Allocate space for iterating combinations */
cmb = (int*) malloc( obj_in->dim * sizeof(int) ); /* Only really need obj_in->dim - 1 but padding won't hurt */
occ = (int*) malloc( obj_in->dim * sizeof(int) );
for(i=0,m=0;i<=obj_in->deg;i++)
{
n = binomial( i + obj_in->dim - 1, obj_in->dim - 1 );
rcombinadic_init( i, obj_in->dim, cmb );
for(j=0;j<n;j++)
{
rcombinadic_occupancy( i, obj_in->dim, cmb, occ );
prd = 1.0;
for(k=0;k<obj_in->dim;k++)
prd *= pow( x_in[k], (double) occ[k] );
vec_out[m++] = prd; /* Place each entry following those from its previous degrees continuously */
rcombinadic_next( i, obj_in->dim, cmb );
}
}
free( cmb );
free( occ );
return 0;
}
/**
* This function evaluate the important A(x) matrix which must be inverted
* in order to generate the global interpolant via the nodal basis functions
* @param obj_in The MLS object
* @param x_in The point at which to evaluate the matrix
* @param mat_out The matrix as output; make sure this is allocated with enough space
* @return Returns 0 if all went well
*/
int mls_matrix_evaluate( mls_t *obj_in, double dlt_in, double *x_in, double *mat_out )
{
int i,j,k,n,p,pdim;
double *x,*vec;
/* Allocate the right size of vector for output from mls_basis_evaluate */
pdim = binomial( obj_in->deg + obj_in->dim, obj_in->dim );
vec = (double*) malloc( pdim * sizeof(double) );
/* Allocate a temporary dim-vector */
x = (double*) malloc( obj_in->dim * sizeof(double) );
/* Zero the matrix before forming the sum of outer products */
for(i=0;i<pdim*pdim;i++)
mat_out[i] = 0.0;
/* Sum all outer products pj pj^T */
for(i=0;i<obj_in->np;i++)
{
/* Evaluate the basis vector at each value of i */
mls_basis_evaluate( obj_in, obj_in->pts + i * obj_in->dim, vec );
/* Function obj_in->wfs at point i is obtained by translating it to be centered at obj_in->pts[i*obj_in->dim+...] */
for(j=0;j<obj_in->dim;j++)
x[j] = x_in[j] - obj_in->pts[i*obj_in->dim+j];
/* Fill in only the upper triangular first; then copy to the lower */
for(j=0;j<pdim;j++)
for(k=j;k<pdim;k++)
mat_out[j*pdim+k] += vec[j] * vec[k] * obj_in->wfs( obj_in->dim, dlt_in, x );
}
/* Copy the upper triangular part of mat_out into the lower triangular */
for(j=0;j<pdim;j++)
for(k=0;k<j;k++)
mat_out[j*pdim+k] = mat_out[k*pdim+j];
free( x );
free( vec );
return 0;
}
/**
* This is where the major difference between RKP and MLS becomes
* evident; this function will use Monte Carlo integration if the
* option is chosen during initialization, or it will use a basic
* stencil method based on polynomials if it is so initialized;
* this matrix is also called the moment matrix in literature
* @param obj_in The RKP object
* @param x_in The point at which to evaluate the matrix
* @return Returns 0 if all went well
*/
int rkp_matrix_evaluate( rkp_t *obj_in, double *x_in )
{
int i,j,k,m,n,p,nq,ret,pdim,*cmb,*occ,*ord;
double prd,*x,*vec;
/* Set up iteration through monomials */
cmb = (int*) malloc( obj_in->dim * sizeof(int) ); /* Only really need obj_in->dim - 1 but padding won't hurt */
occ = (int*) malloc( obj_in->dim * sizeof(int) );
/* Allocate the right size of vector for output from mls_basis_evaluate */
pdim = binomial( obj_in->deg + obj_in->dim, obj_in->dim );
/* Allocate a temporary dim-vector */
x = (double*) malloc( obj_in->dim * sizeof(double) );
vec = (double*) malloc( pdim * sizeof(double) );
/* Zero the matrix before forming the sum of outer products */
for(i=0;i<pdim*pdim;i++)
obj_in->grm[i] = 0.0;
/* Just iterate through all nodes */
for(i=0;i<obj_in->np;i++)
{
/* Generate the point */
for(j=0;j<obj_in->dim;j++)
x[j] = obj_in->pts[i*obj_in->dim+j] - x_in[j];
/* Generate the polynomial vector */
for(p=0,j=0;j<=obj_in->deg;j++)
{
rcombinadic_init( j, obj_in->dim, cmb );
n = binomial( j + obj_in->dim - 1, obj_in->dim - 1 );
for(k=0;k<n;k++)
{
rcombinadic_occupancy( j, obj_in->dim, cmb, occ );
prd = 1.0;
for(m=0;m<obj_in->dim;m++)
prd *= pow( x[m], (double) occ[m] );
vec[p++] = prd;
rcombinadic_next( j, obj_in->dim, cmb );
}
}
/* Now form the outer product */
for(j=0;j<pdim;j++)
{
for(k=j;k<pdim;k++)
{
prd = vec[j] * vec[k] * obj_in->wfs( obj_in->dim, obj_in->dlts[i], x );
obj_in->grm[j*pdim+k] += obj_in->gqw[i] * prd;
}
}
}
/* Copy into lower triangular area */
for(i=0;i<pdim;i++)
for(j=0;j<i;j++)
obj_in->grm[i*pdim+j] = obj_in->grm[j*pdim+i];
free( cmb );
free( occ );
free( x );
free( vec );
return 0;
}
/**
* Does the same thing as rkp_matrix_evaluate with the
* exception that the x_in values are scaled by the dilation
* parameters of each local particle
* @param obj_in RKP object
* @param x_in Point at which to evaluate moments
*/
int rkp_matrix_evaluate_scaled( rkp_t *obj_in, double *x_in )
{
int i,j,k,m,n,p,nq,ret,pdim,*cmb,*occ,*ord;
double prd,*x,*vec;
/* Set up iteration through monomials */
cmb = (int*) malloc( obj_in->dim * sizeof(int) ); /* Only really need obj_in->dim - 1 but padding won't hurt */
occ = (int*) malloc( obj_in->dim * sizeof(int) );
/* Allocate the right size of vector for output from mls_basis_evaluate */
pdim = binomial( obj_in->deg + obj_in->dim, obj_in->dim );
/* Allocate a temporary dim-vector */
x = (double*) malloc( obj_in->dim * sizeof(double) );
vec = (double*) malloc( pdim * sizeof(double) );
/* Zero the matrix before forming the sum of outer products */
for(i=0;i<pdim*pdim;i++)
obj_in->grm[i] = 0.0;
/* Just iterate through all nodes */
for(i=0;i<obj_in->np;i++)
{
/* Generate the point */
for(j=0;j<obj_in->dim;j++)
x[j] = obj_in->pts[i*obj_in->dim+j] - x_in[j];
/* Generate the polynomial vector */
for(p=0,j=0;j<=obj_in->deg;j++)
{
rcombinadic_init( j, obj_in->dim, cmb );
n = binomial( j + obj_in->dim - 1, obj_in->dim - 1 );
for(k=0;k<n;k++)
{
rcombinadic_occupancy( j, obj_in->dim, cmb, occ );
prd = 1.0;
for(m=0;m<obj_in->dim;m++)
prd *= pow( x[m] / obj_in->dlts[i], (double) occ[m] );
vec[p++] = prd;
rcombinadic_next( j, obj_in->dim, cmb );
}
}
/* Now form the outer product */
for(j=0;j<pdim;j++)
{
for(k=j;k<pdim;k++)
{
prd = vec[j] * vec[k] * obj_in->wfs( obj_in->dim, obj_in->dlts[i], x );
obj_in->grm[j*pdim+k] += obj_in->gqw[i] * prd;
}
}
}
/* Copy into lower triangular area */
for(i=0;i<pdim;i++)
for(j=0;j<i;j++)
obj_in->grm[i*pdim+j] = obj_in->grm[j*pdim+i];
free( cmb );
free( occ );
free( x );
free( vec );
return 0;
}
/**
* Use this function to deal with widely varying particle sizes
* when generating wavelets for use in evaluating derivatives
* @param obj_in RKP object to use
* @param x_in Point at which to evaluate the moments
* @param rho_in Scale on which to evaluate the moments
* @return Returns 0 if all is well
*/
int rkp_matrix_evaluate_scaled_const( rkp_t *obj_in, double *x_in, double rho_in )
{
int i,j,k,m,n,p,nq,ret,pdim,*cmb,*occ,*ord;
double prd,*x,*vec;
/* Set up iteration through monomials */
cmb = (int*) malloc( obj_in->dim * sizeof(int) ); /* Only really need obj_in->dim - 1 but padding won't hurt */
occ = (int*) malloc( obj_in->dim * sizeof(int) );
/* Allocate the right size of vector for output from mls_basis_evaluate */
pdim = binomial( obj_in->deg + obj_in->dim, obj_in->dim );
/* Allocate a temporary dim-vector */
x = (double*) malloc( obj_in->dim * sizeof(double) );
vec = (double*) malloc( pdim * sizeof(double) );
/* Zero the matrix before forming the sum of outer products */
for(i=0;i<pdim*pdim;i++)
obj_in->grm[i] = 0.0;
/* Just iterate through all nodes */
for(i=0;i<obj_in->np;i++)
{
/* Generate the point */
for(j=0;j<obj_in->dim;j++)
x[j] = obj_in->pts[i*obj_in->dim+j] - x_in[j];
/* Generate the polynomial vector */
for(p=0,j=0;j<=obj_in->deg;j++)
{
rcombinadic_init( j, obj_in->dim, cmb );
n = binomial( j + obj_in->dim - 1, obj_in->dim - 1 );
for(k=0;k<n;k++)
{
rcombinadic_occupancy( j, obj_in->dim, cmb, occ );
prd = 1.0;
for(m=0;m<obj_in->dim;m++)
prd *= pow( x[m] / rho_in, (double) occ[m] );
vec[p++] = prd;
rcombinadic_next( j, obj_in->dim, cmb );
}
}
/* Now form the outer product */
for(j=0;j<pdim;j++)
{
for(k=j;k<pdim;k++)
{
/* Need to scale the input to the window function here */
prd = vec[j] * vec[k] * obj_in->wfs( obj_in->dim, obj_in->dlts[i], x );
obj_in->grm[j*pdim+k] += obj_in->gqw[i] * prd;
}
}
}
/* Copy into lower triangular area */
for(i=0;i<pdim;i++)
for(j=0;j<i;j++)
obj_in->grm[i*pdim+j] = obj_in->grm[j*pdim+i];
free( cmb );
free( occ );
free( x );
free( vec );
return 0;
}
/**
* This function generates the matrix at the given point and
* inverts it to give the coefficients for the correction function
* by calling rkp_matrix_evaluate to get the moments
* @param obj_in The RKP object
* @param x_in The point at which to generate
* @param dlt_in The dilation factor to use
* @param coeffs_out Where to put the generated coefficients for the correction function
* @return Returns 0 if all is well, < 0 otherwise
*/
int rkp_matrix_generate( rkp_t *obj_in, double *x_in, double *coeffs_out )
{
int i,j,ret,pdim,*ipiv;
/* Build the matrix */
rkp_matrix_evaluate( obj_in, x_in );
/* Set the dimension and allocate pivot space */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
/* Copy into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
/* Invert the matrix and put the LU-decomposed matrix in grmlu */
j = 1;
coeffs_out[0] = 1.0;
for(i=1;i<pdim;i++)
coeffs_out[i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out, &pdim, &ret );
free( ipiv );
return ret;
}
/**
* This function generates the matrix at the given point and
* inverts it to give the coefficients for the correction function
* by calling rkp_matrix_evaluate to get the moments
* @param obj_in The RKP object
* @param x_in The point at which to generate
* @param dlt_in The dilation factor to use
* @param coeffs_out Where to put the generated coefficients for the correction function
* @return Returns 0 if all is well, < 0 otherwise
*/
int rkp_matrix_generate_scaled( rkp_t *obj_in, double *x_in, double *coeffs_out )
{
int i,j,ret,pdim,*ipiv;
/* Build the matrix */
rkp_matrix_evaluate_scaled( obj_in, x_in );
/* Set the dimension and allocate pivot space */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
/* Copy into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
/* Invert the matrix and put the LU-decomposed matrix in grmlu */
j = 1;
coeffs_out[0] = 1.0;
for(i=1;i<pdim;i++)
coeffs_out[i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out, &pdim, &ret );
free( ipiv );
return ret;
}
/**
* This function generates the matrix at the given point and
* inverts it to give the coefficients for the correction function
* by calling rkp_matrix_evaluate_scaled_const to get the moments;
* This function only generates the correction function for the partition
* of unity; it does not generate the wavelets
* @param obj_in The RKP object
* @param x_in The point at which to generate
* @param coeffs_out Where to put the generated coefficients for the correction function
* @param rho_in The position-dependent dilation factor to use to generate the Gram matrix of moments
* @return Returns 0 if all is well, < 0 otherwise
*/
int rkp_matrix_generate_scaled_const( rkp_t *obj_in, double *x_in, double *coeffs_out, double rho_in )
{
int i,j,ret,pdim,*ipiv;
/* Build the matrix */
rkp_matrix_evaluate_scaled_const( obj_in, x_in, rho_in );
/* Set the dimension and allocate pivot space */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
/* Copy into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
/* Invert the matrix and put the LU-decomposed matrix in grmlu */
j = 1;
coeffs_out[0] = 1.0;
for(i=1;i<pdim;i++)
coeffs_out[i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out, &pdim, &ret );
free( ipiv );
return ret;
}
/**
* Generate wavelet wdx_in about point x_in; it is important here to note
* that if wdx_in is equal to 0 (zero), then this will just generate the
* particle functions, in the same way as rkp_matrix_generate.
*/
int rkp_wavelet_generate_order( rkp_t *obj_in, int wdx_in, double *x_in, double *coeffs_out )
{
int i,j,k,m,ret,pdim,*ipiv,*exp;
/* Build the matrix centered at x_in */
rkp_matrix_evaluate( obj_in, x_in );
/* Set the dimensionality */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
exp = (int*) malloc( obj_in->dim * sizeof(int) );
/* Copy into obj_in->grmlu for calculation */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
global_polynomial_vector( wdx_in, obj_in->dim, exp );
/* Solve the right system */
j = 1;
for(i=0;i<pdim;i++)
if( i == wdx_in )
{
coeffs_out[i] = 1.0;
m = 1;
for(k=0;k<obj_in->dim;k++)
m *= factorial( exp[k] );
coeffs_out[i] /= (double) m;
}
else
coeffs_out[i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out, &pdim, &ret );
free( ipiv );
free( exp );
return 0;
}
/**
* Generate wavelet wdx_in about point x_in; it is important here to note
* that if wdx_in is equal to 0 (zero), then this will just generate the
* particle functions, in the same way as rkp_matrix_generate. This is the
* scaled version of the function.
*/
int rkp_wavelet_generate_order_scaled( rkp_t *obj_in, int wdx_in, double *x_in, double *coeffs_out )
{
int i,j,k,m,ret,pdim,*ipiv,*exp;
/* Build the matrix centered at x_in */
rkp_matrix_evaluate_scaled( obj_in, x_in );
/* Set the dimensionality */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
exp = (int*) malloc( obj_in->dim * sizeof(int) );
/* Copy into obj_in->grmlu for calculation */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
global_polynomial_vector( wdx_in, obj_in->dim, exp );
/* Solve the right system */
j = 1;
for(i=0;i<pdim;i++)
if( i == wdx_in )
{
coeffs_out[i] = 1.0;
m = 1;
for(k=0;k<obj_in->dim;k++)
m *= factorial( exp[k] );
coeffs_out[i] /= (double) m;
}
else
coeffs_out[i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out, &pdim, &ret );
free( ipiv );
free( exp );
return 0;
}
/**
* Constantly scaled version of rkp_wavelet_generate_order
*/
int rkp_wavelet_generate_order_scaled_const( rkp_t *obj_in, int wdx_in, double *x_in, double rho_in, double *coeffs_out )
{
int i,j,k,m,ret,pdim,*ipiv,*exp;
/* Build the matrix centered at x_in */
rkp_matrix_evaluate_scaled_const( obj_in, x_in, rho_in );
/* Set the dimensionality */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
exp = (int*) malloc( obj_in->dim * sizeof(int) );
/* Copy into obj_in->grmlu for calculation */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
global_polynomial_vector( wdx_in, obj_in->dim, exp );
/* Solve the right system */
j = 1;
for(i=0;i<pdim;i++)
if( i == wdx_in )
{
coeffs_out[i] = 1.0;
m = 1;
for(k=0;k<obj_in->dim;k++)
m *= factorial( exp[k] );
coeffs_out[i] /= (double) m;
}
else
coeffs_out[i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out, &pdim, &ret );
free( ipiv );
free( exp );
return 0;
}
/**
* Wavelet generator; solves the same problem as the above, but keeps
* different columns of the resulting matrix inverse
* @param obj_in The RKP object
* @param x_in The point at which to center the matrix
* @param dlt_in The dilation parameter to use about this center
* @param coeffs_out The output storage of the coefficients generated
* @return Returns 0 if all went well, < 0 otherwise
*/
int rkp_wavelet_generate( rkp_t *obj_in, double *x_in, double *coeffs_out )
{
int i,j,k,ret,pdim,*ipiv;
double *tmp;
/* Build the matrix */
rkp_matrix_evaluate( obj_in, x_in );
/* Set the dimension and allocate pivot space */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
/* Copy into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
/* Allocate quick storage then release it */
tmp = (double*) malloc( pdim * pdim * sizeof(double) );
/* Save the system matrix in tmp temporarily */
for(i=0;i<pdim*pdim;i++)
tmp[i] = obj_in->grmlu[i];
/* Solve the system and apply it to the correct righthand side pdim - 1 times */
for(k=1;k<pdim;k++)
{
/* Copy the system into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = tmp[i];
/* Invert the matrix and put the LU-decomposed matrix in grmlu */
j = 1;
for(i=0;i<pdim;i++)
if( i == k )
coeffs_out[(k-1)*pdim+i] = 1.0;
else
coeffs_out[(k-1)*pdim+i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out + ( k - 1 ) * pdim, &pdim, &ret );
}
/* Clean everything up */
free( ipiv );
free( tmp );
return 0;
}
/**
* Scaled version of rkp_wavelet_generate
*/
int rkp_wavelet_generate_scaled( rkp_t *obj_in, double *x_in, double *coeffs_out )
{
int i,j,k,ret,pdim,*ipiv;
double *tmp;
/* Build the matrix */
rkp_matrix_evaluate_scaled( obj_in, x_in );
/* Set the dimension and allocate pivot space */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
/* Copy into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
/* Allocate quick storage then release it */
tmp = (double*) malloc( pdim * pdim * sizeof(double) );
/* Save the system matrix in tmp temporarily */
for(i=0;i<pdim*pdim;i++)
tmp[i] = obj_in->grmlu[i];
/* Solve the system and apply it to the correct righthand side pdim - 1 times */
for(k=1;k<pdim;k++)
{
/* Copy the system into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = tmp[i];
/* Invert the matrix and put the LU-decomposed matrix in grmlu */
j = 1;
for(i=0;i<pdim;i++)
if( i == k )
coeffs_out[(k-1)*pdim+i] = 1.0;
else
coeffs_out[(k-1)*pdim+i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out + ( k - 1 ) * pdim, &pdim, &ret );
}
/* Clean everything up */
free( ipiv );
free( tmp );
return 0;
}
/**
* Constantly scaled version of rkp_wavelet_generate
*/
int rkp_wavelet_generate_scaled_const( rkp_t *obj_in, double *x_in, double rho_in, double *coeffs_out )
{
int i,j,k,ret,pdim,*ipiv;
double *tmp;
/* Build the matrix */
rkp_matrix_evaluate_scaled_const( obj_in, x_in, rho_in );
/* Set the dimension and allocate pivot space */
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
ipiv = (int*) malloc( pdim * sizeof(int) );
/* Copy into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = obj_in->grm[i];
/* Allocate quick storage then release it */
tmp = (double*) malloc( pdim * pdim * sizeof(double) );
/* Save the system matrix in tmp temporarily */
for(i=0;i<pdim*pdim;i++)
tmp[i] = obj_in->grmlu[i];
/* Solve the system and apply it to the correct righthand side pdim - 1 times */
for(k=1;k<pdim;k++)
{
/* Copy the system into grmlu */
for(i=0;i<pdim*pdim;i++)
obj_in->grmlu[i] = tmp[i];
/* Invert the matrix and put the LU-decomposed matrix in grmlu */
j = 1;
for(i=0;i<pdim;i++)
if( i == k )
coeffs_out[(k-1)*pdim+i] = 1.0;
else
coeffs_out[(k-1)*pdim+i] = 0.0;
dgesv_( &pdim, &j, obj_in->grmlu, &pdim, ipiv, coeffs_out + ( k - 1 ) * pdim, &pdim, &ret );
}
/* Clean everything up */
free( ipiv );
free( tmp );
return 0;
}
/**
* This function fills in obj_in->coeffs for all of the
* particle basis functions. Most of the near the middle
* of the domain will be identical in their coefficients.
* WARNING: This function needs serious work. It is pointless
* to evaluate the correction polynomial with a fixed dilation
* factor since dlt_in depends on the particle being evaluated
* and the particle evaluated and the point at which the particle
* is being evaluated are different, meaning that calling
* rkp_matrix_generate with obj_in->dlts[i] and with
* obj_in->pts[i*obj_in->dim] is both wrong and useless.
* @param obj_in The RKP object to generate
* @return Returns 0 if all went well
*/
int rkp_basis_generate( rkp_t *obj_in )
{
int i,pdim;
pdim = binomial( obj_in->dim + obj_in->deg, obj_in->dim );
for(i=0;i<obj_in->np;i++)
rkp_matrix_generate( obj_in, obj_in->pts + i * obj_in->dim, obj_in->coeffs + i * pdim );
return 0;
}
/**
* Scaled version of rkp_basis_generate