Ask Your Question

Revision history [back]

Why functions are much slower than var in some calculations?


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!