chihaya
Validating delayed array operations in HDF5
Loading...
Searching...
No Matches
unary_math.hpp
Go to the documentation of this file.
1#ifndef CHIHAYA_UNARY_MATH_HPP
2#define CHIHAYA_UNARY_MATH_HPP
3
4#include "H5Cpp.h"
5#include "ritsuko/ritsuko.hpp"
6
7#include <stdexcept>
8#include <exception>
9#include <string>
10
11#include "utils_public.hpp"
12#include "utils_misc.hpp"
13#include "utils_nary.hpp"
14
20namespace chihaya {
21
34inline ArrayDetails validate_unary_math(const H5::Group& group, const ritsuko::Version& version, const Options& options) {
35 ArrayDetails seed_details;
36 try {
37 auto shandle = group.openGroup("seed");
38 seed_details = validate(shandle, version, options);
39 } catch (...) {
40 std::throw_with_nested(std::runtime_error("failed to validate 'seed'"));
41 }
42 if (seed_details.type == STRING) {
43 throw std::runtime_error("type of 'seed' should be integer, float or boolean");
44 }
45
46 std::string method;
47 try {
48 auto mhandle = group.openDataSet("method");
49 method = read_scalar_string_dataset(mhandle);
50 } catch (...) {
51 std::throw_with_nested(std::runtime_error("failed to validate 'method'"));
52 }
53
54 if (method == "sign") {
55 seed_details.type = INTEGER;
56
57 } else if (method == "abs") {
58 seed_details.type = std::max(seed_details.type, INTEGER);
59
60 } else if (method == "log") {
61 if (!options.details_only) {
62 if (group.exists("base")) {
63 try {
64 auto vhandle = group.openDataSet("base");
65 if (vhandle.getSpace().getSimpleExtentNdims() != 0) {
66 throw std::runtime_error("expected a scalar dataset");
67 }
68 if (version.lt(1, 1, 0)) {
69 if (vhandle.getTypeClass() != H5T_FLOAT) {
70 throw std::runtime_error("expected a floating-point number");
71 }
72 } else {
73 if (ritsuko::hdf5::exceeds_float_limit(vhandle, 64)) {
74 throw std::runtime_error("expected a datatype that fits into a 64-bit float");
75 }
76 }
77 } catch (...) {
78 std::throw_with_nested(std::runtime_error("failed to validate 'base'"));
79 }
80 }
81 }
82 seed_details.type = FLOAT;
83
84 } else if (method == "round" || method == "signif") {
85 if (!options.details_only) {
86 try {
87 auto vhandle = group.openDataSet("digits");
88 if (vhandle.getSpace().getSimpleExtentNdims() != 0) {
89 throw std::runtime_error("'expected a scalar dataset");
90 }
91
92 if (version.lt(1, 1, 0)) {
93 if (vhandle.getTypeClass() != H5T_INTEGER) {
94 throw std::runtime_error("expected an integer");
95 }
96 } else {
97 if (ritsuko::hdf5::exceeds_integer_limit(vhandle, 32, true)) {
98 throw std::runtime_error("expected a datatype that fits into a 32-bit signed integer");
99 }
100 }
101 } catch (...) {
102 std::throw_with_nested(std::runtime_error("failed to validate 'digits'"));
103 }
104 }
105 seed_details.type = FLOAT;
106
107 } else if (is_other_math(method)) {
108 seed_details.type = FLOAT;
109
110 } else {
111 throw std::runtime_error("unrecognized operation '" + method + "' in 'method'");
112 }
113
114 return seed_details;
115}
116
117}
118
119#endif
Namespace for all chihaya functions.
Definition binary_arithmetic.hpp:21
ArrayDetails validate(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition validate.hpp:96
ArrayDetails validate_unary_math(const H5::Group &group, const ritsuko::Version &version, const Options &options)
Definition unary_math.hpp:34
Details about an array.
Definition utils_public.hpp:38
ArrayType type
Definition utils_public.hpp:52
Options for validate().
Definition utils_public.hpp:79
bool details_only
Definition utils_public.hpp:84
Various public utilities.