-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)
8
  • Why vote down? if you don't know just ignore or add a comment Commented May 5, 2016 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? Commented May 5, 2016 at 19:25
  • 1
    I can tell you what each one of those lines means in Fortran, but I can tell you essentially nothing about Python... Commented May 5, 2016 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... Commented May 5, 2016 at 19:52
  • 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. Commented May 5, 2016 at 22:47

1 Answer 1

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) 
Sign up to request clarification or add additional context in comments.

2 Comments

The question asks about the line DIMENSION Z(*). What can we say about z in terms of its type and size (or other attributes)?
Another commenter pointed out already that this statement by itself is not meaningful.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.