MathGroup Archive 2001

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

Search the Archive

Re: Functional programming

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27163] Re: [mg27131] Functional programming
  • From: "Arturo Rania" <abasida at hotmail.com>
  • Date: Thu, 8 Feb 2001 04:40:49 -0500 (EST)
  • References: <200102070712.CAA29571@smc.vnet.net> <005f01c09158$335a9d60$64b9eb94@dqb2301>
  • Sender: owner-wri-mathgroup at wolfram.com

Daniel:

Thanks for your fast, complete and helpful response. It will help me a lot
to improve my style (or, shall I say, to get a style?). Thanks Daniel.


Tomas:

Ok. I may have done better if I explained every step of the algorithm,
sorry. I admit also that the code I sent have some very stupid mistakes (two
lines were absolutely innecessary and I forgot to rip them off). Sorry if
that bothers you but I am just starting, my eye is not sharpened enough, and
besides, I asked the group for help because of the very reason of my bad
finished code.

On the maths, I think you are plainly wrong (in fact, I think you
misunderstood what I said). MatrixPower DOES (you can even see a very
indicative error message) fail to evaluate non-diagonalizable numerical
matrices raised to symbolic powers (I called that a symbolic matrix power,
symbolic running for the exponent not for the matrix). And that is simply
because (correct me if I'm wrong) it uses the eigenvector method. That is
(more or less), first diagonalize the matrix n x n using an eigenbasis of n
independent non-zero vectors and then it raise the diagonal matrix to the
given power (which is a very easy task).

However if the matrix is non-diagonalizable, there is not such an eigenbasis
and so, you simply cannot use the eigenvector method. As a lateral
consecuence MatrixPower fails to handle non-integer powers of a ND matrix
(because it first takes the symbolic power and then evaluates the symbolic
exponent to the given number). On the other hand if you feed MatrixPower
with a ND matrix and an integer, it will have no problem because it will use
another methods (straight fast matrix multiplication?)

See Daniel's example, in the previous post in this topic or try yourself
with any ND matrix.

Fortunatly, diagonalization is not mandatory and there are other ways to do
it, as the program I sent shows.


Thanks for your comments, and sorry for my not-so-good english.



Arturo Rania.



----- Original Message -----
From: "Tomas Garza" <tgarza01 at prodigy.net.mx>
To: mathgroup at smc.vnet.net
Subject: [mg27163] Re: [mg27131] Functional programming


> 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
>
> In[1]:=
> Join[{{a, b, c}, {d, e, f}, {g, h, i}}]
> Out[1]=
> {{a, b, c}, {d, e, f}, {g, h, i}}
> In[2]:=
> Join @@ {{a, b, c}, {d, e, f}, {g, h, i}}
> Out[2]=
> {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 hotmail.com>
To: mathgroup at smc.vnet.net
> To: <mathgroup at smc.vnet.net>
> Sent: Wednesday, February 07, 2001 1:12 AM
> Subject: [mg27163] [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: Re: Functional programming
  • Next by Date: BCH
  • Previous by thread: Re: Functional programming
  • Next by thread: Re: Functional programming