19#ifndef _GIMLI_VECTORTEMPLATES__H
20#define _GIMLI_VECTORTEMPLATES__H
35template <
typename T,
class Iter,
template <
typename,
class >
class Vec >
37IndexArray ids(
const Vec < T, Iter > & e){
38 IndexArray id(e.size());
39 for (Index i = 0; i < e.size(); i ++ )
id[i] = e[i]->
id();
43template <
class ValueType >
bool save(std::vector < ValueType > & a,
const std::string & filename,
IOFormat format = Ascii){
44 return saveVec(a, filename, format);
47template <
class ValueType >
bool load(std::vector < ValueType > & a,
const std::string & filename,
IOFormat format = Ascii,
49 return loadVec(a, filename, format, verbose);
52template <
class Vec >
bool saveVec(
const Vec & a,
const std::string & filename,
53 IOFormat format,
bool verbose =
true){
55 if (filename.rfind(VECTORASCSUFFIX) != std::string::npos) format = Ascii;
56 else if (filename.rfind(VECTORBINSUFFIX) != std::string::npos) format = Binary;
57 std::string fname(filename);
60 if (fname.rfind(
".") == std::string::npos) fname += VECTORASCSUFFIX;
62 std::ofstream file; file.open(fname.c_str());
64 std::cerr << filename <<
": " << strerror(errno) <<
" " << errno << std::endl;
68 file.setf(std::ios::scientific, std::ios::floatfield);
71 for (uint i = 0, imax = a.size(); i < imax; i ++) file << a[i] << std::endl;
74 if (fname.rfind(
".") == std::string::npos) fname += VECTORBINSUFFIX;
79 FILE *file; file = fopen(fname.c_str(),
"w+b");
81 if (verbose) std::cerr << filename <<
": " << strerror(errno) <<
" " << errno << std::endl;
86 uint ret = 0; ret = fwrite((
char*)&count,
sizeof(
int), 1, file);
88 for (uint i = 0; i < a.size(); i++) ret = fwrite((
char*)&a[i],
sizeof(
double), 1, file);
95template <
class Vec >
bool loadVec(Vec & a,
const std::string & filename,
96 IOFormat format,
bool verbose =
true){
98 if (filename.rfind(VECTORASCSUFFIX) != std::string::npos) format = Ascii;
99 else if (filename.rfind(VECTORBINSUFFIX) != std::string::npos) format = Binary;
101 if (!fileExist(filename)){
102 if (fileExist(filename + VECTORBINSUFFIX))
103 return loadVec(a, filename + VECTORBINSUFFIX, Binary);
104 if (fileExist(filename + VECTORASCSUFFIX))
105 return loadVec(a, filename + VECTORASCSUFFIX, Ascii);
108 if (format == Ascii){
111 std::fstream file; openInFile(filename.c_str(), &file);
112 double val;
while(file >> val) a.push_back(val);
129 file = fopen(filename.c_str(),
"r+b");
131 if (verbose) std::cerr << filename <<
": " << strerror(errno) <<
" " << errno << std::endl;
135 int size; ret = fread(&size,
sizeof(
int), 1, file);
138 ret = fread(&a[0],
sizeof(
double), size, file);
149template <
class T > std::vector< T > sort(
const std::vector < T > & a){
150 std::vector < T > t(a);
151 sort(t.begin(), t.end());
155template <
class T > std::vector< T >
unique(
const std::vector < T > & a){
157 std::unique_copy(a.begin(), a.end(), back_inserter(t));
169template <
class Vec >
void clear(Vec & a) {
170 for (
int i = 0; i < (int)a.size(); i ++) a[i] = 0.0;
173template <
typename T,
class Iter,
template <
typename,
class >
class Vec >
174T min(
const Vec< T, Iter > & v){
175 return *std::min_element(&v[0], &v[0] + v.size());
178template <
typename T,
class Iter,
template <
typename,
class >
class Vec >
179T max(
const Vec< T, Iter > & v){
180 return *std::max_element(v.begin(), v.end());
194template <
class Vec >
void echoMinMax(
const Vec & vec,
const std::string & name){
196 std::cout <<
"min " << name <<
" = " << min(vec)
197 <<
" max " << name <<
" = " << max(vec) <<
" (" << vec.size() <<
")" << std::endl;
199 std::cout <<
"min " << name <<
" = ndef."
200 <<
" max " << name <<
" = ndef." << std::endl;
204template <
class Vec >
double median(
const Vec & a) {
205 Index dim = a.size();
206 if (dim == 1)
return a[0];
209 if (std::fabs(dim / 2.0 - rint(dim / 2.0)) < 1e-12){
210 return (tmp[dim / 2] + tmp[dim / 2 - 1]) / 2.0;
212 return tmp[(dim - 1)/ 2];
224template <
class Vec >
double arithmeticMean(
const Vec & a) {
return mean(a); }
226template <
class Vec >
double geometricMean(
const Vec & a) {
229 for (
int i = 0; i < dim; i ++) result += std::log(a[i]);
230 result /= (double)dim;
231 return std::exp(result);
234template <
class Vec >
double harmonicMean(
const Vec & a) {
236 double result = 1.0 / a[0];
238 for (
int i = 1; i < dim; i ++) result += 1.0 / a[i];
243template <
class Vec >
double rms(
const Vec & a) {
return std::sqrt(mean(square(a))); }
245template <
class Vec >
double rms(
const Vec & a,
const Vec & b) {
return rms(a - b); }
247template <
class Vec >
double rrms(
const Vec & a,
const Vec & b) {
return rms((a - b) / a); }
249template <
class Vec >
double normlp(
const Vec & a,
int p) {
250 return std::pow(sum(pow(abs(a), p)), 1.0/(
double)p);
253template <
class Vec >
double norml1(
const Vec & a) {
258template <
class Vec >
double norml2(
const Vec & a) {
262template <
class Vec >
double normlInfinity(
const Vec & a) {
265template <
class Vec >
double euclideanNorm(
const Vec & a) {
269template <
class Vec >
double norm(
const Vec & a) {
274template <
class Vec >
double chiQuad(
const Vec & a,
const Vec & b,
const Vec & err) {
275 Vec tmp((a - b) / err);
276 return dot(tmp, tmp) / a.size();
279template <
class Vec >
double chiQuad(
const Vec & a,
const Vec & b,
const Vec & err,
bool isLog) {
285 tmp = ((log(b) - log(a)) / log(err + 1.0));
286 chiq = mean(tmp * tmp);
288 chiq = mean(((b / a - 1.0) * (b / a - 1.0)) / (err * err));
293template <
class ValueType >
294void rand(Vector < ValueType > & vec, ValueType min = 0.0, ValueType max = 1.0){
295 for (
int i = 0, imax = vec.size(); i < imax; i ++){
296 vec[i] = (ValueType)std::rand() * ((max -min)/ RAND_MAX) + min;
300template <
class ValueType >
304 for (uint i = 0, imax = vec.size(); i < imax; i ++){
306 for (
int j = 0; j < 16; j++) sum += std::rand() & 0xfff;
307 vec[i] = (sum - 0x8000) * (1.0 / 4729.7);
312inline RVector randn(Index n){
One dimensional array aka Vector of limited size.
Definition vector.h:186
GIMLi main namespace for the Geophyiscal Inversion and Modelling Library.
Definition baseentity.h:24
bool loadVec(Vector< ValueType > &a, const std::string &filename, IOFormat format=Ascii)
Definition vector.h:1925
Vector< T > unique(const Vector< T > &a)
Definition vector.h:1604
bool saveVec(const Vector< ValueType > &a, const std::string &filename, IOFormat format=Ascii)
Definition vector.h:1916
IOFormat
Definition gimli.h:289
bool load(Matrix< ValueType > &A, const std::string &filename)
Definition matrix.h:828