Guest User

Untitled

a guest
Jan 18th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. positivizeMatrix[X_] := Module[{res},
  2.  
  3. res = X;
  4.  
  5. auxsystDP = Eigensystem[X];
  6.  
  7. duDP = DiagonalMatrix[auxsystDP[[1]]];
  8.  
  9. powerDP = 20;
  10.  
  11. While[Not[PositiveDefiniteMatrixQ[res]],
  12.  
  13. wDP =
  14. ReplacePart[
  15. duDP, {i_, i_} /;
  16. duDP[[i, i]] <= 0 :> $MachineEpsilon*2^(-powerDP)];
  17. (*Adding the smallest nugget possible.*)
  18.  
  19. res =
  20. Transpose[auxsystDP[[2]]].wDP.Inverse[Transpose[auxsystDP[[2]]]];
  21. (*this matrix is still not symmetric, only PD*)
  22.  
  23. powerDP--;
  24. ];
  25.  
  26. Print[PositiveDefiniteMatrixQ[res]];
  27.  
  28. res = 0.5*(res + Transpose[res]);
  29.  
  30. Print[PositiveDefiniteMatrixQ[res]];
  31. ];
Add Comment
Please, Sign In to add comment