[Tutor] How to convert the fortran logic to python definition

Alan Gauld alan.gauld at yahoo.co.uk
Thu Mar 26 19:15:41 EDT 2020


On 26/03/2020 21:16, SATYABRATA DATTA wrote:
> delta=18.*a*b*c*d-4.*(b**3)*d+b**2*c**2-4.*a*c**3-27.*a**2*d**2
>          print *, delta, a, b, c, d,d1
>          if(delta.gt.0.)then
>          x=2.*b**3-9.*a*b*c+27.*a**2*d
>          y=3.*sqrt(3.)*a*sqrt(delta)
>          if((x.lt.0.and.y.lt.0).or.(x.lt.0.and.y.gt.0))then
>          nii=2
>          else
>          nii=0
>          endif
> How to convert this fortran logic to python where delta(a,b,c,d)
> should return x(a,b,c,d) and y(a,b,c,d) for the next job when
> satisfied the conditions


My Fortran is extremely rusty and was never great to start with.
I've never used it in anger... But here goes a first attempt:


First of all I'm going to guess that your code is the body
of a Fortran function defined something like:

real function delta(a,b,c,d)
   delta=...your code...
   ...here
end function delta

from math import sqrt

def delta(a,b,c,d):
    dval = 18*a*b*c*d-4*(b**3)*d+b**2*c**2-4*a*c**3-27*a**2*d**2
    print( dval, a, b, c, d, d1)  # where is d1 defined?
    if dval > 0:
          x=(2*b**3) - (9*a*b*c) + (27*a**2)*d  # fortran precedence???
          y=3*sqrt(3)*a*sqrt(dval)
    if (x < 0 and y < 0) or (x < 0 and y > 0)
         nii=2
    else:
         nii=0   # no idea what part nii plays.
    return dval

Hopefully those with better Fortran skills can improve on that!


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list