Ensign
Low-rank framework for multi-core CPUs and GPUs
Loading...
Searching...
No Matches
utility.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <generic/common.hpp>
9
10namespace Ensign {
11
12// product of an array of numbers
13template<class T, size_t d>
14T prod(array<T,d> a) {
15 T p=1;
16 for(size_t i=0;i<d;i++)
17 p*=a[i];
18 return p;
19}
20
21/* Computes (exp(z)-1)/z
22*
23* There is a removeable singularity at z=0.
24*/
25template<class T>
26T phi1(T a) {
27 return (abs(a) < 1e-7) ? complex<double>(1.0 + a.real(), a.imag()) : exp(a/2.0)*sinh(a/2.0)/(a/2.0);
28}
29
30/* Computes (exp(i a)-1)/(i a)
31*
32* There is a removeable singularity at a=0.
33*/
34template<class T>
35T phi1_im(T a) { // use it only for purely imaginary
36 if(abs(a.imag()) < 1e-7){
37 return complex<double>(1.0+a.imag(),a.imag());
38 }else{
39 return complex<double>(sin(a.imag())/a.imag(),2*pow(sin(a.imag()/2.0),2)/a.imag());
40 }
41}
42
43/* Computes (exp(i a)-1-i a)/((i a)^2)
44*
45* There is a removeable singularity at a=0.
46*/
47template<class T>
48T phi2_im(T a) { // use it only for purely imaginary
49 if(abs(a.imag()) < 1e-7){
50 return complex<double>(0.5+a.imag(),a.imag());
51 }else{
52 return complex<double>(2*pow(sin(a.imag()/2.0),2)/pow(a.imag(),2),(a.imag()-sin(a.imag()))/pow(a.imag(),2));
53 }
54}
55
56#ifdef __CUDA__
57/* Typesafe wrapper for cudaMalloc
58*/
59void* gpu_malloc(size_t size);
60#endif
61
62
63/* Parser for command line arguments separated by whitespaces
64*/
65template<size_t d>
66array<Index,d> parse(string s) {
67 array<Index,d> out;
68 std::istringstream iss(s);
69 for(size_t i=0;i<d;i++) {
70 if(iss.eof()) {
71 cout << "ERROR: not enough dof provided to parse" << endl;
72 exit(1);
73 }
74 iss >> out[i];
75 }
76
77 if(!iss.eof()) {
78 cout << "ERROR: to many dof provided to parse" << endl;
79 exit(1);
80 }
81 return out;
82}
83
84/* Maximum function which treats NaNs correctly
85*/
86template<class T>
87double max_err(T err, T diff) {
88 if(std::isnan(diff))
89 return std::numeric_limits<T>::infinity();
90 else
91 return max(err, diff);
92}
93
94template <class InputIt, class OutputIt>
95void remove_element(InputIt first, InputIt last, OutputIt d_first, const Index idx)
96{
97 std::copy(first, first + idx, d_first);
98 std::copy(first + idx + 1, last, d_first + idx);
99}
100
101} // namespace Ensign
Provides shared types, platform headers, and basic definitions.
ptrdiff_t Index
Definition common.hpp:83
Core namespace for Ensign's numerical algorithms and data structures.
Definition common.hpp:96
array< Index, d > parse(string s)
Parser for command line arguments separated by whitespaces.
Definition utility.hpp:66
void remove_element(InputIt first, InputIt last, OutputIt d_first, const Index idx)
Definition utility.hpp:95
T phi1_im(T a)
Computes (exp(i a)-1)/(i a)
Definition utility.hpp:35
T prod(array< T, d > a)
Definition utility.hpp:14
double max_err(T err, T diff)
Maximum function which treats NaNs correctly.
Definition utility.hpp:87
T phi1(T a)
Computes (exp(z)-1)/z.
Definition utility.hpp:26
T phi2_im(T a)
Computes (exp(i a)-1-i a)/((i a)^2)
Definition utility.hpp:48