ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 19 Feb 2021 12:53:02 +0100A simple sum causes division by zero exceptionhttps://ask.sagemath.org/question/55766/a-simple-sum-causes-division-by-zero-exception/I'm very new to SageMath and did some small experiments yesterday. This is my Code:
x, n, r, i = var('x, n, r, i')
f(x, n, r) = x * sum((1 + r) ^ i, i, 1, n)
f(1, 30, 0.0)
It works well if `r != 0`, but will raise a exception of "division by zero" if equal.
My ipynb file: nbviewer.jupyter.org/gist/7sDream/5db3cfd153269fd1a1cacaf0b60f69bb
It seems some optimizations of sum did not consider the range of variable `r`.
But there is also no change if I added `assume(r >= 0)`before define `f`.
So how can I disable this sum optimizations? Or what is wrong with the way I use it?7sDreamFri, 19 Feb 2021 12:53:02 +0100https://ask.sagemath.org/question/55766/how to sum sumshttps://ask.sagemath.org/question/55531/how-to-sum-sums/I am new to sagemath and I am trying to calculate some formulas using it.
I learned how to write a sum such as
sage: a = [2, 2, 3, 4]
sage: d = [2, 4, 5, 6]
sage: def pi(i):
....: var("i")
....: f = sum([a[k]*d[k] for k in range(i + 1)])
....: return f
However, I am having difficulties to write a sum for `pi(i)` such as
sage: def Z(a, b):
....: var("a b")
....: l = len(d) - 1
....: def pi(i):
....: var("i")
....: f = sum([a[k]*d[k] for k in range(i + 1)])
....: return f
....: h = sum([pi(i) for i in range(l - 1)])
....: return h
but I got error. I tried to use
for i in range(l - 1):
also it doesn't work.smokzetaMon, 01 Feb 2021 09:49:00 +0100https://ask.sagemath.org/question/55531/implicitly defining a sequence of variableshttps://ask.sagemath.org/question/8181/implicitly-defining-a-sequence-of-variables/To define a general polynomial in Maple one writes
p := sum(a[i]*x^i,i=0..n);
and gets $p = \sum _{i=0}^{n}a_{{i}}{x}^{i}$.
So the "a[i]" are implicitly understood as variables, and their number (n) is also a variable. Or perhaps "a" is implicitly understood as a sequence of variables? I don't know what happens behind the scenes here, but it is very usefull.
Trying to accomplish this in sage I reached
sage: var('x,i,n')
(x, i, n)
sage: a = function('a')
sage: p = sum(a(i)*x^i,i,0,n);p
sum(x^i*a(i), i, 0, n)
Is this the right way? It doesn't behave as nice as in maple. Trying series, taylor, and diff only taylor works correctly:
sage: p.series(x==0,3)
---------------------------------------------------------------------------
RuntimeError Traceback (most recent call last)
....
RuntimeError: power::eval(): division by zero
sage: p.taylor(x,0,3)
x^3*a(3) + x^2*a(2) + x*a(1) + a(0)
sage: p.diff(x)
i*x^(i - 1)*a(i)*D[0](sum)(x^i*a(i), i, 0, n)
In Maple they all give good results.
Am I going at this the right way? Is there a way to implicitly define variables as in Maple?
parzanWed, 22 Jun 2011 06:48:29 +0200https://ask.sagemath.org/question/8181/sage sum not python sumhttps://ask.sagemath.org/question/53399/sage-sum-not-python-sum/ I was using sum in a routine and it bombed. I found that the Python sum I wanted is different from the Sagemath sum, which is symbolic, and I kept getting the Sagemath sum. But I don't see how you differentiate the two, since they have the same name.cybervigilanteFri, 11 Sep 2020 03:36:06 +0200https://ask.sagemath.org/question/53399/Area under function with rectangleshttps://ask.sagemath.org/question/49856/area-under-function-with-rectangles/I'm assigned to write a simple function which gives me the area under x^4 between an interval, but using a series of rectangles instead of a straight integral. I have to write a function that defines the minimum height of these rectangles and another function defining the maximum height, and then find the average value. I am a **total** newbie, so as trivial as it sounds, I've no clue how to define those functions.
I dont know how to make one define minimum heights and the other define maximum, and I don't think what I came up with as a sum of rectangles is correct.
def Smin(n):
sum=0
a=0
b=6
width=(b-a)/n
for i in range (1,n+1):
sum=width*(sum+i**4)
return sum
Any help will be greatly apreciated.alesilybar001Sat, 08 Feb 2020 22:45:41 +0100https://ask.sagemath.org/question/49856/Bug in computing sum (of binomials)https://ask.sagemath.org/question/45136/bug-in-computing-sum-of-binomials/One gets a wrong output when running the following command:
sage: sum(binomial(1,n),n,0,oo)
3
The output should be $2$ instead of $3$. Compare:
sage: sum(binomial(1,n),n,0,oo,algorithm="sympy")
2
ThrashTue, 22 Jan 2019 18:38:13 +0100https://ask.sagemath.org/question/45136/Variable Not Found while Plotting Finite Sumhttps://ask.sagemath.org/question/44530/variable-not-found-while-plotting-finite-sum/Today, while working on a discrete mathematics homework assignement, I came across this strange function:
$$ P(n) = \sum_{k=0}^{n}{\frac{(-1)^k (n-k)!}{k! \cdot n!}} $$.
Part of the question was to evaluate $\lim{n \rightarrow \infty}{P(n)}$ and as I had a hard time doing this by hand I gave it a shot with sagemath. I didn't expect it to evaluate the limit for me (which I tried and it didnt) but I was hoping to plot the graph in a straightforward manner. What I did is
```
var('k n')
```
,
```
p(n) = sum( (factorial(n-k)*(-1)^k)/factorial(k), k, 0, n)/factorial(n)
```
and finally
```
plot(p, (n, 1, 10))
```
which comes back with `ValueError: Variable 'k' not found`.
I found [this] (ask.sagemath.org/question/24293/plotting-value-error-variable-not-found/) related question which however seems to deal with an entirely different problem in the end. There is also [this] (groups.google.com/forum/#!topic/sage-support/azeRkbvtass) question which is closer to the problem I have, however the issue there seems to be that `p(4).n()` doesn't seem to work which happily provides an answer in my case. I have tried `plot(p(n).n(), (n, 1, 10))` which comes back as `TypeError: cannot evaluate symbolic expression numerically`. (Sorry for the non linked links, I will properly set them as soon as I have enough karma.)
I ended up working around the issue using `plot_list([p(i) for i in range(10)])` which was fine since I was interested mostly in integers anyway.
But why is this function not accepted using the normal `plot`? I also read somewhere that using matplotlib might be a workaround, however that'd introduce so much uneccessary syntax to my code. Also, if the issue here is that sagemath can't find a closed expression of my function, shouldn't there be a `plot_n` function that simply evaluates an argument numerically?HansZoellnerSat, 01 Dec 2018 18:30:52 +0100https://ask.sagemath.org/question/44530/sum and Substitutehttps://ask.sagemath.org/question/43804/sum-and-substitute/Hi
why Ds (like D ) does not give a numerical value ?
fs_d=r"!n \triangleq n! \sum_{i = 0}^{n}\frac{(-1)^i}{i!}"
#https://en.wikipedia.org/wiki/Derangement
forget()
var('n,l')
assume(l, 'integer')
assume(l>0)
assume(l, 'integer')
assume(n, 'integer')
assume(n>l)
assume(n, 'integer')
# Derangement
D=function('D')(n,l)
Dn=function('Dn')(n,l)
Dn=factorial(n)*sum(((-1)^l/factorial(l)),l,0,n)
D=factorial(7)*sum(((-1)^l/factorial(l)),l,0,7)
Ds=Dn.substitute({n:7})
show(LatexExpr(fs_d))
show('D : ',D)
show('Ds : ',Ds)
ortolljMon, 01 Oct 2018 17:07:21 +0200https://ask.sagemath.org/question/43804/How can I find the sum of fibonacci (1) + (2) + (4) + (7) + (11) + (16) + ... ?https://ask.sagemath.org/question/43408/how-can-i-find-the-sum-of-fibonacci-1-2-4-7-11-16/ How can I find the sum of fibonacci (1) + (2) + (4) + (7) + (11) + (16) + ... ?
* The numbers in () means the terms of fibonacci sequencepizzaSat, 18 Aug 2018 08:21:34 +0200https://ask.sagemath.org/question/43408/How can I calculate this sum? (accept both sage(cocalc) and by hand)https://ask.sagemath.org/question/43364/how-can-i-calculate-this-sum-accept-both-sagecocalc-and-by-hand/ How can I calculate this sum?
1/1-(x+x^2)^2
* This is the sum of infinity formula : a/1-r, while a=1 and r=(x+x^2)^2
Please also confirm if my formula is right.pizzaMon, 13 Aug 2018 18:24:41 +0200https://ask.sagemath.org/question/43364/How can I find the sum of first 20 positive perfect square?https://ask.sagemath.org/question/43311/how-can-i-find-the-sum-of-first-20-positive-perfect-square/Hi! First post!
When I type " k^2 for k in [1..20] ",
it says
" k**Integer(2) for k in (ellipsis_range(Integer(1),Ellipsis,Integer(20)))
^
SyntaxError: invalid syntax "
What can I type?
pizzaFri, 10 Aug 2018 15:57:42 +0200https://ask.sagemath.org/question/43311/Typing in a command of an adding-to-infinity sumhttps://ask.sagemath.org/question/43339/typing-in-a-command-of-an-adding-to-infinity-sum/1/1-x = x^0+x^1+x^2+...
How can I type in such a command of an adding-to-infinity sum?
Urgent! If anyone have any answer or suggestion, please type in here! Thanks!pizzaSat, 11 Aug 2018 17:14:11 +0200https://ask.sagemath.org/question/43339/How to sum numbers on a list?https://ask.sagemath.org/question/42773/how-to-sum-numbers-on-a-list/ Hi, I'm stuck at this problem:
**Create the SC20 list with the sums of the squares of the 20 first positive integers**
I know that if I want to do a list with the squares of the 20 first positive integers I have to do this:
[(x+1)^2 for x in range(20)]
And I'll get this:
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100, 121, 144, 169, 196, 225, 256, 289, 324, 361, 400]
But I don't know how to do it with the sums:
Basically, what I want to get is this:
[5,14,30,55,...]
Do you know how to do it?
Thanks in advance.
P.S. Sorry for my English.NorraimonFri, 29 Jun 2018 17:39:18 +0200https://ask.sagemath.org/question/42773/Sum using coefficients of a expansionhttps://ask.sagemath.org/question/42416/sum-using-coefficients-of-a-expansion/I'm trying to sum over the coefficients of an expansion but the sum gives zero. I use the taylor method to expand a function f:
f=sin(x)
ft=f.taylor(x,0,6)
then I need to use the coefficients in a sum, but if I for example do the following:
sum(x^(i)*ft.coefficient(x,i),i,0,5)
I get zero. Any idea why this is happening?DaveThu, 24 May 2018 02:40:44 +0200https://ask.sagemath.org/question/42416/summation of matriceshttps://ask.sagemath.org/question/39638/summation-of-matrices/ Can we use sum function to add a number of matrices? If not, then how to add a number of matrices with just a single command. Suppose I want to create 10 random matrices of order 5x5 and then want their sum as a result. I tried the following command, but it didn't work.
A_{i}=random_matrix(ZZ,5,5) for i in range(1,10);
sum(A_{i}, i, 1, 10)
Note: I even could not generate 10 random matrices with the above command.
Deepak SarmaFri, 17 Nov 2017 12:15:03 +0100https://ask.sagemath.org/question/39638/Symbolic sum with q_binomialhttps://ask.sagemath.org/question/36788/symbolic-sum-with-q_binomial/ Hi,
I'm new to sagemath. I'm trying to define a function that depends on a sum of a q-binomial, but I get "unable to convert n to an integer".
So, this works:
sum(binomial(n,k),k,1,n)
(output gives $2^n-1$)
but this doesn't:
sum(q_binomial(n,k),k,1,n)
All I did was add the "q_", which is the proper way to define a q-binomial in sage, as I already tested (try q_binomial(5,3) and the output is correct).
Anyone can help?ricardoklThu, 02 Mar 2017 18:38:16 +0100https://ask.sagemath.org/question/36788/how to turn off symbolic sum evaluationhttps://ask.sagemath.org/question/9649/how-to-turn-off-symbolic-sum-evaluation/I made some symbolic manipulation and finally... I want to save a 'sum()' in a input form (a evaluated version is horrible big). It is possible to turn off the sum evaluation in SAGE? F_RanekMon, 04 Nov 2013 10:15:35 +0100https://ask.sagemath.org/question/9649/Possible bug of sum functionhttps://ask.sagemath.org/question/33068/possible-bug-of-sum-function/Consider the following code:
def s(n):
return sum(k/n for k in range(n + 1))
L0 = s(2)
for n in range(2, 3):
L1 = s(n)
The answers are (using sage 7.1):
print L0, L1
(3/2, 1)
In the second case sage is using the usual integer division of python 2.x while in the first one it is using the exact representation of rationals. Is this a known bug?
jllbMon, 11 Apr 2016 19:15:50 +0200https://ask.sagemath.org/question/33068/Symbolic function that sums over variable sequencehttps://ask.sagemath.org/question/31674/symbolic-function-that-sums-over-variable-sequence/How do I define a symbolic function that takes a sum of variables as value?
I have tried the following:
k=var('k')
f(x)=sum(x[k], k, 1, 5)
But I get the following error:
TypeError: unable to convert k to an integer
I want to be able to symbolically differentiate `f` with regards to e.g. `x[3]`.jonatanFri, 18 Dec 2015 11:16:12 +0100https://ask.sagemath.org/question/31674/Sage Math Cloud Plotting Error, ValueError: cannot convert x to inthttps://ask.sagemath.org/question/31427/sage-math-cloud-plotting-error-valueerror-cannot-convert-x-to-int/ I'm using SageMathCloud to compute a summation
sum([.25^(100-i) * .75^(i) * binomial(100,i) for i in range(0, 63)])
0.00274614363169321
but am not able to plot it
plot(sum([.25^(100-i) * .75^(i) * binomial(100,i) for i in range(0, x)]),(x,1,63))
Error in lines 1-1
Traceback (most recent call last):
File "/projects/sage/sage-6.9/local/lib/python2.7/site-packages/smc_sagews/sage_server.py", line 905, in execute
exec compile(block+'\n', '', 'single') in namespace, locals
File "", line 1, in <module>
File "sage/symbolic/expression.pyx", line 1084, in sage.symbolic.expression.Expression.__int__ (/projects/sage/sage-6.9/src/build/cythonized/sage/symbolic/expression.cpp:8329)
raise ValueError("cannot convert %s to int" % self)
ValueError: cannot convert x to int
What am I doing wrong here?Joshua BurkhartWed, 09 Dec 2015 09:58:40 +0100https://ask.sagemath.org/question/31427/How can we sum over decimal numbers?https://ask.sagemath.org/question/29849/how-can-we-sum-over-decimal-numbers/ I know that for summing over integers from 1 to n we have sum(f,x,1,4) type of thing ...but what if I want to sum like f(1)+f(1.1)+f(1.2)+...+f(3.9)+f(4.0)? Can we create list of numbers from 1 to 4 with step size 0.1 or arbitrary step size which I choose.? what is the command for that?surajThu, 08 Oct 2015 20:32:09 +0200https://ask.sagemath.org/question/29849/Doubly indexed sumhttps://ask.sagemath.org/question/26916/doubly-indexed-sum/ I would like to define a doubly indexed sum. Below is what I did:
V=list(var(','.join(['a_%d%d' % (i,n) for i in [0..5] for n in [0..5]])))
sum(a_in*binomial(5,i)*binomial(5,n) for i in [0..5] for n in [0..5])
I get the global name 'a_' is not defined error. I tried this when there is only one index and it works, but doesnt seem to work for double index. Can anyone help me with this?
cihanFri, 22 May 2015 17:53:04 +0200https://ask.sagemath.org/question/26916/solve equation with double sumhttps://ask.sagemath.org/question/26465/solve-equation-with-double-sum/Hi!
Please help me with my porblem. I have two no-linear equations:
1) f(x)==h(x)
2) g(x)+S_{i,j,k}(x) == 0
I know I can solve (numerically) eq.(1) doing:
x=var('x')
find_root(f(x)==h(x),x,x_min,x_max)
In eq.(2) S_{i,j,k}(x) is a triple sum function of 'x' and i,j and k are the index of the sum.
How can I solve (numerically) eq.(2)?
Waiting for your answers. Thanks a lot!
Best regards
----------------------------------------------------------
Update:
If I run the next code:
import sympy.mpmath
N=20
A=0.7
G_0 = 37.7
B = 0.36
x = sympy.symbols('x')
def S(x_):
return sympy.mpmath.nsum(lambda i, j, k: (12*A**4*x_**6*i**4-30*A**2*x_**3*i**2*(j**2+k**2)+3*(j**2+k**2)**2)/(2*(A**2*x_**3*i**2+j**2+k**2)**(7/2)),[1,N],[1,N],[1,N])
def F(x_):
return G_0 * (x_ - 1/(x_**2))
print(sympy.mpmath.findroot(F(x) + B*A*sqrt(x)*S(x), [0.85,1]) )
I get the next error:
TypeError: unsupported operand parent(s) for '*': 'Symbolic Ring' and '<class 'sympy.mpmath.ctx_mp_python.mpf'>'
What am I doing wrong?
Best regards!
mresimulatorWed, 08 Apr 2015 14:59:34 +0200https://ask.sagemath.org/question/26465/Question about sum and diffhttps://ask.sagemath.org/question/25750/question-about-sum-and-diff/Why this code :
f(x)=sum(diff(sin(x),x,n),n,1,10)
f(x)
does not work?DesruimFri, 06 Feb 2015 18:57:46 +0100https://ask.sagemath.org/question/25750/Sum of two vectorshttps://ask.sagemath.org/question/25331/sum-of-two-vectors/ Hi everybody, i've got this functions and variables:
v = function('v',t)
a_V_ra = function('a_V_ra',t)
a_a_ra=function('a_a_ra',t)
g = var('g')
r_V_ra = vector([v,0,0])
a_V_ra = R_ra*r_V_ra.column()
where R is a matrix and a_V_ra is the vector [cos(psi(t))v(t), sin(psi(t))v(t),0].
But when i do this:
a_a_ra = diff(a_V_ra)+vector([0,0,-g])
it says: *TypeError: unsupported operand parent(s) for '+': 'Full MatrixSpace of 3 by 1 dense matrices over Symbolic Ring' and 'Vector space of dimension 3 over Symbolic Ring'*
I guess it's saying that i'm summing a matrix and a vector, but they're both vectors! What can I do to make it work?
Thank you.
SilviaTue, 23 Dec 2014 10:42:05 +0100https://ask.sagemath.org/question/25331/Condition in sum() function?https://ask.sagemath.org/question/24946/condition-in-sum-function/ Hello,
I'm trying to use the exponential form of the Fourier series representation of a function to plot an approximation of said function using the first five terms.
The actual function is f(t) = 1/2 + j/(2*pi) * E from (n = -5) to (n = 5) of ((-1)^n - 1)/n * e^(j*2*n*pi*t), where n =/= 0
Here I'm using 'E' to indicate summation notation. I apologize if this deviates from an established standard, but I'm having trouble uploading images right now (which would have made the function clearer).
The code I'm using for this function is
var('n, t')
j = i # imaginary unit
expr = ((-1)^n - 1)/n * exp(2*pi*n*j*t) # I think the problem is the division by 'n' here
assume(n, 'integer'); assume(n != 0)
f(t) = 1/2 + j/(2*pi) * sum(expr, n, -5, 5)
However, when I try this, I get the following exception:
<code>
RuntimeError: ECL says: Error executing code in Maxima: expt: undefined: 0 to a negative exponent.
</code>
This seems to be due to the division by <code>n</code> in <code>expr</code>. Are my assumptions not working here? Thus, my actual question is how can I add the condition <code>n != 0</code> to the <code>sum()</code> function in sage?mpattonSat, 22 Nov 2014 19:50:08 +0100https://ask.sagemath.org/question/24946/Summing polynomialshttps://ask.sagemath.org/question/23590/summing-polynomials/I have a ´Univariate Polynomial Ring in t over Symbolic Ring´
sage: cj_t = (-1)**(j-1)/factorial(j-1)*(-j+a)**(j-1/2)*exp(-j)*(t+j)**(-1)
How can I compute the sum over j from 1 to N?
sage: sum(cj_t,1,N)
doesn't work.
sage: sum([cj_t for j in [1..N]])
(Python syntax) doesn't work either.jjackSat, 26 Jul 2014 11:00:15 +0200https://ask.sagemath.org/question/23590/Numeric evaluation of exp(x^2) in a sum TypeErrorhttps://ask.sagemath.org/question/10946/numeric-evaluation-of-expx2-in-a-sum-typeerror/Define simple function of one variable with sum:
sage: r, i = var('r i')
sage: h(r) = sum(exp(i), i, -r, r)
sage: n(h(1))
4.08616126963049
OK, now change the argument of exponent to i^2:
sage: h(r) = sum(exp(i^2), i, -r, r)
sage: n(h(1))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-128-85150c4c79b2> in <module>()
----> 1 n(h(Integer(1)))
/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/misc/functional.pyc in numerical_approx(x, prec, digits)
1395 prec = int((digits+1) * LOG_TEN_TWO_PLUS_EPSILON) + 1
1396 try:
-> 1397 return x._numerical_approx(prec)
1398 except AttributeError:
1399 from sage.rings.complex_double import is_ComplexDoubleElement
/usr/lib/sagemath/local/lib/python2.7/site-packages/sage/symbolic/expression.so in sage.symbolic.expression.Expression._numerical_approx (sage/symbolic/expression.cpp:22617)()
TypeError: cannot evaluate symbolic expression numerically
Why is that and what am I doing wrong?sage_userSun, 19 Jan 2014 06:29:40 +0100https://ask.sagemath.org/question/10946/derivative of multivariate equation with nested sumhttps://ask.sagemath.org/question/10869/derivative-of-multivariate-equation-with-nested-sum/Hello,
I often have to deal with functions like the one below, take derivatives and
so on. I would really like to know if I could use a CAS like SAGE to do this tedious and error prone calculations but I couldn't find a similar kind of function in the docs and tutorials.
My questions are:
* how can I write this function in SAGE ?
for $x\in \mathbf{R}^p; v \in \mathbf{R}^{p \times k}$
$$y(x, v) := \sum^p_{i=1} \sum^p_{j>i} \sum_{f=1}^k v_{i,f} v_{j,f} x_i x_j =
\sum^p_{i=1} \sum^p_{j>i} \langle v_{:,i}, v_{;,j} \rangle x_i x_j$$
* calculate the partial derivatives $\frac{\partial y(x,v)}{\partial v_{i,j}}$ ?
* or the the derivative with respect to the column-vector $\frac{\partial y(x,v)}{\partial v_{:, i} }$ ?
Or is there a better way to work with this kind of function in SAGE? (the function above is only an example)
ThanksibayerMon, 30 Dec 2013 13:36:27 +0100https://ask.sagemath.org/question/10869/Polynomials as a sum of squareshttps://ask.sagemath.org/question/10062/polynomials-as-a-sum-of-squares/Is it possible find a decomposition of a polynomial as a sum of squares in Sage if such representation is possible? For example, if I want to prove that $x^6 - x^5 + x^4 - x^3 + x^2 - x + 2/5>0$ for all $x\in\mathbb{R}$ then Sage would return for example
$$\left (x^2\left (x - \frac{1}{2}\right)\right )^2+\left (\frac{\sqrt{3}x}{2}\left (x - \frac{2}{3}\right)\right )^2+\left (\sqrt{\frac{2}{3}}\left(x - \frac{3}{4}\right )\right )^2+\sqrt{\frac{1}{40}}^2$$
Jaakko SeppäläWed, 24 Apr 2013 14:53:09 +0200https://ask.sagemath.org/question/10062/