-2

How can I translate the following Fortran declarations to Python (numpy array)?

INTEGER, ALLOCATABLE::          I(:)
DOUBLE PRECISION, ALLOCATABLE:: A(:)
DOUBLE PRECISION, ALLOCATABLE:: B(:,:)
DIMENSION Z(*)
DIMENSION X(N)
francescalus
  • 30,576
  • 16
  • 61
  • 96
ravi
  • 1,078
  • 2
  • 17
  • 31
  • Why vote down? if you don't know just ignore **or add a comment** – ravi May 05 '16 at 19:02
  • I can't speak for the downvoter, but that code doesn't actually _do_ anything. What in particular don't you understand? – francescalus May 05 '16 at 19:25
  • My fortran knowledge is very limited. All i want to know is in fortran it seems like DOUBLE PRECISION A(:) is the way to define a empty array of doubles. In python should it be A = array([]) – ravi May 05 '16 at 19:35
  • 1
    I can tell you what each one of those lines means in Fortran, but I can tell you essentially nothing about Python... – francescalus May 05 '16 at 19:49
  • 2
    The question doesn't make sense. Python doesn't use declarations. You should look for the allocate statement and translate that one to something like A = np.array... – Vladimir F Героям слава May 05 '16 at 19:52
  • @francescalus Could you please explain each of the line what it means so that i can translate them to python. Thanks – ravi May 05 '16 at 19:59
  • 1
    This ia not how programming works. You cannot translate line by line, you have to understand what the whole part of program does. But you don't show enough code. Your fourth line for example doesn't make any sense without proper context. – Vladimir F Героям слава May 05 '16 at 22:47
  • I don't get the downvotes. Maybe adding `python` would keep out the commenters who don't know what `numpy` means. Going from Fortran to numpy should be mostly doable for smaller codes, since Fortran is strictly procedural. One thing Python doesn't have is a GOTO statement. See http://stackoverflow.com/questions/18222561/how-can-i-easily-convert-fortran-code-to-python-code-real-code-not-wrappers – roadrunner66 May 05 '16 at 23:34

1 Answers1

1

To declare an integer, just assign like so:

i=1

To declare a float, assign like so :

a=1.0

For numpy arrays better to know the size ahead of time :

b=np.zeros((100,100)) # a 100x100 array, initialized with zeros
x=np.ones(1000)   # a 100 array , initialized with ones

If you don't know the size of the array ahead of time, assign an empty list like so:

xlist=[]

then fill the list (say inside a loop) like so:

xlist.append(5.34) 

Why this is the preferred method see this SO question.

You can make lists of lists, with a 2nd loop, as in this example of a multiplication table:

aa=[]
for i in range(10):
     a=[]
     for j in range(10):
         a.append(i*j)
     aa.append(a)

When you are done creating the list (arbitrary dimensionality) and want to be fast, convert them to numpy arrays like so

my2darray=np.array(aa) 
Community
  • 1
  • 1
roadrunner66
  • 7,772
  • 4
  • 32
  • 38