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);
436 std::ostream& stream_;
437 std::ios::fmtflags flags_;
438 std::streamsize precision_;
439 std::streamsize width_;
444 if (val==std::numeric_limits<double>::infinity()) {
446 }
else if (val==-std::numeric_limits<double>::infinity()) {
448 }
else if (val!=val) {
455 std::streampos start = stream.tellg();
470 ret = std::numeric_limits<double>::quiet_NaN();
473 ret = std::numeric_limits<double>::quiet_NaN();
477 if (non_reg==
"inf") {
478 ret = std::numeric_limits<double>::infinity();
479 }
else if (non_reg==
"-inf") {
480 ret = -std::numeric_limits<double>::infinity();
481 }
else if (non_reg==
"nan") {
482 ret = std::numeric_limits<double>::quiet_NaN();
484 ret = std::numeric_limits<double>::quiet_NaN();
505 template<
typename T,
size_t N>
518 template<
typename T1,
typename T2>
519 ostream&
operator<<(ostream& stream,
const pair<T1, T2>& p) {
524 template<
typename T1,
typename T2>
525 ostream&
operator<<(ostream& stream,
const std::map<T1, T2>& p) {
530 template<
typename T2>
531 ostream&
operator<<(ostream& stream,
const std::map<std::string, T2>& p) {
538 if (a==0 || b==0)
return false;
539 return abs(std::numeric_limits<T>::max()/a) < abs(b);
547 template<
typename T,
typename S>
550 ret.reserve(rhs.size());
551 for (
auto e : rhs) ret.push_back(
static_cast<T>(e));
556 std::vector<T>
vector_slice(
const std::vector<T> &v,
const std::vector<casadi_int> &i) {
558 ret.reserve(i.size());
559 for (casadi_int k=0;k<i.size();++k) {
562 "vector_slice: Indices should be larger than zero."
563 "You have " +
str(j) +
" at location " +
str(k) +
".");
564 casadi_assert(j<v.size(),
565 "vector_slice: Indices should be larger than zero."
566 "You have " +
str(j) +
" at location " +
str(k) +
".");
573 std::vector<T>
vector_select(
const std::vector<T> &v,
const std::vector<bool> &s,
bool invert) {
575 casadi_assert(v.size()==s.size(),
"Dimension mismatch.");
577 for (casadi_int k=0;k<s.size();++k) {
578 if (!s[k]) ret.push_back(v[k]);
581 for (casadi_int k=0;k<s.size();++k) {
582 if (s[k]) ret.push_back(v[k]);
591 ret.insert(ret.begin(), v.begin()+1, v.end());
598 ret.insert(ret.begin(), v.begin(), v.begin()+v.size()-1);
603 std::vector<T>
reverse(
const std::vector<T> &v) {
604 std::vector<T> ret(v.size());
605 std::reverse_copy(v.begin(), v.end(), ret.begin());
610 std::vector<T>
join(
const std::vector<T> &a,
const std::vector<T> &b) {
611 std::vector<T> ret = a;
612 ret.insert(ret.end(), b.begin(), b.end());
617 std::vector<T>
join(
const std::vector<T> &a,
const std::vector<T> &b,
const std::vector<T> &c) {
618 std::vector<T> ret = a;
619 ret.insert(ret.end(), b.begin(), b.end());
620 ret.insert(ret.end(), c.begin(), c.end());
625 std::vector<T>
permute(
const std::vector<T> &a,
const std::vector<casadi_int> &order) {
626 casadi_assert_dev(order.size()==a.size());
632 std::vector<casadi_int>
find(
const std::vector<T> &v) {
633 std::vector<casadi_int> ret;
634 for (casadi_int i=0;i<v.size();++i) {
635 if (v[i]) ret.push_back(i);
642 std::vector<T>
applymap(T (*f)(
const T&) ,
const std::vector<T>& comp) {
643 std::vector<T> ret(comp.size());
644 std::transform(comp.begin(), comp.end(), ret.begin(), f);
649 void applymap(
void (*f)(T &), std::vector<T>& comp) {
650 std::for_each(comp.begin(), comp.end(), f);
653 template<
class S,
class D>
655 casadi_assert(s.size()==d.size(),
"Dimension mismatch.");
656 std::copy(s.begin(), s.end(), d.begin());
659 template<
class S,
class D>
661 casadi_assert(d.empty(),
"Receiving vector must be empty");
663 std::copy(s.begin(), s.end(), d.begin());
666 template<
class S,
class D>
668 for (casadi_int i=0;i<d.size();++i) {
669 d[i] =
static_cast<D>(s[i]);
673 template<
class S,
class D>
676 std::copy(s.begin(), s.end(), d.begin());
681 bool in_range(
const std::vector<T> &v, casadi_int upper) {
686 bool in_range(
const std::vector<T> &v, casadi_int lower, casadi_int upper) {
687 if (v.empty())
return true;
688 casadi_int max = *std::max_element(v.begin(), v.end());
689 if (max >= upper)
return false;
690 casadi_int min = *std::min_element(v.begin(), v.end());
691 return (min >= lower);
694 template<
class T,
class S>
696 std::vector<S>& flat) {
699 for (
const auto& e : nested) {
706 for (
const auto& e : nested) {
707 flat.insert(flat.end(), e.begin(), e.end());
711 template<
class T,
class S,
class I>
713 std::vector<S>& flat,
714 std::vector<I>& indices) {
719 casadi_int N = nested.size();
720 indices.resize(1, 0);
721 indices.reserve(N+1);
722 casadi_int offset = 0;
723 for (
const auto& e : nested) {
725 indices.push_back(offset);
731 std::set<T> s(v.begin(), v.end());
732 return v.size()==s.size();
737 if (v.empty())
return true;
739 for (casadi_int i=1;i<v.size();++i) {
740 if (!(v[i] > el))
return false;
748 if (v.empty())
return true;
750 for (casadi_int i=1;i<v.size();++i) {
751 if (!(v[i] < el))
return false;
759 if (v.empty())
return true;
761 for (casadi_int i=1;i<v.size();++i) {
762 if (!(v[i] <= el))
return false;
770 if (v.empty())
return true;
772 for (casadi_int i=1;i<v.size();++i) {
773 if (!(v[i] >= el))
return false;
791 for (std::size_t i=0; i<v.size(); ++i) {
792 if (v[i]<0)
return true;
799 std::copy(v.begin(), v.end(), std::ostream_iterator<T>(stream,
" "));
803 void write_matlab(std::ostream &stream,
const std::vector<std::vector<T> > &v) {
804 for (casadi_uint i=0; i<v.size(); ++i) {
805 std::copy(v[i].begin(), v[i].end(), std::ostream_iterator<T>(stream,
" "));
814 while (!stream.eof()) {
822 val = std::numeric_limits<T>::infinity();
831 void read_matlab(std::ifstream &file, std::vector<std::vector<T> > &v) {
834 while (!getline(file, line,
'\n').eof()) {
835 std::istringstream reader(line);
836 std::vector<T> lineData;
838 while (!reader.eof()) {
846 val = std::numeric_limits<T>::infinity();
850 lineData.push_back(val);
852 v.push_back(lineData);
856 template<
typename T,
typename F,
typename L>
857 void linspace(std::vector<T> &v,
const F& first,
const L& last) {
859 throw CasadiException(
"std::linspace: vector must contain at least two elements");
862 T increment = (last-first)/
T(v.size()-1);
865 for (
unsigned i=1; i<v.size()-1; ++i)
866 v[i] = v[i-1] + increment;
867 v[v.size()-1] = last;
889 const std::vector<T> &
v_;
895 void sort(
const std::vector<T> &values, std::vector<T> &sorted_values,
896 std::vector<casadi_int> &indices,
bool invert_indices) {
898 if (invert_indices) {
899 std::vector<casadi_int> inverted;
900 sort(values, sorted_values, inverted,
false);
901 indices.resize(inverted.size());
902 for (
size_t i=0; i<inverted.size(); ++i) {
903 indices[inverted[i]] = i;
909 indices.resize(values.size());
910 for (
size_t i=0; i<indices.size(); ++i) indices[i] = i;
913 std::sort(indices.begin(), indices.end(),
sortCompare<T>(values));
916 sorted_values.resize(values.size());
917 for (
size_t i=0; i<values.size(); ++i) {
918 sorted_values[i] = values[indices[i]];
925 for (casadi_int i=0;i<values.size();++i) r*=values[i];
930 T sum(
const std::vector<T> &values) {
932 for (casadi_int i=0;i<values.size();++i) r+=values[i];
937 std::vector<T>
cumsum(
const std::vector<T> &values) {
938 std::vector<T> ret(values.size());
940 for (casadi_int i=0;i<values.size();++i) {
948 std::vector<T>
cumsum0(
const std::vector<T> &values) {
949 std::vector<T> ret(values.size()+1, 0);
951 for (casadi_int i=0;i<values.size();++i) {
959 std::vector<T>
diff(
const std::vector<T> &values) {
960 casadi_assert(!values.empty(),
"Array must be non-empty");
961 std::vector<T> ret(values.size()-1);
962 for (casadi_int i=0;i<values.size()-1;++i) {
963 ret[i] = values[i+1]-values[i];
969 T dot(
const std::vector<T>& a,
const std::vector<T>& b) {
971 for (casadi_int k=0; k<a.size(); ++k) {
980 for (casadi_int k=0; k<x.size(); ++k) {
981 ret = fmax(ret, fabs(x[k]));
989 for (casadi_int k=0; k<x.size(); ++k) {
998 for (casadi_int k=0; k<x.size(); ++k) {
1004 template<
typename T>
1006 casadi_assert(0,
"get_bvec_t only supported for double");
1009 template<
typename T>
1011 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)