Section Arrays: Submatrices#
Adapted from: gjbex/Fortran-MOOC
This program demonstrates matrix slicing in Fortran.#
program submatrices
implicit none
integer, dimension(3, 4) :: A
integer, dimension(12) :: A_array
integer :: i, j
A_array = [ (((i - 1)*size(A, 2) + j, &
j=1, size(A, 2)), &
i=1, size(A, 1)) ]
A = reshape([ (((i - 1)*size(A, 2) + j, &
j=1, size(A, 2)), &
i=1, size(A, 1)) ], &
shape(A))
print *, "A_array contains: ", A_array
print *, "A matrix contains: "
do i = 1, size(A, 1)
print *, A(i, :)
end do
print *
print *, "Row 2 contains: "
print *, A(2, :)
print *, "Column 3 contains: "
print *, A(:, 3)
print '(A, I0)', 'rank = ', rank(A(2, :))
print '(A, I0)', 'shape = ', shape(A(2, :))
end program submatrices
The above program is compiled and run using Fortran Package Manager (fpm):
Build the Program using FPM (Fortran Package Manager)#
import os
root_dir = os.getcwd()
code_dir = root_dir + "/" + "Fortran_Code/Section_Arrays_Submatrices"
os.chdir(code_dir)
build_status = os.system("fpm build 2>/dev/null")
Run the Program using FPM (Fortran Package Manager)#
exec_status = os.system("fpm run 2>/dev/null")
A_array contains: 1 2 3 4 5 6 7 8 9 10 11 12
A matrix contains:
1 4 7 10
2 5 8 11
3 6 9 12
Row 2 contains:
2 5 8 11
Column 3 contains:
7 8 9
rank = 1
shape = 4