forked from melizalab/znote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.hh
99 lines (89 loc) · 2.49 KB
/
common.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#ifndef COMMON_H
#define COMMON_H
/**
* @file common.hh
* @author Daniel Meliza <[email protected]>
* @date Mon Mar 1 13:33:47 2010
*
* @brief Define common types and functions for znote
*
* Copyright C Daniel Meliza, Z Chi 2010. Licensed for use under Creative
* Commons Attribution-Noncommercial-Share Alike 3.0 United States
* License (http://creativecommons.org/licenses/by-nc-sa/3.0/us/).
*/
#include <blitz/array.h>
#include <cmath>
#include <complex>
#include <vector>
#include <cstring>
#ifndef M_PI
#define M_PI 3.14159265358979323846264338327
#endif
// try to determine whether this is blitz 0.9. probably very fragile
#define BZ_MAJOR_VERSION 0
#ifndef BZ_ET_FORWARD_H
#define BZ_MINOR_VERSION 9
#else
#define BZ_MINOR_VERSION 10
#endif
typedef blitz::Array<double,2> dmatrix;
typedef blitz::Array<double,1> dvector;
typedef blitz::Array<int,1> ivector;
typedef blitz::Array<int,2> imatrix;
typedef blitz::Array<std::complex<double>,1> cvector;
typedef blitz::Array<std::complex<double>,2> cmatrix;
typedef blitz::TinyVector<int,2> coord;
typedef blitz::Array<coord,1> coord_list;
typedef std::vector<coord> coord_vector;
typedef std::vector<coord_list> clist_vector;
typedef std::vector<coord_vector> cvec_vector;
/**
* Determine if coordinates are in range for an array.
*
* @param A Array to check
* @param I Indices to check
*
* @return true if A.lbound(i) <= I(i) <= A.ubound(i) for i in [0,N_rank)
*/
template <typename T, int N_rank> inline
bool inrange(const blitz::Array<T,N_rank> &A, const blitz::TinyVector<int,N_rank> &I)
{
for (int i = 0; i < N_rank; i++)
if ((I(i) < A.lbound(i)) || (I(i) > A.ubound(i))) return false;
return true;
}
/**
* Construct an evenly spaced vector across a range.
*
* @param output Output vector. Resized and overwritten.
* @param start Start point
* @param stop Stop point (exclusive)
* @param step Step size
*/
template <typename T> inline
void arange(blitz::Array<T,1> &output, int start, int stop, int step=1)
{
blitz::firstIndex i;
int n = (stop - start) / step;
output.resize(n);
output = start + i*step;
}
/**
* Split a file name into root and extension.
*
* @param in input string
* @param froot file name root
* @param ext file name extension
*
* @return split point
*/
inline
size_t splitext(const std::string &in, std::string &froot, std::string &ext) {
size_t fidx = in.rfind(".");
if (fidx==std::string::npos)
return std::string::npos;
froot.assign(in.substr(0,fidx));
ext.assign(in.substr(fidx));
return fidx;
}
#endif