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