Skip to content

Add __int__, __float__, and __bool__ to the array object #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2021
Merged
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
55 changes: 54 additions & 1 deletion spec/API_specification/array_object.md
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ Transpose of the array.
Calculates the absolute value for each element `x_i` of an array instance `x` (i.e., the element-wise result has the same magnitude as the respective element in `x` but has positive sign).

#### Special Cases

For floating-point operands,

- If `x_i` is `NaN`, the result is `NaN`.
Expand Down Expand Up @@ -377,6 +377,24 @@ Evaluates `x1_i & x2_i` for each element `x1_i` of an array instance `x1` with t
Element-wise results must equal the results returned by the equivalent element-wise function [`bitwise_and(x1, x2)`](elementwise_functions.md#logical_andx1-x2-).
```

(method-__bool__)=
### \_\_bool\_\_(x, /)

Converts a zero-dimensional boolean array to a Python `bool` object.

#### Parameters

- **x**: _<array>_

- zero-dimensional array instance. Must have a boolean data type.

#### Returns

- **out**: _<bool>_

- a Python `bool` object representing the single element of the array `x`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What should the behavior be if the array does not meet the requirement (no zero-dimensional, or different type)?.

Does the standard care whether ValueError or TypeError are raised?

NumPy can raise both:

In [2]: np.__version__
Out[2]: '1.20.0'

In [3]: X = np.zeros((2,2),dtype='d')

In [4]: X.__bool__
Out[4]: <method-wrapper '__bool__' of numpy.ndarray object at 0x2b67bdea50f0>

In [5]: X.__bool__()
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-5-2acecd4ea032> in <module>
----> 1 X.__bool__()

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

In [6]: X.__float__()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-78817c2ae92b> in <module>
----> 1 X.__float__()

TypeError: only size-1 arrays can be converted to Python scalars

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the standard care whether ValueError or TypeError are raised?

In general we've refrained from specifying this, because (a) error handling is broad, (b) existing libraries are very inconsistent, and (c) there are situations where error handling strategy for accelerators may be different from that on CPU on purpose.



(method-__dlpack__)=
### \_\_dlpack\_\_(/, *, stream=None)

Expand Down Expand Up @@ -419,6 +437,7 @@ Exports the array as a DLPack capsule, for consumption by {ref}`function-from_dl

- A DLPack capsule for the array. See {ref}`data-interchange` for details.


(method-__dlpack_device__)=
### \_\_dlpack\_device\_\_()

Expand Down Expand Up @@ -467,6 +486,23 @@ Computes the truth value of `x1_i == x2_i` for each element `x1_i` of an array i
Element-wise results must equal the results returned by the equivalent element-wise function [`equal(x1, x2)`](elementwise_functions.md#equalx1-x2-).
```

(method-__float__)=
### \_\_float\_\_(x, /)

Converts a zero-dimensional floating-point array to a Python `float` object.

#### Parameters

- **x**: _&lt;array&gt;_

- zero-dimensional array instance. Must have a floating-point data type.

#### Returns

- **out**: _&lt;float&gt;_

- a Python `float` object representing the single element of the array `x`.

(method-__floordiv__)=
### \_\_floordiv\_\_(x1, x2, /)

Expand Down Expand Up @@ -550,6 +586,23 @@ Computes the truth value of `x1_i > x2_i` for each element `x1_i` of an array in
Element-wise results must equal the results returned by the equivalent element-wise function [`greater(x1, x2)`](elementwise_functions.md#greaterx1-x2-).
```

(method-__int__)=
### \_\_int\_\_(x, /)

Converts a zero-dimensional integer array to a Python `int` object.

#### Parameters

- **x**: _&lt;array&gt;_

- zero-dimensional array instance. Must have an integer data type.

#### Returns

- **out**: _&lt;int&gt;_

- a Python `int` object representing the single element of the array `x`.

(method-__invert__)=
### \_\_invert\_\_(x, /)

Expand Down