Skip to content

Commit 74c3715

Browse files
mcb5637bigerl
andauthored
Feature: add trigonometry & exponential funcs (#401)
--------- Co-authored-by: bigerl <alexander@bigerl.eu>
1 parent e50fe3a commit 74c3715

File tree

3 files changed

+388
-1
lines changed

3 files changed

+388
-1
lines changed

src/rdf4cpp/Literal.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2588,6 +2588,79 @@ Literal lang_matches(Literal const &lang_tag, Literal const &lang_range, storage
25882588
auto const res = lang_matches(lang_tag.lexical_form(), lang_range.lexical_form());
25892589
return Literal::make_boolean(res, lang_tag.select_node_storage(node_storage));
25902590
}
2591+
Literal Literal::math_pi(storage::DynNodeStoragePtr node_storage) {
2592+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::numbers::pi, node_storage);
2593+
}
2594+
2595+
#define CPP_DOUBLE_OR_RETURN_NULL(source_expr, var_name) \
2596+
auto const var_name##_opt = [&source_obj = (source_expr)]() -> std::optional<double> { \
2597+
if (source_obj.null()) { \
2598+
return std::nullopt; \
2599+
} \
2600+
if (!source_obj.datatype_eq<datatypes::xsd::Double>()) { \
2601+
return source_obj.cast_to_value<datatypes::xsd::Double>(); \
2602+
} \
2603+
return source_obj.value<datatypes::xsd::Double>(); \
2604+
}(); \
2605+
if (!var_name##_opt.has_value()) { \
2606+
return {}; \
2607+
} \
2608+
auto const var_name = *var_name##_opt;
2609+
2610+
Literal Literal::math_exp(storage::DynNodeStoragePtr node_storage) const {
2611+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2612+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::exp(th), select_node_storage(node_storage));
2613+
}
2614+
Literal Literal::math_exp10(storage::DynNodeStoragePtr node_storage) const {
2615+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2616+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::pow(10.0, th), select_node_storage(node_storage));
2617+
}
2618+
Literal Literal::math_log(storage::DynNodeStoragePtr node_storage) const {
2619+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2620+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::log(th), select_node_storage(node_storage));
2621+
}
2622+
Literal Literal::math_log10(storage::DynNodeStoragePtr node_storage) const {
2623+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2624+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::log10(th), select_node_storage(node_storage));
2625+
}
2626+
Literal Literal::math_pow(Literal exp, storage::DynNodeStoragePtr node_storage) const {
2627+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2628+
CPP_DOUBLE_OR_RETURN_NULL(exp, ex);
2629+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::pow(th, ex), select_node_storage(node_storage));
2630+
}
2631+
Literal Literal::math_sqrt(storage::DynNodeStoragePtr node_storage) const {
2632+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2633+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::sqrt(th), select_node_storage(node_storage));
2634+
}
2635+
Literal Literal::math_sin(storage::DynNodeStoragePtr node_storage) const {
2636+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2637+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::sin(th), select_node_storage(node_storage));
2638+
}
2639+
Literal Literal::math_cos(storage::DynNodeStoragePtr node_storage) const {
2640+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2641+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::cos(th), select_node_storage(node_storage));
2642+
}
2643+
Literal Literal::math_tan(storage::DynNodeStoragePtr node_storage) const {
2644+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2645+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::tan(th), select_node_storage(node_storage));
2646+
}
2647+
Literal Literal::math_asin(storage::DynNodeStoragePtr node_storage) const {
2648+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2649+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::asin(th), select_node_storage(node_storage));
2650+
}
2651+
Literal Literal::math_acos(storage::DynNodeStoragePtr node_storage) const {
2652+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2653+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::acos(th), select_node_storage(node_storage));
2654+
}
2655+
Literal Literal::math_atan(storage::DynNodeStoragePtr node_storage) const {
2656+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2657+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::atan(th), select_node_storage(node_storage));
2658+
}
2659+
Literal Literal::math_atan2(Literal y, storage::DynNodeStoragePtr node_storage) const {
2660+
CPP_DOUBLE_OR_RETURN_NULL(*this, th);
2661+
CPP_DOUBLE_OR_RETURN_NULL(y, yd);
2662+
return Literal::make_typed_from_value<datatypes::xsd::Double>(std::atan2(th, yd), select_node_storage(node_storage));
2663+
}
25912664

