Should array-broadcast and array-insert-axis return only immutable array results? Bradley Lucier (11 Oct 2025 18:31 UTC)

Should array-broadcast and array-insert-axis return only immutable array results? Bradley Lucier 11 Oct 2025 18:31 UTC

I wrote a longish post to the Racket Discourse discussing the question:

Should array-broadcast and array-insert-axis return only immutable array
results?

that I include here (without the formatting and links).

Any opinions?

Brad

TL;DR: Should array-broadcast and array-insert-axis return only
immutable array results?

NumPy has deprecated allowing a broadcasted array be mutable
("writable"), probably because, in general, changing one element of a
broadcasted array changes other elements. See numpy.broadcast_arrays —
NumPy v2.3 Manual . Strictly speaking, this restriction is not needed
when the broadcast result has no more elements than the argument.

math/array has a similar routine, array-broadcast, 6.10 Pointwise
Operations , on which I modeled my own version.

I think NumPy's restriction is a good idea, so I thought I would have my
array-broadcast return only immutable arrays.

NumPy also has the function numpy.expand_dims, which adds an axis of
width 1 to its array argument. The result of numpy.expand_dims has the
same elements as the argument, in the same order, so there is no need to
restrict the result to be immutable, and indeed NumPy does not restrict
the result to be immutable.

math/array has the similar (array-axis-insert arr k [dk]), which adds a
new axis with an optional parameter that specifies an axis width greater
than 1.

I modeled the my new array-axis-insert on math/array's procedure.

The question is whether arrays returned by my array-axis-insert should
be immutable, or should be immutable only when dk>1, to use math/array's
notation.

One could similarly ask whether the result of array-broadcast should be
immutable only when there are more elements in the result than in the
argument. Perhaps a similar question led to the following design
decision in math/array: 6.10 Pointwise Operations

     When array-strictness is #t, array-broadcast returns a strict array
when arr is nonstrict and the result has more elements than arr.