Re: reshape consist with Fortran

From: Dennis Shea <shea_at_nyahnyahspammersnyahnyah>
Date: Wed Oct 20 2010 - 10:42:42 MDT

I would like to modify my response to this question.
Fortran's "reshape" and NCL's "onedtond" are the same.

===
However, the way the data are *referenced* is different.

Consider a sequence of numbers in the following order
occupying contiguous memory

1
1
2
3
5
8

Now consider NCL's 0-based representation
and fortran's (default) 1-based representation.
A one-dimensional array would be referenced as

  NCL F
(0) 1 (1) 1
(1) 1 (2) 1
(2) 2 (3) 2
(3) 3 (4) 3
(4) 5 (5) 5
(5) 8 (6) 8

No difference, except for the 0/1-based representations.
Easy!!!!!

======================================================
Multidimensional arrays: Regardless of the languages,
one 'rule' should always be kept in mind:

Fastest varying of one language maps into the fastest
varying of the other language.
======================================================
In NCL, which is row major, the fastest varying subscript
is the rightmost subscript, the slowest varying is the leftmost;
In fortran, which is column major, the opposite is true.

Consider:

NCL(2,3) array: 2-rows, 3-columns

and, two different arrays in fortran

fortran: FA(2,3) and FB(3,2)

    N FA FB
(0,0) 1 (1,1) 1 (1,1) 1
(0,1) 1 (2,1) 1 (2,1) 1
(0,2) 2 (1,2) 2 (3,1) 2
(1,0) 3 (2,2) 3 (1,2) 3
(1,1) 5 (1,3) 5 (2,2) 5
(1,2) 8 (2,3) 8 (3,2) 8

In 'memory' the sequence is the same but how the
values are referenced in the 2 languages is different.
So, the more natural mapping is NCL(2,3) <==> FB(3,2)

===
Now consider, a 'real world' example

NCL: x(time,lat,lon) with sizes (2,3,4)
             maps naturally into
F: x(lon,lat,time) with sizes (4,3,2)

The actual memory order does *NOT* need to be altered.

NCL: x = new ( (/2,3,4/),"float")
F: real x(4,3,2)

The different languages just reference the elements
in memory differently. Again: fastest varying maps in
fastest varying!!!

   x(0,0,0) <==> x(1,1,1)
   x(0,0,1) <==> x(2,1,1)
   x(0,0,2) <==> x(3,1,1)
   x(0,0,3) <==> x(4,1,1)

   x(0,1,0) <==> x(1,2,1)
   x(0,1,1) <==> x(2,2,1)
   x(0,1,2) <==> x(3,2,1)
   x(0,1,3) <==> x(4,2,1)

   x(0,2,0) <==> x(1,3,1)
   x(0,2,1) <==> x(2,3,1)
   x(0,2,2) <==> x(3,3,1)
   x(0,2,3) <==> x(4,3,1)

   x(1,0,0) <==> x(1,1,2)
   x(1,0,1) <==> x(2,1,2)
   x(1,0,2) <==> x(3,1,2)
   x(1,0,3) <==> x(4,1,2)

   x(1,1,0) <==> x(1,2,2)
   x(1,1,1) <==> x(2,2,2)
   x(1,1,2) <==> x(3,2,2)
   x(1,1,3) <==> x(4,2,2)

   x(1,2,0) <==> x(1,3,2)
   x(1,2,1) <==> x(2,3,2)
   x(1,2,2) <==> x(3,3,2)
   x(1,2,3) <==> x(4,3,2)

An example

      REAL, DIMENSION(6) :: B
      REAL, DIMENSION(3,2) :: XA
      REAL, DIMENSION(2,3) :: XB
      B = (/ 1, 1, 2, 3, 5, 8 /)
      XA= RESHAPE( B, (/ 3,2 /) )
      XB= RESHAPE( B, (/ 2,3 /) )

      print *, "B=",B
      print *, "===
      print *, "XA=",XA
      print *, "==="
      print *, "XB=",XB
      end

All will 'look' the same.

>
> Dennis Shea wrote:
>> I would have thought they are the same.
>> Please state why they are not the same.
>>
>> fortran is column major
>> NCL (like C) is row major
>>
>> fortran
>>
>> REAL, DIMENSION(6) :: B
>> REAL, DIMENSION(2,3) :: C1
>> B = (/ 1, 1, 2, 3, 5, 8 /)
>> C1= RESHAPE( B, (/ 2,3 /) )
>>
>> print *, C1
>> end
>>
>> 1.000000
>> 1.000000
>> 2.000000
>> 3.000000
>> 5.000000
>> 8.000000
>>
>> NCL
>> B = (/ 1, 1, 2, 3, 5, 8 /)*1.0
>> C2= onedtond( B, (/ 2,3 /) )
>>
>> print(C2)
>>
>> Variable: C2
>> Type: float
>> Total Size: 24 bytes
>> 6 values
>> Number of Dimensions: 2
>> Dimensions and sizes: [2] x [3]
>> Coordinates:
>> (0,0) 1
>> (0,1) 1
>> (0,2) 2
>> (1,0) 3
>> (1,1) 5
>> (1,2) 8
>>
>>
>>
>> On 10/19/10 5:29 AM, Mehmet Coskun wrote:
>>> Dear All
>>>
>>> Is there any build in function to converts a one-dimensional array to a
>>> multi-dimensional array,which is consistent with Fortran reshape,
>>> onedtond is not same fortran reshape,
>>> mehmet
>>> thank you
>>>
>>>
>>>
>>> _______________________________________________
>>> ncl-talk mailing list
>>> List instructions, subscriber options, unsubscribe:
>>> http://mailman.ucar.edu/mailman/listinfo/ncl-talk
_______________________________________________
ncl-talk mailing list
List instructions, subscriber options, unsubscribe:
http://mailman.ucar.edu/mailman/listinfo/ncl-talk
Received on Wed Oct 20 10:42:46 2010

This archive was generated by hypermail 2.1.8 : Fri Oct 22 2010 - 12:21:46 MDT