lib/osqp/matrix.rb in osqp-0.2.1 vs lib/osqp/matrix.rb in osqp-0.2.2

- old
+ new

@@ -9,18 +9,20 @@ end def []=(row_index, column_index, value) raise IndexError, "row index out of bounds" if row_index < 0 || row_index >= @m raise IndexError, "column index out of bounds" if column_index < 0 || column_index >= @n + # dictionary of keys, optimized for converting to CSC + # TODO try COO for performance if value == 0 (@data[column_index] ||= {}).delete(row_index) else (@data[column_index] ||= {})[row_index] = value end end - def to_ptr + def to_csc cx = [] ci = [] cp = [] # CSC format @@ -33,18 +35,24 @@ end # cumulative column values cp << cx.size end - nnz = cx.size - cx = Utils.float_array(cx) - ci = Utils.int_array(ci) - cp = Utils.int_array(cp) + { + start: cp, + index: ci, + value: cx + } + end - ptr = FFI.csc_matrix(m, n, nnz, cx, ci, cp) - # save refs - ptr.instance_variable_set(:@osqp_refs, [cx, ci, cp]) - ptr + # private, for tests + def nnz + @data.sum { |_, v| v.count } + end + + def initialize_copy(other) + super + @data = @data.transform_values(&:dup) end def self.from_dense(data) data = data.to_a m = data.size