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"
124template <
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 *
static_cast<DataType
>(std::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 (
static_cast<DataType
>(std::abs(
204 static_cast<DataType
>(std::abs(scale)) - \
205 static_cast<DataType
>(1.0))) <= \
206 static_cast<DataType>(2.0) * epsilon)
213 distance2 = norm_v*norm_v - \
214 static_cast<DataType>(2.0)*inner_prod + norm*norm;
219 static_cast<DataType
>(2.0) * epsilon * \
220 static_cast<DataType
>(vector_size))
228 &V[vector_size*i], vector_size, scale, v);
274template <
typename DataType>
292 DataType epsilon = std::numeric_limits<DataType>::epsilon();
299 while (i < num_vectors)
301 if ((success == 0) && (num_trials >= max_num_trials))
303 std::cerr <<
"ERROR: Cannot orthogonalize vectors after " \
304 << num_trials <<
" trials. Aborting." \
316 start_j = i -
static_cast<IndexType>(vector_size);
324 for (j=start_j; j < i; ++j)
328 &vectors[j*vector_size], vector_size);
332 epsilon *
static_cast<DataType
>(std::sqrt(vector_size)))
334 std::cerr <<
"WARNING: norm of the given vector is too " \
335 <<
" small. Cannot reorthogonalize against zero" \
336 <<
"vector. Skipping."
343 &vectors[i*vector_size], &vectors[j*vector_size],
347 DataType scale = inner_prod / (norm_j * norm_j);
351 &vectors[vector_size*j], vector_size, scale,
352 &vectors[vector_size*i]);
356 &vectors[i*vector_size], vector_size);
360 epsilon *
static_cast<DataType
>(std::sqrt(vector_size)))
364 random_number_generator, &vectors[i*vector_size],
365 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 DataType inner_product(const DataType *RESTRICT vector1, const DataType *RESTRICT vector2, const LongIndexType vector_size)
Computes Euclidean inner product of two vectors.
static void subtract_scaled_vector(const DataType *RESTRICT input_vector, const LongIndexType vector_size, const DataType scale, DataType *RESTRICT output_vector)
Subtracts the scaled input vector from the output vector.
static DataType euclidean_norm(const DataType *RESTRICT vector, const LongIndexType vector_size)
Computes the Euclidean norm of a 1D array.