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 horizontally, retaining groups of columns More...
 
std::vector< MatType > horzsplit (const MatType &x, casadi_int incr=1)
 split horizontally, retaining fixed-sized groups of columns More...
 
std::vector< MatType > horzsplit_n (const MatType &x, casadi_int n)
 split horizontally, retaining fixed-sized groups of columns More...
 
std::vector< MatType > vertsplit (const MatType &x, const std::vector< casadi_int > &offset)
 split vertically, retaining groups of rows More...
 
std::vector< casadi_int > offset (const std::vector< MatType > &v, bool vert=true)
 Helper function, get offsets corresponding to a vector of matrices. More...
 
std::vector< MatType > vertsplit (const MatType &x, casadi_int incr=1)
 split vertically, retaining fixed-sized groups of rows More...
 
std::vector< MatType > vertsplit_n (const MatType &x, casadi_int n)
 split vertically, retaining fixed-sized groups of rows More...
 
MatType blockcat (const std::vector< std::vector< MatType > > &v)
 Construct a matrix from a list of list 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)
 chop up into blocks More...
 
std::vector< std::vector< MatType > > blocksplit (const MatType &x, casadi_int vert_incr=1, casadi_int horz_incr=1)
 chop up into blocks More...
 
MatType diagcat (const std::vector< MatType > &A)
 Construct a matrix with given block on the diagonal. More...
 
std::vector< MatType > diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset1, const std::vector< casadi_int > &output_offset2)
 split diagonally, retaining square matrices More...
 
std::vector< MatType > diagsplit (const MatType &x, const std::vector< casadi_int > &output_offset)
 split diagonally, retaining square matrices More...
 
std::vector< MatType > diagsplit (const MatType &x, casadi_int incr=1)
 split diagonally, retaining groups of square matrices More...
 
std::vector< MatType > diagsplit (const MatType &x, casadi_int incr1, casadi_int incr2)
 split diagonally, retaining fixed-sized matrices More...
 
MatType veccat (const std::vector< MatType > &x)
 concatenate vertically while vectorizing all arguments with vec 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)
 make a 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. 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)
 Return a row-wise summation of elements. More...
 
MatType sum2 (const MatType &x)
 Return a column-wise summation of elements. More...
 
MatType sum (const MatType &x)
 Returns summation of 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 669 of file sparsity_interface.hpp.

670  {
671  casadi_assert_dev(horz_incr>=1);
672  casadi_assert_dev(vert_incr>=1);
673  casadi_int sz1 = x.size1();
674  std::vector<casadi_int> offset1 = range(0, sz1, vert_incr);
675  offset1.push_back(sz1);
676  casadi_int sz2 = x.size2();
677  std::vector<casadi_int> offset2 = range(0, sz2, horz_incr);
678  offset2.push_back(sz2);
679  return blocksplit(x, offset1, offset2);
680  }
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.

References casadi::range().

◆ 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 658 of file sparsity_interface.hpp.

660  {
661  std::vector<MatType> rows = MatType::vertsplit(x, vert_offset);
662  std::vector< std::vector< MatType > > ret;
663  for (auto&& r : rows) ret.push_back(MatType::horzsplit(r, horz_offset));
664  return ret;
665  }

◆ diagsplit() [1/3]

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

Definition at line 718 of file sparsity_interface.hpp.

718  {
719  casadi_assert_dev(incr>=1);
720  casadi_assert(x.is_square(), "diagsplit(x,incr)::input must be square but got "
721  + x.dim() + ".");
722  std::vector<casadi_int> offset2 = range(0, x.size2(), incr);
723  offset2.push_back(x.size2());
724  return MatType::diagsplit(x, offset2);
725  }

References casadi::range().

◆ 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 729 of file sparsity_interface.hpp.

729  {
730  casadi_assert_dev(incr1>=1);
731  casadi_assert_dev(incr2>=1);
732  std::vector<casadi_int> offset1 = range(0, x.size1(), incr1);
733  offset1.push_back(x.size1());
734  std::vector<casadi_int> offset2 = range(0, x.size2(), incr2);
735  offset2.push_back(x.size2());
736  return MatType::diagsplit(x, offset1, offset2);
737  }

References casadi::range().

◆ 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 709 of file sparsity_interface.hpp.

710  {
711  casadi_assert(x.is_square(), "diagsplit(x,incr)::input must be square but got "
712  + x.dim() + ".");
713  return MatType::diagsplit(x, output_offset, output_offset);
714  }

◆ horzsplit()

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

Definition at line 751 of file sparsity_interface.hpp.

751  {
752  casadi_assert_dev(incr>=1);
753  casadi_int sz2 = x.size2();
754  std::vector<casadi_int> offset2 = range(0, sz2, incr);
755  offset2.push_back(sz2);
756  return MatType::horzsplit(x, offset2);
757  }

References casadi::range().

◆ horzsplit_n()

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

Definition at line 768 of file sparsity_interface.hpp.

768  {
769  casadi_assert(n>=0, "horzsplit_n(x,n): n (" + str(n) + ") must be non-negative");
770  if (x.size2()==0) return std::vector<MatType>(n, x);
771  casadi_assert(x.size2() % n==0, "horzsplit_n(x,n): x.size2() (" + str(x.size2()) +
772  ") must be a multiple of n (" + str(n) + ")");
773  return horzsplit(x, x.size2()/n);
774  }
static std::vector< MatType > horzsplit(const MatType &x, casadi_int incr)
std::string str(const T &v)
String representation, any type.

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 740 of file sparsity_interface.hpp.

741  {
742  casadi_assert(!args.empty(),
743  "mul(std::vector<MatType> &args): "
744  "supplied list must not be empty.");
745  MatType ret = args[0];
746  for (casadi_int i=1; i<args.size(); ++i) ret = MatType::mtimes(ret, args[i], blas);
747  return ret;
748  }

◆ offset()

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

Definition at line 684 of file sparsity_interface.hpp.

684  {
685  std::vector<casadi_int> ret(v.size()+1);
686  ret[0]=0;
687  for (casadi_int i=0; i<v.size(); ++i) {
688  ret[i+1] = ret[i] + (vert ? v[i].size1() : v[i].size2());
689  }
690  return ret;
691  }

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 647 of file sparsity_interface.hpp.

647  {
648  if (n==1 && m==1) return x;
649  MatType allrows = vertcat(std::vector<MatType>(n, x));
650  if (n==0) allrows = MatType(0, x.size2());
651  MatType ret = horzcat(std::vector<MatType>(m, allrows));
652  if (m==0) ret = MatType(allrows.size1(), 0);
653  return ret;
654  }
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.

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 641 of file sparsity_interface.hpp.

641  {
642  if (x.size2()==1) return x;
643  return reshape(x, x.numel(), 1);
644  }
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 694 of file sparsity_interface.hpp.

694  {
695  std::vector< MatType > x_vec = x;
696  for (typename std::vector< MatType >::iterator it=x_vec.begin();
697  it!=x_vec.end(); ++it) {
698  *it = vec(*it);
699  }
700  if (x_vec.empty()) {
701  return MatType(0, 1);
702  } else {
703  return vertcat(x_vec);
704  }
705  }
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 760 of file sparsity_interface.hpp.

760  {
761  casadi_assert_dev(incr>=1);
762  casadi_int sz1 = x.size1();
763  std::vector<casadi_int> offset1 = range(0, sz1, incr);
764  offset1.push_back(sz1);
765  return MatType::vertsplit(x, offset1);
766  }

References casadi::range().

◆ vertsplit_n()

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

Definition at line 776 of file sparsity_interface.hpp.

776  {
777  casadi_assert(n>=0, "vertsplit_n(x,n): n (" + str(n) + ") must be non-negative");
778  if (x.size1()==0) return std::vector<MatType>(n, x);
779  casadi_assert(x.size1() % n==0, "vertsplit(x,n): x.size1() (" + str(x.size1()) +
780  ") must be a multiple of n (" + str(n) + ")");
781  return vertsplit(x, x.size1()/n);
782  }
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: