Skip to content

Commit 2b97674

Browse files
authored
Disallow scalar operands when using the matmul operator (#307)
1 parent 69430af commit 2b97674

File tree

1 file changed

+17
-33
lines changed

1 file changed

+17
-33
lines changed

spec/API_specification/type_promotion.md

+17-33
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44

55
> Array API specification for type promotion rules.
66
7-
Type promotion rules can be understood at a high level from the following
8-
diagram:
7+
Type promotion rules can be understood at a high level from the following diagram:
98

109
![Type promotion diagram](/_static/images/dtype_promotion_lattice.png)
1110

@@ -19,17 +18,12 @@ A conforming implementation of the array API standard must implement the followi
1918
A conforming implementation of the array API standard may support additional type promotion rules beyond those described in this specification.
2019

2120
```{note}
22-
23-
Type codes are used here to keep tables readable; they are not part of the standard.
24-
In code, use the data type objects specified in {ref}`data-types` (e.g., `int16` rather than `'i2'`).
21+
Type codes are used here to keep tables readable; they are not part of the standard. In code, use the data type objects specified in {ref}`data-types` (e.g., `int16` rather than `'i2'`).
2522
```
2623

2724
<!-- Note: please keep table columns aligned -->
2825

29-
The following type promotion tables specify the casting behavior for
30-
operations involving two array operands. When more than two array operands
31-
participate, application of the promotion tables is associative (i.e., the
32-
result does not depend on operand order).
26+
The following type promotion tables specify the casting behavior for operations involving two array operands. When more than two array operands participate, application of the promotion tables is associative (i.e., the result does not depend on operand order).
3327

3428
### Signed integer type promotion table
3529

@@ -90,39 +84,29 @@ where
9084
- Type promotion of non-numerical data types to numerical data types is unspecified (e.g., `bool` to `intxx` or `floatxx`).
9185

9286
```{note}
93-
94-
Mixed integer and floating-point type promotion rules are not specified
95-
because behavior varies between implementations.
87+
Mixed integer and floating-point type promotion rules are not specified because behavior varies between implementations.
9688
```
9789

9890
### Mixing arrays with Python scalars
9991

100-
Using Python scalars (i.e., instances of `bool`, `int`, `float`) together with
101-
arrays must be supported for:
92+
Using Python scalars (i.e., instances of `bool`, `int`, `float`) together with arrays must be supported for:
10293

103-
- `array <op> scalar`
104-
- `scalar <op> array`
94+
- `array <op> scalar`
95+
- `scalar <op> array`
10596

106-
where `<op>` is a built-in operator, including in-place operators (see
107-
{ref}`operators` for operators supported by the array object) and `scalar` has
108-
a compatible type and value to the array dtype:
109-
- Python `bool` for a `bool` array dtype,
110-
- a Python `int` within the [bounds](data-types) of the given dtype for integer array dtypes,
111-
- a Python `int` or `float` for floating-point array dtypes
97+
where `<op>` is a built-in operator (including in-place operators, but excluding the matmul `@` operator; see {ref}`operators` for operators supported by the array object) and `scalar` has a type and value compatible with the array data type:
11298

113-
The expected behavior is then equivalent to:
99+
- a Python `bool` for a `bool` array data type.
100+
- a Python `int` within the [bounds](data-types) of the given data type for integer array data types.
101+
- a Python `int` or `float` for floating-point array data types.
114102

115-
1. Convert the scalar to a 0-D array with the same dtype as that of the array
116-
used in the expression.
117-
2. Execute the operation for `array <op> 0-D array` (or `0-D array <op>
118-
array` if `scalar` was the left-hand argument).
103+
Provided the above requirements are met, the expected behavior is equivalent to:
119104

120-
```{note}
105+
1. Convert the scalar to zero-dimensional array with the same data type as that of the array used in the expression.
106+
2. Execute the operation for `array <op> 0-D array` (or `0-D array <op> array` if `scalar` was the left-hand argument).
121107

122-
Behaviour is not specified when mixing a Python `float` and an array with an
123-
integer dtype; this may give `float32`, `float64`, or raise an exception -
124-
behavior of implementations will differ.
108+
```{note}
109+
Behavior is not specified when mixing a Python `float` and an array with an integer data type; this may give `float32`, `float64`, or raise an exception. Behavior is implementation-specific.
125110
126-
The behavior is also not specified for integers outside of the bounds of a
127-
given integer dtype. It may overflow, or result in an error.
111+
The behavior is also not specified for integers outside of the bounds of a given integer data type. Integers outside of bounds may result in overflow or an error.
128112
```

0 commit comments

Comments
 (0)