Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9b20278395 | ||
|
|
cfbb06537d |
@@ -0,0 +1,149 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 23-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the dense layers used in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// DEFINE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// TYPE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// Type Name : panic::neural_network::layer_dense
|
||||
//
|
||||
// Description:
|
||||
// A dense layer to use in neural networks
|
||||
//
|
||||
// Member Variables:
|
||||
// length panic::uint_t
|
||||
// Number of elements in the vector.
|
||||
//
|
||||
// data panic::real_t*
|
||||
// Pointer to the allocated vector data.
|
||||
//
|
||||
// Notes:
|
||||
// This vector uses dynamic allocation with new[] and delete[].
|
||||
// Copying performs a deep copy.
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
|
||||
template <typename T>
|
||||
struct vector{
|
||||
panic::uint_t length;
|
||||
T* data;
|
||||
|
||||
// empty contructor
|
||||
vector();
|
||||
|
||||
// contructor with size allocation
|
||||
vector(panic::uint_t size);
|
||||
|
||||
// contructor with size allocation and sets it all to a value
|
||||
vector(panic::uint_t size, T value);
|
||||
|
||||
// copy-contructor
|
||||
// vector a(3);
|
||||
// vector b = a;
|
||||
vector(const vector& other);
|
||||
|
||||
// de-contructor, releases memory
|
||||
~vector();
|
||||
|
||||
// copy-assignment
|
||||
//vector a(5);
|
||||
//vector b(3);
|
||||
//b = a;
|
||||
vector& operator=(const vector& other);
|
||||
|
||||
// function returns size.
|
||||
// const tells compiler that the fuction don't edit the objert.
|
||||
panic::uint_t size() const;
|
||||
|
||||
// function to resize data vector (I need a new that don't save the vlaues)
|
||||
bool resize(panic::uint_t new_size);
|
||||
|
||||
// fill data with value
|
||||
bool fill(T value);
|
||||
|
||||
// lets you read and write v[index]
|
||||
T& operator[](panic::uint_t index);
|
||||
|
||||
// lets you read v[index] from a const vector
|
||||
const T& operator[](panic::uint_t index) const;
|
||||
|
||||
// lets you read and write v.at(index)
|
||||
T& at(panic::uint_t index);
|
||||
|
||||
// lets you read v.at(index) from a const vector
|
||||
const T& at(panic::uint_t index) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// TYPE ALIASES
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
typedef vector<panic::real_t> real_vector;
|
||||
typedef vector<panic::int_t> int_vector;
|
||||
typedef vector<panic::uint_t> uint_vector;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE DECLARATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
extern template struct vector<panic::real_t>;
|
||||
extern template struct vector<panic::int_t>;
|
||||
extern template struct vector<panic::uint_t>;
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -0,0 +1,149 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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.hpp
|
||||
* Revision: 0.1.0
|
||||
* Date: 23-06-2026
|
||||
* Author: Michelle Bausager
|
||||
*
|
||||
* Description:
|
||||
* Defines the dense layers used in neural network
|
||||
*
|
||||
*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
|
||||
#pragma once
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// INCLUDE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
#include <config/types.hpp> // panic::uint_t, panic::int_t, and panic::real_t
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// DEFINE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// TYPE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// Type Name : panic::neural_network::layer_dense
|
||||
//
|
||||
// Description:
|
||||
// A dense layer to use in neural networks
|
||||
//
|
||||
// Member Variables:
|
||||
// length panic::uint_t
|
||||
// Number of elements in the vector.
|
||||
//
|
||||
// data panic::real_t*
|
||||
// Pointer to the allocated vector data.
|
||||
//
|
||||
// Notes:
|
||||
// This vector uses dynamic allocation with new[] and delete[].
|
||||
// Copying performs a deep copy.
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
namespace panic{
|
||||
namespace tensor{
|
||||
|
||||
template <typename T>
|
||||
struct vector{
|
||||
panic::uint_t length;
|
||||
T* data;
|
||||
|
||||
// empty contructor
|
||||
vector();
|
||||
|
||||
// contructor with size allocation
|
||||
vector(panic::uint_t size);
|
||||
|
||||
// contructor with size allocation and sets it all to a value
|
||||
vector(panic::uint_t size, T value);
|
||||
|
||||
// copy-contructor
|
||||
// vector a(3);
|
||||
// vector b = a;
|
||||
vector(const vector& other);
|
||||
|
||||
// de-contructor, releases memory
|
||||
~vector();
|
||||
|
||||
// copy-assignment
|
||||
//vector a(5);
|
||||
//vector b(3);
|
||||
//b = a;
|
||||
vector& operator=(const vector& other);
|
||||
|
||||
// function returns size.
|
||||
// const tells compiler that the fuction don't edit the objert.
|
||||
panic::uint_t size() const;
|
||||
|
||||
// function to resize data vector (I need a new that don't save the vlaues)
|
||||
bool resize(panic::uint_t new_size);
|
||||
|
||||
// fill data with value
|
||||
bool fill(T value);
|
||||
|
||||
// lets you read and write v[index]
|
||||
T& operator[](panic::uint_t index);
|
||||
|
||||
// lets you read v[index] from a const vector
|
||||
const T& operator[](panic::uint_t index) const;
|
||||
|
||||
// lets you read and write v.at(index)
|
||||
T& at(panic::uint_t index);
|
||||
|
||||
// lets you read v.at(index) from a const vector
|
||||
const T& at(panic::uint_t index) const;
|
||||
|
||||
};
|
||||
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// TYPE ALIASES
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
typedef vector<panic::real_t> real_vector;
|
||||
typedef vector<panic::int_t> int_vector;
|
||||
typedef vector<panic::uint_t> uint_vector;
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// EXPLICIT TEMPLATE DECLARATION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
extern template struct vector<panic::real_t>;
|
||||
extern template struct vector<panic::int_t>;
|
||||
extern template struct vector<panic::uint_t>;
|
||||
|
||||
|
||||
} // namespace tensor
|
||||
} // namespace panic
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// VARIABLE DESCRIPTION
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -54,6 +54,13 @@
|
||||
// Variable difinition like:
|
||||
panic::real_t x = 2.4;
|
||||
|
||||
panic::tensor::real_vector a(3);
|
||||
panic::tensor::uint_vector b(3);
|
||||
panic::tensor::int_vector c(10);
|
||||
|
||||
panic::tensor::real_matrix A(2,2, 1);
|
||||
panic::tensor::uint_matrix B(2,2, 2);
|
||||
panic::tensor::int_matrix C(2,2, 3);
|
||||
|
||||
//---------------------------------------------------------------------------------------------------------------------------
|
||||
// FUNCTION PROTOTYPE
|
||||
@@ -63,9 +70,7 @@ panic::real_t x = 2.4;
|
||||
|
||||
int main(void) {
|
||||
|
||||
panic::tensor::real_vector a(3);
|
||||
panic::tensor::uint_vector b(3);
|
||||
panic::tensor::int_vector c(10);
|
||||
|
||||
a[2] = 1.2;
|
||||
b[2] = 3;
|
||||
c[2] = -3;
|
||||
@@ -77,9 +82,6 @@ int main(void) {
|
||||
std::cout << panic::constants::pi << std::endl;
|
||||
std::cout << 1.23249238423847 << std::endl;
|
||||
|
||||
panic::tensor::real_matrix A(2,2, 1);
|
||||
panic::tensor::uint_matrix B(2,2, 2);
|
||||
panic::tensor::int_matrix C(2,2, 3);
|
||||
A(1,0) = 1.2;
|
||||
B(1,0) = 3;
|
||||
C(1,0) = -2;
|
||||
@@ -89,5 +91,7 @@ int main(void) {
|
||||
panic::io::print_matrix(C);
|
||||
|
||||
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,329 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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
|
||||
@@ -0,0 +1,329 @@
|
||||
/**++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
||||
*
|
||||
* 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
|
||||
Reference in New Issue
Block a user