Ask Your Question
0

fast_callable with lists

asked 2016-07-14 09:26:07 +0100

GrantE gravatar image

I tried using the fast_callable function with a List and keep getting the "Unable to convert...to an integer" error. I am using the list to simply pass parameters to the function.

Are there any special rules or techniques for passing Lists or Arrays to fast-callable ?

Thanks.

edit retag flag offensive close merge delete

Comments

1

Hi! Can you give a minimal working (or not working, I guess) code example of exactly what you are trying to do?

kcrisman gravatar imagekcrisman ( 2016-07-14 13:22:07 +0100 )edit

Hi, thanks for your comment - I should've included a working snippet example of what I am trying to do.

  1. This snippet works: k0a=1.8; func(m)=bessel_J(m^2,k0a); fmax=fast_callable(func, vars=[m]); print func(1.); print fmax(1.);

0.581516951731165; 0.581516951731165

  1. This snippet doesn't work: k0a=1.8; vn=[0.0, 1.0, 4.0, 9.0]; func(m)=bessel_J(vn[m],k0a); fmax=fast_callable(func, vars=[m]);

Traceback (click to the left of this block for traceback) ... TypeError: unable to convert m to an integer

GrantE gravatar imageGrantE ( 2016-07-15 07:24:48 +0100 )edit

Even better, would you edit your question and add the example there?

To display blocks of code, either indent them with 4 spaces, or select the corresponding lines and click the "code" button (the icon with '101 010'). Can you edit your question to do that?

To display inline code, surround it within "backticks" or "backquotes" `.

slelievre gravatar imageslelievre ( 2016-07-17 04:08:57 +0100 )edit

2 Answers

Sort by ยป oldest newest most voted
1

answered 2016-07-17 04:16:00 +0100

slelievre gravatar image

The error you encounter is not with fast_callable, but with defining a symbolic function.

In general, it is recommended to write one instruction per line.

This would let you see that while your first example works:

sage: k = 1.8
sage: func(m) = bessel_J(m^2, k)
sage: fmax = fast_callable(func, vars=[m])
sage: print func(1.)
0.581516951731165
sage: print fmax(1.)
0.581516951731165

the second example fails at the stage of defining func(m):

sage: k = 1.8
sage: v = [0.0, 1.0, 4.0, 9.0]
sage: func(m) = bessel_J(v[m], k)
Traceback (most recent call last)
...
TypeError: unable to convert m to an integer

A simpler example of this failure is as follows:

sage: l = [1, 2, 3]
sage: f(m) = l[m]
Traceback (most recent call last)
...
TypeError: unable to convert m to an integer
edit flag offensive delete link more

Comments

Thanks for your comment!
It appears that the problem isn't actually with using a List with fast_callable but when I try to use the List index as a variable. I was able to do a workaround by defining a new function by summing all of the terms which refer to this List in the function using a simple loop. I am then able to use fast_callable with this new function. The speedup is almost 10-fold !

GrantE gravatar imageGrantE ( 2016-07-18 07:07:26 +0100 )edit
0

answered 2016-07-14 13:22:01 +0100

kcrisman gravatar image

I personally suspect you just can't do it without extending fast_callable from its current signature. See the doc:

Currently, x can be an expression object, an element of SR, or a (univariate or multivariate) polynomial; this list will probably be extended soon.

However, hopefully someone else will provide a workaround, perhaps involving creating a list function or something.

edit flag offensive delete link more

Comments

Unless you can make a vector into an Expression?

kcrisman gravatar imagekcrisman ( 2016-07-14 13:22:36 +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

1 follower

Stats

Asked: 2016-07-14 09:20:41 +0100

Seen: 716 times

Last updated: Jul 17 '16