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_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_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)
 
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 (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)
 Enlarge matrix. More...
 
static Sparsity mac (const Sparsity &x, const Sparsity &y, const Sparsity &z)
 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 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 881 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(), 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:1012

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 124 of file generic_shared_impl.hpp.

137  {
138  return reinterpret_cast<casadi_int>(get());
139  }

◆ add() [1/2]

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

Definition at line 1339 of file sparsity.hpp.

1339  {
1340  // Get dimensions of this
1341  const casadi_int sz = nnz();
1342  const casadi_int sz1 = size1();
1343  const casadi_int sz2 = size2();
1344  const casadi_int nel = sz1*sz2;
1345 
1346  // Get dimensions of assigning matrix
1347  const casadi_int val_sz = val_sp.nnz();
1348  const casadi_int val_sz1 = val_sp.size1();
1349  const casadi_int val_sz2 = val_sp.size2();
1350  const casadi_int val_nel = val_sz1*val_sz2;
1351 
1352  // Check if sparsity matches
1353  if (val_sp==*this) {
1354  for (casadi_int k=0; k<sz; ++k) {
1355  data[k] += val_data[k];
1356  }
1357  } else if (this->is_empty()) {
1358  // Quick return
1359  return;
1360  } else if (val_sp.is_empty()) {
1361  // Quick return
1362  return;
1363  } else if (val_nel==1) { // if scalar
1364  if (val_sz!=0) {
1365  for (casadi_int k=0; k<sz; ++k) {
1366  data[k] += val_data[0];
1367  }
1368  }
1369  } else {
1370  // Quick return if empty
1371  if (nel==0 && val_nel==0) return;
1372 
1373  // Make sure that dimension matches
1374  casadi_assert(sz2==val_sz2 && sz1==val_sz1,
1375  "Sparsity::add<DataType>: shape mismatch. lhs is "
1376  + dim() + ", while rhs is " + val_sp.dim() + ".");
1377 
1378  // Sparsity
1379  const casadi_int* c = row();
1380  const casadi_int* rind = colind();
1381  const casadi_int* v_c = val_sp.row();
1382  const casadi_int* v_rind = val_sp.colind();
1383 
1384  // For all columns
1385  for (casadi_int i=0; i<sz2; ++i) {
1386 
1387  // Nonzero of the assigning matrix
1388  casadi_int v_el = v_rind[i];
1389 
1390  // First nonzero of the following column
1391  casadi_int v_el_end = v_rind[i+1];
1392 
1393  // Next row of the assigning matrix
1394  casadi_int v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1395 
1396  // Assign all nonzeros
1397  for (casadi_int el=rind[i]; el!=rind[i+1]; ++el) {
1398 
1399  // Get row
1400  casadi_int j=c[el];
1401 
1402  // Forward the assigning nonzero
1403  while (v_j<j) {
1404  v_el++;
1405  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1406  }
1407 
1408  // Assign nonzero
1409  if (v_j==j) {
1410  data[el] += val_data[v_el++];
1411  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1412  }
1413  }
1414  }
1415  }
1416  }
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:587
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 707 of file sparsity.cpp.

707  {
708  return (*this)->amd();
709  }

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

◆ append()

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

Definition at line 470 of file sparsity.cpp.

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

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

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 97 of file generic_shared_impl.hpp.

75  {
76  node = node_;
77  }

◆ 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 1070 of file sparsity.cpp.

1070  {
1071  casadi_assert(n>=0, "Sparsity::band expects a positive integer as argument");
1072  casadi_assert((p<0? -p : p)<n,
1073  "Sparsity::band: position of band schould be smaller then size argument");
1074 
1075  casadi_int nc = n-(p<0? -p : p);
1076 
1077  std::vector< casadi_int > row(nc);
1078 
1079  casadi_int offset = std::max(p, casadi_int(0));
1080  for (casadi_int i=0;i<nc;i++) {
1081  row[i]=i+offset;
1082  }
1083 
1084  std::vector< casadi_int > colind(n+1);
1085 
1086  offset = std::min(p, casadi_int(0));
1087  for (casadi_int i=0;i<n+1;i++) {
1088  colind[i] = std::max(std::min(i+offset, nc), casadi_int(0));
1089  }
1090 
1091  return Sparsity(n, n, colind, row);
1092 
1093  }
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(), casadi::BSplineCommon::derivative_coeff(), 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 1095 of file sparsity.cpp.

1095  {
1096  // This is not an efficient implementation
1097  Sparsity ret = Sparsity(n, n);
1098  for (casadi_int i=-p;i<=p;++i) {
1099  ret = ret + Sparsity::band(n, i);
1100  }
1101  return ret;
1102  }
static Sparsity band(casadi_int n, casadi_int p)
Create a single band in a square sparsity pattern.
Definition: sparsity.cpp:1070

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 1605 of file sparsity.cpp.

1605  {
1606  std::vector< Sparsity > ret;
1607  for (casadi_int i=0; i<v.size(); ++i)
1608  ret.push_back(horzcat(v[i]));
1609  return vertcat(ret);
1610  }
static Sparsity horzcat(const std::vector< Sparsity > &sp)
Accessed by SparsityInterface.
Definition: sparsity.cpp:1392

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 1419 of file sparsity.hpp.

1419  {
1420  // Get dimensions of this
1421  const casadi_int sz = nnz();
1422  const casadi_int sz1 = size1();
1423  const casadi_int sz2 = size2();
1424  const casadi_int nel = sz1*sz2;
1425 
1426  // Get dimensions of assigning matrix
1427  const casadi_int val_sz = val_sp.nnz();
1428  const casadi_int val_sz1 = val_sp.size1();
1429  const casadi_int val_sz2 = val_sp.size2();
1430  const casadi_int val_nel = val_sz1*val_sz2;
1431 
1432  // Check if sparsity matches
1433  if (val_sp==*this) {
1434  for (casadi_int k=0; k<sz; ++k) {
1435  data[k] |= val_data[k];
1436  }
1437  } else if (this->is_empty()) {
1438  // Quick return
1439  return;
1440  } else if (val_sp.is_empty()) {
1441  // Quick return
1442  return;
1443  } else if (val_nel==1) { // if scalar
1444  if (val_sz!=0) {
1445  for (casadi_int k=0; k<sz; ++k) {
1446  data[k] |= val_data[0];
1447  }
1448  }
1449  } else {
1450  // Quick return if empty
1451  if (nel==0 && val_nel==0) return;
1452 
1453  // Make sure that dimension matches
1454  casadi_assert(sz2==val_sz2 && sz1==val_sz1,
1455  "Sparsity::add<DataType>: shape mismatch. lhs is "
1456  + dim() + ", while rhs is " + val_sp.dim() + ".");
1457 
1458  // Sparsity
1459  const casadi_int* c = row();
1460  const casadi_int* rind = colind();
1461  const casadi_int* v_c = val_sp.row();
1462  const casadi_int* v_rind = val_sp.colind();
1463 
1464  // For all columns
1465  for (casadi_int i=0; i<sz2; ++i) {
1466 
1467  // Nonzero of the assigning matrix
1468  casadi_int v_el = v_rind[i];
1469 
1470  // First nonzero of the following column
1471  casadi_int v_el_end = v_rind[i+1];
1472 
1473  // Next row of the assigning matrix
1474  casadi_int v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1475 
1476  // Assign all nonzeros
1477  for (casadi_int el=rind[i]; el!=rind[i+1]; ++el) {
1478 
1479  // Get row
1480  casadi_int j=c[el];
1481 
1482  // Forward the assigning nonzero
1483  while (v_j<j) {
1484  v_el++;
1485  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1486  }
1487 
1488  // Assign nonzero
1489  if (v_j==j) {
1490  data[el] |= val_data[v_el++];
1491  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1492  }
1493  }
1494  }
1495  }
1496  }

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 711 of file sparsity.cpp.

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

Referenced by 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 1388 of file sparsity.cpp.

1388  {
1389  return (*this)->bw_lower();
1390  }

◆ bw_upper()

casadi_int casadi::Sparsity::bw_upper ( ) const

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

Definition at line 1384 of file sparsity.cpp.

1384  {
1385  return (*this)->bw_upper();
1386  }

◆ 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::FmuFunction::check_hessian(), 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(), 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::CplexInterface::init_mem(), is_equal(), casadi::SparsityInternal::is_equal(), casadi::SparsityInternal::is_stacked(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), kron(), lower(), casadi::FmuFunction::make_symmetric(), mul_sparsityF(), mul_sparsityR(), casadi::SparsityInternal::multiply(), norm_0_mul(), permutation(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::CvodesInterface::psetupF(), casadi::IdasInterface::psetupF(), casadi::FmuFunction::remove_nans(), 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::Rank1::sp_forward(), casadi::Solve< Tr >::sp_forward(), casadi::Transpose::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::Rank1::sp_reverse(), casadi::Solve< Tr >::sp_reverse(), casadi::Transpose::sp_reverse(), casadi::FunctionInternal::sp_reverse(), Sparsity(), 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 1305 of file sparsity.cpp.

1305  {
1306  if (canonical) {
1307  // fallback
1308  } else if (is_dense()) {
1309  return {size1(), size2(), 1};
1310  }
1311  return (*this)->sp();
1312  }

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 1340 of file sparsity.cpp.

1340  {
1341  casadi_assert_dev(v!=nullptr);
1342 
1343  // Get sparsity pattern
1344  casadi_int nrow = v[0];
1345  casadi_int ncol = v[1];
1346  const casadi_int *colind = v+2;
1347  if (colind[0]==1) {
1348  // Dense matrix - deviation from canonical form
1349  return Sparsity::dense(nrow, ncol);
1350  }
1351  casadi_int nnz = colind[ncol];
1352  if (nrow*ncol == nnz) {
1353  // Dense matrix
1354  return Sparsity::dense(nrow, ncol);
1355  } else {
1356  // Sparse matrix
1357  const casadi_int *row = v + 2 + ncol+1;
1358  return Sparsity(nrow, ncol,
1359  std::vector<casadi_int>(colind, colind+ncol+1),
1360  std::vector<casadi_int>(row, row+nnz), order_rows);
1361  }
1362  }

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 1325 of file sparsity.cpp.

1325  {
1326  // Check consistency
1327  casadi_assert_dev(v.size() >= 2);
1328  casadi_int nrow = v[0];
1329  casadi_int ncol = v[1];
1330  casadi_assert_dev(v.size() >= 2 + ncol+1);
1331  casadi_int nnz = v[2 + ncol];
1332  bool dense = v.size() == 2 + ncol+1 && nrow*ncol==nnz;
1333  bool sparse = v.size() == 2 + ncol+1 + nnz;
1334  casadi_assert_dev(dense || sparse);
1335 
1336  // Call array version
1337  return compressed(&v.front(), order_rows);
1338  }
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:1325

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 134 of file generic_shared_impl.hpp.

42  {
43 #ifdef WITH_EXTRA_CHECKS
44  casadi_assert_dev(Function::call_depth_==0);
45 #endif // WITH_EXTRA_CHECKS
46  if (!node) return;
47  if (node->weak_ref_) {
48 #ifdef CASADI_WITH_THREADSAFE_SYMBOLICS
49  auto mutex = node->weak_ref_->get_mutex();
50  // Avoid triggering a delete while a weak_ref.shared_if_alive is being called
51  std::lock_guard<std::mutex> lock(*mutex);
52  // Could it be that this mutex is destroyed when the lock goes out of scope?
53 #endif // CASADI_WITH_THREADSAFE_SYMBOLICS
54 
55  if (--static_cast<Internal*>(node)->count == 0) {
56  delete node;
57  node = nullptr;
58  }
59  } else {
60  if (--static_cast<Internal*>(node)->count == 0) {
61  delete node;
62  node = nullptr;
63  }
64  }
65  }

◆ count_up()

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

Definition at line 133 of file generic_shared_impl.hpp.

32  {
33 #ifdef WITH_EXTRA_CHECKS
34  casadi_assert_dev(Function::call_depth_==0);
35 #endif // WITH_EXTRA_CHECKS
36 
37  if (node) static_cast<Internal*>(node)->count++;
38 
39  }

◆ 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 113 of file generic_shared_impl.hpp.

80  {
81  if (node) {
82  return node->debug_repr(node);
83  } else {
84  return "NULL";
85  }
86  }
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 1012 of file sparsity.cpp.

1012  {
1013  casadi_assert_dev(nrow>=0);
1014  casadi_assert_dev(ncol>=0);
1015  // Column offset
1016  std::vector<casadi_int> colind(ncol+1);
1017  for (casadi_int cc=0; cc<ncol+1; ++cc) colind[cc] = cc*nrow;
1018 
1019  // Row
1020  std::vector<casadi_int> row(ncol*nrow);
1021  for (casadi_int cc=0; cc<ncol; ++cc)
1022  for (casadi_int rr=0; rr<nrow; ++rr)
1023  row[rr+cc*nrow] = rr;
1024 
1025  return Sparsity(nrow, ncol, colind, row);
1026  }

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

Referenced by casadi::Constant< Value >::_get_binary(), casadi::SparsityInternal::_mtimes(), casadi::DaeBuilder::add_variable(), casadi::DaeBuilder::add_variable_new(), casadi::BSpline::BSpline(), casadi::BSplineParametric::BSplineParametric(), compressed(), casadi::GetNonzerosParam::create(), casadi::Matrix< Scalar >::densify(), casadi::MX::densify(), casadi::Determinant::Determinant(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::MXNode::get_mac(), casadi::Nlpsol::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::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(), 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 2012 of file sparsity.cpp.

2012  {
2013  std::stringstream ss;
2014  ss << s;
2015  return deserialize(ss);
2016  }
static Sparsity deserialize(std::istream &stream)
Build Sparsity from serialization.
Definition: sparsity.cpp:1983

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 1996 of file sparsity.cpp.

1996  {
1997  std::vector<casadi_int> i;
1998  s.unpack("SparsityInternal::compressed", i);
1999  if (i.empty()) {
2000  return Sparsity();
2001  } else {
2002  return Sparsity::compressed(i);
2003  }
2004  }

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 1983 of file sparsity.cpp.

1983  {
1984  DeserializingStream s(stream);
1985  return Sparsity::deserialize(s);
1986  }

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 696 of file sparsity.cpp.

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

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 568 of file sparsity.cpp.

568  {
569  // Smallest dimension
570  casadi_int n = std::min(nrow, ncol);
571 
572  // Column offset
573  std::vector<casadi_int> colind(ncol+1, n);
574  for (casadi_int cc=0; cc<n; ++cc) colind[cc] = cc;
575 
576  // Row
577  std::vector<casadi_int> row = range(n);
578 
579  // Create pattern from vectors
580  return Sparsity(nrow, ncol, colind, row);
581  }
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 1520 of file sparsity.cpp.

1520  {
1521  casadi_int n = 0;
1522  casadi_int m = 0;
1523 
1524  std::vector<casadi_int> colind(1, 0);
1525  std::vector<casadi_int> row;
1526 
1527  casadi_int nz = 0;
1528  for (casadi_int i=0;i<v.size();++i) {
1529  const casadi_int* colind_ = v[i].colind();
1530  casadi_int ncol = v[i].size2();
1531  const casadi_int* row_ = v[i].row();
1532  casadi_int sz = v[i].nnz();
1533  for (casadi_int k=1; k<ncol+1; ++k) {
1534  colind.push_back(colind_[k]+nz);
1535  }
1536  for (casadi_int k=0; k<sz; ++k) {
1537  row.push_back(row_[k]+m);
1538  }
1539  n+= v[i].size2();
1540  m+= v[i].size1();
1541  nz+= v[i].nnz();
1542  }
1543 
1544  return Sparsity(m, n, colind, row);
1545  }

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 1612 of file sparsity.cpp.

1614  {
1615  // Consistency check
1616  casadi_assert_dev(!offset1.empty());
1617  casadi_assert_dev(offset1.front()==0);
1618  casadi_assert(offset1.back()==x.size1(),
1619  "diagsplit(Sparsity, offset1, offset2): Last elements of offset1 "
1620  "(" + str(offset1.back()) + ") must equal the number of rows "
1621  "(" + str(x.size1()) + ")");
1622  casadi_assert(offset2.back()==x.size2(),
1623  "diagsplit(Sparsity, offset1, offset2): Last elements of offset2 "
1624  "(" + str(offset2.back()) + ") must equal the number of rows "
1625  "(" + str(x.size2()) + ")");
1626  casadi_assert_dev(is_monotone(offset1));
1627  casadi_assert_dev(is_monotone(offset2));
1628  casadi_assert_dev(offset1.size()==offset2.size());
1629 
1630  // Number of outputs
1631  casadi_int n = offset1.size()-1;
1632 
1633  // Return value
1634  std::vector<Sparsity> ret;
1635 
1636  // Caveat: this is a very silly implementation
1637  IM x2 = IM::zeros(x);
1638 
1639  for (casadi_int i=0; i<n; ++i) {
1640  ret.push_back(x2(Slice(offset1[i], offset1[i+1]),
1641  Slice(offset2[i], offset2[i+1])).sparsity());
1642  }
1643 
1644  return ret;
1645  }
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 544 of file sparsity.cpp.

545  {
546  enlargeColumns(ncol, cc, ind1);
547  enlargeRows(nrow, rr, ind1);
548  }
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:550
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:559

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 550 of file sparsity.cpp.

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

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 559 of file sparsity.cpp.

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

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 615 of file sparsity.cpp.

615  {
616  std::vector<casadi_int> parent(size2()), w(size1() + size2());
617  SparsityInternal::etree(*this, get_ptr(parent), get_ptr(w), ata);
618  return parent;
619  }
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 778 of file sparsity.cpp.

779  {
780  (*this)->export_code(lang, stream, options);
781  }

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 1888 of file sparsity.cpp.

1889  {
1890  if (format_hint.empty()) {
1891  std::string extension = filename.substr(filename.rfind(".")+1);
1892  auto it = file_formats.find(extension);
1893  casadi_assert(it!=file_formats.end(),
1894  "Extension '" + extension + "' not recognised. "
1895  "Valid options: " + str(file_formats) + ".");
1896  return extension;
1897  } else {
1898  auto it = file_formats.find(format_hint);
1899  casadi_assert(it!=file_formats.end(),
1900  "File format hint '" + format_hint + "' not recognised. "
1901  "Valid options: " + str(file_formats) + ".");
1902  return format_hint;
1903  }
1904 
1905  }
static std::set< std::string > file_formats
Enlarge matrix.
Definition: sparsity.hpp:1183
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 735 of file sparsity.cpp.

735  {
736  std::vector<casadi_int> loc;
737  find(loc, ind1);
738  return loc;
739  }
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:735

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 741 of file sparsity.cpp.

741  {
742  (*this)->find(loc, ind1);
743  }

◆ from_file()

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

Definition at line 1925 of file sparsity.cpp.

1925  {
1926  std::string format = file_format(filename, format_hint, file_formats);
1927  std::ifstream in(filename);
1928  casadi_assert(in.good(), "Could not open '" + filename + "'.");
1929  if (format=="mtx") {
1930  std::string line;
1931  std::vector<casadi_int> row, col;
1932  casadi_int size1, size2, nnz;
1933  int line_num = 0;
1934  while (std::getline(in, line)) {
1935  if (line_num==0) {
1936  casadi_assert(line=="%%MatrixMarket matrix coordinate pattern general", "Wrong header");
1937  line_num = 1;
1938  } else if (line_num==1) {
1939  std::stringstream stream(line);
1940  stream >> size1;
1941  stream >> size2;
1942  stream >> nnz;
1943  row.reserve(nnz);
1944  col.reserve(nnz);
1945  line_num = 2;
1946  } else {
1947  std::stringstream stream(line);
1948  casadi_int r, c;
1949  stream >> r;
1950  stream >> c;
1951  row.push_back(r-1);
1952  col.push_back(c-1);
1953  }
1954  }
1955  return triplet(size1, size2, row, col);
1956  } else {
1957  casadi_error("Unknown format '" + format + "'");
1958  }
1959  }
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:1888
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:1127

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

◆ get()

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

Definition at line 2018 of file sparsity.cpp.

2018  {
2019  return static_cast<SparsityInternal*>(SharedObject::get());
2020  }
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

Together with the row-vector, this vector gives the sparsity of the matrix in sparse triplet format, i.e. the column and row for each non-zero elements

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

Definition at line 368 of file sparsity.cpp.

368  {
369  return (*this)->get_col();
370  }

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(), get_triplet(), casadi::DaeBuilderInternal::hess_sparsity(), casadi::SnoptInterface::init(), casadi::MX::set(), casadi::KnitroInterface::solve(), and to_file().

◆ 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 611 of file sparsity.cpp.

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

References casadi::SparsityInternal::get_diag().

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

◆ get_lower()

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

Definition at line 987 of file sparsity.cpp.

987  {
988  return (*this)->get_lower();
989  }

◆ 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 745 of file sparsity.cpp.

745  {
746  (*this)->get_nz(indices);
747  }

◆ 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(), permutation_vector(), casadi::MX::set(), casadi::KnitroInterface::solve(), and to_file().

◆ 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::ProxqpInterface::solve().

◆ get_upper()

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

Definition at line 991 of file sparsity.cpp.

991  {
992  return (*this)->get_upper();
993  }

◆ getCache()

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

Definition at line 524 of file sparsity.cpp.

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

◆ getCount()

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

Definition at line 103 of file generic_shared_impl.hpp.

127  {
128  return (*this)->getCount();
129  }

◆ getEmpty()

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

Definition at line 539 of file sparsity.cpp.

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

◆ getScalar()

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

Definition at line 529 of file sparsity.cpp.

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

◆ getScalarSparse()

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

Definition at line 534 of file sparsity.cpp.

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

◆ 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 795 of file sparsity.cpp.

795  {
796  return (*this)->hash();
797  }

◆ horzcat()

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

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

Definition at line 1392 of file sparsity.cpp.

1392  {
1393  // Quick return if possible
1394  if (sp.empty()) return Sparsity(1, 0);
1395  if (sp.size()==1) return sp.front();
1396 
1397  // Count total nnz
1398  casadi_int nnz_total = 0;
1399  for (casadi_int i=0; i<sp.size(); ++i) nnz_total += sp[i].nnz();
1400 
1401  // Construct from vectors (triplet format)
1402  std::vector<casadi_int> ret_row, ret_col;
1403  ret_row.reserve(nnz_total);
1404  ret_col.reserve(nnz_total);
1405  casadi_int ret_ncol = 0;
1406  casadi_int ret_nrow = 0;
1407  for (casadi_int i=0; i<sp.size() && ret_nrow==0; ++i)
1408  ret_nrow = sp[i].size1();
1409 
1410  // Append all patterns
1411  for (std::vector<Sparsity>::const_iterator i=sp.begin(); i!=sp.end(); ++i) {
1412  // Get sparsity pattern
1413  casadi_int sp_nrow = i->size1();
1414  casadi_int sp_ncol = i->size2();
1415  const casadi_int* sp_colind = i->colind();
1416  const casadi_int* sp_row = i->row();
1417  casadi_assert(sp_nrow==ret_nrow || sp_nrow==0,
1418  "Sparsity::horzcat: Mismatching number of rows");
1419 
1420  // Add entries to pattern
1421  for (casadi_int cc=0; cc<sp_ncol; ++cc) {
1422  for (casadi_int k=sp_colind[cc]; k<sp_colind[cc+1]; ++k) {
1423  ret_row.push_back(sp_row[k]);
1424  ret_col.push_back(cc + ret_ncol);
1425  }
1426  }
1427 
1428  // Update offset
1429  ret_ncol += sp_ncol;
1430  }
1431  return Sparsity::triplet(ret_nrow, ret_ncol, ret_row, ret_col);
1432  }

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 1547 of file sparsity.cpp.

1548  {
1549  // Consistency check
1550  casadi_assert_dev(!offset.empty());
1551  casadi_assert_dev(offset.front()==0);
1552  casadi_assert(offset.back()==x.size2(),
1553  "horzsplit(Sparsity, std::vector<casadi_int>): Last elements of offset "
1554  "(" + str(offset.back()) + ") must equal the number of columns "
1555  "(" + str(x.size2()) + ")");
1556  casadi_assert_dev(is_monotone(offset));
1557 
1558  // Number of outputs
1559  casadi_int n = offset.size()-1;
1560 
1561  // Get the sparsity of the input
1562  const casadi_int* colind_x = x.colind();
1563  const casadi_int* row_x = x.row();
1564 
1565  // Allocate result
1566  std::vector<Sparsity> ret;
1567  ret.reserve(n);
1568 
1569  // Sparsity pattern as CCS vectors
1570  std::vector<casadi_int> colind, row;
1571  casadi_int ncol, nrow = x.size1();
1572 
1573  // Get the sparsity patterns of the outputs
1574  for (casadi_int i=0; i<n; ++i) {
1575  casadi_int first_col = offset[i];
1576  casadi_int last_col = offset[i+1];
1577  ncol = last_col - first_col;
1578 
1579  // Construct the sparsity pattern
1580  colind.resize(ncol+1);
1581  std::copy(colind_x+first_col, colind_x+last_col+1, colind.begin());
1582  for (std::vector<casadi_int>::iterator it=colind.begin()+1; it!=colind.end(); ++it)
1583  *it -= colind[0];
1584  colind[0] = 0;
1585  row.resize(colind.back());
1586  std::copy(row_x+colind_x[first_col], row_x+colind_x[last_col], row.begin());
1587 
1588  // Append to the list
1589  ret.push_back(Sparsity(nrow, ncol, colind, row));
1590  }
1591 
1592  // Return (RVO)
1593  return ret;
1594  }

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 1881 of file sparsity.cpp.

1881  {
1882  if (is_null()) return Dict();
1883  return {{"nrow", size1()}, {"ncol", size2()}, {"colind", get_colind()}, {"row", get_row()}};
1884  }
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(), operator*(), and casadi::MX::project().

◆ is_column()

bool casadi::Sparsity::is_column ( ) const

◆ 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 452 of file sparsity.cpp.

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

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 447 of file sparsity.cpp.

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

References colind(), and row().

◆ is_equal() [3/3]

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

Definition at line 443 of file sparsity.cpp.

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

◆ is_null()

Definition at line 117 of file generic_shared_impl.hpp.

109  {
110  return node==nullptr;
111  }

◆ 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 791 of file sparsity.cpp.

791  {
792  return (*this)->is_reshape(*y);
793  }

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

Definition at line 269 of file sparsity.cpp.

269  {
270  return (*this)->is_scalar(scalar_and_dense);
271  }

Referenced by casadi::SparsityInternal::_mtimes(), casadi::Matrix< Scalar >::_sym(), casadi::FunctionInternal::check_mat(), casadi::Integrator::init(), casadi::Integrator::map2oracle(), and postfix_dim().

◆ 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 1299 of file sparsity.cpp.

1299  {
1300  casadi_assert(is_square(),
1301  "is_singular: only defined for square matrices, but got " + dim());
1302  return sprank(*this)!=size2();
1303  }
static casadi_int sprank(const Sparsity &x)
Enlarge matrix.
Definition: sparsity.cpp:1655
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 439 of file sparsity.cpp.

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

◆ 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().

◆ is_symmetric()

bool casadi::Sparsity::is_symmetric ( ) const

◆ is_transpose()

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

Definition at line 787 of file sparsity.cpp.

787  {
788  return (*this)->is_transpose(*y);
789  }

◆ is_tril()

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

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

Definition at line 321 of file sparsity.cpp.

321  {
322  return (*this)->is_tril(strictly);
323  }

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

◆ 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 1961 of file sparsity.cpp.

1962  {
1963  // Consistency check
1964  casadi_assert(H.is_square(), "H must be square");
1965  casadi_assert(H.size1() == J.size2(), "Dimension mismatch");
1966 
1967  // Add diagonal to H recursively
1968  if (with_x_diag) return kkt(H + diag(H.size()), J, false, with_lam_g_diag);
1969 
1970  // Lower right entry
1971  int ng = J.size1();
1972  Sparsity B = with_lam_g_diag ? diag(ng, ng) : Sparsity(ng, ng);
1973 
1974  // Concatenate
1975  return blockcat({{H, J.T()}, {J, B}});
1976  }
static Sparsity blockcat(const std::vector< std::vector< Sparsity > > &v)
Enlarge matrix.
Definition: sparsity.cpp:1605
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:1961
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 1434 of file sparsity.cpp.

1434  {
1435  casadi_int a_ncol = a.size2();
1436  casadi_int b_ncol = b.size2();
1437  casadi_int a_nrow = a.size1();
1438  casadi_int b_nrow = b.size1();
1439  if (a.is_dense() && b.is_dense()) return Sparsity::dense(a_nrow*b_nrow, a_ncol*b_ncol);
1440 
1441  const casadi_int* a_colind = a.colind();
1442  const casadi_int* a_row = a.row();
1443  const casadi_int* b_colind = b.colind();
1444  const casadi_int* b_row = b.row();
1445 
1446  std::vector<casadi_int> r_colind(a_ncol*b_ncol+1, 0);
1447  std::vector<casadi_int> r_row(a.nnz()*b.nnz());
1448 
1449  casadi_int* r_colind_ptr = get_ptr(r_colind);
1450  casadi_int* r_row_ptr = get_ptr(r_row);
1451 
1452  casadi_int i=0;
1453  casadi_int j=0;
1454  // Loop over the columns
1455  for (casadi_int a_cc=0; a_cc<a_ncol; ++a_cc) {
1456  casadi_int a_start = a_colind[a_cc];
1457  casadi_int a_stop = a_colind[a_cc+1];
1458  // Loop over the columns
1459  for (casadi_int b_cc=0; b_cc<b_ncol; ++b_cc) {
1460  casadi_int b_start = b_colind[b_cc];
1461  casadi_int b_stop = b_colind[b_cc+1];
1462  // Loop over existing nonzeros
1463  for (casadi_int a_el=a_start; a_el<a_stop; ++a_el) {
1464  casadi_int a_r = a_row[a_el];
1465  // Loop over existing nonzeros
1466  for (casadi_int b_el=b_start; b_el<b_stop; ++b_el) {
1467  casadi_int b_r = b_row[b_el];
1468  r_row_ptr[i++] = a_r*b_nrow+b_r;
1469  }
1470  }
1471  j+=1;
1472  r_colind_ptr[j] = r_colind_ptr[j-1] + (b_stop-b_start)*(a_stop-a_start);
1473  }
1474  }
1475  return Sparsity(a_nrow*b_nrow, a_ncol*b_ncol, r_colind, r_row);
1476  }

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

◆ largest_first()

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

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

Definition at line 765 of file sparsity.cpp.

765  {
766  return (*this)->largest_first();
767  }

◆ 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 621 of file sparsity.cpp.

621  {
622  casadi_assert(is_symmetric(),
623  "LDL factorization requires a symmetric matrix");
624  // Recursive call if AMD
625  if (amd) {
626  // Get AMD reordering
627  p = this->amd();
628  // Permute sparsity pattern
629  std::vector<casadi_int> tmp;
630  Sparsity Aperm = sub(p, p, tmp);
631  // Call recursively
632  return Aperm.ldl(tmp, false);
633  }
634  // Dimension
635  casadi_int n=size1();
636  // Natural ordering
637  p = range(n);
638  // Work vector
639  std::vector<casadi_int> w(3*n);
640  // Elimination tree
641  std::vector<casadi_int> parent(n);
642  // Calculate colind in L (strictly lower entries only)
643  std::vector<casadi_int> L_colind(1+n);
644  SparsityInternal::ldl_colind(*this, get_ptr(parent), get_ptr(L_colind), get_ptr(w));
645  // Get rows in L (strictly lower entries only)
646  std::vector<casadi_int> L_row(L_colind.back());
647  SparsityInternal::ldl_row(*this, get_ptr(parent), get_ptr(L_colind), get_ptr(L_row),
648  get_ptr(w));
649  // Sparsity of L^T
650  return Sparsity(n, n, L_colind, L_row, true).T();
651  }
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:707
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 1049 of file sparsity.cpp.

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

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 
)
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 705 of file sparsity.hpp.

705 { 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 583 of file sparsity.cpp.

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

References casadi::SparsityInternal::makeDense().

◆ mtimes()

Sparsity casadi::Sparsity::mtimes ( const Sparsity x,
const Sparsity y 
)
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.

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

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

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

◆ 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 1747 of file sparsity.cpp.

1750  {
1751  // Assert dimensions
1752  casadi_assert(z_sp.size1()==x_sp.size1() && x_sp.size2()==y_sp.size1()
1753  && y_sp.size2()==z_sp.size2(),
1754  "Dimension error. Got x=" + x_sp.dim() + ", y=" + y_sp.dim()
1755  + " and z=" + z_sp.dim() + ".");
1756 
1757  // Direct access to the arrays
1758  const casadi_int* y_colind = y_sp.colind();
1759  const casadi_int* y_row = y_sp.row();
1760  const casadi_int* x_colind = x_sp.colind();
1761  const casadi_int* x_row = x_sp.row();
1762  const casadi_int* z_colind = z_sp.colind();
1763  const casadi_int* z_row = z_sp.row();
1764 
1765  // Loop over the columns of y and z
1766  casadi_int ncol = z_sp.size2();
1767  for (casadi_int cc=0; cc<ncol; ++cc) {
1768  // Get the dense column of z
1769  for (casadi_int kk=z_colind[cc]; kk<z_colind[cc+1]; ++kk) {
1770  w[z_row[kk]] = z[kk];
1771  }
1772 
1773  // Loop over the nonzeros of y
1774  for (casadi_int kk=y_colind[cc]; kk<y_colind[cc+1]; ++kk) {
1775  casadi_int rr = y_row[kk];
1776 
1777  // Loop over corresponding columns of x
1778  bvec_t yy = y[kk];
1779  for (casadi_int kk1=x_colind[rr]; kk1<x_colind[rr+1]; ++kk1) {
1780  w[x_row[kk1]] |= x[kk1] | yy;
1781  }
1782  }
1783 
1784  // Get the sparse column of z
1785  for (casadi_int kk=z_colind[cc]; kk<z_colind[cc+1]; ++kk) {
1786  z[kk] = w[z_row[kk]];
1787  }
1788  }
1789  }
unsigned long long bvec_t

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

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 1791 of file sparsity.cpp.

1794  {
1795  // Assert dimensions
1796  casadi_assert(z_sp.size1()==x_sp.size1() && x_sp.size2()==y_sp.size1()
1797  && y_sp.size2()==z_sp.size2(),
1798  "Dimension error. Got x=" + x_sp.dim() + ", y=" + y_sp.dim()
1799  + " and z=" + z_sp.dim() + ".");
1800 
1801  // Direct access to the arrays
1802  const casadi_int* y_colind = y_sp.colind();
1803  const casadi_int* y_row = y_sp.row();
1804  const casadi_int* x_colind = x_sp.colind();
1805  const casadi_int* x_row = x_sp.row();
1806  const casadi_int* z_colind = z_sp.colind();
1807  const casadi_int* z_row = z_sp.row();
1808 
1809  // Clear residual work vector data from preceding operations (not necessary
1810  // for data from this method if conditional clear code is made unconditional
1811  // in loop)
1812  casadi_int nrow = z_sp.size1();
1813  casadi_fill(w, nrow, static_cast<bvec_t>(0));
1814 
1815  // Loop over the columns of y and z
1816  casadi_int ncol = z_sp.size2();
1817  for (casadi_int cc=0; cc<ncol; ++cc) {
1818  // Get the dense column of z
1819  for (casadi_int kk=z_colind[cc]; kk<z_colind[cc+1]; ++kk) {
1820  w[z_row[kk]] = z[kk];
1821  }
1822 
1823  // Loop over the nonzeros of y
1824  for (casadi_int kk=y_colind[cc]; kk<y_colind[cc+1]; ++kk) {
1825  casadi_int rr = y_row[kk];
1826 
1827  // Loop over corresponding columns of x
1828  bvec_t yy = 0;
1829  for (casadi_int kk1=x_colind[rr]; kk1<x_colind[rr+1]; ++kk1) {
1830  yy |= w[x_row[kk1]];
1831  x[kk1] |= w[x_row[kk1]];
1832  }
1833  y[kk] |= yy;
1834  }
1835 
1836  // Get the sparse column of z, clear work vector for next column
1837  for (casadi_int kk=z_colind[cc]; kk<z_colind[cc+1]; ++kk) {
1838  z[kk] = w[z_row[kk]];
1839  w[z_row[kk]] = 0;
1840  }
1841  }
1842  }
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::LinsolQr::generate(), casadi::HorzRepsum::generate(), casadi::UnaryMX::generate(), 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::MadnlpInterface::init(), casadi::OsqpInterface::init(), casadi::SLEQPInterface::init(), casadi::SnoptInterface::init(), casadi::KinsolInterface::init(), casadi::SundialsInterface::init(), casadi::SuperscsInterface::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::Blocksqp::init_mem(), casadi::CbcInterface::init_mem(), casadi::ClpInterface::init_mem(), casadi::CplexInterface::init_mem(), casadi::HpmpcInterface::init_mem(), casadi::OsqpInterface::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::MX::MX(), casadi::LinsolQr::nfact(), norm_0_mul(), casadi::Matrix< Scalar >::print_default(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::Matrix< Scalar >::print_vector(), casadi::CodeGenerator::project(), casadi::QpoasesInterface::qpoases_init(), casadi::FmuFunction::remove_nans(), casadi::Reshape::Reshape(), row(), set(), 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::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(), and vertcat().

◆ 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 1285 of file sparsity.cpp.

1286  {
1287  casadi_assert(nrow>0, "nrow must be >0.");
1288  std::vector<casadi_int> row(nz.size());
1289  std::vector<casadi_int> col(nz.size());
1290  for (casadi_int i=0;i<nz.size();++i) {
1291  casadi_int k = nz[i];
1292  k-= ind1;
1293  row[i] = k % nrow;
1294  col[i] = k / nrow;
1295  }
1296  return triplet(nrow, ncol, row, col);
1297  }

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 1665 of file sparsity.cpp.

1665  {
1666  // Implementation borrowed from Scipy's sparsetools/csr.h
1667  casadi_assert(A.size1()==x.size2(), "Dimension error. Got " + x.dim()
1668  + " times " + A.dim() + ".");
1669 
1670  casadi_int n_row = A.size2();
1671  casadi_int n_col = x.size1();
1672 
1673  // Allocate work vectors
1674  std::vector<bool> Bwork(n_col);
1675  std::vector<casadi_int> Iwork(n_row+1+n_col);
1676 
1677  const casadi_int* Aj = A.row();
1678  const casadi_int* Ap = A.colind();
1679  const casadi_int* Bj = x.row();
1680  const casadi_int* Bp = x.colind();
1681  casadi_int *Cp = get_ptr(Iwork);
1682  casadi_int *mask = Cp+n_row+1;
1683 
1684  // Pass 1
1685  // method that uses O(n) temp storage
1686  std::fill(mask, mask+n_col, -1);
1687 
1688  Cp[0] = 0;
1689  casadi_int nnz = 0;
1690  for (casadi_int i = 0; i < n_row; i++) {
1691  casadi_int row_nnz = 0;
1692  for (casadi_int jj = Ap[i]; jj < Ap[i+1]; jj++) {
1693  casadi_int j = Aj[jj];
1694  for (casadi_int kk = Bp[j]; kk < Bp[j+1]; kk++) {
1695  casadi_int k = Bj[kk];
1696  if (mask[k] != i) {
1697  mask[k] = i;
1698  row_nnz++;
1699  }
1700  }
1701  }
1702  casadi_int next_nnz = nnz + row_nnz;
1703  nnz = next_nnz;
1704  Cp[i+1] = nnz;
1705  }
1706 
1707  // Pass 2
1708  casadi_int *next = get_ptr(Iwork) + n_row+1;
1709  std::fill(next, next+n_col, -1);
1710  std::vector<bool> & sums = Bwork;
1711  std::fill(sums.begin(), sums.end(), false);
1712  nnz = 0;
1713  Cp[0] = 0;
1714  for (casadi_int i = 0; i < n_row; i++) {
1715  casadi_int head = -2;
1716  casadi_int length = 0;
1717  casadi_int jj_start = Ap[i];
1718  casadi_int jj_end = Ap[i+1];
1719  for (casadi_int jj = jj_start; jj < jj_end; jj++) {
1720  casadi_int j = Aj[jj];
1721  casadi_int kk_start = Bp[j];
1722  casadi_int kk_end = Bp[j+1];
1723  for (casadi_int kk = kk_start; kk < kk_end; kk++) {
1724  casadi_int k = Bj[kk];
1725  sums[k] = true;
1726  if (next[k] == -1) {
1727  next[k] = head;
1728  head = k;
1729  length++;
1730  }
1731  }
1732  }
1733  for (casadi_int jj = 0; jj < length; jj++) {
1734  if (sums[head]) {
1735  nnz++;
1736  }
1737  casadi_int temp = head;
1738  head = next[head];
1739  next[temp] = -1; //clear arrays
1740  sums[temp] = 0;
1741  }
1742  Cp[i+1] = nnz;
1743  }
1744  return nnz;
1745  }

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 1661 of file sparsity.cpp.

1661  {
1662  return &(*this)->sp().front();
1663  }

◆ 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 1314 of file sparsity.cpp.

1314  {
1315  return (*this)->sp();
1316  }

◆ operator SparsityStruct()

casadi::Sparsity::operator SparsityStruct ( ) const

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

Definition at line 1318 of file sparsity.cpp.

1318  {
1319  const casadi_int* sp = *this;
1320  casadi_int nrow = sp[0], ncol = sp[1];
1321  const casadi_int* colind = sp+2, *row = sp+2+ncol+1;
1322  return SparsityStruct{nrow, ncol, colind, row};
1323  }

◆ 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:443

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:2018

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 461 of file sparsity.cpp.

461  {
462  std::vector< unsigned char > mapping;
463  return intersect(b, mapping);
464  }
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 457 of file sparsity.cpp.

457  {
458  return unite(b);
459  }
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 90 of file generic_shared_impl.hpp.

◆ pattern_inverse()

Sparsity casadi::Sparsity::pattern_inverse ( ) const

Definition at line 466 of file sparsity.cpp.

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

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 1364 of file sparsity.cpp.

1364  {
1365  casadi_assert(casadi::is_permutation(p),
1366  "Sparsity::permutation supplied list is not a permutation.");
1367  std::vector<casadi_int> colind = range(p.size()+1);
1368  if (invert) {
1369  return Sparsity(p.size(), p.size(), colind, p);
1370  } else {
1371  return Sparsity(p.size(), p.size(), colind, invert_permutation(p));
1372  }
1373  }
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 1375 of file sparsity.cpp.

1375  {
1376  casadi_assert(is_permutation(), "Sparsity::permutation called on non-permutation matrix.");
1377  if (invert) {
1378  return get_row();
1379  } else {
1380  return invert_permutation(get_row());
1381  }
1382  }
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 769 of file sparsity.cpp.

770  {
771  return (*this)->pmult(p, permute_rows, permute_columns, invert_permutation);
772  }

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 591 of file sparsity.cpp.

591  {
592  if (is_dense()) {
593  if (is_scalar()) {
594  return "";
595  } else if (is_empty(true)) {
596  return "[]";
597  } else if (is_column()) {
598  return "[" + str(size1()) + "]";
599  } else {
600  return "[" + dim(false) + "]";
601  }
602  } else {
603  return "[" + dim(true) + "]";
604  }
605  }
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 653 of file sparsity.cpp.

655  {
656  // Dimensions
657  casadi_int size1=this->size1(), size2=this->size2();
658 
659  // Recursive call if AMD
660  if (amd) {
661  // Get AMD reordering
662  pc = mtimes(T(), *this).amd();
663  // Permute sparsity pattern
664  std::vector<casadi_int> tmp;
665  Sparsity Aperm = sub(range(size1), pc, tmp);
666  // Call recursively
667  return Aperm.qr_sparse(V, R, prinv, tmp, false);
668  }
669 
670  // No column permutation
671  pc = range(size2);
672 
673  // Allocate memory
674  std::vector<casadi_int> leftmost(size1);
675  std::vector<casadi_int> parent(size2);
676  prinv.resize(size1 + size2);
677  std::vector<casadi_int> iw(size1 + 7*size2 + 1);
678 
679  // Initialize QP solve
680  casadi_int nrow_ext, v_nnz, r_nnz;
681  SparsityInternal::qr_init(*this, T(),
682  get_ptr(leftmost), get_ptr(parent), get_ptr(prinv),
683  &nrow_ext, &v_nnz, &r_nnz, get_ptr(iw));
684 
685  // Calculate sparsities
686  std::vector<casadi_int> sp_v(2 + size2 + 1 + v_nnz);
687  std::vector<casadi_int> sp_r(2 + size2 + 1 + r_nnz);
688  SparsityInternal::qr_sparsities(*this, nrow_ext, get_ptr(sp_v), get_ptr(sp_r),
689  get_ptr(leftmost), get_ptr(parent), get_ptr(prinv),
690  get_ptr(iw));
691  prinv.resize(nrow_ext);
692  V = compressed(sp_v, true);
693  R = compressed(sp_r, true);
694  }
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)
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 731 of file sparsity.cpp.

731  {
732  *this = (*this)->_removeDuplicates(mapping);
733  }

◆ repr_el()

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

Definition at line 607 of file sparsity.cpp.

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

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::FmuFunction::check_hessian(), 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(), 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::CplexInterface::init_mem(), is_equal(), casadi::SparsityInternal::is_equal(), casadi::SparsityInternal::is_stacked(), casadi::XFunction< DerivedType, MatType, NodeType >::jac(), kron(), lower(), casadi::FmuFunction::make_symmetric(), mul_sparsityF(), mul_sparsityR(), casadi::SparsityInternal::multiply(), nonzeros(), norm_0_mul(), casadi::Matrix< Scalar >::print_dense(), casadi::Matrix< Scalar >::print_sparse(), casadi::Matrix< Scalar >::print_vector(), casadi::CvodesInterface::psetupF(), casadi::IdasInterface::psetupF(), casadi::FmuFunction::remove_nans(), 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::Rank1::sp_forward(), casadi::Solve< Tr >::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::Rank1::sp_reverse(), casadi::Solve< Tr >::sp_reverse(), casadi::FunctionInternal::sp_reverse(), Sparsity(), 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 1111 of file sparsity.cpp.

1112  {
1113  std::vector<casadi_int> all_rows, all_cols;
1114  all_rows.reserve(row.size()*col.size());
1115  all_cols.reserve(row.size()*col.size());
1116  for (std::vector<casadi_int>::const_iterator c_it=col.begin(); c_it!=col.end(); ++c_it) {
1117  casadi_assert(*c_it>=0 && *c_it<ncol, "Sparsity::rowcol: Column index out of bounds");
1118  for (std::vector<casadi_int>::const_iterator r_it=row.begin(); r_it!=row.end(); ++r_it) {
1119  casadi_assert(*r_it>=0 && *r_it<nrow, "Sparsity::rowcol: Row index out of bounds");
1120  all_rows.push_back(*r_it);
1121  all_cols.push_back(*c_it);
1122  }
1123  }
1124  return Sparsity::triplet(nrow, ncol, all_rows, all_cols);
1125  }

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 727 of file sparsity.cpp.

727  {
728  return (*this)->rowsSequential(strictly);
729  }

◆ 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 703 of file sparsity.cpp.

703  {
704  return (*this)->scc(index, offset);
705  }

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 2006 of file sparsity.cpp.

2006  {
2007  std::stringstream ss;
2008  serialize(ss);
2009  return ss.str();
2010  }
std::string serialize() const
Serialize.
Definition: sparsity.cpp:2006

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 1988 of file sparsity.cpp.

1988  {
1989  if (is_null()) {
1990  s.pack("SparsityInternal::compressed", std::vector<casadi_int>{});
1991  } else {
1992  s.pack("SparsityInternal::compressed", compress());
1993  }
1994  }
std::vector< casadi_int > compress(bool canonical=true) const
Compress a sparsity pattern.
Definition: sparsity.cpp:1305

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 1978 of file sparsity.cpp.

1978  {
1979  SerializingStream s(stream);
1980  serialize(s);
1981  }

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 1248 of file sparsity.hpp.

1248  {
1249  // Get dimensions of this
1250  const casadi_int sz = nnz();
1251  const casadi_int sz1 = size1();
1252  const casadi_int sz2 = size2();
1253 
1254  // Get dimensions of assigning matrix
1255  const casadi_int val_sz = val_sp.nnz();
1256  const casadi_int val_sz1 = val_sp.size1();
1257  const casadi_int val_sz2 = val_sp.size2();
1258  const casadi_int val_nel = val_sz1*val_sz2;
1259 
1260  // Check if sparsity matches
1261  if (val_sp==*this) {
1262  std::copy(val_data, val_data+sz, data);
1263  } else if (this->is_empty()) {
1264  // Quick return
1265  return;
1266  } else if (val_sp.is_empty()) {
1267  // Quick return
1268  return;
1269  } else if (val_nel==1) { // if scalar
1270  std::fill(data, data+sz, val_sz==0 ? DataType(0) : val_data[0]);
1271  } else if (sz2==val_sz2 && sz1==val_sz1) {
1272  // Matching dimensions
1273  // Sparsity
1274  const casadi_int* c = row();
1275  const casadi_int* rind = colind();
1276  const casadi_int* v_c = val_sp.row();
1277  const casadi_int* v_rind = val_sp.colind();
1278 
1279  // For all columns
1280  for (casadi_int i=0; i<sz2; ++i) {
1281 
1282  // Nonzero of the assigning matrix
1283  casadi_int v_el = v_rind[i];
1284 
1285  // First nonzero of the following column
1286  casadi_int v_el_end = v_rind[i+1];
1287 
1288  // Next row of the assigning matrix
1289  casadi_int v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1290 
1291  // Assign all nonzeros
1292  for (casadi_int el=rind[i]; el!=rind[i+1]; ++el) {
1293 
1294  // Get row
1295  casadi_int j=c[el];
1296 
1297  // Forward the assigning nonzero
1298  while (v_j<j) {
1299  v_el++;
1300  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1301  }
1302 
1303  // Assign nonzero
1304  if (v_j==j) {
1305  data[el] = val_data[v_el++];
1306  v_j = v_el<v_el_end ? v_c[v_el] : sz1;
1307  } else {
1308  data[el] = 0;
1309  }
1310  }
1311  }
1312  } else if (sz1==val_sz2 && sz2==val_sz1 && sz2 == 1) {
1313  // Assign transposed (this is column)
1314  const casadi_int* v_cind = val_sp.colind();
1315  const casadi_int* r = row();
1316  for (casadi_int el=0; el<sz; ++el) {
1317  casadi_int rr=r[el];
1318  data[el] = v_cind[rr]==v_cind[rr+1] ? 0 : val_data[v_cind[rr]];
1319  }
1320  } else if (sz1==val_sz2 && sz2==val_sz1 && sz1 == 1) {
1321  // Assign transposed (this is row)
1322  for (casadi_int el=0; el<sz; ++el) data[el] = 0;
1323  const casadi_int* cind = colind();
1324  const casadi_int* v_r = val_sp.row();
1325  for (casadi_int el=0; el<val_sz; ++el) {
1326  casadi_int rr=v_r[el];
1327  if (cind[rr]!=cind[rr+1]) {
1328  data[cind[rr]] = val_data[el];
1329  }
1330  }
1331  } else {
1332  // Make sure that dimension matches
1333  casadi_error("Sparsity::set<DataType>: shape mismatch. lhs is "
1334  + dim() + ", while rhs is " + val_sp.dim() + ".");
1335  }
1336  }

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::FmuFunction::check_hessian(), 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::GetNonzeros::eval_mx(), casadi::SetNonzeros< Add >::eval_mx(), casadi::XFunction< DerivedType, MatType, NodeType >::export_code(), casadi::FunctionInternal::from_compact(), from_file(), casadi::Expm::get_forward(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_partition(), casadi::Dple::get_reverse(), casadi::Expm::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::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(), ldl(), casadi::FmuFunction::make_symmetric(), mtimes(), mul_sparsityF(), 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::FmuFunction::remove_nans(), casadi::replace_mat(), resize(), casadi::Conic::sdp_to_socp_init(), set(), 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::ProxqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::SuperscsInterface::solve(), casadi::Solve< Tr >::sp_forward(), casadi::Integrator::sp_jac_aug(), 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::GetNonzeros::eval_mx(), casadi::FmuFunction::eval_task(), casadi::XFunction< DerivedType, MatType, NodeType >::export_code(), casadi::FunctionInternal::from_compact(), from_file(), casadi::FunctionInternal::get_jac_sparsity_hierarchical(), casadi::FunctionInternal::get_partition(), 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::SuperscsInterface::init(), casadi::FastNewton::init(), casadi::Ipqp::init(), casadi::SymbolicQr::init(), casadi::ClarabelInterface::init_dependent(), casadi::HighsInterface::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(), mtimes(), mul_sparsityF(), mul_sparsityR(), casadi::SparsityInternal::multiply(), norm_0_mul(), casadi::Horzcat::off(), casadi::Diagcat::off(), casadi::OptiNode::parameter(), 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::ProxqpInterface::solve(), casadi::QpoasesInterface::solve(), casadi::Rank1::sp_forward(), casadi::Transpose::sp_forward(), casadi::FunctionInternal::sp_forward_block(), casadi::Integrator::sp_jac_aug(), casadi::Rank1::sp_reverse(), casadi::Transpose::sp_reverse(), casadi::FunctionInternal::sp_reverse(), 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 1844 of file sparsity.cpp.

1844  {
1845  const Sparsity& x = *this;
1846  if (X==x) return Y;
1847  if (X==Y) return x;
1848  std::vector<unsigned char> mapping;
1849  X.unite(x, mapping);
1850 
1851 
1852  const casadi_int* Y_colind = Y.colind();
1853  const casadi_int* Y_row = Y.row();
1854  std::vector<casadi_int> y_colind(Y.size2()+1, 0);
1855  std::vector<casadi_int> y_row;
1856  y_row.reserve(Y.nnz());
1857  casadi_assert_dev(Y.nnz()==mapping.size());
1858 
1859  casadi_int i = 0;
1860  // Loop over columns of Y
1861  for (casadi_int cc=0; cc<Y.size2(); ++cc) {
1862  y_colind[cc+1] = y_colind[cc];
1863  // Loop over nonzeros of Y in column cc
1864  for (casadi_int kk=Y_colind[cc]; kk<Y_colind[cc+1]; ++kk) {
1865  // Get corresponding map entry
1866  casadi_int e = mapping[i++];
1867  if (e==3) {
1868  // Preserve element
1869  y_colind[cc+1]++;
1870  y_row.push_back(Y_row[kk]);
1871  } else {
1872  casadi_assert_dev(e==1);
1873  }
1874  }
1875  }
1876 
1877  Sparsity ret(Y.size1(), Y.size2(), y_colind, y_row, true);
1878  return ret;
1879  }

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

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

◆ 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 1655 of file sparsity.cpp.

1655  {
1656  std::vector<casadi_int> rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock;
1657  x.btf(rowperm, colperm, rowblock, colblock, coarse_rowblock, coarse_colblock);
1658  return coarse_colblock.at(3);
1659  }

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 783 of file sparsity.cpp.

783  {
784  (*this)->spy(stream);
785  }

◆ 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 774 of file sparsity.cpp.

774  {
775  (*this)->spy_matlab(mfile);
776  }

◆ 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 757 of file sparsity.cpp.

757  {
758  return (*this)->star_coloring(ordering, cutoff);
759  }

References casadi::SparsityInternal::star_coloring().

Referenced by casadi::FunctionInternal::get_jac_sparsity_hierarchical_symm(), casadi::FunctionInternal::get_partition(), casadi::FmuFunction::init(), 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 761 of file sparsity.cpp.

761  {
762  return (*this)->star_coloring2(ordering, cutoff);
763  }

References casadi::SparsityInternal::star_coloring2().

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

◆ 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 1651 of file sparsity.cpp.

1651  {
1652  return mtimes(Sparsity::dense(1, x.size1()), x);
1653  }

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

◆ 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 1647 of file sparsity.cpp.

1647  {
1648  return mtimes(x, Sparsity::dense(x.size2(), 1));
1649 
1650  }

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

◆ swap()

Definition at line 106 of file generic_shared_impl.hpp.

120  {
121  GenericShared<Shared, Internal> temp = *this;
122  *this = other;
123  other = temp;
124  }

◆ 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 1906 of file sparsity.cpp.

1906  {
1907  std::string format = file_format(filename, format_hint, file_formats);
1908  std::ofstream out;
1909  Filesystem::open(out, filename);
1910  if (format=="mtx") {
1911  out << std::scientific << std::setprecision(std::numeric_limits<double>::digits10 + 1);
1912  out << "%%MatrixMarket matrix coordinate pattern general" << std::endl;
1913  out << size1() << " " << size2() << " " << nnz() << std::endl;
1914  std::vector<casadi_int> row = get_row();
1915  std::vector<casadi_int> col = get_col();
1916 
1917  for (casadi_int k=0;k<row.size();++k) {
1918  out << row[k]+1 << " " << col[k]+1 << std::endl;
1919  }
1920  } else {
1921  casadi_error("Unknown format '" + format + "'");
1922  }
1923  }
static void open(std::ofstream &, 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::open(), 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 979 of file sparsity.cpp.

979  {
980  return x->_tril(includeDiagonal);
981  }

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 1279 of file sparsity.cpp.

1280  {
1281  std::vector<casadi_int> mapping;
1282  return Sparsity::triplet(nrow, ncol, row, col, mapping, false);
1283  }

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 1127 of file sparsity.cpp.

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

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 983 of file sparsity.cpp.

983  {
984  return x->_triu(includeDiagonal);
985  }

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 1140 of file sparsity.hpp.

1140 {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 749 of file sparsity.cpp.

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

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 1104 of file sparsity.cpp.

1104  {
1105  std::vector<casadi_int> row(1, el), colind(2);
1106  colind[0] = 0;
1107  colind[1] = 1;
1108  return Sparsity(n, 1, colind, row);
1109  }

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 1028 of file sparsity.cpp.

1028  {
1029  casadi_assert(n>=0, "Sparsity::upper expects a positive integer as argument");
1030  casadi_int nrow=n, ncol=n;
1031  std::vector<casadi_int> colind, row;
1032  colind.reserve(ncol+1);
1033  row.reserve((n*(n+1))/2);
1034 
1035  // Loop over columns
1036  colind.push_back(0);
1037  for (casadi_int cc=0; cc<ncol; ++cc) {
1038  // Loop over rows for the upper triangular half
1039  for (casadi_int rr=0; rr<=cc; ++rr) {
1040  row.push_back(rr);
1041  }
1042  colind.push_back(row.size());
1043  }
1044 
1045  // Return the pattern
1046  return Sparsity(nrow, ncol, colind, row);
1047  }

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 1478 of file sparsity.cpp.

1478  {
1479  // Quick return if possible
1480  if (sp.empty()) return Sparsity(0, 1);
1481  if (sp.size()==1) return sp.front();
1482 
1483  // Count total nnz
1484  casadi_int nnz_total = 0;
1485  for (casadi_int i=0; i<sp.size(); ++i) nnz_total += sp[i].nnz();
1486 
1487  // Construct from vectors (triplet format)
1488  std::vector<casadi_int> ret_row, ret_col;
1489  ret_row.reserve(nnz_total);
1490  ret_col.reserve(nnz_total);
1491  casadi_int ret_nrow = 0;
1492  casadi_int ret_ncol = 0;
1493  for (casadi_int i=0; i<sp.size() && ret_ncol==0; ++i)
1494  ret_ncol = sp[i].size2();
1495 
1496  // Append all patterns
1497  for (std::vector<Sparsity>::const_iterator i=sp.begin(); i!=sp.end(); ++i) {
1498  // Get sparsity pattern
1499  casadi_int sp_nrow = i->size1();
1500  casadi_int sp_ncol = i->size2();
1501  const casadi_int* sp_colind = i->colind();
1502  const casadi_int* sp_row = i->row();
1503  casadi_assert(sp_ncol==ret_ncol || sp_ncol==0,
1504  "Sparsity::vertcat: Mismatching number of columns");
1505 
1506  // Add entries to pattern
1507  for (casadi_int cc=0; cc<sp_ncol; ++cc) {
1508  for (casadi_int k=sp_colind[cc]; k<sp_colind[cc+1]; ++k) {
1509  ret_row.push_back(sp_row[k] + ret_nrow);
1510  ret_col.push_back(cc);
1511  }
1512  }
1513 
1514  // Update offset
1515  ret_nrow += sp_nrow;
1516  }
1517  return Sparsity::triplet(ret_nrow, ret_ncol, ret_row, ret_col);
1518  }

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 1596 of file sparsity.cpp.

1597  {
1598  std::vector<Sparsity> ret = horzsplit(x.T(), offset);
1599  for (std::vector<Sparsity>::iterator it=ret.begin(); it!=ret.end(); ++it) {
1600  *it = it->T();
1601  }
1602  return ret;
1603  }
static std::vector< Sparsity > horzsplit(const Sparsity &x, const std::vector< casadi_int > &offset)
Enlarge matrix.
Definition: sparsity.cpp:1547

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

◆ weak()

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

Definition at line 131 of file generic_shared_impl.hpp.

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

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 1183 of file sparsity.hpp.

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


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