Skip to content

build_function with sparse array inputs fail if nrow != ncol #791

@celliern

Description

@celliern

build_function with sparse array seems to fail if the number of rows and cols are different.

Two differents behaviour observed :

nrow > ncol show an error

>>> using ModelingToolkits, SparseArrays

>>> @variables x, y, z
>>> A = [x y
               x x
               0 0]
>>> sparseA = sparse(A)
>>> eval(build_function(A, x, y)[1])(1.0, 2.0)
3×2 Array{Float64,2}:
 1.0  2.0
 1.0  1.0
 0.0  0.0
>>> eval(build_function(sparseA, x, y)[1])(1.0, 2.0) |> Array
ERROR: LoadError: ArgumentError: 3 == length(colptr) < 4

where nrow < ncol create a sparse array which size is (now, nrow) :

>>> A = [x y 0
               x x y]
>>> sparseA = sparse(A)

>>> eval(build_function(A, x, y)[1])(1.0, 2.0)
2×3 Array{Float64,2}:
 1.0  2.0  0.0
 1.0  1.0  2.0

>>> eval(build_function(sparseA, x, y)[1])(1.0, 2.0) |> Array
2×2 Array{Float64,2}:
 1.0  2.0
 1.0  1.0

The in place form of the built function is interesting :

>>> build_function(sparseA, x, y)[2]
:((var"##MTIIPVar#3429", var"##MTKArg#3426", var"##MTKArg#3427")->begin
          @inbounds let (x, y) = (var"##MTKArg#3426", var"##MTKArg#3427")
                  (var"##MTIIPVar#3429").nzval[1] = x
                  (var"##MTIIPVar#3429").nzval[2] = x
                  (var"##MTIIPVar#3429").nzval[3] = y
                  (var"##MTIIPVar#3429").nzval[4] = x
              end
          nothing
      end)

It has only 4 nval (it should have 6) : I think that somewhere during the analysis of the sparse matrix the number of row is used instead the number of columns. This may be linked to #755.

I will try to investigate more deeply to see where it occurs, but I'm not that familiar with the library : I will do my best.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions