first model
This commit is contained in:
@@ -0,0 +1,638 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: math
|
||||
* File Name: maximum.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions that finds the maximum
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/maximum.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t maximum_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c){
|
||||
|
||||
if (!c.resize(a.size())){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(a.size() > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
if (a[i] < k){
|
||||
c[i] = k;
|
||||
}
|
||||
else{
|
||||
c[i] = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t k,
|
||||
panic::tensor::vector<panic::types::uint_t>& c
|
||||
);
|
||||
template bool maximum<panic::types::int_t>(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::types::int_t k,
|
||||
panic::tensor::vector<panic::types::int_t>& c
|
||||
);
|
||||
template bool maximum<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::types::real_t k,
|
||||
panic::tensor::vector<panic::types::real_t>& c
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const T k){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!maximum(a, k, c)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::vector<panic::types::uint_t>
|
||||
maximum(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
maximum(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
maximum(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with another vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c){
|
||||
|
||||
if (a.size() != b.size()){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!c.resize(a.size())){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(a.size() > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
if (a[i] < b[i]){
|
||||
c[i] = b[i];
|
||||
}
|
||||
else{
|
||||
c[i] = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::tensor::vector<panic::types::uint_t>& b,
|
||||
panic::tensor::vector<panic::types::uint_t>& c
|
||||
);
|
||||
template bool maximum(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::tensor::vector<panic::types::int_t>& b,
|
||||
panic::tensor::vector<panic::types::int_t>& c
|
||||
);
|
||||
template bool maximum(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::tensor::vector<panic::types::real_t>& b,
|
||||
panic::tensor::vector<panic::types::real_t>& c
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with another vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!maximum(a, b, c)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::vector<panic::types::uint_t>
|
||||
maximum(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
maximum(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
maximum(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of matrix compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !C.resize(rows, cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
if (A(i,j) < k){
|
||||
C(i,j) = k;
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::types::uint_t k,
|
||||
panic::tensor::matrix<panic::types::uint_t>& C
|
||||
);
|
||||
template bool maximum(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::types::int_t k,
|
||||
panic::tensor::matrix<panic::types::int_t>& C
|
||||
);
|
||||
template bool maximum(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::types::real_t k,
|
||||
panic::tensor::matrix<panic::types::real_t>& C
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of matrix compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const T k){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!maximum(A, k, C)){
|
||||
return panic::tensor::matrix<T>();
|
||||
}
|
||||
|
||||
return C;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::matrix<panic::types::uint_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of two matrices
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( (rows != B.rows()) || (cols != B.cols())){
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !C.resize(rows, cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) < B(i,j)){
|
||||
C(i,j) = B(i,j);
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::tensor::matrix<panic::types::uint_t>& B,
|
||||
panic::tensor::matrix<panic::types::uint_t>& C
|
||||
);
|
||||
template bool maximum(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::tensor::matrix<panic::types::int_t>& B,
|
||||
panic::tensor::matrix<panic::types::int_t>& C
|
||||
);
|
||||
template bool maximum(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::matrix<panic::types::real_t>& B,
|
||||
panic::tensor::matrix<panic::types::real_t>& C
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of two matrices
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!maximum(A, B, C)){
|
||||
return panic::tensor::matrix<T>();
|
||||
}
|
||||
|
||||
return C;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::matrix<panic::types::uint_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::tensor::matrix<panic::types::uint_t>& B
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::tensor::matrix<panic::types::int_t>& B
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::matrix<panic::types::real_t>& B
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum rowwise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( cols != b.size() ){
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !C.resize(rows, cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
if (A(i,j) < b[j]){
|
||||
C(i,j) = b[j];
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum_rowwise(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::tensor::vector<panic::types::uint_t>& b,
|
||||
panic::tensor::matrix<panic::types::uint_t>& C
|
||||
);
|
||||
template bool maximum_rowwise(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::tensor::vector<panic::types::int_t>& b,
|
||||
panic::tensor::matrix<panic::types::int_t>& C
|
||||
);
|
||||
template bool maximum_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b,
|
||||
panic::tensor::matrix<panic::types::real_t>& C
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum rowwise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!maximum_rowwise(A, b, C)){
|
||||
return panic::tensor::matrix<T>();
|
||||
}
|
||||
|
||||
return C;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::matrix<panic::types::uint_t>
|
||||
maximum_rowwise(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
maximum_rowwise(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
maximum_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum_colwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum column-wise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( rows != b.size() ){
|
||||
return false;
|
||||
}
|
||||
|
||||
if ( !C.resize(rows, cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
if (A(i,j) < b[i]){
|
||||
C(i,j) = b[i];
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(i,j);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum_colwise(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::tensor::vector<panic::types::uint_t>& b,
|
||||
panic::tensor::matrix<panic::types::uint_t>& C
|
||||
);
|
||||
template bool maximum_colwise(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::tensor::vector<panic::types::int_t>& b,
|
||||
panic::tensor::matrix<panic::types::int_t>& C
|
||||
);
|
||||
template bool maximum_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b,
|
||||
panic::tensor::matrix<panic::types::real_t>& C
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum_colwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum column-wise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
|
||||
if (!maximum_colwise(A, b, C)){
|
||||
return panic::tensor::matrix<T>();
|
||||
}
|
||||
|
||||
return C;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::matrix<panic::types::uint_t>
|
||||
maximum_colwise(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
maximum_colwise(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
maximum_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,80 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: math
|
||||
* File Name: cos.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate cossinus of x;
|
||||
* This uses panic::math::sin
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#include <math/trigonometry/cos.hpp>
|
||||
#include <math/trigonometry/sin.hpp>
|
||||
|
||||
#include <config/types.hpp>
|
||||
#include <config/constants.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sin
|
||||
//
|
||||
// Description:
|
||||
// Calculates cosinus of x
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T cos(const T x){
|
||||
|
||||
return panic::math::sin(x + static_cast<T>(panic::constants::half_pi));
|
||||
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::real_t cos<panic::types::real_t>(const panic::types::real_t x
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,107 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: math
|
||||
* File Name: sin.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate sinus of x;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
#include <math/trigonometry/sin.hpp>
|
||||
#include <config/types.hpp>
|
||||
#include <config/constants.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sin
|
||||
//
|
||||
// Description:
|
||||
// Calculates sinus of x
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T sin(const T x){
|
||||
|
||||
T temp = x;
|
||||
|
||||
// Reduce x to [-pi, pi].
|
||||
while (temp > panic::constants::pi){
|
||||
temp -= panic::constants::tau;
|
||||
}
|
||||
while (temp < -panic::constants::pi){
|
||||
temp += panic::constants::tau;
|
||||
}
|
||||
|
||||
// Reduce x further to [-pi/2, pi/2].
|
||||
if (temp > panic::constants::half_pi){
|
||||
temp = panic::constants::pi - temp;
|
||||
}else if (x < -panic::constants::half_pi){
|
||||
temp = -panic::constants::pi - temp;
|
||||
}
|
||||
|
||||
const panic::types::real_t x2 = temp * temp;
|
||||
|
||||
|
||||
// Taylor polynomial: // x - x^3/3! + x^5/5! - x^7/7! + x^9/9!
|
||||
return temp * (static_cast<panic::types::real_t>(1.0)
|
||||
+ x2*( static_cast<panic::types::real_t>(-1.0 / 6.0)
|
||||
+ x2*( static_cast<panic::types::real_t>(1.0 / 120.0)
|
||||
+ x2*( static_cast<panic::types::real_t>(-1.0 / 5040.0)
|
||||
+ x2*static_cast<panic::types::real_t>(1.0 / 362880.0)
|
||||
)
|
||||
)
|
||||
)
|
||||
);
|
||||
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::real_t sin<panic::types::real_t>(const panic::types::real_t x
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,118 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: activation_ReLU.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-08-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the activation layer for ReLU
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <neural_network/activation/activation_ReLU.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <math/maximum.hpp>
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t activation_ReLU_omp_min_size = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::neural_network::activation_ReLU
|
||||
//
|
||||
// Description:
|
||||
// Creates an empty layer.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
activation_ReLU::activation_ReLU() {
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_ReLU.forward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the forward pass:
|
||||
// outputs = max(inputs, 0)
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_ReLU::forward(const panic::tensor::real_matrix& inputs){
|
||||
|
||||
panic::math::maximum(inputs, 0.0f, outputs);
|
||||
/*
|
||||
if (inputs.cols() != weights.rows()){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!outputs.resize(inputs.rows(), weights.cols())){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!panic::math::matmul(inputs, weights, outputs)){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!panic::math::add_rowwise(outputs, biases, outputs)){
|
||||
return false;
|
||||
}
|
||||
*/
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_ReLU.backward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the backward pass:
|
||||
// ??
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_ReLU::backward(const panic::tensor::real_matrix& dinputs){
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,157 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: sine_data.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to generate dataset for neural networks;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#include <neural_network/datasets/sine_data.hpp>
|
||||
#include <tensor/vector.hpp>
|
||||
#include <tensor/matrix.hpp>
|
||||
#include <config/types.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <math/trigonometry/sin.hpp>
|
||||
#include <math/trigonometry/cos.hpp>
|
||||
|
||||
#include <tensor/generators/linspace.hpp>
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t sine_data_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace neural_network {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::sine_data
|
||||
//
|
||||
// Description:
|
||||
// Generates dataset with sinus curve
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix<T>& X, panic::tensor::vector<T>& y){
|
||||
|
||||
if ( !X.resize(samples, 1) || !y.resize(samples) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
X.set_col(0, panic::tensor::linspace(static_cast<T>(0), // start
|
||||
lenght, // stop
|
||||
samples // num
|
||||
));
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(samples > sine_data_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < samples; ++i){
|
||||
y[i] = panic::math::sin(X(i,0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sine_data<panic::types::real_t>(const panic::types::uint_t samples,
|
||||
const panic::types::real_t lenght,
|
||||
panic::tensor::matrix<panic::types::real_t>& X,
|
||||
panic::tensor::vector<panic::types::real_t>& y
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::sine_cosine_data
|
||||
//
|
||||
// Description:
|
||||
// Generates dataset with sinus and a cosine curve
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sine_cosine_data(const panic::types::uint_t samples, const T lenght, panic::tensor::matrix<T>& X, panic::tensor::matrix<T>& y){
|
||||
|
||||
if ( !X.resize(samples, 1) || !y.resize(samples,2) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
X.set_col(0, panic::tensor::linspace(static_cast<T>(0), // start
|
||||
lenght, // stop
|
||||
samples // num
|
||||
));
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(samples > sine_data_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < samples; ++i){
|
||||
y(i,0) = panic::math::sin(X(i,0));
|
||||
y(i,1) = panic::math::cos(X(i,0));
|
||||
}
|
||||
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sine_cosine_data<panic::types::real_t>(const panic::types::uint_t samples,
|
||||
const panic::types::real_t lenght,
|
||||
panic::tensor::matrix<panic::types::real_t>& X,
|
||||
panic::tensor::matrix<panic::types::real_t>& y
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace neural_network
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,123 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: spiral_data.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to generate dataset for neural networks;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#include <neural_network/datasets/spiral_data.hpp>
|
||||
|
||||
#include <tensor/vector.hpp>
|
||||
#include <tensor/matrix.hpp>
|
||||
#include <config/types.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
|
||||
#include <math/trigonometry/sin.hpp>
|
||||
#include <math/trigonometry/cos.hpp>
|
||||
#include <math/add.hpp>
|
||||
#include <random/uniform.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t spiral_data_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace neural_network {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::spiral_data
|
||||
//
|
||||
// Description:
|
||||
// Generates dataset with spiral data
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool spiral_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y){
|
||||
|
||||
|
||||
if ( !X.resize(samples*classes, 2) || !y.resize(samples*classes) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(samples*classes > spiral_data_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < classes; ++i){
|
||||
for (panic::types::uint_t j = 0; j < samples; ++j){
|
||||
|
||||
const T radius = static_cast<T>(j)/static_cast<T>(samples-1);
|
||||
|
||||
const T angle = static_cast<T>(i)*T{4} + (T{4}*radius);
|
||||
|
||||
const panic::types::uint_t row_index = (i*samples) + j;
|
||||
|
||||
X(row_index, 0) = radius*panic::math::cos(angle*T{2.5});
|
||||
X(row_index, 1) = radius*panic::math::sin(angle*T{2.5});
|
||||
|
||||
y[row_index] = i;
|
||||
}
|
||||
}
|
||||
|
||||
panic::tensor::matrix<T> random_matrix(samples*classes, 2);
|
||||
panic::random::uniform(random_matrix, T{-0.15}, T{0.15});
|
||||
|
||||
if (!panic::math::add(X, random_matrix, X)){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool spiral_data<panic::types::real_t>(const panic::types::uint_t samples,
|
||||
const panic::types::uint_t classes,
|
||||
panic::tensor::matrix<panic::types::real_t>& X,
|
||||
panic::tensor::uint_vector& y
|
||||
);
|
||||
|
||||
|
||||
} // namespace neural_network
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,118 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: vertical_data.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to generate dataset for neural networks;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#include <neural_network/datasets/vertical_data.hpp>
|
||||
|
||||
#include <tensor/vector.hpp>
|
||||
#include <tensor/matrix.hpp>
|
||||
#include <config/types.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
|
||||
#include <math/trigonometry/sin.hpp>
|
||||
#include <math/trigonometry/cos.hpp>
|
||||
#include <math/add.hpp>
|
||||
#include <random/uniform.hpp>
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t vertical_data_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace neural_network {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::vertical_data
|
||||
//
|
||||
// Description:
|
||||
// Generates dataset with vertical data
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool vertical_data(const panic::types::uint_t samples, const panic::types::uint_t classes, panic::tensor::matrix<T>& X, panic::tensor::uint_vector& y){
|
||||
|
||||
|
||||
if ( !X.resize(samples*classes, 2) || !y.resize(samples*classes) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
panic::tensor::vector<T> x_diviation(samples*classes);
|
||||
panic::tensor::vector<T> y_diviation(samples*classes);
|
||||
|
||||
panic::random::uniform(x_diviation, T{-0.1}, T{0.1});
|
||||
panic::random::uniform(y_diviation, T{-0.5}, T{0.5});
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(samples*classes > vertical_data_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < classes; ++i){
|
||||
for (panic::types::uint_t j = 0; j < samples; ++j){
|
||||
|
||||
const panic::types::uint_t row_index = (i*samples) + j;
|
||||
|
||||
X(row_index, 0) = static_cast<T>(i)/static_cast<T>(classes) + x_diviation[row_index];
|
||||
X(row_index, 1) = T{0.5} + y_diviation[row_index];
|
||||
y[row_index] = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool vertical_data<panic::types::real_t>(const panic::types::uint_t samples,
|
||||
const panic::types::uint_t classes,
|
||||
panic::tensor::matrix<panic::types::real_t>& X,
|
||||
panic::tensor::uint_vector& y
|
||||
);
|
||||
|
||||
|
||||
} // namespace neural_network
|
||||
} // namespace panic
|
||||
@@ -40,6 +40,10 @@
|
||||
#include <tensor/vector.hpp>
|
||||
#include <tensor/matrix.hpp>
|
||||
|
||||
#include <neural_network/layer/layer_dense.hpp>
|
||||
|
||||
#include <neural_network/activation/activation_ReLU.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// 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
|
||||
|
||||
@@ -0,0 +1,168 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* PANIC
|
||||
* Portable Algorithms and Numerics In C++
|
||||
*
|
||||
* Scientific computing from scratch, with feeling.
|
||||
*
|
||||
* Copyright (c) 2026 Michelle Bausager
|
||||
*
|
||||
* This file is part of PANIC.
|
||||
*
|
||||
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
|
||||
* You may redistribute and/or modify it under the terms of the GPL.
|
||||
*
|
||||
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
||||
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
||||
* See the LICENSE file for the full license text.
|
||||
*
|
||||
* SPDX-License-Identifier: GPL-3.0-or-later
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: tensor
|
||||
* File Name: linspace.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to generate linspace tensors
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#include <tensor/generators/linspace.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// PRIVATE CONSTANTS
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
/**
|
||||
* @brief Minimum number of element operations before using the OpenMP-enabled loop.
|
||||
*
|
||||
* Small vectors and matrices are kept serial because the overhead of starting
|
||||
* worker threads can be larger than the work itself.
|
||||
*/
|
||||
static const panic::types::uint_t linspace_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace tensor {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::linspace
|
||||
//
|
||||
// Description:
|
||||
// Creates a vector with a line generated by linspace
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool linspace(const T start,
|
||||
const T stop,
|
||||
const panic::types::uint_t num,
|
||||
panic::tensor::vector<T>& c,
|
||||
const bool endpoint){
|
||||
|
||||
|
||||
if (!c.resize(num)){
|
||||
return false;
|
||||
}
|
||||
|
||||
T step;
|
||||
|
||||
if (endpoint){
|
||||
step = (stop - start) / static_cast<T>(num - 1);
|
||||
}else{
|
||||
step = (stop - start) / static_cast<T>(num);
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(num > linspace_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < num; ++i){
|
||||
c[i] = start + (step*static_cast<T>(i));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool linspace<panic::types::uint_t>(const panic::types::uint_t start,
|
||||
const panic::types::uint_t stop,
|
||||
const panic::types::uint_t num,
|
||||
panic::tensor::vector<panic::types::uint_t>& c,
|
||||
const bool endpoint
|
||||
|
||||
);
|
||||
template bool linspace<panic::types::int_t>(const panic::types::int_t start,
|
||||
const panic::types::int_t stop,
|
||||
const panic::types::uint_t num,
|
||||
panic::tensor::vector<panic::types::int_t>& c,
|
||||
const bool endpoint
|
||||
);
|
||||
template bool linspace<panic::types::real_t>(const panic::types::real_t start,
|
||||
const panic::types::real_t stop,
|
||||
const panic::types::uint_t num,
|
||||
panic::tensor::vector<panic::types::real_t>& c,
|
||||
const bool endpoint
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::linspace
|
||||
//
|
||||
// Description:
|
||||
// Retuens a vector with line of linspace
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> linspace(const T start,
|
||||
const T stop,
|
||||
const panic::types::uint_t num,
|
||||
const bool endpoint){
|
||||
|
||||
panic::tensor::vector<T> c(num);
|
||||
|
||||
if (!linspace(start, stop, num, c, endpoint)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::tensor::vector<panic::types::uint_t> linspace<panic::types::uint_t>(const panic::types::uint_t start,
|
||||
const panic::types::uint_t stop,
|
||||
const panic::types::uint_t num,
|
||||
const bool endpoint
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t> linspace<panic::types::int_t>(const panic::types::int_t start,
|
||||
const panic::types::int_t stop,
|
||||
const panic::types::uint_t num,
|
||||
const bool endpoint
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t> linspace<panic::types::real_t>(const panic::types::real_t start,
|
||||
const panic::types::real_t stop,
|
||||
const panic::types::uint_t num,
|
||||
const bool endpoint
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
+82
-1
@@ -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<T>::operator()(panic::types::uint_t index_n, panic::types::uint_
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::set_row
|
||||
//
|
||||
// Description:
|
||||
// Filles and set a row to a value.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool matrix<T>::set_row(const panic::types::uint_t index_row, const T c){
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(m > matrix_omp_min_size)
|
||||
for (panic::types::uint_t i = 0; i < m; ++i){
|
||||
data[index_row*m + i] = c;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::set_row
|
||||
//
|
||||
// Description:
|
||||
// Filles and set a row to a vector.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool matrix<T>::set_row(const panic::types::uint_t index_row, const panic::tensor::vector<T> v){
|
||||
|
||||
if (m != v.size()){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(m > matrix_omp_min_size)
|
||||
for (panic::types::uint_t i = 0; i < m; ++i){
|
||||
data[index_row*m + i] = v[i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::set_col
|
||||
//
|
||||
// Description:
|
||||
// Filles and set a column to a value.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool matrix<T>::set_col(const panic::types::uint_t index_col, const T c){
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(n > matrix_omp_min_size)
|
||||
for (panic::types::uint_t i = 0; i < n; ++i){
|
||||
data[i*m + index_col] = c;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::set_col
|
||||
//
|
||||
// Description:
|
||||
// Filles and set a colmn to a vector.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool matrix<T>::set_col(const panic::types::uint_t index_col, const panic::tensor::vector<T> v){
|
||||
|
||||
if (n != v.size()){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(n > matrix_omp_min_size)
|
||||
for (panic::types::uint_t i = 0; i < n; ++i){
|
||||
data[i*m + index_col] = v[i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user