Skip to content

Instantly share code, notes, and snippets.

@Chronum94
Last active May 25, 2017 09:01
Show Gist options
  • Save Chronum94/c398fbb883c4d0b8d4983a34bbda7e1d to your computer and use it in GitHub Desktop.
Save Chronum94/c398fbb883c4d0b8d4983a34bbda7e1d to your computer and use it in GitHub Desktop.
Array Stuff
//
// Created by chronum on 5/23/17.
//
#ifndef BLAZE_IDE_TEST_BLAZEGEN_HPP
#define BLAZE_IDE_TEST_BLAZEGEN_HPP
#include <blaze/Blaze.h>
#include <iomanip>
#include <random>
namespace bz=blaze;
namespace blazegen {
// Generates a uniform random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, class Generator = std::mt19937>
bz::DynamicMatrix<T> randu_matrix_real(const unsigned long rows,
const unsigned long cols, Generator& g) {
bz::DynamicMatrix <T> rand_matrix(rows, cols);
std::uniform_real_distribution<T> distribution(0, 1);
for (auto row = 0; row < rows; ++row) {
for (auto col = 0; col < cols; ++col) {
rand_matrix(row, col) = distribution(g);
}
}
return rand_matrix;
}
// Generates a normal random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, class Generator = std::mt19937>
bz::DynamicMatrix<T> randn_matrix_real(const unsigned long rows,
const unsigned long cols,
Generator& g,
double mu = 0.0, double sigma = 1.0) {
bz::DynamicMatrix <T> rand_matrix(rows, cols);
std::normal_distribution<T> distribution(mu, sigma);
for (auto row = 0; row < rows; ++row) {
for (auto col = 0; col < cols; ++col) {
rand_matrix(row, col) = distribution(g);
}
}
return rand_matrix;
}
// Generates a uniform random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, class Generator = std::mt19937>
bz::DynamicVector<T> randu_vector_real(const unsigned long length, Generator& g) {
bz::DynamicVector <T> rand_vector(length);
std::uniform_real_distribution<T> distribution(0, 1);
for (auto element = 0; element < length; ++element) {
rand_vector[element] = distribution(g);
}
return rand_vector;
}
// Generates a normal random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, class Generator = std::mt19937>
bz::DynamicVector<T> randn_vector_real(const unsigned long length,
Generator& g,
double mu = 0.0, double sigma = 1.0) {
bz::DynamicVector <T> rand_vector(length);
std::normal_distribution<T> distribution(mu, sigma);
for (auto element = 0; element < length; ++element) {
rand_vector[element] = distribution(g);
}
return rand_vector;
}
template <typename T>
void print_mat(T const& matrix){
unsigned long rows = matrix.rows(), cols = matrix.columns();
//std::cout << std::setw(10) << std::endl;
//std::cout << std::setprecision(6) << std::endl;
std::cout.width(10);
std::cout.precision(4);
std::cout.left;
std::cout.fixed;
for(auto i = 0UL; i < rows; ++i) {
for (auto j = 0UL; j < cols; ++j){
std::cout << matrix(i, j) << '\t';
}
std::cout << std::endl;
}
}
template <typename T>
bz::CompressedMatrix<T> laplacian_1d(const unsigned long mat_dim){
bz::CompressedMatrix<T> result(mat_dim, mat_dim);
result.reserve(3*mat_dim-2); // A 2nd-order Laplacian has 3n-2 nonzeros.
// Inserting into first and last rows.
result.append(0, 0, 2); // diag.
result.append(0, 1, -1); //upper diag.
result.finalize(0);
result.append(mat_dim-1, mat_dim-1, 2); // diag.
result.append(mat_dim-1, mat_dim-2, -1); //lower diag.
result.finalize(mat_dim-1);
for(unsigned long i = 1; i < mat_dim-1; ++i){
result.append(i, i-1, -1); // lower diag.
result.append(i, i, 2); // diag.
result.append(i, i+1, -1); //upper diag.
result.finalize(i);
}
return result;
}
}
#endif //BLAZE_IDE_TEST_BLAZEGEN_HPP
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment