Ask Your Question

Why functions are much slower than var in some calculations?

asked 2011-12-14 21:44:55 +0200


I am doing symbolic calculations using sage. I want to take derivatives thus I defined some general functions. But I found the functions made other symbolic calculations much more slowly. For example:

a = function('a',t,x,y,z)
var('a1 a2 a3 a4')

# case 1
time matrix(SR, 2, [a,a2,a3,a4]).inverse()
# Time: CPU 0.23 s, Wall: 0.23 s

# case 2
time matrix(SR, 2, [a1,a2,a3,a4]).inverse()
# Time: CPU 0.01 s, Wall: 0.01 s

There is a big time difference simply because in case 1 a is a variable. When considering 4x4 matrices, the difference becomes huge.

I believe there are a few other command slowed down greatly by defining functions, because of the performance difference in my code. I haven't factored out them yet.

Any ideas to improve speed in this case? Thank you very much!

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2011-12-14 22:23:35 +0200

kcrisman gravatar image

Huh, that's weird. I tried it in several different ways and this is consistent.

I can tell you where the bottleneck is. It's in _echelon_in_place_classical. I called your matrices M and N, and:

sage: A1 = M.augment(M.parent().identity_matrix())
sage: time A1 = M.augment(M.parent().identity_matrix())
Time: CPU 0.00 s, Wall: 0.00 s
sage: time A2 = N.augment(N.parent().identity_matrix())
Time: CPU 0.00 s, Wall: 0.00 s
sage: time A1._echelon_in_place_classical()
Time: CPU 0.00 s, Wall: 0.01 s
sage: time A2._echelon_in_place_classical()
Time: CPU 0.24 s, Wall: 0.24 s

My (uninformed) guess is that there is a lot more Python that needs to be used with a "function" than with a "variable", slowing down the Cython. Anyone else?

edit flag offensive delete link more


My guess would be that the code tries to simplify the symbolic expression at one point, which means calling maxima via a pexpect interface. This is going to be much slower than just flinging the variables around.

Volker Braun gravatar imageVolker Braun ( 2011-12-16 06:33:23 +0200 )edit

answered 2011-12-15 08:09:24 +0200

@kcrisman: Thanks a lot for your result!

I am a new sage user thus I still have no idea how A2._echelon_in_place_classical() slows down cython and how to get it improved.

Just to mention, as a work around, now I use a 2d list instead of a matrix object, and hard-cored a formula for inverse matrix to avoid the problem (because I only use 4*4 symmetric matrix now).

On the other hand, it is still extremely interesting to find a solution to this problem :)

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


Asked: 2011-12-14 21:44:55 +0200

Seen: 412 times

Last updated: Dec 15 '11