Skip to content

Commit

Permalink
Add is_nan, is_finite and is_inf.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ivorforce committed Nov 11, 2024
1 parent 7a5edee commit 35b3ecb
Show file tree
Hide file tree
Showing 8 changed files with 135 additions and 0 deletions.
23 changes: 23 additions & 0 deletions doc_classes/nd.xml
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,29 @@
The tolerance values are positive, typically very small numbers. The relative difference (rtol * abs(b)) and the absolute difference atol are added together to compare against the absolute difference between a and b.
</description>
</method>
<method name="is_finite" qualifiers="static">
<return type="NDArray" />
<param index="0" name="a" type="Variant" />
<description>
Test element-wise for finiteness (not infinity and not Not a Number).
The result is returned as a boolean array.
</description>
</method>
<method name="is_inf" qualifiers="static">
<return type="NDArray" />
<param index="0" name="a" type="Variant" />
<description>
Test element-wise for positive or negative infinity.
Returns a boolean array of the same shape as x, True where x == +/-inf, otherwise False.
</description>
</method>
<method name="is_nan" qualifiers="static">
<return type="NDArray" />
<param index="0" name="a" type="Variant" />
<description>
Test element-wise for NaN and return result as a boolean array.
</description>
</method>
<method name="less" qualifiers="static">
<return type="NDArray" />
<param index="0" name="a" type="Variant" />
Expand Down
46 changes: 46 additions & 0 deletions docs/classes/class_nd.rst
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,12 @@ Methods
+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`NDArray<class_NDArray>` | :ref:`is_close<class_nd_method_is_close>`\ (\ a\: ``Variant``, b\: ``Variant``, rtol\: ``float`` = 1e-05, atol\: ``float`` = 1e-08, equal_nan\: ``bool`` = false\ ) |static| |
+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`NDArray<class_NDArray>` | :ref:`is_finite<class_nd_method_is_finite>`\ (\ a\: ``Variant``\ ) |static| |
+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`NDArray<class_NDArray>` | :ref:`is_inf<class_nd_method_is_inf>`\ (\ a\: ``Variant``\ ) |static| |
+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`NDArray<class_NDArray>` | :ref:`is_nan<class_nd_method_is_nan>`\ (\ a\: ``Variant``\ ) |static| |
+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`NDArray<class_NDArray>` | :ref:`less<class_nd_method_less>`\ (\ a\: ``Variant``, b\: ``Variant``\ ) |static| |
+------------------------------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| :ref:`NDArray<class_NDArray>` | :ref:`less_equal<class_nd_method_less_equal>`\ (\ a\: ``Variant``, b\: ``Variant``\ ) |static| |
Expand Down Expand Up @@ -1339,6 +1345,46 @@ The tolerance values are positive, typically very small numbers. The relative di

----

.. _class_nd_method_is_finite:

.. rst-class:: classref-method

:ref:`NDArray<class_NDArray>` **is_finite**\ (\ a\: ``Variant``\ ) |static| :ref:`🔗<class_nd_method_is_finite>`

Test element-wise for finiteness (not infinity and not Not a Number).

The result is returned as a boolean array.

.. rst-class:: classref-item-separator

----

.. _class_nd_method_is_inf:

.. rst-class:: classref-method

:ref:`NDArray<class_NDArray>` **is_inf**\ (\ a\: ``Variant``\ ) |static| :ref:`🔗<class_nd_method_is_inf>`

Test element-wise for positive or negative infinity.

Returns a boolean array of the same shape as x, True where x == +/-inf, otherwise False.

.. rst-class:: classref-item-separator

----

.. _class_nd_method_is_nan:

.. rst-class:: classref-method

:ref:`NDArray<class_NDArray>` **is_nan**\ (\ a\: ``Variant``\ ) |static| :ref:`🔗<class_nd_method_is_nan>`

Test element-wise for NaN and return result as a boolean array.

.. rst-class:: classref-item-separator

----

.. _class_nd_method_less:

.. rst-class:: classref-method
Expand Down
1 change: 1 addition & 0 deletions docs/setup/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Upcoming Changes (main branch)
- Added matrix ``diagonal``, ``diag`` and ``trace`` functions.
- Added ``transpose`` and ``flatten`` to ``NDArray`` methods.
- Added ``is_close``, ``array_equal`` and ``all_close`` functions.
- Added ``is_nan``, ``is_inf`` and ``is_finite`` functions.

**Changed**

