/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ * * 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: mean.cpp * Revision: 0.1.0 * Date: 30-07-2026 * Author: Michelle Bausager * * Description: * Functions to calculate mean of tensor arrays * *++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/ //--------------------------------------------------------------------------------------------------------------------------- // INCLUDE DESCRIPTION //----------------------------------------------------------------------------------------------------- #include #include #include // for panic::vector #include // for panic::matrix #include //--------------------------------------------------------------------------------------------------------------------------- // 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 mean_omp_min_work = 500; //--------------------------------------------------------------------------------------------------------------------------- // INPLEMENTATION //--------------------------------------------------------------------------------------------------------------------------- namespace panic { namespace math { //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::mean // // Description: // Find the mean value for a vector //-------------------------------------------------------------------------------------------------------------------------- template T mean(const panic::tensor::vector& a){ T sum = panic::math::sum(a); return sum / static_cast(a.size()); } //-------------------------------------------------------------------------------------------------------------------------- // EXPLICIT TEMPLATE INSTANTIATION // // The implementation is in this .cpp file. // Build the overload for the official PANIC numeric types. //-------------------------------------------------------------------------------------------------------------------------- template panic::types::uint_t mean(const panic::tensor::vector& a ); template panic::types::int_t mean(const panic::tensor::vector& a ); template panic::types::real_t mean(const panic::tensor::vector& a ); //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::mean // // Description: // Find the mean value for a matrix //-------------------------------------------------------------------------------------------------------------------------- template T mean(const panic::tensor::matrix& A){ panic::types::uint_t rows = A.rows(); panic::types::uint_t cols = A.cols(); panic::types::uint_t work = rows*cols; T sum = panic::math::sum(A); return sum / static_cast(work); } //-------------------------------------------------------------------------------------------------------------------------- // EXPLICIT TEMPLATE INSTANTIATION // // The implementation is in this .cpp file. // Build the overload for the official PANIC numeric types. //-------------------------------------------------------------------------------------------------------------------------- template panic::types::uint_t mean(const panic::tensor::matrix& a ); template panic::types::int_t mean(const panic::tensor::matrix& a ); template panic::types::real_t mean(const panic::tensor::matrix& a ); //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::mean_rowwise // // Description: // Find the mean row-wise of a matrix //-------------------------------------------------------------------------------------------------------------------------- template bool mean_rowwise(const panic::tensor::matrix& A, panic::tensor::vector& 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; } if (!sum_rowwise(A, b)){ return false; } // Each thread handles separate rows and writes to a separate b[i]. PANIC_OMP_PARALLEL_FOR_IF(rows > mean_omp_min_work) for (panic::types::uint_t i = 0; i < rows; ++i){ b[i] /= cols; } return true; } //-------------------------------------------------------------------------------------------------------------------------- // EXPLICIT TEMPLATE INSTANTIATION // // The implementation is in this .cpp file. // Build the overload for the official PANIC numeric types. //-------------------------------------------------------------------------------------------------------------------------- template bool mean_rowwise(const panic::tensor::matrix& A, panic::tensor::vector& b ); template bool mean_rowwise(const panic::tensor::matrix& A, panic::tensor::vector& b ); template bool mean_rowwise(const panic::tensor::matrix& A, panic::tensor::vector& b ); //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::mean_rowwise // // Description: // Returns row-wise sum values //-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::vector mean_rowwise(const panic::tensor::matrix& A){ panic::tensor::vector b; if (!mean_rowwise(A, b)){ return panic::tensor::vector(); } 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 mean_rowwise(const panic::tensor::matrix& A ); template panic::tensor::vector mean_rowwise(const panic::tensor::matrix& A ); template panic::tensor::vector mean_rowwise(const panic::tensor::matrix& A ); //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::mean_colwise // // Description: // Find the mean column-wise of a matrix //-------------------------------------------------------------------------------------------------------------------------- template bool mean_colwise(const panic::tensor::matrix& A, panic::tensor::vector& 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; } if (! sum_colwise(A,b)){ return false; } // Each thread handles separate cols and writes to a separate b[i]. PANIC_OMP_PARALLEL_FOR_IF(work > mean_omp_min_work) for (panic::types::uint_t i = 0; i < cols; ++i){ b[i] /= cols; } return true; } //-------------------------------------------------------------------------------------------------------------------------- // EXPLICIT TEMPLATE INSTANTIATION // // The implementation is in this .cpp file. // Build the overload for the official PANIC numeric types. //-------------------------------------------------------------------------------------------------------------------------- template bool mean_colwise(const panic::tensor::matrix& A, panic::tensor::vector& b ); template bool mean_colwise(const panic::tensor::matrix& A, panic::tensor::vector& b ); template bool mean_colwise(const panic::tensor::matrix& A, panic::tensor::vector& b ); //-------------------------------------------------------------------------------------------------------------------------- // Function Name : panic::math::mean_colwise // // Description: // Returns column-wise mean values //-------------------------------------------------------------------------------------------------------------------------- template panic::tensor::vector mean_colwise(const panic::tensor::matrix& A){ panic::tensor::vector b; if (!mean_colwise(A, b)){ return panic::tensor::vector(); } 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 mean_colwise(const panic::tensor::matrix& A ); template panic::tensor::vector mean_colwise(const panic::tensor::matrix& A ); template panic::tensor::vector mean_colwise(const panic::tensor::matrix& A ); } // namespace math } // namespace panic