00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifndef _UTIL_H_
00011 #define _UTIL_H_
00012
00013 #include <cassert>
00014 #include <iostream>
00015 #include <sstream>
00016 #include <string>
00017 #include <utility>
00018 #include <vector>
00019
00020 #include "debug.h"
00021
00022
00023
00024
00025 template <class Real>
00026 Real sum(safevector<Real> xx) {
00027 Real res=0;
00028 for (unsigned i=0; i<xx.size(); i++) {
00029 res += xx[i];
00030 }
00031 return res;
00032 }
00033
00034 template <class Real>
00035 safevector<Real> cumsum(safevector<Real> xx) {
00036 safevector<Real> res(xx.size());
00037 Real cumsum = 0;
00038 for (unsigned i=0; i<xx.size(); i++) {
00039 cumsum += xx[i];
00040 res[i] = cumsum;
00041 }
00042 return res;
00043 }
00044
00045 inline double num(double x) {
00046 double y=x;
00047 if (x < 0)
00048 y = -x;
00049 return(y);
00050 }
00051
00052
00053 inline int imin(int a, int b) {
00054 if (a<b) return(a);
00055 else return(b);
00056 }
00057
00058 inline int imax(int a, int b)
00059 {
00060 if (a>b) return(a);
00061 else return(b);
00062 }
00063
00064 void error(char* error_text);
00065
00066 template <class T>
00067 std::string numStr(T x) {
00068 std::ostringstream nmbstr; nmbstr << x;
00069 std::string ss = nmbstr.str();
00070
00071
00072
00073
00074
00075 return ss;
00076 }
00077
00078
00079 template <class T>
00080 safevector<T> concat(safevector<T> const& a, safevector<T> const& b) {
00081 safevector<T> c = a;
00082 c.insert(c.end(), b.begin(), b.end());
00083 return c;
00084 }
00085
00086 safevector<double> interval(double start, double stop, double step = 1.0);
00087 safevector<float> getparm(std::string const& strval);
00088
00089 template <class A, class B>
00090 std::ostream& operator<<(std::ostream& out, std::pair<A,B> const& ab) {
00091 out << '(' << ab.first << ", " << ab.second << ')';
00092 return out;
00093 }
00094
00095 template <typename T>
00096 std::ostream& operator<<(std::ostream& out,
00097 safevector< safevector<T> > const& m) {
00098 out.setf(std::ios::fixed);
00099 out.precision(15);
00100
00101
00102 out << '[';
00103 for(unsigned y=0; y<m.size(); ++y) {
00104 if(y > 0) out << std::endl << ";...\n";
00105
00106 for(int x = 0; x < (int)m[y].size(); ++x) {
00107 if(x > 0) out << ", ";
00108 out << m[y][x];
00109 }
00110 }
00111 out << ']';
00112 return out;
00113 }
00114
00115 template <typename T>
00116 std::istream& operator>>(std::istream& in,
00117 safevector< safevector<T> >& m) throw(std::string) {
00118 char c;
00119 in >> c;
00120 if(in.fail() || c != '[')
00121 throw std::string("Expected '[' as matrix start");
00122
00123 typedef safevector<T> Line;
00124 safevector<Line> lines;
00125 Line line;
00126
00127 for(;;) {
00128 T value;
00129 std::string token;
00130
00131 in >> value >> token;
00132 if(in.fail()) throw std::string("Format error when reading matrix.");
00133
00134 line.push_back(value);
00135 if(token == ";" || token == ";..." ||
00136 token == "]" || token == "];") {
00137 if(lines.size() >= 1 &&
00138 lines[0].size() != line.size())
00139 throw std::string("All matrix rows must have the same size.");
00140
00141 lines.push_back(line);
00142 line = Line();
00143
00144 if(token[0] == ']') break;
00145
00146 } else if(token != ",") {
00147 throw std::string("Invalid matrix element delimiter: ") + token;
00148 }
00149 }
00150 m = lines;
00151 return in;
00152 }
00153
00154 template <class T>
00155 std::ostream& operator<<(std::ostream& out, std::vector<T> const& xx) {
00156 out << '[';
00157 for(unsigned i = 0; i < xx.size(); ++i) {
00158 out << xx[i];
00159 if(i + 1 < xx.size()) out << ", ";
00160 }
00161 out << ']';
00162 return out;
00163 }
00164
00165 #endif