Functional programming

• To: mathgroup at smc.vnet.net
• Subject: [mg27131] Functional programming
• From: "Arturo Rania" <abasida at hotmail.com>
• Date: Wed, 7 Feb 2001 02:12:40 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```I'm kind of new to Mathematica and I'm still too procedural in my
programming. I would like to get some advice in order to make my
programming more efficient, and especially more Mathematica oriented. In
order to do this I invite anyone who want to critize or improve a
program I wrote that finds symbolic matrix powers when MatrixPower fails
(when the matrix is non-diagonalizable). I think that it is a perfect
example of unprofesional programming style, but it's improvement can be
a great source of knowledge for me. Thanks for your time and good luck.

NDMatrixPower[M_/;And[MatrixQ[M], Length[M]] == Length[M[[1]]], t_
/; Or[NumericQ[t], Head[t] == Symbol]] :=
Module[{v, w, r, EigenTable, Multiplicidad, A, B, Sol, k},
Clear[v, w, r, EigenTable, Multiplicidad, Sol, k];
v = Eigenvalues[M];
w = Union[v, v];
w[[1]];
EigenTable = Table[{w[[i]], Count[v, w[[i]]]}, {i, 1,
Length[w]}];
Multiplicidad = Join@@Table[i, {j, 1, Length[EigenTable]},
{i, 1, EigenTable[[j, 2]]}] - 1;
A = Table[D[Power[r, j - 1], {r, Multiplicidad[[i]]}] /. r
-> v[[i]], {i, 1, Length[M]}, {j, 1, Length[M]}];
B = Table[D[Power[r, k], {r, Multiplicidad[[i]]}] /. r ->
v[[i]], {i, 1, Length[M]}, {j, 1, 1}];
Sol = Transpose[LinearSolve[A, B]][[1]];
Plus@@Table[MatrixPower[M, i - 1]*Sol[[i]], {i, 1,
Length[v]}] /. k -> t];