Bertini 2 d882924 (2026-10-06 19:56 UTC)
Numerical algebraic geometry library (C++ core)
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) 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
27
28#ifndef BERTINI_DOUBLE_EXTENSIONS_HPP
29#define BERTINI_DOUBLE_EXTENSIONS_HPP
30
31#pragma once
32
33#include <random>
35#include <complex>
36
37
38namespace bertini{
39
40// Forward declaration — full definition in random.hpp / random.cpp.
41// Avoids pulling mpfr_complex.hpp into the Boost.Multiprecision include chain.
42std::mt19937& ThreadEngine();
43
44 // The two double-precision scalar types, named to parallel real_mp / complex_mp.
45 // real_dbl is an explicit alias for double so a future change lives in one place.
46 using real_dbl = double;
47 using complex_dbl = std::complex<double>;
48
52 inline
53 std::complex<double> operator*(unsigned i, std::complex<double> z)
54 {
55 z*=i;
56 return z;
57 }
58
62 inline
63 bool isnan(std::complex<double> const& z)
64 {
65 using std::isnan;
66 return isnan(z.real()) || isnan(z.imag());
67 }
68
69
73 inline
74 double RandReal()
75 {
76 // pinned draw (b2rand/1, ADR-0044)
78 }
79
80 namespace{
81 using real_dbl = double;
82 using complex_dbl = std::complex<double>;
83 }
84
92 inline complex_dbl pow(const complex_dbl & z, int power)
93 {
94 if (power < 0) {
95 return pow(1./z, -power);
96 }
97 else if (power==0)
98 return complex_dbl(1,0);
99 else if(power==1)
100 return z;
101 else if(power==2)
102 return z*z;
103 else if(power==3)
104 return z*z*z;
105 else
106 {
107 unsigned int p(static_cast<unsigned int>(power));
108 complex_dbl result(1,0), z_to_the_current_power_of_two = z;
109 // have copy of p in memory, can freely modify it.
110 do {
111 if ( (p & 1) == 1 ) { // get the lowest bit of the number
112 result *= z_to_the_current_power_of_two;
113 }
114 z_to_the_current_power_of_two *= z_to_the_current_power_of_two; // square z_to_the_current_power_of_two
115 } while (p >>= 1);
116
117 return result;
118 }
119 }
120
121} // namespace bertini
122
123
124#endif // include guard
Dependency-free declarations of the pinned draw primitives (ADR-0044).
double DrawSymmetricDouble()
A draw from this thread's pinned stream, uniform on [-1,1].
Definition algorithm_builder.hpp:36
std::complex< double > operator*(unsigned i, std::complex< double > z)
Overload * for unsigned * complex<double>.
Definition double_extensions.hpp:53
bool isnan(std::complex< double > const &z)
an overload of isnan, for std::complex<double>
Definition double_extensions.hpp:63
std::complex< double > complex_dbl
The double-precision complex number type.
Definition double_extensions.hpp:47
double RandReal()
Gets you a random real number between -1 and 1, fwiw.
Definition double_extensions.hpp:74
std::mt19937 & ThreadEngine()
Returns this thread's canonical mt19937 engine.
complex_dbl pow(const complex_dbl &z, int power)
Compute +,- integral powers of a std::complex<double> number.
Definition double_extensions.hpp:92
double real_dbl
The double-precision real number type.
Definition double_extensions.hpp:46