1 function onvec = orthonormalize_gram_schmidt(vec,A,epsilon)
2 %
function onvec = orthonormalize(vec[,A,epsilon])
4 % Gram-Schmidt orthonormalization of vectors in columns of
5 % matrix vec to onvec. Almost zero vectors are deleted.
6 % the inner product matrix A can be specified additionally, i.e.
9 % note: a threshold epsilon is involved in this routine for detecting
10 % zero columns. This is a quite sensitive quantity! Change with care, as
11 % many functions build on this!! Default is 1e-7
13 % Bernard Haasdonk 13.6.2002
16 epsilon = 1e-7; % => for nonlinear evolution this value is required.
18 %epsilon = 1e-10; => for linear evolution this value was OK.
19 %epsilon = 1e-12; -> This causes error by returning non-orthogonal
20 % vectors (e.g. two identical vectors)
21 % So take epsilon larger than 1e-12!
24 onvec = zeros(size(vec));
32 % check on identity of vectors (can happen, that numerics does not detect
33 % this afterwards due to rounding errors!!)
35 for i=1:(size(vec,2)-1)
36 for j=(i+1):size(vec,2)
37 if isequal(vec(:,i),vec(:,j))
45 for i = 1:size(vec,2);
46 % orthogonalize next vector i and assume, that it is already
47 % orthogonal to previous ones
48 n = sqrt(onvec(:,i)' * A * onvec(:,i));
52 onvec(:,i) = onvec(:,i)/n;
55 % orthogonalize remaining vectors wrt
this one:
57 A_mult_onvec_i = A*onvec(:,i);
59 % create row-vector with projections on the created on-vector
60 coeffs= A_mult_onvec_i
' * onvec(:,i+1:end);
62 % create matrix of scaled versions of actual orthonormalized vector
63 coeffmat = onvec(:,i) * coeffs;
65 % perform orthogonalization
66 onvec(:,i+1:end) = onvec(:,i+1:end) - coeffmat;
72 % eliminate zero-columns
79 %resort zero-columns to back
80 %nsqr = sum(onvec.^2);