diff --git a/include/math/matmul.hpp b/include/math/matmul.hpp index b44f01a..a231288 100644 --- a/include/math/matmul.hpp +++ b/include/math/matmul.hpp @@ -36,17 +36,7 @@ // INCLUDE DESCRIPTION //----------------------------------------------------------------------------------------------------- #include // for panic::real_matrix -//--------------------------------------------------------------------------------------------------------------------------- -// DEFINE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- -//--------------------------------------------------------------------------------------------------------------------------- -// VARIABLE DESCRIPTION -//--------------------------------------------------------------------------------------------------------------------------- - -//--------------------------------------------------------------------------------------------------------------------------- -// FUNCTION PROTOTYPE -//--------------------------------------------------------------------------------------------------------------------------- namespace panic{ namespace math{ diff --git a/include/math/mul.hpp b/include/math/mul.hpp index e69de29..185b029 100644 --- a/include/math/mul.hpp +++ b/include/math/mul.hpp @@ -0,0 +1,306 @@ +/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * + * 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.hpp + * Revision: 0.1.0 + * Date: 25-06-2026 + * Author: Michelle Bausager + * + * Description: + * Functions to multiply panic::tensor + * + *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + * + * @file mul.hpp + * @brief Public API for multiplying operations on PANIC vectors and matrices. + * + * This header contains the declarations that users of the math module should call. + * The comments here describe how each function is used, what dimensions are required, + * and what is returned on failure. + * + * Implementation details, OpenMP thresholds, and explicit template instantiations are + * kept in mul.cpp. + * + *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ +#pragma once + +#include // for panic::vector +#include // for panic::matrix + +namespace panic{ +namespace math{ + + +/** + * @brief Multiplies a scalar to every element of a vector. + * + * Computes: + * @code + * c[i] = a[i] * k + * @endcode + * + * @tparam T Numeric element type. + * @param a Input vector. + * @param k Scalar value multiplied to each element of @p a. + * @param c Output vector. Resized to match @p a. + * + * @return true if @p c was resized and filled successfully. + * @return false if resizing @p c failed. + * + * @note This overload writes the result into an existing vector to avoid + * unnecessary temporary allocations. + */ +template +bool mul(const panic::tensor::vector& a, const T k, panic::tensor::vector& c); + + +/** + * @brief Returns a new vector containing a scalar multiplied to every element. + * + * Computes: + * @code + * result[i] = a[i] * k + * @endcode + * + * @tparam T Numeric element type. + * @param a Input vector. + * @param k Scalar value miltiplied to each element of @p a. + * + * @return A new vector containing the result. + * @return An empty vector if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ +template +panic::tensor::vector mul(const panic::tensor::vector& a, const T k); + + +/** + * @brief Multiplies a vector elementwise too a vector. + * + * Computes: + * @code + * c[i] = a[i] * b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param a First vector. + * @param b Second vector. Must have size of @p a + * @param c Output vector. Resized to match @p a. + * + * @return true if @p c was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ +template +bool mul(const panic::tensor::vector& a, const panic::tensor::vector& b, panic::tensor::vector& c); + +/** + * @brief Returns a new vector containing a vector miltiplied elementwise. + * + * Computes: + * @code + * result[i] = a[i] * b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param a First vector. + * @param b Second vector. Must have size of @p a + * @param k Scalar value multiplied to each element of @p a. + * + * @return A new vector containing the result. + * @return An empty vector if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ +template +panic::tensor::vector mul(const panic::tensor::vector& a, const panic::tensor::vector& b); + + + +/** + * @brief Multiplies a scalar to every element of a matix. + * + * Computes: + * @code + * C(i,j) = A(i,j) * k + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * @param k Scalar value multiplied to each element of @p A. + * @param C Output Matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if resizing @p C failed. + * + * @note This overload writes the result into an existing vector to avoid + * unnecessary temporary allocations. + */ +template +bool mul(const panic::tensor::matrix& A, const T k, panic::tensor::matrix& C); + +/** + * @brief Returns a new matrix containing a scalar multiplied to every element. + * + * Computes: + * @code + * result(i,j) = A(i,j) * k + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * @param k Scalar value multiplied to each element of @p A. + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ +template +panic::tensor::matrix mul(const panic::tensor::matrix& A, const T k); + +/** + * @brief Multiplies a matrix elementwise too a matrix. + * + * Computes: + * @code + * C(i,j) = A(i,j) * B(i,j) + * @endcode + * + * @tparam T Numeric element type. + * @param A First matrix. + * @param B Second matrix. Must have size of @p A. + * @param C Output matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ +template +bool mul(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& C); + +/** + * @brief Returns a new matrix containing a matrix multiplied elementwise. + * + * Computes: + * @code + * result(i,j) = A(i,j) * B(i,j) + * @endcode + * + * @tparam T Numeric element type. + * @param A Input matrix. + * @param B Second matrix. Must have size of @p A. + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ +template +panic::tensor::matrix add(const panic::tensor::matrix& A, const panic::tensor::matrix& B); + + +/** + * @brief Multiplies a vector rowwise too a matrix. + * + * Computes: + * @code + * C(i,j) = A(i,j) * b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.cols(). + * @param C Output matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ +template +bool mul_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C); + +/** + * @brief Multiplies a vector rowwise too a matrix. + * + * Computes: + * @code + * result(i,j) = A(i,j) * b[i] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.cols(). + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ +template +panic::tensor::matrix mul_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b); + +/** + * @brief Multiplies a vector colwise too a matrix. + * + * Computes: + * @code + * C(i,j) = A(i,j) * b[j] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.rows(). + * @param C Output matrix. Resized to match @p A. + * + * @return true if @p C was resized and filled successfully. + * @return false if vector sizes do not match or resizing @p c failed. + */ +template +bool mul_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C); + +/** + * @brief Muliplies a vector colwise too a matrix. + * + * Computes: + * @code + * result(i,j) = A(i,j) * b[j] + * @endcode + * + * @tparam T Numeric element type. + * @param A Matrix. + * @param b Vector. Must have size of @p A.rows(). + * + * @return A new matrix containing the result. + * @return An empty matrix if the operation fails. + * + * @note This overload is convenient, but may allocate a new vector. + */ +template +panic::tensor::matrix mul_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b); + + + + +} // namespace math +} // namespace panic \ No newline at end of file diff --git a/main.cpp b/main.cpp index 928f3b1..99559b6 100644 --- a/main.cpp +++ b/main.cpp @@ -48,6 +48,8 @@ #include #include +#include + // For omp tesing: diff --git a/src/math/mul.cpp b/src/math/mul.cpp new file mode 100644 index 0000000..8c8bd77 --- /dev/null +++ b/src/math/mul.cpp @@ -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 +#include + +#include // for panic::vector +#include // for panic::matrix + +//--------------------------------------------------------------------------------------------------------------------------- +// PRIVATE CONSTANTS +//--------------------------------------------------------------------------------------------------------------------------- +/** + * @brief Minimum number of element operations before using the OpenMP-enabled loop. + * + * Small vectors and matrices are kept serial because the overhead of starting + * worker threads can be larger than the work itself. + */ +static const panic::types::uint_t mul_omp_min_work = 500; +//--------------------------------------------------------------------------------------------------------------------------- +// INPLEMENTATION +//--------------------------------------------------------------------------------------------------------------------------- + +namespace panic { + namespace math { + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a constant to a vector +//-------------------------------------------------------------------------------------------------------------------------- +template +bool mul(const panic::tensor::vector& a, const T k, panic::tensor::vector& c){ + + if (!c.resize(a.size())){ + return false; + } + + PANIC_OMP_PARALLEL_FOR_IF(a.size() > 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(const panic::tensor::vector& a, + const panic::types::uint_t k, + panic::tensor::vector& c +); +template bool mul(const panic::tensor::vector& a, + const panic::types::int_t k, + panic::tensor::vector& c +); +template bool mul(const panic::tensor::vector& a, + const panic::types::real_t k, + panic::tensor::vector& c +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a constant to a vector +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::vector mul(const panic::tensor::vector& a, const T k){ + panic::tensor::vector c(a.size()); + + if (!mul(a, k, c)){ + return panic::tensor::vector(); + } + + return c; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template panic::tensor::vector + mul(const panic::tensor::vector& a, + const panic::types::uint_t k +); +template panic::tensor::vector + mul(const panic::tensor::vector& a, + const panic::types::int_t k +); +template panic::tensor::vector + mul(const panic::tensor::vector& a, + const panic::types::real_t k +); + + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a vector to a vector +//-------------------------------------------------------------------------------------------------------------------------- +template +bool mul(const panic::tensor::vector& a, const panic::tensor::vector& b, panic::tensor::vector& c){ + + if (a.size() != b.size()){ + return false; + } + + if (!c.resize(a.size())){ + return false; + } + + PANIC_OMP_PARALLEL_FOR_IF(a.size() > 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& a, + const panic::tensor::vector& b, + panic::tensor::vector& c +); +template bool mul(const panic::tensor::vector& a, + const panic::tensor::vector& b, + panic::tensor::vector& c +); +template bool mul(const panic::tensor::vector& a, + const panic::tensor::vector& b, + panic::tensor::vector& c +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a vector to a vector +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::vector mul(const panic::tensor::vector& a, const panic::tensor::vector& b){ + panic::tensor::vector c(a.size()); + + if (!mul(a, b, c)){ + return panic::tensor::vector(); + } + + return c; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template panic::tensor::vector + mul(const panic::tensor::vector& a, + const panic::tensor::vector& b +); +template panic::tensor::vector + mul(const panic::tensor::vector& a, + const panic::tensor::vector& b +); +template panic::tensor::vector + mul(const panic::tensor::vector& a, + const panic::tensor::vector& b +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a constant to a matrix +//-------------------------------------------------------------------------------------------------------------------------- +template +bool mul(const panic::tensor::matrix& A, const T k, panic::tensor::matrix& C){ + + panic::types::uint_t rows = A.rows(); + panic::types::uint_t cols = A.cols(); + panic::types::uint_t work = rows*cols; + + if ( !C.resize(rows, cols) ){ + return false; + } + + + PANIC_OMP_PARALLEL_FOR_IF(work > 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& A, + const panic::types::uint_t k, + panic::tensor::matrix& C +); +template bool mul(const panic::tensor::matrix& A, + const panic::types::int_t k, + panic::tensor::matrix& C +); +template bool mul(const panic::tensor::matrix& A, + const panic::types::real_t k, + panic::tensor::matrix& C +); + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a constant to a matrix +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix mul(const panic::tensor::matrix& A, const T k){ + panic::tensor::matrix C; + + if (!mul(A, k, C)){ + return panic::tensor::matrix(); + } + + return C; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template panic::tensor::matrix + mul(const panic::tensor::matrix& A, + const panic::types::uint_t k +); +template panic::tensor::matrix + mul(const panic::tensor::matrix& A, + const panic::types::int_t k +); +template panic::tensor::matrix + mul(const panic::tensor::matrix& A, + const panic::types::real_t k +); + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a matrix to a matrix elementwise +//-------------------------------------------------------------------------------------------------------------------------- +template +bool mul(const panic::tensor::matrix& A, const panic::tensor::matrix& B, panic::tensor::matrix& C){ + + panic::types::uint_t rows = A.rows(); + panic::types::uint_t cols = A.cols(); + panic::types::uint_t work = rows*cols; + + if ( (rows != B.rows()) || (cols != B.cols())){ + return false; + } + + if ( !C.resize(rows, cols) ){ + return false; + } + + + PANIC_OMP_PARALLEL_FOR_IF(work > 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& A, + const panic::tensor::matrix& B, + panic::tensor::matrix& C +); +template bool mul(const panic::tensor::matrix& A, + const panic::tensor::matrix& B, + panic::tensor::matrix& C +); +template bool mul(const panic::tensor::matrix& A, + const panic::tensor::matrix& B, + panic::tensor::matrix& C +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul +// +// Description: +// Multiplies a matrix to a matrix elementwise +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix mul(const panic::tensor::matrix& A, const panic::tensor::matrix& B){ + panic::tensor::matrix C; + + if (!mul(A, B, C)){ + return panic::tensor::matrix(); + } + + return C; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template panic::tensor::matrix + mul(const panic::tensor::matrix& A, + const panic::tensor::matrix& B +); +template panic::tensor::matrix + mul(const panic::tensor::matrix& A, + const panic::tensor::matrix& B +); +template panic::tensor::matrix + mul(const panic::tensor::matrix& A, + const panic::tensor::matrix& B +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul_rowwise +// +// Description: +// Multiplies a vector row-wise to a matrix +//-------------------------------------------------------------------------------------------------------------------------- +template +bool mul_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C){ + + panic::types::uint_t rows = A.rows(); + panic::types::uint_t cols = A.cols(); + panic::types::uint_t work = rows*cols; + + if ( cols != b.size() ){ + return false; + } + + if ( !C.resize(rows, cols) ){ + return false; + } + + + PANIC_OMP_PARALLEL_FOR_IF(work > 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& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool mul_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool mul_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul_rowwise +// +// Description: +// Multiplies a vector row-wise to a matrix +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix mul_rowwise(const panic::tensor::matrix& A, const panic::tensor::vector& b){ + panic::tensor::matrix C; + + if (!mul_rowwise(A, b, C)){ + return panic::tensor::matrix(); + } + + return C; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template panic::tensor::matrix + mul_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + mul_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + mul_rowwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); + + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul_colwise +// +// Description: +// Multiplies a vector coloumn-wise to a matrix +//-------------------------------------------------------------------------------------------------------------------------- +template +bool mul_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b, panic::tensor::matrix& C){ + + panic::types::uint_t rows = A.rows(); + panic::types::uint_t cols = A.cols(); + panic::types::uint_t work = rows*cols; + + if ( rows != b.size() ){ + return false; + } + + if ( !C.resize(rows, cols) ){ + return false; + } + + + PANIC_OMP_PARALLEL_FOR_IF(work > 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& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool mul_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); +template bool mul_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b, + panic::tensor::matrix& C +); + + + +//-------------------------------------------------------------------------------------------------------------------------- +// Function Name : panic::math::mul_colwise +// +// Description: +// Multiplies a vector coloumn-wise to a matrix +//-------------------------------------------------------------------------------------------------------------------------- +template +panic::tensor::matrix mul_colwise(const panic::tensor::matrix& A, const panic::tensor::vector& b){ + panic::tensor::matrix C; + + + if (!mul_colwise(A, b, C)){ + return panic::tensor::matrix(); + } + + return C; +} +//-------------------------------------------------------------------------------------------------------------------------- +// EXPLICIT TEMPLATE INSTANTIATION +// +// The implementation is in this .cpp file. +// Build the overload for the official PANIC numeric types. +//-------------------------------------------------------------------------------------------------------------------------- +template panic::tensor::matrix + mul_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + mul_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); +template panic::tensor::matrix + mul_colwise(const panic::tensor::matrix& A, + const panic::tensor::vector& b +); + + + + } // namespace math +} // namespace panic diff --git a/src/math/mul.hpp b/src/math/mul.hpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/neural_network/layer/layer_dense.cpp b/src/neural_network/layer/layer_dense.cpp index cde95fc..d8c7210 100644 --- a/src/neural_network/layer/layer_dense.cpp +++ b/src/neural_network/layer/layer_dense.cpp @@ -39,6 +39,7 @@ #include #include +#include #include #include @@ -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); }