0
$\begingroup$

how to to include following formula in the FortranForm in a correct format?

R = 
  Compile[{{y}}, 
    Piecewise[{{1.31446 Tan[1.58986 y] + 0.91209 y, 
       Abs[y] < 0.84136}, {1./(Sign[y] - y), 
       0.841360 <= Abs[y] < 1.}}], 
    RuntimeOptions -> "EvaluateSymbolically" -> False 
    , Parallelization -> True, 
    CompilationTarget -> "C"] //FullSimplify;
 DR=D[R[a],a];
FortranForm[DR]
$\endgroup$
11
  • 1
    $\begingroup$ I don't know of any code to create Fortran from WL compiled code, but perhaps you could use CCodeGenerate instead to create C code? $\endgroup$
    – Carl Woll
    Commented Dec 28, 2017 at 17:18
  • $\begingroup$ @CarlWoll how about uncompiled version of the code... unfortunately the code is in fortran :) $\endgroup$
    – user49047
    Commented Dec 28, 2017 at 17:34
  • $\begingroup$ Applying D to your function R is meaningless. If you want to see the C-code for R, use ExportString[R,"C"]. You have to search a bit for the relevant part of the code, though. Afterwards, you can easily translate it to fortran by hand (at least the standard arithmetics can be copied)... $\endgroup$ Commented Dec 28, 2017 at 17:38
  • 1
    $\begingroup$ Can’t you call a C function from Fortran? $\endgroup$
    – Carl Woll
    Commented Dec 28, 2017 at 17:39
  • $\begingroup$ can you explain better what you are actually trying to do? I cant see any reason to use Compile at all if the goal is to create fortran code. $\endgroup$
    – george2079
    Commented Dec 28, 2017 at 19:52

1 Answer 1

2
$\begingroup$

this is simple enough to do by hand..you will spend more time trying to automate the process than just typing the code out.

First a little manipulation of the original expression to get rid of the Abs

cons = {
  c1 -> 1.31446,
  c2 -> 1.58986,
  c3 -> 0.91209,
  c4 -> 0.84136}
dp = Piecewise[
  {{c1 Tan[c2 y] + c3 y, Abs[y] < c4},
   {1/(1 - y), c4 <= y < 1},
   {-1/(1 + y), -1 < y <= -c4 }}]
dd = D[dp, y]

enter image description here

here you can use FortranForm to pull out parts of the expression:

  FortranForm[dd[[1, 1, 1]]]

c3 + c1*c2*Sec(c2*y)**2

just do that as needed and paste to the code as you write:

    real function df(y)
    implicit none
    real::y
    real,parameter::c1=1.31446,c2=1.58986,c3=0.91209,c4=0.84136
    if(abs(y).gt.1)then
      df=0
    elseif( y.le.-c4)then
      df=(1+y)**(-2)
    elseif(y.gt.c4)then
      df=(1-y)**2
    else
      df=c3 + c1*c2*Sec(c2*y)**2
    endif
   end
$\endgroup$
1
  • $\begingroup$ @geogerge2079, how can we define real*8 value: e.g. 1.d0, 2.d0 etc??? it could be useful in fortran coding $\endgroup$
    – ABCDEMMM
    Commented Oct 29, 2018 at 1:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.