[d781a2] | 1 | #include <stdio.h> |
---|
[762407] | 2 | #include "config.h" |
---|
[b1dfaf] | 3 | #include <kernel/mod2.h> |
---|
[d781a2] | 4 | |
---|
| 5 | #ifdef HAVE_SVD |
---|
| 6 | |
---|
[599326] | 7 | #include <Singular/svd_si.h> |
---|
| 8 | #include <kernel/structs.h> |
---|
[737a68] | 9 | #include <kernel/polys.h> |
---|
[0fb34ba] | 10 | #include <polys/matpol.h> |
---|
[599326] | 11 | #include <Singular/lists.h> |
---|
| 12 | #include <kernel/febase.h> |
---|
[d781a2] | 13 | |
---|
[ca474f] | 14 | template class std::vector< amp::mpfr_record* >; |
---|
| 15 | |
---|
[d781a2] | 16 | poly p_svdInit(char *s) |
---|
| 17 | { |
---|
| 18 | poly p=pInit(); |
---|
| 19 | currRing->cf->nRead(s,&pGetCoeff(p)); |
---|
| 20 | return p; |
---|
| 21 | } |
---|
| 22 | |
---|
| 23 | /************************************************************************* |
---|
| 24 | Testing SVD decomposition subroutine |
---|
| 25 | *************************************************************************/ |
---|
| 26 | lists testsvd(matrix M) |
---|
| 27 | { |
---|
| 28 | |
---|
| 29 | const unsigned int Precision=300; |
---|
| 30 | |
---|
| 31 | int max_i=M->nrows; |
---|
| 32 | int max_j=M->ncols; |
---|
| 33 | ap::template_2d_array< amp::ampf<Precision> > a; |
---|
| 34 | int m; |
---|
| 35 | int n; |
---|
| 36 | int maxmn; |
---|
| 37 | int i; |
---|
| 38 | int j; |
---|
| 39 | int gpass; |
---|
| 40 | int pass; |
---|
| 41 | bool waserrors; |
---|
| 42 | bool wsorted; |
---|
| 43 | bool wfailed; |
---|
| 44 | amp::ampf<Precision> materr; |
---|
| 45 | amp::ampf<Precision> orterr; |
---|
| 46 | amp::ampf<Precision> othererr; |
---|
| 47 | amp::ampf<Precision> threshold; |
---|
| 48 | amp::ampf<Precision> failthreshold; |
---|
| 49 | amp::ampf<Precision> failr; |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | materr = 0; |
---|
| 53 | orterr = 0; |
---|
| 54 | othererr = 0; |
---|
| 55 | wsorted = true; |
---|
| 56 | wfailed = false; |
---|
| 57 | waserrors = false; |
---|
| 58 | maxmn = 50; |
---|
| 59 | threshold = 5*100*amp::ampf<Precision>::getAlgoPascalEpsilon(); |
---|
| 60 | failthreshold = amp::ampf<Precision>("5.0E-3"); |
---|
| 61 | a.setbounds(1, max_i, 1, max_j); |
---|
| 62 | |
---|
| 63 | |
---|
| 64 | // |
---|
| 65 | // fill matrix a entries from M |
---|
| 66 | // |
---|
| 67 | for(i=1; i<=max_i; i++) |
---|
| 68 | { |
---|
| 69 | for(j=1; j<=max_j; j++) |
---|
| 70 | { |
---|
| 71 | char *str=pString(MATELEM(M,i,j)); |
---|
| 72 | Print(" to svd:%d,%d=%s\n",i,j,str); |
---|
| 73 | a(i,j) = amp::ampf<Precision>(str); |
---|
| 74 | } |
---|
| 75 | } |
---|
| 76 | //testsvdproblem<Precision>(a, max_i, max_j, materr, orterr, othererr, wsorted, wfailed); |
---|
| 77 | ap::template_2d_array< amp::ampf<Precision> > u; |
---|
| 78 | ap::template_2d_array< amp::ampf<Precision> > vt; |
---|
| 79 | ap::template_1d_array< amp::ampf<Precision> > w; |
---|
| 80 | svd::svddecomposition<Precision>(a, max_i, max_j, 2, 2, 2, w, u, vt); |
---|
| 81 | matrix Mu,Mw,Mvt; |
---|
| 82 | Mu=mpNew(max_i,max_i); |
---|
| 83 | Mw=mpNew(max_i,max_j); |
---|
| 84 | Mvt=mpNew(max_j,max_j); |
---|
| 85 | for(i=1;i<=max_i;i++) |
---|
| 86 | { |
---|
| 87 | for(j=1;j<=max_i;j++) |
---|
| 88 | { |
---|
| 89 | MATELEM(Mu,i,j)=p_svdInit(u(i,j).toString()); |
---|
| 90 | // Print(" after svd:%d,%d=%s\n",i,j,u(i,j).toString()); |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | for(i=1;i<=si_min(max_i,max_j);i++) |
---|
| 94 | { |
---|
| 95 | MATELEM(Mw,i,i)=p_svdInit(w(i).toString()); |
---|
| 96 | //Print(" after svd:%d,%d=%s\n",i,w(i).toString()); |
---|
| 97 | } |
---|
| 98 | for(i=1;i<=max_j;i++) |
---|
| 99 | { |
---|
| 100 | for(j=1;j<=max_j;j++) |
---|
| 101 | { |
---|
| 102 | MATELEM(Mvt,i,j)=p_svdInit(vt(i,j).toString()); |
---|
| 103 | //Print(" after svd:%d,%d=%s\n",i,j,vt(i,j).toString()); |
---|
| 104 | } |
---|
| 105 | } |
---|
| 106 | lists L=(lists)omAlloc(sizeof(slists)); |
---|
| 107 | L->Init(3); |
---|
| 108 | L->m[0].rtyp=MATRIX_CMD; |
---|
| 109 | L->m[1].rtyp=MATRIX_CMD; |
---|
| 110 | L->m[2].rtyp=MATRIX_CMD; |
---|
| 111 | L->m[0].data=(char*)Mu; |
---|
| 112 | L->m[1].data=(char*)Mw; |
---|
| 113 | L->m[2].data=(char*)Mvt; |
---|
| 114 | return L; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | #endif |
---|