I am always amazed while I am reading some solutions of problems in mathematical problems of this site with Mathematica.
For instance, look at the problem 48.
Whereas I am writing 100 lines of code to solve this problem, a proposed 1-line solution is:
romDigits@Take[IntegerDigits[Plus@@Table[i^i,{i,1000}]],-10]
In this post, I would like to "factor" my code using the powerful syntax of Mathematica. Below you will find my code to decompose a complex matrix A as a sum D+N where $D$ is a diagonalisable matrix and $N$ is a nilpotent matrix (called Newton-Raphson method for Dunford decomposition of a matrix).
ClearAll;
Unprotect[MatrixPower]
MatrixPower[m_?SquareMatrixQ, 0] := IdentityMatrix[Length[m]]
Protect[MatrixPower]
DunfordEffective[A_] := Module[{Chi,P,Q,An,Anp1,coeffsP,coeffsQ,PAn,QAn,d,n},
Print[A // MatrixForm];
Chi=CharacteristicPolynomial[A,X];
P=Simplify[Chi/PolynomialGCD[Chi,D[Chi,X]]];
coeffsP=CoefficientList[P,X];
(* Derivative polynomial of P *)
Q=D[P,X];
coeffsQ=CoefficientList[Q,X];
An=A;
PAn=Sum[MatrixPower[An, j - 1] coeffsP[[j]], {j, 1, Length[coeffsP]}];
QAn=Sum[MatrixPower[An, j - 1] coeffsQ[[j]], {j, 1, Length[coeffsQ]}];
Anp1=An-PAn.Inverse[QAn];
While[An != Anp1,
An=Anp1;
PAn=Sum[MatrixPower[An, j - 1] coeffsP[[j]], {j, 1, Length[coeffsP]}];
QAn=Sum[MatrixPower[An, j - 1] coeffsQ[[j]], {j, 1, Length[coeffsQ]}];
Anp1=An-PAn.Inverse[QAn];
];
d=An;
n=A-An;
{d,n,d+n-A} // MatrixForm
]
A={{1,0,1},{1,0,0},{0,0,1}};
DunfordEffective[A]
Could you provide me some critics about this code ? How would it be nicer to read at ? Please, don't hesitate to provide me any critics, I want to improve my programming skills. The aim is not to treat big sized matrices but some programming remarks in Mathematica which could used syntax proper to Mathematica.
Thanks
ClearAll
doesn't make sense. Please press F1 and check the document for its correct usage. $\endgroup$SchurDecomposition
for this. $\endgroup$