Tag Info

Hot answers tagged

22

OK, here's a quick go using Experimental`OptimizeExpression, which is used internally by Compile. I'm not claiming that this is a polished solution to your problem, but hopefully it can be used as a place to start. (Also, forgive any Fortran mistakes, I haven't looked at Fortran for a long time...) xx = x /. Solve[{x^2 + y^2 + z^2 == 1, x + y + z == a, ...


17

Here are three very simple examples to show how to call a Fortran subroutine using LibraryLink. First the subroutine is compiled into object file. Then a wrapper is used to call the Fortran subroutine and compiled into dynamic library. At the end, the library is loaded into Mathematica and run. In the examples Mathematica Version 8 is used. FIRST EXAMPLE ...


7

Since the CellEvaluationFunction is supposed to accept two arguments, the second being the format type, I recommend defining: GFortran[inp_, _] := . . . and then using it directly: CellEvaluationFunction -> GFortran


6

For simplicity and elegance I use Verbeia's nice Riffle-ing for continuation. While it is possible to format real numbers as requested in FortranForm I always thought that this is way too complicated in Mathematica (the code below is slightly modified from my old FeynCalc-Write2 function described here myFortrantoggle=False; (* needed to avoid recursion *) ...


6

You can get a long way to your goal using FortranForm in Mathematica and some replacement rules. ToExpression and ToString are very useful for this kind of application. (I'm giving this one a name for later use below.) step1 = FortranForm[(-48*Sqrt[6]* E^((-2*S[1, x]^2)/bx - (S[1, y]^2)/bx - (S[1, z]^2)/ bz - (S[2, x]^2)/bx - (S[2, y]^2)/bx - (S[2, ...


4

Mathematicas invocation of the compiler doesn't know about where to find the Fortran library. With a little help, however, we can point the way. Mind you this was done on a Mac but the Linux variant of Unix will behave similar. Needs["CCompilerDriver`"]; CreateLibrary[{"MMA.cc", "fadd.o"}, "myadd", "Debug" -> True, "TargetDirectory" -> ".", ...


3

FortranForm is really pretty limited. You need to do something like this: A = {{1, 1. 10^7}, {2., 1}} StringJoin@{"A=reshape((/", Riffle[ ToString[FortranForm[#]] & /@ Flatten[A] , ","], "/),shape(a))"} "A=reshape((/1,1.e7,2.,1/),shape(a))" or StringJoin@{"data a/", Riffle[ ToString[FortranForm[#]] & /@ ...


3

Another way data = {{"(0.277336296055697,0.120000000000000E+000)"}, {"(-1.489297392708939E-002,0.340000000000000E+000)"}}; data = Flatten[StringCases[#, "(" ~~ x__ ~~ "," ~~ y__ ~~ ")"->{x, y}]&/@ a,Infinity]; Complex @@@ Partition[Internal`StringToDouble[#] & /@ data, 2]


2

Yet another one: n = {{"(0.277336296055697,0.120000000000000E+000)"}, \ {"(-1.489297392708939E-002,0.340000000000000E+000)"}}; Complex @@@ ToExpression[StringReplace[Flatten[n], {"(" -> "{", ")" -> "}", "E" -> "*10^"}]] {0.277336 + 0.12 I, -0.014893 + 0.34 I}


1

a = {{"(0.277336296055697,0.120000000000000E+000)"}, {"(-1.489297392708939E-002,0.340000000000000E+000)"}}; Complex @@ (ToExpression /@ {StringDrop[First@#, 1], StringTake[Last@#, 5]}) & [Flatten@StringSplit[a[[#]], ","]] & /@ Range@Length@a {0.277336 + 0.12 I, -6.04833 + 0.34 I}



Only top voted, non community-wiki answers of a minimum length are eligible