symbolic math - Force evaluate index expression before passing to sum() -
i want write (somehow) enhanced sum function takes number of indices @ once, cannot understand how work. here have:
(%i1) nsum(indexes, expr) := if indexes = [] expr else nsum(rest(indexes), sum(expr, first(indexes),1, n)) $ (%i2) nsum([i,j], i+j), nouns; sum: index must symbol; found intosym(first(indexes)) #0: nsum(indexes=[k,j],expr=k+j)
i think fixed forcing maxima expand first(indexes)
symbol before passing sum
function. tried ''(...)
, ev(..., nouns)
, without success.
after reading , trying came following solution uses apply
function pre-evaluate arguments sum
:
nsum(indexes, expr) := if indexes = [] expr else nsum(rest(indexes), apply(sum, ['expr, indexes[1], 1, n])) $
upd1:
unfortunately, there wrong above code, works relatively simple expressions. in case straightforward approach works fine nsum
fails:
(%i1) rot[i](f) := sum(sum(sum(sum( g[r,i]*g[q,j]*w[i,j,k]*('diff(f[k], y[q]) + sum(k[k,q,m]*f[m], m, 1, n)), r, 1, n), j, 1, n), k, 1, n), q, 1, n) $ (%i2) rot2[i](f) := nsum( [r,j,k,q], g[r,i]*g[q,j]*w[i,j,k]*('diff(f['k], y[q]) + sum(k[k,q,m]*f[m], m, 1, n))) $ (%i3) rot[1](f); (%o3) ... yelds result. (%i4) rot2[1](f); apply: subscript must integer; found: k lambda([i,j],diff(ys[i],x[j]))(i=k,j=1)
upd2:
the code works indeed. 'k
accidentally left in rot2
definition instead of k
.
Comments
Post a Comment