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

@system should not convert matrices to Matrix #320

Open
schillic opened this issue Jan 11, 2025 · 3 comments
Open

@system should not convert matrices to Matrix #320

schillic opened this issue Jan 11, 2025 · 3 comments

Comments

@schillic
Copy link
Member

Currently @system calls hcat on every matrix, which yields a Matrix even if the input already was an AbstractMatrix.

if state == var
push!(params, (Expr(:call, :hcat, array), :A))
# obtain "state_dim" for later using in IdentityMultiple
state_dim = Expr(:call, :size, :($array), 1)
got_state_dim = true
num_state_assignments += 1
elseif input == var
push!(params, (Expr(:call, :hcat, array), :B))
num_input_assignments += 1
elseif noise == var
push!(params, (Expr(:call, :hcat, array), :D))

I think this is not good because you may for instance want to work with a sparse matrix.

This change was added in #175 with the motivation to handle scalar inputs.

A simple solution would be to check whether the input has type AbstractMatrix and in that case not call hcat.

This would be mildly breaking.

@blegat
Copy link
Member

blegat commented Jan 13, 2025

I'm not sure to understand, hcat does not turn sparse matrices into dense ones:

julia> using SparseArrays

julia> A = spzeros(2, 2)
2×2 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
       
       

julia> hcat(A)
2×2 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
       
       

julia> hcat(A, A)
2×4 SparseMatrixCSC{Float64, Int64} with 0 stored entries:
               
              

@schillic
Copy link
Member Author

Indeed! I saw that it converts an IdentityMultiple (defined in this library). So I guess it does that for AbstractMatrix when no custom hcat method is implemented.

julia> A = Id(2)
IdentityMultiple{Float64} of value 1.0 and order 2

julia> hcat(A)
2×2 Matrix{Float64}:
 1.0  0.0
 0.0  1.0

@blegat
Copy link
Member

blegat commented Jan 13, 2025

I see, I guess the use of this hcat is to turn vectors to matrices so we can just create a vector_to_matrix function that calls hcat on AbstractVector and does nothing on matrices

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

No branches or pull requests

2 participants