1

I'm relatively new to using Mathematica. Previously I used Matlab, but lately I noticed that the numbers i got in both of them do not match (mostly it seems that Matlab lacks precision...).

The question is: I need a nested table defined recursively, and based on it I'll calculate what I actually need. But the code

L = RecurrenceTable[
   {
    a[i] == -M[[i - 1]].Inverse[Psi[[i - 1]] + 
         L[[i - 1]].Lambda[[i - 1]]],
    a[2] == -M[[1]].Inverse[Psi[[1]]],
    a[1] == {{0}};
    },
   a,
   {i, 1, NN}
   ];

, where M, Psi and Lambda are nested tables themselves defined like

Psi = Table[Table[ __SOME FORMULAS__ , {i, 0, NN - n}, {j, 0, NN - n}], {n, 1, NN - 1}];
PrependTo[Psi, {{-c*P[NN]}}];

produces the following error:

Part::pspec: "Part specification -1+i is neither a machine-sized integer nor a list of machine-sized integers. "
RecurrenceTable::deqn: Equation or list of equations expected instead of Null in the first argument 

Even though I did the same thing like in reference, but i got this error. And considering the performance of numeric calculations - how can I improve it for the code above?

I would really appreciate any help.


NN = N[4, 50];

p[i_, j_] = Binomial[2*NN - i + j, i + j];

Ψ = 
  Table[Table[\[Piecewise]p[n + i, n + j] i == j 1 True, {i, 0, 1}, {j, 0, 1}], {n, 0, 
    NN - 1}];

L = {{{1, 2}, {3, 4}}};
Do[AppendTo[L, Inverse[L[[i - 1]].Ψ[[i - 1]]]], {i, 2, NN}]
(*L=RecurrenceTable[{a[i]\[Equal]-M[[i-1]].Inverse[Ψ[[i-1]]+L[[i-1]].\
Λ[[i-1]]],a[2]\[Equal]-M[[1]].Inverse[Ψ[[1]]],a[1]\[Equal]{{0}}\
;},a,{i,1,NN}];*)

N[L, 5] // MatrixForm
J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
Andrew S.
  • 465
  • 3
  • 7
  • Look at the differences between = and := –  Oct 19 '14 at 23:35
  • 1
    Those are immediate and delayed assignments. Is there any difference between them considering the "constant" values I should receive? (as all the parameters are predefined) – Andrew S. Oct 20 '14 at 09:21
  • I'd like to take a further look at this: could you provide actual runnable code (using simple functions if you don't want to provide the originals)? In general, you can't use "=" when the right hand side is undefined, so you'd have to compute M, Psi, and Lambda first. –  Oct 20 '14 at 15:06
  • The RecurrenceTable code gives the Recursion Depth error if I substitute "=" with ":=". – Andrew S. Oct 20 '14 at 18:13
  • It's quite hard to remove the "extra" code... This is the maximum I could do (there are two versions inside comment):

    `NN=N[4,50];

    p[i_,j_]=Binomial[2*NN-i+j,i+j];

    [CapitalPsi]=Table[Table[[Piecewise] p[n+i,n+j] i==j 1 True

    ,{i,0,1},{j,0,1}],{n,0,NN-1}];

    L={{{1,2},{3,4}}}; Do[ AppendTo[ L,Inverse[L[[i-1]].[CapitalPsi][[i-1]]] ], {i,2,NN} ] (L=RecurrenceTable[ { a[i][Equal]-M[[i-1]].Inverse[[CapitalPsi][[i-1]]+L[[i-1]].[CapitalLambda][[i-1]]], a[2]==-M[[1]].Inverse[[CapitalPsi][[1]]], a[1][Equal]{{0}}; }, a, {i,1,NN} ];)

    N[L,5] // MatrixForm`

    How should I paste the code here?

    – Andrew S. Oct 20 '14 at 18:50

1 Answers1

1

I should've mentioned this earlier, but, instead of RecurrenceTable try defining your recursions like this (using Fibbonaci numbers as an example):

fib[0] = 1; 
fib[1] = 1; 
fib[n_] := fib[n] = fib[n-1] + fib[n-2]; 

When you do fib[6] (for example), it will fib[6] and remember the value, making future recursions faster.

It also means you can nest recursions, since everything will be computed on an "as needed" basis.

More about this paradigm:

http://reference.wolfram.com/language/tutorial/FunctionsThatRememberValuesTheyHaveFound.html

  • 1
    Thanks for this, but this was the first thing I did. Strangely, but it took more time to calculate the result this way (compared to the tabled version). I should try and rewrite it from scratch. I'll try it tomorrow and tell the results. – Andrew S. Oct 20 '14 at 23:04
  • When I wrote it the way you suggested, the time to calculate became enormous... Using Tables for a parameter value 30 it took me 1.7 seconds to calc, but this way it takes ~1 second for param = 5 and grows exponentially. Might be I wrote something incorrectly, but I double checked it. – Andrew S. Oct 23 '14 at 15:25
  • If the rewritten code's not private, could you cut/paste it. I'd like to run it directly and see what's going on? –  Oct 23 '14 at 19:16