97 lines
3.7 KiB
C++
97 lines
3.7 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
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// DEFINE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// VARIABLE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// FUNCTION PROTOTYPE
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
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
|