mul.cpp/hpp

added math/mul.cpp/hpp, not tested but should work
This commit is contained in:
2026-07-28 20:09:16 +02:00
parent c7e87fe191
commit 47671354ce
6 changed files with 899 additions and 12 deletions
+587
View File
@@ -0,0 +1,587 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: mul.cpp
* Revision: 0.1.0
* Date: 25-06-2026
* Author: Michelle Bausager
*
* Description:
* Functions to multiply panic::tensor
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <math/mul.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 mul_omp_min_work = 500;
//---------------------------------------------------------------------------------------------------------------------------
// INPLEMENTATION
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace math {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::mul
//
// Description:
// Multiplies a constant to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool mul(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() > mul_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 mul<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 mul<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 mul<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::mul
//
// Description:
// Multiplies a constant to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> mul(const panic::tensor::vector<T>& a, const T k){
panic::tensor::vector<T> c(a.size());
if (!mul(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>
mul(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::types::uint_t k
);
template panic::tensor::vector<panic::types::int_t>
mul(const panic::tensor::vector<panic::types::int_t>& a,
const panic::types::int_t k
);
template panic::tensor::vector<panic::types::real_t>
mul(const panic::tensor::vector<panic::types::real_t>& a,
const panic::types::real_t k
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::mul
//
// Description:
// Multiplies a vector to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool mul(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() > mul_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 mul(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 mul(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 mul(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::mul
//
// Description:
// Multiplies a vector to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> mul(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
panic::tensor::vector<T> c(a.size());
if (!mul(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>
mul(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>
mul(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>
mul(const panic::tensor::vector<panic::types::real_t>& a,
const panic::tensor::vector<panic::types::real_t>& b
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::mul
//
// Description:
// Multiplies a constant to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool mul(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 > mul_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 mul(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 mul(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 mul(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::mul
//
// Description:
// Multiplies a constant to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> mul(const panic::tensor::matrix<T>& A, const T k){
panic::tensor::matrix<T> C;
if (!mul(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>
mul(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::types::uint_t k
);
template panic::tensor::matrix<panic::types::int_t>
mul(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::types::int_t k
);
template panic::tensor::matrix<panic::types::real_t>
mul(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::types::real_t k
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::mul
//
// Description:
// Multiplies a matrix to a matrix elementwise
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool mul(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 > mul_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 mul(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 mul(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 mul(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::mul
//
// Description:
// Multiplies a matrix to a matrix elementwise
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> mul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
panic::tensor::matrix<T> C;
if (!mul(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>
mul(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>
mul(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>
mul(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::matrix<panic::types::real_t>& B
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::mul_rowwise
//
// Description:
// Multiplies a vector row-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool mul_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 > mul_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 mul_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 mul_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 mul_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::mul_rowwise
//
// Description:
// Multiplies a vector row-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> mul_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
panic::tensor::matrix<T> C;
if (!mul_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>
mul_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>
mul_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>
mul_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::mul_colwise
//
// Description:
// Multiplies a vector coloumn-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool mul_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 > mul_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
const T temp = b[i];
for (panic::types::uint_t j = 0; j < cols; ++j){
C(i,j) = A(i,j) * temp;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//
// The implementation is in this .cpp file.
// Build the overload for the official PANIC numeric types.
//--------------------------------------------------------------------------------------------------------------------------
template bool mul_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 mul_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 mul_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::mul_colwise
//
// Description:
// Multiplies a vector coloumn-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> mul_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
panic::tensor::matrix<T> C;
if (!mul_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>
mul_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>
mul_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>
mul_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b
);
} // namespace math
} // namespace panic
View File
+4 -2
View File
@@ -39,6 +39,7 @@
#include <config/omp.hpp>
#include <math/matmul.hpp>
#include <math/mul.hpp>
#include <math/add.hpp>
#include <random/uniform.hpp>
@@ -81,10 +82,11 @@ layer_dense::layer_dense() {
layer_dense::layer_dense(panic::types::uint_t input_size, panic::types::uint_t neurons) {
weights.resize(input_size, neurons);
panic::random::uniform(weights);
//panic::math::matmul(weights, 0.01f);
panic::math::mul(weights, 0.01f, weights);
biases.resize(neurons);
panic::random::uniform(biases);
biases.fill(0);
//panic::random::uniform(biases);
outputs.resize(0,0);
}