Activation Softmax Forward done
This commit is contained in:
@@ -0,0 +1,625 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: div.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 25-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to divdes panic::tensor
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/div.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 div_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::div
|
||||
//
|
||||
// Description:
|
||||
// Divedes a constant to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool div(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c){
|
||||
|
||||
if (!c.resize(a.size())){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (k == T{0}){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(a.size() > div_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
c[i] = a[i] / k;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool div<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 div<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 div<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::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a constant to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> div(const panic::tensor::vector<T>& a, const T k){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!div(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>
|
||||
div(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
div(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
div(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a vector to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool div(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;
|
||||
}
|
||||
|
||||
bool valid = true;
|
||||
|
||||
// Check all divisors in parallel.
|
||||
// valid remains true only if every divisor is nonzero.
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF( a.size() > div_omp_min_work, &&, valid )
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
const bool nonzero = b[i] != T{0};
|
||||
valid = valid && nonzero;
|
||||
|
||||
if (nonzero){
|
||||
c[i] = a[i] / b[i];
|
||||
}
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool div(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 div(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 div(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::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a vector to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> div(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!div(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>
|
||||
div(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>
|
||||
div(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>
|
||||
div(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a constant to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool div(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;
|
||||
}
|
||||
|
||||
if (k == T{0}){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > div_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
C(i,j) = A(i,j) / k;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool div(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 div(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 div(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::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a constant to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> div(const panic::tensor::matrix<T>& A, const T k){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!div(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>
|
||||
div(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
div(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
div(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a matrix to a matrix elementwise
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool div(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;
|
||||
}
|
||||
|
||||
bool valid = true;
|
||||
|
||||
// Check all divisors in parallel.
|
||||
// valid remains true only if every divisor is nonzero.
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF( work > div_omp_min_work, &&, valid )
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i) {
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j) {
|
||||
|
||||
const bool nonzero = B(i,j) != T{0};
|
||||
valid = valid && nonzero;
|
||||
|
||||
if (nonzero) {
|
||||
C(i,j) = A(i,j) / B(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return valid;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool div(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 div(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 div(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::div
|
||||
//
|
||||
// Description:
|
||||
// Divides a matrix to a matrix elementwise
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> div(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!div(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>
|
||||
div(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>
|
||||
div(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>
|
||||
div(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::matrix<panic::types::real_t>& B
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::div_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Divides a vector row-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool div_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;
|
||||
}
|
||||
|
||||
|
||||
bool valid = true;
|
||||
|
||||
// Check all divisors in parallel.
|
||||
// valid remains true only if every divisor is nonzero.
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF( work > div_omp_min_work, &&, valid )
|
||||
for (panic::types::uint_t i = 0; i < cols; ++i){
|
||||
const bool nonzero = b[i] != T{0};
|
||||
valid = valid && nonzero;
|
||||
for (panic::types::uint_t j = 0; j < rows; ++j){
|
||||
if (nonzero){
|
||||
C(j,i) = A(j,i) / b[i];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool div_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 div_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 div_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::div_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Divides a vector row-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> div_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!div_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>
|
||||
div_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>
|
||||
div_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>
|
||||
div_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::div_colwise
|
||||
//
|
||||
// Description:
|
||||
// Divides a vector coloumn-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool div_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;
|
||||
}
|
||||
|
||||
bool valid = true;
|
||||
|
||||
// Check all divisors in parallel.
|
||||
// valid remains true only if every divisor is nonzero.
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF( work > div_omp_min_work, &&, valid )
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
const bool nonzero = b[i] != T{0};
|
||||
valid = valid && nonzero;
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
if (nonzero){
|
||||
C(i,j) = A(i,j) / b[i];
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return valid;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool div_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 div_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 div_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::div_colwise
|
||||
//
|
||||
// Description:
|
||||
// Divides a vector coloumn-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> div_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
|
||||
if (!div_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>
|
||||
div_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>
|
||||
div_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>
|
||||
div_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
+2
-2
@@ -73,7 +73,7 @@ T max(const panic::tensor::vector<T>& a){
|
||||
|
||||
// Find the maximum in parallel for large vectors.
|
||||
// Each thread computes a local maximum, then OpenMP combines them into y.
|
||||
#pragma omp parallel for if(a.size() > max_omp_min_work) reduction(max:y)
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF(a.size() > max_omp_min_work, max, y)
|
||||
for (panic::types::uint_t i = 1; i < a.size(); ++i) {
|
||||
if (a[i] > y) {
|
||||
y = a[i];
|
||||
@@ -114,7 +114,7 @@ T max(const panic::tensor::matrix<T>& A){
|
||||
|
||||
// Find the maximum in parallel for large vectors.
|
||||
// Each thread computes a local maximum, then OpenMP combines them into y.
|
||||
#pragma omp parallel for if(work > max_omp_min_work) reduction(max:y)
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF(work > max_omp_min_work, max, y)
|
||||
for (panic::types::uint_t i = 0; i < A.rows(); ++i) {
|
||||
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
|
||||
if (A(i,j) > y) {
|
||||
|
||||
@@ -0,0 +1,582 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: sub.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 25-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to subtracts panic::tensors togther;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/sub.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 sub_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a constant to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sub(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() > sub_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
c[i] = a[i] - k;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sub<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 sub<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 sub<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::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a constant to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> sub(const panic::tensor::vector<T>& a, const T k){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!sub(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>
|
||||
sub(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
sub(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
sub(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a vector to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sub(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() > sub_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
c[i] = a[i] - b[i];
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sub(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 sub(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 sub(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::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a vector to a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> sub(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!sub(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>
|
||||
sub(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>
|
||||
sub(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>
|
||||
sub(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a constant to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sub(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 > sub_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
C(i,j) = A(i,j) - k;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sub(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 sub(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 sub(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::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a constant to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> sub(const panic::tensor::matrix<T>& A, const T k){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!sub(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>
|
||||
sub(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
sub(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
sub(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a matrix to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sub(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 > sub_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
C(i,j) = A(i,j) - B(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 sub(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 sub(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 sub(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::sub
|
||||
//
|
||||
// Description:
|
||||
// subtracts a matrix to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> sub(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!sub(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>
|
||||
sub(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>
|
||||
sub(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>
|
||||
sub(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::matrix<panic::types::real_t>& B
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sub_rowwise
|
||||
//
|
||||
// Description:
|
||||
// subtracts a vector row-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sub_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 > sub_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
C(i,j) = A(i,j) - b[j];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sub_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 sub_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 sub_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::sub_rowwise
|
||||
//
|
||||
// Description:
|
||||
// subtracts a vector row-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> sub_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!sub_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>
|
||||
sub_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>
|
||||
sub_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>
|
||||
sub_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sub_colwise
|
||||
//
|
||||
// Description:
|
||||
// subtracs a vector coloumn-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sub_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 > sub_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
C(i,j) = A(i,j) - b[i];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool sub_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 sub_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 sub_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::sub_colwise
|
||||
//
|
||||
// Description:
|
||||
// subtracts a vector coloumn-wise to a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> sub_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
|
||||
if (!sub_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>
|
||||
sub_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>
|
||||
sub_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>
|
||||
sub_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,327 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: sum.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 25-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to sum up arrays
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/sum.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 sum_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sum
|
||||
//
|
||||
// Description:
|
||||
// Find the sum value for a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T sum(const panic::tensor::vector<T>& a){
|
||||
|
||||
T result = T{0};
|
||||
|
||||
// Find the maximum in parallel for large vectors.
|
||||
// Each thread computes a partial sum, then OpenMP combines them into result.
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF(a.size() > sum_omp_min_work, +, result)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i) {
|
||||
result += a[i];
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t sum<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& a
|
||||
);
|
||||
template panic::types::int_t sum<panic::types::int_t>(const panic::tensor::vector<panic::types::int_t>& a
|
||||
);
|
||||
template panic::types::real_t sum<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sum
|
||||
//
|
||||
// Description:
|
||||
// Find the sum value for a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T sum(const panic::tensor::matrix<T>& A){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
T result = A(0,0);
|
||||
|
||||
// Find the maximum in parallel for large vectors.
|
||||
// Each thread computes a partial sum, then OpenMP combines them into result.
|
||||
PANIC_OMP_PARALLEL_FOR_REDUCTION_IF(work > sum_omp_min_work, +, result)
|
||||
for (panic::types::uint_t i = 0; i < A.rows(); ++i) {
|
||||
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
|
||||
result += A(i,j);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t sum<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& a
|
||||
);
|
||||
template panic::types::int_t sum<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& a
|
||||
);
|
||||
template panic::types::real_t sum<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sum_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Find the sum row-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sum_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& b){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !b.resize(rows) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Each thread handles separate rows and writes to a separate b[i].
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > sum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
b[i] = T{0};
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
b[i] += 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 sum_rowwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template bool sum_rowwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template bool sum_rowwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sum_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Returns row-wise sum values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> sum_rowwise(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::vector<T> b;
|
||||
|
||||
if (!sum_rowwise(A, b)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// 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>
|
||||
sum_rowwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
sum_rowwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
sum_rowwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sum_colwise
|
||||
//
|
||||
// Description:
|
||||
// Find the sum column-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool sum_colwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& b){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !b.resize(cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Each thread handles separate cols and writes to a separate b[i].
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > sum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < cols; ++i){
|
||||
b[i] = T{0};
|
||||
for (panic::types::uint_t j = 0; j < rows; ++j){
|
||||
b[i] += 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 sum_colwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template bool sum_colwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template bool sum_colwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sum_colwise
|
||||
//
|
||||
// Description:
|
||||
// Returns column-wise sum values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> sum_colwise(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::vector<T> b;
|
||||
|
||||
if (!sum_colwise(A, b)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// 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>
|
||||
sum_colwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
sum_colwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
sum_colwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
+8
-23
@@ -22,7 +22,7 @@
|
||||
*
|
||||
* Project Name: PANIC
|
||||
* Module Name: neural_network
|
||||
* File Name: activation_ReLU.cpp
|
||||
* File Name: activation_relu.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-08-2026
|
||||
* Author: Michelle Bausager
|
||||
@@ -35,7 +35,7 @@
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <neural_network/activation/activation_ReLU.hpp>
|
||||
#include <neural_network/activation/activation_relu.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <math/clip.hpp>
|
||||
@@ -61,53 +61,38 @@ namespace panic{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::neural_network::activation_ReLU
|
||||
// Constructor Name : panic::neural_network::activation_relu
|
||||
//
|
||||
// Description:
|
||||
// Creates an empty layer.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
activation_ReLU::activation_ReLU() {
|
||||
activation_relu::activation_relu() {
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_ReLU.forward
|
||||
// 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){
|
||||
bool activation_relu::forward(const panic::tensor::real_matrix& inputs){
|
||||
|
||||
panic::math::clip_lower(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
|
||||
// Function Name : panic::neural_network::activation_relu.backward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the backward pass:
|
||||
// ??
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_ReLU::backward(const panic::tensor::real_matrix& dinputs){
|
||||
bool activation_relu::backward(const panic::tensor::real_matrix& dinputs){
|
||||
|
||||
|
||||
return true;
|
||||
@@ -0,0 +1,144 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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_softmax.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-08-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the activation layer for Softmax
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <neural_network/activation/activation_softmax.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <math/exp.hpp>
|
||||
#include <math/max.hpp>
|
||||
#include <math/sum.hpp>
|
||||
#include <math/div.hpp>
|
||||
#include <math/sub.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_softmax_omp_min_size = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic{
|
||||
namespace neural_network{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::neural_network::activation_softmax
|
||||
//
|
||||
// Description:
|
||||
// Creates an empty layer.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
activation_softmax::activation_softmax() {
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_softmax.forward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the forward pass:
|
||||
// outputs = max(inputs, 0)
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_softmax::forward(const panic::tensor::real_matrix& inputs){
|
||||
|
||||
panic::tensor::real_vector row_maximums;
|
||||
panic::tensor::real_vector row_sums;
|
||||
|
||||
// Each row represents one sample.
|
||||
// Find the maximum class score in each sample
|
||||
//
|
||||
// row_maximums[i] = max(inputs(i, 0), ..., inputs(i, cols - 1))
|
||||
if (! panic::math::max_rowwise(inputs, row_maximums)){
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
// row_maximums contains one value per row, so it behaves
|
||||
// like a column vector broadcast across all columns:
|
||||
//
|
||||
// outputs(i, j) = inputs(i, j) - row_maximums[i]
|
||||
if (!panic::math::sub_colwise(inputs, row_maximums, outputs)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Convert the shifted scores into positive exponential values
|
||||
if (!panic::math::exp(outputs, outputs)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Sum the exponential values in each sample
|
||||
//
|
||||
// row_sums[i] = sum(outputs(i, 0), ..., outputs(i, cols - 1))
|
||||
if (!panic::math::sum_rowwise(outputs, row_sums)){
|
||||
return false;
|
||||
}
|
||||
|
||||
// row_sums contains one value per row, so broadcast it
|
||||
// across all columns and normalize each sample:
|
||||
//
|
||||
// outputs(i, j) = outputs(i, j) / row_sums[i]
|
||||
if (!panic::math::div_colwise(outputs, row_sums, outputs)){
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::activation_softmax.backward
|
||||
//
|
||||
// Description:
|
||||
// Calculated the backward pass:
|
||||
// ??
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_softmax::backward(const panic::tensor::real_matrix& dinputs){
|
||||
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
@@ -42,7 +42,8 @@
|
||||
|
||||
#include <neural_network/layer/layer_dense.hpp>
|
||||
|
||||
#include <neural_network/activation/activation_ReLU.hpp>
|
||||
#include <neural_network/activation/activation_relu.hpp>
|
||||
#include <neural_network/activation/activation_softmax.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// IMPLEMENTATION
|
||||
@@ -171,18 +172,47 @@ bool model::add_layer_dense(
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::neural_network::model::add_activation_ReLU
|
||||
// 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();
|
||||
// model.activation_relu();
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool model::add_activation_ReLU(){
|
||||
bool model::add_activation_relu(){
|
||||
|
||||
// Create the ReLU layer.
|
||||
activation_ReLU* new_layer = new activation_ReLU();
|
||||
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::add_activation_softmax
|
||||
//
|
||||
// Description:
|
||||
// Creates a activation Softmax layer and adds it to the model.
|
||||
//
|
||||
// Example:
|
||||
// model.activation_softmax();
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool model::add_activation_softmax(){
|
||||
|
||||
// Create the ReLU layer.
|
||||
activation_softmax* new_layer = new activation_softmax();
|
||||
|
||||
if (new_layer == 0){
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user