Hex  1.0
Hydrogen-electron collision solver
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
Data Structures | Typedefs | Functions
arrays.h File Reference
#include <cassert>
#include <complex>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <iomanip>
#include <map>
#include <numeric>
#include <typeinfo>
#include <type_traits>
#include <vector>
#include "hdffile.h"
#include "complex.h"
#include "misc.h"
#include "arrithm.h"
Include dependency graph for arrays.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

class  PlainAllocator< T >
 Basic memory allocator. More...
 
class  Array< T, Alloc >
 A comfortable data array class. More...
 
class  NumberArray< T, Alloc >
 A comfortable number array class. More...
 
class  ArrayView< T >
 Array view. More...
 
class  Array< T, Alloc >
 A comfortable data array class. More...
 
class  NumberArray< T, Alloc >
 A comfortable number array class. More...
 

Typedefs

typedef NumberArray< int > iArray
 
typedef NumberArray< long > lArray
 
typedef NumberArray< double > rArray
 
typedef NumberArray< ComplexcArray
 
typedef ArrayView< int > iArrayView
 
typedef ArrayView< long > lArrayView
 
typedef ArrayView< double > rArrayView
 
typedef ArrayView< ComplexcArrayView
 
typedef Array< iArrayiArrays
 
typedef Array< lArraylArrays
 
typedef Array< rArrayrArrays
 
typedef Array< cArraycArrays
 

Functions

template<class T >
operator| (const ArrayView< T > a, const ArrayView< T > b)
 Scalar product of two arrays. More...
 
template<class T1 , class T2 >
auto outer_product (const ArrayView< T1 > a, const ArrayView< T2 > b) -> NumberArray< decltype(T1(0)*T2(0))>
 Outer product of two arrays. More...
 
template<typename T >
std::ostream & operator<< (std::ostream &out, ArrayView< T > const &a)
 Output to text stream. More...
 
template<typename T >
NumberArray< T > linspace (T start, T end, unsigned samples)
 Generate uniform grid. More...
 
template<typename T >
NumberArray< T > logspace (T x0, T x1, size_t samples)
 Generate logarithmic grid. More...
 
template<typename T >
NumberArray< T > geomspace (T x0, T x1, size_t samples, double q)
 Generate geometric grid. More...
 
template<class T >
void write_array (const ArrayView< T > array, const char *filename)
 
template<class T1 , class T2 >
void write_array (const ArrayView< T1 > grid, const ArrayView< T2 > array, const char *filename)
 
template<typename Fetcher >
bool write_1D_data (size_t m, const char *filename, Fetcher fetch)
 Write elements. More...
 
template<class Fetcher >
bool write_2D_data (size_t m, size_t n, const char *filename, Fetcher fetch)
 
rArray concatenate ()
 
template<typename... Params>
rArray concatenate (rArray const &v1, Params...p)
 
rArray abs (const cArrayView u)
 
rArrays abs (cArrays const &u)
 
template<typename T >
min (const ArrayView< T > a)
 Minimal element of array. More...
 
template<typename T >
max (const ArrayView< T > a)
 Maximal element of array. More...
 
template<typename T >
NumberArray< T > pow (NumberArray< T > const &u, double e)
 Return per-element power. More...
 
template<typename T >
Array< T > pow (Array< T > const &u, double e)
 Return per-element power. More...
 
template<class T >
NumberArray< T > sqrt (NumberArray< T > const &A)
 Return per-element square root. More...
 
template<class T >
NumberArray< T > sin (NumberArray< T > const &A)
 Return per-element sine. More...
 
template<class T >
NumberArray< T > asin (NumberArray< T > const &A)
 Return per-element arcsine. More...
 
template<class T >
NumberArray< T > cos (NumberArray< T > const &A)
 Return per-element cosine. More...
 
NumberArray< double > hypot (NumberArray< double > const &A, NumberArray< double > const &B)
 Return per-element hypot. More...
 
NumberArray< double > atan2 (NumberArray< double > const &A, NumberArray< double > const &B)
 Return per-element atan2. More...
 
NumberArray< double > sqrabs (NumberArray< Complex > const &A)
 Return per-element square of absolute value. More...
 
NumberArray< double > realpart (NumberArray< Complex > const &A)
 Return per-element real part. More...
 
NumberArray< double > imagpart (NumberArray< Complex > const &A)
 Return per-element imag part. More...
 
template<typename T >
sum (const ArrayView< T > v)
 Sum elements in array. More...
 
template<typename T >
NumberArray< T > sums (const ArrayView< NumberArray< T >> v)
 Sum arrays. More...
 
