Bertini 2 - C++ Core
Software for Numerical Algebraic Geometry
Loading...
Searching...
No Matches
double_extensions.hpp
Go to the documentation of this file.
1//This file is part of Bertini 2.
2//
3//bertini2/double_extensions.hpp is free software: you can redistribute it and/or modify
4//it under the terms of the GNU General Public License as published by
5//the Free Software Foundation, either version 3 of the License, or
6//(at your option) any later version.
7//
8//bertini2/double_extensions.hpp is distributed in the hope that it will be useful,
9//but WITHOUT ANY WARRANTY; without even the implied warranty of
10//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11//GNU General Public License for more details.
12//
13//You should have received a copy of the GNU General Public License
14//along with bertini2/double_extensions.hpp. If not, see <http://www.gnu.org/licenses/>.
15//
16// Copyright(C) 2015 - 2017 by Bertini2 Development Team
17//
18// See <http://www.gnu.org/licenses/> for a copy of the license,
19// as well as COPYING. Bertini2 is provided with permitted
20// additional terms in the b2/licenses/ directory.
21
22// individual authors of this file include:
23// silviana amethyst, University of Wisconsin Eau Claire
24
30
31#ifndef BERTINI_DOUBLE_EXTENSIONS_HPP
32#define BERTINI_DOUBLE_EXTENSIONS_HPP
33
34#pragma once
35
36#include <random>
37#include <complex>
38
39
40namespace bertini{
41
42 using dbl = std::complex<double>;
43 using dbl_complex = std::complex<double>;
44
48 inline
49 std::complex<double> operator*(unsigned i, std::complex<double> z)
50 {
51 z*=i;
52 return z;
53 }
54
58 inline
59 bool isnan(std::complex<double> const& z)
60 {
61 using std::isnan;
62 return isnan(z.real()) || isnan(z.imag());
63 }
64
65
69 inline
70 double RandReal()
71 {
72 static std::default_random_engine generator;
73 static std::uniform_real_distribution<double> distribution(-1.0,1.0);
74 return distribution(generator);
75 }
76
77 namespace{
78 using dbl = std::complex<double>;
79 }
80
88 inline dbl pow(const dbl & z, int power)
89 {
90 if (power < 0) {
91 return pow(1./z, -power);
92 }
93 else if (power==0)
94 return dbl(1,0);
95 else if(power==1)
96 return z;
97 else if(power==2)
98 return z*z;
99 else if(power==3)
100 return z*z*z;
101 else
102 {
103 unsigned int p(power);
104 dbl result(1,0), z_to_the_current_power_of_two = z;
105 // have copy of p in memory, can freely modify it.
106 do {
107 if ( (p & 1) == 1 ) { // get the lowest bit of the number
108 result *= z_to_the_current_power_of_two;
109 }
110 z_to_the_current_power_of_two *= z_to_the_current_power_of_two; // square z_to_the_current_power_of_two
111 } while (p >>= 1);
112
113 return result;
114 }
115 }
116
117} // namespace bertini
118
119
120#endif // include guard
121
std::complex< double > operator*(unsigned i, std::complex< double > z)
Overload * for unsigned * complex<double>
Definition double_extensions.hpp:49
bool isnan(std::complex< double > const &z)
Definition double_extensions.hpp:59
double RandReal()
Gets you a random real number between -1 and 1, fwiw.
Definition double_extensions.hpp:70
dbl pow(const dbl &z, int power)
Definition double_extensions.hpp:88