ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Mar 2018 17:01:48 -0500Is it possible to get fortran code for an expression?https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/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?
Cheers
Tue, 13 Mar 2018 20:55:01 -0500https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/Comment by kcrisman for <p>Hello, is it possible to get the fortran code for expressions in SageMath?</p>
<p>Here's what I mean:</p>
<pre><code>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
</code></pre>
<p>Right now the best I can do as a workaround is:</p>
<pre><code>sage: eq.__str__().replace('^', '**')
x**a + 3/2*y**b
</code></pre>
<p>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?</p>
<p>Cheers</p>
https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?comment=41530#post-id-41530But 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.Tue, 13 Mar 2018 21:41:43 -0500https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?comment=41530#post-id-41530Comment by kcrisman for <p>Hello, is it possible to get the fortran code for expressions in SageMath?</p>
<p>Here's what I mean:</p>
<pre><code>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
</code></pre>
<p>Right now the best I can do as a workaround is:</p>
<pre><code>sage: eq.__str__().replace('^', '**')
x**a + 3/2*y**b
</code></pre>
<p>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?</p>
<p>Cheers</p>
https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?comment=41529#post-id-41529Sage 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](http://doc.sagemath.org/html/en/thematic_tutorials/numerical_sage/f2py.html) but of course this would be the opposite.Tue, 13 Mar 2018 21:40:30 -0500https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?comment=41529#post-id-41529Answer by Emmanuel Charpentier for <p>Hello, is it possible to get the fortran code for expressions in SageMath?</p>
<p>Here's what I mean:</p>
<pre><code>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
</code></pre>
<p>Right now the best I can do as a workaround is:</p>
<pre><code>sage: eq.__str__().replace('^', '**')
x**a + 3/2*y**b
</code></pre>
<p>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?</p>
<p>Cheers</p>
https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?answer=41553#post-id-41553Try :
maxima.fortran?
maxima.f90?
One of these may fulfill your needs..
The right question is, of course, why bother with Fortran ?
Wed, 14 Mar 2018 17:01:48 -0500https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?answer=41553#post-id-41553