List of all members | Public Types | Public Member Functions | Static Public Member Functions | Protected Member Functions | Static Private Member Functions
casadi::Sparsity Class Reference

General sparsity class. More...

#include <sparsity.hpp>

Detailed Description

The storage format is a compressed column storage (CCS) format.
In this format, the structural non-zero elements are stored in column-major order, starting from the upper left corner of the matrix and ending in the lower right corner.

In addition to the dimension (size1(), size2()), (i.e. the number of rows and the number of columns respectively), there are also two vectors of integers:

  1. "colind" [length size2()+1], which contains the index to the first non-zero element on or after the corresponding column. All the non-zero elements of a particular column i are thus the elements with index el that fulfills: colind[i] <= el < colind[i+1].
  2. "row" [same length as the number of non-zero elements, nnz()] The rows for each of the structural non-zeros.

Note that with this format, it is cheap to loop over all the non-zero elements of a particular column, at constant time per element, but expensive to jump to access a location (i, j).

If the matrix is dense, i.e. length(row) == size1()*size2(), the format reduces to standard dense column major format, which allows access to an arbitrary element in constant time.

Since the object is reference counted (it inherits from SharedObject), several matrices are allowed to share the same sparsity pattern.

The implementations of methods marked as such in this class has been taken from the CSparse package and modified to fit CasADi data structures and separation of sparsity pattern calculation and numerical evaluation. These functions are Copyright(c) Timothy A. Davis, 2006-2009 and licensed as a derivative work under the GNU LGPL

See also
Matrix
Author
Joel Andersson
Date
2010-2015

Extra doc: https://github.com/casadi/casadi/wiki/L_b9

Definition at line 103 of file sparsity.hpp.

Inheritance diagram for casadi::Sparsity:
Inheritance graph
[legend]
Collaboration diagram for casadi::Sparsity:
Collaboration graph
[legend]

Public Types

typedef SparsityInterface< SparsityB
 Base class. More...
 
using internal_base_type = SharedObjectInternal
 
using base_type = SharedObject
 

Public Member Functions

 Sparsity (casadi_int dummy=0)
 Default constructor. More...
 
 Sparsity (casadi_int nrow, casadi_int ncol)
 Pattern with all structural zeros. More...
 
 Sparsity (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &colind, const std::vector< casadi_int > &row, bool order_rows=false)
 Construct from sparsity pattern vectors given in compressed column storage format. More...
 
 Sparsity (const std::pair< casadi_int, casadi_int > &rc)
 Create a sparse matrix with all structural zeros. More...
 
 Sparsity (casadi_int nrow, casadi_int ncol, const casadi_int *colind, const casadi_int *row, bool order_rows=false)
 Construct from sparsity pattern vectors given in compressed column storage format. More...
 
SparsityInternalget () const
 
const std::vector< casadi_int > permutation_vector (bool invert=false) const
 Construct permutation vector from permutation matrix. More...
 
Sparsity get_diag (std::vector< casadi_int > &mapping) const
 
std::vector< casadi_int > compress (bool canonical=true) const
 Compress a sparsity pattern. More...
 
const SparsityInternaloperator-> () const
 Access a member function or object. More...
 
const SparsityInternaloperator* () const
 Reference to internal structure. More...
 
bool operator!= (const Sparsity &y) const
 Check if two sparsity patterns are difference. More...
 
bool is_stacked (const Sparsity &y, casadi_int n) const
 Check if pattern is horizontal repeat of another. More...
 
 operator const casadi_int * () const
 Implicit or explicit type conversion to C representation. More...
 
 operator const std::vector< casadi_int > & () const
 Implicit or explicit type conversion to compact representation. More...
 
 operator SparsityStruct () const
 Implicit or explicit type conversion to C representation. More...
 
Dict info () const
 
void to_file (const std::string &filename, const std::string &format_hint="") const
 
void serialize (std::ostream &stream) const
 Serialize. More...
 
std::string serialize () const
 Serialize. More...
 
void serialize (SerializingStream &s) const
 Serialize an object. More...
 
const casadi_int * row () const
 Get a reference to row-vector,. More...
 
const casadi_int * colind () const
 Get a reference to the colindex of all column element (see class description) More...
 
std::vector< casadi_int > get_row () const
 Get the row for each non-zero entry. More...
 
std::vector< casadi_int > get_colind () const
 Get the column index for each column. More...
 
casadi_int colind (casadi_int cc) const
 Get a reference to the colindex of column cc (see class description) More...
 
casadi_int row (casadi_int el) const
 Get the row of a non-zero element. More...
 
std::vector< casadi_int > get_col () const
 Get the column for each non-zero entry. More...
 
void resize (casadi_int nrow, casadi_int ncol)
 Resize. More...
 
casadi_int add_nz (casadi_int rr, casadi_int cc)
 Get the index of a non-zero element. More...
 
casadi_int get_nz (casadi_int rr, casadi_int cc) const
 Get the index of an existing non-zero element. More...
 
bool has_nz (casadi_int rr, casadi_int cc) const
 Returns true if the pattern has a non-zero at location rr, cc. More...
 
std::vector< casadi_int > get_nz (const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc) const
 Get a set of non-zero element. More...
 
void get_nz (std::vector< casadi_int > &indices) const
 Get the nonzero index for a set of elements. More...
 
std::vector< casadi_int > get_lower () const
 Get nonzeros in lower triangular part. More...
 
std::vector< casadi_int > get_upper () const
 Get nonzeros in upper triangular part. More...
 
void get_ccs (std::vector< casadi_int > &colind, std::vector< casadi_int > &row) const
 Get the sparsity in compressed column storage (CCS) format. More...
 
void get_crs (std::vector< casadi_int > &rowind, std::vector< casadi_int > &col) const
 Get the sparsity in compressed row storage (CRS) format. More...
 
void get_triplet (std::vector< casadi_int > &row, std::vector< casadi_int > &col) const
 Get the sparsity in sparse triplet format. More...
 
Sparsity sub (const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, std::vector< casadi_int > &mapping, bool ind1=false) const
 Get a submatrix. More...
 
Sparsity sub (const std::vector< casadi_int > &rr, const Sparsity &sp, std::vector< casadi_int > &mapping, bool ind1=false) const
 Get a set of elements. More...
 
Sparsity T () const
 Transpose the matrix. More...
 
Sparsity transpose (std::vector< casadi_int > &mapping, bool invert_mapping=false) const
 Transpose the matrix and get the reordering of the non-zero entries. More...
 
bool is_transpose (const Sparsity &y) const
 Check if the sparsity is the transpose of another. More...
 
bool is_reshape (const Sparsity &y) const
 Check if the sparsity is a reshape of another. More...
 
bool is_compactible (std::vector< casadi_int > &row, std::vector< casadi_int > &col) const
 Check if the nonzero pattern is the Cartesian product of a row and a column subset. More...
 
bool is_subset (const Sparsity &rhs) const
 Is subset? More...
 
Sparsity sparsity_cast_mod (const Sparsity &X, const Sparsity &Y) const
 Propagates subset according to sparsity cast. More...
 
Sparsity pattern_inverse () const
 Take the inverse of a sparsity pattern; flip zeros and non-zeros. More...
 
template<typename DataType >
void set (DataType *data, const DataType *val_data, const Sparsity &val_sp) const
 
template<typename DataType >
void add (DataType *data, const DataType *val_data, const Sparsity &val_sp) const
 
template<typename DataType >
void bor (DataType *data, const DataType *val_data, const Sparsity &val_sp) const
 
std::string class_name () const
 Get class name. More...
 
void disp (std::ostream &stream, bool more=false) const
 Print a description of the object. More...
 
std::string get_str (bool more=false) const
 Get string representation. More...
 
void print_ptr (std::ostream &stream=casadi::uout()) const
 
void own (SharedObjectInternal *node)
 
void assign (SharedObjectInternal *node)
 Assign the node to a node class pointer without reference counting. More...
 
casadi_int getCount () const
 Get the reference count. More...
 
void swap (GenericShared &other)
 Swap content with another instance. More...
 
std::string debug_repr () const
 
bool is_null () const
 Is a null pointer? More...
 
casadi_int __hash__ () const
 Returns a number that is unique for a given Node. More...
 
GenericWeakRef< SharedObject, SharedObjectInternal > * weak ()
 Get a weak reference to the object. More...
 
Check if two sparsity patterns are identical
bool is_equal (const Sparsity &y) const
 
bool is_equal (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &colind, const std::vector< casadi_int > &row) const
 
bool is_equal (casadi_int nrow, casadi_int ncol, const casadi_int *colind, const casadi_int *row) const
 
bool operator== (const Sparsity &y) const
 
Size and element counting
casadi_int size1 () const
 Get the number of rows. More...
 
casadi_int rows () const
 Get the number of rows, Octave-style syntax. More...
 
casadi_int size2 () const
 Get the number of columns. More...
 
casadi_int columns () const
 Get the number of columns, Octave-style syntax. More...
 
casadi_int numel () const
 The total number of elements, including structural zeros, i.e. size2()*size1() More...
 
double density () const
 The percentage of nonzero. More...
 
bool is_empty (bool both=false) const
 Check if the sparsity is empty. More...
 
casadi_int nnz () const
 Get the number of (structural) non-zeros. More...
 
casadi_int nnz_upper (bool strictly=false) const
 Number of non-zeros in the upper triangular half,. More...
 
casadi_int nnz_lower (bool strictly=false) const
 Number of non-zeros in the lower triangular half,. More...
 
casadi_int nnz_diag () const
 Number of non-zeros on the diagonal, i.e. the number of elements (i, j) with j==i. More...
 
casadi_int bw_upper () const
 Upper half-bandwidth. More...
 
casadi_int bw_lower () const
 Lower half-bandwidth. More...
 
std::pair< casadi_int, casadi_int > size () const
 Get the shape. More...
 
casadi_int size (casadi_int axis) const
 Get the size along a particular dimensions. More...
 
Sparsity combine (const Sparsity &y, bool f0x_is_zero, bool function0_is_zero, std::vector< unsigned char > &mapping) const
 Combine two sparsity patterns. More...
 
Sparsity combine (const Sparsity &y, bool f0x_is_zero, bool function0_is_zero) const
 Combine two sparsity patterns. More...
 
Sparsity unite (const Sparsity &y, std::vector< unsigned char > &mapping) const
 Union of two sparsity patterns. More...
 
Sparsity unite (const Sparsity &y) const
 Union of two sparsity patterns. More...
 
Sparsity operator+ (const Sparsity &b) const
 Union of two sparsity patterns. More...
 
Sparsity intersect (const Sparsity &y, std::vector< unsigned char > &mapping) const
 Intersection of two sparsity patterns. More...
 
Sparsity intersect (const Sparsity &y) const
 Intersection of two sparsity patterns. More...
 
Sparsity operator* (const Sparsity &b) const
 Intersection of two sparsity patterns. More...
 

Static Public Member Functions

static Sparsity create (SparsityInternal *node)
 Create from node. More...
 
static Sparsity upper (casadi_int n)
 Create a upper triangular square sparsity pattern *. More...
 
static Sparsity lower (casadi_int n)
 Create a lower triangular square sparsity pattern *. More...
 
static Sparsity band (casadi_int n, casadi_int p)
 Create a single band in a square sparsity pattern. More...
 
static Sparsity banded (casadi_int n, casadi_int p)
 Create banded square sparsity pattern. More...
 
static Sparsity rowcol (const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, casadi_int nrow, casadi_int ncol)
 Construct a block sparsity pattern from (row, col) vectors. More...
 
static Sparsity nonzeros (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &nz, bool ind1=SWIG_IND1)
 Create a sparsity from nonzeros. More...
 
static Sparsity permutation (const std::vector< casadi_int > &p, bool invert=false)
 Construct a permutation matrix P from a permutation vector p. More...
 
static Sparsity from_file (const std::string &filename, const std::string &format_hint="")
 
static Sparsity deserialize (std::istream &stream)
 Build Sparsity from serialization. More...
 
static Sparsity deserialize (const std::string &s)
 Build Sparsity from serialization. More...
 
static Sparsity deserialize (DeserializingStream &s)
 Deserialize. More...
 
static void mul_sparsityF (const bvec_t *x, const Sparsity &x_sp, const bvec_t *y, const Sparsity &y_sp, bvec_t *z, const Sparsity &z_sp, bvec_t *w)
 Propagate sparsity using 0-1 logic through a matrix product,. More...
 
static void mul_activityF (const bvec_t *x, const Sparsity &x_sp, const bvec_t *y, const Sparsity &y_sp, bvec_t *z, const Sparsity &z_sp, bvec_t *w)
 Propagate signal activity through a matrix product, forward mode. More...
 
static void mul_sparsityR (bvec_t *x, const Sparsity &x_sp, bvec_t *y, const Sparsity &y_sp, bvec_t *z, const Sparsity &z_sp, bvec_t *w)
 Propagate sparsity using 0-1 logic through a matrix product,. More...
 
static Sparsity scalar (bool dense_scalar=true)
 Create a scalar sparsity pattern *. More...
 
static Sparsity dense (casadi_int nrow, casadi_int ncol=1)
 Create a dense rectangular sparsity pattern *. More...
 
static Sparsity dense (const std::pair< casadi_int, casadi_int > &rc)
 Create a dense rectangular sparsity pattern *. More...
 
static Sparsity unit (casadi_int n, casadi_int el)
 Create the sparsity pattern for a unit vector of length n and a nonzero on. More...
 
static Sparsity diag (casadi_int nrow)
 Create diagonal sparsity pattern *. More...
 
static Sparsity diag (casadi_int nrow, casadi_int ncol)
 Create diagonal sparsity pattern *. More...
 
static Sparsity diag (const std::pair< casadi_int, casadi_int > &rc)
 Create diagonal sparsity pattern *. More...
 
static Sparsity triplet (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, std::vector< casadi_int > &mapping, bool invert_mapping)
 Create a sparsity pattern given the nonzeros in sparse triplet form *. More...
 
static Sparsity triplet (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &row, const std::vector< casadi_int > &col)
 Create a sparsity pattern given the nonzeros in sparse triplet form *. More...
 
static Sparsity compressed (const std::vector< casadi_int > &v, bool order_rows=false)
 
static Sparsity compressed (const casadi_int *v, bool order_rows=false)
 

Protected Member Functions

void count_up ()
 
void count_down ()
 

Static Private Member Functions

static std::vector< Sparsitydiagsplit (const Sparsity &x, const std::vector< casadi_int > &output_offset)
 
static std::vector< Sparsitydiagsplit (const Sparsity &x, casadi_int incr)
 
static std::vector< Sparsitydiagsplit (const Sparsity &x, casadi_int incr1, casadi_int incr2)
 
static Sparsity mtimes (const std::vector< Sparsity > &args, const std::string &blas="reference")
 
static std::vector< Sparsityhorzsplit (const Sparsity &x, casadi_int incr)
 
static std::vector< Sparsityvertsplit (const Sparsity &x, casadi_int incr)
 
typedef std::unordered_multimap< std::size_t, WeakRefCachingMap
 Enlarge matrix. More...
 
static std::set< std::string > file_formats = {"mtx"}
 Enlarge matrix. More...
 
