Fortran Matrix Functions
Fortran was built for linear algebra, so matrix multiplication, transposition and dot products are part of the language. This lesson covers reshape, transpose, matmul, dot_product and a few related helpers.
reshape
reshape(source, shape) rearranges the elements of source into an array of a new shape. The elements are taken in order and placed in column-major order, so the first column fills first:
program reshape_demo
implicit none
integer :: i
integer :: a(2, 3), b(3, 2)
a = reshape([1, 2, 3, 4, 5, 6], [2, 3])
b = reshape([1, 2, 3, 4, 5, 6], [3, 2], order=[2, 1]) ! fill row by row
print '(a)', 'a ='
do i = 1, 2
print '(3i3)', a(i, :)
end do
print '(a)', 'b (filled by rows) ='
do i = 1, 3
print '(2i3)', b(i, :)
end do
end program reshape_demoa =
1 3 5
2 4 6
b (filled by rows) =
1 2
3 4
5 6
order=[2, 1] tells reshape to vary the second index fastest, which matches how you usually write a matrix row by row in the source code. The optional pad argument supplies values when source is too short.
transpose
program transpose_demo
implicit none
integer :: m(2, 3), t(3, 2)
integer :: i
m = reshape([1, 2, 3, 4, 5, 6], [2, 3], order=[2, 1])
t = transpose(m)
do i = 1, 3
print '(2i3)', t(i, :)
end do
end program transpose_demo 1 4
2 5
3 6
dot_product and matmul
dot_product(u, v) is Σ uᵢvᵢ for two vectors of the same size. matmul(a, b) is the matrix product: the number of columns of a must equal the number of rows of b. matmul also multiplies a matrix by a vector.
program products
implicit none
real :: u(3) = [1.0, 2.0, 3.0], v(3) = [4.0, -5.0, 6.0]
real :: a(2, 3), b(3, 2), c(2, 2)
integer :: i
print '(a, f6.1)', 'u . v = ', dot_product(u, v)
a = reshape([1.0, 2.0, 3.0, &
4.0, 5.0, 6.0], [2, 3], order=[2, 1])
b = reshape([ 7.0, 8.0, &
9.0, 10.0, &
11.0, 12.0], [3, 2], order=[2, 1])
c = matmul(a, b)
print '(a)', 'a x b ='
do i = 1, 2
print '(2f8.1)', c(i, :)
end do
print '(a, 2f8.1)', 'a x u = ', matmul(a, u)
end program productsu . v = 12.0
a x b =
58.0 64.0
139.0 154.0
a x u = 14.0 32.0
Note:
a * bis the element-wise product and requires equal shapes;matmul(a, b)is the linear-algebra product. Mixing them up is a common beginner bug.
Rotation matrices: a worked example
A 2-D rotation by angle θ is the matrix [[cos θ, -sin θ], [sin θ, cos θ]]. Applying it to several points at once is a single matmul when the points are the columns of a matrix:
program rotate_points
use iso_fortran_env, only: real64
implicit none
real(real64), parameter :: pi = 4.0_real64 * atan(1.0_real64)
real(real64) :: rot(2, 2), pts(2, 4), moved(2, 4), theta
integer :: k
theta = pi / 2.0_real64 ! 90 degrees
rot = reshape([cos(theta), -sin(theta), &
sin(theta), cos(theta)], [2, 2], order=[2, 1])
! the corners of a unit square, one point per column
pts = reshape([0.0_real64, 0.0_real64, 1.0_real64, 0.0_real64, &
1.0_real64, 1.0_real64, 0.0_real64, 1.0_real64], [2, 4])
moved = matmul(rot, pts)
do k = 1, 4
print '(a, 2f6.2, a, 2f6.2, a)', '(', pts(:, k), ' ) -> (', moved(:, k), ' )'
end do
end program rotate_points( 0.00 0.00 ) -> ( 0.00 0.00 )
( 1.00 0.00 ) -> ( 0.00 1.00 )
( 1.00 1.00 ) -> ( -1.00 1.00 )
( 0.00 1.00 ) -> ( -1.00 0.00 )
Identity matrices and the diagonal
There is no built-in eye function, but an identity matrix is easy to build, and the diagonal can be read with an implied do loop:
program identity
implicit none
integer, parameter :: n = 4
real :: id(n, n), m(n, n)
integer :: i
id = 0.0
do i = 1, n
id(i, i) = 1.0
end do
m = reshape([(real(i), i = 1, n * n)], [n, n])
print '(a, l1)', 'm x I == m: ', all(matmul(m, id) == m)
print '(a, *(f5.1))', 'diagonal of m: ', [(m(i, i), i = 1, n)]
print '(a, f5.1)', 'trace of m: ', sum([(m(i, i), i = 1, n)])
end program identitym x I == m: T
diagonal of m: 1.0 6.0 11.0 16.0
trace of m: 34.0
Tip: For large or performance-critical linear algebra (solving systems, eigenvalues, SVD) real programs call LAPACK and BLAS, which are themselves written in Fortran.
matmulis fine for small and medium matrices, and gfortran can even be told to call BLAS for it (-fexternal-blas).
Test yourself
What is the shape of matmul(a, b) if a is 3×2 and b is 2×5?
- 2×2
- 3×5
- 5×3
- It is invalid
What does reshape([1, 2, 3, 4], [2, 2]) give as its first row?
1 21 33 42 4
Which expression computes Σ xᵢyᵢ for two vectors?
x * ymatmul(x, y)dot_product(x, y)product(x + y)