I have recently asked a question concerning string replacement rules for FortranForm
. This question has been answered by Carl Woll - thanks for that!
Now as I was implementing this I was thinking of overcoming number definitions and simply replace real numbers with something like 1. --> 1._rkind
. This replacement should work for all fractions, powers, etc.
I have found a post where this task has been tackled but unfortunately my Mathematica skills are very basic and I do not really understand what is going on there. Anyway, I have tried and this is what I was coming up so far:
G = -4/3 pi (-RAi + RAip1) (RAi^2 + RAi RAip1 + RAip1^2) rhoAi;
testReplace[someEq_] := Module[{trep},
Clear[fort, fortranreal];
fort[x_Real] := fortranreal[x];
fort[x_] := x;
trep = StringReplace[ToString@MapAll[fort, FortranForm[#]], Shortest["fortranreal(" ~~ s : __ ~~ ")"] :> s <> "_rkind"] & /@ someEq
];
This leaves me with the (unfortunately not quite useful) output:
(-4*pi*(-RAi + RAip1)*(RAi**2 + RAi*RAip1 + RAip1**2)*rhoAi)/3.
because obviously 4
is not an integer and would cause problems with the Fortran compiler. Next thing I've tried was replacing someEq
with N[someEq]
in the Module output which then leaves me with:
-1.3333333333333333_rkind pi RAi**2 + RAi*RAip1 + RAip1**2 RAip1 + RAi*-1._rkind rhoAi
where all the brackets are stripped away and e.g. -RAi
gets replaced with RAi*-1._rkind
.
What I am trying to achieve is evaluating all fractions and then attach _rkind
to all real values. Can anyone tell me what am I doing wrong?
3.
is missing something like3._rkind
as well. $\endgroup$3.
is also unnecessary because its an integer value. Only if you have actual decimals ( say0.3
) , then you need to specify the type if you want it to be other than single precision. $\endgroup$