Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions src/rdf4cpp/Literal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2588,6 +2588,79 @@ Literal lang_matches(Literal const &lang_tag, Literal const &lang_range, storage
auto const res = lang_matches(lang_tag.lexical_form(), lang_range.lexical_form());
return Literal::make_boolean(res, lang_tag.select_node_storage(node_storage));
}
Literal Literal::math_pi(storage::DynNodeStoragePtr node_storage) {
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::numbers::pi, node_storage);
}

#define CPP_DOUBLE_OR_RETURN_NULL(source_expr, var_name) \
auto const var_name##_opt = [&source_obj = (source_expr)]() -> std::optional<double> { \
if (source_obj.null()) { \
return std::nullopt; \
} \
if (!source_obj.datatype_eq<datatypes::xsd::Double>()) { \
return source_obj.cast_to_value<datatypes::xsd::Double>(); \
} \
return source_obj.value<datatypes::xsd::Double>(); \
}(); \
if (!var_name##_opt.has_value()) { \
return {}; \
} \
auto const var_name = *var_name##_opt;

Literal Literal::math_exp(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::exp(th), select_node_storage(node_storage));
}
Literal Literal::math_exp10(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::pow(10.0, th), select_node_storage(node_storage));
}
Literal Literal::math_log(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::log(th), select_node_storage(node_storage));
}
Literal Literal::math_log10(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::log10(th), select_node_storage(node_storage));
}
Literal Literal::math_pow(Literal exp, storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
CPP_DOUBLE_OR_RETURN_NULL(exp, ex);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::pow(th, ex), select_node_storage(node_storage));
}
Literal Literal::math_sqrt(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::sqrt(th), select_node_storage(node_storage));
}
Literal Literal::math_sin(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::sin(th), select_node_storage(node_storage));
}
Literal Literal::math_cos(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::cos(th), select_node_storage(node_storage));
}
Literal Literal::math_tan(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::tan(th), select_node_storage(node_storage));
}
Literal Literal::math_asin(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::asin(th), select_node_storage(node_storage));
}
Literal Literal::math_acos(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::acos(th), select_node_storage(node_storage));
}
Literal Literal::math_atan(storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::atan(th), select_node_storage(node_storage));
}
Literal Literal::math_atan2(Literal y, storage::DynNodeStoragePtr node_storage) const {
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
CPP_DOUBLE_OR_RETURN_NULL(y, yd);
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::atan2(th, yd), select_node_storage(node_storage));
}

inline namespace shorthands {

Expand Down
87 changes: 86 additions & 1 deletion src/rdf4cpp/Literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ struct Literal : Node {

if constexpr (std::same_as<T, Boolean>) {
// any -> bool
TriBool t = this->ebv();
TriBool const t = this->ebv();
if (t == TriBool::Err)
return std::nullopt;
else if (t == TriBool::True)
Expand Down Expand Up @@ -1560,6 +1560,91 @@ struct Literal : Node {

friend struct Node;
friend Literal lang_matches(Literal const &lang_tag, Literal const &lang_range, storage::DynNodeStoragePtr node_storage) noexcept;

/**
* https://www.w3.org/TR/xpath-functions/#func-math-pi
* @return std::numbers::pi as xsd::Double
*/
[[nodiscard]] static Literal math_pi(storage::DynNodeStoragePtr node_storage = storage::default_node_storage);
/**
* https://www.w3.org/TR/xpath-functions/#func-math-exp
* returns null, if this is not of type xsd::Double.
* @return pow(e, this)
*/
[[nodiscard]] Literal math_exp(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-exp10
* returns null, if this is not of type xsd::Double.
* @return pow(10, this)
*/
[[nodiscard]] Literal math_exp10(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-log
* returns null, if this is not of type xsd::Double.
* @return log(e, this)
*/
[[nodiscard]] Literal math_log(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-log10
* returns null, if this is not of type xsd::Double.
* @return log(10, this)
*/
[[nodiscard]] Literal math_log10(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-pow
* returns null, if this is not of type xsd::Double or exp not numeric.
* @return pow(this, exp)
*/
[[nodiscard]] Literal math_pow(Literal exp, storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-sqrt
* returns null, if this is not of type xsd::Double.
* @return sqrt(this)
*/
[[nodiscard]] Literal math_sqrt(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;

/**
* https://www.w3.org/TR/xpath-functions/#func-math-sin
* returns null, if this is not of type xsd::Double.
* @return sin(this)
*/
[[nodiscard]] Literal math_sin(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-cos
* returns null, if this is not of type xsd::Double.
* @return cos(this)
*/
[[nodiscard]] Literal math_cos(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-tan
* returns null, if this is not of type xsd::Double.
* @return tan(this)
*/
[[nodiscard]] Literal math_tan(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-asin
* returns null, if this is not of type xsd::Double.
* @return asin(this)
*/
[[nodiscard]] Literal math_asin(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-acos
* returns null, if this is not of type xsd::Double.
* @return acos(this)
*/
[[nodiscard]] Literal math_acos(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-atan
* returns null, if this is not of type xsd::Double.
* @return atan(this)
*/
[[nodiscard]] Literal math_atan(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
/**
* https://www.w3.org/TR/xpath-functions/#func-math-atan2
* returns null, if this or y is not of type xsd::Double.
* @return atan2(this, y)
*/
[[nodiscard]] Literal math_atan2(Literal y, storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
};

/**
Expand Down
Loading
Loading