Files
Bausager 47671354ce mul.cpp/hpp
added math/mul.cpp/hpp, not tested but should work
2026-07-28 20:09:16 +02:00

87 lines
2.9 KiB
C++

/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: matmul.hpp
* Revision: 0.1.0
* Date: 24-06-2026
* Author: Michelle Bausager
*
* Description:
* Functions to print out tensors with std::cout << x std::endl;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <tensor/matrix.hpp> // for panic::real_matrix
namespace panic{
namespace math{
/**
* @brief Calculates matrix multiplication of two matrices.
*
* Computes:
* @code
* C(n, p) = A(n,m) + B(m,p)
* @endcode
*
* @tparam T Numeric element type.
* @param A First matrix.
* @param B Second matrix. @ p B.rows needs to be the size of @p A.cols.
* @param C Output matrix. Resized to C(A.rows, B.cols).
*
* @return true if @p C was resized and filled successfully.
* @return false if resizing @p C failed or A==B.
*
* @note This overload writes the result into an existing matrix to avoid
* unnecessary temporary allocations.
*/
template <typename T>
bool matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
/**
* @brief Returns a new matrix of multiplication of two matrices.
*
* Computes:
* @code
* result(n, p) = A(n,m) + B(m,p)
* @endcode
*
* @tparam T Numeric element type.
* @param A First matrix.
* @param B Second matrix. @ p B.rows needs to be the size of @p A.cols.
*
* @return A new matrix containing the result.
* @return An empty vector if the operation fails.
*
* @note This overload is convenient, but may allocate a new matrix.
*/
template <typename T>
panic::tensor::matrix<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
} // namespace math
} // namespace panic