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#include "ritsuko/hdf5/hdf5.hpp"
7
8#include <stdexcept>
9#include <vector>
10#include <string>
11
12#include "utils_unary.hpp"
13#include "utils_misc.hpp"
14#include "utils_public.hpp"
15
21namespace chihaya {
22
27namespace unary_math {
28
37inline ArrayDetails validate(const H5::Group& handle, const ritsuko::Version& version, Options& options) {
38 auto seed_details = internal_misc::load_seed_details(handle, "seed", version, options);
39 if (seed_details.type == STRING) {
40 throw std::runtime_error("type of 'seed' should be integer, float or boolean");
41 }
42
43 // Checking the method.
44 auto method = internal_unary::load_method(handle);
45 if (method == "sign") {
46 seed_details.type = INTEGER;
47
48 } else if (method == "abs") {
49 seed_details.type = std::max(seed_details.type, INTEGER);
50
51 } else if (
52 method == "log1p" ||
53 method == "sqrt" ||
54 method == "exp" ||
55 method == "expm1" ||
56 method == "ceiling" ||
57 method == "floor" ||
58 method == "trunc" ||
59 method == "sin" ||
60 method == "cos" ||
61 method == "tan" ||
62 method == "acos" ||
63 method == "asin" ||
64 method == "atan" ||
65 method == "sinh" ||
66 method == "cosh" ||
67 method == "tanh" ||
68 method == "acosh" ||
69 method == "asinh" ||
70 method == "atanh")
71 {
72 seed_details.type = FLOAT;
73
74 } else if (method == "log") {
75 if (!options.details_only) {
76 if (handle.exists("base")) {
77 if (handle.childObjType("base") != H5O_TYPE_DATASET) {
78 throw std::runtime_error("expected 'base' to be a dataset for a log transformation");
79 }
80 auto vhandle = handle.openDataSet("base");
81 if (!ritsuko::hdf5::is_scalar(vhandle)) {
82 throw std::runtime_error("'base' should be a scalar");
83 }
84
85 if (version.lt(1, 1, 0)) {
86 if (vhandle.getTypeClass() != H5T_FLOAT) {
87 throw std::runtime_error("'base' should be a floating-point number");
88 }
89 } else {
90 if (ritsuko::hdf5::exceeds_float_limit(vhandle, 64)) {
91 throw std::runtime_error("'base' should have a datatype that fits into a 64-bit float");
92 }
93 }
94 }
95 }
96 seed_details.type = FLOAT;
97
98 } else if (method == "round" || method == "signif") {
99 if (!options.details_only) {
100 auto vhandle = ritsuko::hdf5::open_dataset(handle, "digits");
101 if (!ritsuko::hdf5::is_scalar(vhandle)) {
102 throw std::runtime_error("'digits' should be a scalar");
103 }
104
105 if (version.lt(1, 1, 0)) {
106 if (vhandle.getTypeClass() != H5T_INTEGER) {
107 throw std::runtime_error("'digits' should be an integer");
108 }
109 } else {
110 if (ritsuko::hdf5::exceeds_integer_limit(vhandle, 32, true)) {
111 throw std::runtime_error("'digits' should have a datatype that fits into a 32-bit signed integer");
112 }
113 }
114 }
115 seed_details.type = FLOAT;
116
117 } else {
118 throw std::runtime_error("unrecognized operation in 'method' (got '" + method + "')");
119 }
120
121 return seed_details;
122}
123
124}
125
126}
127
128#endif
ArrayDetails validate(const H5::Group &handle, const ritsuko::Version &version, Options &options)
Definition: unary_math.hpp:37
Namespace for all chihaya functions.
Definition: binary_arithmetic.hpp:22
Details about an array.
Definition: utils_public.hpp:36
Validation options.
Definition: utils_public.hpp:66
bool details_only
Definition: utils_public.hpp:71
Various public utilities.