# sum with indexed variable

how do i write a symbolic expression/function to calculate this sum based on the given value of the variable 'k'?

add a comment

sum with indexed variable

add a comment

1

Define `a`

as a *function* of `i`

. Possible use :

```
a = function("a")
d = var("d")
# I don't like to scratch Sage's predefined identifiers...
j, k = var("j, k", domain="integer")
S = sum(a(j)*d, j, 1, k+1)
```

**EDIT :** If you want to fiddle with output cosmetics, you might try :

```
a = function("a", nargs=1,
print_func=lambda self, *args: r"%s[%s]"%(self, args[0]),
print_latex_func=lambda self, *args: r"%s_{%s}"%(self, args[0]))
d = SR.var("d")
j, k = SR.var("j, k", domain="integer")
```

which allows for :

```
sage: sum(a(j)*d(), j, 0, k + 1)
d*sum(a[j], j, 0, k + 1)
```

and `latex(sum(a(j)*d(), j, 0, k + 1))`

gets interpreted as :

$$d {\sum_{j=0}^{k + 1} a_{j}}$$

HTH,

Please start posting anonymously - your entry will be published after you log in or create a new account.

Asked: ** 2022-03-06 20:46:48 +0200 **

Seen: **173 times**

Last updated: **Mar 07 '22**

Copyright Sage, 2010. Some rights reserved under creative commons license. Content on this site is licensed under a Creative Commons Attribution Share Alike 3.0 license.