Ask Your Question
1

A problem with fast_callable

asked 2021-10-21 09:09:39 +0200

RozaTh gravatar image

I am having a problem with getting consistent results from fast_callable (Sagemath 9.2). Consider the following example:

import numpy as np
x = var('x')
coords(x) = [2.0 * x,  0.0]

c0 = fast_callable(coords[0], vars=[x])
c1 = fast_callable(coords[1], vars=[x])

print(c0(np.array((1.0,2.0))))
print(c1(np.array((1.0,2.0))))

The result of the first print is [2. 4.] as expected but the result of the second print is just 0.0 instead of [0.0,0.0]. In my real application coords dynamically changes, and becuase of this I have to check whether a compomnant is constant or not before applying fast_callable.

1-Is there a way to get a consistent behavior out of fast_callable?

2-Is this a feature or a bug?

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2021-10-21 20:23:39 +0200

nbruin gravatar image

I'd say this is a feature. This is what these routines basically translate to:

sage: c0.op_list()                                                              
[('load_arg', 0), ('load_const', 2.00000000000000), 'mul', 'return']
sage: c1.op_list()                                                              
[('load_const', 0.000000000000000), 'return']

As you can see, c0 translates to multiplying the argument by 2, but c1 translates to returning the constant 0.00: so no chance for np.array to distribute. What you'd basically want is for c0 to multiply its argument by 0.

For the system to do something more intelligent, you'd have to give fast_callable more information about the domain and codomain of the map you'd like to construct. I'm not so sure that's implemented, though.

In your scenario, it seems the undesired behaviour would only arise for constant expressions, so the easy solution is probably to special case those.

edit flag offensive delete link more

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: 2021-10-21 09:09:39 +0200

Seen: 66 times

Last updated: Oct 21 '21