Re: Functional programming
- To: mathgroup at smc.vnet.net
- Subject: [mg27149] Re: Functional programming
- From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
- Date: Thu, 8 Feb 2001 04:40:39 -0500 (EST)
- Organization: Universitaet Leipzig
- References: <95qsgd$spr@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Hi, it is a bit hard to improve the source code when no input data given. So only a few comments: > 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}, (* You don't need to Clear[] the variables in a module because new unique symbols names are generated when Module is called *) > Clear[v, w, r, EigenTable, Multiplicidad, Sol, k]; > v = Eigenvalues[M]; (* Why Union[{v,v}] the Union [v] will work and is faster *) > w = Union[v, v]; (* Hmm what is the task of evaluating w[[1]] but don't use the result .. *) > w[[1]]; (* Try {First[#], Length[#]} & /@ Split[Eigenvalues] and forget the line below and the Union[]. You Don't need to Sort the eigenvalues because Eigenvalues[] return already a sorted list. *) > EigenTable = Table[{w[[i]], Count[v, w[[i]]]}, {i, 1, > Length[w]}]; (* I'm not sure about that, but Flatten[Table[i - 1, {i, Length[#]}] & /@ Split[v]] will do the same. *) > 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]; For the rest I would like to see the original formulas. Hope that helps Jens