From: Gideon on
I have two, possibly related, questions on the FortranForm command.

1. Is it possible to get Fortran 90/95 style output? In particular,
I'd like to have long lines broken up with the end of line &
character, rather than the sixth column indicator as it is now.

2. Is it possible to get it to output decimals where it has whole
numbers? For instance, in some code I have, a factor of sqrt(2)
appears. FortranForm renders this as Sqrt(2). My compiler (gfortran
4.4) doesn't like this because it sees this as the square root of an
integer. I have been resolving this with find-replace, and suspect I
could also deal with it by compiler flags, but it's annoying.

From: David Reiss on
I think that the short answer to your questions is that FortranForm is
a very old component of Mathematica that I suspect has not been
updated since version 1.0 (and toe documentation indicates this). So,
for question # 1 the answer is no. For question #2 you need to write
a function of your own that searches out exact expressions and
substitutes the numerical values with the appropriate precision & c.

Hope this helps,

David


On May 4, 6:30 am, Gideon <gideon.simp...(a)gmail.com> wrote:
> I have two, possibly related, questions on the FortranForm command.
>
> 1. Is it possible to get Fortran 90/95 style output? In particular,
> I'd like to have long lines broken up with the end of line &
> character, rather than the sixth column indicator as it is now.
>
> 2. Is it possible to get it to output decimals where it has whole
> numbers? For instance, in some code I have, a factor of sqrt(2)
> appears. FortranForm renders this as Sqrt(2). My compiler (gfortran
> 4.4) doesn't like this because it sees this as the square root of an
> integer. I have been resolving this with find-replace, and suspect I
> could also deal with it by compiler flags, but it's annoying.


From: Gerry Flanagan on
There is a library package available by Mark Sofroniou.
http://library.wolfram.com/infocenter/MathSource/60/
It is a much more general approach than the built-in FotranForm, and
there are options to do just what you're asking. Unfortunately, it
hasn't been maintained for the latest Mathematica versions (to my knowledge), so
you may get a few warnings and errors, particularly related to
subexpression optimization. I've generally managed work-arounds.
Gerry F.


On 5/4/2010 6:30 AM, Gideon wrote:
> I have two, possibly related, questions on the FortranForm command.
>
> 1. Is it possible to get Fortran 90/95 style output? In particular,
> I'd like to have long lines broken up with the end of line&
> character, rather than the sixth column indicator as it is now.
>
> 2. Is it possible to get it to output decimals where it has whole
> numbers? For instance, in some code I have, a factor of sqrt(2)
> appears. FortranForm renders this as Sqrt(2). My compiler (gfortran
> 4.4) doesn't like this because it sees this as the square root of an
> integer. I have been resolving this with find-replace, and suspect I
> could also deal with it by compiler flags, but it's annoying.
>
>
>


From: slawek on

U=BFytkownik "Gideon" <gideon.simpson(a)gmail.com> napisa=B3 w wiadomo=B6cigrup
dyskusyjnych:hrosvr$5sc$1(a)smc.vnet.net...
> I have two, possibly related, questions on the FortranForm command.
>
> 1. Is it possible to get Fortran 90/95 style output? In particular,
> I'd like to have long lines broken up with the end of line &
> character, rather than the sixth column indicator as it is now.

The output in the "fortran form" is not a "clean" Fortran IV/77/90/95/2003
source. :(

A simple gawk script should help.

BTW, CForm does not give "clean" C source.

> 2. Is it possible to get it to output decimals where it has whole
> numbers? For instance, in some code I have, a factor of sqrt(2)

Use N, here is a example (20-digit acc.):

N[yourExpressionHere,20] // FortranForm
N[Sqrt[2]] // FortranForm

slawek