Files
panic/include/tensor/matrix.hpp
T
2026-07-29 17:34:22 +02:00

264 lines
6.3 KiB
C++

/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* PANIC
* Portable Algorithms and Numerics In C++
*
* Scientific computing from scratch, with feeling.
*
* Copyright (c) 2026 Michelle Bausager
*
* This file is part of PANIC.
*
* PANIC is free software licensed under the GNU General Public License v3.0 or later.
* You may redistribute and/or modify it under the terms of the GPL.
*
* PANIC is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the LICENSE file for the full license text.
*
* SPDX-License-Identifier: GPL-3.0-or-later
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* Project Name: PANIC
* Module Name: tensor
* File Name: matrix.hpp
* Revision: 0.1.0
* Date: 23-06-2026
* Author: Michelle Bausager
*
* Description:
* Defines the matrix object that will be used in the rest of the libraries.
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
#pragma once
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
#include <tensor/vector.hpp>
namespace panic{
namespace tensor{
/**
* @brief struct for matrix object
*
* The struct is used for all PANIC libraries
* It uses dynamic allocation with new[] nad delete[].
* Copying performs a deep copy.
*/
template <typename T>
struct matrix{
// Variable for number of rows
panic::types::uint_t n;
// Variable for number of cols
panic::types::uint_t m;
// Variable for length of data array
panic::types::uint_t length;
// Pointer to data array
T* data;
/**
* @brief Empthy constructor
*
*/
matrix();
/**
* @brief Contructor with size allocation.
*
* @param rows Number of rows in the matrix
* @param cols Number of columns in the matrix
*/
matrix(panic::types::uint_t rows, panic::types::uint_t cols);
/**
* @brief Contructor with size allocation and sets it all to a value.
*
* @param rows Number of rows in the matrix
* @param cols Number of columns in the matrix
* @param value all the values in the matrix.
*/
matrix(panic::types::uint_t rows, panic::types::uint_t cols, T value);
/**
* @brief Copy-contructor.
*
* Computes:
* @code
* matrix A(3,3);
* matrix B = A;
* @endcode
*
*/
matrix(const matrix& other);
/**
* @brief De-contructor, releases memory.
*
* @note Releases memory.
*/
~matrix();
/**
* @brief Copy-assignment.
*
* Computes:
* @code
* matrix A(3,3);
* matrix B(5,8);
* B = A;
* @endcode
*
*/
matrix& operator=(const matrix& other);
/**
* @brief Returns number of rows.
*
* Computes:
* @code
* n = A.rows();
* @endcode
*
* @note The const tells compiler that the fuction don't edit the objert.
*/
panic::types::uint_t rows() const;
/**
* @brief Returns number of columns.
*
* Computes:
* @code
* m = A.cols();
* @endcode
*
* @note The const tells compiler that the fuction don't edit the objert.
*/
panic::types::uint_t cols() const;
/**
* @brief Function to resize data matrix.
*
* Computes:
* @code
* A.resize(3,4);
* @endcode
*
* @note It hold all the privious functions if avaliable.
*/
bool resize(panic::types::uint_t new_n, panic::types::uint_t new_m);
/**
* @brief Fill data with value.
*
* Computes:
* @code
* A.fill(3.1415);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool fill(T value);
/**
* @brief Read and write matrix data using row and column
*
* Computes:
* @code
* n = A(1,2);
* @endcode
*
* @note This is unchecked and fast.
*/
T& operator()(panic::types::uint_t index_n, panic::types::uint_t index_m);
/**
* @brief Read from const matrix data using row and column
*
* Computes:
* @code
* n = const A(1,2);
* @endcode
*
* @note This is unchecked and fast.
*/
const T& operator()(panic::types::uint_t index_n, panic::types::uint_t index_m) const;
/**
* @brief Filles and set a row to a value.
*
* Computes:
* @code
* A.set_row(1, 3);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_row(const panic::types::uint_t index_row, const T v);
/**
* @brief Filles and set a row to a vector.
*
* Computes:
* @code
* A.set_row(1, vector);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_row(const panic::types::uint_t index_row, const panic::tensor::vector<T> v);
/**
* @brief Filles and set a column to a value.
*
* Computes:
* @code
* A.set_column(1, 3);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_col(const panic::types::uint_t index_col, const T v);
/**
* @brief Filles and set a column to a vector.
*
* Computes:
* @code
* A.set_col(1, vector);
* @endcode
*
* @note Sets all values in the matrix.
*/
bool set_col(const panic::types::uint_t index_col, const panic::tensor::vector<T> v);
};
//---------------------------------------------------------------------------------------------------------------------------
// TYPE ALIASES
//---------------------------------------------------------------------------------------------------------------------------
typedef matrix<panic::types::real_t> real_matrix;
typedef matrix<panic::types::int_t> int_matrix;
typedef matrix<panic::types::uint_t> uint_matrix;
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
extern template struct matrix<panic::types::real_t>;
extern template struct matrix<panic::types::int_t>;
extern template struct matrix<panic::types::uint_t>;
} // namespace tensor
} // namespace panic