| 1 | /* -*- mode: C++ -*- */ |
|---|
| 2 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 3 | // FILE : BaseC2.LinComb.tol |
|---|
| 4 | // PURPOSE: Defines class @BaseC2.LinComb |
|---|
| 5 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 6 | |
|---|
| 7 | Text _.autodoc.member.BaseC2.LinComb = "Defines a new base B2 of " |
|---|
| 8 | "order K2 as a linear combination B2=V*B1 of functions of a previous base " |
|---|
| 9 | "B1 of order K1>=K2. The matrix of change V must have rank K2 to ensure " |
|---|
| 10 | "linear independence."; |
|---|
| 11 | |
|---|
| 12 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 13 | Class @BaseC2.LinComb : @BaseC2.Cache |
|---|
| 14 | ////////////////////////////////////////////////////////////////////////////// |
|---|
| 15 | { |
|---|
| 16 | @BaseC2 _.base; |
|---|
| 17 | VMatrix _.V; |
|---|
| 18 | |
|---|
| 19 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 20 | Static Text _.autodoc.member.New = "Creates an instance B2 of " |
|---|
| 21 | "@BaseC2.LinComb as B2 = V*B1"; |
|---|
| 22 | Static @BaseC2.LinComb New(@BaseC2 B1, VMatrix V) |
|---|
| 23 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 24 | { |
|---|
| 25 | Real n = (B1[1])::_.n; |
|---|
| 26 | |
|---|
| 27 | @BaseC2.Concat aux = [[ |
|---|
| 28 | Real _.n = B1::_.n; |
|---|
| 29 | Real _.K = VRows(V); |
|---|
| 30 | Set _.base = base; |
|---|
| 31 | VMatrix _.V = V |
|---|
| 32 | ]]}; |
|---|
| 33 | |
|---|
| 34 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 35 | Static Text _.autodoc.member.Compact = "Creates an instance of " |
|---|
| 36 | "@BaseC2.LinComb that is close to be orthonormal and has at least " |
|---|
| 37 | "the specified ratio of information of original base"; |
|---|
| 38 | Static @BaseC2.LinComb Compact( |
|---|
| 39 | @BaseC2 base, |
|---|
| 40 | Real informationRatio) |
|---|
| 41 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 42 | { |
|---|
| 43 | Real K1 = base::_.K; |
|---|
| 44 | |
|---|
| 45 | VMatrix u = Rand(K1*10, K1, -1, 1); |
|---|
| 46 | |
|---|
| 47 | VMatrix Pu = Group("ConcatColumns", For(1, K1, VMatrix(Real k) |
|---|
| 48 | { |
|---|
| 49 | base::function.v(k,u) |
|---|
| 50 | })); |
|---|
| 51 | |
|---|
| 52 | Set Pu.svd = SVD(VMat2Mat(Pu)); |
|---|
| 53 | |
|---|
| 54 | Matrix Pu.eigenvalue = RPow(Tra(SubDiag(Pu.svd[2],0)),2); |
|---|
| 55 | Matrix Pu.eigenvalueCum = |
|---|
| 56 | { |
|---|
| 57 | Matrix aux = DifEq(1/(1-B),Pu.eigenvalue); |
|---|
| 58 | aux*(1/MatSum(Pu.eigenvalue)) |
|---|
| 59 | }; |
|---|
| 60 | VMatrix ratio = Rand(K1,1,informationRatio,informationRatio); |
|---|
| 61 | Real K.new = MatSum(LE(Pu.eigenvalueCum,ratio)); |
|---|
| 62 | VMatrix V = Mat2VMat(SubCol(Pu.svd[3],Range(1,K.new,1))); |
|---|
| 63 | New(base, V) |
|---|
| 64 | }; |
|---|
| 65 | |
|---|
| 66 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 67 | Real _build.cache(VMatrix X, Real order) |
|---|
| 68 | //////////////////////////////////////////////////////////////////////////// |
|---|
| 69 | { |
|---|
| 70 | Real _.base::upgrade.cache.v(order); |
|---|
| 71 | VMatrix If(order>=0, { _.function.r := |
|---|
| 72 | _V*_.base::function.m(X) |
|---|
| 73 | }); |
|---|
| 74 | Set If(order>=1, { _.partial.derivative.1 := |
|---|
| 75 | For(1, _.n, VMatrix(Real i) |
|---|
| 76 | { _V*_.base::partial.derivative.1.m(X,i) }) |
|---|
| 77 | }); |
|---|
| 78 | Set If(order>=2, { _.partial.derivative.2 := |
|---|
| 79 | For(1, _.n, Set(Real i) |
|---|
| 80 | { |
|---|
| 81 | For(1, i, VMatrix(Real j) |
|---|
| 82 | { |
|---|
| 83 | _V*_.base::partial.derivative.2.m(X,i,j) |
|---|
| 84 | }) |
|---|
| 85 | }) |
|---|
| 86 | }); |
|---|
| 87 | True |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | }; |
|---|