Calculate the L1 Norm of a Vector
This is a part of the student software manual project for Math 5610: Computational Linear Algebra and Solution of Systems of Equations.
Routine Name: l1_vec_norm
Author: Christian Bolander
Language: Fortran. This code can be compiled using the GNU Fortran compiler by
$ gfortran -c l1_vec_norm.f90
and can be added to a program using
$ gfortran program.f90 l1_vec_norm.o
Description/Purpose: This routine calculates the -norm of an arbitrary vector, a.
Input:
n : INTEGER - the length of the vector, a
a : REAL - an arbitrary vector of length n
Output:
norm : REAL - the -norm of the vector, a.
Usage/Example:
This routine can be implemented in a program as follows
n = 12
doo = 1.0D0
norm = 0.0D0
ALLOCATE(a(1:n))
DO i = 1, n
a(i) = -doo
END DO
CALL l1_vec_norm(a, n, norm)
WRITE(*,*) norm
The output from the above code:
12.000000000000000
which is the -norm of the vector a.
Implementation/Code: The code for l1_vec_norm can be seen below.
SUBROUTINE l1_vec_norm(a, n, norm)
IMPLICIT NONE
INTEGER, INTENT(IN) :: n
REAL*8, INTENT(IN) :: a(1:n)
REAL*8, INTENT(OUT) :: norm
INTEGER :: i
norm = 0.0D0
! Sum up the absolute value of each value in the vector a to find
! the l1-norm of the vector.
DO i = 1, n
norm = norm + ABS(a(i))
END DO
END SUBROUTINE