template<typename T >
Array< bool > operator== (const ArrayView< T > u, T x)
 
template<typename T >
Array< bool > operator== (const ArrayView< T > u, const ArrayView< T > v)
 Comparison of two arrays. More...
 
bool all (const ArrayView< bool > B)
 Check that all values are "true". More...
 
bool any (const ArrayView< bool > B)
 Check if any value is "true". More...
 
template<typename TFunctor , typename TArray >
void eval (TFunctor f, TArray grid, TArray &vals)
 
template<typename Tidx , typename Tval >
void merge (NumberArray< Tidx > &idx1, NumberArray< Tval > &arr1, NumberArray< Tidx > const &idx2, NumberArray< Tval > const &arr2)
 
template<typename T >
NumberArray< T > join (const ArrayView< NumberArray< T >> arrays)
 Join elements from all subarrays. More...
 
template<class T >
NumberArray< T > sorted_unique (const ArrayView< T > v, int n=1)
 Drop all redundant repetitions from sorted array. More...
 
template<class T >
void smoothen (ArrayView< T > v)
 Running average. More...
 
template<class T >
std::string to_string (const ArrayView< T > v, char sep= ' ')
 Convert ArrayView<T> to a string. More...
 
rArray threshold (const rArrayView a, double eps)
 Drop small elements of array (replace by zero). More...
 

Typedef Documentation

typedef Array<cArray> cArrays
typedef NumberArray<int> iArray
typedef Array<iArray> iArrays
typedef ArrayView<int> iArrayView
typedef NumberArray<long> lArray
typedef Array<lArray> lArrays
typedef ArrayView<long> lArrayView
typedef NumberArray<double> rArray
typedef Array<rArray> rArrays
typedef ArrayView<double> rArrayView

Function Documentation

rArray abs ( const cArrayView  u)
rArrays abs ( cArrays const &  u)
bool all ( const ArrayView< bool >  B)
inline
bool any ( const ArrayView< bool >  B)
inline
template<class T >
NumberArray<T> asin ( NumberArray< T > const &  A)
NumberArray<double> atan2 ( NumberArray< double > const &  A,
NumberArray< double > const &  B 
)
rArray concatenate ( )
inline

Variadic template recurrence starter. For documentation of the function itself see the other "concatenate".

template<typename... Params>
rArray concatenate ( rArray const &  v1,
Params...  p 
)

Concatenate several arrays. The function template uses variadic templates feature of C++, so the number of subarrays to concatenate is completely arbitrary. It should be noted, though, that long concatenation list may slow down the template instantiation during compilation and hence the overall compilation time.

Parameters
v1First array.
...pAll other arrays.
template<class T >
NumberArray<T> cos ( NumberArray< T > const &  A)
template<typename TFunctor , typename TArray >
void eval ( TFunctor  f,
TArray  grid,
TArray &  vals 
)

Evaluation of a function over a grid.

Parameters
fFunctor to be evaluated using the operator() (double) interface.
gridArray-like type containing the evaluation points.
valsArray-like type to hold evaluated function on return. It is required that enough space is reserved, at least grid.size().
template<typename T >
NumberArray<T> geomspace ( x0,
x1,
size_t  samples,
double  q 
)

Return a geometrically increasing array

\[ a_1, a_2, a_3, \dots, a_n, \]

where \( a_1 \) is equal to "start", \( a_n \) is equal to "end" and \( n \) is equal to "samples". For consecutive elements it holds

\[ \frac{a_{k+1} - a_{k}}{a_k - a_{k-1}} = q \ . \]

Altogether is

\[ a_k = a_1 + (a_n - a_1) \frac{1-q^{k-1}}{1-q^{n-1}} \]

NumberArray<double> hypot ( NumberArray< double > const &  A,
NumberArray< double > const &  B 
)
NumberArray<double> imagpart ( NumberArray< Complex > const &  A)
template<typename T >
NumberArray<T> join ( const ArrayView< NumberArray< T >>  arrays)
template<typename T >
NumberArray<T> linspace ( start,
end,
unsigned  samples 
)

Return a uniform array

\[ a_1, a_2, a_3, \dots, a_n, \]

where \( a_1 \) is equal to "start", \( a_n \) is equal to "end" and \( n \) is equal to "samples". For consecutive elements it holds

\[ a_{k+1} - a_k = \frac{a_n - a_1}{n - 1} \ . \]

Parameters
startLeft boundary and first sample for "samples" > 0.
endRight boundary and last sample for "samples" > 1.
samplesSample count.
template<typename T >
NumberArray<T> logspace ( x0,
x1,
size_t  samples 
)

