26 #ifndef CASADI_MISC_HPP
27 #define CASADI_MISC_HPP
29 #include "exception.hpp"
30 #include "casadi_common.hpp"
47 mem = proto_.checkout();
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);
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>
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);
196 std::vector<T>
permute(
const std::vector<T> &a,
const std::vector<casadi_int> &order);
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);
323 void write_matlab(std::ostream &stream,
const std::vector<T> &v);
327 void write_matlab(std::ostream &stream,
const std::vector<std::vector<T> > &v);
331 void read_matlab(std::istream &stream, 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);
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();
491 template<
typename T,
size_t N>
504 template<
typename T1,
typename T2>
505 ostream&
operator<<(ostream& stream,
const pair<T1, T2>& p) {
510 template<
typename T1,
typename T2>
511 ostream&
operator<<(ostream& stream,
const std::map<T1, T2>& p) {
516 template<
typename T2>
517 ostream&
operator<<(ostream& stream,
const std::map<std::string, T2>& p) {
524 if (a==0 || b==0)
return false;
525 return abs(std::numeric_limits<T>::max()/a) < abs(b);
533 template<
typename T,
typename S>
536 ret.reserve(rhs.size());
537 for (
auto e : rhs) ret.push_back(
static_cast<T>(e));
542 std::vector<T>
vector_slice(
const std::vector<T> &v,
const std::vector<casadi_int> &i) {
544 ret.reserve(i.size());
545 for (casadi_int k=0;k<i.size();++k) {
548 "vector_slice: Indices should be larger than zero."
549 "You have " +
str(j) +
" at location " +
str(k) +
".");
550 casadi_assert(j<v.size(),
551 "vector_slice: Indices should be larger than zero."
552 "You have " +
str(j) +
" at location " +
str(k) +
".");
559 std::vector<T>
vector_select(
const std::vector<T> &v,
const std::vector<bool> &s,
bool invert) {
561 casadi_assert(v.size()==s.size(),
"Dimension mismatch.");
563 for (casadi_int k=0;k<s.size();++k) {
564 if (!s[k]) ret.push_back(v[k]);
567 for (casadi_int k=0;k<s.size();++k) {
568 if (s[k]) ret.push_back(v[k]);
577 ret.insert(ret.begin(), v.begin()+1, v.end());
584 ret.insert(ret.begin(), v.begin(), v.begin()+v.size()-1);
589 std::vector<T>
reverse(
const std::vector<T> &v) {
590 std::vector<T> ret(v.size());
591 std::reverse_copy(v.begin(), v.end(), ret.begin());
596 std::vector<T>
join(
const std::vector<T> &a,
const std::vector<T> &b) {
597 std::vector<T> ret = a;
598 ret.insert(ret.end(), b.begin(), b.end());
603 std::vector<T>
join(
const std::vector<T> &a,
const std::vector<T> &b,
const std::vector<T> &c) {
604 std::vector<T> ret = a;
605 ret.insert(ret.end(), b.begin(), b.end());
606 ret.insert(ret.end(), c.begin(), c.end());
611 std::vector<T>
permute(
const std::vector<T> &a,
const std::vector<casadi_int> &order) {
612 casadi_assert_dev(order.size()==a.size());
618 std::vector<casadi_int>
find(
const std::vector<T> &v) {
619 std::vector<casadi_int> ret;
620 for (casadi_int i=0;i<v.size();++i) {
621 if (v[i]) ret.push_back(i);
628 std::vector<T>
applymap(T (*f)(
const T&) ,
const std::vector<T>& comp) {
629 std::vector<T> ret(comp.size());
630 std::transform(comp.begin(), comp.end(), ret.begin(), f);
635 void applymap(
void (*f)(T &), std::vector<T>& comp) {
636 std::for_each(comp.begin(), comp.end(), f);
639 template<
class S,
class D>
641 casadi_assert(s.size()==d.size(),
"Dimension mismatch.");
642 std::copy(s.begin(), s.end(), d.begin());
645 template<
class S,
class D>
647 casadi_assert(d.empty(),
"Receiving vector must be empty");
649 std::copy(s.begin(), s.end(), d.begin());
652 template<
class S,
class D>
654 for (casadi_int i=0;i<d.size();++i) {
655 d[i] =
static_cast<D>(s[i]);
659 template<
class S,
class D>
662 std::copy(s.begin(), s.end(), d.begin());
667 bool in_range(
const std::vector<T> &v, casadi_int upper) {
672 bool in_range(
const std::vector<T> &v, casadi_int lower, casadi_int upper) {
673 if (v.empty())
return true;
674 casadi_int max = *std::max_element(v.begin(), v.end());
675 if (max >= upper)
return false;
676 casadi_int min = *std::min_element(v.begin(), v.end());
677 return (min >= lower);
680 template<
class T,
class S>
682 std::vector<S>& flat) {
685 for (
const auto& e : nested) {
692 for (
const auto& e : nested) {
693 flat.insert(flat.end(), e.begin(), e.end());
697 template<
class T,
class S,
class I>
699 std::vector<S>& flat,
700 std::vector<I>& indices) {
705 casadi_int N = nested.size();
706 indices.resize(1, 0);
707 indices.reserve(N+1);
708 casadi_int offset = 0;
709 for (
const auto& e : nested) {
711 indices.push_back(offset);
717 std::set<T> s(v.begin(), v.end());
718 return v.size()==s.size();
723 if (v.empty())
return true;
725 for (casadi_int i=1;i<v.size();++i) {
726 if (!(v[i] > el))
return false;
734 if (v.empty())
return true;
736 for (casadi_int i=1;i<v.size();++i) {
737 if (!(v[i] < el))
return false;
745 if (v.empty())
return true;
747 for (casadi_int i=1;i<v.size();++i) {
748 if (!(v[i] <= el))
return false;
756 if (v.empty())
return true;
758 for (casadi_int i=1;i<v.size();++i) {
759 if (!(v[i] >= el))
return false;
777 for (std::size_t i=0; i<v.size(); ++i) {
778 if (v[i]<0)
return true;
785 std::copy(v.begin(), v.end(), std::ostream_iterator<T>(stream,
" "));
789 void write_matlab(std::ostream &stream,
const std::vector<std::vector<T> > &v) {
790 for (casadi_uint i=0; i<v.size(); ++i) {
791 std::copy(v[i].begin(), v[i].end(), std::ostream_iterator<T>(stream,
" "));
800 while (!stream.eof()) {
808 val = std::numeric_limits<T>::infinity();
817 void read_matlab(std::ifstream &file, std::vector<std::vector<T> > &v) {
820 while (!getline(file, line,
'\n').eof()) {
821 std::istringstream reader(line);
822 std::vector<T> lineData;
824 while (!reader.eof()) {
832 val = std::numeric_limits<T>::infinity();
836 lineData.push_back(val);
838 v.push_back(lineData);
842 template<
typename T,
typename F,
typename L>
843 void linspace(std::vector<T> &v,
const F& first,
const L& last) {
845 throw CasadiException(
"std::linspace: vector must contain at least two elements");
848 T increment = (last-first)/
T(v.size()-1);
851 for (
unsigned i=1; i<v.size()-1; ++i)
852 v[i] = v[i-1] + increment;
853 v[v.size()-1] = last;
875 const std::vector<T> &
v_;
881 void sort(
const std::vector<T> &values, std::vector<T> &sorted_values,
882 std::vector<casadi_int> &indices,
bool invert_indices) {
884 if (invert_indices) {
885 std::vector<casadi_int> inverted;
886 sort(values, sorted_values, inverted,
false);
887 indices.resize(inverted.size());
888 for (
size_t i=0; i<inverted.size(); ++i) {
889 indices[inverted[i]] = i;
895 indices.resize(values.size());
896 for (
size_t i=0; i<indices.size(); ++i) indices[i] = i;
899 std::sort(indices.begin(), indices.end(),
sortCompare<T>(values));
902 sorted_values.resize(values.size());
903 for (
size_t i=0; i<values.size(); ++i) {
904 sorted_values[i] = values[indices[i]];
911 for (casadi_int i=0;i<values.size();++i) r*=values[i];
916 T sum(
const std::vector<T> &values) {
918 for (casadi_int i=0;i<values.size();++i) r+=values[i];
923 std::vector<T>
cumsum(
const std::vector<T> &values) {
924 std::vector<T> ret(values.size());
926 for (casadi_int i=0;i<values.size();++i) {
934 std::vector<T>
cumsum0(
const std::vector<T> &values) {
935 std::vector<T> ret(values.size()+1, 0);
937 for (casadi_int i=0;i<values.size();++i) {
945 std::vector<T>
diff(
const std::vector<T> &values) {
946 casadi_assert(!values.empty(),
"Array must be non-empty");
947 std::vector<T> ret(values.size()-1);
948 for (casadi_int i=0;i<values.size()-1;++i) {
949 ret[i] = values[i+1]-values[i];
955 T dot(
const std::vector<T>& a,
const std::vector<T>& b) {
957 for (casadi_int k=0; k<a.size(); ++k) {
966 for (casadi_int k=0; k<x.size(); ++k) {
967 ret = fmax(ret, fabs(x[k]));
975 for (casadi_int k=0; k<x.size(); ++k) {
984 for (casadi_int k=0; k<x.size(); ++k) {
992 casadi_assert(0,
"get_bvec_t only supported for double");
997 casadi_assert(0,
"get_bvec_t only supported for double");
scoped_checkout(scoped_checkout &&that)
scoped_checkout(const T &proto)
scoped_checkout(const scoped_checkout &that)=delete
std::vector< casadi_int > range(casadi_int start, casadi_int stop, casadi_int step, casadi_int len)
Range function.
bvec_t * get_bvec_t(std::vector< double > &v)
bool is_equally_spaced(const std::vector< double > &v)
T product(const std::vector< T > &values)
product
std::vector< T > vector_tail(const std::vector< T > &v)
Return all but the first element of a vector.
std::string join(const std::vector< std::string > &l, const std::string &delim)
bool is_decreasing(const std::vector< T > &v)
Check if the vector is strictly decreasing.
std::vector< casadi_int > invert_permutation(const std::vector< casadi_int > &a)
inverse a permutation vector
void assign_vector(const std::vector< S > &s, std::vector< D > &d)
T norm_1(const std::vector< T > &x)
unsigned long long bvec_t
void init_vector(std::vector< S > &d, const std::vector< D > &s)
T norm_inf(const std::vector< T > &x)
void copy_vector(const std::vector< S > &s, std::vector< D > &d)
bool is_range(const std::vector< casadi_int > &v, casadi_int start, casadi_int stop, casadi_int step)
Check if a vector matches a range.
std::string temporary_file(const std::string &prefix, const std::string &suffix)
bool startswith(const std::string &s, const std::string &p)
Checks if s starts with p.
int to_int(casadi_int rhs)
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 isUnique(const std::vector< T > &v)
bool is_increasing(const std::vector< T > &v)
Check if the vector is strictly increasing.
CASADI_EXPORT std::string replace(const std::string &s, const std::string &p, const std::string &r)
Replace all occurences of p with r in s.
void sort(const std::vector< T > &values, std::vector< T > &sorted_values, std::vector< casadi_int > &indices, bool invert_indices=false)
Sort the data in a vector.
std::vector< casadi_int > tensor_permute_mapping(const std::vector< casadi_int > &dims, const std::vector< casadi_int > &order)
Computes a mapping for a (dense) tensor permutation.
std::vector< casadi_int > find(const std::vector< T > &v)
find nonzeros
bool is_monotone(const std::vector< T > &v)
Check if the vector is monotone.
std::string str_bvec(bvec_t v)
std::vector< bool > boolvec_or(const std::vector< bool > &lhs, const std::vector< bool > &rhs)
Or operation on boolean vector.
void linspace(std::vector< T > &v, const F &first, const L &last)
Matlab's linspace.
std::string str(const T &v)
String representation, any type.
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 write_matlab(std::ostream &stream, const std::vector< T > &v)
Print vector, matlab style.
std::vector< T > vector_static_cast(const std::vector< S > &rhs)
std::vector< bool > boolvec_not(const std::vector< bool > &v)
Invert all entries.
std::vector< std::string > StringVector
std::vector< T > cumsum(const std::vector< T > &values)
cumulative sum
std::vector< bool > boolvec_and(const std::vector< bool > &lhs, const std::vector< bool > &rhs)
And operation on boolean vector.
bool any(const std::vector< bool > &v)
Check if any arguments are true.
void normalized_setup(std::istream &stream)
void read_matlab(std::istream &stream, std::vector< T > &v)
Read vector, matlab style.
T dot(const std::vector< T > &a, const std::vector< T > &b)
bool all(const std::vector< bool > &v)
Check if all arguments are true.
bool is_strictly_monotone(const std::vector< T > &v)
Check if the vector is strictly monotone.
std::vector< T > diff(const std::vector< T > &values)
diff
std::vector< T > vector_select(const std::vector< T > &v, const std::vector< bool > &s, bool invert=false)
Select subset of vector.
std::vector< T > vector_slice(const std::vector< T > &v, const std::vector< casadi_int > &i)
Slicing vector.
std::vector< T > vector_init(const std::vector< T > &v)
Return all but the last element of a vector.
std::vector< T > applymap(T(*f)(const T &), const std::vector< T > &comp)
T * get_ptr(std::vector< T > &v)
Get a pointer to the data contained in the vector.
T norm_2(const std::vector< T > &x)
std::vector< T > cumsum0(const std::vector< T > &values)
cumulative sum, starting with zero
bool in_range(const std::vector< T > &v, casadi_int upper)
Check if for each element of v holds: v_i < upper.
bool is_permutation(const std::vector< casadi_int > &order)
Does the list represent a permutation?
bool is_nonincreasing(const std::vector< T > &v)
Check if the vector is non-increasing.
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.
std::vector< T > permute(const std::vector< T > &a, const std::vector< casadi_int > &order)
permute a list
T sum(const std::vector< T > &values)
sum
std::vector< T > reverse(const std::vector< T > &v)
Reverse a list.
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.
bvec_t bvec_or(const bvec_t *arg, casadi_int n)
Bit-wise or operation on bvec_t array.
int normalized_in(std::istream &stream, double &ret)
void normalized_out(std::ostream &stream, double val)
std::vector< casadi_int > boolvec_to_index(const std::vector< bool > &v)
bool mul_overflows(const T &a, const T &b)
ostream & operator<<(ostream &stream, const vector< T > &v)
Enables flushing an std::vector to a stream (prints representation)
bool operator()(casadi_int i, casadi_int j) const
const std::vector< T > & v_
sortCompare(const std::vector< T > &v)