
236
13
Numerical
Algorithms
FORTRAN
Code (CLS) for Solving a Purely Overdetermined Linear
System with Optional Linear Equality Constraints
subroutine cls (g,
xm,
d, n,m, nd,md, f, h, nc, ncd,mcd, test,
real g(nd,md), xm(md), d(nd), f (ncd,mcd), h(ncd), test,
sq
integer n, m, nd, md, nc, ncd, mcd, itr, ierr
/*
subroutine cls (constrained least squares) by William Menke
/*
solves g *
xm
=
d for xm
/*
g is n by m (dimensioned nd by md)
/*
f
is
nc by m (dimensioned ncd by mcd)
/*
division by zero if denominator smaller than test
/*
system triangularized only if itr=0
/*
sq
returned as rms error
/*
ierr returned as zero on no error
/*
problem must be overdetermined with no redundant constraints
/*
solution in several steps:
/*
1)
lower-left triangularization of constraint matrix by
/*
Householder reduction and simultaneous rotation of
/*
least squares matrix.
/*
2)
upper-right triangularization
of
least squares matrix
/*
by Householder reduction, ignoring first nc columns
/*
3) rotation of least squares vector to match matrix
/*
4) back-solution of constraint matrix for first nc unknowns
/*
5)
back-solution of least squares matrix for rest of unknowns
/*
6)
computation of residual error
/*
7)
back-rotation of unknowns
dimension
~(100)~
~(100)
/*
temp vectors
save u, v, nlast, mlast
/*
must be saved if itr=l option used
if( m .It.
1
)
then
/*
not enough unknowns
L
itr,
sq,
ierr)
with linear constraints f
*
xm
=
h
ierr
=
1
return
else if( m
.gt.
(n+nc)
)
then
/*
not enough data
ierr
=
1
return
else if( nc
.gt.
m
)
then
/*
too many constraints
ierr
=
return
end if
if( itr .eq.
0
if( nc .
else
then
/*
triangularize systems
eq. m
)
then
/*
number of Householder rotations one
nlast
=
nc-1
/*
less if problem is even-determined
nlast
=
nc
end if
if
(
n+nc .eq. m
)
then
mlast
=
m-1
else
mlast
=
m
end if