MathGroup Archive 2001

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Functional programming

  • To: mathgroup at
  • Subject: [mg27162] Re: [mg27131] Functional programming
  • From: Tomas Garza <tgarza01 at>
  • Date: Thu, 8 Feb 2001 04:40:48 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

I think your problem goes far beyond just being too procedural in your
programming. To start with, MatrixPower cannot fail when finding symbolic
matrix powers: at most, it will yield increasingly more complex expressions
and eventually will get you out of memory. And how could a symbolic matrix
be non-diagonalizable? Anyway, a quick look on your program reveals a number
of incoherent statements. To mention just the first few:

1. What do you want with w = Union[v, v] and {w[[i]], Count[v, w[[i]]]}? If
you're looking for different eigenvalues it would be enough to ask for
Union[v] and Length[Union[v]]. If yours is a symbolic matrix you'll always
get three different eigenvalues.
2. What do you expect from EigenTable = Table[{w[[i]], Count[v, w[[i]]]},
{i, 1,
Length[w]}]?  Clearly you'll get a list of the eigenvalues together with a 1
each, which is their multiplicity.
3. Join[a1, a2, . ] concatenates lists a1, a2,..., together. Notice the
difference between

Join[{{a, b, c}, {d, e, f}, {g, h, i}}]
{{a, b, c}, {d, e, f}, {g, h, i}}
Join @@ {{a, b, c}, {d, e, f}, {g, h, i}}
{a, b, c, d, e, f, g, h, i}

If the latter is what you intended you had better use a simple Flatten.

Etc. It would be much easier if you could provide the group with a
description of the problem and the solution you intend to implement, instead
of just a piece of code.

Tomas Garza
Mexico City

----- Original Message -----
From: "Arturo Rania" <abasida at>
To: mathgroup at
Subject: [mg27162] [mg27131] Functional programming

> 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];
> Any comments are welcome,
> Arturo Rania

  • Prev by Date: strange behavior
  • Next by Date: Re: LogPlot/Plot-Identity
  • Previous by thread: Functional programming
  • Next by thread: Re: Functional programming