Dynamic Neural Field Composer 0.0.0
A C++20 library and interactive application for building and simulating Dynamic Neural Field (DNF) architectures.
Loading...
Searching...
No Matches
collapse.h
Go to the documentation of this file.
1#pragma once
2
3#include <map>
4#include <sstream>
5
6#include "tools/math.h"
7#include "element.h"
8
9//https://github.com/stevenlovegrove/Pangolin/issues/352
10#ifdef max
11#undef max
12#endif
13
14#ifdef min
15#undef min
16#endif
17
19{
22 enum class CompressionType : int
23 {
24 SUM,
25 AVERAGE,
26 MAXIMUM,
27 MINIMUM
28 };
29
31 inline const std::map<CompressionType, std::string> CompressionTypeToString = {
32 {CompressionType::SUM, "sum"},
33 {CompressionType::AVERAGE, "average"},
34 {CompressionType::MAXIMUM, "maximum"},
35 {CompressionType::MINIMUM, "minimum"}
36 };
37
40 enum class ProjectionAxis : int
41 {
42 X,
43 Y
44 };
45
47 inline const std::map<ProjectionAxis, std::string> ProjectionAxisToString = {
48 {ProjectionAxis::X, "x"},
50 };
51
64
68 {
72
82
83 bool operator==(const CollapseParameters& other) const
84 {
85 return compression == other.compression &&
86 keepAxis == other.keepAxis &&
88 }
89
90 [[nodiscard]] std::string toString() const override
91 {
92 std::ostringstream result;
93 result << "Parameters: ["
94 << "Compression: " << CompressionTypeToString.at(compression) << ", "
95 << "Keep axis: " << ProjectionAxisToString.at(keepAxis) << ", "
96 << "Input field dimensions: " << inputDimensions.toString()
97 << "]";
98 return result.str();
99 }
100 };
101
113 class Collapse final : public Element
114 {
115 private:
116 CollapseParameters parameters;
117 std::vector<double> scratch;
118 public:
122 Collapse(const ElementCommonParameters& elementCommonParameters,
123 const CollapseParameters& parameters);
124
125 void init() override;
126 void step(double t, double deltaT) override;
127 void addInput(const std::shared_ptr<Element>& inputElement,
128 const std::string& inputComponent = "output") override;
129 std::string toString() const override;
130 std::shared_ptr<Element> clone() const override;
131
133 void changeInputDimensions(const ElementDimensions& newInputDimensions);
134
135 void setParameters(const CollapseParameters& parameters);
136 [[nodiscard]] CollapseParameters getParameters() const;
137 };
138}
Collapses a 2D input field to a 1D output by reducing along one axis.
Definition collapse.h:114
void addInput(const std::shared_ptr< Element > &inputElement, const std::string &inputComponent="output") override
Register inputElement as an upstream source for this element.
Definition collapse.cpp:60
void step(double t, double deltaT) override
Advance the element by one time step.
Definition collapse.cpp:38
void setParameters(const CollapseParameters &parameters)
Definition collapse.cpp:139
std::string toString() const override
Definition collapse.cpp:113
CollapseParameters getParameters() const
Definition collapse.cpp:145
void init() override
Initialize the element (called once before the simulation loop).
Definition collapse.cpp:16
void changeInputDimensions(const ElementDimensions &newInputDimensions)
Resize the 2D input field dimensions and rebuild the input buffer.
Definition collapse.cpp:126
std::shared_ptr< Element > clone() const override
Definition collapse.cpp:121
Abstract base class for all simulation elements.
Definition element.h:28
ProjectionAxis
Spatial axis a projection acts on.
Definition collapse.h:41
CompressionType
Reduction applied when collapsing one axis of a 2D field to 1D.
Definition collapse.h:23
@ MAXIMUM
Maximum along the collapsed axis.
@ AVERAGE
Mean along the collapsed axis.
@ SUM
Sum along the collapsed axis.
@ MINIMUM
Minimum along the collapsed axis.
Definition element_parameters.h:10
tools::math::ReduceOp toReduceOp(const CompressionType type)
Maps a CompressionType to the corresponding math reduction op.
Definition collapse.h:53
const std::map< CompressionType, std::string > CompressionTypeToString
Maps CompressionType values to human-readable strings.
Definition collapse.h:31
const std::map< ProjectionAxis, std::string > ProjectionAxisToString
Maps ProjectionAxis values to human-readable strings.
Definition collapse.h:47
ReduceOp
Definition math.h:723
Parameters for a Collapse (2D -> 1D) element.
Definition collapse.h:68
CollapseParameters(const CompressionType compression=CompressionType::SUM, const ProjectionAxis keepAxis=ProjectionAxis::X, const ElementDimensions &inputDimensions=ElementDimensions{ 100, 100, 1.0, 1.0 })
Construct Collapse parameters.
Definition collapse.h:77
CompressionType compression
Reduction applied along the collapsed axis.
Definition collapse.h:69
ElementDimensions inputDimensions
Spatial dimensions of the 2D source field.
Definition collapse.h:71
bool operator==(const CollapseParameters &other) const
Definition collapse.h:83
ProjectionAxis keepAxis
Axis kept in the 1D output (the other is collapsed).
Definition collapse.h:70
std::string toString() const override
Definition collapse.h:90
Definition element_parameters.h:188
Definition element_parameters.h:159
std::string toString() const
Definition element_parameters.cpp:55
Definition element_parameters.h:206