26 #ifndef CASADI_SPARSITY_INTERFACE_HPP
27 #define CASADI_SPARSITY_INTERFACE_HPP
29 #include "casadi_misc.hpp"
50 template<
typename MatType>
54 inline const MatType&
self()
const {
return static_cast<const MatType&
>(*this); }
55 inline MatType&
self() {
return static_cast<MatType&
>(*this); }
59 static std::vector< std::vector< MatType > >
60 blocksplit(
const MatType& x,
const std::vector<casadi_int>& vert_offset,
61 const std::vector<casadi_int>& horz_offset);
62 static std::vector< std::vector< MatType > >
63 blocksplit(
const MatType& x, casadi_int vert_incr, casadi_int horz_incr);
64 static MatType
veccat(
const std::vector< MatType >& x);
65 static MatType
vec(
const MatType& x);
66 static MatType
repmat(
const MatType& x, casadi_int n, casadi_int m=1);
67 static std::vector<casadi_int>
offset(
const std::vector< MatType > &v,
bool vert=
true);
68 static std::vector< MatType >
diagsplit(
const MatType& x,
69 const std::vector<casadi_int>& output_offset);
70 static std::vector< MatType >
diagsplit(
const MatType& x, casadi_int incr);
71 static std::vector< MatType >
diagsplit(
const MatType& x, casadi_int incr1, casadi_int incr2);
72 static MatType
mtimes(
const std::vector<MatType> &args,
73 const std::string& blas =
"reference");
74 static std::vector<MatType >
horzsplit(
const MatType& x, casadi_int incr);
75 static std::vector<MatType >
vertsplit(
const MatType& x, casadi_int incr);
76 static std::vector<MatType >
horzsplit_n(
const MatType& x, casadi_int n);
77 static std::vector<MatType >
vertsplit_n(
const MatType& x, casadi_int n);
78 static MatType
repmat(
const MatType &A,
const std::pair<casadi_int, casadi_int>& rc) {
79 return MatType::repmat(A, rc.first, rc.second);
171 inline friend MatType
horzcat(
const std::vector<MatType> &v) {
172 return MatType::horzcat(v);
249 inline friend MatType
vertcat(
const std::vector<MatType> &v) {
250 return MatType::vertcat(v);
327 inline friend std::vector<MatType >
329 return MatType::horzsplit(x,
offset);
408 inline friend std::vector<MatType >
horzsplit(
const MatType& x, casadi_int incr=1) {
409 return MatType::horzsplit(x, incr);
472 inline friend std::vector<MatType >
horzsplit_n(
const MatType& x, casadi_int n) {
473 return MatType::horzsplit_n(x, n);
536 inline friend std::vector<MatType >
538 return MatType::vertsplit(x,
offset);
583 inline friend std::vector<casadi_int >
offset(
const std::vector<MatType> &v,
bool vert=
true) {
584 return MatType::offset(v, vert);
663 inline friend std::vector<MatType >
vertsplit(
const MatType &x, casadi_int incr=1) {
664 return MatType::vertsplit(x, incr);
717 inline friend std::vector<MatType >
vertsplit_n(
const MatType& x, casadi_int n) {
718 return MatType::vertsplit_n(x, n);
778 inline friend MatType
blockcat(
const std::vector< std::vector<MatType > > &v) {
779 return MatType::blockcat(v);
813 inline friend MatType
814 blockcat(
const MatType &A,
const MatType &B,
const MatType &C,
const MatType &D) {
869 inline friend std::vector< std::vector< MatType > >
871 const std::vector<casadi_int>& vert_offset,
872 const std::vector<casadi_int>& horz_offset) {
873 return MatType::blocksplit(x, vert_offset, horz_offset);
938 inline friend std::vector< std::vector< MatType > >
939 blocksplit(
const MatType& x, casadi_int vert_incr=1, casadi_int horz_incr=1) {
940 return MatType::blocksplit(x, vert_incr, horz_incr);
1005 inline friend MatType
diagcat(
const std::vector<MatType> &A) {
1006 return MatType::diagcat(A);
1053 friend std::vector< MatType >
1055 const std::vector<casadi_int>& output_offset1,
1056 const std::vector<casadi_int>& output_offset2) {
1057 return MatType::diagsplit(x, output_offset1, output_offset2);
1116 inline friend std::vector< MatType >
1117 diagsplit(
const MatType& x,
const std::vector<casadi_int>& output_offset) {
1118 return MatType::diagsplit(x, output_offset);
1200 inline friend std::vector< MatType >
1202 return MatType::diagsplit(x, incr);
1241 inline friend std::vector< MatType >
1242 diagsplit(
const MatType& x, casadi_int incr1, casadi_int incr2) {
1243 return MatType::diagsplit(x, incr1, incr2);
1287 inline friend MatType
veccat(
const std::vector< MatType >& x) {
1288 return MatType::veccat(x);
1381 inline friend MatType
mtimes(
const MatType &x,
const MatType &y,
1382 const std::string& blas =
"reference") {
1383 return MatType::mtimes(x, y, blas);
1417 inline friend MatType
mtimes(
const std::vector<MatType> &args,
1418 const std::string& blas =
"reference") {
1419 return MatType::mtimes(args, blas);
1488 inline friend MatType
1489 mac(
const MatType &x,
const MatType &y,
const MatType &z,
1490 const std::string& blas =
"reference") {
1491 return MatType::mac(x, y, z, blas);
1588 inline friend MatType
vec(
const MatType& x) {
1589 return MatType::vec(x);
1669 inline friend MatType
reshape(
const MatType& x, casadi_int nrow, casadi_int ncol) {
1670 return MatType::reshape(x, nrow, ncol);
1704 inline friend MatType
reshape(
const MatType& x, std::pair<casadi_int, casadi_int> rc) {
1705 return MatType::reshape(x, rc.first, rc.second);
1752 return MatType::reshape(x, sp);
1816 return MatType::sparsity_cast(x, sp);
1869 inline friend casadi_int
sprank(
const MatType& x) {
1870 return MatType::sprank(x);
1921 inline friend casadi_int
norm_0_mul(
const MatType &x,
const MatType &y) {
1922 return MatType::norm_0_mul(x, y);
1982 inline friend MatType
triu(
const MatType& x,
bool includeDiagonal=
true) {
1983 return MatType::triu(x, includeDiagonal);
2036 inline friend MatType
tril(
const MatType& x,
bool includeDiagonal=
true) {
2037 return MatType::tril(x, includeDiagonal);
2096 inline friend MatType
kron(
const MatType& a,
const MatType& b) {
2097 return MatType::kron(a, b);
2166 inline friend MatType
repmat(
const MatType &A, casadi_int n, casadi_int m=1) {
2167 return MatType::repmat(A, n, m);
2196 inline friend MatType
repmat(
const MatType &A,
const std::pair<casadi_int, casadi_int>& rc) {
2197 return MatType::repmat(A, rc);
2205 inline friend MatType
horzcat(
const MatType &x,
const MatType &y) {
2206 return horzcat(std::vector<MatType>{x, y});
2214 inline friend MatType
horzcat(
const MatType &x,
const MatType &y,
const MatType &z) {
2215 return horzcat(std::vector<MatType>{x, y, z});
2223 inline friend MatType
horzcat(
const MatType &x,
const MatType &y,
const MatType &z,
2225 return horzcat(std::vector<MatType>{x, y, z, w});
2233 inline friend MatType
horzcat(
const MatType &x,
const MatType &y,
const MatType &z,
2234 const MatType &w,
const MatType &v) {
2235 return horzcat(std::vector<MatType>{x, y, z, w, v});
2243 inline friend MatType
horzcat(
const MatType &x,
const MatType &y,
const MatType &z,
2244 const MatType &w,
const MatType &v,
const MatType &u) {
2245 return horzcat(std::vector<MatType>{x, y, z, w, v, u});
2253 inline friend MatType
vertcat(
const MatType &x,
const MatType &y) {
2254 return vertcat(std::vector<MatType>{x, y});
2262 inline friend MatType
vertcat(
const MatType &x,
const MatType &y,
const MatType &z) {
2263 return vertcat(std::vector<MatType>{x, y, z});
2271 inline friend MatType
vertcat(
const MatType &x,
const MatType &y,
const MatType &z,
2273 return vertcat(std::vector<MatType>{x, y, z, w});
2281 inline friend MatType
vertcat(
const MatType &x,
const MatType &y,
const MatType &z,
2282 const MatType &w,
const MatType &v) {
2283 return vertcat(std::vector<MatType>{x, y, z, w, v});
2291 inline friend MatType
vertcat(
const MatType &x,
const MatType &y,
const MatType &z,
2292 const MatType &w,
const MatType &v,
const MatType &u) {
2293 return vertcat(std::vector<MatType>{x, y, z, w, v, u});
2301 inline friend MatType
diagcat(
const MatType &x,
const MatType &y) {
2302 return diagcat(std::vector<MatType>{x, y});
2310 inline friend MatType
diagcat(
const MatType &x,
const MatType &y,
const MatType &z) {
2311 return diagcat(std::vector<MatType>{x, y, z});
2319 inline friend MatType
diagcat(
const MatType &x,
const MatType &y,
const MatType &z,
2321 return diagcat(std::vector<MatType>{x, y, z, w});
2329 inline friend MatType
diagcat(
const MatType &x,
const MatType &y,
const MatType &z,
2330 const MatType &w,
const MatType &v) {
2331 return diagcat(std::vector<MatType>{x, y, z, w, v});
2339 inline friend MatType
diagcat(
const MatType &x,
const MatType &y,
const MatType &z,
2340 const MatType &w,
const MatType &v,
const MatType &u) {
2341 return diagcat(std::vector<MatType>{x, y, z, w, v, u});
2394 inline friend MatType
sum1(
const MatType &x) {
return MatType::sum1(x);}
2439 inline friend MatType
sum2(
const MatType &x) {
return MatType::sum2(x);}
2490 inline friend MatType
sum(
const MatType &x) {
2491 if (x.is_vector()) {
2492 if (x.is_column()) {
2493 return MatType::sum1(x);
2495 return MatType::sum2(x);
2498 if (x.size1()>x.size2()) {
2499 return MatType::sum2(MatType::sum1(x));
2501 return MatType::sum1(MatType::sum2(x));
2510 template<
typename MatType>
2512 if (x.size2()==1)
return x;
2513 return reshape(x, x.numel(), 1);
2516 template<
typename MatType>
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);
2528 template<
typename MatType>
2529 std::vector< std::vector< MatType > >
2531 const std::vector<casadi_int>& vert_offset,
2532 const std::vector<casadi_int>& horz_offset) {
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));
2539 template<
typename MatType>
2540 std::vector< std::vector< MatType > >
2542 casadi_int vert_incr, casadi_int horz_incr) {
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);
2554 template<
typename MatType>
2555 std::vector<casadi_int>
2557 std::vector<casadi_int> ret(v.size()+1);
2559 for (casadi_int i=0; i<v.size(); ++i) {
2560 ret[i+1] = ret[i] + (vert ? v[i].size1() : v[i].size2());
2565 template<
typename MatType>
2567 std::vector< MatType > x_vec = x;
2568 for (
typename std::vector< MatType >::iterator it=x_vec.begin();
2569 it!=x_vec.end(); ++it) {
2572 if (x_vec.empty()) {
2573 return MatType(0, 1);
2575 return vertcat(x_vec);
2579 template<
typename MatType>
2580 std::vector< MatType >
2582 const std::vector<casadi_int>& output_offset) {
2583 casadi_assert(x.is_square(),
"diagsplit(x,incr)::input must be square but got "
2585 return MatType::diagsplit(x, output_offset, output_offset);
2588 template<
typename MatType>
2589 std::vector< MatType >
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 "
2594 std::vector<casadi_int> offset2 =
range(0, x.size2(), incr);
2595 offset2.push_back(x.size2());
2596 return MatType::diagsplit(x, offset2);
2599 template<
typename MatType>
2600 std::vector< MatType >
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);
2611 template<
typename MatType>
2613 const std::string& blas) {
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);
2622 template<
typename MatType>
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);
2631 template<
typename MatType>
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);
2639 template<
typename MatType>
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);
2647 template<
typename MatType>
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);
Sparsity interface class.
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 veccat(const std::vector< MatType > &x)
static std::vector< casadi_int > offset(const std::vector< MatType > &v, bool vert=true)
static MatType repmat(const MatType &x, casadi_int n, casadi_int m=1)
static std::vector< MatType > diagsplit(const MatType &x, const std::vector< casadi_int > &output_offset)
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< MatType > diagsplit(const MatType &x, casadi_int incr1, casadi_int incr2)
static std::vector< MatType > horzsplit(const MatType &x, casadi_int incr)
static MatType vec(const MatType &x)
static std::vector< std::vector< MatType > > blocksplit(const MatType &x, casadi_int vert_incr, casadi_int horz_incr)
static std::vector< MatType > vertsplit(const MatType &x, casadi_int incr)
static MatType mtimes(const std::vector< MatType > &args, const std::string &blas="reference")
static std::vector< MatType > diagsplit(const MatType &x, casadi_int incr)
static MatType repmat(const MatType &A, const std::pair< casadi_int, casadi_int > &rc)
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.