21 #include "../_c_basic_algebra/c_vector_operations.h"
22 #include "../_random_generator/random_array_generator.h"
23 #include "../_random_generator/random_number_generator.h"
124 template <
typename DataType>
135 if ((num_ortho == 0) || (num_vectors < 2))
140 else if ((num_ortho < 0) ||
141 (num_ortho >
static_cast<FlagType>(num_vectors)))
144 num_steps = num_vectors;
149 num_steps = num_ortho;
155 if (num_steps >
static_cast<IndexType>(vector_size))
157 num_steps = vector_size;
164 DataType epsilon = std::numeric_limits<DataType>::epsilon();
168 for (
IndexType step=0; step < num_steps; ++step)
171 if ((last_vector % num_vectors) >= step)
173 i = (last_vector % num_vectors) - step;
178 i = (last_vector % num_vectors) - step + num_vectors;
183 &V[vector_size*i], vector_size);
186 if (norm < epsilon * sqrt(vector_size))
188 std::cerr <<
"WARNING: norm of the given vector is too small. " \
189 <<
"Cannot orthogonalize against zero vector. " \
190 <<
"Skipping." << std::endl;
196 &V[vector_size*i], v, vector_size);
199 DataType scale = inner_prod / (norm * norm);
203 if (std::abs(std::abs(scale) - 1.0) <= 2.0 * epsilon)
210 distance2 = norm_v*norm_v - 2.0*inner_prod + norm*norm;
214 if (distance2 < 2.0 * epsilon * vector_size)
222 &V[vector_size*i], vector_size, scale, v);
268 template <
typename DataType>
286 DataType epsilon = std::numeric_limits<DataType>::epsilon();
293 while (i < num_vectors)
295 if ((success == 0) && (num_trials >= max_num_trials))
297 std::cerr <<
"ERROR: Cannot orthogonalize vectors after " \
298 << num_trials <<
" trials. Aborting." \
310 start_j = i -
static_cast<IndexType>(vector_size);
318 for (j=start_j; j < i; ++j)
322 &vectors[j*vector_size], vector_size);
325 if (norm_j < epsilon * sqrt(vector_size))
327 std::cerr <<
"WARNING: norm of the given vector is too " \
328 <<
" small. Cannot reorthogonalize against zero" \
329 <<
"vector. Skipping."
336 &vectors[i*vector_size], &vectors[j*vector_size],
340 DataType scale = inner_prod / (norm_j * norm_j);
344 &vectors[vector_size*j], vector_size, scale,
345 &vectors[vector_size*i]);
349 &vectors[i*vector_size], vector_size);
352 if (norm_i < epsilon * sqrt(vector_size))
356 random_number_generator, &vectors[i*vector_size],
357 vector_size, num_threads);
static void generate_random_array(RandomNumberGenerator &random_number_generator, DataType *array, const LongIndexType array_size, const IndexType num_threads)
Generates a pseudo-random array with Rademacher distribution where elements are either +1 or -1.
Generates 64-bit integers on multiple parallel threads.
A static class for orthogonalization of vector bases. This class acts as a templated namespace,...
static void gram_schmidt_process(const DataType *V, const LongIndexType vector_size, const IndexType num_vectors, const IndexType last_vector, const FlagType num_ortho, DataType *r)
Modified Gram-Schmidt orthogonalization process to orthogonalize the vector v against a subset of the...
static void orthogonalize_vectors(DataType *vectors, const LongIndexType vector_size, const IndexType num_vectors)
Orthogonalizes set of vectors mutually using modified Gram-Schmidt process.
static void subtract_scaled_vector(const DataType *input_vector, const LongIndexType vector_size, const DataType scale, DataType *output_vector)
Subtracts the scaled input vector from the output vector.
static DataType inner_product(const DataType *vector1, const DataType *vector2, const LongIndexType vector_size)
Computes Euclidean inner product of two vectors.
static DataType euclidean_norm(const DataType *vector, const LongIndexType vector_size)
Computes the Euclidean norm of a 1D array.