From f5e0ee209b35a452ddbe1f0f334dabd681263af5 Mon Sep 17 00:00:00 2001 From: Michelle Date: Wed, 29 Jul 2026 17:34:22 +0200 Subject: [PATCH] first model --- include/config/constants.hpp | 12 + include/math/add.hpp | 20 +- include/math/maximum.hpp | 353 ++++++++++ include/math/mul.hpp | 10 +- include/math/trigonometry/cos.hpp | 61 ++ include/math/trigonometry/sin.hpp | 60 ++ .../activation/activation_ReLU.hpp | 94 +++ include/neural_network/datasets/sine_data.hpp | 96 +++ .../neural_network/datasets/spiral_data.hpp | 74 ++ .../neural_network/datasets/vertical_data.hpp | 74 ++ include/neural_network/model/model.hpp | 17 +- include/random/uniform.hpp | 7 - include/tensor/generators/linspace.hpp | 100 +++ include/tensor/matrix.hpp | 50 ++ main.cpp | 97 +-- src/math/maximum.cpp | 638 ++++++++++++++++++ src/math/trigonometry/cos.cpp | 80 +++ src/math/trigonometry/sin.cpp | 107 +++ .../activation/activation_ReLU.cpp | 118 ++++ src/neural_network/datasets/sine_data.cpp | 157 +++++ src/neural_network/datasets/spiral_data.cpp | 123 ++++ src/neural_network/datasets/vertical_data.cpp | 118 ++++ src/neural_network/model/model.cpp | 33 +- src/tensor/generators/linspace.cpp | 168 +++++ src/tensor/matrix.cpp | 83 ++- 25 files changed, 2642 insertions(+), 108 deletions(-) create mode 100644 include/math/maximum.hpp create mode 100644 include/math/trigonometry/cos.hpp create mode 100644 include/math/trigonometry/sin.hpp create mode 100644 include/neural_network/activation/activation_ReLU.hpp create mode 100644 include/neural_network/datasets/sine_data.hpp create mode 100644 include/neural_network/datasets/spiral_data.hpp create mode 100644 include/neural_network/datasets/vertical_data.hpp create mode 100644 include/tensor/generators/linspace.hpp create mode 100644 src/math/maximum.cpp create mode 100644 src/math/trigonometry/cos.cpp create mode 100644 src/math/trigonometry/sin.cpp create mode 100644 src/neural_network/activation/activation_ReLU.cpp create mode 100644 src/neural_network/datasets/sine_data.cpp create mode 100644 src/neural_network/datasets/spiral_data.cpp create mode 100644 src/neural_network/datasets/vertical_data.cpp create mode 100644 src/tensor/generators/linspace.cpp diff --git a/include/config/constants.hpp b/include/config/constants.hpp index 08c1deb..8e03c5e 100644 --- a/include/config/constants.hpp +++ b/include/config/constants.hpp @@ -63,6 +63,18 @@ namespace panic { //--------------------------------------------------------------------------------------------------------------------------- static const panic::types::real_t pi = static_cast(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(2); + //--------------------------------------------------------------------------------------------------------------------------- + //--------------------------------------------------------------------------------------------------------------------------- // // Type Name : panic::constants::tau // diff --git a/include/math/add.hpp b/include/math/add.hpp index f60beb0..5cec00c 100644 --- a/include/math/add.hpp +++ b/include/math/add.hpp @@ -31,18 +31,6 @@ * Description: * 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 @@ -221,7 +209,7 @@ panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::ten * * Computes: * @code - * C(i,j) = A(i,j) + b[i] + * C(i,j) = A(i,j) + b[j] * @endcode * * @tparam T Numeric element type. @@ -240,7 +228,7 @@ bool add_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector< * * Computes: * @code - * result(i,j) = A(i,j) + b[i] + * result(i,j) = A(i,j) + b[j] * @endcode * * @tparam T Numeric element type. @@ -260,7 +248,7 @@ panic::tensor::matrix add_rowwise(const panic::tensor::matrix& A, const pa * * Computes: * @code - * C(i,j) = A(i,j) + b[j] + * C(i,j) = A(i,j) + b[i] * @endcode * * @tparam T Numeric element type. @@ -279,7 +267,7 @@ bool add_colwise(const panic::tensor::matrix& A, const panic::tensor::vector< * * Computes: * @code - * result(i,j) = A(i,j) + b[j] + * result(i,j) = A(i,j) + b[i] * @endcode * * @tparam T Numeric element type. diff --git a/include/math/maximum.hpp b/include/math/maximum.hpp new file mode 100644 index 0000000..1892614 --- /dev/null +++ b/include/math/maximum.hpp @@ -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 // for panic::vector +#include // 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 +bool maximum(const panic::tensor::vector& a, const T k, panic::tensor::vector& 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 +panic::tensor::vector maximum(const panic::tensor::vector& 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 +bool maximum(const panic::tensor::vector& a, const panic::tensor::vector& b, panic::tensor::vector& 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 +panic::tensor::vector maximum(const panic::tensor::vector& a, const panic::tensor::vector& 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 +bool maximum(const panic::tensor::matrix& A, const T k, panic::tensor::matrix& 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 +panic::tensor::matrix maximum(const panic::tensor::matrix& 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 +bool maximum(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& 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 +panic::tensor::matrix maximum(const panic::tensor::matrix& A, const panic::tensor::matrix& 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 +bool maximum_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& 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 +panic::tensor::matrix maximum_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& 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 +bool maximum_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& 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 +panic::tensor::matrix maximum_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b); + + + + + + + + + + + + + + + + + + + + + + + + + +} // namespace math +} // namespace panic \ No newline at end of file diff --git a/include/math/mul.hpp b/include/math/mul.hpp index 185b029..015ddec 100644 --- a/include/math/mul.hpp +++ b/include/math/mul.hpp @@ -218,7 +218,7 @@ bool mul(const panic::tensor::matrix& A, const panic::tensor::matrix& B, p * @note This overload is convenient, but may allocate a new vector. */ template -panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::tensor::matrix& B); +panic::tensor::matrix mul(const panic::tensor::matrix& A, const panic::tensor::matrix& B); /** @@ -226,7 +226,7 @@ panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::ten * * Computes: * @code - * C(i,j) = A(i,j) * b[i] + * C(i,j) = A(i,j) * b[j] * @endcode * * @tparam T Numeric element type. @@ -245,7 +245,7 @@ bool mul_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector< * * Computes: * @code - * result(i,j) = A(i,j) * b[i] + * result(i,j) = A(i,j) * b[j] * @endcode * * @tparam T Numeric element type. @@ -265,7 +265,7 @@ panic::tensor::matrix mul_rowwise(const panic::tensor::matrix& A, const pa * * Computes: * @code - * C(i,j) = A(i,j) * b[j] + * C(i,j) = A(i,j) * b[i] * @endcode * * @tparam T Numeric element type. @@ -284,7 +284,7 @@ bool mul_colwise(const panic::tensor::matrix& A, const panic::tensor::vector< * * Computes: * @code - * result(i,j) = A(i,j) * b[j] + * result(i,j) = A(i,j) * b[i] * @endcode * * @tparam T Numeric element type. diff --git a/include/math/trigonometry/cos.hpp b/include/math/trigonometry/cos.hpp new file mode 100644 index 0000000..7130521 --- /dev/null +++ b/include/math/trigonometry/cos.hpp @@ -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 +T cos(const T x); + + +} // namespace math +} // namespace panic \ No newline at end of file diff --git a/include/math/trigonometry/sin.hpp b/include/math/trigonometry/sin.hpp new file mode 100644 index 0000000..1163bff --- /dev/null +++ b/include/math/trigonometry/sin.hpp @@ -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 +T sin(const T x); + + +} // namespace math +} // namespace panic \ No newline at end of file diff --git a/include/neural_network/activation/activation_ReLU.hpp b/include/neural_network/activation/activation_ReLU.hpp new file mode 100644 index 0000000..82d79b2 --- /dev/null +++ b/include/neural_network/activation/activation_ReLU.hpp @@ -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 // panic::uint_t, panic::int_t, and panic::real_t +#include // for base layer struct + +#include + + +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 + diff --git a/include/neural_network/datasets/sine_data.hpp b/include/neural_network/datasets/sine_data.hpp new file mode 100644 index 0000000..df34784 --- /dev/null +++ b/include/neural_network/datasets/sine_data.hpp @@ -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 +#include +#include +//--------------------------------------------------------------------------------------------------------------------------- +// 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 +bool sine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix& X, panic::tensor::vector& 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 +bool sine_cosine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix& X, panic::tensor::matrix& y); + + + + + } // namespace neural_network +} // namespace panic \ No newline at end of file diff --git a/include/neural_network/datasets/spiral_data.hpp b/include/neural_network/datasets/spiral_data.hpp new file mode 100644 index 0000000..5b3bd5f --- /dev/null +++ b/include/neural_network/datasets/spiral_data.hpp @@ -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 +#include +#include +//--------------------------------------------------------------------------------------------------------------------------- +// 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 +bool spiral_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix& X, panic::tensor::uint_vector& y); + + + + + + } // namespace neural_network +} // namespace panic \ No newline at end of file diff --git a/include/neural_network/datasets/vertical_data.hpp b/include/neural_network/datasets/vertical_data.hpp new file mode 100644 index 0000000..e59daa3 --- /dev/null +++ b/include/neural_network/datasets/vertical_data.hpp @@ -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 +#include +#include +//--------------------------------------------------------------------------------------------------------------------------- +// 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 +bool vertical_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix& X, panic::tensor::uint_vector& y); + + + + + + } // namespace neural_network +} // namespace panic \ No newline at end of file diff --git a/include/neural_network/model/model.hpp b/include/neural_network/model/model.hpp index 0ed4ebb..a47e622 100644 --- a/include/neural_network/model/model.hpp +++ b/include/neural_network/model/model.hpp @@ -118,8 +118,6 @@ struct model{ */ bool add(layer* new_layer); - // Create and add a dense layer. - /** * @brief Adds a dense layer to the model. * @@ -140,6 +138,21 @@ struct model{ 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 * diff --git a/include/random/uniform.hpp b/include/random/uniform.hpp index 4951c2d..97788a4 100644 --- a/include/random/uniform.hpp +++ b/include/random/uniform.hpp @@ -187,10 +187,3 @@ panic::tensor::real_vector uniform_vector(const panic::types::real_t min, const } // namespace random } // namespace panic -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE -//--------------------------------------------------------------------------------------------------------------------------- \ No newline at end of file diff --git a/include/tensor/generators/linspace.hpp b/include/tensor/generators/linspace.hpp new file mode 100644 index 0000000..a46152d --- /dev/null +++ b/include/tensor/generators/linspace.hpp @@ -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 // for panic::vector +#include + +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 +bool linspace(const T start, + const T stop, + const panic::types::uint_t num, + panic::tensor::vector& 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 +panic::tensor::vector linspace(const T start, + const T stop, + const panic::types::uint_t num, + const bool endpoint=true); + + + + +} // namespace tensor +} // namespace panic \ No newline at end of file diff --git a/include/tensor/matrix.hpp b/include/tensor/matrix.hpp index 6b9f5df..c249483 100644 --- a/include/tensor/matrix.hpp +++ b/include/tensor/matrix.hpp @@ -36,6 +36,7 @@ // INCLUDE DESCRIPTION //--------------------------------------------------------------------------------------------------------------------------- #include // panic::uint_t, panic::int_t, and panic::real_t +#include namespace panic{ @@ -187,6 +188,55 @@ struct matrix{ */ 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 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 v); + }; diff --git a/main.cpp b/main.cpp index 99559b6..f5147b9 100644 --- a/main.cpp +++ b/main.cpp @@ -49,6 +49,16 @@ #include #include +#include +#include +#include +#include +#include +#include +#include + + +#include @@ -691,92 +701,27 @@ int main(void) { //benchmark_omp_min_work(); // 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(1), static_cast(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; - panic::neural_network::layer_dense layer_dense01(3,4); - std::cout << layer_dense01.forward(D1) << std::endl; - panic::io::print_matrix(layer_dense01.outputs); - + panic::tensor::real_matrix X; + panic::tensor::uint_vector y; + 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; - mymodel.add_layer_dense(3,4); - mymodel.forward(D1); - - + // Create Dense layer with 2 input features and 3 output values + mymodel.add_layer_dense(2,3); + + mymodel.add_activation_ReLU(); + mymodel.forward(X); + panic::io::print_matrix(mymodel.outputs); diff --git a/src/math/maximum.cpp b/src/math/maximum.cpp new file mode 100644 index 0000000..4e96761 --- /dev/null +++ b/src/math/maximum.cpp @@ -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 +#include + +#include // for panic::vector +#include // 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 +bool maximum(const panic::tensor::vector& a, const T k, panic::tensor::vector& 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(const panic::tensor::vector& a, + const panic::types::uint_t k, + panic::tensor::vector& c +); +template bool maximum(const panic::tensor::vector& a, + const panic::types::int_t k, + panic::tensor::vector& c +); +template bool maximum(const panic::tensor::vector& a, + const panic::types::real_t k, + panic::tensor::vector& c +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of vector compared with scalar +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::vector maximum(const panic::tensor::vector& a, const T k){ + panic::tensor::vector c(a.size()); + + if (!maximum(a, k, c)){ + return panic::tensor::vector(); + } + + 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 + maximum(const panic::tensor::vector& a, + const panic::types::uint_t k +); +template panic::tensor::vector + maximum(const panic::tensor::vector& a, + const panic::types::int_t k +); +template panic::tensor::vector + maximum(const panic::tensor::vector& a, + const panic::types::real_t k +); + + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of vector compared with another vector +//-------------------------------------------------------------------------------------------------------------------------- +template +bool maximum(const panic::tensor::vector& a, const panic::tensor::vector& b, panic::tensor::vector& 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& a, + const panic::tensor::vector& b, + panic::tensor::vector& c +); +template bool maximum(const panic::tensor::vector& a, + const panic::tensor::vector& b, + panic::tensor::vector& c +); +template bool maximum(const panic::tensor::vector& a, + const panic::tensor::vector& b, + panic::tensor::vector& c +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of vector compared with another vector +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::vector maximum(const panic::tensor::vector& a, const panic::tensor::vector& b){ + panic::tensor::vector c(a.size()); + + if (!maximum(a, b, c)){ + return panic::tensor::vector(); + } + + 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 + maximum(const panic::tensor::vector& a, + const panic::tensor::vector& b +); +template panic::tensor::vector + maximum(const panic::tensor::vector& a, + const panic::tensor::vector& b +); +template panic::tensor::vector + maximum(const panic::tensor::vector& a, + const panic::tensor::vector& b +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of matrix compared with scalar +//-------------------------------------------------------------------------------------------------------------------------- +template +bool maximum(const panic::tensor::matrix& A, const T k, panic::tensor::matrix& 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& A, + const panic::types::uint_t k, + panic::tensor::matrix& C +); +template bool maximum(const panic::tensor::matrix& A, + const panic::types::int_t k, + panic::tensor::matrix& C +); +template bool maximum(const panic::tensor::matrix& A, + const panic::types::real_t k, + panic::tensor::matrix& C +); + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of matrix compared with scalar +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix maximum(const panic::tensor::matrix& A, const T k){ + panic::tensor::matrix C; + + if (!maximum(A, k, C)){ + return panic::tensor::matrix(); + } + + 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 + maximum(const panic::tensor::matrix& A, + const panic::types::uint_t k +); +template panic::tensor::matrix + maximum(const panic::tensor::matrix& A, + const panic::types::int_t k +); +template panic::tensor::matrix + maximum(const panic::tensor::matrix& A, + const panic::types::real_t k +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of two matrices +//-------------------------------------------------------------------------------------------------------------------------- +template +bool maximum(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& 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& A, + const panic::tensor::matrix& B, + panic::tensor::matrix& C +); +template bool maximum(const panic::tensor::matrix& A, + const panic::tensor::matrix& B, + panic::tensor::matrix& C +); +template bool maximum(const panic::tensor::matrix& A, + const panic::tensor::matrix& B, + panic::tensor::matrix& C +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum +// +// Description: +// Clips maximum of two matrices +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix maximum(const panic::tensor::matrix& A, const panic::tensor::matrix& B){ + panic::tensor::matrix C; + + if (!maximum(A, B, C)){ + return panic::tensor::matrix(); + } + + 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 + maximum(const panic::tensor::matrix& A, + const panic::tensor::matrix& B +); +template panic::tensor::matrix + maximum(const panic::tensor::matrix& A, + const panic::tensor::matrix& B +); +template panic::tensor::matrix + maximum(const panic::tensor::matrix& A, + const panic::tensor::matrix& B +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum_rowwise +// +// Description: +// Clips maximum rowwise of matrix compared with vector +//-------------------------------------------------------------------------------------------------------------------------- +template +bool maximum_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& 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& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool maximum_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool maximum_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum_rowwise +// +// Description: +// Clips maximum rowwise of matrix compared with vector +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix maximum_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b){ + panic::tensor::matrix C; + + if (!maximum_rowwise(A, b, C)){ + return panic::tensor::matrix(); + } + + 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 + maximum_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + maximum_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + maximum_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum_colwise +// +// Description: +// Clips maximum column-wise of matrix compared with vector +//-------------------------------------------------------------------------------------------------------------------------- +template +bool maximum_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& 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& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool maximum_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool maximum_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::maximum_colwise +// +// Description: +// Clips maximum column-wise of matrix compared with vector +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix maximum_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b){ + panic::tensor::matrix C; + + + if (!maximum_colwise(A, b, C)){ + return panic::tensor::matrix(); + } + + 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 + maximum_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + maximum_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + maximum_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); + + + + + + + + + + + + + + + + + + + + + + + + + + + } // namespace math +} // namespace panic diff --git a/src/math/trigonometry/cos.cpp b/src/math/trigonometry/cos.cpp new file mode 100644 index 0000000..d200772 --- /dev/null +++ b/src/math/trigonometry/cos.cpp @@ -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 +#include + +#include +#include + +//--------------------------------------------------------------------------------------------------------------------------- +// INPLEMENTATION +//--------------------------------------------------------------------------------------------------------------------------- + +namespace panic { + namespace math { + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::sin +// +// Description: +// Calculates cosinus of x +//-------------------------------------------------------------------------------------------------------------------------- +template +T cos(const T x){ + + return panic::math::sin(x + static_cast(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(const panic::types::real_t x +); + + + + + + + + + + } // namespace math +} // namespace panic \ No newline at end of file diff --git a/src/math/trigonometry/sin.cpp b/src/math/trigonometry/sin.cpp new file mode 100644 index 0000000..8dc4d8b --- /dev/null +++ b/src/math/trigonometry/sin.cpp @@ -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 +#include +#include + +//--------------------------------------------------------------------------------------------------------------------------- +// INPLEMENTATION +//--------------------------------------------------------------------------------------------------------------------------- + +namespace panic { + namespace math { + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::sin +// +// Description: +// Calculates sinus of x +//-------------------------------------------------------------------------------------------------------------------------- +template +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(1.0) + + x2*( static_cast(-1.0 / 6.0) + + x2*( static_cast(1.0 / 120.0) + + x2*( static_cast(-1.0 / 5040.0) + + x2*static_cast(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(const panic::types::real_t x +); + + + + + + + + + + } // namespace math +} // namespace panic \ No newline at end of file diff --git a/src/neural_network/activation/activation_ReLU.cpp b/src/neural_network/activation/activation_ReLU.cpp new file mode 100644 index 0000000..3c8ebf0 --- /dev/null +++ b/src/neural_network/activation/activation_ReLU.cpp @@ -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 +#include + +#include + + + +//--------------------------------------------------------------------------------------------------------------------------- +// 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 diff --git a/src/neural_network/datasets/sine_data.cpp b/src/neural_network/datasets/sine_data.cpp new file mode 100644 index 0000000..473787b --- /dev/null +++ b/src/neural_network/datasets/sine_data.cpp @@ -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 +#include +#include +#include +#include + +#include +#include + +#include +//--------------------------------------------------------------------------------------------------------------------------- +// 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 +bool sine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix& X, panic::tensor::vector& y){ + + if ( !X.resize(samples, 1) || !y.resize(samples) ){ + return false; + } + + X.set_col(0, panic::tensor::linspace(static_cast(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(const panic::types::uint_t samples, + const panic::types::real_t lenght, + panic::tensor::matrix& X, + panic::tensor::vector& y +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::neural_network::sine_cosine_data +// +// Description: +// Generates dataset with sinus and a cosine curve +//-------------------------------------------------------------------------------------------------------------------------- +template +bool sine_cosine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix& X, panic::tensor::matrix& y){ + + if ( !X.resize(samples, 1) || !y.resize(samples,2) ){ + return false; + } + + X.set_col(0, panic::tensor::linspace(static_cast(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(const panic::types::uint_t samples, + const panic::types::real_t lenght, + panic::tensor::matrix& X, + panic::tensor::matrix& y +); + + + + + + + + + + } // namespace neural_network +} // namespace panic \ No newline at end of file diff --git a/src/neural_network/datasets/spiral_data.cpp b/src/neural_network/datasets/spiral_data.cpp new file mode 100644 index 0000000..542fc8a --- /dev/null +++ b/src/neural_network/datasets/spiral_data.cpp @@ -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 + +#include +#include +#include +#include + + +#include +#include +#include +#include + +//--------------------------------------------------------------------------------------------------------------------------- +// 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 +bool spiral_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix& 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(j)/static_cast(samples-1); + + const T angle = static_cast(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 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(const panic::types::uint_t samples, + const panic::types::uint_t classes, + panic::tensor::matrix& X, + panic::tensor::uint_vector& y +); + + + } // namespace neural_network +} // namespace panic \ No newline at end of file diff --git a/src/neural_network/datasets/vertical_data.cpp b/src/neural_network/datasets/vertical_data.cpp new file mode 100644 index 0000000..416f622 --- /dev/null +++ b/src/neural_network/datasets/vertical_data.cpp @@ -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 + +#include +#include +#include +#include + + +#include +#include +#include +#include + + +//--------------------------------------------------------------------------------------------------------------------------- +// 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 +bool vertical_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix& X, panic::tensor::uint_vector& y){ + + + if ( !X.resize(samples*classes, 2) || !y.resize(samples*classes) ){ + return false; + } + + panic::tensor::vector x_diviation(samples*classes); + panic::tensor::vector 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(i)/static_cast(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(const panic::types::uint_t samples, + const panic::types::uint_t classes, + panic::tensor::matrix& X, + panic::tensor::uint_vector& y +); + + + } // namespace neural_network +} // namespace panic \ No newline at end of file diff --git a/src/neural_network/model/model.cpp b/src/neural_network/model/model.cpp index 147b10c..fb767df 100644 --- a/src/neural_network/model/model.cpp +++ b/src/neural_network/model/model.cpp @@ -40,6 +40,10 @@ #include #include +#include + +#include + //--------------------------------------------------------------------------------------------------------------------------- // IMPLEMENTATION //--------------------------------------------------------------------------------------------------------------------------- @@ -143,7 +147,7 @@ bool model::add(layer* new_layer){ // Creates a dense layer and adds it to the model. // // Example: -// model.add_dense(100, 64); +// model.add_layer_dense(100, 64); //-------------------------------------------------------------------------------------------------------------------------- bool model::add_layer_dense( panic::types::uint_t input_size, @@ -166,7 +170,34 @@ bool model::add_layer_dense( 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 diff --git a/src/tensor/generators/linspace.cpp b/src/tensor/generators/linspace.cpp new file mode 100644 index 0000000..62c26a4 --- /dev/null +++ b/src/tensor/generators/linspace.cpp @@ -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 +#include + +#include // for panic::vector +#include // 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 +bool linspace(const T start, + const T stop, + const panic::types::uint_t num, + panic::tensor::vector& c, + const bool endpoint){ + + + if (!c.resize(num)){ + return false; + } + + T step; + + if (endpoint){ + step = (stop - start) / static_cast(num - 1); + }else{ + step = (stop - start) / static_cast(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(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(const panic::types::uint_t start, + const panic::types::uint_t stop, + const panic::types::uint_t num, + panic::tensor::vector& c, + const bool endpoint + +); +template bool linspace(const panic::types::int_t start, + const panic::types::int_t stop, + const panic::types::uint_t num, + panic::tensor::vector& c, + const bool endpoint +); +template bool linspace(const panic::types::real_t start, + const panic::types::real_t stop, + const panic::types::uint_t num, + panic::tensor::vector& c, + const bool endpoint +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::linspace +// +// Description: +// Retuens a vector with line of linspace +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::vector linspace(const T start, + const T stop, + const panic::types::uint_t num, + const bool endpoint){ + + panic::tensor::vector c(num); + + if (!linspace(start, stop, num, c, endpoint)){ + return panic::tensor::vector(); + } + + 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 linspace(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 linspace(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 linspace(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 \ No newline at end of file diff --git a/src/tensor/matrix.cpp b/src/tensor/matrix.cpp index 26cf821..ae79787 100644 --- a/src/tensor/matrix.cpp +++ b/src/tensor/matrix.cpp @@ -48,7 +48,7 @@ * 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 matrix_omp_min_size = 10000; +static const panic::types::uint_t matrix_omp_min_size = 500; namespace panic{ namespace tensor{ @@ -308,6 +308,87 @@ const T& matrix::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 +bool matrix::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 +bool matrix::set_row(const panic::types::uint_t index_row, const panic::tensor::vector 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 +bool matrix::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 +bool matrix::set_col(const panic::types::uint_t index_col, const panic::tensor::vector 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 //---------------------------------------------------------------------------------------------------------------------------