Ask Your Question

Is it possible to get fortran code for an expression?

asked 2018-03-14 02:55:01 +0200

tomcho gravatar image

Hello, is it possible to get the fortran code for expressions in SageMath?

Here's what I mean:

sage: var('a b x y')
( a, b, x, y )
sage: eq = x^a + 3/2*y^b
sage: get_fortran(eq)
x**a + 3.0/2.0*y**b

Right now the best I can do as a workaround is:

sage: eq.__str__().replace('^', '**')
x**a + 3/2*y**b

but the numbers appear as integers, which makes the division be performed as integer division in Fortran, which forces me to manually change that. Is there a function I can use to get what I want?


edit retag flag offensive close merge delete


Sage doesn't use Fortran for very many things - notably R and perhaps some other numerical routines. So there isn't a "canonical" Fortran version of most things. You may also be interested in f2py but of course this would be the opposite.

kcrisman gravatar imagekcrisman ( 2018-03-14 03:40:30 +0200 )edit

But really the question is, what do you want this for? Fortran doesn't really have "symbolic variables" and probably would be very inefficient for a lot of the things you want to do; if you ask a more specific question about what you need it for, perhaps there is something more targeted we can offer Fortran-wise.

kcrisman gravatar imagekcrisman ( 2018-03-14 03:41:43 +0200 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2018-03-14 23:01:48 +0200

Emmanuel Charpentier gravatar image

Try :


One of these may fulfill your needs..

The right question is, of course, why bother with Fortran ?

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: 2018-03-14 02:55:01 +0200

Seen: 473 times

Last updated: Mar 14 '18