List of all members | Static Public Member Functions | Protected Member Functions | Friends
casadi::SparsityInterface< MatType > Class Template Reference

Sparsity interface class. More...

#include <sparsity_interface.hpp>

Detailed Description

template<typename MatType>
class casadi::SparsityInterface< MatType >

This is a common base class for GenericMatrix (i.e. MX and Matrix<>) and Sparsity, introducing a uniform syntax and implementing common functionality using the curiously recurring template pattern (CRTP) idiom.

Author
Joel Andersson
Date
2014

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

Definition at line 51 of file sparsity_interface.hpp.

Inheritance diagram for casadi::SparsityInterface< MatType >:
Inheritance graph
[legend]
Collaboration diagram for casadi::SparsityInterface< MatType >:
Collaboration graph
[legend]

Static Public Member Functions

static std::vector< std::vector< MatType > > blocksplit (const MatType &x, const std::vector< casadi_int > &vert_offset, const std::vector< casadi_int > &horz_offset)
 
static std::vector< std::vector< MatType > > blocksplit (const MatType &x, casadi_int vert_incr, casadi_int horz_incr)
 
static MatType veccat (const std::vector< MatType > &x)
 
static MatType vec (const MatType &x)
 
static MatType repmat (const MatType &x, casadi_int n, casadi_int m=1)
 
static std::vector< casadi_int > offset (const std::vector< MatType > &v, bool vert=true)
 
static std::vector< MatType > diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset)
 
static std::vector< MatType > diagsplit (const MatType &x, casadi_int incr)
 
static std::vector< MatType > diagsplit (const MatType &x, casadi_int incr1, casadi_int incr2)
 
static MatType mtimes (const std::vector< MatType > &args, const std::string &blas="reference")
 
static std::vector< MatType > horzsplit (const MatType &x, casadi_int incr)
 
static std::vector< MatType > vertsplit (const MatType &x, casadi_int incr)
 
static std::vector< MatType > horzsplit_n (const MatType &x, casadi_int n)
 
static std::vector< MatType > vertsplit_n (const MatType &x, casadi_int n)
 
static MatType repmat (const MatType &A, const std::pair< casadi_int, casadi_int > &rc)
 

Protected Member Functions

const MatType & self () const
 
MatType & self ()
 

Friends

MatType horzcat (const std::vector< MatType > &v)
 Concatenate a list of matrices horizontally. More...
 
MatType vertcat (const std::vector< MatType > &v)
 Concatenate a list of matrices vertically. More...
 
std::vector< MatType > horzsplit (const MatType &x, const std::vector< casadi_int > &offset)
 Split a matrix horizontally into groups of columns. More...
 
std::vector< MatType > horzsplit (const MatType &x, casadi_int incr=1)
 Split a matrix horizontally into fixed-size groups of columns. More...
 
std::vector< MatType > horzsplit_n (const MatType &x, casadi_int n)
 Split a matrix horizontally into a given number of equal groups of columns. More...
 
std::vector< MatType > vertsplit (const MatType &x, const std::vector< casadi_int > &offset)
 Split a matrix vertically into groups of rows. More...
 
std::vector< casadi_int > offset (const std::vector< MatType > &v, bool vert=true)
 Get the block offsets of a list of matrices. More...
 
std::vector< MatType > vertsplit (const MatType &x, casadi_int incr=1)
 Split a matrix vertically into fixed-size groups of rows. More...
 
std::vector< MatType > vertsplit_n (const MatType &x, casadi_int n)
 Split a matrix vertically into a given number of equal groups of rows. More...
 
MatType blockcat (const std::vector< std::vector< MatType > > &v)
 Construct a matrix from a list of lists of blocks. More...
 
MatType blockcat (const MatType &A, const MatType &B, const MatType &C, const MatType &D)
 Construct a matrix from 4 blocks. More...
 
std::vector< std::vector< MatType > > blocksplit (const MatType &x, const std::vector< casadi_int > &vert_offset, const std::vector< casadi_int > &horz_offset)
 Split a matrix into a grid of blocks. More...
 
std::vector< std::vector< MatType > > blocksplit (const MatType &x, casadi_int vert_incr=1, casadi_int horz_incr=1)
 Split a matrix into a grid of fixed-size blocks. More...
 
MatType diagcat (const std::vector< MatType > &A)
 Construct a block-diagonal matrix from a list of matrices. More...
 
std::vector< MatType > diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset1, const std::vector< casadi_int > &output_offset2)
 Split a matrix into diagonal blocks of given row and column boundaries. More...
 
std::vector< MatType > diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset)
 Split a square matrix into square diagonal blocks of given boundaries. More...
 
std::vector< MatType > diagsplit (const MatType &x, casadi_int incr=1)
 Split a square matrix into square diagonal blocks of fixed size. More...
 
std::vector< MatType > diagsplit (const MatType &x, casadi_int incr1, casadi_int incr2)
 Split a matrix into diagonal blocks of fixed size. More...
 
MatType veccat (const std::vector< MatType > &x)
 Concatenate vertically after vectorizing every argument. More...
 
MatType mtimes (const MatType &x, const MatType &y, const std::string &blas="reference")
 Matrix product of two matrices. More...
 
MatType mtimes (const std::vector< MatType > &args, const std::string &blas="reference")
 Matrix product of n matrices. More...
 
MatType mac (const MatType &x, const MatType &y, const MatType &z, const std::string &blas="reference")
 Multiply-accumulate operation. More...
 
MatType transpose (const MatType &X)
 Transpose. More...
 
MatType vec (const MatType &x)
 Reshape a matrix into a column vector. More...
 
MatType reshape (const MatType &x, casadi_int nrow, casadi_int ncol)
 Returns a reshaped version of the matrix. More...
 
MatType reshape (const MatType &x, std::pair< casadi_int, casadi_int > rc)
 Returns a reshaped version of the matrix, dimensions as a vector. More...
 
MatType reshape (const MatType &x, const Sparsity &sp)
 Reshape the matrix to a given sparsity pattern. More...
 
MatType sparsity_cast (const MatType &x, const Sparsity &sp)
 Cast matrix nonzeros to different Sparsity. More...
 
casadi_int sprank (const MatType &x)
 Obtain the structural rank of a sparsity-pattern. More...
 
casadi_int norm_0_mul (const MatType &x, const MatType &y)
 0-norm (nonzero count) of a Matrix-matrix product More...
 
MatType triu (const MatType &x, bool includeDiagonal=true)
 Get the upper triangular part of a matrix. More...
 
MatType tril (const MatType &x, bool includeDiagonal=true)
 Get the lower triangular part of a matrix. More...
 
MatType kron (const MatType &a, const MatType &b)
 Kronecker tensor product. More...
 
MatType repmat (const MatType &A, casadi_int n, casadi_int m=1)
 Repeat matrix A n times vertically and m times horizontally. More...
 
MatType repmat (const MatType &A, const std::pair< casadi_int, casadi_int > &rc)
 Repeat matrix A n times vertically and m times horizontally. More...
 
MatType horzcat (const MatType &x, const MatType &y)
 Concatenate horizontally, two matrices. More...
 
MatType horzcat (const MatType &x, const MatType &y, const MatType &z)
 Concatenate horizontally, three matrices. More...
 
MatType horzcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w)
 Concatenate horizontally, four matrices. More...
 
MatType horzcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v)
 Concatenate horizontally, five matrices. More...
 
MatType horzcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v, const MatType &u)
 Concatenate horizontally, six matrices. More...
 
MatType vertcat (const MatType &x, const MatType &y)
 Concatenate vertically, two matrices. More...
 
MatType vertcat (const MatType &x, const MatType &y, const MatType &z)
 Concatenate vertically, three matrices. More...
 
MatType vertcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w)
 Concatenate vertically, four matrices. More...
 
MatType vertcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v)
 Concatenate vertically, five matrices. More...
 
MatType vertcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v, const MatType &u)
 Concatenate vertically, six matrices. More...
 
MatType diagcat (const MatType &x, const MatType &y)
 Concatenate along diagonal, two matrices. More...
 
MatType diagcat (const MatType &x, const MatType &y, const MatType &z)
 Concatenate along diagonal, three matrices. More...
 
MatType diagcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w)
 Concatenate along diagonal, four matrices. More...
 
MatType diagcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v)
 Concatenate along diagonal, five matrices. More...
 
MatType diagcat (const MatType &x, const MatType &y, const MatType &z, const MatType &w, const MatType &v, const MatType &u)
 Concatenate along diagonal, six matrices. More...
 
MatType sum1 (const MatType &x)
 Sum the elements of every column. More...
 
MatType sum2 (const MatType &x)
 Sum the elements of every row. More...
 
MatType sum (const MatType &x)
 Sum all elements. More...
 

Member Function Documentation

◆ blocksplit() [1/2]

template<typename MatType >
std::vector< std::vector< MatType > > casadi::SparsityInterface< MatType >::blocksplit ( const MatType &  x,
casadi_int  vert_incr,
casadi_int  horz_incr 
)
static

Definition at line 2541 of file sparsity_interface.hpp.

2542  {
2543  casadi_assert(vert_incr>=1, "blocksplit: vert_incr (" + str(vert_incr) + ") must be positive");
2544  casadi_assert(horz_incr>=1, "blocksplit: horz_incr (" + str(horz_incr) + ") must be positive");
2545  casadi_int sz1 = x.size1();
2546  std::vector<casadi_int> offset1 = range(0, sz1, vert_incr);
2547  offset1.push_back(sz1);
2548  casadi_int sz2 = x.size2();
2549  std::vector<casadi_int> offset2 = range(0, sz2, horz_incr);
2550  offset2.push_back(sz2);
2551  return blocksplit(x, offset1, offset2);
2552  }
static std::vector< std::vector< MatType > > blocksplit(const MatType &x, const std::vector< casadi_int > &vert_offset, const std::vector< casadi_int > &horz_offset)
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.
std::string str(const T &v)
String representation, any type.

References casadi::range(), and casadi::str().

◆ blocksplit() [2/2]

template<typename MatType >
std::vector< std::vector< MatType > > casadi::SparsityInterface< MatType >::blocksplit ( const MatType &  x,
const std::vector< casadi_int > &  vert_offset,
const std::vector< casadi_int > &  horz_offset 
)
static

Definition at line 2530 of file sparsity_interface.hpp.

2532  {
2533  std::vector<MatType> rows = MatType::vertsplit(x, vert_offset);
2534  std::vector< std::vector< MatType > > ret;
2535  for (auto&& r : rows) ret.push_back(MatType::horzsplit(r, horz_offset));
2536  return ret;
2537  }

◆ diagsplit() [1/3]

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit ( const MatType &  x,
casadi_int  incr 
)
static

Definition at line 2590 of file sparsity_interface.hpp.

2590  {
2591  casadi_assert(incr>=1, "diagsplit(x,incr): incr (" + str(incr) + ") must be positive");
2592  casadi_assert(x.is_square(), "diagsplit(x,incr)::input must be square but got "
2593  + x.dim() + ".");
2594  std::vector<casadi_int> offset2 = range(0, x.size2(), incr);
2595  offset2.push_back(x.size2());
2596  return MatType::diagsplit(x, offset2);
2597  }

References casadi::range(), and casadi::str().

◆ diagsplit() [2/3]

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit ( const MatType &  x,
casadi_int  incr1,
casadi_int  incr2 
)
static

Definition at line 2601 of file sparsity_interface.hpp.

2601  {
2602  casadi_assert(incr1>=1, "diagsplit: incr1 (" + str(incr1) + ") must be positive");
2603  casadi_assert(incr2>=1, "diagsplit: incr2 (" + str(incr2) + ") must be positive");
2604  std::vector<casadi_int> offset1 = range(0, x.size1(), incr1);
2605  offset1.push_back(x.size1());
2606  std::vector<casadi_int> offset2 = range(0, x.size2(), incr2);
2607  offset2.push_back(x.size2());
2608  return MatType::diagsplit(x, offset1, offset2);
2609  }

References casadi::range(), and casadi::str().

◆ diagsplit() [3/3]

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::diagsplit ( const MatType &  x,
const std::vector< casadi_int > &  output_offset 
)
static

Definition at line 2581 of file sparsity_interface.hpp.

2582  {
2583  casadi_assert(x.is_square(), "diagsplit(x,incr)::input must be square but got "
2584  + x.dim() + ".");
2585  return MatType::diagsplit(x, output_offset, output_offset);
2586  }

◆ horzsplit()

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::horzsplit ( const MatType &  x,
casadi_int  incr 
)
static

Definition at line 2623 of file sparsity_interface.hpp.

2623  {
2624  casadi_assert(incr>=1, "horzsplit(x,incr): incr (" + str(incr) + ") must be positive");
2625  casadi_int sz2 = x.size2();
2626  std::vector<casadi_int> offset2 = range(0, sz2, incr);
2627  offset2.push_back(sz2);
2628  return MatType::horzsplit(x, offset2);
2629  }

References casadi::range(), and casadi::str().

◆ horzsplit_n()

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::horzsplit_n ( const MatType &  x,
casadi_int  n 
)
static

Definition at line 2640 of file sparsity_interface.hpp.

2640  {
2641  casadi_assert(n>=0, "horzsplit_n(x,n): n (" + str(n) + ") must be non-negative");
2642  if (x.size2()==0) return std::vector<MatType>(n, x);
2643  casadi_assert(n>0 && x.size2() % n==0, "horzsplit_n(x,n): x.size2() (" + str(x.size2()) +
2644  ") must be a multiple of n (" + str(n) + ")");
2645  return horzsplit(x, x.size2()/n);
2646  }
static std::vector< MatType > horzsplit(const MatType &x, casadi_int incr)

References casadi::str().

◆ mtimes()

template<typename MatType >
MatType casadi::SparsityInterface< MatType >::mtimes ( const std::vector< MatType > &  args,
const std::string &  blas = "reference" 
)
static

Definition at line 2612 of file sparsity_interface.hpp.

2613  {
2614  casadi_assert(!args.empty(),
2615  "mul(std::vector<MatType> &args): "
2616  "supplied list must not be empty.");
2617  MatType ret = args[0];
2618  for (casadi_int i=1; i<args.size(); ++i) ret = MatType::mtimes(ret, args[i], blas);
2619  return ret;
2620  }

◆ offset()

template<typename MatType >
std::vector< casadi_int > casadi::SparsityInterface< MatType >::offset ( const std::vector< MatType > &  v,
bool  vert = true 
)
static

Definition at line 2556 of file sparsity_interface.hpp.

2556  {
2557  std::vector<casadi_int> ret(v.size()+1);
2558  ret[0]=0;
2559  for (casadi_int i=0; i<v.size(); ++i) {
2560  ret[i+1] = ret[i] + (vert ? v[i].size1() : v[i].size2());
2561  }
2562  return ret;
2563  }

Referenced by casadi::MX::horzsplit(), and casadi::MX::vertsplit().

◆ repmat() [1/2]

template<typename MatType >
static MatType casadi::SparsityInterface< MatType >::repmat ( const MatType &  A,
const std::pair< casadi_int, casadi_int > &  rc 
)
inlinestatic

Definition at line 78 of file sparsity_interface.hpp.

78  {
79  return MatType::repmat(A, rc.first, rc.second);
80  }

◆ repmat() [2/2]

template<typename MatType >
MatType casadi::SparsityInterface< MatType >::repmat ( const MatType &  x,
casadi_int  n,
casadi_int  m = 1 
)
static

Definition at line 2517 of file sparsity_interface.hpp.

2517  {
2518  casadi_assert(n>=0 && m>=0,
2519  "repmat(x,n,m): n (" + str(n) + ") and m (" + str(m) + ") must be non-negative");
2520  if (n==1 && m==1) return x;
2521  MatType allrows = vertcat(std::vector<MatType>(n, x));
2522  if (n==0) allrows = MatType(0, x.size2());
2523  MatType ret = horzcat(std::vector<MatType>(m, allrows));
2524  if (m==0) ret = MatType(allrows.size1(), 0);
2525  return ret;
2526  }
friend MatType vertcat(const std::vector< MatType > &v)
Concatenate a list of matrices vertically.
friend MatType horzcat(const std::vector< MatType > &v)
Concatenate a list of matrices horizontally.

References casadi::str().

Referenced by casadi::Blocksqp::feasibilityRestorationPhase(), and casadi::MXNode::get_repmat().

◆ self() [1/2]

template<typename MatType >
MatType& casadi::SparsityInterface< MatType >::self ( )
inlineprotected

Definition at line 55 of file sparsity_interface.hpp.

55 { return static_cast<MatType&>(*this); }

◆ self() [2/2]

template<typename MatType >
const MatType& casadi::SparsityInterface< MatType >::self ( ) const
inlineprotected

Definition at line 54 of file sparsity_interface.hpp.

54 { return static_cast<const MatType&>(*this); }

◆ vec()

template<typename MatType >
MatType casadi::SparsityInterface< MatType >::vec ( const MatType &  x)
static

Definition at line 2511 of file sparsity_interface.hpp.

2511  {
2512  if (x.size2()==1) return x;
2513  return reshape(x, x.numel(), 1);
2514  }
friend MatType reshape(const MatType &x, casadi_int nrow, casadi_int ncol)
Returns a reshaped version of the matrix.

◆ veccat()

template<typename MatType >
MatType casadi::SparsityInterface< MatType >::veccat ( const std::vector< MatType > &  x)
static

Definition at line 2566 of file sparsity_interface.hpp.

2566  {
2567  std::vector< MatType > x_vec = x;
2568  for (typename std::vector< MatType >::iterator it=x_vec.begin();
2569  it!=x_vec.end(); ++it) {
2570  *it = vec(*it);
2571  }
2572  if (x_vec.empty()) {
2573  return MatType(0, 1);
2574  } else {
2575  return vertcat(x_vec);
2576  }
2577  }
static MatType vec(const MatType &x)

Referenced by casadi::MX::conditional(), casadi::MX::if_else(), casadi::MX::matrix_expand(), casadi::MX::stop_diff(), and casadi::MX::transform().

◆ vertsplit()

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::vertsplit ( const MatType &  x,
casadi_int  incr 
)
static

Definition at line 2632 of file sparsity_interface.hpp.

2632  {
2633  casadi_assert(incr>=1, "vertsplit(x,incr): incr (" + str(incr) + ") must be positive");
2634  casadi_int sz1 = x.size1();
2635  std::vector<casadi_int> offset1 = range(0, sz1, incr);
2636  offset1.push_back(sz1);
2637  return MatType::vertsplit(x, offset1);
2638  }

References casadi::range(), and casadi::str().

◆ vertsplit_n()

template<typename MatType >
std::vector< MatType > casadi::SparsityInterface< MatType >::vertsplit_n ( const MatType &  x,
casadi_int  n 
)
static

Definition at line 2648 of file sparsity_interface.hpp.

2648  {
2649  casadi_assert(n>=0, "vertsplit_n(x,n): n (" + str(n) + ") must be non-negative");
2650  if (x.size1()==0) return std::vector<MatType>(n, x);
2651  casadi_assert(n>0 && x.size1() % n==0, "vertsplit_n(x,n): x.size1() (" + str(x.size1()) +
2652  ") must be a multiple of n (" + str(n) + ")");
2653  return vertsplit(x, x.size1()/n);
2654  }
static std::vector< MatType > vertsplit(const MatType &x, casadi_int incr)

References casadi::str().


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