Ask Your Question

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

asked 2012-03-29 20:38:42 -0500

nrsaxena gravatar image

updated 2012-03-30 02:15:33 -0500

niles gravatar image

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

var('p N M W C')
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

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 "", line 10, in <module>
    exec compile(u'open("","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("dmFyKCdwIE4gTSBXIEMnKQphc3N1bWUoQywnaW50ZWdlcicpCnBjKHAsVykgPSAxIC0gKDEtcCleVwpwcmMocCxNLFcsQykgPSAxIC0gc3VtKGJpbm9taWFsKE0saSkqcGMocCxXKV5pKigxLXBjKHAsVykpXihNLWkpIGZvciBpIGluIHJhbmdlKEMpKQpwY29sKHAsTixNLFcsQykgPSAxIC0gKDEtcHJjKHAsTSxXLEMpKV5OCnBsb3QocGNvbChwLzIuMF4zMywyXjIwLDJeMTAsOCwxKSxwLDAsMTAwMCk="),globals())+"\\n"); execfile(os.path.abspath(""))
  File "", line 1, in <module>

  File "/private/var/folders/04/ymgbq2r15hx_q1knz7jxlqzc0000gn/T/tmpcfnMcC/", 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


moved comment...

nrsaxena gravatar imagenrsaxena ( 2012-03-30 04:19:32 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2012-03-30 02:14:41 -0500

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:


image description

edit flag offensive delete link more


p.s. If you do want to use symbolics, see the answer to this question: Note that plotting will still not work though, for reasons mentioned here:

niles gravatar imageniles ( 2012-03-30 02:30:39 -0500 )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 14:35:03 -0500 )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


Asked: 2012-03-29 20:38:42 -0500

Seen: 2,810 times

Last updated: Mar 30 '12