I'm trying to use Mathematica for building complex derivatives for my Fortran code. Therefore I have to do some wild string replacements to make that work. Anyway, I got quite far already but now it would come in handy if I could automatically overwrite let's say the Power
method.
What I want is that Mathematica should replace e.g. x^(1/4)
with x**z14
on FortranForm
. What does work is this:
i=1;
j=4;
Unprotect[Power];
Power /: Format[Power[x_, i/j], FortranForm]:=x^ToExpression["z" <> ToString[i] <> ToString[j]]
Protect[Power];
ToString[Power[x, i/j], FortranForm]
This will give x**z14
as expected.
If I try to automate the process for more fractions
Unprotect[Power];
For[i = 1, i <= 9, i++,
For[j = 1, j <= 9, j++,
If[j > 1 && j != i && Mod[i, j] != 0,
Power /: Format[Power[x_, i/j], FortranForm]:=x^ToExpression["z"<> ToString[i] <> ToString[j]];
,Null
]
]
]
]
Protect[Power];
ToString[Power[x, i/j], FortranForm]
I get x**z1010
instead?
Can anyone help me and tell me what I'm doing wrong? Or is this even possible?
Thanks!