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.Wed, 18 May 2022 14:32:15 +0200recurrence relationhttps://ask.sagemath.org/question/62494/recurrence-relation/I have the following recurrence relation (which gives the cycle index of the symmetric group), where $a_k$ are just indeterminates:
$Z_0 := 1, \quad Z_n := \frac1n \sum_{k=1}^n a_k Z_{n-k} \quad \forall n>0$
I tried the following, but it doesn't work:
def Z(n):
if n == 0:
return 1
else:
a = var(','.join('a%s'%(i+1) for i in range(n)))
return 1/n*sum(a[k-1]*Z(n-k),k,1,n)
What am I doing wrong?
**Edit**: Based on dan_fulea's answer, I found a way how to put `a` inside `Z(n)`:
def Z(n):
if n == 0:
return 1
a = var(','.join(f'a{k}' for k in [0..n]))
return 1/n * sum([ a[k] * Z(n-k) for k in [1..n] ]).expand()ThrashWed, 18 May 2022 14:32:15 +0200https://ask.sagemath.org/question/62494/Matrix recursionhttps://ask.sagemath.org/question/56075/matrix-recursion/I need to construct a non linear matricial recursive system for the series of matrix $M_k$ like the following one
$M_k[i][j] = F(M_{k-1}[i][j],M_{k-1}[i][j]) \text{ for } M_0 \text{ given}$
I have read that for one dimension one can use **Sympy** `rec`. But is there a way to do it simply in **Sagemath**?CyrilleTue, 09 Mar 2021 18:49:38 +0100https://ask.sagemath.org/question/56075/recurrence-rsolve-checkhttps://ask.sagemath.org/question/55961/recurrence-rsolve-check/ I am was testing some ideas and don't understand where this went wrong.
from sympy import Function, rsolve
from sympy.abc import n
y = Function('y');z=Function('z')
f = y(n)-y(n-1)-(-1)^n*binomial(x,n);g=z(n)-z(n-1)-binomial(x,n)
f is the sume of alternating binomial coefficients which is known.
g is just the summation of binomial coefficients (purportedly nonexistent)
frec = rsolve(f,y(n),{y(0):1});grec = rsolve(g,z(n),{z(0):1})
frec
--
zrec
Now this is correct, the alternating sum of binomials
fdiff = frec-(frec.subs({n : n-1}))
simplify(fdiff)
Whereas this is not correct?
gdiff = grec-(grec.subs({n : n-1}))
simplify(gdiff)
Am I missing something in rsolve usage?
rrogersMon, 01 Mar 2021 02:01:28 +0100https://ask.sagemath.org/question/55961/Derivative of a recurrence equationhttps://ask.sagemath.org/question/46543/derivative-of-a-recurrence-equation/Given:
var('β α γ t R')
c = function('c')
g = function('g')
f = function('f')
λ = function('λ')
suppose I define a function as
def U(l):
eq = 0
for i in range(l):
eq += (β^i)*(((c(t+i)+α*g(t+i))^(1-R))/(1-R))
return eq
How do I take the first order derivative w.r.t. `C_t` `C_t+1` and so forth?
I tried:
n = 2
L = U(n)
L.derivative(c(t))
without any luck.marco_repettoThu, 16 May 2019 10:16:22 +0200https://ask.sagemath.org/question/46543/Recurrence, for-loop and zero.https://ask.sagemath.org/question/37380/recurrence-for-loop-and-zero/Hello guys! Hope you guys are having a nice day.
I have some problem as follows: I setup some recurrence as follows
def f(j,k):
if k==0:
m=j
elif j==0:
m=k
else:
m=j/k*(f(j-1,k)+1/k*(f(j-1,k)))+k/j*(f(j,k-1)+1/j*(f(j,k-1)))
return m
And if we calculate
`f(1,1),
f(2,1),
f(1,2),
f(2,2)`
in sage, then what we get are
4,
35/2,
35/2,
105/2.
However, if we do the for loop as in the below, I found the above results to be zero!!:
for k in range(3):
for j in range(3):
(j,k), expVal(j,k)
And the calculation result from the sage is:
((0, 0), 0)
((1, 0), 1)
((2, 0), 2)
((0, 1), 1)
**((1, 1), 0)**
**((2, 1), 0)**
((0, 2), 2)
**((1, 2), 0)**
**((2, 2), 0)**
And observe the bolded result above... it is weird that the result is zero... Can anyone help me fix this please...
Thank you for any help! Hope you have a nice rest of the day!
sssageeeeMon, 24 Apr 2017 00:09:35 +0200https://ask.sagemath.org/question/37380/How to solve non_homogenous recurrence in sage using rsolve functions?https://ask.sagemath.org/question/35626/how-to-solve-non_homogenous-recurrence-in-sage-using-rsolve-functions/a(n+1)^2-5a(n+1)^2+6a(n)^2=7n a0=a1=1
I've used rsolve for this recurrence relation but I got the error min() arg is an empty sequence
any help would be appreciated.`from sympy import Function, rsolve,Poly
from sympy.abc import n
b=Function('b')
a=Function('a')
f=(b(n+2))^2-5*(b(n+1))^2+6*(b(n))^2-7*n
inits={b(0):1,b(1):1}
b(n)=rsolve(f,b(n),inits)
b(n)`
the error is:ValueError: min() arg is an empty sequenceM95Thu, 17 Nov 2016 15:27:48 +0100https://ask.sagemath.org/question/35626/Recurrent definiton of a functionhttps://ask.sagemath.org/question/26190/recurrent-definiton-of-a-function/ Hi, I am trying to compose a code for calculating the numbers H01 defined as a sum of the following recursively defined numbers:
H11(r) = \sum_{a + b = r} a*b/r (H11(a) + H12(a)) * H11(b)
H12(r) = \sum_{a + b = r} a*b/r (H11(a) + H12(a)) * (H11(b) + H12(b))
H01(r) = H11(r) + H12(r)
I ended up with the following code:
memoH11 = {1:0, 2:1/2}
def H11(r1):
if memoH11.has_key(r1):
return memoH11[r1]
else:
a = 0
for j1 in range(1,r1):
a += j1*(r1 - j1)/r1 * (H11(j1) + H12(j1)) * H11(r1-j1)
memoH11[r1] = a
return a
memoH12 = {1:0, 2:0}
def H12(r2):
if memoH12.has_key(r2):
return memoH12[r2]
else:
b = 0
for j2 in range(1,r2):
b += j2*(r2 - j2)/r2 * ( H11(j2) + H12(j2) ) * ( H11(r2 - j2) + H12(r2-j2) )
memoH12[r2] = b
return b
def H01(r):
return H11(r) + H12(r)
And here problems start. If I just type
H01(8)
it produces the incorrect value 27/8. But if I type
H01(6), H01(8)
it results the correct tuple (7/6, 15/4).
So how to fix the code ? Thanks in advance for any help.
uglychamaeleonSat, 14 Mar 2015 20:45:32 +0100https://ask.sagemath.org/question/26190/Solving Recurrence equation for $n$https://ask.sagemath.org/question/25816/solving-recurrence-equation-for-n/ I have a recurrence equation $t_n$ which is defined as
$$
t_n=2.5t_{n-1}-1.5t_{n-4},\qquad \text{for }n\geq5
$$
Where $t_1=5$, $t_2=10.5$, $t_3=26.25$, and $t_4=62.625$.
I need to solve my recurrence equation for $n$ when $t_n=13\times10^{10}$. How can I achieve this in Sage?studentThu, 12 Feb 2015 02:52:16 +0100https://ask.sagemath.org/question/25816/Recurrence equation value errorhttps://ask.sagemath.org/question/9049/recurrence-equation-value-error/Hello,
As an anxious student I tried googling for an answer for 2hours without any result. I'm new Sagemath user.
I have to solve equation:
a[n+2]-4*a[n+1]+4*a[n]=2^n+cos(n*pi/2), a[0]=1, a[1]=2
My sage input is:
from sympy import *
y = Function('y')
n = Symbol('n',integer=True)
rsolve(y(n+2)-4*y(n+1)+4*y(n)-2^n-cos(n*pi/2), y(n), {y(0):1,y(1):2})
But I'm getting an error:
> ValueError: 'y' expected, got 'cos'
Sage has some problems with cosine function, any idea how to solve it?blu3Fri, 08 Jun 2012 15:22:35 +0200https://ask.sagemath.org/question/9049/