id
int64
0
877k
file_name
stringlengths
3
109
file_path
stringlengths
13
185
content
stringlengths
31
9.38M
size
int64
31
9.38M
language
stringclasses
1 value
extension
stringclasses
11 values
total_lines
int64
1
340k
avg_line_length
float64
2.18
149k
max_line_length
int64
7
2.22M
alphanum_fraction
float64
0
1
repo_name
stringlengths
6
66
repo_stars
int64
94
47.3k
repo_forks
int64
0
12k
repo_open_issues
int64
0
3.4k
repo_license
stringclasses
11 values
repo_extraction_date
stringclasses
197 values
exact_duplicates_redpajama
bool
2 classes
near_duplicates_redpajama
bool
2 classes
exact_duplicates_githubcode
bool
2 classes
exact_duplicates_stackv2
bool
1 class
exact_duplicates_stackv1
bool
2 classes
near_duplicates_githubcode
bool
2 classes
near_duplicates_stackv1
bool
2 classes
near_duplicates_stackv2
bool
1 class
1,531,556
init.cpp
metric-space-ai_metric/python/src/transform/init.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <pybind11/pybind11.h> namespace py = pybind11; void export_metric_wavelet(py::module& m); PYBIND11_MODULE(transform, m) { export_metric_wavelet(m); }
404
C++
.cpp
12
31.333333
69
0.739691
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,557
wavelet.cpp
metric-space-ai_metric/python/src/transform/wavelet.cpp
#include "metric/transform/wavelet.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> namespace py = pybind11; void export_metric_wavelet(py::module& m) { using T = double; using Container = std::vector<T>; m.def("dwt", &wavelet::dwt<Container>, py::arg("x"), py::arg("wavelet_type") ); m.def("idwt", &wavelet::idwt<Container>); m.def("wmaxlev", &wavelet::wmaxlev); }
468
C++
.cpp
16
25.5625
45
0.66147
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,558
init.cpp
metric-space-ai_metric/python/src/utils/init.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <pybind11/pybind11.h> namespace py = pybind11; void export_metric_sparsify(py::module& m); void export_metric_dimension(py::module& m); void export_metric_datasets(py::module& m); void export_metric_graphs(py::module& m); void export_metric_distribution(py::module& m); PYBIND11_MODULE(utils, m) { export_metric_sparsify(m); export_metric_dimension(m); export_metric_datasets(m); export_metric_graphs(m); export_metric_distribution(m); }
708
C++
.cpp
20
32.8
69
0.741228
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,559
distribution.cpp
metric-space-ai_metric/python/src/utils/distribution.cpp
#include <pybind11/pybind11.h> #include <random> namespace py = pybind11; template <typename Value> void wrap_metric_distribution(py::module& m) { auto module = m.def_submodule("distribution"); py::class_<std::uniform_real_distribution<Value>>(module, "Uniform", "Uniform distribution") .def(py::init<Value, Value>(), py::arg("a"), py::arg("b") = 1.0 ); py::class_<std::normal_distribution<Value>>(module, "Normal", "Gaussian distribution") .def(py::init<Value, Value>(), py::arg("mean"), py::arg("stddev") = 1.0 ); } void export_metric_distribution(py::module& m) { wrap_metric_distribution<double>(m); }
706
C++
.cpp
20
29.3
96
0.618768
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,560
dimension.cpp
metric-space-ai_metric/python/src/utils/dimension.cpp
#include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <string> #include <vector> namespace py = pybind11; template <typename V, typename T> void register_wrapper_dimension(py::module& m) { using Dimension = cross::dimension<V, T>; using Callback = py::object; auto dim = py::class_<Dimension>(m, "Dimension"); dim.def("dispose", &Dimension::dispose); dim.def("get_offset", &Dimension::get_offset); dim.def("get_bit_index", &Dimension::get_bit_index); dim.def("filter_range", &Dimension::filter_range); dim.def("filter_exact", &Dimension::filter_exact); dim.def("filter_all", &Dimension::filter_all); dim.def("filter_with_predicate", &Dimension::filter_with_predicate); dim.def("filter_function", &Dimension::filter_function); dim.def("bottom", &Dimension::bottom); dim.def("top", &Dimension::top); dim.def("feature_all_count", &Dimension::feature_all_count); dim.def("feature", +[](Dimension& self, Callback& key){ return self.feature(key); }); dim.def("feature", +[](Dimension& self, Callback& add_func, Callback& remove_func, Callback& init_func){ return self.feature(add_func, remove_func, init_func); }); dim.def("feature", +[](Dimension& self, Callback& add_func, Callback& remove_func, Callback& init_func, Callback& key_func){ return self.feature(add_func, remove_func, init_func, key_func); }); dim.def("feature_count", +[](Dimension& self){ return self.feature_count(); }); dim.def("feature_count", +[](Dimension& self, Callback& key_func){ return self.feature_count(key_func); }); dim.def("feature_sum", +[](Dimension& self, Callback& val_func){ return self.feature_sum(val_func); }); dim.def("feature_sum", +[](Dimension& self, Callback& val_func, Callback& key_func){ return self.feature_sum(val_func, key_func); }); dim.def("feature_all", +[](Dimension& self, Callback& add_func, Callback& remove_func, Callback& init_func){ return self.feature_all(add_func, remove_func, init_func); }); dim.def("feature_all_sum", +[](Dimension& self, Callback& val_func){ return self.feature_all_sum(val_func); }); } void export_metric_dimension(py::module& m) { // disable for now, as there is no example of usage and no way to test it // register_wrapper_dimension<double, py::object>(m); // register_wrapper_dimension<int, py::object>(m); // register_wrapper_dimension<std::string, py::object>(m); // register_wrapper_dimension<py::object, py::object>(m); }
2,616
C++
.cpp
57
41.298246
128
0.666406
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,561
graph.cpp
metric-space-ai_metric/python/src/utils/graph.cpp
#include "metric/utils/graph.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> namespace py = pybind11; // here we need another approach (dynamic class resolution) //template <typename WeightType = bool, bool isDense = false, bool isSymmetric = true> //metric::Graph<WeightType, isDense, isSymmetric> createGraph(bool isDense = false, bool isSymmetric = true) //{ // return metric::Graph<WeightType, isDense, isSymmetric>(); //} void register_wrapper_graph(py::module& m) { using Class = metric::Graph<>; auto cls = py::class_<Class>(m, "Graph", "Graph based on blaze-lib"); cls.def(py::init<>()); cls.def(py::init<size_t>(), py::arg("nodes_number") ); cls.def(py::init<const std::vector<std::pair<size_t, size_t>>&>(), py::arg("edges_pairs") ); cls.def_property_readonly("nodes_number", &Class::getNodesNumber); cls.def_property_readonly("is_valid", &Class::isValid); cls.def("neighbours", &Class::template getNeighbours<>, py::arg("node_index"), py::arg("max_deep") ); cls.def("get_matrix", &Class::get_matrix); cls.def("build", &Class::buildEdges, py::arg("edges_pairs") ); cls.def("__repr__", [](const Class &a) { return "<Graph>"; }); } void register_wrapper_grids(py::module& m) { using namespace metric; py::class_<Grid4, metric::Graph<>>(m, "Grid4") .def(py::init<size_t>(), py::arg("nodes_number")) .def(py::init<size_t, size_t>(), py::arg("width"), py::arg("height")) .def("__repr__", [](const Grid4 &a) { return "<Grid4>"; }); py::class_<Grid6, metric::Graph<>>(m, "Grid6", "Hexagonal grid") .def(py::init<size_t>(), py::arg("nodes_number")) .def(py::init<size_t, size_t>(), py::arg("width"), py::arg("height")) .def("__repr__", [](const Grid6 &a) { return "<Grid6>"; }); py::class_<Grid8, metric::Graph<>>(m, "Grid8") .def(py::init<size_t>(), py::arg("nodes_number")) .def(py::init<size_t, size_t>(), py::arg("width"), py::arg("height")) .def("__repr__", [](const Grid8 &a) { return "<Grid8>"; }); } void export_metric_graphs(py::module& m) { register_wrapper_graph(m); register_wrapper_grids(m); }
2,257
C++
.cpp
52
38.5
108
0.60919
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,562
sparsify.cpp
metric-space-ai_metric/python/src/utils/sparsify.cpp
#include "metric/utils/graph/sparsify.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> namespace py = pybind11; template<typename Tv> void register_wrapper_sparsify(py::module& m) { m.def("sparsify_effective_resistance", &metric::sparsify_effective_resistance<Tv>, "Apply Spielman-Srivastava sparsification: sampling by effective resistances.", py::arg("a"), py::arg("ep") = 0.3, py::arg("matrix_conc_const") = 4.0, py::arg("jl_fac") = 4.0 ); m.def("sparsify_spanning_tree", &metric::sparsify_spanning_tree<Tv>, "Apply Kruskal's algorithm.", py::arg("a"), py::arg("minimum") = false ); } void export_metric_sparsify(py::module& m) { register_wrapper_sparsify<double>(m); }
800
C++
.cpp
23
30.043478
87
0.661935
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,563
dataset.cpp
metric-space-ai_metric/python/src/utils/dataset.cpp
#include "metric/utils/datasets.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> namespace py = pybind11; void register_wrapper_datasets(py::module& m) { using Class = Datasets; auto dim = py::class_<Class>(m, "Datasets"); dim.def(py::init<>()); dim.def("get_mnist", &Class::getMnist, py::arg("filename") ); } void export_metric_datasets(py::module& m) { register_wrapper_datasets(m); }
462
C++
.cpp
16
25.75
48
0.682432
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,564
sorensen.cpp
metric-space-ai_metric/python/src/distance/sorensen.cpp
#include "metric/distance/k-related/L1.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> namespace py = pybind11; template<typename Value, typename Container> void register_wrapper(py::module& m) { using Metric = metric::Sorensen<Value>; using BContainer = blaze::CompressedVector<Value>; auto cls = py::class_<Metric>(m, "Sorensen", "Sorensen–Dice coefficient"); cls.def(py::init<>()); cls.def("__call__", (Value (Metric::*)(const Container&, const Container&) const) &Metric::operator(), py::arg("a"), py::arg("b") ); cls.def("__call__", (Value (Metric::*)(const BContainer&, const BContainer&) const) &Metric::operator(), py::arg("a"), py::arg("b") ); cls.def("__repr__", [](const Metric &a) { return "<Sorensen>"; }); } void export_metric_sorensen(py::module& m) { register_wrapper<double, std::vector<double>>(m); }
958
C++
.cpp
25
34.24
108
0.646552
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,565
init.cpp
metric-space-ai_metric/python/src/distance/init.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <pybind11/pybind11.h> namespace py = pybind11; void export_metric_EMD(py::module& m); void export_metric_RandomEMD(py::module& m); void export_metric_kohonen(py::module& m); void export_metric_sorensen(py::module& m); void export_metric_SSIM(py::module& m); void export_metric_TWED(py::module& m); void export_metric_Edit(py::module& m); PYBIND11_MODULE(distance, m) { export_metric_EMD(m); export_metric_RandomEMD(m); export_metric_kohonen(m); export_metric_sorensen(m); export_metric_SSIM(m); export_metric_TWED(m); export_metric_Edit(m); }
821
C++
.cpp
24
31.541667
69
0.728878
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,566
kohonen.cpp
metric-space-ai_metric/python/src/distance/kohonen.cpp
#include "metric/distance/k-structured/Kohonen.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <pybind11/iostream.h> #include <vector> namespace py = pybind11; template<typename DistanceType, typename Sample, typename Graph, typename Metric> void register_wrapper_kohonen(py::module& m) { using Class = metric::Kohonen<DistanceType, Sample>; auto metric = py::class_<Class>(m, "Kohonen"); metric.def(py::init<const std::vector<Sample>&, Graph, Metric, double, double, size_t>(), py::arg("samples"), py::arg("graph"), py::arg("metric"), py::arg("start_learn_rate") = 0.8, py::arg("finish_learn_rate") = 0.0, py::arg("iterations") = 20 ); metric.def(py::init<const std::vector<Sample>&, size_t, size_t>(), py::arg("samples"), py::arg("nodes_width"), py::arg("nodes_height") ); metric.def("__call__", &Class::operator(), "Compute the EMD for two records in the Kohonen space.", py::arg("sample1"), py::arg("sample2") ); metric.def("print_shortest_path", &Class::print_shortest_path, py::call_guard<py::scoped_ostream_redirect, py::scoped_estream_redirect>(), "Recursive function that reconstructs the shortest backwards node by node.", py::arg("from_node"), py::arg("to_node") ); } void export_metric_kohonen(py::module& m) { using DistanceType = double; using SampleType = std::vector<DistanceType>; register_wrapper_kohonen<DistanceType, SampleType, metric::Grid4, metric::Euclidean<DistanceType>>(m); }
1,640
C++
.cpp
41
34.463415
106
0.652856
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,567
RandomEMD.cpp
metric-space-ai_metric/python/src/distance/RandomEMD.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include "metric/distance/k-random/RandomEMD.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> #include <string> #include <typeinfo> namespace py = pybind11; template<typename Sample, typename D = double> void wrap_metric_RandomEMD(py::module& m) { using Class = metric::RandomEMD<Sample, D>; auto emd = py::class_<Class>(m, "RandomEMD", "Random Earth mover's distance"); emd.def(py::init<>(), "Default constructor"); emd.def(py::init<double>(), py::arg("precision") ); D (Class::*call)(const Sample&, const Sample&) const = &Class::operator(); emd.def("__call__", call); } void export_metric_RandomEMD(py::module& m) { wrap_metric_RandomEMD<std::vector<double>, double>(m); }
1,005
C++
.cpp
28
33.428571
82
0.711479
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,568
SSIM.cpp
metric-space-ai_metric/python/src/distance/SSIM.cpp
#include "metric/distance/k-structured/SSIM.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> namespace py = pybind11; template<typename DistanceType, typename Value> void register_wrapper_SSIM(py::module& m) { using Metric = metric::SSIM<DistanceType, Value>; using ValueType = typename Value::value_type; using Container = typename std::vector<Value>; py::class_<Metric>(m, "SSIM", "Structural similarity (for images)") .def(py::init<>(), "Default constructor") .def(py::init<ValueType, ValueType>(), "Init constructor", py::arg("dynamic_range"), py::arg("masking") ) .def("__call__", &Metric::template operator()<Container>, "Calculate structural similarity for images in given containers" ) .def_readonly("dynamic_range", &Metric::dynamic_range, "dynamic range of the pixel values (default: 255.0)") .def_readonly("masking", &Metric::masking, "masking (default: 2.0)"); } void export_metric_SSIM(py::module& m) { register_wrapper_SSIM<double, std::vector<double>>(m); }
1,160
C++
.cpp
27
37.222222
116
0.666962
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,569
EMD.cpp
metric-space-ai_metric/python/src/distance/EMD.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include "metric/distance/k-structured/EMD.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> #include <string> #include <typeinfo> namespace py = pybind11; template<typename Value> void wrap_metric_EMD(py::module& m) { using Container = std::vector<std::vector<Value>>; using Vector = std::vector<Value>; using Class = metric::EMD<Value>; auto emd = py::class_<Class>(m, "EMD", "Earth mover's distance"); emd.def(py::init<>(), "Default constructor"); emd.def(py::init<Container&&>(), "Move constructor", py::arg("cost_matrix") ); emd.def(py::init<const Container&, const Value&>(), py::arg("cost_matrix"), py::arg("extra_mass_penalty") = -1 ); emd.def(py::init<std::size_t, std::size_t, const Value&>(), py::arg("rows"), py::arg("cols"), py::arg("extra_mass_penalty") = -1 ); Value (Class::*call)(const Vector&, const Vector&) const = &Class::template operator()<Vector>; emd.def("__call__", call); Container (*func1)(size_t,size_t) = &metric::EMD_details::ground_distance_matrix_of_2dgrid<Value>; //Container (*func2)(Container) = &metric::EMD_details::ground_distance_matrix_of_2dgrid<Value>; emd.def_static("ground_distance_matrix_of_2dgrid", func1, py::arg("cols"), py::arg("rows")); //emd.def("ground_distance_matrix_of_2dgrid", func2, (py::arg("grid")); emd.def_static("max_in_distance_matrix", metric::EMD_details::max_in_distance_matrix<Container>); } void export_metric_EMD(py::module& m) { wrap_metric_EMD<double>(m); }
1,859
C++
.cpp
44
38.022727
102
0.663712
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,570
Edit.cpp
metric-space-ai_metric/python/src/distance/Edit.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include "metric/distance/k-structured/Edit.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <string> #include <vector> namespace py = pybind11; template<typename Value> void register_wrapper_edit(py::module& m) { using Metric = metric::Edit<Value>; auto p1 = &Metric::template operator()<std::basic_string_view<Value>>; auto p2 = &Metric::template operator()<std::vector<double>>; int (Metric::*p3)(const Value*, const Value*) const = &Metric::operator(); py::class_<Metric>(m, "Edit") .def(py::init<>()) .def("__call__", p1) .def("__call__", p2) .def("__call__", p3); } void export_metric_Edit(py::module& m) { register_wrapper_edit<char>(m); }
996
C++
.cpp
28
32.071429
78
0.671518
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,571
TWED.cpp
metric-space-ai_metric/python/src/distance/TWED.cpp
#include "metric/distance/k-structured/TWED.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> namespace py = pybind11; template<typename ValueType> void register_wrapper_TWED(py::module& m) { using Metric = metric::TWED<ValueType>; py::class_<Metric>(m, "TWED") .def(py::init<ValueType, ValueType>(), py::arg("penalty") = 0, py::arg("elastic") = 1) .def("__call__", &Metric::template operator()<std::vector<double>>) .def("__call__", &Metric::template operator()<std::vector<int>>) .def("__call__", &Metric::template operator()<std::vector<unsigned long long>>) .def_readonly("penalty", &Metric::penalty) .def_readonly("elastic", &Metric::elastic) .def_readonly("is_zero_padded", &Metric::is_zero_padded); } void export_metric_TWED(py::module& m) { register_wrapper_TWED<double>(m); }
915
C++
.cpp
21
39.142857
94
0.662921
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,572
standards.cpp
metric-space-ai_metric/python/src/distance/subs/standards.cpp
#include "metric/distance/k-related/Standards.hpp" #include "stl_wrappers.hpp" #include "metric_types.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <pybind11/functional.h> namespace py = pybind11; template<typename T> const std::string python__str__(const T&) { return "<" + metric::getTypeName<T>() + ">"; } template<typename Value, typename Container> void register_wrapper_euclidean(py::module& m) { using Metric = metric::Euclidean<Value>; auto p1 = &Metric::template operator()<Container>; Value (Metric::*p2)(const Value&, const Value&) const = &Metric::operator(); py::class_<Metric>(m, "Euclidean") .def(py::init<>()) .def("__call__", p1) .def("__call__", p2) .def("__call__", &Metric::template operator()<std::vector<Value>>) .def("__repr__", &python__str__<Metric>); } template<typename Value, typename Container> void register_wrapper_manhatten(py::module& m) { using Metric = metric::Manhatten<Value>; py::class_<Metric>(m, "Manhatten") .def(py::init<>()) .def("__call__", &Metric::template operator()<Container>) .def("__call__", &Metric::template operator()<std::vector<Value>>) .def("__repr__", &python__str__<Metric>); } template<typename Value, typename Container> void register_wrapper_pnorm(py::module& m) { using Metric = metric::P_norm<Value>; py::class_<Metric>(m, "P_norm") .def(py::init<Value>(), py::arg("p") = 1) .def("__call__", &Metric::template operator()<Container>) .def("__call__", &Metric::template operator()<std::vector<Value>>) .def("__repr__", &python__str__<Metric>); } template<typename Value, typename Container> void register_wrapper_euclidean_thresholded(py::module& m) { using Metric = metric::Euclidean_thresholded<Value>; py::class_<Metric>(m, "Euclidean_thresholded") .def(py::init<>()) .def(py::init<Value, Value>(), py::arg("thres"), py::arg("factor")) .def("__call__", &Metric::template operator()<Container>) .def("__call__", &Metric::template operator()<std::vector<Value>>) .def("__repr__", &python__str__<Metric>); } template<typename Value, typename Container> void register_wrapper_cosine(py::module& m) { using Metric = metric::Cosine<Value>; py::class_<Metric>(m, "Cosine") .def(py::init<>()) .def("__call__", &Metric::template operator()<Container>) .def("__call__", &Metric::template operator()<std::vector<Value>>) .def("__repr__", &python__str__<Metric>); } template<typename Value, typename Container> void register_wrapper_chebyshev(py::module& m) { using Metric = metric::Chebyshev<Value>; py::class_<Metric>(m, "Chebyshev") .def(py::init<>()) .def("__call__", &Metric::template operator()<Container>) .def("__call__", &Metric::template operator()<std::vector<Value>>) .def("__repr__", &python__str__<Metric>); } void export_metric_standards(py::module& m) { register_wrapper_euclidean<double, NumpyToVectorAdapter<double>>(m); register_wrapper_manhatten<double, NumpyToVectorAdapter<double>>(m); register_wrapper_pnorm<double, NumpyToVectorAdapter<double>>(m); register_wrapper_euclidean_thresholded<double, NumpyToVectorAdapter<double>>(m); register_wrapper_cosine<double, NumpyToVectorAdapter<double>>(m); register_wrapper_chebyshev<double, NumpyToVectorAdapter<double>>(m); } PYBIND11_MODULE(standards, m) { export_metric_standards(m); }
3,566
C++
.cpp
81
39.234568
84
0.652562
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,573
init.cpp
metric-space-ai_metric/python/src/space/init.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <pybind11/pybind11.h> namespace py = pybind11; void export_metric_matrix(py::module& m); void export_metric_Tree(py::module& m); PYBIND11_MODULE(space, m) { export_metric_matrix(m); export_metric_Tree(m); }
465
C++
.cpp
14
30.785714
69
0.731544
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,574
tree.cpp
metric-space-ai_metric/python/src/space/tree.cpp
#include "metric/space/tree.hpp" #include "metric/distance/k-related/Standards.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> #include <tuple> namespace py = pybind11; /* TODO: template <class Archive, class Stream> void deserialize(Archive& input, Stream& stream); template <class Archive> void serialize(Archive& archive); void traverse(const std::function<void(Node_ptr)>& f); void print(std::ostream& ostr) const; std::string to_json(std::function<std::string(const RecType&)> printer); void traverse_child(const std::function<void(Node_ptr)>& f); bool same_tree(const Node_ptr lhs, const Node_ptr rhs) const; friend std::ostream& operator<<(std::ostream& ostr, const Tree& t); blaze::SymmetricMatrix<blaze::DynamicMatrix<Distance, blaze::rowMajor>> matrix() const; */ template <typename RecType, typename Metric> void register_wrapper_Tree(py::module& m) { using Tree = metric::Tree<RecType, Metric>; using Node = metric::Node<RecType, Metric>; using Container = std::vector<RecType>; using Distance = typename Tree::Distance; auto tree = py::class_<Tree>(m, "Tree"); tree.def(py::init<int>(), "Empty tree", py::arg("truncate") = -1); // FIXME: broken in C++ add_value // tree.def(py::init<const RecType&, int>( // ( // py::arg("p"), // py::arg("truncate") = -1 // ) // )); tree.def(py::init<const Container&, int>(), py::arg("p"), py::arg("truncate") = -1 ); std::vector<std::vector<std::size_t>> (Tree::*clustering1) ( const std::vector<double>&, const std::vector<std::size_t>&, const std::vector<RecType>& ) = &Tree::clustering; // FIXME: broken in C++ // std::vector<std::vector<std::size_t>> (Tree::*clustering2) ( // const std::vector<double>&, // const std::vector<std::size_t>& // ) = &Tree::clustering; std::vector<std::vector<std::size_t>> (Tree::*clustering3) ( const std::vector<double>&, const std::vector<RecType>& ) = &Tree::clustering; size_t (Tree::*insert1) (const RecType&) = &Tree::insert; bool (Tree::*insert2) (const std::vector<RecType>&) = &Tree::insert; std::tuple<std::size_t, bool> (Tree::*insert_if1) (const RecType&, Distance) = &Tree::insert_if; std::size_t (Tree::*insert_if2) (const std::vector<RecType>&, Distance) = &Tree::insert_if; tree.def("clustering", clustering1); // tree.def("clustering", clustering2); tree.def("clustering", clustering3); tree.def("insert", insert1); tree.def("insert", insert2); tree.def("insert", insert_if1); tree.def("insert", insert_if2); tree.def("erase", &Tree::erase); tree.def("__getitem__", &Tree::operator[]); tree.def("nn", &Tree::nn, py::return_value_policy::reference_internal); tree.def("knn", &Tree::knn); tree.def("rnn", &Tree::rnn); tree.def("__len__", &Tree::size); tree.def("empty", &Tree::size); tree.def("to_vector", &Tree::toVector); tree.def("check_covering", &Tree::check_covering); tree.def("distance_by_id", &Tree::distance_by_id); tree.def("distance", &Tree::distance); std::string (Tree::*to_json1)(std::function<std::string(const RecType&)>) = &Tree::to_json; std::string (Tree::*to_json2)() = &Tree::to_json; tree.def("to_json", to_json1); tree.def("to_json", to_json2); void (Tree::*print)() const = &Tree::print; tree.def("level_size", &Tree::levelSize); tree.def("print", print); tree.def("print_levels", &Tree::print_levels); tree.def("root", &Tree::get_root, py::return_value_policy::reference_internal); tree.def("__eq__", &Tree::operator==); // just simple wrapper to return something auto node = py::class_<Node>(m, "Node"); } void export_metric_Tree(py::module& m) { register_wrapper_Tree<std::vector<double>, metric::Euclidean<double>>(m); }
3,966
C++
.cpp
90
39.844444
100
0.638529
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,575
matrix.cpp
metric-space-ai_metric/python/src/space/matrix.cpp
#include "metric/space/matrix.hpp" #include "metric/distance/k-related/Standards.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <vector> #include <utility> #include <string> namespace py = pybind11; template<typename Container, typename RecType, typename Metric> metric::Matrix<RecType, Metric> createMatrix(const Metric& m, const Container& data) { return metric::Matrix<RecType, Metric>(data, m); } template<typename RecType, typename Metric> metric::Matrix<RecType, Metric> createMatrixEmpty(const Metric& m) { return metric::Matrix<RecType, Metric>(m); } template<typename RecType, typename Metric> void register_wrapper_matrix(py::module& m, const std::string& postfix) { using Matrix = metric::Matrix<RecType, Metric>; using Container = std::vector<RecType>; m.def("create_matrix", &createMatrix<Container, RecType, Metric>, "Distance matrix factory", py::arg("metric"), py::arg("items") ); m.def("create_matrix", &createMatrix<RecType, RecType, Metric>, py::arg("metric"), py::arg("item") ); m.def("create_matrix", &createMatrixEmpty<RecType, Metric>, py::arg("metric") ); size_t (Matrix::*insert1)(const RecType&) = &Matrix::insert; std::vector<size_t> (Matrix::*insert2)(const Container&) = &Matrix::insert; std::pair<std::size_t, bool> (Matrix::*insert_if1)(const RecType&, typename Matrix::distType) = &Matrix::insert_if; std::vector<std::pair<std::size_t, bool>> (Matrix::*insert_if2)(const Container&, typename Matrix::distType) = &Matrix::insert_if; const std::string name = std::string("Matrix_") + postfix; py::class_<Matrix>(m, name.c_str()) .def(py::init<>(), "empty matrix") .def(py::init<const RecType&>(), "Construct a new Matrix with one data record", py::arg("p") ) .def(py::init<const Container&>(), "Construct a new Matrix with set of data records", py::arg("items") ) .def("__repr__", [](const Matrix& self){ return "<Matrix object>"; }) .def("insert", insert1, "append data record to the matrix", py::arg("item") ) .def("insert_if", insert_if1, "append data record into the Matrix only if distance bigger than a threshold", py::arg("item"), py::arg("threshold") ) .def("insert", insert2, "append set of data records to the matrix", py::arg("items") ) .def("insert_if", insert_if2, "append data records into the Matrix only if distance bigger than a threshold", py::arg("items"), py::arg("threshold") ) .def("erase", &Matrix::erase, "erase data record from Matrix by ID", py::arg("index") ) .def("__getitem__", &Matrix::operator[], "access a data record by ID", py::arg("index") ) .def("__setitem__", &Matrix::set, "change data record by ID", py::arg("index"), py::arg("record") ) .def("__call__", &Matrix::operator(), "access a distance by two IDs", py::arg("i"), py::arg("j") ) .def("__len__", &Matrix::size, "amount of data records" ) .def("nn", &Matrix::nn, "find nearest neighbour of data record", py::arg("p") ) .def("knn", &Matrix::knn, "find K nearest neighbours of data record", py::arg("p"), py::arg("k") = 10 ) .def("rnn", &Matrix::rnn, "find all nearest neighbour in range [0;distance]", py::arg("p"), py::arg("range") = 1.0 ); } void export_metric_matrix(py::module& m) { register_wrapper_matrix<std::vector<double>, metric::Euclidean<double>>(m, "Euclidean"); register_wrapper_matrix<std::vector<double>, metric::Manhatten<double>>(m, "Manhatten"); register_wrapper_matrix<std::vector<double>, metric::P_norm<double>>(m, "P_norm"); }
4,196
C++
.cpp
108
30.731481
134
0.581515
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,576
mgc.cpp
metric-space-ai_metric/python/src/correlation/mgc.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include "metric/correlation/mgc.hpp" #include "metric_types.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <string> #include <vector> class NotUsed {}; namespace py = pybind11; template<typename Metric1, typename Metric2> metric::MGC<NotUsed, Metric1, NotUsed, Metric2> createMGC(Metric1 metric1, Metric2 metric2) { return metric::MGC<NotUsed, Metric1, NotUsed, Metric2>(metric1, metric2); } template <class ValueType, class Metric1, class Metric2> void wrap_metric_MGC(py::module& m) { using Container = std::vector<std::vector<ValueType>>; using Class = metric::MGC<NotUsed, Metric1, NotUsed, Metric2>; m.def("create_mgc", &createMGC<Metric1, Metric2>, "internal method to create MGC instances", py::arg("metric1"), py::arg("metric2") ); const std::string name = std::string("MGC_") + metric::getTypeName<Metric1>() + std::string("_") + metric::getTypeName<Metric2>(); auto mgc = py::class_<Class>(m, name.c_str()); auto corr_ptr = &Class::template operator()<Container, Container>; mgc.def("__call__", corr_ptr, "Multiscale Graph Correlation between a and b", py::arg("a"), py::arg("b") ); auto xcorr_ptr = &Class::template xcorr<Container, Container>; mgc.def("xcorr", xcorr_ptr, "Return vector of MGC values calculated for different data shifts", py::arg("a"), py::arg("b"), py::arg("n") ); auto estimate_ptr = &Class::template estimate<Container, Container>; mgc.def("estimate", estimate_ptr, "Estimate of the correlation between a and b", py::arg("a"), py::arg("b"), py::arg("b_sample_size") = 250, py::arg("threshold") = 0.05, py::arg("max_iterations") = 1000 ); // should be private // m.def("compute_distance_matrix", &computeDistanceMatrix<Container, Metric1>); // m.def("compute_distance_matrix", &computeDistanceMatrix<Container, Metric2>); } template <class T> void wrap_metric_MGC_direct(py::module& m) { using Metric = metric::MGC_direct; auto mgc = py::class_<Metric>(m, "MGC_direct"); mgc.def("__call__", &Metric::xcorr<T>); // FIXME: unsupported argument types mgc.def("xcorr", &Metric::xcorr<T>); // FIXME: unsupported argument types mgc.def("center_distance_matrix", &Metric::center_distance_matrix<T>); mgc.def("rank_distance_matrix", &Metric::rank_distance_matrix<T>); mgc.def("rank_distance_matrix", &Metric::rank_distance_matrix<T>); mgc.def("center_ranked_distance_matrix", &Metric::center_ranked_distance_matrix<T>); mgc.def("local_covariance", &Metric::local_covariance<T>); mgc.def("normalize_generalized_correlation", &Metric::normalize_generalized_correlation<T>); mgc.def("rational_approximation", &Metric::rational_approximation<T>); mgc.def("normal_CDF_inverse", &Metric::normal_CDF_inverse<T>); mgc.def("icdf_normal", &Metric::icdf_normal<T>); mgc.def("significant_local_correlation", &Metric::significant_local_correlation<T>); mgc.def("frobeniusNorm", &Metric::frobeniusNorm<T>); mgc.def("max_in_matrix_regarding_second_boolean_matrix", &Metric::max_in_matrix_regarding_second_boolean_matrix<T>); mgc.def("optimal_local_generalized_correlation", &Metric::optimal_local_generalized_correlation<T>); } void export_metric_MGC(py::module& m) { using T = double; using Container = std::vector<T>; using Functor = std::function<T(const Container&, const Container&)>; boost::mpl::for_each<metric::MetricTypes, boost::mpl::make_identity<boost::mpl::_1>>([&](auto metr1) { using Metric1 = typename decltype(metr1)::type; boost::mpl::for_each<metric::MetricTypes, boost::mpl::make_identity<boost::mpl::_1>>([&](auto metr2) { using Metric2 = typename decltype(metr2)::type; wrap_metric_MGC<T, Metric1, Metric2>(m); }); }); wrap_metric_MGC<T, Functor, Functor>(m); wrap_metric_MGC_direct<T>(m); } PYBIND11_MODULE(mgc, m) { export_metric_MGC(m); }
4,355
C++
.cpp
97
39.907216
120
0.672166
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,577
entropy.cpp
metric-space-ai_metric/python/src/correlation/entropy.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include "metric_types.hpp" #include "metric/correlation/entropy.hpp" #include <pybind11/pybind11.h> #include <pybind11/stl.h> #include <pybind11/numpy.h> #include <pybind11/functional.h> #include <vector> #include <string> namespace py = pybind11; template <typename Metric = metric::Euclidean<double>()> metric::Entropy<void, Metric> createEntropy( const Metric& metric, size_t k = 7, size_t p = 70, bool exp = false ){ return metric::Entropy<void, Metric>(metric, k, p, exp); } template <typename Container, typename Metric> void wrap_metric_entropy(py::module& m) { using Class = metric::Entropy<void, Metric>; m.def("Entropy", &createEntropy<Metric>, "Factory of Entropy instances", py::arg("metric"), py::arg("k") = 7, py::arg("p") = 70, py::arg("exp") = false ); const std::string name = std::string("Entropy_") + metric::getTypeName<Metric>(); auto cls = py::class_<Class>(m, name.c_str()); cls.def("__call__", &Class::template operator()<Container>, "Calculate entropy", py::arg("data") ); cls.def("estimate", &Class::template estimate<Container>, "Estimate", py::arg("data"), py::arg("sample_size") = 250, py::arg("threshold") = 0.05, py::arg("max_iterations") = 100 ); } void export_metric_entropy(py::module& m) { using Value = double; using RecType = std::vector<Value>; using Container = std::vector<RecType>; using Functor = std::function<Value(const RecType&, const RecType&)>; boost::mpl::for_each<metric::MetricTypes, boost::mpl::make_identity<boost::mpl::_1>>([&](auto metr) { using MetricType = typename decltype(metr)::type; wrap_metric_entropy<Container, MetricType>(m); }); wrap_metric_entropy<Container, Functor>(m); } PYBIND11_MODULE(entropy, m) { export_metric_entropy(m); }
2,152
C++
.cpp
62
30.129032
105
0.655588
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,578
wavelet_benchmarks.cpp
metric-space-ai_metric/benchmarks/transform/wavelet_benchmarks.cpp
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_ENABLE_BENCHMARKING #include <catch2/catch.hpp> #include <metric/transform/wavelet.hpp> using T = double; TEST_CASE("Daubechies matrix construction", "[transform][wavelet]") { BENCHMARK("Daubechies matrix construction") { return wavelet::DaubechiesMat<T>(48000, 2); }; }
335
C++
.cpp
12
25.5
67
0.768987
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,579
convolution_benchmarks.cpp
metric-space-ai_metric/benchmarks/image_processing_benchmarks/convolution_benchmarks.cpp
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_ENABLE_BENCHMARKING #include <catch2/catch.hpp> #include <iostream> #include "metric/utils/image_processing/convolution.hpp" using namespace metric; using T = double; using Conv = Convolution2d<T, 1>; std::tuple<Conv::Image, Conv::FilterKernel> generateImageAndKernel(size_t imageWidth, size_t imageHeight, size_t kernelWidth, size_t kernelHeight) { std::normal_distribution<T> normalDistribution(0.5, 0.25); std::mt19937 randomEngine{std::random_device()()}; Conv::FilterKernel kernel = blaze::generate(kernelHeight, kernelWidth, [&normalDistribution, &randomEngine](size_t i, size_t j) { return normalDistribution(randomEngine); }); Conv::Image image; for (auto& channel: image) { channel = blaze::generate(imageHeight, imageWidth, [&normalDistribution, &randomEngine](size_t i, size_t j) { return normalDistribution(randomEngine); }); } return {image, kernel}; } TEST_CASE("Convolution2d benchmarks") { const auto [imageWidth, imageHeight] = GENERATE(table<size_t, size_t>({{320, 240}, {640, 480}, {1920, 1080}})); const auto kernelSize = GENERATE(3, 5, 7); const size_t kernelWidth = kernelSize; const size_t kernelHeight = kernelSize; const std::string imageSizeString = std::to_string(imageWidth) + "x" + std::to_string(imageHeight); const std::string kernelSizeString = std::to_string(kernelWidth) + "x" + std::to_string(kernelHeight); const std::string postfix = "[" + imageSizeString + " " + kernelSizeString + "]"; BENCHMARK("Convolution2d() " + postfix) { return Conv(imageWidth, imageHeight, kernelWidth, kernelHeight); }; BENCHMARK_ADVANCED("setKernel() " + postfix)(Catch::Benchmark::Chronometer meter) { const auto [image, kernel] = generateImageAndKernel(imageWidth, imageHeight, kernelWidth, kernelHeight); auto conv = Conv(imageWidth, imageHeight, kernelWidth, kernelHeight); meter.measure([&conv, &kernel] { return conv.setKernel(kernel); }); }; BENCHMARK_ADVANCED("operator() " + postfix)(Catch::Benchmark::Chronometer meter) { const auto [image, kernel] = generateImageAndKernel(imageWidth, imageHeight, kernelWidth, kernelHeight); auto conv = Conv(imageWidth, imageHeight, kernelWidth, kernelHeight); conv.setKernel(kernel); meter.measure([&conv, &image] { return conv(image); }); }; }
2,643
C++
.cpp
55
40
106
0.659268
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,580
kohonen_outliers_clustering_tests.cpp
metric-space-ai_metric/tests/mapping_tests/kohonen_outliers_clustering_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <vector> #include <chrono> #include "metric/mapping.hpp" TEMPLATE_TEST_CASE("Main", "[mapping]", float, double) { using Record = std::vector<TestType>; size_t best_w_grid_size = 3; size_t best_h_grid_size = 2; // init std::vector<Record> dataset = { {0, 0.1}, {0.2, 0}, {0, 1.2}, {0.1, 1}, {0.1, 2}, {0.2, 2}, {1, 0}, {1.2, 0.1}, {1.3, 1.1}, {0.9, 1}, {1.1, 2}, {0.9, 1.9}, }; std::vector<Record> test_set = { {0.5, 0.5}, {5, 5}, {0, 0.1}, {0, 1}, }; int num_clusters = 3; TestType sigma = 1.5; TestType random_seed = 777; metric::KOC_factory<Record, metric::Grid4> simple_koc_factory(best_w_grid_size, best_h_grid_size, sigma, 0.5, 0.0, 300, -1, 1, 2, 0.5, random_seed); auto simple_koc = simple_koc_factory(dataset, num_clusters); // original dataset auto anomalies = simple_koc.check_if_anomaly(dataset); // there are should be no anomalies in the train dataset for (int i = 0; i < anomalies.size(); i++) { REQUIRE(anomalies[i] == 0); } auto assignments = simple_koc.assign_to_clusters(dataset); // there are should be no zero-named clusters in the train dataset for (int i = 0; i < assignments.size(); i++) { REQUIRE(assignments[i] != 0); } // test dataset anomalies = simple_koc.check_if_anomaly(test_set); // first two records should be anomalies, others two should not REQUIRE(anomalies[0] == 1); REQUIRE(anomalies[1] == 1); REQUIRE(anomalies[2] == 0); REQUIRE(anomalies[3] == 0); assignments = simple_koc.assign_to_clusters(test_set); // first two records should zero-named anomalies REQUIRE(assignments[0] == 0); REQUIRE(assignments[1] == 0); // top outliers auto [idxs, sorted_distances] = simple_koc.top_outliers(test_set, 10); // indexes should be as following REQUIRE(idxs[0] == 1); REQUIRE(idxs[1] == 0); REQUIRE(idxs[2] == 2); REQUIRE(idxs[3] == 3); } TEMPLATE_TEST_CASE("Empty", "[mapping]", float, double) { using Record = std::vector<TestType>; size_t best_w_grid_size = 3; size_t best_h_grid_size = 2; // init std::vector<Record> dataset = { {0, 0.1}, {0.2, 0}, {0, 1.2}, {0.1, 1}, {0.1, 2}, {0.2, 2}, {1, 0}, {1.2, 0.1}, {1.3, 1.1}, {0.9, 1}, {1.1, 2}, {0.9, 1.9}, }; std::vector<Record> test_set = { {0.5, 0.5}, {5, 5}, {0, 0.1}, {0, 1}, }; int num_clusters = 3; TestType sigma = 1.5; TestType random_seed = 777; // empty test dataset std::vector<Record> empty_set; metric::KOC_factory<Record, metric::Grid4> koc_factory_1(best_w_grid_size, best_h_grid_size, sigma, 0.5, 0.0, 300, -1, 1, 2, 0.5, random_seed); auto koc_1 = koc_factory_1(dataset, num_clusters); auto anomalies = koc_1.check_if_anomaly(empty_set); // all should be ok, but zero sized result REQUIRE(anomalies.size() == 0); auto assignments = koc_1.assign_to_clusters(empty_set); // all should be ok, but zero sized result REQUIRE(assignments.size() == 0); //top outliers auto [idxs, sorted_distances] = koc_1.top_outliers(empty_set, 10); // all should be ok, but zero sized result REQUIRE(idxs.size() == 0); REQUIRE(sorted_distances.size() == 0); }
3,426
C++
.cpp
116
26.594828
153
0.65122
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,581
sw_det_tests.cpp
metric-space-ai_metric/tests/mapping_tests/sw_det_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ //#define CATCH_CONFIG_MAIN #include <catch2/catch.hpp> //#include "metric/mapping.hpp" #include "metric/mapping/deterministic_switch_detector.hpp" //#include <iostream> TEMPLATE_TEST_CASE("switch_detector", "[mapping]", float, double) { blaze::DynamicMatrix<TestType> dataset = { {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 15}, {0, 13, 17, 20}, {0, 25, 22, 25}, {0, 10, 15, 10}, {0, 10, 10, 10}, {0, 5, 5, 10}, {0, 5, 2, 5}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 20, 20, 20}, {0, 30, 30, 30}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 100, 100, 100}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10}, {0, 10, 10, 10} }; auto d = DetSwitchDetector<TestType>(8, 0.5); auto switches = d.encode(dataset); //std::cout << std::endl << "switches:" << std::endl << switches << std::endl; REQUIRE( ((blaze::sum(blaze::abs(switches)) == 2) && (switches(23, 0) == 1) && (switches(31, 0) == -1)) ); d = DetSwitchDetector<TestType>(8); switches = d.encode(dataset); //std::cout << std::endl << "switches:" << std::endl << switches << std::endl; REQUIRE( ((blaze::sum(blaze::abs(switches)) == 1) && (switches(23, 0) == 1)) ); }
2,041
C++
.cpp
61
26.57377
110
0.491603
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,582
pcfa_tests.cpp
metric-space-ai_metric/tests/mapping_tests/pcfa_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <cmath> #include <deque> #include <vector> #include "metric/mapping/PCFA.hpp" #include "metric/utils/metric_err.hpp" TEMPLATE_TEST_CASE("PCFA", "[mapping]", std::deque<float>, std::deque<double>, std::vector<float>, std::vector<double>) { TestType d0_blaze{0, 1, 2}; TestType d1_blaze{0, 1, 3}; std::vector<TestType> d_train = {d0_blaze, d1_blaze}; using RecType = TestType; auto pcfa0 = metric::PCFA<RecType, void>(d_train, 2); // auto pcfa = metric::PCFA_factory(d_train, 2); // we also can use factory for autodeduction auto weights = pcfa0.weights(); auto bias = pcfa0.average(); // model saved to vector and matrix auto pcfa = metric::PCFA<RecType, void>(weights, bias); // model leaded, same as pcfa0 RecType d2_blaze{0, 1, 4}; RecType d3_blaze{0, 2, 2}; std::vector<RecType> d_test = {d0_blaze, d2_blaze, d3_blaze}; // Check compression, i.e. PCFA::encode(...) typename TestType::value_type maxerr = 0; typename TestType::value_type err = 0; auto d_compressed = pcfa.encode(d_test); std::vector<RecType> t_compressed = {{-0.5, 0}, {1.5, 0}, {-0.5, 1}}; for (size_t i = 0; i < d_compressed.size(); i++) { for (size_t j = 0; j < d_compressed[i].size(); j++) { err = abs(d_compressed[i][j] - t_compressed[i][j]); if (err > maxerr) maxerr = err; } } REQUIRE(maxerr == Approx(0)); // check the output of PCFA::encode // Check decompression, i.e. PCFA::decode(...) maxerr = 0; err = 0; auto d_restored = pcfa.decode(d_compressed); std::vector<RecType> t_restored = {{0, 1, 2}, {0, 1, 4}, {0, 2, 2}}; for (size_t i = 0; i < d_restored.size(); i++) { for (size_t j = 0; j < d_restored[i].size(); j++) { err = abs(d_restored[i][j] - t_restored[i][j]); if (err > maxerr) maxerr = err; } } REQUIRE(maxerr == Approx(0)); // Check eigenmodes, i.e. PCFA::eigenmodes() maxerr = 0; err = 0; std::vector<RecType> t_eigenmodes = {{0, 1, 2.5}, {0, 0, 1}, {0, 1, 0}}; auto d_eigenmodes = pcfa.eigenmodes(); for (size_t i = 0; i < d_eigenmodes.size(); i++) { for (size_t j = 0; j < d_eigenmodes[i].size(); j++) { err = abs(d_eigenmodes[i][j] - t_eigenmodes[i][j]); if (err > maxerr) maxerr = err; } } REQUIRE(maxerr == Approx(0)); }
2,490
C++
.cpp
68
34.088235
119
0.643036
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,583
dspcc_tests.cpp
metric-space-ai_metric/tests/mapping_tests/dspcc_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <vector> #include <deque> #include <cmath> //#include <chrono> #include "metric/mapping/DSPCC.hpp" #include "metric/utils/metric_err.hpp" #include "metric/distance/k-related/Standards.hpp" // we use Euclidean metric for mean squared error evaluation TEMPLATE_TEST_CASE("DSPCC", "[mapping]", std::deque<float>, std::deque<double>, std::vector<float>, std::vector<double>) { TestType d0 {0, 1, 2, 3, 4, 5, 6, 100, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23}; TestType d1 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 100}; std::vector<TestType> d = {d0, d1}; float freq_time_balance = 0.5; // try values from 0 to 1 (e g 0, 0.5, 1) to get the following portions of freq-domain: 0, 4/9, 8/9 auto bundle = metric::DSPCC<TestType, void>(d, 4, 2, freq_time_balance, 4); auto pre_encoded = bundle.test_public_wrapper_encode(d); auto pre_decoded = bundle.test_public_wrapper_decode(pre_encoded); auto encoded = bundle.encode(d); auto decoded = bundle.decode(encoded); typename TestType::value_type maxerr = 0; typename TestType::value_type err = 0; for (size_t i = 0; i < d.size(); ++i) { for (size_t j = 0; j < d[i].size(); ++j) { err = abs(d[i][j] - decoded[i][j]); if (err > maxerr) maxerr = err; } } REQUIRE(maxerr < std::numeric_limits<typename TestType::value_type>::epsilon() * 1e7); }
1,735
C++
.cpp
35
44.942857
134
0.64095
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,584
mapping_tests.cpp
metric-space-ai_metric/tests/mapping_tests/mapping_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <algorithm> #include "metric/distance.hpp" #include "metric/utils/graph.hpp" using namespace Catch::literals; TEMPLATE_TEST_CASE("Metric", "[mapping]", float, double) { using Vector = std::vector<TestType>; metric::Euclidean<TestType> metric; Vector vNull = {}; Vector v0 = { 0, 0, 0, 0, 0 }; Vector v7 = { 7, 7, 7.5, 7, 7 }; Vector v = { 1, 2.3, -2.7, 0, 3 }; //TestType t = 0.0000001; REQUIRE(metric(vNull, vNull) == 0_a); REQUIRE(metric(v0, v0) == 0_a); REQUIRE(metric(v7, v) == 15.070832757349542_a); } TEST_CASE("Grid4", "[mapping]") { metric::Grid4 grid5(5); REQUIRE_FALSE(grid5.isValid()); metric::Grid4 grid25(25); REQUIRE(grid25.isValid()); REQUIRE(grid25.getNodesNumber() == 25); metric::Grid4 grid32(3, 2); REQUIRE(grid32.isValid()); REQUIRE(grid32.getNodesNumber() == 6); auto neighboursList = grid25.getNeighbours(9, 3); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } std::vector<size_t> neighbours0 = { 9 }; REQUIRE(neighboursList[0] == neighbours0); std::vector<size_t> neighbours1 = { 4, 8, 14 }; REQUIRE(neighboursList[1] == neighbours1); std::vector<size_t> neighbours2 = { 3, 7, 13, 19 }; REQUIRE(neighboursList[2] == neighbours2); } TEST_CASE("Grid6", "[mapping]") { metric::Grid6 grid5(5); REQUIRE_FALSE(grid5.isValid()); metric::Grid6 grid25(25); REQUIRE(grid25.isValid()); REQUIRE(grid25.getNodesNumber() == 25); metric::Grid6 grid30(6, 5); REQUIRE(grid30.isValid()); REQUIRE(grid30.getNodesNumber() == 30); auto neighboursList = grid30.getNeighbours(12, 3); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } std::vector<size_t> neighbours0 = { 12 }; REQUIRE(neighboursList[0] == neighbours0); std::vector<size_t> neighbours1 = { 6, 13, 18 }; REQUIRE(neighboursList[1] == neighbours1); std::vector<size_t> neighbours2 = { 0, 1, 7, 14, 19, 24, 25 }; REQUIRE(neighboursList[2] == neighbours2); } TEST_CASE("Grid8", "[mapping]") { metric::Grid8 grid5(5); REQUIRE_FALSE(grid5.isValid()); metric::Grid8 grid25(25); REQUIRE(grid25.isValid()); REQUIRE(grid25.getNodesNumber() == 25); metric::Grid8 grid6(3, 2); REQUIRE(grid6.isValid()); REQUIRE(grid6.getNodesNumber() == 6); auto neighboursList = grid25.getNeighbours(9, 3); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } std::vector<size_t> neighbours0 = { 9 }; REQUIRE(neighboursList[0] == neighbours0); std::vector<size_t> neighbours1 = { 3, 4, 8, 13, 14 }; REQUIRE(neighboursList[1] == neighbours1); std::vector<size_t> neighbours2 = { 2, 7, 12, 17, 18, 19 }; REQUIRE(neighboursList[2] == neighbours2); std::vector<size_t> neighbours3 = { 1, 6, 11, 16, 21, 22, 23, 24 }; REQUIRE(neighboursList[3] == neighbours3); } TEST_CASE("LPS", "[mapping]") { metric::LPS lps5(5); REQUIRE(lps5.isValid()); metric::LPS lps(11); REQUIRE(lps.isValid()); auto neighboursList = lps.getNeighbours(9, 2); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } std::vector<size_t> neighbours0 = { 9 }; REQUIRE(neighboursList[0] == neighbours0); std::vector<size_t> neighbours1 = { 5, 8, 10 }; REQUIRE(neighboursList[1] == neighbours1); std::vector<size_t> neighbours2 = { 0, 4, 6, 7 }; REQUIRE(neighboursList[2] == neighbours2); /* LPS(41) */ metric::LPS lps41(41); REQUIRE(lps41.isValid()); neighboursList = lps41.getNeighbours(9, 2); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } neighbours0 = { 9 }; REQUIRE(neighboursList[0] == neighbours0); neighbours1 = { 8, 10, 32 }; REQUIRE(neighboursList[1] == neighbours1); neighbours2 = { 7, 11, 31, 33, 36, 37 }; REQUIRE(neighboursList[2] == neighbours2); } TEST_CASE("Paley", "[mapping]") { metric::Paley paley12(12); REQUIRE_FALSE(paley12.isValid()); metric::Paley paley13(13); REQUIRE(paley13.isValid()); auto neighboursList = paley13.getNeighbours(9, 1); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } std::vector<size_t> neighbours0 = { 9 }; REQUIRE(neighboursList[0] == neighbours0); std::vector<size_t> neighbours1 = { 0, 5, 6, 8, 10, 12 }; REQUIRE(neighboursList[1] == neighbours1); } TEST_CASE("Margulis", "[mapping]") { metric::Margulis margulis5(5); REQUIRE_FALSE(margulis5.isValid()); metric::Margulis margulis25(25); REQUIRE(margulis25.isValid()); auto neighboursList = margulis25.getNeighbours(7, 1); for (auto& neighbours : neighboursList) { std::sort(neighbours.begin(), neighbours.end()); } std::vector<size_t> neighbours0 = { 7 }; REQUIRE(neighboursList[0] == neighbours0); std::vector<size_t> neighbours1 = { 2, 5, 9, 12 }; REQUIRE(neighboursList[1] == neighbours1); }
5,407
C++
.cpp
149
31.993289
71
0.656676
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,585
Connected_Components_Test.cpp
metric-space-ai_metric/tests/correlation_tests/Connected_Components_Test.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <iostream> #include <vector> #include <string> #include "../details/connected-components.hpp" template <typename T> class Matrix { std::vector<std::vector<T>> mx; public: using ElementType = T; Matrix() {} Matrix(size_t size) : mx(size, std::vector<T>(size)) { } Matrix(size_t size, size_t, bool) : mx(size, std::vector<T>(size)) { } typedef T value_type; size_t size() const { return mx.size(); } void resize(size_t new_size, size_t) { mx.resize(new_size); for (auto& v : mx) { v.resize(new_size); } } size_t rows() const { return mx.size(); } std::vector<T>& operator[](const size_t row) { return mx[row]; } std::vector<T> operator[](const size_t row) const { return mx[row]; } T& operator()(size_t row, size_t col) { return mx[row][col]; } T operator()(size_t row, size_t col) const { return mx[row][col]; } void reset() { for (auto& e : mx) e.assign(size(), 0); } void resize(size_t size) { std::vector<T> v(size); mx.resize(size, v); } }; template <typename Ty> void MatrixTest(Matrix<Ty> M) { for (size_t i = 0; i < M.size(); ++i) { for (size_t j = 0; j < M.size(); ++j) if (M[i][j]) std::cout << i << "," << j << " "; else std::cout << " "; std::cout << std::endl; } std::cout << std::endl << "===========================================" << std::endl; } int main() { std::vector<Matrix<unsigned>> res; Matrix<unsigned> M(9); std::cout << "Empty graph (with 0 vertices):" << std::endl; res = connected_components(M, 0); for (auto mx : res) MatrixTest(mx); M[0][1] = M[1][0] = M[1][2] = M[2][1] = M[2][4] = M[4][2] = M[4][3] = M[3][4] = M[2][5] = M[5][2] = M[3][6] = M[6][3] = M[7][3] = M[3][7] = M[7][8] = M[8][7] = M[5][8] = M[8][5] = true; res = connected_components(M, 0); std::cout << "Full graph:" << std::endl; for (auto mx : res) MatrixTest(mx); std::cout << std::endl << std::endl; //We will disconnect (2, 4) and (5, 8) pairs of vertices //the graph will fall to two components M[4][2] = M[2][4] = M[5][8] = M[8][5] = false; res = connected_components(M, 0); std::cout << "Splitted graph:" << std::endl; for (auto mx : res) MatrixTest(mx); std::cout << std::endl << std::endl; // Disconnect (3, 7) M[3][7] = M[7][3] = false; std::cout << "3 component graph:" << std::endl; res = connected_components(M, 0); for (auto mx : res) MatrixTest(mx); std::cout << std::endl << std::endl; //// Disconnect (7, 8) std::cout << "4 component graph (with 2 null graphs (with 1 vertix)):" << std::endl; M[7][8] = M[8][7] = false; res = connected_components(M, 0); for (auto mx : res) MatrixTest(mx); std::cout << std::endl << std::endl; }
3,263
C++
.cpp
100
26.81
111
0.52992
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,586
correlation_tests.cpp
metric-space-ai_metric/tests/correlation_tests/correlation_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <algorithm> #include <deque> #include "metric/correlation.hpp" #include "metric/distance.hpp" #include "metric/utils/graph/connected_components.hpp" #include <random> #include <limits> using namespace Catch::literals; template <typename T> std::vector<T> generateVector(size_t size) { std::default_random_engine g; std::normal_distribution<T> nd(0, 1); std::vector<T> vector(size); for (auto i = 0; i < size; ++i) { vector[i] = nd(g); } return vector; } template <typename T> std::vector<std::vector<T>> generateMatrix(size_t rows, size_t cols) { std::vector<std::vector<T>> matrix; matrix.reserve(rows); for (auto i = 0; i < rows; ++i) { matrix.emplace_back(generateVector<T>(cols)); } return matrix; } template <typename T> blaze::DynamicMatrix<T> generateRandomMatrix(size_t rows, size_t cols) { std::default_random_engine g; std::normal_distribution<T> nd(0, 1); blaze::DynamicMatrix<T> matrix(rows, cols); for (auto i = 0; i < rows; ++i) { for (auto j = 0; j < cols; ++j) { matrix(i, j) = nd(g); } } return matrix; } TEMPLATE_TEST_CASE("Metrics", "correlation", float, double) { auto vectorNull = generateVector<TestType>(0); metric::Euclidean<TestType> Euclidean; metric::Manhatten<TestType> manhatten; REQUIRE(Euclidean(vectorNull, vectorNull) == 0); REQUIRE(manhatten(vectorNull, vectorNull) == 0); std::vector<TestType> vector0(1000, 0); REQUIRE(Euclidean(vector0, vector0) == 0); REQUIRE(manhatten(vector0, vector0) == 0); auto vector = generateVector<TestType>(2000); REQUIRE(Euclidean(vector, vector) == 0); REQUIRE(manhatten(vector, vector) == 0); } TEST_CASE("CRACKER") { blaze::DynamicMatrix<bool> M(10, 10, true); blaze::row(M, 2) = false; auto result = metric::graph::largest_connected_component(M)[0]; } //TEST_CASE("MGC") //{ // // double t = 1e-13; // std::vector<std::vector<double>> A1 = { { -1.08661677587398 }, { -1.00699896410939 }, { -0.814135753976830 }, // { -0.875364720432552 }, { -0.659607023272462 }, { -0.798949992922930 }, { -0.431585448024267 }, // { -0.619123703544758 }, { -0.351492263653510 }, { -0.394814371972061 }, { -0.309693618374598 }, // { -0.352009525808777 }, { -0.0803413535982411 }, { 0.0103940699342647 }, { -0.130735385695596 }, // { -0.138214899507693 }, { 0.0279270082022143 }, { 0.141670765995995 }, { 0.112221224566625 }, // { 0.376767573021755 }, { 0.186729429735154 }, { 0.597349318463320 }, { 0.451380104139401 }, // { 0.639237742050564 }, { 0.797420868050314 }, { 0.690091614630087 }, { 0.921722674141222 }, // { 0.852593762434809 }, { 0.954771723842945 }, { 1.03297970279357 } }; // // std::deque<std::array<float, 1>> B1 = { { 2.70625143351230 }, { 1.41259513494005 }, { 0.666086793692617 }, // { 0.647856446084279 }, { 0.887764969338737 }, { 0.286220905202707 }, { 0.543682026943014 }, // { 0.0402339224257120 }, { 0.105812168910424 }, { 0.0230915137205610 }, { 0.00298976085950325 }, // { 0.00366997150982423 }, { 0.000384825484363474 }, { 7.27293780465119e-05 }, { 2.50809340229209e-07 }, // { 0.00306636655437742 }, { 0.000456283181338950 }, { 0.00801756105329616 }, { 1.17238339150888e-09 }, // { 0.0803830108071682 }, { 0.0774478107095828 }, { 0.0474847202878941 }, { 0.0818772460512609 }, // { 0.486406609209630 }, { 0.197547677770060 }, { 0.628321368933714 }, { 1.02400551043736 }, // { 0.552591658802459 }, { 1.52144482984914 }, { 3.43908991254968 } }; // // typedef std::vector<double> Rec1; // typedef std::array<float, 1> Rec2; // // typedef metric::Euclidean<double> Met1; // typedef metric::Manhatten<float> Met2; // // auto mgc_corr = metric::MGC<Rec1, Met1, Rec2, Met2>(); // // auto result = mgc_corr(A1, B1); // REQUIRE(result == 0.28845660296530595_a); // // auto mgc_corr2 = metric::MGC<Rec2, Met2, Rec1, Met1>(); // result = mgc_corr2(B1, A1); // REQUIRE(result == 0.28845660296530595_a); // // typedef std::vector<double> Rec; // auto mgc = metric::MGC<Rec, Met1, Rec, Met1>(); // auto m1 = generateMatrix<double>(4, 4); // auto m2 = generateMatrix<double>(4, 4); // mgc(m1, m2); // // /* MGC corelation */ // std::vector<double> correlationReference = {0.3406052387919164, // 0.36670317239506234, // 0.35527060120466675, // 0.3899053662191226, // 0.2884565911772877, // 0.37837904236096026, // 0.4589100366738734, // 0.3811881240213428, // 0.44657481646290537}; // auto correlation = mgc.xcorr(A1, B1, 4); // // REQUIRE(correlationReference.size() == correlation.size()); // // for (auto i = 0; i < correlation.size(); ++i) { // REQUIRE(correlationReference[i] == Approx(correlation[i])); // } //} TEMPLATE_TEST_CASE("MGC_Estimate", "[correlation]", float, double) { const int dataSize = 1e6; std::default_random_engine generator; std::normal_distribution<TestType> normal(0, 0.5); std::vector<std::vector<TestType>> dataX; std::vector<std::vector<TestType>> dataY; std::uniform_real_distribution<TestType> uniform(0, 1); for (auto i = 0; i < dataSize; ++i) { TestType x1 = uniform(generator); TestType x2 = uniform(generator); TestType x3 = uniform(generator); TestType y1 = x1 * x1 + normal(generator); TestType y2 = x2 * x2 * x2 + normal(generator); TestType y3 = std::pow(x3, 0.5) + normal(generator); dataX.emplace_back(std::initializer_list<TestType> { x1, x2, x3 }); dataY.emplace_back(std::initializer_list<TestType> { y1, y2, y3 }); } typedef std::vector<TestType> Rec; typedef metric::Euclidean<TestType> Met; auto mgc = metric::MGC<Rec, Met, Rec, Met>(); auto result = mgc.estimate(dataX, dataY); } TEST_CASE("MGC_construct") { // this test has no asserts, it just checks compilation metric::MGC<float, metric::Euclidean<float>, float, metric::Euclidean<float>> m1; m1(std::vector<float>{}, std::vector<float>{}); metric::MGC<float, metric::Euclidean<float>, std::vector<float>, metric::Manhatten<float>> m2; m2(std::vector<float> {}, std::vector<std::vector<float>> {}); metric::MGC<float, metric::Euclidean<float>, std::vector<double>, metric::Manhatten<double>> m3; m3(std::vector<float> {}, std::vector<std::vector<double>> {}); metric::MGC<float, metric::Euclidean<float>, std::vector<double>, metric::Manhatten<double>> m4(metric::Euclidean<float>{}); m4(std::vector<float> {}, std::vector<std::vector<double>> {}); metric::MGC<float, metric::Euclidean<float>, std::vector<double>, metric::Manhatten<double>> m5(metric::Euclidean<float> {}, metric::Manhatten<double>{}); m5(std::vector<float> {}, std::vector<std::vector<double>> {}); metric::Euclidean<float> e1; metric::Manhatten<double> e2; metric::MGC<float, metric::Euclidean<float>, std::vector<double>, metric::Manhatten<double>> m6(e1, e2); m6(std::vector<float> {}, std::vector<std::vector<double>> {}); std::function<float(float, float)> f1 = [](float, float) ->float {return 0;}; using func_metric = std::function<float(float, float)>; metric::MGC<float, func_metric, float, func_metric> m7(f1, f1); m7(std::vector<float>{}, std::vector<float>{}); }
7,833
C++
.cpp
178
40.185393
115
0.638918
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,587
reimannian_test.cpp
metric-space-ai_metric/tests/distance_tests/reimannian_test.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <catch2/catch.hpp> #include "metric/distance/d-spaced/Riemannian.hpp" #include <numeric> #include <stdexcept> using namespace Catch::literals; TEMPLATE_TEST_CASE("riemannian_distance", "[distance]", double) { blaze::DynamicMatrix<TestType> A {{1, 2, -3}, {0, -2, 4}, {0, 0, 1}}; blaze::DynamicMatrix<TestType> B {{2, 0, -2}, {0, 1, 1}, {0, 0, 1}}; auto rd = metric::RiemannianDistance<void, metric::Euclidean<TestType>>(); REQUIRE(rd.matDistance(A, B) == 0.6931471805599455_a); std::vector<std::vector<TestType>> ds1 {{0, 1}, {0, 0}, {1, 1}, {1, 0}}; std::vector<std::vector<TestType>> ds2 {{0, 0}, {1, 1}, {2, 2}, {2, 1}}; REQUIRE(rd(ds1, ds2) == 0.8086438137089399_a); REQUIRE(rd.estimate(ds1, ds2, 2) == 0.0_a); // TODO run with larger dataset }
1,031
C++
.cpp
21
46
79
0.663327
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,588
cramervon_mises_tests.cpp
metric-space-ai_metric/tests/distance_tests/cramervon_mises_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <catch2/catch.hpp> #include <vector> #include "metric/distance.hpp" using namespace Catch::literals; TEMPLATE_TEST_CASE("cramervon_basic_use", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; metric::CramervonMises<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 5.0; // 5.0 % REQUIRE(result == 0.305698_a); } TEMPLATE_TEST_CASE("cramervon_different_dimensions", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 1, 2, 3, 4, 5 }; metric::CramervonMises<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 5.0; // 5.0% REQUIRE(result == 0.81095_a); } TEMPLATE_TEST_CASE("cramervon_ks_equal_samples", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; metric::CramervonMises<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //double t = 0.01; // 0.01% REQUIRE(result == 0.0_a); } TEMPLATE_TEST_CASE("cramervon_ks_single_value_distribution", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 0, 0, 0, 0, 0, 0 }; std::vector<TestType> samples_2 = { 1, 1, 1, 1, 1, 1, 1 }; metric::CramervonMises<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); REQUIRE(std::isnan(result)); } TEMPLATE_TEST_CASE("cramervon_ks_non_intersect_distribution", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 1, 2, 2, 2, 2, 1, 1, 0 }; std::vector<TestType> samples_2 = { 10, 11, 11, 12, 12, 12, 12, 11, 11, 10 }; metric::CramervonMises<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); REQUIRE(std::isnan(result)); }
2,367
C++
.cpp
53
41.264151
90
0.660203
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,589
kolmogorov_smirnov_tests.cpp
metric-space-ai_metric/tests/distance_tests/kolmogorov_smirnov_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <catch2/catch.hpp> #include <vector> #include "metric/distance.hpp" TEMPLATE_TEST_CASE("Kolmogorov_Smirnov_basic_use", "[distance][kolmogorov-smirnov]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; metric::KolmogorovSmirnov<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 10.0; // 10.0% REQUIRE(result == Approx(0.1).margin(0.01)); } TEMPLATE_TEST_CASE("ks_different_dimensions", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 1, 2, 3, 4, 5 }; metric::KolmogorovSmirnov<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 10.0; // 10.0% REQUIRE(result == Approx(0.45).margin(0.045)); } TEMPLATE_TEST_CASE("ks_equal_samples", "[distance]", float, double) { /*** here are some data records ***/ std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; metric::KolmogorovSmirnov<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 0.01; // 0.01% REQUIRE(result == Approx(0.0)); } TEMPLATE_TEST_CASE("ks_single_value_distribution", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 0, 0, 0, 0, 0, 0 }; std::vector<TestType> samples_2 = { 1, 1, 1, 1, 1, 1, 1 }; metric::KolmogorovSmirnov<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); TestType t = 0.01; // 0.01% REQUIRE(result == Approx(0.0)); } TEMPLATE_TEST_CASE("ks_non_intersect_distribution", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 1, 2, 2, 2, 2, 1, 1, 0 }; std::vector<TestType> samples_2 = { 10, 11, 11, 12, 12, 12, 12, 11, 11, 10 }; metric::KolmogorovSmirnov<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); TestType t = 0.01; // 0.01% REQUIRE(result == Approx(0.0)); }
2,486
C++
.cpp
55
41.8
99
0.654782
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,590
entropy_vmixing_tests.cpp
metric-space-ai_metric/tests/distance_tests/entropy_vmixing_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <catch2/catch.hpp> #include "metric/utils/type_traits.hpp" #include "metric/correlation/entropy.hpp" #include "metric/distance/k-structured/Edit.hpp" #include "metric/distance/k-related/Standards.hpp" #include <vector> #include <deque> #include <array> #include <stdexcept> using namespace Catch::literals; TEMPLATE_TEST_CASE("entropy", "[distance]", float, double) { std::vector<std::vector<TestType>> v1 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 1 } }; std::vector<std::deque<TestType>> v2 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 1 } }; std::vector<std::array<TestType,2>> v3 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 1 } }; std::deque<std::vector<TestType>> v4 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 1 } }; std::deque<std::array<TestType, 2>> v7 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 1 } }; using Metric = metric::Chebyshev<TestType>; REQUIRE(metric::Entropy<void, Metric>(Metric(), 3, 2)(v1) == -4.4489104772539489_a); //-4.4489104772539374)); REQUIRE(metric::Entropy<void, Metric>(Metric(), 3, 2)(v2) == -4.4489104772539489_a); REQUIRE(metric::Entropy<void, Metric>(Metric(), 3, 2)(v3) == -4.4489104772539489_a); REQUIRE(metric::Entropy<void, Metric>(Metric(), 3, 2)(v4) == -4.4489104772539489_a); REQUIRE(metric::Entropy<void, Metric>(Metric(), 3, 2)(v7) == -4.4489104772539489_a); REQUIRE(metric::Entropy<void, Metric>()(v1) == -5.3989104772539491_a); //-5.3989104772539376)); // -5.3989104772539225)); REQUIRE(metric::Entropy<void, Metric>()(v2) == -5.3989104772539491_a); REQUIRE(metric::Entropy<void, Metric>()(v3) == -5.3989104772539491_a); REQUIRE(metric::Entropy<void, Metric>()(v4) == -5.3989104772539491_a); REQUIRE(metric::Entropy<void, Metric>()(v7) == -5.3989104772539491_a); std::vector<std::string> v8 = { "AAA", "HJGJHFG", "BBB", "AAAA", "long long long long long long string", "abcdefghjklmnopqrstuvwxyz" }; REQUIRE(metric::Entropy<void, metric::Edit<int>>(metric::Edit<int>(), 3, 2.0)(v8) == -9.3586210470159283_a); //0.58333333333333337)); } TEMPLATE_TEST_CASE("vmixing", "[distance]", float, double) { std::vector<std::vector<TestType>> v11 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 0 } }; std::vector<std::vector<TestType>> v12 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 1, 0 } }; auto vms = metric::VMixing_simple<void, metric::Euclidean<TestType>>(); auto vm = metric::VMixing<void, metric::Euclidean<TestType>>(); //REQUIRE(Approx(metric::VOI_simple(v11, v12), 11.813781191217037)); REQUIRE(vms(v11, v12) == 0.0608977897890286_a); //0.75859128220107586)); //REQUIRE(Approx(metric::VOI(v11, v12), -34.913083805038639)); //-31.36271401282686)); REQUIRE(vm(v11, v12) == 11.821247820859364_a); //12.70867240400823)); //12.708672404008247)); std::vector<std::deque<TestType>> v21 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 0 } }; std::vector<std::deque<TestType>> v22 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 1, 0 } }; //REQUIRE(Approx(metric::VOI_simple(v21, v22), 11.813781191217037)); REQUIRE(vms(v21, v22) == 0.0608977897890286_a); //REQUIRE(Approx(metric::VOI(v21, v22), -34.913083805038639)); REQUIRE(vm(v21, v22) == 11.821247820859364_a); std::deque<std::vector<TestType>> v31 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 0 } }; std::deque<std::vector<TestType>> v32 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 1, 0 } }; //REQUIRE(Approx(metric::VOI_simple(v31, v32), 11.813781191217037)); REQUIRE(vms(v31, v32) == 0.0608977897890286_a); //REQUIRE(Approx(metric::VOI(v31, v32), -34.913083805038639)); REQUIRE(vm(v31, v32) == 11.821247820859364_a); std::deque<std::deque<TestType>> v41 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 5, 0 } }; std::deque<std::deque<TestType>> v42 = { { 5, 5 }, { 2, 2 }, { 3, 3 }, { 1, 0 } }; //REQUIRE(Approx(metric::VOI_simple(v41, v42), 11.813781191217037)); REQUIRE(vms(v41, v42) == 0.0608977897890286_a); //REQUIRE(Approx(metric::VOI(v41, v42), -34.913083805038639)); REQUIRE(vm(v41, v42) == 11.821247820859364_a); std::vector<std::vector<TestType>> ds1 {{0, 1, 0}, {0, 0, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}, {0, 1, 0}, {0, 0, 0}, {1, 1, 0}, {1, 0, 0}, {0, 0, 0}}; std::vector<std::vector<TestType>> ds2 {{0, 0, 0}, {1, 1, 0}, {2, 2, 0}, {2, 1, 0}, {0, 0, 0}, {0, 0, 0}, {1, 1, 0}, {2, 2, 0}, {2, 1, 0}, {0, 0, 0}, {0, 0, 0}, {1, 1, 0}, {2, 2, 0}, {2, 1, 0}, {0, 0, 0}}; REQUIRE(vms.estimate(ds1, ds2, 5) == 1.2722224834467826_a); REQUIRE(vm.estimate(ds1, ds2, 5) == 0.096469697241235622_a); }
4,991
C++
.cpp
74
61.310811
139
0.57668
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,591
random_emd_tests.cpp
metric-space-ai_metric/tests/distance_tests/random_emd_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <catch2/catch.hpp> #include <vector> #include "metric/distance.hpp" using namespace Catch::literals; TEMPLATE_TEST_CASE("basic_use", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 }; metric::RandomEMD<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 1.0; // 1.0% REQUIRE(result == Approx(1.0).margin(0.01)); } TEMPLATE_TEST_CASE("different_dimensions", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 1, 2, 3, 4, 5 }; metric::RandomEMD<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 1.0; // 1.0% REQUIRE(result == Approx(2.18).margin(0.0218)); } TEMPLATE_TEST_CASE("equal_samples", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; std::vector<TestType> samples_2 = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; metric::RandomEMD<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); //TestType t = 0.01; // 0.01% REQUIRE(result == Approx(0.0)); } TEMPLATE_TEST_CASE("single_value_distribution", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 0, 0, 0, 0, 0, 0 }; std::vector<TestType> samples_2 = { 1, 1, 1, 1, 1, 1, 1 }; metric::RandomEMD<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); REQUIRE(std::isnan(result)); } TEMPLATE_TEST_CASE("non_intersect_distribution", "[distance]", float, double) { std::vector<TestType> samples_1 = { 0, 1, 1, 2, 2, 2, 2, 1, 1, 0 }; std::vector<TestType> samples_2 = { 10, 11, 11, 12, 12, 12, 12, 11, 11, 10 }; metric::RandomEMD<std::vector<TestType>, TestType> distance; auto result = distance(samples_1, samples_2); REQUIRE(std::isnan(result)); }
2,326
C++
.cpp
53
40.396226
78
0.652722
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,592
kohonen_distance_tests.cpp
metric-space-ai_metric/tests/distance_tests/kohonen_distance_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <iostream> #include <vector> #include "metric/distance.hpp" #include <chrono> TEMPLATE_TEST_CASE("kohonen", "[distance]", float, double) { size_t grid_w = 3; size_t grid_h = 2; using Record = std::vector<TestType>; std::vector<Record> train_dataset = { {0, 0}, {1, 0}, {2, 0}, {0, 1}, {1, 1}, {2, 1}, }; metric::Kohonen<TestType, Record> distance_1(train_dataset, grid_w, grid_h); TestType result; result = distance_1(train_dataset[0], train_dataset[1]); REQUIRE(result > 0); REQUIRE(result < 2); result = distance_1(train_dataset[0], train_dataset[2]); REQUIRE(result > 1); REQUIRE(result < 3); result = distance_1(train_dataset[0], train_dataset[3]); REQUIRE(result > 0); REQUIRE(result < 2); result = distance_1(train_dataset[0], train_dataset[4]); REQUIRE(result > 1); REQUIRE(result < 3); result = distance_1(train_dataset[0], train_dataset[5]); REQUIRE(result > 2); REQUIRE(result < 4); using Vector = std::vector<TestType>; using Metric = metric::Euclidean<TestType>; using Graph = metric::Grid6; using Distribution = std::uniform_real_distribution<TestType>; Distribution distr(-1, 1); metric::SOM<Vector, Graph, Metric> som_model(Graph(grid_w, grid_h), Metric(), 0.8, 0.2, 50, distr); som_model.train(train_dataset); metric::Kohonen<TestType, Vector, Graph, Metric> distance_2(som_model, train_dataset); result = distance_2(train_dataset[0], train_dataset[1]); REQUIRE(result > 0); REQUIRE(result < 2); result = distance_2(train_dataset[0], train_dataset[2]); REQUIRE(result > 1); // TODO(victor1234): figure out with this check // REQUIRE(result < 3); result = distance_2(train_dataset[0], train_dataset[3]); REQUIRE(result > 0); REQUIRE(result < 2); result = distance_2(train_dataset[0], train_dataset[4]); REQUIRE(result > 1); REQUIRE(result < 3); result = distance_2(train_dataset[0], train_dataset[5]); REQUIRE(result > 2); REQUIRE(result < 4); metric::SOM<Vector, Graph, Metric> negative_som_model(Graph(grid_w, grid_h), Metric(), -0.8, -0.2, 50, distr); negative_som_model.train(train_dataset); metric::Kohonen<TestType, Vector, Graph, Metric> distance_3(negative_som_model, train_dataset); result = distance_3(train_dataset[0], train_dataset[1]); REQUIRE(result > 0); REQUIRE(result < 2); result = distance_3(train_dataset[0], train_dataset[2]); REQUIRE(result > 1); REQUIRE(result < 3); result = distance_3(train_dataset[0], train_dataset[3]); REQUIRE(result > 0); REQUIRE(result < 2); result = distance_3(train_dataset[0], train_dataset[4]); REQUIRE(result > 1); REQUIRE(result < 3); result = distance_3(train_dataset[0], train_dataset[5]); REQUIRE(result > 2); REQUIRE(result < 4); }
2,957
C++
.cpp
79
35.329114
111
0.707385
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,593
knn_graph_tests.cpp
metric-space-ai_metric/tests/space_tests/knn_graph_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include <iostream> #include <metric/space/knn_graph.hpp> #include <metric/distance.hpp> TEMPLATE_TEST_CASE("valid_params", "[space]", float, double) { size_t neighbors_num = 3; size_t search_neighbors_num = 3; std::vector<std::vector<TestType>> table = { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 3 }, { 5, 3 }, { 4, 6 }, { 5, 1 }, { 4, 1 }, { 3, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 6, 6 }, { 7, 6 }, { 0, 2 }, { 0, 9 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, }; std::vector<TestType> query = { 7, 7 }; // valid params auto g_0 = metric::KNNGraph<std::vector<TestType>, metric::Euclidean<TestType>>(table, neighbors_num, 2.5 * neighbors_num); // nothing bad should be happened INFO(" --- valid params --- "); //std::cout << "gnn search:" << std::endl; auto found_0 = g_0.gnnn_search(query, search_neighbors_num); std::sort(found_0.begin(), found_0.end()); CAPTURE(found_0); REQUIRE(found_0.size() == 3); // we expect only 4, 5, 6, 13 or 14 nodes (depends on random) // BOOST_TEST((found_0[0] == 4 || found_0[0] == 5 || found_0[0] == 6 || found_0[0] == 13 || found_0[0] == 14 )); // BOOST_TEST((found_0[1] == 4 || found_0[1] == 5 || found_0[1] == 6 || found_0[1] == 13 || found_0[1] == 14 )); // BOOST_TEST((found_0[2] == 4 || found_0[2] == 5 || found_0[2] == 6 || found_0[2] == 13 || found_0[2] == 14 )); } /*TEMPLATE_TEST_CASE("negaive parameters", "[space]", float, double) { size_t neighbors_num = 3; size_t search_neighbors_num = 3; std::vector<std::vector<TestType>> table = { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 3 }, { 5, 3 }, { 4, 6 }, { 5, 1 }, { 4, 1 }, { 3, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 6, 6 }, { 7, 6 }, { 0, 2 }, { 0, 9 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, }; std::vector<TestType> query = { 7, 7 }; /// // negative params auto g_1 = metric::KNNGraph<std::vector<TestType>, metric::Euclidean<TestType>>(table, -neighbors_num, -2.5 * neighbors_num); // nothing bad should be happened //std::cout << " --- negative params --- " << std::endl; //std::cout << "gnn search:" << std::endl; auto found_1 = g_1.gnnn_search(query, search_neighbors_num); //print_vector(found_1); REQUIRE(found_1.size() > 0); // negative params for search, nothing bad should be happened too //std::cout << "gnn search:" << std::endl; found_1 = g_1.gnnn_search(query, -search_neighbors_num, -10); //print_vector(found_1); REQUIRE(found_1.size() == 0); } */ TEMPLATE_TEST_CASE("params more than nodes exists", "[space]", float, double) { size_t neighbors_num = 3; size_t search_neighbors_num = 3; std::vector<std::vector<TestType>> table = { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 3 }, { 5, 3 }, { 4, 6 }, { 5, 1 }, { 4, 1 }, { 3, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 6, 6 }, { 7, 6 }, { 0, 2 }, { 0, 9 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, }; std::vector<TestType> query = { 7, 7 }; /// // params more than nodes exists auto g_2 = metric::KNNGraph<std::vector<TestType>, metric::Euclidean<TestType>>(table, table.size() * 10, table.size() * 25); // nothing bad should be happened //std::cout << " --- params more than nodes exists --- " << std::endl; //std::cout << "gnn search:" << std::endl; auto found_2 = g_2.gnnn_search(query, table.size() * 50); //print_vector(found_2); REQUIRE(found_2.size() > 0); } TEMPLATE_TEST_CASE("empty dataset", "[space]", float, double) { size_t neighbors_num = 3; size_t search_neighbors_num = 3; std::vector<std::vector<TestType>> table = { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 3 }, { 5, 3 }, { 4, 6 }, { 5, 1 }, { 4, 1 }, { 3, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 6, 6 }, { 7, 6 }, { 0, 2 }, { 0, 9 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, }; std::vector<TestType> query = { 7, 7 }; /// // empty dataset auto g_3 = metric::KNNGraph<std::vector<TestType>, metric::Euclidean<TestType>>(std::vector<std::vector<TestType>>{}, neighbors_num, 2.5 * neighbors_num); // nothing bad should be happened //std::cout << " --- empty dataset --- " << std::endl; //std::cout << "gnn search:" << std::endl; auto found_3 = g_3.gnnn_search(query, search_neighbors_num); //print_vector(found_3); REQUIRE(found_3.size() == 0); } TEMPLATE_TEST_CASE("dataset and query with different dimensions", "[space]", float, double) { size_t neighbors_num = 3; size_t search_neighbors_num = 3; std::vector<std::vector<TestType>> table = { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 3 }, { 5, 3 }, { 4, 6 }, { 5, 1 }, { 4, 1 }, { 3, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 6, 6 }, { 7, 6 }, { 0, 2 }, { 0, 9 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, }; std::vector<TestType> query = { 7, 7 }; /// // dataset and query with different dimensions auto g_4 = metric::KNNGraph<std::vector<TestType>, metric::Euclidean<TestType>>(std::vector<std::vector<TestType>>{}, neighbors_num, 2.5 * neighbors_num); // nothing bad should be happened //std::cout << " --- dataset and query with different dimensions --- " << std::endl; //std::cout << "gnn search:" << std::endl; auto found_4 = g_4.gnnn_search({ 7, 5, 7, 5, 7, 5, 7, 5 }, search_neighbors_num); //print_vector(found_4); REQUIRE(found_4.size() == 0); } TEMPLATE_TEST_CASE("copy constructor", "[space]", float, double) { size_t neighbors_num = 3; size_t search_neighbors_num = 3; std::vector<std::vector<TestType>> table = { { 0, 1 }, { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 3 }, { 5, 3 }, { 4, 6 }, { 5, 1 }, { 4, 1 }, { 3, 2 }, { 0, 3 }, { 1, 3 }, { 2, 3 }, { 6, 6 }, { 7, 6 }, { 0, 2 }, { 0, 9 }, { 0, 4 }, { 0, 5 }, { 0, 6 }, { 0, 7 }, { 0, 8 }, }; std::vector<TestType> query = { 7, 7 }; /// // auto g_0 = metric::KNNGraph<std::vector<TestType>, metric::Euclidean<TestType>>(table, neighbors_num, 2.5 * neighbors_num); auto found_0 = g_0.gnnn_search(query, search_neighbors_num); auto g_5 = metric::KNNGraph(g_0); // nothing bad should be happened //std::cout << " --- nothing bad should be happened --- " << std::endl; //std::cout << "gnn search:" << std::endl; auto found_5 = g_5.gnnn_search(query, search_neighbors_num); //print_vector(found_5); REQUIRE(found_5.size() == found_0.size()); } TEMPLATE_TEST_CASE("knn graph insert", "[space]", float, double) { std::vector<TestType> data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; auto graph = metric::KNNGraph<TestType, metric::Euclidean<TestType>>(data, 3, 8); auto nn = graph.gnnn_search(1.1, 1); REQUIRE(nn.size() == 1); auto nn1 = graph.gnnn_search(1.8, 2); REQUIRE(nn1.size() == 2); auto id1 = graph.insert(2.1); REQUIRE(id1 == 10); REQUIRE(graph.size() == id1+1); REQUIRE(graph[id1] == TestType(2.1)); auto nn2 = graph.gnnn_search(1.8, 1); REQUIRE(nn2.size() == 1); std::vector<TestType> data2 = {4.3, 5.1, 6.2}; auto id2 = graph.insert(data2); //BOOST_TEST(id2 == (std::vector<std::size_t>{11,12,13}), boost::test_tools::per_element()); REQUIRE(id2 == std::vector<std::size_t>{11, 12, 13}); auto id3 = graph.insert_if(11, 100); REQUIRE(id3.first == 0); REQUIRE(id3.second == false); std::vector<TestType> data3 = { 11.5, 14, 10.02}; auto id4 = graph.insert_if(data3, 1000.5); //BOOST_TEST(id4 == (std::vector<std::pair<std::size_t, bool>>{{0, false}, {0, false}, {0, false}}), boost::test_tools::per_element()); REQUIRE(id4 == std::vector<std::pair<size_t, bool>>{{0, false}, {0, false}, {0, false}}); }
8,894
C++
.cpp
282
25.400709
155
0.497365
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,594
space_tree_tests.cpp
metric-space-ai_metric/tests/space_tests/space_tree_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Panda Team */ #include <catch2/catch.hpp> #include <stdexcept> #include <vector> #include "metric/space.hpp" using namespace Catch::literals; template <typename T, typename V=int> struct distance { V operator()(const T& lhs, const T& rhs) const { return std::abs(lhs - rhs); } }; TEST_CASE("test_insert", "[space]") { std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; metric::Tree<int, distance<int>> tree; for (auto t : data) { tree.insert(t); REQUIRE(tree.check_covering()); } } TEST_CASE("test_insert_batch", "[space]") { std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; metric::Tree<int, distance<int>> tree; tree.insert(data); REQUIRE(tree.check_covering()); } TEST_CASE("test_nn", "[space]") { std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; metric::Tree<int, distance<int>> tree; tree.insert(data); //tree.print(); REQUIRE(tree.nn(200)->get_data() == 200); } TEST_CASE("test_knn", "[space]") { std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; metric::Tree<int, distance<int>> tree; tree.insert(data); auto k1 = tree.knn(3, 15); REQUIRE(k1.size() == 7); REQUIRE(k1[0].first->get_data() == 3); REQUIRE(k1[1].first->get_data() == 1); REQUIRE(k1[2].first->get_data() == 5); REQUIRE(k1[3].first->get_data() == -10); REQUIRE(k1[4].first->get_data() == 50); REQUIRE(k1[5].first->get_data() == 200); REQUIRE(k1[6].first->get_data() == -200); } TEST_CASE("test_erase", "[space]") { std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; metric::Tree<int, distance<int>> tree; tree.insert(data); auto tree_size = tree.size(); REQUIRE(tree_size == data.size()); REQUIRE(tree.check_covering()); for (auto d : data) { tree.erase(d); REQUIRE(tree_size -1 == tree.size()); tree_size = tree.size(); REQUIRE(tree.check_covering()); } REQUIRE(tree.size() == 0); REQUIRE(tree.empty() == true); } TEST_CASE("test_erase_root", "[space]") { std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; metric::Tree<int, distance<int>> tree; tree.insert(data); for (int i = 0; i < 7; i++) { auto root = tree.get_root(); tree.erase(root->get_data()); REQUIRE(tree.check_covering()); } } TEMPLATE_TEST_CASE("test_insert_if", "[space]", float, double) { metric::Tree<int, distance<int>> tree; tree.insert(1); REQUIRE_FALSE(std::get<1>(tree.insert_if(2, 10))); REQUIRE(std::get<1>(tree.insert_if(15, 10))); REQUIRE_FALSE(std::get<1>(tree.insert_if(14, 10))); REQUIRE(std::get<1>(tree.insert_if(26, 10))); } TEST_CASE("test_insert2", "[space]") { std::vector<int> data = { 7, 8, 9, 10, 11, 12, 13 }; metric::Tree<int, distance<int>> tree; tree.insert(data); //tree.print(); } TEST_CASE("test_to_json", "[space]") { metric::Tree<int, distance<int>> tree; tree.insert(1); auto s = tree.to_json(); std::string json1 = "{\n\"nodes\": [\n{ \"id\":0, \"values\":1}\n],\n\"edges\": [\n]}\n"; REQUIRE(s == json1); tree.insert(2); std::string json2 = "{\n\"nodes\": [\n{ \"id\":0, \"values\":1},\n{ \"id\":1, \"values\":2}\n],\n\"edges\": [\n{ " "\"source\":0, \"target\":1, \"distance\":1}\n]}\n"; REQUIRE(tree.to_json() == json2); } // TEMPLATE_TEST_CASE("test_serialize_boost_text", "[space]", float, double) // { // std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; // metric::Tree<int, distance<int>> tree; // tree.insert(data); // std::ostringstream os; // boost::archive::text_oarchive oar(os); // tree.serialize(oar); // metric::Tree<int, distance<int>> tree1; // std::istringstream is(os.str()); // boost::archive::text_iarchive iar(is); // tree1.deserialize(iar, is); // REQUIRE(tree1.check_covering()); // REQUIRE(tree1 == tree); // } // TEMPLATE_TEST_CASE("test_serialize_boost_binary", "[space]", float, double) // { // std::vector<int> data = { 3, 5, -10, 50, 1, -200, 200 }; // metric::Tree<int, distance<int>> tree; // tree.insert(data); // std::ostringstream os; // boost::archive::binary_oarchive oar(os); // tree.serialize(oar); // metric::Tree<int, distance<int>> tree1; // std::istringstream is(os.str()); // boost::archive::binary_iarchive iar(is); // tree1.deserialize(iar, is); // REQUIRE(tree1.check_covering()); // REQUIRE(tree1 == tree); // } /*struct Record { float v; std::vector<float> vv; int a; float operator-(const Record& r) const { return v - r.v; } bool operator!=(const Record rhs) const { return v != rhs.v || vv != rhs.vv || a != rhs.a; } template <typename Archive> void serialize(Archive& ar, const unsigned int) { ar& BOOST_SERIALIZATION_NVP(v) & BOOST_SERIALIZATION_NVP(vv) & BOOST_SERIALIZATION_NVP(a); } };*/ // TEMPLATE_TEST_CASE("test_serialize_boost_record_binary", "[space]", float, double) // { // std::vector<Record> data = { { 3.0f, { 1, 2, 3 }, 1 }, { 5.0f, { 1, 6, 3 }, 2 }, { -10.0f, { 1, 6, 3 }, 3 }, // { 50.0f, { 1, 6, 3 }, 4 }, { 1.0f, { 1, 6, 3 }, 5 }, { -200.0f, { 1, 6, 3 }, 6 }, { 200.0f, { 1, 6, 3 }, 7 } }; // metric::Tree<Record, distance<Record, float>> tree; // tree.insert(data); // std::ostringstream os; // boost::archive::binary_oarchive oar(os); // tree.serialize(oar); // metric::Tree<Record, distance<Record, float>> tree1; // std::istringstream is(os.str()); // boost::archive::binary_iarchive iar(is); // tree1.deserialize(iar, is); // REQUIRE(tree1.check_covering()); // REQUIRE(tree1 == tree); // } // TEMPLATE_TEST_CASE("test_serialize_boost_record_text", "[space]", float, double) // { // std::vector<Record> data = { { 3.0f, { 1, 2, 3 }, 1 }, { 5.0f, { 1, 6, 3 }, 2 }, { -10.0f, { 1, 6, 3 }, 3 }, // { 50.0f, { 1, 6, 3 }, 4 }, { 1.0f, { 1, 6, 3 }, 5 }, { -200.0f, { 1, 6, 3 }, 6 }, { 200.0f, { 1, 6, 3 }, 7 } }; // metric::Tree<Record, distance<Record, float>> tree; // tree.insert(data); // std::ostringstream os; // boost::archive::text_oarchive oar(os); // tree.serialize(oar); // metric::Tree<Record, distance<Record, float>> tree1; // std::istringstream is(os.str()); // boost::archive::text_iarchive iar(is); // tree1.deserialize(iar, is); // REQUIRE(tree1.check_covering()); // REQUIRE(tree1 == tree); // } // TEMPLATE_TEST_CASE("test_serialize_boost_record_xml", "[space]", float, double) // { // std::vector<Record> data = { { 3.0f, { 1, 2, 3 }, 1 }, { 5.0f, { 1, 6, 3 }, 2 }, { -10.0f, { 1, 6, 3 }, 3 }, // { 50.0f, { 1, 6, 3 }, 4 }, { 1.0f, { 1, 6, 3 }, 5 }, { -200.0f, { 1, 6, 3 }, 6 }, { 200.0f, { 1, 6, 3 }, 7 } }; // metric::Tree<Record, distance<Record>> tree; // tree.insert(data); // std::ostringstream os; // boost::archive::xml_oarchive oar(os); // tree.serialize(oar); // std::cout << os.str() << std::endl; // metric::Tree<Record, distance<Record>> tree1; // std::istringstream is(os.str()); // boost::archive::xml_iarchive iar(is); // tree1.deserialize(iar, is); // REQUIRE(tree1.check_covering()); // REQUIRE(tree1 == tree); // } TEST_CASE("cluster1", "[space]") { std::vector<int> data = { 7, 8, 9, 10, 11, 12, 13 }; metric::Tree<int, distance<int>> tree; tree.insert(data); //tree.print(); std::vector<double> distribution = { 0.1, 0.2, 0.3, 0.5 }; std::vector<std::size_t> IDS = { 1, 2, 3 }; std::vector<int> points = { 8, 9, 10 }; auto result = tree.clustering(distribution, IDS, data); auto result2 = tree.clustering(distribution, points); std::vector<std::vector<std::size_t>> test_result = { {}, { 1 }, { 0 }, { 2 } }; REQUIRE(result == test_result); REQUIRE(result2 == test_result); } TEST_CASE("cluster2", "[space]") { std::vector<int> data = { 7, 8, 9, 10, 11, 12, 13 }; metric::Tree<int, distance<int>> tree; tree.insert(data); //tree.print(); std::vector<double> distribution = { 0.1, 0.2, 0.3, 0.5 }; std::vector<std::size_t> IDS = { 3 }; std::vector<int> points = { 10 }; auto result = tree.clustering(distribution, IDS, data); auto result2 = tree.clustering(distribution, points); std::vector<std::vector<std::size_t>> test_result = { {}, { 3 }, { 4 }, { 2 } }; REQUIRE(result == test_result); REQUIRE(result2 == test_result); std::size_t i = 0; /* for (auto& v : result) { std::cout << distribution[i] << " = {"; for (auto p : v) { std::cout << "[" << data[p] << ":" << p << "]" << ", "; } std::cout << "}" << std::endl; i++; } */ } TEST_CASE("cluster3", "[space]") { std::vector<int> data = { 7, 8, 9, 10, 11, 12, 13 }; metric::Tree<int, distance<int>> tree; tree.insert(data); //tree.print(); std::vector<double> distribution = { 0.1, 0.2, 0.5, 0.9 }; std::vector<std::size_t> IDS = { 3 }; std::vector<int> points = { 10 }; auto result = tree.clustering(distribution, IDS, data); auto result2 = tree.clustering(distribution, points); std::vector<std::vector<std::size_t>> test_result = { {}, { 3 }, { 4, 2 }, { 1, 0, 5 } }; REQUIRE(result == test_result); REQUIRE(result2 == test_result); } TEST_CASE("cluster_exception_unsorted", "[space]") { std::vector<int> data = { 7, 8, 9, 10, 11, 12, 13 }; metric::Tree<int, distance<int>> tree; tree.insert(data); std::vector<double> distribution1 = { 0.9, 0.1, 0.2, 0.5, 0.9 }; std::vector<double> distribution2 = { 0.1, 0.2, 0.5, 0.9 }; std::vector<double> distribution3 = { 0.1, 0.2, 0.5, 0.9, 1, 5 }; std::vector<std::size_t> IDS = { 3 }; std::vector<int> points = { 10 }; REQUIRE_THROWS_AS(tree.clustering(distribution1, IDS, data), metric::unsorted_distribution_exception); REQUIRE_THROWS_AS(tree.clustering(distribution1, points), metric::unsorted_distribution_exception); REQUIRE_THROWS_AS(tree.clustering(distribution3, IDS, data), metric::bad_distribution_exception); REQUIRE_THROWS_AS(tree.clustering(distribution3, points), metric::bad_distribution_exception); REQUIRE_NOTHROW(tree.clustering(distribution2, IDS, data)); REQUIRE_NOTHROW(tree.clustering(distribution2, points)); } TEST_CASE("tree_element_access", "[space]") { metric::Tree<int, distance<int>> tree; std::vector<int> data = {1,2,3,4,5,6,7,8,9,10}; tree.insert(data); for(std::size_t i = 0; i < data.size(); i++) { REQUIRE(tree[i] == data[i]); } } TEMPLATE_TEST_CASE("tree_distance_by_id", "[space]", float, double) { std::vector<TestType> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; metric::Tree<TestType, distance<TestType, TestType>> tree; tree.insert(data); /* for(std::size_t id = 0; id < 10; id++) { std::cout << id << " -> " << tree[id] << std::endl; } */ // clang-format off /* (5) ├──(2) | ├──(0) | | └──(1) | └──(3) | └──(4) ├──(6) | └──(7) | └──(8) └──(9) */ // clang-format on // same ID for (std::size_t i = 0; i < 10; ++i) { REQUIRE(tree.distance_by_id(i, i) == 0); } //parent - child REQUIRE(tree.distance_by_id(5, 2) == 3); REQUIRE(tree.distance_by_id(2, 5) == 3); // same level, one parent REQUIRE(tree.distance_by_id(0, 3) == Approx((1.0 + 2.0) / 2)); REQUIRE(tree.distance_by_id(3, 0) == Approx((1.0 + 2.0) / 2)); // different levels, one subtree REQUIRE(tree.distance_by_id(1, 3) == Approx((1.0 + 2.0 + 1.0) / 3)); REQUIRE(tree.distance_by_id(3, 1) == Approx((1.0 + 2.0 + 1.0) / 3)); //same levels, different subtree REQUIRE(tree.distance_by_id(0, 7) == Approx((1. + 1. + 2. + 3.) / 4)); REQUIRE(tree.distance_by_id(7, 0) == Approx((1. + 1. + 2. + 3.) / 4)); //different levels, different subtree REQUIRE(tree.distance_by_id(1, 7) == Approx((1. + 1. + 1. + 2. + 3.) / 5)); REQUIRE(tree.distance_by_id(7, 1) == Approx((1. + 1. + 1. + 2. + 3.) / 5)); REQUIRE_THROWS_AS(tree.distance_by_id(100, 0), std::runtime_error); REQUIRE_THROWS_AS(tree.distance_by_id(1, 100), std::runtime_error); REQUIRE_THROWS_AS(tree.distance_by_id(100, 100), std::runtime_error); } TEMPLATE_TEST_CASE("tree_distance_by_value", "[space]", float, double) { std::vector<TestType> data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; metric::Tree<TestType, distance<TestType, TestType>> tree; tree.insert(data); // same value for(std::size_t i = 0; i < 10; ++i) { REQUIRE(tree.distance(data[i],data[i]) == 0); } //parent - child REQUIRE(tree.distance(data[5], data[2]) == 3); REQUIRE(tree.distance(data[2], data[5]) == 3); REQUIRE(tree.distance(6.2, 3.3) == 3); REQUIRE(tree.distance(3.3, 6.2) == 3); // same level, one parent REQUIRE(tree.distance(data[0], data[3]) == Approx((1.0 + 2.0) / 2)); REQUIRE(tree.distance(data[3], data[0]) == Approx((1.0 + 2.0)/2)); REQUIRE(tree.distance(1.3, 4.2) == Approx((1.0 + 2.0) / 2)); REQUIRE(tree.distance(4.2, 1.3) == Approx((1.0 + 2.0) / 2)); // different levels, one subtree REQUIRE(tree.distance(2.2, 3.9) == Approx((1.0 + 2.0 + 1.0) / 3)); REQUIRE(tree.distance(3.9, 2.2) == Approx((1.0 + 2.0 + 1.0) / 3)); //same levels, different subtree REQUIRE(tree.distance(0.1, 8.2) == Approx((1. + 1. + 2. + 3.) / 4)); REQUIRE(tree.distance(8.2, 0.7) == Approx((1. + 1. + 2. + 3.) / 4)); //different levels, different subtree REQUIRE(tree.distance(1.9, 7.9) == Approx((1. + 1. + 1. + 2. + 3.)/5)); REQUIRE(tree.distance(7.9, 1.9) == Approx((1. + 1. + 1. + 2. + 3.)/5)); } TEMPLATE_TEST_CASE("tree_to_distance_matrix", "[space]", float, double) { std::vector<TestType> data = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; metric::Tree<TestType, distance<TestType, TestType>> tree; tree.insert(data); auto m = tree.matrix(); distance<TestType, TestType> dist; for(std::size_t i = 0; i < m.rows(); i++) { for (std::size_t j = 0; j < m.columns(); j++) { if( i < j) { REQUIRE(m(i,j) == dist(data[i], data[j])); } else { REQUIRE(m(j, i) == dist(data[i], data[j])); } } } }
14,590
C++
.cpp
377
34.867374
122
0.57154
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,595
space_matrix_tests.cpp
metric-space-ai_metric/tests/space_tests/space_matrix_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #include <catch2/catch.hpp> #include <stdexcept> #include "metric/metric.hpp" TEMPLATE_TEST_CASE("matrix_constructor", "[space]", float, double) { // create empty matrix metric::Matrix<std::vector<TestType>, metric::Euclidean<TestType>> m; REQUIRE(m.size() == std::size_t(0)); // create matrix with initial values metric::Matrix<std::vector<TestType>, metric::Euclidean<TestType>> m1(std::vector<TestType>{1}); REQUIRE(m1.size() == std::size_t(1)); std::vector<std::vector<TestType>> data = {{1}, {2}, {3}}; metric::Matrix<std::vector<TestType>, metric::Euclidean<TestType>> m2(data); REQUIRE(m2.size() == std::size_t(3)); } TEMPLATE_TEST_CASE("matrix_insert", "[space]", float, double) { metric::Matrix<TestType, metric::Euclidean<TestType>> m; REQUIRE(m.size() == std::size_t(0)); std::size_t id1 = m.insert(TestType(1.0)); REQUIRE(id1 == std::size_t(0)); REQUIRE(m.size() == std::size_t(1)); std::vector<TestType> d = {1,2,3,4}; auto ids = m.insert(d); REQUIRE(m.size() == std::size_t(5)); std::vector<std::size_t> ids_eta = {1, 2, 3, 4}; //BOOST_CHECK_EQUAL_COLLECTIONS(ids.begin(), ids.end(), ids_eta.begin(),ids_eta.end()); REQUIRE(ids == ids_eta); REQUIRE(m(0, 1) == TestType(0)); REQUIRE(m(0, 2) == TestType(1.0)); REQUIRE(m(2, 0) == TestType(1.0)); REQUIRE(m(0, 3) == TestType(2.0)); REQUIRE(m(3, 0) == TestType(2.0)); REQUIRE(m.check_matrix()); } TEMPLATE_TEST_CASE("matrix_insert_if", "[space]", float, double) { metric::Matrix<TestType, metric::Euclidean<TestType>> m; REQUIRE(m.size() == std::size_t(0)); auto i1 = m.insert_if(TestType(1), TestType(10)); REQUIRE(m.size() == std::size_t(1)); REQUIRE(m[0] == TestType(1)); REQUIRE(i1.first == bool(0)); REQUIRE(i1.second == true); auto i2 = m.insert_if(TestType(2), TestType(10)); REQUIRE(m.size() == std::size_t(1)); REQUIRE(m[0] == TestType(1)); REQUIRE(i2 == std::pair(std::size_t(0),false)); std::vector<TestType> d = {2,3,50, 13, 100}; auto ids = m.insert_if(d,10); std::vector<std::pair<std::size_t, bool>> ids_eta = { {0, false}, {0, false}, {1, true}, {2, true}, {3, true} }; REQUIRE(m.size() == 4); REQUIRE(m[0] == TestType(1)); REQUIRE(m[1] == TestType(50)); REQUIRE(m[2] == TestType(13)); REQUIRE(m[3] == TestType(100)); //BOOST_CHECK_EQUAL_COLLECTIONS(ids.begin(), ids.end(), ids_eta.begin(), ids_eta.end()); REQUIRE(ids == ids_eta); REQUIRE(m.check_matrix()); } TEMPLATE_TEST_CASE("matrix_set", "[space]", float, double) { std::vector<TestType> data(10); std::iota(data.begin(), data.end(), 0); metric::Matrix<TestType, metric::Euclidean<TestType>> m(data); REQUIRE(m.size() == std::size_t(10)); REQUIRE(m.check_matrix()); m.set(std::size_t(0),TestType(100)); REQUIRE(m[0] == TestType(100)); REQUIRE(m.check_matrix()); REQUIRE(m[1] == TestType(1)); m.set(std::size_t(1),TestType(10)); REQUIRE(m[1] == TestType(10)); REQUIRE(m.check_matrix()); } TEMPLATE_TEST_CASE("matrix_erase", "[space]", float, double) { std::vector<TestType> data(10); std::iota(data.begin(), data.end(), 0); metric::Matrix<TestType, metric::Euclidean<TestType>> m(data); REQUIRE(m.check_matrix()); REQUIRE(m.size() == std::size_t(10)); m.erase(0); REQUIRE(m.size() == std::size_t(9)); auto nn = m.nn(TestType(1)); REQUIRE(nn == std::size_t(0)); REQUIRE(m[nn] == TestType(1)); REQUIRE(m.check_matrix()); auto item2 = m[2]; m.erase(2); REQUIRE(m.size() == std::size_t(8)); auto nn1 = m.nn(item2); REQUIRE(nn1 != item2); REQUIRE(m[0] == TestType(1)); REQUIRE(m[1] == TestType(2)); REQUIRE(m.check_matrix()); //BOOST_CHECK_THROW(m.erase(11), std::invalid_argument); REQUIRE_THROWS_AS(m.erase(11), std::invalid_argument); } TEMPLATE_TEST_CASE("test_knn", "[space]", float, double) { metric::Matrix<TestType, metric::Euclidean<TestType>> m; for(int i = 0; i < 10; i++) { m.insert(static_cast<TestType>(i)); } auto knn1 = m.knn(0.0, std::size_t(3)); REQUIRE(knn1.size() == std::size_t(3)); using knn_type_t = std::vector<std::pair<std::size_t, TestType>>; knn_type_t e1{{0, 0}, {1, 1}, {2, 2}}; //BOOST_CHECK_EQUAL_COLLECTIONS(knn1.begin(), knn1.end(), e1.begin(), e1.end()); REQUIRE(knn1 == e1); auto knn2 = m.knn(0.0, std::size_t(11)); REQUIRE(knn2.size() == std::size_t(10)); knn_type_t e2{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9,9}}; //BOOST_CHECK_EQUAL_COLLECTIONS(knn2.begin(), knn2.end(), e2.begin(), e2.end()); REQUIRE(knn2 == e2); auto knn3 = m.knn(1.0,std::size_t(3)); REQUIRE(knn3.size() == std::size_t(3)); knn_type_t e3{{1, 0}, {0, 1}, {2, 1}}; //BOOST_CHECK_EQUAL_COLLECTIONS(knn3.begin(), knn3.end(), e3.begin(), e3.end()); REQUIRE(knn3 == e3); auto knn4 = m.knn(0.5, std::size_t(3)); REQUIRE(knn4.size() == std::size_t(3)); knn_type_t e4{{0, 0.5}, {1, 0.5}, {2, 1.5}}; //BOOST_CHECK_EQUAL_COLLECTIONS(knn4.begin(), knn4.end(), e4.begin(), e4.end()); REQUIRE(knn4 == e4); } TEMPLATE_TEST_CASE("test_rnn", "[space]", float, double) { metric::Matrix<TestType, metric::Euclidean<TestType>> m{}; for (int i = 0; i < 10; i++) { m.insert(static_cast<TestType>(i)); } auto knn1 = m.rnn(0.0, 2.0); REQUIRE(knn1.size() == std::size_t(3)); using knn_type_t = std::vector<std::pair<std::size_t, TestType>>; knn_type_t e1{{0, 0}, {1, 1}, {2, 2}}; //BOOST_CHECK_EQUAL_COLLECTIONS(knn1.begin(), knn1.end(), e1.begin(), e1.end()); REQUIRE(knn1 == e1); auto knn2 = m.rnn(0.0, 12.0); REQUIRE(knn2.size() == std::size_t(10)); knn_type_t e2{{0, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}, {6, 6}, {7, 7}, {8, 8}, {9, 9}}; //BOOST_CHECK_EQUAL_COLLECTIONS(knn2.begin(), knn2.end(), e2.begin(), e2.end()); REQUIRE(knn2 == e2); }
6,240
C++
.cpp
144
38.770833
116
0.596665
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,596
float16_tests.cpp
metric-space-ai_metric/tests/utils/float16_tests.cpp
#include <catch2/catch.hpp> #include "metric/utils/types/BFloat16.h" #include <limits> #include <cmath> TEST_CASE("bfloat16_bfloat16_detail_builtin_isinf", "[float16]"){ // Does not work with CPP11_CMATH, works //numeric Limits bfloat16::bfloat16 inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 min = std::numeric_limits<bfloat16::bfloat16>::min(); //0x0080 bfloat16::bfloat16 low = std::numeric_limits<bfloat16::bfloat16>::lowest(); //0xFF7F bfloat16::bfloat16 max = std::numeric_limits<bfloat16::bfloat16>::max(); //0x7F7F bfloat16::bfloat16 eps = std::numeric_limits<bfloat16::bfloat16>::epsilon(); //0x0280 bfloat16::bfloat16 rr = std::numeric_limits<bfloat16::bfloat16>::round_error(); //0x3F00 bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF bfloat16::bfloat16 den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 //numeric Limits tests REQUIRE(true == bfloat16::detail::builtin_isinf(inf) ); REQUIRE(false == bfloat16::detail::builtin_isinf(QNaN) ); REQUIRE(false == bfloat16::detail::builtin_isinf(min) ); REQUIRE(false == bfloat16::detail::builtin_isinf(low) ); REQUIRE(false == bfloat16::detail::builtin_isinf(max) ); REQUIRE(false == bfloat16::detail::builtin_isinf(eps) ); REQUIRE(false == bfloat16::detail::builtin_isinf(rr) ); REQUIRE(false == bfloat16::detail::builtin_isinf(SNaN) ); REQUIRE(false == bfloat16::detail::builtin_isinf(den) ); //positive REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(5)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(10.5)) ); //negative REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(-5)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::builtin_isinf(bfloat16::bfloat16(-10.5)) ); } TEST_CASE("bfloat16_bfloat16_detail_builtin_isnan", "[float16]"){ // Does not work with CPP11_CMATH, works //numeric Limits bfloat16::bfloat16 inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 min = std::numeric_limits<bfloat16::bfloat16>::min(); //0x0080 bfloat16::bfloat16 low = std::numeric_limits<bfloat16::bfloat16>::lowest(); //0xFF7F bfloat16::bfloat16 max = std::numeric_limits<bfloat16::bfloat16>::max(); //0x7F7F bfloat16::bfloat16 eps = std::numeric_limits<bfloat16::bfloat16>::epsilon(); //0x0280 bfloat16::bfloat16 rr = std::numeric_limits<bfloat16::bfloat16>::round_error(); //0x3F00 bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF bfloat16::bfloat16 den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 //numeric Limits tests REQUIRE(false == bfloat16::detail::builtin_isnan(inf) ); REQUIRE(false == bfloat16::detail::builtin_isnan(min) ); REQUIRE(false == bfloat16::detail::builtin_isnan(low) ); REQUIRE(false == bfloat16::detail::builtin_isnan(max) ); REQUIRE(false == bfloat16::detail::builtin_isnan(eps) ); REQUIRE(false == bfloat16::detail::builtin_isnan(rr) ); REQUIRE(false == bfloat16::detail::builtin_isnan(den) ); REQUIRE(true == bfloat16::detail::builtin_isnan(SNaN) ); REQUIRE(true == bfloat16::detail::builtin_isnan(QNaN) ); //positive REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(5)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(10.5)) ); //negative REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(-5)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::builtin_isnan(bfloat16::bfloat16(-10.5)) ); } TEST_CASE("bfloat16_bfloat16_detail_builtin_signbit", "[float16]"){ // Does not work with CPP11_CMATH, otherwise: works //numeric Limits bfloat16::bfloat16 inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 min = std::numeric_limits<bfloat16::bfloat16>::min(); //0x0080 bfloat16::bfloat16 low = std::numeric_limits<bfloat16::bfloat16>::lowest(); //0xFF7F bfloat16::bfloat16 max = std::numeric_limits<bfloat16::bfloat16>::max(); //0x7F7F bfloat16::bfloat16 eps = std::numeric_limits<bfloat16::bfloat16>::epsilon(); //0x0280 bfloat16::bfloat16 rr = std::numeric_limits<bfloat16::bfloat16>::round_error(); //0x3F00 bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF bfloat16::bfloat16 den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 //numeric Limits tests REQUIRE(false == bfloat16::detail::builtin_signbit(inf) ); REQUIRE(false == bfloat16::detail::builtin_signbit(QNaN) ); REQUIRE(false == bfloat16::detail::builtin_signbit(min) ); REQUIRE(true == bfloat16::detail::builtin_signbit(low) ); REQUIRE(false == bfloat16::detail::builtin_signbit(max) ); REQUIRE(false == bfloat16::detail::builtin_signbit(eps) ); REQUIRE(false == bfloat16::detail::builtin_signbit(rr) ); REQUIRE(false == bfloat16::detail::builtin_signbit(SNaN) ); REQUIRE(false == bfloat16::detail::builtin_signbit(den) ); REQUIRE(false == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(5)) ); REQUIRE(false == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(-5)) ); REQUIRE(true == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::detail::builtin_signbit(bfloat16::bfloat16(-10.5)) ); } TEST_CASE("bfloat16_bfloat16_detail_sign_mask", "[float16]"){ // works //numeric Limits bfloat16::detail::uint32 inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 bfloat16::detail::uint32 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::detail::uint32 min = std::numeric_limits<bfloat16::bfloat16>::min(); //0x0080 bfloat16::detail::uint32 low = std::numeric_limits<bfloat16::bfloat16>::lowest(); //0xFF7F bfloat16::detail::uint32 max = std::numeric_limits<bfloat16::bfloat16>::max(); //0x7F7F bfloat16::detail::uint32 eps = std::numeric_limits<bfloat16::bfloat16>::epsilon(); //0x0280 bfloat16::detail::uint32 rr = std::numeric_limits<bfloat16::bfloat16>::round_error(); //0x3F00 bfloat16::detail::uint32 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF bfloat16::detail::uint32 den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 // sign_mask returns 0 (pos) or -1 (neg), and false = 0 //numeric Limits tests REQUIRE(false == bfloat16::detail::sign_mask(inf) ); REQUIRE(false == bfloat16::detail::sign_mask(QNaN) ); REQUIRE(false == bfloat16::detail::sign_mask(min) ); REQUIRE(false == bfloat16::detail::sign_mask(low) ); REQUIRE(false == bfloat16::detail::sign_mask(max) ); REQUIRE(false == bfloat16::detail::sign_mask(eps) ); REQUIRE(false == bfloat16::detail::sign_mask(rr) ); REQUIRE(false == bfloat16::detail::sign_mask(SNaN) ); REQUIRE(false == bfloat16::detail::sign_mask(den) ); //positive REQUIRE(false == bfloat16::detail::sign_mask(bfloat16::detail::uint32(5)) ); REQUIRE(false == bfloat16::detail::sign_mask(bfloat16::detail::uint32(10)) ); //negative REQUIRE(-1 == bfloat16::detail::sign_mask(bfloat16::detail::uint32(-5)) ); REQUIRE(-1 == bfloat16::detail::sign_mask(bfloat16::detail::uint32(-10)) ); } TEST_CASE("bfloat16_bfloat16_detail_arithmetic_shift", "[float16]"){ // works bfloat16::detail::uint32 max = 4294967295; //2^32 - 1 //Arithmetic Shift right a given number of times REQUIRE(4294967295 == bfloat16::detail::arithmetic_shift(max, 0 ) ); REQUIRE(2147483647 == bfloat16::detail::arithmetic_shift(max, 1 ) ); REQUIRE(1073741823 == bfloat16::detail::arithmetic_shift(max, 2 ) ); REQUIRE(536870911 == bfloat16::detail::arithmetic_shift(max, 3 ) ); REQUIRE(268435455 == bfloat16::detail::arithmetic_shift(max, 4 ) ); REQUIRE(134217727 == bfloat16::detail::arithmetic_shift(max, 5 ) ); REQUIRE(67108863 == bfloat16::detail::arithmetic_shift(max, 6 ) ); REQUIRE(33554431 == bfloat16::detail::arithmetic_shift(max, 7 ) ); REQUIRE(16777215 == bfloat16::detail::arithmetic_shift(max, 8 ) ); REQUIRE(8388607 == bfloat16::detail::arithmetic_shift(max, 9 ) ); REQUIRE(4194303 == bfloat16::detail::arithmetic_shift(max, 10) ); REQUIRE(2097151 == bfloat16::detail::arithmetic_shift(max, 11) ); REQUIRE(1048575 == bfloat16::detail::arithmetic_shift(max, 12) ); REQUIRE(524287 == bfloat16::detail::arithmetic_shift(max, 13) ); REQUIRE(262143 == bfloat16::detail::arithmetic_shift(max, 14) ); REQUIRE(131071 == bfloat16::detail::arithmetic_shift(max, 15) ); REQUIRE(65535 == bfloat16::detail::arithmetic_shift(max, 16) ); REQUIRE(32767 == bfloat16::detail::arithmetic_shift(max, 17) ); REQUIRE(16383 == bfloat16::detail::arithmetic_shift(max, 18) ); REQUIRE(8191 == bfloat16::detail::arithmetic_shift(max, 19) ); REQUIRE(4095 == bfloat16::detail::arithmetic_shift(max, 20) ); REQUIRE(2047 == bfloat16::detail::arithmetic_shift(max, 21) ); REQUIRE(1023 == bfloat16::detail::arithmetic_shift(max, 22) ); REQUIRE(511 == bfloat16::detail::arithmetic_shift(max, 23) ); REQUIRE(255 == bfloat16::detail::arithmetic_shift(max, 24) ); REQUIRE(127 == bfloat16::detail::arithmetic_shift(max, 25) ); REQUIRE(63 == bfloat16::detail::arithmetic_shift(max, 26) ); REQUIRE(31 == bfloat16::detail::arithmetic_shift(max, 27) ); REQUIRE(15 == bfloat16::detail::arithmetic_shift(max, 28) ); REQUIRE(7 == bfloat16::detail::arithmetic_shift(max, 29) ); REQUIRE(3 == bfloat16::detail::arithmetic_shift(max, 30) ); REQUIRE(1 == bfloat16::detail::arithmetic_shift(max, 31) ); } TEST_CASE("bfloat16_bfloat16_detail_compsignal", "[float16]"){ // Both variations work //numeric Limits bfloat16::bfloat16 inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 min = std::numeric_limits<bfloat16::bfloat16>::min(); //0x0080 bfloat16::bfloat16 low = std::numeric_limits<bfloat16::bfloat16>::lowest(); //0xFF7F bfloat16::bfloat16 max = std::numeric_limits<bfloat16::bfloat16>::max(); //0x7F7F bfloat16::bfloat16 eps = std::numeric_limits<bfloat16::bfloat16>::epsilon(); //0x0280 bfloat16::bfloat16 rr = std::numeric_limits<bfloat16::bfloat16>::round_error(); //0x3F00 bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF bfloat16::bfloat16 den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 //positive REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.32)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.32), bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(0.10), SNaN) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), bfloat16::bfloat16(32)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(32), bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(10), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(10), SNaN) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), bfloat16::bfloat16(32.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(32.1), bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(10.1), SNaN) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), bfloat16::bfloat16(32.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(32.5), bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(10.5), SNaN) ); //negative REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.32)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.32), bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-0.10), SNaN) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), bfloat16::bfloat16(-32)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-32), bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-10), SNaN) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-32.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-32.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.1), SNaN) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-32.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-32.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), inf) ); REQUIRE(false == bfloat16::detail::compsignal(inf, bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), min) ); REQUIRE(false == bfloat16::detail::compsignal(min, bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), max) ); REQUIRE(false == bfloat16::detail::compsignal(max, bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), eps) ); REQUIRE(false == bfloat16::detail::compsignal(eps, bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), rr) ); REQUIRE(false == bfloat16::detail::compsignal(rr, bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), den) ); REQUIRE(false == bfloat16::detail::compsignal(den, bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::detail::compsignal(bfloat16::bfloat16(-10.5), SNaN) ); //numeric Limits tests REQUIRE(false == bfloat16::detail::compsignal(inf, min) ); REQUIRE(false == bfloat16::detail::compsignal(inf, max) ); REQUIRE(false == bfloat16::detail::compsignal(inf, eps) ); REQUIRE(false == bfloat16::detail::compsignal(inf, rr) ); REQUIRE(false == bfloat16::detail::compsignal(inf, den) ); REQUIRE(false == bfloat16::detail::compsignal(min, max) ); REQUIRE(false == bfloat16::detail::compsignal(min, eps) ); REQUIRE(false == bfloat16::detail::compsignal(min, rr) ); REQUIRE(false == bfloat16::detail::compsignal(min, den) ); REQUIRE(false == bfloat16::detail::compsignal(max, eps) ); REQUIRE(false == bfloat16::detail::compsignal(max, rr) ); REQUIRE(false == bfloat16::detail::compsignal(max, den) ); REQUIRE(false == bfloat16::detail::compsignal(rr, den) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN,SNaN) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN,QNaN) ); //QNaN REQUIRE(true == bfloat16::detail::compsignal(QNaN, inf) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, min) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, max) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, eps) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, rr) ); REQUIRE(true == bfloat16::detail::compsignal(QNaN, den) ); REQUIRE(true == bfloat16::detail::compsignal(inf, QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(min, QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(max, QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(eps, QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(rr, QNaN) ); REQUIRE(true == bfloat16::detail::compsignal(den, QNaN) ); //SNaN REQUIRE(true == bfloat16::detail::compsignal(SNaN, inf) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, min) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, max) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, eps) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, rr) ); REQUIRE(true == bfloat16::detail::compsignal(SNaN, den) ); REQUIRE(true == bfloat16::detail::compsignal(inf, SNaN) ); REQUIRE(true == bfloat16::detail::compsignal(min, SNaN) ); REQUIRE(true == bfloat16::detail::compsignal(max, SNaN) ); REQUIRE(true == bfloat16::detail::compsignal(eps, SNaN) ); REQUIRE(true == bfloat16::detail::compsignal(rr, SNaN) ); REQUIRE(true == bfloat16::detail::compsignal(den, SNaN) ); } TEST_CASE("bfloat16_bfloat16_detail_signal1", "[float16]"){ //works bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF REQUIRE(0x7FFF == bfloat16::detail::signal(QNaN) ); REQUIRE(0x7FFF == bfloat16::detail::signal(SNaN) ); } TEST_CASE("bfloat16_bfloat16_detail_signal2", "[float16]"){ //works bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF REQUIRE(0x7FFF == bfloat16::detail::signal(bfloat16::bfloat16(10), QNaN) ); REQUIRE(0x7FFF == bfloat16::detail::signal(bfloat16::bfloat16(10), SNaN) ); REQUIRE(0x7FFF == bfloat16::detail::signal(QNaN, bfloat16::bfloat16(10)) ); REQUIRE(0x7FFF == bfloat16::detail::signal(SNaN, bfloat16::bfloat16(10)) ); } TEST_CASE("bfloat16_bfloat16_detail_signal3", "[float16]"){ //works bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF REQUIRE(0x7FFF == bfloat16::detail::signal(QNaN, bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(0x7FFF == bfloat16::detail::signal(SNaN, bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(0x7FFF == bfloat16::detail::signal(bfloat16::bfloat16(10), QNaN, bfloat16::bfloat16(10)) ); REQUIRE(0x7FFF == bfloat16::detail::signal(bfloat16::bfloat16(10), SNaN, bfloat16::bfloat16(10)) ); REQUIRE(0x7FFF == bfloat16::detail::signal(bfloat16::bfloat16(10), bfloat16::bfloat16(10), QNaN) ); REQUIRE(0x7FFF == bfloat16::detail::signal(bfloat16::bfloat16(10), bfloat16::bfloat16(10), SNaN) ); } TEST_CASE("bfloat16_bfloat16_detail_select", "[float16]"){ //works bfloat16::bfloat16 SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF REQUIRE(0x4120 == bfloat16::detail::select(bfloat16::bfloat16(10), SNaN) ); //0x4120 = 10.0 bf16 REQUIRE(0xC120 == bfloat16::detail::select(bfloat16::bfloat16(-10), SNaN) ); //0xC120 = -10.0 bf16 } TEST_CASE("bfloat16_bfloat16_detail_invalid", "[float16]"){ //works REQUIRE(0x7FFF == bfloat16::detail::invalid() ); // 0x7FF = QNaN } TEST_CASE("bfloat16_bfloat16_detail_pole", "[float16]"){ // works REQUIRE(0xFF80 == bfloat16::detail::pole(0x8000) ); //0xFF80 - Neg Inf REQUIRE(0x7F80 == bfloat16::detail::pole(0x0) ); //0x7F80 = Pos Inf } TEST_CASE("bfloat16_bfloat16_detail_check_underflow", "[float16]"){ // works //numeric Limits unsigned int inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 unsigned int QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF unsigned int min = std::numeric_limits<bfloat16::bfloat16>::min(); //0x0080 unsigned int low = std::numeric_limits<bfloat16::bfloat16>::lowest(); //0xFF7F unsigned int max = std::numeric_limits<bfloat16::bfloat16>::max(); //0x7F7F unsigned int eps = std::numeric_limits<bfloat16::bfloat16>::epsilon(); //0x280 unsigned int rr = std::numeric_limits<bfloat16::bfloat16>::round_error(); //0x3F00 unsigned int SNaN = std::numeric_limits<bfloat16::bfloat16>::signaling_NaN(); //0x7FBF unsigned int den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 //numeric Limits REQUIRE(inf == bfloat16::detail::check_underflow(inf) ); REQUIRE(QNaN == bfloat16::detail::check_underflow(QNaN) ); REQUIRE(min == bfloat16::detail::check_underflow(min) ); REQUIRE(low == bfloat16::detail::check_underflow(low) ); REQUIRE(max == bfloat16::detail::check_underflow(max) ); REQUIRE(eps == bfloat16::detail::check_underflow(eps) ); REQUIRE(rr == bfloat16::detail::check_underflow(rr) ); REQUIRE(SNaN == bfloat16::detail::check_underflow(SNaN) ); REQUIRE(den == bfloat16::detail::check_underflow(den) ); //positive REQUIRE(0x3DCC == bfloat16::detail::check_underflow(bfloat16::bfloat16(0.10)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == bfloat16::detail::check_underflow(bfloat16::bfloat16(0.5)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x40A0 == bfloat16::detail::check_underflow(bfloat16::bfloat16(5)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x4120 == bfloat16::detail::check_underflow(bfloat16::bfloat16(10)) ); //0x4120 = 10.0 bf16 REQUIRE(0x4121 == bfloat16::detail::check_underflow(bfloat16::bfloat16(10.1)) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == bfloat16::detail::check_underflow(bfloat16::bfloat16(10.5)) ); // 0x4128 = 10.5 bf16 //negative REQUIRE(0xBDCC == bfloat16::detail::check_underflow(bfloat16::bfloat16(-0.10)) ); // 0xBDCC = -0.099609375 bf16 REQUIRE(0xBF00 == bfloat16::detail::check_underflow(bfloat16::bfloat16(-0.5)) ); // 0xBF00 = -0.5 bf16 REQUIRE(0xC0A0 == bfloat16::detail::check_underflow(bfloat16::bfloat16(-5)) ); // 0xC0A0 = -5.0 bf16 REQUIRE(0xC120 == bfloat16::detail::check_underflow(bfloat16::bfloat16(-10)) ); //0xC120 = -10.0 bf16 REQUIRE(0xC121 == bfloat16::detail::check_underflow(bfloat16::bfloat16(-10.1)) ); // 0xC121 = -10.0625 bf16 REQUIRE(0xC128 == bfloat16::detail::check_underflow(bfloat16::bfloat16(-10.5)) ); // 0xC128 = -10.5 bf16 } TEST_CASE("bfloat16_bfloat16_detail_overflow", "[float16]"){ //works REQUIRE(0x7F80 == bfloat16::detail::overflow<std::round_toward_infinity>(0x0) ); // pos inf REQUIRE(0xFF7F == bfloat16::detail::overflow<std::round_toward_infinity>(0x8000) );// lowest REQUIRE(0x7F7F == bfloat16::detail::overflow<std::round_toward_neg_infinity>(0x0) );// max REQUIRE(0xFF80 == bfloat16::detail::overflow<std::round_toward_neg_infinity>(0x8000) ); // neg inf REQUIRE(0x7F7F == bfloat16::detail::overflow<std::round_toward_zero>(0x0) ); // max REQUIRE(0xFF7F == bfloat16::detail::overflow<std::round_toward_zero>(0x8000) ); // lowest REQUIRE(0x7F80 == bfloat16::detail::overflow<std::round_to_nearest>(0x0) ); // pos inf REQUIRE(0xFF80 == bfloat16::detail::overflow<std::round_to_nearest>(0x8000) ); // neg inf } TEST_CASE("bfloat16_bfloat16_detail_underflow", "[float16]"){ //works REQUIRE(0x7F80 == bfloat16::detail::overflow<std::round_toward_infinity>(0x0) ); // pos inf REQUIRE(0xFF7F == bfloat16::detail::overflow<std::round_toward_infinity>(0x8000) ); // lowest REQUIRE(0x7F7F == bfloat16::detail::overflow<std::round_toward_neg_infinity>(0x0) ); // lowest REQUIRE(0xFF80 == bfloat16::detail::overflow<std::round_toward_neg_infinity>(0x8000) ); // neg inf } TEST_CASE("bfloat16_bfloat16_detail_rounded", "[float16]"){ //works bfloat16::bfloat16 five = bfloat16::bfloat16(5); bfloat16::bfloat16 negfive = bfloat16::bfloat16(-5); bfloat16::bfloat16 ten = bfloat16::bfloat16(10); bfloat16::bfloat16 negten = bfloat16::bfloat16(-10); bfloat16::bfloat16 tenone = bfloat16::bfloat16(10.1); bfloat16::bfloat16 negtenone = bfloat16::bfloat16(-10.1); bfloat16::bfloat16 tenfive = bfloat16::bfloat16(10.5); bfloat16::bfloat16 negtenfive = bfloat16::bfloat16(-10.5); bfloat16::bfloat16 pointten = bfloat16::bfloat16(0.10); bfloat16::bfloat16 negpointten = bfloat16::bfloat16(-0.10); bfloat16::bfloat16 pointfive = bfloat16::bfloat16(0.5); bfloat16::bfloat16 negpointfive = bfloat16::bfloat16(-0.5); unsigned int val; //positive // Tests for bfloat16(0.10) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(pointten, 0,1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(pointten, 0,1); REQUIRE(0x3DCD == val ); // 0x3DCD = 0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(pointten, 0,1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(pointten, 1,1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(pointten, 1,1); REQUIRE(0x3DCD == val ); // 0x3DCD = 0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(pointten, 1,1); REQUIRE(0x3DCD == val ); // 0x3DCD = 0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(pointten, 0,1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(pointten, 0,1); REQUIRE(0x3DCD == val ); // 0x3DCD = 0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(pointten, 0,1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(pointten, 1,1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(pointten, 1,1); REQUIRE(0x3DCD == val ); // 0x3DCD = 0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(pointten, 1,1); REQUIRE(0x3DCD == val ); // 0x3DCD = 0.10009765625 bf16 // Tests for bfloat16(0.5) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(pointfive, 0,1); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(pointfive, 0,1); REQUIRE(0x3F01 == val ); // 0x3F01 = 0.50390625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(pointfive, 0,1); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(pointfive, 1,1); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(pointfive, 1,1); REQUIRE(0x3F01 == val ); // 0x3F01 = 0.50390625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(pointfive, 1,1); REQUIRE(0x3F01 == val ); // 0x3F01 = 0.50390625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(pointfive, 0,1); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(pointfive, 0,1); REQUIRE(0x3F01 == val ); // 0x3F01 = 0.50390625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(pointfive, 0,1); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(pointfive, 1,1); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(pointfive, 1,1); REQUIRE(0x3F01 == val ); // 0x3F01 = 0.50390625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(pointfive, 1,1); REQUIRE(0x3F01 == val ); // 0x3F01 = 0.50390625 bf16 // Tests for bfloat16(5) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(five, 0,1); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(five, 0,1); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(five, 0,1); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(five, 1,1); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(five, 1,1); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(five, 1,1); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(five, 0,1); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(five, 0,1); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(five, 0,1); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(five, 1,1); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(five, 1,1); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(five, 1,1); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 // Tests for bfloat16(10) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(ten, 0,1); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(ten, 0,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(ten, 0,1); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(ten, 1,1); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(ten, 1,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(ten, 1,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(ten, 0,1); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(ten, 0,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(ten, 0,1); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(ten, 1,1); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(ten, 1,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(ten, 1,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 // Tests for bfloat16(10.1) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(tenone, 0,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(tenone, 0,1); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(tenone, 0,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(tenone, 1,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(tenone, 1,1); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(tenone, 1,1); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(tenone, 0,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(tenone, 0,1); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(tenone, 0,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(tenone, 1,1); REQUIRE(0x4121 == val ); // 0x4121 = 10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(tenone, 1,1); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(tenone, 1,1); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 // Tests for bfloat16(10.5) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(tenfive, 0,1); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(tenfive, 0,1); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(tenfive, 0,1); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(tenfive, 1,1); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(tenfive, 1,1); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(tenfive, 1,1); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(tenfive, 0,1); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(tenfive, 0,1); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(tenfive, 0,1); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(tenfive, 1,1); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(tenfive, 1,1); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(tenfive, 1,1); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 //negative // Tests for bfloat16(-0.10) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negpointten, 0,1); REQUIRE(0xBDCD == val ); // 0xBDCD = -0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negpointten, 0,1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negpointten, 0,1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negpointten, 1,1); REQUIRE(0xBDCD == val ); // 0xBDCD = -0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negpointten, 1,1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negpointten, 1,1); REQUIRE(0xBDCD == val ); // 0xBDCD = -0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negpointten, 0,1); REQUIRE(0xBDCD == val ); // 0xBDCD = -0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negpointten, 0,1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negpointten, 0,1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negpointten, 1,1); REQUIRE(0xBDCD == val ); // 0xBDCD = -0.10009765625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negpointten, 1,1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negpointten, 1,1); REQUIRE(0xBDCD == val ); // 0xBDCD = -0.10009765625 bf16 // Tests for bfloat16(-0.5) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negpointfive, 0,1); REQUIRE(0xBF01 == val ); // 0xBF01 = -0.50390625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negpointfive, 0,1); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negpointfive, 0,1); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negpointfive, 1,1); REQUIRE(0xBF01 == val ); // 0xBF01 = -0.50390625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negpointfive, 1,1); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negpointfive, 1,1); REQUIRE(0xBF01 == val ); // 0xBF01 = -0.50390625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negpointfive, 0,1); REQUIRE(0xBF01 == val ); // 0xBF01 = -0.50390625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negpointfive, 0,1); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negpointfive, 0,1); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negpointfive, 1,1); REQUIRE(0xBF01 == val ); // 0xBF01 = -0.50390625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negpointfive, 1,1); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negpointfive, 1,1); REQUIRE(0xBF01 == val ); // 0xBF01 = -0.50390625 bf16 // Tests for bfloat16(-5) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negfive, 0,1); REQUIRE(0xC0A1 == val ); // 0xC0A1 = -5.03125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negfive, 0,1); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negfive, 0,1); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negfive, 1,1); REQUIRE(0xC0A1 == val ); // 0xC0A1 = -5.03125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negfive, 1,1); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negfive, 1,1); REQUIRE(0xC0A1 == val ); // 0xC0A1 = -5.03125 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negfive, 0,1); REQUIRE(0xC0A1 == val ); // 0xC0A1 = -5.03125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negfive, 0,1); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negfive, 0,1); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negfive, 1,1); REQUIRE(0xC0A1 == val ); // 0xC0A1 = -5.03125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negfive, 1,1); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negfive, 1,1); REQUIRE(0xC0A1 == val ); // 0xC0A1 = -5.03125 bf16 // Tests for bfloat16(-10) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negten, 0,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negten, 0,1); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negten, 0,1); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negten, 1,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negten, 1,1); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negten, 1,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negten, 0,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negten, 0,1); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negten, 0,1); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negten, 1,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negten, 1,1); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negten, 1,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 // Tests for bfloat16(-10.1) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negtenone, 0,1); REQUIRE(0xC122 == val ); // 0xC122 = -10.125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negtenone, 0,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negtenone, 0,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negtenone, 1,1); REQUIRE(0xC122 == val ); // 0xC122 = -10.125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negtenone, 1,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negtenone, 1,1); REQUIRE(0xC122 == val ); // 0xC122 = -10.125 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negtenone, 0,1); REQUIRE(0xC122 == val ); // 0xC122 = -10.125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negtenone, 0,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negtenone, 0,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negtenone, 1,1); REQUIRE(0xC122 == val ); // 0xC122 = -10.125 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negtenone, 1,1); REQUIRE(0xC121 == val ); // 0xC121 = -10.0625 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negtenone, 1,1); REQUIRE(0xC122 == val ); // 0xC122 = -10.125 bf16 // Tests for bfloat16(-10.5) val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negtenfive, 0,1); REQUIRE(0xC129 == val ); // 0xC129 = -10.5625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negtenfive, 0,1); REQUIRE(0xC128 == val ); // 0xC128 = -10.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negtenfive, 0,1); REQUIRE(0xC128 == val ); // 0xC128 = -10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, false>(negtenfive, 1,1); REQUIRE(0xC129 == val ); // 0xC129 = -10.5625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, false>(negtenfive, 1,1); REQUIRE(0xC128 == val ); // 0xC128 = -10.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, false>(negtenfive, 1,1); REQUIRE(0xC129 == val ); // 0xC129 = -10.5625 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negtenfive, 0,1); REQUIRE(0xC129 == val ); // 0xC129 = -10.5625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negtenfive, 0,1); REQUIRE(0xC128 == val ); // 0xC128 = -10.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negtenfive, 0,1); REQUIRE(0xC128 == val ); // 0xC128 = -10.5 bf16 val = bfloat16::detail::rounded<std::round_toward_neg_infinity, true>(negtenfive, 1,1); REQUIRE(0xC129 == val ); // 0xC129 = -10.5625 bf16 val = bfloat16::detail::rounded<std::round_toward_infinity, true>(negtenfive, 1,1); REQUIRE(0xC128 == val ); // 0xC128 = -10.5 bf16 val = bfloat16::detail::rounded<std::round_to_nearest, true>(negtenfive, 1,1); REQUIRE(0xC129 == val ); // 0xC129 = -10.5625 bf16 } TEST_CASE("bfloat16_bfloat16_detail_integral", "[float16]"){ //works unsigned int val; //positive //for bfloat16(0.3) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(0.3)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(0.3)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(0.3)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(0.3)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(0.3)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(0.3)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 //for bfloat16(0.5) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(0.5)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(0.5)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(0.5)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(0.5)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(0.5)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(0.5)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 //for bfloat16(0.8) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(0.8)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(0.8)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(0.8)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(0.8)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(0.8)); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(0.8)); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 //for bfloat16(6.3) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(6.3)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(6.3)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(6.3)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(6.3)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(6.3)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(6.3)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 //for bfloat16(6.5) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(6.5)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(6.5)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(6.5)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(6.5)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(6.5)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(6.5)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 //for bfloat16(6.8) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(6.8)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(6.8)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(6.8)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(6.8)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(6.8)); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(6.8)); REQUIRE(0x40C0 == val ); // 0x40C0 = 6.0 bf16 //negative //for bfloat16(-0.3) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(-0.3)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(-0.3)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(-0.3)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(-0.3)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(-0.3)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(-0.3)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 //for bfloat16(-0.5) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(-0.5)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(-0.5)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(-0.5)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(-0.5)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(-0.5)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(-0.5)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 //for bfloat16(-0.8) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(-0.8)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(-0.8)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(-0.8)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(-0.8)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(-0.8)); REQUIRE(0x8000 == val ); // 0x8000 = -0.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(-0.8)); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 //for bfloat16(-6.3) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(-6.3)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(-6.3)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(-6.3)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(-6.3)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(-6.3)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(-6.3)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 //for bfloat16(-6.5) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(-6.5)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(-6.5)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(-6.5)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(-6.5)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(-6.5)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(-6.5)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 //for bfloat16(-6.8) val =bfloat16::detail::integral<std::round_to_nearest , false, false>(bfloat16::bfloat16(-6.8)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , false, false>(bfloat16::bfloat16(-6.8)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , false, false>(bfloat16::bfloat16(-6.8)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val =bfloat16::detail::integral<std::round_to_nearest , true, false>(bfloat16::bfloat16(-6.8)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val =bfloat16::detail::integral<std::round_toward_infinity , true, false>(bfloat16::bfloat16(-6.8)); REQUIRE(0xC0C0 == val ); // 0xC0C0 = -6.0 bf16 val =bfloat16::detail::integral<std::round_toward_neg_infinity , true, false>(bfloat16::bfloat16(-6.8)); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 } TEST_CASE("bfloat16_bfloat16_detail_fixed2bfloat16", "[float16]"){ //works //creating fixed point values for conversion bfloat16::detail::f31 pointten(bfloat16::bfloat16(0.10)); bfloat16::detail::f31 pointfive(bfloat16::bfloat16(0.5)); bfloat16::detail::f31 one(bfloat16::bfloat16(1.0)); bfloat16::detail::f31 onepointsix(bfloat16::bfloat16(1.6)); bfloat16::detail::f31 twopointfour(bfloat16::bfloat16(2.4)); bfloat16::detail::f31 tenpointeight(bfloat16::bfloat16(10.8)); bfloat16::detail::f31 fiftypointfive(bfloat16::bfloat16(50.5)); bfloat16::detail::f31 fivehundred(bfloat16::bfloat16(586.3)); bfloat16::detail::f31 negpointten(bfloat16::bfloat16(-0.10)); bfloat16::detail::f31 negpointfive(bfloat16::bfloat16(-0.5)); bfloat16::detail::f31 negone(bfloat16::bfloat16(-1.0)); bfloat16::detail::f31 negonepointsix(bfloat16::bfloat16(-1.6)); bfloat16::detail::f31 negtwopointfour(bfloat16::bfloat16(-2.4)); bfloat16::detail::f31 negtenpointeight(bfloat16::bfloat16(-10.8)); bfloat16::detail::f31 negfiftypointfive(bfloat16::bfloat16(-50.5)); bfloat16::detail::f31 negfivehundred(bfloat16::bfloat16(-586.3)); unsigned int possign = (0x4120 & 0x8000); // sign from 10 = 0x0 unsigned int negsign = (0xC120 & 0x8000); // sign from -10 = 0x8000 unsigned int val; //positive fixed point test //tests for (0.10) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(pointten.m, pointten.exp+14, possign); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(pointten.m, pointten.exp+14, possign); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(pointten.m, pointten.exp+14, possign); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 //tests for (0.5) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(pointfive.m, pointfive.exp+14, possign); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(pointfive.m, pointfive.exp+14, possign); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(pointfive.m, pointfive.exp+14, possign); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 //tests for (1.0) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(one.m, one.exp+14, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(one.m, one.exp+14, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(one.m, one.exp+14, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 //tests for (1.6) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(onepointsix.m, onepointsix.exp+14, possign); REQUIRE(0x3FCC == val ); // 0x3FCC = 1.59375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(onepointsix.m, onepointsix.exp+14, possign); REQUIRE(0x3FCC == val ); // 0x3FCC = 1.59375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(onepointsix.m, onepointsix.exp+14, possign); REQUIRE(0x3FCC == val ); // 0x3FCC = 1.59375 bf16 //tests for (2.4) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(twopointfour.m, twopointfour.exp+14, possign); REQUIRE(0x4019 == val ); // 0x4019 = 2.390625 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(twopointfour.m, twopointfour.exp+14, possign); REQUIRE(0x4019 == val ); // 0x4019 = 2.390625 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(twopointfour.m, twopointfour.exp+14, possign); REQUIRE(0x4019 == val ); // 0x4019 = 2.390625 bf16 //tests for (10.8) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(tenpointeight.m, tenpointeight.exp+14, possign); REQUIRE(0x412C == val ); // 0x412C = 10.75 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(tenpointeight.m, tenpointeight.exp+14, possign); REQUIRE(0x412C == val ); // 0x412C = 10.75 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(tenpointeight.m, tenpointeight.exp+14, possign); REQUIRE(0x412C == val ); // 0x412C = 10.75 bf16 //tests for (50.5) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(fiftypointfive.m, fiftypointfive.exp+14, possign); REQUIRE(0x424A == val ); // 0x424A = 50.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(fiftypointfive.m, fiftypointfive.exp+14, possign); REQUIRE(0x424A == val ); // 0x424A = 50.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(fiftypointfive.m, fiftypointfive.exp+14, possign); REQUIRE(0x424A == val ); // 0x424A = 50.5 bf16 //tests for (586.3) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(fivehundred.m, fivehundred.exp+14, possign); REQUIRE(0x4412 == val ); // 0x4412 = 584.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,false, false, true>(fivehundred.m, fivehundred.exp+14, possign); REQUIRE(0x4412 == val ); // 0x4412 = 584.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,false, false, true>(fivehundred.m, fivehundred.exp+14, possign); REQUIRE(0x4412 == val ); // 0x4412 = 584.0 bf16 //negative fixed Point Test //tests for (-0.10) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negpointten.m, negpointten.exp+14, negsign); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negpointten.m, negpointten.exp+14, negsign); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negpointten.m, negpointten.exp+14, negsign); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 //tests for (-0.5) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negpointfive.m, negpointfive.exp+14, negsign); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negpointfive.m, negpointfive.exp+14, negsign); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negpointfive.m, negpointfive.exp+14, negsign); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 //tests for (-1.0) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negone.m, negone.exp+14, negsign); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negone.m, negone.exp+14, negsign); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negone.m, negone.exp+14, negsign); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 //tests for (-1.6) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negonepointsix.m, negonepointsix.exp+14, negsign); REQUIRE(0xBFCC == val ); // 0xBFCC = -1.59375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negonepointsix.m, negonepointsix.exp+14, negsign); REQUIRE(0xBFCC == val ); // 0xBFCC = -1.59375 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negonepointsix.m, negonepointsix.exp+14, negsign); REQUIRE(0xBFCC == val ); // 0xBFCC = -1.59375 bf16 //tests for (-2.4) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negtwopointfour.m, negtwopointfour.exp+14, negsign); REQUIRE(0xC019 == val ); // 0xC019 = -2.390625 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negtwopointfour.m, negtwopointfour.exp+14, negsign); REQUIRE(0xC019 == val ); // 0xC019 = -2.390625 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negtwopointfour.m, negtwopointfour.exp+14, negsign); REQUIRE(0xC019 == val ); // 0xC019 = -2.390625 bf16 //tests for (-10.8) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negtenpointeight.m, negtenpointeight.exp+14, negsign); REQUIRE(0xC12C == val ); // 0xC12C = -10.75 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negtenpointeight.m, negtenpointeight.exp+14, negsign); REQUIRE(0xC12C == val ); // 0xC12C = -10.75 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negtenpointeight.m, negtenpointeight.exp+14, negsign); REQUIRE(0xC12C == val ); // 0xC12C = -10.75 bf16 //tests for (-50.5) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negfiftypointfive.m, negfiftypointfive.exp+14, negsign); REQUIRE(0xC24A == val ); // 0xC24A = -50.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negfiftypointfive.m, negfiftypointfive.exp+14, negsign); REQUIRE(0xC24A == val ); // 0xC24A = -50.5 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negfiftypointfive.m, negfiftypointfive.exp+14, negsign); REQUIRE(0xC24A == val ); // 0xC24A = -50.5 bf16 //tests for (-586.3) val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,true, false, true>(negfivehundred.m, negfivehundred.exp+14, negsign); REQUIRE(0xC412 == val ); // 0xC412 = -584.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_infinity,31,true, false, true>(negfivehundred.m, negfivehundred.exp+14, negsign); REQUIRE(0xC412 == val ); // 0xC412 = -584.0 bf16 val = bfloat16::detail::fixed2bfloat16<std::round_toward_neg_infinity,31,true, false, true>(negfivehundred.m, negfivehundred.exp+14, negsign); REQUIRE(0xC412 == val ); // 0xC412 = -584.0 bf16 } //TODO, conversion from double -> bfloat TEST_CASE("bfloat16_bfloat16_detail_float2bfloat16_float", "[float16]"){ //works //Constructor //positive Tests REQUIRE(0x3DCC == bfloat16::bfloat16(0.1)); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == bfloat16::bfloat16(0.5)); // 0x3F00 = 0.5 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(1.0)); // 0x3F80 = 1.0 bf16 REQUIRE(0x3FCC == bfloat16::bfloat16(1.6)); // 0x3FCC = 1.59375 bf16 REQUIRE(0x4019 == bfloat16::bfloat16(2.4)); // 0x4019 = 2.390625 bf16 REQUIRE(0x412C == bfloat16::bfloat16(10.8)); // 0x412C = 10.75 bf16 REQUIRE(0x424A == bfloat16::bfloat16(50.5)); // 0x424A = 50.5 bf16 REQUIRE(0x4412 == bfloat16::bfloat16(586.3)); // 0x4412 = 584.0 bf16 //negative Tests REQUIRE(0xBDCC == bfloat16::bfloat16(-0.1)); // 0xBDCC = -0.099609375 bf16 REQUIRE(0xBF00 == bfloat16::bfloat16(-0.5)); // 0xBF00 = -0.5 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-1.0)); // 0xBF80 = -1.0 bf16 REQUIRE(0xBFCC == bfloat16::bfloat16(-1.6)); // 0xBFCC = -1.59375 bf16 REQUIRE(0xC019 == bfloat16::bfloat16(-2.4)); // 0xC019 = -2.390625 bf16 REQUIRE(0xC12C == bfloat16::bfloat16(-10.8)); // 0xC12C = -10.75 bf16 REQUIRE(0xC24A == bfloat16::bfloat16(-50.5)); // 0xC24A = -50.5 bf16 REQUIRE(0xC412 == bfloat16::bfloat16(-586.3)); // 0xC412 = -584.0 bf16 bfloat16::detail::uint16 val; //convert function //positive tests val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(0.1); REQUIRE(0x3DCC == val ); // 0x3DCC = 0.099609375 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(0.5); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(1.0); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(1.6); REQUIRE(0x3FCC == val ); // 0x3FCC = 1.59375 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(2.4); REQUIRE(0x4019 == val ); // 0x4019 = 2.390625 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(10.8); REQUIRE(0x412C == val ); // 0x412C = 10.75 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(50.5); REQUIRE(0x424A == val ); // 0x424A = 50.5 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(586.3); REQUIRE(0x4412 == val ); // 0x4412 = 584.0 bf16 //negative Tests val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-0.1); REQUIRE(0xBDCC == val ); // 0xBDCC = -0.099609375 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-0.5); REQUIRE(0xBF00 == val ); // 0xBF00 = -0.5 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-1.0); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-1.6); REQUIRE(0xBFCC == val ); // 0xBFCC = -1.59375 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-2.4); REQUIRE(0xC019 == val ); // 0xC019 = -2.390625 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-10.8); REQUIRE(0xC12C == val ); // 0xC12C = -10.75 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-50.5); REQUIRE(0xC24A == val ); // 0xC24A = -50.5 bf16 val = bfloat16::detail::float2bfloat16<std::round_to_nearest, float>(-586.3); REQUIRE(0xC412 == val ); // 0xC412 = -584.0 bf16 //from float->bfloat->float //positive REQUIRE(0.099609375 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(0.1))); REQUIRE(0.5 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(0.5)) ); REQUIRE(1.0 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(1.0)) ); REQUIRE(1.59375 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(1.6)) ); REQUIRE(2.390625 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(2.4)) ); REQUIRE(10.75 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(10.8)) ); REQUIRE(50.5 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(50.5)) ); REQUIRE(584 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(586.3)) ); //negative REQUIRE(-0.099609375 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-0.1)) ); REQUIRE(-0.5 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-0.5)) ); REQUIRE(-1.0 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-1.0)) ); REQUIRE(-1.59375 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-1.6)) ); REQUIRE(-2.390625 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-2.4)) ); REQUIRE(-10.75 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-10.8)) ); REQUIRE(-50.5 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-50.5)) ); REQUIRE(-584 == bfloat16::detail::bfloat162float_temp(bfloat16::bfloat16(-586.3)) ); } TEST_CASE("bfloat16_bfloat16_detail_int2bfloat16_bfloat162int", "[floa16]"){ //works unsigned int val; //positive val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(0); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(1); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(2); REQUIRE(0x4000 == val ); // 0x4000 = 2.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(3); REQUIRE(0x4040 == val ); // 0x4040 = 3.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(4); REQUIRE(0x4080 == val ); // 0x4080 = 4.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(5); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(6); REQUIRE(0x40C0 == val ); // 0x40c0 = 6.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(7); REQUIRE(0x40E0 == val ); // 0x40E0 = 7.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(8); REQUIRE(0x4100 == val ); // 0x4100 = 8.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(9); REQUIRE(0x4110 == val ); // 0x4110 = 9.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(10); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(20); REQUIRE(0x41A0 == val ); // 0x41A0 = 20.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(30); REQUIRE(0x41F0 == val ); // 0x41F0 = 30.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(40); REQUIRE(0x4220 == val ); // 0x4220 = 40.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(50); REQUIRE(0x4248 == val ); // 0x4220 = 40.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(60); REQUIRE(0x4270 == val ); // 0x4270 = 60.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(70); REQUIRE(0x428C == val ); // 0x428C = 70.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(80); REQUIRE(0x42A0 == val ); // 0x42A0 = 80.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(90); REQUIRE(0x42B4 == val ); // 0x42B4 = 90.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(100); REQUIRE(0x42C8 == val ); // 0x42C8 = 100.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(200); REQUIRE(0x4348 == val ); // 0x4348 = 200.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(300); REQUIRE(0x4396 == val ); // 0x4396 = 300.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(400); REQUIRE(0x43C8 == val ); // 0x43C8 = 400.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(500); REQUIRE(0x43FA == val ); // 0x43FA = 500.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(600); REQUIRE(0x4416 == val ); // 0x4416 = 600.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(700); REQUIRE(0x442F == val ); // 0x442F = 700.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(800); REQUIRE(0x4448 == val ); // 0x4448 = 800.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(900); REQUIRE(0x4461 == val ); // 0x4461 = 900.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(1000); REQUIRE(0x447A == val ); // 0x447A = 1000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(2000); REQUIRE(0x44FA == val ); // 0x44FA = 2000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(3000); REQUIRE(0x453C == val ); // 0x453C = 3008.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(4000); REQUIRE(0x457A == val ); // 0x457A = 4000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(5000); REQUIRE(0x459C == val ); // 0x459C = 4992.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(6000); REQUIRE(0x45BC == val ); // 0x45BC = 6016.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(7000); REQUIRE(0x45DB == val ); // 0x45DB = 7008.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(8000); REQUIRE(0x45FA == val ); // 0x45FA = 8000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(9000); REQUIRE(0x460D == val ); // 0x460D = 9024.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(10000); REQUIRE(0x461C == val ); // 0x461C = 9984.0 bf16 //negative val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-0); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-1); REQUIRE(0xBF80 == val ); // 0xBF80 = -1.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-2); REQUIRE(0xC000 == val ); // 0xC000 = -2.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-3); REQUIRE(0xC040 == val ); // 0xC040 = -3.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-4); REQUIRE(0xC080 == val ); // 0xC080 = -4.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-5); REQUIRE(0xC0A0 == val ); // 0xC0A0 = -5.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-6); REQUIRE(0xC0C0 == val ); // 0xC0c0 = -6.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-7); REQUIRE(0xC0E0 == val ); // 0xC0E0 = -7.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-8); REQUIRE(0xC100 == val ); // 0xC100 = -8.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-9); REQUIRE(0xC110 == val ); // 0xC110 = -9.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-10); REQUIRE(0xC120 == val ); // 0xC120 = -10.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-20); REQUIRE(0xC1A0 == val ); // 0xC1A0 = -20.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-30); REQUIRE(0xC1F0 == val ); // 0xC1F0 = -30.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-40); REQUIRE(0xC220 == val ); // 0xC220 = -40.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-50); REQUIRE(0xC248 == val ); // 0xC248 = -50.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-60); REQUIRE(0xC270 == val ); // 0xC270 = -60.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-70); REQUIRE(0xC28C == val ); // 0xC28C = -70.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-80); REQUIRE(0xC2A0 == val ); // 0xC2A0 = -80.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-90); REQUIRE(0xC2B4 == val ); // 0xC2B4 = -90.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-100); REQUIRE(0xC2C8 == val ); // 0xC2C8 = -100.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-200); REQUIRE(0xC348 == val ); // 0xC348 = -200.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-300); REQUIRE(0xC396 == val ); // 0xC396 = -300.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-400); REQUIRE(0xC3C8 == val ); // 0xC3C8 = -400.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-500); REQUIRE(0xC3FA == val ); // 0xC3FA = -500.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-600); REQUIRE(0xC416 == val ); // 0xC416 = -600.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-700); REQUIRE(0xC42F == val ); // 0xC42F = -700.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-800); REQUIRE(0xC448 == val ); // 0xC448 = -800.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-900); REQUIRE(0xC461 == val ); // 0xC461 = -900.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-1000); REQUIRE(0xC47A == val ); // 0xC47A = -1000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-2000); REQUIRE(0xC4FA == val ); // 0xC4FA = -2000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-3000); REQUIRE(0xC53C == val ); // 0xC53C = -3008.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-4000); REQUIRE(0xC57A == val ); // 0xC57A = -4000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-5000); REQUIRE(0xC59C == val ); // 0xC59C = -4992.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-6000); REQUIRE(0xC5BC == val ); // 0xC5BC = -6016.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-7000); REQUIRE(0xC5DB == val ); // 0xC5DB = -7008.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-8000); REQUIRE(0xC5FA == val ); // 0xC5FA = -8000.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-9000); REQUIRE(0xC60D == val ); // 0xC60D = -9024.0 bf16 val = bfloat16::detail::int2bfloat16<std::round_to_nearest, int>(-10000); REQUIRE(0xC61C == val ); // 0xC61C = -9984.0 bf16 //bfloat162int int result; result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(0.1)); REQUIRE(0 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, false, false, int>(bfloat16::bfloat16(0.1)); REQUIRE(0 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(0.5)); REQUIRE(0 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, false, false, int>(bfloat16::bfloat16(0.5)); REQUIRE(1 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(1.0)); REQUIRE(1 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, false, false, int>(bfloat16::bfloat16(1.0)); REQUIRE(1 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(1.4)); REQUIRE(1 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, false, false, int>(bfloat16::bfloat16(1.4)); REQUIRE(1 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(2.1)); REQUIRE(2 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(2.1)); REQUIRE(2 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(10.8)); REQUIRE(11 == result ); result = bfloat16::detail::bfloat162int<std::round_to_nearest, true, false, int>(bfloat16::bfloat16(10.8)); REQUIRE(11 == result ); } TEST_CASE("bfloat16_bfloat16_detail_f31", "[float16]"){ //works bfloat16::detail::f31 pointone(bfloat16::bfloat16(0.1)); bfloat16::detail::f31 pointfive(bfloat16::bfloat16(0.5)); bfloat16::detail::f31 five(bfloat16::bfloat16(5)); bfloat16::detail::f31 ten(bfloat16::bfloat16(10.0)); bfloat16::detail::f31 tenone(bfloat16::bfloat16(10.1)); bfloat16::detail::f31 tenfive(bfloat16::bfloat16(10.5)); bfloat16::detail::f31 twentyfive(bfloat16::bfloat16(20.5)); bfloat16::detail::f31 result(24, 7); unsigned int possign = (0x4120 & 0x8000); unsigned int negsign = (0xC120 & 0x8000); unsigned int val; //add //0.1 result = pointone + pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3E4C == val ); // 0x3e4c = 0.19921875 bf16 result = pointone + pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3F1A == val ); // 0x3f1a = 0.6015625 bf16 result = pointone + five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x40A3 == val ); // 0x40a3 = 5.09375 bf16 result = pointone + ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4122 == val ); // 0x4122 = 10.125 bf16 result = pointone + tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4123 == val ); // 0x4123 = 10.1875 bf16 result = pointone + tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x412A == val ); // 0x412A = 10.625 bf16 result = pointone + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A5 == val ); // 0x41A5 = 20.625 bf16 //0.5 result = pointfive + pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = pointfive + five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x40B0 == val ); // 0x40B8 = 5.75 bf16 result = pointfive + ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 result = pointfive + tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4129 == val ); // 0x4129 = 10.5625 bf16 result = pointfive + tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4130 == val ); // 0x4130 = 11.0 bf16 result = pointfive + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A8 == val ); // 0x41A8 = 21.0 bf16 //5 result = five + five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 result = five + ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4170 == val ); // 0x4170 = 15.0 bf16 result = five + tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4171 == val ); // 0x4171 = 15.0625 bf16 result = five + tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4178 == val ); // 0x4178 = 15.5 bf16 result = five + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41CC == val ); // 0x41CC = 25.5 bf16 //10 result = ten + ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A0 == val ); // 0x41A0 = 20.0 bf16 result = ten + tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A0 == val ); // 0x41A0 = 20.0 bf16 result = ten + tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A4 == val ); // 0x41A4 = 20.5 bf16 result = ten + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41F4 == val ); // 0x41F4 = 30.5 bf16 //10.1 result = tenone + tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A1 == val ); // 0x41A1 = 20.125 bf16 result = tenone + tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A4 == val ); // 0x41A4 = 20.5 bf16 result = tenone + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41F4 == val ); // 0x41F4 = 30.5 bf16 //10.5 result = tenfive + tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A8 == val ); // 0x41A8 = 21.0 bf16 result = tenfive + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41F8 == val ); // 0x41F8 = 31.0 bf16 //20.5 result = twentyfive + twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4224 == val ); // 0x4224 = 41.0 bf16 //sub //0.1 result = pointone - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 result = pointfive - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3ECD == val ); // 0x3ECD = 0.400390625 bf16 result = five - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x409D == val ); // 0x409D = 4.90625 bf16 result = ten - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x411E == val ); // 0x411E = 9.875 bf16 result = tenone - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x411F == val ); // 0x411F = 9.9375 bf16 result = tenfive - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4126 == val ); // 0x4126 = 10.375 bf16 result = twentyfive - pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A3 == val ); // 0x41A3 = 20.375 bf16 //0.5 result = pointfive - pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 result = five - pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4090 == val ); // 0x4090 = 4.5 bf16 result = ten - pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4118 == val ); // 0x4118 = 9.5 bf16 result = tenone - pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4119 == val ); // 0x4119 = 9.5625 bf16 result = tenfive - pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 result = twentyfive - pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x41A0 == val ); // 0x41A0 = 20.0 bf16 //5 result = five - five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 result = ten - five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 result = tenone - five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x40A2 == val ); // 0x40A2 = 5.0625 bf16 result = tenfive - five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x40B0 == val ); // 0x40B0 = 5.5 bf16 result = twentyfive - five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4178 == val ); // 0x4178 = 15.5 bf16 //10 result = ten - ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 result = tenone - ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3D80 == val ); // 0x3D80 = 0.0625 bf16 result = tenfive - ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3F00 == val ); // 0x3F00 = 0.5 bf16 result = twentyfive - ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4128 == val ); // 0x4128 = 10.5 bf16 //10.1 result = tenone - tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 result = tenfive - tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x3EE0 == val ); // 0x3EE0 = 0.4375 bf16 result = twentyfive - tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4127 == val ); // 0x4127 = 10.4375 bf16 //10.5 result = tenfive - tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 result = twentyfive - tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 //20.5 result = twentyfive - twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+14, possign); REQUIRE(0x0 == val ); // 0x0 = 0.0 bf16 //mult //0.1 result = pointone * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3C23 == val ); // 0x3C23 = 0.00994873046875 bf16 result = pointfive * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3D4C == val ); // 0x3D4C = 0.0498046875 bf16 result = five * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3EFF == val ); // 0x3EFF = 0.498046875 bf16 result = ten * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3F7F == val ); // 0x3F7F = 0.99609375 bf16 result = tenone * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = tenfive * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3F86 == val ); // 0x3F86 = 1.046875 bf16 result = twentyfive * pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4003 == val ); // 0x4003 = 2.046875 bf16 //0.5 result = pointfive * pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x3E80 == val ); // 0x3E80 = 0.25 bf16 result = five * pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4020 == val ); // 0x4020 = 2.5 bf16 result = ten * pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x40A0 == val ); // 0x40A0 = 5.0 bf16 result = tenone * pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 result = tenfive * pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x40A8 == val ); // 0x40A8 = 5.25 bf16 result = twentyfive * pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4124 == val ); // 0x4124 = 10.25 bf16 //5 result = five * five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x41C8 == val ); // 0x41C8 = 25.0 bf16 result = ten * five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4248 == val ); // 0x4248 = 50.0 bf16 result = tenone * five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4249 == val ); // 0x4249 = 50.25 bf16 result = tenfive * five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4252 == val ); // 0x4252 = 52.5 bf16 result = twentyfive * five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42CD == val ); // 0x42CD = 102.5 bf16 //10 result = ten * ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42C8 == val ); // 0x42C8 = 100.0 bf16 result = tenone * ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42C9 == val ); // 0x42C9 = 100.5 bf16 result = tenfive * ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42D2 == val ); // 0x42D2 = 105.0 bf16 result = twentyfive * ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x434D == val ); // 0x434D = 205.0 bf16 //10.1 result = tenone * tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42CB == val ); // 0x42CB = 101.5 bf16 result = tenfive * tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42D3 == val ); // 0x42D3 = 105.5 bf16 result = twentyfive * tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x434E == val ); // 0x434E = 206.0 bf16 //10.5 result = tenfive * tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x42DC == val ); // 0x42DC = 110.0 bf16 result = twentyfive * tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x4357 == val ); // 0x4357 = 215.0 bf16 //20.5 result = twentyfive * twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp-98, possign); REQUIRE(0x43D2 == val ); // 0x43D2 = 420.0 bf16 //Div //0.1 result = pointone / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = pointfive / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x40A1 == val ); // 0x40A1 = 5.03125 bf16 result = five / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4249 == val ); // 0x4249 = 50.25 bf16 result = ten / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x42C9 == val ); // 0x42C9 = 100.5 bf16 result = tenone / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x42CA == val ); // 0x42CA = 101.0 bf16 result = tenfive / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x42D3 == val ); // 0x42D3 = 105.5 bf16 result = twentyfive / pointone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x434E == val ); // 0x434E = 206.0 bf16 //0.5 result = pointfive / pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = five / pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4120 == val ); // 0x4120 = 10.0 bf16 result = ten / pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x41A0 == val ); // 0x41A0 = 20.0 bf16 result = tenone / pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x41A1 == val ); // 0x41A1 = 20.125 bf16 result = tenfive / pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x41A8 == val ); // 0x41A8 = 21.0 bf16 result = twentyfive / pointfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4224 == val ); // 0x4224 = 41.0 bf16 //5 result = five / five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = ten / five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4000 == val ); // 0x4000 = 2.0 bf16 result = tenone / five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4001 == val ); // 0x4001 = 2.015625 bf16 result = tenfive / five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4006 == val ); // 0x4006 = 2.09375 bf16 result = twentyfive / five; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4083 == val ); // 0x4083 = 4.09375 bf16 //10 result = ten / ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = tenone / ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F81 == val ); // 0x3F81 = 1.0078125 bf16 result = tenfive / ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F86 == val ); // 0x3F86 = 1.046875 bf16 result = twentyfive / ten; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4003 == val ); // 0x4003 = 2.046875 bf16 //10.1 result = tenone / tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = tenfive / tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F86 == val ); // 0x3F86 = 1.046875 bf16 result = twentyfive / tenone; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x4002 == val ); // 0x4002 = 2.03125 bf16 //10.5 result = tenfive / tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 result = twentyfive / tenfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3FFA == val ); // 0x3FFA = 1.953125 bf16 //20.5 result = twentyfive / twentyfive; val = bfloat16::detail::fixed2bfloat16<std::round_to_nearest,31,false, false, true>(result.m, result.exp+126, possign); REQUIRE(0x3F80 == val ); // 0x3F80 = 1.0 bf16 } TEST_CASE("bfloat16_bfloat16_detail_mod", "[float16]"){ //works int *quo; unsigned int test = bfloat16::detail::mod<false, false>(bfloat16::bfloat16(1000), bfloat16::bfloat16(6), quo); REQUIRE(0x4080 == test ); // 0x4080 = 4.0 bf16 test = bfloat16::detail::mod<false, false>(bfloat16::bfloat16(1000), bfloat16::bfloat16(2), quo); REQUIRE(0x0 == test ); // 0x0 = 0.0 bf16 test = bfloat16::detail::mod<false, false>(bfloat16::bfloat16(1000), bfloat16::bfloat16(1), quo); REQUIRE(0x0 == test ); // 0x0 = 0.0 bf16 test = bfloat16::detail::mod<false, false>(bfloat16::bfloat16(1000), bfloat16::bfloat16(150), quo); REQUIRE(0x42C8 == test ); // 0x42C8 = 100.0 bf16 test = bfloat16::detail::mod<false, false>(bfloat16::bfloat16(10), bfloat16::bfloat16(3), quo); REQUIRE(0x3F80 == test ); // 0x3F80 = 1.0 bf16 } TEST_CASE("bfloat16_operator_equal", "[float16]"){ //works //numeric Limits REQUIRE(0x7F80 == std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(0x7FFF == std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(0x0080 == std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(0xFF7F == std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(0x7F7F == std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(0x0280 == std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(0x3F00 == std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(0x7FBF == std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(0x0001 == std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(bfloat16::bfloat16(0.5) == bfloat16::bfloat16(0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(0.1) == bfloat16::bfloat16(0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(0.5) == bfloat16::bfloat16(0.1) ); REQUIRE(bfloat16::bfloat16(10) == bfloat16::bfloat16(10) ); REQUIRE_FALSE(bfloat16::bfloat16(20) == bfloat16::bfloat16(10) ); REQUIRE_FALSE(bfloat16::bfloat16(10) == bfloat16::bfloat16(20) ); REQUIRE(bfloat16::bfloat16(10.1) == bfloat16::bfloat16(10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(20.1) == bfloat16::bfloat16(10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(10.1) == bfloat16::bfloat16(20.1) ); REQUIRE(bfloat16::bfloat16(10.5) == bfloat16::bfloat16(10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(20.5) == bfloat16::bfloat16(10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(10.5) == bfloat16::bfloat16(20.5) ); //negative REQUIRE(bfloat16::bfloat16(-0.5) == bfloat16::bfloat16(-0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-0.1) == bfloat16::bfloat16(-0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-0.5) == bfloat16::bfloat16(-0.1) ); REQUIRE(bfloat16::bfloat16(-10) == bfloat16::bfloat16(-10) ); REQUIRE_FALSE(bfloat16::bfloat16(-20) == bfloat16::bfloat16(-10) ); REQUIRE_FALSE(bfloat16::bfloat16(-10) == bfloat16::bfloat16(-20) ); REQUIRE(bfloat16::bfloat16(-10.1) == bfloat16::bfloat16(-10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(-20.1) == bfloat16::bfloat16(-10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(-10.1) == bfloat16::bfloat16(-20.1) ); REQUIRE(bfloat16::bfloat16(-10.5) == bfloat16::bfloat16(-10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-20.5) == bfloat16::bfloat16(-10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-10.5) == bfloat16::bfloat16(-20.5) ); } TEST_CASE("bfloat16_operator_notequal", "[float16]"){ //works //numeric Limits REQUIRE(0x7F80 == std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(0x7FFF == std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(0x0080 == std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(0xFF7F == std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(0x7F7F == std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(0x0280 == std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(0x3F00 == std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(0x7FBF == std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(0x0001 == std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(bfloat16::bfloat16(0.5) == bfloat16::bfloat16(0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(0.1) == bfloat16::bfloat16(0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(0.5) == bfloat16::bfloat16(0.1) ); REQUIRE(bfloat16::bfloat16(10) == bfloat16::bfloat16(10) ); REQUIRE_FALSE(bfloat16::bfloat16(20) == bfloat16::bfloat16(10) ); REQUIRE_FALSE(bfloat16::bfloat16(10) == bfloat16::bfloat16(20) ); REQUIRE(bfloat16::bfloat16(10.1) == bfloat16::bfloat16(10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(20.1) == bfloat16::bfloat16(10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(10.1) == bfloat16::bfloat16(20.1) ); REQUIRE(bfloat16::bfloat16(10.5) == bfloat16::bfloat16(10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(20.5) == bfloat16::bfloat16(10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(10.5) == bfloat16::bfloat16(20.5) ); //negative REQUIRE(bfloat16::bfloat16(-0.5) == bfloat16::bfloat16(-0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-0.1) == bfloat16::bfloat16(-0.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-0.5) == bfloat16::bfloat16(-0.1) ); REQUIRE(bfloat16::bfloat16(-10) == bfloat16::bfloat16(-10) ); REQUIRE_FALSE(bfloat16::bfloat16(20) == bfloat16::bfloat16(10) ); REQUIRE_FALSE(bfloat16::bfloat16(10) == bfloat16::bfloat16(20) ); REQUIRE(bfloat16::bfloat16(-10.1) == bfloat16::bfloat16(-10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(-20.1) == bfloat16::bfloat16(-10.1) ); REQUIRE_FALSE(bfloat16::bfloat16(-10.1) == bfloat16::bfloat16(-20.1) ); REQUIRE(bfloat16::bfloat16(-10.5) == bfloat16::bfloat16(-10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-20.5) == bfloat16::bfloat16(-10.5) ); REQUIRE_FALSE(bfloat16::bfloat16(-10.5) == bfloat16::bfloat16(-20.5) ); } TEST_CASE("bfloat16_operator_less", "[float16]"){ //works //numeric Limits REQUIRE(false == 0x7F80 < std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(false == 0x7FFF < std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(false == 0x0080 < std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(false == 0xFF7F < std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(false == 0x7F7F < std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(false == 0x0280 < std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(false == 0x3F00 < std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(false == 0x7FBF < std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(false == 0x0001 < std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(false == bfloat16::bfloat16(0.5) < bfloat16::bfloat16(0.5) ); REQUIRE(true == bfloat16::bfloat16(0.1) < bfloat16::bfloat16(0.5) ); REQUIRE(false == bfloat16::bfloat16(0.5) < bfloat16::bfloat16(0.1) ); REQUIRE(false == bfloat16::bfloat16(10) < bfloat16::bfloat16(10) ); REQUIRE(false == bfloat16::bfloat16(20) < bfloat16::bfloat16(10) ); REQUIRE(true == bfloat16::bfloat16(10) < bfloat16::bfloat16(20) ); REQUIRE(false == bfloat16::bfloat16(10.1) < bfloat16::bfloat16(10.1) ); REQUIRE(false == bfloat16::bfloat16(20.1) < bfloat16::bfloat16(10.1) ); REQUIRE(true == bfloat16::bfloat16(10.1) < bfloat16::bfloat16(20.1) ); REQUIRE(false == bfloat16::bfloat16(10.5) < bfloat16::bfloat16(10.5) ); REQUIRE(false == bfloat16::bfloat16(20.5) < bfloat16::bfloat16(10.5) ); REQUIRE(true == bfloat16::bfloat16(10.5) < bfloat16::bfloat16(20.5) ); //negative REQUIRE(false == bfloat16::bfloat16(-0.5) < bfloat16::bfloat16(-0.5) ); REQUIRE(false == bfloat16::bfloat16(-0.1) < bfloat16::bfloat16(-0.5) ); REQUIRE(true == bfloat16::bfloat16(-0.5) < bfloat16::bfloat16(-0.1) ); REQUIRE(false == bfloat16::bfloat16(-10) < bfloat16::bfloat16(-10) ); REQUIRE(true == bfloat16::bfloat16(-20) < bfloat16::bfloat16(-10) ); REQUIRE(false == bfloat16::bfloat16(-10) < bfloat16::bfloat16(-20) ); REQUIRE(false == bfloat16::bfloat16(-10.1) < bfloat16::bfloat16(-10.1) ); REQUIRE(true == bfloat16::bfloat16(-20.1) < bfloat16::bfloat16(-10.1) ); REQUIRE(false == bfloat16::bfloat16(-10.1) < bfloat16::bfloat16(-20.1) ); REQUIRE(false == bfloat16::bfloat16(-10.5) < bfloat16::bfloat16(-10.5) ); REQUIRE(true == bfloat16::bfloat16(-20.5) < bfloat16::bfloat16(-10.5) ); REQUIRE(false == bfloat16::bfloat16(-10.5) < bfloat16::bfloat16(-20.5) ); } TEST_CASE("bfloat16_operator_greater", "[float16]"){ //works //numeric Limits REQUIRE(false == 0x7F80 > std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(false == 0x7FFF > std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(false == 0x0080 > std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(false == 0xFF7F > std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(false == 0x7F7F > std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(false == 0x0280 > std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(false == 0x3F00 > std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(false == 0x7FBF > std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(false == 0x0001 > std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(false == bfloat16::bfloat16(0.5) > bfloat16::bfloat16(0.5) ); REQUIRE(false == bfloat16::bfloat16(0.1) > bfloat16::bfloat16(0.5) ); REQUIRE(true == bfloat16::bfloat16(0.5) > bfloat16::bfloat16(0.1) ); REQUIRE(false == bfloat16::bfloat16(10) > bfloat16::bfloat16(10) ); REQUIRE(true == bfloat16::bfloat16(20) > bfloat16::bfloat16(10) ); REQUIRE(false == bfloat16::bfloat16(10) > bfloat16::bfloat16(20) ); REQUIRE(false == bfloat16::bfloat16(10.1) > bfloat16::bfloat16(10.1) ); REQUIRE(true == bfloat16::bfloat16(20.1) > bfloat16::bfloat16(10.1) ); REQUIRE(false == bfloat16::bfloat16(10.1) > bfloat16::bfloat16(20.1) ); REQUIRE(false == bfloat16::bfloat16(10.5) > bfloat16::bfloat16(10.5) ); REQUIRE(true == bfloat16::bfloat16(20.5) > bfloat16::bfloat16(10.5) ); REQUIRE(false == bfloat16::bfloat16(10.5) > bfloat16::bfloat16(20.5) ); //negative REQUIRE(false == bfloat16::bfloat16(-0.5) > bfloat16::bfloat16(-0.5) ); REQUIRE(true == bfloat16::bfloat16(-0.1) > bfloat16::bfloat16(-0.5) ); REQUIRE(false == bfloat16::bfloat16(-0.5) > bfloat16::bfloat16(-0.1) ); REQUIRE(false == bfloat16::bfloat16(-10) > bfloat16::bfloat16(-10) ); REQUIRE(false == bfloat16::bfloat16(-20) > bfloat16::bfloat16(-10) ); REQUIRE(true == bfloat16::bfloat16(-10) > bfloat16::bfloat16(-20) ); REQUIRE(false == bfloat16::bfloat16(-10.1) > bfloat16::bfloat16(-10.1) ); REQUIRE(false == bfloat16::bfloat16(-20.1) > bfloat16::bfloat16(-10.1) ); REQUIRE(true == bfloat16::bfloat16(-10.1) > bfloat16::bfloat16(-20.1) ); REQUIRE(false == bfloat16::bfloat16(-10.5) > bfloat16::bfloat16(-10.5) ); REQUIRE(false == bfloat16::bfloat16(-20.5) > bfloat16::bfloat16(-10.5) ); REQUIRE(true == bfloat16::bfloat16(-10.5) > bfloat16::bfloat16(-20.5) ); } TEST_CASE("bfloat16_operator_lessequal", "[float16]"){ //works //numeric Limits REQUIRE(true == 0x7F80 <= std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(true == 0x7FFF <= std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(true == 0x0080 <= std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(true == 0xFF7F <= std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(true == 0x7F7F <= std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(true == 0x0280 <= std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(true == 0x3F00 <= std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(true == 0x7FBF <= std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(true == 0x0001 <= std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(true == bfloat16::bfloat16(0.5) <= bfloat16::bfloat16(0.5) ); REQUIRE(true == bfloat16::bfloat16(0.1) <= bfloat16::bfloat16(0.5) ); REQUIRE(false == bfloat16::bfloat16(0.5) <= bfloat16::bfloat16(0.1) ); REQUIRE(true == bfloat16::bfloat16(10) <= bfloat16::bfloat16(10) ); REQUIRE(false == bfloat16::bfloat16(20) <= bfloat16::bfloat16(10) ); REQUIRE(true == bfloat16::bfloat16(10) <= bfloat16::bfloat16(20) ); REQUIRE(true == bfloat16::bfloat16(10.1) <= bfloat16::bfloat16(10.1) ); REQUIRE(false == bfloat16::bfloat16(20.1) <= bfloat16::bfloat16(10.1) ); REQUIRE(true == bfloat16::bfloat16(10.1) <= bfloat16::bfloat16(20.1) ); REQUIRE(true == bfloat16::bfloat16(10.5) <= bfloat16::bfloat16(10.5) ); REQUIRE(false == bfloat16::bfloat16(20.5) <= bfloat16::bfloat16(10.5) ); REQUIRE(true == bfloat16::bfloat16(10.5) <= bfloat16::bfloat16(20.5) ); //negative REQUIRE(true == bfloat16::bfloat16(-0.5) <= bfloat16::bfloat16(-0.5) ); REQUIRE(false == bfloat16::bfloat16(-0.1) <= bfloat16::bfloat16(-0.5) ); REQUIRE(true == bfloat16::bfloat16(-0.5) <= bfloat16::bfloat16(-0.1) ); REQUIRE(true == bfloat16::bfloat16(-10) <= bfloat16::bfloat16(-10) ); REQUIRE(true == bfloat16::bfloat16(-20) <= bfloat16::bfloat16(-10) ); REQUIRE(false == bfloat16::bfloat16(-10) <= bfloat16::bfloat16(-20) ); REQUIRE(true == bfloat16::bfloat16(-10.1) <= bfloat16::bfloat16(-10.1) ); REQUIRE(true == bfloat16::bfloat16(-20.1) <= bfloat16::bfloat16(-10.1) ); REQUIRE(false == bfloat16::bfloat16(-10.1) <= bfloat16::bfloat16(-20.1) ); REQUIRE(true == bfloat16::bfloat16(-10.5) <= bfloat16::bfloat16(-10.5) ); REQUIRE(true == bfloat16::bfloat16(-20.5) <= bfloat16::bfloat16(-10.5) ); REQUIRE(false == bfloat16::bfloat16(-10.5) <= bfloat16::bfloat16(-20.5) ); } TEST_CASE("bfloat16_operator_greaterequal", "[float16]"){ //works //numeric Limits REQUIRE(true == 0x7F80 >= std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(true == 0x7FFF >= std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(true == 0x0080 >= std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(true == 0xFF7F >= std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(true == 0x7F7F >= std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(true == 0x0280 >= std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(true == 0x3F00 >= std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(true == 0x7FBF >= std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(true == 0x0001 >= std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(true == bfloat16::bfloat16(0.5) >= bfloat16::bfloat16(0.5) ); REQUIRE(false == bfloat16::bfloat16(0.1) >= bfloat16::bfloat16(0.5) ); REQUIRE(true == bfloat16::bfloat16(0.5) >= bfloat16::bfloat16(0.1) ); REQUIRE(true == bfloat16::bfloat16(10) >= bfloat16::bfloat16(10) ); REQUIRE(true == bfloat16::bfloat16(20) >= bfloat16::bfloat16(10) ); REQUIRE(false == bfloat16::bfloat16(10) >= bfloat16::bfloat16(20) ); REQUIRE(true == bfloat16::bfloat16(10.1) >= bfloat16::bfloat16(10.1) ); REQUIRE(true == bfloat16::bfloat16(20.1) >= bfloat16::bfloat16(10.1) ); REQUIRE(false == bfloat16::bfloat16(10.1) >= bfloat16::bfloat16(20.1) ); REQUIRE(true == bfloat16::bfloat16(10.5) >= bfloat16::bfloat16(10.5) ); REQUIRE(true == bfloat16::bfloat16(20.5) >= bfloat16::bfloat16(10.5) ); REQUIRE(false == bfloat16::bfloat16(10.5) >= bfloat16::bfloat16(20.5) ); //negative REQUIRE(true == bfloat16::bfloat16(-0.5) >= bfloat16::bfloat16(-0.5) ); REQUIRE(true == bfloat16::bfloat16(-0.1) >= bfloat16::bfloat16(-0.5) ); REQUIRE(false == bfloat16::bfloat16(-0.5) >= bfloat16::bfloat16(-0.1) ); REQUIRE(true == bfloat16::bfloat16(-10) >= bfloat16::bfloat16(-10) ); REQUIRE(false == bfloat16::bfloat16(-20) >= bfloat16::bfloat16(-10) ); REQUIRE(true == bfloat16::bfloat16(-10) >= bfloat16::bfloat16(-20) ); REQUIRE(true == bfloat16::bfloat16(-10.1) >= bfloat16::bfloat16(-10.1) ); REQUIRE(false == bfloat16::bfloat16(-20.1) >= bfloat16::bfloat16(-10.1) ); REQUIRE(true == bfloat16::bfloat16(-10.1) >= bfloat16::bfloat16(-20.1) ); REQUIRE(true == bfloat16::bfloat16(-10.5) >= bfloat16::bfloat16(-10.5) ); REQUIRE(false == bfloat16::bfloat16(-20.5) >= bfloat16::bfloat16(-10.5) ); REQUIRE(true == bfloat16::bfloat16(-10.5) >= bfloat16::bfloat16(-20.5) ); } TEST_CASE("bfloat16_operator_positive", "[float16]"){ //works //numeric Limits REQUIRE(0x7F80 == + std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(0x7FFF == + std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(0x0080 == + std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(0xFF7F == + std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(0x7F7F == + std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(0x0280 == + std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(0x3F00 == + std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(0x7FBF == + std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(0x0001 == + std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(0x3DCC == + bfloat16::bfloat16(0.1) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == + bfloat16::bfloat16(0.5) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x4120 == + bfloat16::bfloat16(10) ); // 0x4120 = 10.0 bf16 REQUIRE(0x4121 == + bfloat16::bfloat16(10.1) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == + bfloat16::bfloat16(10.5) ); // 0x4128 = 10.5 bf16 //negative REQUIRE(0xBDCC == + bfloat16::bfloat16(-0.1) ); // 0xBDCC = -0.099609375 bf16 REQUIRE(0xBF00 == + bfloat16::bfloat16(-0.5) ); // 0xBF00 = -0.5 bf16 REQUIRE(0xC120 == + bfloat16::bfloat16(-10) ); // 0xC120 = -10.0 bf16 REQUIRE(0xC121 == + bfloat16::bfloat16(-10.1) ); // 0xC121 = -10.0625 bf16 REQUIRE(0xC128 == + bfloat16::bfloat16(-10.5) ); // 0xC128 = -10.5 bf16 } TEST_CASE("bfloat16_operator_negative", "[float16]"){ //works //numeric Limits REQUIRE(0xFF80 == - std::numeric_limits<bfloat16::bfloat16>::infinity()); REQUIRE(0xfFFF == - std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()); REQUIRE(0x8080 == - std::numeric_limits<bfloat16::bfloat16>::min()); REQUIRE(0x7F7F == - std::numeric_limits<bfloat16::bfloat16>::lowest()); REQUIRE(0xFF7F == - std::numeric_limits<bfloat16::bfloat16>::max()); REQUIRE(0x8280 == - std::numeric_limits<bfloat16::bfloat16>::epsilon()); REQUIRE(0xBF00 == - std::numeric_limits<bfloat16::bfloat16>::round_error()); REQUIRE(0xFFBF == - std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()); REQUIRE(0x8001 == - std::numeric_limits<bfloat16::bfloat16>::denorm_min()); //positive REQUIRE(0xBDCC == - bfloat16::bfloat16(0.1) ); // 0xBDCC = -0.099609375 bf16 REQUIRE(0xBF00 == - bfloat16::bfloat16(0.5) ); // 0xBF00 = -0.5 bf16 REQUIRE(0xC120 == - bfloat16::bfloat16(10) ); // 0xC120 = -10.0 bf16 REQUIRE(0xC121 == - bfloat16::bfloat16(10.1) ); // 0xC121 = -10.0625 bf16 REQUIRE(0xC128 == - bfloat16::bfloat16(10.5) ); // 0xC128 = -10.5 bf16 //negative REQUIRE(0x3DCC == - bfloat16::bfloat16(-0.1) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == - bfloat16::bfloat16(-0.5) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x4120 == - bfloat16::bfloat16(-10) ); // 0x4120 = 10.0 bf16 REQUIRE(0x4121 == - bfloat16::bfloat16(-10.1) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == - bfloat16::bfloat16(-10.5) ); // 0x4128 = 10.5 bf16 } TEST_CASE("Temp_BF16_ADD", "[float16]"){ //calculated with the temporary solution REQUIRE(0x3E4C == bfloat16::bfloat16(0.1) + bfloat16::bfloat16(0.1) ); // 0x3e4c = 0.19921875 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-0.1) + bfloat16::bfloat16(0.1) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(0.1) + bfloat16::bfloat16(-0.1) ); // 0x0 = 0.0 bf16 REQUIRE(0xBE4C == bfloat16::bfloat16(-0.1) + bfloat16::bfloat16(-0.1) ); // 0xBE4C h = -0.19921875 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(0.5) + bfloat16::bfloat16(0.5) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-0.5) + bfloat16::bfloat16(0.5) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(0.5) + bfloat16::bfloat16(-0.5) ); // 0x0 = 0.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-0.5) + bfloat16::bfloat16(-0.5) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x40A0 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-5) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x4080 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-6) ); // 0x4080 = 4.0 bf16 REQUIRE(0x4040 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-7) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4000 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-8) ); // 0x4000 = 2.0 bf16 REQUIRE(0x41A0 == bfloat16::bfloat16(10) + bfloat16::bfloat16(10) ); // 0x41A0 = 20.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-10) + bfloat16::bfloat16(10) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-10) ); // 0x0 = 0.0 bf16 REQUIRE(0xC1A0 == bfloat16::bfloat16(-10) + bfloat16::bfloat16(-10) ); // 0xC1A0 = -20.0 bf16 REQUIRE(0x41A1 == bfloat16::bfloat16(10.1) + bfloat16::bfloat16(10.1) ); // 0x41A1 = 20.125 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-10.1) + bfloat16::bfloat16(10.1) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(10.1) + bfloat16::bfloat16(-10.1) ); // 0x0 = 0.0 bf16 REQUIRE(0xC1A1 == bfloat16::bfloat16(-10.1) + bfloat16::bfloat16(-10.1) ); // 0xC1A1 h = -20.125 bf16 REQUIRE(0x41A8 == bfloat16::bfloat16(10.5) + bfloat16::bfloat16(10.5) ); // 0x41A8 = 21.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-10.5) + bfloat16::bfloat16(10.5) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(10.5) + bfloat16::bfloat16(-10.5) ); // 0x0 = 0.0 bf16 REQUIRE(0xC1A8 == bfloat16::bfloat16(-10.5) + bfloat16::bfloat16(-10.5) ); // 0xC1A8 h = -21.0 bf16 } //TODO TEST_CASE("bfloat16_operator_add", "[floa16]"){ //Arithmetic type is not enabled for me //works ,or not //REQUIRE(0x40A0 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-5)); //REQUIRE(0x4080 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-6)); //REQUIRE(0x4040 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-7)); //REQUIRE(0x4000 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-8)); //positive REQUIRE(0x3E4C == bfloat16::bfloat16(0.1) + bfloat16::bfloat16(0.1)); REQUIRE(0x0 == bfloat16::bfloat16(-0.1) + bfloat16::bfloat16(0.1)); REQUIRE(0x3F80 == bfloat16::bfloat16(0.5) + bfloat16::bfloat16(0.5)); REQUIRE(0x0 == bfloat16::bfloat16(-0.5) + bfloat16::bfloat16(0.5)); REQUIRE(0x41A0 == bfloat16::bfloat16(10) + bfloat16::bfloat16(10)); REQUIRE(0x0 == bfloat16::bfloat16(-10) + bfloat16::bfloat16(10)); REQUIRE(0x41A1 == bfloat16::bfloat16(10.1) + bfloat16::bfloat16(10.1)); REQUIRE(0x0 == bfloat16::bfloat16(-10.1) + bfloat16::bfloat16(10.1)); REQUIRE(0x41A8 == bfloat16::bfloat16(10.5) + bfloat16::bfloat16(10.5)); REQUIRE(0x0 == bfloat16::bfloat16(-10.5) + bfloat16::bfloat16(10.5)); //negative REQUIRE(0x0 == bfloat16::bfloat16(0.1) + bfloat16::bfloat16(-0.1)); REQUIRE(0xBE4C == bfloat16::bfloat16(-0.1) + bfloat16::bfloat16(-0.1)); REQUIRE(0x0 == bfloat16::bfloat16(0.5) + bfloat16::bfloat16(-0.5)); REQUIRE(0xBF80 == bfloat16::bfloat16(-0.5) + bfloat16::bfloat16(-0.5)); REQUIRE(0x0 == bfloat16::bfloat16(10) + bfloat16::bfloat16(-10)); REQUIRE(0xC1A0 == bfloat16::bfloat16(-10) + bfloat16::bfloat16(-10)); REQUIRE(0x0 == bfloat16::bfloat16(10.1) + bfloat16::bfloat16(-10.1)); REQUIRE(0xC1A1 == bfloat16::bfloat16(-10.1) + bfloat16::bfloat16(-10.1)); REQUIRE(0x0 == bfloat16::bfloat16(10.5) + bfloat16::bfloat16(-10.5)); REQUIRE(0xC1A8 == bfloat16::bfloat16(-10.5) + bfloat16::bfloat16(-10.5)); } TEST_CASE("bfloat16_operator_sub", "[float16]"){ //works, depends on add above, at the moment with the temp solution //REQUIRE(0x40A0 == bfloat16::bfloat16(10) - bfloat16::bfloat16(5)); REQUIRE(0x0 == bfloat16::bfloat16(0.1) - bfloat16::bfloat16(0.1) ); // 0x0 = 0.0 bf16 REQUIRE(0xBE4C == bfloat16::bfloat16(-0.1) - bfloat16::bfloat16(0.1) ); // 0xBE4C = -0.19921875 bf16 REQUIRE(0x3E4C == bfloat16::bfloat16(0.1) - bfloat16::bfloat16(-0.1) ); // 0x3E4C = 0.19921875 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-0.1) - bfloat16::bfloat16(-0.1) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(0.5) - bfloat16::bfloat16(0.5) ); // 0x0 = 0.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-0.5) - bfloat16::bfloat16(0.5) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(0.5) - bfloat16::bfloat16(-0.5) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-0.5) - bfloat16::bfloat16(-0.5) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(10) - bfloat16::bfloat16(10) ); // 0x0 = 0.0 bf16 REQUIRE(0xC1A0 == bfloat16::bfloat16(-10) - bfloat16::bfloat16(10) ); // 0xC1A0 = -20.0 bf16 REQUIRE(0x41A0 == bfloat16::bfloat16(10) - bfloat16::bfloat16(-10) ); // 0x41A0 = 10.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-10) - bfloat16::bfloat16(-10) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(10.1) - bfloat16::bfloat16(10.1) ); // 0x0 = 0.0 bf16 REQUIRE(0xC1A1 == bfloat16::bfloat16(-10.1) - bfloat16::bfloat16(10.1) ); // 0xC1A1 = -20.125 bf16 REQUIRE(0x41A1 == bfloat16::bfloat16(10.1) - bfloat16::bfloat16(-10.1) ); // 0x41A1 = 20.125 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-10.1) - bfloat16::bfloat16(-10.1) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(10.5) - bfloat16::bfloat16(10.5) ); // 0x0 = 0.0 bf16 REQUIRE(0xC1A8 == bfloat16::bfloat16(-10.5) - bfloat16::bfloat16(10.5) ); // 0xC1A8 = -21.0 bf16 REQUIRE(0x41A8 == bfloat16::bfloat16(10.5) - bfloat16::bfloat16(-10.5) ); // 0x41A8 = 21.0 bf16 REQUIRE(0x0 == bfloat16::bfloat16(-10.5) - bfloat16::bfloat16(-10.5) ); // 0x0 = 0.0 bf16 } TEST_CASE("bfloat16_operator_mult", "[float16]"){ //calculated with the temporary solution REQUIRE(0x0 == bfloat16::bfloat16(0) * bfloat16::bfloat16(0) ); // 0x0 = 0.0 bf16 REQUIRE(0x3C22 == bfloat16::bfloat16(0.1) * bfloat16::bfloat16(0.1) ); // 0x3C22 = 0.0098876953125 bf16 REQUIRE(0xBC22 == bfloat16::bfloat16(-0.1) * bfloat16::bfloat16(0.1) ); // 0xBC22 = -0.0098876953125 bf16 REQUIRE(0xBC22 == bfloat16::bfloat16(0.1) * bfloat16::bfloat16(-0.1) ); // 0xBC22 = -0.0098876953125 bf16 REQUIRE(0x3C22 == bfloat16::bfloat16(-0.1) * bfloat16::bfloat16(-0.1) ); // 0x3C22 = 0.0098876953125 bf16 REQUIRE(0x3E80 == bfloat16::bfloat16(0.5) * bfloat16::bfloat16(0.5) ); // 0x3E80 = 0.25 bf16 REQUIRE(0xBE80 == bfloat16::bfloat16(-0.5) * bfloat16::bfloat16(0.5) ); // 0xBE80 = -0.25 bf16 REQUIRE(0xBE80 == bfloat16::bfloat16(0.5) * bfloat16::bfloat16(-0.5) ); // 0xBE80 = -0.25 bf16 REQUIRE(0x3E80 == bfloat16::bfloat16(-0.5) * bfloat16::bfloat16(-0.5) ); // 0x3E80 = 0.25 bf16 REQUIRE(0x42C8 == bfloat16::bfloat16(10) * bfloat16::bfloat16(10) ); // 0x42C8 = 100.0 bf16 REQUIRE(0xC2C8 == bfloat16::bfloat16(-10) * bfloat16::bfloat16(10) ); // 0xC2C8 = -100.0 bf16 REQUIRE(0xC2C8 == bfloat16::bfloat16(10) * bfloat16::bfloat16(-10) ); // 0xC2C8 = -100.0 bf16 REQUIRE(0x42C8 == bfloat16::bfloat16(-10) * bfloat16::bfloat16(-10) ); // 0x42C8 = 100.0 bf16 REQUIRE(0x42CA == bfloat16::bfloat16(10.1) * bfloat16::bfloat16(10.1) ); // 0x42CA = 101.0 bf16 REQUIRE(0xC2CA == bfloat16::bfloat16(-10.1) * bfloat16::bfloat16(10.1) ); // 0xC2CA = -101.0 bf16 REQUIRE(0xC2CA == bfloat16::bfloat16(10.1) * bfloat16::bfloat16(-10.1) ); // 0xC2CA = -101.0 bf16 REQUIRE(0x42CA == bfloat16::bfloat16(-10.1) * bfloat16::bfloat16(-10.1) ); // 0x42CA = 101.0 bf16 REQUIRE(0x42DC == bfloat16::bfloat16(10.5) * bfloat16::bfloat16(10.5) ); // 0x42DC = 110.0 bf16 REQUIRE(0xC2DC == bfloat16::bfloat16(-10.5) * bfloat16::bfloat16(10.5) ); // 0xC2DC = -110.0 bf16 REQUIRE(0xC2DC == bfloat16::bfloat16(10.5) * bfloat16::bfloat16(-10.5) ); // 0xC2DC = -110.0 bf16 REQUIRE(0x42DC == bfloat16::bfloat16(-10.5) * bfloat16::bfloat16(-10.5) ); // 0x42DC = 110.0 bf16 } TEST_CASE("bfloat16_operator_div", "[float16]"){ //calculated with the temporary solution REQUIRE(0x3F80 == bfloat16::bfloat16(0.1) / bfloat16::bfloat16(0.1) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-0.1) / bfloat16::bfloat16(0.1) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(0.1) / bfloat16::bfloat16(-0.1) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(-0.1) / bfloat16::bfloat16(-0.1) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(0.5) / bfloat16::bfloat16(0.5) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-0.5) / bfloat16::bfloat16(0.5) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(0.5) / bfloat16::bfloat16(-0.5) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(-0.5) / bfloat16::bfloat16(-0.5) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(10) / bfloat16::bfloat16(10) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-10) / bfloat16::bfloat16(10) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(10) / bfloat16::bfloat16(-10) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(-10) / bfloat16::bfloat16(-10) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(10.1) / bfloat16::bfloat16(10.1) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-10.1) / bfloat16::bfloat16(10.1) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(10.1) / bfloat16::bfloat16(-10.1) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(-10.1) / bfloat16::bfloat16(-10.1) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(10.5) / bfloat16::bfloat16(10.5) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(-10.5) / bfloat16::bfloat16(10.5) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::bfloat16(10.5) / bfloat16::bfloat16(-10.5) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::bfloat16(-10.5) / bfloat16::bfloat16(-10.5) ); // 0x3F80 = 1.0 bf16 } /* TEST_CASE("bfloat16_operator_input_output", "[floa16]"){ //TODO input //works //positive std::cout << bfloat16::bfloat16(0.1) << std::endl; std::cout << bfloat16::bfloat16(0.5) << std::endl; //negative std::cout << bfloat16::bfloat16(-0.1) << std::endl; std::cout << bfloat16::bfloat16(-0.5) << std::endl; } */ TEST_CASE("bfloat16_fabs", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); // input +inf REQUIRE(0x7F80 == bfloat16::fabs(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); // input -inf REQUIRE(0x7FFF == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(0x0080 == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::min()) ); REQUIRE(0x7F7F == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::lowest()) ); REQUIRE(0x7F7F == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::max()) ); REQUIRE(0x0280 == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::epsilon()) ); REQUIRE(0x3F00 == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::round_error()) ); REQUIRE(0x7FBF == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()) ); REQUIRE(0x0001 == bfloat16::fabs(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); //positive REQUIRE(0x0 == bfloat16::fabs(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3DCC == bfloat16::fabs(bfloat16::bfloat16(0.1)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == bfloat16::fabs(bfloat16::bfloat16(0.5)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x4120 == bfloat16::fabs(bfloat16::bfloat16(10)) ); // 0x4120 = 10.0 bf16 REQUIRE(0x4121 == bfloat16::fabs(bfloat16::bfloat16(10.1)) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == bfloat16::fabs(bfloat16::bfloat16(10.5)) ); // 0x4128 = 10.5 bf16 //negative REQUIRE(0x0 == bfloat16::fabs(bfloat16::bfloat16(-0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3DCC == bfloat16::fabs(bfloat16::bfloat16(-0.1)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == bfloat16::fabs(bfloat16::bfloat16(-0.5)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x4120 == bfloat16::fabs(bfloat16::bfloat16(-10)) ); // 0x4120 = 10.0 bf16 REQUIRE(0x4121 == bfloat16::fabs(bfloat16::bfloat16(-10.1)) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == bfloat16::fabs(bfloat16::bfloat16(-10.5)) ); // 0x4128 = 10.5 bf16 } TEST_CASE("bfloat16_abs", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7FFF == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(0x0080 == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::min()) ); REQUIRE(0x7F7F == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::lowest()) ); REQUIRE(0x7F7F == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::max()) ); REQUIRE(0x0280 == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::epsilon()) ); REQUIRE(0x3F00 == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::round_error()) ); REQUIRE(0x7FBF == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()) ); REQUIRE(0x0001 == bfloat16::abs(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); //positive REQUIRE(0x3DCC == bfloat16::abs(bfloat16::bfloat16(0.1)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == bfloat16::abs(bfloat16::bfloat16(0.5)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x4120 == bfloat16::abs(bfloat16::bfloat16(10)) ); // 0x4120 = 10.0 bf16 REQUIRE(0x4121 == bfloat16::abs(bfloat16::bfloat16(10.1)) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == bfloat16::abs(bfloat16::bfloat16(10.5)) ); // 0x4128 = 10.5 bf16 //negative REQUIRE(0x3DCC == bfloat16::abs(bfloat16::bfloat16(-0.1)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3F00 == bfloat16::abs(bfloat16::bfloat16(-0.5)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x4120 == bfloat16::abs(bfloat16::bfloat16(-10)) ); // 0x4120 = 10.0 bf16 REQUIRE(0x4121 == bfloat16::abs(bfloat16::bfloat16(-10.1)) ); // 0x4121 = 10.0625 bf16 REQUIRE(0x4128 == bfloat16::abs(bfloat16::bfloat16(-10.5)) ); // 0x4128 = 10.5 bf16 } TEST_CASE("bfloat16_fmod", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::fmod(bfloat16::bfloat16(0), bfloat16::bfloat16(2)) ); REQUIRE(0x0 == bfloat16::fmod(bfloat16::bfloat16(-0), bfloat16::bfloat16(2)) ); // should be -0 REQUIRE(isnan(bfloat16::fmod(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::fmod(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::fmod(bfloat16::bfloat16(2), bfloat16::bfloat16(0)) )); REQUIRE(0x0000 == bfloat16::fmod(bfloat16::bfloat16(2), bfloat16::bfloat16(-2)) ); REQUIRE(0x4000 == bfloat16::fmod(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x4000 == bfloat16::fmod(bfloat16::bfloat16(2), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::fmod(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::fmod(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(),bfloat16::bfloat16(2)) )); //positive REQUIRE(0x0000 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(2)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(3)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x0000 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x4080 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(6)) ); // 0x4080 = 4.0 bf16 REQUIRE(0x4040 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(7)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4000 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(8)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x3F80 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(9)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x0000 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(2.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x4040 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(3.5)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x3F80 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(4.5)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4090 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(5.5)) ); // 0x4090 = 4.5 bf16 REQUIRE(0x4060 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(6.5)) ); // 0x4060 = 3.5 bf16 REQUIRE(0x4020 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(7.5)) ); // 0x4020 = 2.5 bf16 REQUIRE(0x3FC0 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(8.5)) ); // 0x3FC0 = 1.5 bf16 REQUIRE(0x3F00 == bfloat16::fmod(bfloat16::bfloat16(10), bfloat16::bfloat16(9.5)) ); // 0x3F00 = 0.5 bf16 //negative REQUIRE(0x8000 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(2)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xBF80 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(3)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC000 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(4)) ); // 0xC000 = -2.0 bf16 REQUIRE(0x8000 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xC080 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(6)) ); // 0xC080 = -4.0 bf16 REQUIRE(0xC040 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(7)) ); // 0xC040 = -3.0 bf16 REQUIRE(0xC000 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(8)) ); // 0xC000 = -2.0 bf16 REQUIRE(0xBF80 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(9)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x8000 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(2.5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xC040 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(3.5)) ); // 0xC040 = -3.0 bf16 REQUIRE(0xBF80 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(4.5)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC090 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(5.5)) ); // 0xC090 = -4.5 bf16 REQUIRE(0xC060 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(6.5)) ); // 0xC060 = -3.5 bf16 REQUIRE(0xC020 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(7.5)) ); // 0xC020 = -2.5 bf16 REQUIRE(0xBFC0 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(8.5)) ); // 0xBFC0 = -1.5 bf16 REQUIRE(0xBF00 == bfloat16::fmod(bfloat16::bfloat16(-10), bfloat16::bfloat16(9.5)) ); // 0xBF00 = -0.5 bf16 } TEST_CASE("bfloat16_remainder", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::remainder(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::remainder(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::remainder(bfloat16::bfloat16(2), bfloat16::bfloat16(0)) )); REQUIRE(isnan(bfloat16::remainder(bfloat16::bfloat16(2), bfloat16::bfloat16(-0)) )); REQUIRE(isnan(bfloat16::remainder(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::remainder(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(2)) )); //positive REQUIRE(0x0000 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(2)) ); // 0x0000 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(3)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x0000 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0x0000 = 0.0 bf16 REQUIRE(0xC000 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(6)) ); // 0xC000 = -2.0 bf16 REQUIRE(0x4040 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(7)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4000 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(8)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x3F80 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(9)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x0000 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(2.5)) ); // 0x0000 = 0.0 bf16 REQUIRE(0xBF00 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(3.5)) ); // 0xBF00 = -0.5 bf16 REQUIRE(0x3F80 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(4.5)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(5.5)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC040 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(6.5)) ); // 0xC040 = -2.0 bf16 REQUIRE(0x4020 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(7.5)) ); // 0x4020 = 2.5 bf16 REQUIRE(0x3FC0 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(8.5)) ); // 0x3FC0 = 1.5 bf16 REQUIRE(0x3F00 == bfloat16::remainder(bfloat16::bfloat16(10), bfloat16::bfloat16(9.5)) ); // 0x3F00 = 0.5 bf16 //negative REQUIRE(0x8000 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(2)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xBF80 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(3)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC000 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(4)) ); // 0xC000 = -2.0 bf16 REQUIRE(0x8000 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x4000 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(6)) ); // 0x4000 = 2.0 bf16 REQUIRE(0xC040 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(7)) ); // 0xC040 = -3.0 bf16 REQUIRE(0xC000 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(8)) ); // 0xC000 = -2.0 bf16 REQUIRE(0xBF80 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(9)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x8000 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(2.5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x3F00 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(3.5)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0xBF80 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(4.5)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(5.5)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4040 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(6.5)) ); // 0x4040 = 3.0 bf16 REQUIRE(0xC020 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(7.5)) ); // 0xC020 = -2.5 bf16 REQUIRE(0xBFC0 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(8.5)) ); // 0xBFC0 = -1.5 bf16 REQUIRE(0xBF00 == bfloat16::remainder(bfloat16::bfloat16(-10), bfloat16::bfloat16(9.5)) ); // 0xBF00 = -0.5 bf16 } /* TEST_CASE("bfloat16_remquo", "[floa16]"){ //works, but got illegal operand error, when i try to get the *quo value from the function return //I tested it without this part (quo) and it worked completely fine. int * quo; //positive REQUIRE(0x0000 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(2), quo) ); // 0x0000 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(3), quo) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(4), quo) ); // 0x4000 = 2.0 bf16 REQUIRE(0x0000 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(5), quo) ); // 0x0000 = 0.0 bf16 REQUIRE(0xC000 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(6), quo) ); // 0xC000 = -2.0 bf16 REQUIRE(0x4040 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(7), quo) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4000 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(8), quo) ); // 0x4000 = 2.0 bf16 REQUIRE(0x3F80 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(9), quo) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x0000 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(2.5), quo) ); // 0x0000 = 0.0 bf16 REQUIRE(0xBF00 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(3.5), quo) ); // 0xBF00 = -0.5 bf16 REQUIRE(0x3F80 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(4.5), quo) ); // 0x3F80 = 1.0 bf16 REQUIRE(0xBF80 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(5.5), quo) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC040 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(6.5), quo) ); // 0xC040 = -3.0 bf16 REQUIRE(0x4020 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(7.5), quo) ); // 0x4020 = 2.5 bf16 REQUIRE(0x3FC0 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(8.5), quo) ); // 0x3FC0 = 1.5 bf16 REQUIRE(0x3F00 == bfloat16::remquo(bfloat16::bfloat16(10), bfloat16::bfloat16(9.5), quo) ); // 0x3F00 = 0.5 bf16 //negative REQUIRE(0x8000 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(2), quo) ); // 0x8000 = -0.0 bf16 REQUIRE(0xBF80 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(3), quo) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC000 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(4), quo) ); // 0xC000 = -2.0 bf16 REQUIRE(0x8000 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(5), quo) ); // 0x8000 = -0.0 bf16 REQUIRE(0x4000 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(6), quo) ); // 0x4000 = 2.0 bf16 REQUIRE(0xC040 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(7), quo) ); // 0xC040 = -3.0 bf16 REQUIRE(0xC000 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(8), quo) ); // 0xC000 = -4.0 bf16 REQUIRE(0xBF80 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(9), quo) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x8000 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(2.5), quo) ); // 0x8000 = -0.0 bf16 REQUIRE(0x3F00 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(3.5), quo) ); // 0x3F00 = 0.5 bf16 REQUIRE(0xBF80 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(4.5), quo) ); // 0xBF80 = -1.0 bf16 REQUIRE(0x3F80 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(5.5), quo) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4040 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(6.5), quo) ); // 0x4040 = 3.0 bf16 REQUIRE(0xC020 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(7.5), quo) ); // 0xC020 = -2.5 bf16 REQUIRE(0xBFC0 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(8.5), quo) ); // 0xBFC0 = -1.5 bf16 REQUIRE(0xBF00 == bfloat16::remquo(bfloat16::bfloat16(-10), bfloat16::bfloat16(9.5), quo) ); // 0xBF00 = -0.5 bf16 } */ TEST_CASE("bfloat16_fma", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::fma(bfloat16::bfloat16(0), std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::fma(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(0), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::fma(bfloat16::bfloat16(0), std::numeric_limits<bfloat16::bfloat16>::infinity(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::fma(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(0), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::fma(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::fma(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive 3x negative 0x REQUIRE(0x4250 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(5), bfloat16::bfloat16(2)) ); // 0x4250 = 52.0 bf16 REQUIRE(0x41C8 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(2), bfloat16::bfloat16(5)) ); // 0x41C8 = 25.0 bf16 REQUIRE(0x4250 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(10), bfloat16::bfloat16(2)) ); // 0x4250 = 52.0 bf16 REQUIRE(0x41A0 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(2), bfloat16::bfloat16(10)) ); // 0x41A0 = 20.0 bf16 REQUIRE(0x41C8 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0x41C8 = 25.0 bf16 REQUIRE(0x41A0 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(5), bfloat16::bfloat16(10)) ); // 0x41A0 = 20.0 bf16 //positive 2x negative 1x REQUIRE(0x4240 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(5), bfloat16::bfloat16(-2)) ); // 0x4240 = 48.0 bf16 REQUIRE(0x4170 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(2), bfloat16::bfloat16(-5)) ); // 0x4170 = 15.0 bf16 REQUIRE(0x4240 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(10), bfloat16::bfloat16(-2)) ); // 0x4240 = 48.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(2), bfloat16::bfloat16(-10)) ); // 0x0 = 0.0 bf16 REQUIRE(0x4170 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(10), bfloat16::bfloat16(-5)) ); // 0x4170 = 15.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(5), bfloat16::bfloat16(-10)) ); // 0x0 = 0.0 bf16 //positive 2x negative 1x REQUIRE(0xC240 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(-5), bfloat16::bfloat16(2)) ); // 0xC240 = -48.0 bf16 REQUIRE(0xC170 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(-2), bfloat16::bfloat16(5)) ); // 0xC170 = -15.0 bf16 REQUIRE(0xC240 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(-10), bfloat16::bfloat16(2)) ); // 0xC240 = -48.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(-2), bfloat16::bfloat16(10)) ); // 0x0 = 0.0 bf16 REQUIRE(0xC170 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0xC170 = -15.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(-5), bfloat16::bfloat16(10)) ); // 0x0 = 0.0 bf16 //positive 2x negative 1x REQUIRE(0xC240 == bfloat16::fma(bfloat16::bfloat16(-10), bfloat16::bfloat16(5), bfloat16::bfloat16(2)) ); // 0xC240 = -48.0 bf16 REQUIRE(0xC170 == bfloat16::fma(bfloat16::bfloat16(-10), bfloat16::bfloat16(2), bfloat16::bfloat16(5)) ); // 0xC170 = -15.0 bf16 REQUIRE(0xC240 == bfloat16::fma(bfloat16::bfloat16(-5), bfloat16::bfloat16(10), bfloat16::bfloat16(2)) ); // 0xC240 = -48.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(-5), bfloat16::bfloat16(2), bfloat16::bfloat16(10)) ); // 0x0 = 0.0 bf16 REQUIRE(0xC170 == bfloat16::fma(bfloat16::bfloat16(-2), bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0xC170 = -15.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(-2), bfloat16::bfloat16(5), bfloat16::bfloat16(10)) ); // 0x0 = 0.0 bf16 //positive 1x negative x2 REQUIRE(0xC250 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(-5), bfloat16::bfloat16(-2)) ); // 0xC250 = -52.0 bf16 REQUIRE(0xC1C8 == bfloat16::fma(bfloat16::bfloat16(10), bfloat16::bfloat16(-2), bfloat16::bfloat16(-5)) ); // 0xC1C8 = -25.0 bf16 REQUIRE(0xC250 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(-10), bfloat16::bfloat16(-2)) ); // 0xC250 = -52.0 bf16 REQUIRE(0xC1A0 == bfloat16::fma(bfloat16::bfloat16(5), bfloat16::bfloat16(-2), bfloat16::bfloat16(-10)) ); // 0xC1A0 = -20.0 bf16 REQUIRE(0xC1C8 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(-10), bfloat16::bfloat16(-5)) ); // 0xC1C8 = -25.0 bf16 REQUIRE(0xC1A0 == bfloat16::fma(bfloat16::bfloat16(2), bfloat16::bfloat16(-5), bfloat16::bfloat16(-10)) ); // 0xC1A0 = -20.0 bf16 //positive 1x negative x2 REQUIRE(0x4250 == bfloat16::fma(bfloat16::bfloat16(-10), bfloat16::bfloat16(-5), bfloat16::bfloat16(2)) ); // 0x4250 = 52.0 bf16 REQUIRE(0x41C8 == bfloat16::fma(bfloat16::bfloat16(-10), bfloat16::bfloat16(-2), bfloat16::bfloat16(5)) ); // 0x41C8 = 25.0 bf16 REQUIRE(0x4250 == bfloat16::fma(bfloat16::bfloat16(-5), bfloat16::bfloat16(-10), bfloat16::bfloat16(2)) ); // 0x4250 = 52.0 bf16 REQUIRE(0x41A0 == bfloat16::fma(bfloat16::bfloat16(-5), bfloat16::bfloat16(-2), bfloat16::bfloat16(10)) ); // 0x41A0 = 20.0 bf16 REQUIRE(0x41C8 == bfloat16::fma(bfloat16::bfloat16(-2), bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0x41C8 = 25.0 bf16 REQUIRE(0x41A0 == bfloat16::fma(bfloat16::bfloat16(-2), bfloat16::bfloat16(-5), bfloat16::bfloat16(10)) ); // 0x41A0 = 20.0 bf16 //positive 0x negative x3 REQUIRE(0x4240 == bfloat16::fma(bfloat16::bfloat16(-10), bfloat16::bfloat16(-5), bfloat16::bfloat16(-2)) ); // 0x4240 = 48.0 bf16 REQUIRE(0x4170 == bfloat16::fma(bfloat16::bfloat16(-10), bfloat16::bfloat16(-2), bfloat16::bfloat16(-5)) ); // 0x4170 = 15.0 bf16 REQUIRE(0x4240 == bfloat16::fma(bfloat16::bfloat16(-5), bfloat16::bfloat16(-10), bfloat16::bfloat16(-2)) ); // 0x4240 = 48.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(-5), bfloat16::bfloat16(-2), bfloat16::bfloat16(-10)) ); // 0x0 = 0.0 bf16 REQUIRE(0x4170 == bfloat16::fma(bfloat16::bfloat16(-2), bfloat16::bfloat16(-10), bfloat16::bfloat16(-5)) ); // 0x4170 = 15.0 bf16 REQUIRE(0x0 == bfloat16::fma(bfloat16::bfloat16(-2), bfloat16::bfloat16(-5), bfloat16::bfloat16(-10)) ); // 0x0 = 0.0 bf16 } TEST_CASE("bfloat16_fmax", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x4000 == bfloat16::fmax(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(0x4000 == bfloat16::fmax(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) ); REQUIRE(isnan(bfloat16::fmax(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //numeric values REQUIRE(0x3F00 == bfloat16::fmax(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.1)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x3DCC == bfloat16::fmax(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(0.1)) ); // 0x3DCC = 0.1 bf16 REQUIRE(0x3F00 == bfloat16::fmax(bfloat16::bfloat16(0.5), bfloat16::bfloat16(-0.1)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0xBDCC == bfloat16::fmax(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.1)) ); // 0xBDCC = -0.1 bf16 REQUIRE(0x4120 == bfloat16::fmax(bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0x4120 = 10 bf16 REQUIRE(0x40A0 == bfloat16::fmax(bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0x40A0 = 5 bf16 REQUIRE(0x4120 == bfloat16::fmax(bfloat16::bfloat16(10), bfloat16::bfloat16(-5)) ); // 0x4120 = 10 bf16 REQUIRE(0xC0A0 == bfloat16::fmax(bfloat16::bfloat16(-10), bfloat16::bfloat16(-5)) ); // 0xC0A0 = -5 bf16 REQUIRE(0x4128 == bfloat16::fmax(bfloat16::bfloat16(10.5), bfloat16::bfloat16(5.5)) ); // 0x4128 = 10.5 bf16 REQUIRE(0x40B0 == bfloat16::fmax(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(5.5)) ); // 0x40B0 = 5.5 bf16 REQUIRE(0x4128 == bfloat16::fmax(bfloat16::bfloat16(10.5), bfloat16::bfloat16(-5.5)) ); // 0x4128 = 10.5 bf16 REQUIRE(0xC0B0 == bfloat16::fmax(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-5.5)) ); // 0xC0B0 = -5.5 bf16 } TEST_CASE("bfloat16_fmin", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x4000 == bfloat16::fmin(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(0x4000 == bfloat16::fmin(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) ); REQUIRE(isnan(bfloat16::fmin(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //numeric values REQUIRE(0x3DCC == bfloat16::fmin(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.1)) ); // 0x3DCC = 0.1 bf16 REQUIRE(0xBF00 == bfloat16::fmin(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(0.1)) ); // 0xBF00 = -0.5 bf16 REQUIRE(0xBDCC == bfloat16::fmin(bfloat16::bfloat16(0.5), bfloat16::bfloat16(-0.1)) ); // 0xBDCC = -0.1 bf16 REQUIRE(0xBF00 == bfloat16::fmin(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.1)) ); // 0xBF00 = -0.5 bf16 REQUIRE(0x40A0 == bfloat16::fmin(bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0xC120 == bfloat16::fmin(bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0xC120 = -10.0 bf16 REQUIRE(0xC0A0 == bfloat16::fmin(bfloat16::bfloat16(10), bfloat16::bfloat16(-5)) ); // 0xC0A0 = -5.0 bf16 REQUIRE(0xC120 == bfloat16::fmin(bfloat16::bfloat16(-10), bfloat16::bfloat16(-5)) ); // 0xC120 = -10.0 bf16 REQUIRE(0x40B0 == bfloat16::fmin(bfloat16::bfloat16(10.5), bfloat16::bfloat16(5.5)) ); // 0x40B0 = 5.5 bf16 REQUIRE(0xC128 == bfloat16::fmin(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(5.5)) ); // 0xC128 = -10.5 bf16 REQUIRE(0xC0B0 == bfloat16::fmin(bfloat16::bfloat16(10.5), bfloat16::bfloat16(-5.5)) ); // 0xC0B0 = -5.5 bf16 REQUIRE(0xC128 == bfloat16::fmin(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-5.5)) ); // 0xC128 = -10.5 bf16 } TEST_CASE("bfloat16_fdim", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::fdim(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16()))); REQUIRE(isnan(bfloat16::fdim(bfloat16::bfloat16(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()))); //numeric values REQUIRE(0x40A0 == bfloat16::fdim(bfloat16::bfloat16(10), bfloat16::bfloat16(5)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x0 == bfloat16::fdim(bfloat16::bfloat16(-10), bfloat16::bfloat16(5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x4170 == bfloat16::fdim(bfloat16::bfloat16(10), bfloat16::bfloat16(-5)) ); // 0x4170 = 15.0 bf16 REQUIRE(0x0 == bfloat16::fdim(bfloat16::bfloat16(-10), bfloat16::bfloat16(-5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x40A0 == bfloat16::fdim(bfloat16::bfloat16(10.5), bfloat16::bfloat16(5.5)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x0 == bfloat16::fdim(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(5.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x4180 == bfloat16::fdim(bfloat16::bfloat16(10.5), bfloat16::bfloat16(-5.5)) ); // 0x4180 = 16.0 bf16 REQUIRE(0x0 == bfloat16::fdim(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-5.5)) ); // 0x0 = 0.0 bf16 } TEST_CASE("bfloat16_nanh", "[float16]"){ //works REQUIRE(isnan(bfloat16::nanh("h") )); } TEST_CASE("bfloat16_exp", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x3F80 == bfloat16::exp(bfloat16::bfloat16(-0)) ); REQUIRE(0x0 == bfloat16::exp(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::exp(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::exp(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x3F80 == bfloat16::exp(bfloat16::bfloat16(0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x402D == bfloat16::exp(bfloat16::bfloat16(1)) ); // 0x402D = 2.703125 bf16 REQUIRE(0x40EC == bfloat16::exp(bfloat16::bfloat16(2)) ); // 0x40EC = 7.375 bf16 REQUIRE(0x41A0 == bfloat16::exp(bfloat16::bfloat16(3)) ); // 0x41A0 = 20.0 bf16 REQUIRE(0x425A == bfloat16::exp(bfloat16::bfloat16(4)) ); // 0x425A = 54.5 bf16 REQUIRE(0x4314 == bfloat16::exp(bfloat16::bfloat16(5)) ); // 0x4314 = 148.0 bf16 REQUIRE(0x43C9 == bfloat16::exp(bfloat16::bfloat16(6)) ); // 0x43C9 = 402.0 bf16 REQUIRE(0x4489 == bfloat16::exp(bfloat16::bfloat16(7)) ); // 0x4489 = 1096.0 bf16 REQUIRE(0x453A == bfloat16::exp(bfloat16::bfloat16(8)) ); // 0x453A = 2976.0 bf16 REQUIRE(0x45FD == bfloat16::exp(bfloat16::bfloat16(9)) ); // 0x45FD = 8096.0 bf16 REQUIRE(0x46AC == bfloat16::exp(bfloat16::bfloat16(10)) ); // 0x46AC = 22016.0 bf16 REQUIRE(0x4101 == bfloat16::exp(bfloat16::bfloat16(2.1)) ); // 0x4101 = 8.0625 bf16 REQUIRE(0x410E == bfloat16::exp(bfloat16::bfloat16(2.2)) ); // 0x410E = 8.875 bf16 REQUIRE(0x411F == bfloat16::exp(bfloat16::bfloat16(2.3)) ); // 0x411F = 9.9375 bf16 REQUIRE(0x412E == bfloat16::exp(bfloat16::bfloat16(2.4)) ); // 0x412E = 10.875 bf16 REQUIRE(0x4142 == bfloat16::exp(bfloat16::bfloat16(2.5)) ); // 0x4142 = 12.125 bf16 REQUIRE(0x4156 == bfloat16::exp(bfloat16::bfloat16(2.6)) ); // 0x4156 = 13.375 bf16 REQUIRE(0x416B == bfloat16::exp(bfloat16::bfloat16(2.7)) ); // 0x416B = 14.6875 bf16 REQUIRE(0x4183 == bfloat16::exp(bfloat16::bfloat16(2.8)) ); // 0x4183 = 16.375 bf16 REQUIRE(0x4190 == bfloat16::exp(bfloat16::bfloat16(2.9)) ); // 0x4190 = 18.0 bf16 //negative REQUIRE(0x3EBC == bfloat16::exp(bfloat16::bfloat16(-1)) ); // 0x3EBC = 0.3671875 bf16 REQUIRE(0x3E0A == bfloat16::exp(bfloat16::bfloat16(-2)) ); // 0x3E0A = 0.134765625 bf16 REQUIRE(0x3D4B == bfloat16::exp(bfloat16::bfloat16(-3)) ); // 0x3D4B = 0.049560546875 bf16 REQUIRE(0x3C96 == bfloat16::exp(bfloat16::bfloat16(-4)) ); // 0x3C96 = 0.018310546875 bf16 REQUIRE(0x3BDC == bfloat16::exp(bfloat16::bfloat16(-5)) ); // 0x3BDC = 0.0067138671875 bf16 REQUIRE(0x3B22 == bfloat16::exp(bfloat16::bfloat16(-6)) ); // 0x3B22 = 0.002471923828125 bf16 REQUIRE(0x3A6F == bfloat16::exp(bfloat16::bfloat16(-7)) ); // 0x3A6F = 0.000911712646484375 bf16 REQUIRE(0x39AF == bfloat16::exp(bfloat16::bfloat16(-8)) ); // 0x39AF = 0.0003337860107421875 bf16 REQUIRE(0x3901 == bfloat16::exp(bfloat16::bfloat16(-9)) ); // 0x3901 = 0.00012302398681640625 bf16 REQUIRE(0x02FA == bfloat16::exp(bfloat16::bfloat16(-10)) ); // 0x02FA = 3.67341984632e-37 bf16 REQUIRE(0x3DFC == bfloat16::exp(bfloat16::bfloat16(-2.1)) ); // 0x3DFC = 0.123046875 bf16 REQUIRE(0x3DE5 == bfloat16::exp(bfloat16::bfloat16(-2.2)) ); // 0x3DE5 = 0.11181640625 bf16 REQUIRE(0x3DCD == bfloat16::exp(bfloat16::bfloat16(-2.3)) ); // 0x3DCD = 0.10009765625 bf16 REQUIRE(0x3DBB == bfloat16::exp(bfloat16::bfloat16(-2.4)) ); // 0x3DBB = 0.09130859375 bf16 REQUIRE(0x3DA8 == bfloat16::exp(bfloat16::bfloat16(-2.5)) ); // 0x3DA8 = 0.08203125 bf16 REQUIRE(0x3D99 == bfloat16::exp(bfloat16::bfloat16(-2.6)) ); // 0x3D99 = 0.07470703125 bf16 REQUIRE(0x3D8B == bfloat16::exp(bfloat16::bfloat16(-2.7)) ); // 0x3D8B = 0.06787109375 bf16 REQUIRE(0x3D79 == bfloat16::exp(bfloat16::bfloat16(-2.8)) ); // 0x3D79 = 0.060791015625 bf16 REQUIRE(0x3D63 == bfloat16::exp(bfloat16::bfloat16(-2.9)) ); // 0x3D63 = 0.055419921875 bf16 } TEST_CASE("bfloat16_exp2", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x3F80 == bfloat16::exp2(bfloat16::bfloat16(-0)) ); REQUIRE(0x0 == bfloat16::exp2(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::exp2(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::exp2(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x3F80 == bfloat16::exp2(bfloat16::bfloat16(0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::exp2(bfloat16::bfloat16(1)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4080 == bfloat16::exp2(bfloat16::bfloat16(2)) ); // 0x4080 = 4.0 bf16 REQUIRE(0x4100 == bfloat16::exp2(bfloat16::bfloat16(3)) ); // 0x4100 = 8.0 bf16 REQUIRE(0x4180 == bfloat16::exp2(bfloat16::bfloat16(4)) ); // 0x4180 = 16.0 bf16 REQUIRE(0x4200 == bfloat16::exp2(bfloat16::bfloat16(5)) ); // 0x4200 = 32.0 bf16 REQUIRE(0x4280 == bfloat16::exp2(bfloat16::bfloat16(6)) ); // 0x4280 = 64.0 bf16 REQUIRE(0x4300 == bfloat16::exp2(bfloat16::bfloat16(7)) ); // 0x4300 = 128.0 bf16 REQUIRE(0x4380 == bfloat16::exp2(bfloat16::bfloat16(8)) ); // 0x4380 = 256.0 bf16 REQUIRE(0x4400 == bfloat16::exp2(bfloat16::bfloat16(9)) ); // 0x4400 = 512.0 bf16 REQUIRE(0x4480 == bfloat16::exp2(bfloat16::bfloat16(10)) ); // 0x4480 = 1024.0 bf16 REQUIRE(0x4088 == bfloat16::exp2(bfloat16::bfloat16(2.1)) ); // 0x4088 = 4.25 bf16 REQUIRE(0x4091 == bfloat16::exp2(bfloat16::bfloat16(2.2)) ); // 0x4091 = 4.53125 bf16 REQUIRE(0x409D == bfloat16::exp2(bfloat16::bfloat16(2.3)) ); // 0x409D = 4.90625 bf16 REQUIRE(0x40A7 == bfloat16::exp2(bfloat16::bfloat16(2.4)) ); // 0x40A7 = 5.21875 bf16 REQUIRE(0x40B5 == bfloat16::exp2(bfloat16::bfloat16(2.5)) ); // 0x40B5 = 5.65625 bf16 REQUIRE(0x40C1 == bfloat16::exp2(bfloat16::bfloat16(2.6)) ); // 0x40C1 = 6.03125 bf16 REQUIRE(0x40CE == bfloat16::exp2(bfloat16::bfloat16(2.7)) ); // 0x40CE = 6.4375 bf16 REQUIRE(0x40DE == bfloat16::exp2(bfloat16::bfloat16(2.8)) ); // 0x40DE = 6.9375 bf16 REQUIRE(0x40ED == bfloat16::exp2(bfloat16::bfloat16(2.9)) ); // 0x40ED = 7.40625 bf16 //negative REQUIRE(0x3F00 == bfloat16::exp2(bfloat16::bfloat16(-1)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x3E80 == bfloat16::exp2(bfloat16::bfloat16(-2)) ); // 0x3E80 = 0.25 bf16 REQUIRE(0x3E00 == bfloat16::exp2(bfloat16::bfloat16(-3)) ); // 0x3E00 = 0.125 bf16 REQUIRE(0x3D80 == bfloat16::exp2(bfloat16::bfloat16(-4)) ); // 0x3D80 = 0.0625 bf16 REQUIRE(0x3D00 == bfloat16::exp2(bfloat16::bfloat16(-5)) ); // 0x3D00 = 0.03125 bf16 REQUIRE(0x3C80 == bfloat16::exp2(bfloat16::bfloat16(-6)) ); // 0x3C80 = 0.015625 bf16 REQUIRE(0x3C00 == bfloat16::exp2(bfloat16::bfloat16(-7)) ); // 0x3C00 = 0.0078125 bf16 REQUIRE(0x3B80 == bfloat16::exp2(bfloat16::bfloat16(-8)) ); // 0x3B80 = 0.00390625 bf16 REQUIRE(0x3B00 == bfloat16::exp2(bfloat16::bfloat16(-9)) ); // 0x3B00 = 0.001953125 bf16 REQUIRE(0x3A80 == bfloat16::exp2(bfloat16::bfloat16(-10)) ); // 0x3A80 = 0.0009765625 bf16 REQUIRE(0x3E6F == bfloat16::exp2(bfloat16::bfloat16(-2.1)) ); // 0x3E6F = 0.2333984375 bf16 REQUIRE(0x3E60 == bfloat16::exp2(bfloat16::bfloat16(-2.2)) ); // 0x3E60 = 0.21875 bf16 REQUIRE(0x3E50 == bfloat16::exp2(bfloat16::bfloat16(-2.3)) ); // 0x3E50 = 0.203125 bf16 REQUIRE(0x3E43 == bfloat16::exp2(bfloat16::bfloat16(-2.4)) ); // 0x3E43 = 0.1904296875 bf16 REQUIRE(0x3E35 == bfloat16::exp2(bfloat16::bfloat16(-2.5)) ); // 0x3E35 = 0.1767578125 bf16 REQUIRE(0x3E29 == bfloat16::exp2(bfloat16::bfloat16(-2.6)) ); // 0x3E29 = 0.1650390625 bf16 REQUIRE(0x3E1E == bfloat16::exp2(bfloat16::bfloat16(-2.7)) ); // 0x3E1E = 0.154296875 bf16 REQUIRE(0x3E13 == bfloat16::exp2(bfloat16::bfloat16(-2.8)) ); // 0x3E13 = 0.1435546875 bf16 REQUIRE(0x3E0A == bfloat16::exp2(bfloat16::bfloat16(-2.9)) ); // 0x3E0A = 0.134765625 bf16 } TEST_CASE("bfloat16_expm1", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::expm1(bfloat16::bfloat16(-0)) ); REQUIRE(0x0 == bfloat16::expm1(bfloat16::bfloat16(0)) ); REQUIRE(0xBF80 == bfloat16::expm1(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::expm1(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::expm1(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::expm1(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3FDB == bfloat16::expm1(bfloat16::bfloat16(1)) ); // 0x3FDB = 1.7109375 bf16 REQUIRE(0x40CC == bfloat16::expm1(bfloat16::bfloat16(2)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x4198 == bfloat16::expm1(bfloat16::bfloat16(3)) ); // 0x4198 = 19.0 bf16 REQUIRE(0x4256 == bfloat16::expm1(bfloat16::bfloat16(4)) ); // 0x4256 = 53.5 bf16 REQUIRE(0x4313 == bfloat16::expm1(bfloat16::bfloat16(5)) ); // 0x4313 = 147.0 bf16 REQUIRE(0x43C9 == bfloat16::expm1(bfloat16::bfloat16(6)) ); // 0x43C9 = 402.0 bf16 REQUIRE(0x4488 == bfloat16::expm1(bfloat16::bfloat16(7)) ); // 0x4488 = 1088.0 bf16 REQUIRE(0x453A == bfloat16::expm1(bfloat16::bfloat16(8)) ); // 0x453A = 2976.0 bf16 REQUIRE(0x45FD == bfloat16::expm1(bfloat16::bfloat16(9)) ); // 0x45FD = 8096.0 bf16 REQUIRE(0x46AC == bfloat16::expm1(bfloat16::bfloat16(10)) ); // 0x46AC = 22016.0 bf16 REQUIRE(0x40E3 == bfloat16::expm1(bfloat16::bfloat16(2.1)) ); // 0x40E3 = 7.09375 bf16 REQUIRE(0x40FD == bfloat16::expm1(bfloat16::bfloat16(2.2)) ); // 0x40FD = 7.90625 bf16 REQUIRE(0x410F == bfloat16::expm1(bfloat16::bfloat16(2.3)) ); // 0x410F = 8.9375 bf16 REQUIRE(0x411E == bfloat16::expm1(bfloat16::bfloat16(2.4)) ); // 0x411E = 9.875 bf16 REQUIRE(0x4132 == bfloat16::expm1(bfloat16::bfloat16(2.5)) ); // 0x4132 = 11.125 bf16 REQUIRE(0x4146 == bfloat16::expm1(bfloat16::bfloat16(2.6)) ); // 0x4146 = 12.375 bf16 REQUIRE(0x415B == bfloat16::expm1(bfloat16::bfloat16(2.7)) ); // 0x415B = 13.6875 bf16 REQUIRE(0x4176 == bfloat16::expm1(bfloat16::bfloat16(2.8)) ); // 0x4176 = 15.375 bf16 REQUIRE(0x4188 == bfloat16::expm1(bfloat16::bfloat16(2.9)) ); // 0x4188 = 17.0 bf16 //negative REQUIRE(0xBF21 == bfloat16::expm1(bfloat16::bfloat16(-1)) ); // 0xBF21 = -0.62890625 bf16 REQUIRE(0xBF5D == bfloat16::expm1(bfloat16::bfloat16(-2)) ); // 0xBF5D = -0.86328125 bf16 REQUIRE(0xBF73 == bfloat16::expm1(bfloat16::bfloat16(-3)) ); // 0xBF73 = -0.94921875 bf16 REQUIRE(0xBF7B == bfloat16::expm1(bfloat16::bfloat16(-4)) ); // 0xBF7B = -0.98046875 bf16 REQUIRE(0xBF7E == bfloat16::expm1(bfloat16::bfloat16(-5)) ); // 0xBF7E = -0.9921875 bf16 REQUIRE(0xBF7F == bfloat16::expm1(bfloat16::bfloat16(-6)) ); // 0xBF7F = -0.99609375 bf16 REQUIRE(0xBF7F == bfloat16::expm1(bfloat16::bfloat16(-7)) ); // 0xBF7F = -0.99609375 bf16 REQUIRE(0xBF7F == bfloat16::expm1(bfloat16::bfloat16(-8)) ); // 0xBF7F = -0.99609375 bf16 REQUIRE(0xBF7F == bfloat16::expm1(bfloat16::bfloat16(-9)) ); // 0xBF7F = -0.99609375 bf16 REQUIRE(0xBF7F == bfloat16::expm1(bfloat16::bfloat16(-10)) ); // 0xBF7F = -0.99609375 bf16 REQUIRE(0xBF60 == bfloat16::expm1(bfloat16::bfloat16(-2.1)) ); // 0xBF60 = -0.875 bf16 REQUIRE(0xBF63 == bfloat16::expm1(bfloat16::bfloat16(-2.2)) ); // 0xBF63 = -0.88671875 bf16 REQUIRE(0xBF66 == bfloat16::expm1(bfloat16::bfloat16(-2.3)) ); // 0xBF66 = -0.8984375 bf16 REQUIRE(0xBF68 == bfloat16::expm1(bfloat16::bfloat16(-2.4)) ); // 0xBF68 = -0.90625 bf16 REQUIRE(0xBF6A == bfloat16::expm1(bfloat16::bfloat16(-2.5)) ); // 0xBF6A = -0.9140625 bf16 REQUIRE(0xBF6C == bfloat16::expm1(bfloat16::bfloat16(-2.6)) ); // 0xBF6C = -0.921875 bf16 REQUIRE(0xBF6E == bfloat16::expm1(bfloat16::bfloat16(-2.7)) ); // 0xBF6E = -0.9296875 bf16 REQUIRE(0xBF70 == bfloat16::expm1(bfloat16::bfloat16(-2.8)) ); // 0xBF70 = -0.9375 bf16 REQUIRE(0xBF71 == bfloat16::expm1(bfloat16::bfloat16(-2.9)) ); // 0xBF71 = -0.94140625 bf16 } TEST_CASE("bfloat16_log", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::log(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::log(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0xFF80 == bfloat16::log(bfloat16::bfloat16(0)) ); // 0xFF80 = -inf bf16 REQUIRE(0x0 == bfloat16::log(bfloat16::bfloat16(1)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F31 == bfloat16::log(bfloat16::bfloat16(2)) ); // 0x3F31 = 0.69140625 bf16 REQUIRE(0x3F8C == bfloat16::log(bfloat16::bfloat16(3)) ); // 0x3F8C = 1.09375 bf16 REQUIRE(0x3FB1 == bfloat16::log(bfloat16::bfloat16(4)) ); // 0x3FB1 = 1.3828125 bf16 REQUIRE(0x3FCE == bfloat16::log(bfloat16::bfloat16(5)) ); // 0x3FCE = 1.609375 bf16 REQUIRE(0x3FE5 == bfloat16::log(bfloat16::bfloat16(6)) ); // 0x3FE5 = 1.7890625 bf16 REQUIRE(0x3FF9 == bfloat16::log(bfloat16::bfloat16(7)) ); // 0x3FF9 = 1.9453125 bf16 REQUIRE(0x4005 == bfloat16::log(bfloat16::bfloat16(8)) ); // 0x4005 = 2.078125 bf16 REQUIRE(0x400C == bfloat16::log(bfloat16::bfloat16(9)) ); // 0x400C = 2.1875 bf16 REQUIRE(0x4013 == bfloat16::log(bfloat16::bfloat16(10)) ); // 0x4013 = 2.296875 bf16 REQUIRE(0x3F3D == bfloat16::log(bfloat16::bfloat16(2.1)) ); // 0x3F3D = 0.73828125 bf16 REQUIRE(0x3F48 == bfloat16::log(bfloat16::bfloat16(2.2)) ); // 0x3F48 = 0.78125 bf16 REQUIRE(0x3F54 == bfloat16::log(bfloat16::bfloat16(2.3)) ); // 0x3F54 = 0.828125 bf16 REQUIRE(0x3F5F == bfloat16::log(bfloat16::bfloat16(2.4)) ); // 0x3F5F = 0.87109375 bf16 REQUIRE(0x3F6A == bfloat16::log(bfloat16::bfloat16(2.5)) ); // 0x3F6A = 0.9140625 bf16 REQUIRE(0x3F73 == bfloat16::log(bfloat16::bfloat16(2.6)) ); // 0x3F73 = 0.94921875 bf16 REQUIRE(0x3F7D == bfloat16::log(bfloat16::bfloat16(2.7)) ); // 0x3F7D = 0.98828125 bf16 REQUIRE(0x3F83 == bfloat16::log(bfloat16::bfloat16(2.8)) ); // 0x3F83 = 1.0234375 bf16 REQUIRE(0x3F87 == bfloat16::log(bfloat16::bfloat16(2.9)) ); // 0x3F87 = 1.0546875 bf16 //negative REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-9)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-10)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log(bfloat16::bfloat16(-2.9)) ); // 0xFFC0 = NaN bf16 } TEST_CASE("bfloat16_log10", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::log10(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::log10(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0xFF80 == bfloat16::log10(bfloat16::bfloat16(0)) ); // 0xFF80 = -inf bf16 REQUIRE(0x0 == bfloat16::log10(bfloat16::bfloat16(1)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3E9A == bfloat16::log10(bfloat16::bfloat16(2)) ); // 0x3E9A = 0.30078125 bf16 REQUIRE(0x3EF4 == bfloat16::log10(bfloat16::bfloat16(3)) ); // 0x3EF4 = 0.4765625 bf16 REQUIRE(0x3F1A == bfloat16::log10(bfloat16::bfloat16(4)) ); // 0x3F1A = 0.6015625 bf16 REQUIRE(0x3F32 == bfloat16::log10(bfloat16::bfloat16(5)) ); // 0x3F32 = 0.6953125 bf16 REQUIRE(0x3F47 == bfloat16::log10(bfloat16::bfloat16(6)) ); // 0x3F47 = 0.77734375 bf16 REQUIRE(0x3F58 == bfloat16::log10(bfloat16::bfloat16(7)) ); // 0x3F58 = 0.84375 bf16 REQUIRE(0x3F67 == bfloat16::log10(bfloat16::bfloat16(8)) ); // 0x3F67 = 0.90234375 bf16 REQUIRE(0x3F74 == bfloat16::log10(bfloat16::bfloat16(9)) ); // 0x3F74 = 0.953125 bf16 REQUIRE(0x3F80 == bfloat16::log10(bfloat16::bfloat16(10)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::log10(bfloat16::bfloat16(100)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x3EA4 == bfloat16::log10(bfloat16::bfloat16(2.1)) ); // 0x3EA4 = bf16 REQUIRE(0x3EAE == bfloat16::log10(bfloat16::bfloat16(2.2)) ); // 0x3EAE = bf16 REQUIRE(0x3EB8 == bfloat16::log10(bfloat16::bfloat16(2.3)) ); // 0x3EB8 = bf16 REQUIRE(0x3EC1 == bfloat16::log10(bfloat16::bfloat16(2.4)) ); // 0x3EC1 = bf16 REQUIRE(0x3ECB == bfloat16::log10(bfloat16::bfloat16(2.5)) ); // 0x3ECB = bf16 REQUIRE(0x3ED3 == bfloat16::log10(bfloat16::bfloat16(2.6)) ); // 0x3ED3 = bf16 REQUIRE(0x3EDB == bfloat16::log10(bfloat16::bfloat16(2.7)) ); // 0x3EDB = bf16 REQUIRE(0x3EE4 == bfloat16::log10(bfloat16::bfloat16(2.8)) ); // 0x3EE4 = bf16 REQUIRE(0x3EEC == bfloat16::log10(bfloat16::bfloat16(2.9)) ); // 0x3EEC = bf16 //negative REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-1)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-3)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-4)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-5)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-6)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-7)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-8)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-9)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-10)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.1)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.2)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.3)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.4)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.5)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.6)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.7)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.8)) ); // 0x7FC0 = NaN bf16 REQUIRE(0x7FC0 == bfloat16::log10(bfloat16::bfloat16(-2.9)) ); // 0x7FC0 = NaN bf16 } TEST_CASE("bfloat16_log2", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::log2(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::log2(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0xFF80 == bfloat16::log2(bfloat16::bfloat16(0)) ); // 0xFF80 = -inf bf16 REQUIRE(0x0 == bfloat16::log2(bfloat16::bfloat16(1)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::log2(bfloat16::bfloat16(2)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3FCA == bfloat16::log2(bfloat16::bfloat16(3)) ); // 0x3FCA = 1.578125 bf16 REQUIRE(0x4000 == bfloat16::log2(bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4014 == bfloat16::log2(bfloat16::bfloat16(5)) ); // 0x4014 = 2.3125 bf16 REQUIRE(0x4025 == bfloat16::log2(bfloat16::bfloat16(6)) ); // 0x4025 = 2.578125 bf16 REQUIRE(0x4033 == bfloat16::log2(bfloat16::bfloat16(7)) ); // 0x4033 = 2.796875 bf16 REQUIRE(0x4040 == bfloat16::log2(bfloat16::bfloat16(8)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x404A == bfloat16::log2(bfloat16::bfloat16(9)) ); // 0x404A = 3.15625 bf16 REQUIRE(0x4054 == bfloat16::log2(bfloat16::bfloat16(10)) ); // 0x4054 = 3.3125 bf16 REQUIRE(0x3F88 == bfloat16::log2(bfloat16::bfloat16(2.1)) ); // 0x3F88 = 1.0625 bf16 REQUIRE(0x3F90 == bfloat16::log2(bfloat16::bfloat16(2.2)) ); // 0x3F90 = 1.125 bf16 REQUIRE(0x3F99 == bfloat16::log2(bfloat16::bfloat16(2.3)) ); // 0x3F99 = 1.1953125 bf16 REQUIRE(0x3FA0 == bfloat16::log2(bfloat16::bfloat16(2.4)) ); // 0x3FA0 = 1.25 bf16 REQUIRE(0x3FA9 == bfloat16::log2(bfloat16::bfloat16(2.5)) ); // 0x3FA9 = 1.3203125 bf16 REQUIRE(0x3FB0 == bfloat16::log2(bfloat16::bfloat16(2.6)) ); // 0x3FB0 = 1.375 bf16 REQUIRE(0x3FB6 == bfloat16::log2(bfloat16::bfloat16(2.7)) ); // 0x3FB6 = 1.421875 bf16 REQUIRE(0x3FBD == bfloat16::log2(bfloat16::bfloat16(2.8)) ); // 0x3FBD = 1.4765625 bf16 REQUIRE(0x3FC4 == bfloat16::log2(bfloat16::bfloat16(2.9)) ); // 0x3FC4 = 1.53125 bf16 REQUIRE(0x3F80 == bfloat16::log2(bfloat16::bfloat16(2)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::log2(bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4040 == bfloat16::log2(bfloat16::bfloat16(8)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4080 == bfloat16::log2(bfloat16::bfloat16(16)) ); // 0x4080 = 4.0 bf16 REQUIRE(0x40A0 == bfloat16::log2(bfloat16::bfloat16(32)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40C0 == bfloat16::log2(bfloat16::bfloat16(64)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40E0 == bfloat16::log2(bfloat16::bfloat16(128)) ); // 0x40E0 = 7.0 bf16 REQUIRE(0x4100 == bfloat16::log2(bfloat16::bfloat16(256)) ); // 0x4100 = 8.0 bf16 REQUIRE(0x4110 == bfloat16::log2(bfloat16::bfloat16(512)) ); // 0x4110 = 9.0 bf16 REQUIRE(0x4120 == bfloat16::log2(bfloat16::bfloat16(1024)) ); // 0x4120 = 10.0 bf16 //negative REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-9)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-10)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log2(bfloat16::bfloat16(-2.9)) ); // 0xFFC0 = NaN bf16 } TEST_CASE("bfloat16_log1p", "[float16]"){ //calculated with the temporary solution //ERRhandling // numeric Limits REQUIRE(0x7F80 == bfloat16::log(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::log(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::log1p(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F31 == bfloat16::log1p(bfloat16::bfloat16(1)) ); // 0x3F31 = 0.69140625 bf16 REQUIRE(0x3F8C == bfloat16::log1p(bfloat16::bfloat16(2)) ); // 0x3F8C = 1.09375 bf16 REQUIRE(0x3FB1 == bfloat16::log1p(bfloat16::bfloat16(3)) ); // 0x3FB1 = 1.3828125 bf16 REQUIRE(0x3FCE == bfloat16::log1p(bfloat16::bfloat16(4)) ); // 0x3FCE = 1.609375 bf16 REQUIRE(0x3FE5 == bfloat16::log1p(bfloat16::bfloat16(5)) ); // 0x3FE5 = 1.7890625 bf16 REQUIRE(0x3FF9 == bfloat16::log1p(bfloat16::bfloat16(6)) ); // 0x3FF9 = 1.9453125 bf16 REQUIRE(0x4005 == bfloat16::log1p(bfloat16::bfloat16(7)) ); // 0x4005 = 2.078125 bf16 REQUIRE(0x400C == bfloat16::log1p(bfloat16::bfloat16(8)) ); // 0x400C = 2.1875 bf16 REQUIRE(0x4013 == bfloat16::log1p(bfloat16::bfloat16(9)) ); // 0x4013 = 2.296875 bf16 REQUIRE(0x4019 == bfloat16::log1p(bfloat16::bfloat16(10)) ); // 0x4019 = 2.390625 bf16 REQUIRE(0x3F90 == bfloat16::log1p(bfloat16::bfloat16(2.1)) ); // 0x3F90 = 1.125 bf16 REQUIRE(0x3F94 == bfloat16::log1p(bfloat16::bfloat16(2.2)) ); // 0x3F94 = 1.15625 bf16 REQUIRE(0x3F98 == bfloat16::log1p(bfloat16::bfloat16(2.3)) ); // 0x3F98 = 1.1875 bf16 REQUIRE(0x3F9C == bfloat16::log1p(bfloat16::bfloat16(2.4)) ); // 0x3F9C = 1.21875 bf16 REQUIRE(0x3FA0 == bfloat16::log1p(bfloat16::bfloat16(2.5)) ); // 0x3FA0 = 1.25 bf16 REQUIRE(0x3FA3 == bfloat16::log1p(bfloat16::bfloat16(2.6)) ); // 0x3FA3 = 1.2734375 bf16 REQUIRE(0x3FA7 == bfloat16::log1p(bfloat16::bfloat16(2.7)) ); // 0x3FA7 = 1.3046875 bf16 REQUIRE(0x3FAA == bfloat16::log1p(bfloat16::bfloat16(2.8)) ); // 0x3FAA = 1.328125 bf16 REQUIRE(0x3FAD == bfloat16::log1p(bfloat16::bfloat16(2.9)) ); // 0x3FAD = 1.3515625 bf16 //negative REQUIRE(0xFF80 == bfloat16::log1p(bfloat16::bfloat16(-1)) ); // 0xFF80 = -inf bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-9)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-10)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::log1p(bfloat16::bfloat16(-2.9)) ); // 0xFFC0 = NaN bf16 } TEST_CASE("bfloat16_sqrt_temp", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::sqrt(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::sqrt(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::sqrt(bfloat16::bfloat16(1)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3FB5 == bfloat16::sqrt(bfloat16::bfloat16(2)) ); // 0x3FB5 = 1.4140625 bf16 REQUIRE(0x3FDD == bfloat16::sqrt(bfloat16::bfloat16(3)) ); // 0x3FDD = 1.7265625 bf16 REQUIRE(0x4000 == bfloat16::sqrt(bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x400F == bfloat16::sqrt(bfloat16::bfloat16(5)) ); // 0x400F = 2.234375 bf16 REQUIRE(0x401C == bfloat16::sqrt(bfloat16::bfloat16(6)) ); // 0x401C = 2.4375 bf16 REQUIRE(0x4029 == bfloat16::sqrt(bfloat16::bfloat16(7)) ); // 0x4029 = 2.640625 bf16 REQUIRE(0x4035 == bfloat16::sqrt(bfloat16::bfloat16(8)) ); // 0x4035 = 2.828125 bf16 REQUIRE(0x4040 == bfloat16::sqrt(bfloat16::bfloat16(9)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x404A == bfloat16::sqrt(bfloat16::bfloat16(10)) ); // 0x404A = 3.15625 bf16 REQUIRE(0x3F80 == bfloat16::sqrt(bfloat16::bfloat16(1)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::sqrt(bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4040 == bfloat16::sqrt(bfloat16::bfloat16(9)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4080 == bfloat16::sqrt(bfloat16::bfloat16(16)) ); // 0x4080 = 4.0 bf16 REQUIRE(0x40A0 == bfloat16::sqrt(bfloat16::bfloat16(25)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40C0 == bfloat16::sqrt(bfloat16::bfloat16(36)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40E0 == bfloat16::sqrt(bfloat16::bfloat16(49)) ); // 0x40E0 = 7.0 bf16 REQUIRE(0x4100 == bfloat16::sqrt(bfloat16::bfloat16(64)) ); // 0x4100 = 8.0 bf16 REQUIRE(0x4110 == bfloat16::sqrt(bfloat16::bfloat16(81)) ); // 0x4110 = 9.0 bf16 REQUIRE(0x4120 == bfloat16::sqrt(bfloat16::bfloat16(100)) ); // 0x4120 = 10.0 bf16 REQUIRE(0x3FB9 == bfloat16::sqrt(bfloat16::bfloat16(2.1)) ); // 0x3FB9 = 1.4453125 bf16 REQUIRE(0x3FBD == bfloat16::sqrt(bfloat16::bfloat16(2.2)) ); // 0x3FBD = 1.4765625 bf16 REQUIRE(0x3FC1 == bfloat16::sqrt(bfloat16::bfloat16(2.3)) ); // 0x3FC1 = 1.5078125 bf16 REQUIRE(0x3FC5 == bfloat16::sqrt(bfloat16::bfloat16(2.4)) ); // 0x3FC5 = 1.5390625 bf16 REQUIRE(0x3FCA == bfloat16::sqrt(bfloat16::bfloat16(2.5)) ); // 0x3FCA = 1.578125 bf16 REQUIRE(0x3FCE == bfloat16::sqrt(bfloat16::bfloat16(2.6)) ); // 0x3FCE = 1.609375 bf16 REQUIRE(0x3FD1 == bfloat16::sqrt(bfloat16::bfloat16(2.7)) ); // 0x3FD1 = 1.6328125 bf16 REQUIRE(0x3FD6 == bfloat16::sqrt(bfloat16::bfloat16(2.8)) ); // 0x3FD6 = 1.671875 bf16 REQUIRE(0x3FD9 == bfloat16::sqrt(bfloat16::bfloat16(2.9)) ); // 0x3FD9 = 1.6953125 bf16 //negative REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-9)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-10)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.1)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.2)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.3)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.4)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.5)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.6)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.7)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.8)) ); // 0xFFC0 = NaN bf16 REQUIRE(0xFFC0 == bfloat16::sqrt(bfloat16::bfloat16(-2.9)) ); // 0xFFC0 = NaN bf16 } TEST_CASE("bfloat16_sqrt", "[floa16]"){ //works //positive REQUIRE(0x4040 == bfloat16::sqrt(bfloat16::bfloat16(9))); REQUIRE(0x4080 == bfloat16::sqrt(bfloat16::bfloat16(16))); REQUIRE(0x40A0 == bfloat16::sqrt(bfloat16::bfloat16(25))); REQUIRE(0x40C0 == bfloat16::sqrt(bfloat16::bfloat16(36))); REQUIRE(0x40E0 == bfloat16::sqrt(bfloat16::bfloat16(49))); REQUIRE(0x4100 == bfloat16::sqrt(bfloat16::bfloat16(64))); REQUIRE(0x4120 == bfloat16::sqrt(bfloat16::bfloat16(100))); } TEST_CASE("bfloat16_cbrt", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::cbrt(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::cbrt(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::cbrt(bfloat16::bfloat16(1)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3FA1 == bfloat16::cbrt(bfloat16::bfloat16(2)) ); // 0x3FA1 = 1.2578125 bf16 REQUIRE(0x3FB8 == bfloat16::cbrt(bfloat16::bfloat16(3)) ); // 0x3FB8 = 1.4375 bf16 REQUIRE(0x3FCB == bfloat16::cbrt(bfloat16::bfloat16(4)) ); // 0x3FCB = 1.5859375 bf16 REQUIRE(0x3FDA == bfloat16::cbrt(bfloat16::bfloat16(5)) ); // 0x3FDA = 1.703125 bf16 REQUIRE(0x3FE8 == bfloat16::cbrt(bfloat16::bfloat16(6)) ); // 0x3FE8 = 1.8125 bf16 REQUIRE(0x3FF4 == bfloat16::cbrt(bfloat16::bfloat16(7)) ); // 0x3FF4 = 1.90625 bf16 REQUIRE(0x4000 == bfloat16::cbrt(bfloat16::bfloat16(8)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4005 == bfloat16::cbrt(bfloat16::bfloat16(9)) ); // 0x4005 = 2.078125 bf16 REQUIRE(0x4009 == bfloat16::cbrt(bfloat16::bfloat16(10)) ); // 0x4009 = 2.140625 bf16 REQUIRE(0x3F80 == bfloat16::cbrt(bfloat16::bfloat16(1)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::cbrt(bfloat16::bfloat16(8)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4040 == bfloat16::cbrt(bfloat16::bfloat16(27)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4080 == bfloat16::cbrt(bfloat16::bfloat16(64)) ); // 0x4080 = 4.0 bf16 REQUIRE(0x40A0 == bfloat16::cbrt(bfloat16::bfloat16(125)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40C0 == bfloat16::cbrt(bfloat16::bfloat16(216)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40DF == bfloat16::cbrt(bfloat16::bfloat16(343)) ); // 0x40DF = 7.0 bf16 REQUIRE(0x4100 == bfloat16::cbrt(bfloat16::bfloat16(512)) ); // 0x4100 = 8.0 bf16 REQUIRE(0x410F == bfloat16::cbrt(bfloat16::bfloat16(729)) ); // 0x410F = 9.0 bf16 REQUIRE(0x4120 == bfloat16::cbrt(bfloat16::bfloat16(1000)) ); // 0x4120 = 10.0 bf16 REQUIRE(0x3FA3 == bfloat16::cbrt(bfloat16::bfloat16(2.1)) ); // 0x3FA3 = 1.2734375 bf16 REQUIRE(0x3FA6 == bfloat16::cbrt(bfloat16::bfloat16(2.2)) ); // 0x3FA6 = 1.296875 bf16 REQUIRE(0x3FA8 == bfloat16::cbrt(bfloat16::bfloat16(2.3)) ); // 0x3FA8 = 1.3125 bf16 REQUIRE(0x3FAB == bfloat16::cbrt(bfloat16::bfloat16(2.4)) ); // 0x3FAB = 1.3359375 bf16 REQUIRE(0x3FAD == bfloat16::cbrt(bfloat16::bfloat16(2.5)) ); // 0x3FAD = 1.3515625 bf16 REQUIRE(0x3FAF == bfloat16::cbrt(bfloat16::bfloat16(2.6)) ); // 0x3FAF = 1.3671875 bf16 REQUIRE(0x3FB1 == bfloat16::cbrt(bfloat16::bfloat16(2.7)) ); // 0x3FB1 = 1.3828125 bf16 REQUIRE(0x3FB4 == bfloat16::cbrt(bfloat16::bfloat16(2.8)) ); // 0x3FB4 = 1.40625 bf16 REQUIRE(0x3FB6 == bfloat16::cbrt(bfloat16::bfloat16(2.9)) ); // 0x3FB6 = 1.421875 bf16 //negative REQUIRE(0xBF80 == bfloat16::cbrt(bfloat16::bfloat16(-1)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBFA1 == bfloat16::cbrt(bfloat16::bfloat16(-2)) ); // 0xBFA1 = -1.2578125 bf16 REQUIRE(0xBFB8 == bfloat16::cbrt(bfloat16::bfloat16(-3)) ); // 0xBFB8 = -1.4375 bf16 REQUIRE(0xBFCB == bfloat16::cbrt(bfloat16::bfloat16(-4)) ); // 0xBFCB = -1.5859375 bf16 REQUIRE(0xBFDA == bfloat16::cbrt(bfloat16::bfloat16(-5)) ); // 0xBFDA = -1.703125 bf16 REQUIRE(0xBFE8 == bfloat16::cbrt(bfloat16::bfloat16(-6)) ); // 0xBFE8 = -1.8125 bf16 REQUIRE(0xBFF4 == bfloat16::cbrt(bfloat16::bfloat16(-7)) ); // 0xBFF4 = -1.90625 bf16 REQUIRE(0xC000 == bfloat16::cbrt(bfloat16::bfloat16(-8)) ); // 0xC000 = -2.0 bf16 REQUIRE(0xC005 == bfloat16::cbrt(bfloat16::bfloat16(-9)) ); // 0xC005 = -2.078125 bf16 REQUIRE(0xC009 == bfloat16::cbrt(bfloat16::bfloat16(-10)) ); // 0xC009 = -2.140625 bf16 REQUIRE(0xBFA3 == bfloat16::cbrt(bfloat16::bfloat16(-2.1)) ); // 0xBFA3 = -1.2734375 bf16 REQUIRE(0xBFA6 == bfloat16::cbrt(bfloat16::bfloat16(-2.2)) ); // 0xBFA6 = -1.296875 bf16 REQUIRE(0xBFA8 == bfloat16::cbrt(bfloat16::bfloat16(-2.3)) ); // 0xBFA8 = -1.3125 bf16 REQUIRE(0xBFAB == bfloat16::cbrt(bfloat16::bfloat16(-2.4)) ); // 0xBFAB = -1.3359375 bf16 REQUIRE(0xBFAD == bfloat16::cbrt(bfloat16::bfloat16(-2.5)) ); // 0xBFAD =-1.3515625 bf16 REQUIRE(0xBFAF == bfloat16::cbrt(bfloat16::bfloat16(-2.6)) ); // 0xBFAF = -1.3671875 bf16 REQUIRE(0xBFB1 == bfloat16::cbrt(bfloat16::bfloat16(-2.7)) ); // 0xBFB1 = -1.3828125 bf16 REQUIRE(0xBFB4 == bfloat16::cbrt(bfloat16::bfloat16(-2.8)) ); // 0xBFB4 = -1.40625 bf16 REQUIRE(0xBFB6 == bfloat16::cbrt(bfloat16::bfloat16(-2.9)) ); // 0xBFB6 = -1.421875 bf16 } TEST_CASE("bfloat16_hypot2", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::hypot(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::hypot(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::hypot(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()))); //positive REQUIRE(0x0 == bfloat16::hypot(bfloat16::bfloat16(0), bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(4)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(3)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(3)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(5)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(5)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(4)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(12)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(12), bfloat16::bfloat16(5)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(12), bfloat16::bfloat16(13)) ); // 0x418D = 17.625 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(13), bfloat16::bfloat16(12)) ); // 0x418D = 17.625 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(13)) ); // 0x415E = 13.875 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(13), bfloat16::bfloat16(5)) ); // 0x415E = 13.875 bf16 //negative 1x REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(4)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(3)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(3)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(5)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(5)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(4)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(12)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(-12), bfloat16::bfloat16(5)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(-12), bfloat16::bfloat16(13)) ); // 0x418D = 17.625 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(-13), bfloat16::bfloat16(12)) ); // 0x418D = 17.625 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(13)) ); // 0x415E = 13.875 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(-13), bfloat16::bfloat16(5)) ); // 0x415E = 13.875 bf16 REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(-4)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(-3)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-3)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(-5)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(-5)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-4)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-12)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(12), bfloat16::bfloat16(-5)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(12), bfloat16::bfloat16(-13)) ); // 0x418D = 17.625 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(13), bfloat16::bfloat16(-12)) ); // 0x418D = 17.625 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-13)) ); // 0x415E = 13.875 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(13), bfloat16::bfloat16(-5)) ); // 0x415E = 13.875 bf16 //negative 2x REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(-4)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40A0 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(-3)) ); // 0x40A0 = 5.0 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-3)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40BA == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(-5)) ); // 0x40BA = 5.8125 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(-5)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x40CC == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-4)) ); // 0x40CC = 6.375 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-12)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x4150 == bfloat16::hypot(bfloat16::bfloat16(-12), bfloat16::bfloat16(-5)) ); // 0x4150 = 13.0 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(-12), bfloat16::bfloat16(-13)) ); // 0x418D = 17.625 bf16 REQUIRE(0x418D == bfloat16::hypot(bfloat16::bfloat16(-13), bfloat16::bfloat16(-12)) ); // 0x418D = 17.625 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-13)) ); // 0x415E = 13.875 bf16 REQUIRE(0x415E == bfloat16::hypot(bfloat16::bfloat16(-13), bfloat16::bfloat16(-5)) ); // 0x415E = 13.875 bf16 } TEST_CASE("bfloat16_hypot3", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::hypot(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::hypot(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::hypot(bfloat16::bfloat16(2), bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::hypot(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::hypot(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::hypot(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::hypot(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive 3x REQUIRE(0x0 == bfloat16::hypot(bfloat16::bfloat16(0), bfloat16::bfloat16(0), bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(4), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(5), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(5), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(3), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(4), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(3), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 //positiv 2x negativ 1x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(4), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(5), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(5), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(3), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(4), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(3), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 //positiv 2x negativ 1x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(-4), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(-5), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(-5), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(-3), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-4), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-3), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 //positiv 2x negativ 1x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(4), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(5), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(5), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(3), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(4), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(3), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 //positiv 1x negativ 2x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(-4), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(-5), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(-5), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(-3), bfloat16::bfloat16(5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-4), bfloat16::bfloat16(3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-3), bfloat16::bfloat16(4)) ); // 0x40E2 = 7.0625 bf16 //positiv 1x negativ 2x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(4), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(5), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(5), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(3), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(4), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(3), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 //positiv 1x negativ 2x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(-4), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(3), bfloat16::bfloat16(-5), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(-5), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(4), bfloat16::bfloat16(-3), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-4), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(5), bfloat16::bfloat16(-3), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 //negativ 3x REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(-4), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-3), bfloat16::bfloat16(-5), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(-5), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-4), bfloat16::bfloat16(-3), bfloat16::bfloat16(-5)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-4), bfloat16::bfloat16(-3)) ); // 0x40E2 = 7.0625 bf16 REQUIRE(0x40E2 == bfloat16::hypot(bfloat16::bfloat16(-5), bfloat16::bfloat16(-3), bfloat16::bfloat16(-4)) ); // 0x40E2 = 7.0625 bf16 } TEST_CASE("bfloat16_pow", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(0), bfloat16::bfloat16(-3)) ); REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(-0), bfloat16::bfloat16(-3)) );//-inf REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(0), bfloat16::bfloat16(-4)) ); REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(-0), bfloat16::bfloat16(-4)) ); REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(0), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(-0), -std::numeric_limits<bfloat16::bfloat16>::infinity() ) ); REQUIRE(0x0 == bfloat16::pow(bfloat16::bfloat16(0), bfloat16::bfloat16(3)) ); REQUIRE(0x0 == bfloat16::pow(bfloat16::bfloat16(-0), bfloat16::bfloat16(3)) ); // -0 REQUIRE(0x0 == bfloat16::pow(bfloat16::bfloat16(0), bfloat16::bfloat16(4)) ); REQUIRE(0x0 == bfloat16::pow(bfloat16::bfloat16(-0), bfloat16::bfloat16(4)) ); REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(-1), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(-1), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(1), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(0x3F80 == bfloat16::pow(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(0)) ); REQUIRE(isnan(bfloat16::pow(bfloat16::bfloat16(-2), bfloat16::bfloat16(1.5)) )); REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(-1), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::pow(bfloat16::bfloat16(2), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(-1), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::pow(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::pow(bfloat16::bfloat16(2), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::pow(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::pow(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(0x8000 == bfloat16::pow(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(-3)) ); REQUIRE(0x0 == bfloat16::pow(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(-1.5)) ); REQUIRE(0x0 == bfloat16::pow(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(-4)) ); REQUIRE(0xFF80 == bfloat16::pow(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(3)) ); REQUIRE(0x7F80 == bfloat16::pow(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(1.5)) ); REQUIRE(0x7F80 == bfloat16::pow(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(4)) ); REQUIRE(0x0 == bfloat16::pow(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(-5)) ); REQUIRE(0x7F80 == bfloat16::pow(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(5)) ); //positive REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(0), bfloat16::bfloat16(0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(1), bfloat16::bfloat16(0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(1), bfloat16::bfloat16(2)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(1), bfloat16::bfloat16(10)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(1)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4080 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(2)) ); // 0x4080 = 4.0 bf16 REQUIRE(0x4100 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(3)) ); // 0x4100 = 8.0 bf16 REQUIRE(0x4180 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(4)) ); // 0x4180 = 16.0 bf16 REQUIRE(0x4200 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(5)) ); // 0x4200 = 32.0 bf16 REQUIRE(0x4280 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(6)) ); // 0x4280 = 64.0 bf16 //negative REQUIRE(0x3F00 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(-1)) ); // 0x3F00 = 0.5 bf16 REQUIRE(0x3e80 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(-2)) ); // 0x3e80 = 0.25 bf16 REQUIRE(0x3e00 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(-3)) ); // 0x3e00 = 0.125 bf16 REQUIRE(0x3d80 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(-4)) ); // 0x3d80 = 0.0625 bf16 REQUIRE(0x3d00 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(-5)) ); // 0x3d00 = 0.03125 bf16 REQUIRE(0x3c80 == bfloat16::pow(bfloat16::bfloat16(2), bfloat16::bfloat16(-6)) ); // 0x3c80 = 0.015625 bf16 } //TODO pointers do something stupid, won't get the result /*TEST_CASE("bfloat16_sincos", "[bfloat16]"){ bfloat16::bfloat16 *sin; bfloat16::bfloat16 *cos; bfloat16::sincos(bfloat16::bfloat16(1), sin, cos); bfloat16::bfloat16 sinv = *sin; bfloat16::bfloat16 cosv = *cos; std::cout << "sin: " << sinv << " cos: " << cosv << std::endl; }*/ TEST_CASE("bfloat16_sin", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::sin(std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::sin(-std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::sin(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::sin(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F57 == bfloat16::sin(bfloat16::bfloat16(1)) ); // 0x3F57 = 0.83984375 bf16 REQUIRE(0x3F68 == bfloat16::sin(bfloat16::bfloat16(2)) ); // 0x3F68 = 0.90625 bf16 REQUIRE(0x3E10 == bfloat16::sin(bfloat16::bfloat16(3)) ); // 0x3E10 = 0.140625 bf16 REQUIRE(0xBF41 == bfloat16::sin(bfloat16::bfloat16(4)) ); // 0xBF41 = -0.75390625 bf16 REQUIRE(0xBF75 == bfloat16::sin(bfloat16::bfloat16(5)) ); // 0xBF75 = -0.95703125 bf16 REQUIRE(0xBE8F == bfloat16::sin(bfloat16::bfloat16(6)) ); // 0xBE8F = -0.279296875 bf16 REQUIRE(0x3F28 == bfloat16::sin(bfloat16::bfloat16(7)) ); // 0x3F28 = 0.65625 bf16 REQUIRE(0x3F7D == bfloat16::sin(bfloat16::bfloat16(8)) ); // 0x3F7D = 0.98828125 bf16 REQUIRE(0x3ED3 == bfloat16::sin(bfloat16::bfloat16(9)) ); // 0x3ED3 = 0.412109375 bf16 //negative REQUIRE(0xBF57 == bfloat16::sin(bfloat16::bfloat16(-1)) ); // 0xBF57 = -0.83984375 bf16 REQUIRE(0xBF68 == bfloat16::sin(bfloat16::bfloat16(-2)) ); // 0xBF68 = -0.90625 bf16 REQUIRE(0xBE10 == bfloat16::sin(bfloat16::bfloat16(-3)) ); // 0xBE10 = -0.140625 bf16 REQUIRE(0x3F41 == bfloat16::sin(bfloat16::bfloat16(-4)) ); // 0x3F41 = 0.75390625 bf16 REQUIRE(0x3F75 == bfloat16::sin(bfloat16::bfloat16(-5)) ); // 0x3F75 = 0.95703125 bf16 REQUIRE(0x3E8F == bfloat16::sin(bfloat16::bfloat16(-6)) ); // 0x3E8F = 0.279296875 bf16 REQUIRE(0xBF28 == bfloat16::sin(bfloat16::bfloat16(-7)) ); // 0xBF28 = -0.65625 bf16 REQUIRE(0xBF7D == bfloat16::sin(bfloat16::bfloat16(-8)) ); // 0xBF7D = -0.98828125 bf16 REQUIRE(0xBED3 == bfloat16::sin(bfloat16::bfloat16(-9)) ); // 0xBED3 = -0.412109375 bf16 } TEST_CASE("bfloat16_cos", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::cos(std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::cos(-std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::cos(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x3F80 == bfloat16::cos(bfloat16::bfloat16(0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F0A == bfloat16::cos(bfloat16::bfloat16(1)) ); // 0x3F0A = 0.5390625 bf16 REQUIRE(0xBED5 == bfloat16::cos(bfloat16::bfloat16(2)) ); // 0xBED5 = -0.416015625 bf16 REQUIRE(0xBF7D == bfloat16::cos(bfloat16::bfloat16(3)) ); // 0xBF7D = -0.98828125 bf16 REQUIRE(0xBF27 == bfloat16::cos(bfloat16::bfloat16(4)) ); // 0xBF27 = -0.65234375 bf16 REQUIRE(0x3E91 == bfloat16::cos(bfloat16::bfloat16(5)) ); // 0x3E91 = 0.283203125 bf16 REQUIRE(0x3F75 == bfloat16::cos(bfloat16::bfloat16(6)) ); // 0x3F75 = 0.95703125 bf16 REQUIRE(0x3F40 == bfloat16::cos(bfloat16::bfloat16(7)) ); // 0x3F40 = 0.75 bf16 REQUIRE(0xBE14 == bfloat16::cos(bfloat16::bfloat16(8)) ); // 0xBE14 = -0.14453125 bf16 REQUIRE(0xBF69 == bfloat16::cos(bfloat16::bfloat16(9)) ); // 0xBF69 = -0.91015625 bf16 //negative REQUIRE(0x3F0A == bfloat16::cos(bfloat16::bfloat16(-1)) ); // 0x3F0A = 0.5390625 bf16 REQUIRE(0xBED5 == bfloat16::cos(bfloat16::bfloat16(-2)) ); // 0xBED5 = -0.416015625 bf16 REQUIRE(0xBF7D == bfloat16::cos(bfloat16::bfloat16(-3)) ); // 0xBF7D = -0.98828125 bf16 REQUIRE(0xBF27 == bfloat16::cos(bfloat16::bfloat16(-4)) ); // 0xBF27 = -0.65234375 bf16 REQUIRE(0x3E91 == bfloat16::cos(bfloat16::bfloat16(-5)) ); // 0x3E91 = 0.283203125 bf16 REQUIRE(0x3F75 == bfloat16::cos(bfloat16::bfloat16(-6)) ); // 0x3F75 = 0.95703125 bf16 REQUIRE(0x3F40 == bfloat16::cos(bfloat16::bfloat16(-7)) ); // 0x3F40 = 0.75 bf16 REQUIRE(0xBE14 == bfloat16::cos(bfloat16::bfloat16(-8)) ); // 0xBE14 = -0.14453125 bf16 REQUIRE(0xBF69 == bfloat16::cos(bfloat16::bfloat16(-9)) ); // 0xBF69 = -0.91015625 bf16 } TEST_CASE("bfloat16_tan", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::tan(std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::tan(-std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::tan(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::tan(bfloat16::bfloat16(0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3FC7 == bfloat16::tan(bfloat16::bfloat16(1)) ); // 0x3FC7 = 1.5546875 bf16 REQUIRE(0xC00B == bfloat16::tan(bfloat16::bfloat16(2)) ); // 0xC00B = -2.171875 bf16 REQUIRE(0xBE11 == bfloat16::tan(bfloat16::bfloat16(3)) ); // 0xBE11 = -0.1416015625 bf16 REQUIRE(0x3F94 == bfloat16::tan(bfloat16::bfloat16(4)) ); // 0x3F94 = 1.15625 bf16 REQUIRE(0xC058 == bfloat16::tan(bfloat16::bfloat16(5)) ); // 0xC058 = -3.375 bf16 REQUIRE(0xBE94 == bfloat16::tan(bfloat16::bfloat16(6)) ); // 0xBE94 = -0.2890625 bf16 REQUIRE(0x3F5F == bfloat16::tan(bfloat16::bfloat16(7)) ); // 0x3F5F = 0.87109375 bf16 REQUIRE(0xC0D9 == bfloat16::tan(bfloat16::bfloat16(8)) ); // 0xC0D9 = -6.78125 bf16 REQUIRE(0xBEE7 == bfloat16::tan(bfloat16::bfloat16(9)) ); // 0xBEE7 = -0.451171875 bf16 //negative REQUIRE(0xBFC7 == bfloat16::tan(bfloat16::bfloat16(-1)) ); // 0xBFC7 = -1.5546875 bf16 REQUIRE(0x400B == bfloat16::tan(bfloat16::bfloat16(-2)) ); // 0x400B = 2.171875 bf16 REQUIRE(0x3E11 == bfloat16::tan(bfloat16::bfloat16(-3)) ); // 0x3E11 = 0.1416015625 bf16 REQUIRE(0xBF94 == bfloat16::tan(bfloat16::bfloat16(-4)) ); // 0xBF94 = -1.15625 bf16 REQUIRE(0x4058 == bfloat16::tan(bfloat16::bfloat16(-5)) ); // 0x4058 = 3.375 bf16 REQUIRE(0x3E94 == bfloat16::tan(bfloat16::bfloat16(-6)) ); // 0x3E94 = 0.2890625 bf16 REQUIRE(0xBF5F == bfloat16::tan(bfloat16::bfloat16(-7)) ); // 0xBF5F = -0.87109375 bf16 REQUIRE(0x40D9 == bfloat16::tan(bfloat16::bfloat16(-8)) ); // 0x40D9 = 6.78125 bf16 REQUIRE(0x3EE7 == bfloat16::tan(bfloat16::bfloat16(-9)) ); // 0x3EE7 = 0.451171875 bf16 } TEST_CASE("bfloat16_asin", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::asin(bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::asin(bfloat16::bfloat16(-2)) )); REQUIRE(isnan(bfloat16::asin(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::asin(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3DCC == bfloat16::asin(bfloat16::bfloat16(0.1)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3E4D == bfloat16::asin(bfloat16::bfloat16(0.2)) ); // 0x3E4D = 0.2001953125 bf16 REQUIRE(0x3E9B == bfloat16::asin(bfloat16::bfloat16(0.3)) ); // 0x3E9B = 0.302734375 bf16 REQUIRE(0x3ED1 == bfloat16::asin(bfloat16::bfloat16(0.4)) ); // 0x3ED1 = 0.408203125 bf16 REQUIRE(0x3F06 == bfloat16::asin(bfloat16::bfloat16(0.5)) ); // 0x3F06 = 0.5234375 bf16 REQUIRE(0x3F23 == bfloat16::asin(bfloat16::bfloat16(0.6)) ); // 0x3F23 = 0.63671875 bf16 REQUIRE(0x3F46 == bfloat16::asin(bfloat16::bfloat16(0.7)) ); // 0x3F46 = 0.7734375 bf16 REQUIRE(0x3F6C == bfloat16::asin(bfloat16::bfloat16(0.8)) ); // 0x3F6C = 0.921875 bf16 REQUIRE(0x3F8E == bfloat16::asin(bfloat16::bfloat16(0.9)) ); // 0x3F8E = 1.109375 bf16 REQUIRE(0x3FC9 == bfloat16::asin(bfloat16::bfloat16(1.0)) ); // 0x3FC9 = 1.5703125 bf16 //negative REQUIRE(0xBDCC == bfloat16::asin(bfloat16::bfloat16(-0.1)) ); // 0xBDCC = -0.099609375 bf16 REQUIRE(0xBE4D == bfloat16::asin(bfloat16::bfloat16(-0.2)) ); // 0xBE4D = -0.2001953125 bf16 REQUIRE(0xBE9B == bfloat16::asin(bfloat16::bfloat16(-0.3)) ); // 0xBE9B = -0.302734375 bf16 REQUIRE(0xBED1 == bfloat16::asin(bfloat16::bfloat16(-0.4)) ); // 0xBED1 = -0.408203125 bf16 REQUIRE(0xBF06 == bfloat16::asin(bfloat16::bfloat16(-0.5)) ); // 0xBF06 = -0.5234375 bf16 REQUIRE(0xBF23 == bfloat16::asin(bfloat16::bfloat16(-0.6)) ); // 0xBF23 = -0.63671875 bf16 REQUIRE(0xBF46 == bfloat16::asin(bfloat16::bfloat16(-0.7)) ); // 0xBF46 = -0.7734375 bf16 REQUIRE(0xBF6C == bfloat16::asin(bfloat16::bfloat16(-0.8)) ); // 0xBF6C = -0.921875 bf16 REQUIRE(0xBF8E == bfloat16::asin(bfloat16::bfloat16(-0.9)) ); // 0xBF8E = -1.109375 bf16 REQUIRE(0xBFC9 == bfloat16::asin(bfloat16::bfloat16(-1.0)) ); // 0xBFC9 = -1.5703125 bf16 } TEST_CASE("bfloat16_acos", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(isnan(bfloat16::acos(bfloat16::bfloat16(2)) )); REQUIRE(isnan(bfloat16::acos(bfloat16::bfloat16(-2)) )); REQUIRE(isnan(bfloat16::acos(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x3FC9 == bfloat16::acos(bfloat16::bfloat16(0.0)) ); // 0x3FC9 = 1.5703125 bf16 REQUIRE(0x3FBC == bfloat16::acos(bfloat16::bfloat16(0.1)) ); // 0x3FBC = 1.46875 bf16 REQUIRE(0x3FAF == bfloat16::acos(bfloat16::bfloat16(0.2)) ); // 0x3FAF = 1.3671875 bf16 REQUIRE(0x3FA2 == bfloat16::acos(bfloat16::bfloat16(0.3)) ); // 0x3FA2 = 1.265625 bf16 REQUIRE(0x3F94 == bfloat16::acos(bfloat16::bfloat16(0.4)) ); // 0x3F94 = 1.15625 bf16 REQUIRE(0x3F86 == bfloat16::acos(bfloat16::bfloat16(0.5)) ); // 0x3F86 = 1.046875 bf16 REQUIRE(0x3F6E == bfloat16::acos(bfloat16::bfloat16(0.6)) ); // 0x3F6E = 0.9296875 bf16 REQUIRE(0x3F4B == bfloat16::acos(bfloat16::bfloat16(0.7)) ); // 0x3F4B = bf16 REQUIRE(0x3F26 == bfloat16::acos(bfloat16::bfloat16(0.8)) ); // 0x3F26 = 0.79296875 bf16 REQUIRE(0x3EE8 == bfloat16::acos(bfloat16::bfloat16(0.9)) ); // 0x3EE8 = 0.453125 bf16 REQUIRE(0x0 == bfloat16::acos(bfloat16::bfloat16(1.0)) ); // 0x0 = 0.0 bf16 //negative REQUIRE(0x3FD5 == bfloat16::acos(bfloat16::bfloat16(-0.1)) ); // 0x3FD5 = 1.6640625 bf16 REQUIRE(0x3FE2 == bfloat16::acos(bfloat16::bfloat16(-0.2)) ); // 0x3FE2 = 1.765625 bf16 REQUIRE(0x3FEF == bfloat16::acos(bfloat16::bfloat16(-0.3)) ); // 0x3FEF = 1.8671875 bf16 REQUIRE(0x3FFD == bfloat16::acos(bfloat16::bfloat16(-0.4)) ); // 0x3FFD = 1.9765625 bf16 REQUIRE(0x4006 == bfloat16::acos(bfloat16::bfloat16(-0.5)) ); // 0x4006 = 2.09375 bf16 REQUIRE(0x400D == bfloat16::acos(bfloat16::bfloat16(-0.6)) ); // 0x400D = 2.203125 bf16 REQUIRE(0x4016 == bfloat16::acos(bfloat16::bfloat16(-0.7)) ); // 0x4016 = 2.34375 bf16 REQUIRE(0x401F == bfloat16::acos(bfloat16::bfloat16(-0.8)) ); // 0x401F = 2.484375 bf16 REQUIRE(0x402B == bfloat16::acos(bfloat16::bfloat16(-0.9)) ); // 0x402B = 2.671875 bf16 REQUIRE(0x4049 == bfloat16::acos(bfloat16::bfloat16(-1.0)) ); // 0x4049 = 3.140625 bf16 } TEST_CASE("bfloat16_atan", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x3FC9 == bfloat16::atan(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xBFC9 == bfloat16::atan(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::sin(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::atan(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3E49 == bfloat16::atan(bfloat16::bfloat16(0.2)) ); // 0x3E49 = 0.1962890625 bf16 REQUIRE(0x3EC2 == bfloat16::atan(bfloat16::bfloat16(0.4)) ); // 0x3EC2 = 0.37890625 bf16 REQUIRE(0x3F09 == bfloat16::atan(bfloat16::bfloat16(0.6)) ); // 0x3F09 = 0.53515625 bf16 REQUIRE(0x3F2C == bfloat16::atan(bfloat16::bfloat16(0.8)) ); // 0x3F2C = 0.671875 bf16 REQUIRE(0x3F49 == bfloat16::atan(bfloat16::bfloat16(1.0)) ); // 0x3F49 = 0.78515625 bf16 REQUIRE(0x3F5F == bfloat16::atan(bfloat16::bfloat16(1.2)) ); // 0x3F5F = 0.87109375 bf16 REQUIRE(0x3F73 == bfloat16::atan(bfloat16::bfloat16(1.4)) ); // 0x3F73 = 0.94921875 bf16 REQUIRE(0x3F81 == bfloat16::atan(bfloat16::bfloat16(1.6)) ); // 0x3F81 = 1.0078125 bf16 REQUIRE(0x3F88 == bfloat16::atan(bfloat16::bfloat16(1.8)) ); // 0x3F88 = 1.0625 bf16 REQUIRE(0x3F8D == bfloat16::atan(bfloat16::bfloat16(2.0)) ); // 0x3F8D = 1.1015625 bf16 //negative REQUIRE(0xBE49 == bfloat16::atan(bfloat16::bfloat16(-0.2)) ); // 0xBE49 = -0.1962890625 bf16 REQUIRE(0xBEC2 == bfloat16::atan(bfloat16::bfloat16(-0.4)) ); // 0xBEC2 = -0.37890625 bf16 REQUIRE(0xBF09 == bfloat16::atan(bfloat16::bfloat16(-0.6)) ); // 0xBF09 = -0.53515625 bf16 REQUIRE(0xBF2C == bfloat16::atan(bfloat16::bfloat16(-0.8)) ); // 0xBF2C = -0.671875 bf16 REQUIRE(0xBF49 == bfloat16::atan(bfloat16::bfloat16(-1.0)) ); // 0xBF49 = -0.78515625 bf16 REQUIRE(0xBF5F == bfloat16::atan(bfloat16::bfloat16(-1.2)) ); // 0xBF5F = -0.87109375 bf16 REQUIRE(0xBF73 == bfloat16::atan(bfloat16::bfloat16(-1.4)) ); // 0xBF73 = -0.94921875 bf16 REQUIRE(0xBF81 == bfloat16::atan(bfloat16::bfloat16(-1.6)) ); // 0xBF81 = -1.0078125 bf16 REQUIRE(0xBF88 == bfloat16::atan(bfloat16::bfloat16(-1.8)) ); // 0xBF88 = -1.0625 bf16 REQUIRE(0xBF8D == bfloat16::atan(bfloat16::bfloat16(-2.0)) ); // 0xBF8D = -1.1015625 bf16 } TEST_CASE("bfloat16_atan2", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(-0), bfloat16::bfloat16(-0)) ); // should be -+pi == 0x4049/0xC049 REQUIRE(0x4049 == bfloat16::atan2(bfloat16::bfloat16(-0), bfloat16::bfloat16(-3)) ); REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(-0), bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(-0), bfloat16::bfloat16(3)) ); REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(0), bfloat16::bfloat16(-0)) ); REQUIRE(0x4049 == bfloat16::atan2(bfloat16::bfloat16(0), bfloat16::bfloat16(-3)) ); REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(0), bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(0), bfloat16::bfloat16(3)) ); REQUIRE(0x3FC9 == bfloat16::atan2(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(3)) ); REQUIRE(0x3FC9 == bfloat16::atan2(std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(-3)) ); REQUIRE(0xBFC9 == bfloat16::atan2(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(3)) ); REQUIRE(0xBFC9 == bfloat16::atan2(-std::numeric_limits<bfloat16::bfloat16>::infinity(), bfloat16::bfloat16(-3)) ); REQUIRE(0x4016 == bfloat16::atan2(std::numeric_limits<bfloat16::bfloat16>::infinity(), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xC016 == bfloat16::atan2(-std::numeric_limits<bfloat16::bfloat16>::infinity(), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x3F49 == bfloat16::atan2(std::numeric_limits<bfloat16::bfloat16>::infinity(), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xBF49 == bfloat16::atan2(-std::numeric_limits<bfloat16::bfloat16>::infinity(), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xBFC9 == bfloat16::atan2(bfloat16::bfloat16(-3), bfloat16::bfloat16(0)) ); REQUIRE(0xBFC9 == bfloat16::atan2(bfloat16::bfloat16(-3), bfloat16::bfloat16(-0)) ); REQUIRE(0x3FC9 == bfloat16::atan2(bfloat16::bfloat16(3), bfloat16::bfloat16(0)) ); REQUIRE(0x3FC9 == bfloat16::atan2(bfloat16::bfloat16(3), bfloat16::bfloat16(-0)) ); REQUIRE(0x4049 == bfloat16::atan2(bfloat16::bfloat16(3), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xC049 == bfloat16::atan2(bfloat16::bfloat16(-3), -std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(3), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x8000 == bfloat16::atan2(bfloat16::bfloat16(-3), std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::atan2(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16(3)) )); REQUIRE(isnan(bfloat16::atan2(bfloat16::bfloat16(3), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); REQUIRE(isnan(bfloat16::atan2(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::atan2(bfloat16::bfloat16(0.0), bfloat16::bfloat16(2.0)) ); // 0x0 = 0.0 bf16 = 0 d REQUIRE(0x3E49 == bfloat16::atan2(bfloat16::bfloat16(0.4), bfloat16::bfloat16(2.0)) ); // 0x3E49 = 0.1962890625 bf16 REQUIRE(0x3EC2 == bfloat16::atan2(bfloat16::bfloat16(0.8), bfloat16::bfloat16(2.0)) ); // 0x3EC2 = 0.37890625 bf16 REQUIRE(0x3F09 == bfloat16::atan2(bfloat16::bfloat16(1.2), bfloat16::bfloat16(2.0)) ); // 0x3F09 = 0.53515625 bf16 REQUIRE(0x3F2C == bfloat16::atan2(bfloat16::bfloat16(1.6), bfloat16::bfloat16(2.0)) ); // 0x3F2C = 0.671875 bf16 REQUIRE(0x3F49 == bfloat16::atan2(bfloat16::bfloat16(2.0), bfloat16::bfloat16(2.0)) ); // 0x3F49 = 0.78515625 bf16 REQUIRE(0x3F5F == bfloat16::atan2(bfloat16::bfloat16(2.4), bfloat16::bfloat16(2.0)) ); // 0x3F5F = 0.87109375 bf16 REQUIRE(0x3F73 == bfloat16::atan2(bfloat16::bfloat16(2.8), bfloat16::bfloat16(2.0)) ); // 0x3F73 = 0.94921875 bf16 REQUIRE(0x3F81 == bfloat16::atan2(bfloat16::bfloat16(3.2), bfloat16::bfloat16(2.0)) ); // 0x3F81 = 1.0078125 bf16 REQUIRE(0x3F88 == bfloat16::atan2(bfloat16::bfloat16(3.6), bfloat16::bfloat16(2.0)) ); // 0x3F88 = 1.0625 bf16 REQUIRE(0x3F8D == bfloat16::atan2(bfloat16::bfloat16(4.0), bfloat16::bfloat16(2.0)) ); // 0x3F8D = 1.1015625 bf16 //negative REQUIRE(0xBE49 == bfloat16::atan2(bfloat16::bfloat16(-0.4), bfloat16::bfloat16(2.0)) ); // 0xBE49 = -0.1962890625 bf16 REQUIRE(0xBEC2 == bfloat16::atan2(bfloat16::bfloat16(-0.8), bfloat16::bfloat16(2.0)) ); // 0xBEC2 = -0.37890625 bf16 REQUIRE(0xBF09 == bfloat16::atan2(bfloat16::bfloat16(-1.2), bfloat16::bfloat16(2.0)) ); // 0xBF09 = -0.53515625 bf16 REQUIRE(0xBF2C == bfloat16::atan2(bfloat16::bfloat16(-1.6), bfloat16::bfloat16(2.0)) ); // 0xBF2C = -0.671875 bf16 REQUIRE(0xBF49 == bfloat16::atan2(bfloat16::bfloat16(-2.0), bfloat16::bfloat16(2.0)) ); // 0xBF49 = -0.78515625 bf16 REQUIRE(0xBF5F == bfloat16::atan2(bfloat16::bfloat16(-2.4), bfloat16::bfloat16(2.0)) ); // 0xBF5F = -0.87109375 bf16 REQUIRE(0xBF73 == bfloat16::atan2(bfloat16::bfloat16(-2.8), bfloat16::bfloat16(2.0)) ); // 0xBF73 = -0.94921875 bf16 REQUIRE(0xBF81 == bfloat16::atan2(bfloat16::bfloat16(-3.2), bfloat16::bfloat16(2.0)) ); // 0xBF81 = -1.0078125 bf16 REQUIRE(0xBF88 == bfloat16::atan2(bfloat16::bfloat16(-3.6), bfloat16::bfloat16(2.0)) ); // 0xBF88 = -1.0625 bf16 REQUIRE(0xBF8D == bfloat16::atan2(bfloat16::bfloat16(-4.0), bfloat16::bfloat16(2.0)) ); // 0xBF8D = -1.1015625 bf16 } TEST_CASE("bfloat16_sinh", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::sinh(bfloat16::bfloat16(-0)) ); REQUIRE(0x7F80 == bfloat16::sinh(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xFF80 == bfloat16::sinh(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::sinh(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::sinh(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3E4D == bfloat16::sinh(bfloat16::bfloat16(0.2)) ); // 0x3E4D = 0.2001953125 bf16 REQUIRE(0x3ED1 == bfloat16::sinh(bfloat16::bfloat16(0.4)) ); // 0x3ED1 = 0.408203125 bf16 REQUIRE(0x3F22 == bfloat16::sinh(bfloat16::bfloat16(0.6)) ); // 0x3F22 = 0.6328125 bf16 REQUIRE(0x3F62 == bfloat16::sinh(bfloat16::bfloat16(0.8)) ); // 0x3F62 = 0.8828125 bf16 REQUIRE(0x3F96 == bfloat16::sinh(bfloat16::bfloat16(1.0)) ); // 0x3F96 = 1.171875 bf16 REQUIRE(0x3FC0 == bfloat16::sinh(bfloat16::bfloat16(1.2)) ); // 0x3FC0 = 1.5 bf16 REQUIRE(0x3FF3 == bfloat16::sinh(bfloat16::bfloat16(1.4)) ); // 0x3FF3 = 1.8984375 bf16 REQUIRE(0x4017 == bfloat16::sinh(bfloat16::bfloat16(1.6)) ); // 0x4017 = 2.359375 bf16 REQUIRE(0x403B == bfloat16::sinh(bfloat16::bfloat16(1.8)) ); // 0x403B = 2.921875 bf16 REQUIRE(0x4068 == bfloat16::sinh(bfloat16::bfloat16(2.0)) ); // 0x4068 = 3.625 bf16 //negative REQUIRE(0xBE4D == bfloat16::sinh(bfloat16::bfloat16(-0.2)) ); // 0xBE4D = -0.2001953125 bf16 REQUIRE(0xBED1 == bfloat16::sinh(bfloat16::bfloat16(-0.4)) ); // 0xBED1 = -0.408203125 bf16 REQUIRE(0xBF22 == bfloat16::sinh(bfloat16::bfloat16(-0.6)) ); // 0xBF22 = -0.6328125 bf16 REQUIRE(0xBF62 == bfloat16::sinh(bfloat16::bfloat16(-0.8)) ); // 0xBF62 = -0.8828125 bf16 REQUIRE(0xBF96 == bfloat16::sinh(bfloat16::bfloat16(-1.0)) ); // 0xBF96 = -1.171875 bf16 REQUIRE(0xBFC0 == bfloat16::sinh(bfloat16::bfloat16(-1.2)) ); // 0xBFC0 = -1.5 bf16 REQUIRE(0xBFF3 == bfloat16::sinh(bfloat16::bfloat16(-1.4)) ); // 0xBFF3 = -1.8984375 bf16 REQUIRE(0xC017 == bfloat16::sinh(bfloat16::bfloat16(-1.6)) ); // 0xC017 = -2.359375 bf16 REQUIRE(0xC03B == bfloat16::sinh(bfloat16::bfloat16(-1.8)) ); // 0xC03B = -2.921875 bf16 REQUIRE(0xC068 == bfloat16::sinh(bfloat16::bfloat16(-2.0)) ); // 0xC068 = -3.625 bf16 } TEST_CASE("bfloat16_cosh", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::cosh(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::cosh(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::cosh(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x3F80 == bfloat16::cosh(bfloat16::bfloat16(0.0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F82 == bfloat16::cosh(bfloat16::bfloat16(0.2)) ); // 0x3F82 = 1.015625 bf16 REQUIRE(0x3F8A == bfloat16::cosh(bfloat16::bfloat16(0.4)) ); // 0x3F8A = 1.078125 bf16 REQUIRE(0x3F97 == bfloat16::cosh(bfloat16::bfloat16(0.6)) ); // 0x3F97 = 1.1796875 bf16 REQUIRE(0x3FAA == bfloat16::cosh(bfloat16::bfloat16(0.8)) ); // 0x3FAA = 1.328125 bf16 REQUIRE(0x3FC5 == bfloat16::cosh(bfloat16::bfloat16(1.0)) ); // 0x3FC5 = 1.5390625 bf16 REQUIRE(0x3FE6 == bfloat16::cosh(bfloat16::bfloat16(1.2)) ); // 0x3FE6 = 1.796875 bf16 REQUIRE(0x4009 == bfloat16::cosh(bfloat16::bfloat16(1.4)) ); // 0x4009 = 2.140625 bf16 REQUIRE(0x4024 == bfloat16::cosh(bfloat16::bfloat16(1.6)) ); // 0x4024 = 2.5625 bf16 REQUIRE(0x4046 == bfloat16::cosh(bfloat16::bfloat16(1.8)) ); // 0x4046 = 3.09375 bf16 REQUIRE(0x4070 == bfloat16::cosh(bfloat16::bfloat16(2.0)) ); // 0x4070 = 3.75 bf16 //negative REQUIRE(0x3F82 == bfloat16::cosh(bfloat16::bfloat16(-0.2)) ); // 0x3F82 = 1.015625 bf16 REQUIRE(0x3F8A == bfloat16::cosh(bfloat16::bfloat16(-0.4)) ); // 0x3F8A = 1.078125 bf16 REQUIRE(0x3F97 == bfloat16::cosh(bfloat16::bfloat16(-0.6)) ); // 0x3F97 = 1.1796875 bf16 REQUIRE(0x3FAA == bfloat16::cosh(bfloat16::bfloat16(-0.8)) ); // 0x3FAA = 1.328125 bf16 REQUIRE(0x3FC5 == bfloat16::cosh(bfloat16::bfloat16(-1.0)) ); // 0x3FC5 = 1.5390625 bf16 REQUIRE(0x3FE6 == bfloat16::cosh(bfloat16::bfloat16(-1.2)) ); // 0x3FE6 = 1.796875 bf16 REQUIRE(0x4009 == bfloat16::cosh(bfloat16::bfloat16(-1.4)) ); // 0x4009 = 2.140625 bf16 REQUIRE(0x4024 == bfloat16::cosh(bfloat16::bfloat16(-1.6)) ); // 0x4024 = 2.5625 bf16 REQUIRE(0x4046 == bfloat16::cosh(bfloat16::bfloat16(-1.8)) ); // 0x4046 = 3.09375 bf16 REQUIRE(0x4070 == bfloat16::cosh(bfloat16::bfloat16(-2.0)) ); // 0x4070 = 3.75 bf16 } TEST_CASE("bfloat16_tanh", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x3F80 == bfloat16::tanh(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xBF80 == bfloat16::tanh(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::tanh(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::tanh(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3E49 == bfloat16::tanh(bfloat16::bfloat16(0.2)) ); // 0x3E49 = 0.1962890625 bf16 REQUIRE(0x3EC1 == bfloat16::tanh(bfloat16::bfloat16(0.4)) ); // 0x3EC1 = 0.376953125 bf16 REQUIRE(0x3F09 == bfloat16::tanh(bfloat16::bfloat16(0.6)) ); // 0x3F09 = 0.53515625 bf16 REQUIRE(0x3F29 == bfloat16::tanh(bfloat16::bfloat16(0.8)) ); // 0x3F29 = 0.66015625 bf16 REQUIRE(0x3F42 == bfloat16::tanh(bfloat16::bfloat16(1.0)) ); // 0x3F42 = 0.7578125 bf16 REQUIRE(0x3F55 == bfloat16::tanh(bfloat16::bfloat16(1.2)) ); // 0x3F55 = 0.83203125 bf16 REQUIRE(0x3F62 == bfloat16::tanh(bfloat16::bfloat16(1.4)) ); // 0x3F62 = 0.8828125 bf16 REQUIRE(0x3F6B == bfloat16::tanh(bfloat16::bfloat16(1.6)) ); // 0x3F6B = 0.91796875 bf16 REQUIRE(0x3F72 == bfloat16::tanh(bfloat16::bfloat16(1.8)) ); // 0x3F72 = 0.9453125 bf16 REQUIRE(0x3F76 == bfloat16::tanh(bfloat16::bfloat16(2.0)) ); // 0x3F76 = 0.9609375 bf16 //negative REQUIRE(0xBE49 == bfloat16::tanh(bfloat16::bfloat16(-0.2)) ); // 0xBE49 = -0.1962890625 bf16 REQUIRE(0xBEC1 == bfloat16::tanh(bfloat16::bfloat16(-0.4)) ); // 0xBEC1 = -0.376953125 bf16 REQUIRE(0xBF09 == bfloat16::tanh(bfloat16::bfloat16(-0.6)) ); // 0xBF09 = -0.53515625 bf16 REQUIRE(0xBF29 == bfloat16::tanh(bfloat16::bfloat16(-0.8)) ); // 0xBF29 = -0.66015625 bf16 REQUIRE(0xBF42 == bfloat16::tanh(bfloat16::bfloat16(-1.0)) ); // 0xBF42 = -0.7578125 bf16 REQUIRE(0xBF55 == bfloat16::tanh(bfloat16::bfloat16(-1.2)) ); // 0xBF55 = -0.83203125 bf16 REQUIRE(0xBF62 == bfloat16::tanh(bfloat16::bfloat16(-1.4)) ); // 0xBF62 = -0.8828125 bf16 REQUIRE(0xBF6B == bfloat16::tanh(bfloat16::bfloat16(-1.6)) ); // 0xBF6B = -0.91796875 bf16 REQUIRE(0xBF72 == bfloat16::tanh(bfloat16::bfloat16(-1.8)) ); // 0xBF72 = -0.9453125 bf16 REQUIRE(0xBF76 == bfloat16::tanh(bfloat16::bfloat16(-2.0)) ); // 0xBF76 = -0.9609375 bf16 } TEST_CASE("bfloat16_asinh", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::asinh(bfloat16::bfloat16(-0)) ); REQUIRE(0x7F80 == bfloat16::asinh(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xFF80 == bfloat16::asinh(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::asinh(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::asinh(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3E4A == bfloat16::asinh(bfloat16::bfloat16(0.2)) ); // 0x3E4A = 0.197265625 bf16 REQUIRE(0x3EC6 == bfloat16::asinh(bfloat16::bfloat16(0.4)) ); // 0x3EC6 = 0.38671875 bf16 REQUIRE(0x3F11 == bfloat16::asinh(bfloat16::bfloat16(0.6)) ); // 0x3F11 = 0.56640625 bf16 REQUIRE(0x3F3A == bfloat16::asinh(bfloat16::bfloat16(0.8)) ); // 0x3F3A = 0.7265625 bf16 REQUIRE(0x3F61 == bfloat16::asinh(bfloat16::bfloat16(1.0)) ); // 0x3F61 = 0.87890625 bf16 REQUIRE(0x3F81 == bfloat16::asinh(bfloat16::bfloat16(1.2)) ); // 0x3F81 = 1.0078125 bf16 REQUIRE(0x3F91 == bfloat16::asinh(bfloat16::bfloat16(1.4)) ); // 0x3F91 = 1.1328125 bf16 REQUIRE(0x3F9F == bfloat16::asinh(bfloat16::bfloat16(1.6)) ); // 0x3F9F = 1.2421875 bf16 REQUIRE(0x3FAC == bfloat16::asinh(bfloat16::bfloat16(1.8)) ); // 0x3FAC = 1.34375 bf16 REQUIRE(0x3FB8 == bfloat16::asinh(bfloat16::bfloat16(2.0)) ); // 0x3FB8 = 1.4375 bf16 //negative REQUIRE(0xBE4A == bfloat16::asinh(bfloat16::bfloat16(-0.2)) ); // 0xBE4A = -0.197265625 bf16 REQUIRE(0xBEC6 == bfloat16::asinh(bfloat16::bfloat16(-0.4)) ); // 0xBEC6 = -0.38671875 bf16 REQUIRE(0xBF11 == bfloat16::asinh(bfloat16::bfloat16(-0.6)) ); // 0xBF11 = -0.56640625 bf16 REQUIRE(0xBF3A == bfloat16::asinh(bfloat16::bfloat16(-0.8)) ); // 0xBF3A = -0.7265625 bf16 REQUIRE(0xBF61 == bfloat16::asinh(bfloat16::bfloat16(-1.0)) ); // 0xBF61 = -0.87890625 bf16 REQUIRE(0xBF81 == bfloat16::asinh(bfloat16::bfloat16(-1.2)) ); // 0xBF81 = -1.0078125 bf16 REQUIRE(0xBF91 == bfloat16::asinh(bfloat16::bfloat16(-1.4)) ); // 0xBF91 = -1.1328125 bf16 REQUIRE(0xBF9F == bfloat16::asinh(bfloat16::bfloat16(-1.6)) ); // 0xBF9F = -1.2421875 bf16 REQUIRE(0xBFAC == bfloat16::asinh(bfloat16::bfloat16(-1.8)) ); // 0xBFAC = -1.34375 bf16 REQUIRE(0xBFB8 == bfloat16::asinh(bfloat16::bfloat16(-2.0)) ); // 0xBFB8 = -1.4375 bf16 } TEST_CASE("bfloat16_acosh", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::acosh(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::acosh(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0xffc0 == bfloat16::acosh(bfloat16::bfloat16(0.0)) ); // 0xffc0 = NaN bf16 REQUIRE(0xffc0 == bfloat16::acosh(bfloat16::bfloat16(0.5)) ); // 0xffc0 = NaN bf16 REQUIRE(0x0 == bfloat16::acosh(bfloat16::bfloat16(1.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3FA8 == bfloat16::acosh(bfloat16::bfloat16(2.0)) ); // 0x3FA8 = 1.3125 bf16 REQUIRE(0x3FE1 == bfloat16::acosh(bfloat16::bfloat16(3.0)) ); // 0x3FE1 = 1.7578125 bf16 REQUIRE(0x4004 == bfloat16::acosh(bfloat16::bfloat16(4.0)) ); // 0x4004 = 2.0625 bf16 REQUIRE(0x4012 == bfloat16::acosh(bfloat16::bfloat16(5.0)) ); // 0x4012 = 2.28125 bf16 REQUIRE(0x401E == bfloat16::acosh(bfloat16::bfloat16(6.0)) ); // 0x401E = 2.46875 bf16 //negative REQUIRE(0xffc0 == bfloat16::acosh(bfloat16::bfloat16(-0.5)) ); // 0xffc0 = NaN bf16 REQUIRE(0xffc0 == bfloat16::acosh(bfloat16::bfloat16(-1.0)) ); // 0xffc0 = NaN bf16 REQUIRE(0xffc0 == bfloat16::acosh(bfloat16::bfloat16(-5.0)) ); // 0xffc0 = NaN bf16 } TEST_CASE("bfloat16_atanh", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(std::numeric_limits<bfloat16::bfloat16>::infinity() == bfloat16::atanh(bfloat16::bfloat16(1)) ); REQUIRE(-std::numeric_limits<bfloat16::bfloat16>::infinity() == bfloat16::atanh(bfloat16::bfloat16(-1)) ); REQUIRE(isnan(bfloat16::atanh(bfloat16::bfloat16(3)) )); REQUIRE(isnan(bfloat16::atanh(bfloat16::bfloat16(-3)) )); REQUIRE(isnan(bfloat16::atanh(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //solution in rad //positive REQUIRE(0x0 == bfloat16::atanh(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3DCC == bfloat16::atanh(bfloat16::bfloat16(0.1)) ); // 0x3DCC = 0.099609375 bf16 REQUIRE(0x3E4E == bfloat16::atanh(bfloat16::bfloat16(0.2)) ); // 0x3E4E = 0.201171875 bf16 REQUIRE(0x3E9D == bfloat16::atanh(bfloat16::bfloat16(0.3)) ); // 0x3E9D = 0.306640625 bf16 REQUIRE(0x3ED7 == bfloat16::atanh(bfloat16::bfloat16(0.4)) ); // 0x3ED7 = 0.419921875 bf16 REQUIRE(0x3F0C == bfloat16::atanh(bfloat16::bfloat16(0.5)) ); // 0x3F0C = 0.546875 bf16 REQUIRE(0x3F30 == bfloat16::atanh(bfloat16::bfloat16(0.6)) ); // 0x3F30 = 0.6875 bf16 REQUIRE(0x3F5D == bfloat16::atanh(bfloat16::bfloat16(0.7)) ); // 0x3F5D = 0.86328125 bf16 REQUIRE(0x3F8B == bfloat16::atanh(bfloat16::bfloat16(0.8)) ); // 0x3F8B = 1.0859375 bf16 REQUIRE(0x3FBB == bfloat16::atanh(bfloat16::bfloat16(0.9)) ); // 0x3FBB = 1.4609375 bf16 //negative REQUIRE(0xBDCC == bfloat16::atanh(bfloat16::bfloat16(-0.1)) ); // 0xBDCC = -0.099609375 bf16 REQUIRE(0xBE4E == bfloat16::atanh(bfloat16::bfloat16(-0.2)) ); // 0xBE4E = -0.201171875 bf16 REQUIRE(0xBE9D == bfloat16::atanh(bfloat16::bfloat16(-0.3)) ); // 0xBE9D = -0.306640625 bf16 REQUIRE(0xBED7 == bfloat16::atanh(bfloat16::bfloat16(-0.4)) ); // 0xBED7 = -0.419921875 bf16 REQUIRE(0xBF0C == bfloat16::atanh(bfloat16::bfloat16(-0.5)) ); // 0xBF0C = -0.546875 bf16 REQUIRE(0xBF30 == bfloat16::atanh(bfloat16::bfloat16(-0.6)) ); // 0xBF30 = -0.6875 bf16 REQUIRE(0xBF5D == bfloat16::atanh(bfloat16::bfloat16(-0.7)) ); // 0xBF5D = -0.86328125 bf16 REQUIRE(0xBF8B == bfloat16::atanh(bfloat16::bfloat16(-0.8)) ); // 0xBF8B = -1.0859375 bf16 REQUIRE(0xBFBB == bfloat16::atanh(bfloat16::bfloat16(-0.9)) ); // 0xBFBB = -1.4609375 bf16 } TEST_CASE("bfloat16_erf", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x3F80 == bfloat16::erf(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0xBF80 == bfloat16::erf(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::erf(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::erf(bfloat16::bfloat16(0.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3E63 == bfloat16::erf(bfloat16::bfloat16(0.2)) ); // 0x3E63 = 0.2216796875 bf16 REQUIRE(0x3EDA == bfloat16::erf(bfloat16::bfloat16(0.4)) ); // 0x3EDA = 0.42578125 bf16 REQUIRE(0x3F1A == bfloat16::erf(bfloat16::bfloat16(0.6)) ); // 0x3F1A = 0.6015625 bf16 REQUIRE(0x3F3D == bfloat16::erf(bfloat16::bfloat16(0.8)) ); // 0x3F3D = 0.73828125 bf16 REQUIRE(0x3F57 == bfloat16::erf(bfloat16::bfloat16(1.0)) ); // 0x3F57 = 0.83984375 bf16 REQUIRE(0x3F68 == bfloat16::erf(bfloat16::bfloat16(1.2)) ); // 0x3F68 = 0.90625 bf16 REQUIRE(0x3F73 == bfloat16::erf(bfloat16::bfloat16(1.4)) ); // 0x3F73 = 0.94921875 bf16 REQUIRE(0x3F79 == bfloat16::erf(bfloat16::bfloat16(1.6)) ); // 0x3F79 = 0.97265625 bf16 REQUIRE(0x3F7D == bfloat16::erf(bfloat16::bfloat16(1.8)) ); // 0x3F7D = 0.98828125 bf16 REQUIRE(0x3F7E == bfloat16::erf(bfloat16::bfloat16(2.0)) ); // 0x3F7E = 0.9921875 bf16 //negative REQUIRE(0xBE63 == bfloat16::erf(bfloat16::bfloat16(-0.2)) ); // 0xBE63 = -0.2216796875 bf16 REQUIRE(0xBEDA == bfloat16::erf(bfloat16::bfloat16(-0.4)) ); // 0xBEDA = -0.42578125 bf16 REQUIRE(0xBF1A == bfloat16::erf(bfloat16::bfloat16(-0.6)) ); // 0xBF1A = -0.6015625 bf16 REQUIRE(0xBF3D == bfloat16::erf(bfloat16::bfloat16(-0.8)) ); // 0xBF3D = -0.73828125 bf16 REQUIRE(0xBF57 == bfloat16::erf(bfloat16::bfloat16(-1.0)) ); // 0xBF57 = -0.83984375 bf16 REQUIRE(0xBF68 == bfloat16::erf(bfloat16::bfloat16(-1.2)) ); // 0xBF68 = -0.90625 bf16 REQUIRE(0xBF73 == bfloat16::erf(bfloat16::bfloat16(-1.4)) ); // 0xBF73 = -0.94921875 bf16 REQUIRE(0xBF79 == bfloat16::erf(bfloat16::bfloat16(-1.6)) ); // 0xBF79 = -0.97265625 bf16 REQUIRE(0xBF7D == bfloat16::erf(bfloat16::bfloat16(-1.8)) ); // 0xBF7D = -0.98828125 bf16 REQUIRE(0xBF7E == bfloat16::erf(bfloat16::bfloat16(-2.0)) ); // 0xBF7E = -0.9921875 bf16 } TEST_CASE("bfloat16_erfc", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::erfc(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x4000 == bfloat16::erfc(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::erfc(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x3F80 == bfloat16::erfc(bfloat16::bfloat16(0.0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F47 == bfloat16::erfc(bfloat16::bfloat16(0.2)) ); // 0x3F47 = 0.77734375 bf16 REQUIRE(0x3F12 == bfloat16::erfc(bfloat16::bfloat16(0.4)) ); // 0x3F12 = 0.5703125 bf16 REQUIRE(0x3ECB == bfloat16::erfc(bfloat16::bfloat16(0.6)) ); // 0x3ECB = 0.396484375 bf16 REQUIRE(0x3E84 == bfloat16::erfc(bfloat16::bfloat16(0.8)) ); // 0x3E84 = 0.2578125 bf16 REQUIRE(0x3E21 == bfloat16::erfc(bfloat16::bfloat16(1.0)) ); // 0x3E21 = 0.1572265625 bf16 REQUIRE(0x3DBA == bfloat16::erfc(bfloat16::bfloat16(1.2)) ); // 0x3DBA = 0.0908203125 bf16 REQUIRE(0x3D44 == bfloat16::erfc(bfloat16::bfloat16(1.4)) ); // 0x3D44 = 0.0478515625 bf16 REQUIRE(0x3CC6 == bfloat16::erfc(bfloat16::bfloat16(1.6)) ); // 0x3CC6 = 0.024169921875 bf16 REQUIRE(0x3C35 == bfloat16::erfc(bfloat16::bfloat16(1.8)) ); // 0x3C35 = 0.01104736328125 bf16 REQUIRE(0x3B99 == bfloat16::erfc(bfloat16::bfloat16(2.0)) ); // 0x3B99 = 0.004669189453125 bf16 //negative REQUIRE(0x3F9C == bfloat16::erfc(bfloat16::bfloat16(-0.2)) ); // 0x3F9C = 1.21875 bf16 REQUIRE(0x3FB6 == bfloat16::erfc(bfloat16::bfloat16(-0.4)) ); // 0x3FB6 = 1.421875 bf16 REQUIRE(0x3FCD == bfloat16::erfc(bfloat16::bfloat16(-0.6)) ); // 0x3FCD = 1.6015625 bf16 REQUIRE(0x3FDE == bfloat16::erfc(bfloat16::bfloat16(-0.8)) ); // 0x3FDE = 1.734375 bf16 REQUIRE(0x3FEB == bfloat16::erfc(bfloat16::bfloat16(-1.0)) ); // 0x3FEB = 1.8359375 bf16 REQUIRE(0x3FF4 == bfloat16::erfc(bfloat16::bfloat16(-1.2)) ); // 0x3FF4 = 1.90625 bf16 REQUIRE(0x3FF9 == bfloat16::erfc(bfloat16::bfloat16(-1.4)) ); // 0x3FF9 = 1.9453125 bf16 REQUIRE(0x3FFC == bfloat16::erfc(bfloat16::bfloat16(-1.6)) ); // 0x3FFC = 1.96875 bf16 REQUIRE(0x3FFE == bfloat16::erfc(bfloat16::bfloat16(-1.8)) ); // 0x3FFE = 1.984375 bf16 REQUIRE(0x3FFF == bfloat16::erfc(bfloat16::bfloat16(-2.0)) ); // 0x3FFF = 1.9921875 bf16 } TEST_CASE("bfloat16_lgamma", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::lgamma(bfloat16::bfloat16(-0)) ); REQUIRE(0x7F80 == bfloat16::lgamma(bfloat16::bfloat16(0)) ); REQUIRE(0x7F80 == bfloat16::lgamma(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::lgamma(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::lgamma(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::lgamma(bfloat16::bfloat16(1.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::lgamma(bfloat16::bfloat16(2.0)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F31 == bfloat16::lgamma(bfloat16::bfloat16(3.0)) ); // 0x3F31 = 0.69140625 bf16 REQUIRE(0x3FE5 == bfloat16::lgamma(bfloat16::bfloat16(4.0)) ); // 0x3FE5 = 1.7890625 bf16 REQUIRE(0x404B == bfloat16::lgamma(bfloat16::bfloat16(5.0)) ); // 0x404B = 3.171875 bf16 REQUIRE(0x4099 == bfloat16::lgamma(bfloat16::bfloat16(6.0)) ); // 0x4099 = 4.78125 bf16 REQUIRE(0x40D2 == bfloat16::lgamma(bfloat16::bfloat16(7.0)) ); // 0x40D2 = 6.5625 bf16 REQUIRE(0x4108 == bfloat16::lgamma(bfloat16::bfloat16(8.0)) ); // 0x4108 = 8.5 bf16 REQUIRE(0x4129 == bfloat16::lgamma(bfloat16::bfloat16(9.0)) ); // 0x4129 = 10.5625 bf16 REQUIRE(0x414C == bfloat16::lgamma(bfloat16::bfloat16(10.0)) ); // 0x414C = 12.75 bf16 REQUIRE(0x4171 == bfloat16::lgamma(bfloat16::bfloat16(11.0)) ); // 0x4171 = 15.0625 bf16 //negative REQUIRE(0x7F80 == bfloat16::lgamma(bfloat16::bfloat16(-1.0)) ); // 0x7F80 = inf bf16 REQUIRE(0x7F80 == bfloat16::lgamma(bfloat16::bfloat16(-2.0)) ); // 0x7F80 = inf bf16 } TEST_CASE("bfloat16_tgamma", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::tgamma(bfloat16::bfloat16(-0)) ); REQUIRE(0x7F80 == bfloat16::tgamma(bfloat16::bfloat16(0)) ); REQUIRE(0x7F80 == bfloat16::tgamma(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::tgamma(-std::numeric_limits<bfloat16::bfloat16>::infinity()) )); REQUIRE(isnan(bfloat16::tgamma(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x3F80 == bfloat16::tgamma(bfloat16::bfloat16(1.0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::tgamma(bfloat16::bfloat16(2.0)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::tgamma(bfloat16::bfloat16(3.0)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x40C0 == bfloat16::tgamma(bfloat16::bfloat16(4.0)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x41C0 == bfloat16::tgamma(bfloat16::bfloat16(5.0)) ); // 0x41C0 = 24.0 bf16 REQUIRE(0x42F0 == bfloat16::tgamma(bfloat16::bfloat16(6.0)) ); // 0x42F0 = 120.0 bf16 REQUIRE(0x4434 == bfloat16::tgamma(bfloat16::bfloat16(7.0)) ); // 0x4434 = 720.0 bf16 REQUIRE(0x459D == bfloat16::tgamma(bfloat16::bfloat16(8.0)) ); // 0x459D = 5024.0 bf16 REQUIRE(0x471D == bfloat16::tgamma(bfloat16::bfloat16(9.0)) ); // 0x471D = 40192.0 bf16 REQUIRE(0x7F80 == bfloat16::tgamma(bfloat16::bfloat16(10.0)) ); // 0x7F80 = inf bf16 REQUIRE(0x7F80 == bfloat16::tgamma(bfloat16::bfloat16(11.0)) ); // 0x7F80 = inf bf16 //negative REQUIRE(isnan(bfloat16::tgamma(bfloat16::bfloat16(-1.0))) ); // 0xFFC0 = NaN bf16 REQUIRE(isnan(bfloat16::tgamma(bfloat16::bfloat16(-2.0))) ); // 0xFFC0 = NaN bf16 } TEST_CASE("bfloat16_ceil", "[floa16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::ceil(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::ceil(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::ceil(bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::ceil(bfloat16::bfloat16(-0)) ); REQUIRE(isnan(bfloat16::ceil(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x3F80 == bfloat16::ceil(bfloat16::bfloat16(0.3)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::ceil(bfloat16::bfloat16(0.5)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::ceil(bfloat16::bfloat16(0.8)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x40E0 == bfloat16::ceil(bfloat16::bfloat16(6.3)) ); // 0x40E0 = 7.0 bf16 REQUIRE(0x40E0 == bfloat16::ceil(bfloat16::bfloat16(6.5)) ); // 0x40E0 = 7.0 bf16 REQUIRE(0x40E0 == bfloat16::ceil(bfloat16::bfloat16(6.8)) ); // 0x40E0 = 7.0 bf16 //negative REQUIRE(0x8000 == bfloat16::ceil(bfloat16::bfloat16(-0.3)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x8000 == bfloat16::ceil(bfloat16::bfloat16(-0.5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x8000 == bfloat16::ceil(bfloat16::bfloat16(-0.8)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xC0C0 == bfloat16::ceil(bfloat16::bfloat16(-6.3)) ); // 0xC0C0 = -6.0 bf16 REQUIRE(0xC0C0 == bfloat16::ceil(bfloat16::bfloat16(-6.5)) ); // 0xC0C0 = -6.0 bf16 REQUIRE(0xC0C0 == bfloat16::ceil(bfloat16::bfloat16(-6.8)) ); // 0xC0C0 = -6.0 bf16 } TEST_CASE("bfloat16_floor", "[floa16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::floor(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::floor(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::floor(bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::floor(bfloat16::bfloat16(-0)) ); REQUIRE(isnan(bfloat16::floor(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::floor(bfloat16::bfloat16(0.3)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::floor(bfloat16::bfloat16(0.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::floor(bfloat16::bfloat16(0.8)) ); // 0x0 = 0.0 bf16 REQUIRE(0x40C0 == bfloat16::floor(bfloat16::bfloat16(6.3)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40C0 == bfloat16::floor(bfloat16::bfloat16(6.5)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40C0 == bfloat16::floor(bfloat16::bfloat16(6.8)) ); // 0x40C0 = 6.0 bf16 //negative REQUIRE(0xBF80 == bfloat16::floor(bfloat16::bfloat16(-0.3)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::floor(bfloat16::bfloat16(-0.5)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::floor(bfloat16::bfloat16(-0.8)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC0E0 == bfloat16::floor(bfloat16::bfloat16(-6.3)) ); // 0xC0E0 = -7.0 bf16 REQUIRE(0xC0E0 == bfloat16::floor(bfloat16::bfloat16(-6.5)) ); // 0xC0E0 = -7.0 bf16 REQUIRE(0xC0E0 == bfloat16::floor(bfloat16::bfloat16(-6.8)) ); // 0xC0E0 = -7.0 bf16 } /*TEST_CASE("bfloat16_trunc", "[floa16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::trunc(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::trunc(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(-0)) ); REQUIRE(isnan(bfloat16::trunc(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(0.3)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(0.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(0.8)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(6.3)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(6.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(6.8)) ); // 0x0 = 0.0 bf16 //negative REQUIRE(0x8000 == bfloat16::trunc(bfloat16::bfloat16(-0.3)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x8000 == bfloat16::trunc(bfloat16::bfloat16(-0.5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x8000 == bfloat16::trunc(bfloat16::bfloat16(-0.8)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(-6.3)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(-6.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::trunc(bfloat16::bfloat16(-6.8)) ); // 0x0 = 0.0 bf16 }*/ TEST_CASE("bfloat16_round", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::round(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::round(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::round(bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::round(bfloat16::bfloat16(-0)) ); REQUIRE(isnan(bfloat16::round(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::round(bfloat16::bfloat16(0.3)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::round(bfloat16::bfloat16(0.5)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::round(bfloat16::bfloat16(0.8)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x40C0 == bfloat16::round(bfloat16::bfloat16(6.3)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40C0 == bfloat16::round(bfloat16::bfloat16(6.5)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40E0 == bfloat16::round(bfloat16::bfloat16(6.8)) ); // 0x40E0 = 7.0 bf16 //negative REQUIRE(0x8000 == bfloat16::round(bfloat16::bfloat16(-0.3)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xBF80 == bfloat16::round(bfloat16::bfloat16(-0.5)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xBF80 == bfloat16::round(bfloat16::bfloat16(-0.8)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC0C0 == bfloat16::round(bfloat16::bfloat16(-6.3)) ); // 0xC0C0 = -6.0 bf16 REQUIRE(0xC0C0 == bfloat16::round(bfloat16::bfloat16(-6.5)) ); // 0xC0C0 = -6.0 bf16 REQUIRE(0xC0E0 == bfloat16::round(bfloat16::bfloat16(-6.8)) ); // 0xC0E0 = -7.0 bf16 } TEST_CASE("bfloat16_lround", "[float16]"){ //works //positive REQUIRE(0 == bfloat16::lround(bfloat16::bfloat16(0.3)) ); REQUIRE(1 == bfloat16::lround(bfloat16::bfloat16(0.5)) ); REQUIRE(1 == bfloat16::lround(bfloat16::bfloat16(0.8)) ); REQUIRE(6 == bfloat16::lround(bfloat16::bfloat16(6.3)) ); REQUIRE(7 == bfloat16::lround(bfloat16::bfloat16(6.5)) ); REQUIRE(7 == bfloat16::lround(bfloat16::bfloat16(6.8)) ); //negative REQUIRE(0 == bfloat16::lround(bfloat16::bfloat16(-0.3)) ); REQUIRE(-1 == bfloat16::lround(bfloat16::bfloat16(-0.5)) ); REQUIRE(-1 == bfloat16::lround(bfloat16::bfloat16(-0.8)) ); REQUIRE(-6 == bfloat16::lround(bfloat16::bfloat16(-6.3)) ); REQUIRE(-7 == bfloat16::lround(bfloat16::bfloat16(-6.5)) ); REQUIRE(-7 == bfloat16::lround(bfloat16::bfloat16(-6.8)) ); } TEST_CASE("bfloat16_lrint", "[float16]"){ //works //positive REQUIRE(0 == bfloat16::lrint(bfloat16::bfloat16(0.3)) ); REQUIRE(0 == bfloat16::lrint(bfloat16::bfloat16(0.5)) ); REQUIRE(1 == bfloat16::lrint(bfloat16::bfloat16(0.8)) ); REQUIRE(6 == bfloat16::lrint(bfloat16::bfloat16(6.3)) ); REQUIRE(6 == bfloat16::lrint(bfloat16::bfloat16(6.5)) ); REQUIRE(7 == bfloat16::lrint(bfloat16::bfloat16(6.8)) ); //negative REQUIRE(0 == bfloat16::lrint(bfloat16::bfloat16(-0.3)) ); REQUIRE(0 == bfloat16::lrint(bfloat16::bfloat16(-0.5)) ); REQUIRE(-1 == bfloat16::lrint(bfloat16::bfloat16(-0.8)) ); REQUIRE(-6 == bfloat16::lrint(bfloat16::bfloat16(-6.3)) ); REQUIRE(-6 == bfloat16::lrint(bfloat16::bfloat16(-6.5)) ); REQUIRE(-7 == bfloat16::lrint(bfloat16::bfloat16(-6.8)) ); } TEST_CASE("bfloat16_nearbyint", "[floa16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x7F80 == bfloat16::nearbyint(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::nearbyint(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x0 == bfloat16::nearbyint(bfloat16::bfloat16(0)) ); REQUIRE(0x0 == bfloat16::nearbyint(bfloat16::bfloat16(-0)) ); REQUIRE(isnan(bfloat16::nearbyint(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::nearbyint(bfloat16::bfloat16(0.3)) ); // 0x0 = 0.0 bf16 REQUIRE(0x0 == bfloat16::nearbyint(bfloat16::bfloat16(0.5)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::nearbyint(bfloat16::bfloat16(0.8)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x40C0 == bfloat16::nearbyint(bfloat16::bfloat16(6.3)) ); // 0x40C0 = 6.0 bf16 REQUIRE(0x40E0 == bfloat16::nearbyint(bfloat16::bfloat16(6.5)) ); // 0x40E0 = 7.0 bf16 REQUIRE(0x40E0 == bfloat16::nearbyint(bfloat16::bfloat16(6.8)) ); // 0x40E0 = 7.0 bf16 //negative REQUIRE(0x8000 == bfloat16::nearbyint(bfloat16::bfloat16(-0.3)) ); // 0x8000 = -0.0 bf16 REQUIRE(0x8000 == bfloat16::nearbyint(bfloat16::bfloat16(-0.5)) ); // 0x8000 = -0.0 bf16 REQUIRE(0xBF80 == bfloat16::nearbyint(bfloat16::bfloat16(-0.8)) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC0C0 == bfloat16::nearbyint(bfloat16::bfloat16(-6.3)) ); // 0xC0C0 = -6.0 bf16 REQUIRE(0xC0E0 == bfloat16::nearbyint(bfloat16::bfloat16(-6.5)) ); // 0xC0E0 = -7.0 bf16 REQUIRE(0xC0E0 == bfloat16::nearbyint(bfloat16::bfloat16(-6.8)) ); // 0xC0E0 = -7.0 bf16 } TEST_CASE("bfloat16_llround", "[floa16]"){ //works //positive REQUIRE(0 == bfloat16::llround(bfloat16::bfloat16(0.3)) ); REQUIRE(1 == bfloat16::llround(bfloat16::bfloat16(0.5)) ); REQUIRE(1 == bfloat16::llround(bfloat16::bfloat16(0.8)) ); REQUIRE(6 == bfloat16::llround(bfloat16::bfloat16(6.3)) ); REQUIRE(7 == bfloat16::llround(bfloat16::bfloat16(6.5)) ); REQUIRE(7 == bfloat16::llround(bfloat16::bfloat16(6.8)) ); //negative REQUIRE(0 == bfloat16::llround(bfloat16::bfloat16(-0.3)) ); REQUIRE(-1 == bfloat16::llround(bfloat16::bfloat16(-0.5)) ); REQUIRE(-1 == bfloat16::llround(bfloat16::bfloat16(-0.8)) ); REQUIRE(-6 == bfloat16::llround(bfloat16::bfloat16(-6.3)) ); REQUIRE(-7 == bfloat16::llround(bfloat16::bfloat16(-6.5)) ); REQUIRE(-7 == bfloat16::llround(bfloat16::bfloat16(-6.8)) ); } TEST_CASE("bfloat16_llrint", "[float16]"){ //works //positive REQUIRE(0 == bfloat16::llrint(bfloat16::bfloat16(0.3)) ); REQUIRE(0 == bfloat16::llrint(bfloat16::bfloat16(0.5)) ); REQUIRE(1 == bfloat16::llrint(bfloat16::bfloat16(0.8)) ); REQUIRE(6 == bfloat16::llrint(bfloat16::bfloat16(6.3)) ); REQUIRE(6 == bfloat16::llrint(bfloat16::bfloat16(6.5)) ); REQUIRE(7 == bfloat16::llrint(bfloat16::bfloat16(6.8)) ); //negative REQUIRE(0 == bfloat16::llrint(bfloat16::bfloat16(-0.3)) ); REQUIRE(0 == bfloat16::llrint(bfloat16::bfloat16(-0.5)) ); REQUIRE(-1 == bfloat16::llrint(bfloat16::bfloat16(-0.8)) ); REQUIRE(-6 == bfloat16::llrint(bfloat16::bfloat16(-6.3)) ); REQUIRE(-6 == bfloat16::llrint(bfloat16::bfloat16(-6.5)) ); REQUIRE(-7 == bfloat16::llrint(bfloat16::bfloat16(-6.8)) ); } TEST_CASE("bfloat16_frexp", "[float16]"){ } TEST_CASE("bfloat16_scalbln", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::scalbln(bfloat16::bfloat16(0), 1) ); REQUIRE(0x0 == bfloat16::scalbln(bfloat16::bfloat16(-0), 1) ); REQUIRE(0x7F80 == bfloat16::scalbln(std::numeric_limits<bfloat16::bfloat16>::infinity(), 1) ); REQUIRE(0xFF80 == bfloat16::scalbln(-std::numeric_limits<bfloat16::bfloat16>::infinity(), 1) ); REQUIRE(isnan(bfloat16::scalbln(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), 1) )); //positive REQUIRE(0x3F80 == bfloat16::scalbln(bfloat16::bfloat16(1), 0) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::scalbln(bfloat16::bfloat16(1), 1) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4080 == bfloat16::scalbln(bfloat16::bfloat16(1), 2) ); // 0x4080 = 4.0 bf16 //negative REQUIRE(0xBF80 == bfloat16::scalbln(bfloat16::bfloat16(-1), 0) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC000 == bfloat16::scalbln(bfloat16::bfloat16(-1), 1) ); // 0xC000 = -2.0 bf16 REQUIRE(0xC080 == bfloat16::scalbln(bfloat16::bfloat16(-1), 2) ); // 0xC080 = -4.0 bf16 } TEST_CASE("bfloat16_scalbln2", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::scalbn(bfloat16::bfloat16(0), 1) ); REQUIRE(0x0 == bfloat16::scalbn(bfloat16::bfloat16(-0), 1) ); REQUIRE(0x7F80 == bfloat16::scalbn(std::numeric_limits<bfloat16::bfloat16>::infinity(), 1) ); REQUIRE(0xFF80 == bfloat16::scalbn(-std::numeric_limits<bfloat16::bfloat16>::infinity(), 1) ); REQUIRE(isnan(bfloat16::scalbn(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), 1) )); //depends on scalbln //positive REQUIRE(0x3F80 == bfloat16::scalbn(bfloat16::bfloat16(1), 0) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::scalbn(bfloat16::bfloat16(1), 1) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4080 == bfloat16::scalbn(bfloat16::bfloat16(1), 2) ); // 0x4080 = 4.0 bf16 //negative REQUIRE(0xBF80 == bfloat16::scalbn(bfloat16::bfloat16(-1), 0) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC000 == bfloat16::scalbn(bfloat16::bfloat16(-1), 1) ); // 0xC000 = -2.0 bf16 REQUIRE(0xC080 == bfloat16::scalbn(bfloat16::bfloat16(-1), 2) ); // 0xC080 = -4.0 bf16 } TEST_CASE("bfloat16_ldexp", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(0x0 == bfloat16::ldexp(bfloat16::bfloat16(0), 1) ); REQUIRE(0x0 == bfloat16::ldexp(bfloat16::bfloat16(-0), 1) ); REQUIRE(0x7F80 == bfloat16::ldexp(std::numeric_limits<bfloat16::bfloat16>::infinity(), 1) ); REQUIRE(0xFF80 == bfloat16::ldexp(-std::numeric_limits<bfloat16::bfloat16>::infinity(), 1) ); REQUIRE(isnan(bfloat16::ldexp(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), 1) )); //depends on scalbln //positive REQUIRE(0x3F80 == bfloat16::ldexp(bfloat16::bfloat16(1), 0) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::ldexp(bfloat16::bfloat16(1), 1) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4080 == bfloat16::ldexp(bfloat16::bfloat16(1), 2) ); // 0x4080 = 4.0 bf16 //negative REQUIRE(0xBF80 == bfloat16::ldexp(bfloat16::bfloat16(-1), 0) ); // 0xBF80 = -1.0 bf16 REQUIRE(0xC000 == bfloat16::ldexp(bfloat16::bfloat16(-1), 1) ); // 0xC000 = -2.0 bf16 REQUIRE(0xC080 == bfloat16::ldexp(bfloat16::bfloat16(-1), 2) ); // 0xC080 = -4.0 bf16 } TEST_CASE("bfloat16_modf", "[float16]"){ } TEST_CASE("bfloat16_ilogb", "[float16]"){ //works //postive REQUIRE(0 == bfloat16::ilogb(bfloat16::bfloat16(1)) ); REQUIRE(1 == bfloat16::ilogb(bfloat16::bfloat16(2)) ); REQUIRE(1 == bfloat16::ilogb(bfloat16::bfloat16(3)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(4)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(5)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(6)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(7)) ); REQUIRE(3 == bfloat16::ilogb(bfloat16::bfloat16(8)) ); REQUIRE(3 == bfloat16::ilogb(bfloat16::bfloat16(9)) ); REQUIRE(3 == bfloat16::ilogb(bfloat16::bfloat16(10)) ); //negative REQUIRE(0 == bfloat16::ilogb(bfloat16::bfloat16(-1)) ); REQUIRE(1 == bfloat16::ilogb(bfloat16::bfloat16(-2)) ); REQUIRE(1 == bfloat16::ilogb(bfloat16::bfloat16(-3)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(-4)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(-5)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(-6)) ); REQUIRE(2 == bfloat16::ilogb(bfloat16::bfloat16(-7)) ); REQUIRE(3 == bfloat16::ilogb(bfloat16::bfloat16(-8)) ); REQUIRE(3 == bfloat16::ilogb(bfloat16::bfloat16(-9)) ); REQUIRE(3 == bfloat16::ilogb(bfloat16::bfloat16(-10)) ); } TEST_CASE("bfloat16_logb", "[float16]"){ //calculated with the temporary solution //ERRhandling / numeric Limits REQUIRE(0xFF80 == bfloat16::logb(bfloat16::bfloat16(-0)) ); REQUIRE(0xFF80 == bfloat16::logb(bfloat16::bfloat16(0)) ); REQUIRE(0x7F80 == bfloat16::logb(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(0x7F80 == bfloat16::logb(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(isnan(bfloat16::logb(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) )); //positive REQUIRE(0x0 == bfloat16::logb(bfloat16::bfloat16(1)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::logb(bfloat16::bfloat16(2)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::logb(bfloat16::bfloat16(3)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(5)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(6)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(7)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4040 == bfloat16::logb(bfloat16::bfloat16(8)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4040 == bfloat16::logb(bfloat16::bfloat16(9)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4040 == bfloat16::logb(bfloat16::bfloat16(10)) ); // 0x4040 = 3.0 bf16 //negative REQUIRE(0x0 == bfloat16::logb(bfloat16::bfloat16(-1)) ); // 0x0 = 0.0 bf16 REQUIRE(0x3F80 == bfloat16::logb(bfloat16::bfloat16(-2)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x3F80 == bfloat16::logb(bfloat16::bfloat16(-3)) ); // 0x3F80 = 1.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(-4)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(-5)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(-6)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4000 == bfloat16::logb(bfloat16::bfloat16(-7)) ); // 0x4000 = 2.0 bf16 REQUIRE(0x4040 == bfloat16::logb(bfloat16::bfloat16(-8)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4040 == bfloat16::logb(bfloat16::bfloat16(-9)) ); // 0x4040 = 3.0 bf16 REQUIRE(0x4040 == bfloat16::logb(bfloat16::bfloat16(-10)) ); // 0x4040 = 3.0 bf16 } TEST_CASE("bfloat16_nextafter", "[float16]"){ } TEST_CASE("bfloat16_nexttoward", "[float16]"){ } TEST_CASE("bfloat16_copysign", "[float16]"){ //works REQUIRE(0xC000 == bfloat16::copysign(bfloat16::bfloat16(2), bfloat16::bfloat16(-2)) ); // 0xC000 = -2.0 bf16 REQUIRE(0x4000 == bfloat16::copysign(bfloat16::bfloat16(-2), bfloat16::bfloat16(2)) ); // 0x4000 = 2.0 bf16 } TEST_CASE("bfloat16_fpclassify", "[float16]"){ //works //ERRhandling / numeric Limits bfloat16::bfloat16 inf = std::numeric_limits<bfloat16::bfloat16>::infinity(); //0x7F80 bfloat16::bfloat16 QNaN = std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(); //0x7FFF bfloat16::bfloat16 den = std::numeric_limits<bfloat16::bfloat16>::denorm_min(); //0x0001 REQUIRE(0 == bfloat16::fpclassify(QNaN) ); //FP_NAN = 0 REQUIRE(1 == bfloat16::fpclassify(inf) ); //FP_INFINITE = 1 REQUIRE(2 == bfloat16::fpclassify(bfloat16::bfloat16(0)) ); // FP_ZERO = 2 REQUIRE(3 == bfloat16::fpclassify(den) ); // FP_SUBNORMAL = 3 REQUIRE(4 == bfloat16::fpclassify(bfloat16::bfloat16(2)) ); // FP_NORMAL = 4 REQUIRE(4 == bfloat16::fpclassify(bfloat16::bfloat16(5)) ); // FP_NORMAL = 4 REQUIRE(4 == bfloat16::fpclassify(bfloat16::bfloat16(100)) ); // FP_NORMAL = 4 REQUIRE(4 == bfloat16::fpclassify(bfloat16::bfloat16(264)) ); // FP_NORMAL = 4 } TEST_CASE("bfloat16_isfinite", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(false == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(false == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(true == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::min()) ); REQUIRE(true == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::lowest()) ); REQUIRE(true == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::max()) ); REQUIRE(true == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::epsilon()) ); REQUIRE(true == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::round_error()) ); REQUIRE(false == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()) ); REQUIRE(true == bfloat16::isfinite(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); //positive REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(0.5)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(10.5)) ); //negative REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::isfinite(bfloat16::bfloat16(-10.5)) ); } TEST_CASE("bfloat16_isinf", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(true == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::min()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::lowest()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::max()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::epsilon()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::round_error()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()) ); REQUIRE(false == bfloat16::isinf(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); //positive REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(10.5)) ); //negative REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::isinf(bfloat16::bfloat16(-10.5)) ); } TEST_CASE("bfloat16_isnan", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(true == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::min()) ); REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::lowest()) ); REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::max()) ); REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::epsilon()) ); REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::round_error()) ); REQUIRE(true == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::signaling_NaN()) ); REQUIRE(false == bfloat16::isnan(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); //positive REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(10.5)) ); //negative REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::isnan(bfloat16::bfloat16(-10.5)) ); } TEST_CASE("bfloat16_isnormal", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(false == bfloat16::isnormal(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); REQUIRE(false == bfloat16::isnormal(bfloat16::bfloat16(0)) ); REQUIRE(false == bfloat16::isnormal(bfloat16::bfloat16(-0)) ); REQUIRE(false == bfloat16::isnormal(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(false == bfloat16::isnormal(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(false == bfloat16::isnormal(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); //positive REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(0.1)) ); REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(0.5)) ); REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(1)) ); REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(5)) ); //negative REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(-0.1)) ); REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(-1)) ); REQUIRE(true == bfloat16::isnormal(bfloat16::bfloat16(-5)) ); } TEST_CASE("bfloat16_signbit", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(false == bfloat16::signbit(std::numeric_limits<bfloat16::bfloat16>::denorm_min()) ); REQUIRE(false == bfloat16::signbit(bfloat16::bfloat16(0)) ); REQUIRE(false == bfloat16::signbit(bfloat16::bfloat16(-0)) ); REQUIRE(false == bfloat16::signbit(std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(true == bfloat16::signbit(-std::numeric_limits<bfloat16::bfloat16>::infinity()) ); REQUIRE(false == bfloat16::signbit(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); //positive REQUIRE(false == bfloat16::signbit(bfloat16::bfloat16(0.1)) ); REQUIRE(false == bfloat16::signbit(bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::signbit(bfloat16::bfloat16(1)) ); REQUIRE(false == bfloat16::signbit(bfloat16::bfloat16(5)) ); //negative REQUIRE(true == bfloat16::signbit(bfloat16::bfloat16(-0.1)) ); REQUIRE(true == bfloat16::signbit(bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::signbit(bfloat16::bfloat16(-1)) ); REQUIRE(true == bfloat16::signbit(bfloat16::bfloat16(-5)) ); } TEST_CASE("bfloat16_isgreater", "[float16]"){ //works //positive REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(0.1), bfloat16::bfloat16(0.5)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.1)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(20), bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(10), bfloat16::bfloat16(20)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(10.1), bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(20.1), bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(10.1), bfloat16::bfloat16(20.1)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(10.5), bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(20.5), bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(10.5), bfloat16::bfloat16(20.5)) ); //negative REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(-0.1), bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.1)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-10), bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-20), bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(-10), bfloat16::bfloat16(-20)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-20.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-20.1)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::isgreater(bfloat16::bfloat16(-20.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::isgreater(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-20.5)) ); } TEST_CASE("bfloat16_isgreaterequal", "[float16]"){ //works //positive REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.5)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(20), bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(10), bfloat16::bfloat16(20)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(10.1), bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(20.1), bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(10.1), bfloat16::bfloat16(20.1)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(10.5), bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(20.5), bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(10.5), bfloat16::bfloat16(20.5)) ); //negative REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-10), bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(-20), bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-10), bfloat16::bfloat16(-20)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(-20.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-20.1)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::isgreaterequal(bfloat16::bfloat16(-20.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::isgreaterequal(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-20.5)) ); } TEST_CASE("bfloat16_isless", "[float16]"){ //works //positive REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(20), bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(10), bfloat16::bfloat16(20)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(10.1), bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(20.1), bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(10.1), bfloat16::bfloat16(20.1)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(10.5), bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(20.5), bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(10.5), bfloat16::bfloat16(20.5)) ); //negative REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-10), bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(-20), bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-10), bfloat16::bfloat16(-20)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(-20.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-20.1)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::isless(bfloat16::bfloat16(-20.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::isless(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-20.5)) ); } TEST_CASE("bfloat16_islessequal", "[float16]"){ //works //positive REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.10)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.5)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(20), bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(10), bfloat16::bfloat16(20)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(10.1), bfloat16::bfloat16(10.1)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(20.1), bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(10.1), bfloat16::bfloat16(20.1)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(10.5), bfloat16::bfloat16(10.5)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(20.5), bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(10.5), bfloat16::bfloat16(20.5)) ); //negative REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.10)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.5)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-10), bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-20), bfloat16::bfloat16(-10)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(-10), bfloat16::bfloat16(-20)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-20.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-20.1)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::islessequal(bfloat16::bfloat16(-20.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(false == bfloat16::islessequal(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-20.5)) ); } TEST_CASE("bfloat16_islessgreater", "[float16]"){ //works //positive REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(0.5), bfloat16::bfloat16(0.10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(0.10), bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(10), bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(20), bfloat16::bfloat16(10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(10), bfloat16::bfloat16(20)) ); REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(10.1), bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(20.1), bfloat16::bfloat16(10.1)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(10.1), bfloat16::bfloat16(20.1)) ); REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(10.5), bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(20.5), bfloat16::bfloat16(10.5)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(10.5), bfloat16::bfloat16(20.5)) ); //negative REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-0.5), bfloat16::bfloat16(-0.10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-0.10), bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(-10), bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-20), bfloat16::bfloat16(-10)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-10), bfloat16::bfloat16(-20)) ); REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-20.1), bfloat16::bfloat16(-10.1)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-10.1), bfloat16::bfloat16(-20.1)) ); REQUIRE(false == bfloat16::islessgreater(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-20.5), bfloat16::bfloat16(-10.5)) ); REQUIRE(true == bfloat16::islessgreater(bfloat16::bfloat16(-10.5), bfloat16::bfloat16(-20.5)) ); } TEST_CASE("bfloat16_isunordered", "[float16]"){ //works //ERRhandling / numeric Limits REQUIRE(true == bfloat16::isunordered(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); REQUIRE(true == bfloat16::isunordered(std::numeric_limits<bfloat16::bfloat16>::quiet_NaN(), bfloat16::bfloat16()) ); REQUIRE(true == bfloat16::isunordered(bfloat16::bfloat16(), std::numeric_limits<bfloat16::bfloat16>::quiet_NaN()) ); //numeric values REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(1), bfloat16::bfloat16(5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(-1), bfloat16::bfloat16(5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(1), bfloat16::bfloat16(-5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(-1), bfloat16::bfloat16(-5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(0.1), bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(0.1), bfloat16::bfloat16(-0.5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(-0.1), bfloat16::bfloat16(0.5)) ); REQUIRE(false == bfloat16::isunordered(bfloat16::bfloat16(-0.1), bfloat16::bfloat16(-0.5)) ); }
306,555
C++
.cpp
4,259
69.708382
196
0.700208
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,597
dsv_tests.cpp
metric-space-ai_metric/tests/utils/dsv_tests.cpp
#include <catch2/catch.hpp> #include <limits> #include <iostream> #include "metric/utils/datasets.hpp" using namespace std; using namespace metric; TEMPLATE_TEST_CASE("base","[utils]", float, double) { using Matrix = blaze::DynamicMatrix<TestType, blaze::columnMajor>; using Vector = blaze::DynamicVector<TestType>; blaze::DynamicMatrix<TestType> m{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {1, 1.1, 1.2}, {1.3, 1.4, -1.5}, {16, -17, 18}, {-0.19, 2, 0.21}, {-22, 23, 24}, {-25, 26, 27}, {-2.8e-8, 29, 3e7}}; { std::ofstream file("matrix.dsv"); file << "1;2; 3" << std::endl; file << "4 , 5,6" << std::endl; file << "7 8 9" << std::endl; file << "1. , 1.1 ,1.2" << std::endl; file << "1,3 1,4 -1,5" << std::endl; file << "16 ,-17,18" << std::endl; file << "-,19 2 ,21" << std::endl; file << "-22 , 23 ,24" << std::endl; file << "[-25,26,27] " << std::endl; file << " [-2,8e-8 29 3,0e007] " << std::endl; file.close(); REQUIRE(Datasets::readDenseMatrixFromFile<TestType>("matrix.dsv") == m); } }
1,314
C++
.cpp
36
26.416667
74
0.453543
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,598
convolution_tests.cpp
metric-space-ai_metric/tests/image_processing_tests/convolution_tests.cpp
#include <catch2/catch.hpp> #include <iostream> #include "metric/utils/image_processing/convolution.hpp" using namespace metric; TEMPLATE_TEST_CASE("Convolution2d", "[convolution]", float, double) { using Conv = Convolution2d<TestType, 1>; typename Conv::Image image{ typename Conv::Channel{{0,1,2,2}, {3,4,5,5}, {6,7,8,1}} }; typename Conv::FilterKernel kernel{{5,7,2}, {1,9,9}, {4,3,2}}; auto conv = Conv(image[0].columns(), image[0].rows(), kernel.columns(), kernel.rows()); typename Conv::Image result{ typename Conv::Channel{{26, 61, 78, 55,}, {97, 156, 171, 109}, {146, 194, 153, 77}} }; SECTION("operator()(image)") { conv.setKernel(kernel); REQUIRE(conv(image) == result); } SECTION("operator()(image, kernel)") { REQUIRE(conv(image, kernel) == result); } }
930
C++
.cpp
30
24.533333
67
0.588501
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,599
lapack_tests.cpp
metric-space-ai_metric/tests/wrapper_tests/lapack_tests.cpp
#include <catch2/catch.hpp> #include <limits> #include "metric/distance/d-spaced/Riemannian.hpp" #include <iostream> using namespace metric; using Matrix = blaze::DynamicMatrix<double>; using Vector = blaze::DynamicVector<double>; TEST_CASE("sygv_c") { std::vector<double> A = { 1, 2, 2, 4 }; std::vector<double> B = { 5, 6, 6, 8 }; std::vector<double> W(2); std::vector<double> work(5); int info; dsygv(1, 'N', 'U', 2, A.data(), 2, B.data(), 2, W.data(), work.data(), 5, info); //std::cout << W[0] << " " << W[1] << std::endl; } TEST_CASE("sygv_blaze") { Matrix A{{1, 2}, {2, 4}}; Matrix B{{5, 6}, {6, 8}}; Vector w{0, 0}; sygv<double, double>(A, B, w); //std::cout << w[0] << " " << w[1] << std::endl; } TEST_CASE("riemannian_distance") { Matrix A{{1, 2}, {2, 4}}; Matrix B{{5, 6}, {6, 8}}; auto rd = metric::RiemannianDistance<void, metric::Euclidean<double>>(); std::cout << rd.matDistance(A, B) << std::endl; std::vector<std::vector<double>> ds1 {{0, 1}, {0, 0}, {1, 1}, {1, 0}}; std::vector<std::vector<double>> ds2 {{0, 0}, {1, 1}, {2, 2}, {2, 1}}; rd(ds1, ds2); rd.estimate(ds1, ds2); }
1,192
C++
.cpp
36
29.416667
84
0.566286
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,600
dnn_tests.cpp
metric-space-ai_metric/tests/dnn_tests/dnn_tests.cpp
#include <catch2/catch.hpp> #include <limits> #include <iostream> #include "metric/utils/dnn.hpp" using namespace metric::dnn; using Matrix = blaze::DynamicMatrix<double>; using Vector = blaze::DynamicVector<double>; using RowVector = blaze::DynamicVector<double, blaze::rowVector>; TEST_CASE("base", "[dnn]") { Network<double> net; VerboseCallback<double> verboseCallback; RMSProp<double> opt; //RegressionMSE<double> output; } TEST_CASE("identity", "[dnn]") { Matrix input{{-2, -1, 0, 1, 2, 3, 4, 5}}; Matrix output{{-2, -1, 0, 1, 2, 3, 4, 5}}; Matrix result; Identity<double>::activate(input, result); REQUIRE(result == output); } TEST_CASE("relu") { Matrix input{ {-2, -1, 0, 1, 2, 3, 4, 5}}; Matrix output{ {0, 0, 0, 1, 2, 3, 4, 5}}; Matrix result(input.rows(), input.columns()); ReLU<double>::activate(input, result); REQUIRE(result == output); } TEST_CASE("sigmoid") { Matrix input{ {-10, -1, 0, 1, 10}}; Matrix output{ {0.00004539786870, 0.26894142137000, 0.5, 0.73105857863001, 0.99995460213130}}; Matrix result; Sigmoid<double>::activate(input, result); REQUIRE(result.rows() == output.rows()); for (auto i = 0; i < output.columns(); ++i) { REQUIRE(result(0, i) == Approx(output(0, i))); } } //BOOST_AUTO_TEST_CASE(maxpolling) //{ // MaxPooling<double, Identity<double>> maxPolling(4, 4, 1, 2, 2); // // Matrix input{ {0}, {1}, {2}, {3}, {8}, {7}, {5}, {6}, {4}, {3}, {1}, {2}, {0}, {-1}, {-2}, {-3}}; // Matrix output = { {8}, {6}, {4}, {2} }; // // maxPolling.forward(input); // // BOOST_CHECK_EQUAL(maxPolling.output(), output); //} TEST_CASE("fullyconnected") { FullyConnected<double, ReLU<double>> fc(4, 2); fc.initConstant(1, 0.5); Matrix input{{0, 1, 2, 3}}; fc.forward(input); Matrix output{{6.5, 6.5}}; REQUIRE(fc.output() == output); } TEST_CASE("convolutional") { Conv2d<double, Identity<double>> convLayer(3, 3, 1, 2, 2, 2, 1); blaze::DynamicMatrix<double> X{{0, 1, 2, 3, 4, 5, 6, 7, 8}}; std::vector<double> K = {0, 1, 2, 3, 4, 5, 6, 7}; std::vector<double> bias = {1, 2}; convLayer.setParameters({K, bias}); convLayer.forward(X); blaze::DynamicMatrix<double> Y{{20, 26, 38, 44, 53, 75, 119, 141}}; REQUIRE(convLayer.output() == Y); } /*blaze::DynamicMatrix<double> A = {{0, 1, 2, 3}, {4, 5, 6, 7}}; blaze::DynamicMatrix<double> B = {{8, 9, 10, 1.2}, {1.2, 1.3, 1.4, 1.5}}; blaze::DynamicMatrix<double> C(2, 2); //blaze::gemm(CblasRowMajor, CblasNoTrans, CblasTrans, 2, 2, 4, // 1, A.data(), 4, B.data(), 2, 1, C.data(), 2); */ /*double A0[] = {0, 1, 2, 3, 4, 5, 6, 7}; double B0[] = {8, 9, 10, 1.2, 1.3, 1.4, 1.5}; double C0[4]; blaze::gemm(CblasRowMajor, CblasNoTrans, CblasTrans, 2, 2, 4, 1, A.data(), 4, B.data(), 4, 0, C.data(), 2); //blaze::gemm(CblasRowMajor, CblasNoTrans, CblasTrans, 2, 2, 4, // 1, A0, 4, B0, 4, 0, C0, 2); std::cout << C << std::endl; std::cout << A * blaze::trans(B) << std::endl;*//* } */ TEST_CASE("convolution_same") { Conv2d<double, Identity<double>> convLayer(3, 3, 1, 1, 3, 3, 1, true); blaze::DynamicMatrix<double> X{{0, 1, 2, 3, 4, 5, 6, 7, 8}}; std::vector<double> K = {0, 0, 0, 0, 1, 0, 0, 0, 0}; std::vector<double> bias = {0}; convLayer.setParameters({K, bias}); convLayer.forward(X); blaze::DynamicMatrix<double> Y{{0, 1, 2, 3, 4, 5, 6, 7, 8}}; REQUIRE(convLayer.output() == Y); } TEST_CASE("deconvolutional") { Conv2dTranspose<double, Identity<double>> convTransposeLayer(2, 2, 1, 1, 2, 2); blaze::DynamicMatrix<double> X{{0, 1, 2, 3}}; std::vector<double> K = {0, 1, 2, 3}; std::vector<double> bias = {1}; convTransposeLayer.setParameters({K, bias}); convTransposeLayer.forward(X); blaze::DynamicMatrix<double> Y {{1, 1, 2, 1, 5, 7, 5, 13, 10}}; REQUIRE(convTransposeLayer.output() == Y); } /*BOOST_AUTO_TEST_CASE(network_json) { auto json = R"({ "0": { "type": "FullyConnected", "inputSize": 3, "outputSize": 2, "activation": "ReLU" }, "1": { "type": "FullyConnected", "inputSize": 2, "outputSize": 1, "activation": "Sigmoid" }, "train": { "loss": "RegressionMSE", "optimizer": {"type": "RMSProp", "learningRate": 0.01, "eps": 1e-6, "decay": 0.9} } } )"_json; using Scalar = double; Network<Scalar> nt(json.dump()); blaze::DynamicMatrix<Scalar> data = {{0.1, 0.2, 0.3}}; blaze::DynamicMatrix<Scalar> labels = {{0.4}}; nt.fit(data, labels, 1, 1); std::cout << nt.predict(data) << std::endl; }*//* BOOST_AUTO_TEST_CASE(rmsprop) { RowVector weights = {-0.021, 1.03, -0.05, -.749, 0.009}; RowVector gradients = {1.000, -3.24, -0.60, 2.79, 1.820}; // Defining the expected updates RowVector first_update = {-0.0220, 1.0310, -0.0490, -0.7500, 0.0080}; RowVector second_update = {-0.0227, 1.0317, -0.0482, -0.7507, 0.0072}; // Testing auto optimizer = RMSProp<double>(0.0001, 1e-8, 0.99); optimizer.update(gradients, weights); for (size_t i = 0; i < weights.size(); i++) { BOOST_TEST(std::abs(first_update[i] - weights[i]) < 1e-3); } optimizer.update(gradients, weights); for (size_t i = 0; i < weights.size(); i++) { BOOST_TEST(std::abs(second_update[i] - weights[i]) < 1e-3); } }*/
5,371
C++
.cpp
170
28.188235
100
0.600933
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,601
dnn_json_tests.cpp
metric-space-ai_metric/tests/dnn_tests/dnn_json_tests.cpp
#include <catch2/catch.hpp> #include <limits> #include <iostream> #include "metric/utils/dnn.hpp" using namespace metric::dnn; using Matrix = blaze::DynamicMatrix<double>; using Vector = blaze::DynamicVector<double>; TEST_CASE("fullyconnected_json", "[dnn]") { auto json = R"( { "type": "FullyConnected", "inputSize": 100, "outputSize": 10, "activation": "Identity" } )"_json; FullyConnected<double, Identity<double>> fc(json); REQUIRE(fc.getInputSize() == 100); REQUIRE(fc.getOutputSize() == 10); REQUIRE(json == fc.toJson()); } //BOOST_AUTO_TEST_CASE(maxpolling_json) //{ // auto json = R"( // { // "inputSize": 100, // "outputSize": 10 // } // )"_json; // // MaxPooling<double, Identity<double>> mp(json); // // BOOST_CHECK_EQUAL(mp.getInputSize(), 100); // BOOST_CHECK_EQUAL(mp.getOutputSize(), 10); // // BOOST_CHECK_EQUAL(json, mp.toJson()); //} TEST_CASE("network_json", "[dnn]") { auto json = R"({ "0": { "type": "FullyConnected", "inputSize": 100, "outputSize": 10, "activation": "ReLU" }, "train": { "loss": "RegressionMSE", "optimizer": {"type": "RMSProp", "learningRate": 0.01, "eps": 1e-6, "decay": 0.9} } } )"_json; Network<double> nt(json.dump()); REQUIRE(json == nt.toJson()); nt.save("net.cereal"); nt.load("net.cereal"); REQUIRE(json == nt.toJson()); std::stringstream ss; nt.save(ss); nt.load(ss); REQUIRE(json == nt.toJson()); }
1,558
C++
.cpp
68
19.147059
51
0.594026
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,602
datasets_tests.cpp
metric-space-ai_metric/tests/dnn_tests/datasets_tests.cpp
#include <catch2/catch.hpp> #include <iostream> #include "metric/utils/datasets.hpp" using namespace metric; TEST_CASE("notexist", "[datasets]") { auto [shape, data] = Datasets::loadImages("not-exist"); REQUIRE(shape.empty()); } TEST_CASE("empty", "[datasets]") { auto [shape, data] = Datasets::loadImages("empty"); std::vector<uint32_t> emptyShape = {0, 0, 0, 1}; REQUIRE(shape == emptyShape); } TEST_CASE("wrong_shape", "[datasets]") { auto [shape, data] = Datasets::loadImages("wrong-shape"); REQUIRE(shape.empty()); } TEST_CASE("images_list", "[datasets]") { auto [shape, data] = Datasets::loadImages("images-list"); std::vector<uint32_t> trueShape = {2, 3, 2, 1}; std::vector<uint8_t> trueData = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 110, 120}; REQUIRE(trueShape == shape); REQUIRE(trueData == data); }
846
C++
.cpp
29
27
58
0.670777
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,603
ensemble_tests.cpp
metric-space-ai_metric/tests/ensembles_tests/ensemble_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Panda Team */ #include <catch2/catch.hpp> #include "metric/mapping.hpp" #include "assets/helpers.cpp" // csv reader template <class ContainerType> struct TestDataset { typedef typename ContainerType::value_type Record; TestDataset(ContainerType dataset) { data = dataset; if (data.size() < 1) return; for (int i = 0; i < (int)dataset[0].size() - 1; ++i) { features.push_back([=](auto r) { return r[i]; }); } response = [](Record r) { if (r[r.size() - 1] >= 0.5) return true; else return false; }; } ContainerType data; std::vector<std::function<double(Record)>> features; std::function<bool(Record)> response; }; template <class ContainerType> struct AllDatasets { typedef typename ContainerType::value_type Record; AllDatasets<ContainerType>() { ContainerType table = { { 0, 3, 5, 0 }, { 1, 4, 5, 0 }, { 2, 5, 2, 1 }, { 3, 6, 2, 1 } }; TestDataset<ContainerType> ds = TestDataset(table); dataset.push_back(ds); table = { { 3, 6, 2, 1 } }; ds = TestDataset(table); dataset.push_back(ds); table = { { 0, 0 }, { 1, 0 }, { 2, 1 }, { 3, 1 } }; ds = TestDataset(table); dataset.push_back(ds); // no features at all table = { { 0 }, { 0 }, { 1 }, { 1 } }; ds = TestDataset(table); dataset.push_back(ds); table = { {}, }; ds = TestDataset(table); dataset.push_back(ds); table = {}; ds = TestDataset(table); ds.features = {}; ds.response = [](Record r) { throw "response accessor called on empty dataset"; return false; // this is to fit the signature }; dataset.push_back(ds); } std::vector<TestDataset<ContainerType>> dataset; }; template <class ContainerType> struct AllExamples { typedef typename ContainerType::value_type Record; AllExamples<ContainerType>() { ContainerType table = { { 0, 3, 5, 0 }, { 2, 5, 2, 1 } }; TestDataset<ContainerType> ds = TestDataset(table); dataset.push_back(ds); table = { { 0, 0, 0, 0 } }; ds = TestDataset(table); dataset.push_back(ds); table = { { 0, 0 } }; ds = TestDataset(table); dataset.push_back(ds); table = { { 0 }, { 0 } }; ds = TestDataset(table); dataset.push_back(ds); table = { { 0 } }; ds = TestDataset(table); dataset.push_back(ds); table = { {} }; ds = TestDataset(table); dataset.push_back(ds); table = {}; ds = TestDataset(table); ds.features = {}; ds.response = [](Record r) { throw "response accessor must not be used in example for prediction"; return false; // this is to fit the signature }; dataset.push_back(ds); table = { { 3, 6, 2 }, { 10, 10, 10 } }; ds = TestDataset(table); for (int i = 0; i < (int)ds.data[0].size(); ++i) { // no response column ds.features.push_back([=](auto r) { return r[i]; }); } ds.response = [](Record r) { throw "response accessor must nut be used in example for prediction"; return false; // this is to fit the signature }; dataset.push_back(ds); table = { { 10, 10, 10 } }; ds = TestDataset(table); for (int i = 0; i < (int)ds.data[0].size(); ++i) { // no response column ds.features.push_back([=](auto r) { return r[i]; }); } ds.response = [](Record r) { throw "response accessor must nut be used in example for prediction"; return false; // this is to fit the signature }; dataset.push_back(ds); // TODO add special datasets } std::vector<TestDataset<ContainerType>> dataset; }; struct WeightVector { WeightVector(int sz, std::vector<double> values) { size_t v_idx = 0; for (size_t i = 0; i < sz; i++) { w.push_back(values[v_idx]); v_idx++; // carousel if (v_idx >= values.size()) v_idx = 0; } // no normalization } std::vector<double> w; }; struct AllWeightVectors { AllWeightVectors(int sz) { std::vector<double> values = { 0.1, 0.3 }; WeightVector v = WeightVector(sz, values); w.push_back(v); values = { 0, 0.5, 1 }; v = WeightVector(sz, values); w.push_back(v); values = { 0 }; // senseless v = WeightVector(sz, values); w.push_back(v); values = { 0, -10, 10 }; // not feasable v = WeightVector(sz, values); w.push_back(v); } std::vector<WeightVector> w; }; typedef typename std::vector<int> TypeV; typedef typename std::vector<TypeV> ContainerTypeV; // vector typedef typename std::vector<int> TypeD; typedef typename std::deque<TypeD> ContainerTypeD; // deque typedef typename metric::edmClassifier<TypeV, CC45> C45_V; typedef typename metric::edmClassifier<TypeV, CSVM> SVM_V; typedef typename metric::edmClassifier<TypeD, CC45> C45_D; typedef typename metric::edmClassifier<TypeD, CSVM> SVM_D; // weak learners against all datasets ans all test samples defined // with datasets based on vector TEMPLATE_TEST_CASE("test_weak_V", "[ensemble]", C45_V, SVM_V) { AllDatasets<ContainerTypeV> DS; AllExamples<ContainerTypeV> EX; for (auto ds : DS.dataset) { TestType l = TestType(); REQUIRE_NOTHROW(l.train(ds.data, ds.features, ds.response)); for (auto ex : EX.dataset) { std::vector<bool> prediction; REQUIRE_NOTHROW(l.predict(ex.data, ex.features, prediction)); } } } //* // boosting with both weak learners against all datasets ans all test samples defined // with datasets based on vector TEMPLATE_TEST_CASE("test_boosting_V", "[ensemble]", C45_V, SVM_V) { AllDatasets<ContainerTypeV> DS; AllExamples<ContainerTypeV> EX; for (auto ds : DS.dataset) { TestType wl = TestType(); // weak int ens_len[6] = { -2, -1, 0, 1, 2, 10 }; // ensemble length for (int idx1 = 0; idx1 < 6; idx1++) { double share[10] = { -0.5, 0, 0.0001, 0.1, 0.5, 0.75, 0.9, 0.9999, 1.0, 0.5 }; for (int idx2 = 0; idx2 < 10; idx2++) { double share_minor[10] = { -0.5, 0, 0.0001, 0.1, 0.5, 0.75, 0.9, 0.9999, 1.0, 0.5 }; for (int idx3 = 0; idx3 < 10; idx3++) { // boosting with weak learner of Learner type auto boosting = metric::Boosting<TypeV, TestType, metric::SubsampleRUS<TypeV>>( ens_len[idx1], share[idx2], share_minor[idx3], wl); // test traing REQUIRE_NOTHROW(boosting.train(ds.data, ds.features, ds.response)); for (auto ex : EX.dataset) { std::vector<bool> prediction; REQUIRE_NOTHROW(boosting.predict(ex.data, ex.features, prediction)); } // additional parameter loop for Bagging only double share_learner_type[10] = { -0.5, 0, 0.0001, 0.1, 0.5, 0.75, 0.9, 0.9999, 1.0, 0.5 }; for (int idx4 = 0; idx4 < 10; idx4++) { // bagging of 2 weak learners: types are edmC45 and Learner using WeakLrnVariant = std::variant<metric::edmC45<TypeV>, TestType>; std::vector<WeakLrnVariant> clSet2 = {}; WeakLrnVariant weak1 = metric::edmC45<TypeV>(2, 1e-3, 0.25, true); WeakLrnVariant weak2 = TestType(); clSet2.push_back(weak1); clSet2.push_back(weak2); auto bagging = metric::Bagging<TypeV, WeakLrnVariant, metric::SubsampleRUS<TypeV>>( ens_len[idx1], share[idx2], share_minor[idx3], { share_learner_type[idx3], 1.0 - share_learner_type[idx3] }, clSet2); // test traing REQUIRE_NOTHROW(bagging.train(ds.data, ds.features, ds.response)); for (auto ex : EX.dataset) { std::vector<bool> prediction; REQUIRE_NOTHROW(bagging.predict(ex.data, ex.features, prediction)); } } } } } } } // testing both subsamlers againt satasets, weight vectors and container types TEMPLATE_TEST_CASE("test_subsamplers", "[enemble]", ContainerTypeV, ContainerTypeD) { AllDatasets<TestType> DS; for (auto ds : DS.dataset) { TestType subset = {}; metric::Subsample<TypeD> sSimple; metric::SubsampleRUS<TypeD> sRUS; int ens_len[6] = { 0, 1, 2, 10 }; // ensemble length for (int idx1 = 0; idx1 < 6; idx1++) { auto all_w = AllWeightVectors(ds.data.size()); for (auto w : all_w.w) { double share[10] = { -0.5, 0, 0.0001, 0.1, 0.5, 0.75, 0.9, 0.9999, 1.0, 0.5 }; for (int idx2 = 0; idx2 < 10; idx2++) { double share_minor[10] = { -0.5, 0, 0.0001, 0.1, 0.5, 0.75, 0.9, 0.9999, 1.0, 0.5 }; for (int idx3 = 0; idx3 < 10; idx3++) { bool replacement[2] = { true, false }; for (int idx4 = 0; idx4 < 2; idx4++) { REQUIRE_NOTHROW(sSimple(ds.data, ds.features, ds.response, w.w, share[idx2], 1 - share_minor[idx3], subset, replacement[idx4])); REQUIRE_NOTHROW(sRUS(ds.data, ds.features, ds.response, w.w, share[idx2], 1 - share_minor[idx3], subset, replacement[idx4])); } } } } } } }
10,390
C++
.cpp
252
30.900794
111
0.535739
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,604
helpers.cpp
metric-space-ai_metric/tests/ensembles_tests/assets/helpers.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Panda Team */ #include <iostream> #include <fstream> #include <vector> #include <string> template <class ContainerType> ContainerType read_csv(std::string filename) { typedef typename ContainerType::value_type LINE; std::string line; int pos; ContainerType array = {}; std::ifstream in(filename); if (!in.is_open()) { std::cout << "Failed to open file" << std::endl; return array; } while (getline(in, line)) { LINE ln; while ((pos = line.find(',')) >= 0) { std::string field = line.substr(0, pos); line = line.substr(pos + 1); ln.push_back(field); } ln.push_back(line); array.push_back(ln); } return array; }
968
C++
.cpp
34
23.264706
69
0.620838
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,605
dwt_tests.cpp
metric-space-ai_metric/tests/transform_test/dwt_tests.cpp
#include <catch2/catch.hpp> #include "metric/transform/wavelet.hpp" #include <random> using DM = blaze::DynamicMatrix<double>; using SM = blaze::CompressedMatrix<double>; using VBM = std::vector<blaze::DynamicVector<double>>; using VVM = std::vector<std::vector<double>>; using BV = blaze::DynamicVector<double>; using SV = std::vector<double>; TEMPLATE_TEST_CASE("dwt2blaze", "[transform]", DM, SM) { TestType data2d = { { 0, 0, 0, 0, 0, 0, 1, 2, 3, 4 }, { 0, 0, 0, 0, 0, 0, 8, 7, 6, 5 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 5, 1, 5, 0, 0, 0, 0, 0 }, { 0, 0, 1, 5, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, -2, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, -1, 0, 0, 0, 0, 0, 0, 0 }, }; std::tuple<TestType, TestType, TestType, TestType> splitted; TestType restored; REQUIRE_NOTHROW(splitted = wavelet::dwt2(data2d, 4)); REQUIRE_NOTHROW(restored = wavelet::idwt2(std::get<0>(splitted), std::get<1>(splitted), std::get<2>(splitted), std::get<3>(splitted), 4, data2d.rows(), data2d.columns())); auto maxdiff = double(blaze::max(blaze::abs(restored - data2d))); REQUIRE(maxdiff <= std::numeric_limits<double>::epsilon()*1e6); } TEMPLATE_TEST_CASE("dwt2old", "[transform]", VBM, VVM) { TestType data2d = { { 0, 0, 0, 0, 0, 0, 1, 2, 3, 4 }, { 0, 0, 0, 0, 0, 0, 8, 7, 6, 5 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 5, 1, 5, 0, 0, 0, 0, 0 }, { 0, 0, 1, 5, 1, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, -2, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 }, { 1, 1, -1, 0, 0, 0, 0, 0, 0, 0 }, }; std::tuple<TestType, TestType, TestType, TestType> splitted; TestType restored; REQUIRE_NOTHROW(splitted = wavelet::dwt2(data2d, 4)); REQUIRE_NOTHROW(restored = wavelet::idwt2(std::get<0>(splitted), std::get<1>(splitted), std::get<2>(splitted), std::get<3>(splitted), 4, data2d.size(), data2d[0].size())); double maxdiff = 0; for (size_t i = 0; i < restored.size(); ++i) { for (size_t j = 0; j < restored[0].size(); ++j) { double diff = std::abs(restored[i][j] - data2d[i][j]); if (diff > maxdiff) maxdiff = diff; } } REQUIRE( maxdiff <= std::numeric_limits<double>::epsilon()*1e6 ); } TEMPLATE_TEST_CASE("dwt_d", "[transform]", BV/*, SV*/) { TestType data = {1, 2, 3, 4, 1, 0, 20, 0, 0, 5, -10, 0, -4, -2, 0, 0, 0, 0, 0, 1, 2, 3, 4}; std::tuple<TestType, TestType> splitted; TestType restored; int wavelet = 4; REQUIRE_NOTHROW(splitted = wavelet::dwt(data, wavelet)); REQUIRE_NOTHROW(restored = wavelet::idwt(std::get<0>(splitted), std::get<1>(splitted), wavelet, data.size())); double maxdiff = 0; for (size_t i = 0; i < restored.size(); ++i) { double diff = std::abs(restored[i] - data[i]); if (diff > maxdiff) maxdiff = diff; } REQUIRE( maxdiff <= std::numeric_limits<double>::epsilon()*1e6 ); } TEST_CASE("Wavelet reconstruction") { std::random_device rd; std::mt19937 me{rd()}; std::uniform_real_distribution<double> d(-10, 10); using Data = blaze::DynamicVector<double>; auto l = GENERATE(100, 1000, 10000, 100000); Data v(l); for (size_t i = 0; i < l; ++i) { v[i] = d(me); } std::tuple<Data, Data> splitted; Data restored; int wavelet = GENERATE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); REQUIRE_NOTHROW(splitted = wavelet::dwt(v, wavelet)); REQUIRE_NOTHROW(restored = wavelet::idwt(std::get<0>(splitted), std::get<1>(splitted), wavelet, v.size())); double maxdiff = 0; for (size_t i = 0; i < restored.size(); ++i) { double diff = std::abs(restored[i] - v[i]); if (diff > maxdiff) maxdiff = diff; } REQUIRE( maxdiff <= std::numeric_limits<double>::epsilon()*1e7 ); } //TEST_CASE("1d") //{ // using V = blaze::DynamicVector<double>; // // V data = {1, 2, 3, 4}; // auto [cA, cD] = wavelet::dwt(data, 1); // // std::cout << cA << std::endl; // std::cout << cD << std::endl; // //}
4,932
C++
.cpp
126
31.388889
95
0.488596
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,606
hog_tests.cpp
metric-space-ai_metric/tests/transform_test/hog_tests.cpp
#include <catch2/catch.hpp> #include <limits> #include <iostream> #include "metric/utils/datasets.hpp" #include "metric/transform/hog.hpp" using namespace metric; using Matrix = blaze::DynamicMatrix<double>; using Vector = blaze::DynamicVector<double>; TEST_CASE("hog", "[transform][hog]") { auto hog = HOG<double>(9, 128, 2); auto [shape, data] = Datasets::loadImages("images-list"); blaze::DynamicMatrix<double> image(shape[1], shape[2]); size_t index = 0; for (size_t i = 0; i < shape[1]; ++i) { for (auto e = image.begin(i); e != image.end(i); ++e) { *e = data[index++]; } } //blaze::DynamicMatrix<double> image = blaze::generate( 320, 320, []( size_t i, size_t j ){ return 2.1F + 1.1F*(i*3UL+j); } ); //std::cout << image << std::endl; blaze::DynamicVector<double, blaze::rowVector> r = blaze::trans(hog.encode(image)); //std::cout << r.size() << std::endl; //std::cout << r << std::endl; auto distance = hog.getGroundDistance(image, 1, 1); //std::cout << distance. << std::endl; //std::cout << distance << std::endl; }
1,058
C++
.cpp
28
35.607143
127
0.655206
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,607
wavelet_tests.cpp
metric-space-ai_metric/tests/transform_test/wavelet_tests.cpp
#include <catch2/catch.hpp> #include "metric/transform/wavelet.hpp" TEMPLATE_TEST_CASE("DaubechiesMat", "[transform][wavelet]", float, double) { auto wnum = GENERATE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); const auto w = wavelet::dbwavf<blaze::DynamicVector<TestType>>(wnum); auto [Lo_D, Hi_D, Lo_R, Hi_R] = wavelet::orthfilt(w); std::reverse(Lo_D.begin(), Lo_D.end()); std::reverse(Hi_D.begin(), Hi_D.end()); const size_t signalLength = 24; const auto dmat = wavelet::DaubechiesMat<TestType>(signalLength, wnum * 2); const size_t fl = wnum * 2; REQUIRE(blaze::subvector(blaze::row(dmat, 0), 0, fl) == Lo_D); REQUIRE(blaze::subvector(blaze::row(dmat, dmat.rows() / 2), 0, fl) == Hi_D); const auto sumLow = blaze::sum(blaze::row(dmat, 0)); for (size_t i = 0; i < dmat.rows() / 2; ++i) { REQUIRE(Approx(sumLow) == blaze::sum(blaze::row(dmat, i))); } const auto sumHigh = blaze::sum(blaze::row(dmat, dmat.rows() / 2)); for (size_t i = dmat.rows() / 2; i < dmat.rows(); ++i) { /* Set scale = 1 due problem with Approx(0.0) comparation */ REQUIRE(Approx(sumHigh).scale(1.) == blaze::sum(blaze::row(dmat, i))); } } TEMPLATE_TEST_CASE("DaubechiesMat()_ZeroDerivative", "[transform][wavelet]", float, double) { const blaze::DynamicVector<TestType> data = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; const auto dmat = wavelet::DaubechiesMat<TestType>(data.size(), 4, wavelet::Padding::ZeroDerivative); const auto result = dmat * data; const blaze::DynamicVector<TestType> expected = {{1.379538}, {3.725003}, {6.553430}, {9.381957}, {12.339693}, {-0.129410}, {-9.901802e-13}, {-9.9e-13}, {-9.9e-13}, {0.482963}}; REQUIRE(result.size() == expected.size()); for (size_t i = 0; i < expected.size(); ++i) { if (result[i] > 1e-10) { REQUIRE(expected[i] == Approx(result[i])); } } } TEMPLATE_TEST_CASE("dbwavf", "[transform][wavelet]", std::vector<float>, std::vector<double>, blaze::DynamicVector<float>, blaze::DynamicVector<double>, blaze::CompressedVector<float>, blaze::CompressedVector<double>) { std::array<TestType, 11> coeffs; coeffs[1] = {0.5000000000000000, 0.5000000000000000}; coeffs[2] = {0.3415063509461097, 0.5915063509461097, 0.15849364905389035, -0.09150635094610966}; coeffs[3] = {0.23523360389208187, 0.5705584579157218, 0.3251825002631163, -0.09546720778416369, -0.06041610415519811, 0.024908749868441868}; coeffs[4] = {0.16290171402564918, 0.5054728575459145, 0.44610006912337985, -0.019787513117822324, -0.13225358368451987, 0.02180815023708863, 0.02325180053549088, -0.007493494665180737}; coeffs[5] = {0.1132094912917792, 0.4269717713525142, 0.5121634721295986, 0.09788348067390469, -0.17132835769146745, -0.02280056594177365, 0.05485132932106683, -0.004413400054179128, -0.008895935050977096, 0.002358713969533936}; coeffs[6] = {0.07887121600145072, 0.3497519070376178, 0.5311318799408691, 0.22291566146501776, -0.15999329944606142, -0.09175903203014758, 0.0689440464873723, 0.019461604854164663, -0.022331874165094537, 0.0003916255761485779, 0.003378031181463938, -0.0007617669028012533}; coeffs[7] = {0.05504971537281186, 0.28039564181276255, 0.5155742458180986, 0.3321862411055397, -0.10175691123134625, -0.15841750564033286, 0.050423232504694084, 0.057001722579871586, -0.02689122629484544, -0.01171997078210329, 0.008874896189680764, 0.00030375749770106936, -0.0012739523590936867, 0.00025011342656124536}; coeffs[8] = {0.03847781105407624, 0.22123362357612492, 0.4777430752138737, 0.4139082662111959, -0.011192867666880218, -0.20082931639048904, 0.0003340970462201188, 0.09103817842365776, -0.01228195052284841, -0.031175103325139432, 0.009886079648350761, 0.006184422409815923, -0.003443859628441809, -0.00027700227447938935, 0.00047761485564962614, -8.30686306866127e-05}; coeffs[9] = {0.0269251747946628, 0.17241715190697796, 0.42767453217970763, 0.46477285718314737, 0.09418477475318378, -0.2073758809009385, -0.0684767745123831, 0.1050341711395062, 0.021726337730614546, -0.04782363206009703, 0.00017744640661651893, 0.015812082926255862, -0.0033398101131385774, -0.0030274802875450662, 0.0013064836402472458, 0.00016290733567609223, -0.00017816487951077768, 2.7822757017154863e-05}; coeffs[10] = {0.01885857879612069, 0.1330610913969209, 0.3727875357432334, 0.48681405536682, 0.19881887088450872, -0.1766681008970563, -0.13855493936048316, 0.09006372426669666, 0.06580149355053501, -0.050483285598389716, -0.020829624043800808, 0.023484907048698562, 0.002550218483907239, -0.00758950116792825, 0.0009866626824816026, 0.0014088432950973382, -0.00048497391992820553, -8.235450304538899e-05, 6.617718342555339e-05, -9.379207813750205e-06}; const int wnum = GENERATE(1, 2, 3, 4, 5, 6, 7, 8, 9, 10); const auto r = wavelet::dbwavf<TestType>(wnum); REQUIRE(r.size() == coeffs[wnum].size()); for (size_t i = 0; i < r.size(); ++i) { REQUIRE(Approx(r[i]) == coeffs[wnum][i]); } }
5,042
C++
.cpp
80
59.5375
114
0.718529
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,608
sparsification_tests.cpp
metric-space-ai_metric/tests/sparsification_tests/sparsification_tests.cpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #include <catch2/catch.hpp> #include "metric/utils/graph/sparsify.hpp" TEMPLATE_TEST_CASE("kruskal_empty_graph", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(0, 0); blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input); REQUIRE(output.rows() == 0); REQUIRE(output.columns() == 0); } TEMPLATE_TEST_CASE("kruskal_not_connected_nodes", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(10, 10); input.reserve(0); blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input); REQUIRE(output.rows() == 10); REQUIRE(output.columns() == 10); REQUIRE(output.nonZeros() == 0); } TEMPLATE_TEST_CASE("kruskal_two_nodes_one_connection", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(2, 2); input.reserve(1); input(0, 1) = 5; blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input); REQUIRE(output.rows() == 2); REQUIRE(output.columns() == 2); REQUIRE(output.nonZeros() == 2); REQUIRE(output(1, 0) == 5); REQUIRE(output(0, 1) == 5); } TEMPLATE_TEST_CASE("kruskal_two_trees", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(4, 4); input.reserve(4); input(0, 1) = 1; input(1, 2) = 100; input(2, 3) = 2; input(0, 3) = 200; blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input); REQUIRE(output.rows() == 4); REQUIRE(output.columns() == 4); REQUIRE(output.nonZeros() == 6); REQUIRE(output(1, 0) == 1); REQUIRE(output(3, 2) == 2); REQUIRE(output(2, 1) == 100); REQUIRE(output(0, 1) == 1); REQUIRE(output(2, 3) == 2); REQUIRE(output(1, 2) == 100); } TEMPLATE_TEST_CASE("kruskal_matrix_creation_order", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(4, 4); input.reserve(4); input.append(0, 1, 1); input.finalize(1); input.append(1, 2, 100); input.finalize(2); input.append(2, 3, 2); input.append(0, 3, 200); input.finalize(3); blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input); REQUIRE(output.rows() == 4); REQUIRE(output.columns() == 4); REQUIRE(output.nonZeros() == 6); REQUIRE(output(1, 0) == 1); REQUIRE(output(3, 2) == 2); REQUIRE(output(2, 1) == 100); REQUIRE(output(0, 1) == 1); REQUIRE(output(2, 3) == 2); REQUIRE(output(1, 2) == 100); } TEMPLATE_TEST_CASE("kruskal_maximum_spanning_tree", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(4, 4); input.reserve(4); input(0, 1) = 1; input(1, 2) = 100; input(2, 3) = 2; input(0, 3) = 200; blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input, false); REQUIRE(output.rows() == 4); REQUIRE(output.columns() == 4); REQUIRE(output.nonZeros() == 6); REQUIRE(output(3, 2) == 2); REQUIRE(output(2, 1) == 100); REQUIRE(output(0, 3) == 200); REQUIRE(output(2, 3) == 2); REQUIRE(output(1, 2) == 100); REQUIRE(output(3, 0) == 200); } TEMPLATE_TEST_CASE("kruskal_input_bad_size", "[sparsification]", float, double) { blaze::CompressedMatrix<TestType, blaze::columnMajor> input(3, 4); input.reserve(1); input(0, 1) = 1; try { blaze::CompressedMatrix<TestType, blaze::columnMajor> output = metric::sparsify_spanning_tree(input); throw std::runtime_error("matrix property of n==m is not checked"); } catch (std::invalid_argument& e) { // nothing here } }
4,213
C++
.cpp
115
31.852174
89
0.650295
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,609
archive.h
metric-space-ai_metric/examples/space_examples/assets/3dparty/serialize/archive.h
/* -*- mode: c++ -*- */ // archive.h // // Dmitry Vinokurov <vinick@omskproject.sotline.ru> // Last change: Time-stamp: <22-Sep-2005 15:03:55 vinick> // // #ifndef ARCHIVE_H_GUARD #define ARCHIVE_H_GUARD //#include "meta/type_traits.h" //#include "meta/eval_if.h" // #include "stl_saver.h" // #include "stl_loader.h" // #include "string_serialize.h" // #include "metaserialize.h" // #include "splitmember.h" #include <vector> #include <set> #include <map> #include <list> #include <string> #include <deque> #include <cstring> namespace serialize { class access { public: template <typename Archive, typename T> static void member_load( Archive & ar, T &t) { t.load(ar,0); } template <typename Archive, typename T> static void member_save( Archive & ar, const T &t) { t.save(ar,0); } }; template<class Archive, class T> struct member_saver { static void invoke(Archive & ar, const T & t ) { access::member_save(ar, t); } }; template<class Archive, class T> struct member_loader { static void invoke(Archive & ar, T & t ) { access::member_load(ar, t); } }; // detail template<class Archive, class T> inline void split_member( Archive & ar, T & t) { using typex = typename std::conditional<Archive::is_saving, serialize::member_saver<Archive,T>, serialize::member_loader<Archive, T>>::type; typex::invoke(ar, t); } // split member function serialize funcition into save/load #define SERIALIZE_SPLIT_MEMBERS() \ friend class access; \ template<class Archive> \ void serialize(Archive &ar, const unsigned int) \ { \ serialize::split_member(ar, *this); \ } template <typename Archive, typename Container> void save_collection(Archive & ar, const Container & c) { unsigned int count = c.size(); ar & count; typename Container::const_iterator p = c.begin(); while(count-- > 0 ) { ar & *p; ++p; } } template<typename Archive, typename T, typename Allocator> inline void save(Archive & ar, const std::vector<T,Allocator> & v) { save_collection<Archive,std::vector<T,Allocator> >(ar,v); } //! outer wrapper for save compound types which may serialize themself template<typename Archive,typename T> inline void save(Archive & ar, const T & t) { const_cast<T&>(t).serialize(ar, 0); } template<typename Archive, typename T, typename Allocator> inline void save(Archive & ar, const std::list<T,Allocator> & v) { save_collection<Archive,std::list<T,Allocator> >(ar,v); } template<typename Archive, typename T, typename Allocator> inline void save(Archive & ar, const std::deque<T,Allocator> & v) { save_collection<Archive,std::deque<T,Allocator> >(ar,v); } template<typename Archive, typename Key,typename Type, typename Compare, typename Allocator> inline void save(Archive & ar, const std::map<Key,Type,Compare,Allocator> & v) { save_collection<Archive,std::map<Key,Type,Compare,Allocator> >(ar,v); } template<typename Archive, typename Key, typename Compare, typename Allocator> inline void save(Archive & ar, const std::set<Key,Compare,Allocator> & v) { save_collection<Archive,std::set<Key,Compare,Allocator> >(ar,v); } template <typename Archive, typename First, typename Second> inline void save(Archive & ar, const std::pair<First,Second> & v) { ar & v.first & v.second; } template<class Container> class reserve_imp { public: void operator()(Container &s, unsigned int count) const { s.reserve(count); } }; template<class Container> class no_reserve_imp { public: void operator()(Container & /* s */, unsigned int /* count */) const{} }; // sequential container input template<class Archive, class Container> struct archive_input_seq { inline void operator()(Archive &ar, Container &s) { typedef typename Container::value_type type; type t; ar.load(t); s.push_back(t); } }; // map input template<class Archive, class Container> struct archive_input_map { inline void operator()(Archive &ar, Container &s) { typedef std::pair<typename Container::key_type, typename Container::mapped_type > type; type t; ar.load(t); s.insert(t); } }; // set input template<class Archive, class Container> struct archive_input_set { inline void operator()(Archive &ar, Container &s) { typedef typename Container::value_type type; type t; ar.load(t); s.insert(t); } }; template <typename Archive, typename Container,typename InputFunction, class R> void load_collection(Archive & ar, Container & s) { s.clear(); // retrieve number of elements unsigned int count=0; ar & count; R rx; rx(s, count); InputFunction ifunc; for(;count > 0;--count) { ifunc(ar, s); } } //! outer wrapper for load compound types which may serialize themself template<typename Archive,typename T> inline void load(Archive & ar, T & t) { t.serialize(ar, 0); } template<typename Archive,typename T, typename Allocator> inline void load(Archive & ar, std::vector<T,Allocator> & v) { serialize::load_collection<Archive, std::vector<T,Allocator>, archive_input_seq<Archive, std::vector<T, Allocator> >, reserve_imp<std::vector<T, Allocator> > >(ar,v); } template<typename Archive,typename T, typename Allocator> inline void load(Archive & ar, std::list<T,Allocator> & v) { load_collection<Archive, std::list<T,Allocator>, archive_input_seq<Archive, std::list<T, Allocator> >, no_reserve_imp<std::list<T, Allocator> > >(ar,v); } template<typename Archive,typename T, typename Allocator> inline void load(Archive & ar, std::deque<T,Allocator> & v) { load_collection<Archive, std::deque<T,Allocator>, archive_input_seq<Archive, std::deque<T, Allocator> >, no_reserve_imp<std::deque<T, Allocator> > >(ar,v); } template<typename Archive,typename Key,typename Type,typename Compare, typename Allocator> inline void load(Archive & ar, std::map<Key,Type,Compare,Allocator> & v) { load_collection<Archive, std::map<Key,Type,Compare,Allocator>, archive_input_map<Archive, std::map<Key, Type, Compare, Allocator> >, no_reserve_imp<std::map<Key, Type, Compare, Allocator > > >(ar,v); } template<typename Archive,typename Key,typename Compare, typename Allocator> inline void load(Archive & ar, std::set<Key,Compare,Allocator> & v) { load_collection<Archive, std::set<Key,Compare,Allocator>, archive_input_set<Archive, std::set<Key, Compare, Allocator> >, no_reserve_imp<std::set<Key, Compare, Allocator> > >(ar,v); } template <typename Archive, typename First, typename Second> inline void load(Archive & ar, std::pair<First,Second> & v) { ar & v.first & v.second; } //! outer wrapper for saving char strings template<typename Archive> inline void save(Archive & ar, const char * s) { std::size_t l = strlen(s); ar & l; ar.binary_save(s,l); } //! outer wrapper for saving std::string template<typename Archive> inline void save(Archive & ar, const std::string & s) { std::size_t l = s.size(); ar.save(l); ar.binary_save(s.data(),l); } //! outer wrapper for loading char strings //! buffer must be inirialized and has enough size; template<typename Archive> inline void load(Archive & ar, char * s) { std::size_t l=0; ar.load(l); ar.binary_load(s,l); s[l]='\0'; } //! outer wrapper for loading std::string template<typename Archive> inline void load(Archive & ar, std::string &s) { std::size_t l=0; ar.load(l); s.resize(l); ar.binary_load(const_cast<char*>(s.data()),l); } /** \class saver saver.h * \brief metastructure for different ways of saving fundamental and compound types * */ //! partial specialization of struct saver for compound types template <typename T,typename Archive> struct compound_saver { static void invoke(Archive & ar,const T &t) { serialize::save(ar,t); } }; //! partial specialization of struct saver for fundamental types template <typename T,typename Archive> struct binary_saver { static void invoke(Archive & ar,const T &t) { ar.binary_save(&t,sizeof(T)); } }; /** \class loader * \brief metastructure for different ways of loading fundamental and compound types * */ template <typename T,typename Archive> struct compound_loader { static void invoke(Archive & ar,T &t) { serialize::load(ar,t); } }; //! partial specialization of struct loader for fundumental types template <typename T,typename Archive> struct binary_loader { static void invoke(Archive & ar,T &t) { ar.binary_load(&t,sizeof(T)); } }; /** \class oarchive archive.h "serialize/archive.h" * * */ template<typename OStream> class oarchive { OStream & os; typedef typename OStream::char_type char_type; typedef oarchive<OStream> self_type; public: constexpr static bool is_saving = true; oarchive(OStream & ostr):os(ostr) {} ~oarchive() { os.flush();} template <typename T> oarchive & operator & (const T & t) { save(t); return *this; } template<typename T> oarchive & operator << (const T & t) { *this & t; return *this; } template<typename T> void save(const T &t) { using typex = typename std::conditional<std::is_fundamental<T>::value, serialize::binary_saver<T,oarchive<OStream>>, serialize::compound_saver<T, oarchive<OStream>>>::type; typex::invoke(*this,t); } void binary_save(const void * address, std::size_t count) { count = ( count + sizeof(typename OStream::char_type) - 1) / sizeof(typename OStream::char_type); os.write(static_cast<const typename OStream::char_type *>(address), count ); } }; /** \class iarchive archive.h "serialize/archive.h" * * */ template <typename IStream> class iarchive { private: IStream & is; public: //enum { is_saving = 1 }; constexpr static bool is_saving = false; iarchive(IStream & istr):is(istr) {} ~iarchive() {} bool operator !()const { return is.fail();} operator void *() const {return is.operator void*();} template <typename T> iarchive & operator >> (T & t) { return *this & t; } template <typename T> iarchive & operator & (T & t) { load(t); return *this; } template<typename T> void load(T &t) { using typex = typename std::conditional<std::is_fundamental<T>::value, serialize::binary_loader<T,iarchive<IStream>>, serialize::compound_loader<T, iarchive<IStream>>>::type; typex::invoke(*this,t); } void binary_load(void * address, std::size_t count) { std::size_t s = count / sizeof(typename IStream::char_type); is.read(static_cast<typename IStream::char_type *>(address),s); } }; }; #endif // ARCHIVE_H_GUARD
11,116
C++
.h
403
24.138958
178
0.672954
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,614
donuts.hpp
metric-space-ai_metric/examples/transform_examples/donuts.hpp
#ifndef DONUTS_HPP #define DONUTS_HPP #include <blaze/Blaze.h> #include <cmath> #include <iostream> //#include <tuple> //#include <opencv2/opencv.hpp> /* blaze::DynamicMatrix<double> create_filter(double sigma) { std::size_t size = ceil(sigma)*3 + 1; blaze::DynamicMatrix<double> out (size, size, 0); double sz = (double)(size - 1)/2.0; double x, y, h; double max_h = 0; double sum_h = 0; for (int yi = 0; yi<size; ++yi) { for (int xi = 0; xi<size; ++xi) { x = -sz + xi; y = -sz + yi; h = exp(-(x*x + y*y)/(2*sigma*sigma)); if (h>max_h) max_h = h; sum_h += h; out(yi, xi) = h; } } // TODO add h(h<eps*max(h(:))) = 0 for stability if (sum_h != 0) { out = out/sum_h; } return out; } blaze::DynamicMatrix<double> gblur3(blaze::DynamicMatrix<double> in) { auto im = blaze2cv(in); cv::Mat imb; cv::GaussianBlur(im, imb, cv::Size(3, 3), 0); return cv2blaze(imb); } blaze::DynamicMatrix<double> gaussianBlur(blaze::DynamicMatrix<double> in, double sigma) { auto im = blaze2cv(in); cv::Mat krn = blaze2cv(create_filter(sigma)); cv::Mat imb; cv::filter2D(im, imb, cv::DataType<double>::type, krn); return cv2blaze(imb); } // */ template <typename T> std::vector<std::vector<T>> binarize(std::vector<std::vector<T>> in, T level) { std::vector<std::vector<T>> out; for (size_t y=0; y<in.size(); ++y) { std::vector<T> line; for (size_t x=0; x<in[0].size(); ++x) { if (in[y][x] < level) line.push_back(0); else line.push_back(1); } out.push_back(line); } return out; } template <typename T> std::vector<std::vector<T>> otsu_binarize(std::vector<std::vector<T>> in, float expo_corr = 0, T max = 1, int levels = 100) { std::vector<std::vector<T>> out; size_t height = in.size(); assert(height>0); size_t width = in[0].size(); std::vector<T> v_levels; T step = max/(float)levels; T curr_level = 0; for (int i=0; i<levels; ++i) { v_levels.push_back(curr_level); curr_level += step; } assert(abs(v_levels[levels-1] - max + step) < 1e-7); // TODO replace hardcode with type-dependent check! v_levels.push_back(max); size_t pixel_counter = 0; // TODO remove after testing T lum_sum = 0; std::vector<T> lum_hist (levels, 0); std::vector<int> hist (levels, 0); for (size_t y=0; y<height; ++y) { for (size_t x=0; x<width; ++x) { for (size_t i=1; i<levels+1; ++i) { // very inefficient!! TODO replace at least with binary search.. if (in[y][x] > v_levels[i-1] && in[y][x] < v_levels[i]) { hist[i-1]++; lum_hist[i-1] += in[y][x]; lum_sum += in[y][x]; pixel_counter++; } } } } //assert(pixel_counter==height*width); // FIXME detector pixel loss, TODO fix!! // finding Otsu threshold according to https://en.wikipedia.org/wiki/Otsu%27s_method double omega0 = 0; //(double)hist[0] / (double)pixel_counter; double omega1; // = 1 - omega0; double weighted_lum_sum_0 = 0; double weighted_lum_sum_1 = lum_sum; double mu0; double mu1; //std::vector<double> variance; size_t max_variance_idx = 0; double max_variance = 0; double variance; double mu_diff; for (size_t i=0; i<levels+1; ++i) { omega0 += (double)hist[i] / (double)pixel_counter; omega1 = 1 - omega0; weighted_lum_sum_0 += (double)lum_hist[i]; weighted_lum_sum_1 = lum_sum - weighted_lum_sum_0; mu0 = weighted_lum_sum_0 / omega0; mu1 = weighted_lum_sum_1 / omega1; mu_diff = mu0 - mu1; variance = omega0*omega1*mu_diff*mu_diff; if (variance > max_variance) { max_variance = variance; max_variance_idx = i; } } // here we have max_variance_idx that referce to thresold luminosity we can obtain from v_levels T threshold = v_levels[max_variance_idx]; threshold += max*expo_corr; // applying manual 'exposure correction' for (size_t y=0; y<in.size(); ++y) { std::vector<T> line; for (size_t x=0; x<in[0].size(); ++x) { if (in[y][x] < threshold) line.push_back(0); else line.push_back(1); } out.push_back(line); } return out; } /* // TODO complete if needed blaze::SymmetricMatrix<blaze::CompressedMatrix<int>> imgToGraph4(std::vector<std::vector<int>> in) { size_t h = in.size(); size_t w = in[0].size(); assert(h>0); blaze::SymmetricMatrix<blaze::CompressedMatrix<int>> out; size_t node_idx = w; // indexing starts from 0, but we start pricessing from 2nd row size_t odd = 1; for (size_t y = 1; y<in.size()-1; ++y) { // start from 2nd row, stop befpre last row //if (odd==0) odd = 1; else odd = (odd==1 ? 0 : 1); for (size_t x = 1+odd; x<in[0].size()-1-odd; x += 2) { // start from 2nd col, stop before last col if (in[y][x] > 0) { if (in[y-1][x] > 0) out(node_idx, node_idx - w) = 1; //up if (in[y+1][x] > 0) out(node_idx, node_idx + w) = 1; //down if (in[y][x-1] > 0) out(node_idx, node_idx - 1) = 1; //left if (in[y][x+1] > 0) out(node_idx, node_idx + 1) = 1; //right } } node_idx += w; } for (size_t x = 1; x<in[0].size()-1; ++x) { // first and last rows if (in[0][x] > 0) { // 1st row if (in[1][x] > 0) out(x, x+w) = 1; // down if (in[0][x-1] > 0) out(x, x-1) = 1; // left if (in[0][x+1] > 0) out(x, x+1) = 1; // right } size_t offset = (h-1)*w; if (in[h-1][x] > 0) { // last row if (in[h-2][x] > 0) out(offset+x, offset+x+w) = 1; // up if (in[h-1][x-1] > 0) out(offset+x, offset+x-1) = 1; // left if (in[h-1][x+1] > 0) out(offset+x, offset+x+1) = 1; // right } } for (size_t y = 1; y<in.size()-1; ++y) { // first and last columns // TODO vertical lines! } // TODO corners! return out; } // */ std::vector<std::vector<double>> radial_diff(std::vector<std::vector<double>> in, double step=1.5) { size_t height = in.size(); assert(height>0); size_t width = in[0].size(); int y0 = (int)round(height/2); int x0 = (int)round(width/2); std::vector<std::vector<double>> out; for (int y=0; y<(int)in.size(); ++y) { std::vector<double> line; for (int x=0; x<(int)in[0].size(); ++x) { double fi = atan2(x - x0, y - y0); // TODO remove trigonometry functions, components can be computed without angle! double xi = x - sin(fi) * step; // point for interpolation, '-' for direction to center double yi = y - cos(fi) * step; size_t bxi = (size_t)floor(xi); // base size_t byi = (size_t)floor(yi); double oxi = xi - bxi; // offset double oyi = yi - byi; double c_i = // biliniar interpolation in[byi][bxi] * (1 - oxi) * (1 - oyi) + in[byi + 1][bxi] * oxi * (1 - oyi) + in[byi][bxi + 1] * (1 - oxi) * oyi + in[byi + 1][bxi + 1] * oxi * oyi; line.push_back(abs(c_i - in[y][x])); //line.push_back(c_i); } out.push_back(line); } return out; } template <typename T> blaze::DynamicMatrix<T> radial_diff(const blaze::DynamicMatrix<T> & in, double step=1.5) { size_t height = in.rows(); assert(height>0); size_t width = in.columns(); int y0 = (int)round(height/2); int x0 = (int)round(width/2); blaze::DynamicMatrix<T> out(in.rows(), in.columns()); for (int y=0; y<(int)in.rows(); ++y) { for (int x=0; x<(int)in.columns(); ++x) { //double fi = atan2(x - x0, y - y0); // components can be computed without angle! //double xi = x - sin(fi) * step; // point for interpolation, '-' for direction to center //double yi = y - cos(fi) * step; double d_center = sqrt(pow(x - x0, 2) + pow(y - y0, 2)); if (d_center < 1.5) { out(y, x) = 0; continue; } double xi = x - (x - x0) * step / d_center; // point for interpolation, '-' for direction to center double yi = y - (y - y0) * step / d_center; size_t bxi = (size_t)floor(xi); // base size_t byi = (size_t)floor(yi); double oxi = xi - bxi; // offset double oyi = yi - byi; double c_i = // biliniar interpolation in(byi, bxi) * (1 - oxi) * (1 - oyi) + in(byi + 1, bxi) * oxi * (1 - oyi) + in(byi, bxi + 1) * (1 - oxi) * oyi + in(byi + 1, bxi + 1) * oxi * oyi; out(y, x) = abs(c_i - in(y, x)); } } return out; } /* template <typename T> // old crop version, TODO debug if needed std::tuple<std::vector<std::vector<T>>, std::vector<std::vector<T>>> split_donut(std::vector<std::vector<T>> in, float crop_share = 3.0/5.0) { assert(crop_share>0 && crop_share<1); size_t height = in.size(); assert(height>0); size_t wigth = in[0].size(); size_t crop_middle = round(in[0].size()*crop_share); size_t crop_left = (wigth - crop_middle)/2; size_t crop_right = crop_left + crop_middle; size_t crop_top = (height - crop_middle)/2; // inner part will be ever square size_t crop_bottom = crop_top + crop_middle; std::vector<std::vector<T>> inner; std::vector<std::vector<T>> outer = in; for (size_t y = crop_top; y<crop_bottom; ++y) { std::vector<T> line; for (size_t x = crop_left; x<crop_right; ++x) { line.push_back(in[y][x]); // FIXME some indexing bug.. outer[y][x] = 0; } inner.push_back(line); } return std::make_tuple(inner, outer); } // */ template <typename T> blaze::DynamicMatrix<T> weightingMask(size_t h, size_t w, T radius, T sigma) { // radius is where we expect the donut outline T center_x = T(w) / 2.0; T center_y = T(h) / 2.0; auto mask = blaze::DynamicMatrix<T>(h, w); T r, y_dist, x_dist, r_dist; for (size_t i = 0; i < h; ++i) { for (size_t j = 0; j < w; ++j) { r = sqrt(pow(i - center_y, 2) + pow(j - center_x, 2)); //mask(i, j) = gauss(r, radius, sigma); mask(i, j) = exp(-1 * (pow(r - radius, 2) / pow(2 * sigma, 2))); // non-normalized gaussian } } return mask; } /* void eat_donut(int number) { // old test batch procewssing function boost::gil::rgb8_image_t img; //boost::gil::read_image("assets/" + std::to_string(number) + ".jpg", img, boost::gil::jpeg_tag()); //boost::gil::read_image("assets/new/crop_2020-07-27_16_23_01_776_donut" + std::to_string(number) + ".png", img, boost::gil::jpeg_tag()); //boost::gil::png_read_and_convert_image("assets/new/crop_2020-07-27_16_23_01_776_donut" + std::to_string(number) + ".png", img); boost::gil::read_image("assets/new/crop_2020-07-27_16_23_01_776_donut" + std::to_string(number) + ".png", img, boost::gil::png_tag()); auto gray = boost::gil::color_converted_view<boost::gil::gray8_pixel_t>(const_view(img)); std::vector<std::vector<double>> p; for (int y=0; y<gray.height(); ++y) { std::vector<double> line; for (int x=1; x<gray.width(); ++x) { line.push_back(gray(x, y)/255.0); } p.push_back(line); } auto pr = radial_diff(p); //crop auto [inner, outer] = split_donut(pr, 0.35); // parameter is share of inner size // double max_val = 0; // blaze::DynamicMatrix<double> inner_blaze (inner.size(), inner[0].size(), 0); // for (size_t y=0; y<inner.size(); ++y) { // for (size_t x=0; x<inner[0].size(); ++x) { // inner_blaze(y, x) = inner[y][x]*255; // if (inner_blaze(y, x) > max_val) { // max_val = inner_blaze(y, x); // } // } // } // blaze::DynamicMatrix<double> outer_blaze (outer.size(), outer[0].size(), 0); // for (size_t y=0; y<outer.size(); ++y) { // for (size_t x=0; x<outer[0].size(); ++x) { // outer_blaze(y, x) = outer[y][x]*255; // if (outer_blaze(y, x) > max_val) { // max_val = outer_blaze(y, x); // } // } // } //auto inner_result = metric::fit_hysteresis(inner_blaze); //auto outer_result = metric::fit_hysteresis(outer_blaze); // TODO output!! std::string filenum_prefix = "donut_" + std::to_string(number) + "_"; vector2bmp(p, filenum_prefix + "input.bmp"); vv_to_csv(p, filenum_prefix + "input.csv"); vector2bmp(pr, filenum_prefix + "radial_diff.bmp", 4); vv_to_csv(pr, filenum_prefix + "radial_diff.csv"); vector2bmp(outer, filenum_prefix + "radial_outer.bmp", 4); vv_to_csv(outer, filenum_prefix + "radial_outer.csv"); vector2bmp(inner, filenum_prefix + "radial_inner.bmp", 4); vv_to_csv(inner, filenum_prefix + "radial_inner.csv"); auto outer0 = otsu_binarize(outer, 0); vector2bmp(outer0, filenum_prefix + "binarized_outer_0.bmp"); vv_to_csv(outer0, filenum_prefix + "binarized_outer_0.csv"); auto outer007 = otsu_binarize(outer, 0.07); vector2bmp(outer007, filenum_prefix + "binarized_outer_007.bmp"); vv_to_csv(outer007, filenum_prefix + "binarized_outer_007.csv"); auto outer1 = otsu_binarize(outer, 0.1); vector2bmp(outer1, filenum_prefix + "binarized_outer_01.bmp"); vv_to_csv(outer1, filenum_prefix + "binarized_outer_01.csv"); auto inner0 = otsu_binarize(inner); vector2bmp(inner0, filenum_prefix + "binarized_inner_0.bmp"); vv_to_csv(inner0, filenum_prefix + "binarized_inner_0.csv"); auto inner007 = otsu_binarize(inner, 0.07); vector2bmp(inner007, filenum_prefix + "binarized_inner_007.bmp"); vv_to_csv(inner007, filenum_prefix + "binarized_inner_007.csv"); auto inner1 = otsu_binarize(inner, 0.1); vector2bmp(inner1, filenum_prefix + "binarized_inner_01.bmp"); vv_to_csv(inner1, filenum_prefix + "binarized_inner_01.csv"); } // */ //int main() { // for (size_t i = 1; i<=8; i++) { // eat_donut(i); // } // std::cout << "\ncomplete\n"; // return 0; //} #endif // DONUTS_HPP
14,880
C++
.h
368
32.923913
141
0.541216
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,616
transform.hpp
metric-space-ai_metric/metric/transform.hpp
/* PANDA presents ███╗ ███╗███████╗████████╗██████╗ ██╗ ██████╗ ████████╗██████╗ █████╗ ███╗ ██╗███████╗███████╗ ██████╗ ██████╗ ███╗ ███╗ ████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝ ╚══██╔══╝██╔══██╗██╔══██╗████╗ ██║██╔════╝██╔════╝██╔═══██╗██╔══██╗████╗ ████║ ██╔████╔██║█████╗ ██║ ██████╔╝██║██║ ██║ ██████╔╝███████║██╔██╗ ██║███████╗█████╗ ██║ ██║██████╔╝██╔████╔██║ ██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██║ ██║ ██╔══██╗██╔══██║██║╚██╗██║╚════██║██╔══╝ ██║ ██║██╔══██╗██║╚██╔╝██║ ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║╚██████╗ ██║ ██║ ██║██║ ██║██║ ╚████║███████║██║ ╚██████╔╝██║ ██║██║ ╚═╝ ██║ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ Licensed under MPL 2.0. a library for data time-frequency domain transformation This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) PANDA Team */ #ifndef _METRIC_TRANSFORM_HPP #define _METRIC_TRANSFORM_HPP #include "transform/discrete_cosine.hpp" #include "transform/wavelet.hpp" #endif
2,464
C++
.h
20
60.2
118
0.29485
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,617
metric.hpp
metric-space-ai_metric/metric/metric.hpp
/* PANDA presents ███╗ ███╗███████╗████████╗██████╗ ██╗ ██████╗ ████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝ ██╔████╔██║█████╗ ██║ ██████╔╝██║██║ ██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██║ ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║╚██████╗ Licensed under MPL 2.0. Michael Welsch (c) 2018. A framework to process arbitrary data This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Michael Welsch */ #ifndef _PANDA_METRIC_HPP #define _PANDA_METRIC_HPP #include "correlation.hpp" #include "distance.hpp" #include "mapping.hpp" #include "space.hpp" #include "transform.hpp" #endif
1,149
C++
.h
23
31.347826
67
0.529332
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,618
distance.hpp
metric-space-ai_metric/metric/distance.hpp
/* PANDA presents ███╗ ███╗███████╗████████╗██████╗ ██╗ ██████╗ ██████╗ ██╗███████╗████████╗ █████╗ ███╗ ██╗ ██████╗███████╗ ████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝ ██╔══██╗██║██╔════╝╚══██╔══╝██╔══██╗████╗ ██║██╔════╝██╔════╝ ██╔████╔██║█████╗ ██║ ██████╔╝██║██║ ██║ ██║██║███████╗ ██║ ███████║██╔██╗ ██║██║ █████╗ ██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██║ ██║ ██║██║╚════██║ ██║ ██╔══██║██║╚██╗██║██║ ██╔══╝ ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║╚██████╗ ██████╔╝██║███████║ ██║ ██║ ██║██║ ╚████║╚██████╗███████╗ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═════╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝╚══════╝ Licensed under MPL 2.0. a library for metrics / distance functions This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) PANDA Team */ #ifndef _METRIC_DISTANCE_HPP #define _METRIC_DISTANCE_HPP // include the implementation #include "distance/k-related/L1.hpp" #include "distance/k-related/Standards.hpp" #include "distance/k-structured/EMD.hpp" #include "distance/k-structured/Edit.hpp" #include "distance/k-structured/Kohonen.hpp" #include "distance/k-structured/SSIM.hpp" #include "distance/k-structured/TWED.hpp" #include "distance/k-random/CramervonMises.hpp" #include "distance/k-random/KolmogorovSmirnov.hpp" #include "distance/k-random/RandomEMD.hpp" #endif //_METRIC_DISTANCE_HPP
2,648
C++
.h
29
52.068966
113
0.435095
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,619
correlation.hpp
metric-space-ai_metric/metric/correlation.hpp
/* PANDA presents ███╗ ███╗███████╗████████╗██████╗ ██╗ ██████╗ ██████╗ ██████╗ ██████╗ ██████╗ ███████╗██╗ █████╗ ████████╗██╗ ██████╗ ███╗ ██╗ ████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝ ██╔════╝██╔═══██╗██╔══██╗██╔══██╗██╔════╝██║ ██╔══██╗╚══██╔══╝██║██╔═══██╗████╗ ██║ ██╔████╔██║█████╗ ██║ ██████╔╝██║██║ ██║ ██║ ██║██████╔╝██████╔╝█████╗ ██║ ███████║ ██║ ██║██║ ██║██╔██╗ ██║ ██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██║ ██║ ██║ ██║██╔══██╗██╔══██╗██╔══╝ ██║ ██╔══██║ ██║ ██║██║ ██║██║╚██╗██║ ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║╚██████╗ ╚██████╗╚██████╔╝██║ ██║██║ ██║███████╗███████╗██║ ██║ ██║ ██║╚██████╔╝██║ ╚████║ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚══════╝╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═══╝ Licensed under MPL 2.0. a library for metrics / distance functions This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) PANDA Team */ #ifndef _METRIC_CORRELATION_HPP #define _METRIC_CORRELATION_HPP #include "correlation/mgc.hpp" // include the implementation #endif // header guard
2,582
C++
.h
20
62.75
120
0.275697
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,620
space.hpp
metric-space-ai_metric/metric/space.hpp
/* PANDA presents ███╗ ███╗███████╗████████╗██████╗ ██╗ ██████╗ ███████╗██████╗ █████╗ ██████╗███████╗ ████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝ ██╔════╝██╔══██╗██╔══██╗██╔════╝██╔════╝ ██╔████╔██║█████╗ ██║ ██████╔╝██║██║ ███████╗██████╔╝███████║██║ █████╗ ██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██║ ╚════██║██╔═══╝ ██╔══██║██║ ██╔══╝ ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║╚██████╗ ███████║██║ ██║ ██║╚██████╗███████╗ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚══════╝╚═╝ ╚═╝ ╚═╝ ╚═════╝╚══════╝ Licensed under MPL 2.0. a library for metric space data containers and corresponding basic methods This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) PANDA Team */ #ifndef _METRIC_SPACE_HPP #define _METRIC_SPACE_HPP #include "space/knn_graph.hpp" #include "space/matrix.hpp" #include "space/tree.hpp" #endif
1,919
C++
.h
21
48.047619
91
0.357352
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,621
mapping.hpp
metric-space-ai_metric/metric/mapping.hpp
/* PANDA presents ███╗ ███╗███████╗████████╗██████╗ ██╗ ██████╗ ███╗ ███╗ █████╗ ██████╗ ██████╗ ██╗███╗ ██╗ ██████╗ ████╗ ████║██╔════╝╚══██╔══╝██╔══██╗██║██╔════╝ ████╗ ████║██╔══██╗██╔══██╗██╔══██╗██║████╗ ██║██╔════╝ ██╔████╔██║█████╗ ██║ ██████╔╝██║██║ ██╔████╔██║███████║██████╔╝██████╔╝██║██╔██╗ ██║██║ ███╗ ██║╚██╔╝██║██╔══╝ ██║ ██╔══██╗██║██║ ██║╚██╔╝██║██╔══██║██╔═══╝ ██╔═══╝ ██║██║╚██╗██║██║ ██║ ██║ ╚═╝ ██║███████╗ ██║ ██║ ██║██║╚██████╗ ██║ ╚═╝ ██║██║ ██║██║ ██║ ██║██║ ╚████║╚██████╔╝ ╚═╝ ╚═╝╚══════╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═════╝ Licensed under MPL 2.0. a library for metric spacing converting (dimension reduction and expanding) This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) PANDA Team */ #ifndef _METRIC_MAPPING_HPP #define _METRIC_MAPPING_HPP #include "mapping/ESN.hpp" #include "mapping/KOC.hpp" #include "mapping/PCFA.hpp" #include "mapping/SOM.hpp" #include "mapping/affprop.hpp" #include "mapping/dbscan.hpp" #include "mapping/ensembles.hpp" #include "mapping/hierarchClustering.hpp" #include "mapping/kmeans.hpp" #include "mapping/kmedoids.hpp" #endif
2,412
C++
.h
28
47.785714
109
0.40103
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,622
affprop.hpp
metric-space-ai_metric/metric/mapping/affprop.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 M.Welsch */ #ifndef _METRIC_MAPPING_AFFPROP_HPP #define _METRIC_MAPPING_AFFPROP_HPP #include "../space/matrix.hpp" #include <algorithm> #include <tuple> #include <type_traits> #include <vector> namespace metric { /* A Affinity Propagation implementation based on a similarity matrix. */ // Reference: // Clustering by Passing Messages Between Data Points. // Brendan J. Frey and Delbert Dueck // Science, vol 315, pages 972-976, 2007. // template <typename RecType, typename Metric> class AffProp { using Value = typename std::invoke_result<Metric, const RecType &, const RecType &>::type; public: AffProp() = default; AffProp(Value preference, int maxiter = 200, Value tol = 1.0e-6, Value damp = 0.5) : preference(preference), maxiter(maxiter), tol(tol), damp(damp) { } /** * @brief * * @param data distance matrix * @param preference * @param maxiter * @param tol * @param damp * @return */ auto operator()(const Matrix<RecType, Metric> &DM) const -> std::tuple<std::vector<std::size_t>, std::vector<std::size_t>, std::vector<std::size_t>>; private: Value preference = 0.5; int maxiter = 200; Value tol = 1.0e-6; Value damp = 0.5; }; } // namespace metric #include "affprop.cpp" #endif
1,474
C++
.h
51
26.901961
94
0.711348
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,623
esn_switch_detector.hpp
metric-space-ai_metric/metric/mapping/esn_switch_detector.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2021 Panda Team */ #ifndef _METRIC_ESN_SWITCH_DETECTOR_HPP #define _METRIC_ESN_SWITCH_DETECTOR_HPP #include "ESN.hpp" /** * @class SwitchPredictor - trainable estimator of On-Off and Off-On switches in 3-dimensional timeseries. */ template <typename value_type> class SwitchPredictor { public: /** * @brief create & train model using Blaze dynamic matrix * * @param training_data - 3-dimensional timeseries, samples in rows * @param labels - matrix with single column of label values: -1, 0 or 1. * Number of rows should be the same as in training_dataset * @param wnd_size_ - size of the window used to compute additional sliding stddev feature in preprocessing * and entropy in postprocessing * @param cmp_wnd_sz - size of window used to ensure detected entropy peak really switches the state * @param washout_ - amount of samples to be excluded from training and prediction due to reservoir washout * @param contrast_threshold_ - rate of contrast between averages in the windows of size cmp_wnd_size * to the left and to the right, needed to check if current point contains a switch * @param alpha - ESN parameter * @param beta - ESN parameter */ SwitchPredictor(const blaze::DynamicMatrix<value_type> &training_data, const blaze::DynamicMatrix<value_type> &labels, const size_t wnd_size_ = 15, const size_t cmp_wnd_sz_ = 80, // 150, const size_t washout_ = 500, // 2500, const value_type contrast_threshold_ = 0.2, // 0.3, const value_type alpha_ = 0.4, // 0.1, const value_type beta_ = 0.5); /** * @brief create & train model using vectors of STL containers * * @param training_data - timeseries, each RecType contains 3 values and represents a sample * @param labels - vector of containers with single label value in each: -1, 0 or 1. * Length should be equal to the length of training_dataset * @param wnd_size_ - size of the window used to compute additional sliding stddev feature in preprocessing * and entropy in postprocessing * @param cmp_wnd_sz - size of window used to ensure detected entropy peak really switches the state * @param washout_ - amount of samples to be excluded from training and prediction due to reservoir washout * @param contrast_threshold_ - rate of contrast between averages in the windows of size cmp_wnd_size * to the left and to the right, needed to check if current point contains a switch * @param alpha - ESN parameter * @param beta - ESN parameter */ template <typename RecType> // to be deduced SwitchPredictor(const std::vector<RecType> &training_data, const std::vector<RecType> &labels, const size_t wnd_size_ = 15, const size_t cmp_wnd_sz_ = 80, // 150, const size_t washout_ = 500, // 2500, const value_type contrast_threshold_ = 0.2, // 0.3, const value_type alpha_ = 0.4, // 0.1, const value_type beta_ = 0.5); /** * @brief load trained model from file * * @param filename - file with Blaze image of the model */ SwitchPredictor(const std::string &filename); /** * @brief estimates switches in offline timeseries * * @param dataset - 3-dimensional timeseries with sample in rows * @return column of -1, 0 or 1 values, where -1 and 1 represent On-Off and Off-On switches respectively */ blaze::DynamicMatrix<value_type> encode(const blaze::DynamicMatrix<value_type> &dataset); /** * @brief estimates switches in offline timeseries * * @param dataset - vector of 3-dimensional samples * @return column of -1, 0 or 1 values, where -1 and 1 represent On-Off and Off-On switches respectively */ template <typename RecType> std::vector<value_type> encode(const std::vector<RecType> &dataset); /** * @brief encode_raw - adds the passed slice to buffer and encodes the other slice of the same size taken from * buffer at 150 samples in the past (before last 150 samples) * @param indices - vector of int timestamps of the same size as the input slice * @param dataset - the input timeseries, vector of samples (triplets) * @return - estimation for samples before 150 last ones, * tuple: vector of indices related to the output extimations, and vector of estimated switches, * encoded as: 0 - no switch. -1 - On-Off switch, 1 - Off-On switch * If buffer size is not enough for estimation, the output is empty or of less size than input */ std::tuple<std::vector<unsigned long long int>, std::vector<value_type>> encode_raw(const std::vector<unsigned long long int> &indices, const std::vector<std::vector<value_type>> &dataset); /** * @brief encode - adds the passed slice to buffer and encodes the other slice of the same size taken from buffer * at 150 samples in the past (before last 150 samples) * @param indices - vector of int timestamps of the same size as the input slice * @param dataset - the input timeseries, vector of samples (triplets) * @return - estimation for samples before 150 last ones, * vector of tuples, where 1st value is index of the sample, 2nd is nonzero estimated switch encoded as * -1 - On-Off switch, 1 - Off-On switch. Zero (non-switch) samples are omitted. * If buffer size is not enough for estimation, the output is empty or of less size than input */ std::vector<std::tuple<unsigned long long int, value_type>> encode(const std::vector<unsigned long long int> &indices, const std::vector<std::vector<value_type>> &dataset); /** * @brief make_pairs - converts output of .encode_raw to result similar to output of .encode * i.e. removes zero (non-switch) outputs and rearranges data by pairs * @param indices - indices of input estimations * @param raw_switches - estomated switch values * encoded as: 0 - no switch. -1 - On-Off switch, 1 - Off-On switch * @return */ std::vector<std::tuple<unsigned long long int, value_type>> make_pairs(const std::vector<unsigned long long int> &indices, const std::vector<value_type> &raw_switches); /** * @brief saves the model into the Blaze image * @param desired filename, file will be overwritten if exists */ void save(const std::string &filename); /** * @brief get_parameters * @return tuple of parameters the model was created with */ std::tuple<size_t, size_t, size_t, value_type, value_type, value_type> get_parameters(); private: metric::ESN<std::vector<value_type>, void> esn; size_t wnd_size; // size of the window used to compute additional sliding stddev feature // and postprocessing entropy filter, currently hardcoded to 15 size_t cmp_wnd_sz; // size of window used to ensure detected entropy peak really turns state (i.e is a switch) size_t washout; // ESN parameter, amount of samples to be excluded from training and prediction due to reservoir // washout value_type contrast_threshold; // rate of contrast between averages in the cmp_wnd_size to the left and to the right, // needed to ensure that current point contains a switch value_type alpha; // ESN parameter value_type beta; // ESN parameter std::vector<std::vector<value_type>> buffer = {}; // buffer for accumulation of samples passed online std::vector<unsigned long long int> buffer_idx = {}; // indices size_t online_cnt = 0; // online sample counter /** * @brief v_stddev - compute standard deviation of the vector v * @param v * @param population - flag, determines wheter to compute * biased ("population", divided by N) or unbuased ("sample", divide by N-1) standard deviation * @return standard deviation */ value_type v_stddev(const std::vector<value_type> &v, const bool population = false); /** * @brief preprocess - adds sliding window wtddev feature to the input dataset given as Blaze matrix * @param input - dataset passed by user * @return - augmented dataset */ blaze::DynamicMatrix<value_type, blaze::rowMajor> preprocess(const blaze::DynamicMatrix<value_type, blaze::rowMajor> &input); /** * @brief preprocess - adds sliding window wtddev feature to the input dataset given as vector of RecType samples * @param input - dataset passed by user * @return - augmented dataset */ template <typename RecType> std::vector<RecType> preprocess(const std::vector<RecType> &input); /** * @brief class_entropy - computes entropy of class (switch of no switch) values within sliding window of size * wnd_size * @param data - vector of class values * @param threshold - bound that separate classes, typically 0.5 * @return entropy value */ value_type class_entropy(const blaze::DynamicVector<value_type> &data, const value_type threshold); /** * @brief train - trains model using dataset and labels passed as Blaze matrices * @param training_data * @param labels */ void train(const blaze::DynamicMatrix<value_type> &training_data, const blaze::DynamicMatrix<value_type> &labels); /** * @brief train - trains model using dataset and labels passed as vectors of samples * @param training_data * @param labels */ template <typename RecType> // to be deduced void train(const std::vector<RecType> &training_data, const std::vector<RecType> &labels); }; #include "esn_switch_detector.cpp" #endif // _METRIC_ESN_SWITCH_DETECTOR_HPP
9,437
C++
.h
183
48.650273
117
0.730798
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,624
kmedoids.hpp
metric-space-ai_metric/metric/mapping/kmedoids.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 M.Welsch */ #ifndef _METRIC_MAPPING_KMEDOIDS_HPP #define _METRIC_MAPPING_KMEDOIDS_HPP #include "../space/matrix.hpp" #include <tuple> #include <vector> namespace metric { /** * @brief * * @param DM * @param k * @return */ template <typename RecType, typename Metric> auto kmedoids(const metric::Matrix<RecType, Metric> &DM, int k) -> std::tuple<std::vector<int>, std::vector<int>, std::vector<int>>; } // namespace metric #include "kmedoids.cpp" #endif
682
C++
.h
24
26.833333
69
0.734255
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,625
autoencoder.hpp
metric-space-ai_metric/metric/mapping/autoencoder.hpp
#include "../utils/dnn.hpp" #include <chrono> namespace metric { template <typename InputDataType, typename Scalar> class Autoencoder : public dnn::Network<Scalar> { private: using typename dnn::Network<Scalar>::Matrix; Matrix trainData; Scalar normValue; Matrix convertData(const std::vector<InputDataType> &inputData); std::vector<InputDataType> convertToOutput(const Matrix &data, bool doDenormalization = true); /* Load data (previous data will be erased) */ void loadTrainData(const std::vector<InputDataType> data); public: /* Empty constructor */ Autoencoder(); /* Construct with provided data */ Autoencoder(const std::string &jsonString); ~Autoencoder() = default; void setNormValue(InputDataType _normValue = 0); void train(const std::vector<InputDataType> data, size_t epochs, size_t batchSize); /* Return latent vector */ std::vector<Scalar> encode(const std::vector<InputDataType> &data); /* Return data with size samplesSize * featuresLength */ std::vector<InputDataType> decode(const std::vector<Scalar> &data); /* Predict by autoencoder */ std::vector<InputDataType> predict(const std::vector<InputDataType> data); }; template <typename InputDataType, typename Scalar> Autoencoder<InputDataType, Scalar>::Autoencoder() : normValue(255) {} template <typename InputDataType, typename Scalar> Autoencoder<InputDataType, Scalar>::Autoencoder(const std::string &jsonString) : Autoencoder() { this->constructFromJsonString(jsonString); } template <typename InputDataType, typename Scalar> void Autoencoder<InputDataType, Scalar>::loadTrainData(const std::vector<InputDataType> data) { trainData = convertData(data); } template <typename InputDataType, typename Scalar> void Autoencoder<InputDataType, Scalar>::train(const std::vector<InputDataType> data, size_t epochs, size_t batchSize) { loadTrainData(data); this->fit(trainData, trainData, batchSize, epochs, 123); } template <typename InputDataType, typename Scalar> std::vector<InputDataType> Autoencoder<InputDataType, Scalar>::predict(const std::vector<InputDataType> data) { auto t1 = std::chrono::high_resolution_clock::now(); auto prediction = dnn::Network<Scalar>::predict(convertData(data)); auto t2 = std::chrono::high_resolution_clock::now(); auto d = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1); std::cout << "Prediction time: " << d.count() << " s" << std::endl; return convertToOutput(prediction); } template <typename InputDataType, typename Scalar> typename Autoencoder<InputDataType, Scalar>::Matrix Autoencoder<InputDataType, Scalar>::convertData(const std::vector<InputDataType> &inputData) { assert(this->num_layers() > 0); /* Convert features to scalar type */ std::vector<Scalar> dataScalar(inputData.begin(), inputData.end()); auto featuresLength = this->layers[0]->inputSize; Matrix data(dataScalar.size() / featuresLength, featuresLength, dataScalar.data()); /* Norm features [0..1] */ if (normValue != 0) { data /= Scalar(normValue); } return data; } template <typename InputDataType, typename Scalar> void Autoencoder<InputDataType, Scalar>::setNormValue(InputDataType _normValue) { normValue = _normValue; } template <typename InputDataType, typename Scalar> std::vector<InputDataType> Autoencoder<InputDataType, Scalar>::convertToOutput(const Matrix &data, bool doDenormalization) { Matrix temp(data); if (doDenormalization && (normValue != 0)) { temp *= normValue; } std::vector<InputDataType> output; for (auto i = 0; i < temp.rows(); ++i) { auto dataPointer = blaze::row(temp, i).data(); std::vector<Scalar> vectorScalar(dataPointer, dataPointer + temp.columns()); output.insert(output.end(), vectorScalar.begin(), vectorScalar.end()); } return output; } template <typename InputDataType, typename Scalar> std::vector<Scalar> Autoencoder<InputDataType, Scalar>::encode(const std::vector<InputDataType> &data) { assert(this->num_layers() % 2 == 0); auto input = convertData(data); const size_t encoderLastLayerNumber = this->num_layers() / 2; this->layers[0]->forward(input); for (size_t i = 1; i < encoderLastLayerNumber; i++) { this->layers[i]->forward(this->layers[i - 1]->output()); } Matrix output = this->layers[encoderLastLayerNumber - 1]->output(); std::vector<Scalar> vectorScalar(output.data(), output.data() + output.columns()); return vectorScalar; } template <typename InputDataType, typename Scalar> std::vector<InputDataType> Autoencoder<InputDataType, Scalar>::decode(const std::vector<Scalar> &data) { assert(this->num_layers() % 2 == 0); Matrix latentVector(1, data.size(), data.data()); const size_t decoderFirstLayerNumber = this->num_layers() / 2; this->layers[decoderFirstLayerNumber]->forward(latentVector); for (size_t i = decoderFirstLayerNumber + 1; i < this->num_layers(); i++) { this->layers[i]->forward(this->layers[i - 1]->output()); } Matrix output = this->layers[this->num_layers() - 1]->output(); return convertToOutput(output); } } // namespace metric
5,051
C++
.h
118
40.550847
120
0.753528
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,626
KOC.hpp
metric-space-ai_metric/metric/mapping/KOC.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_MAPPING_KOC_HPP #define _METRIC_MAPPING_KOC_HPP #include "SOM.hpp" #include <array> #include <vector> #include <algorithm> #include <chrono> #include <cmath> #include <numeric> #include <random> #ifndef M_PI #define M_PI (3.14159265358979323846) #endif namespace metric { namespace KOC_details { /** * @class KOC * *@brief * */ template <typename RecType, class Graph = metric::Grid6, class Metric = metric::Euclidean<typename RecType::value_type>, class Distribution = std::uniform_real_distribution<typename RecType::value_type>> class KOC { typedef typename RecType::value_type T; public: /** * @brief Construct a new KOC object * * @param graph - pre created graph (with metric::Graph interface) over which SOM is being constructed. Default is * metric::Grid6 (hexagones grid). * @param metric - metric or distance being used for SOM training. Default is metric::Euclidean. * @param anomaly_sigma * @param s_learn_rate - start learning rate for SOM training. Shows how error can influence on the sample on the * first iterations of the training. * @param f_learn_rate - finish learning rate for SOM training. Shows how error can influence on the sample on the * last iterations of the training. * @param iterations - maximum number of iterations for SOM training. * @param distribution - distribution used for creating initial weights (positions) of the SOM nodes. */ KOC(const Graph &graph, const Metric &metric, double anomaly_sigma = 1.0, double start_learn_rate = 0.8, double finish_learn_rate = 0.0, size_t iterations = 20, Distribution distribution = Distribution(-1, 1)) : som(graph, metric::Euclidean<double>(), start_learn_rate, finish_learn_rate, iterations, distribution), metric(metric), anomaly_sigma(anomaly_sigma), iterations(iterations), random_seed(std::chrono::system_clock::now().time_since_epoch().count()) { } /** * @brief Construct a new KOC object * * @param graph - pre created graph (with metric::Graph interface) over which SOM is being constructed. Default is * metric::Grid6 (hexagones grid). * @param metric - metric or distance being used for SOM training. Default is metric::Euclidean. * @param anomaly_sigma * @param s_learn_rate - start learning rate for SOM training. Shows how error can influence on the sample on the * first iterations of the training. * @param f_learn_rate - finish learning rate for SOM training. Shows how error can influence on the sample on the * last iterations of the training. * @param iterations - maximum number of iterations for SOM training. * @param distribution - distribution used for creating initial weights (positions) of the SOM nodes. * @param neighborhood_start_size - number of neighbours of the checking node which weights (positions) will be * corrected while SOM training. * @param neighborhood_range_decay - shows how distance from the checking node influence to weights (positions) of * the checking node neighbours while SOM training. * @param random_seed - the value used to initialize the random number generator, which is used for the initial * distribution of weight (positions). */ KOC(const Graph &graph, const Metric &metric, double anomaly_sigma, double start_learn_rate, double finish_learn_rate, size_t iterations, Distribution distribution, double neighborhood_start_size, double neighborhood_range_decay, long long random_seed) : som(graph, metric::Euclidean<double>(), start_learn_rate, finish_learn_rate, iterations, distribution, neighborhood_start_size, neighborhood_range_decay, random_seed), metric(metric), anomaly_sigma(anomaly_sigma), iterations(iterations), random_seed(random_seed) { } /** * @brief Destroy the KOC object * */ ~KOC() = default; /** * @brief * * @param samples * @param num_clusters */ void train(const std::vector<RecType> &samples, int num_clusters, int min_cluster_size = 1); /** * @brief * * @param samples * * @return std::vector<bool> */ std::vector<bool> check_if_anomaly(const std::vector<RecType> &samples); /** * @brief * * @param sample * * @return bool */ bool check_if_anomaly(const RecType &sample); /** * @brief * * @param samples * * @return std::vector<int> */ std::vector<int> assign_to_clusters(const std::vector<RecType> &samples); /** * @brief * * @param samples * @param count * * @return std::tuple<std::vector<size_t>, std::vector<typename RecType::value_type>> - two vectors, the first with * indexes of the top outlier samples (sorted from the farest), and the second with distances to std deviation edge * from nodes coordinates (multiplied to anomaly_sigma, according to formula: distance = <distance to the best * matchin node> - <std_deviation_of_the_node><anomaly_sigma>) */ std::tuple<std::vector<size_t>, std::vector<typename RecType::value_type>> top_outliers(const std::vector<RecType> &samples, int count = 10); private: int min_cluster_size = 1; double anomaly_sigma = 1; size_t iterations; long long random_seed; std::vector<int> clusters; std::vector<int> centroids; std::vector<int> clusters_counts; std::vector<T> nodes_std_deviations; Metric metric; SOM<RecType, Graph, metric::Euclidean<double>, Distribution> som; /** * @brief * * @param samples * @param sampleSize */ void calculate_std_deviations_for_nodes(const std::vector<RecType> &samples, int sampleSize); /** * @brief * * @param num_clusters * * @return std::tuple<std::vector<int>, std::vector<int>, std::vector<int>> */ std::tuple<std::vector<int>, std::vector<int>, std::vector<int>> clusterize_nodes(int num_clusters, int min_cluster_size = 1); /** * @brief * * @param samples * @param sampleSize * @param num_clusters */ void estimate(const std::vector<RecType> &samples, const size_t sampleSize, int num_clusters, int min_cluster_size = 1); }; } // namespace KOC_details // template <typename RecType, class Graph = metric::Grid6, class Metric = metric::Euclidean<typename RecType::value_type>, class Distribution = std::uniform_real_distribution<typename RecType::value_type>> struct KOC_factory { typedef typename RecType::value_type T; typedef typename KOC_details::KOC<RecType, Graph, Metric, Distribution> KOC; /** * @brief * @param nodesNumber - total number of the nodes for graph over which SOM is being constructed. From this number it * should be possible to extract the int square root. * @param start_learn_rate - start learning rate for SOM training. Shows how error can influence on the sample on * the first iterations of the training. * @param finish_learn_rate - finish learning rate for SOM training. Shows how error can influence on the sample on * the last iterations of the training. * @param iterations - maximum number of iterations for SOM training. * @param distribution_min - left side (minimum) of the distribution used for creating initial weights (positions) * of the SOM nodes. * @param distribution_max - right side (maximum) of the distribution used for creating initial weights (positions) * of the SOM nodes. */ KOC_factory(size_t nodesNumber, double anomaly_sigma, double start_learn_rate = 0.8, double finish_learn_rate = 0.0, size_t iterations = 20, T distribution_min = -1, T distribution_max = 1); /** * @brief * * @param nodesWidth - number of the nodes in width for graph over which SOM is being constructed. * @param nodesHeight - number of the nodes in height for graph over which SOM is being constructed. * @param start_learn_rate - start learning rate for SOM training. Shows how error can influence on the sample on * the first iterations of the training. * @param finish_learn_rate - finish learning rate for SOM training. Shows how error can influence on the sample on * the last iterations of the training. * @param iterations - maximum number of iterations for SOM training. * @param distribution_min - left side (minimum) of the distribution used for creating initial weights (positions) * of the SOM nodes. * @param distribution_max - right side (maximum) of the distribution used for creating initial weights (positions) * of the SOM nodes. */ KOC_factory(size_t nodesWidth = 5, size_t nodesHeight = 4, double anomaly_sigma = 1.0, double start_learn_rate = 0.8, double finish_learn_rate = 0.0, size_t iterations = 20, T distribution_min = -1, T distribution_max = 1); /** * @brief * * @param nodesNumber - total number of the nodes for graph over which SOM is being constructed. From this number it * should be possible to extract the int square root. * @param start_learn_rate - start learning rate for SOM training. Shows how error can influence on the sample on * the first iterations of the training. * @param finish_learn_rate - finish learning rate for SOM training. Shows how error can influence on the sample on * the last iterations of the training. * @param iterations - maximum number of iterations for SOM training. * @param distribution_min - left side (minimum) of the distribution used for creating initial weights (positions) * of the SOM nodes. * @param distribution_max - right side (maximum) of the distribution used for creating initial weights (positions) * of the SOM nodes. * @param neighborhood_start_size - number of neighbours of the checking node which weights (positions) will be * corrected while SOM training. * @param neigbour_range_decay - shows how distance from the checking node influence to weights (positions) of the * checking node neighbours while SOM training. * @param random_seed - the value used to initialize the random number generator, which is used for the initial * distribution of weight (positions). */ KOC_factory(size_t nodesNumber, double anomaly_sigma, double start_learn_rate, double finish_learn_rate, size_t iterations, T distribution_min, T distribution_max, double neighborhood_start_size, double neighborhood_range_decay, long long random_seed); /** * @brief * * @param nodesWidth - number of the nodes in width for graph over which SOM is being constructed. * @param nodesHeight - number of the nodes in height for graph over which SOM is being constructed. * @param s_learn_rate - start learning rate for SOM training. Shows how error can influence on the sample on the * first iterations of the training. * @param f_learn_rate - finish learning rate for SOM training. Shows how error can influence on the sample on the * last iterations of the training. * @param iterations - maximum number of iterations for SOM training. * @param distribution_min - left side (minimum) of the distribution used for creating initial weights (positions) * of the SOM nodes. * @param distribution_max - right side (maximum) of the distribution used for creating initial weights (positions) * of the SOM nodes. * @param neighborhood_start_size - number of neighbours of the checking node which weights (positions) will be * corrected while SOM training. * @param neigbour_range_decay - shows how distance from the checking node influence to weights (positions) of the * checking node neighbours while SOM training. * @param random_seed - the value used to initialize the random number generator, which is used for the initial * distribution of weight (positions). */ KOC_factory(size_t nodesWidth, size_t nodesHeight, double anomaly_sigma, double start_learn_rate, double finish_learn_rate, size_t iterations, T distribution_min, T distribution_max, double neighborhood_start_size, double neighborhood_range_decay, long long random_seed); /** * @brief * * @param samples * @param num_clusters - number of expected clusters * * @return KOC_details::KOC<RecType, Graph, Metric, Distribution> */ KOC operator()(const std::vector<RecType> &samples, int num_clusters, int min_cluster_size = 1); private: Metric metric; Graph graph; Distribution distribution; double start_learn_rate; double finish_learn_rate; size_t iterations; double neighborhood_start_size; double neighborhood_range_decay; long long random_seed; double anomaly_sigma; }; } // namespace metric #include "KOC.cpp" #endif
12,580
C++
.h
280
42.071429
120
0.742925
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,627
SOM.hpp
metric-space-ai_metric/metric/mapping/SOM.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_MAPPING_SOM_HPP #define _METRIC_MAPPING_SOM_HPP /* for specific setting check: Appropriate Learning Rate and Neighborhood Function of Self-organizing Map (SOM) ... International Journal of Modeling and Optimization, Vol. 6, No. 1, February 2016 W. Natita, W. Wiboonsak, and S. Dusadee */ #include "metric/distance/k-related/Standards.hpp" #include "metric/utils/graph.hpp" #include <assert.h> #include <array> #include <iostream> #include <vector> #include <algorithm> #include <chrono> #include <cmath> #include <numeric> #include <random> #ifndef M_PI #define M_PI (3.14159265358979323846) #endif namespace metric { /** * @class SOM * *@brief * */ template <typename RecType, class Graph = metric::Grid6, class Metric = metric::Euclidean<typename RecType::value_type>, class Distribution = std::uniform_real_distribution<typename RecType::value_type>> class SOM { using T = typename RecType::value_type; public: /** * @brief Construct a new SOM object * * @param nodesNumber * @param metric */ explicit SOM(size_t nodesNumber, Metric metric = Metric()); /** * @brief Construct a new SOM object * * @param nodesWidth * @param nodesHeight * @param metric */ SOM(size_t nodesWidth, size_t nodesHeight, Metric metric = Metric()); /** * @brief Construct a new SOM object * * @param graph * @param metric * @param s_learn_rate * @param f_learn_rate * @param iterations * @param distribution */ SOM(const Graph &graph, const Metric &metric = Metric(), double start_learn_rate = 0.8, double finish_learn_rate = 0.0, size_t iterations = 20, Distribution distribution = Distribution(-1, 1)); /** * @brief Construct a new SOM object * * @param graph * @param metric * @param s_learn_rate * @param f_learn_rate * @param iterations * @param distribution * @param neighborhood_start_size * @param neighborhood_range_decay * @param random_seed */ SOM(const Graph &graph, const Metric &metric, double start_learn_rate, double finish_learn_rate, size_t iterations, Distribution distribution, double neighborhood_start_size, double neighborhood_range_decay, long long random_seed); /** * @brief Destroy the SOM object * */ ~SOM() = default; /** * @brief * * @param samples */ void train(const std::vector<std::vector<T>> &samples); /** * @brief * * @param samples */ void estimate(const std::vector<std::vector<T>> &samples, const size_t sampleSize); /** * @brief * * @param sample * @return */ std::vector<double> operator()(const RecType &samples) const { return encode(samples); } /** * @brief * * @param sample * @return */ std::vector<double> encode(const RecType &sample) const; /** * @brief Best matching unit * * @param sample * @return size_t */ size_t BMU(const RecType &sample) const; /** * @brief * * @return true * @return false */ bool isValid() const { return valid; } /** * @brief * * @param samples * * @return double */ double std_deviation(const std::vector<std::vector<T>> &samples) const; /** * @brief * * @return */ size_t getNodesNumber() const { return graph.getNodesNumber(); } /** * @brief * * @return */ const std::vector<std::vector<T>> &get_weights() const { return weights; } /** * @brief * * @return */ Graph &get_graph() { return graph; } /** * @brief * * @return */ const Metric &get_metric() const { return metric; } /** * @brief * * @param weights */ void updateWeights(const std::vector<std::vector<T>> &new_weights); bool is_weights_changed() { return weights_changed_; } protected: size_t input_dimensions = 0; // dimensions of inputs vector Metric metric; Graph graph; Distribution distribution = Distribution(-1, 1); double start_learn_rate = 0.8; double finish_learn_rate = 0.0; size_t iterations = 20; double neighborhood_start_size; double neighborhood_range_decay; long long random_seed; bool weights_changed_ = false; /** * @brief * * @param samples */ void subsampled_train(const std::vector<std::vector<T>> &samples, int sampleSize); private: bool valid; std::vector<std::vector<T>> weights; // coordinates in the input_dimensions space }; } // namespace metric #include "SOM.cpp" #endif
4,557
C++
.h
192
21.208333
120
0.696536
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,628
hierarchClustering.hpp
metric-space-ai_metric/metric/mapping/hierarchClustering.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_MAPPING_HIERARCHCLUSTERING_HPP #define _METRIC_MAPPING_HIERARCHCLUSTERING_HPP #include <vector> namespace metric { /** * @class Cluster * * @brief */ template <typename T> class Cluster { public: explicit Cluster() = default; /** * @brief Construct a new Cluster object * * @param d */ explicit Cluster(const std::vector<T> &d) : data(d) { calculateCentroid(); } explicit Cluster(std::vector<T> &&d) : data(d) { calculateCentroid(); } /** * @brief * */ template <typename VecType, typename SampleType = std::vector<VecType>> void calculateCentroid() { centroid = SampleType(data[0].size()); VecType sum; for (size_t i = 0; i < data[0].size(); i++) { sum = 0; for (size_t j = 0; j < data.size(); j++) { sum += data[j][i]; } centroid[i] = sum / data.size(); } } /** * @brief * */ void calculateCentroid() { centroid = data[0]; } /** * @brief * */ std::vector<T> data; /** * @brief * */ T centroid; }; /** * @class HierarchicalClustering * * @brief * */ template <typename T, typename Distance> class HierarchicalClustering { public: /** * @brief Construct a new Hierarchical Clustering object * * @param data * @param k */ HierarchicalClustering(const std::vector<T> &data, const int &k) { sourceData = data; clustersNum = k; } /** * @brief * */ void initialize(); /** * @brief * */ void hierarchical_clustering(); /** * @brief * */ std::vector<Cluster<T>> clusters; /** * @brief * */ std::vector<T> sourceData; /** * @brief * */ int clustersNum; private: std::vector<std::vector<double>> calculateDistances(); }; } // namespace metric #include "hierarchClustering.cpp" #endif
1,989
C++
.h
107
16.009346
97
0.648794
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,629
DSPCC.hpp
metric-space-ai_metric/metric/mapping/DSPCC.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_DSPCC_HPP #define _METRIC_DSPCC_HPP #include <cmath> // for log2 #include <stack> #include <tuple> #include "metric/mapping/PCFA.hpp" #include <blaze/Blaze.h> namespace metric { /** * @class VibrationFeatureExtractor * *@brief wrapper for PCFA that combines it with DWT decomposition and DCT pre- and postprocessing * */ template <typename RecType, typename Metric> class DSPCC { private: template <typename C, int = determine_container_type<C>::code> struct determine_RecTypeInner // type for internal processing, that can not be Blaze vector { using type = void; }; template <typename C> struct determine_RecTypeInner<C, 1> // STL container { using type = C; }; template <typename C> struct determine_RecTypeInner<C, 2> // Blaze vector { using type = std::vector<typename C::ElementType>; // we use STL vector for internal computations }; public: using value_type = typename determine_element_type<RecType>::type; using RecTypeInner = typename determine_RecTypeInner<RecType>::type; /** * @brief * * @param TrainingDataset - training dataset * @param n_features_ - desired number of features of frequency and time PCFAs * @param n_subbands_ - (maximum) number of DWT subbands * @param time_freq_balance_ - share of frequency domain data in mixed PCFA output codes, values from 0 to 1 * @param n_top_features_ - number of featores of top PCFA */ DSPCC(const std::vector<RecType> &TrainingDataset, size_t n_features_ = 1, size_t n_subbands_ = 4, float time_freq_balance_ = 0.5, size_t n_top_features_ = 16); /** * @brief computes features for arbitrary waveforms by subbands - first stage of compressing * * @param Data - waveforms of same length and format as TrainingDataset * @return */ std::vector<std::vector<RecTypeInner>> time_freq_PCFA_encode(const std::vector<RecTypeInner> &Data); /** * @brief restores waveforms fromf features computed by time_freq_PCFA_encode * * @param Codes - compressed codes * @return */ std::vector<RecTypeInner> time_freq_PCFA_decode(const std::vector<std::vector<RecTypeInner>> &Codes); /** * @brief get features for arbitrary waveforms * * @param Data - waveforms of same length and format as TrainingDataset * @return */ std::vector<RecType> encode(const std::vector<RecType> &Data); /** * @brief restores waveforms * * @param Codes - compressed codes * @return */ std::vector<RecType> decode(const std::vector<RecType> &Codes); // public wrapper of the private method, FOR TEST PURPOSE ONLY, to be deleted after testing std::tuple<std::deque<std::vector<RecType>>, std::deque<std::vector<RecType>>> test_public_wrapper_encode(const std::vector<RecType> &Curves) { // TODO remove when tested return outer_encode(Curves); } // public wrapper of the private method, FOR TEST PURPOSE ONLY, to be deleted after testing std::vector<RecType> test_public_wrapper_decode( const std::tuple<std::deque<std::vector<RecType>>, std::deque<std::vector<RecType>>> &TimeFreqMixedData) { // TODO remove when tested return outer_decode(TimeFreqMixedData); } /** * @brief returns size of subband * * @return */ size_t get_subband_size() { return resulting_subband_length; } private: std::vector<metric::PCFA<RecTypeInner, Metric>> freq_PCA_models; std::vector<metric::PCFA<RecTypeInner, Metric>> time_PCA_models; std::vector<metric::PCFA<RecTypeInner, Metric>> top_PCA_model; // TODO solve initialization issue, remove wrapping vector std::stack<size_t> subband_length; size_t n_subbands; size_t resulting_subband_length; float time_freq_balance; size_t n_features; size_t n_features_freq; size_t n_features_time; size_t n_top_subbands; std::default_random_engine rgen; /** * @brief conditionally compiled function for selection between outer and inner RecType: STL case * * @param TrainingDataset - training dataset * @param n_features_ - desired number of features of frequency and time PCFAs * @param n_subbands_ - (maximum) number of DWT subbands * @param time_freq_balance_ - share of frequency domain data in mixed PCFA output codes, values from 0 to 1 * @param n_top_features_ - number of featores of top PCFA * @return */ template <typename R> typename std::enable_if< // DSPCC<RecType, Metric>:: template determine_container_type<R>::code == 1, determine_container_type<R>::code == 1, void>::type select_train(const std::vector<RecType> &TrainingDataset, size_t n_features_, size_t n_subbands_, float time_freq_balance_, size_t n_top_features_); /** * @brief conditionally compiled function for selection between outer and inner RecType: Blaze case * * @param TrainingDataset - training dataset * @param n_features_ - desired number of features of frequency and time PCFAs * @param n_subbands_ - (maximum) number of DWT subbands * @param time_freq_balance_ - share of frequency domain data in mixed PCFA output codes, values from 0 to 1 * @param n_top_features_ - number of featores of top PCFA * @return */ template <typename R> typename std::enable_if<determine_container_type<R>::code == 2, void>::type select_train(const std::vector<RecType> &TrainingDataset, size_t n_features_, size_t n_subbands_, float time_freq_balance_, size_t n_top_features_); /** * @brief training procedure called in constructor * * @param TrainingDataset - training dataset * @param n_features_ - desired number of features of frequency and time PCFAs * @param n_subbands_ - (maximum) number of DWT subbands * @param time_freq_balance_ - share of frequency domain data in mixed PCFA output codes, values from 0 to 1 * @param n_top_features_ - number of featores of top PCFA * @return */ void train(const std::vector<DSPCC<RecType, Metric>::RecTypeInner> &TrainingDataset, size_t n_features_, size_t n_subbands_, float time_freq_balance_, size_t n_top_features_); /** * @brief conditionally compiled function for selection betwween outer and inner RecType: STL case * * @param Data - waveforms of same length and format as TrainingDataset * @return */ template <typename R> typename std::enable_if<determine_container_type<R>::code == 1, // STL case std::vector<RecType>>::type select_encode(const std::vector<RecType> &Data); /** * @brief conditionally compiled function for selection betwween outer and inner RecType: Blaze case * * @param Data - waveforms of same length and format as TrainingDataset * @return */ template <typename R> typename std::enable_if< // DSPCC<RecType, Metric>:: template determine_container_type<R>::code == 2, // Blaze case determine_container_type<R>::code == 2, // Blaze case std::vector<RecType>>::type select_encode(const std::vector<RecType> &Data); /** * @brief conditionally compiled function for selection betwween outer and inner RecType: STL case * * @param Codes - compressed codes * @return */ template <typename R> typename std::enable_if<determine_container_type<R>::code == 1, // STL case std::vector<RecType>>::type select_decode(const std::vector<RecType> &Codes); /** * @brief conditionally compiled function for selection betwween outer and inner RecType: Blaze case * * @param Codes - compressed codes * @return */ template <typename R> typename std::enable_if<determine_container_type<R>::code == 2, // Blaze case std::vector<RecType>>::type select_decode(const std::vector<RecType> &Codes); /** * @brief get features for arbitrary waveforms by subbands * * @param Data - waveforms of same length and format as TrainingDataset * @return */ std::vector<std::vector<RecTypeInner>> time_freq_PCFA_encode( const std::tuple<std::deque<std::vector<RecTypeInner>>, std::deque<std::vector<RecTypeInner>>> &PreEncoded); /** * @brief * * @param * @return */ std::vector<RecTypeInner> mixed_code_serialize(const std::vector<std::vector<RecTypeInner>> &PCFA_encoded); /** * @brief * * @param * @return */ std::vector<std::vector<RecTypeInner>> mixed_code_deserialize(const std::vector<RecTypeInner> &Codes); /** * @brief apply DWT, DCT to input data, mix wave and spectrum, rearrande by subbands * * @param Curves - waveforms of same length and format as TrainingDataset * @return - RecType containers grouped in vectors by subband, which are collected in the single vector */ std::tuple<std::deque<std::vector<RecTypeInner>>, std::deque<std::vector<RecTypeInner>>> outer_encode(const std::vector<RecTypeInner> &Curves); /** * @brief separates mix wave and spectrum, apply iDCT, reconstructs the subband waveform, applies iDWT, rearrande by * records * * @param TimeFreqMixedData - vector of vectors (per subband) of RecType wave-time combined vectors for each * compressed record * @return - vector of decoded RecType curves */ std::vector<RecTypeInner> outer_decode(const std::tuple<std::deque<std::vector<RecTypeInner>>, std::deque<std::vector<RecTypeInner>>> &TimeFreqMixedData); /** * @brief rearranges data from per-subband to per-record order * * @param Codes_by_subbands - vector of vectors (per subband) of compressed codes for each record * @return - the same compressed codes grouped in vectors by records, all collected in the single vector */ std::vector<std::vector<RecType>> rearrange_to_records(const std::vector<RecType> &Codes_by_subbands); /** * @brief rearranges data from record-major to per-subband order * * @param Codes_by_records - compressed codes grouped in vectors by records, all collected in the single vector * @return - vector of vectors (per subband) of compressed codes for each record */ std::vector<std::vector<RecType>> rearrange_to_subbands(const std::vector<RecType> &Codes_by_records); /** * @brief determines break index using given length of the subband waveform * * @param length - length of the filtered suband waveform * @param crop_share - desired portion to be cropped * @return - index to be used to break the subband waveform apart for separate time domain and frequency domain * compression */ size_t crop_index(size_t length, float crop_share); /** * @brief * * @param * @return */ size_t subband_size(size_t original_size, size_t depth, size_t wavelet_length = 10); // hardcoded DB5 wavelet!! TODO update /** * @brief * * @param * @return */ size_t original_size(size_t subband_size, size_t depth, size_t wavelet_length = 10); }; } // namespace metric #include "DSPCC.cpp" #endif // _METRIC_DSPCC_HPP
10,846
C++
.h
277
36.357401
117
0.730864
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,630
kmeans.hpp
metric-space-ai_metric/metric/mapping/kmeans.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 M.Welsch */ #ifndef _METRIC_MAPPING_KMEANS_HPP #define _METRIC_MAPPING_KMEANS_HPP /* A k-means implementation with optimized seeding. Input (vector of fixed-size vector, clustersize) for example: std::vector<std::vector<float, 5>> data{ {0, 0, 0, 0, 0}, {1.74120000000000, 4.07812000000000, -0.0927036000000, 41.7888000000000, 41.7888000000000}, {7.75309000000000, 16.2466000000000, 3.03956000000000, 186.074000000000, 186.074000000000}, {2.85493000000000, 3.25380000000000, 2.50559000000000, 68.5184000000000, 68.5184000000000}, {5.81414000000000, 8.14015000000000, 3.22950000000000, 139.539000000000, 139.539000000000}, {2.57927000000000, 2.63399000000000, 2.46802000000000, 61.9026000000000, 61.9026000000000}}; auto [means, idx] = kmeans(data, 4); // clusters the data in 4 groups. means: A vector holding the means (same type as input data) idx: A vector containing the cluster index */ #include <string> #include <tuple> #include <vector> namespace metric { /** * @brief * * @param data * @param k * @param maxiter * @param distance_measure * @return */ template <typename T> auto kmeans(const std::vector<std::vector<T>> &data, int k, int maxiter = 200, std::string distance_measure = "Euclidean", long long random_seed = -1) -> std::tuple<std::vector<int>, std::vector<std::vector<T>>, std::vector<int>>; } // namespace metric #include "kmeans.cpp" #endif
1,623
C++
.h
43
35.72093
95
0.742694
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,631
deterministic_switch_detector.hpp
metric-space-ai_metric/metric/mapping/deterministic_switch_detector.hpp
#ifndef _METRIC_DETERMINISTIC_SWITCH_DETECTOR_HPP #define _METRIC_DETERMINISTIC_SWITCH_DETECTOR_HPP #include <blaze/Blaze.h> /** * @class SwitchDetector - non-ML deterministic detector of On-Off and Off-On switches in 3-dimensional timeseries. */ template <typename value_type> class DetSwitchDetector { public: /** * @brief DetSwitchDetector - create instance using weight matrices * @param W1_ - hidden layer weights, 4x5 matrix: * rows correspond to R, G, B stddev feature filtered signal, respective, * columns correspond to R, G, B, stddev feature and bias neuron inputs * @param Wo_ - single output neuron mixing weights, r, g, b, stddev weights, respective, in single row * @param wnd_size_ - stddev computation sliding window size * @param update_rate_ - parameter to control off latency: weight of new signal in the cumulative output: * 1 - no latency, 0 - no update (eternal delay) */ DetSwitchDetector(const blaze::DynamicMatrix<value_type> &W1_, const blaze::DynamicMatrix<value_type> &Wo_, const size_t wnd_size_ = 15, const value_type update_rate_ = 0.0025); /** * @brief DetSwitchDetector - create instance using parameters * @param wnd_size_ - stddev computation sliding window size * @param update_rate_ - parameter to control off latency: weight of new signal in the cumulative output: * 1 - no latency, 0 - no update (eternal delay) * @param w_r - weight for R: incerasing will cause cufoff at lover input signal * @param w_g - same for G * @param w_b - same for B * @param w_s - same for stddev additional feature * @param magn - overall scale factor * @param rgb_offs - single offset value for R, G and B * @param s_offs - offset value for stddev feature: insreasing will lift the zero point up * @param rgb_wo - output neuron mixing weights, same value for R, G and B * @param s_wo - same for stddev feature */ DetSwitchDetector(const size_t wnd_size_ = 15, const value_type update_rate_ = 0.0025, const value_type w_r = 2, const value_type w_g = 2, const value_type w_b = 2, const value_type w_s = 15, const value_type magn = 0.002, const value_type rgb_offs = -5, const value_type s_offs = 0, const value_type rgb_wo = 1, const value_type s_wo = 2); /** * @brief encode - apply to GRB timeseries * @param dataset - input timeseries with points in rows, row should contain at least 4 values: * first is ignored (it may be timestamp), others are R, G, B input * @return - single column matrix of same number of rows as input, containing found switches: * -1 - off, 1 - on, 0 - no switch */ blaze::DynamicMatrix<value_type> encode(const blaze::DynamicMatrix<value_type> &dataset); private: blaze::DynamicMatrix<value_type> W1; // hodden layer weights (4 rows) blaze::DynamicMatrix<value_type> Wo; // output neuron weights (single row) size_t wnd_size; // stddev sliding window size value_type update_rate; // update rate between between 0 and 1, the greater, the lower is latency value_type lat = 0; // we keep latency value between calls }; #include "deterministic_switch_detector.cpp" #endif // _METRIC_DETERMINISTIC_SWITCH_DETECTOR_HPP
3,216
C++
.h
56
54.446429
115
0.716053
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,632
dbscan.hpp
metric-space-ai_metric/metric/mapping/dbscan.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 M.Welsch */ #ifndef _METRIC_MAPPING_DBSCAN_HPP #define _METRIC_MAPPING_DBSCAN_HPP /* A DBSCAN implementation based on distance matrix. */ // References: // // Martin Ester, Hans-peter Kriegel, Jörg S, and Xiaowei Xu // A density-based algorithm for discovering clusters // in large spatial databases with noise. 1996. #include "../space/matrix.hpp" #include <string> #include <vector> namespace metric { /** * @brief Density-based spatial clustering of applications with noise (DBSCAN) * * @param dm distance matrix * @param eps the maximum distance between neighbor objects * @param minpts minimum number of neighboring objects needed to form a cluster * @return three vectors in a tuple, first vector of size N ( size of initial dataset) contains number of the cluster * of corresponding source point, second contains indices of center points of clusters, third vector contains * size of corresponding cluster. */ template <typename RecType, typename Metric, typename T> std::tuple<std::vector<int>, std::vector<int>, std::vector<int>> dbscan(const metric::Matrix<RecType, Metric> &dm, T eps, std::size_t minpts); } // namespace metric #include "dbscan.cpp" #endif
1,456
C++
.h
36
38.472222
118
0.739377
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,633
ESN.hpp
metric-space-ai_metric/metric/mapping/ESN.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_MAPPING_ESN_HPP #define _METRIC_MAPPING_ESN_HPP #include <blaze/Blaze.h> #include "PCFA.hpp" // this is for metafunctions determine_container_type and determine_container_type // TODO move them to some single common unit and include it instead of PCFA.hpp !! #include <tuple> namespace metric { /** * @class ESN * * @brief */ template <typename RecType, typename Metric> class ESN { public: using value_type = typename determine_element_type<RecType>::type; /** * @brief Construct a new ESN object * * @param w_size * @param w_connections * @param w_sr * @param alpha_ * @param washout_ * @param beta_ */ ESN(const size_t w_size = 500, // number of elements in reservoir const value_type w_connections = 10, // number of interconnections (for each reservoir element) const value_type w_sr = 0.6, // desired spectral radius of the reservoir const value_type alpha_ = 0.5, // leak rate const size_t washout_ = 1, // number of samples excluded from output for washout const value_type beta_ = 0.5 // ridge solver metaparameter ); /** * @brief load trained model from file * @param filename */ ESN(const std::string &filename); /** * @brief ESN - create model using trained components given as matrices * @param W_in_ * @param W_ * @param W_out_ * @param alpha_ * @param washout * @param beta */ ESN(const blaze::DynamicMatrix<value_type> &W_in_, const blaze::CompressedMatrix<value_type> &W_, const blaze::DynamicMatrix<value_type> &W_out_, const value_type alpha_ = 0.5, // leak rate const size_t washout_ = 1, // number of samples excluded from output for washout const value_type beta_ = 0.5 // ridge solver metaparameter ); /** * @brief * * @param Samples * @param Target */ void train(const blaze::DynamicMatrix<value_type> &Samples, const blaze::DynamicMatrix<value_type> &Target); /** * @brief * * @param Samples * @param Target */ void train(const std::vector<RecType> &Samples, const std::vector<RecType> &Target); /** * @brief * * @param Samples * @return */ blaze::DynamicMatrix<value_type> predict(const blaze::DynamicMatrix<value_type> &Samples); /** * @brief * * @param Samples * @return */ std::vector<RecType> predict(const std::vector<RecType> &Samples); /** * @brief * * @param filename */ void save(const std::string &filename); /** * @brief get_components - export all components of NN needed * @return */ std::tuple<blaze::DynamicMatrix<value_type>, blaze::CompressedMatrix<value_type>, blaze::DynamicMatrix<value_type>, value_type, size_t, value_type> get_components(); private: blaze::DynamicMatrix<value_type> W_in; blaze::CompressedMatrix<value_type> W; blaze::DynamicMatrix<value_type> W_out = blaze::DynamicMatrix<value_type>(0, 0); bool trained = false; value_type alpha = 0.5; value_type beta = 0.5; size_t washout = 1; std::default_random_engine rgen; void create_W(const size_t w_size, const value_type w_connections, const value_type w_sr); blaze::DynamicMatrix<value_type> vector_to_blaze(const std::vector<RecType> &In); template <typename R> typename std::enable_if<determine_container_type<R>::code == 1, std::vector<R>>::type blaze2RecType(const blaze::DynamicMatrix<typename ESN<R, Metric>::value_type> &In); template <typename R> typename std::enable_if<determine_container_type<R>::code == 2, std::vector<R>>::type blaze2RecType(const blaze::DynamicMatrix<typename ESN<R, Metric>::value_type> &In); }; // class ESN } // namespace metric #include "ESN.cpp" #endif // _METRIC_MAPPING_DETAILS_ESN_HPP
3,887
C++
.h
120
29.808333
116
0.709436
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,634
ensembles.hpp
metric-space-ai_metric/metric/mapping/ensembles.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_MAPPING_ENSEMBLES_HPP #define _METRIC_MAPPING_ENSEMBLES_HPP #include "ensembles/DT.hpp" #include <functional> #include <limits> #include <memory> #include <tuple> #include <variant> #include <vector> namespace metric { /** * @class Subsample * @brief base subsampler functor class template * */ template <class Record> class Subsample { public: /** * @class El * @brief * */ struct El { int original_idx; double value; }; /** * @brief * * @param data * @param features * @param response * @param weights * @param portion * @param portion_major * @param out_data * @param replacement */ template <typename ConType> void operator()(const ConType &data, const std::vector<std::function<double(Record)>> &features, const std::function<bool(Record)> &response, const std::vector<double> &weights, double portion, double portion_major, // not in use in base class ConType &out_data, bool replacement = false) const; /** * @brief * * @param roulette * @param replacement * @return */ int montecarlo(std::vector<El> &roulette, bool replacement = false) const; /** * @brief * * @param roulette */ void print_roulette(std::vector<El> &roulette) const; }; /** * @class SubsampleRUS * @brief * */ template <class Record> class SubsampleRUS : public Subsample<Record> { public: /** * @brief * * @param data * @param features * @param response * @param weights * @param portion * @param portion_major * @param out_data * @param replacement */ template <typename ConType> void operator()(const ConType &data, const std::vector<std::function<double(Record)>> &features, const std::function<bool(Record)> &response, const std::vector<double> &weights, double portion, double portion_major, ConType &out_data, bool replacement = false); }; /** * @class TestCl * @brief * */ template <class Record> class TestCl { public: /** * @brief Construct a new Test Cl object * * @param var_idx_ * @param invert_ */ TestCl(int var_idx_, bool invert_); /** * @brief train model on test data set * * @param data test dataset * @param features * @param response */ template <typename ConType> void train(ConType &data, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response); /** * @brief * * @param data * @param features * @param predictions */ template <typename ConType> void predict(ConType data, std::vector<std::function<double(Record)>> features, std::vector<bool> &predictions); /** * @brief clone object * * @return shared pointer to clone of the current object */ std::shared_ptr<TestCl<Record>> clone(); private: int var_idx; // specific input param bool invert; // specific input param double middle; // specific for classifier learned model data }; /** * @class Boosting * @brief * * @tparam Record data record type * @tparam WeakLearner single weak learner class * @tparam Subsampler subsampler functor */ template <class Record, class WeakLearner, typename Subsampler> class Boosting { public: /** * @brief Construct a new Boosting object * * @param ensemble_size_ * @param share_overall_ * @param share_minor_ * @param weak_classifier_ */ Boosting(int ensemble_size_, double share_overall_, double share_minor_, WeakLearner weak_classifier_); /** * @brief train model on test dataset * * @param data * @param features * @param response * @param replacement */ template <typename ConType> void train(ConType &data, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response, bool replacement = false); /** * @brief * * @param data * @param features * @param predictions */ template <typename ConType> void predict(ConType &data, std::vector<std::function<double(Record)>> &features, std::vector<bool> &predictions); /** * @brief create clone of the current object * * @return shared pointer to clone of the current object */ std::shared_ptr<Boosting<Record, WeakLearner, Subsampler>> clone(); private: size_t ensemble_size; double share_overall; double share_minor; WeakLearner weak_classifier; int n_samples; int n_features; std::vector<double> ens_alpha; std::vector<std::shared_ptr<WeakLearner>> ens_classifiers; std::vector<std::shared_ptr<WeakLearner>> cloned_classifiers; // when building the tree of classifiers // TODO think on the better solution }; /** * @class Bagging_tuple * @brief * * @tparam Record data record type * @tparam WeakLearnerTuple tuple of weak learners * @tparam Subsampler subsampler function */ template <class Record, class WeakLearnerTuple, typename Subsampler> // tuple of weak learners class Bagging_tuple { public: /** * @brief Construct a new Bagging_tuple object * * @param ensemble_size_ * @param share_overall_ * @param share_minor_ * @param type_weinght_ * @param weak_classifiers_ */ Bagging_tuple(int ensemble_size_, double share_overall_, double share_minor_, std::vector<double> type_weinght_, WeakLearnerTuple weak_classifiers_); /** * @brief train model on test dataset * * @param data * @param features * @param response * @param replacement */ template <typename ConType> void train(ConType &data, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response, bool replacement = false); /** * @brief * * @tparam ConType * @param data * @param features * @param predictions */ template <typename ConType> void predict(ConType &data, std::vector<std::function<double(Record)>> &features, std::vector<bool> &predictions); // TODO implement clone() if needed to use as weak private: template <typename T1, typename T2, typename ConType> void myfunc( // TODO refactor if needed T1 &t1, T2 &t2, int sw, ConType &data, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response, std::vector<double> &prediction); // out // // tuple runtime processing members template <size_t I, typename T> using el = typename std::tuple_element<I, T>::type; template <std::size_t I = 0, typename ConType, typename... Tp, typename... Tp2> inline typename std::enable_if<I == sizeof...(Tp), void>::type for_index(int, std::tuple<Tp...> &, std::tuple<Tp2...> &, int, ConType &, std::vector<std::function<double(Record)>> &, std::function<bool(Record)> &, std::vector<double> &); template <std::size_t I = 0, typename ConType, typename... Tp, typename... Tp2> inline typename std::enable_if < I<sizeof...(Tp), void>::type for_index(int index, std::tuple<Tp...> &t1, std::tuple<Tp2...> &t2, int sw, // 1 - train, 2 - predict ConType &data, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response, std::vector<double> &prediction); // out, only used in prediction mode (sw == 2) // define for_index function of type void only if I < els in pack // // creating tuple of weak learner vectors template <size_t I, typename T> struct tuple_n { template <typename... Args> using type = typename tuple_n<I - 1, T>::template type<std::vector<std::shared_ptr<el<I - 1, T>>>, Args...>; }; // TODO replace with shared_ptr<el<I-1, T> template <typename T> struct tuple_n<0, T> { template <typename... Args> using type = std::tuple<Args...>; }; template <typename T> using tuple_of = typename tuple_n<std::tuple_size<T>::value, T>::template type<>; int ensemble_size; double share_overall; double share_minor; std::vector<double> type_weight; WeakLearnerTuple weak_classifiers; tuple_of<WeakLearnerTuple> ensemble; // recursively defined type int n_samples; int n_features; int const weak_type_num = std::tuple_size<WeakLearnerTuple>::value; }; /** * @class Bagging * @brief * * @tparam Record * @tparam WeakLearnerVariant * @tparam Subsampler */ template <class Record, class WeakLearnerVariant, typename Subsampler> // tuple of weak learners class Bagging { public: /** * @brief Construct a new Bagging object * * @param ensemble_size_ * @param share_overall_ * @param share_minor_ * @param type_weinght_ * @param weak_classifiers_ */ Bagging(int ensemble_size_, double share_overall_, double share_minor_, std::vector<double> type_weinght_, std::vector<WeakLearnerVariant> weak_classifiers_); /** * @brief train model on test dataset * * @param data * @param features * @param response * @param replacement */ template <typename ConType> void train(ConType &data, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response, bool replacement = false); /** * @brief * * @param data * @param features * @param predictions */ template <typename ConType> void predict(ConType &data, std::vector<std::function<double(Record)>> &features, std::vector<bool> &predictions); /** * @brief create clone of current object * * @return shared_pointer to clone of the current object */ std::shared_ptr<Bagging<Record, WeakLearnerVariant, Subsampler>> clone(); private: int ensemble_size; double share_overall; double share_minor; std::vector<double> type_weight; std::vector<WeakLearnerVariant> weak_classifiers; std::vector<WeakLearnerVariant> ensemble; int n_samples; int n_features; }; } // namespace metric #include "ensembles/ensembles.cpp" #endif
9,740
C++
.h
334
26.494012
115
0.707955
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,635
PCFA.hpp
metric-space-ai_metric/metric/mapping/PCFA.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_MAPPING_PCFA_HPP #define _METRIC_MAPPING_PCFA_HPP #include <blaze/Blaze.h> #include <type_traits> namespace metric { /** * @brief * * @param In * @param n_components * @param averages - outputs average curve * @return */ template <class BlazeMatrix> blaze::DynamicMatrix<typename BlazeMatrix::ElementType> PCA_col(const BlazeMatrix &In, int n_components, blaze::DynamicVector<typename BlazeMatrix::ElementType> &averages); /** * @brief * * @param In * @param n_components * @param averages - outputs average curve * @return */ template <class BlazeMatrix> blaze::DynamicMatrix<typename BlazeMatrix::ElementType> PCA(const BlazeMatrix &In, int n_components, blaze::DynamicVector<typename BlazeMatrix::ElementType, blaze::rowVector> &averages); /** * @class PCFA_col * *@brief simple linear encoder based on PCA for data organized by columns * */ template <typename V> class PCFA_col { public: using value_type = V; /** * @brief Construct a new PCFA_col object * * @param TrainingData - training dataset with curves in columns * @param n_features - desired length of compressed code */ PCFA_col(const blaze::DynamicMatrix<value_type> &TrainingData, size_t n_features = 1); /** * @brief * * @param Data * @return */ blaze::DynamicMatrix<value_type> encode(const blaze::DynamicMatrix<value_type> &Data); /** * @brief * * @param Codes * @param unshift - flag for adding average curve to each decoded one * @return blaze::DynamicMatrix<value_type> */ blaze::DynamicMatrix<value_type> decode(const blaze::DynamicMatrix<value_type> &Codes, bool unshift = true); /** * @brief returns the average curve of training dataset, used for center shift * * @return blaze::DynamicMatrix<value_type> */ blaze::DynamicMatrix<value_type> average(); /** * @brief returns the encoder matrix concatenated with the average curve of training dataset, used for center shift * * @return blaze::DynamicMatrix<value_type> */ blaze::DynamicMatrix<value_type> eigenmodes(); private: blaze::DynamicMatrix<value_type> W_decode; blaze::DynamicMatrix<value_type> W_encode; blaze::DynamicVector<value_type> averages; std::default_random_engine rgen; }; // common metafunctions for PCFA and DSPCC template <typename> struct determine_container_type // checks whether container is STL container (1) or Blaze vector (2) { constexpr static int code = 0; }; template <template <typename, typename> class Container, typename ValueType, typename Allocator> struct determine_container_type<Container<ValueType, Allocator>> { constexpr static int code = 1; }; template <template <typename, bool> class Container, typename ValueType, bool F> struct determine_container_type<Container<ValueType, F>> { constexpr static int code = 2; }; // template<typename> // struct determine_element_type // old version, replaced with value_type/ElementType field detector! // { // using type = void; // }; // template <typename ValueType, typename Allocator> // struct determine_element_type<std::vector<ValueType, Allocator>> // { // using type = typename std::vector<ValueType, Allocator>::value_type; // }; // template <typename ValueType, bool F> // struct determine_element_type<blaze::DynamicVector<ValueType, F>> // { // using type = typename blaze::DynamicVector<ValueType, F>::ElementType; // }; // template <typename ValueType, bool F> // struct determine_element_type<blaze::DynamicMatrix<ValueType, F>> // { // using type = typename blaze::DynamicMatrix<ValueType, F>::ElementType; // }; template <typename C, int = determine_container_type<C>::code> struct determine_element_type // determines type of element both for STL containers and Blaze vectors { using type = void; }; template <typename C> struct determine_element_type<C, 1> { using type = typename C::value_type; }; template <typename C> struct determine_element_type<C, 2> { using type = typename C::ElementType; }; /** * @class PCFA * *@brief simple linear encoder based on PCA * */ template <typename RecType, typename Metric> class PCFA { // private: public: using value_type = typename determine_element_type<RecType>::type; // constexpr static int ContainerCode = determine_container_type<RecType>::code; /** * @brief Construct a new PCFA object from dataset in blaze DynamicMatrix * * @param TrainingData - training dataset with curves in rows * @param n_features - desired length of compressed code */ PCFA(const blaze::DynamicMatrix<value_type> &TrainingData, size_t n_features = 1); /** * @brief Construct a new PCFA object vrom vector of records * * @param TrainingData - training dataset, vector of records * @param n_features - desired length of compressed code */ PCFA(const std::vector<RecType> &TrainingData, const size_t n_features = 1); /** * @brief Construct PCFA from trained decode weight matrix and vector of averages * @param Weights * @param averages */ PCFA(const blaze::DynamicMatrix<value_type> &Weights, const blaze::DynamicVector<value_type, blaze::rowVector> &avgs); /** * @brief Construct PCFA from trained decode weight matrix and vector of averages given as vector containers * @param Weights * @param avgs */ PCFA(const std::vector<RecType> &Weights, const RecType &avgs); /** * @brief * * @param Data * @return */ blaze::DynamicMatrix<value_type> encode(const blaze::DynamicMatrix<value_type> &Data); /** * @brief * * @param Data * @return */ std::vector<RecType> encode(const std::vector<RecType> &Data); /** * @brief * * @param Codes * @param unshift - flag for adding average curve to each decoded one * @return blaze::DynamicMatrix<value_type> */ blaze::DynamicMatrix<value_type> decode(const blaze::DynamicMatrix<value_type> &Codes, bool unshift = true); /** * @brief * * @param Data * @return */ std::vector<RecType> decode(const std::vector<RecType> &Data, bool unshift = true); /** * @brief returns the average curve of training dataset, used for center shift * * @return blaze::DynamicMatrix<value_type> */ blaze::DynamicMatrix<value_type> average_mat(); /** * @brief returns the average curve of training dataset, used for center shift * * @return RecType */ // std::vector<RecType> average(); RecType average(); /** * @brief returns weights * * @return matrix of decode weights */ std::vector<RecType> weights(); /** * @brief returns the encoder matrix concatenated with the average curve of training dataset, used for center shift * * @return blaze::DynamicMatrix<value_type> */ blaze::DynamicMatrix<value_type> eigenmodes_mat(); /** * @brief returns the encoder matrix concatenated with the average curve of training dataset, used for center shift * * @return blaze::DynamicMatrix<value_type> */ std::vector<RecType> eigenmodes(); private: blaze::DynamicMatrix<value_type> W_decode; blaze::DynamicMatrix<value_type> W_encode; blaze::DynamicVector<value_type, blaze::rowVector> averages; std::default_random_engine rgen; blaze::DynamicMatrix<value_type> vector_to_blaze(const std::vector<RecType> &In); template <typename R> typename std::enable_if<determine_container_type<R>::code == 1, std::vector<R>>::type blaze2RecType(const blaze::DynamicMatrix<typename PCFA<R, Metric>::value_type> &In); template <typename R> typename std::enable_if<determine_container_type<R>::code == 2, std::vector<R>>::type blaze2RecType(const blaze::DynamicMatrix<typename PCFA<R, Metric>::value_type> &In); }; /** * @brief Creates a new PCFA_col object from dataset of Blaze Matrix type * * @param TrainingData * @param n_features */ template <typename BlazeMatrix> PCFA_col<typename BlazeMatrix::ElementType> PCFA_col_factory(const BlazeMatrix &TrainingData, size_t n_features = 1); /** * @brief Creates a new PCFA object from dataset of Blaze Matrix type * * @param TrainingData * @param n_features */ template <typename BlazeMatrix> PCFA<typename BlazeMatrix::ElementType, void> PCFA_factory(const BlazeMatrix &TrainingData, size_t n_features = 1); /** * @brief Creates a new PCFA object from vector of STL containers as dataset * * @param TrainingData * @param n_features */ template <template <typename, typename> class Container, typename ValueType, typename Allocator> PCFA<Container<ValueType, Allocator>, void> PCFA_factory(std::vector<Container<ValueType, Allocator>> &TrainingData, size_t n_features = 1); /** * @brief Creates a new PCFA object from vector of Blaze vectors as dataset * * @param TrainingData * @param n_features */ template <template <typename, bool> class Container, typename ValueType, bool F> PCFA<Container<ValueType, F>, void> PCFA_factory(std::vector<Container<ValueType, F>> &TrainingData, size_t n_features = 1); } // namespace metric #include "PCFA.cpp" #endif // _METRIC_MAPPING_PCFA_HPP
9,254
C++
.h
276
31.206522
117
0.73423
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,636
Redif.hpp
metric-space-ai_metric/metric/mapping/Redif.hpp
#ifndef _METRIC_MAPPING_REDIF_HPP #define _METRIC_MAPPING_REDIF_HPP #include "metric/distance/k-related/Standards.hpp" #include <blaze/Blaze.h> #include <vector> namespace metric { /** * @class Kohonen * * @brief * * Encode dataset based on an inverse Diffusion Process. * By training a model based on an inverse (or backward) Diffusion * Process, noisy records can be denoised (encoded) and decoded. * * The noisy training data is denoised by solving a backward diffusion * process with the help of graph Laplacian of a random neighborhood graph * generated by the samples. * */ template <typename Tv = double, class Metric = metric::Euclidean<Tv>> class Redif { public: /** * @brief * Constructor for Redif * * @param trainData - Data Set for training the Denoising Model * @param nNeighbors - The number of nearest neighbors considered in building the distance matrix. (Default: 10) * @param nIter - Number of iterations (default: 15) * @param metric - Metric used for train and evaluation. */ Redif(const std::vector<std::vector<Tv>> &trainData, size_t nNeighbors = 10, size_t nIter = 15, Metric metric = Metric()); ~Redif() = default; /** * @brief * Encodes (denoises) record based on pretrained Diffusion model. * Based on a training set and the Laplacian Matrices of all iterations of * the backward Diffusion process records `x` dataset are being denoised. * * @param x - 'Noisy' Records. * * @return denoised (encoded) records. */ std::vector<std::vector<Tv>> encode(const std::vector<std::vector<Tv>> &x); /** * @brief * Decodes denoised (encoded) data based on the pretrained denoising Diffusion model. * Based on a denoised training set and the Laplacian Matrices of all * iterations of the reverse diffusion process, the process is reveresed back. * * @param xEncoded - Denoised (encoded) records. * * @return Decoded records. */ std::vector<std::vector<Tv>> decode(const std::vector<std::vector<Tv>> &xEncoded); /** * @brief * Accesses property xTrainEncoded. */ std::vector<std::vector<Tv>> get_train_encoded() { std::vector<std::vector<Tv>> encoded_data_as_vectors(xTrainEncoded.rows(), std::vector<Tv>(xTrainEncoded.columns())); for (size_t i = 0; i < xTrainEncoded.rows(); i++) for (size_t j = 0; j < xTrainEncoded.columns(); j++) encoded_data_as_vectors[i][j] = xTrainEncoded(i, j); return encoded_data_as_vectors; } private: /** * @brief * * @param idx * @param data * @param start * @param end */ template <bool flag = blaze::rowVector> void Quicksort(blaze::DynamicVector<size_t> &idx, blaze::DynamicVector<Tv, flag> &data, size_t start, size_t end); template <bool flag = blaze::rowVector> size_t partition(blaze::DynamicVector<size_t> &idx, blaze::DynamicVector<Tv, flag> &data, size_t start, size_t end); /** * @brief * Encodes (denoises) record based on pretrained Diffusion model. * Based on a training set and the Laplacian Matrices of all iterations of * the backward Diffusion process records `x` dataset are being denoised. * * @param x - 'Noisy' Records * * @return denoised (encoded) records. */ blaze::DynamicMatrix<Tv> encode(const blaze::DynamicMatrix<Tv> &x); /** * @brief * Decodes denoised (encoded) data based on the pretrained denoising Diffusion model. * Based on a denoised training set and the Laplacian Matrices of all * iterations of the reverse diffusion process, the process is reveresed back. * * @param xEncoded - Denoised (encoded) records. * * @return Decoded records. */ blaze::DynamicMatrix<Tv> decode(const blaze::DynamicMatrix<Tv> &xEncoded); /** * @brief * Calculates local Distance Matrix based on `nNeighbors` and `metric`. * * @param dataSample - Data samples. * * @return Local Distance Matrix */ blaze::DynamicMatrix<Tv> getLocalDistMatrix(const blaze::DynamicMatrix<Tv> &dataSample); /** * @brief * Calculates the weighted Graph Laplacian. * * @param localDist - Local Distance Matrix * * @return Weighted Graph Laplacian. */ blaze::DynamicMatrix<Tv> calcWeightedGraphLaplacian(const blaze::DynamicMatrix<Tv> &localDist); /** * @brief * Builds the Diffusion Model based on train dataset. * * The noisy training data is denoised by solving a backward diffusion * process with the help of graph Laplacian of a random neighborhood graph * generated by the samples. The Laplacian Matrices are saved for each iteration. * * @param nIter - Number of iterations (default: 15) * * @return Denoised data (encoded train dataset). */ blaze::DynamicMatrix<Tv> trainModel(size_t nIter); /** * @brief * The number of nearest neighbors considered in building the distance matrix. */ size_t nNeighbors; /** * @brief * Metric used for train and evaluation. */ Metric metric; /** * @brief * Data Set for training the Denoising Model (original dataset). */ blaze::DynamicMatrix<Tv> xTrain; /** * @brief * Encoded train dataset. */ blaze::DynamicMatrix<Tv> xTrainEncoded; /** * @brief * Diffusion Model. Laplacian Matrices of each iteration. */ std::vector<blaze::DynamicMatrix<Tv>> LArray; }; } // namespace metric #include "Redif.cpp" #endif // Header Guard
5,300
C++
.h
164
29.506098
117
0.718169
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,637
DT.hpp
metric-space-ai_metric/metric/mapping/ensembles/DT.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Max Filippov */ #ifndef _METRIC_MAPPING_ENSEMBLES_DT_HPP #define _METRIC_MAPPING_ENSEMBLES_DT_HPP #include <functional> #include <iostream> #include <memory> #include <vector> #include <math.h> // log2 #include <queue> namespace metric { /** * @class DT * * @brief Metric decision tree */ template <class Record> class DT { public: // forbid creation of copies and allow only move DT(const DT &) = delete; DT(DT &&) = default; // need to clone the tree DT &operator=(const DT &) = delete; DT &operator=(DT &&) = default; // need to clone the tree /** * @brief Construct a new DT object * * @param entropy_threshold * @param gain_threshold */ DT(double entropy_threshold = 0, double gain_threshold = 0); /** * @brief train classifier on test dataset * * @param payments test dataset * @param response */ template <typename ConType, typename VariantType> void train(const ConType &payments, std::vector<VariantType> dimensions, std::function<int(const Record &)> &response); /** * @brief * * @param input_data * @param dimensions * @param predictions */ template <typename ConType, typename VariantType> void predict(const ConType &input_data, std::vector<VariantType> dimensions, std::vector<int> &predictions); private: struct Node // of the tree { std::vector<std::shared_ptr<Node>> children = {}; std::vector<Record> medoid_records = {}; int field_index = -1; int predicted_class = -1; // used in leafs std::vector<int> prediction_distribution = {}; }; // queue is used instead of recursion; we need subset only in queue when building tree, not in resulting tree struct NodeDataUnit { std::shared_ptr<Node> node; std::shared_ptr<std::vector<int>> subset; double entropy = 0; int debug_id; }; template <typename NumType> auto distance_matrix_of_subset(const std::vector<int> &subset, const std::vector<std::vector<NumType>> &feature_dist) -> std::tuple<std::vector<std::vector<NumType>>, std::vector<int>>; template <typename ConType, typename NumType> auto split_subset(const std::vector<int> &subset, const std::vector<std::vector<std::vector<NumType>>> &distances, const ConType &data, const std::function<int(Record)> &response) -> std::tuple<std::vector<std::shared_ptr<std::vector<int>>>, std::vector<double>, std::vector<int>, int, double>; template <typename ConType, typename NumType> auto read_data( // TODO return without copying!! ConType &payments, std::vector<std::function<NumType(Record)>> &features, std::function<int(Record)> &response) -> std::tuple<std::vector<std::vector<std::vector<NumType>>>, std::vector<int>>; template <typename ConType> inline void add_distribution_to_node( // mutates *new_node! const ConType &payments, const std::function<int(Record)> &response, const std::shared_ptr<std::vector<int>> &new_subset, const std::shared_ptr<Node> &new_node // subject to change ); // private properties: std::vector<int> unique_labels = {}; std::shared_ptr<Node> root = nullptr; double entropy_threshold = 0; double gain_threshold = 0; }; } // namespace metric #include "DT/DT.cpp" #endif
3,387
C++
.h
96
32.666667
115
0.715465
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,638
correlation_weighted_accuracy.hpp
metric-space-ai_metric/metric/mapping/ensembles/DT/correlation_weighted_accuracy.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Michael Welsch, Signal Empowering Technology */ /* correlation weighted accuracy */ #ifndef _METRIC_MAPPING_ENSEMBLES_DT_CWA_HPP #define _METRIC_MAPPING_ENSEMBLES_DT_CWA_HPP namespace metric { /** * @brief Correlation weighted accuracy * * @param a * @param b * @return */ template <typename Container> double correlation_weighted_accuracy(Container a, Container b); } // namespace metric #include "correlation_weighted_accuracy.cpp" #endif
672
C++
.h
23
27.521739
93
0.777778
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,639
dimension.hpp
metric-space-ai_metric/metric/mapping/ensembles/DT/dimension.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Panda Team */ #ifndef _METRIC_MAPPING_ENSEMBLES_DT_DIMENSION_HPP #define _METRIC_MAPPING_ENSEMBLES_DT_DIMENSION_HPP #include <functional> #include <tuple> #include <type_traits> #include <variant> // for DimensionSet #include <vector> namespace metric { /** * @class Dimension * * @brief utility class for Metric decision tree constructing */ template <typename MetricType, typename Accessor> class Dimension { public: using InputValueType = typename MetricType::value_type; using ReturnValueType = typename MetricType::distance_type; MetricType DistanceFunctor; /** * @brief Construct a new Dimension object * * @param accessor_ field accessor * @param m metric object */ template <typename A, typename M> Dimension(A accessor_, const M &m) : DistanceFunctor(m), accessor(accessor_) {} /** * @brief Calculate distance between fields in records * * @param r1 data record * @param r2 data record * @return distance between fileds in records r1 and r2 */ template <typename Record> ReturnValueType get_distance(const Record &r1, const Record &r2) { return DistanceFunctor(accessor(r1), accessor(r2)); } private: Accessor accessor; }; /** * @brief function contructing Dimension boject in convenient way * * @param m metric object * @param a field accessor * @return Dimension(a,m) */ template <typename Accessor, typename Metric> inline auto make_dimension(Metric &&m, Accessor a) -> Dimension<Metric, Accessor> { return Dimension<Metric, Accessor>(a, m); } } // namespace metric #endif // DIMENSION_HPP
1,776
C++
.h
59
28.101695
114
0.756883
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,640
edm_wrappers.hpp
metric-space-ai_metric/metric/mapping/ensembles/DT/edm_wrappers.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2018 Panda Team */ #ifndef _METRIC_MAPPING_ENSEMBLES_DT_EDM_WRAPPERS_HPP #define _METRIC_MAPPING_ENSEMBLES_DT_EDM_WRAPPERS_HPP #include <functional> #include <memory> #include <vector> #include "metric/3rdparty/libedm/C45.h" #include "metric/3rdparty/libedm/Classifier.h" #include "metric/3rdparty/libedm/DataSet.h" #include "metric/3rdparty/libedm/Prediction.h" #include "metric/3rdparty/libedm/RandSequence.h" #include "metric/3rdparty/libedm/b-svm.h" #include "metric/3rdparty/libedm/svm.h" namespace metric { /** * @class edmClassifier * * @brief * */ template <class Record, class edmCl> class edmClassifier { public: /** * @brief train model on test dataset * * @param payments test dataset * @param features * @param response */ template <typename ConType> void train(ConType &payments, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> &response); /** * @brief use model to classify input data * * @param data input data * @param features * @param predictions[out] prediction result */ template <typename ConType> void predict(ConType &data, std::vector<std::function<double(Record)>> &features, std::vector<bool> &predictions); /** * @brief clone object * */ std::shared_ptr<edmClassifier<Record, edmCl>> clone(); protected: template <typename ConType> void get_prediction(ConType &data, std::vector<std::function<double(Record)>> &features, std::shared_ptr<edmCl> model, std::vector<bool> &predictions); template <typename ConType> libedm::CDataset read_data(ConType &payments, std::vector<std::function<double(Record)>> &features, std::function<bool(Record)> response = nullptr) const; private: // ptr is needed due to "named ctor idiom" used in libEDM: default ctor is private std::shared_ptr<edmCl> model = nullptr; }; /** * @class edmC45 * @brief * */ template <class Record> class edmC45 : public edmClassifier<Record, libedm::CC45> { public: // ctor takes params for edmCl and stores it in order to use later in Train /** * @brief Construct a new edm C45 object * * @param UMINOBJS_ * @param UEpsilon_ * @param UCF_ * @param WillPrune_ */ edmC45(int UMINOBJS_ = 2, double UEpsilon_ = 1e-3, double UCF_ = 0.25, double WillPrune_ = true); /** * @brief train model on test dataset * * @param payments test dataset * @param features * @param response */ template <typename ConType> void train(ConType payments, std::vector<std::function<double(Record)>> features, std::function<bool(Record)> response); /** * @brief use model to classify input data * * @param data input data * @param features * @param predictions[out] prediction result */ template <typename ConType> void predict(ConType &data, std::vector<std::function<double(Record)>> &features, std::vector<bool> &predictions); /** * @brief clone object * */ std::shared_ptr<edmC45<Record>> clone(); private: std::shared_ptr<libedm::CC45> model; int UMINOBJS; double UEpsilon; double UCF; double WillPrune; }; /** * @class edmSVM * @brief * */ template <class Record> class edmSVM : public edmClassifier<Record, libedm::CSVM> { public: // ctor takes params for edmCl and stores it in order to use later in Train /** * @brief Construct a new edmSVM object * * @param usvm_type * @param ukernel_type * @param udegree * @param ucoef0 * @param ucache_size * @param ueps * @param uC * @param unr_weight * @param uweight_label * @param uweight * @param unu * @param up * @param ushrinking * @param uprobability */ edmSVM(int usvm_type = C_SVC, int ukernel_type = RBF, int udegree = 3, double ucoef0 = 0, double ucache_size = 100, double ueps = 0.001, double uC = 1, int unr_weight = 0, int *uweight_label = NULL, double *uweight = NULL, double unu = 0.5, double up = 0.1, int ushrinking = 1, int uprobability = 0); /** * @brief train model on test dataset * * @param payments test dataset * @param features * @param response */ template <typename ConType> void train(ConType payments, std::vector<std::function<double(Record)>> features, std::function<bool(Record)> response); /** * @brief use model to classify input data * * @param data input data * @param features * @param predictions[out] prediction result */ template <typename ConType> void predict(ConType &data, std::vector<std::function<double(Record)>> &features, std::vector<bool> &predictions); /** * @brief clone object * */ std::shared_ptr<edmSVM<Record>> clone(); private: std::shared_ptr<libedm::CSVM> model; int svm_type; int kernel_type; int degree; double coef0; double cache_size; double eps; double C; int nr_weight; int *weight_label; double *weight; double nu; double p; int shrinking; int probability; }; } // namespace metric #include "edm_wrappers.cpp" #endif
5,138
C++
.h
183
25.453552
116
0.714054
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,641
fast-dct-lee.h
metric-space-ai_metric/metric/3rdparty/DCT/fast-dct-lee.h
/* * Fast discrete cosine transform algorithms (C) * * Copyright (c) 2018 Project Nayuki. (MIT License) * https://www.nayuki.io/page/fast-discrete-cosine-transform-algorithms * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * - The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * - The Software is provided "as is", without warranty of any kind, express or * implied, including but not limited to the warranties of merchantability, * fitness for a particular purpose and noninfringement. In no event shall the * authors or copyright holders be liable for any claim, damages or other * liability, whether in an action of contract, tort or otherwise, arising from, * out of or in connection with the Software or the use or other dealings in the * Software. */ #pragma once #include <stdbool.h> #include <stddef.h> #ifdef __cplusplus extern "C" { #endif bool FastDctLee_transform(double vector[], size_t len); bool FastDctLee_inverseTransform(double vector[], size_t len); #ifdef __cplusplus } #endif
1,516
C++
.h
33
44.121212
83
0.76845
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,643
Prediction.h
metric-space-ai_metric/metric/3rdparty/libedm/Prediction.h
/* Copyright (c) 2014, Qiangli Zhao and Yanhuang Jiang All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CRESULT_INC #define CRESULT_INC #include <ctime> #include <vector> namespace libedm { // predicted result for each instance // class CDataset; class CPrediction //: public CObj { private: int ClassNum; int CaseNum; std::vector<std::vector<double>> Probs; // probability that each instance belongs to each class type double Accuracy; std::vector<int> PredLabelIndices; // predicted class label for each instance std::vector<bool> IsCorrect; // is the prediction correct? public: CPrediction(const CDataset &Dataset, const std::vector<std::vector<double>> &Probabilities, clock_t PredictTime); ~CPrediction(); const std::vector<std::vector<double>> &GetProbs() const; const std::vector<int> &GetPredictedLabelIndices() const; const std::vector<bool> &GetCorrectness() const; double GetAccuracy() const; int GetClassNum() { return ClassNum; }; int GetCaseNum() { return CaseNum; }; }; } // namespace libedm #include "Prediction.cpp" #endif
2,491
C++
.h
53
45.207547
114
0.800743
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,647
RandSequence.h
metric-space-ai_metric/metric/3rdparty/libedm/RandSequence.h
/* Copyright (c) 2014, Qiangli Zhao and Yanhuang Jiang All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef RANDSEQUENCE_INC #define RANDSEQUENCE_INC #include <climits> // for INT_MAX // added by Max Filippov, Oct 21 namespace libedm { // roulette implementing weighted sampling // this function is implemented in RandSequence.cpp int IntRand(int Max); class CRoulette { public: CRoulette(const std::vector<double> &Weights) { double Sum = 0; for (int i = 0; i < (int)Weights.size(); i++) Sum += Weights[i]; double Intv = 0; for (int i = 0; i < (int)Weights.size(); i++) { Intv += Weights[i]; Roulette.push_back(Intv / Sum); } } int Poll() { double r = (double)IntRand(INT_MAX) / (INT_MAX - 1); int RouSize = (int)Roulette.size(); int i; for (i = 0; i < RouSize; i++) if (r <= Roulette[i]) break; if (i >= RouSize) i = RouSize - 1; return i; } private: std::vector<double> Roulette; }; } // namespace libedm namespace libedm { class CRandSequence { public: int Poll(); CRandSequence(int UMax); void Reset(); private: std::vector<int> OldIds; int Max; }; } // namespace libedm #include "RandSequence.cpp" #endif
2,603
C++
.h
73
33.410959
80
0.762151
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,648
Classifier.h
metric-space-ai_metric/metric/3rdparty/libedm/Classifier.h
/* Copyright (c) 2014, Qiangli Zhao and Yanhuang Jiang All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the copyright holders nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ #ifndef CLASSIFIER_FILE #define CLASSIFIER_FILE #include <string> //#include "Obj.h" namespace libedm { // base class of all classifier // class CObj; class CDataset; class CPrediction; class CClassifier //: virtual public CObj { public: // predict a dataset virtual CPrediction *Classify(const CDataset &DataSet) const = 0; virtual inline ~CClassifier() = 0; // classifier is saved into file, and can be reconstructed from it // virtual int Save(const string &Path,const string &FileName) const =0; // virtual bool Dump(const string &FileName) const =0; virtual CClassifier *Clone() const = 0; private: }; inline CClassifier::~CClassifier() {} typedef CClassifier *CreateFunc(const CDataset &Dataset, const void *Params); typedef CClassifier *FileCreateFunc(const std::string &Path, const std::string &FileName); } // namespace libedm #endif
2,382
C++
.h
50
45.92
90
0.804057
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,650
energy_encoder.hpp
metric-space-ai_metric/metric/transform/energy_encoder.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Michael Welsch */ #ifndef _METRIC_ENERGY_ENCODER_HPP #define _METRIC_ENERGY_ENCODER_HPP #include <cstddef> #include <vector> namespace metric { // functor for computing energy // very simple and inoptimal recursive functor without static members, to be used as reference class EnergyEncoder { public: EnergyEncoder(int wavelet_type = 4, std::size_t splits = 2, bool bothsided = true); template <template <typename, typename> class Container, typename Allocator, typename ValueType> Container<ValueType, Allocator> operator()(Container<ValueType, Allocator> &in) const; std::vector<std::size_t> freq_bounds(std::size_t len) const; private: int wavelet_type; std::size_t subbands; bool bothsided; }; // functions for computing sizes std::size_t subband_size(std::size_t original_size, std::size_t depth, std::size_t wavelet_length); std::size_t original_size(std::size_t subband_size, std::size_t depth, std::size_t wavelet_length); } // namespace metric #include "energy_encoder.cpp" #endif
1,237
C++
.h
30
39.166667
99
0.764657
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,651
wavelet2d.hpp
metric-space-ai_metric/metric/transform/wavelet2d.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Michael Welsch */ #ifndef _METRIC_TRANSFORM_WAVELET2D_HPP #define _METRIC_TRANSFORM_WAVELET2D_HPP #include "metric/utils/image_processing/convolution.hpp" #include "wavelet.hpp" //#include <blaze/Blaze.h> #include <cmath> // for only sqrt namespace wavelet { template <typename T, size_t Channels> class Convolution2dCustom : public metric::Convolution2d<T, Channels> { public: Convolution2dCustom(size_t imageWidth, size_t imageHeight, size_t kernelWidth, size_t kernelHeight // const PadDirection pd = PadDirection::POST, // const PadType pt = PadType::CIRCULAR, // const size_t stride = 1 ) { // this->padWidth = kernelWidth - 1; // this->padHeight = kernelHeight - 1; // metric::Convolution2d<T, Channels>(imageWidth, imageHeight, kernelWidth, kernelHeight); // TODO // remove this->padWidth = 0; this->padHeight = 0; metric::PadDirection pd = metric::PadDirection::POST; // metric::PadDirection pd = metric::PadDirection::BOTH; metric::PadType pt = metric::PadType::CIRCULAR; // metric::PadType pt = metric::PadType::REPLICATE; size_t stride = 2; this->padModel = std::make_shared<metric::PadModel<T>>(pd, pt, 0); // auto t1 = Clock::now(); this->convLayer = std::make_shared<typename metric::Convolution2d<T, Channels>::ConvLayer2d>( imageWidth + this->padWidth, imageHeight + this->padHeight, 1, 1, kernelWidth, kernelHeight, stride); // auto t2 = Clock::now(); // auto d = std::chrono::duration_cast<std::chrono::duration<double>>(t2 - t1); } }; template <typename Container2d> std::tuple<Container2d, Container2d, Container2d, Container2d> create2dKernels(int order) { using El = typename Container2d::ElementType; // now we support only Blaze matrices assert(order % 2 == 0); El coeff = 2 / sqrt(2); std::vector<El> scaling(order); std::vector<El> wavelet(order); scaling = wavelet::dbwavf<std::vector<El>>(order / 2); int sign = -1; // because we start filling wavelet vector from the last point for (size_t i = 0; i < scaling.size(); ++i) { scaling[i] = scaling[i] * coeff; wavelet[wavelet.size() - i - 1] = scaling[i] * sign; sign *= -1; } Container2d ll(order, order); Container2d lh(order, order); Container2d hl(order, order); Container2d hh(order, order); El ll_el, hh_el; for (size_t i = 0; i < scaling.size(); ++i) { for (size_t j = 0; j <= i; ++j) { // hh, ll are symmetric cases ll_el = scaling[i] * scaling[j]; ll(i, j) = ll_el; ll(j, i) = ll_el; hh_el = wavelet[i] * wavelet[j]; hh(i, j) = hh_el; hh(j, i) = hh_el; } } for (size_t i = 0; i < scaling.size(); ++i) { for (size_t j = 0; j < scaling.size(); ++j) { // non-symmetric, loop through all values lh(i, j) = scaling[i] * wavelet[j]; hl(i, j) = wavelet[i] * scaling[j]; } } return std::make_tuple(ll, lh, hl, hh); } template <typename T> std::tuple<blaze::DynamicMatrix<T>, blaze::DynamicMatrix<T>, blaze::DynamicMatrix<T>, blaze::DynamicMatrix<T>> dwt2_conv2(blaze::DynamicMatrix<T> const &x, std::tuple<blaze::DynamicMatrix<T>, blaze::DynamicMatrix<T>, blaze::DynamicMatrix<T>, blaze::DynamicMatrix<T>> const &kernels) { // based on 2d convolution auto ll_k = std::get<0>(kernels); auto lh_k = std::get<1>(kernels); auto hl_k = std::get<2>(kernels); auto hh_k = std::get<3>(kernels); assert(ll_k.rows() == lh_k.rows()); assert(ll_k.rows() == hl_k.rows()); assert(ll_k.rows() == hh_k.rows()); assert(ll_k.columns() == lh_k.columns()); assert(ll_k.columns() == hl_k.columns()); assert(ll_k.columns() == hh_k.columns()); auto c2d = wavelet::Convolution2dCustom<double, 1>(x.rows(), x.columns(), ll_k.rows(), ll_k.columns()); // auto c2d = metric::Convolution2d<double, 1>(x.rows(), x.columns(), ll_k.rows(), ll_k.columns()); blaze::StaticVector<blaze::DynamicMatrix<double>, 1> vx{x}; auto ll = c2d(vx, ll_k); auto lh = c2d(vx, lh_k); auto hl = c2d(vx, hl_k); auto hh = c2d(vx, hh_k); return std::make_tuple(ll[0], lh[0], hl[0], hh[0]); } } // namespace wavelet #endif // WAVELET2D_HPP
4,300
C++
.h
105
38.257143
111
0.666986
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,652
discrete_cosine.hpp
metric-space-ai_metric/metric/transform/discrete_cosine.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_TRANSFORM_DISCRETE_COSINE_HPP #define _METRIC_TRANSFORM_DISCRETE_COSINE_HPP namespace metric { /** * @brief apply forward or invese DCT depending on bool flag * * @param Slices * @param inverse * @return true * @return false */ template <class BlazeMatrix> bool apply_DCT(BlazeMatrix &Slices, bool inverse = false); } // namespace metric #include "discrete_cosine.cpp" #endif
634
C++
.h
21
28.571429
87
0.762768
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,653
hog.hpp
metric-space-ai_metric/metric/transform/hog.hpp
#ifndef PANDA_METRIC_HOG_HPP #define PANDA_METRIC_HOG_HPP #include <blaze/Math.h> namespace metric { template <typename T> class HOG { public: using Matrix = blaze::DynamicMatrix<T>; using DistanceMatrix = blaze::SymmetricMatrix<Matrix>; using Vector = blaze::DynamicVector<T>; HOG(const size_t orientations, const size_t cellSize, const size_t blockSize); Vector encode(const HOG::Matrix &image) const; typename HOG<T>::DistanceMatrix getGroundDistance(const blaze::DynamicMatrix<T> &image, const T rotation_cost, const T move_cost, const T threshold = 0); private: size_t orientations; size_t cellSize; size_t blockSize; DistanceMatrix getSpatialDistance(size_t n_hog_bins, size_t blocks_per_image_rows, size_t blocks_per_image_columns); typename HOG<T>::DistanceMatrix getOrientationDistance(const T angleUnitCost = 20); }; } // namespace metric #include "hog.cpp" #endif // PANDA_METRIC_HOG_HPP
940
C++
.h
23
38.086957
117
0.768212
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,654
distance_potential_minimization.hpp
metric-space-ai_metric/metric/transform/distance_potential_minimization.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Michael Welsch */ #ifndef _METRIC_DISTANCE_POTENTIAL_MINIMIZATION_CPP #define _METRIC_DISTANCE_POTENTIAL_MINIMIZATION_CPP #include <blaze/Blaze.h> #include <tuple> namespace metric { // output = ellipse parameter [xc, yc, a, b, phi] static std::vector<double> fit_hysteresis(const blaze::DynamicVector<double> &x, const blaze::DynamicVector<double> &y, size_t grid_row, size_t grid_column, size_t steps = 200, std::vector<double> sigma = {50, 30, 15, 5}); static std::vector<double> fit_hysteresis(const blaze::DynamicMatrix<double> &I, double xc0, double yc0, double r0, size_t steps = 200, std::vector<double> sigma = {50, 30, 15, 5}, double incr = 0.2, double thresh = 1e-6); static std::vector<double> fit_hysteresis(const blaze::DynamicMatrix<double> &I, size_t steps = 200, std::vector<double> sigma = {50, 30, 15, 5}); } // namespace metric #include "distance_potential_minimization.cpp" #endif
1,194
C++
.h
23
47.565217
119
0.703098
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,655
wavelet.hpp
metric-space-ai_metric/metric/transform/wavelet.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Michael Welsch */ #ifndef _METRIC_TRANSFORM_WAVELET_HPP #define _METRIC_TRANSFORM_WAVELET_HPP #include <algorithm> #include <blaze/Blaze.h> #include <cmath> #include <deque> #include <functional> #include <iostream> #include <tuple> #include <vector> namespace wavelet { namespace types { // type traits // TODO move to some commonly included file /** * @brief if T is a container and implemented operator[] value is true, otherwise value is false * * @tparam T checking type * */ template <typename T> class has_index_operator { struct nil_t { }; template <typename U> static constexpr auto test(U *) -> decltype(std::declval<U &>()[0]); template <typename> static constexpr auto test(...) -> nil_t; public: using type = typename std::decay<decltype(test<T>(nullptr))>::type; static const bool value = !std::is_same<type, nil_t>::value; }; /** * @brief extract returning type of operator[] in container, in case of STL containers is equivalent ot value_type * for example: underlying_type<std::vector<int>> == int, * underlying_type<std::vector<std::vector<int>>> == std::vector<int>, * @tparam T Container type */ template <typename T> using index_value_type_t = typename has_index_operator<T>::type; } // namespace types template <typename Container2d, bool SO, bool F = blaze::IsDenseMatrix<Container2d>::value> struct InternalBlazeType { }; // internal matrix and vector types of sparsity as Container2d, TODO use on dwt2 or remove template <typename Container2d, bool SO> struct InternalBlazeType<Container2d, SO, true> { using El = typename Container2d::ElementType; using vector_type = blaze::DynamicVector<El, SO>; using matrix_type = blaze::DynamicMatrix<El, SO>; }; template <typename Container2d, bool SO> struct InternalBlazeType<Container2d, SO, false> { using El = typename Container2d::ElementType; using vector_type = blaze::CompressedVector<El, SO>; using matrix_type = blaze::CompressedMatrix<El, SO>; }; template <typename Container2d, bool SO = blaze::rowMajor> using InternalBlazeVecT = typename InternalBlazeType<Container2d, SO>::vector_type; template <typename Container2d, bool SO = blaze::rowMajor> using InternalBlazeMatT = typename InternalBlazeType<Container2d, SO>::matrix_type; // wavelet functions /** * Padding type */ enum class Padding { ZeroDerivative, Periodized }; ///** // * @brief valid convolution // * // * @param f // * @param g // * @return // */ // template <typename T> // std::vector<T> conv_valid(std::vector<T> const& f, std::vector<T> const& g); /** * @brief valid convolution * * @param f * @param g * @return */ template <typename Container> Container conv_valid(Container const &f, Container const &g); // overload added by Max F ///** // * @brief full convolution // * // * @param f // * @param g // * @return // */ // template <typename T> // std::vector<T> conv(std::vector<T> const& f, std::vector<T> const& g); /** * @brief full convolution * * @param f * @param g * @return */ template <typename Container> Container conv(Container const &f, Container const &g); // overload added by Max F /** * @brief linspace (erzeugt einen linearen Datenvektor) * * @param a * @param b * @param n * @return */ template <typename Container> Container linspace(typename Container::value_type a, typename Container::value_type b, int n); ///** // * @brief upsconv // * // * @param x // * @param f // * @param len // * @return // */ // template <typename T> // std::vector<T> upsconv(std::vector<T> const& x, std::vector<T> const& f, int len); /** * @brief upsconv * * @param x * @param f * @param len * @return */ template <typename Container> Container upsconv(Container const &x, Container const &f, int len); // overload added by Max F /** * @brief returns the scaling filter associated with the Daubechies wavelet. * * @param wnum Daubechies wavelet vanishing moment, a positive integer in the closed interval [1, 10] * @return */ template <typename Container> Container dbwavf(const int wnum); ///** // * @brief // * // * @param W_in // * @return // */ // template <typename T> // std::tuple<std::vector<T>, std::vector<T>, std::vector<T>, std::vector<T>> orthfilt(std::vector<T> const& W_in); /** * @brief * * @param W_in * @return */ template <typename Container> std::tuple<Container, Container, Container, Container> orthfilt(Container const &W_in); // added by Max F ///** // * @brief // * // * @param x // * @param waveletType // * @return // */ // template <typename T> // std::tuple<std::vector<T>, std::vector<T>> dwt(std::vector<T> const& x, int waveletType); /** * @brief * * @param x * @param waveletType * @return */ template <typename Container> std::tuple<Container, Container> dwt(Container const &x, int waveletType); // overload added by Max F ///** // * @brief // * // * @param a // * @param d // * @param waveletType // * @param lx // * @return // */ // template <typename T> // std::vector<T> idwt(std::vector<T> a, std::vector<T> d, int waveletType, int lx); /** * @brief * * @param a * @param d * @param waveletType * @param lx * @return */ template <typename Container> Container idwt(Container a, Container d, int waveletType, int lx); // overload added by Max F, called in DSPCC /** * @brief * * @param sizeX * @param waveletType * @return */ static int wmaxlev(int sizeX, int waveletType); ///** // * @brief // * // * @param x // * @param order // * @param waveletType // * @return // */ // template <typename T> // std::deque<std::vector<T>> wavedec(std::vector<T> const& x, int order, int waveletType); /** * @brief * * @param x * @param order * @param waveletType * @return */ template <typename Container> std::deque<Container> wavedec(Container const &x, int order, int waveletType); ///** // * @brief // * // * @param subBands // * @param waveletType // * @return // */ // template <typename T> // std::vector<T> waverec(std::deque<std::vector<T>> const& subBands, int waveletType); /** * @brief * * @param subBands * @param waveletType * @return */ template <typename Container> Container waverec(std::deque<Container> const &subBands, int waveletType); ///** // * @brief // * // * @param data // * @param tresh // * @return // */ // template <typename T> // std::deque<std::vector<T>> denoise(std::deque<std::vector<T>> const& data, T const& tresh); // not implemented /** * @brief * * @param data * @param tresh * @return */ template <typename Container> std::deque<Container> denoise(std::deque<Container> const &data, typename Container::value_type const &tresh); // not implemented ///** // * @brief // * // * @param data // * @return // */ // template <typename T> // std::tuple<std::deque<std::vector<T>>, std::deque<std::vector<T>>> sparse(std::deque<std::vector<T>> const& data); // // not implemented /** * @brief * * @param data * @return */ template <typename Container> std::tuple<std::deque<Container>, std::deque<Container>> sparse(std::deque<Container> const &data); // not implemented /** * @brief distance measure by time elastic cost matrix. * * @param As * @param Bs * @param penalty * @param elastic * @return */ template <typename T> // T TWED(blaze::CompressedVector<T> const& As, blaze::CompressedVector<T> const& Bs, T const& penalty = 0, // T const& elastic = 1); // original code T TWED(blaze::CompressedVector<T> const &As, blaze::CompressedVector<T> const &Bs, T const &penalty, T const &elastic); // edited by Max F because of "redefinition of default argument" compile-time error. I hope // this does not break anything.. // 2d functions ///** // * @brief non-blaze dwt2 // * // * @param x // * @param waveletType // * @return // */ // template <typename Container> // dwt2(std::vector<Container> const & x, int waveletType); /** * @brief blaze matrix overload of dwt2 * * @param x * @param waveletType * @return */ template <typename Container> typename std::enable_if< !blaze::IsMatrix<Container>::value, std::tuple<std::vector<Container>, std::vector<Container>, std::vector<Container>, std::vector<Container>>>::type dwt2(std::vector<Container> const &x, int waveletType); /** * @brief * * @param x * @param waveletType * @return */ template <typename Container2d> // std::tuple<Container2d, Container2d, Container2d, Container2d> typename std::enable_if<blaze::IsMatrix<Container2d>::value, std::tuple<Container2d, Container2d, Container2d, Container2d>>::type dwt2(Container2d const &x, int waveletType); /** * @brief * * @param ll * @param lh * @param hl * @param hh * @param waveletType * @param hx * @param wx * @return */ template <typename Container> std::vector<Container> idwt2(std::vector<Container> const &ll, std::vector<Container> const &lh, std::vector<Container> const &hl, std::vector<Container> const &hh, int waveletType, int hx, int wx); /** * @brief * * @param in * @param waveletType * @param hx * @param wx * @return */ template <typename Container> std::vector<Container> idwt2(std::tuple<std::vector<Container>, std::vector<Container>, std::vector<Container>, std::vector<Container>> in, int waveletType, int hx, int wx); /** * @brief * * @param ll * @param lh * @param hl * @param hh * @param waveletType * @param hx * @param wx * @return */ template <typename Container2d> // Container2d idwt2( typename std::enable_if<blaze::IsMatrix<Container2d>::value, Container2d>::type idwt2(Container2d const &ll, Container2d const &lh, Container2d const &hl, Container2d const &hh, int waveletType, int hx, int wx); } // namespace wavelet #include "wavelet.cpp" #endif
9,903
C++
.h
368
25.184783
120
0.695771
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,656
poor_mans_quantum.hpp
metric-space-ai_metric/metric/utils/poor_mans_quantum.hpp
/* PANDA presents ██████╗ ██████╗ ██████╗ ██████╗ ███╗ ███╗ █████╗ ███╗ ██╗███████╗ ██████╗ ██╗ ██╗ █████╗ ███╗ ██╗████████╗██╗ ██╗███╗ ███╗ ██╔══██╗██╔═══██╗██╔═══██╗██╔══██╗ ████╗ ████║██╔══██╗████╗ ██║██╔════╝ ██╔═══██╗██║ ██║██╔══██╗████╗ ██║╚══██╔══╝██║ ██║████╗ ████║ ██████╔╝██║ ██║██║ ██║██████╔╝ ██╔████╔██║███████║██╔██╗ ██║███████╗ ██║ ██║██║ ██║███████║██╔██╗ ██║ ██║ ██║ ██║██╔████╔██║ ██╔═══╝ ██║ ██║██║ ██║██╔══██╗ ██║╚██╔╝██║██╔══██║██║╚██╗██║╚════██║ ██║▄▄ ██║██║ ██║██╔══██║██║╚██╗██║ ██║ ██║ ██║██║╚██╔╝██║ ██║ ╚██████╔╝╚██████╔╝██║ ██║ ██║ ╚═╝ ██║██║ ██║██║ ╚████║███████║ ╚██████╔╝╚██████╔╝██║ ██║██║ ╚████║ ██║ ╚██████╔╝██║ ╚═╝ ██║ ╚═╝ ╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝╚══════╝ ╚══▀▀═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝ Licensed under MPL 2.0. Michael Welsch (c) 2019. a library for computing with random distributed variables a random distributed variable is just like a single value but with uncertainty around it. you can do most operation with them like with real numbers and a few special ones, that do not make sense for real numbers. This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Michael Welsch */ #ifndef _METRIC_UTILS_POOR_MANS_QUANTUM_HPP #define _METRIC_UTILS_POOR_MANS_QUANTUM_HPP #ifndef RV_SAMPLES #define RV_SAMPLES 10000 // precision [1000 .. 1000000] #endif #ifndef RV_ERROR #define RV_ERROR 0.05 // statistical error level alpha [0.001 .. 0.2] #endif #include "poor_mans_quantum/distributions/Discrete.hpp" namespace metric { template <typename Distribution = Discrete<float>, typename T = float> class PMQ { public: Distribution _dist; /** * @brief Construct a new PMQ object * * @param par1 * @param par2 * @param n * @param d */ PMQ(T par1 = 1.0, T par2 = 0.0, size_t n = 1000, Distribution d = Distribution()); /** * @brief Construct a new PMQ object * * @param data * @param d */ PMQ(std::vector<T> data, Distribution d = Distribution()); /** * @brief * * @return */ T rnd(); /** * @brief * * @return */ T median(); /** * @brief * * @param p * @return */ T quantil(T p); /** * @brief * * @return */ T mean(); /** * @brief * * @return */ T variance(); /** * @brief * * @param x * @return */ T pdf(T x); /** * @brief * * @param x * @return */ T cdf(T x); /** * @brief * * @param x * @return */ T icdf(T x); /** * @brief * * @return size_t */ size_t size(); /** * @brief * * @tparam mT * @param rv * @return */ template <typename mT> mT believe(mT rv); /** * @brief * * @param confidencelevel * @return */ std::tuple<PMQ<Discrete<float>>, PMQ<Discrete<float>>> confidence(const T &confidencelevelb = 1 - RV_ERROR) const; /** * @brief * * @tparam mT1 * @tparam mT2 * @param set_0_left * @param set_0_right * @param confidencelevel * @return */ template <typename mT1, typename mT2> std::tuple<PMQ<Discrete<float>>, PMQ<Discrete<float>>> merged_confidence(const mT1 &set_0_left, const mT2 &set_0_right, const T confidencelevel = 1 - RV_ERROR) const; /** * @brief * * @tparam mT1 * @tparam mT2 * @param set_left * @param set_right * @return */ template <typename mT1, typename mT2> float in_confidence(const mT1 &set_left, const mT2 &set_right) const; /** * @brief * * @tparam mT1 * @tparam mT2 * @param set_left * @param set_right * @return */ template <typename mT1, typename mT2> float out_confidence(const mT1 &set_left, const mT2 &set_right) const; /** * @brief * * @return */ float is_normal_distributed(); /** * @brief * * @return */ float is_weibull_distributed(); private: std::mt19937_64 _generator; }; } // namespace metric #include "poor_mans_quantum/poor_mans_quantum.cpp" #endif // headerguard
5,444
C++
.h
171
21.467836
118
0.515997
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,657
type_traits.hpp
metric-space-ai_metric/metric/utils/type_traits.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2020 Panda Team */ #ifndef METRIC_UTILS_TYPE_TRAITS_HPP #define METRIC_UTILS_TYPE_TRAITS_HPP #include <type_traits> #include <utility> namespace metric::type_traits { /** * @brief if T is a container and implemented operator[] value is true, otherwise value is false * * @tparam T checking type * */ template <typename T> class is_has_index_operator { struct nil_t { }; template <typename U> static constexpr auto test(U *) -> decltype(std::declval<U &>()[0]); template <typename> static constexpr auto test(...) -> nil_t; public: using type = typename std::decay<decltype(test<T>(nullptr))>::type; static const bool value = !std::is_same<type, nil_t>::value; }; /** * @brief if T is a class that has resize() method value is true, otherwise value is false * * @tparam T checking type */ template <typename T> class is_has_resize_method { struct nil_t { }; template <typename U> static constexpr auto test(U *) -> decltype(std::declval<U &>().resize(0)); template <typename> static constexpr auto test(...) -> nil_t; public: using type = typename std::decay<decltype(test<T>(nullptr))>::type; static const bool value = !std::is_same<type, nil_t>::value; }; /** * @brief extract returning type of operator[] in container, in case of STL containers is equivalent ot value_type * for example: underlying_type<std::vector<int>> == int, * underlying_type<std::vector<std::vector<int>>> == std::vector<int>, * @tparam T Container type */ template <typename T> using index_value_type_t = typename is_has_index_operator<T>::type; /** * @brief helper for is_has_index_operator metafunction */ template <typename T> inline constexpr bool is_has_index_operator_v = is_has_index_operator<T>::value; /** * @brief helper for is_has_resize_method metafunction */ template <typename T> inline constexpr bool is_has_resize_method_v = is_has_resize_method<T>::value; /** * @brief if T is a container supported [] operator and underlying type of container is integral type * value is true, otherwise value is false */ template <typename T> struct is_container_of_integrals { static constexpr auto test() { if constexpr (is_has_index_operator_v<T>) { using T1 = index_value_type_t<T>; if constexpr (is_has_index_operator_v<T1>) { using T2 = index_value_type_t<T1>; return std::is_integral_v<T2>; } else { return std::is_integral_v<T1>; } } else { return false; } } static const bool value = test(); }; /** * @brief helper for is_container_of_integrals metafunction */ template <typename T> inline constexpr bool is_container_of_integrals_v = is_container_of_integrals<T>::value; template <typename T> struct underlying_type_impl0 { using type = typename std::decay<T>::type; }; template <typename T> using underlaying0_t = typename underlying_type_impl0<T>::type; template <typename T> struct underlying_type_impl1 { using type = typename std::decay<typename is_has_index_operator<T>::type>::type; }; template <typename T> using underlaying1_t = typename underlying_type_impl1<T>::type; template <typename T> struct underlying_type_impl2 { using type = typename std::decay<typename is_has_index_operator<typename is_has_index_operator<T>::type>::type>::type; }; template <typename T> using underlaying2_t = typename underlying_type_impl2<T>::type; /** * @brief extract underlying type of container, * for example: underlying_type<std::vector<int>> == int, * underlying_type<std::vector<std::vector<int>>> == int, * */ template <typename T> struct underlying_type { static constexpr auto level() -> int { if constexpr (is_has_index_operator_v<T>) { using T1 = index_value_type_t<T>; if constexpr (is_has_index_operator_v<T1>) { return 2; } return 1; } return 0; } using type = std::conditional_t<level() == 2, underlaying2_t<T>, std::conditional_t<level() == 1, underlaying1_t<T>, underlaying0_t<T>>>; }; /** * @brief helper for underlying_type metafunction */ template <typename T> using underlying_type_t = typename underlying_type<T>::type; } // namespace metric::type_traits #endif
4,350
C++
.h
120
34.008333
114
0.713913
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,658
genetic.hpp
metric-space-ai_metric/metric/utils/genetic.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. /* based on Olivier Mallets implementation https://github.com/olmallet81/GALGO-2.0 */ #ifndef _METRIC_GENETIC_HPP #define _METRIC_GENETIC_HPP #include <iostream> namespace metric { template <typename P, typename T> class Genetic; namespace genetic_details { template <typename T, int N = 16> class Parameter; template <typename P, typename T> class Chromosome; template <typename P, typename T> class Population; // convenient typedefs template <typename P, typename T> using CHR = std::shared_ptr<Chromosome<P, T>>; template <typename T> using PAR = std::unique_ptr<Parameter<T>>; } // namespace genetic_details } // namespace metric #include "genetic/Chromosome.hpp" #include "genetic/Parameter.hpp" #include "genetic/Population.hpp" #include "genetic/evolution_methods.hpp" #include "genetic/genetic_impl.hpp" #endif // header guard
1,046
C++
.h
27
37.222222
80
0.779104
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,659
dsv.hpp
metric-space-ai_metric/metric/utils/dsv.hpp
#ifndef PANDA_METRIC_DSV_HPP #define PANDA_METRIC_DSV_HPP #include <blaze/Math.h> namespace metric { } #endif // PANDA_METRIC_DSV_HPP
138
C++
.h
6
21.333333
30
0.78125
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,660
ThreadPool.hpp
metric-space-ai_metric/metric/utils/ThreadPool.hpp
#ifndef VINCENT_CPP_THREADPOOL_H #define VINCENT_CPP_THREADPOOL_H #include <atomic> #include <condition_variable> #include <functional> #include <list> #include <mutex> #include <thread> #include <vector> class ThreadPool { public: typedef std::function<void()> callable; ThreadPool(size_t maxThreads); void execute(const callable &block); void close(); size_t getThreadsCount() const { return threads_.size(); } size_t getQueueSize() const { return queue_.size(); } private: class CloseException : public std::exception { }; private: std::mutex mutex_; std::condition_variable cvEmpty_; std::atomic<bool> isClosed_; std::list<callable> queue_; std::vector<std::thread> threads_; void addWorker(); void putToQueue(const callable &block); callable getFromQueue(); }; #include "ThreadPool.cpp" #endif // VINCENT_CPP_THREADPOOL_H
856
C++
.h
32
24.84375
59
0.756426
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,661
auto_detect_metric.hpp
metric-space-ai_metric/metric/utils/auto_detect_metric.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 PANDA Team */ namespace metric { /** * @class MetricAutoDetector * @brief * */ class MetricAutoDetector { public: /** * @brief Construct a new MetricAutoDetector object * */ explicit MetricAutoDetector(); /** * @brief * */ template <typename Record, typename Graph> std::string detect(Graph &graph, int graph_w, int graph_h, std::vector<Record> dataset, bool isEstimate = true); /** * @brief * */ void set_verbose(bool isVerbose) { verbose = isVerbose; } private: bool verbose = false; template <typename Record, typename Graph, typename Metric> double get_mean_distance_difference(Graph &graph, Metric distance, std::vector<Record> dataset, std::vector<size_t> randomized_indexes, bool isEstimate); }; } // end namespace metric #include "auto_detect_metric/auto_detect_metric.cpp"
1,053
C++
.h
38
25.236842
113
0.729353
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
true
false
true
false
1,531,662
graph.hpp
metric-space-ai_metric/metric/utils/graph.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Michael Welsch */ #pragma once #ifndef _METRIC_UTILS_GRAPH_HPP #define _METRIC_UTILS_GRAPH_HPP #include "type_traits.hpp" #include <blaze/Blaze.h> #include <stack> #include <type_traits> namespace metric { // Graph based on blaze-lib /** * @class Graph * @brief * */ template <typename WeightType = bool, bool isDense = false, bool isSymmetric = true> class Graph { // used only in getNeighboursOld, TODO remove if method removed static constexpr bool isWeighted = !std::is_same<WeightType, bool>::value; using InnerMatrixType = typename std::conditional<isDense, blaze::DynamicMatrix<WeightType>, blaze::CompressedMatrix<WeightType>>::type; using MatrixType = typename std::conditional<isSymmetric, blaze::SymmetricMatrix<InnerMatrixType>, InnerMatrixType>::type; public: /** * @brief Construct a new Graph object * */ Graph(); /** * @brief Construct a new Graph object * * @param nodesNumber */ explicit Graph(size_t nodesNumber); /** * @brief Construct a new Graph object * * @param edgesPairs */ Graph(const std::vector<std::pair<size_t, size_t>> &edgesPairs); /** * @brief Construct a new Graph object * * @param matrix */ Graph(MatrixType &&matrix); /** * @brief Destroy the Graph object * */ ~Graph() = default; /** * @brief * * @return */ size_t getNodesNumber() const; /** * @brief * * @return */ bool isValid() const; /** * @brief * * @param nodeIndex * @param maxDeep * @return */ std::vector<std::vector<size_t>> getNeighboursOld(const size_t nodeIndex, const size_t maxDeep); /** * @brief * * @param nodeIndex * @param maxDeep * @return */ template <typename T = WeightType, bool denseFlag = isDense> typename std::enable_if_t<!std::is_same<T, bool>::value, std::vector<std::vector<size_t>>> getNeighbours(const size_t nodeIndex, const size_t maxDeep); // not bool /** * @brief * * @param nodeIndex * @param maxDeep * @return */ template <typename T = WeightType, bool denseFlag = isDense> typename std::enable_if_t<std::is_same<T, bool>::value && !denseFlag, std::vector<std::vector<size_t>>> getNeighbours(const size_t nodeIndex, const size_t maxDeep); // bool, not dense /** * @brief * * @param nodeIndex * @param maxDeep * @return */ template <typename T = WeightType, bool denseFlag = isDense> typename std::enable_if_t<std::is_same<T, bool>::value && denseFlag, std::vector<std::vector<size_t>>> getNeighbours(const size_t nodeIndex, const size_t maxDeep); // bool, dense /** * @brief * * @return */ const MatrixType &get_matrix() const; /** * @brief * * @param edgesPairs */ void buildEdges(const std::vector<std::pair<size_t, size_t>> &edgesPairs); /** * @brief * * @param edgeMatrix */ void updateEdges(const MatrixType &edgeMatrix); bool is_matrix_changed() { return matrix_changed_; } protected: size_t nodesNumber = 0; bool matrix_changed_ = false; bool valid = false; MatrixType matrix; size_t modularPow(const size_t base, const size_t exponent, const size_t modulus); }; /** * @class Grid4 * @brief * */ class Grid4 : public Graph<> { public: /** * @brief Construct a new Grid 4 object * * @param nodesNumber */ explicit Grid4(size_t nodesNumber); /** * @brief Construct a new Grid 4 object * * @param width * @param height */ Grid4(size_t width, size_t height); private: void construct(size_t width, size_t height); }; /** * @class Grid6 * @brief * */ class Grid6 : public Graph<> { public: /** * @brief Construct a new Grid 6 object * * @param nodesNumber */ explicit Grid6(size_t nodesNumber); /** * @brief Construct a new Grid 6 object * * @param width * @param height */ Grid6(size_t width, size_t height); private: void construct(size_t width, size_t height); }; /** * @class Grid8 * @brief * */ class Grid8 : public Graph<> { public: /** * @brief Construct a new Grid 8 object * * @param nodesNumber */ explicit Grid8(size_t nodesNumber); /** * @brief Construct a new Grid 8 object * * @param width * @param height */ Grid8(size_t width, size_t height); private: void construct(size_t width, size_t height); }; /** * @class Paley * @brief * */ class Paley : public Graph<> { public: /** * @brief Construct a new Paley object * * @param nodesNumber */ explicit Paley(size_t nodesNumber); }; /** * @class LPS * @brief * */ class LPS : public Graph<> { public: /** * @brief Construct a new LPS object * * @param nodesNumber */ explicit LPS(size_t nodesNumber); private: bool miller_rabin_pass(const size_t a, const size_t s, const size_t d, const size_t nodesNumber); bool millerRabin(const size_t nodesNumber); }; /** * @class Margulis * @brief * */ class Margulis : public Graph<> { public: /** * @brief Construct a new Margulis object * * @param nodesNumber */ explicit Margulis(size_t nodesNumber); }; // /** * @brief random weighted graph for usage as e. g. ESN reservoir, TODO implement * * @tparam WType * @tparam isDense */ template <typename WType, bool isDense> class RandomUniform : public Graph<WType, isDense, false> { public: /** * @brief Construct a new Random Uniform object * * @param nNodes * @param lower_bound * @param upper_bound * @param nConections */ RandomUniform(size_t nNodes, WType lower_bound = -1, WType upper_bound = -1, int nConections = 0); private: void fill(blaze::CompressedMatrix<WType> &matrix, WType lower_bound, WType upper_bound, int nConections); void fill(blaze::DynamicMatrix<WType> &matrix, WType lower_bound, WType upper_bound, int nConections); template <typename MType> void fill(MType &matrix, WType lower_bound, WType upper_bound); }; /** * @brief create Graph object based on blaze::CompressedMatrix * * @tparam ValueType * @param matrix * @return */ template <class ValueType> Graph<ValueType, false, false> make_graph(blaze::CompressedMatrix<ValueType> &&matrix); /** * @brief create Graph object based on blaze::SymetricMatrix<CompressedMatrix> * * @tparam ValueType * @param matrix * @return */ template <class ValueType> Graph<ValueType, false, true> make_graph(blaze::SymmetricMatrix<blaze::CompressedMatrix<ValueType>> &&matrix); /** * @brief create Graph object based on blaze::DynamicMatrix * * @tparam ValueType * @param matrix * @return */ template <class ValueType> Graph<ValueType, true, false> make_graph(blaze::DynamicMatrix<ValueType> &&matrix); /** * @brief create Graph object based on blaze::SymetricMatrix<DynamicMatrix> * * @tparam ValueType * @param matrix * @return */ template <class ValueType> Graph<ValueType, true, true> make_graph(blaze::SymmetricMatrix<blaze::DynamicMatrix<ValueType>> &&matrix); } // end namespace metric #include "graph/graph.cpp" #endif // header guards
7,130
C++
.h
306
20.901961
114
0.701462
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,663
visualizer.hpp
metric-space-ai_metric/metric/utils/visualizer.hpp
/* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. Copyright (c) 2019 Panda Team */ #ifndef _METRIC_UTILS_VISUALIZER_HPP #define _METRIC_UTILS_VISUALIZER_HPP #include <blaze/Blaze.h> #include <string> namespace mat2bmp { /** * @brief * * @param m * @param filename */ template <class BlazeMatrix> void blaze2bmp(BlazeMatrix m, std::string filename); /** * @brief * * @param m * @param filename */ template <class BlazeMatrix> void blaze2bmp_norm(BlazeMatrix m, std::string filename); } // namespace mat2bmp #include "visualizer.cpp" #endif
711
C++
.h
28
23.464286
86
0.740741
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false
1,531,664
datasets.hpp
metric-space-ai_metric/metric/utils/datasets.hpp
#include <fstream> #include <iostream> #include <regex> #include <tuple> #include <cereal/archives/binary.hpp> #include <cereal/types/vector.hpp> #include <blaze/Math.h> namespace metric { } class Datasets { public: std::tuple<std::vector<uint8_t>, std::vector<uint32_t>, std::vector<uint8_t>> getMnist(const std::string filename); template <typename T> static blaze::DynamicMatrix<T> readDenseMatrixFromFile(const std::string filepath); static std::tuple<std::vector<uint32_t>, std::vector<uint8_t>> loadImages(const std::string imagesListFilename); private: static std::regex getDelimiterAndSetDecimal(std::string &string); template <typename T> static blaze::DynamicVector<T, blaze::rowVector> getRowFromStrings(const std::vector<std::string> &stringElements); }; std::tuple<std::vector<uint8_t>, std::vector<uint32_t>, std::vector<uint8_t>> inline Datasets::getMnist( const std::string filename) { std::ifstream dataFile(filename, std::ifstream::binary); if (dataFile) { cereal::BinaryInputArchive ia(dataFile); std::vector<uint8_t> labels; std::vector<uint32_t> shape; std::vector<uint8_t> features; ia(labels, shape, features); return {labels, shape, features}; } else { std::cout << "Could not open " << filename << std::endl; return {{}, {}, {}}; } } inline std::regex Datasets::getDelimiterAndSetDecimal(std::string &string) { std::regex delimiter; std::regex decimal; bool dotFound = (string.find('.') != std::string::npos); bool commaFound = (string.find(',') != std::string::npos); std::regex r("\\s*;\\s*"); if (std::regex_search(string, r)) { if (commaFound) { string = std::regex_replace(string, std::regex(","), "."); } return r; } if (commaFound) { if (dotFound) { return std::regex(R"(\s*,\s*)"); } else { if (std::regex_search(string, std::regex(R"(\s+)"))) { if (std::regex_search(string, std::regex(R"(\s+,\s+|,\S+,)"))) { return std::regex(R"(\s*,\s*)"); } else { string = std::regex_replace(string, std::regex(","), "."); return std::regex(R"(\s+)"); } } else { return std::regex(R"(\s*,\s*)"); } } } else { return std::regex(R"(\s+)"); } } template <typename T> blaze::DynamicMatrix<T> Datasets::readDenseMatrixFromFile(const std::string filepath) { /* Open file */ std::ifstream file(filepath); std::string line; /* Count rows */ auto rowsNumber = std::count(std::istreambuf_iterator<char>(file), std::istreambuf_iterator<char>(), '\n'); file.seekg(0); /* Reserve rows */ std::vector<blaze::DynamicVector<T, blaze::rowVector>> rows; rows.reserve(rowsNumber); while (std::getline(file, line)) { /* Trim */ line = std::regex_replace(line, std::regex(R"(^\s+|\s+$)"), ""); line = std::regex_replace(line, std::regex(R"(^\[|\]$)"), ""); /* Get delimiter and replace decimal char to dot (if needs) */ auto delimiter = getDelimiterAndSetDecimal(line); /* Split */ std::sregex_token_iterator first{line.begin(), line.end(), delimiter, -1}; std::vector<std::string> row{first, {}}; rows.push_back(getRowFromStrings<T>(row)); } /* Construct matrix */ blaze::DynamicMatrix<T> matrix(rows.size(), rows[0].size()); for (size_t i = 0; i < matrix.rows(); ++i) { blaze::row(matrix, i) = rows[i]; } return matrix; } template <typename T> blaze::DynamicVector<T, blaze::rowVector> Datasets::getRowFromStrings(const std::vector<std::string> &stringElements) { blaze::DynamicVector<T, blaze::rowVector> row(stringElements.size()); for (size_t i = 0; i < row.size(); ++i) { row[i] = std::stod(stringElements[i]); } return row; } inline std::tuple<std::vector<uint32_t>, std::vector<uint8_t>> Datasets::loadImages(const std::string imagesListFilename) { /* Open imagesList file */ std::ifstream imagesListFile(imagesListFilename); if (imagesListFile.is_open()) { /* Init shape */ std::vector<uint32_t> shape = {0, 0, 0, 1}; std::vector<uint8_t> data; std::string line; while (getline(imagesListFile, line)) { std::ifstream imageFile(line); if (!imageFile.is_open()) { std::cout << "Could not open " << line << std::endl; continue; } /* Check format */ std::string format; imageFile >> format; if (format != "P5") { std::cout << "Format of " << line << " is not P6" << std::endl; continue; } /* Read and check resolution */ uint32_t width, height; imageFile >> width >> height; imageFile.close(); /* Load binary image data */ imageFile.open(line, std::ifstream::binary); size_t dataSize = width * height * sizeof(uint8_t); imageFile.seekg(-dataSize, imageFile.end); std::vector<uint8_t> imageData(width * height); imageFile.read(reinterpret_cast<char *>(imageData.data()), dataSize); /* Copy to general container */ data.insert(data.end(), imageData.begin(), imageData.end()); ++shape[0]; shape[1] = width; shape[2] = height; std::cout << line << std::endl; } if (shape[0] * shape[1] * shape[2] * shape[3] != data.size()) { std::cout << "All images must have the same shape" << std::endl; return {{}, {}}; } else { return {shape, data}; } } else { std::cout << "Could not open " << imagesListFilename << std::endl; return {{}, {}}; } }
5,248
C++
.h
156
30.660256
117
0.661386
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
false
false
1,531,666
metric_err.hpp
metric-space-ai_metric/metric/utils/metric_err.hpp
#include <cmath> template <typename T> std::tuple<double, double> stats(std::vector<T> v) { size_t n = v.size(); double mean = 0; for (size_t i = 0; i < n; ++i) { mean += v[i]; } mean = mean / (double)n; double dev = 0; for (size_t i = 0; i < n; ++i) { double diff = v[i] - mean; dev += diff * diff; } dev = sqrt(dev); return std::make_tuple(mean, dev); } template <typename Metric, typename Container> typename Metric::distance_type norm(Container obj) { // now support only STL containers and k-related metrics size_t len = obj.size(); Container zero_obj = Container(len, 0); Metric metric; return metric(obj, zero_obj); } template <typename Metric, typename Container> typename Metric::distance_type distance(Container obj1, Container obj2) { Metric metric; return metric(obj1, obj2); } template <typename Metric, typename Container> typename Metric::distance_type normalized_error(Container original, Container predicted) { return distance<Metric>(original, predicted) / norm<Metric>(original); } // correct only for vectors of equal size!! template <typename Metric, typename Container, template <typename, typename> class OuterContainer, typename OuterAllocator> std::vector<typename Metric::distance_type> normalized_errors(OuterContainer<Container, OuterAllocator> original, OuterContainer<Container, OuterAllocator> predicted) { std::vector<typename Metric::distance_type> errors; for (size_t i = 0; i < predicted.size(); ++i) { auto err = normalized_error<Metric>(original[i], predicted[i]); errors.push_back(err); } return errors; } // correct only for vectors of equal size!! template <typename Metric, typename Container, template <typename, typename> class OuterContainer, typename OuterAllocator> std::tuple<double, double, double, double, double, double> normalized_err_stats(OuterContainer<Container, OuterAllocator> original, OuterContainer<Container, OuterAllocator> predicted) { std::vector<typename Metric::distance_type> norms; std::vector<typename Metric::distance_type> errors; std::vector<typename Metric::distance_type> normalized_errors; for (size_t i = 0; i < predicted.size(); ++i) { Container cropped_original(original[i].begin(), original[i].begin() + predicted[i].size()); // auto norm_original = norm<Metric>(original[i]); auto norm_original = norm<Metric>(cropped_original); auto err = distance<Metric>(original[i], predicted[i]); norms.push_back(norm_original); errors.push_back(err); normalized_errors.push_back(err / norm_original); } // mean and stddev for each vector auto norm_stats = stats(norms); auto err_stats = stats(errors); auto n_err_stats = stats(normalized_errors); return std::make_tuple(std::get<0>(norm_stats), std::get<1>(norm_stats), std::get<0>(err_stats), std::get<1>(err_stats), std::get<0>(n_err_stats), std::get<1>(n_err_stats)); }
2,900
C++
.h
71
38.323944
118
0.730824
metric-space-ai/metric
34
14
44
MPL-2.0
9/20/2024, 10:43:29 PM (Europe/Amsterdam)
false
false
false
false
false
false
true
false