I want to write an (somehow) enhanced sum function which takes a number of indices at once, but I cannot understand how to get it work. Here is what I currently have:
(%i1) nsum(indexes, expr) :=
if indexes = []
then expr
else nsum(rest(indexes), sum(expr, first(indexes),1, N)) $
(%i2) nsum([i,j], i+j), nouns;
sum: index must be a symbol; found intosym(first(indexes))
#0: nsum(indexes=[k,j],expr=k+j)
I think this could be fixed by forcing Maxima expand first(indexes)
into a symbol before passing to sum
function. I tried ''(...)
and ev(..., nouns)
, but without any success.
After some reading and trying I came to the following solution which uses
apply
function to pre-evaluate arguments forsum
:UPD1:
Unfortunately, there is something wrong with the above code, as it works well only for relatively simple expressions. In my case the straightforward approach works fine where
nsum
fails:UPD2:
The code works indeed. It was
'k
accidentally left inrot2
definition instead of justk
.