# Question about sum and diff

Why this code :

```
f(x)=sum(diff(sin(x),x,n),n,1,10)
f(x)
```

does not work?

Question about sum and diff

Why this code :

```
f(x)=sum(diff(sin(x),x,n),n,1,10)
f(x)
```

does not work?

add a comment

1

When you write `sum(diff(sin(x),x,n),n,1,10)`

, you try to do a symbolic sum, hence the Python name `n`

should be defined as a symbolic variable (an element of the `Symbolic Ring`

). However, in Sage, the name `n`

corresponds to the function that makes numerical_approx:

```
sage: n
<function numerical_approx at 0xb3f63684>
sage: n(pi)
3.14159265358979
```

This explains why you got the error `TypeError: no canonical coercion from <type 'function'> to Symbolic Ring`

, this is because Sage try (without success) to transform the `numerical_approx`

function into an element of the `Symbolic Ring`

.

So you may try to overwrite the name `n`

to correspond to the symbolic variable `"n"`

:

```
sage: n = SR.var('n')
sage: sum(diff(sin(x),x,n),n,1,10)
0
```

But then the result is unexpected ! The problem is that now, when you write `diff(sin(x),x,n)`

, Sage does not understands "differentiate n times relative to x", but "differentiate relative to x and then to n", so you get `0`

since a function of `x`

has a zero derivative relative to `n`

.

For Sage to understand "differentiate n times relative to x", `n`

needs to be an integer, not a symbol. So, instead you can do a non-symbolic sum of a list that contains all derivatives:

```
sage: f(x) = sum([diff(sin(x),x,n) for n in range(1,11)])
sage: f(x)
cos(x) - sin(x)
```

Which seems correct.

Asked: **
2015-02-06 11:57:46 -0500
**

Seen: **299 times**

Last updated: **Feb 06 '15**

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.