Ask Your Question
1

TypeError: range() integer end argument expected, got sage.symbolic.expression.Expression.

asked 2012-03-30 03:38:42 +0100

nrsaxena gravatar image

updated 2012-03-30 09:15:33 +0100

niles gravatar image

I am trying to generate some plots using the following function definition

var('p N M W C')
assume(C,'integer')
pc(p,W) = 1 - (1-p)^W
prc(p,M,W,C) = 1 - sum(binomial(M,i)*pc(p,W)^i*(1-pc(p,W))^(M-i) for i in range(C))
pcol(p,N,M,W,C) = 1 - (1-prc(p,M,W,C))^N
plot(pcol(p/2.0^33,2^20,2^10,8,1),p,0,1000)

However, I get range TypeError even when I have declared that C is integer type. See error listing below:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_34.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCdwIE4gTSBXIEMnKQphc3N1bWUoQywnaW50ZWdlcicpCnBjKHAsVykgPSAxIC0gKDEtcCleVwpwcmMocCxNLFcsQykgPSAxIC0gc3VtKGJpbm9taWFsKE0saSkqcGMocCxXKV5pKigxLXBjKHAsVykpXihNLWkpIGZvciBpIGluIHJhbmdlKEMpKQpwY29sKHAsTixNLFcsQykgPSAxIC0gKDEtcHJjKHAsTSxXLEMpKV5OCnBsb3QocGNvbChwLzIuMF4zMywyXjIwLDJeMTAsOCwxKSxwLDAsMTAwMCk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>

  File "/private/var/folders/04/ymgbq2r15hx_q1knz7jxlqzc0000gn/T/tmpcfnMcC/___code___.py", line 6, in <module>
    __tmp__=var("p,M,W,C"); prc = symbolic_expression(_sage_const_1  - sum(binomial(M,i)*pc(p,W)**i*(_sage_const_1 -pc(p,W))**(M-i) for i in range(C))).function(p,M,W,C)
TypeError: range() integer end argument expected, got sage.symbolic.expression.Expression.
edit retag flag offensive close merge delete

Comments

moved comment...

nrsaxena gravatar imagenrsaxena ( 2012-03-30 11:19:32 +0100 )edit

1 Answer

Sort by ยป oldest newest most voted
1

answered 2012-03-30 09:14:41 +0100

niles gravatar image

The range function needs an actual integer, not a symbolic expression with an additional attribute assigned by the assume function. Is there a reason you need to be working with symbolic expressions at all instead of Python functions? Consider the following:

def pc(p,W):
    return 1-(1-p)^W
def prc(p,M,W,C):
    return 1 - sum(binomial(M,i)*pc(p,W)^i*(1-pc(p,W))^(M-i) for i in range(C))
def pcol(p,N,M,W,C):
    return 1 - (1-prc(p,M,W,C))^N

These are Python functions, so you don't need to declare the variables. The return expressions will be evaluated when you supply actual numbers for the arguments (as is done by the plot function). One difference is that

print pcol

will return

<function pcol at 0x4325500>

instead of a symbolic expression indicating the arithmetic the function performs. But I don't see why this would matter if you just want to plot a graph. On that note, to plot the graph you just need to declare the variable that you're plotting:

var('p')
plot(pcol(p/2.0^33,2^20,2^10,8,1),p,0,1000)

image description

edit flag offensive delete link more

Comments

p.s. If you do want to use symbolics, see the answer to this question: http://ask.sagemath.org/question/1182/easy-beginner-sum-problemneed-a-summation-variable. Note that plotting will still not work though, for reasons mentioned here: http://ask.sagemath.org/question/75/plot-error-a-free-variable

niles gravatar imageniles ( 2012-03-30 09:30:39 +0100 )edit

Thanks that solves the issue for me. I went the symbolic route thinking that floating-point round-off errors will be managed better by the sage tools.

nrsaxena gravatar imagenrsaxena ( 2012-03-30 21:35:03 +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: 2012-03-30 03:38:42 +0100

Seen: 4,080 times

Last updated: Mar 30 '12