Ask Your Question
1

Symbolic sum with q_binomial

asked 2017-03-02 18:38:16 +0100

ricardokl gravatar image

updated 2018-10-11 11:57:56 +0100

FrédéricC gravatar image

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?

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
1

answered 2017-03-02 22:53:47 +0100

eric_g gravatar image

Actually, q_binomial is not a symbolic function, contrary to binomial. Therefore it does not accept symbolic variables like n or k as arguments. Only specific values can be given to q_binomial. To check this, do not hesitate to have a look at the code of the function (taking the advantage that SageMath is open source!) by typing q_binomial??, and compare with binomial??.

edit flag offensive delete link more

Comments

Thanks, good to know about these differences, I am new to all of this. But how should I proceed then? I need to use the full definition on the q_binomial explicitly? I also had some difficulty with this. I can define a sum with a arbitrary upper value 'k', but I can't do the same with a product (to use the q-factorial).

ricardokl gravatar imagericardokl ( 2017-03-03 15:53:06 +0100 )edit

I am not sure if this answers your question, but note that you can still perform a sum, provided you specify n:

sage: sum(q_binomial(5,k) for k in [1..5])
2*q^6 + 2*q^5 + 6*q^4 + 6*q^3 + 6*q^2 + 4*q + 5

Note that in the above writing, k is not a symbolic variable, but an integer ranging in [1,5].

eric_g gravatar imageeric_g ( 2017-03-03 23:17:51 +0100 )edit
0

answered 2017-03-10 18:19:40 +0100

ricardokl gravatar image

updated 2017-03-10 18:20:44 +0100

Update:

I defined the expressions I was looking for by using the lambda function:

var('q,n,k,m')
qfac= lambda n : prod([(1-q^(k))/(1-q) for k in (1..n)]) #q-factorial
qbin = lambda n,m : qfac(n)/(qfac(m)*qfac(n-m)) #q-binomial
S= lambda n : sum([qbin(n,k) for k in (1..n)])

I also stopped using the iput sum(funcion,variable,lower limit,upper limit), and I am using sum([list]), where the list is defined by [expression(k) for k in (1..n)]. Although the first one works for simple stuff, It is not so good for symbolic.

Now I can call, as an example, S(4).

S(4)

A second way I defined the sum above is by defining a list in a specific range,

S = [sum([qbin(n,k) for k in (1..n)]) for n in (1..5)]

And I can call S(4) by calling the S[3] (lists start in 0, and use square brackets )

S[3]

I can also call all elements in a range of the list by

S[0:4]

This calls all elements up to S[3], which I find a bit confusing...

I'm still new to all of this... perhaps I can use "def" to do the same, maybe it is better, but I'm satisfied for now. Any comments are welcome.

edit flag offensive delete link more

Comments

I had the impression the code would run and show the output...

ricardokl gravatar imagericardokl ( 2017-03-10 18:22:19 +0100 )edit

Your Answer

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

Add Answer

Question Tools

Stats

Asked: 2017-03-02 18:38:16 +0100

Seen: 871 times

Last updated: Mar 10 '17