max.hpp is done
This commit is contained in:
@@ -0,0 +1,712 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: clip.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions that clips the value
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the max values in the vector or scalar.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < k)
|
||||
* c[i] = k
|
||||
* else
|
||||
* c[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k Scalar value compared 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 <typename T>
|
||||
bool clip_lower(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a cliped max value in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < k)
|
||||
* result[i] = k
|
||||
* else
|
||||
* result[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k Scalar value compared 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 <typename T>
|
||||
panic::tensor::vector<T> clip_lower(const panic::tensor::vector<T>& a, const T k);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the max elementwise in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < b[i])
|
||||
* c[i] = b[i]
|
||||
* else
|
||||
* c[i] = a[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 <typename T>
|
||||
bool clip_lower(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector containing cliped max elementwise of a vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < b[i])
|
||||
* result[i] = b[i]
|
||||
* else
|
||||
* result[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a First vector.
|
||||
* @param b Second vector. Must have size 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 <typename T>
|
||||
panic::tensor::vector<T> clip_lower(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the max elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < k)
|
||||
* C(i,j) = k
|
||||
* else
|
||||
* C(i,j) = A(i,j)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value compared 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 matrix to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_lower(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the cliped max elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < k)
|
||||
* result(i,j) = k
|
||||
* else
|
||||
* result(i,j) = A(i,j)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value compared 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 matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> clip_lower(const panic::tensor::matrix<T>& A, const T k);
|
||||
|
||||
/**
|
||||
* @brief Clips the max of a matrix elementwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < B(i,j))
|
||||
* C(i,j) = B(i,j)
|
||||
* else
|
||||
* C(i,j) = A(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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_lower(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a matrix elementwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < B(i,j))
|
||||
* result(i,j) = B(i,j)
|
||||
* else
|
||||
* result(i,j) = A(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 matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> clip_lower(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
|
||||
|
||||
/**
|
||||
* @brief Clips the max of a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[j])
|
||||
* C(i,j) = b[j])
|
||||
* else
|
||||
* C(i,j) = A(i,j)
|
||||
* @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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_lower_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a vector rowwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[j])
|
||||
* result(i,j) = b[j])
|
||||
* else
|
||||
* result(i,j) = A(i,j)
|
||||
* @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 <typename T>
|
||||
panic::tensor::matrix<T> clip_lower_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
/**
|
||||
* @brief Clips the max of a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[i])
|
||||
* C(i,j) = b[i])
|
||||
* else
|
||||
* C(i,j) = A(i,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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_lower_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a vector rowwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[i])
|
||||
* result(i,j) = b[i])
|
||||
* else
|
||||
* result(i,j) = A(i,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 <typename T>
|
||||
panic::tensor::matrix<T> clip_lower_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the min values in the vector or scalar.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] > k)
|
||||
* c[i] = k
|
||||
* else
|
||||
* c[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k Scalar value compared 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 <typename T>
|
||||
bool clip_higher(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a cliped min value in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] > k)
|
||||
* result[i] = k
|
||||
* else
|
||||
* result[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k Scalar value compared 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 <typename T>
|
||||
panic::tensor::vector<T> clip_higher(const panic::tensor::vector<T>& a, const T k);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the min elementwise in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] > b[i])
|
||||
* c[i] = b[i]
|
||||
* else
|
||||
* c[i] = a[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 <typename T>
|
||||
bool clip_higher(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector containing cliped min elementwise of a vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] > b[i])
|
||||
* result[i] = b[i]
|
||||
* else
|
||||
* result[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a First vector.
|
||||
* @param b Second vector. Must have size 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 <typename T>
|
||||
panic::tensor::vector<T> clip_higher(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the min elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > k)
|
||||
* C(i,j) = k
|
||||
* else
|
||||
* C(i,j) = A(i,j)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value compared 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 matrix to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_higher(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the cliped min elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > k)
|
||||
* result(i,j) = k
|
||||
* else
|
||||
* result(i,j) = A(i,j)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value compared 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 matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> clip_higher(const panic::tensor::matrix<T>& A, const T k);
|
||||
|
||||
/**
|
||||
* @brief Clips the min of a matrix elementwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > B(i,j))
|
||||
* C(i,j) = B(i,j)
|
||||
* else
|
||||
* C(i,j) = A(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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_higher(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a matrix elementwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > B(i,j))
|
||||
* result(i,j) = B(i,j)
|
||||
* else
|
||||
* result(i,j) = A(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 matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> clip_higher(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
|
||||
|
||||
/**
|
||||
* @brief Clips the min of a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > b[j])
|
||||
* C(i,j) = b[j])
|
||||
* else
|
||||
* C(i,j) = A(i,j)
|
||||
* @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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_higher_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a vector rowwise cliped to the minimum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > b[j])
|
||||
* result(i,j) = b[j])
|
||||
* else
|
||||
* result(i,j) = A(i,j)
|
||||
* @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 <typename T>
|
||||
panic::tensor::matrix<T> clip_higher_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
/**
|
||||
* @brief Clips the min of a vector columnwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > b[i])
|
||||
* C(i,j) = b[i])
|
||||
* else
|
||||
* C(i,j) = A(i,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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool clip_higher_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a vector column-wise cliped to the minimum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) > b[i])
|
||||
* result(i,j) = b[i])
|
||||
* else
|
||||
* result(i,j) = A(i,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 <typename T>
|
||||
panic::tensor::matrix<T> clip_higher_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,145 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: exp.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate the exponential of numbers
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief calculates the exponential of a value.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = exp(k)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param k Value to take the exp of.
|
||||
*
|
||||
* @return The calculated value
|
||||
*
|
||||
* @note This function is omp-friendly.
|
||||
*/
|
||||
template <typename T>
|
||||
T exp(const T x);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Calculates the exponential elementwise in a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* c[i] = exp(a[i])
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @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 <typename T>
|
||||
bool exp(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Calculates the exponential elementwise in a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[i] = exp(a[i])
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
*
|
||||
* @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 <typename T>
|
||||
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Calculates the expnential elementwise of a matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* C(i,j) = exp(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @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 <typename T>
|
||||
bool exp(const panic::tensor::matrix<T>& A, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns the calculated ecponential elementwise of the matrix
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result(i,j) = exp(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @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 <typename T>
|
||||
panic::tensor::matrix<T> exp(const panic::tensor::matrix<T>& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,159 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: max.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to find the maximum value;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the maximum value of a vector
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = max(a)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
*
|
||||
* @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 <typename T>
|
||||
T max(const panic::tensor::vector<T>& a);
|
||||
|
||||
/**
|
||||
* @brief Returns the maximum value of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result = max(A)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @return A new matrix containing the result.
|
||||
* @return An empty matrix if the operation fails.
|
||||
*
|
||||
*/
|
||||
template <typename T>
|
||||
T max(const panic::tensor::matrix<T>& A);
|
||||
|
||||
/**
|
||||
* @brief Find the maximum values row-wise of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* C(i) = max(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param C Output matrix. Resized to match @p A.cols().
|
||||
*
|
||||
* @return true if @p c was resized and filled successfully.
|
||||
* @return false if vector sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool max_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the maiximum row-wise elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[i] = max(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @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 <typename T>
|
||||
panic::tensor::vector<T> max_rowwise(const panic::tensor::matrix<T>& A);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Find the maximum values column-wise of a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* C(j) = max(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param C Output matrix. Resized to match @p A.rows().
|
||||
*
|
||||
* @return true if @p c was resized and filled successfully.
|
||||
* @return false if vector sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool max_colwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the maiximum column-wise elementwise.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* result[j] = max(A(i,j))
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
*
|
||||
* @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 <typename T>
|
||||
panic::tensor::vector<T> max_colwise(const panic::tensor::matrix<T>& A);
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -1,353 +0,0 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: maximum.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions that finds the maximum
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
|
||||
#include <tensor/vector.hpp> // for panic::vector
|
||||
#include <tensor/matrix.hpp> // for panic::matrix
|
||||
|
||||
namespace panic{
|
||||
namespace math{
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the maximum values in the vector or scalar.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < k)
|
||||
* c[i] = k
|
||||
* else
|
||||
* c[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k Scalar value compared 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 <typename T>
|
||||
bool maximum(const panic::tensor::vector<T>& a, const T k, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a cliped maximum value in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < k)
|
||||
* result[i] = k
|
||||
* else
|
||||
* result[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a Input vector.
|
||||
* @param k Scalar value compared 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 <typename T>
|
||||
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const T k);
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the maximum elementwise in the vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < b[i])
|
||||
* c[i] = b[i]
|
||||
* else
|
||||
* c[i] = a[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 <typename T>
|
||||
bool maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b, panic::tensor::vector<T>& c);
|
||||
|
||||
/**
|
||||
* @brief Returns a new vector containing cliped maximum elementwise of a vector.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (a[i] < b[i])
|
||||
* result[i] = b[i]
|
||||
* else
|
||||
* result[i] = a[i]
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param a First vector.
|
||||
* @param b Second vector. Must have size 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 <typename T>
|
||||
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @brief Clips the maximum elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < k)
|
||||
* C(i,j) = k
|
||||
* else
|
||||
* C(i,j) = A(i,j)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value compared 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 matrix to avoid
|
||||
* unnecessary temporary allocations.
|
||||
*/
|
||||
template <typename T>
|
||||
bool maximum(const panic::tensor::matrix<T>& A, const T k, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing the cliped maximum elementwise in the matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < k)
|
||||
* result(i,j) = k
|
||||
* else
|
||||
* result(i,j) = A(i,j)
|
||||
* @endcode
|
||||
*
|
||||
* @tparam T Numeric element type.
|
||||
* @param A Input matrix.
|
||||
* @param k Scalar value compared 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 matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const T k);
|
||||
|
||||
/**
|
||||
* @brief Clips the maximum of a matrix elementwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < B(i,j))
|
||||
* C(i,j) = B(i,j)
|
||||
* else
|
||||
* C(i,j) = A(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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a matrix elementwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < B(i,j))
|
||||
* result(i,j) = B(i,j)
|
||||
* else
|
||||
* result(i,j) = A(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 matrix.
|
||||
*/
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B);
|
||||
|
||||
/**
|
||||
* @brief Clips the maximum of a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[j])
|
||||
* C(i,j) = b[j])
|
||||
* else
|
||||
* C(i,j) = A(i,j)
|
||||
* @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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a vector rowwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[j])
|
||||
* result(i,j) = b[j])
|
||||
* else
|
||||
* result(i,j) = A(i,j)
|
||||
* @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 <typename T>
|
||||
panic::tensor::matrix<T> maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
/**
|
||||
* @brief Clips the maximum of a vector rowwise too a matrix.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[i])
|
||||
* C(i,j) = b[i])
|
||||
* else
|
||||
* C(i,j) = A(i,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 matrix sizes do not match or resizing @p c failed.
|
||||
*/
|
||||
template <typename T>
|
||||
bool maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b, panic::tensor::matrix<T>& C);
|
||||
|
||||
/**
|
||||
* @brief Returns a new matrix containing a vector rowwise cliped to the maixmum.
|
||||
*
|
||||
* Computes:
|
||||
* @code
|
||||
* if (A(i,j) < b[i])
|
||||
* result(i,j) = b[i])
|
||||
* else
|
||||
* result(i,j) = A(i,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 <typename T>
|
||||
panic::tensor::matrix<T> maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -49,13 +49,14 @@
|
||||
#include <neural_network/model/model.hpp>
|
||||
|
||||
#include <math/mul.hpp>
|
||||
#include <math/maximum.hpp>
|
||||
#include <math/clip.hpp>
|
||||
#include <neural_network/activation/activation_ReLU.hpp>
|
||||
#include <tensor/generators/linspace.hpp>
|
||||
#include <math/trigonometry/sin.hpp>
|
||||
#include <neural_network/datasets/sine_data.hpp>
|
||||
#include <neural_network/datasets/spiral_data.hpp>
|
||||
#include <neural_network/datasets/vertical_data.hpp>
|
||||
#include <math/exp.hpp>
|
||||
|
||||
|
||||
#include <math.h>
|
||||
@@ -721,9 +722,10 @@ int main(void) {
|
||||
|
||||
mymodel.forward(X);
|
||||
|
||||
panic::io::print_matrix(mymodel.outputs);
|
||||
|
||||
//panic::io::print_matrix(mymodel.outputs);
|
||||
|
||||
std::cout << panic::math::exp(15.5f) << std::endl;
|
||||
std::cout << std::exp(15.5) << std::endl;
|
||||
|
||||
|
||||
return 0;
|
||||
|
||||
+1233
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,298 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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: exp.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to calculate the exponential of numbers
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/exp.hpp>
|
||||
#include <config/omp.hpp>
|
||||
#include <config/types.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 exp_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::exp
|
||||
//
|
||||
// Description:
|
||||
// Calculates the exponential
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T exp(const T x){
|
||||
|
||||
// The identity:
|
||||
//
|
||||
// e^(-x) = 1 / e^x
|
||||
//
|
||||
// lets the rest of the function work only with positive values.
|
||||
if (x < static_cast<T>(0)) {
|
||||
return static_cast<T>(1) / exp(-x);
|
||||
}
|
||||
|
||||
// Natural logarithm of 2.
|
||||
//
|
||||
// This is useful because:
|
||||
//
|
||||
// e^(ln(2)) = 2
|
||||
//
|
||||
const T ln2 = static_cast<T>(0.69314718055994530942);
|
||||
|
||||
// Split x into:
|
||||
//
|
||||
// x = k * ln(2) + r
|
||||
//
|
||||
// For example, when x = 2:
|
||||
//
|
||||
// k = floor(2 / ln(2)) = 2
|
||||
// r = 2 - 2 * ln(2) ≈ 0.6137
|
||||
//
|
||||
// This makes r small, which makes the Taylor series
|
||||
// converge much faster.
|
||||
const panic::types::uint_t k = static_cast<panic::types::uint_t>(x / ln2);
|
||||
|
||||
const T r = x - static_cast<T>(k) * ln2;
|
||||
|
||||
// Taylor series:
|
||||
//
|
||||
// r² r³ r⁴
|
||||
// e^r = 1 + r + ---- + ---- + ---- + ...
|
||||
// 2! 3! 4!
|
||||
//
|
||||
// result starts with the first term: 1.
|
||||
T result = static_cast<T>(1);
|
||||
|
||||
// term also starts at 1, representing:
|
||||
//
|
||||
// r^0 / 0! = 1
|
||||
T term = static_cast<T>(1);
|
||||
|
||||
|
||||
for (panic::types::uint_t n = 1; n <= 15; ++n){
|
||||
|
||||
// Produce the next Taylor term from the previous one.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// n = 1: term = 1 * r / 1 = r
|
||||
// n = 2: term = r * r / 2 = r² / 2!
|
||||
// n = 3: term = r²/2 * r/3 = r³ / 3!
|
||||
//
|
||||
// This avoids separately calculating powers and factorials.
|
||||
term *= r / static_cast<T>(n);
|
||||
|
||||
// Add the new term to the approximation.
|
||||
result += term;
|
||||
}
|
||||
|
||||
// We currently have e^r.
|
||||
//
|
||||
// From:
|
||||
//
|
||||
// x = k * ln(2) + r
|
||||
//
|
||||
// we get:
|
||||
//
|
||||
// e^x = e^(k * ln(2)) * e^r
|
||||
// = 2^k * e^r
|
||||
//
|
||||
// Therefore, multiply the result by 2 exactly k times.
|
||||
for (panic::types::uint_t i = 0; i < k; ++i) {
|
||||
result *= static_cast<T>(2);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::real_t exp<panic::types::real_t>(const panic::types::real_t x
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::exp
|
||||
//
|
||||
// Description:
|
||||
// Calculates the exponential elementwise of a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool exp(const panic::tensor::vector<T>& a, panic::tensor::vector<T>& c){
|
||||
|
||||
if (!c.resize(a.size())){
|
||||
return false;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(a.size() > exp_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
c[i] = exp(a[i]);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool exp<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
panic::tensor::vector<panic::types::real_t>& c
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::exp
|
||||
//
|
||||
// Description:
|
||||
// Calculates the exponential elementwise for a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> exp(const panic::tensor::vector<T>& a){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!exp(a, 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::real_t>
|
||||
exp(const panic::tensor::vector<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::exp
|
||||
//
|
||||
// Description:
|
||||
// calculates the exponential elementwise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool exp(const panic::tensor::matrix<T>& A, 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 > exp_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) = exp(A(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 exp(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::matrix<panic::types::real_t>& C
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::exp
|
||||
//
|
||||
// Description:
|
||||
// Calculates the exponential element-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> exp(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!exp(A, 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::real_t>
|
||||
exp(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -0,0 +1,339 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 25-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions to add panic::tensors togther;
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/add.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 max_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::max
|
||||
//
|
||||
// Description:
|
||||
// Find the max value for a vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T max(const panic::tensor::vector<T>& a){
|
||||
|
||||
T y = a[0];
|
||||
|
||||
// Find the maximum in parallel for large vectors.
|
||||
// Each thread computes a local maximum, then OpenMP combines them into y.
|
||||
#pragma omp parallel for if(a.size() > max_omp_min_work) reduction(max:y)
|
||||
for (panic::types::uint_t i = 1; i < a.size(); ++i) {
|
||||
if (a[i] > y) {
|
||||
y = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
return y;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t max<panic::types::uint_t>(const panic::tensor::vector<panic::types::uint_t>& a
|
||||
);
|
||||
template panic::types::int_t max<panic::types::int_t>(const panic::tensor::vector<panic::types::int_t>& a
|
||||
);
|
||||
template panic::types::real_t max<panic::types::real_t>(const panic::tensor::vector<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::max
|
||||
//
|
||||
// Description:
|
||||
// Find the max value for a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T max(const panic::tensor::matrix<T>& A){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
T y = A(0,0);
|
||||
|
||||
// Find the maximum in parallel for large vectors.
|
||||
// Each thread computes a local maximum, then OpenMP combines them into y.
|
||||
#pragma omp parallel for if(work > max_omp_min_work) reduction(max:y)
|
||||
for (panic::types::uint_t i = 0; i < A.rows(); ++i) {
|
||||
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
|
||||
if (A(i,j) > y) {
|
||||
y = A(i,j);
|
||||
}
|
||||
}
|
||||
}
|
||||
return y;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template panic::types::uint_t max<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& a
|
||||
);
|
||||
template panic::types::int_t max<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& a
|
||||
);
|
||||
template panic::types::real_t max<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& a
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::max_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Find the maximum row-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool max_rowwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& b){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !b.resize(rows) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Each thread handles separate rows and writes to a separate b[i].
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > max_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
b[i] = A(i,0);
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
if (b[i] < A(i,j)){
|
||||
b[i] = A(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 max_rowwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template bool max_rowwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template bool max_rowwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::max_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Returns row-wise max values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> max_rowwise(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::vector<T> b;
|
||||
|
||||
if (!max_rowwise(A, b)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// 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>
|
||||
max_rowwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
max_rowwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
max_rowwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::max_colwise
|
||||
//
|
||||
// Description:
|
||||
// Find the maximum column-wise of a matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool max_colwise(const panic::tensor::matrix<T>& A, panic::tensor::vector<T>& b){
|
||||
|
||||
panic::types::uint_t rows = A.rows();
|
||||
panic::types::uint_t cols = A.cols();
|
||||
panic::types::uint_t work = rows*cols;
|
||||
|
||||
if ( !b.resize(cols) ){
|
||||
return false;
|
||||
}
|
||||
|
||||
// Each thread handles separate cols and writes to a separate b[i].
|
||||
PANIC_OMP_PARALLEL_FOR_IF(work > max_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < cols; ++i){
|
||||
b[i] = A(0,i);
|
||||
for (panic::types::uint_t j = 0; j < rows; ++j){
|
||||
if (b[i] < A(j,i)){
|
||||
b[i] = A(j,i);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool max_colwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
panic::tensor::vector<panic::types::uint_t>& b
|
||||
);
|
||||
template bool max_colwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
panic::tensor::vector<panic::types::int_t>& b
|
||||
);
|
||||
template bool max_colwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::max_colwise
|
||||
//
|
||||
// Description:
|
||||
// Returns column-wise max values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> max_colwise(const panic::tensor::matrix<T>& A){
|
||||
panic::tensor::vector<T> b;
|
||||
|
||||
if (!max_colwise(A, b)){
|
||||
return panic::tensor::vector<T>();
|
||||
}
|
||||
|
||||
return b;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// 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>
|
||||
max_colwise<panic::types::uint_t>(const panic::tensor::matrix<panic::types::uint_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
max_colwise<panic::types::int_t>(const panic::tensor::matrix<panic::types::int_t>& A
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
max_colwise<panic::types::real_t>(const panic::tensor::matrix<panic::types::real_t>& A
|
||||
);
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -1,638 +0,0 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
*
|
||||
* 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: maximum.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 29-07-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Functions that finds the maximum
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//-----------------------------------------------------------------------------------------------------
|
||||
|
||||
#include <math/maximum.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 maximum_omp_min_work = 500;
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INPLEMENTATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
namespace panic {
|
||||
namespace math {
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(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() > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
if (a[i] < k){
|
||||
c[i] = k;
|
||||
}
|
||||
else{
|
||||
c[i] = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum<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 maximum<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 maximum<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::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const T k){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!maximum(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>
|
||||
maximum(const panic::tensor::vector<panic::types::uint_t>& a,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::int_t>
|
||||
maximum(const panic::tensor::vector<panic::types::int_t>& a,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::vector<panic::types::real_t>
|
||||
maximum(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with another vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(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() > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < a.size(); ++i){
|
||||
if (a[i] < b[i]){
|
||||
c[i] = b[i];
|
||||
}
|
||||
else{
|
||||
c[i] = a[i];
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//
|
||||
// The implementation is in this .cpp file.
|
||||
// Build the overload for the official PANIC numeric types.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template bool maximum(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 maximum(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 maximum(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::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of vector compared with another vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::vector<T> maximum(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::vector<T> c(a.size());
|
||||
|
||||
if (!maximum(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>
|
||||
maximum(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>
|
||||
maximum(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>
|
||||
maximum(const panic::tensor::vector<panic::types::real_t>& a,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of matrix compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(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 > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
if (A(i,j) < k){
|
||||
C(i,j) = k;
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(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 maximum(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 maximum(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 maximum(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::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of matrix compared with scalar
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const T k){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!maximum(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>
|
||||
maximum(const panic::tensor::matrix<panic::types::uint_t>& A,
|
||||
const panic::types::uint_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::int_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::int_t>& A,
|
||||
const panic::types::int_t k
|
||||
);
|
||||
template panic::tensor::matrix<panic::types::real_t>
|
||||
maximum(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::types::real_t k
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of two matrices
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum(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 > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
if (A(i,j) < B(i,j)){
|
||||
C(i,j) = B(i,j);
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(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 maximum(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 maximum(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 maximum(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::maximum
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum of two matrices
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!maximum(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>
|
||||
maximum(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>
|
||||
maximum(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>
|
||||
maximum(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::matrix<panic::types::real_t>& B
|
||||
);
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum rowwise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum_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 > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
if (A(i,j) < b[j]){
|
||||
C(i,j) = b[j];
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(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 maximum_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 maximum_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 maximum_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::maximum_rowwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum rowwise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
if (!maximum_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>
|
||||
maximum_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>
|
||||
maximum_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>
|
||||
maximum_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::maximum_colwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum column-wise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool maximum_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 > maximum_omp_min_work)
|
||||
for (panic::types::uint_t i = 0; i < rows; ++i){
|
||||
for (panic::types::uint_t j = 0; j < cols; ++j){
|
||||
|
||||
if (A(i,j) < b[i]){
|
||||
C(i,j) = b[i];
|
||||
}
|
||||
else{
|
||||
C(i,j) = A(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 maximum_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 maximum_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 maximum_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::maximum_colwise
|
||||
//
|
||||
// Description:
|
||||
// Clips maximum column-wise of matrix compared with vector
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::tensor::matrix<T> maximum_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
|
||||
panic::tensor::matrix<T> C;
|
||||
|
||||
|
||||
if (!maximum_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>
|
||||
maximum_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>
|
||||
maximum_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>
|
||||
maximum_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
|
||||
const panic::tensor::vector<panic::types::real_t>& b
|
||||
);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
} // namespace math
|
||||
} // namespace panic
|
||||
@@ -38,7 +38,7 @@
|
||||
#include <neural_network/activation/activation_ReLU.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
#include <math/maximum.hpp>
|
||||
#include <math/clip.hpp>
|
||||
|
||||
|
||||
|
||||
@@ -78,7 +78,7 @@ activation_ReLU::activation_ReLU() {
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
bool activation_ReLU::forward(const panic::tensor::real_matrix& inputs){
|
||||
|
||||
panic::math::maximum(inputs, 0.0f, outputs);
|
||||
panic::math::clip_lower(inputs, 0.0f, outputs);
|
||||
/*
|
||||
if (inputs.cols() != weights.rows()){
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user