! For 'F90' extension files, strictly free form is required. ! although that can be overriden by the use of '/source_form=fixed' ! qualifier ! Hence, no column-1 comment indicator or coulmn-6 continuation ! indicator. ! Fortran Example-5: Arrays program arrays Implicit none ! This is not to allow any implicit typing ! Hence, every variable must be declared. Integer, parameter:: d1=3, d2=4, d3=5 Integer AA (d1,d2), BB(d2,d3), CC(d3,d2) ! explicit-shape dec Integer A(d1), B(d2), DD(d1,d3) Integer BBB(4, 0:4) ! Size 4-by-5 Integer J, K Integer I ! Integer DD(:,:,:), E(:) ! Assumed-shape dec used only for ! subprogram parameters (Dummy Arguments) or ! Deferred-shape declaration ! The first case must appear only in subprograms ! while the second case needs an attribute such as ! "Allocatable" as in: ! Integer, Allocatable, Dimension(:,:,:) :: DD ! Integer, Allocatable :: DD(:,:,:) ! Allocatable arrays will have memory space ! dynamically allocated later in the program as in: ! Allocate (DD(10,20,30),Stat=Err) ! Assumed-Shape arrays are only for Dummy arguments of ! Subprograms and should not have this attribute while ! Allocatable arrays are not Dummy arguments and their ! declarations must have this attribute. ! Hence, it would cause a syntax error here since it is neither ! Rank: Number of dimensions, up to seven. ! Extent: The difference between the lower bound and the upper ! bound of each dimension. ! To be exact, it is (UpperboundIndex-LowerboundIndex+1) ! The implied default lower bound is one when omitted. ! Shape: Rank and Extent combined together. ! Size: Number of all elements ! Conformability: Two arrays are comformable when they have the same shape. ! The two arrays BB and BBB are conformable above. ! But, BB and CC are not. A = (/(I, I=1,3)/) ! An array constructor, only for one-dimensional ! arrays. ! For multi-dimensional arrays, the function ! 'Reshape' needs to be used. ! Even above use of 'I' would cause a syntax error if it were not ! declared. B = (/1,2,3,4/) ! d1 =d1+1 ! This would cause a syntax error: ! PARAMETER Constant Name invalid in this context. AA = Reshape ((/((1,2,3),I=1,4)/), (/d1,d2/)) BB = Reshape ((/(I,I=1,20)/), (/d2,d3/)) Write(555,233) "THE CONTENTS OF AA:" Call Printt(AA) BBB = BB Print 233, 'The contents of BBB which is also BB:::' write(555,233) 'The Contents of BBB which is also BB:::' Call Printt(BBB) 233 format (//1X, A /) ! '1X' is necessary not to lose the first character ! of the string when printing on paper/screen as a format ! is being used. ! 'A' format descriptor is for a character string ! of undetermined length being printed left-justified ! while 'Aw' is for one of determined length ! right-justified within the field width given by 'w' do I=1,d2 print 222, (BBB(I,J), J=0,d3-1) 222 format (1X, 10I5) ! '1X' means one blank space. end do CC = Transpose(BB) DD = MatMul(AA,Transpose(CC)) print 233, "The product of AA and BB:" Write(555,233) "The Product of AA and BB:::" do I=1,d1 print 222, (DD(i,j), j=1,d3) write(555,222) (DD(I,J), J=1,d3) end do DD = Mul(AA,BB) ! Use this function just to multiply them. Write(555,233) "THE RESULT OF USING ARRAY-VALUED FUNCTION" Call Printt(DD) ! Now try AA*CC, which will cause a syntax error: ! Shapes of arguments inconsistent or nonconformable ! DD = MatMul(AA,CC) ! ! Extension of scalar operators BBB = BB * BBB print * Print *, 'The Scalar multiplication of BB*BBB' ! Not using a format for printing, there is no potential loss ! of the first character of the output string. print * write(555,233) "The Scalar Multiplication of BB*BBB" Call Printt(BBB) do i=1,d2 print 222, (BBB(i,j), j=0,d3-1) end do print * Print *, "DOT PRODUCT OF FIRST ROWS OF BB AND BBB:::", & Dot_Product (BB(1,:),BBB(1,:)) write(555,*) " " write(555, *) "DOT PRODUCT OF FIRST ROWS OF BB AND BBB:::", & Dot_Product (BB(1,:),BBB(1,:)) ! Arguments of above function need to be conformable one-dim arrays. Contains Function Mul (A,B) ! An Array valued-function that returns an array Integer, dimension (:,:):: A,B ! Assumed-Shape declaration ! These arrays' shape will be determined from corresponding ! Actual arguments Integer, dimension (Size(A,1), Size(B,2)):: Mul ! Automatic array usually used for local arrays ! which is special case of explicit-shape arrays except that ! at least one extent has a non-constant. Mul=MatMul(A,B) End function Mul !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Subroutine Printt (A) Integer A(:,:) Integer I, J do I=Lbound(A,1), UBound(A,1) write(555,222) (A(I,J), J=LBound(A,2), Ubound(A,2)) 222 format (1X, 10I5) End do End subroutine Printt end program !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!!!!!!PROGRAM OUTPUTS!!!!!!!! THE CONTENTS OF AA: 1 1 1 1 2 2 2 2 3 3 3 3 The Contents of BBB which is also BB::: 1 5 9 13 17 2 6 10 14 18 3 7 11 15 19 4 8 12 16 20 The Product of AA and BB::: 10 26 42 58 74 20 52 84 116 148 30 78 126 174 222 THE RESULT OF USING ARRAY-VALUED FUNCTION 10 26 42 58 74 20 52 84 116 148 30 78 126 174 222 The Scalar Multiplication of BB*BBB 1 25 81 169 289 4 36 100 196 324 9 49 121 225 361 16 64 144 256 400 DOT PRODUCT OF FIRST ROWS OF BB AND BBB::: 7965