Skip to content

Instantly share code, notes, and snippets.

@jesyspa
Forked from Chronum94/blazegen.cpp
Last active May 24, 2017 05:50
Show Gist options
  • Save jesyspa/25f8b9653e85a10935dfc755100e3ea9 to your computer and use it in GitHub Desktop.
Save jesyspa/25f8b9653e85a10935dfc755100e3ea9 to your computer and use it in GitHub Desktop.
Array Stuff
#pragma once
#include <blaze/Blaze.h>
#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, typename Generator>
bz::DynamicMatrix<T> randu_matrix_real(unsigned long rows, unsigned long cols, Generator& rand_gen) {
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(rand_gen);
}
}
return rand_matrix;
}
// Generates a normal random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, typename Generator>
bz::DynamicMatrix<T> randn_matrix_real(unsigned long rows,
unsigned long cols,
Generator& rand_gen,
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(rand_gen);
}
}
return rand_matrix;
}
// Generates a uniform random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, class Generator>
bz::DynamicVector<T> randu_vector_real(unsigned long length, Generator& rand_gen) {
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(rand_gen);
}
return rand_vector;
}
// Generates a normal random matrix of type T using a (P)RNG
// of class Generator.
template<typename T, class Generator>
bz::DynamicVector<T> randn_vector_real(const unsigned long length,
Generator& rand_gen,
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(rand_gen);
}
return rand_vector;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment