casadi_misc.hpp
1 /*
2  * This file is part of CasADi.
3  *
4  * CasADi -- A symbolic framework for dynamic optimization.
5  * Copyright (C) 2010-2023 Joel Andersson, Joris Gillis, Moritz Diehl,
6  * KU Leuven. All rights reserved.
7  * Copyright (C) 2011-2014 Greg Horn
8  *
9  * CasADi is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 3 of the License, or (at your option) any later version.
13  *
14  * CasADi is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with CasADi; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  *
23  */
24 
25 
26 #ifndef CASADI_MISC_HPP
27 #define CASADI_MISC_HPP
28 
29 #include "exception.hpp"
30 #include "casadi_common.hpp"
31 
39 namespace casadi {
40 
41 #ifndef SWIG
42 
43 template<typename T>
44 class scoped_checkout {
45 public:
46  scoped_checkout(const T& proto) : proto_(proto) {
47  mem = proto_.checkout();
48  }
49 
50  scoped_checkout(scoped_checkout&& that) : mem(that.mem), proto_(that.proto_) {
51  that.mem = -1;
52  }
53 
54  scoped_checkout(const scoped_checkout& that) = delete;
55 
56  ~scoped_checkout() {
57  if (mem!=-1) proto_.release(mem);
58  }
59 
60  operator casadi_int() const {
61  return mem;
62  }
63 
64 private:
65  int mem;
66  const T& proto_;
67 };
68 
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());
82 
86  CASADI_EXPORT bool is_range(const std::vector<casadi_int>& v,
87  casadi_int start, casadi_int stop, casadi_int step=1);
88 
89  CASADI_EXPORT std::string join(const std::vector<std::string>& l, const std::string& delim=",");
90 
92  CASADI_EXPORT bool startswith(const std::string& s, const std::string& p);
93 
95  CASADI_EXPORT std::string replace(const std::string& s,
96  const std::string& p, const std::string& r);
97 
105  CASADI_EXPORT std::vector<casadi_int> range(casadi_int stop);
106 
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);
119 
120  CASADI_EXPORT std::vector<casadi_int> boolvec_to_index(const std::vector<bool> &v);
121 
122  CASADI_EXPORT bool is_equally_spaced(const std::vector<double> &v);
123 
125  CASADI_EXPORT std::vector<casadi_int> tensor_permute_mapping(const std::vector<casadi_int>& dims,
126  const std::vector<casadi_int>& order);
127 
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);
132 
133  template<typename T, typename S>
134  std::vector<T> vector_static_cast(const std::vector<S>& rhs);
135 
136  CASADI_EXPORT std::string str_bvec(bvec_t v);
137 
144  template<typename T>
145  std::vector<T> vector_slice(const std::vector<T> &v, const std::vector<casadi_int> &i);
146 
153  template<typename T>
154  std::vector<T> vector_select(const std::vector<T> &v, const std::vector<bool> &s,
155  bool invert=false);
156 
160  template<typename T>
161  std::vector<T> vector_tail(const std::vector<T> &v);
162 
166  template<typename T>
167  std::vector<T> vector_init(const std::vector<T> &v);
168 
172  template<typename T>
173  std::vector<T> reverse(const std::vector<T> &v);
174 
178  template<typename T>
179  std::vector<T> join(const std::vector<T> &a, const std::vector<T> &b);
180 
184  template<typename T>
185  std::vector<T> join(const std::vector<T> &a, const std::vector<T> &b, const std::vector<T> &c);
186 
190  bool is_permutation(const std::vector<casadi_int> &order);
191 
195  template<typename T>
196  std::vector<T> permute(const std::vector<T> &a, const std::vector<casadi_int> &order);
197 
201  std::vector<casadi_int> invert_permutation(const std::vector<casadi_int> &a);
202 
206  template<typename T>
207  std::vector<casadi_int> find(const std::vector<T> &v);
208 
209  #endif // SWIG
210 
212  template<typename T>
213  bool in_range(const std::vector<T> &v, casadi_int upper);
214 
216  template<typename T>
217  bool in_range(const std::vector<T> &v, casadi_int lower, casadi_int upper);
218 
219  // Assert that a indices are in a range
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) + ").")
226 
227  // Assert that a indices are bounded
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 "\
233  + str(upper) + ".")
234 
242  CASADI_EXPORT std::vector<casadi_int> complement(const std::vector<casadi_int> &v,
243  casadi_int size);
244 
253  CASADI_EXPORT std::vector<casadi_int> lookupvector(const std::vector<casadi_int> &v,
254  casadi_int size);
255  CASADI_EXPORT std::vector<casadi_int> lookupvector(const std::vector<casadi_int> &v);
256 
262  template<class T, class S>
263  void flatten_nested_vector(const std::vector< std::vector<T> >& nested,
264  std::vector<S>& flat);
265 
271  template<class T, class S, class I>
272  void flatten_nested_vector(const std::vector< std::vector<T> >& nested,
273  std::vector<S>& flat,
274  std::vector<I>& indices);
275 
277 #ifndef SWIG
281  template<class T>
282  std::vector<T> applymap(T (*f)(const T&), const std::vector<T>& comp);
283 
287  template<class T>
288  void applymap(void (*f)(T&), std::vector<T>& comp);
289 #endif // SWIG
291 
292 
294  template<typename T>
295  bool is_increasing(const std::vector<T> &v);
296 
298  template<typename T>
299  bool is_decreasing(const std::vector<T> &v);
300 
302  template<typename T>
303  bool is_nonincreasing(const std::vector<T> &v);
304 
306  template<typename T>
307  bool is_nondecreasing(const std::vector<T> &v);
308 
310  template<typename T>
311  bool is_monotone(const std::vector<T> &v);
312 
314  template<typename T>
315  bool is_strictly_monotone(const std::vector<T> &v);
316 
318  template<typename T>
319  bool has_negative(const std::vector<T> &v);
320 
322  template<typename T>
323  void write_matlab(std::ostream &stream, const std::vector<T> &v);
324 
326  template<typename T>
327  void write_matlab(std::ostream &stream, const std::vector<std::vector<T> > &v);
328 
330  template<typename T>
331  void read_matlab(std::istream &stream, std::vector<T> &v);
332 
334  template<typename T>
335  void read_matlab(std::ifstream &file, std::vector<std::vector<T> > &v);
336 
337 #ifndef SWIG
339  template<typename T, typename F, typename L>
340  void linspace(std::vector<T> &v, const F& first, const L& last);
341 
344  CASADI_EXPORT bvec_t* get_bvec_t(std::vector<double>& v);
345 
347  CASADI_EXPORT const bvec_t* get_bvec_t(const std::vector<double>& v);
348 
350  CASADI_EXPORT bvec_t bvec_or(const bvec_t* arg, casadi_int n);
351 
353  template<typename T>
354  bvec_t* get_bvec_t(std::vector<T>& v);
355 
357  template<typename T>
358  const bvec_t* get_bvec_t(const std::vector<T>& v);
359 
361  template<typename T>
362  T* get_ptr(std::vector<T> &v);
363 
365  template<typename T>
366  const T* get_ptr(const std::vector<T> &v);
367 
369 
378  template<typename T>
379  void sort(const std::vector<T> &values, std::vector<T> &sorted_values,
380  std::vector<casadi_int> &indices, bool invert_indices =false);
381 
385  template<typename T>
386  T product(const std::vector<T> &values);
387 
391  template<typename T>
392  T sum(const std::vector<T> &values);
393 
397  template<typename T>
398  std::vector<T> cumsum(const std::vector<T> &values);
399 
403  template<typename T>
404  std::vector<T> diff(const std::vector<T> &values);
405 
409  template<typename T>
410  std::vector<T> cumsum0(const std::vector<T> &values);
411 #endif //SWIG
412 
414  template<typename T>
415  bool is_regular(const std::vector<T> &v) {
416  for (auto&& vk : v) {
417  if (vk!=vk || vk==std::numeric_limits<T>::infinity() ||
418  vk==-std::numeric_limits<T>::infinity()) return false;
419  }
420  return true;
421  }
422 
423  // Create a temporary file
424  CASADI_EXPORT std::string temporary_file(const std::string& prefix, const std::string& suffix);
425 
426  CASADI_EXPORT void normalized_setup(std::istream& stream);
427  CASADI_EXPORT void normalized_setup(std::ostream& stream);
428 
429 
430  class CASADI_EXPORT StreamStateGuard {
431  public:
432  explicit StreamStateGuard(std::ostream& os);
434 
435  private:
436  std::ostream& stream_;
437  std::ios::fmtflags flags_;
438  std::streamsize precision_;
439  std::streamsize width_;
440  std::locale locale_;
441  };
442 
443  inline void normalized_out(std::ostream& stream, double val) {
444  if (val==std::numeric_limits<double>::infinity()) {
445  stream << "inf";
446  } else if (val==-std::numeric_limits<double>::infinity()) {
447  stream << "-inf";
448  } else if (val!=val) {
449  stream << "nan";
450  } else {
451  stream << val;
452  }
453  }
454  inline int normalized_in(std::istream& stream, double& ret) {
455  std::streampos start = stream.tellg();
456  stream >> ret;
457  // Failed to interpret as double?
458  if (stream.fail()) {
459  // Clear error flag
460  stream.clear();
461  // Reset stream position
462  // Need to parse e.g "-inf"
463  stream.seekg(start);
464  // Might be a inf/nan
465  std::string non_reg;
466  stream >> non_reg;
467  // Break on trailing whitespace
468  if (stream.fail()) {
469  if (stream.eof()) {
470  ret = std::numeric_limits<double>::quiet_NaN();
471  return -1; // End of stream
472  } else {
473  ret = std::numeric_limits<double>::quiet_NaN();
474  return 1; // Failed to parse to string
475  }
476  }
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();
483  } else {
484  ret = std::numeric_limits<double>::quiet_NaN();
485  return 2; // Failed to interpret as number
486  }
487  }
488  return 0;
489  }
490 
491 } // namespace casadi
492 
493 #ifndef SWIG
494 // In std namespace
495 namespace std {
496 
498  template<typename T>
499  ostream& operator<<(ostream& stream, const vector<T>& v) {
500  stream << casadi::str(v);
501  return stream;
502  }
503 
505  template<typename T, size_t N>
506  ostream& operator<<(ostream& stream, const array<T, N>& v) {
507  stream << casadi::str(v);
508  return stream;
509  }
510 
512  template<typename T>
513  ostream& operator<<(ostream& stream, const set<T>& v) {
514  stream << casadi::str(v);
515  return stream;
516  }
517 
518  template<typename T1, typename T2>
519  ostream& operator<<(ostream& stream, const pair<T1, T2>& p) {
520  stream << casadi::str(p);
521  return stream;
522  }
523 
524  template<typename T1, typename T2>
525  ostream& operator<<(ostream& stream, const std::map<T1, T2>& p) {
526  stream << casadi::str(p);
527  return stream;
528  }
529 
530  template<typename T2>
531  ostream& operator<<(ostream& stream, const std::map<std::string, T2>& p) {
532  stream << casadi::str(p);
533  return stream;
534  }
535 
536  template<typename T>
537  bool mul_overflows(const T& a, const T& b) {
538  if (a==0 || b==0) return false;
539  return abs(std::numeric_limits<T>::max()/a) < abs(b);
540  }
541 
542 } // namespace std
543 
544 // Implementations
545 namespace casadi {
546 
547  template<typename T, typename S>
548  std::vector<T> vector_static_cast(const std::vector<S>& rhs) {
549  std::vector<T> ret;
550  ret.reserve(rhs.size());
551  for (auto e : rhs) ret.push_back(static_cast<T>(e));
552  return ret;
553  }
554 
555  template<typename T>
556  std::vector<T> vector_slice(const std::vector<T> &v, const std::vector<casadi_int> &i) {
557  std::vector<T> ret;
558  ret.reserve(i.size());
559  for (casadi_int k=0;k<i.size();++k) {
560  casadi_int j = i[k];
561  casadi_assert(j>=0,
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) + ".");
567  ret.push_back(v[j]);
568  }
569  return ret;
570  }
571 
572  template<typename T>
573  std::vector<T> vector_select(const std::vector<T> &v, const std::vector<bool> &s, bool invert) {
574  std::vector<T> ret;
575  casadi_assert(v.size()==s.size(), "Dimension mismatch.");
576  if (invert) {
577  for (casadi_int k=0;k<s.size();++k) {
578  if (!s[k]) ret.push_back(v[k]);
579  }
580  } else {
581  for (casadi_int k=0;k<s.size();++k) {
582  if (s[k]) ret.push_back(v[k]);
583  }
584  }
585  return ret;
586  }
587 
588  template<typename T>
589  std::vector<T> vector_tail(const std::vector<T> &v) {
590  std::vector<T> ret;
591  ret.insert(ret.begin(), v.begin()+1, v.end());
592  return ret;
593  }
594 
595  template<typename T>
596  std::vector<T> vector_init(const std::vector<T> &v) {
597  std::vector<T> ret;
598  ret.insert(ret.begin(), v.begin(), v.begin()+v.size()-1);
599  return ret;
600  }
601 
602  template<typename T>
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());
606  return ret;
607  }
608 
609  template<typename T>
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());
613  return ret;
614  }
615 
616  template<typename T>
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());
621  return ret;
622  }
623 
624  template<typename T>
625  std::vector<T> permute(const std::vector<T> &a, const std::vector<casadi_int> &order) {
626  casadi_assert_dev(order.size()==a.size());
627  casadi_assert_dev(is_permutation(order));
628  return vector_slice(a, order);
629  }
630 
631  template<typename T>
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);
636  }
637  return ret;
638  }
639 
640 #ifndef SWIG
641  template<class T>
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);
645  return ret;
646  }
647 
648  template<class T>
649  void applymap(void (*f)(T &), std::vector<T>& comp) {
650  std::for_each(comp.begin(), comp.end(), f);
651  }
652 
653  template<class S, class D>
654  void copy_vector(const std::vector<S>& s, std::vector<D>& d) {
655  casadi_assert(s.size()==d.size(), "Dimension mismatch.");
656  std::copy(s.begin(), s.end(), d.begin());
657  }
658 
659  template<class S, class D>
660  void assign_vector(const std::vector<S>& s, std::vector<D>& d) {
661  casadi_assert(d.empty(), "Receiving vector must be empty");
662  d.resize(s.size());
663  std::copy(s.begin(), s.end(), d.begin());
664  }
665 
666  template<class S, class D>
667  void copy_vector(const S* s, std::vector<D>& d) {
668  for (casadi_int i=0;i<d.size();++i) {
669  d[i] = static_cast<D>(s[i]);
670  }
671  }
672 
673  template<class S, class D>
674  void init_vector(std::vector<S>& d, const std::vector<D>& s) {
675  d.resize(s.size());
676  std::copy(s.begin(), s.end(), d.begin());
677  }
678 #endif //SWIG
679 
680  template<typename T>
681  bool in_range(const std::vector<T> &v, casadi_int upper) {
682  return in_range(v, 0, upper);
683  }
684 
685  template<typename T>
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);
692  }
693 
694  template<class T, class S>
695  void flatten_nested_vector(const std::vector< std::vector<T> >& nested,
696  std::vector<S>& flat) {
697  // Count total elements in nested
698  casadi_int N = 0;
699  for (const auto& e : nested) {
700  N += e.size();
701  }
702 
703  // Populate flat, one nested section at a time
704  flat.clear();
705  flat.reserve(N);
706  for (const auto& e : nested) {
707  flat.insert(flat.end(), e.begin(), e.end());
708  }
709  }
710 
711  template<class T, class S, class I>
712  void flatten_nested_vector(const std::vector< std::vector<T> >& nested,
713  std::vector<S>& flat,
714  std::vector<I>& indices) {
715  // Delegate
716  flatten_nested_vector(nested, flat);
717 
718  // Build up 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) {
724  offset += e.size();
725  indices.push_back(offset);
726  }
727  }
728 
729  template<typename T>
730  bool isUnique(const std::vector<T> &v) {
731  std::set<T> s(v.begin(), v.end());
732  return v.size()==s.size();
733  }
734 
735  template<typename T>
736  bool is_increasing(const std::vector<T> &v) {
737  if (v.empty()) return true;
738  T el = v[0];
739  for (casadi_int i=1;i<v.size();++i) {
740  if (!(v[i] > el)) return false;
741  el = v[i];
742  }
743  return el==el; // nan -> false
744  }
745 
746  template<typename T>
747  bool is_decreasing(const std::vector<T> &v) {
748  if (v.empty()) return true;
749  T el = v[0];
750  for (casadi_int i=1;i<v.size();++i) {
751  if (!(v[i] < el)) return false;
752  el = v[i];
753  }
754  return el==el; // nan -> false
755  }
756 
757  template<typename T>
758  bool is_nonincreasing(const std::vector<T> &v) {
759  if (v.empty()) return true;
760  T el = v[0];
761  for (casadi_int i=1;i<v.size();++i) {
762  if (!(v[i] <= el)) return false;
763  el = v[i];
764  }
765  return el==el; // nan -> false
766  }
767 
768  template<typename T>
769  bool is_nondecreasing(const std::vector<T> &v) {
770  if (v.empty()) return true;
771  T el = v[0];
772  for (casadi_int i=1;i<v.size();++i) {
773  if (!(v[i] >= el)) return false;
774  el = v[i];
775  }
776  return el==el; // nan -> false
777  }
778 
779  template<typename T>
780  bool is_monotone(const std::vector<T> &v) {
781  return is_nondecreasing(v) || is_nonincreasing(v);
782  }
783 
784  template<typename T>
785  bool is_strictly_monotone(const std::vector<T> &v) {
786  return is_decreasing(v) || is_increasing(v);
787  }
788 
789  template<typename T>
790  bool has_negative(const std::vector<T> &v) {
791  for (std::size_t i=0; i<v.size(); ++i) {
792  if (v[i]<0) return true;
793  }
794  return false;
795  }
796 
797  template<typename T>
798  void write_matlab(std::ostream &stream, const std::vector<T> &v) {
799  std::copy(v.begin(), v.end(), std::ostream_iterator<T>(stream, " "));
800  }
801 
802  template<typename T>
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, " "));
806  stream << std::endl;
807  }
808  }
809 
810  template<typename T>
811  void read_matlab(std::istream &stream, std::vector<T> &v) {
812  v.clear();
813 
814  while (!stream.eof()) {
815  T val;
816  stream >> val;
817  if (stream.fail()) {
818  stream.clear();
819  std::string s;
820  stream >> s;
821  if (s=="inf")
822  val = std::numeric_limits<T>::infinity();
823  else
824  break;
825  }
826  v.push_back(val);
827  }
828  }
829 
830  template<typename T>
831  void read_matlab(std::ifstream &file, std::vector<std::vector<T> > &v) {
832  v.clear();
833  std::string line;
834  while (!getline(file, line, '\n').eof()) {
835  std::istringstream reader(line);
836  std::vector<T> lineData;
837 
838  while (!reader.eof()) {
839  T val;
840  reader >> val;
841  if (reader.fail()) {
842  reader.clear();
843  std::string s;
844  reader >> s;
845  if (s=="inf")
846  val = std::numeric_limits<T>::infinity();
847  else
848  break;
849  }
850  lineData.push_back(val);
851  }
852  v.push_back(lineData);
853  }
854  }
855 
856  template<typename T, typename F, typename L>
857  void linspace(std::vector<T> &v, const F& first, const L& last) {
858  if (v.size()<2)
859  throw CasadiException("std::linspace: vector must contain at least two elements");
860 
861  // Increment
862  T increment = (last-first)/T(v.size()-1);
863 
864  v[0] = first;
865  for (unsigned i=1; i<v.size()-1; ++i)
866  v[i] = v[i-1] + increment;
867  v[v.size()-1] = last;
868  }
869 
870  template<typename T>
871  T* get_ptr(std::vector<T> &v) {
872  if (v.empty())
873  return nullptr;
874  else
875  return &v.front();
876  }
877 
878  template<typename T>
879  const T* get_ptr(const std::vector<T> &v) {
880  if (v.empty())
881  return nullptr;
882  else
883  return &v.front();
884  }
885 
886  // Helper class
887  template<typename T>
888  struct sortCompare {
889  const std::vector<T> &v_;
890  sortCompare(const std::vector<T> &v) : v_(v) {}
891  bool operator() (casadi_int i, casadi_int j) const { return v_[i]<v_[j];}
892  };
893 
894  template<typename T>
895  void sort(const std::vector<T> &values, std::vector<T> &sorted_values,
896  std::vector<casadi_int> &indices, bool invert_indices) {
897  // Call recursively if indices need to be inverted
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;
904  }
905  return;
906  }
907 
908  // Create list of indices
909  indices.resize(values.size());
910  for (size_t i=0; i<indices.size(); ++i) indices[i] = i;
911 
912  // Sort this list by the values
913  std::sort(indices.begin(), indices.end(), sortCompare<T>(values));
914 
915  // Sort the values accordingly
916  sorted_values.resize(values.size());
917  for (size_t i=0; i<values.size(); ++i) {
918  sorted_values[i] = values[indices[i]];
919  }
920  }
921 
922  template<typename T>
923  T product(const std::vector<T> &values) {
924  T r = 1;
925  for (casadi_int i=0;i<values.size();++i) r*=values[i];
926  return r;
927  }
928 
929  template<typename T>
930  T sum(const std::vector<T> &values) {
931  T r = 0;
932  for (casadi_int i=0;i<values.size();++i) r+=values[i];
933  return r;
934  }
935 
936  template<typename T>
937  std::vector<T> cumsum(const std::vector<T> &values) {
938  std::vector<T> ret(values.size());
939  T acc = 0;
940  for (casadi_int i=0;i<values.size();++i) {
941  acc+= values[i];
942  ret[i] = acc;
943  }
944  return ret;
945  }
946 
947  template<typename T>
948  std::vector<T> cumsum0(const std::vector<T> &values) {
949  std::vector<T> ret(values.size()+1, 0);
950  T acc = 0;
951  for (casadi_int i=0;i<values.size();++i) {
952  acc+= values[i];
953  ret[i+1] = acc;
954  }
955  return ret;
956  }
957 
958  template<typename T>
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];
964  }
965  return ret;
966  }
967 
968  template<typename T>
969  T dot(const std::vector<T>& a, const std::vector<T>& b) {
970  T ret = 0;
971  for (casadi_int k=0; k<a.size(); ++k) {
972  ret += a[k]*b[k];
973  }
974  return ret;
975  }
976 
977  template<typename T>
978  T norm_inf(const std::vector<T>& x) {
979  T ret = 0;
980  for (casadi_int k=0; k<x.size(); ++k) {
981  ret = fmax(ret, fabs(x[k]));
982  }
983  return ret;
984  }
985 
986  template<typename T>
987  T norm_1(const std::vector<T>& x) {
988  T ret = 0;
989  for (casadi_int k=0; k<x.size(); ++k) {
990  ret += fabs(x[k]);
991  }
992  return ret;
993  }
994 
995  template<typename T>
996  T norm_2(const std::vector<T>& x) {
997  T ret = 0;
998  for (casadi_int k=0; k<x.size(); ++k) {
999  ret += x[k]*x[k];
1000  }
1001  return sqrt(ret);
1002  }
1003 
1004  template<typename T>
1005  bvec_t* get_bvec_t(std::vector<T>& v) {
1006  casadi_assert(0, "get_bvec_t only supported for double");
1007  }
1008 
1009  template<typename T>
1010  const bvec_t* get_bvec_t(const std::vector<T>& v) {
1011  casadi_assert(0, "get_bvec_t only supported for double");
1012  }
1013 
1016  typedef std::vector<std::string> StringVector;
1018 
1019 } // namespace casadi
1020 #endif // SWIG
1021 
1022 #endif // CASADI_MISC_HPP
StreamStateGuard(std::ostream &os)
The casadi namespace.
Definition: archiver.hpp:32
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)