Advertisement
Guest User

Untitled

a guest
Jun 19th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.38 KB | None | 0 0
  1. hoSVD[m_] := With[{d = ArrayDepth[m]},
  2. {Fold[Transpose[#1, RotateRight@Range[d]].#2 &, m, #], ConjugateTranspose /@ #} &@
  3. Table[Conjugate@First@SingularValueDecomposition@Flatten[m, {{i}, Delete[Range[d], i]}], {i, d}]]
  4.  
  5. m = RandomReal[NormalDistribution[], {3, 4}];
  6. {s, u} = hoSVD[m];
  7. Max@Abs[m - Transpose@u[[1]].s.u[[2]]]
  8.  
  9. dot[s_, u_, n_] := Transpose[Transpose[s, Ordering[#]].u, #] &@Append[#, n] &@
  10. Delete[#, n] &@Range@ArrayDepth[s];
  11.  
  12. m2 = dot[dot[s, u[[1]], 1], u[[2]], 2];
  13. Max@Abs[m - m2]
  14.  
  15. m = RandomReal[NormalDistribution[], {3, 4, 5}];
  16. {s, u} = hoSVD[m];
  17. m2 = dot[dot[dot[s, u[[1]], 1], u[[2]], 2], u[[3]], 3];
  18. Max@Abs[m - m2]
  19.  
  20. m = RandomReal[NormalDistribution[], {3, 4, 5, 6}];
  21. {s, u} = hoSVD[m];
  22. Max@Abs[Fold[dot[#, #2[[1]], #2[[2]]] &, s, Transpose@{u, Range@Length[u]}] - m]
  23.  
  24. defaultTol=0.00000001;
  25. rankFunction[mat_,tol_:defaultTol]:=Length@Select[Diagonal@mat,Abs@#>tol&];
  26.  
  27. hoSVD2[m_,rankF_:rankFunction]:=
  28. Module[{d=ArrayDepth[m],us,Ak,r,u2,u,w,v,rotatePermutation,flateningDimensions},
  29. Ak=m;
  30. rotatePermutation=RotateRight@Range[d];
  31. flateningDimensions={{1},Delete[Range[d],1]};
  32.  
  33. us=
  34. Table[
  35. {u,w,v}=SingularValueDecomposition@Flatten[Ak,flateningDimensions];
  36. r=rankF@w;
  37. u2=u[[All,;;r]];
  38. Ak=Transpose[(ConjugateTranspose@u2).Ak,rotatePermutation];
  39.  
  40. u2
  41. ,
  42. {i,d}
  43. ];
  44.  
  45. (*s=Ak;*)
  46. {Ak,us}
  47. ];
  48.  
  49. (*u(n).s*)
  50. dot2[s_,u_,n_]:=
  51. Module[{perm,permInv},
  52. permInv=Range@ArrayDepth[s]//Delete[#,n]&//Prepend[#,n]&;
  53. perm=Ordering[permInv];
  54. Transpose[u.Transpose[s,perm],permInv]
  55. ];
  56.  
  57. interpolateVector[v_,x_]:=Interpolation[v][x];
  58. interpU[u_,i_,j_,x_]:=interpolateVector[u[[i,All,j]],x[[i]]];
  59. interpS[s_,spos_,u_,x_]:=Part[s,Sequence@@spos]*Times@@Table[interpU[u,i,spos[[i]],x],{i,Length@spos}];
  60. interpM[s_,positiveS_,u_,x_]:=interpS[s,#,u,x]&/@positiveS//Total;
  61.  
  62. f[x_,y_,z_]:=Cos[x+y]Sin[z]
  63. nPoints=6;
  64. m=Table[f[x,y,z],{x,Range[nPoints]},{y,Range[nPoints]},{z,Range[nPoints]}]*1.;
  65.  
  66. {s,u}=hoSVD2[m];
  67. m2=dot2[dot2[dot2[s,u[[1]],1],u[[2]],2],u[[3]],3];
  68. Max@Abs[m-m2]
  69.  
  70. x={2.,4.1,3.1};
  71. f@@x
  72.  
  73. positiveS=Position[s,x_/;Abs@x>0.00001];
  74. interpM[s,positiveS,u,x]
  75.  
  76. nPoints2=100;
  77. mBig=Table[f[x,y,z],{x,Range[nPoints2]},{y,Range[nPoints2]},{z,Range[nPoints2]}]*1.;
  78. {s,u}=hoSVD2[mBig];//Timing(*finishes in a reasonable amount of time*)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement