Julia: normalize(a) for multidimensional arrays

Created on 2 Jan 2020  Â·  2Comments  Â·  Source: JuliaLang/julia

Currently norm is definined for arbitrary dimension Arrays, while normalize only works on vectors,

arr = collect(reshape(1:6, (2,3)))
norm(arr)      # works
normalize!(arr) # throws
normalize(arr) # throws

I find this odd and it bites me every now and then. For instance I often want to normalize before plotting.
Can we have normalize/normalize! methods that works on AbstractArray (or ducktyped method on Any)?

good first issue help wanted linear algebra

Most helpful comment

I suspect that this is a relic of the days prior to #27401 when norm only worked on 1d arrays. Now that norm works on multi-dimensional arrays, I agree that normalize should as well.

Should be an easy change to this code:

  1. Change the type signatures and documentation from AbstractVector to AbstractArray

  2. Change this line from v[1] to first(v). (This is a bug anyway, since v[1] is wrong for non 1-based arrays.)

  3. Change the documentation to refer to "array" rather than "vector" and use a rather than v.

  4. Add a few tests.

  5. Add a NEWS item.

A PR would be welcome and should be easy even for newcomers.

All 2 comments

I suspect that this is a relic of the days prior to #27401 when norm only worked on 1d arrays. Now that norm works on multi-dimensional arrays, I agree that normalize should as well.

Should be an easy change to this code:

  1. Change the type signatures and documentation from AbstractVector to AbstractArray

  2. Change this line from v[1] to first(v). (This is a bug anyway, since v[1] is wrong for non 1-based arrays.)

  3. Change the documentation to refer to "array" rather than "vector" and use a rather than v.

  4. Add a few tests.

  5. Add a NEWS item.

A PR would be welcome and should be easy even for newcomers.

Seems like this was closed in 8f9dd5d.

Was this page helpful?
0 / 5 - 0 ratings