Matrix
First matrix implementation with print_matrix in io
This commit is contained in:
@@ -0,0 +1,330 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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.cpp
|
||||
* 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.
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <tensor/matrix.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// DEFINE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// TYPE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
static const panic::uint_t matrix_omp_min_size = 10000;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::matrix::matrix
|
||||
//
|
||||
// Description:
|
||||
// Creates an empty matrix.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
matrix<T>::matrix() {
|
||||
n = 0;
|
||||
m = 0;
|
||||
length = 0;
|
||||
data = 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::matrix::matrix
|
||||
//
|
||||
// Description:
|
||||
// Creates a matrix with size allocation and initializes all values to zero.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
matrix<T>::matrix(panic::uint_t rows, panic::uint_t cols){
|
||||
n = rows;
|
||||
m = cols;
|
||||
length = n*m;
|
||||
|
||||
if (length == 0){
|
||||
data = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = static_cast<T>(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::matrix::matrix
|
||||
//
|
||||
// Description:
|
||||
// Creates a matrix with size allocation and initializes all values.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
matrix<T>::matrix(panic::uint_t rows, panic::uint_t cols, T value){
|
||||
n = rows;
|
||||
m = cols;
|
||||
length = n*m;
|
||||
|
||||
if (length == 0){
|
||||
data = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Constructor Name : panic::tensor::matrix::matrix
|
||||
//
|
||||
// Description:
|
||||
// Copy-contructor, makes a deep copy of another matrix like this:
|
||||
// matrix A(3);
|
||||
// matrix B = A;
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
matrix<T>::matrix(const matrix& other){
|
||||
n = other.n;
|
||||
m = other.m;
|
||||
length = other.length;
|
||||
|
||||
if (length == 0){
|
||||
data = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = other.data[i];
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Deconstructor Name : panic::tensor::matrix::~matrix
|
||||
//
|
||||
// Description:
|
||||
// Deletes the data and releases the memory.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
matrix<T>::~matrix(){
|
||||
delete[] data;
|
||||
|
||||
data = 0;
|
||||
n = 0;
|
||||
m = 0;
|
||||
length = 0;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::operator=
|
||||
//
|
||||
// Description:
|
||||
// Copy-assignment. Copies from another matrix like this:
|
||||
// matrix A(5);
|
||||
// matrix B(3);
|
||||
// B = A;
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
matrix<T>& matrix<T>::operator=(const matrix& other){
|
||||
if (this == &other){
|
||||
return *this;
|
||||
}
|
||||
|
||||
T* new_data = 0;
|
||||
|
||||
if (other.length > 0){
|
||||
new_data = new T[other.length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < other.length; ++i){
|
||||
new_data[i] = other.data[i];
|
||||
}
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
data = new_data;
|
||||
n = other.n;
|
||||
m = other.m;
|
||||
length = other.length;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::rows
|
||||
//
|
||||
// Description:
|
||||
// Returns the length/size of the matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::uint_t matrix<T>::rows() const{
|
||||
return n;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::cols
|
||||
//
|
||||
// Description:
|
||||
// Returns the length/size of the matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
panic::uint_t matrix<T>::cols() const{
|
||||
return m;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::resize
|
||||
//
|
||||
// Description:
|
||||
// Resizes the matrix to new dimentions and keeps old values
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool matrix<T>::resize(panic::uint_t new_n, panic::uint_t new_m){
|
||||
|
||||
if ((new_n == n) && (new_m == m)){
|
||||
return true;
|
||||
}
|
||||
|
||||
if ((new_n == 0) || (new_m == 0)){
|
||||
delete[] data;
|
||||
n = 0;
|
||||
m = 0;
|
||||
length = 0;
|
||||
data = 0;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
panic::uint_t new_length = new_n*new_m;
|
||||
|
||||
T* new_data = new T[new_length];
|
||||
panic::uint_t copy_size = length;
|
||||
|
||||
if (new_length < length){
|
||||
copy_size = new_length;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(copy_size > matrix_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < copy_size; ++i){
|
||||
new_data[i] = data[i];
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(new_length - copy_size > matrix_omp_min_size)
|
||||
for (panic::uint_t i = copy_size; i < new_length; ++i){
|
||||
new_data[i] = static_cast<T>(0);
|
||||
}
|
||||
|
||||
delete[] data;
|
||||
|
||||
data = new_data;
|
||||
n = new_n;
|
||||
m = new_m;
|
||||
length = new_length;
|
||||
|
||||
return true;
|
||||
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::fill
|
||||
//
|
||||
// Description:
|
||||
// Fills te matrix with a value
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool matrix<T>::fill(T value){
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = value;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::operator()
|
||||
//
|
||||
// Description:
|
||||
// Lets you read and write A(2,3)
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
T& matrix<T>::operator()(panic::uint_t index_n, panic::uint_t index_m){
|
||||
return data[index_n*m + index_m];
|
||||
}
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::tensor::matrix::operator[]
|
||||
//
|
||||
// Description:
|
||||
// Lets you read A(2,3) from a const matrix
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
const T& matrix<T>::operator()(panic::uint_t index_n, panic::uint_t index_m) const{
|
||||
return data[index_n*m + index_m];
|
||||
}
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE INSTANTIATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
template struct matrix<panic::real_t>;
|
||||
template struct matrix<panic::int_t>;
|
||||
template struct matrix<panic::uint_t>;
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
@@ -1,117 +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: <module-name>
|
||||
* File Name: <file-name>
|
||||
* Revision: 0.1.0
|
||||
* Date: <yyyy-mm-dd>
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* <short description of what this file/module does>
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
// includes like:
|
||||
// #include <stdint.h>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// DEFINE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// difinitions like:
|
||||
// #define TEST_FALG 1
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// Variable difinition like:
|
||||
// static uint32_t AmountOfCool = 0;
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// Function prototypes like:
|
||||
// static void TestLeds(void);
|
||||
|
||||
|
||||
|
||||
// Function description like:
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::matmul_serial
|
||||
//
|
||||
// Description:
|
||||
// Performs serial matrix multiplication:
|
||||
//
|
||||
// out = a * b
|
||||
//
|
||||
// Inputs:
|
||||
// a const panic::Matrix&
|
||||
// Left-hand input matrix.
|
||||
//
|
||||
// b const panic::Matrix&
|
||||
// Right-hand input matrix.
|
||||
//
|
||||
// Outputs:
|
||||
// out panic::Matrix&
|
||||
// Output matrix. Must be allocated before calling this function.
|
||||
// The function writes the result into this matrix.
|
||||
//
|
||||
// Returns:
|
||||
// panic::Status
|
||||
// panic::Status::Ok Matrix multiplication completed successfully.
|
||||
// panic::Status::SizeError Matrix dimensions are incompatible.
|
||||
// panic::Status::NullError One or more internal data pointers are null.
|
||||
//
|
||||
// Notes:
|
||||
// This function does not allocate memory.
|
||||
// The output matrix must have dimensions
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
// or
|
||||
|
||||
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
// Function Name : panic::math::sqrt
|
||||
//
|
||||
// Description:
|
||||
// Computes an approximation of the square root of a scalar value.
|
||||
//
|
||||
// Inputs:
|
||||
// x panic::float_t
|
||||
// Input value.
|
||||
//
|
||||
// Outputs:
|
||||
// None.
|
||||
//
|
||||
// Returns:
|
||||
// panic::float_t
|
||||
// Approximate square root of x.
|
||||
//
|
||||
// Notes:
|
||||
// This implementation is written from scratch and is intended to be readable
|
||||
// and portable rather than maximally optimized.
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
+10
-1
@@ -24,7 +24,7 @@
|
||||
* Module Name: tensor
|
||||
* File Name: vector.cpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 21-06-2026
|
||||
* Date: 23-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
@@ -36,6 +36,7 @@
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <tensor/vector.hpp>
|
||||
#include <config/omp.hpp>
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// DEFINE DESCRIPTION
|
||||
@@ -48,6 +49,7 @@
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
static const panic::uint_t vector_omp_min_size = 10000;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
@@ -85,6 +87,7 @@ vector<T>::vector(panic::uint_t size){
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < size; ++i){
|
||||
data[i] = static_cast<T>(0);
|
||||
}
|
||||
@@ -108,6 +111,7 @@ vector<T>::vector(panic::uint_t size, T value){
|
||||
|
||||
data = new T[size];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < size; ++i){
|
||||
data[i] = value;
|
||||
}
|
||||
@@ -132,6 +136,7 @@ vector<T>::vector(const vector& other){
|
||||
|
||||
data = new T[length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = other.data[i];
|
||||
}
|
||||
@@ -171,6 +176,7 @@ vector<T>& vector<T>::operator=(const vector& other){
|
||||
if (other.length > 0){
|
||||
new_data = new T[other.length];
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < other.length; ++i){
|
||||
new_data[i] = other.data[i];
|
||||
}
|
||||
@@ -221,10 +227,12 @@ bool vector<T>::resize(panic::uint_t new_size){
|
||||
copy_size = new_size;
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(copy_size > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < copy_size; ++i){
|
||||
new_data[i] = data[i];
|
||||
}
|
||||
|
||||
PANIC_OMP_PARALLEL_FOR_IF(copy_size - new_size > vector_omp_min_size)
|
||||
for (panic::uint_t i = copy_size; i < new_size; ++i){
|
||||
new_data[i] = static_cast<T>(0);
|
||||
}
|
||||
@@ -246,6 +254,7 @@ bool vector<T>::resize(panic::uint_t new_size){
|
||||
//--------------------------------------------------------------------------------------------------------------------------
|
||||
template <typename T>
|
||||
bool vector<T>::fill(T value){
|
||||
PANIC_OMP_PARALLEL_FOR_IF(length > vector_omp_min_size)
|
||||
for (panic::uint_t i = 0; i < length; ++i){
|
||||
data[i] = value;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user