I made seed and uniform functions, but they are not omp friendly. They are runnning omp themself, but is not safe for threading/parallizing. Uniform is aproximated and is NOT validaded up against a real uniform distribution.
260 lines
9.9 KiB
C++
260 lines
9.9 KiB
C++
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
*
|
|
* PANIC
|
|
* Portable Algorithms and Numerics In C++
|
|
*
|
|
* Scientific computing from scratch, with feeling.
|
|
*
|
|
* Copyright (c) 2026 Michelle Bausager
|
|
*
|
|
* This file is part of PANIC.
|
|
*
|
|
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
|
* You may redistribute and/or modify it under the terms of the GPL.
|
|
*
|
|
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
* See the LICENSE file for the full license text.
|
|
*
|
|
* SPDX-License-Identifier: GPL-3.0-or-later
|
|
*
|
|
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
*
|
|
* Project Name: PANIC
|
|
* Module Name: random
|
|
* File Name: uniform.cpp
|
|
* Revision: 0.1.0
|
|
* Date: 29-06-2026
|
|
* Author: Michelle Bausager
|
|
*
|
|
* Description:
|
|
* Defines the dense layers used in neural network
|
|
*
|
|
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// INCLUDE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
#include <random/uniform.hpp>
|
|
#include <config/types.hpp>
|
|
#include <config/omp.hpp>
|
|
#include <random/seed.hpp>
|
|
|
|
#include <tensor/vector.hpp>
|
|
|
|
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// VARIABLE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
static const panic::types::uint_t uniform_omp_min_work = 5000;
|
|
static const panic::types::uint_t max_uint_t = ~static_cast<panic::types::uint_t>(0);
|
|
static const panic::types::real_t real_max_unit = static_cast<panic::types::real_t>(max_uint_t);
|
|
|
|
static panic::types::uint_t uniform_state = static_cast<panic::types::uint_t>(1);
|
|
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// FUNCTION PROTOTYPE
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
namespace panic{
|
|
namespace random{
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::random::uniform
|
|
//
|
|
// Description:
|
|
// returns a value from a uniform distribution bewteen 0 and 1
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
panic::types::real_t uniform(){
|
|
panic::random::seed_t seed;
|
|
uniform_state += static_cast<panic::types::uint_t>(1);
|
|
seed.set(uniform_state);
|
|
|
|
return (static_cast<panic::types::real_t>(seed.state_at(seed.get()) / real_max_unit));
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::random::uniform
|
|
//
|
|
// Description:
|
|
// returns a value from a uniform distribution bewteen min and max
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
T uniform(const T min, const T max){
|
|
return (min + static_cast<T>(static_cast<panic::types::real_t>((max - min)) * uniform()));
|
|
}
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// EXPLICIT TEMPLATE INSTANTIATION
|
|
//
|
|
// The implementation is in this .cpp file.
|
|
// Build the overload for the official PANIC numeric types.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template panic::types::uint_t
|
|
uniform<panic::types::uint_t>(const panic::types::uint_t min,
|
|
const panic::types::uint_t mix
|
|
);
|
|
template panic::types::int_t
|
|
uniform<panic::types::int_t>(const panic::types::int_t min,
|
|
const panic::types::int_t max
|
|
);
|
|
template panic::types::real_t
|
|
uniform<panic::types::real_t>(const panic::types::real_t min,
|
|
const panic::types::real_t max
|
|
);
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::random::uniform
|
|
//
|
|
// Description:
|
|
// Fills a vector with a uniform distribution
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
bool uniform(panic::tensor::real_vector& a){
|
|
|
|
panic::random::seed_t seed;
|
|
panic::types::uint_t work = a.size();
|
|
uniform_state += work;
|
|
seed.set(uniform_state);
|
|
|
|
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
|
for (panic::types::uint_t i = 0; i < work; ++i){
|
|
a[i] = (static_cast<panic::types::real_t>(seed.state_at(i)) / real_max_unit);
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::random::uniform
|
|
//
|
|
// Description:
|
|
// Fills a vector with a uniform distribution with limits
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool uniform(panic::tensor::vector<T>& a, const T min, const T max){
|
|
|
|
panic::random::seed_t seed;
|
|
panic::types::uint_t work = a.size();
|
|
|
|
uniform_state += work;
|
|
seed.set(uniform_state);
|
|
|
|
panic::types::real_t temp = static_cast<panic::types::real_t>(max - min);
|
|
|
|
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
|
for (panic::types::uint_t i = 0; i < work; ++i){
|
|
a[i] = min + static_cast<T>((temp*(static_cast<panic::types::real_t>(seed.state_at(i)) / real_max_unit)));
|
|
}
|
|
|
|
return true;
|
|
}
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// EXPLICIT TEMPLATE INSTANTIATION
|
|
//
|
|
// The implementation is in this .cpp file.
|
|
// Build the overload for the official PANIC numeric types.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template bool uniform<panic::types::uint_t>(panic::tensor::vector<panic::types::uint_t>& a,
|
|
const panic::types::uint_t min,
|
|
const panic::types::uint_t mix
|
|
);
|
|
template bool uniform<panic::types::int_t>(panic::tensor::vector<panic::types::int_t>& a,
|
|
const panic::types::int_t min,
|
|
const panic::types::int_t max
|
|
);
|
|
template bool uniform<panic::types::real_t>(panic::tensor::vector<panic::types::real_t>& a,
|
|
const panic::types::real_t min,
|
|
const panic::types::real_t max
|
|
);
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::random::uniform
|
|
//
|
|
// Description:
|
|
// Fills a matrix with a uniform distribution
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
bool uniform(panic::tensor::real_matrix& A){
|
|
|
|
panic::random::seed_t seed;
|
|
|
|
panic::types::uint_t rows = A.rows();
|
|
panic::types::uint_t cols = A.cols();
|
|
|
|
panic::types::uint_t work = rows*cols;
|
|
|
|
uniform_state += work;
|
|
seed.set(uniform_state);
|
|
|
|
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
|
for (panic::types::uint_t i = 0; i < rows; ++i){
|
|
for (panic::types::uint_t j = 0; j < cols; ++j){
|
|
panic::types::uint_t index = i * cols + j;
|
|
A(i,j) = (static_cast<panic::types::real_t>(seed.state_at(index)) / real_max_unit);
|
|
}
|
|
}
|
|
|
|
|
|
return true;
|
|
}
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::random::uniform
|
|
//
|
|
// Description:
|
|
// Fills a matrix with a uniform distribution with limits
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool uniform(panic::tensor::matrix<T>& A, const T min, const T max){
|
|
|
|
panic::random::seed_t seed;
|
|
|
|
panic::types::uint_t rows = A.rows();
|
|
panic::types::uint_t cols = A.cols();
|
|
|
|
panic::types::uint_t work = rows*cols;
|
|
|
|
uniform_state += work;
|
|
seed.set(uniform_state);
|
|
|
|
panic::types::real_t temp = static_cast<panic::types::real_t>(max - min);
|
|
|
|
PANIC_OMP_PARALLEL_FOR_IF(work > uniform_omp_min_work)
|
|
for (panic::types::uint_t i = 0; i < rows; ++i){
|
|
for (panic::types::uint_t j = 0; j < cols; ++j){
|
|
panic::types::uint_t index = i * cols + j;
|
|
A(i,j) = min + static_cast<T>((temp*(static_cast<panic::types::real_t>(seed.state_at(index)) / real_max_unit)));
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// EXPLICIT TEMPLATE INSTANTIATION
|
|
//
|
|
// The implementation is in this .cpp file.
|
|
// Build the overload for the official PANIC numeric types.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template bool uniform<panic::types::uint_t>(panic::tensor::matrix<panic::types::uint_t>& a,
|
|
const panic::types::uint_t min,
|
|
const panic::types::uint_t mix
|
|
);
|
|
template bool uniform<panic::types::int_t>(panic::tensor::matrix<panic::types::int_t>& a,
|
|
const panic::types::int_t min,
|
|
const panic::types::int_t max
|
|
);
|
|
template bool uniform<panic::types::real_t>(panic::tensor::matrix<panic::types::real_t>& a,
|
|
const panic::types::real_t min,
|
|
const panic::types::real_t max
|
|
);
|
|
|
|
|
|
} // namespace random
|
|
} // namespace panic
|