Skip to content

StateSpace with all SparseArrays converts to dense on creation #311

@imciner2

Description

@imciner2

Trying to create a StateSpace object with all sparse arrays for A, B, C and D leads to the actual StateSpace object having dense matrices instead of sparse ones. If all the matrices given are sparse, then the default statespace type should preserve the sparsity of them. This is extremely important when the system is large and has a lot of zeros in the A matrix.

Reproducing script:

A = spzeros( 2, 2)
A[1,1] = 1
A[2,2] = 1

B = spzeros( 2, 1 )
B[2, 1] = 1

C = spzeros( 1, 2 )
C[1,1] = 1

D = spzeros( 1, 1 )

s = StateSpace( A, B, C, D )
issparse( s.A )

Sample output:

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

julia> A[1,1] = 1
       A[2,2] = 1
1

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

julia> B[2, 1] = 1
1

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

julia> C[1,1] = 1
1

julia> D = spzeros( 1, 1 )
1×1 SparseMatrixCSC{Float64,Int64} with 0 stored entries

julia> A
2×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries:
  [1, 1]  =  1.0
  [2, 2]  =  1.0

julia> B
2×1 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [2, 1]  =  1.0

julia> C
1×2 SparseMatrixCSC{Float64,Int64} with 1 stored entry:
  [1, 1]  =  1.0

julia> D
1×1 SparseMatrixCSC{Float64,Int64} with 0 stored entries

julia> s = StateSpace( A, B, C, D )
StateSpace{Float64,Array{Float64,2}}
A =
 1.0  0.0
 0.0  1.0
B =
 0.0
 1.0
C =
 1.0  0.0
D =
 0.0

Continuous-time state-space model

julia> s.A
2×2 Array{Float64,2}:
 1.0  0.0
 0.0  1.0

julia> issparse( s.A )
false

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughacktoberfesthttps://discourse.julialang.org/t/hacktoberfest-2020-x-the-julia-language/46927help wanted

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions