ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Wed, 14 Mar 2018 23:01:48 +0100Is 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
Wed, 14 Mar 2018 02:55:01 +0100https://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=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.Wed, 14 Mar 2018 03:40:30 +0100https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?comment=41529#post-id-41529Comment 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.Wed, 14 Mar 2018 03:41:43 +0100https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?comment=41530#post-id-41530Answer 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 23:01:48 +0100https://ask.sagemath.org/question/41525/is-it-possible-to-get-fortran-code-for-an-expression/?answer=41553#post-id-41553