question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Issues with "Mixing arrays and Python scalars" section

See original GitHub issue

A few issues with the mixing arrays and Python scalars section that came up from testing:

  • The matmul operator (@) does not (and should not) support scalar types, so it should be excluded.
  • Should boolean arrays be required to work with arithmetic operators. NumPy gives errors in some cases, for example:
>>> import numpy as np
>>> np.array(False) - False
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead. 
  • How should casting work? For example, int8 array + large Python int. Should it cast the int, or give an error. Or should this be unspecified.
  • The above question also applies for Python float -> float32 casting, which is implicitly supported by the text that is currently there, but is unsafe in general (is there a “safe” variant of float64 -> float32, or is it always unsafe? Either way, this would be value based casting, which I thought we were trying to avoid).

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

1reaction
shoyercommented, Dec 19, 2020

Unsigned integer overflow is technically undefined behavior in C/C++, and that seems to work out OK. It’s not great, but I think these inconsistencies are mostly an indication that nobody expects Python’s big ints to do something sensible when combined with fixed size arrays.

1reaction
shoyercommented, Dec 17, 2020

Right, we intentionally deviated from NumPy here. There is no notion of separate “scalar” types, and 0d arrays are not treated differently from other arrays.

On Wed, Dec 16, 2020 at 5:29 PM Aaron Meurer notifications@github.com wrote:

Yeah, the behavior is the same whether it’s an “array scalar” or “scalar constant” (or whatever NumPy calls them). But we don’t make any such distinction in the spec, and don’t special-case 0-D arrays as far as type promotion is concerned:

Type promotion rules must apply when determining the common result type for two array operands during an arithmetic operation, regardless of array dimension. Accordingly, zero-dimensional arrays must be subject to the same type promotion rules as dimensional arrays.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/data-apis/array-api/issues/98#issuecomment-747144919, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJJFVUXHRC3JRSUHYQIS6DSVFNGJANCNFSM4UODP77Q .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Future NumPy behavior when mixing arrays, NumPy scalars ...
In this poll we ask for your opinion on how NumPy should work when arrays, NumPy scalars, and Python scalars are mixed.
Read more >
deprecate scalar conversions for rank>0 arrays · Issue #10404
I'm fine with rank-0 conversions, but I found that discrimination of conversion based on array size can easily lead to inconsistencies ...
Read more >
numpy - "Only size-1 arrays can be converted to Python scalars"
A python scalar is just one number. An array with 2 numbers can be converted into just one, can it? But which line...
Read more >
NEP 50 — Promotion rules for Python scalars - NumPy
The reason is that value-based promotion is disabled when all objects are scalars or 0-D arrays. NumPy thus returns the same type as...
Read more >
Chapter 4. NumPy Basics: Arrays and Vectorized Computation
Operations between Arrays and Scalars ... Arrays are important because they enable you to express batch operations on data without writing any for...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found