Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eachrow, eachcol, and reshape missing #1376

Open
mgkurtz opened this issue May 30, 2023 · 4 comments
Open

eachrow, eachcol, and reshape missing #1376

mgkurtz opened this issue May 30, 2023 · 4 comments

Comments

@mgkurtz
Copy link
Contributor

mgkurtz commented May 30, 2023

I personally prefer a functional programming style. Thus I am missing some functions to deal with matrices. Notably missing are eachrow, eachcol, and reshape. We could copy the first two from Base as

Base.eachrow(a::MatrixElem) = (view(a, i, :) for i in axes(a, 1))
Base.eachcol(a::MatrixElem) = (view(a, :, i) for i in axes(a, 2))

As view returns 1×n respectively m×1-matrices (instead of Vectors like it does for Matrix) the resulting eachrow and eachcol will accordingly also be somewhat inconsistent to their behavior for Matrix, but consistent with all other functions dealing with MatrixElem.

Reshaping with shared data would need support from the appropriate MatElem implementations. A flat copying reshape instead would be possible using the existing matrix interface. Just create a similar matrix with new size and fill in the values from the old matrix.

@fieker
Copy link
Contributor

fieker commented May 30, 2023 via email

@mgkurtz
Copy link
Contributor Author

mgkurtz commented May 31, 2023

Sure, I can come along Thursday or Friday. A usage example for eachrow and reshape would be the task to LLL-reduce a Vector of matrices:

lll(a::Vector{ZZMatrix}) = isempty(a) ? a : frommatrix(lll(asmatrix(a)), size(a[1])...)

asmatrix(v::Vector{T}) :: T where T <: MatElem = reduce(vcat, reshape.(v, 1, :))

frommatrix(a::T, r::Int, c::Int) :: Vector{T} where T <: MatElem = [reshape(x, r, c) for x in eachrow(a)]

@fieker
Copy link
Contributor

fieker commented May 31, 2023 via email

@fieker
Copy link
Contributor

fieker commented May 31, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants