first model

This commit is contained in:
2026-07-29 17:34:22 +02:00
parent 47671354ce
commit f5e0ee209b
25 changed files with 2642 additions and 108 deletions
+12
View File
@@ -63,6 +63,18 @@ namespace panic {
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
static const panic::types::real_t pi = static_cast<panic::types::real_t>(3.14159265358979323846); static const panic::types::real_t pi = static_cast<panic::types::real_t>(3.14159265358979323846);
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
// Type Name : panic::constants::half_pi
//
// Description:
// Default difinition of pi (3.14159265358979323846) used inside PANIC.
// static_cast makes sure the right number of decimals are used when defining the constant.
//
// Underlying Type:
// panic::types::real_t
//---------------------------------------------------------------------------------------------------------------------------
static const panic::types::real_t half_pi = pi / static_cast<panic::types::real_t>(2);
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// //
// Type Name : panic::constants::tau // Type Name : panic::constants::tau
// //
+4 -16
View File
@@ -31,18 +31,6 @@
* Description: * Description:
* Functions to add panic::tensor togther; * Functions to add panic::tensor togther;
* *
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* @file add.hpp
* @brief Public API for element-wise addition operations on PANIC vectors and matrices.
*
* This header contains the declarations that users of the math module should call.
* The comments here describe how each function is used, what dimensions are required,
* and what is returned on failure.
*
* Implementation details, OpenMP thresholds, and explicit template instantiations are
* kept in add.cpp.
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once #pragma once
@@ -221,7 +209,7 @@ panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const panic::ten
* *
* Computes: * Computes:
* @code * @code
* C(i,j) = A(i,j) + b[i] * C(i,j) = A(i,j) + b[j]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
@@ -240,7 +228,7 @@ bool add_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<
* *
* Computes: * Computes:
* @code * @code
* result(i,j) = A(i,j) + b[i] * result(i,j) = A(i,j) + b[j]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
@@ -260,7 +248,7 @@ panic::tensor::matrix<T> add_rowwise(const panic::tensor::matrix<T>& A, const pa
* *
* Computes: * Computes:
* @code * @code
* C(i,j) = A(i,j) + b[j] * C(i,j) = A(i,j) + b[i]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
@@ -279,7 +267,7 @@ bool add_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<
* *
* Computes: * Computes:
* @code * @code
* result(i,j) = A(i,j) + b[j] * result(i,j) = A(i,j) + b[i]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
+353
View File
@@ -0,0 +1,353 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
*
* 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: math
* File Name: maximum.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions that finds the maximum
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
#include <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // for panic::matrix
namespace panic{
namespace math{
/**
* @brief Clips the maximum values in the vector or scalar.
*
* Computes:
* @code
* if (a[i] < k)
* c[i] = k
* else
* c[i] = a[i]
* @endcode
*
* @tparam T Numeric element type.
* @param a Input vector.
* @param k Scalar value compared to each element of @p a.
* @param c Output vector. Resized to match @p a.
*
* @return true if @p c was resized and filled successfully.
* @return false if resizing @p c failed.
*
* @note This overload writes the result into an existing vector to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool maximum(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c);
/**
* @brief Returns a cliped maximum value in the vector.
*
* Computes:
* @code
* if (a[i] < k)
* result[i] = k
* else
* result[i] = a[i]
* @endcode
*
* @tparam T Numeric element type.
* @param a Input vector.
* @param k Scalar value compared to each element of @p a.
*
* @return A new vector containing the result.
* @return An empty vector if the operation fails.
*
* @note This overload is convenient, but may allocate a new vector.
*/
template <typename T>
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const T k);
/**
* @brief Clips the maximum elementwise in the vector.
*
* Computes:
* @code
* if (a[i] < b[i])
* c[i] = b[i]
* else
* c[i] = a[i]
* @endcode
*
* @tparam T Numeric element type.
* @param a First vector.
* @param b Second vector. Must have size of @p a
* @param c Output vector. Resized to match @p a.
*
* @return true if @p c was resized and filled successfully.
* @return false if vector sizes do not match or resizing @p c failed.
*/
template <typename T>
bool maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
/**
* @brief Returns a new vector containing cliped maximum elementwise of a vector.
*
* Computes:
* @code
* if (a[i] < b[i])
* result[i] = b[i]
* else
* result[i] = a[i]
* @endcode
*
* @tparam T Numeric element type.
* @param a First vector.
* @param b Second vector. Must have size of @p a
*
* @return A new vector containing the result.
* @return An empty vector if the operation fails.
*
* @note This overload is convenient, but may allocate a new vector.
*/
template <typename T>
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
/**
* @brief Clips the maximum elementwise in the matrix.
*
* Computes:
* @code
* if (A(i,j) < k)
* C(i,j) = k
* else
* C(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Input matrix.
* @param k Scalar value compared to each element of @p a.
* @param C Output Matrix. Resized to match @p A.
*
* @return true if @p C was resized and filled successfully.
* @return false if resizing @p C failed.
*
* @note This overload writes the result into an existing matrix to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool maximum(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C);
/**
* @brief Returns a new matrix containing the cliped maximum elementwise in the matrix.
*
* Computes:
* @code
* if (A(i,j) < k)
* result(i,j) = k
* else
* result(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Input matrix.
* @param k Scalar value compared to each element of @p a.
*
* @return A new matrix containing the result.
* @return An empty matrix if the operation fails.
*
* @note This overload is convenient, but may allocate a new matrix.
*/
template <typename T>
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const T k);
/**
* @brief Clips the maximum of a matrix elementwise too a matrix.
*
* Computes:
* @code
* if (A(i,j) < B(i,j))
* C(i,j) = B(i,j)
* else
* C(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A First matrix.
* @param B Second matrix. Must have size of @p A.
* @param C Output matrix. Resized to match @p A.
*
* @return true if @p C was resized and filled successfully.
* @return false if matrix sizes do not match or resizing @p c failed.
*/
template <typename T>
bool maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
/**
* @brief Returns a new matrix containing a matrix elementwise cliped to the maixmum.
*
* Computes:
* @code
* if (A(i,j) < B(i,j))
* result(i,j) = B(i,j)
* else
* result(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Input matrix.
* @param B Second matrix. Must have size of @p A.
*
* @return A new matrix containing the result.
* @return An empty matrix if the operation fails.
*
* @note This overload is convenient, but may allocate a new matrix.
*/
template <typename T>
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
/**
* @brief Clips the maximum of a vector rowwise too a matrix.
*
* Computes:
* @code
* if (A(i,j) < b[j])
* C(i,j) = b[j])
* else
* C(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Matrix.
* @param b Vector. Must have size of @p A.cols().
* @param C Output matrix. Resized to match @p A.
*
* @return true if @p C was resized and filled successfully.
* @return false if matrix sizes do not match or resizing @p c failed.
*/
template <typename T>
bool maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
/**
* @brief Returns a new matrix containing a vector rowwise cliped to the maixmum.
*
* Computes:
* @code
* if (A(i,j) < b[j])
* result(i,j) = b[j])
* else
* result(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Matrix.
* @param b Vector. Must have size of @p A.cols().
*
* @return A new matrix containing the result.
* @return An empty matrix if the operation fails.
*
* @note This overload is convenient, but may allocate a new vector.
*/
template <typename T>
panic::tensor::matrix<T> maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
/**
* @brief Clips the maximum of a vector rowwise too a matrix.
*
* Computes:
* @code
* if (A(i,j) < b[i])
* C(i,j) = b[i])
* else
* C(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Matrix.
* @param b Vector. Must have size of @p A.rows().
* @param C Output matrix. Resized to match @p A.
*
* @return true if @p C was resized and filled successfully.
* @return false if matrix sizes do not match or resizing @p c failed.
*/
template <typename T>
bool maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
/**
* @brief Returns a new matrix containing a vector rowwise cliped to the maixmum.
*
* Computes:
* @code
* if (A(i,j) < b[i])
* result(i,j) = b[i])
* else
* result(i,j) = A(i,j)
* @endcode
*
* @tparam T Numeric element type.
* @param A Matrix.
* @param b Vector. Must have size of @p A.rows().
*
* @return A new matrix containing the result.
* @return An empty matrix if the operation fails.
*
* @note This overload is convenient, but may allocate a new vector.
*/
template <typename T>
panic::tensor::matrix<T> maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
} // namespace math
} // namespace panic
+5 -5
View File
@@ -218,7 +218,7 @@ bool mul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, p
* @note This overload is convenient, but may allocate a new vector. * @note This overload is convenient, but may allocate a new vector.
*/ */
template <typename T> template <typename T>
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B); panic::tensor::matrix<T> mul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
/** /**
@@ -226,7 +226,7 @@ panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const panic::ten
* *
* Computes: * Computes:
* @code * @code
* C(i,j) = A(i,j) * b[i] * C(i,j) = A(i,j) * b[j]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
@@ -245,7 +245,7 @@ bool mul_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<
* *
* Computes: * Computes:
* @code * @code
* result(i,j) = A(i,j) * b[i] * result(i,j) = A(i,j) * b[j]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
@@ -265,7 +265,7 @@ panic::tensor::matrix<T> mul_rowwise(const panic::tensor::matrix<T>& A, const pa
* *
* Computes: * Computes:
* @code * @code
* C(i,j) = A(i,j) * b[j] * C(i,j) = A(i,j) * b[i]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
@@ -284,7 +284,7 @@ bool mul_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<
* *
* Computes: * Computes:
* @code * @code
* result(i,j) = A(i,j) * b[j] * result(i,j) = A(i,j) * b[i]
* @endcode * @endcode
* *
* @tparam T Numeric element type. * @tparam T Numeric element type.
+61
View File
@@ -0,0 +1,61 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: math
* File Name: cos.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to calculate cossinus of x;
* This uses panic::math::sin
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
namespace panic{
namespace math{
/**
* @brief Calculates cos value of x
*
* Computes:
* @code
* c = cos(a)
* @endcode
* @tparam T Numeric element type.
* @param x Real input value
*
* @return Cosinus of x
*
* @note This function is omp-friendly
*/
template <typename T>
T cos(const T x);
} // namespace math
} // namespace panic
+60
View File
@@ -0,0 +1,60 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: math
* File Name: sin.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to calculate sinus of x;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
namespace panic{
namespace math{
/**
* @brief Calculates sin value of x
*
* Computes:
* @code
* c = sin(a)
* @endcode
* @tparam T Numeric element type.
* @param x Real input value
*
* @return Sinus of x
*
* @note This function is omp-friendly
*/
template <typename T>
T sin(const T x);
} // namespace math
} // namespace panic
@@ -0,0 +1,94 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: activation_ReLU.hpp
* Revision: 0.1.0
* Date: 29-08-2026
* Author: Michelle Bausager
*
* Description:
* Defines the activation layer for ReLU
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
#include <neural_network/layer/layer.hpp> // for base layer struct
#include <tensor/matrix.hpp>
namespace panic{
namespace neural_network{
/**
* @brief struct for ReLU activation layer used in neural networks
*
* Computes:
* @code
* panic::neural_network::activation_ReLU myactivation();
* myactivation.forward(inputMatrix);
* @endcode
*
* The struct is used in PANIC nural_network library.
*/
struct activation_ReLU : public layer{
/**
* @brief Empthy constructor
*
*/
activation_ReLU();
/**
* @brief Default de-constructor
*
*/
~activation_ReLU() = default;
/**
* @brief Forward function for layer
*
* @param inputs Data input for forward pass.
*
* @Note Calculates -> outputs = inputs * weights + biases
*/
bool forward(const panic::tensor::real_matrix& inputs);
/**
* @brief Backward function for layer
*
* @param inputs Data input for bacward pass.
*
* @Note Calculates derivative of forward function.
*/
bool backward(const panic::tensor::real_matrix& dinpus);
};
} // namespace tensor
} // namespace panic
@@ -0,0 +1,96 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: sine_data.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate dataset for neural networks;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <config/types.hpp>
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace neural_network {
/**
* @brief Generates a dataset of sines values
*
* Computes:
* @code
* csine(10, 1, X, y)
* @endcode
*
* @tparam T Numeric element type.
* @param samples Number of samples dataset.
* @param lenght Interval of the sinus cuve form zero.
* @param X Output X matrix
* @param y Output y matrix
*
* @return true if @p X and @p y was resized and filled successfully.
* @return false if resizing @p X or @p y failed.
*
* @note This funtion writes the result into an existing vector to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool sine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix<T>& X, panic::tensor::vector<T>& y);
/**
* @brief Generates a dataset of sines and cosine values
*
* Computes:
* @code
* csine(10, 1, X, y)
* @endcode
*
* @tparam T Numeric element type.
* @param samples Number of samples dataset.
* @param lenght Interval of the sinus cuve form zero.
* @param X Output X matrix
* @param y Output y matrix
*
* @return true if @p X and @p y was resized and filled successfully.
* @return false if resizing @p X or @p y failed.
*
* @note This funtion writes the result into an existing vector to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool sine_cosine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix<T>& X, panic::tensor::matrix<T>& y);
} // namespace neural_network
} // namespace panic
@@ -0,0 +1,74 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: spiral_data.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate dataset for neural networks;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <config/types.hpp>
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace neural_network {
/**
* @brief Generates a dataset of spiral values
*
* Computes:
* @code
* spiral_data(10, 1, X, y)
* @endcode
*
* @tparam T Numeric element type.
* @param samples Number of samples dataset.
* @param classes Number of classes in the spiral data.
* @param X Output X matrix
* @param y Output y matrix
*
* @return true if @p X and @p y was resized and filled successfully.
* @return false if resizing @p X or @p y failed.
*
* @note This funtion writes the result into an existing vector to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool spiral_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y);
} // namespace neural_network
} // namespace panic
@@ -0,0 +1,74 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: vertical_data.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate dataset for neural networks;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <config/types.hpp>
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace neural_network {
/**
* @brief Generates a dataset of vertical values
*
* Computes:
* @code
* vertical_data(10, 1, X, y)
* @endcode
*
* @tparam T Numeric element type.
* @param samples Number of samples dataset.
* @param classes Number of classes in the vertical data.
* @param X Output X matrix
* @param y Output y matrix
*
* @return true if @p X and @p y was resized and filled successfully.
* @return false if resizing @p X or @p y failed.
*
* @note This funtion writes the result into an existing vector to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool vertical_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y);
} // namespace neural_network
} // namespace panic
+15 -2
View File
@@ -118,8 +118,6 @@ struct model{
*/ */
bool add(layer* new_layer); bool add(layer* new_layer);
// Create and add a dense layer.
/** /**
* @brief Adds a dense layer to the model. * @brief Adds a dense layer to the model.
* *
@@ -140,6 +138,21 @@ struct model{
panic::types::uint_t neuron_count panic::types::uint_t neuron_count
); );
/**
* @brief Adds a activation ReLU layer to the model.
*
* Computes:
* @code
* model.add_activation_ReLU(3,4);
* @endcode
*
*
* @return true if layer is added
*
* @note This function is convenient, but it allocates a new layer.
*/
bool add_activation_ReLU();
/** /**
* @brief Loops over all layers forward function * @brief Loops over all layers forward function
* *
-7
View File
@@ -187,10 +187,3 @@ panic::tensor::real_vector uniform_vector(const panic::types::real_t min, const
} // namespace random } // namespace random
} // namespace panic } // namespace panic
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
+100
View File
@@ -0,0 +1,100 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
*
* 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: tensor
* File Name: linspace.hpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate linspace tensors
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
#include <tensor/vector.hpp> // for panic::vector
#include <config/types.hpp>
namespace panic{
namespace tensor{
/**
* @brief Outputs a linear line in a vector
*
* Computes:
* @code
* c[i] = linspace(-5, 5, 10, true);
* @endcode
*
* @tparam T Numeric element type.
* @param start Start of the line.
* @param stop End of the line.
* @param num Number of points in the array.
* @param endpoint If the endpint should be the same as @p stop.
* @param c Output vector.
*
* @return true if @p c was resized and filled successfully.
* @return false if resizing @p c failed.
*
* @note This overload writes the result into an existing vector to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool linspace(const T start,
const T stop,
const panic::types::uint_t num,
panic::tensor::vector<T>& c,
const bool endpoint=true);
/**
* @brief Returns a vector with linear line.
*
* Computes:
* @code
* result[i] = linspace(-5, 5, 10, true);
* @endcode
*
* @param T Numeric element type.
* @param start Start of the line.
* @param stop End of the line.
* @param num Number of points in the array.
* @param endpoint If the endpint should be the same as @p stop.
*
* @return A new vector containing the result.
* @return An empty vector if the operation fails.
*
* @note This overload is convenient, but may allocate a new vector.
*/
template <typename T>
panic::tensor::vector<T> linspace(const T start,
const T stop,
const panic::types::uint_t num,
const bool endpoint=true);
} // namespace tensor
} // namespace panic
+50
View File
@@ -36,6 +36,7 @@
// INCLUDE DESCRIPTION // INCLUDE DESCRIPTION
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t #include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
#include <tensor/vector.hpp>
namespace panic{ namespace panic{
@@ -187,6 +188,55 @@ struct matrix{
*/ */
const T& operator()(panic::types::uint_t index_n, panic::types::uint_t index_m) const; const T& operator()(panic::types::uint_t index_n, panic::types::uint_t index_m) const;
/**
* @brief Filles and set a row to a value.
*
* Computes:
* @code
* A.set_row(1, 3);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_row(const panic::types::uint_t index_row, const T v);
/**
* @brief Filles and set a row to a vector.
*
* Computes:
* @code
* A.set_row(1, vector);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_row(const panic::types::uint_t index_row, const panic::tensor::vector<T> v);
/**
* @brief Filles and set a column to a value.
*
* Computes:
* @code
* A.set_column(1, 3);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_col(const panic::types::uint_t index_col, const T v);
/**
* @brief Filles and set a column to a vector.
*
* Computes:
* @code
* A.set_col(1, vector);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_col(const panic::types::uint_t index_col, const panic::tensor::vector<T> v);
}; };
+21 -76
View File
@@ -49,6 +49,16 @@
#include <neural_network/model/model.hpp> #include <neural_network/model/model.hpp>
#include <math/mul.hpp> #include <math/mul.hpp>
#include <math/maximum.hpp>
#include <neural_network/activation/activation_ReLU.hpp>
#include <tensor/generators/linspace.hpp>
#include <math/trigonometry/sin.hpp>
#include <neural_network/datasets/sine_data.hpp>
#include <neural_network/datasets/spiral_data.hpp>
#include <neural_network/datasets/vertical_data.hpp>
#include <math.h>
@@ -691,92 +701,27 @@ int main(void) {
//benchmark_omp_min_work(); //benchmark_omp_min_work();
// Comment out benchmark_omp_min_work() when it is not needed. // Comment out benchmark_omp_min_work() when it is not needed.
a[2] = 1.2;
b[2] = 3;
c[2] = -3;
panic::io::print_vector(a);
panic::io::print_vector(b);
panic::io::print_vector(c);
std::cout << a[100] << std::endl;
std::cout << a.at(100) << std::endl;
std::cout << panic::constants::pi << std::endl;
std::cout << 1.23249238423847 << std::endl;
A(1,0) = 1.2;
B(1,0) = 3;
C(1,0) = -2;
panic::io::print_matrix(A);
panic::io::print_matrix(B);
panic::io::print_matrix(C);
panic::tensor::real_matrix A1(100, 100, 0.999/100);
panic::tensor::real_matrix A2(100, 100, 100);
for (panic::types::uint_t i = 0; i < 5; ++i)
{
std::cout << A2(0,0) << std::endl;
A2 = panic::math::matmul(A1, A2);
}
panic::tensor::real_matrix B1(25, 100, 100);
panic::neural_network::layer_dense dense(100,10);
std::cout << dense.forward(B1) << std::endl;
std::cout << "random" << std::endl;
for (int i = 0; i < 5; ++i)
{
std::cout << panic::random::uniform() << std::endl;
}
std::cout << "random(min, max)" << std::endl;
panic::types::real_t a1 = -15;
panic::types::real_t a2 = 15;
for (int i = 0; i < 5; ++i)
{
std::cout << panic::random::uniform(a1,a2) << std::endl;
}
panic::tensor::real_vector a3(2);
panic::random::uniform(a3);
panic::io::print_vector(a3);
panic::tensor::uint_vector a4(2);
panic::random::uniform(a4, static_cast<panic::types::uint_t>(1), static_cast<panic::types::uint_t>(3));
panic::io::print_vector(a4);
panic::tensor::real_matrix D1(3,3);
panic::random::uniform(D1);
panic::io::print_matrix(D1);
panic::tensor::real_matrix D3(3,3);
panic::random::uniform(D3, 100.f, 200.f);
panic::io::print_matrix(D3);
std::cout << "neural_network" << std::endl; std::cout << "neural_network" << std::endl;
panic::neural_network::layer_dense layer_dense01(3,4); panic::tensor::real_matrix X;
std::cout << layer_dense01.forward(D1) << std::endl; panic::tensor::uint_vector y;
panic::io::print_matrix(layer_dense01.outputs);
panic::types::uint_t samples = 100;
panic::types::uint_t classes = 3;
panic::neural_network::spiral_data(samples, classes, X, y);
panic::neural_network::model mymodel; panic::neural_network::model mymodel;
mymodel.add_layer_dense(3,4); // Create Dense layer with 2 input features and 3 output values
mymodel.forward(D1); mymodel.add_layer_dense(2,3);
mymodel.add_activation_ReLU();
mymodel.forward(X);
panic::io::print_matrix(mymodel.outputs);
+638
View File
@@ -0,0 +1,638 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
*
* 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: math
* File Name: maximum.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions that finds the maximum
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <math/maximum.hpp>
#include <config/omp.hpp>
#include <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // for panic::matrix
//---------------------------------------------------------------------------------------------------------------------------
// PRIVATE CONSTANTS
//---------------------------------------------------------------------------------------------------------------------------
/**
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
*
* Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself.
*/
static const panic::types::uint_t maximum_omp_min_work = 500;
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace math {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of vector compared with scalar
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool maximum(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c){
if (!c.resize(a.size())){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(a.size() > maximum_omp_min_work)
for (panic::types::uint_t i = 0; i < a.size(); ++i){
if (a[i] < k){
c[i] = k;
}
else{
c[i] = a[i];
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool maximum<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::types::uint_t k,
panic::tensor::vector<panic::types::uint_t>& c
);
template bool maximum<panic::types::int_t>(const panic::tensor::vector<panic::types::int_t>& a,
const panic::types::int_t k,
panic::tensor::vector<panic::types::int_t>& c
);
template bool maximum<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& a,
const panic::types::real_t k,
panic::tensor::vector<panic::types::real_t>& c
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of vector compared with scalar
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const T k){
panic::tensor::vector<T> c(a.size());
if (!maximum(a, k, c)){
return panic::tensor::vector<T>();
}
return c;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::vector<panic::types::uint_t>
maximum(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::types::uint_t k
);
template panic::tensor::vector<panic::types::int_t>
maximum(const panic::tensor::vector<panic::types::int_t>& a,
const panic::types::int_t k
);
template panic::tensor::vector<panic::types::real_t>
maximum(const panic::tensor::vector<panic::types::real_t>& a,
const panic::types::real_t k
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of vector compared with another vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c){
if (a.size() != b.size()){
return false;
}
if (!c.resize(a.size())){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(a.size() > maximum_omp_min_work)
for (panic::types::uint_t i = 0; i < a.size(); ++i){
if (a[i] < b[i]){
c[i] = b[i];
}
else{
c[i] = a[i];
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool maximum(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::tensor::vector<panic::types::uint_t>& b,
panic::tensor::vector<panic::types::uint_t>& c
);
template bool maximum(const panic::tensor::vector<panic::types::int_t>& a,
const panic::tensor::vector<panic::types::int_t>& b,
panic::tensor::vector<panic::types::int_t>& c
);
template bool maximum(const panic::tensor::vector<panic::types::real_t>& a,
const panic::tensor::vector<panic::types::real_t>& b,
panic::tensor::vector<panic::types::real_t>& c
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of vector compared with another vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
panic::tensor::vector<T> c(a.size());
if (!maximum(a, b, c)){
return panic::tensor::vector<T>();
}
return c;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::vector<panic::types::uint_t>
maximum(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::tensor::vector<panic::types::uint_t>& b
);
template panic::tensor::vector<panic::types::int_t>
maximum(const panic::tensor::vector<panic::types::int_t>& a,
const panic::tensor::vector<panic::types::int_t>& b
);
template panic::tensor::vector<panic::types::real_t>
maximum(const panic::tensor::vector<panic::types::real_t>& a,
const panic::tensor::vector<panic::types::real_t>& b
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of matrix compared with scalar
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool maximum(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C){
panic::types::uint_t rows = A.rows();
panic::types::uint_t cols = A.cols();
panic::types::uint_t work = rows*cols;
if ( !C.resize(rows, cols) ){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
for (panic::types::uint_t j = 0; j < cols; ++j){
if (A(i,j) < k){
C(i,j) = k;
}
else{
C(i,j) = A(i,j);
}
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::types::uint_t k,
panic::tensor::matrix<panic::types::uint_t>& C
);
template bool maximum(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::types::int_t k,
panic::tensor::matrix<panic::types::int_t>& C
);
template bool maximum(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::types::real_t k,
panic::tensor::matrix<panic::types::real_t>& C
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of matrix compared with scalar
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const T k){
panic::tensor::matrix<T> C;
if (!maximum(A, k, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::types::uint_t k
);
template panic::tensor::matrix<panic::types::int_t>
maximum(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::types::int_t k
);
template panic::tensor::matrix<panic::types::real_t>
maximum(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::types::real_t k
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of two matrices
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C){
panic::types::uint_t rows = A.rows();
panic::types::uint_t cols = A.cols();
panic::types::uint_t work = rows*cols;
if ( (rows != B.rows()) || (cols != B.cols())){
return false;
}
if ( !C.resize(rows, cols) ){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
for (panic::types::uint_t j = 0; j < cols; ++j){
if (A(i,j) < B(i,j)){
C(i,j) = B(i,j);
}
else{
C(i,j) = A(i,j);
}
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::matrix<panic::types::uint_t>& B,
panic::tensor::matrix<panic::types::uint_t>& C
);
template bool maximum(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::tensor::matrix<panic::types::int_t>& B,
panic::tensor::matrix<panic::types::int_t>& C
);
template bool maximum(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::matrix<panic::types::real_t>& B,
panic::tensor::matrix<panic::types::real_t>& C
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum
//
// Description:
// Clips maximum of two matrices
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
panic::tensor::matrix<T> C;
if (!maximum(A, B, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::matrix<panic::types::uint_t>& B
);
template panic::tensor::matrix<panic::types::int_t>
maximum(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::tensor::matrix<panic::types::int_t>& B
);
template panic::tensor::matrix<panic::types::real_t>
maximum(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::matrix<panic::types::real_t>& B
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum_rowwise
//
// Description:
// Clips maximum rowwise of matrix compared with vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C){
panic::types::uint_t rows = A.rows();
panic::types::uint_t cols = A.cols();
panic::types::uint_t work = rows*cols;
if ( cols != b.size() ){
return false;
}
if ( !C.resize(rows, cols) ){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
for (panic::types::uint_t j = 0; j < cols; ++j){
if (A(i,j) < b[j]){
C(i,j) = b[j];
}
else{
C(i,j) = A(i,j);
}
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool maximum_rowwise(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b,
panic::tensor::matrix<panic::types::uint_t>& C
);
template bool maximum_rowwise(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::tensor::vector<panic::types::int_t>& b,
panic::tensor::matrix<panic::types::int_t>& C
);
template bool maximum_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b,
panic::tensor::matrix<panic::types::real_t>& C
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum_rowwise
//
// Description:
// Clips maximum rowwise of matrix compared with vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
panic::tensor::matrix<T> C;
if (!maximum_rowwise(A, b, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
maximum_rowwise(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b
);
template panic::tensor::matrix<panic::types::int_t>
maximum_rowwise(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::tensor::vector<panic::types::int_t>& b
);
template panic::tensor::matrix<panic::types::real_t>
maximum_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum_colwise
//
// Description:
// Clips maximum column-wise of matrix compared with vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C){
panic::types::uint_t rows = A.rows();
panic::types::uint_t cols = A.cols();
panic::types::uint_t work = rows*cols;
if ( rows != b.size() ){
return false;
}
if ( !C.resize(rows, cols) ){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
for (panic::types::uint_t j = 0; j < cols; ++j){
if (A(i,j) < b[i]){
C(i,j) = b[i];
}
else{
C(i,j) = A(i,j);
}
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool maximum_colwise(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b,
panic::tensor::matrix<panic::types::uint_t>& C
);
template bool maximum_colwise(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::tensor::vector<panic::types::int_t>& b,
panic::tensor::matrix<panic::types::int_t>& C
);
template bool maximum_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b,
panic::tensor::matrix<panic::types::real_t>& C
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::maximum_colwise
//
// Description:
// Clips maximum column-wise of matrix compared with vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
panic::tensor::matrix<T> C;
if (!maximum_colwise(A, b, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
maximum_colwise(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::tensor::vector<panic::types::uint_t>& b
);
template panic::tensor::matrix<panic::types::int_t>
maximum_colwise(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::tensor::vector<panic::types::int_t>& b
);
template panic::tensor::matrix<panic::types::real_t>
maximum_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b
);
} // namespace math
} // namespace panic
+80
View File
@@ -0,0 +1,80 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: math
* File Name: cos.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to calculate cossinus of x;
* This uses panic::math::sin
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <math/trigonometry/cos.hpp>
#include <math/trigonometry/sin.hpp>
#include <config/types.hpp>
#include <config/constants.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace math {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::sin
//
// Description:
// Calculates cosinus of x
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T cos(const T x){
return panic::math::sin(x + static_cast<T>(panic::constants::half_pi));
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::types::real_t cos<panic::types::real_t>(const panic::types::real_t x
);
} // namespace math
} // namespace panic
+107
View File
@@ -0,0 +1,107 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: math
* File Name: sin.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to calculate sinus of x;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <math/trigonometry/sin.hpp>
#include <config/types.hpp>
#include <config/constants.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace math {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::sin
//
// Description:
// Calculates sinus of x
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T sin(const T x){
T temp = x;
// Reduce x to [-pi, pi].
while (temp > panic::constants::pi){
temp -= panic::constants::tau;
}
while (temp < -panic::constants::pi){
temp += panic::constants::tau;
}
// Reduce x further to [-pi/2, pi/2].
if (temp > panic::constants::half_pi){
temp = panic::constants::pi - temp;
}else if (x < -panic::constants::half_pi){
temp = -panic::constants::pi - temp;
}
const panic::types::real_t x2 = temp * temp;
// Taylor polynomial: // x - x^3/3! + x^5/5! - x^7/7! + x^9/9!
return temp * (static_cast<panic::types::real_t>(1.0)
+ x2*( static_cast<panic::types::real_t>(-1.0 / 6.0)
+ x2*( static_cast<panic::types::real_t>(1.0 / 120.0)
+ x2*( static_cast<panic::types::real_t>(-1.0 / 5040.0)
+ x2*static_cast<panic::types::real_t>(1.0 / 362880.0)
)
)
)
);
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::types::real_t sin<panic::types::real_t>(const panic::types::real_t x
);
} // namespace math
} // namespace panic
@@ -0,0 +1,118 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: activation_ReLU.cpp
* Revision: 0.1.0
* Date: 29-08-2026
* Author: Michelle Bausager
*
* Description:
* Defines the activation layer for ReLU
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <neural_network/activation/activation_ReLU.hpp>
#include <config/omp.hpp>
#include <math/maximum.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// PRIVATE CONSTANTS
//---------------------------------------------------------------------------------------------------------------------------
/**
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
*
* Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself.
*/
static const panic::types::uint_t activation_ReLU_omp_min_size = 500;
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic{
namespace neural_network{
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::neural_network::activation_ReLU
//
// Description:
// Creates an empty layer.
//--------------------------------------------------------------------------------------------------------------------------
activation_ReLU::activation_ReLU() {
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::activation_ReLU.forward
//
// Description:
// Calculated the forward pass:
// outputs = max(inputs, 0)
//--------------------------------------------------------------------------------------------------------------------------
bool activation_ReLU::forward(const panic::tensor::real_matrix& inputs){
panic::math::maximum(inputs, 0.0f, outputs);
/*
if (inputs.cols() != weights.rows()){
return false;
}
if (!outputs.resize(inputs.rows(), weights.cols())){
return false;
}
if (!panic::math::matmul(inputs, weights, outputs)){
return false;
}
if (!panic::math::add_rowwise(outputs, biases, outputs)){
return false;
}
*/
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::activation_ReLU.backward
//
// Description:
// Calculated the backward pass:
// ??
//--------------------------------------------------------------------------------------------------------------------------
bool activation_ReLU::backward(const panic::tensor::real_matrix& dinputs){
return true;
}
} // namespace tensor
} // namespace panic
+157
View File
@@ -0,0 +1,157 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: sine_data.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate dataset for neural networks;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <neural_network/datasets/sine_data.hpp>
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
#include <config/types.hpp>
#include <config/omp.hpp>
#include <math/trigonometry/sin.hpp>
#include <math/trigonometry/cos.hpp>
#include <tensor/generators/linspace.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// PRIVATE CONSTANTS
//---------------------------------------------------------------------------------------------------------------------------
/**
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
*
* Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself.
*/
static const panic::types::uint_t sine_data_omp_min_work = 500;
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace neural_network {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::sine_data
//
// Description:
// Generates dataset with sinus curve
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool sine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix<T>& X, panic::tensor::vector<T>& y){
if ( !X.resize(samples, 1) || !y.resize(samples) ){
return false;
}
X.set_col(0, panic::tensor::linspace(static_cast<T>(0), // start
lenght, // stop
samples // num
));
PANIC_OMP_PARALLEL_FOR_IF(samples > sine_data_omp_min_work)
for (panic::types::uint_t i = 0; i < samples; ++i){
y[i] = panic::math::sin(X(i,0));
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool sine_data<panic::types::real_t>(const panic::types::uint_t samples,
const panic::types::real_t lenght,
panic::tensor::matrix<panic::types::real_t>& X,
panic::tensor::vector<panic::types::real_t>& y
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::sine_cosine_data
//
// Description:
// Generates dataset with sinus and a cosine curve
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool sine_cosine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix<T>& X, panic::tensor::matrix<T>& y){
if ( !X.resize(samples, 1) || !y.resize(samples,2) ){
return false;
}
X.set_col(0, panic::tensor::linspace(static_cast<T>(0), // start
lenght, // stop
samples // num
));
PANIC_OMP_PARALLEL_FOR_IF(samples > sine_data_omp_min_work)
for (panic::types::uint_t i = 0; i < samples; ++i){
y(i,0) = panic::math::sin(X(i,0));
y(i,1) = panic::math::cos(X(i,0));
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool sine_cosine_data<panic::types::real_t>(const panic::types::uint_t samples,
const panic::types::real_t lenght,
panic::tensor::matrix<panic::types::real_t>& X,
panic::tensor::matrix<panic::types::real_t>& y
);
} // namespace neural_network
} // namespace panic
+123
View File
@@ -0,0 +1,123 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: spiral_data.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate dataset for neural networks;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <neural_network/datasets/spiral_data.hpp>
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
#include <config/types.hpp>
#include <config/omp.hpp>
#include <math/trigonometry/sin.hpp>
#include <math/trigonometry/cos.hpp>
#include <math/add.hpp>
#include <random/uniform.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// PRIVATE CONSTANTS
//---------------------------------------------------------------------------------------------------------------------------
/**
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
*
* Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself.
*/
static const panic::types::uint_t spiral_data_omp_min_work = 500;
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace neural_network {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::spiral_data
//
// Description:
// Generates dataset with spiral data
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool spiral_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y){
if ( !X.resize(samples*classes, 2) || !y.resize(samples*classes) ){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(samples*classes > spiral_data_omp_min_work)
for (panic::types::uint_t i = 0; i < classes; ++i){
for (panic::types::uint_t j = 0; j < samples; ++j){
const T radius = static_cast<T>(j)/static_cast<T>(samples-1);
const T angle = static_cast<T>(i)*T{4} + (T{4}*radius);
const panic::types::uint_t row_index = (i*samples) + j;
X(row_index, 0) = radius*panic::math::cos(angle*T{2.5});
X(row_index, 1) = radius*panic::math::sin(angle*T{2.5});
y[row_index] = i;
}
}
panic::tensor::matrix<T> random_matrix(samples*classes, 2);
panic::random::uniform(random_matrix, T{-0.15}, T{0.15});
if (!panic::math::add(X, random_matrix, X)){
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool spiral_data<panic::types::real_t>(const panic::types::uint_t samples,
const panic::types::uint_t classes,
panic::tensor::matrix<panic::types::real_t>& X,
panic::tensor::uint_vector& y
);
} // namespace neural_network
} // namespace panic
@@ -0,0 +1,118 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: neural_network
* File Name: vertical_data.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate dataset for neural networks;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <neural_network/datasets/vertical_data.hpp>
#include <tensor/vector.hpp>
#include <tensor/matrix.hpp>
#include <config/types.hpp>
#include <config/omp.hpp>
#include <math/trigonometry/sin.hpp>
#include <math/trigonometry/cos.hpp>
#include <math/add.hpp>
#include <random/uniform.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// PRIVATE CONSTANTS
//---------------------------------------------------------------------------------------------------------------------------
/**
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
*
* Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself.
*/
static const panic::types::uint_t vertical_data_omp_min_work = 500;
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace neural_network {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::vertical_data
//
// Description:
// Generates dataset with vertical data
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool vertical_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y){
if ( !X.resize(samples*classes, 2) || !y.resize(samples*classes) ){
return false;
}
panic::tensor::vector<T> x_diviation(samples*classes);
panic::tensor::vector<T> y_diviation(samples*classes);
panic::random::uniform(x_diviation, T{-0.1}, T{0.1});
panic::random::uniform(y_diviation, T{-0.5}, T{0.5});
PANIC_OMP_PARALLEL_FOR_IF(samples*classes > vertical_data_omp_min_work)
for (panic::types::uint_t i = 0; i < classes; ++i){
for (panic::types::uint_t j = 0; j < samples; ++j){
const panic::types::uint_t row_index = (i*samples) + j;
X(row_index, 0) = static_cast<T>(i)/static_cast<T>(classes) + x_diviation[row_index];
X(row_index, 1) = T{0.5} + y_diviation[row_index];
y[row_index] = i;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool vertical_data<panic::types::real_t>(const panic::types::uint_t samples,
const panic::types::uint_t classes,
panic::tensor::matrix<panic::types::real_t>& X,
panic::tensor::uint_vector& y
);
} // namespace neural_network
} // namespace panic
+32 -1
View File
@@ -40,6 +40,10 @@
#include <tensor/vector.hpp> #include <tensor/vector.hpp>
#include <tensor/matrix.hpp> #include <tensor/matrix.hpp>
#include <neural_network/layer/layer_dense.hpp>
#include <neural_network/activation/activation_ReLU.hpp>
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
// IMPLEMENTATION // IMPLEMENTATION
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
@@ -143,7 +147,7 @@ bool model::add(layer* new_layer){
// Creates a dense layer and adds it to the model. // Creates a dense layer and adds it to the model.
// //
// Example: // Example:
// model.add_dense(100, 64); // model.add_layer_dense(100, 64);
//-------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------
bool model::add_layer_dense( bool model::add_layer_dense(
panic::types::uint_t input_size, panic::types::uint_t input_size,
@@ -166,7 +170,34 @@ bool model::add_layer_dense(
return true; return true;
} }
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::model::add_activation_ReLU
//
// Description:
// Creates a activation ReLU layer and adds it to the model.
//
// Example:
// model.add_activation_ReLU();
//--------------------------------------------------------------------------------------------------------------------------
bool model::add_activation_ReLU(){
// Create the ReLU layer.
activation_ReLU* new_layer = new activation_ReLU();
if (new_layer == 0){
return false;
}
// Add it to the model.
//
// If add() fails, delete the layer so we do not leak memory.
if (!add(new_layer)){
delete new_layer;
return false;
}
return true;
}
//-------------------------------------------------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::neural_network::model::forward // Function Name : panic::neural_network::model::forward
+168
View File
@@ -0,0 +1,168 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
*
* 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: tensor
* File Name: linspace.cpp
* Revision: 0.1.0
* Date: 29-07-2026
* Author: Michelle Bausager
*
* Description:
* Functions to generate linspace tensors
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#include <tensor/generators/linspace.hpp>
#include <config/omp.hpp>
#include <tensor/vector.hpp> // for panic::vector
#include <tensor/matrix.hpp> // for panic::matrix
//---------------------------------------------------------------------------------------------------------------------------
// PRIVATE CONSTANTS
//---------------------------------------------------------------------------------------------------------------------------
/**
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
*
* Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself.
*/
static const panic::types::uint_t linspace_omp_min_work = 500;
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace tensor {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::linspace
//
// Description:
// Creates a vector with a line generated by linspace
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool linspace(const T start,
const T stop,
const panic::types::uint_t num,
panic::tensor::vector<T>& c,
const bool endpoint){
if (!c.resize(num)){
return false;
}
T step;
if (endpoint){
step = (stop - start) / static_cast<T>(num - 1);
}else{
step = (stop - start) / static_cast<T>(num);
}
PANIC_OMP_PARALLEL_FOR_IF(num > linspace_omp_min_work)
for (panic::types::uint_t i = 0; i < num; ++i){
c[i] = start + (step*static_cast<T>(i));
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool linspace<panic::types::uint_t>(const panic::types::uint_t start,
const panic::types::uint_t stop,
const panic::types::uint_t num,
panic::tensor::vector<panic::types::uint_t>& c,
const bool endpoint
);
template bool linspace<panic::types::int_t>(const panic::types::int_t start,
const panic::types::int_t stop,
const panic::types::uint_t num,
panic::tensor::vector<panic::types::int_t>& c,
const bool endpoint
);
template bool linspace<panic::types::real_t>(const panic::types::real_t start,
const panic::types::real_t stop,
const panic::types::uint_t num,
panic::tensor::vector<panic::types::real_t>& c,
const bool endpoint
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::linspace
//
// Description:
// Retuens a vector with line of linspace
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> linspace(const T start,
const T stop,
const panic::types::uint_t num,
const bool endpoint){
panic::tensor::vector<T> c(num);
if (!linspace(start, stop, num, c, endpoint)){
return panic::tensor::vector<T>();
}
return c;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template panic::tensor::vector<panic::types::uint_t> linspace<panic::types::uint_t>(const panic::types::uint_t start,
const panic::types::uint_t stop,
const panic::types::uint_t num,
const bool endpoint
);
template panic::tensor::vector<panic::types::int_t> linspace<panic::types::int_t>(const panic::types::int_t start,
const panic::types::int_t stop,
const panic::types::uint_t num,
const bool endpoint
);
template panic::tensor::vector<panic::types::real_t> linspace<panic::types::real_t>(const panic::types::real_t start,
const panic::types::real_t stop,
const panic::types::uint_t num,
const bool endpoint
);
} // namespace tensor
} // namespace panic
+82 -1
View File
@@ -48,7 +48,7 @@
* Small vectors and matrices are kept serial because the overhead of starting * Small vectors and matrices are kept serial because the overhead of starting
* worker threads can be larger than the work itself. * worker threads can be larger than the work itself.
*/ */
static const panic::types::uint_t matrix_omp_min_size = 10000; static const panic::types::uint_t matrix_omp_min_size = 500;
namespace panic{ namespace panic{
namespace tensor{ namespace tensor{
@@ -308,6 +308,87 @@ const T& matrix<T>::operator()(panic::types::uint_t index_n, panic::types::uint_
} }
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::matrix::set_row
//
// Description:
// Filles and set a row to a value.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool matrix<T>::set_row(const panic::types::uint_t index_row, const T c){
PANIC_OMP_PARALLEL_FOR_IF(m > matrix_omp_min_size)
for (panic::types::uint_t i = 0; i < m; ++i){
data[index_row*m + i] = c;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::matrix::set_row
//
// Description:
// Filles and set a row to a vector.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool matrix<T>::set_row(const panic::types::uint_t index_row, const panic::tensor::vector<T> v){
if (m != v.size()){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(m > matrix_omp_min_size)
for (panic::types::uint_t i = 0; i < m; ++i){
data[index_row*m + i] = v[i];
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::matrix::set_col
//
// Description:
// Filles and set a column to a value.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool matrix<T>::set_col(const panic::types::uint_t index_col, const T c){
PANIC_OMP_PARALLEL_FOR_IF(n > matrix_omp_min_size)
for (panic::types::uint_t i = 0; i < n; ++i){
data[i*m + index_col] = c;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::matrix::set_col
//
// Description:
// Filles and set a colmn to a vector.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool matrix<T>::set_col(const panic::types::uint_t index_col, const panic::tensor::vector<T> v){
if (n != v.size()){
return false;
}
PANIC_OMP_PARALLEL_FOR_IF(n > matrix_omp_min_size)
for (panic::types::uint_t i = 0; i < n; ++i){
data[i*m + index_col] = v[i];
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION // EXPLICIT TEMPLATE INSTANTIATION
//--------------------------------------------------------------------------------------------------------------------------- //---------------------------------------------------------------------------------------------------------------------------