3
\brief Gram-Schmidt orthogonalize a set of vectors
10
#include <libciomr/libciomr.h>
14
/* #define STANDALONE */
17
** SCHMIDT(): Gram-Schmidt orthogonalize a set of vectors
19
** Assume we're orthogonalizing the ROWS, since in C
20
** a vector is usually a row more often than a column.
22
** David Sherrill, Feb 1994
24
** \param A = matrix to orthogonalize (matrix of doubles)
25
** \param rows = rows of A
26
** \param cols = columns of A
31
void schmidt(double **A, int rows, int cols, FILE *outfile)
34
double normval, dotval;
38
/* initialize working array */
39
tmp = init_array(cols);
41
/* always take the first vector (normalized) as given */
42
dot_arr(A[0], A[0], cols, &normval) ; /* normval = dot (A0 * A0) */
43
normval = sqrt(normval) ;
45
for (i=0; i<cols; i++) A[0][i] /= normval;
47
/* now, one at a time, get the new rows */
48
for (i=1; i<rows; i++) {
49
for (I=0; I<cols; I++) tmp[I] = A[i][I] ;
51
dot_arr(A[i], A[j], cols, &dotval) ;
52
for (I=0; I<cols; I++) tmp[I] -= dotval * A[j][I];
54
dot_arr(tmp, tmp, cols, &normval);
55
normval = sqrt(normval);
56
/* fprintf(outfile,"\n norm[%d] = %20.15f\n",i, (1.0/normval));
58
for (I=0; I<cols; I++) A[i][I] = tmp[I] / normval;
71
double **mat, **mat_copy, **mat_x_mat ;
72
void schmidt(double **A, int rows, int cols) ;
74
mat = init_matrix(3, 3) ;
75
mat_copy = init_matrix(3, 3) ;
76
mat_x_mat = init_matrix(3, 3) ;
77
mat[0][0] = 1.0 ; mat[0][1] = 0.0 ; mat[0][2] = 1.0 ;
78
mat[1][0] = 1.0 ; mat[1][1] = -1.0 ; mat[1][2] = 0.0 ;
79
mat[2][0] = 0.0 ; mat[2][1] = 0.0 ; mat[2][2] = 0.5 ;
81
ffile(&outfile, "output.dat", 0) ;
82
fprintf(outfile, "Matrix before Gram-Schmidt process\n") ;
83
print_mat(mat,3,3,outfile) ;
85
fprintf(outfile, "\nMatrix after Gram-Schmidt process\n") ;
86
print_mat(mat,3,3,outfile) ;
88
fprintf(outfile, "\nTest A * A = \n") ;
90
mmult(mat, 0, mat, 1, mat_x_mat, 0, 3, 3, 3, 0) ;
91
print_mat(mat_x_mat,3,3,outfile) ;
94
free_matrix(mat_copy,3) ;
95
free_matrix(mat_x_mat,3) ;