Create a 1D flat array and 2D diagonal-offset/position view on all right-left diagonals to an NxM 2D array

I am struggling using Python numpy to reverse the right-left diagonals of an array and to convert them to a special diagonal items arrangement … How can this be done in Julia? Or what would be the simple formulas of obtaining the indices representing a 1D and 2D view ( in terms of diagonal index plus index of position in the diagonal ) on 2D right-left diagonals for forth and back operations?

https://docs.julialang.org/en/v1/stdlib/LinearAlgebra/#LinearAlgebra.diagind

a la

  reverse!(@view A[diagind(A, d)])

See also Linear Algebra · The Julia Language

The detail missed in your reply seems to be that the diagonals need to be the right to left ones instead of the usual main left-to-right top-down diagonals.

Create a reversed (dims=2) view of the matrix first.

(Easier said than done?)

What is a view? How to get the indices of the view in the underlying array? What does reversing an array ( reversed view) mean? I mean to know that flipping the array right to left makes it necessary to reverse the got diagonal to get it right … and writing the modified diagonal back from 1D array of stacked diagonals to the original array hard to accomplish …

https://docs.julialang.org/en/v1/base/arrays/#Views-(SubArrays-and-other-view-types)

julia> A=ans
3×4 reshape(::UnitRange{Int64}, 3, 4) with eltype Int64:
 1  4  7  10
 2  5  8  11
 3  6  9  12

julia> Aview = @view A[:,reverse(1:n)]
3×4 view(reshape(::UnitRange{Int64}, 3, 4), :, 4:-1:1) with eltype Int64:
 10  7  4  1
 11  8  5  2
 12  9  6  3

Now Aview is backed by A but appears to have its columns in reverse order.

Disregard the reshape. I just used that to create a comprehensible matrix.

Does this do what you need?

julia> A=collect(A)
3×4 Matrix{Int64}:
 1  4  7  10
 2  5  8  11
 3  6  9  12

julia> Aview = @view A[:,reverse(1:n)]
3×4 view(::Matrix{Int64}, :, 4:-1:1) with eltype Int64:
 10  7  4  1
 11  8  5  2
 12  9  6  3

julia> reverse!(@view Aview[diagind(Aview, 1)])
3-element view(reshape(view(::Matrix{Int64}, :, 4:-1:1), 12), 4:4:12) with eltype Int64:
 3
 5
 7

julia> Aview
3×4 view(::Matrix{Int64}, :, 4:-1:1) with eltype Int64:
 10  3  4  1
 11  8  5  2
 12  9  6  7

julia> A
3×4 Matrix{Int64}:
 1  4  3  10
 2  5  8  11
 7  6  9  12

What does @view mean? What is Aview? How to get the indices of item [1,2] in the original array? What is n in 1:n? It is not defined …

Can a view be created without the need of creating an actual array in first place? Just by specifying the shape/dimensions of the array?

@view creates a view of the array using the indices.
https://docs.julialang.org/en/v1/base/arrays/#Base.@view
https://docs.julialang.org/en/v1/base/arrays/#Base.view

Sorry, n is 4, different from m, which is not shown and is the number of rows.

1:n is syntatic sugar for a range Mathematics · The Julia Language

No, a view is an lightweight construct from an array. It makes no sense to have a view without an array.

In other words I can’t get the target coordinates in the underlying array out of the view? What I am looking for is a way to obtain the transform function from one system of indices to another … this does not need an actual array which is only required if I want to get values out of it. Such “general view” could allow access to values of any array with same shape not being bound to an existing array with values …

It does appear that one can @view a range, which is an AbstractArray. I’m unaware of a method for diagind that works with just the dimensions of the array, or an implementation of AbstractArray that looks like a 2-dimensional view.

In other words it is necessary for my purpose to define/create own functions which when fed with indices specifying the right-left diagonal offset and the index of item in it calculate the indices to the target array with for example reversed diagonals? How would such function look like for the example of specifying x,y returning xTarget, yTarget for obtaining a value which would be of an array with reversed anti-diagonals ?

You might find something useful in the source for diagind (link below), but it relies on a Julia matrix being indexable with a single index. If you were to create your function in Julia, you could do that with the snippets from above if you implement a lazy AbstractArray whose values are the flat array indices, ie., like A above.

OK … so it is possible to index any multi-dimensional array by a single index without flatting it in first place? This means there is a native view on the flat array for each array? How to get the flat index out of a view on an array given the multi-dimensional indices?

julia> typeof(Aview)
SubArray{Int64, 2, Matrix{Int64}, Tuple{Base.Slice{Base.OneTo{Int64}}, StepRange{Int64, Int64}}, false}

SubArray is the structure for a view of an array. It contains the information for the view index mapping.

https://docs.julialang.org/en/v1/devdocs/subarrays/#SubArray-design

This means there is a native view on the flat array for each array?

Not really an explicit view, it’s just part of the implementation of an array to support linear indexing.

1 Like

By the way: do you know how it comes that Julia went the path of one-based array indices? What was the main reason for such design choice?

Bezanson said, “We have chosen 1 to be more similar to existing math software”

1 Like

OK … this seems to be a software oriented decision … I for myself and from the perspective of the oOo-system (see github and dev.to for more if interested) consider zero based indexing what screws your mind into counting apples in the basket starting with zero - what makes absolutely no sense … any street and any postal code starts from one … this is sane … not harming the mind with concepts far from reality putting screwed labels on items in a container … I suppose that the insanity which begun with C propagated itself over the history of programming languages. Nice to see that there is an option to this screwed way driving your mind crazy … By the way: WHO is Bezanson ?