25922665
inline namespace shorthands {
25932666

src/rdf4cpp/Literal.hpp

Lines changed: 86 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -751,7 +751,7 @@ struct Literal : Node {
751751

752752
if constexpr (std::same_as<T, Boolean>) {
753753
// any -> bool
754-
TriBool t = this->ebv();
754+
TriBool const t = this->ebv();
755755
if (t == TriBool::Err)
756756
return std::nullopt;
757757
else if (t == TriBool::True)
@@ -1560,6 +1560,91 @@ struct Literal : Node {
15601560

15611561
friend struct Node;
15621562
friend Literal lang_matches(Literal const &lang_tag, Literal const &lang_range, storage::DynNodeStoragePtr node_storage) noexcept;
1563+
1564+
/**
1565+
* https://www.w3.org/TR/xpath-functions/#func-math-pi
1566+
* @return std::numbers::pi as xsd::Double
1567+
*/
1568+
[[nodiscard]] static Literal math_pi(storage::DynNodeStoragePtr node_storage = storage::default_node_storage);
1569+
/**
1570+
* https://www.w3.org/TR/xpath-functions/#func-math-exp
1571+
* returns null, if this is not of type xsd::Double.
1572+
* @return pow(e, this)
1573+
*/
1574+
[[nodiscard]] Literal math_exp(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1575+
/**
1576+
* https://www.w3.org/TR/xpath-functions/#func-math-exp10
1577+
* returns null, if this is not of type xsd::Double.
1578+
* @return pow(10, this)
1579+
*/
1580+
[[nodiscard]] Literal math_exp10(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1581+
/**
1582+
* https://www.w3.org/TR/xpath-functions/#func-math-log
1583+
* returns null, if this is not of type xsd::Double.
1584+
* @return log(e, this)
1585+
*/
1586+
[[nodiscard]] Literal math_log(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1587+
/**
1588+
* https://www.w3.org/TR/xpath-functions/#func-math-log10
1589+
* returns null, if this is not of type xsd::Double.
1590+
* @return log(10, this)
1591+
*/
1592+
[[nodiscard]] Literal math_log10(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1593+
/**
1594+
* https://www.w3.org/TR/xpath-functions/#func-math-pow
1595+
* returns null, if this is not of type xsd::Double or exp not numeric.
1596+
* @return pow(this, exp)
1597+
*/
1598+
[[nodiscard]] Literal math_pow(Literal exp, storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1599+
/**
1600+
* https://www.w3.org/TR/xpath-functions/#func-math-sqrt
1601+
* returns null, if this is not of type xsd::Double.
1602+
* @return sqrt(this)
1603+
*/
1604+
[[nodiscard]] Literal math_sqrt(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1605+
1606+
/**
1607+
* https://www.w3.org/TR/xpath-functions/#func-math-sin
1608+
* returns null, if this is not of type xsd::Double.
1609+
* @return sin(this)
1610+
*/
1611+
[[nodiscard]] Literal math_sin(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1612+
/**
1613+
* https://www.w3.org/TR/xpath-functions/#func-math-cos
1614+
* returns null, if this is not of type xsd::Double.
1615+
* @return cos(this)
1616+
*/
1617+
[[nodiscard]] Literal math_cos(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1618+
/**
1619+
* https://www.w3.org/TR/xpath-functions/#func-math-tan
1620+
* returns null, if this is not of type xsd::Double.
1621+
* @return tan(this)
1622+
*/
1623+
[[nodiscard]] Literal math_tan(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1624+
/**
1625+
* https://www.w3.org/TR/xpath-functions/#func-math-asin
1626+
* returns null, if this is not of type xsd::Double.
1627+
* @return asin(this)
1628+
*/
1629+
[[nodiscard]] Literal math_asin(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1630+
/**
1631+
* https://www.w3.org/TR/xpath-functions/#func-math-acos
1632+
* returns null, if this is not of type xsd::Double.
1633+
* @return acos(this)
1634+
*/
1635+
[[nodiscard]] Literal math_acos(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1636+
/**
1637+
* https://www.w3.org/TR/xpath-functions/#func-math-atan
1638+
* returns null, if this is not of type xsd::Double.
1639+
* @return atan(this)
1640+
*/
1641+
[[nodiscard]] Literal math_atan(storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
1642+
/**
1643+
* https://www.w3.org/TR/xpath-functions/#func-math-atan2
1644+
* returns null, if this or y is not of type xsd::Double.
1645+
* @return atan2(this, y)
1646+
*/
1647+
[[nodiscard]] Literal math_atan2(Literal y, storage::DynNodeStoragePtr node_storage = keep_node_storage) const;
15631648
};
15641649

15651650
/**

0 commit comments

Comments
 (0)