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)
 
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)
 Matrix product of two matrices. More...
 
MatType mtimes (const std::vector< MatType > &args)
 Matrix product of n matrices. More...
 
MatType mac (const MatType &x, const MatType &y, const MatType &z)
 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 645 of file sparsity_interface.hpp.

646  {
647  casadi_assert_dev(horz_incr>=1);
648  casadi_assert_dev(vert_incr>=1);
649  casadi_int sz1 = x.size1();
650  std::vector<casadi_int> offset1 = range(0, sz1, vert_incr);
651  offset1.push_back(sz1);
652  casadi_int sz2 = x.size2();
653  std::vector<casadi_int> offset2 = range(0, sz2, horz_incr);
654  offset2.push_back(sz2);
655  return blocksplit(x, offset1, offset2);
656  }
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 634 of file sparsity_interface.hpp.

636  {
637  std::vector<MatType> rows = MatType::vertsplit(x, vert_offset);
638  std::vector< std::vector< MatType > > ret;
639  for (auto&& r : rows) ret.push_back(MatType::horzsplit(r, horz_offset));
640  return ret;
641  }

◆ diagsplit() [1/3]

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

Definition at line 694 of file sparsity_interface.hpp.

694  {
695  casadi_assert_dev(incr>=1);
696  casadi_assert(x.is_square(), "diagsplit(x,incr)::input must be square but got "
697  + x.dim() + ".");
698  std::vector<casadi_int> offset2 = range(0, x.size2(), incr);
699  offset2.push_back(x.size2());
700  return MatType::diagsplit(x, offset2);
701  }

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

705  {
706  casadi_assert_dev(incr1>=1);
707  casadi_assert_dev(incr2>=1);
708  std::vector<casadi_int> offset1 = range(0, x.size1(), incr1);
709  offset1.push_back(x.size1());
710  std::vector<casadi_int> offset2 = range(0, x.size2(), incr2);
711  offset2.push_back(x.size2());
712  return MatType::diagsplit(x, offset1, offset2);
713  }

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

686  {
687  casadi_assert(x.is_square(), "diagsplit(x,incr)::input must be square but got "
688  + x.dim() + ".");
689  return MatType::diagsplit(x, output_offset, output_offset);
690  }

◆ horzsplit()

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

Definition at line 726 of file sparsity_interface.hpp.

726  {
727  casadi_assert_dev(incr>=1);
728  casadi_int sz2 = x.size2();
729  std::vector<casadi_int> offset2 = range(0, sz2, incr);
730  offset2.push_back(sz2);
731  return MatType::horzsplit(x, offset2);
732  }

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

743  {
744  casadi_assert(n>=0, "horzsplit_n(x,n): n (" + str(n) + ") must be non-negative");
745  if (x.size2()==0) return std::vector<MatType>(n, x);
746  casadi_assert(x.size2() % n==0, "horzsplit_n(x,n): x.size2() (" + str(x.size2()) +
747  ") must be a multiple of n (" + str(n) + ")");
748  return horzsplit(x, x.size2()/n);
749  }
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)
static

Definition at line 716 of file sparsity_interface.hpp.

716  {
717  casadi_assert(!args.empty(),
718  "mul(std::vector<MatType> &args): "
719  "supplied list must not be empty.");
720  MatType ret = args[0];
721  for (casadi_int i=1; i<args.size(); ++i) ret = MatType::mtimes(ret, args[i]);
722  return ret;
723  }

◆ offset()

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

Definition at line 660 of file sparsity_interface.hpp.

660  {
661  std::vector<casadi_int> ret(v.size()+1);
662  ret[0]=0;
663  for (casadi_int i=0; i<v.size(); ++i) {
664  ret[i+1] = ret[i] + (vert ? v[i].size1() : v[i].size2());
665  }
666  return ret;
667  }

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

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

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

623  {
624  if (n==1 && m==1) return x;
625  MatType allrows = vertcat(std::vector<MatType>(n, x));
626  if (n==0) allrows = MatType(0, x.size2());
627  MatType ret = horzcat(std::vector<MatType>(m, allrows));
628  if (m==0) ret = MatType(allrows.size1(), 0);
629  return ret;
630  }
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 617 of file sparsity_interface.hpp.

617  {
618  if (x.size2()==1) return x;
619  return reshape(x, x.numel(), 1);
620  }
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 670 of file sparsity_interface.hpp.

670  {
671  std::vector< MatType > x_vec = x;
672  for (typename std::vector< MatType >::iterator it=x_vec.begin();
673  it!=x_vec.end(); ++it) {
674  *it = vec(*it);
675  }
676  if (x_vec.empty()) {
677  return MatType(0, 1);
678  } else {
679  return vertcat(x_vec);
680  }
681  }
static MatType vec(const MatType &x)

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

◆ vertsplit()

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

Definition at line 735 of file sparsity_interface.hpp.

735  {
736  casadi_assert_dev(incr>=1);
737  casadi_int sz1 = x.size1();
738  std::vector<casadi_int> offset1 = range(0, sz1, incr);
739  offset1.push_back(sz1);
740  return MatType::vertsplit(x, offset1);
741  }

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

751  {
752  casadi_assert(n>=0, "vertsplit_n(x,n): n (" + str(n) + ") must be non-negative");
753  if (x.size1()==0) return std::vector<MatType>(n, x);
754  casadi_assert(x.size1() % n==0, "vertsplit(x,n): x.size1() (" + str(x.size1()) +
755  ") must be a multiple of n (" + str(n) + ")");
756  return vertsplit(x, x.size1()/n);
757  }
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: