views:

155

answers:

1

Hello,

I'm trying to use linsolve( ) from within a subroutine in order to code up a solution algorithm that is independent of the dimension of the problem.

The desire is to dynamically allocate the equation and variable arrays within the subroutine itself, pass these to linsolve( ) and hopefully get back the results.

/* SUBROUTINE IDEA -- INCORRECT SYNTAX */
solution(p):=(
  array(eq,p),
  array(a,p),

  for i:0 thru p do (
    eq[i]: sum(binom(j+1,i)*a[j],j,i,p) = binom(p,i)
  ),

  linsolve(eq,a)  /* THIS IS WHERE THE PROBLEM LIES */
)$

Clearly, linsolve( ) doesn't like being passed arrays in this way.

If I manually provide the list of elements of the arrays, like below, it works for one particular case, which of course defeats the purpose of having the general subroutine.

linsolve([eq[0],eq[1],eq[2]],[a[0],a[1],a[2]])  
/* UGLY: HAVE TO PASS EACH ELEMENT IN BY HAND */

Any insight would be appreciated!

A: 

Apparently in Maxima, lists and arrays are NOT the same underlying object.

Arrays are more complex and a bit of mess (as suggested in this posting to the Maxima mailing list).

So, given this bit of information, let's stay away from arrays and work with lists.

solution(p):= block([a, eq],        /* give subroutine variables local scope */
    v : makelist(a[i], i, 0, p),    /* create list of unknowns (0-indexed) */
   eq : makelist(sum(binom(j+1,i)*a[j],j,i,p) = binom(p,i), i, 0, p),  
                                    /* create list of equations (0-indexed) */
   linsolve(eq, v)
)$

So this now gives a general solution that scales with the dimension of the problem.

Thanks to Robert Dodier for coding style suggestions ("natural" Maxima).

(FYI - Problem Background: the problem that leads to this linear system is the finite summation of integer powers, i.e. finite sum of k^2, or k^3, ..., or in general k^p. Turns out that though the first case finite sum of k is straightforward, the general solution is surprisingly complicated.

A partial discussion is here: Finite Summation by Recurrence Relations, Part 2. Part 3, including the solution above, will be added shortly.)

AKE