26 #ifndef CASADI_MISC_HPP
27 #define CASADI_MISC_HPP
29 #include "exception.hpp"
30 #include "casadi_common.hpp"
44 class scoped_checkout {
46 scoped_checkout(
const T& proto) : proto_(proto) {
47 mem = proto_.checkout();
50 scoped_checkout(scoped_checkout&& that) : mem(that.mem), proto_(that.proto_) {
54 scoped_checkout(
const scoped_checkout& that) =
delete;
57 if (mem!=-1) proto_.release(mem);
60 operator casadi_int()
const {
80 CASADI_EXPORT std::vector<casadi_int> range(casadi_int start, casadi_int stop, casadi_int step=1,
81 casadi_int len=std::numeric_limits<casadi_int>::max());
86 CASADI_EXPORT
bool is_range(
const std::vector<casadi_int>& v,
87 casadi_int start, casadi_int stop, casadi_int step=1);
89 CASADI_EXPORT std::string join(
const std::vector<std::string>& l,
const std::string& delim=
",");
92 CASADI_EXPORT
bool startswith(
const std::string& s,
const std::string& p);
95 CASADI_EXPORT std::string replace(
const std::string& s,
96 const std::string& p,
const std::string& r);
105 CASADI_EXPORT std::vector<casadi_int> range(casadi_int stop);
108 CASADI_EXPORT
bool all(
const std::vector<bool> &v);
110 CASADI_EXPORT
bool any(
const std::vector<bool> &v);
112 CASADI_EXPORT std::vector<bool> boolvec_not(
const std::vector<bool> &v);
114 CASADI_EXPORT std::vector<bool> boolvec_and(
const std::vector<bool> &lhs,
115 const std::vector<bool> &rhs);
117 CASADI_EXPORT std::vector<bool> boolvec_or(
const std::vector<bool> &lhs,
118 const std::vector<bool> &rhs);
120 CASADI_EXPORT std::vector<casadi_int> boolvec_to_index(
const std::vector<bool> &v);
122 CASADI_EXPORT
bool is_equally_spaced(
const std::vector<double> &v);
125 CASADI_EXPORT std::vector<casadi_int> tensor_permute_mapping(
const std::vector<casadi_int>& dims,
126 const std::vector<casadi_int>& order);
128 CASADI_EXPORT
int to_int(casadi_int rhs);
129 CASADI_EXPORT std::vector<int> to_int(
const std::vector<casadi_int>& rhs);
130 CASADI_EXPORT std::vector< std::vector<int> > to_int(
131 const std::vector< std::vector<casadi_int> >& rhs);
133 template<
typename T,
typename S>
134 std::vector<T> vector_static_cast(
const std::vector<S>& rhs);
136 CASADI_EXPORT std::string str_bvec(bvec_t v);
145 std::vector<T> vector_slice(
const std::vector<T> &v,
const std::vector<casadi_int> &i);
154 std::vector<T> vector_select(
const std::vector<T> &v,
const std::vector<bool> &s,
161 std::vector<T> vector_tail(
const std::vector<T> &v);
167 std::vector<T> vector_init(
const std::vector<T> &v);
173 std::vector<T> reverse(
const std::vector<T> &v);
179 std::vector<T> join(
const std::vector<T> &a,
const std::vector<T> &b);
185 std::vector<T> join(
const std::vector<T> &a,
const std::vector<T> &b,
const std::vector<T> &c);
190 bool is_permutation(
const std::vector<casadi_int> &order);
196 std::vector<T> permute(
const std::vector<T> &a,
const std::vector<casadi_int> &order);
201 std::vector<casadi_int> invert_permutation(
const std::vector<casadi_int> &a);
207 std::vector<casadi_int> find(
const std::vector<T> &v);
213 bool in_range(
const std::vector<T> &v, casadi_int upper);
217 bool in_range(
const std::vector<T> &v, casadi_int lower, casadi_int upper);
220 #define casadi_assert_in_range(v, lower, upper) \
221 casadi_assert(in_range(v, lower, upper), \
222 "Out of bounds error. Got elements in range [" \
223 + str(*std::min_element(v.begin(), v.end())) + ","\
224 + str(*std::max_element(v.begin(), v.end())) + "], which is outside the range ["\
225 + str(lower) + "," + str(upper) + ").")
228 #define casadi_assert_bounded(v, upper) \
229 casadi_assert(in_range(v, upper), \
230 "Out of bounds error. Got elements in range [" \
231 + str(*std::min_element(v.begin(), v.end())) + ","\
232 + str(*std::max_element(v.begin(), v.end())) + "], which exceeds the upper bound "\
242 CASADI_EXPORT std::vector<casadi_int>
complement(
const std::vector<casadi_int> &v,
253 CASADI_EXPORT std::vector<casadi_int>
lookupvector(
const std::vector<casadi_int> &v,
255 CASADI_EXPORT std::vector<casadi_int>
lookupvector(
const std::vector<casadi_int> &v);
262 template<
class T,
class S>
264 std::vector<S>& flat);
271 template<
class T,
class S,
class I>
273 std::vector<S>& flat,
274 std::vector<I>& indices);
282 std::vector<T> applymap(T (*f)(
const T&),
const std::vector<T>& comp);
288 void applymap(
void (*f)(T&), std::vector<T>& comp);
327 void write_matlab(std::ostream &stream,
const std::vector<std::vector<T> > &v);
335 void read_matlab(std::ifstream &file, std::vector<std::vector<T> > &v);
339 template<
typename T,
typename F,
typename L>
340 void linspace(std::vector<T> &v,
const F& first,
const L& last);
344 CASADI_EXPORT bvec_t* get_bvec_t(std::vector<double>& v);
347 CASADI_EXPORT
const bvec_t* get_bvec_t(
const std::vector<double>& v);
350 CASADI_EXPORT bvec_t bvec_or(
const bvec_t* arg, casadi_int n);
354 bvec_t* get_bvec_t(std::vector<T>& v);
358 const bvec_t* get_bvec_t(
const std::vector<T>& v);
362 T* get_ptr(std::vector<T> &v);
366 const T* get_ptr(
const std::vector<T> &v);
379 void sort(
const std::vector<T> &values, std::vector<T> &sorted_values,
380 std::vector<casadi_int> &indices,
bool invert_indices =
false);
386 T product(
const std::vector<T> &values);
392 T sum(
const std::vector<T> &values);
398 std::vector<T> cumsum(
const std::vector<T> &values);
404 std::vector<T> diff(
const std::vector<T> &values);
410 std::vector<T> cumsum0(
const std::vector<T> &values);
416 for (
auto&& vk : v) {
417 if (vk!=vk || vk==std::numeric_limits<T>::infinity() ||
418 vk==-std::numeric_limits<T>::infinity())
return false;
424 CASADI_EXPORT std::string
temporary_file(
const std::string& prefix,
const std::string& suffix);
430 if (val==std::numeric_limits<double>::infinity()) {
432 }
else if (val==-std::numeric_limits<double>::infinity()) {
434 }
else if (val!=val) {
441 std::streampos start = stream.tellg();
456 ret = std::numeric_limits<double>::quiet_NaN();
459 ret = std::numeric_limits<double>::quiet_NaN();
463 if (non_reg==
"inf") {
464 ret = std::numeric_limits<double>::infinity();
465 }
else if (non_reg==
"-inf") {
466 ret = -std::numeric_limits<double>::infinity();
467 }
else if (non_reg==
"nan") {
468 ret = std::numeric_limits<double>::quiet_NaN();
470 ret = std::numeric_limits<double>::quiet_NaN();
485 ostream& operator<<(ostream& stream,
const vector<T>& v) {
486 stream << casadi::str(v);
492 ostream& operator<<(ostream& stream,
const set<T>& v) {
493 stream << casadi::str(v);
497 template<
typename T1,
typename T2>
498 ostream& operator<<(ostream& stream,
const pair<T1, T2>& p) {
499 stream << casadi::str(p);
503 template<
typename T1,
typename T2>
504 ostream& operator<<(ostream& stream,
const std::map<T1, T2>& p) {
505 stream << casadi::str(p);
509 template<
typename T2>
510 ostream& operator<<(ostream& stream,
const std::map<std::string, T2>& p) {
511 stream << casadi::str(p);
516 bool mul_overflows(
const T& a,
const T& b) {
517 if (a==0 || b==0)
return false;
518 return abs(std::numeric_limits<T>::max()/a) < abs(b);
526 template<
typename T,
typename S>
527 std::vector<T> vector_static_cast(
const std::vector<S>& rhs) {
529 ret.reserve(rhs.size());
530 for (
auto e : rhs) ret.push_back(
static_cast<T
>(e));
535 std::vector<T> vector_slice(
const std::vector<T> &v,
const std::vector<casadi_int> &i) {
537 ret.reserve(i.size());
538 for (casadi_int k=0;k<i.size();++k) {
541 "vector_slice: Indices should be larger than zero."
542 "You have " + str(j) +
" at location " + str(k) +
".");
543 casadi_assert(j<v.size(),
544 "vector_slice: Indices should be larger than zero."
545 "You have " + str(j) +
" at location " + str(k) +
".");
552 std::vector<T> vector_select(
const std::vector<T> &v,
const std::vector<bool> &s,
bool invert) {
554 casadi_assert(v.size()==s.size(),
"Dimension mismatch.");
556 for (casadi_int k=0;k<s.size();++k) {
557 if (!s[k]) ret.push_back(v[k]);
560 for (casadi_int k=0;k<s.size();++k) {
561 if (s[k]) ret.push_back(v[k]);
568 std::vector<T> vector_tail(
const std::vector<T> &v) {
570 ret.insert(ret.begin(), v.begin()+1, v.end());
575 std::vector<T> vector_init(
const std::vector<T> &v) {
577 ret.insert(ret.begin(), v.begin(), v.begin()+v.size()-1);
582 std::vector<T> reverse(
const std::vector<T> &v) {
583 std::vector<T> ret(v.size());
584 std::reverse_copy(v.begin(), v.end(), ret.begin());
589 std::vector<T> join(
const std::vector<T> &a,
const std::vector<T> &b) {
590 std::vector<T> ret = a;
591 ret.insert(ret.end(), b.begin(), b.end());
596 std::vector<T> join(
const std::vector<T> &a,
const std::vector<T> &b,
const std::vector<T> &c) {
597 std::vector<T> ret = a;
598 ret.insert(ret.end(), b.begin(), b.end());
599 ret.insert(ret.end(), c.begin(), c.end());
604 std::vector<T> permute(
const std::vector<T> &a,
const std::vector<casadi_int> &order) {
605 casadi_assert_dev(order.size()==a.size());
606 casadi_assert_dev(is_permutation(order));
607 return vector_slice(a, order);
611 std::vector<casadi_int> find(
const std::vector<T> &v) {
612 std::vector<casadi_int> ret;
613 for (casadi_int i=0;i<v.size();++i) {
614 if (v[i]) ret.push_back(i);
621 std::vector<T> applymap(T (*f)(
const T&) ,
const std::vector<T>& comp) {
622 std::vector<T> ret(comp.size());
623 std::transform(comp.begin(), comp.end(), ret.begin(), f);
628 void applymap(
void (*f)(T &), std::vector<T>& comp) {
629 std::for_each(comp.begin(), comp.end(), f);
632 template<
class S,
class D>
633 void copy_vector(
const std::vector<S>& s, std::vector<D>& d) {
634 casadi_assert(s.size()==d.size(),
"Dimension mismatch.");
635 std::copy(s.begin(), s.end(), d.begin());
638 template<
class S,
class D>
639 void assign_vector(
const std::vector<S>& s, std::vector<D>& d) {
640 casadi_assert(d.empty(),
"Receiving vector must be empty");
642 std::copy(s.begin(), s.end(), d.begin());
645 template<
class S,
class D>
646 void copy_vector(
const S* s, std::vector<D>& d) {
647 for (casadi_int i=0;i<d.size();++i) {
648 d[i] =
static_cast<D
>(s[i]);
652 template<
class S,
class D>
653 void init_vector(std::vector<S>& d,
const std::vector<D>& s) {
655 std::copy(s.begin(), s.end(), d.begin());
660 bool in_range(
const std::vector<T> &v, casadi_int upper) {
665 bool in_range(
const std::vector<T> &v, casadi_int lower, casadi_int upper) {
666 if (v.empty())
return true;
667 casadi_int max = *std::max_element(v.begin(), v.end());
668 if (max >= upper)
return false;
669 casadi_int min = *std::min_element(v.begin(), v.end());
670 return (min >= lower);
673 template<
class T,
class S>
675 std::vector<S>& flat) {
678 for (
const auto& e : nested) {
685 for (
const auto& e : nested) {
686 flat.insert(flat.end(), e.begin(), e.end());
690 template<
class T,
class S,
class I>
692 std::vector<S>& flat,
693 std::vector<I>& indices) {
698 casadi_int N = nested.size();
699 indices.resize(1, 0);
700 indices.reserve(N+1);
701 casadi_int offset = 0;
702 for (
const auto& e : nested) {
704 indices.push_back(offset);
709 bool isUnique(
const std::vector<T> &v) {
710 std::set<T> s(v.begin(), v.end());
711 return v.size()==s.size();
716 if (v.empty())
return true;
718 for (casadi_int i=1;i<v.size();++i) {
719 if (!(v[i] > el))
return false;
727 if (v.empty())
return true;
729 for (casadi_int i=1;i<v.size();++i) {
730 if (!(v[i] < el))
return false;
738 if (v.empty())
return true;
740 for (casadi_int i=1;i<v.size();++i) {
741 if (!(v[i] <= el))
return false;
749 if (v.empty())
return true;
751 for (casadi_int i=1;i<v.size();++i) {
752 if (!(v[i] >= el))
return false;
770 for (std::size_t i=0; i<v.size(); ++i) {
771 if (v[i]<0)
return true;
777 void write_matlab(std::ostream &stream,
const std::vector<T> &v) {
778 std::copy(v.begin(), v.end(), std::ostream_iterator<T>(stream,
" "));
782 void write_matlab(std::ostream &stream,
const std::vector<std::vector<T> > &v) {
783 for (casadi_uint i=0; i<v.size(); ++i) {
784 std::copy(v[i].begin(), v[i].end(), std::ostream_iterator<T>(stream,
" "));
790 void read_matlab(std::istream &stream, std::vector<T> &v) {
793 while (!stream.eof()) {
801 val = std::numeric_limits<T>::infinity();
810 void read_matlab(std::ifstream &file, std::vector<std::vector<T> > &v) {
813 while (!getline(file, line,
'\n').eof()) {
814 std::istringstream reader(line);
815 std::vector<T> lineData;
817 while (!reader.eof()) {
825 val = std::numeric_limits<T>::infinity();
829 lineData.push_back(val);
831 v.push_back(lineData);
835 template<
typename T,
typename F,
typename L>
836 void linspace(std::vector<T> &v,
const F& first,
const L& last) {
838 throw CasadiException(
"std::linspace: vector must contain at least two elements");
841 T increment = (last-first)/T(v.size()-1);
844 for (
unsigned i=1; i<v.size()-1; ++i)
845 v[i] = v[i-1] + increment;
846 v[v.size()-1] = last;
850 T* get_ptr(std::vector<T> &v) {
858 const T* get_ptr(
const std::vector<T> &v) {
868 const std::vector<T> &v_;
869 sortCompare(
const std::vector<T> &v) : v_(v) {}
870 bool operator() (casadi_int i, casadi_int j)
const {
return v_[i]<v_[j];}
874 void sort(
const std::vector<T> &values, std::vector<T> &sorted_values,
875 std::vector<casadi_int> &indices,
bool invert_indices) {
877 if (invert_indices) {
878 std::vector<casadi_int> inverted;
879 sort(values, sorted_values, inverted,
false);
880 indices.resize(inverted.size());
881 for (
size_t i=0; i<inverted.size(); ++i) {
882 indices[inverted[i]] = i;
888 indices.resize(values.size());
889 for (
size_t i=0; i<indices.size(); ++i) indices[i] = i;
892 std::sort(indices.begin(), indices.end(), sortCompare<T>(values));
895 sorted_values.resize(values.size());
896 for (
size_t i=0; i<values.size(); ++i) {
897 sorted_values[i] = values[indices[i]];
902 T product(
const std::vector<T> &values) {
904 for (casadi_int i=0;i<values.size();++i) r*=values[i];
909 T sum(
const std::vector<T> &values) {
911 for (casadi_int i=0;i<values.size();++i) r+=values[i];
916 std::vector<T> cumsum(
const std::vector<T> &values) {
917 std::vector<T> ret(values.size());
919 for (casadi_int i=0;i<values.size();++i) {
927 std::vector<T> cumsum0(
const std::vector<T> &values) {
928 std::vector<T> ret(values.size()+1, 0);
930 for (casadi_int i=0;i<values.size();++i) {
938 std::vector<T> diff(
const std::vector<T> &values) {
939 casadi_assert(!values.empty(),
"Array must be non-empty");
940 std::vector<T> ret(values.size()-1);
941 for (casadi_int i=0;i<values.size()-1;++i) {
942 ret[i] = values[i+1]-values[i];
948 T dot(
const std::vector<T>& a,
const std::vector<T>& b) {
950 for (casadi_int k=0; k<a.size(); ++k) {
957 T norm_inf(
const std::vector<T>& x) {
959 for (casadi_int k=0; k<x.size(); ++k) {
960 ret = fmax(ret, fabs(x[k]));
966 T norm_1(
const std::vector<T>& x) {
968 for (casadi_int k=0; k<x.size(); ++k) {
975 T norm_2(
const std::vector<T>& x) {
977 for (casadi_int k=0; k<x.size(); ++k) {
984 bvec_t* get_bvec_t(std::vector<T>& v) {
985 casadi_assert(0,
"get_bvec_t only supported for double");
989 const bvec_t* get_bvec_t(
const std::vector<T>& v) {
990 casadi_assert(0,
"get_bvec_t only supported for double");
995 typedef std::vector<std::string> StringVector;
bool is_decreasing(const std::vector< T > &v)
Check if the vector is strictly decreasing.
CASADI_EXPORT std::string temporary_file(const std::string &prefix, const std::string &suffix)
void flatten_nested_vector(const std::vector< std::vector< T > > &nested, std::vector< S > &flat)
Flatten a nested std::vector tot a single flattened vector.
bool has_negative(const std::vector< T > &v)
Check if the vector has negative entries.
bool is_increasing(const std::vector< T > &v)
Check if the vector is strictly increasing.
bool is_monotone(const std::vector< T > &v)
Check if the vector is monotone.
void write_matlab(std::ostream &stream, const std::vector< T > &v)
Print vector, matlab style.
CASADI_EXPORT void normalized_setup(std::istream &stream)
void read_matlab(std::istream &stream, std::vector< T > &v)
Read vector, matlab style.
bool is_strictly_monotone(const std::vector< T > &v)
Check if the vector is strictly monotone.
CASADI_EXPORT std::vector< casadi_int > complement(const std::vector< casadi_int > &v, casadi_int size)
Returns the list of all i in [0, size[ not found in supplied list.
bool in_range(const std::vector< T > &v, casadi_int upper)
Check if for each element of v holds: v_i < upper.
bool is_nonincreasing(const std::vector< T > &v)
Check if the vector is non-increasing.
bool is_regular(const std::vector< T > &v)
Checks if array does not contain NaN or Inf.
bool is_nondecreasing(const std::vector< T > &v)
Check if the vector is non-decreasing.
int normalized_in(std::istream &stream, double &ret)
CASADI_EXPORT std::vector< casadi_int > lookupvector(const std::vector< casadi_int > &v, casadi_int size)
Returns a vector for quickly looking up entries of supplied list.
void normalized_out(std::ostream &stream, double val)