added the dense layer with a forward functions. I also made the add functions for most cases.
364 lines
12 KiB
C++
364 lines
12 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: add.hpp
|
|
* Revision: 0.1.0
|
|
* Date: 25-06-2026
|
|
* Author: Michelle Bausager
|
|
*
|
|
* Description:
|
|
* Functions to add panic::tensor togther;
|
|
*
|
|
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
|
#pragma one
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// INCLUDE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
#include <tensor/vector.hpp> // for panic::real_vector (uint_vector, int_vector)
|
|
#include <tensor/matrix.hpp> // for panic::real_matrix (uint_matrix, int_matrix)
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// DEFINE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// VARIABLE DESCRIPTION
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
// FUNCTION PROTOTYPE
|
|
//---------------------------------------------------------------------------------------------------------------------------
|
|
namespace panic{
|
|
namespace math{
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds a scaler to a panic::tensor::vector
|
|
// c(2) = a(2) + k
|
|
//
|
|
// Inputs:
|
|
//
|
|
// a const panic::tensor::vector<T>&
|
|
//
|
|
// k const <T>
|
|
//
|
|
// Outputs:
|
|
// c panic::tensor::vector<T>&
|
|
//
|
|
// Returns:
|
|
// bool
|
|
//
|
|
// Notes:
|
|
// The c vector is resized if nessessary.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool add(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds a scaler to a panic::tensor::vector
|
|
// c(2) = a(2) + k
|
|
//
|
|
// Inputs:
|
|
//
|
|
// a const panic::tensor::vector<T>&
|
|
//
|
|
// k const <T>
|
|
//
|
|
// Outputs:
|
|
// None.
|
|
//
|
|
// Returns:
|
|
// c panic::tensor::vector<T>
|
|
//
|
|
// Notes:
|
|
// Makes new vector and returns the result.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a, const T k);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::vector
|
|
// c(2) = a(2) + b(2)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// a const panic::tensor::vector<T>&
|
|
//
|
|
// b const panic::tensor::vector<T>&
|
|
//
|
|
// Outputs:
|
|
// c panic::tensor::vector<T>&
|
|
//
|
|
// Returns:
|
|
// bool
|
|
//
|
|
// Notes:
|
|
// If input vector dimentions are not correct, output is a empty vector and returns false.
|
|
// The c vector is resized if nessessary.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool add(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::vector
|
|
// c(2) = a(2) + b(2)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// a const panic::tensor::vector<T>&
|
|
//
|
|
// b const panic::tensor::vector<T>&
|
|
//
|
|
// Outputs:
|
|
// None.
|
|
//
|
|
// Returns:
|
|
// c panic::tensor::vector<T>
|
|
//
|
|
// Notes:
|
|
// If input vector dimentions are not correct, return is an empty vector.
|
|
// Makes new vector and returns the result.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds a scaler to a panic::tensor::matrix
|
|
// C(2,2) = A(2,2) + k
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// k const T
|
|
//
|
|
// Outputs:
|
|
// C panic::tensor::matrix<T>&
|
|
//
|
|
// Returns:
|
|
// bool
|
|
//
|
|
// Notes:
|
|
// The C matrix is resized if nessessary.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool add(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds a scaler to a panic::tensor::matrix
|
|
// C(2,2) = A(2,2) + k
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// k const T
|
|
//
|
|
// Outputs:
|
|
// None.
|
|
//
|
|
// Returns:
|
|
// C panic::tensor::matrix<T>
|
|
//
|
|
// Notes:
|
|
// Creates new matrix, C, for the result.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const T k);
|
|
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::matrix
|
|
// C(2,2) = A(2,2) + B(2,2)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// B const panic::tensor::matrix<T>&
|
|
//
|
|
// Outputs:
|
|
// C panic::tensor::matrix<T>&
|
|
//
|
|
// Returns:
|
|
// bool
|
|
//
|
|
// Notes:
|
|
// If input matrix dimentions are not correct, outputs an a empty matrix and returns false.
|
|
// The C matrix is resized if nessessary.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::matrix
|
|
// C(2,2) = A(2,2) + B(2,2)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// B const panic::tensor::matrix<T>&
|
|
//
|
|
// Outputs:
|
|
// None.
|
|
//
|
|
// Returns:
|
|
// C panic::tensor::matrix<T>
|
|
//
|
|
// Notes:
|
|
// If input matrix dimentions are not correct, returns an a empty matrix.
|
|
// Creates new matrix, C, for the result.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add_rowwise
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::matrix
|
|
// C(2,3) = A(2,3) + b(2)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// b const panic::tensor::vector<T>&
|
|
//
|
|
// Outputs:
|
|
// C panic::tensor::matrix&
|
|
//
|
|
// Returns:
|
|
// bool
|
|
//
|
|
// Notes:
|
|
// If input vector dimentions are not correct, outputs an a empty matrix and returns false.
|
|
// The C matrix is resized if nessessary.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool add_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add_rowwise
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::matrix
|
|
// C(2,3) = A(2,3) + b(2)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// b const panic::tensor::vector<T>&
|
|
//
|
|
// Outputs:
|
|
// None.
|
|
//
|
|
// Returns:
|
|
// C panic::tensor::matrix<T>&
|
|
//
|
|
// Notes:
|
|
// If input vector dimentions are not correct, returns an a empty matrix.
|
|
// Creates new matrix, C, for the result.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
panic::tensor::matrix<T> add_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add_colwise
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::matrix
|
|
// C(2,3) = A(2,3) + b(3)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// b const panic::tensor::vector<T>&
|
|
//
|
|
// Outputs:
|
|
// C panic::tensor::matrix&
|
|
//
|
|
// Returns:
|
|
// bool
|
|
//
|
|
// Notes:
|
|
// If input vector dimentions are not correct, outputs an a empty matrix and returns false.
|
|
// The C matrix is resized if nessessary.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
bool add_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
|
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
// Function Name : panic::math::add_colwise
|
|
//
|
|
// Description:
|
|
// Adds two panic::tensor::matrix
|
|
// C(2,3) = A(2,3) + b(3)
|
|
//
|
|
// Inputs:
|
|
//
|
|
// A const panic::tensor::matrix<T>&
|
|
//
|
|
// b const panic::tensor::vector<T>&
|
|
//
|
|
// Outputs:
|
|
// None.
|
|
//
|
|
// Returns:
|
|
// C panic::tensor::matrix<T>&
|
|
//
|
|
// Notes:
|
|
// If input vector dimentions are not correct, returns an a empty matrix.
|
|
// Creates new matrix, C, for the result.
|
|
//--------------------------------------------------------------------------------------------------------------------------
|
|
template <typename T>
|
|
panic::tensor::matrix<T> add_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
|
|
|
} // namespace math
|
|
} // namespace panic
|