rbmatlab  1.16.09
 All Classes Namespaces Files Functions Variables Modules Pages
gram_schmidt_reiterate.m
Go to the documentation of this file.
1 function varargout = gram_schmidt_reiterate(X, K, epsilon, n_on)
2 % function varargout = gram_schmidt_reiterate(X, k, epsilon, n_on)
3 % performs classical gram schmidt orthonormalization algorithm with
4 % re-orthogonalization. loss of orthogonality is a multiple of machine
5 % precision.
6 %
7 % Input parameter
8 % - X: vectors to be orthonormalized
9 % - K: inner product matrix
10 % - epsilon: norm threshold
11 % - n_on: if given, first n_on vectors are assumed to be already orthonormal
12 % Output parameter
13 % - varargout{1}: orthonormalized vectors
14 % - varargout{2}: indices of vectors included in onb
15 
16 if nargin < 4
17  n_on = 0;
18 end
19 
20 Xon = X(:, 1:n_on);
21 ind = 1:n_on;
22 % storing matrix avoids multiple computation of inner products
23 KXon = K * Xon;
24 
25 for i = (n_on + 1):size(X, 2)
26 
27  v = X(:, i);
28 
29  norm = sqrt(v' * K * v);
30 
31  if norm > epsilon
32 
33  % two orthogonalization iterations suffice
34  for j = 1:2
35 
36  v = v - Xon * (KXon' * v);
37  end
38 
39  w = K * v;
40  norm = sqrt(v' * w);
41 
42  % include new vector
43  if norm > epsilon
44  ind = [ind, i];
45  Xon = [Xon, v / norm];
46  KXon = [KXon, w / norm];
47  end
48  end
49 end
50 
51 varargout{1} = Xon;
52 varargout{2} = ind;
53 
54 end