MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Ask for help: how to let mathematica output Fortran

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129445] Re: Ask for help: how to let mathematica output Fortran
  • From: Gerry Flanagan <gvflanagan at gmail.com>
  • Date: Mon, 14 Jan 2013 23:28:44 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20130114050030.2C9F068C7@smc.vnet.net>

There's an old package in the Wolfram library, Optimize.m 
(http://library.wolfram.com/infocenter/MathSource/3947/) that does 
exactly what you want. Comes up with an warning message in V8, but it 
still seems to function. When I used to do lots of Mathematica to 
Fortran work, I found the package Format.m 
(http://library.wolfram.com/infocenter/MathSource/60/) very valuable. As 
downloaded, it complains a lot about recent Mathematica versions, but I managed 
to keep it functioning with some modification.

On 1/14/2013 12:00 AM, Tang Laoya wrote:
> Dear all,
>
> I am new to mathematica and I am trying to let mathematicat output some express to Fortran code. However, it seems that it will output some duplicated expressions so that these expressions would be calculated more than once. Is there any good method to define a rule to let mathematica recognize duplicated expressions and assign them to a new variable and then simplify the Fortran code?
>
> Thanks,
> Tang Laoya
>
>
>
>
> P.S:
>
> The following is a test code of mathematica and the solution of x, y and z contains many duplicated expression, for example, (-36*a + 20*a**3 - 216*b)**2.
>
> sol = Solve[{x^2 + y^2 + z^2 == 1, x + y + z == a , x*y*z == b}, {x,
>      y, z}];
> xx = x /. sol
> yy = y /. sol
> zz = z /. sol
> Print["Writing Fortran Code . . . : / "];
> SetDirectory["F:\\tang\\mathtest"];
> strm = OpenWrite["test.f90", FormatType -> FotranForm,
>     PageWidth -> 70];
> (* write subroutine of invisopar*)
> WriteString[strm, "subroutine test(x,y,z,a,b)\n"];
> WriteString[strm, "implicit none\n"];
> WriteString[strm, "real*8::x,y,z,a,b\n"];
> nroot = Length[xx];
> For[ii = 1, ii <= nroot,
>    WriteString[strm,
>     "x = " <> ToString[FortranForm[xx[[ii]]]] <> "\n"]; ii++];
> For[ii = 1, ii <= nroot,
>    WriteString[strm,
>     "y = " <> ToString[FortranForm[yy[[ii]]]] <> "\n"]; ii++];
> For[ii = 1, ii <= nroot,
>    WriteString[strm,
>     "z = " <> ToString[FortranForm[zz[[ii]]]] <> "\n"]; ii++];
> WriteString[strm, "end subroutine\n"];
> Close[strm];
> Print["Finished Writing Fortran Code . . . : / "];
>
>
> When the test.f90 is outputted, the first solution of x is:
> x = a/3. + (-6 + 2*a**2)/(3.*2**0.6666666666666666*(-36*a + 20*a**3 + Sqrt(4*(-6 + 2*a**2)**3 + (-36*a + 20*a**3 - 216*b)**2) - 216*b)**0.3333333333333333) - (-36*a + 20*a**3 + Sqrt(4*(-6 + 2*a**2)**3 + (-36*a + 20*a**3 - 216*b)**2) - 216*b)**0.3333333333333333/(6.*2**0.3333333333333333)
>
> I would like that the code is as follows:
> tmp0 = 216*b
> tmp1 = -36*a + 20*a**3
> tmp2 = (tmp1 - tmp0 )**2
> tmp3 = -6 + 2*a**2
> tmp4 = 4*tmp3**3
> tmp5 = Sqrt(tmp4 + tmp2 )
> tmp6 = tmp1 + tmp5 - tmp0
> tmp7 = tmp6**0.3333333333333333
> tmp8 = 2**0.6666666666666666
>
> x = a/3. + tmp3 /(3.*tmp8 *tmp7 ) - tmp7/(6.*tmp8)
>
>
>




  • Prev by Date: Re: NSolve output
  • Next by Date: Re: NSolve output
  • Previous by thread: Ask for help: how to let mathematica output Fortran code with fewest
  • Next by thread: Re: Ask for help: how to let mathematica output Fortran code with fewest