void enlarge (casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
 Enlarge matrix. More...
 
void enlargeRows (casadi_int nrow, const std::vector< casadi_int > &rr, bool ind1=false)
 Enlarge the matrix along the first dimension (i.e. insert rows) More...
 
void enlargeColumns (casadi_int ncol, const std::vector< casadi_int > &cc, bool ind1=false)
 Enlarge the matrix along the second dimension (i.e. insert columns) More...
 
Sparsity makeDense (std::vector< casadi_int > &mapping) const
 Make a patten dense. More...
 
std::vector< casadi_int > erase (const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, bool ind1=false)
 Erase rows and/or columns of a matrix. More...
 
std::vector< casadi_int > erase (const std::vector< casadi_int > &rr, bool ind1=false)
 Erase elements of a matrix. More...
 
void append (const Sparsity &sp)
 Append another sparsity patten vertically (NOTE: only efficient if vector) More...
 
void appendColumns (const Sparsity &sp)
 Append another sparsity patten horizontally. More...
 
bool is_scalar (bool scalar_and_dense=false) const
 Is scalar? More...
 
bool is_dense () const
 Is dense? More...
 
bool is_row () const
 Check if the pattern is a row vector (i.e. size1()==1) More...
 
bool is_column () const
 Check if the pattern is a column vector (i.e. size2()==1) More...
 
bool is_vector () const
 Check if the pattern is a row or column vector. More...
 
bool is_diag () const
 Is diagonal? More...
 
bool is_square () const
 Is square? More...
 
bool is_symmetric () const
 Is symmetric? More...
 
bool is_triu (bool strictly=false) const
 Is upper triangular? More...
 
bool is_tril (bool strictly=false) const
 Is lower triangular? More...
 
bool is_singular () const
 Check whether the sparsity-pattern indicates structural singularity. More...
 
bool is_permutation () const
 Is this a permutation matrix? More...
 
bool is_selection (bool allow_empty=false) const
 Is this a selection matrix? More...
 
bool is_orthonormal (bool allow_empty=false) const
 Are both rows and columns orthonormal ? More...
 
bool is_orthonormal_rows (bool allow_empty=false) const
 Are the rows of the pattern orthonormal ? More...
 
bool is_orthonormal_columns (bool allow_empty=false) const
 Are the columns of the pattern orthonormal ? More...
 
bool rowsSequential (bool strictly=true) const
 Do the rows appear sequentially on each column. More...
 
void removeDuplicates (std::vector< casadi_int > &mapping)
 Remove duplicate entries. More...
 
std::vector< casadi_int > etree (bool ata=false) const
 Calculate the elimination tree. More...
 
Sparsity ldl (std::vector< casadi_int > &p, bool amd=true) const
 Symbolic LDL factorization. More...
 
void qr_sparse (Sparsity &V, Sparsity &R, std::vector< casadi_int > &prinv, std::vector< casadi_int > &pc, bool amd=true) const
 Symbolic QR factorization. More...
 
casadi_int dfs (casadi_int j, casadi_int top, std::vector< casadi_int > &xi, std::vector< casadi_int > &pstack, const std::vector< casadi_int > &pinv, std::vector< bool > &marked) const
 Depth-first search on the adjacency graph of the sparsity. More...
 
casadi_int scc (std::vector< casadi_int > &index, std::vector< casadi_int > &offset) const
 Find the strongly connected components of the bigraph defined by the sparsity pattern. More...
 
casadi_int btf (std::vector< casadi_int > &rowperm, std::vector< casadi_int > &colperm, std::vector< casadi_int > &rowblock, std::vector< casadi_int > &colblock, std::vector< casadi_int > &coarse_rowblock, std::vector< casadi_int > &coarse_colblock) const
 Calculate the block triangular form (BTF) More...
 
std::vector< casadi_int > amd () const
 Approximate minimal degree preordering. More...
 
void spsolve (bvec_t *X, bvec_t *B, bool tr) const
 Propagate sparsity through a linear solve. More...
 
std::vector< casadi_int > find (bool ind1=SWIG_IND1) const
 Get the location of all non-zero elements as they would appear in a Dense matrix. More...
 
void find (std::vector< casadi_int > &loc, bool ind1=false) const
 Get the location of all nonzero elements (inplace version) More...
 
Sparsity uni_coloring (const Sparsity &AT=Sparsity(), casadi_int cutoff=std::numeric_limits< casadi_int >::max()) const
 Perform a unidirectional coloring: A greedy distance-2 coloring algorithm. More...
 
Sparsity star_coloring_new (std::vector< casadi_int > &which_color, const Dict &opts=Dict()) const
 Perform a star coloring of a symmetric matrix: More...
 
Sparsity star_coloring (casadi_int ordering=1, casadi_int cutoff=std::numeric_limits< casadi_int >::max()) const
 Perform a star coloring of a symmetric matrix: More...
 
Sparsity star_coloring2 (casadi_int ordering=1, casadi_int cutoff=std::numeric_limits< casadi_int >::max()) const
 Perform a star coloring of a symmetric matrix: More...
 
std::vector< casadi_int > largest_first () const
 Order the columns by decreasing degree. More...
 
Sparsity pmult (const std::vector< casadi_int > &p, bool permute_rows=true, bool permute_columns=true, bool invert_permutation=false) const
 Permute rows and/or columns. More...
 
std::string dim (bool with_nz=false) const
 Get the dimension as a string. More...
 
std::string postfix_dim () const
 Dimension string as a postfix to a name. More...
 
std::string repr_el (casadi_int k) const
 Describe the nonzero location k as a string. More...
 
void spy (std::ostream &stream=casadi::uout()) const
 Print a textual representation of sparsity. More...
 
void spy_matlab (const std::string &mfile) const
 Generate a script for Matlab or Octave which visualizes. More...
 
void export_code (const std::string &lang, std::ostream &stream=casadi::uout(), const Dict &options=Dict()) const
 Export matrix in specific language. More...
 
std::size_t hash () const
 Enlarge matrix. More...
 
template<typename T >
void set (T *data, const T *val_data, const Sparsity &val_sp) const
 Assign the nonzero entries of one sparsity pattern to the nonzero. More...
 
template<typename T >
void add (T *data, const T *val_data, const Sparsity &val_sp) const
 Add the nonzero entries of one sparsity pattern to the nonzero entries. More...
 
template<typename T >
void bor (T *data, const T *val_data, const Sparsity &val_sp) const
 Bitwise or of the nonzero entries of one sparsity pattern and the nonzero. More...
 
static Sparsity horzcat (const std::vector< Sparsity > &sp)
 Accessed by SparsityInterface. More...
 
static Sparsity vertcat (const std::vector< Sparsity > &sp)
 Enlarge matrix. More...
 
static Sparsity blockcat (const std::vector< std::vector< Sparsity > > &v)
 Enlarge matrix. More...
 
static Sparsity diagcat (const std::vector< Sparsity > &v)
 Enlarge matrix. More...
 
static std::vector< Sparsityhorzsplit (const Sparsity &x, const std::vector< casadi_int > &offset)
 Enlarge matrix. More...
 
static std::vector< Sparsityvertsplit (const Sparsity &x, const std::vector< casadi_int > &offset)
 Enlarge matrix. More...
 
static std::vector< Sparsitydiagsplit (const Sparsity &x, const std::vector< casadi_int > &offset1, const std::vector< casadi_int > &offset2)
 Enlarge matrix. More...
 
static Sparsity mtimes (const Sparsity &x, const Sparsity &y, const std::string &blas="reference")
 Enlarge matrix. More...
 
static Sparsity mac (const Sparsity &x, const Sparsity &y, const Sparsity &z, const std::string &="reference")
 Enlarge matrix. More...
 
static Sparsity reshape (const Sparsity &x, casadi_int nrow, casadi_int ncol)
 Enlarge matrix. More...
 
static Sparsity reshape (const Sparsity &x, const Sparsity &sp)
 Enlarge matrix. More...
 
static Sparsity sparsity_cast (const Sparsity &x, const Sparsity &sp)
 Enlarge matrix. More...
 
static casadi_int sprank (const Sparsity &x)
 Enlarge matrix. More...
 
static casadi_int norm_0_mul (const Sparsity &x, const Sparsity &A)
 Enlarge matrix. More...
 
static Sparsity kron (const Sparsity &a, const Sparsity &b)
 Enlarge matrix. More...
 
static Sparsity kron_contract (const Sparsity &sp_m, const Sparsity &sp_x, bool inner)
 Output sparsity of casadi::KronContract. More...
 
static Sparsity triu (const Sparsity &x, bool includeDiagonal=true)
 Enlarge matrix. More...
 
static Sparsity tril (const Sparsity &x, bool includeDiagonal=true)
 Enlarge matrix. More...
 
static Sparsity sum2 (const Sparsity &x)
 Enlarge matrix. More...
 
static Sparsity sum1 (const Sparsity &x)
 Enlarge matrix. More...
 
static CachingMapgetCache ()
 Cached sparsity patterns. More...
 
static const SparsitygetScalar ()
 (Dense) scalar More...
 
static const SparsitygetScalarSparse ()
 (Sparse) scalar More...
 
static const SparsitygetEmpty ()
 Empty zero-by-zero. More...
 
static std::string type_name ()
 Readable name of the public class. More...
 
static bool test_cast (const SharedObjectInternal *ptr)
 Check if a particular cast is allowed. More...
 
static Sparsity kkt (const Sparsity &H, const Sparsity &J, bool with_x_diag=true, bool with_lam_g_diag=true)
 Get KKT system sparsity. More...
 
static std::string file_format (const std::string &filename, const std::string &format_hint, const std::set< std::string > &file_formats)
 Enlarge matrix. More...
 

Member Typedef Documentation

◆ B

Definition at line 138 of file sparsity.hpp.

◆ base_type

Definition at line 103 of file shared_object.hpp.

◆ CachingMap

typedef std::unordered_multimap<std::size_t, WeakRef> casadi::Sparsity::CachingMap

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 924 of file sparsity.hpp.

◆ internal_base_type

Definition at line 102 of file shared_object.hpp.

Constructor & Destructor Documentation

◆ Sparsity() [1/5]

casadi::Sparsity::Sparsity ( casadi_int  dummy = 0)
explicit

Definition at line 68 of file sparsity.cpp.

68  {
69  casadi_assert_dev(dummy==0);
70  }

Referenced by band(), banded(), compressed(), dense(), deserialize(), diag(), diagcat(), enlargeColumns(), enlargeRows(), horzcat(), horzsplit(), kkt(), kron(), kron_contract(), ldl(), lower(), permutation(), triplet(), unit(), upper(), and vertcat().

◆ Sparsity() [2/5]

casadi::Sparsity::Sparsity ( casadi_int  nrow,
casadi_int  ncol 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ba

Definition at line 78 of file sparsity.cpp.

78  {
79  casadi_assert_dev(nrow>=0);
80  casadi_assert_dev(ncol>=0);
81  std::vector<casadi_int> row, colind(ncol+1, 0);
82  assign_cached(nrow, ncol, colind, row);
83  }
const casadi_int * row() const
Get a reference to row-vector,.
Definition: sparsity.cpp:164
const casadi_int * colind() const
Get a reference to the colindex of all column element (see class description)
Definition: sparsity.cpp:168

References colind(), and row().

◆ Sparsity() [3/5]

casadi::Sparsity::Sparsity ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  colind,
const std::vector< casadi_int > &  row,
bool  order_rows = false 
)

Definition at line 92 of file sparsity.cpp.

93  {
94  casadi_assert_dev(nrow>=0);
95  casadi_assert_dev(ncol>=0);
96  assign_cached(nrow, ncol, colind, row, order_rows);
97  }

References colind(), and row().

◆ Sparsity() [4/5]

casadi::Sparsity::Sparsity ( const std::pair< casadi_int, casadi_int > &  rc)
explicit

Extra doc: https://github.com/casadi/casadi/wiki/L_bb

Definition at line 85 of file sparsity.cpp.

85  {
86  casadi_assert_dev(rc.first>=0);
87  casadi_assert_dev(rc.second>=0);
88  std::vector<casadi_int> row, colind(rc.second+1, 0);
89  assign_cached(rc.first, rc.second, colind, row);
90  }

References colind(), and row().

◆ Sparsity() [5/5]

casadi::Sparsity::Sparsity ( casadi_int  nrow,
casadi_int  ncol,
const casadi_int *  colind,
const casadi_int *  row,
bool  order_rows = false 
)

Definition at line 99 of file sparsity.cpp.

100  {
101  casadi_assert_dev(nrow>=0);
102  casadi_assert_dev(ncol>=0);
103  if (colind==nullptr || colind[ncol]==nrow*ncol) {
104  *this = dense(nrow, ncol);
105  } else {
106  std::vector<casadi_int> colindv(colind, colind+ncol+1);
107  std::vector<casadi_int> rowv(row, row+colind[ncol]);
108  assign_cached(nrow, ncol, colindv, rowv, order_rows);
109  }
110  }
static Sparsity dense(casadi_int nrow, casadi_int ncol=1)
Create a dense rectangular sparsity pattern *.
Definition: sparsity.cpp:1028

References colind(), dense(), and row().

Member Function Documentation

◆ __hash__()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::__hash__
inherited

If the Object does not point to any node, "0" is returned.

Extra doc: https://github.com/casadi/casadi/wiki/L_av

Definition at line 123 of file generic_shared_impl.hpp.

148  {
149  return reinterpret_cast<casadi_int>(get());
150  }

◆ add() [1/2]

template<typename DataType >
void casadi::Sparsity::add ( DataType *  data,
const DataType *  val_data,
const Sparsity val_sp 
) const

Definition at line 1361 of file sparsity.hpp.

1361  {
1362  // Get dimensions of this
1363  const casadi_int sz = nnz();
1364  const casadi_int sz1 = size1();
1365  const casadi_int sz2 = size2();
1366  const casadi_int nel = sz1*sz2;
1367 
1368  // Get dimensions of assigning matrix
1369  const casadi_int val_sz = val_sp.nnz();
1370  const casadi_int val_sz1 = val_sp.size1();
1371  const casadi_int val_sz2 = val_sp.size2();
1372  const casadi_int val_nel = val_sz1*val_sz2;
1373 
1374  // Check if sparsity matches
1375  if (val_sp==*this) {
1376  for (casadi_int k=0; k<sz; ++k) {
1377  data[k] += val_data[k];
1378  }
1379  } else if (this->is_empty()) {
1380  // Quick return
1381  return;
1382  } else if (val_sp.is_empty()) {
1383  // Quick return
1384  return;
1385  } else if (val_nel==1) { // if scalar
1386  if (val_sz!=0) {
1387  for (casadi_int k=0; k<sz; ++k) {
1388  data[k] += val_data[0];
1389  }
1390  }
1391  } else {
1392  // Quick return if empty
1393  if (nel==0 && val_nel==0) return;
1394 
1395  // Make sure that dimension matches
1396  casadi_assert(sz2==val_sz2 && sz1==val_sz1,
1397  "Sparsity::add<DataType>: shape mismatch. lhs is "
1398  + dim() + ", while rhs is " + val_sp.dim() + ".");
1399 
1400  // Sparsity
1401  const casadi_int* c = row();
1402  const casadi_int* rind = colind();
1403  const casadi_int* v_c = val_sp.row();
1404  const casadi_int* v_rind = val_sp.colind();
1405 
1406  // For all columns
1407  for (casadi_int i=0; i<sz2; ++i) {
1408 
1409  // Nonzero of the assigning matrix
1410  casadi_int v_el = v_rind[i];
1411 
1412  // First nonzero of the following column
1413  casadi_int v_el_end = v_rind[i+1];
1414 
1415  // Next row of the assigning matrix
1416  casadi_int v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1417 
1418  // Assign all nonzeros
1419  for (casadi_int el=rind[i]; el!=rind[i+1]; ++el) {
1420 
1421  // Get row
1422  casadi_int j=c[el];
1423 
1424  // Forward the assigning nonzero
1425  while (v_j<j) {
1426  v_el++;
1427  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1428  }
1429 
1430  // Assign nonzero
1431  if (v_j==j) {
1432  data[el] += val_data[v_el++];
1433  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1434  }
1435  }
1436  }
1437  }
1438  }
casadi_int size1() const
Get the number of rows.
Definition: sparsity.cpp:124
std::string dim(bool with_nz=false) const
Get the dimension as a string.
Definition: sparsity.cpp:588
casadi_int nnz() const
Get the number of (structural) non-zeros.
Definition: sparsity.cpp:148
casadi_int size2() const
Get the number of columns.
Definition: sparsity.cpp:128
bool is_empty(bool both=false) const
Check if the sparsity is empty.
Definition: sparsity.cpp:144

References colind(), dim(), is_empty(), nnz(), row(), size1(), and size2().

◆ add() [2/2]

template<typename T >
void casadi::Sparsity::add ( T data,
const T val_data,
const Sparsity val_sp 
) const

of another sparsity pattern

Extra doc: https://github.com/casadi/casadi/wiki/L_dm

◆ add_nz()

casadi_int casadi::Sparsity::add_nz ( casadi_int  rr,
casadi_int  cc 
)

Add the element if it does not exist and copy object if it's not unique

Extra doc: https://github.com/casadi/casadi/wiki/L_ce

Definition at line 194 of file sparsity.cpp.

194  {
195  // If negative index, count from the back
196  if (rr<0) rr += size1();
197  if (cc<0) cc += size2();
198 
199  // Check consistency
200  casadi_assert(rr>=0 && rr<size1(), "Row index out of bounds");
201  casadi_assert(cc>=0 && cc<size2(), "Column index out of bounds");
202 
203  // Quick return if matrix is dense
204  if (is_dense()) return rr+cc*size1();
205 
206  // Get sparsity pattern
207  casadi_int size1=this->size1(), size2=this->size2(), nnz=this->nnz();
208  const casadi_int *colind = this->colind(), *row = this->row();
209 
210  // Quick return if we are adding an element to the end
211  if (colind[cc]==nnz || (colind[cc+1]==nnz && row[nnz-1]<rr)) {
212  std::vector<casadi_int> rowv(nnz+1);
213  std::copy(row, row+nnz, rowv.begin());
214  rowv[nnz] = rr;
215  std::vector<casadi_int> colindv(colind, colind+size2+1);
216  for (casadi_int c=cc; c<size2; ++c) colindv[c+1]++;
217  assign_cached(size1, size2, colindv, rowv);
218  return rowv.size()-1;
219  }
220 
221  // go to the place where the element should be
222  casadi_int ind;
223  for (ind=colind[cc]; ind<colind[cc+1]; ++ind) { // better: loop from the back to the front
224  if (row[ind] == rr) {
225  return ind; // element exists
226  } else if (row[ind] > rr) {
227  break; // break at the place where the element should be added
228  }
229  }
230 
231  // insert the element
232  std::vector<casadi_int> rowv = get_row(), colindv = get_colind();
233  rowv.insert(rowv.begin()+ind, rr);
234  for (casadi_int c=cc+1; c<size2+1; ++c) colindv[c]++;
235 
236  // Return the location of the new element
237  assign_cached(size1, size2, colindv, rowv);
238  return ind;
239  }
std::vector< casadi_int > get_colind() const
Get the column index for each column.
Definition: sparsity.cpp:364
std::vector< casadi_int > get_row() const
Get the row for each non-zero entry.
Definition: sparsity.cpp:372
bool is_dense() const
Is dense?
Definition: sparsity.cpp:273

References colind(), get_colind(), get_row(), is_dense(), nnz(), row(), size1(), and size2().

◆ amd()

std::vector< casadi_int > casadi::Sparsity::amd ( ) const

Fill-reducing ordering applied to the sparsity pattern of a linear system prior to factorization. The system must be symmetric, for an unsymmetric matrix A, first form the square of the pattern, A'*A.

The implementation is a modified version of cs_amd in CSparse Copyright(c) Timothy A. Davis, 2006-2009 Licensed as a derivative work under the GNU LGPL

Extra doc: https://github.com/casadi/casadi/wiki/L_d8

Definition at line 709 of file sparsity.cpp.

709  {
710  return (*this)->amd();
711  }

Referenced by casadi::LinsolLdl::init(), ldl(), and qr_sparse().

◆ append()

void casadi::Sparsity::append ( const Sparsity sp)

Definition at line 471 of file sparsity.cpp.

471  {
472  if (sp.size1()==0 && sp.size2()==0) {
473  // Appending pattern is empty
474  return;
475  } else if (size1()==0 && size2()==0) {
476  // This is empty
477  *this = sp;
478  } else {
479  casadi_assert(size2()==sp.size2(),
480  "Sparsity::append: Dimension mismatch. "
481  "You attempt to append a shape " + sp.dim()
482  + " to a shape " + dim()
483  + ". The number of columns must match.");
484  if (sp.size1()==0) {
485  // No rows to add
486  return;
487  } else if (size1()==0) {
488  // No rows before
489  *this = sp;
490  } else if (is_column()) {
491  // Append to vector (inefficient)
492  *this = (*this)->_appendVector(*sp);
493  } else {
494  // Append to matrix (inefficient)
495  *this = vertcat({*this, sp});
496  }
497  }
498  }
static Sparsity vertcat(const std::vector< Sparsity > &sp)
Enlarge matrix.
Definition: sparsity.cpp:1551
bool is_column() const
Check if the pattern is a column vector (i.e. size2()==1)
Definition: sparsity.cpp:285

References casadi::SparsityInternal::_appendVector(), dim(), is_column(), size1(), size2(), and vertcat().

◆ appendColumns()

void casadi::Sparsity::appendColumns ( const Sparsity sp)

Definition at line 500 of file sparsity.cpp.

500  {
501  if (sp.size1()==0 && sp.size2()==0) {
502  // Appending pattern is empty
503  return;
504  } else if (size1()==0 && size2()==0) {
505  // This is empty
506  *this = sp;
507  } else {
508  casadi_assert(size1()==sp.size1(),
509  "Sparsity::appendColumns: Dimension mismatch. You attempt to "
510  "append a shape " + sp.dim() + " to a shape "
511  + dim() + ". The number of rows must match.");
512  if (sp.size2()==0) {
513  // No columns to add
514  return;
515  } else if (size2()==0) {
516  // No columns before
517  *this = sp;
518  } else {
519  // Append to matrix (expensive)
520  *this = (*this)->_appendColumns(*sp);
521  }
522  }
523  }

References casadi::SparsityInternal::_appendColumns(), dim(), size1(), and size2().

Referenced by casadi::Sqpmethod::init().

◆ assign()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::assign ( Internal *  node)
inherited

improper use will cause memory leaks!

Extra doc: https://github.com/casadi/casadi/wiki/L_at

Definition at line 96 of file generic_shared_impl.hpp.

86  {
87  node = node_;
88  }

◆ band()

Sparsity casadi::Sparsity::band ( casadi_int  n,
casadi_int  p 
)
static

band(n, 0) is equivalent to diag(n)
band(n, -1) has a band below the diagonal

Parameters
pindicate

Extra doc: https://github.com/casadi/casadi/wiki/L_bj

Definition at line 1086 of file sparsity.cpp.

1086  {
1087  casadi_assert(n>=0, "Sparsity::band expects a positive integer as argument");
1088  casadi_assert((p<0? -p : p)<n,
1089  "Sparsity::band: position of band schould be smaller then size argument");
1090 
1091  casadi_int nc = n-(p<0? -p : p);
1092 
1093  std::vector< casadi_int > row(nc);
1094 
1095  casadi_int offset = std::max(p, casadi_int(0));
1096  for (casadi_int i=0;i<nc;i++) {
1097  row[i]=i+offset;
1098  }
1099 
1100  std::vector< casadi_int > colind(n+1);
1101 
1102  offset = std::min(p, casadi_int(0));
1103  for (casadi_int i=0;i<n+1;i++) {
1104  colind[i] = std::max(std::min(i+offset, nc), casadi_int(0));
1105  }
1106 
1107  return Sparsity(n, n, colind, row);
1108 
1109  }
static std::vector< casadi_int > offset(const std::vector< Sparsity > &v, bool vert=true)
Sparsity(casadi_int dummy=0)
Default constructor.
Definition: sparsity.cpp:68

References colind(), casadi::SparsityInterface< Sparsity >::offset(), row(), and Sparsity().

Referenced by banded(), and casadi::SlicotDple::init_mem().

◆ banded()

Sparsity casadi::Sparsity::banded ( casadi_int  n,
casadi_int  p 
)
static

banded(n, 0) is equivalent to diag(n)
banded(n, 1) is tri-diagonal matrix
Extra doc: https://github.com/casadi/casadi/wiki/L_bk

Definition at line 1111 of file sparsity.cpp.

1111  {
1112  // This is not an efficient implementation
1113  Sparsity ret = Sparsity(n, n);
1114  for (casadi_int i=-p;i<=p;++i) {
1115  ret = ret + Sparsity::band(n, i);
1116  }
1117  return ret;
1118  }
static Sparsity band(casadi_int n, casadi_int p)
Create a single band in a square sparsity pattern.
Definition: sparsity.cpp:1086

References band(), and Sparsity().

◆ blockcat()

Sparsity casadi::Sparsity::blockcat ( const std::vector< std::vector< Sparsity > > &  v)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1678 of file sparsity.cpp.

1678  {
1679  std::vector< Sparsity > ret;
1680  for (casadi_int i=0; i<v.size(); ++i)
1681  ret.push_back(horzcat(v[i]));
1682  return vertcat(ret);
1683  }
static Sparsity horzcat(const std::vector< Sparsity > &sp)
Accessed by SparsityInterface.
Definition: sparsity.cpp:1408

References horzcat(), and vertcat().

Referenced by kkt().

◆ bor() [1/2]

template<typename DataType >
void casadi::Sparsity::bor ( DataType *  data,
const DataType *  val_data,
const Sparsity val_sp 
) const

Definition at line 1441 of file sparsity.hpp.

1441  {
1442  // Get dimensions of this
1443  const casadi_int sz = nnz();
1444  const casadi_int sz1 = size1();
1445  const casadi_int sz2 = size2();
1446  const casadi_int nel = sz1*sz2;
1447 
1448  // Get dimensions of assigning matrix
1449  const casadi_int val_sz = val_sp.nnz();
1450  const casadi_int val_sz1 = val_sp.size1();
1451  const casadi_int val_sz2 = val_sp.size2();
1452  const casadi_int val_nel = val_sz1*val_sz2;
1453 
1454  // Check if sparsity matches
1455  if (val_sp==*this) {
1456  for (casadi_int k=0; k<sz; ++k) {
1457  data[k] |= val_data[k];
1458  }
1459  } else if (this->is_empty()) {
1460  // Quick return
1461  return;
1462  } else if (val_sp.is_empty()) {
1463  // Quick return
1464  return;
1465  } else if (val_nel==1) { // if scalar
1466  if (val_sz!=0) {
1467  for (casadi_int k=0; k<sz; ++k) {
1468  data[k] |= val_data[0];
1469  }
1470  }
1471  } else {
1472  // Quick return if empty
1473  if (nel==0 && val_nel==0) return;
1474 
1475  // Make sure that dimension matches
1476  casadi_assert(sz2==val_sz2 && sz1==val_sz1,
1477  "Sparsity::add<DataType>: shape mismatch. lhs is "
1478  + dim() + ", while rhs is " + val_sp.dim() + ".");
1479 
1480  // Sparsity
1481  const casadi_int* c = row();
1482  const casadi_int* rind = colind();
1483  const casadi_int* v_c = val_sp.row();
1484  const casadi_int* v_rind = val_sp.colind();
1485 
1486  // For all columns
1487  for (casadi_int i=0; i<sz2; ++i) {
1488 
1489  // Nonzero of the assigning matrix
1490  casadi_int v_el = v_rind[i];
1491 
1492  // First nonzero of the following column
1493  casadi_int v_el_end = v_rind[i+1];
1494 
1495  // Next row of the assigning matrix
1496  casadi_int v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1497 
1498  // Assign all nonzeros
1499  for (casadi_int el=rind[i]; el!=rind[i+1]; ++el) {
1500 
1501  // Get row
1502  casadi_int j=c[el];
1503 
1504  // Forward the assigning nonzero
1505  while (v_j<j) {
1506  v_el++;
1507  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1508  }
1509 
1510  // Assign nonzero
1511  if (v_j==j) {
1512  data[el] |= val_data[v_el++];
1513  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1514  }
1515  }
1516  }
1517  }
1518  }

References colind(), dim(), is_empty(), nnz(), row(), size1(), and size2().

◆ bor() [2/2]

template<typename T >
void casadi::Sparsity::bor ( T data,
const T val_data,
const Sparsity val_sp 
) const

entries of another sparsity pattern

Extra doc: https://github.com/casadi/casadi/wiki/L_dn

Referenced by casadi::Project::sp_reverse().

◆ btf()

casadi_int casadi::Sparsity::btf ( std::vector< casadi_int > &  rowperm,
std::vector< casadi_int > &  colperm,
std::vector< casadi_int > &  rowblock,
std::vector< casadi_int > &  colblock,
std::vector< casadi_int > &  coarse_rowblock,
std::vector< casadi_int > &  coarse_colblock 
) const

See Direct Methods for Sparse Linear Systems by Davis (2006).

The function computes the Dulmage-Mendelsohn decomposition, which allows you to reorder the rows and columns of a matrix to bring it into block triangular form (BTF).

It will not consider the distance of off-diagonal elements to the diagonal: there is no guarantee you will get a block-diagonal matrix if you supply a randomly permuted block-diagonal matrix.

If your matrix is symmetrical, this method is of limited use; permutation can make it non-symmetric.

See also
scc

The implementation is a modified version of cs_dmperm in CSparse Copyright(c) Timothy A. Davis, 2006-2009 Licensed as a derivative work under the GNU LGPL

Extra doc: https://github.com/casadi/casadi/wiki/L_d7

Examples
matrix/btf.py.

Definition at line 713 of file sparsity.cpp.

716  {
717  try {
718  return (*this)->btf(rowperm, colperm, rowblock, colblock,
719  coarse_rowblock, coarse_colblock);
720  } catch (std::exception &e) {
721  CASADI_THROW_ERROR("btf", e.what());
722  }
723  }

Referenced by casadi::SymbolicQr::det(), casadi::SymbolicQr::init(), and sprank().

◆ bw_lower()

casadi_int casadi::Sparsity::bw_lower ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_by

Definition at line 1404 of file sparsity.cpp.

1404  {
1405  return (*this)->bw_lower();
1406  }

◆ bw_upper()

casadi_int casadi::Sparsity::bw_upper ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_bx

Definition at line 1400 of file sparsity.cpp.

1400  {
1401  return (*this)->bw_upper();
1402  }

◆ class_name()

std::string casadi::SharedObject::class_name ( ) const
inherited

◆ colind() [1/2]

const casadi_int * casadi::Sparsity::colind ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_c8

Definition at line 168 of file sparsity.cpp.

168  {
169  return (*this)->colind();
170  }

Referenced by casadi::SparsityInternal::_mtimes(), add(), add_nz(), band(), casadi::SparsityInternal::bfs(), casadi::KinsolInterface::bjac(), bor(), casadi::Blocksqp::calcHessianUpdateExact(), casadi::Blocksqp::calcLagrangeGradient(), casadi::casadi_func_cons_jac(), casadi::GenericMatrix< MatType >::colind(), colind(), casadi::SparsityInternal::combineGen(), compressed(), casadi::construct_nlpsol(), casadi::IndexReduction::dae_struct_detect(), dense(), casadi::detect_simple_bounds_gen(), diag(), diagcat(), casadi::KinsolInterface::djac(), casadi::Transpose::eval_gen(), casadi::BonminUserClass::eval_h(), casadi::IpoptUserClass::eval_h(), casadi::AlpaqaProblem::eval_hess_L(), casadi::AlpaqaProblem::eval_hess_ψ(), casadi::AlpaqaProblem::eval_jac_g(), casadi::BonminUserClass::eval_jac_g(), casadi::IpoptUserClass::eval_jac_g(), casadi::FmuFunction::eval_task(), casadi::FmuFunction::finalize_hessian(), get_ccs(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::FunctionInternal::get_sparsity_out(), horzsplit(), casadi::SqicInterface::init(), casadi::Dple::init(), casadi::AmplInterface::init(), casadi::Blocksqp::init(), casadi::BonminInterface::init(), casadi::FatropConicInterface::init(), casadi::HpipmInterface::init(), casadi::HpmpcInterface::init(), casadi::SlicotDple::init(), casadi::SuperscsInterface::init(), casadi::ClarabelInterface::init_dependent(), casadi::HighsInterface::init_dependent(), casadi::MosekInterface::init_dependent(), casadi::XpressInterface::init_dependent(), casadi::CplexInterface::init_mem(), is_equal(), casadi::SparsityInternal::is_equal(), casadi::SparsityInternal::is_stacked(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), kron(), kron_contract(), casadi::kron_sp_gen(), lower(), casadi::mul_bvec_fwd(), mul_sparsityR(), casadi::SparsityInternal::multiply(), norm_0_mul(), permutation(), casadi::FunctionInternal::print_canonical(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::CvodesInterface::psetupF(), casadi::IdasInterface::psetupF(), casadi::Conic::sdp_to_socp_init(), set(), casadi::WorhpInterface::set_work(), casadi::CbcInterface::solve(), casadi::ClpInterface::solve(), casadi::CplexInterface::solve(), casadi::GurobiInterface::solve(), casadi::OoqpInterface::solve(), casadi::OsqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::SuperscsInterface::solve(), casadi::WorhpInterface::solve(), casadi::Blocksqp::solveQP(), casadi::KronContract::sp_forward(), casadi::Rank1::sp_forward(), casadi::Solve< Tr >::sp_forward(), casadi::Transpose::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::KronContract::sp_reverse(), casadi::Rank1::sp_reverse(), casadi::Solve< Tr >::sp_reverse(), casadi::Transpose::sp_reverse(), casadi::FunctionInternal::sp_reverse(), Sparsity(), casadi::SparsityInternal::star_coloring_new(), casadi::SparsityInternal::uni_coloring(), unit(), casadi::DaeBuilderInternal::update_dependencies(), upper(), and casadi::SnoptInterface::userfun().

◆ colind() [2/2]

casadi_int casadi::Sparsity::colind ( casadi_int  cc) const

Extra doc: https://github.com/casadi/casadi/wiki/L_cb

Definition at line 180 of file sparsity.cpp.

180  {
181  if (cc<0 || cc>size2()) {
182  throw std::out_of_range("Sparsity::colind: Index "
183  + str(cc) + " out of range [0," + str(size2()) + "]");
184  }
185  return colind()[cc];
186  }
std::string str(const T &v)
String representation, any type.

References colind(), size2(), and casadi::str().

◆ columns()

casadi_int casadi::Sparsity::columns ( ) const
inline

Definition at line 340 of file sparsity.hpp.

340 {return size2();}

◆ combine() [1/2]

Sparsity casadi::Sparsity::combine ( const Sparsity y,
bool  f0x_is_zero,
bool  function0_is_zero 
) const

Returns the new sparsity pattern as well as a mapping with the same length as the number of non-zero elements The mapping matrix contains the arguments for each nonzero, the first bit indicates if the first argument is nonzero, the second bit indicates if the second argument is nonzero (note that none of, one of or both of the arguments can be nonzero)

Extra doc: https://github.com/casadi/casadi/wiki/L_cl

Definition at line 404 of file sparsity.cpp.

405  {
406  return (*this)->combine(y, f0x_is_zero, fx0_is_zero);
407  }

References casadi::SparsityInternal::combine().

◆ combine() [2/2]

Sparsity casadi::Sparsity::combine ( const Sparsity y,
bool  f0x_is_zero,
bool  function0_is_zero,
std::vector< unsigned char > &  mapping 
) const

Returns the new sparsity pattern as well as a mapping with the same length as the number of non-zero elements The mapping matrix contains the arguments for each nonzero, the first bit indicates if the first argument is nonzero, the second bit indicates if the second argument is nonzero (note that none of, one of or both of the arguments can be nonzero)

Extra doc: https://github.com/casadi/casadi/wiki/L_cl

Definition at line 398 of file sparsity.cpp.

400  {
401  return (*this)->combine(y, f0x_is_zero, fx0_is_zero, mapping);
402  }

References casadi::SparsityInternal::combine().

Referenced by casadi::MXNode::get_binary().

◆ compress()

std::vector< casadi_int > casadi::Sparsity::compress ( bool  canonical = true) const

Definition at line 1321 of file sparsity.cpp.

1321  {
1322  if (canonical) {
1323  // fallback
1324  } else if (is_dense()) {
1325  return {size1(), size2(), 1};
1326  }
1327  return (*this)->sp();
1328  }

References is_dense(), size1(), and size2().

Referenced by casadi::CodeGenerator::add_sparsity(), and serialize().

◆ compressed() [1/2]

Sparsity casadi::Sparsity::compressed ( const casadi_int *  v,
bool  order_rows = false 
)
static

Create from a single vector containing the pattern in compressed column storage format: The format: The first two entries are the number of rows (nrow) and columns (ncol) The next ncol+1 entries are the column offsets (colind). Note that the last element, colind[ncol], gives the number of nonzeros The last colind[ncol] entries are the row indices

Definition at line 1356 of file sparsity.cpp.

1356  {
1357  casadi_assert_dev(v!=nullptr);
1358 
1359  // Get sparsity pattern
1360  casadi_int nrow = v[0];
1361  casadi_int ncol = v[1];
1362  const casadi_int *colind = v+2;
1363  if (colind[0]==1) {
1364  // Dense matrix - deviation from canonical form
1365  return Sparsity::dense(nrow, ncol);
1366  }
1367  casadi_int nnz = colind[ncol];
1368  if (nrow*ncol == nnz) {
1369  // Dense matrix
1370  return Sparsity::dense(nrow, ncol);
1371  } else {
1372  // Sparse matrix
1373  const casadi_int *row = v + 2 + ncol+1;
1374  return Sparsity(nrow, ncol,
1375  std::vector<casadi_int>(colind, colind+ncol+1),
1376  std::vector<casadi_int>(row, row+nnz), order_rows);
1377  }
1378  }

References colind(), dense(), nnz(), row(), and Sparsity().

◆ compressed() [2/2]

Sparsity casadi::Sparsity::compressed ( const std::vector< casadi_int > &  v,
bool  order_rows = false 
)
static

Create from a single vector containing the pattern in compressed column storage format: The format: The first two entries are the number of rows (nrow) and columns (ncol) The next ncol+1 entries are the column offsets (colind). Note that the last element, colind[ncol], gives the number of nonzeros The last colind[ncol] entries are the row indices

Definition at line 1341 of file sparsity.cpp.

1341  {
1342  // Check consistency
1343  casadi_assert_dev(v.size() >= 2);
1344  casadi_int nrow = v[0];
1345  casadi_int ncol = v[1];
1346  casadi_assert_dev(v.size() >= 2 + ncol+1);
1347  casadi_int nnz = v[2 + ncol];
1348  bool dense = v.size() == 2 + ncol+1 && nrow*ncol==nnz;
1349  bool sparse = v.size() == 2 + ncol+1 + nnz;
1350  casadi_assert_dev(dense || sparse);
1351 
1352  // Call array version
1353  return compressed(&v.front(), order_rows);
1354  }
std::pair< casadi_int, casadi_int > size() const
Get the shape.
Definition: sparsity.cpp:152
static Sparsity compressed(const std::vector< casadi_int > &v, bool order_rows=false)
Definition: sparsity.cpp:1341

References dense(), nnz(), and size().

Referenced by deserialize(), casadi::GenericExternal::get_jac_sparsity(), casadi::GenericExternal::get_sparsity_in(), casadi::GenericExternal::get_sparsity_out(), and qr_sparse().

◆ count_down()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_down
protectedinherited

Definition at line 133 of file generic_shared_impl.hpp.

46  {
47 #ifdef WITH_EXTRA_CHECKS
48  casadi_assert_dev(Function::call_depth_==0);
49 #endif // WITH_EXTRA_CHECKS
50  if (!node) return;
51 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
52  GenericWeakRef<Shared, Internal>* weak_ref =
53  node->weak_ref_.load(std::memory_order_acquire);
54 #else
55  GenericWeakRef<Shared, Internal>* weak_ref = node->weak_ref_;
56 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
57  if (weak_ref) {
58 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
59  // get_mutex() returns a shared_ptr copy, so the mutex outlives this lock
60  // even if delete node (below) destroys the WeakRefInternal holding it
61  auto mutex = weak_ref->get_mutex();
62  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
63  std::lock_guard<std::mutex> lock(*mutex);
64 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
65 
66  if (--static_cast<Internal*>(node)->count == 0) {
67  delete node;
68  node = nullptr;
69  }
70  } else {
71  if (--static_cast<Internal*>(node)->count == 0) {
72  delete node;
73  node = nullptr;
74  }
75  }
76  }

◆ count_up()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::count_up
protectedinherited

Definition at line 132 of file generic_shared_impl.hpp.

36  {
37 #ifdef WITH_EXTRA_CHECKS
38  casadi_assert_dev(Function::call_depth_==0);
39 #endif // WITH_EXTRA_CHECKS
40 
41  if (node) static_cast<Internal*>(node)->count++;
42 
43  }

◆ create()

Sparsity casadi::Sparsity::create ( SparsityInternal node)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bc

Definition at line 72 of file sparsity.cpp.

72  {
73  Sparsity ret;
74  ret.own(node);
75  return ret;
76  }

References casadi::GenericShared< Shared, Internal >::own().

◆ debug_repr()

std::string casadi::GenericShared< SharedObject , SharedObjectInternal >::debug_repr
inherited

Definition at line 112 of file generic_shared_impl.hpp.

91  {
92  if (node) {
93  return node->debug_repr(node);
94  } else {
95  return "NULL";
96  }
97  }
std::string debug_repr(const Internal *) const

◆ dense() [1/2]

Sparsity casadi::Sparsity::dense ( casadi_int  nrow,
casadi_int  ncol = 1 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_be

Definition at line 1028 of file sparsity.cpp.

1028  {
1029  casadi_assert_dev(nrow>=0);
1030  casadi_assert_dev(ncol>=0);
1031  // Column offset
1032  std::vector<casadi_int> colind(ncol+1);
1033  for (casadi_int cc=0; cc<ncol+1; ++cc) colind[cc] = cc*nrow;
1034 
1035  // Row
1036  std::vector<casadi_int> row(ncol*nrow);
1037  for (casadi_int cc=0; cc<ncol; ++cc)
1038  for (casadi_int rr=0; rr<nrow; ++rr)
1039  row[rr+cc*nrow] = rr;
1040 
1041  return Sparsity(nrow, ncol, colind, row);
1042  }

References colind(), row(), and Sparsity().

Referenced by casadi::Constant< Value >::_get_binary(), casadi::SparsityInternal::_mtimes(), casadi::BSpline::BSpline(), casadi::BSplineParametric::BSplineParametric(), compressed(), casadi::GetNonzerosParam::create(), casadi::Matrix< Scalar >::densify(), casadi::MX::densify(), casadi::Determinant::Determinant(), casadi::OnnxFunction::get_forward(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::OnnxFunction::get_jacobian(), casadi::MXNode::get_mac(), casadi::Nlpsol::get_reverse(), casadi::OnnxFunction::get_reverse(), casadi::BlazingSplineFunction::get_sparsity_in(), casadi::Expm::get_sparsity_in(), casadi::FmuFunction::get_sparsity_in(), casadi::Integrator::get_sparsity_in(), casadi::Interpolant::get_sparsity_in(), casadi::BlazingSplineFunction::get_sparsity_out(), casadi::Conic::get_sparsity_out(), casadi::FmuFunction::get_sparsity_out(), casadi::Integrator::get_sparsity_out(), casadi::Interpolant::get_sparsity_out(), casadi::Nlpsol::get_sparsity_out(), casadi::MX::inf(), casadi::SqicInterface::init(), casadi::Dple::init(), casadi::Blocksqp::init(), casadi::HpipmInterface::init(), casadi::HpmpcInterface::init(), casadi::Feasiblesqpmethod::init(), casadi::Qrsqp::init(), casadi::Sqpmethod::init(), casadi::SlicotDple::init_mem(), casadi::Inverse::Inverse(), casadi::FunctionInternal::jac_sparsity(), kron(), casadi::Onnx::load(), casadi::LogSumExp::LogSumExp(), casadi::SparsityInternal::makeDense(), casadi::MX::MX(), casadi::MX::nan(), casadi::GenericMatrix< MatType >::ones(), casadi::SparsityInternal::pattern_inverse(), casadi::Convexify::setup(), casadi::MX::solve(), Sparsity(), sum1(), sum2(), casadi::GenericMatrix< MatType >::sym(), casadi::SymbolicMX::SymbolicMX(), casadi::OnnxFunction::tensor_sparsity(), and casadi::GenericMatrix< MatType >::zeros().

◆ dense() [2/2]

static Sparsity casadi::Sparsity::dense ( const std::pair< casadi_int, casadi_int > &  rc)
inlinestatic

Extra doc: https://github.com/casadi/casadi/wiki/L_be

Definition at line 162 of file sparsity.hpp.

162  {
163  return dense(rc.first, rc.second);
164  }

◆ density()

double casadi::Sparsity::density ( ) const

Equivalent to (100.0 * nnz())/numel(), but avoids overflow

Extra doc: https://github.com/casadi/casadi/wiki/L_br

Definition at line 136 of file sparsity.cpp.

136  {
137  double r = 100;
138  r *= static_cast<double>(nnz());
139  r /= static_cast<double>(size1());
140  r /= static_cast<double>(size2());
141  return r;
142  }

References nnz(), size1(), and size2().

Referenced by casadi::FunctionInternal::get_jac_sparsity_gen(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), and casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm().

◆ deserialize() [1/3]

Sparsity casadi::Sparsity::deserialize ( const std::string &  s)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_c4

Definition at line 2103 of file sparsity.cpp.

2103  {
2104  std::stringstream ss;
2105  ss << s;
2106  return deserialize(ss);
2107  }
static Sparsity deserialize(std::istream &stream)
Build Sparsity from serialization.
Definition: sparsity.cpp:2074

References deserialize().

◆ deserialize() [2/3]

Sparsity casadi::Sparsity::deserialize ( DeserializingStream s)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_c6

Definition at line 2087 of file sparsity.cpp.

2087  {
2088  std::vector<casadi_int> i;
2089  s.unpack("SparsityInternal::compressed", i);
2090  if (i.empty()) {
2091  return Sparsity();
2092  } else {
2093  return Sparsity::compressed(i);
2094  }
2095  }

References compressed(), Sparsity(), and casadi::DeserializingStream::unpack().

◆ deserialize() [3/3]

Sparsity casadi::Sparsity::deserialize ( std::istream &  stream)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_c3

Definition at line 2074 of file sparsity.cpp.

2074  {
2075  DeserializingStream s(stream);
2076  return Sparsity::deserialize(s);
2077  }

Referenced by deserialize().

◆ dfs()

casadi_int casadi::Sparsity::dfs ( casadi_int  j,
casadi_int  top,
std::vector< casadi_int > &  xi,
std::vector< casadi_int > &  pstack,
const std::vector< casadi_int > &  pinv,
std::vector< bool > &  marked 
) const

See Direct Methods for Sparse Linear Systems by Davis (2006).

Extra doc: https://github.com/casadi/casadi/wiki/L_d5

Definition at line 698 of file sparsity.cpp.

701  {
702  return (*this)->dfs(j, top, xi, pstack, pinv, marked);
703  }

Referenced by casadi::SparsityInternal::scc().

◆ diag() [1/3]

static Sparsity casadi::Sparsity::diag ( casadi_int  nrow)
inlinestatic

◆ diag() [2/3]

Sparsity casadi::Sparsity::diag ( casadi_int  nrow,
casadi_int  ncol 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bi

Definition at line 569 of file sparsity.cpp.

569  {
570  // Smallest dimension
571  casadi_int n = std::min(nrow, ncol);
572 
573  // Column offset
574  std::vector<casadi_int> colind(ncol+1, n);
575  for (casadi_int cc=0; cc<n; ++cc) colind[cc] = cc;
576 
577  // Row
578  std::vector<casadi_int> row = range(n);
579 
580  // Create pattern from vectors
581  return Sparsity(nrow, ncol, colind, row);
582  }
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.

References colind(), casadi::range(), row(), and Sparsity().

◆ diag() [3/3]

static Sparsity casadi::Sparsity::diag ( const std::pair< casadi_int, casadi_int > &  rc)
inlinestatic

Extra doc: https://github.com/casadi/casadi/wiki/L_bi

Definition at line 192 of file sparsity.hpp.

192  {
193  return diag(rc.first, rc.second);
194  }

◆ diagcat()

Sparsity casadi::Sparsity::diagcat ( const std::vector< Sparsity > &  v)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1593 of file sparsity.cpp.

1593  {
1594  casadi_int n = 0;
1595  casadi_int m = 0;
1596 
1597  std::vector<casadi_int> colind(1, 0);
1598  std::vector<casadi_int> row;
1599 
1600  casadi_int nz = 0;
1601  for (casadi_int i=0;i<v.size();++i) {
1602  const casadi_int* colind_ = v[i].colind();
1603  casadi_int ncol = v[i].size2();
1604  const casadi_int* row_ = v[i].row();
1605  casadi_int sz = v[i].nnz();
1606  for (casadi_int k=1; k<ncol+1; ++k) {
1607  colind.push_back(colind_[k]+nz);
1608  }
1609  for (casadi_int k=0; k<sz; ++k) {
1610  row.push_back(row_[k]+m);
1611  }
1612  n+= v[i].size2();
1613  m+= v[i].size1();
1614  nz+= v[i].nnz();
1615  }
1616 
1617  return Sparsity(m, n, colind, row);
1618  }

References colind(), row(), and Sparsity().

◆ diagsplit()

std::vector< Sparsity > casadi::Sparsity::diagsplit ( const Sparsity x,
const std::vector< casadi_int > &  offset1,
const std::vector< casadi_int > &  offset2 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1685 of file sparsity.cpp.

1687  {
1688  // Consistency check
1689  casadi_assert_dev(!offset1.empty());
1690  casadi_assert_dev(offset1.front()==0);
1691  casadi_assert(offset1.back()==x.size1(),
1692  "diagsplit(Sparsity, offset1, offset2): Last elements of offset1 "
1693  "(" + str(offset1.back()) + ") must equal the number of rows "
1694  "(" + str(x.size1()) + ")");
1695  casadi_assert(offset2.back()==x.size2(),
1696  "diagsplit(Sparsity, offset1, offset2): Last elements of offset2 "
1697  "(" + str(offset2.back()) + ") must equal the number of rows "
1698  "(" + str(x.size2()) + ")");
1699  casadi_assert_dev(is_monotone(offset1));
1700  casadi_assert_dev(is_monotone(offset2));
1701  casadi_assert_dev(offset1.size()==offset2.size());
1702 
1703  // Number of outputs
1704  casadi_int n = offset1.size()-1;
1705 
1706  // Return value
1707  std::vector<Sparsity> ret;
1708 
1709  // Caveat: this is a very silly implementation
1710  IM x2 = IM::zeros(x);
1711 
1712  for (casadi_int i=0; i<n; ++i) {
1713  ret.push_back(x2(Slice(offset1[i], offset1[i+1]),
1714  Slice(offset2[i], offset2[i+1])).sparsity());
1715  }
1716 
1717  return ret;
1718  }
static MatType zeros(casadi_int nrow=1, casadi_int ncol=1)
Create a dense matrix or a matrix with specified sparsity with all entries zero.
bool is_monotone(const std::vector< T > &v)
Check if the vector is monotone.
Matrix< casadi_int > IM
Definition: im_fwd.hpp:31

References casadi::is_monotone(), size1(), size2(), casadi::str(), and casadi::GenericMatrix< MatType >::zeros().

◆ dim()

std::string casadi::Sparsity::dim ( bool  with_nz = false) const

◆ disp()

void casadi::SharedObject::disp ( std::ostream &  stream,
bool  more = false 
) const
inherited

Definition at line 35 of file shared_object.cpp.

35  {
36  if (is_null()) {
37  stream << "NULL";
38  } else {
39  (*this)->disp(stream, more);
40  }
41  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::is_null().

Referenced by casadi::Nlpsol::disp_more(), and casadi::CsparseInterface::nfact().

◆ enlarge()

void casadi::Sparsity::enlarge ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc,
bool  ind1 = false 
)

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 545 of file sparsity.cpp.

546  {
547  enlargeColumns(ncol, cc, ind1);
548  enlargeRows(nrow, rr, ind1);
549  }
void enlargeColumns(casadi_int ncol, const std::vector< casadi_int > &cc, bool ind1=false)
Enlarge the matrix along the second dimension (i.e. insert columns)
Definition: sparsity.cpp:551
void enlargeRows(casadi_int nrow, const std::vector< casadi_int > &rr, bool ind1=false)
Enlarge the matrix along the first dimension (i.e. insert rows)
Definition: sparsity.cpp:560

References enlargeColumns(), and enlargeRows().

Referenced by casadi::MX::enlarge(), and casadi::Sqpmethod::init().

◆ enlargeColumns()

void casadi::Sparsity::enlargeColumns ( casadi_int  ncol,
const std::vector< casadi_int > &  cc,
bool  ind1 = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_ct

Definition at line 551 of file sparsity.cpp.

551  {
552  casadi_assert_dev(cc.size() == size2());
553  if (cc.empty()) {
554  *this = Sparsity(size1(), ncol);
555  } else {
556  *this = (*this)->_enlargeColumns(ncol, cc, ind1);
557  }
558  }

References size1(), size2(), and Sparsity().

Referenced by enlarge(), and casadi::FunctionInternal::from_compact().

◆ enlargeRows()

void casadi::Sparsity::enlargeRows ( casadi_int  nrow,
const std::vector< casadi_int > &  rr,
bool  ind1 = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_cs

Definition at line 560 of file sparsity.cpp.

560  {
561  casadi_assert_dev(rr.size() == size1());
562  if (rr.empty()) {
563  *this = Sparsity(nrow, size2());
564  } else {
565  *this = (*this)->_enlargeRows(nrow, rr, ind1);
566  }
567  }

References size1(), size2(), and Sparsity().

Referenced by enlarge(), and casadi::FunctionInternal::from_compact().

◆ erase() [1/2]

std::vector< casadi_int > casadi::Sparsity::erase ( const std::vector< casadi_int > &  rr,
bool  ind1 = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_cw

Definition at line 346 of file sparsity.cpp.

346  {
347  std::vector<casadi_int> mapping;
348  *this = (*this)->_erase(rr, ind1, mapping);
349  return mapping;
350  }

◆ erase() [2/2]

std::vector< casadi_int > casadi::Sparsity::erase ( const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc,
bool  ind1 = false 
)

Extra doc: https://github.com/casadi/casadi/wiki/L_cv

Definition at line 339 of file sparsity.cpp.

340  {
341  std::vector<casadi_int> mapping;
342  *this = (*this)->_erase(rr, cc, ind1, mapping);
343  return mapping;
344  }

Referenced by casadi::MX::erase(), and casadi::FmuFunction::init().

◆ etree()

std::vector< casadi_int > casadi::Sparsity::etree ( bool  ata = false) const

See Direct Methods for Sparse Linear Systems by Davis (2006). If the parameter ata is false, the algorithm is equivalent to MATLAB's etree(A), except that the indices are zero-based. If ata is true, the algorithm is equivalent to MATLAB's etree(A, 'col').

The implementation is a modified version of cs_etree in CSparse Copyright(c) Timothy A. Davis, 2006-2009 Licensed as a derivative work under the GNU LGPL

Extra doc: https://github.com/casadi/casadi/wiki/L_d2

Definition at line 616 of file sparsity.cpp.

616  {
617  std::vector<casadi_int> parent(size2()), w(size1() + size2());
618  SparsityInternal::etree(*this, get_ptr(parent), get_ptr(w), ata);
619  return parent;
620  }
static void etree(const casadi_int *sp, casadi_int *parent, casadi_int *w, casadi_int ata)
Calculate the elimination tree for a matrix.
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.

References casadi::SparsityInternal::etree(), casadi::get_ptr(), size1(), and size2().

◆ export_code()

void casadi::Sparsity::export_code ( const std::string &  lang,
std::ostream &  stream = casadi::uout(),
const Dict options = Dict() 
) const

lang: only 'matlab' supported for now

 * options:
 *   inline: Indicates if you want everything on a single line (default: False)
 *   name: Name of exported variable (default: 'sp')
 *   as_matrix: Matlab does not have a sparsity object. (default: false)
*               With this option true, a numeric matrix will be constructed
 * 

Extra doc: https://github.com/casadi/casadi/wiki/L_dj

Definition at line 789 of file sparsity.cpp.

790  {
791  (*this)->export_code(lang, stream, options);
792  }

Referenced by casadi::XFunction< DerivedType, MatType, NodeType >::export_code(), casadi::MXFunction::export_code_body(), and casadi::Linsol::nfact().

◆ file_format()

std::string casadi::Sparsity::file_format ( const std::string &  filename,
const std::string &  format_hint,
const std::set< std::string > &  file_formats 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1978 of file sparsity.cpp.

1979  {
1980  if (format_hint.empty()) {
1981  std::string extension = filename.substr(filename.rfind(".")+1);
1982  auto it = file_formats.find(extension);
1983  casadi_assert(it!=file_formats.end(),
1984  "Extension '" + extension + "' not recognised. "
1985  "Valid options: " + str(file_formats) + ".");
1986  return extension;
1987  } else {
1988  auto it = file_formats.find(format_hint);
1989  casadi_assert(it!=file_formats.end(),
1990  "File format hint '" + format_hint + "' not recognised. "
1991  "Valid options: " + str(file_formats) + ".");
1992  return format_hint;
1993  }
1994 
1995  }
static std::set< std::string > file_formats
Enlarge matrix.
Definition: sparsity.hpp:1242
std::string filename(const std::string &path)
Definition: ghc.cpp:55

References file_formats, casadi::filename(), and casadi::str().

Referenced by from_file(), and to_file().

◆ find() [1/2]

std::vector< casadi_int > casadi::Sparsity::find ( bool  ind1 = SWIG_IND1) const

A : DenseMatrix 4 x 3 B : SparseMatrix 4 x 3 , 5 structural non-zeros

k = A.find() A[k] will contain the elements of A that are non-zero in B

Inverse of nonzeros.

Extra doc: https://github.com/casadi/casadi/wiki/L_da

Definition at line 737 of file sparsity.cpp.

737  {
738  std::vector<casadi_int> loc;
739  find(loc, ind1);
740  return loc;
741  }
std::vector< casadi_int > find(bool ind1=SWIG_IND1) const
Get the location of all non-zero elements as they would appear in a Dense matrix.
Definition: sparsity.cpp:737

Referenced by casadi::GetNonzeros::ad_forward(), casadi::SetNonzeros< Add >::ad_forward(), casadi::GetNonzeros::ad_reverse(), casadi::SetNonzeros< Add >::ad_reverse(), casadi::GetNonzeros::eval_mx(), casadi::SetNonzeros< Add >::eval_mx(), casadi::Matrix< Scalar >::set(), and casadi::MX::set().

◆ find() [2/2]

void casadi::Sparsity::find ( std::vector< casadi_int > &  loc,
bool  ind1 = false 
) const

Definition at line 743 of file sparsity.cpp.

743  {
744  (*this)->find(loc, ind1);
745  }

◆ from_file()

Sparsity casadi::Sparsity::from_file ( const std::string &  filename,
const std::string &  format_hint = "" 
)
static

Definition at line 2015 of file sparsity.cpp.

2015  {
2016  std::string format = file_format(filename, format_hint, file_formats);
2017  auto in_ptr = Filesystem::ifstream_ptr(filename);
2018  std::istream& in = *in_ptr;
2019  if (format=="mtx") {
2020  std::string line;
2021  std::vector<casadi_int> row, col;
2022  casadi_int size1, size2, nnz;
2023  int line_num = 0;
2024  while (std::getline(in, line)) {
2025  if (line_num==0) {
2026  if (!line.empty() && line.back()=='\r') line.pop_back();
2027  casadi_assert(line=="%%MatrixMarket matrix coordinate pattern general", "Wrong header");
2028  line_num = 1;
2029  } else if (line_num==1) {
2030  std::stringstream stream(line);
2031  stream >> size1;
2032  stream >> size2;
2033  stream >> nnz;
2034  row.reserve(nnz);
2035  col.reserve(nnz);
2036  line_num = 2;
2037  } else {
2038  std::stringstream stream(line);
2039  casadi_int r, c;
2040  stream >> r;
2041  stream >> c;
2042  row.push_back(r-1);
2043  col.push_back(c-1);
2044  }
2045  }
2046  return triplet(size1, size2, row, col);
2047  } else {
2048  casadi_error("Unknown format '" + format + "'");
2049  }
2050  }
static std::unique_ptr< std::istream > ifstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::in, bool fail=true)
Definition: filesystem.cpp:135
static std::string file_format(const std::string &filename, const std::string &format_hint, const std::set< std::string > &file_formats)
Enlarge matrix.
Definition: sparsity.cpp:1978
static Sparsity triplet(casadi_int nrow, casadi_int ncol, const std::vector< casadi_int > &row, const std::vector< casadi_int > &col, std::vector< casadi_int > &mapping, bool invert_mapping)
Create a sparsity pattern given the nonzeros in sparse triplet form *.
Definition: sparsity.cpp:1143

References file_format(), file_formats, casadi::filename(), casadi::Filesystem::ifstream_ptr(), nnz(), row(), size1(), size2(), and triplet().

◆ get()

SparsityInternal * casadi::Sparsity::get ( ) const

Definition at line 2109 of file sparsity.cpp.

2109  {
2110  return static_cast<SparsityInternal*>(SharedObject::get());
2111  }
SharedObjectInternal * get() const
Get a const pointer to the node.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::get().

Referenced by casadi::SparsityInternal::is_equal(), casadi::SparsityInternal::maxtrans(), and operator*().

◆ get_ccs()

void casadi::Sparsity::get_ccs ( std::vector< casadi_int > &  colind,
std::vector< casadi_int > &  row 
) const

Definition at line 376 of file sparsity.cpp.

376  {
377  colind = get_colind();
378  row = get_row();
379  }

References colind(), get_colind(), get_row(), and row().

Referenced by get_crs().

◆ get_col()

std::vector< casadi_int > casadi::Sparsity::get_col ( ) const

◆ get_colind()

std::vector< casadi_int > casadi::Sparsity::get_colind ( ) const

Together with the row-vector, one obtains the sparsity pattern in the column compressed format.

Extra doc: https://github.com/casadi/casadi/wiki/L_ca

Definition at line 364 of file sparsity.cpp.

364  {
365  return (*this)->get_colind();
366  }

Referenced by add_nz(), casadi::OsqpInterface::codegen_body(), casadi::OsqpInterface::codegen_init_mem(), get_ccs(), casadi::GenericMatrix< MatType >::get_colind(), info(), casadi::FatropInterface::init(), casadi::HpipmInterface::init(), casadi::OsqpInterface::init_mem(), and casadi::SuperscsInterface::init_mem().

◆ get_crs()

void casadi::Sparsity::get_crs ( std::vector< casadi_int > &  rowind,
std::vector< casadi_int > &  col 
) const

Definition at line 381 of file sparsity.cpp.

381  {
382  T().get_ccs(rowind, col);
383  }
Sparsity T() const
Transpose the matrix.
Definition: sparsity.cpp:394
void get_ccs(std::vector< casadi_int > &colind, std::vector< casadi_int > &row) const
Get the sparsity in compressed column storage (CCS) format.
Definition: sparsity.cpp:376

References get_ccs(), and T().

◆ get_diag()

Sparsity casadi::Sparsity::get_diag ( std::vector< casadi_int > &  mapping) const

Get the diagonal of the matrix/create a diagonal matrix (mapping will contain the nonzero mapping) When the input is square, the diagonal elements are returned. If the input is vector-like, a diagonal matrix is constructed with it.

Definition at line 612 of file sparsity.cpp.

612  {
613  return (*this)->get_diag(mapping);
614  }

References casadi::SparsityInternal::get_diag().

Referenced by casadi::MX::diag().

◆ get_lower()

std::vector< casadi_int > casadi::Sparsity::get_lower ( ) const

Definition at line 1003 of file sparsity.cpp.

1003  {
1004  return (*this)->get_lower();
1005  }

◆ get_nz() [1/3]

casadi_int casadi::Sparsity::get_nz ( casadi_int  rr,
casadi_int  cc 
) const

◆ get_nz() [2/3]

std::vector< casadi_int > casadi::Sparsity::get_nz ( const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc 
) const

return -1 if the element does not exist

Extra doc: https://github.com/casadi/casadi/wiki/L_cg

Definition at line 264 of file sparsity.cpp.

265  {
266  return (*this)->get_nz(rr, cc);
267  }

◆ get_nz() [3/3]

void casadi::Sparsity::get_nz ( std::vector< casadi_int > &  indices) const

The index vector is used both for input and outputs and must be sorted by increasing nonzero index, i.e. column-wise. Elements not found in the sparsity pattern are set to -1.

Extra doc: https://github.com/casadi/casadi/wiki/L_ch

Definition at line 747 of file sparsity.cpp.

747  {
748  (*this)->get_nz(indices);
749  }

◆ get_row()

std::vector< casadi_int > casadi::Sparsity::get_row ( ) const

Together with the column-vector, this vector gives the sparsity of the matrix in sparse triplet format, and together with the colind vector, one obtains the sparsity in column compressed format.

Extra doc: https://github.com/casadi/casadi/wiki/L_c9

Definition at line 372 of file sparsity.cpp.

372  {
373  return (*this)->get_row();
374  }

Referenced by add_nz(), casadi::OsqpInterface::codegen_init_mem(), get_ccs(), casadi::GenericMatrix< MatType >::get_row(), get_triplet(), casadi::DaeBuilderInternal::hess_sparsity(), info(), casadi::FatropInterface::init(), casadi::HpipmInterface::init(), casadi::OsqpInterface::init_mem(), casadi::SuperscsInterface::init_mem(), casadi::SparsityInternal::is_compactible(), permutation_vector(), casadi::MX::set(), casadi::KnitroInterface::solve(), to_file(), and casadi::UnoInterface::UnoInterface().

◆ get_str()

std::string casadi::SharedObject::get_str ( bool  more = false) const
inlineinherited

Definition at line 91 of file shared_object.hpp.

91  {
92  std::stringstream ss;
93  disp(ss, more);
94  return ss.str();
95  }
void disp(std::ostream &stream, bool more=false) const
Print a description of the object.

◆ get_triplet()

void casadi::Sparsity::get_triplet ( std::vector< casadi_int > &  row,
std::vector< casadi_int > &  col 
) const

Definition at line 385 of file sparsity.cpp.

385  {
386  row = get_row();
387  col = get_col();
388  }
std::vector< casadi_int > get_col() const
Get the column for each non-zero entry.
Definition: sparsity.cpp:368

References get_col(), get_row(), and row().

Referenced by casadi::PiqpInterface::solve(), and casadi::ProxqpInterface::solve().

◆ get_upper()

std::vector< casadi_int > casadi::Sparsity::get_upper ( ) const

Definition at line 1007 of file sparsity.cpp.

1007  {
1008  return (*this)->get_upper();
1009  }

◆ getCache()

Sparsity::CachingMap & casadi::Sparsity::getCache ( )
static

Definition at line 525 of file sparsity.cpp.

525  {
526  static CachingMap ret;
527  return ret;
528  }
std::unordered_multimap< std::size_t, WeakRef > CachingMap
Enlarge matrix.
Definition: sparsity.hpp:924

◆ getCount()

casadi_int casadi::GenericShared< SharedObject , SharedObjectInternal >::getCount
inherited

Definition at line 102 of file generic_shared_impl.hpp.

138  {
139  return (*this)->getCount();
140  }

◆ getEmpty()

const Sparsity & casadi::Sparsity::getEmpty ( )
static

Definition at line 540 of file sparsity.cpp.

540  {
541  static EmptySparsity ret;
542  return ret;
543  }

◆ getScalar()

const Sparsity & casadi::Sparsity::getScalar ( )
static

Definition at line 530 of file sparsity.cpp.

530  {
531  static ScalarSparsity ret;
532  return ret;
533  }

◆ getScalarSparse()

const Sparsity & casadi::Sparsity::getScalarSparse ( )
static

Definition at line 535 of file sparsity.cpp.

535  {
536  static ScalarSparseSparsity ret;
537  return ret;
538  }

◆ has_nz()

bool casadi::Sparsity::has_nz ( casadi_int  rr,
casadi_int  cc 
) const

Definition at line 241 of file sparsity.cpp.

241  {
242  return get_nz(rr, cc)!=-1;
243  }
casadi_int get_nz(casadi_int rr, casadi_int cc) const
Get the index of an existing non-zero element.
Definition: sparsity.cpp:246

References get_nz().

◆ hash()

std::size_t casadi::Sparsity::hash ( ) const

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 811 of file sparsity.cpp.

811  {
812  return (*this)->hash();
813  }

Referenced by casadi::get_const().

◆ horzcat()

Sparsity casadi::Sparsity::horzcat ( const std::vector< Sparsity > &  sp)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_cq

Definition at line 1408 of file sparsity.cpp.

1408  {
1409  // Quick return if possible
1410  if (sp.empty()) return Sparsity(1, 0);
1411  if (sp.size()==1) return sp.front();
1412 
1413  // Count total nnz
1414  casadi_int nnz_total = 0;
1415  for (casadi_int i=0; i<sp.size(); ++i) nnz_total += sp[i].nnz();
1416 
1417  // Construct from vectors (triplet format)
1418  std::vector<casadi_int> ret_row, ret_col;
1419  ret_row.reserve(nnz_total);
1420  ret_col.reserve(nnz_total);
1421  casadi_int ret_ncol = 0;
1422  casadi_int ret_nrow = 0;
1423  for (casadi_int i=0; i<sp.size() && ret_nrow==0; ++i)
1424  ret_nrow = sp[i].size1();
1425 
1426  // Append all patterns
1427  for (std::vector<Sparsity>::const_iterator i=sp.begin(); i!=sp.end(); ++i) {
1428  // Get sparsity pattern
1429  casadi_int sp_nrow = i->size1();
1430  casadi_int sp_ncol = i->size2();
1431  const casadi_int* sp_colind = i->colind();
1432  const casadi_int* sp_row = i->row();
1433  casadi_assert(sp_nrow==ret_nrow || sp_nrow==0,
1434  "Sparsity::horzcat: Mismatching number of rows");
1435 
1436  // Add entries to pattern
1437  for (casadi_int cc=0; cc<sp_ncol; ++cc) {
1438  for (casadi_int k=sp_colind[cc]; k<sp_colind[cc+1]; ++k) {
1439  ret_row.push_back(sp_row[k]);
1440  ret_col.push_back(cc + ret_ncol);
1441  }
1442  }
1443 
1444  // Update offset
1445  ret_ncol += sp_ncol;
1446  }
1447  return Sparsity::triplet(ret_nrow, ret_ncol, ret_row, ret_col);
1448  }

References nnz(), size1(), Sparsity(), and triplet().

Referenced by blockcat().

◆ horzsplit()

std::vector< Sparsity > casadi::Sparsity::horzsplit ( const Sparsity x,
const std::vector< casadi_int > &  offset 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1620 of file sparsity.cpp.

1621  {
1622  // Consistency check
1623  casadi_assert_dev(!offset.empty());
1624  casadi_assert_dev(offset.front()==0);
1625  casadi_assert(offset.back()==x.size2(),
1626  "horzsplit(Sparsity, std::vector<casadi_int>): Last elements of offset "
1627  "(" + str(offset.back()) + ") must equal the number of columns "
1628  "(" + str(x.size2()) + ")");
1629  casadi_assert_dev(is_monotone(offset));
1630 
1631  // Number of outputs
1632  casadi_int n = offset.size()-1;
1633 
1634  // Get the sparsity of the input
1635  const casadi_int* colind_x = x.colind();
1636  const casadi_int* row_x = x.row();
1637 
1638  // Allocate result
1639  std::vector<Sparsity> ret;
1640  ret.reserve(n);
1641 
1642  // Sparsity pattern as CCS vectors
1643  std::vector<casadi_int> colind, row;
1644  casadi_int ncol, nrow = x.size1();
1645 
1646  // Get the sparsity patterns of the outputs
1647  for (casadi_int i=0; i<n; ++i) {
1648  casadi_int first_col = offset[i];
1649  casadi_int last_col = offset[i+1];
1650  ncol = last_col - first_col;
1651 
1652  // Construct the sparsity pattern
1653  colind.resize(ncol+1);
1654  std::copy(colind_x+first_col, colind_x+last_col+1, colind.begin());
1655  for (std::vector<casadi_int>::iterator it=colind.begin()+1; it!=colind.end(); ++it)
1656  *it -= colind[0];
1657  colind[0] = 0;
1658  row.resize(colind.back());
1659  std::copy(row_x+colind_x[first_col], row_x+colind_x[last_col], row.begin());
1660 
1661  // Append to the list
1662  ret.push_back(Sparsity(nrow, ncol, colind, row));
1663  }
1664 
1665  // Return (RVO)
1666  return ret;
1667  }

References colind(), casadi::is_monotone(), casadi::SparsityInterface< Sparsity >::offset(), row(), size1(), size2(), Sparsity(), and casadi::str().

Referenced by vertsplit().

◆ info()

Dict casadi::Sparsity::info ( ) const

Obtain information about sparsity

Definition at line 1971 of file sparsity.cpp.

1971  {
1972  if (is_null()) return Dict();
1973  return {{"nrow", size1()}, {"ncol", size2()}, {"colind", get_colind()}, {"row", get_row()}};
1974  }
GenericType::Dict Dict
C++ equivalent of Python's dict or MATLAB's struct.

References get_colind(), get_row(), casadi::GenericShared< SharedObject, SharedObjectInternal >::is_null(), size1(), and size2().

◆ intersect() [1/2]

Sparsity casadi::Sparsity::intersect ( const Sparsity y) const

Returns the new sparsity pattern as well as a mapping with the same length as the number of non-zero elements The value is 1 if the non-zero comes from the first (i.e. this) object, 2 if it is from the second and 3 (i.e. 1 | 2) if from both

Extra doc: https://github.com/casadi/casadi/wiki/L_cn

Definition at line 422 of file sparsity.cpp.

422  {
423  return (*this)->combine(y, true, true);
424  }

References casadi::SparsityInternal::combine().

◆ intersect() [2/2]

Sparsity casadi::Sparsity::intersect ( const Sparsity y,
std::vector< unsigned char > &  mapping 
) const

Returns the new sparsity pattern as well as a mapping with the same length as the number of non-zero elements The value is 1 if the non-zero comes from the first (i.e. this) object, 2 if it is from the second and 3 (i.e. 1 | 2) if from both

Extra doc: https://github.com/casadi/casadi/wiki/L_cn

Definition at line 417 of file sparsity.cpp.

418  {
419  return (*this)->combine(y, true, true, mapping);
420  }

References casadi::SparsityInternal::combine().

Referenced by casadi::MXNode::get_dot(), casadi::Project::get_nzref(), operator*(), and casadi::MX::project().

◆ is_column()

bool casadi::Sparsity::is_column ( ) const

◆ is_compactible()

bool casadi::Sparsity::is_compactible ( std::vector< casadi_int > &  row,
std::vector< casadi_int > &  col 
) const

If true, returns the (sorted) row and column index sets such that nz(i, j) <-> i in row and j in col. The nonzero buffer of a compactible matrix in CCS is laid out exactly as a column-major dense row.size() x col.size() matrix, so it can be consumed by dense kernels without gather/scatter.

Extra doc: https://github.com/casadi/casadi/wiki/L_2gf

Definition at line 806 of file sparsity.cpp.

807  {
808  return (*this)->is_compactible(row, col);
809  }

References row().

Referenced by casadi::PseudoDenseMultiplication::try_create().

◆ is_dense()

bool casadi::Sparsity::is_dense ( ) const

◆ is_diag()

bool casadi::Sparsity::is_diag ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_247

Definition at line 277 of file sparsity.cpp.

277  {
278  return (*this)->is_diag();
279  }

Referenced by casadi::SparsityInternal::_mtimes().

◆ is_empty()

bool casadi::Sparsity::is_empty ( bool  both = false) const

◆ is_equal() [1/3]

bool casadi::Sparsity::is_equal ( casadi_int  nrow,
casadi_int  ncol,
const casadi_int *  colind,
const casadi_int *  row 
) const

Definition at line 453 of file sparsity.cpp.

454  {
455  return (*this)->is_equal(nrow, ncol, colind, row);
456  }

References colind(), and row().

◆ is_equal() [2/3]

bool casadi::Sparsity::is_equal ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  colind,
const std::vector< casadi_int > &  row 
) const

Definition at line 448 of file sparsity.cpp.

449  {
450  return (*this)->is_equal(nrow, ncol, colind, row);
451  }

References colind(), and row().

◆ is_equal() [3/3]

bool casadi::Sparsity::is_equal ( const Sparsity y) const

Definition at line 444 of file sparsity.cpp.

444  {
445  return (*this)->is_equal(y);
446  }

◆ is_null()

Definition at line 116 of file generic_shared_impl.hpp.

120  {
121  return node==nullptr;
122  }

◆ is_orthonormal()

bool casadi::Sparsity::is_orthonormal ( bool  allow_empty = false) const
Parameters
[in]allow_emptyDisregard empty rows and columns in the analysis

Extra doc: https://github.com/casadi/casadi/wiki/L_24f

Definition at line 305 of file sparsity.cpp.

305  {
306  return (*this)->is_orthonormal(allow_empty);
307  }

Referenced by casadi::MX::solve().

◆ is_orthonormal_columns()

bool casadi::Sparsity::is_orthonormal_columns ( bool  allow_empty = false) const
Parameters
[in]allow_emptyDisregard empty columns in the analysis

Extra doc: https://github.com/casadi/casadi/wiki/L_24h

Definition at line 313 of file sparsity.cpp.

313  {
314  return (*this)->is_orthonormal_columns(allow_empty);
315  }

◆ is_orthonormal_rows()

bool casadi::Sparsity::is_orthonormal_rows ( bool  allow_empty = false) const
Parameters
[in]allow_emptyDisregard empty rows in the analysis

Extra doc: https://github.com/casadi/casadi/wiki/L_24g

Definition at line 309 of file sparsity.cpp.

309  {
310  return (*this)->is_orthonormal_rows(allow_empty);
311  }

◆ is_permutation()

bool casadi::Sparsity::is_permutation ( ) const

A Matrix P is permutation matrix if right multiplication with a dense vector v leads to a vector with the same elements, but permuted.

Implies square

Equivalent to is_orthonormal(false)

Extra doc: https://github.com/casadi/casadi/wiki/L_24d

Definition at line 297 of file sparsity.cpp.

297  {
298  return (*this)->is_permutation();
299  }

Referenced by permutation_vector().

◆ is_reshape()

bool casadi::Sparsity::is_reshape ( const Sparsity y) const

Definition at line 802 of file sparsity.cpp.

802  {
803  return (*this)->is_reshape(*y);
804  }

Referenced by casadi::MXNode::get_reshape(), casadi::SparsityCast::get_reshape(), casadi::MX::MX(), casadi::MX::reshape(), and reshape().

◆ is_row()

bool casadi::Sparsity::is_row ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_cx

Definition at line 281 of file sparsity.cpp.

281  {
282  return (*this)->is_row();
283  }

Referenced by casadi::GenericMatrix< MatType >::is_row().

◆ is_scalar()

bool casadi::Sparsity::is_scalar ( bool  scalar_and_dense = false) const

◆ is_selection()

bool casadi::Sparsity::is_selection ( bool  allow_empty = false) const

A Matrix S is selection matrix if right multiplication with a dense vector leads to a vector with a subset of the elements of the original vector

Parameters
[in]allow_emptyAllow the resultant vector to have structural zeros

Equivalent to is_orthonormal_rows(allow_empty)

Extra doc: https://github.com/casadi/casadi/wiki/L_24e

Definition at line 301 of file sparsity.cpp.

301  {
302  return (*this)->is_selection(allow_empty);
303  }

◆ is_singular()

bool casadi::Sparsity::is_singular ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_24c

Definition at line 1315 of file sparsity.cpp.

1315  {
1316  casadi_assert(is_square(),
1317  "is_singular: only defined for square matrices, but got " + dim());
1318  return sprank(*this)!=size2();
1319  }
static casadi_int sprank(const Sparsity &x)
Enlarge matrix.
Definition: sparsity.cpp:1728
bool is_square() const
Is square?
Definition: sparsity.cpp:293

References dim(), is_square(), size2(), and sprank().

Referenced by casadi::Integrator::init(), casadi::Rootfinder::init(), and casadi::CsparseInterface::nfact().

◆ is_square()

bool casadi::Sparsity::is_square ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_248

Definition at line 293 of file sparsity.cpp.

293  {
294  return (*this)->is_square();
295  }

Referenced by casadi::Expm::Expm(), is_singular(), casadi::GenericMatrix< MatType >::is_square(), kkt(), and casadi::Convexify::setup().

◆ is_stacked()

bool casadi::Sparsity::is_stacked ( const Sparsity y,
casadi_int  n 
) const

Definition at line 440 of file sparsity.cpp.

440  {
441  return (*this)->is_stacked(y, n);
442  }

◆ is_subset()

bool casadi::Sparsity::is_subset ( const Sparsity rhs) const

Definition at line 426 of file sparsity.cpp.

426  {
427  return (*this)->is_subset(rhs);
428  }

Referenced by casadi::SparsityCast::ad_reverse(), and casadi::Project::get_project().

◆ is_symmetric()

bool casadi::Sparsity::is_symmetric ( ) const

◆ is_transpose()

bool casadi::Sparsity::is_transpose ( const Sparsity y) const

Definition at line 798 of file sparsity.cpp.

798  {
799  return (*this)->is_transpose(*y);
800  }

◆ is_tril()

bool casadi::Sparsity::is_tril ( bool  strictly = false) const

◆ is_triu()

bool casadi::Sparsity::is_triu ( bool  strictly = false) const

Extra doc: https://github.com/casadi/casadi/wiki/L_24a

Definition at line 325 of file sparsity.cpp.

325  {
326  return (*this)->is_triu(strictly);
327  }

Referenced by casadi::IpoptInterface::init(), casadi::GenericMatrix< MatType >::is_triu(), and casadi::Convexify::setup().

◆ is_vector()

bool casadi::Sparsity::is_vector ( ) const

◆ kkt()

Sparsity casadi::Sparsity::kkt ( const Sparsity H,
const Sparsity J,
bool  with_x_diag = true,
bool  with_lam_g_diag = true 
)
static

[H + I1, J'; J, I2] where I1 and I2 are optional

Extra doc: https://github.com/casadi/casadi/wiki/L_dk

Definition at line 2052 of file sparsity.cpp.

2053  {
2054  // Consistency check
2055  casadi_assert(H.is_square(), "H must be square");
2056  casadi_assert(H.size1() == J.size2(), "Dimension mismatch");
2057 
2058  // Add diagonal to H recursively
2059  if (with_x_diag) return kkt(H + diag(H.size()), J, false, with_lam_g_diag);
2060 
2061  // Lower right entry
2062  int ng = J.size1();
2063  Sparsity B = with_lam_g_diag ? diag(ng, ng) : Sparsity(ng, ng);
2064 
2065  // Concatenate
2066  return blockcat({{H, J.T()}, {J, B}});
2067  }
static Sparsity blockcat(const std::vector< std::vector< Sparsity > > &v)
Enlarge matrix.
Definition: sparsity.cpp:1678
static Sparsity kkt(const Sparsity &H, const Sparsity &J, bool with_x_diag=true, bool with_lam_g_diag=true)
Get KKT system sparsity.
Definition: sparsity.cpp:2052
SparsityInterface< Sparsity > B
Base class.
Definition: sparsity.hpp:138

References blockcat(), diag(), is_square(), size(), size1(), size2(), Sparsity(), and T().

Referenced by casadi::Ipqp::init(), and casadi::Qrqp::init().

◆ kron()

Sparsity casadi::Sparsity::kron ( const Sparsity a,
const Sparsity b 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1450 of file sparsity.cpp.

1450  {
1451  casadi_int a_ncol = a.size2();
1452  casadi_int b_ncol = b.size2();
1453  casadi_int a_nrow = a.size1();
1454  casadi_int b_nrow = b.size1();
1455  if (a.is_dense() && b.is_dense()) return Sparsity::dense(a_nrow*b_nrow, a_ncol*b_ncol);
1456 
1457  const casadi_int* a_colind = a.colind();
1458  const casadi_int* a_row = a.row();
1459  const casadi_int* b_colind = b.colind();
1460  const casadi_int* b_row = b.row();
1461 
1462  std::vector<casadi_int> r_colind(a_ncol*b_ncol+1, 0);
1463  std::vector<casadi_int> r_row(a.nnz()*b.nnz());
1464 
1465  casadi_int* r_colind_ptr = get_ptr(r_colind);
1466  casadi_int* r_row_ptr = get_ptr(r_row);
1467 
1468  casadi_int i=0;
1469  casadi_int j=0;
1470  // Loop over the columns
1471  for (casadi_int a_cc=0; a_cc<a_ncol; ++a_cc) {
1472  casadi_int a_start = a_colind[a_cc];
1473  casadi_int a_stop = a_colind[a_cc+1];
1474  // Loop over the columns
1475  for (casadi_int b_cc=0; b_cc<b_ncol; ++b_cc) {
1476  casadi_int b_start = b_colind[b_cc];
1477  casadi_int b_stop = b_colind[b_cc+1];
1478  // Loop over existing nonzeros
1479  for (casadi_int a_el=a_start; a_el<a_stop; ++a_el) {
1480  casadi_int a_r = a_row[a_el];
1481  // Loop over existing nonzeros
1482  for (casadi_int b_el=b_start; b_el<b_stop; ++b_el) {
1483  casadi_int b_r = b_row[b_el];
1484  r_row_ptr[i++] = a_r*b_nrow+b_r;
1485  }
1486  }
1487  j+=1;
1488  r_colind_ptr[j] = r_colind_ptr[j-1] + (b_stop-b_start)*(a_stop-a_start);
1489  }
1490  }
1491  return Sparsity(a_nrow*b_nrow, a_ncol*b_ncol, r_colind, r_row);
1492  }

References colind(), dense(), casadi::get_ptr(), is_dense(), nnz(), row(), size1(), size2(), and Sparsity().

Referenced by casadi::MXNode::get_kron(), and casadi::Kron::Kron().

◆ kron_contract()

Sparsity casadi::Sparsity::kron_contract ( const Sparsity sp_m,
const Sparsity sp_x,
bool  inner 
)
static

Given an (mA*mB, nA*nB) pattern sp_m and an (xrow, xcol) pattern sp_x, computes the output sparsity. Both modes share the same (yrow, ycol) = sp_m.size() / sp_x.size() shape; what differs is which axis pair indexes the output:

  • inner = true: (i, j) ∈ y iff ∃ (r, s) ∈ sp_x with (i*xrow + r, j*xcol + s) ∈ sp_m; output indexed by the outer block of M.
  • inner = false: (r, s) ∈ y iff ∃ (i, j) ∈ sp_x with (i*yrow + r, j*ycol + s) ∈ sp_m; output indexed by the inner block of M.

    Extra doc: https://github.com/casadi/casadi/wiki/L_2hn

Definition at line 1494 of file sparsity.cpp.

1494  {
1495  casadi_int xrow = sp_x.size1();
1496  casadi_int xcol = sp_x.size2();
1497  casadi_assert(xrow > 0 && xcol > 0,
1498  "Sparsity::kron_contract: sp_x must be nonempty");
1499  casadi_assert(sp_m.size1() % xrow == 0 && sp_m.size2() % xcol == 0,
1500  "Sparsity::kron_contract: sp_m dims must be multiples of sp_x dims");
1501  casadi_int yrow = sp_m.size1() / xrow;
1502  casadi_int ycol = sp_m.size2() / xcol;
1503  // Block-split factor of M's row/col indices. Inner: M_row=i*xrow+r, so
1504  // split_r=xrow. Outer: M_row=i*yrow+r, so split_r=yrow. Same for cols.
1505  casadi_int split_r = inner ? xrow : yrow;
1506  casadi_int split_c = inner ? xcol : ycol;
1507  // Densify x's pattern into a dense lookup
1508  std::vector<bool> x_dense(xrow * xcol, false);
1509  const casadi_int* x_colind = sp_x.colind();
1510  const casadi_int* x_row = sp_x.row();
1511  for (casadi_int cc = 0; cc < xcol; ++cc) {
1512  for (casadi_int el = x_colind[cc]; el < x_colind[cc+1]; ++el) {
1513  x_dense[cc*xrow + x_row[el]] = true;
1514  }
1515  }
1516  // Walk m's CSC. For each (outer, inner) decomposition of m's row/col,
1517  // the (y, x) assignment swaps with `inner`.
1518  std::vector<bool> y_dense(yrow * ycol, false);
1519  const casadi_int* m_colind = sp_m.colind();
1520  const casadi_int* m_row = sp_m.row();
1521  casadi_int outer_c_count = sp_m.size2() / split_c; // ycol if inner, xcol otherwise
1522  for (casadi_int outer_c = 0; outer_c < outer_c_count; ++outer_c) {
1523  for (casadi_int inner_c = 0; inner_c < split_c; ++inner_c) {
1524  casadi_int cc = outer_c*split_c + inner_c;
1525  casadi_int y_c = inner ? outer_c : inner_c;
1526  casadi_int x_c = inner ? inner_c : outer_c;
1527  for (casadi_int el = m_colind[cc]; el < m_colind[cc+1]; ++el) {
1528  casadi_int rr = m_row[el];
1529  casadi_int outer_r = rr / split_r;
1530  casadi_int inner_r = rr % split_r;
1531  casadi_int y_r = inner ? outer_r : inner_r;
1532  casadi_int x_r = inner ? inner_r : outer_r;
1533  if (x_dense[x_c*xrow + x_r]) {
1534  y_dense[y_c*yrow + y_r] = true;
1535  }
1536  }
1537  }
1538  }
1539  // Build CSC from y_dense
1540  std::vector<casadi_int> y_colind(ycol + 1, 0);
1541  std::vector<casadi_int> y_row;
1542  for (casadi_int j = 0; j < ycol; ++j) {
1543  for (casadi_int i = 0; i < yrow; ++i) {
1544  if (y_dense[j*yrow + i]) y_row.push_back(i);
1545  }
1546  y_colind[j+1] = y_row.size();
1547  }
1548  return Sparsity(yrow, ycol, y_colind, y_row);
1549  }

References colind(), row(), size1(), size2(), and Sparsity().

Referenced by casadi::MXNode::get_kron_contract(), and casadi::KronContract::KronContract().

◆ largest_first()

std::vector< casadi_int > casadi::Sparsity::largest_first ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_de

Definition at line 776 of file sparsity.cpp.

776  {
777  return (*this)->largest_first();
778  }

◆ ldl()

Sparsity casadi::Sparsity::ldl ( std::vector< casadi_int > &  p,
bool  amd = true 
) const

Returns the sparsity pattern of L^T

The implementation is a modified version of LDL Copyright(c) Timothy A. Davis, 2005-2013 Licensed as a derivative work under the GNU LGPL

Extra doc: https://github.com/casadi/casadi/wiki/L_d3

Definition at line 622 of file sparsity.cpp.

622  {
623  casadi_assert(is_symmetric(),
624  "LDL factorization requires a symmetric matrix");
625  // Recursive call if AMD
626  if (amd) {
627  // Get AMD reordering
628  p = this->amd();
629  // Permute sparsity pattern
630  std::vector<casadi_int> tmp;
631  Sparsity Aperm = sub(p, p, tmp);
632  // Call recursively
633  return Aperm.ldl(tmp, false);
634  }
635  // Dimension
636  casadi_int n=size1();
637  // Natural ordering
638  p = range(n);
639  // Work vector
640  std::vector<casadi_int> w(3*n);
641  // Elimination tree
642  std::vector<casadi_int> parent(n);
643  // Calculate colind in L (strictly lower entries only)
644  std::vector<casadi_int> L_colind(1+n);
645  SparsityInternal::ldl_colind(*this, get_ptr(parent), get_ptr(L_colind), get_ptr(w));
646  // Get rows in L (strictly lower entries only)
647  std::vector<casadi_int> L_row(L_colind.back());
648  SparsityInternal::ldl_row(*this, get_ptr(parent), get_ptr(L_colind), get_ptr(L_row),
649  get_ptr(w));
650  // Sparsity of L^T
651  return Sparsity(n, n, L_colind, L_row, true).T();
652  }
static void ldl_colind(const casadi_int *sp, casadi_int *parent, casadi_int *l_colind, casadi_int *w)
Calculate the column offsets for the L factor of an LDL^T factorization.
static void ldl_row(const casadi_int *sp, const casadi_int *parent, casadi_int *l_colind, casadi_int *l_row, casadi_int *w)
Calculate the row indices for the L factor of an LDL^T factorization.
Sparsity sub(const std::vector< casadi_int > &rr, const std::vector< casadi_int > &cc, std::vector< casadi_int > &mapping, bool ind1=false) const
Get a submatrix.
Definition: sparsity.cpp:334
std::vector< casadi_int > amd() const
Approximate minimal degree preordering.
Definition: sparsity.cpp:709
bool is_symmetric() const
Is symmetric?
Definition: sparsity.cpp:317

References amd(), casadi::get_ptr(), is_symmetric(), ldl(), casadi::SparsityInternal::ldl_colind(), casadi::SparsityInternal::ldl_row(), casadi::range(), size1(), Sparsity(), and sub().

Referenced by casadi::SuperscsInterface::init(), casadi::LinsolLdl::init(), and ldl().

◆ lower()

Sparsity casadi::Sparsity::lower ( casadi_int  n)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bh

Definition at line 1065 of file sparsity.cpp.

1065  {
1066  casadi_assert(n>=0, "Sparsity::lower expects a positive integer as argument");
1067  casadi_int nrow=n, ncol=n;
1068  std::vector<casadi_int> colind, row;
1069  colind.reserve(ncol+1);
1070  row.reserve((n*(n+1))/2);
1071 
1072  // Loop over columns
1073  colind.push_back(0);
1074  for (casadi_int cc=0; cc<ncol; ++cc) {
1075  // Loop over rows for the lower triangular half
1076  for (casadi_int rr=cc; rr<nrow; ++rr) {
1077  row.push_back(rr);
1078  }
1079  colind.push_back(row.size());
1080  }
1081 
1082  // Return the pattern
1083  return Sparsity(nrow, ncol, colind, row);
1084  }

References colind(), row(), and Sparsity().

Referenced by casadi::Convexify::setup(), and casadi::OptiNode::variable().

◆ mac()

static Sparsity casadi::Sparsity::mac ( const Sparsity x,
const Sparsity y,
const Sparsity z,
const std::string &  = "reference" 
)
inlinestatic

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 730 of file sparsity.hpp.

731  { return z;}

◆ makeDense()

Sparsity casadi::Sparsity::makeDense ( std::vector< casadi_int > &  mapping) const

Extra doc: https://github.com/casadi/casadi/wiki/L_cu

Definition at line 584 of file sparsity.cpp.

584  {
585  return (*this)->makeDense(mapping);
586  }

References casadi::SparsityInternal::makeDense().

◆ mtimes()

Sparsity casadi::Sparsity::mtimes ( const Sparsity x,
const Sparsity y,
const std::string &  blas = "reference" 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 430 of file sparsity.cpp.

431  {
432  // Check matching dimensions
433  casadi_assert(x.size2()==y.size1(),
434  "Matrix product with incompatible dimensions. Lhs is "
435  + x.dim() + " and rhs is " + y.dim() + ".");
436 
437  return x->_mtimes(y);
438  }

References casadi::SparsityInternal::_mtimes(), dim(), size1(), and size2().

Referenced by casadi::MX::mtimes(), qr_sparse(), sum1(), and sum2().

◆ mul_activityF()

void casadi::Sparsity::mul_activityF ( const bvec_t x,
const Sparsity x_sp,
const bvec_t y,
const Sparsity y_sp,
bvec_t z,
const Sparsity z_sp,
bvec_t w 
)
static

Like mul_sparsityF, but combines the two factors of each term with AND rather than OR (an inactive factor annihilates that term), so an output is active only where some term has both factors active. No memory allocation; work vector w.

Extra doc: https://github.com/casadi/casadi/wiki/L_2in

Definition at line 1874 of file sparsity.cpp.

1877  {
1878  mul_bvec_fwd<true>(x, x_sp, y, y_sp, z, z_sp, w);
1879  }

Referenced by casadi::Multiplication::eval_activity().

◆ mul_sparsityF()

void casadi::Sparsity::mul_sparsityF ( const bvec_t x,
const Sparsity x_sp,
const bvec_t y,
const Sparsity y_sp,
bvec_t z,
const Sparsity z_sp,
bvec_t w 
)
static

no memory allocation: z = mul(x, y) with work vector Forward mode.

Extra doc: https://github.com/casadi/casadi/wiki/L_co

Definition at line 1867 of file sparsity.cpp.

1870  {
1871  mul_bvec_fwd<false>(x, x_sp, y, y_sp, z, z_sp, w);
1872  }

Referenced by casadi::Multiplication::sp_forward().

◆ mul_sparsityR()

void casadi::Sparsity::mul_sparsityR ( bvec_t x,
const Sparsity x_sp,
bvec_t y,
const Sparsity y_sp,
bvec_t z,
const Sparsity z_sp,
bvec_t w 
)
static

no memory allocation: z = mul(x, y) with work vector Reverse mode.

Extra doc: https://github.com/casadi/casadi/wiki/L_cp

Definition at line 1881 of file sparsity.cpp.

1884  {
1885  // Assert dimensions
1886  casadi_assert(z_sp.size1()==x_sp.size1() && x_sp.size2()==y_sp.size1()
1887  && y_sp.size2()==z_sp.size2(),
1888  "Dimension error. Got x=" + x_sp.dim() + ", y=" + y_sp.dim()
1889  + " and z=" + z_sp.dim() + ".");
1890 
1891  // Direct access to the arrays
1892  const casadi_int* y_colind = y_sp.colind();
1893  const casadi_int* y_row = y_sp.row();
1894  const casadi_int* x_colind = x_sp.colind();
1895  const casadi_int* x_row = x_sp.row();
1896  const casadi_int* z_colind = z_sp.colind();
1897  const casadi_int* z_row = z_sp.row();
1898 
1899  // Clear residual work vector data from preceding operations (not necessary
1900  // for data from this method if conditional clear code is made unconditional
1901  // in loop)
1902  casadi_int nrow = z_sp.size1();
1903  casadi_fill(w, nrow, static_cast<bvec_t>(0));
1904 
1905  // Loop over the columns of y and z
1906  casadi_int ncol = z_sp.size2();
1907  for (casadi_int cc=0; cc<ncol; ++cc) {
1908  // Get the dense column of z
1909  for (casadi_int kk=z_colind[cc]; kk<z_colind[cc+1]; ++kk) {
1910  w[z_row[kk]] = z[kk];
1911  }
1912 
1913  // Loop over the nonzeros of y
1914  for (casadi_int kk=y_colind[cc]; kk<y_colind[cc+1]; ++kk) {
1915  casadi_int rr = y_row[kk];
1916 
1917  // Loop over corresponding columns of x
1918  bvec_t yy = 0;
1919  for (casadi_int kk1=x_colind[rr]; kk1<x_colind[rr+1]; ++kk1) {
1920  yy |= w[x_row[kk1]];
1921  x[kk1] |= w[x_row[kk1]];
1922  }
1923  y[kk] |= yy;
1924  }
1925 
1926  // Get the sparse column of z, clear work vector for next column
1927  for (casadi_int kk=z_colind[cc]; kk<z_colind[cc+1]; ++kk) {
1928  z[kk] = w[z_row[kk]];
1929  w[z_row[kk]] = 0;
1930  }
1931  }
1932  }
unsigned long long bvec_t
void casadi_fill(T1 *x, casadi_int n, T1 alpha)
FILL: x <- alpha.

References casadi::casadi_fill(), colind(), dim(), row(), size1(), and size2().

Referenced by casadi::Multiplication::sp_reverse().

◆ nnz()

casadi_int casadi::Sparsity::nnz ( ) const
See also
numel()

Extra doc: https://github.com/casadi/casadi/wiki/L_bt

Definition at line 148 of file sparsity.cpp.

148  {
149  return (*this)->nnz();
150  }

Referenced by casadi::Matrix< Scalar >::_sym(), casadi::MX::_sym(), casadi::SetNonzeros< Add >::ad_forward(), casadi::GetNonzeros::ad_reverse(), casadi::SetNonzeros< Add >::ad_reverse(), add(), add_nz(), bor(), casadi::Switch::codegen_body(), casadi::FastNewton::codegen_body(), casadi::Sqpmethod::codegen_body(), casadi::OsqpInterface::codegen_init_mem(), casadi::Sqpmethod::codegen_solve_elastic_mode(), casadi::SparsityInternal::combineGen1(), compressed(), casadi::ConstantFile::ConstantFile(), casadi::ConstantMX::create(), density(), casadi::FmuFunction::eval(), casadi::Switch::eval(), casadi::HorzRepsum::eval_gen(), casadi::AlpaqaProblem::eval_hess_L(), casadi::AlpaqaProblem::eval_hess_ψ(), casadi::AlpaqaProblem::eval_jac_g(), casadi::Scpgen::eval_mat(), casadi::SetNonzeros< Add >::eval_mx(), casadi::Switch::eval_sx(), casadi::LinsolQr::finalize(), from_file(), casadi::LinsolLdl::generate(), casadi::HorzRepsum::generate(), casadi::UnaryMX::generate(), casadi::CodeGenerator::generate_dump(), casadi::LinsolQr::generate_factorize(), casadi::get_const(), casadi::AlpaqaProblem::get_hess_L_num_nonzeros(), casadi::AlpaqaProblem::get_hess_ψ_num_nonzeros(), casadi::AlpaqaProblem::get_jac_g_num_nonzeros(), casadi::FunctionInternal::get_jac_sparsity_gen(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::BonminInterface::get_nlp_info(), casadi::IpoptInterface::get_nlp_info(), casadi::Call::get_output(), casadi::MultipleOutput::get_output(), casadi::MXNode::get_project(), casadi::MXNode::get_sparsity_cast(), casadi::FunctionInternal::get_sparsity_out(), horzcat(), casadi::FmuFunction::init(), casadi::Switch::init(), casadi::AmplInterface::init(), casadi::Blocksqp::init(), casadi::BonminInterface::init(), casadi::CbcInterface::init(), casadi::ClpInterface::init(), casadi::FatropConicInterface::init(), casadi::FatropInterface::init(), casadi::HpipmInterface::init(), casadi::HpmpcInterface::init(), casadi::IpoptInterface::init(), casadi::MosekInterface::init(), casadi::OsqpInterface::init(), casadi::PiqpInterface::init(), casadi::SLEQPInterface::init(), casadi::SnoptInterface::init(), casadi::KinsolInterface::init(), casadi::SundialsInterface::init(), casadi::SuperscsInterface::init(), casadi::XpressInterface::init(), casadi::FastNewton::init(), casadi::Feasiblesqpmethod::init(), casadi::Ipqp::init(), casadi::Newton::init(), casadi::Qrqp::init(), casadi::Qrsqp::init(), casadi::Scpgen::init(), casadi::Sqpmethod::init(), casadi::ClarabelInterface::init_dependent(), casadi::HighsInterface::init_dependent(), casadi::MosekInterface::init_dependent(), casadi::XpressInterface::init_dependent(), casadi::Blocksqp::init_mem(), casadi::CbcInterface::init_mem(), casadi::ClpInterface::init_mem(), casadi::CplexInterface::init_mem(), casadi::HpmpcInterface::init_mem(), casadi::OsqpInterface::init_mem(), casadi::PiqpInterface::init_mem(), casadi::ProxqpInterface::init_mem(), casadi::QpoasesInterface::init_mem(), casadi::SuperscsInterface::init_mem(), casadi::LinsolLdl::init_mem(), casadi::LinsolQr::init_mem(), casadi::Lsqr::init_mem(), kron(), casadi::MosekInterface::MosekInterface(), casadi::MX::MX(), casadi::LinsolQr::nfact(), norm_0_mul(), casadi::FunctionInternal::print_canonical(), casadi::Matrix< Scalar >::print_default(), casadi::Matrix< Scalar >::print_dense(), casadi::MXFunction::print_res(), casadi::Matrix< Scalar >::print_sparse(), casadi::Matrix< Scalar >::print_vector(), casadi::CodeGenerator::project(), casadi::QpoasesInterface::qpoases_init(), casadi::Reshape::Reshape(), row(), set(), casadi::MadmpecInterface::set_ccopt_prob(), casadi::FatropInterface::set_fatrop_prob(), casadi::IpoptInterface::set_ipopt_prob(), casadi::MadnlpInterface::set_madnlp_prob(), casadi::Blocksqp::set_work(), casadi::BonminInterface::set_work(), casadi::IpoptInterface::set_work(), casadi::SLEQPInterface::set_work(), casadi::SnoptInterface::set_work(), casadi::KinsolInterface::set_work(), casadi::SundialsInterface::set_work(), casadi::WorhpInterface::set_work(), casadi::FastNewton::set_work(), casadi::Newton::set_work(), casadi::Qrsqp::set_work(), casadi::Scpgen::set_work(), casadi::Convexify::setup(), casadi::CplexInterface::solve(), casadi::PiqpInterface::solve(), casadi::ProxqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::SuperscsInterface::solve(), casadi::Ipqp::solve(), casadi::KnitroInterface::solve(), casadi::Feasiblesqpmethod::solve(), casadi::Qrsqp::solve(), casadi::Sqpmethod::solve(), casadi::Sqpmethod::solve_elastic_mode(), casadi::MXNode::sp_forward(), casadi::HorzRepsum::sp_forward(), casadi::Split::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::MXNode::sp_reverse(), casadi::HorzRepmat::sp_reverse(), casadi::HorzRepsum::sp_reverse(), casadi::Split::sp_reverse(), casadi::FunctionInternal::sp_reverse(), casadi::MX::sparsity_cast(), sparsity_cast(), casadi::SparsityCast::SparsityCast(), to_file(), vertcat(), and casadi::XpressInterface::XpressInterface().

◆ nnz_diag()

casadi_int casadi::Sparsity::nnz_diag ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_bw

Definition at line 360 of file sparsity.cpp.

360  {
361  return (*this)->nnz_diag();
362  }

◆ nnz_lower()

casadi_int casadi::Sparsity::nnz_lower ( bool  strictly = false) const

i.e. the number of elements (i, j) with j<=i

Extra doc: https://github.com/casadi/casadi/wiki/L_bv

Definition at line 352 of file sparsity.cpp.

352  {
353  return (*this)->nnz_lower(strictly);
354  }

Referenced by casadi::WorhpInterface::set_work().

◆ nnz_upper()

casadi_int casadi::Sparsity::nnz_upper ( bool  strictly = false) const

i.e. the number of elements (i, j) with j>=i

Extra doc: https://github.com/casadi/casadi/wiki/L_bu

Definition at line 356 of file sparsity.cpp.

356  {
357  return (*this)->nnz_upper(strictly);
358  }

Referenced by casadi::OsqpInterface::codegen_init_mem(), casadi::OoqpInterface::init(), casadi::OsqpInterface::init(), casadi::MumpsInterface::init_mem(), and casadi::OsqpInterface::init_mem().

◆ nonzeros()

Sparsity casadi::Sparsity::nonzeros ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  nz,
bool  ind1 = SWIG_IND1 
)
static

Inverse of find()

Extra doc: https://github.com/casadi/casadi/wiki/L_23g

Definition at line 1301 of file sparsity.cpp.

1302  {
1303  casadi_assert(nrow>0, "nrow must be >0.");
1304  std::vector<casadi_int> row(nz.size());
1305  std::vector<casadi_int> col(nz.size());
1306  for (casadi_int i=0;i<nz.size();++i) {
1307  casadi_int k = nz[i];
1308  k-= ind1;
1309  row[i] = k % nrow;
1310  col[i] = k / nrow;
1311  }
1312  return triplet(nrow, ncol, row, col);
1313  }

References row(), and triplet().

◆ norm_0_mul()

casadi_int casadi::Sparsity::norm_0_mul ( const Sparsity x,
const Sparsity A 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1738 of file sparsity.cpp.

1738  {
1739  // Implementation borrowed from Scipy's sparsetools/csr.h
1740  casadi_assert(A.size1()==x.size2(), "Dimension error. Got " + x.dim()
1741  + " times " + A.dim() + ".");
1742 
1743  casadi_int n_row = A.size2();
1744  casadi_int n_col = x.size1();
1745 
1746  // Allocate work vectors
1747  std::vector<bool> Bwork(n_col);
1748  std::vector<casadi_int> Iwork(n_row+1+n_col);
1749 
1750  const casadi_int* Aj = A.row();
1751  const casadi_int* Ap = A.colind();
1752  const casadi_int* Bj = x.row();
1753  const casadi_int* Bp = x.colind();
1754  casadi_int *Cp = get_ptr(Iwork);
1755  casadi_int *mask = Cp+n_row+1;
1756 
1757  // Pass 1
1758  // method that uses O(n) temp storage
1759  std::fill(mask, mask+n_col, -1);
1760 
1761  Cp[0] = 0;
1762  casadi_int nnz = 0;
1763  for (casadi_int i = 0; i < n_row; i++) {
1764  casadi_int row_nnz = 0;
1765  for (casadi_int jj = Ap[i]; jj < Ap[i+1]; jj++) {
1766  casadi_int j = Aj[jj];
1767  for (casadi_int kk = Bp[j]; kk < Bp[j+1]; kk++) {
1768  casadi_int k = Bj[kk];
1769  if (mask[k] != i) {
1770  mask[k] = i;
1771  row_nnz++;
1772  }
1773  }
1774  }
1775  casadi_int next_nnz = nnz + row_nnz;
1776  nnz = next_nnz;
1777  Cp[i+1] = nnz;
1778  }
1779 
1780  // Pass 2
1781  casadi_int *next = get_ptr(Iwork) + n_row+1;
1782  std::fill(next, next+n_col, -1);
1783  std::vector<bool> & sums = Bwork;
1784  std::fill(sums.begin(), sums.end(), false);
1785  nnz = 0;
1786  Cp[0] = 0;
1787  for (casadi_int i = 0; i < n_row; i++) {
1788  casadi_int head = -2;
1789  casadi_int length = 0;
1790  casadi_int jj_start = Ap[i];
1791  casadi_int jj_end = Ap[i+1];
1792  for (casadi_int jj = jj_start; jj < jj_end; jj++) {
1793  casadi_int j = Aj[jj];
1794  casadi_int kk_start = Bp[j];
1795  casadi_int kk_end = Bp[j+1];
1796  for (casadi_int kk = kk_start; kk < kk_end; kk++) {
1797  casadi_int k = Bj[kk];
1798  sums[k] = true;
1799  if (next[k] == -1) {
1800  next[k] = head;
1801  head = k;
1802  length++;
1803  }
1804  }
1805  }
1806  for (casadi_int jj = 0; jj < length; jj++) {
1807  if (sums[head]) {
1808  nnz++;
1809  }
1810  casadi_int temp = head;
1811  head = next[head];
1812  next[temp] = -1; //clear arrays
1813  sums[temp] = false;
1814  }
1815  Cp[i+1] = nnz;
1816  }
1817  return nnz;
1818  }

References colind(), dim(), casadi::get_ptr(), nnz(), row(), size1(), and size2().

Referenced by casadi::GenericMatrix< MatType >::norm_0_mul().

◆ numel()

casadi_int casadi::Sparsity::numel ( ) const

Beware of overflow

See also
nnz()

Extra doc: https://github.com/casadi/casadi/wiki/L_bq

Definition at line 132 of file sparsity.cpp.

132  {
133  return (*this)->numel();
134  }

Referenced by casadi::FunctionInternal::get_sparsity_out(), casadi::Nlpsol::init(), casadi::Matrix< Scalar >::print_default(), and casadi::reduce_index_gen().

◆ operator const casadi_int *()

casadi::Sparsity::operator const casadi_int * ( ) const

In the C runtime, sparsity patterns are represented as a "const casadi_int*". This enables using the C runtime functions using a natural syntax.

Extra doc: https://github.com/casadi/casadi/wiki/L_bn

Definition at line 1734 of file sparsity.cpp.

1734  {
1735  return &(*this)->sp().front();
1736  }

◆ operator const std::vector< casadi_int > &()

casadi::Sparsity::operator const std::vector< casadi_int > & ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_bo

Definition at line 1330 of file sparsity.cpp.

1330  {
1331  return (*this)->sp();
1332  }

◆ operator SparsityStruct()

casadi::Sparsity::operator SparsityStruct ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_bp

Definition at line 1334 of file sparsity.cpp.

1334  {
1335  const casadi_int* sp = *this;
1336  casadi_int nrow = sp[0], ncol = sp[1];
1337  const casadi_int* colind = sp+2, *row = sp+2+ncol+1;
1338  return SparsityStruct{nrow, ncol, colind, row};
1339  }

◆ operator!=()

bool casadi::Sparsity::operator!= ( const Sparsity y) const
inline

Definition at line 302 of file sparsity.hpp.

302 {return !is_equal(y);}
bool is_equal(const Sparsity &y) const
Definition: sparsity.cpp:444

References casadi::is_equal().

◆ operator*() [1/2]

const SparsityInternal & casadi::Sparsity::operator* ( ) const

Definition at line 116 of file sparsity.cpp.

116  {
117  return *static_cast<const SparsityInternal*>(get());
118  }
SparsityInternal * get() const
Definition: sparsity.cpp:2109

References get().

◆ operator*() [2/2]

Sparsity casadi::Sparsity::operator* ( const Sparsity b) const

Returns the new sparsity pattern as well as a mapping with the same length as the number of non-zero elements The value is 1 if the non-zero comes from the first (i.e. this) object, 2 if it is from the second and 3 (i.e. 1 | 2) if from both

Extra doc: https://github.com/casadi/casadi/wiki/L_cn

Definition at line 462 of file sparsity.cpp.

462  {
463  std::vector< unsigned char > mapping;
464  return intersect(b, mapping);
465  }
Sparsity intersect(const Sparsity &y, std::vector< unsigned char > &mapping) const
Intersection of two sparsity patterns.
Definition: sparsity.cpp:417

References intersect().

◆ operator+()

Sparsity casadi::Sparsity::operator+ ( const Sparsity b) const

Extra doc: https://github.com/casadi/casadi/wiki/L_cm

Definition at line 458 of file sparsity.cpp.

458  {
459  return unite(b);
460  }
Sparsity unite(const Sparsity &y, std::vector< unsigned char > &mapping) const
Union of two sparsity patterns.
Definition: sparsity.cpp:409

References unite().

◆ operator->()

const SparsityInternal * casadi::Sparsity::operator-> ( ) const

Definition at line 112 of file sparsity.cpp.

112  {
113  return static_cast<const SparsityInternal*>(SharedObject::operator->());
114  }
SharedObjectInternal * operator->() const
Access a member function or object.

References casadi::GenericShared< SharedObject, SharedObjectInternal >::operator->().

◆ operator==()

bool casadi::Sparsity::operator== ( const Sparsity y) const
inline

Definition at line 298 of file sparsity.hpp.

298 { return is_equal(y);}

References casadi::is_equal().

◆ own()

void casadi::GenericShared< SharedObject , SharedObjectInternal >::own ( Internal *  node)
inherited

Assign the node to a node class pointer (or null)

Definition at line 89 of file generic_shared_impl.hpp.

◆ pattern_inverse()

Sparsity casadi::Sparsity::pattern_inverse ( ) const

Definition at line 467 of file sparsity.cpp.

467  {
468  return (*this)->pattern_inverse();
469  }

References casadi::SparsityInternal::pattern_inverse().

Referenced by casadi::HpipmInterface::init(), and casadi::HpmpcInterface::init().

◆ permutation()

Sparsity casadi::Sparsity::permutation ( const std::vector< casadi_int > &  p,
bool  invert = false 
)
static

Right multiplication of P with a vector leads to the same results as indexing that vector with p

P @ v = v[p]

The inverse of a permutation matrix is equal to its transpose (property of orthonormality)

Extra doc: https://github.com/casadi/casadi/wiki/L_244

Definition at line 1380 of file sparsity.cpp.

1380  {
1381  casadi_assert(casadi::is_permutation(p),
1382  "Sparsity::permutation supplied list is not a permutation.");
1383  std::vector<casadi_int> colind = range(p.size()+1);
1384  if (invert) {
1385  return Sparsity(p.size(), p.size(), colind, p);
1386  } else {
1387  return Sparsity(p.size(), p.size(), colind, invert_permutation(p));
1388  }
1389  }
std::vector< casadi_int > invert_permutation(const std::vector< casadi_int > &a)
inverse a permutation vector
bool is_permutation(const std::vector< casadi_int > &order)
Does the list represent a permutation?

References colind(), casadi::invert_permutation(), casadi::is_permutation(), casadi::range(), and Sparsity().

◆ permutation_vector()

const std::vector< casadi_int > casadi::Sparsity::permutation_vector ( bool  invert = false) const

Extra doc: https://github.com/casadi/casadi/wiki/L_245

Definition at line 1391 of file sparsity.cpp.

1391  {
1392  casadi_assert(is_permutation(), "Sparsity::permutation called on non-permutation matrix.");
1393  if (invert) {
1394  return get_row();
1395  } else {
1396  return invert_permutation(get_row());
1397  }
1398  }
bool is_permutation() const
Is this a permutation matrix?
Definition: sparsity.cpp:297

References get_row(), casadi::invert_permutation(), and is_permutation().

Referenced by casadi::MXNode::get_mac().

◆ pmult()

Sparsity casadi::Sparsity::pmult ( const std::vector< casadi_int > &  p,
bool  permute_rows = true,
bool  permute_columns = true,
bool  invert_permutation = false 
) const

Multiply the sparsity with a permutation matrix from the left and/or from the right P * A * trans(P), A * trans(P) or A * trans(P) with P defined by an index vector containing the row for each col. As an alternative, P can be transposed (inverted).

Extra doc: https://github.com/casadi/casadi/wiki/L_df

Definition at line 780 of file sparsity.cpp.

781  {
782  return (*this)->pmult(p, permute_rows, permute_columns, invert_permutation);
783  }

References casadi::invert_permutation(), and casadi::SparsityInternal::pmult().

Referenced by casadi::SparsityInternal::star_coloring(), and casadi::SparsityInternal::star_coloring2().

◆ postfix_dim()

std::string casadi::Sparsity::postfix_dim ( ) const

Rules:

  1. Dense and scalar: ""
  2. 0-by-0: "[]"
  3. Dense column vector: "[5]"
  4. Dense matrix: "[5x10]"
  5. Otherwise: "[5x10,3nz]"

    Extra doc: https://github.com/casadi/casadi/wiki/L_dg

Definition at line 592 of file sparsity.cpp.

592  {
593  if (is_dense()) {
594  if (is_scalar()) {
595  return "";
596  } else if (is_empty(true)) {
597  return "[]";
598  } else if (is_column()) {
599  return "[" + str(size1()) + "]";
600  } else {
601  return "[" + dim(false) + "]";
602  }
603  } else {
604  return "[" + dim(true) + "]";
605  }
606  }
bool is_scalar(bool scalar_and_dense=false) const
Is scalar?
Definition: sparsity.cpp:269

References dim(), is_column(), is_dense(), is_empty(), is_scalar(), size1(), and casadi::str().

◆ print_ptr()

void casadi::SharedObject::print_ptr ( std::ostream &  stream = casadi::uout()) const
inherited

Print the pointer to the internal class

Definition at line 43 of file shared_object.cpp.

43  {
44  stream << get();
45  }

References casadi::GenericShared< SharedObject, SharedObjectInternal >::get().

◆ qr_sparse()

void casadi::Sparsity::qr_sparse ( Sparsity V,
Sparsity R,
std::vector< casadi_int > &  prinv,
std::vector< casadi_int > &  pc,
bool  amd = true 
) const

Returns the sparsity pattern of V (compact representation of Q) and R as well as vectors needed for the numerical factorization and solution. The implementation is a modified version of CSparse Copyright(c) Timothy A. Davis, 2006-2009 Licensed as a derivative work under the GNU LGPL

Extra doc: https://github.com/casadi/casadi/wiki/L_d4

Definition at line 654 of file sparsity.cpp.

656  {
657  // Dimensions
658  casadi_int size1=this->size1(), size2=this->size2();
659 
660  // Recursive call if AMD
661  if (amd) {
662  // Get AMD reordering
663  pc = mtimes(T(), *this).amd();
664  // Permute sparsity pattern
665  std::vector<casadi_int> tmp;
666  Sparsity Aperm = sub(range(size1), pc, tmp);
667  // Call recursively
668  Aperm.qr_sparse(V, R, prinv, tmp, false);
669  return;
670  }
671 
672  // No column permutation
673  pc = range(size2);
674 
675  // Allocate memory
676  std::vector<casadi_int> leftmost(size1);
677  std::vector<casadi_int> parent(size2);
678  prinv.resize(size1 + size2);
679  std::vector<casadi_int> iw(size1 + 7*size2 + 1);
680 
681  // Initialize QP solve
682  casadi_int nrow_ext, v_nnz, r_nnz;
683  SparsityInternal::qr_init(*this, T(),
684  get_ptr(leftmost), get_ptr(parent), get_ptr(prinv),
685  &nrow_ext, &v_nnz, &r_nnz, get_ptr(iw));
686 
687  // Calculate sparsities
688  std::vector<casadi_int> sp_v(2 + size2 + 1 + v_nnz);
689  std::vector<casadi_int> sp_r(2 + size2 + 1 + r_nnz);
690  SparsityInternal::qr_sparsities(*this, nrow_ext, get_ptr(sp_v), get_ptr(sp_r),
691  get_ptr(leftmost), get_ptr(parent), get_ptr(prinv),
692  get_ptr(iw));
693  prinv.resize(nrow_ext);
694  V = compressed(sp_v, true);
695  R = compressed(sp_r, true);
696  }
static void qr_init(const casadi_int *sp, const casadi_int *sp_tr, casadi_int *leftmost, casadi_int *parent, casadi_int *pinv, casadi_int *nrow_ext, casadi_int *v_nnz, casadi_int *r_nnz, casadi_int *w)
Setup QP solver.
static void qr_sparsities(const casadi_int *sp_a, casadi_int nrow_ext, casadi_int *sp_v, casadi_int *sp_r, const casadi_int *leftmost, const casadi_int *parent, const casadi_int *pinv, casadi_int *iw)
Get the row indices for V and R in QR factorization.
static Sparsity mtimes(const Sparsity &x, const Sparsity &y, const std::string &blas="reference")
Enlarge matrix.
Definition: sparsity.cpp:430

References amd(), compressed(), casadi::get_ptr(), mtimes(), casadi::SparsityInternal::qr_init(), qr_sparse(), casadi::SparsityInternal::qr_sparsities(), casadi::range(), size1(), size2(), sub(), and T().

Referenced by casadi::FastNewton::init(), casadi::LinsolQr::init(), casadi::Qrqp::init(), and qr_sparse().

◆ removeDuplicates()

void casadi::Sparsity::removeDuplicates ( std::vector< casadi_int > &  mapping)

The same indices will be removed from the mapping vector, which must have the same length as the number of nonzeros

Extra doc: https://github.com/casadi/casadi/wiki/L_d1

Definition at line 733 of file sparsity.cpp.

733  {
734  *this = (*this)->_removeDuplicates(mapping);
735  }

◆ repr_el()

std::string casadi::Sparsity::repr_el ( casadi_int  k) const

Definition at line 608 of file sparsity.cpp.

608  {
609  return (*this)->repr_el(k);
610  }

Referenced by casadi::OracleFunction::calc_function().

◆ reshape() [1/2]

Sparsity casadi::Sparsity::reshape ( const Sparsity x,
casadi_int  nrow,
casadi_int  ncol 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 260 of file sparsity.cpp.

260  {
261  return x->_reshape(nrow, ncol);
262  }

References casadi::SparsityInternal::_reshape().

Referenced by casadi::MX::reshape().

◆ reshape() [2/2]

Sparsity casadi::Sparsity::reshape ( const Sparsity x,
const Sparsity sp 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 250 of file sparsity.cpp.

250  {
251  casadi_assert_dev(x.is_reshape(sp));
252  return sp;
253  }

References is_reshape().

◆ resize()

void casadi::Sparsity::resize ( casadi_int  nrow,
casadi_int  ncol 
)

Definition at line 188 of file sparsity.cpp.

188  {
189  if (size1()!=nrow || size2() != ncol) {
190  *this = (*this)->_resize(nrow, ncol);
191  }
192  }

References size1(), and size2().

Referenced by casadi::SparsityInternal::combineGen1(), casadi::SparsityInternal::dmperm(), and casadi::SparsityInternal::pmult().

◆ row() [1/2]

const casadi_int * casadi::Sparsity::row ( ) const

containing rows for all non-zero elements (see class description)

Extra doc: https://github.com/casadi/casadi/wiki/L_c7

Definition at line 164 of file sparsity.cpp.

164  {
165  return (*this)->row();
166  }

Referenced by casadi::SparsityInternal::_mtimes(), casadi::GetNonzeros::ad_forward(), casadi::SetNonzeros< Add >::ad_forward(), casadi::SetNonzeros< Add >::ad_reverse(), add(), add_nz(), band(), casadi::SparsityInternal::bfs(), casadi::KinsolInterface::bjac(), bor(), casadi::Blocksqp::calcHessianUpdateExact(), casadi::Blocksqp::calcLagrangeGradient(), casadi::casadi_func_cons_jac(), casadi::SparsityInternal::combineGen(), compressed(), casadi::construct_nlpsol(), casadi::IndexReduction::dae_struct_detect(), dense(), casadi::detect_simple_bounds_gen(), diag(), diagcat(), casadi::KinsolInterface::djac(), casadi::BonminUserClass::eval_h(), casadi::IpoptUserClass::eval_h(), casadi::AlpaqaProblem::eval_hess_L(), casadi::AlpaqaProblem::eval_hess_ψ(), casadi::AlpaqaProblem::eval_jac_g(), casadi::BonminUserClass::eval_jac_g(), casadi::IpoptUserClass::eval_jac_g(), casadi::GetNonzeros::eval_mx(), casadi::SetNonzeros< Add >::eval_mx(), casadi::FmuFunction::eval_task(), casadi::FmuFunction::finalize_hessian(), from_file(), get_ccs(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::FunctionInternal::get_sparsity_out(), get_triplet(), horzsplit(), casadi::SqicInterface::init(), casadi::FmuFunction::init(), casadi::AmplInterface::init(), casadi::Blocksqp::init(), casadi::FatropConicInterface::init(), casadi::HpipmInterface::init(), casadi::HpmpcInterface::init(), casadi::SuperscsInterface::init(), casadi::ClarabelInterface::init_dependent(), casadi::HighsInterface::init_dependent(), casadi::MosekInterface::init_dependent(), casadi::XpressInterface::init_dependent(), casadi::CplexInterface::init_mem(), is_compactible(), is_equal(), casadi::SparsityInternal::is_equal(), casadi::SparsityInternal::is_stacked(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), kron(), kron_contract(), lower(), casadi::mul_bvec_fwd(), mul_sparsityR(), casadi::SparsityInternal::multiply(), nonzeros(), norm_0_mul(), casadi::FunctionInternal::print_canonical(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::Matrix< Scalar >::print_vector(), casadi::CvodesInterface::psetupF(), casadi::IdasInterface::psetupF(), casadi::GenericMatrix< MatType >::row(), row(), rowcol(), casadi::Conic::sdp_to_socp_init(), set(), casadi::WorhpInterface::set_work(), casadi::CbcInterface::solve(), casadi::ClpInterface::solve(), casadi::CplexInterface::solve(), casadi::GurobiInterface::solve(), casadi::OoqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::SuperscsInterface::solve(), casadi::WorhpInterface::solve(), casadi::Blocksqp::solveQP(), casadi::KronContract::sp_forward(), casadi::Rank1::sp_forward(), casadi::Solve< Tr >::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::KronContract::sp_reverse(), casadi::Rank1::sp_reverse(), casadi::Solve< Tr >::sp_reverse(), casadi::FunctionInternal::sp_reverse(), Sparsity(), casadi::SparsityInternal::star_coloring_new(), to_file(), triplet(), casadi::SparsityInternal::uni_coloring(), unit(), casadi::DaeBuilderInternal::update_dependencies(), and upper().

◆ row() [2/2]

casadi_int casadi::Sparsity::row ( casadi_int  el) const

Extra doc: https://github.com/casadi/casadi/wiki/L_cc

Definition at line 172 of file sparsity.cpp.

172  {
173  if (el<0 || el>=nnz()) {
174  throw std::out_of_range("Sparsity::row: Index " + str(el)
175  + " out of range [0," + str(nnz()) + ")");
176  }
177  return row()[el];
178  }

References nnz(), row(), and casadi::str().

◆ rowcol()

Sparsity casadi::Sparsity::rowcol ( const std::vector< casadi_int > &  row,
const std::vector< casadi_int > &  col,
casadi_int  nrow,
casadi_int  ncol 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bl

Definition at line 1127 of file sparsity.cpp.

1128  {
1129  std::vector<casadi_int> all_rows, all_cols;
1130  all_rows.reserve(row.size()*col.size());
1131  all_cols.reserve(row.size()*col.size());
1132  for (std::vector<casadi_int>::const_iterator c_it=col.begin(); c_it!=col.end(); ++c_it) {
1133  casadi_assert(*c_it>=0 && *c_it<ncol, "Sparsity::rowcol: Column index out of bounds");
1134  for (std::vector<casadi_int>::const_iterator r_it=row.begin(); r_it!=row.end(); ++r_it) {
1135  casadi_assert(*r_it>=0 && *r_it<nrow, "Sparsity::rowcol: Row index out of bounds");
1136  all_rows.push_back(*r_it);
1137  all_cols.push_back(*c_it);
1138  }
1139  }
1140  return Sparsity::triplet(nrow, ncol, all_rows, all_cols);
1141  }

References row(), and triplet().

◆ rows()

casadi_int casadi::Sparsity::rows ( ) const
inline

Definition at line 334 of file sparsity.hpp.

334 {return size1();}

◆ rowsSequential()

bool casadi::Sparsity::rowsSequential ( bool  strictly = true) const
Parameters
[in]strictlyif true, then do not allow multiple entries

Extra doc: https://github.com/casadi/casadi/wiki/L_d0

Definition at line 729 of file sparsity.cpp.

729  {
730  return (*this)->rowsSequential(strictly);
731  }

◆ scalar()

static Sparsity casadi::Sparsity::scalar ( bool  dense_scalar = true)
inlinestatic

◆ scc()

casadi_int casadi::Sparsity::scc ( std::vector< casadi_int > &  index,
std::vector< casadi_int > &  offset 
) const

of a square matrix

See Direct Methods for Sparse Linear Systems by Davis (2006). Returns:

  • Number of components
  • Offset for each components (length: 1 + number of components)
  • Indices for each components, component i has indices index[offset[i]], ..., index[offset[i+1]]

In the case that the matrix is symmetric, the result has a particular interpretation: Given a symmetric matrix A and n = A.scc(p, r)

=> A[p, p] will appear block-diagonal with n blocks and with the indices of the block boundaries to be found in r.

The implementation is a modified version of cs_scc in CSparse Copyright(c) Timothy A. Davis, 2006-2009 Licensed as a derivative work under the GNU LGPL

Extra doc: https://github.com/casadi/casadi/wiki/L_d6

Examples
matrix/scc.py.

Definition at line 705 of file sparsity.cpp.

705  {
706  return (*this)->scc(index, offset);
707  }

References casadi::SparsityInterface< Sparsity >::offset().

Referenced by casadi::Conic::sdp_to_socp_init(), and casadi::Convexify::setup().

◆ serialize() [1/3]

std::string casadi::Sparsity::serialize ( ) const

Extra doc: https://github.com/casadi/casadi/wiki/L_c2

Definition at line 2097 of file sparsity.cpp.

2097  {
2098  std::stringstream ss;
2099  serialize(ss);
2100  return ss.str();
2101  }
std::string serialize() const
Serialize.
Definition: sparsity.cpp:2097

Referenced by serialize().

◆ serialize() [2/3]

void casadi::Sparsity::serialize ( SerializingStream s) const

Extra doc: https://github.com/casadi/casadi/wiki/L_c5

Definition at line 2079 of file sparsity.cpp.

2079  {
2080  if (is_null()) {
2081  s.pack("SparsityInternal::compressed", std::vector<casadi_int>{});
2082  } else {
2083  s.pack("SparsityInternal::compressed", compress());
2084  }
2085  }
std::vector< casadi_int > compress(bool canonical=true) const
Compress a sparsity pattern.
Definition: sparsity.cpp:1321

References compress(), casadi::GenericShared< SharedObject, SharedObjectInternal >::is_null(), and casadi::SerializingStream::pack().

◆ serialize() [3/3]

void casadi::Sparsity::serialize ( std::ostream &  stream) const

Extra doc: https://github.com/casadi/casadi/wiki/L_c1

Definition at line 2069 of file sparsity.cpp.

2069  {
2070  SerializingStream s(stream);
2071  serialize(s);
2072  }

References serialize().

◆ set() [1/2]

template<typename DataType >
void casadi::Sparsity::set ( DataType *  data,
const DataType *  val_data,
const Sparsity val_sp 
) const

Definition at line 1270 of file sparsity.hpp.

1270  {
1271  // Get dimensions of this
1272  const casadi_int sz = nnz();
1273  const casadi_int sz1 = size1();
1274  const casadi_int sz2 = size2();
1275 
1276  // Get dimensions of assigning matrix
1277  const casadi_int val_sz = val_sp.nnz();
1278  const casadi_int val_sz1 = val_sp.size1();
1279  const casadi_int val_sz2 = val_sp.size2();
1280  const casadi_int val_nel = val_sz1*val_sz2;
1281 
1282  // Check if sparsity matches
1283  if (val_sp==*this) {
1284  std::copy(val_data, val_data+sz, data);
1285  } else if (this->is_empty()) {
1286  // Quick return
1287  return;
1288  } else if (val_sp.is_empty()) {
1289  // Quick return
1290  return;
1291  } else if (val_nel==1) { // if scalar
1292  std::fill(data, data+sz, val_sz==0 ? DataType(0) : val_data[0]);
1293  } else if (sz2==val_sz2 && sz1==val_sz1) {
1294  // Matching dimensions
1295  // Sparsity
1296  const casadi_int* c = row();
1297  const casadi_int* rind = colind();
1298  const casadi_int* v_c = val_sp.row();
1299  const casadi_int* v_rind = val_sp.colind();
1300 
1301  // For all columns
1302  for (casadi_int i=0; i<sz2; ++i) {
1303 
1304  // Nonzero of the assigning matrix
1305  casadi_int v_el = v_rind[i];
1306 
1307  // First nonzero of the following column
1308  casadi_int v_el_end = v_rind[i+1];
1309 
1310  // Next row of the assigning matrix
1311  casadi_int v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1312 
1313  // Assign all nonzeros
1314  for (casadi_int el=rind[i]; el!=rind[i+1]; ++el) {
1315 
1316  // Get row
1317  casadi_int j=c[el];
1318 
1319  // Forward the assigning nonzero
1320  while (v_j<j) {
1321  v_el++;
1322  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1323  }
1324 
1325  // Assign nonzero
1326  if (v_j==j) {
1327  data[el] = val_data[v_el++];
1328  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1329  } else {
1330  data[el] = 0;
1331  }
1332  }
1333  }
1334  } else if (sz1==val_sz2 && sz2==val_sz1 && sz2 == 1) {
1335  // Assign transposed (this is column)
1336  const casadi_int* v_cind = val_sp.colind();
1337  const casadi_int* r = row();
1338  for (casadi_int el=0; el<sz; ++el) {
1339  casadi_int rr=r[el];
1340  data[el] = v_cind[rr]==v_cind[rr+1] ? 0 : val_data[v_cind[rr]];
1341  }
1342  } else if (sz1==val_sz2 && sz2==val_sz1 && sz1 == 1) {
1343  // Assign transposed (this is row)
1344  for (casadi_int el=0; el<sz; ++el) data[el] = 0;
1345  const casadi_int* cind = colind();
1346  const casadi_int* v_r = val_sp.row();
1347  for (casadi_int el=0; el<val_sz; ++el) {
1348  casadi_int rr=v_r[el];
1349  if (cind[rr]!=cind[rr+1]) {
1350  data[cind[rr]] = val_data[el];
1351  }
1352  }
1353  } else {
1354  // Make sure that dimension matches
1355  casadi_error("Sparsity::set<DataType>: shape mismatch. lhs is "
1356  + dim() + ", while rhs is " + val_sp.dim() + ".");
1357  }
1358  }

References colind(), dim(), is_empty(), nnz(), row(), size1(), and size2().

◆ set() [2/2]

template<typename T >
void casadi::Sparsity::set ( T data,
const T val_data,
const Sparsity val_sp 
) const

entries of another sparsity pattern

Extra doc: https://github.com/casadi/casadi/wiki/L_dl

Referenced by casadi::Project::sp_forward().

◆ size() [1/2]

std::pair< casadi_int, casadi_int > casadi::Sparsity::size ( ) const

◆ size() [2/2]

casadi_int casadi::Sparsity::size ( casadi_int  axis) const

Extra doc: https://github.com/casadi/casadi/wiki/L_c0

Definition at line 156 of file sparsity.cpp.

156  {
157  switch (axis) {
158  case 1: return size1();
159  case 2: return size2();
160  }
161  casadi_error("Axis must be 1 or 2.");
162  }

References size1(), and size2().

◆ size1()

casadi_int casadi::Sparsity::size1 ( ) const

Definition at line 124 of file sparsity.cpp.

124  {
125  return (*this)->size1();
126  }

Referenced by casadi::SolveUnity< Tr >::A_sp(), casadi::GetNonzeros::ad_forward(), casadi::SetNonzeros< Add >::ad_forward(), casadi::SetNonzeros< Add >::ad_reverse(), add(), add_nz(), append(), appendColumns(), casadi::Function::assert_sparsity_out(), bor(), casadi::FunctionInternal::check_mat(), casadi::FastNewton::codegen_body(), casadi::FunctionInternal::codegen_meta(), casadi::SparsityInternal::combineGen(), compress(), casadi::Conic::Conic(), casadi::IndexReduction::dae_struct_detect(), density(), diagsplit(), enlargeColumns(), enlargeRows(), casadi::FunctionInternal::ensure_stacked(), etree(), casadi::DenseKronContract::eval_kernel(), casadi::DenseSparseKronContract::eval_kernel(), casadi::GetNonzeros::eval_mx(), casadi::SetNonzeros< Add >::eval_mx(), casadi::XFunction< DerivedType, MatType, NodeType >::export_code(), casadi::FmuFunction::finalize_hessian(), casadi::FunctionInternal::from_compact(), from_file(), casadi::DenseKronContract::generate(), casadi::DenseSparseKronContract::generate(), casadi::Expm::get_forward(), casadi::OnnxFunction::get_forward(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::OnnxFunction::get_jacobian(), casadi::FunctionInternal::get_partition(), casadi::Dple::get_reverse(), casadi::Expm::get_reverse(), casadi::OnnxFunction::get_reverse(), casadi::FunctionInternal::get_sparsity_out(), horzcat(), horzsplit(), info(), casadi::Dple::init(), casadi::FmuFunction::init(), casadi::Rootfinder::init(), casadi::Switch::init(), casadi::FatropInterface::init(), casadi::IpoptInterface::init(), casadi::SlicotExpm::init(), casadi::FastNewton::init(), casadi::LinsolLdl::init(), casadi::SymbolicQr::init_mem(), casadi::SparsityInternal::is_equal(), casadi::SparsityInternal::is_stacked(), casadi::FunctionInternal::jac_sparsity(), kkt(), kron(), kron_contract(), ldl(), casadi::Onnx::load(), mtimes(), casadi::mul_bvec_fwd(), mul_sparsityR(), casadi::SparsityInternal::multiply(), norm_0_mul(), casadi::Vertcat::off(), casadi::Diagcat::off(), casadi::OptiNode::parameter(), postfix_dim(), casadi::Matrix< Scalar >::print_default(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::Matrix< Scalar >::print_vector(), casadi::Call::projectArg(), casadi::IdasInterface::psetupF(), qr_sparse(), casadi::replace_mat(), resize(), casadi::Conic::sdp_to_socp_init(), set(), casadi::MadmpecInterface::set_ccopt_prob(), casadi::FatropInterface::set_fatrop_prob(), casadi::IpoptInterface::set_ipopt_prob(), casadi::MadnlpInterface::set_madnlp_prob(), casadi::FastNewton::set_work(), casadi::Convexify::setup(), casadi::simpleIntegrator(), size(), casadi::CbcInterface::solve(), casadi::ClpInterface::solve(), casadi::PiqpInterface::solve(), casadi::ProxqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::SuperscsInterface::solve(), casadi::KronContract::sp_forward(), casadi::Solve< Tr >::sp_forward(), casadi::Integrator::sp_jac_aug(), casadi::KronContract::sp_reverse(), casadi::Solve< Tr >::sp_reverse(), sum1(), to_file(), and casadi::OptiNode::variable().

◆ size2()

casadi_int casadi::Sparsity::size2 ( ) const

Definition at line 128 of file sparsity.cpp.

128  {
129  return (*this)->size2();
130  }

Referenced by casadi::SparsityInternal::_mtimes(), casadi::GetNonzeros::ad_forward(), casadi::SetNonzeros< Add >::ad_reverse(), add(), add_nz(), append(), appendColumns(), casadi::Function::assert_sparsity_out(), casadi::KinsolInterface::bjac(), bor(), casadi::casadi_func_cons_jac(), casadi::FunctionInternal::check_mat(), casadi::FastNewton::codegen_body(), casadi::FunctionInternal::codegen_meta(), colind(), casadi::SparsityInternal::combineGen(), compress(), casadi::Conic::Conic(), casadi::IndexReduction::dae_struct_detect(), density(), diagsplit(), casadi::KinsolInterface::djac(), enlargeColumns(), enlargeRows(), casadi::FunctionInternal::ensure_stacked(), etree(), casadi::Transpose::eval_gen(), casadi::BonminUserClass::eval_h(), casadi::IpoptUserClass::eval_h(), casadi::BonminUserClass::eval_jac_g(), casadi::IpoptUserClass::eval_jac_g(), casadi::DenseKronContract::eval_kernel(), casadi::DenseSparseKronContract::eval_kernel(), casadi::GetNonzeros::eval_mx(), casadi::FmuFunction::eval_task(), casadi::XFunction< DerivedType, MatType, NodeType >::export_code(), casadi::FunctionInternal::from_compact(), from_file(), casadi::DenseKronContract::generate(), casadi::DenseSparseKronContract::generate(), casadi::OnnxFunction::get_forward(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::OnnxFunction::get_jacobian(), casadi::FunctionInternal::get_partition(), casadi::OnnxFunction::get_reverse(), casadi::FunctionInternal::get_sparsity_out(), horzsplit(), info(), casadi::Dple::init(), casadi::FmuFunction::init(), casadi::FatropConicInterface::init(), casadi::FatropInterface::init(), casadi::HpipmInterface::init(), casadi::HpmpcInterface::init(), casadi::IpoptInterface::init(), casadi::SuperscsInterface::init(), casadi::FastNewton::init(), casadi::Ipqp::init(), casadi::SymbolicQr::init(), casadi::ClarabelInterface::init_dependent(), casadi::HighsInterface::init_dependent(), casadi::MosekInterface::init_dependent(), casadi::XpressInterface::init_dependent(), casadi::Blocksqp::init_mem(), casadi::CbcInterface::init_mem(), casadi::ClpInterface::init_mem(), casadi::CplexInterface::init_mem(), casadi::QpoasesInterface::init_mem(), casadi::SparsityInternal::is_equal(), is_singular(), casadi::SparsityInternal::is_stacked(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), casadi::FunctionInternal::jac_sparsity(), kkt(), kron(), kron_contract(), casadi::kron_sp_gen(), casadi::Onnx::load(), mtimes(), casadi::mul_bvec_fwd(), mul_sparsityR(), casadi::SparsityInternal::multiply(), norm_0_mul(), casadi::Horzcat::off(), casadi::Diagcat::off(), casadi::OptiNode::parameter(), casadi::FunctionInternal::print_canonical(), casadi::Matrix< Scalar >::print_default(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::Call::projectArg(), casadi::CvodesInterface::psetupF(), qr_sparse(), casadi::replace_mat(), resize(), casadi::Conic::sdp_to_socp_init(), set(), casadi::FastNewton::set_work(), size(), casadi::CbcInterface::solve(), casadi::ClpInterface::solve(), casadi::CplexInterface::solve(), casadi::GurobiInterface::solve(), casadi::PiqpInterface::solve(), casadi::ProxqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::KronContract::sp_forward(), casadi::Rank1::sp_forward(), casadi::Transpose::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::Integrator::sp_jac_aug(), casadi::KronContract::sp_reverse(), casadi::Rank1::sp_reverse(), casadi::Transpose::sp_reverse(), casadi::FunctionInternal::sp_reverse(), casadi::SparsityInternal::star_coloring_new(), sum2(), to_file(), casadi::OptiNode::variable(), and vertcat().

◆ sparsity_cast()

Sparsity casadi::Sparsity::sparsity_cast ( const Sparsity x,
const Sparsity sp 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 255 of file sparsity.cpp.

255  {
256  casadi_assert_dev(x.nnz()==sp.nnz());
257  return sp;
258  }

References nnz().

◆ sparsity_cast_mod()

Sparsity casadi::Sparsity::sparsity_cast_mod ( const Sparsity X,
const Sparsity Y 
) const

Assumption: 'this' is a subset of X.

Example:

X = [ * . . .; . * . .; . . * .; . . . *] Y = [* *; . . ; * *] x = [ * . . .; . . . .; . . * .; . . . *] returns [* *; . . ; . *]

Extra doc: https://github.com/casadi/casadi/wiki/L_246

Definition at line 1934 of file sparsity.cpp.

1934  {
1935  const Sparsity& x = *this;
1936  if (X==x) return Y;
1937  if (X==Y) return x;
1938  std::vector<unsigned char> mapping;
1939  X.unite(x, mapping);
1940 
1941 
1942  const casadi_int* Y_colind = Y.colind();
1943  const casadi_int* Y_row = Y.row();
1944  std::vector<casadi_int> y_colind(Y.size2()+1, 0);
1945  std::vector<casadi_int> y_row;
1946  y_row.reserve(Y.nnz());
1947  casadi_assert_dev(Y.nnz()==mapping.size());
1948 
1949  casadi_int i = 0;
1950  // Loop over columns of Y
1951  for (casadi_int cc=0; cc<Y.size2(); ++cc) {
1952  y_colind[cc+1] = y_colind[cc];
1953  // Loop over nonzeros of Y in column cc
1954  for (casadi_int kk=Y_colind[cc]; kk<Y_colind[cc+1]; ++kk) {
1955  // Get corresponding map entry
1956  casadi_int e = mapping[i++];
1957  if (e==3) {
1958  // Preserve element
1959  y_colind[cc+1]++;
1960  y_row.push_back(Y_row[kk]);
1961  } else {
1962  casadi_assert_dev(e==1);
1963  }
1964  }
1965  }
1966 
1967  Sparsity ret(Y.size1(), Y.size2(), y_colind, y_row, true);
1968  return ret;
1969  }

References casadi::X, and casadi::Y.

Referenced by casadi::SparsityCast::ad_forward(), casadi::SparsityCast::ad_reverse(), and casadi::SparsityCast::eval_mx().

◆ sprank()

casadi_int casadi::Sparsity::sprank ( const Sparsity x)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1728 of file sparsity.cpp.

1728  {
1729  std::vector<casadi_int> rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock;
1730  x.btf(rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock);
1731  return coarse_colblock.at(3);
1732  }

References btf().

Referenced by is_singular(), and casadi::GenericMatrix< MatType >::sprank().

◆ spsolve()

void casadi::Sparsity::spsolve ( bvec_t X,
bvec_t B,
bool  tr 
) const

◆ spy()

void casadi::Sparsity::spy ( std::ostream &  stream = casadi::uout()) const

Extra doc: https://github.com/casadi/casadi/wiki/L_dh

Definition at line 794 of file sparsity.cpp.

794  {
795  (*this)->spy(stream);
796  }

◆ spy_matlab()

void casadi::Sparsity::spy_matlab ( const std::string &  mfile) const

the sparsity using the spy command

Extra doc: https://github.com/casadi/casadi/wiki/L_di

Definition at line 785 of file sparsity.cpp.

785  {
786  (*this)->spy_matlab(mfile);
787  }

◆ star_coloring()

Sparsity casadi::Sparsity::star_coloring ( casadi_int  ordering = 1,
casadi_int  cutoff = std::numeric_limits<casadi_int>::max() 
) const

A greedy distance-2 coloring algorithm Algorithm 4.1 in What Color Is Your Jacobian? Graph Coloring for Computing Derivatives A. H. GEBREMEDHIN, F. MANNE, A. POTHEN SIAM Rev., 47(4), 629–705 (2006)

Ordering options: None (0), largest first (1)

Extra doc: https://github.com/casadi/casadi/wiki/L_dc

Definition at line 768 of file sparsity.cpp.

768  {
769  return (*this)->star_coloring(ordering, cutoff);
770  }

References casadi::SparsityInternal::star_coloring().

Referenced by casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::FunctionInternal::get_partition(), and casadi::SparsityInternal::star_coloring().

◆ star_coloring2()

Sparsity casadi::Sparsity::star_coloring2 ( casadi_int  ordering = 1,
casadi_int  cutoff = std::numeric_limits<casadi_int>::max() 
) const

A new greedy distance-2 coloring algorithm Algorithm 4.1 in NEW ACYCLIC AND STAR COLORING ALGORITHMS WITH APPLICATION TO COMPUTING HESSIANS A. H. GEBREMEDHIN, A. TARAFDAR, F. MANNE, A. POTHEN SIAM J. SCI. COMPUT. Vol. 29, No. 3, pp. 1042–1072 (2007)

Ordering options: None (0), largest first (1)

Extra doc: https://github.com/casadi/casadi/wiki/L_dd

Definition at line 772 of file sparsity.cpp.

772  {
773  return (*this)->star_coloring2(ordering, cutoff);
774  }

References casadi::SparsityInternal::star_coloring2().

Referenced by casadi::SparsityInternal::star_coloring2().

◆ star_coloring_new()

Sparsity casadi::Sparsity::star_coloring_new ( std::vector< casadi_int > &  which_color,
const Dict opts = Dict() 
) const

A greedy distance-2 coloring algorithm Algorithm 4.1 in What Color Is Your Jacobian? Graph Coloring for Computing Derivatives A. H. GEBREMEDHIN, F. MANNE, A. POTHEN SIAM Rev., 47(4), 629–705 (2006) or, alternatively (new_algo = true), NEW ACYCLIC AND STAR COLORING ALGORITHMS WITH APPLICATION TO COMPUTING HESSIANS A. H. GEBREMEDHIN, A. TARAFDAR, F. MANNE, A. POTHEN SIAM J. SCI. COMPUT. Vol. 29, No. 3, pp. 1042–1072 (2007)

Extra doc: https://github.com/casadi/casadi/wiki/L_2jz

Definition at line 759 of file sparsity.cpp.

760  {
761  try {
762  return (*this)->star_coloring_new(which_color, opts);
763  } catch (std::exception &e) {
764  CASADI_THROW_ERROR("star_coloring_new", e.what());
765  }
766  }

References casadi::SparsityInternal::star_coloring_new().

Referenced by casadi::FmuFunction::init().

◆ sub() [1/2]

Sparsity casadi::Sparsity::sub ( const std::vector< casadi_int > &  rr,
const Sparsity sp,
std::vector< casadi_int > &  mapping,
bool  ind1 = false 
) const

Returns the sparsity of the corresponding elements, with a mapping such that submatrix[k] = originalmatrix[mapping[k]]

Extra doc: https://github.com/casadi/casadi/wiki/L_cj

Definition at line 329 of file sparsity.cpp.

330  {
331  return (*this)->sub(rr, *sp, mapping, ind1);
332  }

References casadi::SparsityInternal::sub().

◆ sub() [2/2]

Sparsity casadi::Sparsity::sub ( const std::vector< casadi_int > &  rr,
const std::vector< casadi_int > &  cc,
std::vector< casadi_int > &  mapping,
bool  ind1 = false 
) const

Returns the sparsity of the submatrix, with a mapping such that submatrix[k] = originalmatrix[mapping[k]]

Extra doc: https://github.com/casadi/casadi/wiki/L_ci

Definition at line 334 of file sparsity.cpp.

335  {
336  return (*this)->sub(rr, cc, mapping, ind1);
337  }

References casadi::SparsityInternal::sub().

Referenced by casadi::Matrix< Scalar >::get(), casadi::MX::get(), casadi::FmuInternal::hess_sparsity(), casadi::LinsolLdl::init(), casadi::FmuInternal::jac_sparsity(), ldl(), qr_sparse(), casadi::Convexify::setup(), casadi::SuperscsInterface::solve(), and casadi::FunctionInternal::to_compact().

◆ sum1()

Sparsity casadi::Sparsity::sum1 ( const Sparsity x)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1724 of file sparsity.cpp.

1724  {
1725  return mtimes(Sparsity::dense(1, x.size1()), x);
1726  }

References dense(), mtimes(), and size1().

Referenced by casadi::SparsityInternal::is_compactible().

◆ sum2()

Sparsity casadi::Sparsity::sum2 ( const Sparsity x)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1720 of file sparsity.cpp.

1720  {
1721  return mtimes(x, Sparsity::dense(x.size2(), 1));
1722 
1723  }

References dense(), mtimes(), and size2().

Referenced by casadi::SparsityInternal::is_compactible().

◆ swap()

Definition at line 105 of file generic_shared_impl.hpp.

131  {
132  GenericShared<Shared, Internal> temp = *this;
133  *this = other;
134  other = temp;
135  }

◆ T()

Sparsity casadi::Sparsity::T ( ) const

◆ test_cast()

bool casadi::Sparsity::test_cast ( const SharedObjectInternal ptr)
static

Definition at line 120 of file sparsity.cpp.

120  {
121  return dynamic_cast<const SparsityInternal*>(ptr)!=nullptr;
122  }

◆ to_file()

void casadi::Sparsity::to_file ( const std::string &  filename,
const std::string &  format_hint = "" 
) const

Export sparsity pattern to file

Supported formats:

Definition at line 1996 of file sparsity.cpp.

1996  {
1997  std::string format = file_format(filename, format_hint, file_formats);
1998  auto out_ptr = Filesystem::ofstream_ptr(filename);
1999  std::ostream& out = *out_ptr;
2000  if (format=="mtx") {
2001  out << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
2002  out << "%%MatrixMarket matrix coordinate pattern general" << std::endl;
2003  out << size1() << " " << size2() << " " << nnz() << std::endl;
2004  std::vector<casadi_int> row = get_row();
2005  std::vector<casadi_int> col = get_col();
2006 
2007  for (casadi_int k=0;k<row.size();++k) {
2008  out << row[k]+1 << " " << col[k]+1 << std::endl;
2009  }
2010  } else {
2011  casadi_error("Unknown format '" + format + "'");
2012  }
2013  }
static std::unique_ptr< std::ostream > ofstream_ptr(const std::string &path, std::ios_base::openmode mode=std::ios_base::out)
Definition: filesystem.cpp:115

References file_format(), file_formats, casadi::filename(), get_col(), get_row(), nnz(), casadi::Filesystem::ofstream_ptr(), row(), size1(), and size2().

Referenced by casadi::FatropInterface::init().

◆ transpose()

Sparsity casadi::Sparsity::transpose ( std::vector< casadi_int > &  mapping,
bool  invert_mapping = false 
) const
Parameters
[out]mappingthe non-zeros of the original matrix for each non-zero of the new matrix

Extra doc: https://github.com/casadi/casadi/wiki/L_ck

Definition at line 390 of file sparsity.cpp.

390  {
391  return (*this)->transpose(mapping, invert_mapping);
392  }

References casadi::SparsityInternal::transpose().

Referenced by casadi::XFunction< DerivedType, MatType, NodeType >::jac(), and casadi::Conic::sdp_to_socp_init().

◆ tril()

Sparsity casadi::Sparsity::tril ( const Sparsity x,
bool  includeDiagonal = true 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 995 of file sparsity.cpp.

995  {
996  return x->_tril(includeDiagonal);
997  }

References casadi::SparsityInternal::_tril().

Referenced by casadi::Convexify::setup(), and casadi::GenericMatrix< MatType >::tril().

◆ triplet() [1/2]

Sparsity casadi::Sparsity::triplet ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  row,
const std::vector< casadi_int > &  col 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bm

Definition at line 1295 of file sparsity.cpp.

1296  {
1297  std::vector<casadi_int> mapping;
1298  return Sparsity::triplet(nrow, ncol, row, col, mapping, false);
1299  }

References row(), and triplet().

◆ triplet() [2/2]

Sparsity casadi::Sparsity::triplet ( casadi_int  nrow,
casadi_int  ncol,
const std::vector< casadi_int > &  row,
const std::vector< casadi_int > &  col,
std::vector< casadi_int > &  mapping,
bool  invert_mapping 
)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bm

Definition at line 1143 of file sparsity.cpp.

1145  {
1146  // Assert dimensions
1147  casadi_assert_dev(nrow>=0);
1148  casadi_assert_dev(ncol>=0);
1149  casadi_assert(col.size()==row.size(), "inconsistent lengths");
1150 
1151  // Create the return sparsity pattern and access vectors
1152  std::vector<casadi_int> r_colind(ncol+1, 0);
1153  std::vector<casadi_int> r_row;
1154  r_row.reserve(row.size());
1155 
1156  // Consistency check and check if elements are already perfectly ordered with no duplicates
1157  casadi_int last_col=-1, last_row=-1;
1158  bool perfectly_ordered=true;
1159  for (casadi_int k=0; k<col.size(); ++k) {
1160  // Consistency check
1161  casadi_assert(col[k]>=0 && col[k]<ncol,
1162  "Column index (" + str(col[k]) + ") out of bounds [0," + str(ncol) + "[");
1163  casadi_assert(row[k]>=0 && row[k]<nrow,
1164  "Row index out of bounds (" + str(row[k]) + ") out of bounds [0," + str(nrow) + "[");
1165 
1166  // Check if ordering is already perfect
1167  perfectly_ordered = perfectly_ordered && (col[k]<last_col ||
1168  (col[k]==last_col && row[k]<=last_row));
1169  last_col = col[k];
1170  last_row = row[k];
1171  }
1172 
1173  // Quick return if perfectly ordered
1174  if (perfectly_ordered) {
1175  // Save rows
1176  r_row.resize(row.size());
1177  std::copy(row.begin(), row.end(), r_row.begin());
1178 
1179  // Find offset index
1180  casadi_int el=0;
1181  for (casadi_int i=0; i<ncol; ++i) {
1182  while (el<col.size() && col[el]==i) el++;
1183  r_colind[i+1] = el;
1184  }
1185 
1186  // Identity mapping
1187  mapping.resize(row.size());
1188  for (casadi_int k=0; k<row.size(); ++k) mapping[k] = k;
1189 
1190  // Quick return
1191  return Sparsity(nrow, ncol, r_colind, r_row);
1192  }
1193 
1194  // Reuse data
1195  std::vector<casadi_int>& mapping1 = invert_mapping ? r_row : mapping;
1196  std::vector<casadi_int>& mapping2 = invert_mapping ? mapping : r_row;
1197 
1198  // Make sure that enough memory is allocated to use as a work vector
1199  mapping1.reserve(std::max(nrow+1, static_cast<casadi_int>(col.size())));
1200 
1201  // Number of elements in each row
1202  std::vector<casadi_int>& rowcount = mapping1; // reuse memory
1203  rowcount.resize(nrow+1);
1204  std::fill(rowcount.begin(), rowcount.end(), 0);
1205  for (std::vector<casadi_int>::const_iterator it=row.begin(); it!=row.end(); ++it) {
1206  rowcount[*it+1]++;
1207  }
1208 
1209  // Cumsum to get index offset for each row
1210  for (casadi_int i=0; i<nrow; ++i) {
1211  rowcount[i+1] += rowcount[i];
1212  }
1213 
1214  // New row for each old row
1215  mapping2.resize(row.size());
1216  for (casadi_int k=0; k<row.size(); ++k) {
1217  mapping2[rowcount[row[k]]++] = k;
1218  }
1219 
1220  // Number of elements in each col
1221  // reuse memory, r_colind is already the right size
1222  std::vector<casadi_int>& colcount = r_colind;
1223  // and is filled with zeros
1224  for (std::vector<casadi_int>::const_iterator it=mapping2.begin(); it!=mapping2.end(); ++it) {
1225  colcount[col[*it]+1]++;
1226  }
1227 
1228  // Cumsum to get index offset for each col
1229  for (casadi_int i=0; i<ncol; ++i) {
1230  colcount[i+1] += colcount[i];
1231  }
1232 
1233  // New col for each old col
1234  mapping1.resize(col.size());
1235  for (std::vector<casadi_int>::const_iterator it=mapping2.begin(); it!=mapping2.end(); ++it) {
1236  mapping1[colcount[col[*it]]++] = *it;
1237  }
1238 
1239  // Current element in the return matrix
1240  casadi_int r_el = 0;
1241  r_row.resize(col.size());
1242 
1243  // Current nonzero
1244  std::vector<casadi_int>::const_iterator it=mapping1.begin();
1245 
1246  // Loop over columns
1247  r_colind[0] = 0;
1248  for (casadi_int i=0; i<ncol; ++i) {
1249 
1250  // Previous row (to detect duplicates)
1251  casadi_int j_prev = -1;
1252 
1253  // Loop over nonzero elements of the col
1254  while (it!=mapping1.end() && col[*it]==i) {
1255 
1256  // Get the element
1257  casadi_int el = *it;
1258  it++;
1259 
1260  // Get the row
1261  casadi_int j = row[el];
1262 
1263  // If not a duplicate, save to return matrix
1264  if (j!=j_prev)
1265  r_row[r_el++] = j;
1266 
1267  if (invert_mapping) {
1268  // Save to the inverse mapping
1269  mapping2[el] = r_el-1;
1270  } else {
1271  // If not a duplicate, save to the mapping vector
1272  if (j!=j_prev)
1273  mapping1[r_el-1] = el;
1274  }
1275 
1276  // Save row
1277  j_prev = j;
1278  }
1279 
1280  // Update col offset
1281  r_colind[i+1] = r_el;
1282  }
1283 
1284  // Resize the row vector
1285  r_row.resize(r_el);
1286 
1287  // Resize mapping matrix
1288  if (!invert_mapping) {
1289  mapping1.resize(r_el);
1290  }
1291 
1292  return Sparsity(nrow, ncol, r_colind, r_row);
1293  }

References row(), Sparsity(), and casadi::str().

Referenced by casadi::SparsityInternal::_mtimes(), casadi::SparsityInternal::_reshape(), casadi::BSpline::dual(), from_file(), casadi::FunctionInternal::get_jac_sparsity_gen(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::DaeBuilderInternal::hess_sparsity(), horzcat(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), casadi::DaeBuilderInternal::jac_sparsity(), nonzeros(), casadi::SparsityInternal::pmult(), casadi::QpoasesInterface::qpoases_init(), rowcol(), casadi::Matrix< Scalar >::set(), casadi::MX::set(), casadi::SparsityInternal::star_coloring(), casadi::SparsityInternal::sub(), casadi::SparsityInternal::transpose(), triplet(), and vertcat().

◆ triu()

Sparsity casadi::Sparsity::triu ( const Sparsity x,
bool  includeDiagonal = true 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 999 of file sparsity.cpp.

999  {
1000  return x->_triu(includeDiagonal);
1001  }

References casadi::SparsityInternal::_triu().

Referenced by casadi::OsqpInterface::codegen_init_mem(), casadi::OsqpInterface::init_mem(), casadi::Convexify::setup(), and casadi::GenericMatrix< MatType >::triu().

◆ type_name()

static std::string casadi::Sparsity::type_name ( )
inlinestatic

Definition at line 1199 of file sparsity.hpp.

1199 {return "Sparsity";}

◆ uni_coloring()

Sparsity casadi::Sparsity::uni_coloring ( const Sparsity AT = Sparsity(),
casadi_int  cutoff = std::numeric_limits<casadi_int>::max() 
) const

(Algorithm 3.1 in A. H. GEBREMEDHIN, F. MANNE, A. POTHEN)

Extra doc: https://github.com/casadi/casadi/wiki/L_db

Examples
matrix/coloring.py.

Definition at line 751 of file sparsity.cpp.

751  {
752  if (AT.is_null()) {
753  return (*this)->uni_coloring(T(), cutoff);
754  } else {
755  return (*this)->uni_coloring(AT, cutoff);
756  }
757  }

References casadi::GenericShared< Shared, Internal >::is_null(), T(), and casadi::SparsityInternal::uni_coloring().

Referenced by casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_partition(), and casadi::FmuFunction::init().

◆ unit()

Sparsity casadi::Sparsity::unit ( casadi_int  n,
casadi_int  el 
)
static

position el *

Extra doc: https://github.com/casadi/casadi/wiki/L_bf

Definition at line 1120 of file sparsity.cpp.

1120  {
1121  std::vector<casadi_int> row(1, el), colind(2);
1122  colind[0] = 0;
1123  colind[1] = 1;
1124  return Sparsity(n, 1, colind, row);
1125  }

References colind(), row(), and Sparsity().

Referenced by casadi::SuperscsInterface::init().

◆ unite() [1/2]

Sparsity casadi::Sparsity::unite ( const Sparsity y) const

Extra doc: https://github.com/casadi/casadi/wiki/L_cm

Definition at line 413 of file sparsity.cpp.

413  {
414  return (*this)->combine(y, false, false);
415  }

References casadi::SparsityInternal::combine().

◆ unite() [2/2]

Sparsity casadi::Sparsity::unite ( const Sparsity y,
std::vector< unsigned char > &  mapping 
) const

◆ upper()

Sparsity casadi::Sparsity::upper ( casadi_int  n)
static

Extra doc: https://github.com/casadi/casadi/wiki/L_bg

Definition at line 1044 of file sparsity.cpp.

1044  {
1045  casadi_assert(n>=0, "Sparsity::upper expects a positive integer as argument");
1046  casadi_int nrow=n, ncol=n;
1047  std::vector<casadi_int> colind, row;
1048  colind.reserve(ncol+1);
1049  row.reserve((n*(n+1))/2);
1050 
1051  // Loop over columns
1052  colind.push_back(0);
1053  for (casadi_int cc=0; cc<ncol; ++cc) {
1054  // Loop over rows for the upper triangular half
1055  for (casadi_int rr=0; rr<=cc; ++rr) {
1056  row.push_back(rr);
1057  }
1058  colind.push_back(row.size());
1059  }
1060 
1061  // Return the pattern
1062  return Sparsity(nrow, ncol, colind, row);
1063  }

References colind(), row(), and Sparsity().

Referenced by casadi::Convexify::setup().

◆ vertcat()

Sparsity casadi::Sparsity::vertcat ( const std::vector< Sparsity > &  sp)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1551 of file sparsity.cpp.

1551  {
1552  // Quick return if possible
1553  if (sp.empty()) return Sparsity(0, 1);
1554  if (sp.size()==1) return sp.front();
1555 
1556  // Count total nnz
1557  casadi_int nnz_total = 0;
1558  for (casadi_int i=0; i<sp.size(); ++i) nnz_total += sp[i].nnz();
1559 
1560  // Construct from vectors (triplet format)
1561  std::vector<casadi_int> ret_row, ret_col;
1562  ret_row.reserve(nnz_total);
1563  ret_col.reserve(nnz_total);
1564  casadi_int ret_nrow = 0;
1565  casadi_int ret_ncol = 0;
1566  for (casadi_int i=0; i<sp.size() && ret_ncol==0; ++i)
1567  ret_ncol = sp[i].size2();
1568 
1569  // Append all patterns
1570  for (std::vector<Sparsity>::const_iterator i=sp.begin(); i!=sp.end(); ++i) {
1571  // Get sparsity pattern
1572  casadi_int sp_nrow = i->size1();
1573  casadi_int sp_ncol = i->size2();
1574  const casadi_int* sp_colind = i->colind();
1575  const casadi_int* sp_row = i->row();
1576  casadi_assert(sp_ncol==ret_ncol || sp_ncol==0,
1577  "Sparsity::vertcat: Mismatching number of columns");
1578 
1579  // Add entries to pattern
1580  for (casadi_int cc=0; cc<sp_ncol; ++cc) {
1581  for (casadi_int k=sp_colind[cc]; k<sp_colind[cc+1]; ++k) {
1582  ret_row.push_back(sp_row[k] + ret_nrow);
1583  ret_col.push_back(cc);
1584  }
1585  }
1586 
1587  // Update offset
1588  ret_nrow += sp_nrow;
1589  }
1590  return Sparsity::triplet(ret_nrow, ret_ncol, ret_row, ret_col);
1591  }

References nnz(), size2(), Sparsity(), and triplet().

Referenced by append(), and blockcat().

◆ vertsplit()

std::vector< Sparsity > casadi::Sparsity::vertsplit ( const Sparsity x,
const std::vector< casadi_int > &  offset 
)
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1669 of file sparsity.cpp.

1670  {
1671  std::vector<Sparsity> ret = horzsplit(x.T(), offset);
1672  for (std::vector<Sparsity>::iterator it=ret.begin(); it!=ret.end(); ++it) {
1673  *it = it->T();
1674  }
1675  return ret;
1676  }
static std::vector< Sparsity > horzsplit(const Sparsity &x, const std::vector< casadi_int > &offset)
Enlarge matrix.
Definition: sparsity.cpp:1620

References horzsplit(), casadi::SparsityInterface< Sparsity >::offset(), and T().

◆ weak()

Extra doc: https://github.com/casadi/casadi/wiki/L_aw

Definition at line 130 of file generic_shared_impl.hpp.

143  {
144  return (*this)->weak();
145  }

Member Data Documentation

◆ file_formats

std::set< std::string > casadi::Sparsity::file_formats = {"mtx"}
static

Make the matrix larger by inserting empty rows and columns, keeping the existing non-zeros

For the matrices A to B A(m, n) length(jj)=m , length(ii)=n B(nrow, ncol)

A=enlarge(m, n, ii, jj) makes sure that

B[jj, ii] == A

Extra doc: https://github.com/casadi/casadi/wiki/L_cr

Definition at line 1242 of file sparsity.hpp.

Referenced by file_format(), from_file(), and to_file().


The documentation for this class was generated from the following files: