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, ...


19

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}


2

Change rows and columns to taste. rows = 2; columns = 3; matr = RandomReal[{10^-12, 10^-6}, {rows, columns}]; (matr2 = Table[Row[{ "matrix(", i, ",", j, ") = ", FortranForm[matr[[i, j]]]}], {i, Length[matr]}, {j, Length[matr[[1]]]}] // Flatten) // Column


2

building on @rcollyer's answer: The main improvement here is we print the list of elements in a way that the whole list can be copied at once. (also handle arbitrary dimensions automatically ) SetAttributes[fortranprint, HoldFirst] fortranprint[array_Symbol] := fortranprint[array, SymbolName[Unevaluated[array]]]; fortranprint[array_, name_] := ...


2

There is no subtlety to this, but it works: Table[i j, {i, 5}, {j, 5}] MapIndexed[Print["a(", #2[[1]], "," , #2[[2]], ") = ", #1] &, %, {2}];


1

here is a stab/start at it.. (at least I hope to clarify what I think you are asking..) Clear[fort, fortranreal] fort[x_Real ] := fortranreal[x] fort[x_] := x StringReplace[ ToString@MapAll[fort, FortranForm[# ]] , Shortest[ "fortranreal(" ~~ s : __ ~~ ")" ] :> s <> "_db" ] & /@ { 2 a^2 , 2. a^2, 2. 10^-20 a^3, 2. 10^51 ...


1

There's special function for generating fortran-like forms: SetOptions[$Output, PageWidth -> 100] FortranForm[string] And certainly you can manage string manually. To insert new strings in the string you can write something like this: string = StringJoin[ToString /@ Range[1000]]; step = 10 StringInsert[string, "&&\n&&", Range[step, ...


1

You could also use Export to export to a file directly. Or use CopyToClipboard : matr = RandomReal[{10^-12, 10^-6}, {4, 6}]; matr2 = Table[FortranForm[matr[[i, j]]], {i, 4}, {j, 6}]; matr3 = ExportString[ Flatten@Table[ "matrix(" <> ToString[i] <> "," <> ToString[j] <> ") = " <> ToString[matr2[[i, j]]], {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