/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * 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 #include #include #include #include //--------------------------------------------------------------------------------------------------------------------------- // VARIABLE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- static const panic::types::uint_t uniform_omp_min_work = 5000; static const panic::types::uint_t max_uint_t = ~static_cast(0); static const panic::types::real_t real_max_unit = static_cast(max_uint_t); static panic::types::uint_t uniform_state = static_cast(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(1); seed.set(uniform_state); return (static_cast(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 T uniform(const T min, const T max){ return (min + static_cast(static_cast((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(const panic::types::uint_t min, const panic::types::uint_t mix ); template panic::types::int_t uniform(const panic::types::int_t min, const panic::types::int_t max ); template panic::types::real_t uniform(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(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 bool uniform(panic::tensor::vector& 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(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((temp*(static_cast(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::tensor::vector& a, const panic::types::uint_t min, const panic::types::uint_t mix ); template bool uniform(panic::tensor::vector& a, const panic::types::int_t min, const panic::types::int_t max ); template bool uniform(panic::tensor::vector& 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(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 bool uniform(panic::tensor::matrix& 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(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((temp*(static_cast(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::tensor::matrix& a, const panic::types::uint_t min, const panic::types::uint_t mix ); template bool uniform(panic::tensor::matrix& a, const panic::types::int_t min, const panic::types::int_t max ); template bool uniform(panic::tensor::matrix& a, const panic::types::real_t min, const panic::types::real_t max ); } // namespace random } // namespace panic