Dense Layer, Add

added the dense layer with a forward functions. I also made the add functions for most cases.
This commit is contained in:
2026-06-25 19:43:07 +02:00
parent 9b20278395
commit 189d605bfa
27 changed files with 1657 additions and 902 deletions
+11 -10
View File
@@ -22,7 +22,7 @@
*
* Project Name: PANIC
* Module Name: io
* File Name: print_tensor.hpp
* File Name: print_tensor.cpp
* Revision: 0.1.0
* Date: 21-06-2026
* Author: Michelle Bausager
@@ -36,6 +36,7 @@
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <iostream> // for std::cout, std::endl
//#include <io/print_tensor.hpp>
#include <tensor/vector.hpp> // for panic::tensor::vector
#include <tensor/matrix.hpp>
@@ -64,7 +65,7 @@ namespace panic {
//--------------------------------------------------------------------------------------------------------------------------
void print_vector(const panic::tensor::uint_vector& v){
std::cout << "[";
for (panic::uint_t i = 0; i < v.size()-1; ++i){
for (panic::types::uint_t i = 0; i < v.size()-1; ++i){
std::cout << v[i] << ", ";
}
std::cout << v[v.size()-1]<< "]" << std::endl;
@@ -72,7 +73,7 @@ void print_vector(const panic::tensor::uint_vector& v){
}
void print_vector(const panic::tensor::int_vector& v){
std::cout << "[";
for (panic::uint_t i = 0; i < v.size()-1; ++i){
for (panic::types::uint_t i = 0; i < v.size()-1; ++i){
std::cout << v[i] << ", ";
}
std::cout << v[v.size()-1]<< "]" << std::endl;
@@ -80,7 +81,7 @@ void print_vector(const panic::tensor::int_vector& v){
}
void print_vector(const panic::tensor::real_vector& v){
std::cout << "[";
for (panic::uint_t i = 0; i < v.size()-1; ++i){
for (panic::types::uint_t i = 0; i < v.size()-1; ++i){
std::cout << v[i] << ", ";
}
std::cout << v[v.size()-1]<< "]" << std::endl;
@@ -98,9 +99,9 @@ void print_vector(const panic::tensor::real_vector& v){
//--------------------------------------------------------------------------------------------------------------------------
void print_matrix(const panic::tensor::uint_matrix& A){
std::cout << "[";
for (panic::uint_t i = 0; i < A.rows(); ++i){
for (panic::types::uint_t i = 0; i < A.rows(); ++i){
std::cout << "[";
for (panic::uint_t j = 0; j < A.cols(); ++j){
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
std::cout << A(i,j) << ", ";
}
if (i < A.rows()-1){
@@ -113,9 +114,9 @@ void print_matrix(const panic::tensor::uint_matrix& A){
void print_matrix(const panic::tensor::int_matrix& A){
std::cout << "[";
for (panic::uint_t i = 0; i < A.rows(); ++i){
for (panic::types::uint_t i = 0; i < A.rows(); ++i){
std::cout << "[";
for (panic::uint_t j = 0; j < A.cols(); ++j){
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
std::cout << A(i,j) << ", ";
}
if (i < A.rows()-1){
@@ -128,9 +129,9 @@ void print_matrix(const panic::tensor::int_matrix& A){
void print_matrix(const panic::tensor::real_matrix& A){
std::cout << "[";
for (panic::uint_t i = 0; i < A.rows(); ++i){
for (panic::types::uint_t i = 0; i < A.rows(); ++i){
std::cout << "[";
for (panic::uint_t j = 0; j < A.cols(); ++j){
for (panic::types::uint_t j = 0; j < A.cols(); ++j){
std::cout << A(i,j) << ", ";
}
if (i < A.rows()-1){
+563
View File
@@ -0,0 +1,563 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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/matrix.hpp> // for panic::tensor::real_matrix
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
static const panic::types::uint_t add_omp_min_work = 10000;
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace math {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
// Description:
// Adds a constant to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool add(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() > add_omp_min_work)
for (panic::types::uint_t i = 0; i < a.size(); ++i){
c[i] = a[i] + k;
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template bool add<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 add<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 add<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::add
//
// Description:
// Adds a constant to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a, const T k){
panic::tensor::vector<T> c(a.size());
if (!add(a, k, c)){
return panic::tensor::vector<T>();
}
return c;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template panic::tensor::vector<panic::types::uint_t>
add(const panic::tensor::vector<panic::types::uint_t>& a,
const panic::types::uint_t k
);
template panic::tensor::vector<panic::types::int_t>
add(const panic::tensor::vector<panic::types::int_t>& a,
const panic::types::int_t k
);
template panic::tensor::vector<panic::types::real_t>
add(const panic::tensor::vector<panic::types::real_t>& a,
const panic::types::real_t k
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
// Description:
// Adds a vector to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool add(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() > add_omp_min_work)
for (panic::types::uint_t i = 0; i < a.size(); ++i){
c[i] = a[i] + b[i];
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template bool add(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 add(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 add(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::add
//
// Description:
// Adds a vector to a vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::vector<T> add(const panic::tensor::vector<T>& a, const panic::tensor::vector<T>& b){
panic::tensor::vector<T> c(a.size());
if (!add(a, b, c)){
return panic::tensor::vector<T>();
}
return c;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template panic::tensor::vector<panic::types::uint_t>
add(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>
add(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>
add(const panic::tensor::vector<panic::types::real_t>& a,
const panic::tensor::vector<panic::types::real_t>& b
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
// Description:
// Adds a constant to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool add(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 > add_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) = A(i,j) + k;
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template bool add(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 add(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 add(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::add
//
// Description:
// Adds a constant to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const T k){
panic::tensor::matrix<T> C;
if (!add(A, k, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
add(const panic::tensor::matrix<panic::types::uint_t>& A,
const panic::types::uint_t k
);
template panic::tensor::matrix<panic::types::int_t>
add(const panic::tensor::matrix<panic::types::int_t>& A,
const panic::types::int_t k
);
template panic::tensor::matrix<panic::types::real_t>
add(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::types::real_t k
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add
//
// Description:
// Adds a matrix to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool add(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 > add_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) = A(i,j) + B(i,j);
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template bool add(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 add(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 add(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::add
//
// Description:
// Adds a matrix to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> add(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
panic::tensor::matrix<T> C;
if (!add(A, B, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
add(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>
add(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>
add(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::matrix<panic::types::real_t>& B
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add_rowwise
//
// Description:
// Adds a vector row-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool add_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 > add_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) = A(i,j) + b[j];
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template bool add_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 add_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 add_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::add_rowwise
//
// Description:
// Adds a vector row-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> add_rowwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
panic::tensor::matrix<T> C;
if (!add_rowwise(A, b, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
add_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>
add_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>
add_rowwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b
);
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::add_colwise
//
// Description:
// Adds a vector coloumn-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool add_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 > add_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
const T temp = b[i];
for (panic::types::uint_t j = 0; j < cols; ++j){
C(i,j) = A(i,j) + temp;
}
}
return true;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template bool add_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 add_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 add_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::add_colwise
//
// Description:
// Adds a vector coloumn-wise to a matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> add_colwise(const panic::tensor::matrix<T>& A, const panic::tensor::vector<T>& b){
panic::tensor::matrix<T> C;
if (!add_colwise(A, b, C)){
return panic::tensor::matrix<T>();
}
return C;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
template panic::tensor::matrix<panic::types::uint_t>
add_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>
add_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>
add_colwise(const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::vector<panic::types::real_t>& b
);
} // namespace math
} // namespace panic
+169
View File
@@ -0,0 +1,169 @@
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*
* 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: matmul.cpp
* Revision: 0.1.0
* Date: 24-06-2026
* Author: Michelle Bausager
*
* Description:
* Functions to print out tensors with std::cout << x std::endl;
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//-----------------------------------------------------------------------------------------------------
#include <math/matmul.hpp>
#include <tensor/matrix.hpp> // for panic::tensor::real_matrix
#include <config/omp.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
static const panic::types::uint_t matmul_omp_min_work = 10000;
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
namespace panic {
namespace math {
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::matmul
//
// Description:
// Multiply two matrices into C.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C){
const panic::types::uint_t rows = A.rows();
const panic::types::uint_t cols = B.cols();
const panic::types::uint_t inner = A.cols();
// Dimention mismacth
if( inner != B.rows()){
C.resize(0,0);
return false;
}
// In-place case: matmul(A, B, A) or matmul(A, B, B)
// This is not allowed since operation is unsafe.
// Use A = matmul(A, B) or B = matmul(A, B) instead.
if (&C == &A || &C == &B){
return false;
}
if (!C.resize(rows, cols)){
return false;
}
const panic::types::uint_t work = rows*cols*inner;
PANIC_OMP_PARALLEL_FOR_IF(work > matmul_omp_min_work)
for (panic::types::uint_t i = 0; i < rows; ++i){
for (panic::types::uint_t j = 0; j < cols; ++j){
T sum = static_cast<T>(0);
for (panic::types::uint_t k = 0; k < inner; ++k){
sum += A(i,k) * B(k,j);
}
C(i,j) = sum;
}
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::math::matmul
//
// Description:
// Multiply two matrices and returns the result.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::tensor::matrix<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B){
panic::tensor::matrix<T> C;
if (!matmul(A,B,C)){
return panic::tensor::matrix<T>();
}
return C;
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE DECLARATION
//---------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------------------------------------------------------------------------------------
// Function : bool matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B, panic::tensor::matrix<T>& C)
template bool matmul<panic::types::uint_t>(
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 matmul<panic::types::int_t>(
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 matmul<panic::types::real_t>(
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 : panic::tensor::matrix<T> matmul(const panic::tensor::matrix<T>& A, const panic::tensor::matrix<T>& B)
template panic::tensor::matrix<panic::types::uint_t> matmul<panic::types::uint_t>(
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> matmul<panic::types::int_t>(
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> matmul<panic::types::real_t>(
const panic::tensor::matrix<panic::types::real_t>& A,
const panic::tensor::matrix<panic::types::real_t>& B
);
} // namespace math
} // namespace panic
-329
View File
@@ -1,329 +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: neural_network
* File Name: layer_dense.cpp
* Revision: 0.1.0
* Date: 23-06-2026
* Author: Michelle Bausager
*
* Description:
* Defines the dense layers used in neural network
*
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <tensor/vector.hpp>
#include <config/omp.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// TYPE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
static const panic::uint_t vector_omp_min_size = 10000;
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
namespace panic{
namespace tensor{
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
//
// Description:
// Creates an empty vector.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector() {
length = 0;
data = 0;
}
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
//
// Description:
// Creates a vector with size allocation and initializes all values to zero.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(panic::uint_t size){
length = size;
if (length == 0){
data = 0;
return;
}
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);
}
}
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
//
// Description:
// Creates a vector with size allocation and initializes all values.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(panic::uint_t size, T value){
length = size;
if (size == 0){
data = 0;
return;
}
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;
}
}
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
//
// Description:
// Copy-contructor, makes a deep copy of another vector like this:
// vector a(3);
// vector b = a;
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(const vector& other){
length = other.length;
if (length == 0){
data = 0;
return;
}
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];
}
}
//--------------------------------------------------------------------------------------------------------------------------
// Deconstructor Name : panic::tensor::vector::~vector
//
// Description:
// Deletes the data and releases the memory.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::~vector(){
delete[] data;
data = 0;
length = 0;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::operator=
//
// Description:
// Copy-assignment. Copies from another vector like this:
// vector a(5);
// vector b(3);
// b = a;
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>& vector<T>::operator=(const vector& 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 > vector_omp_min_size)
for (panic::uint_t i = 0; i < other.length; ++i){
new_data[i] = other.data[i];
}
}
delete[] data;
data = new_data;
length = other.length;
return *this;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::resize
//
// Description:
// Returns the length/size of the vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::uint_t vector<T>::size() const{
return length;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::resize
//
// Description:
// Resizes the vector to new length and keeps old values
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool vector<T>::resize(panic::uint_t new_size){
if (new_size == length){
return true;
}
if (new_size == 0){
data = 0;
length = 0;
return true;
}
T* new_data = new T[new_size];
panic::uint_t copy_size = length;
if (new_size < length){
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);
}
delete[] data;
data = new_data;
length = new_size;
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::fill
//
// Description:
// Fills te vector with a value
//--------------------------------------------------------------------------------------------------------------------------
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;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::operator[]
//
// Description:
// Lets you read and write v[index]
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T& vector<T>::operator[](panic::uint_t index){
return data[index];
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::operator[]
//
// Description:
// Lets you read v[index] from a const vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
const T& vector<T>::operator[](panic::uint_t index) const{
return data[index];
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::at
//
// Description:
// Lets you read and write v.at(index) with index bounse
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T& vector<T>::at(panic::uint_t index){
if (index >= length){
return data[length-1];
}
return data[index];
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::at
//
// Description:
// Lets you read v[index] from a const vector with index bounse
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
const T& vector<T>::at(panic::uint_t index) const{
if (index >= length){
return data[length-1];
}
return data[index];
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//---------------------------------------------------------------------------------------------------------------------------
template struct vector<panic::real_t>;
template struct vector<panic::int_t>;
template struct vector<panic::uint_t>;
} // namespace tensor
} // namespace panic
+46 -230
View File
@@ -35,8 +35,10 @@
//---------------------------------------------------------------------------------------------------------------------------
// INCLUDE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
#include <tensor/vector.hpp>
#include <neural_network/layer/layer_dense.hpp>
#include <config/omp.hpp>
#include <math/matmul.hpp>
#include <math/add.hpp>
//---------------------------------------------------------------------------------------------------------------------------
// DEFINE DESCRIPTION
@@ -49,97 +51,37 @@
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
static const panic::uint_t vector_omp_min_size = 10000;
static const panic::types::uint_t layer_dense_omp_min_size = 10000;
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
//---------------------------------------------------------------------------------------------------------------------------
namespace panic{
namespace tensor{
namespace neural_network{
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
// Constructor Name : panic::neural_network::layer_dense
//
// Description:
// Creates an empty vector.
// Creates an empty layer.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector() {
length = 0;
data = 0;
layer_dense::layer_dense() {
weights.resize(0,0);
biases.resize(0);
outputs.resize(0,0);
}
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
// Constructor Name : panic::neural_network::layer_dense
//
// Description:
// Creates a vector with size allocation and initializes all values to zero.
// Creates an empty layer.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(panic::uint_t size){
length = size;
if (length == 0){
data = 0;
return;
}
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);
}
}
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
//
// Description:
// Creates a vector with size allocation and initializes all values.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(panic::uint_t size, T value){
length = size;
if (size == 0){
data = 0;
return;
}
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;
}
}
//--------------------------------------------------------------------------------------------------------------------------
// Constructor Name : panic::tensor::vector::vector
//
// Description:
// Copy-contructor, makes a deep copy of another vector like this:
// vector a(3);
// vector b = a;
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(const vector& other){
length = other.length;
if (length == 0){
data = 0;
return;
}
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];
}
layer_dense::layer_dense(panic::types::uint_t input_size, panic::types::uint_t neurons) {
weights.resize(input_size, neurons);
biases.resize(neurons);
outputs.resize(0,0);
}
//--------------------------------------------------------------------------------------------------------------------------
@@ -148,182 +90,56 @@ vector<T>::vector(const vector& other){
// Description:
// Deletes the data and releases the memory.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::~vector(){
delete[] data;
data = 0;
length = 0;
}
//template <typename T>
//vector<T>::~vector(){
// delete[] data;
//
// data = 0;
// length = 0;
//}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::operator=
// Function Name : panic::neural_network::layer_dense.forward
//
// Description:
// Copy-assignment. Copies from another vector like this:
// vector a(5);
// vector b(3);
// b = a;
// Calculated the forward pass:
// outputs = inputs * weights + biases
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>& vector<T>::operator=(const vector& other){
if (this == &other){
return *this;
bool layer_dense::forward(const panic::tensor::real_matrix& inputs){
if (inputs.cols() != weights.rows()){
return false;
}
T* new_data = 0;
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];
}
if (!outputs.resize(inputs.rows(), weights.cols())){
return false;
}
delete[] data;
data = new_data;
length = other.length;
return *this;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::resize
//
// Description:
// Returns the length/size of the vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::uint_t vector<T>::size() const{
return length;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::resize
//
// Description:
// Resizes the vector to new length and keeps old values
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool vector<T>::resize(panic::uint_t new_size){
if (new_size == length){
return true;
if (!panic::math::matmul(inputs, weights, outputs)){
return false;
}
if (new_size == 0){
data = 0;
length = 0;
return true;
}
T* new_data = new T[new_size];
panic::uint_t copy_size = length;
if (new_size < length){
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);
}
delete[] data;
data = new_data;
length = new_size;
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::fill
//
// Description:
// Fills te vector with a value
//--------------------------------------------------------------------------------------------------------------------------
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;
if (!panic::math::add_rowwise(outputs, biases, outputs)){
return false;
}
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::operator[]
// Function Name : panic::neural_network::layer_dense.backward
//
// Description:
// Lets you read and write v[index]
// Calculated the backward pass:
// ??
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T& vector<T>::operator[](panic::uint_t index){
return data[index];
bool layer_dense::backward(const panic::tensor::real_matrix& dinputs){
return true;
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::operator[]
//
// Description:
// Lets you read v[index] from a const vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
const T& vector<T>::operator[](panic::uint_t index) const{
return data[index];
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::at
//
// Description:
// Lets you read and write v.at(index) with index bounse
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T& vector<T>::at(panic::uint_t index){
if (index >= length){
return data[length-1];
}
return data[index];
}
//--------------------------------------------------------------------------------------------------------------------------
// Function Name : panic::tensor::vector::at
//
// Description:
// Lets you read v[index] from a const vector with index bounse
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
const T& vector<T>::at(panic::uint_t index) const{
if (index >= length){
return data[length-1];
}
return data[index];
}
//---------------------------------------------------------------------------------------------------------------------------
// EXPLICIT TEMPLATE INSTANTIATION
//---------------------------------------------------------------------------------------------------------------------------
template struct vector<panic::real_t>;
template struct vector<panic::int_t>;
template struct vector<panic::uint_t>;
} // namespace tensor
} // namespace panic
} // namespace panic
+21 -21
View File
@@ -49,7 +49,7 @@
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
static const panic::uint_t matrix_omp_min_size = 10000;
static const panic::types::uint_t matrix_omp_min_size = 10000;
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
@@ -79,7 +79,7 @@ matrix<T>::matrix() {
// 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){
matrix<T>::matrix(panic::types::uint_t rows, panic::types::uint_t cols){
n = rows;
m = cols;
length = n*m;
@@ -92,7 +92,7 @@ matrix<T>::matrix(panic::uint_t rows, panic::uint_t cols){
data = new T[length];
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
for (panic::uint_t i = 0; i < length; ++i){
for (panic::types::uint_t i = 0; i < length; ++i){
data[i] = static_cast<T>(0);
}
@@ -105,7 +105,7 @@ matrix<T>::matrix(panic::uint_t rows, panic::uint_t cols){
// 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){
matrix<T>::matrix(panic::types::uint_t rows, panic::types::uint_t cols, T value){
n = rows;
m = cols;
length = n*m;
@@ -118,7 +118,7 @@ matrix<T>::matrix(panic::uint_t rows, panic::uint_t cols, T value){
data = new T[length];
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
for (panic::uint_t i = 0; i < length; ++i){
for (panic::types::uint_t i = 0; i < length; ++i){
data[i] = value;
}
}
@@ -145,7 +145,7 @@ matrix<T>::matrix(const matrix& other){
data = new T[length];
PANIC_OMP_PARALLEL_FOR_IF(length > matrix_omp_min_size)
for (panic::uint_t i = 0; i < length; ++i){
for (panic::types::uint_t i = 0; i < length; ++i){
data[i] = other.data[i];
}
}
@@ -186,8 +186,8 @@ matrix<T>& matrix<T>::operator=(const matrix& other){
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){
PANIC_OMP_PARALLEL_FOR_IF(other.length > matrix_omp_min_size)
for (panic::types::uint_t i = 0; i < other.length; ++i){
new_data[i] = other.data[i];
}
}
@@ -209,7 +209,7 @@ matrix<T>& matrix<T>::operator=(const matrix& other){
// Returns the length/size of the matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::uint_t matrix<T>::rows() const{
panic::types::uint_t matrix<T>::rows() const{
return n;
}
@@ -220,7 +220,7 @@ panic::uint_t matrix<T>::rows() const{
// Returns the length/size of the matrix
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::uint_t matrix<T>::cols() const{
panic::types::uint_t matrix<T>::cols() const{
return m;
}
@@ -231,7 +231,7 @@ panic::uint_t matrix<T>::cols() const{
// 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){
bool matrix<T>::resize(panic::types::uint_t new_n, panic::types::uint_t new_m){
if ((new_n == n) && (new_m == m)){
return true;
@@ -247,22 +247,22 @@ bool matrix<T>::resize(panic::uint_t new_n, panic::uint_t new_m){
return true;
}
panic::uint_t new_length = new_n*new_m;
panic::types::uint_t new_length = new_n*new_m;
T* new_data = new T[new_length];
panic::uint_t copy_size = length;
panic::types::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){
for (panic::types::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){
for (panic::types::uint_t i = copy_size; i < new_length; ++i){
new_data[i] = static_cast<T>(0);
}
@@ -286,7 +286,7 @@ bool matrix<T>::resize(panic::uint_t new_n, panic::uint_t new_m){
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){
for (panic::types::uint_t i = 0; i < length; ++i){
data[i] = value;
}
@@ -300,7 +300,7 @@ bool matrix<T>::fill(T value){
// 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){
T& matrix<T>::operator()(panic::types::uint_t index_n, panic::types::uint_t index_m){
return data[index_n*m + index_m];
}
@@ -312,7 +312,7 @@ T& matrix<T>::operator()(panic::uint_t index_n, panic::uint_t index_m){
// 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{
const T& matrix<T>::operator()(panic::types::uint_t index_n, panic::types::uint_t index_m) const{
return data[index_n*m + index_m];
}
@@ -321,9 +321,9 @@ const T& matrix<T>::operator()(panic::uint_t index_n, panic::uint_t index_m) con
// EXPLICIT TEMPLATE INSTANTIATION
//---------------------------------------------------------------------------------------------------------------------------
template struct matrix<panic::real_t>;
template struct matrix<panic::int_t>;
template struct matrix<panic::uint_t>;
template struct matrix<panic::types::real_t>;
template struct matrix<panic::types::int_t>;
template struct matrix<panic::types::uint_t>;
} // namespace tensor
+20 -20
View File
@@ -49,7 +49,7 @@
//---------------------------------------------------------------------------------------------------------------------------
// VARIABLE DESCRIPTION
//---------------------------------------------------------------------------------------------------------------------------
static const panic::uint_t vector_omp_min_size = 10000;
static const panic::types::uint_t vector_omp_min_size = 10000;
//---------------------------------------------------------------------------------------------------------------------------
// FUNCTION PROTOTYPE
@@ -77,7 +77,7 @@ vector<T>::vector() {
// Creates a vector with size allocation and initializes all values to zero.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(panic::uint_t size){
vector<T>::vector(panic::types::uint_t size){
length = size;
if (length == 0){
@@ -88,7 +88,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){
for (panic::types::uint_t i = 0; i < size; ++i){
data[i] = static_cast<T>(0);
}
@@ -101,7 +101,7 @@ vector<T>::vector(panic::uint_t size){
// Creates a vector with size allocation and initializes all values.
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
vector<T>::vector(panic::uint_t size, T value){
vector<T>::vector(panic::types::uint_t size, T value){
length = size;
if (size == 0){
@@ -112,7 +112,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){
for (panic::types::uint_t i = 0; i < size; ++i){
data[i] = value;
}
}
@@ -137,7 +137,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){
for (panic::types::uint_t i = 0; i < length; ++i){
data[i] = other.data[i];
}
}
@@ -177,7 +177,7 @@ vector<T>& vector<T>::operator=(const vector& other){
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){
for (panic::types::uint_t i = 0; i < other.length; ++i){
new_data[i] = other.data[i];
}
}
@@ -197,7 +197,7 @@ vector<T>& vector<T>::operator=(const vector& other){
// Returns the length/size of the vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
panic::uint_t vector<T>::size() const{
panic::types::uint_t vector<T>::size() const{
return length;
}
@@ -208,7 +208,7 @@ panic::uint_t vector<T>::size() const{
// Resizes the vector to new length and keeps old values
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
bool vector<T>::resize(panic::uint_t new_size){
bool vector<T>::resize(panic::types::uint_t new_size){
if (new_size == length){
return true;
}
@@ -221,19 +221,19 @@ bool vector<T>::resize(panic::uint_t new_size){
}
T* new_data = new T[new_size];
panic::uint_t copy_size = length;
panic::types::uint_t copy_size = length;
if (new_size < length){
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){
for (panic::types::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){
for (panic::types::uint_t i = copy_size; i < new_size; ++i){
new_data[i] = static_cast<T>(0);
}
@@ -255,7 +255,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){
for (panic::types::uint_t i = 0; i < length; ++i){
data[i] = value;
}
@@ -269,7 +269,7 @@ bool vector<T>::fill(T value){
// Lets you read and write v[index]
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T& vector<T>::operator[](panic::uint_t index){
T& vector<T>::operator[](panic::types::uint_t index){
return data[index];
}
@@ -280,7 +280,7 @@ T& vector<T>::operator[](panic::uint_t index){
// Lets you read v[index] from a const vector
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
const T& vector<T>::operator[](panic::uint_t index) const{
const T& vector<T>::operator[](panic::types::uint_t index) const{
return data[index];
}
@@ -291,7 +291,7 @@ const T& vector<T>::operator[](panic::uint_t index) const{
// Lets you read and write v.at(index) with index bounse
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
T& vector<T>::at(panic::uint_t index){
T& vector<T>::at(panic::types::uint_t index){
if (index >= length){
return data[length-1];
@@ -307,7 +307,7 @@ T& vector<T>::at(panic::uint_t index){
// Lets you read v[index] from a const vector with index bounse
//--------------------------------------------------------------------------------------------------------------------------
template <typename T>
const T& vector<T>::at(panic::uint_t index) const{
const T& vector<T>::at(panic::types::uint_t index) const{
if (index >= length){
return data[length-1];
}
@@ -320,9 +320,9 @@ const T& vector<T>::at(panic::uint_t index) const{
// EXPLICIT TEMPLATE INSTANTIATION
//---------------------------------------------------------------------------------------------------------------------------
template struct vector<panic::real_t>;
template struct vector<panic::int_t>;
template struct vector<panic::uint_t>;
template struct vector<panic::types::real_t>;
template struct vector<panic::types::int_t>;
template struct vector<panic::types::uint_t>;
} // namespace tensor