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)
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)
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)