Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
ca_kernels_test.cc
Go to the documentation of this file.
1/*
2 Aleph_w
3
4 Data structures & Algorithms
5 version 2.0.0b
6 https://github.com/lrleon/Aleph-w
7
8 This file is part of Aleph-w library
9
10 Copyright (c) 2002-2026 Leandro Rabindranath Leon
11*/
12
25#include <array>
26#include <stdexcept>
27
28#include <gtest/gtest.h>
29
30#include <ca-kernels.H>
31#include <ca-traits.H>
32
33using namespace Aleph;
34using namespace Aleph::CA;
35
36namespace
37{
38
39constexpr double eps = 1e-12;
40
41} // namespace
42
44{
45 const auto k = laplacian_5p_kernel<double>();
46
47 EXPECT_DOUBLE_EQ(k.center(), -4.0);
48 EXPECT_DOUBLE_EQ(k(0, 1), 1.0);
49 EXPECT_DOUBLE_EQ(k(1, 0), 1.0);
50 EXPECT_DOUBLE_EQ(k(1, 2), 1.0);
51 EXPECT_DOUBLE_EQ(k(2, 1), 1.0);
52 EXPECT_DOUBLE_EQ(k(0, 0), 0.0);
53 EXPECT_DOUBLE_EQ(k.sum(), 0.0);
54
55 // Moore<2,1> order is row-major over the 3x3 stencil, skipping centre.
56 EXPECT_DOUBLE_EQ(k.neighbour_weight(0), 0.0); // (-1, -1)
57 EXPECT_DOUBLE_EQ(k.neighbour_weight(1), 1.0); // (-1, 0)
58 EXPECT_DOUBLE_EQ(k.neighbour_weight(3), 1.0); // ( 0, -1)
59 EXPECT_DOUBLE_EQ(k.neighbour_weight(4), 1.0); // ( 0, 1)
60 EXPECT_DOUBLE_EQ(k.neighbour_weight(6), 1.0); // ( 1, 0)
61}
62
64{
65 const auto lap9 = laplacian_9p_kernel<double>();
66 const auto mean3 = mean_3x3_kernel<double>();
67 const auto mean5 = mean_5x5_kernel<double>();
70
71 EXPECT_NEAR(lap9.sum(), 0.0, eps);
72 EXPECT_NEAR(mean3.sum(), 1.0, eps);
73 EXPECT_NEAR(mean5.sum(), 1.0, eps);
74 EXPECT_NEAR(gauss3.sum(), 1.0, eps);
75 EXPECT_NEAR(gauss5.sum(), 1.0, eps);
76
77 EXPECT_DOUBLE_EQ(mean5.center(), 1.0 / 25.0);
78 EXPECT_DOUBLE_EQ(gauss3.center(), 4.0 / 16.0);
79 EXPECT_DOUBLE_EQ(gauss5.center(), 36.0 / 256.0);
80}
81
83{
84 const auto mean = mean_3x3_kernel<double>();
85 const std::array<double, 8> neighbours{1.0, 2.0, 3.0, 4.0,
86 6.0, 7.0, 8.0, 9.0};
87 const double conv = mean.apply(5.0, Neighbor_View<double>(neighbours.data(),
88 neighbours.size()));
89 EXPECT_NEAR(conv, 5.0, eps);
90
91 const auto lap = laplacian_5p_kernel<double>();
92 const std::array<double, 8> constant{2.0, 2.0, 2.0, 2.0,
93 2.0, 2.0, 2.0, 2.0};
95 constant.size())),
96 0.0, eps);
97}
98
100{
101 const auto sx = sobel_x_kernel<double>();
102 const auto sy = sobel_y_kernel<double>();
103
104 // Values are f(row, col) = col in a 3x3 window centred at (0,0).
105 const std::array<double, 8> x_neighbours{-1.0, 0.0, 1.0,
106 -1.0, 1.0,
107 -1.0, 0.0, 1.0};
109 x_neighbours.size())),
110 8.0, eps);
112 x_neighbours.size())),
113 0.0, eps);
114
115 // Values are f(row, col) = row.
116 const std::array<double, 8> y_neighbours{-1.0, -1.0, -1.0,
117 0.0, 0.0,
118 1.0, 1.0, 1.0};
120 y_neighbours.size())),
121 8.0, eps);
123 y_neighbours.size())),
124 0.0, eps);
125}
126
128{
129 const auto k = gaussian_3x3_kernel<double>();
130 auto m = k.to_matrix();
131
132 EXPECT_DOUBLE_EQ(m.get_entry(0, 0), 1.0 / 16.0);
133 EXPECT_DOUBLE_EQ(m.get_entry(1, 1), 4.0 / 16.0);
134 EXPECT_DOUBLE_EQ(m.get_entry(2, 2), 1.0 / 16.0);
135}
136
138{
139 EXPECT_THROW((Kernel2D<double, 3, 3>{1.0, 2.0}), std::length_error);
140 EXPECT_THROW((Kernel2D<double, 3, 3>{{{1.0, 2.0, 3.0},
141 {4.0, 5.0},
142 {6.0, 7.0, 8.0}}}),
143 std::length_error);
144
145 const auto k = laplacian_5p_kernel<double>();
146 EXPECT_THROW(k(3, 0), std::out_of_range);
147 EXPECT_THROW(k.weight_at_offset(-2, 0), std::out_of_range);
148 EXPECT_THROW(k.neighbour_weight(8), std::out_of_range);
149
150 const std::array<double, 2> too_short{1.0, 2.0};
152 too_short.size())),
153 std::length_error);
154}
Fixed 2-D convolution kernels for continuous cellular automata.
Common typedefs and tag types for the Cellular Automata module.
Dense odd-sized 2-D convolution kernel.
Definition ca-kernels.H:83
#define TEST(name)
size_t blossom_maximum_cardinality_matching(const GT &g, DynDlist< typename GT::Arc * > &matching, SA sa=SA())
Alias of compute_maximum_cardinality_general_matching().
Definition Blossom.H:466
std::span< const T > Neighbor_View
Read-only view over a contiguous range of neighbour values.
Definition ca-traits.H:90
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
auto mean(const Container &data) -> std::decay_t< decltype(*std::begin(data))>
Compute the arithmetic mean.
Definition stat_utils.H:190
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
static int * k