Expand Down
15 changes: 15 additions & 0 deletions src/nd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ void nd::_bind_methods() {
godot::ClassDB::bind_static_method("nd", D_METHOD("less", "a", "b"), &nd::less);
godot::ClassDB::bind_static_method("nd", D_METHOD("less_equal", "a", "b"), &nd::less_equal);
godot::ClassDB::bind_static_method("nd", D_METHOD("is_close", "a", "b", "rtol", "atol", "equal_nan"), &nd::is_close, DEFVAL(1e-05), DEFVAL(1e-08), DEFVAL(false));
godot::ClassDB::bind_static_method("nd", D_METHOD("is_nan", "a"), &nd::is_nan);
godot::ClassDB::bind_static_method("nd", D_METHOD("is_inf", "a"), &nd::is_inf);
godot::ClassDB::bind_static_method("nd", D_METHOD("is_finite", "a"), &nd::is_finite);

godot::ClassDB::bind_static_method("nd", D_METHOD("logical_and", "a", "b"), &nd::logical_and);
godot::ClassDB::bind_static_method("nd", D_METHOD("logical_or", "a", "b"), &nd::logical_or);
Expand Down Expand Up @@ -1098,6 +1101,18 @@ Ref<NDArray> nd::is_close(const Variant& a, const Variant& b, double_t rtol, dou
}, a, b);
}

Ref<NDArray> nd::is_nan(const Variant& a) {
return VARRAY_MAP1(is_nan, a);
}

Ref<NDArray> nd::is_inf(const Variant& a) {
return VARRAY_MAP1(is_inf, a);
}

Ref<NDArray> nd::is_finite(const Variant& a) {
return VARRAY_MAP1(is_finite, a);
}

Ref<NDArray> nd::logical_and(const Variant& a, const Variant& b) {
return VARRAY_MAP2(logical_and, a, b);
}
Expand Down
3 changes: 3 additions & 0 deletions src/nd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ class nd : public Object {
static Ref<NDArray> less(const Variant& a, const Variant& b);
static Ref<NDArray> less_equal(const Variant& a, const Variant& b);
static Ref<NDArray> is_close(const Variant& a, const Variant& b, double_t rtol, double_t atol, bool equal_nan);
static Ref<NDArray> is_nan(const Variant& a);
static Ref<NDArray> is_inf(const Variant& a);
static Ref<NDArray> is_finite(const Variant& a);

// Logical.
static Ref<NDArray> logical_and(const Variant& a, const Variant& b);
Expand Down
36 changes: 36 additions & 0 deletions src/vatensor/comparison.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,42 @@ void va::is_close(VStoreAllocator& allocator, VArrayTarget target, const VData&
::is_close(allocator, target, a, b, rtol, atol, equal_nan);
}

void va::is_nan(VStoreAllocator& allocator, VArrayTarget target, const VData& a) {
va::xoperation_inplace<
Feature::is_nan,
promote::num_in_nat_out
>(
va::XFunction<xt::math::isnan_fun> {},
allocator,
target,
a
);
}

void va::is_finite(VStoreAllocator& allocator, VArrayTarget target, const VData& a) {
va::xoperation_inplace<
Feature::is_finite,
promote::num_in_nat_out
>(
va::XFunction<xt::math::isfinite_fun> {},
allocator,
target,
a
);
}

void va::is_inf(VStoreAllocator& allocator, VArrayTarget target, const VData& a) {
va::xoperation_inplace<
Feature::is_inf,
promote::num_in_nat_out
>(
va::XFunction<xt::math::isinf_fun> {},
allocator,
target,
a
);
}

bool va::array_equal(const VData& a, const VData& b) {
return va::vreduce<
Feature::array_equal,
Expand Down
4 changes: 4 additions & 0 deletions src/vatensor/comparison.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace va {
void less_equal(VStoreAllocator& allocator, VArrayTarget target, const VData& a, const VData& b);
void is_close(VStoreAllocator& allocator, VArrayTarget target, const VData& a, const VData& b, double rtol = 1e-05, double atol = 1e-08, bool equal_nan = false);

void is_nan(VStoreAllocator& allocator, VArrayTarget target, const VData& a);
void is_finite(VStoreAllocator& allocator, VArrayTarget target, const VData& a);
void is_inf(VStoreAllocator& allocator, VArrayTarget target, const VData& a);

bool array_equal(const VData& a, const VData& b);
bool all_close(const VData& a, const VData& b, double rtol = 1e-05, double atol = 1e-08, bool equal_nan = false);
}
Expand Down
7 changes: 7 additions & 0 deletions src/vatensor/vfeature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace va {
less,
less_equal,
is_close,
is_nan,
is_inf,
is_finite,
array_equal,
all_close,

Expand Down Expand Up @@ -132,6 +135,10 @@ namespace va {
FEATURE_NAME_CASE(greater_equal)
FEATURE_NAME_CASE(less)
FEATURE_NAME_CASE(less_equal)
FEATURE_NAME_CASE(is_close)
FEATURE_NAME_CASE(is_nan)
FEATURE_NAME_CASE(is_inf)
FEATURE_NAME_CASE(is_finite)
FEATURE_NAME_CASE(array_equal)
FEATURE_NAME_CASE(all_close)

Expand Down

0 comments on commit 35b3ecb

Please sign in to comment.