how can I give an array variable in maxima a value?

Viewed 99

I've got

expand(product(1-p*a[i], i, 0, 1));

now I want to substitute a complex value for a[0] and a[1]:

subst(array_make([(-%i)-1,%i-1]), a, expand(product(1-p*a[i], i, 0, 1)));

help please!

2 Answers

Maxima's handling of arrays is somewhat inconsistent. Here's a couple of ideas to get the result you want.

(I notice that your first input has %1 instead of %i, I guess that's a typographic error.)

(1) Substitute a value for each subscripted variable.

myexpr: expand (product (1 - p*a[i], i, 0, 1));
subst ([a[0] = (-%i)-1, a[1] = %i - 1], myexpr);

(2) Substitute a value for the array. I'll use ev to do that because it appears that subst has a bug in it; I'll file a bug report about that.

Maxima doesn't have a built-in function to create an array with the contents of a list, so I'll define a helper function array_from for that.

myexpr: expand (product (1 - p*a[i], i, 0, 1));
array_from(l) := fillarray (make_array (any, length(l)), l);
ev(myexpr, a = array_from([(-%i)-1, %i - 1]));
expand(subst(1.0*%i-1.0, a(1), subst(-1.0*%i-1.0, a(0), expand(product(1-s/a(i), i, 0, 1)))));
Related