Return a uniformly diverging array

\[ a_1, a_2, a_3, \dots, a_n, \]

where \( a_1 \) is equal to "start", \( a_n \) is equal to "end" and \( n \) is equal to "samples". For consecutive elements it holds

\[ \frac{a_{k+1}}{a_k} = q = \left(\frac{a_n}{a_1}\right)^{1/(n-1)} \ . \]

Parameters
x0Left boundary and first sample for "samples" > 0.
x1Right boundary and last sample for "samples" > 1.
samplesSample count.
template<typename T >
T max ( const ArrayView< T >  a)
template<typename Tidx , typename Tval >
void merge ( NumberArray< Tidx > &  idx1,
NumberArray< Tval > &  arr1,
NumberArray< Tidx > const &  idx2,
NumberArray< Tval > const &  arr2 
)

Sum two indexed arrays if their (sorted) indices perfectly match. Or, generally, create output array with elements that are sum of corresponding elements of both arrays or equal to a single element in one array, if that its index doesn't have a counterpart in the other array.

Parameters
idx1Sorted (!) indices of the first array.
arr1Merge TO array.
idx2Sorted (!) indices of the second array.
arr2Merge FROM array.
template<typename T >
T min ( const ArrayView< T >  a)
template<typename T >
std::ostream& operator<< ( std::ostream &  out,
ArrayView< T > const &  a 
)
template<typename T >
Array<bool> operator== ( const ArrayView< T >  u,
x 
)

Comparison of an array and a number.

Parameters
uArray.
xNumber.
Returns
Vector of bools for element-wise comparisons.
template<typename T >
Array<bool> operator== ( const ArrayView< T >  u,
const ArrayView< T >  v 
)
template<class T >
T operator| ( const ArrayView< T >  a,
const ArrayView< T >  b 
)
template<class T1 , class T2 >
auto outer_product ( const ArrayView< T1 >  a,
const ArrayView< T2 >  b 
) -> NumberArray<decltype(T1(0) * T2(0))>

Returns a new array with the following values:

\[ a_1 b_1, a_1 b_2, \dots, a_1, b_n, a_2 b_1, \dots, a_m b_n \]

template<typename T >
NumberArray<T> pow ( NumberArray< T > const &  u,
double  e 
)
template<typename T >
Array<T> pow ( Array< T > const &  u,
double  e 
)
NumberArray<double> realpart ( NumberArray< Complex > const &  A)
template<class T >
NumberArray<T> sin ( NumberArray< T > const &  A)
template<class T >
void smoothen ( ArrayView< T >  v)

Overwrite the given vector v = {v1, v2, v3, ..., vN} by vector {v1, (v1+v2)/2, (v2+v3)/2, ..., (v_(N-1)+vN)/2}.

template<class T >
NumberArray<T> sorted_unique ( const ArrayView< T >  v,
int  n = 1 
)
NumberArray<double> sqrabs ( NumberArray< Complex > const &  A)
template<class T >
NumberArray<T> sqrt ( NumberArray< T > const &  A)
template<typename T >
T sum ( const ArrayView< T >  v)
template<typename T >
NumberArray<T> sums ( const ArrayView< NumberArray< T >>  v)
rArray threshold ( const rArrayView  a,
double  eps 
)
template<class T >
std::string to_string ( const ArrayView< T >  v,
char  sep = ' ' 
)
template<typename Fetcher >
bool write_1D_data ( size_t  m,
const char *  filename,
Fetcher  fetch 
)

For all integers from 0 to m-1 call fetch(i) and save the results to a text file as a single column.

template<class Fetcher >
bool write_2D_data ( size_t  m,
size_t  n,
const char *  filename,
Fetcher  fetch 
)

Write 2D data to file. To allow maximum flexibility, only extensions of the data are passed to the function and a functor that will be repeatedly called with coordinate pair for new data element.

Parameters
mRow count.
nColumn count.
filenameFilename of the file to create/overwrite.
fetchFunctor with interface
double operator() (size_t, size_t);
Returns
Write success indicator (false for failure).
template<class T >
void write_array ( const ArrayView< T >  array,
const char *  filename 
)

Write array to file. Array will be written as a single column into an ASCII file.

Parameters
arrayThe array to write.
filenameName of the file to create/overwrite.
template<class T1 , class T2 >
void write_array ( const ArrayView< T1 >  grid,
const ArrayView< T2 >  array,
const char *  filename 
)

Write array to file. Array will be written as a two columns into an ASCII file, first column contains grid information.

Parameters
gridThe 1D grid labels for the data.
arrayThe array to write.
filenameName of the file to create/overwrite.