Started on Random Library

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.
This commit is contained in:
2026-07-28 17:32:49 +02:00
parent 52684e6b8a
commit 441540a996
7 changed files with 1359 additions and 15 deletions
+110
View File
@@ -0,0 +1,110 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: seed.hpp
* Revision: 0.1.0
* Date: 28-07-2026
* Author: Michelle Bausager
*
* Description:
* Defines seed for use in other functions in random/
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::random::seed
//
// Description:
// base for random libary
//--------------------------------------------------------------------------------------------------------------------------
namespace panic{
namespace random{
/**
* @brief struct for seed object used in random/
*
* The struct is used for PANIC random library.
*/
struct seed_t{
// Variable to store the value the random value is based on.
panic::types::uint_t value;
/**
* @brief Empthy constructor
*
*/
seed_t();
/**
* @brief Contructor with seed.
*
* @param seed The seed that is used as the base for the object.
*/
seed_t(panic::types::uint_t seed);
/**
* @brief Function to set the seed..
*
* @param seed The seed that is used as the base for the object.
*/
bool set(panic::types::uint_t seed);
// Creates a deterministic state from the seed and an index.
// This is OMP-friendly because it does not modify shared memory.
/**
* @brief Returns a random number based on seed and index
*
* @param index Value the random number will be drawn from.
*
* @Note this can be used with omp in a loop if the index in the loop
* is used as the input to this function.
*/
panic::types::uint_t state_at(panic::types::uint_t index) const;
/**
* @brief Returns the base seed value.
*
*/
panic::types::uint_t get();
};
} // namespace random
} // namespace panic
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
+196
View File
@@ -0,0 +1,196 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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.hpp
* Revision: 0.1.0
* Date: 29-06-2026
* Author: Michelle Bausager
*
* Description:
* Defindes the functions that returns values based on a uniform distribution.
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// TYPE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::random::seed
//
// Description:
// base for random libary
//--------------------------------------------------------------------------------------------------------------------------
namespace panic{
namespace random{
/**
* @brief Returns a value from a uniform distribution bewteen 0 and 1.
*
* Computes:
* @code
* c = uniform();
* @endcode
*
* @return a value from a uniform distribution between 0 and 1
*
* @note This is not a omp-safe function
*/
panic::types::real_t uniform();
/**
* @brief Returns a value from a uniform distribution with limits.
*
* Computes:
* @code
* c = uniform(0.1f, 42.0f);
* @endcode
*
* @tparam T Numeric element type.
* @param min minimum limit for return value.
* @param max maximum limit for return value.
*
* @return a value from a uniform distribution with limits
*
* @note This is not a omp-safe function
*/
template <typename T>
T uniform(const T min, const T max);
/**
* @brief Filleds a vector with values from a uniform distribution from 0 to 1.
*
* Computes:
* @code
* panic::tensor::vector a(5);
* uniform(a);
* @endcode
*
* @tparam T Numeric element type.
* @param a Output vector filled iwht new values
*
* @return True of vector is filled correctly
*
* @note This is not a omp-safe function
*/
bool uniform(panic::tensor::real_vector& a);
/**
* @brief Filleds a vector with values from a uniform distribution from min to max.
*
* Computes:
* @code
* panic::tensor::real_vector a(5);
* uniform(a, 1.0f, 42.0f);
* @endcode
*
* @tparam T Numeric element type.
* @param a Output vector filled iwht new values
* @param min minimum limit for return value.
* @param max maximum limit for return value.
*
* @return True of vector is filled correctly
*
* @note This is not a omp-safe function
*/
template <typename T>
bool uniform(panic::tensor::vector<T>& a, const T min, const T max);
/**
* @brief Filleds a matrix with values from a uniform distribution from 0 to 1.
*
* Computes:
* @code
* panic::tensor::matrix A(5);
* uniform(A);
* @endcode
*
* @tparam T Numeric element type.
* @param a Output matrix filled with new values
*
* @return True of matrix is filled correctly
*
* @note This is not a omp-safe function
*/
bool uniform(panic::tensor::real_matrix& A);
/**
* @brief Filleds a matrix with values from a uniform distribution from min to max.
*
* Computes:
* @code
* panic::tensor::real_matrix A(5,5);
* uniform(A, 1.0f, 42.0f);
* @endcode
*
* @tparam T Numeric element type.
* @param a Output matrix filled with new values
* @param min minimum limit for return value.
* @param max maximum limit for return value.
*
* @return True of matrix is filled correctly
*
* @note This is not a omp-safe function
*/
template <typename T>
bool uniform(panic::tensor::matrix<T>& A, const T min, const T max);
/*
panic::tensor::real_vector uniform_vector(panic::tensor::real_vector& a);
panic::tensor::real_vector uniform_vector(const panic::types::real_t min, const panic::types::real_t max);
// Create and return vector
// Create and return matrix
*/
} // namespace random
} // namespace panic
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------