12#ifndef NUMERICAL_COMPLEX_MATH_HPP
13#define NUMERICAL_COMPLEX_MATH_HPP
25#if !defined(NUMERICAL_OUT_OF_LINE)
26# if defined(_MSC_VER) && !defined(__clang__)
27# define NUMERICAL_OUT_OF_LINE __declspec(noinline)
29# define NUMERICAL_OUT_OF_LINE [[gnu::noinline]]
36 template <std::
floating_po
int R>
37 inline constexpr R
exp_bound {
static_cast<R
>(
static_cast<int>(
static_cast<R
>(std::numeric_limits<R>::max_exponent - 1) * std::numbers::ln2_v<R>))};
40 template <std::
floating_po
int R>
41 inline constexpr R
half_exp_bound {
static_cast<R
>(
static_cast<int>(
static_cast<R
>(std::numeric_limits<R>::max_exponent - 1) * std::numbers::ln2_v<R> / R {2}))};
53#elif defined(__APPLE__)
68#elif defined(__APPLE__)
79 template <std::
floating_po
int R>
86 template <std::
floating_po
int R>
99#if !defined(__aarch64__) && !defined(_M_ARM64)
102# if defined(__GLIBC__) && defined(_GLIBCXX_USE_C99_COMPLEX) && _GLIBCXX_USE_C99_COMPLEX
103 static const bool fused {[] {
104 const volatile double re {0.00028503236970950325};
105 const volatile double im {-0.7408643619401744};
106 const std::complex<double> z {re, im};
107 const std::complex<double>
answer {std::tan(z)};
111 const double sinh_y {std::sinh(z.imag())};
125 template <std::
floating_po
int R>
131 const R second {c * d};
132#if defined(__aarch64__) || defined(_M_ARM64)
133 return std::fma(a, b, second);
136 return std::fma(a, b, second);
138 const R first {a * b};
139 return first + second;
144 template <std::
floating_po
int R>
149#if defined(__aarch64__) || defined(_M_ARM64)
150 return std::fma(a, b, s);
153 return std::fma(a, b, s);
162 template <std::
floating_po
int R>
167 if (std::abs(x) > std::numeric_limits<R>::min()) {
179 template <std::
floating_po
int R>
182 constexpr R
inf {std::numeric_limits<R>::infinity()};
183 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
185 const R
y {z.imag()};
186 if (std::isfinite(x)) {
187 if (!std::isfinite(
y)) {
206 return {std::numeric_limits<R>::max() *
cosine, std::numeric_limits<R>::max() *
sine};
208 const R scale {std::exp(x)};
212 if (std::isfinite(
y)) {
213 const R magnitude {std::signbit(x) ? R {0} :
inf};
215 return {magnitude,
y};
217 return {std::copysign(magnitude, std::cos(
y)), std::copysign(magnitude, std::sin(
y))};
219 if (!std::signbit(x)) {
222 return {R {0}, std::copysign(R {0},
y)};
234 template <std::
floating_po
int R>
237 constexpr R
inf {std::numeric_limits<R>::infinity()};
238 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
239 const R x {z.real()};
240 const R
y {z.imag()};
241 if (std::isfinite(x)) {
242 if (!std::isfinite(
y)) {
243 return {
y -
y, x == R {0} ? R {0} :
nan};
249 if (std::abs(x) >
bound) {
252 if (std::signbit(x)) {
264 return {std::numeric_limits<R>::max() *
cosine, std::numeric_limits<R>::max() *
sine};
266 const R scale {std::exp(
rx)};
269 return {std::cosh(x) *
cosine, std::sinh(x) *
sine};
272 if (std::isfinite(
y) &&
y != R {0}) {
273 return {std::copysign(
inf, std::cos(
y)), std::copysign(
inf, std::sin(
y)) * std::copysign(R {1}, x)};
276 return {
inf,
y * std::copysign(R {1}, x)};
287 template <std::
floating_po
int R>
290 constexpr R
inf {std::numeric_limits<R>::infinity()};
291 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
292 const bool negative {std::signbit(z.real())};
293 const R x {std::abs(z.real())};
294 const R
y {z.imag()};
295 if (std::isfinite(x)) {
296 if (!std::isfinite(
y)) {
320 return {std::numeric_limits<R>::max() *
cosine, std::numeric_limits<R>::max() *
sine};
322 const R scale {std::exp(
rx)};
325 return {std::sinh(x) *
cosine, std::cosh(x) *
sine};
328 if (std::isfinite(
y) &&
y != R {0}) {
344 template <std::
floating_po
int R>
347 return complex_cosh(std::complex<R> {-z.imag(), z.real()});
354 template <std::
floating_po
int R>
357 constexpr R
inf {std::numeric_limits<R>::infinity()};
358 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
359 const bool negative {std::signbit(z.real())};
360 const R x {std::abs(z.real())};
361 const R
y {z.imag()};
362 if (std::isfinite(
y)) {
363 if (!std::isfinite(x)) {
376 if (std::abs(
y) >
bound) {
379 if (std::signbit(
y)) {
390 return {std::numeric_limits<R>::max() *
sine, std::numeric_limits<R>::max() *
cosine};
392 const R scale {std::exp(
iy)};
401 if (std::isfinite(x)) {
405 const R re {std::copysign(
inf,
sine)};
407 return {
negative ? -re : re, std::signbit(
y) ? -im : im};
418 template <std::
floating_po
int R>
421 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
422 const R x {z.real()};
423 const R
y {z.imag()};
424 if (!std::isfinite(x) || !std::isfinite(
y)) {
426 if (std::isfinite(
y) && std::abs(
y) > R {1}) {
427 const R
product {std::sin(
y) * std::cos(
y)};
428 return {std::copysign(R {1}, x), std::copysign(R {0},
product)};
430 return {std::copysign(R {1}, x), std::copysign(R {0},
y)};
435 return {x == R {0} ? x :
nan,
nan};
441 if (std::abs(x) >
bound) {
451 im /= std::exp(R {2} *
rx);
453 return {std::copysign(R {1}, x), im};
457 if (std::abs(x) > std::numeric_limits<R>::min()) {
462 if (std::abs(
sinh_x) > std::abs(
cosine) * std::numeric_limits<R>::epsilon()) {
467 return {re /
den, im /
den};
474 template <std::
floating_po
int R>
477 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
478 const R x {z.real()};
479 const R
y {z.imag()};
480 if (!std::isfinite(x) || !std::isfinite(
y)) {
482 if (std::isfinite(x) && std::abs(x) > R {1}) {
483 const R
product {std::sin(x) * std::cos(x)};
484 return {std::copysign(R {0},
product), std::copysign(R {1},
y)};
486 return {std::copysign(R {0}, x), std::copysign(R {1},
y)};
497 if (std::abs(
y) >
bound) {
507 re /= std::exp(R {2} *
iy);
509 return {re, std::copysign(R {1},
y)};
513 if (std::abs(
y) > std::numeric_limits<R>::min()) {
518 if (std::abs(
sinh_y) > std::abs(
cosine) * std::numeric_limits<R>::epsilon()) {
523 return {re /
den, im /
den};
534 std::array<double, 5> terms {};
536 terms[0] = std::fma(x, x, -terms[1]);
538 terms[2] = std::fma(
y,
y, -terms[3]);
541 for (std::size_t i {first + 1}; i < terms.size(); ++i) {
542 const double term {terms[i]};
544 while (j > first && std::abs(terms[j - 1]) > std::abs(
term)) {
545 terms[j] = terms[j - 1];
552 for (std::size_t i {0}; i <= 3; ++i) {
553 const double sum {terms[i + 1] + terms[i]};
554 const double error {(terms[i + 1] -
sum) + terms[i]};
559 double total {terms[4] + terms[3]};
570 const double wide_x {
static_cast<double>(x)};
571 const double wide_y {
static_cast<double>(
y)};
576 template <std::
floating_po
int R>
588 template <std::
floating_po
int R>
591 constexpr R
inf {std::numeric_limits<R>::infinity()};
592 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
593 const R x {z.real()};
594 const R
y {z.imag()};
595 if (x == R {0} &&
y == R {0}) {
596 const R
angle {std::signbit(x) ? std::numbers::pi_v<R> : R {0}};
597 return {-R {1} / std::abs(x), std::copysign(
angle,
y)};
599 if (std::isnan(x) || std::isnan(
y)) {
600 return {std::isinf(x) || std::isinf(
y) ?
inf :
nan,
nan};
602 R larger {std::abs(x)};
603 R smaller {std::abs(
y)};
605 if (larger < smaller) {
606 std::swap(larger, smaller);
608 constexpr R
least {std::numeric_limits<R>::min()};
609 if (larger > std::numeric_limits<R>::max() / R {2}) {
611 larger = std::ldexp(larger, scale);
612 smaller = smaller >=
least * R {2} ? std::ldexp(smaller, scale) : R {0};
614 else if (larger <
least) {
615 scale = std::numeric_limits<R>::digits;
616 larger = std::ldexp(larger, scale);
617 smaller = std::ldexp(smaller, scale);
620 if (larger == R {1}) {
621 const R
square {smaller * smaller};
622 re = std::log1p(
square) / R {2};
624 else if (larger > R {1} && larger < R {2} && smaller < R {1}) {
625 R offset {(larger - R {1}) * (larger + R {1})};
626 if (smaller >= std::numeric_limits<R>::epsilon()) {
629 re = std::log1p(offset) / R {2};
632 const R offset {(larger - R {1}) * (larger + R {1})};
633 re = std::log1p(offset) / R {2};
639 const R
correction {
static_cast<R
>(scale) * std::numbers::ln2_v<R>};
640 re = std::log(std::hypot(larger, smaller)) -
correction;
642 return {re, std::atan2(
y, x)};
653 template <std::
floating_po
int R>
656 constexpr R
inf {std::numeric_limits<R>::infinity()};
657 constexpr R
nan {std::numeric_limits<R>::quiet_NaN()};
658 constexpr R
least {std::numeric_limits<R>::min()};
659 constexpr R half {0.5};
667 return {std::isnan(
y) ?
nan : R {0}, std::copysign(
inf,
y)};
669 return {x, std::isnan(
y) ?
nan : std::copysign(R {0},
y)};
671 if (std::isnan(x) || std::isnan(
y)) {
676 return {R {0}, std::copysign(std::sqrt(-x),
y)};
678 return {std::abs(std::sqrt(x)),
y};
681 const R
part {std::abs(
y) >= R {2} *
least ? std::sqrt(half * std::abs(
y)) : half * std::sqrt(R {2} *std::abs(
y))};
685 if (std::abs(x) > std::numeric_limits<R>::max() / R {4}) {
687 x = std::ldexp(x, -2);
688 y = std::ldexp(
y, -2);
690 else if (std::abs(
y) > std::numeric_limits<R>::max() / R {4}) {
692 x = std::abs(x) >= R {4} *
least ? std::ldexp(x, -2) : R {0};
693 y = std::ldexp(
y, -2);
695 else if (std::abs(x) < R {2} *
least && std::abs(
y) < R {2} *
least) {
696 scale = -((std::numeric_limits<R>::digits + 1) / 2);
697 x = std::ldexp(x, -2 * scale);
698 y = std::ldexp(
y, -2 * scale);
704 re = std::sqrt(half * (
modulus + x));
705 if (scale == 1 && std::abs(
y) < R {1}) {
707 re = std::ldexp(re, scale);
711 im = half * (
y / re);
715 im = std::sqrt(half * (
modulus - x));
716 if (scale == 1 && std::abs(
y) < R {1}) {
717 re = std::abs(
y / im);
718 im = std::ldexp(im, scale);
722 re = std::abs(half * (
y / im));
726 re = std::ldexp(re, scale);
727 im = std::ldexp(im, scale);
729 return {re, std::copysign(im,
y)};
738 template <std::
floating_po
int R>
741 const R x {z.real()};
742 const R
y {z.imag()};
743 const R
half_pi {std::numbers::pi_v<R> / R {2}};
744 const R
eps {std::numeric_limits<R>::epsilon()};
745 const bool x_finite {std::isfinite(x)};
749 return {std::copysign(R {0}, x), std::copysign(
half_pi,
y)};
751 if (std::isinf(x) || x == R {0}) {
752 return {std::copysign(R {0}, x),
y_finite ? std::copysign(
half_pi,
y) : std::numeric_limits<R>::quiet_NaN()};
754 return {std::numeric_limits<R>::quiet_NaN(), std::numeric_limits<R>::quiet_NaN()};
756 if (x == R {0} &&
y == R {0}) {
759 if (std::abs(x) >= R {16} /
eps || std::abs(
y) >= R {16} /
eps) {
761 if (std::abs(
y) <= R {1}) {
764 else if (std::abs(x) <= R {1}) {
768 const R h {std::hypot(x / R {2},
y / R {2})};
769 re = x / h / h / R {4};
771 return {re, std::copysign(
half_pi,
y)};
774 if (std::abs(x) == R {1} && std::abs(
y) <
eps *
eps) {
775 re = std::copysign(R {0.5}, x) * (std::numbers::ln2_v<R> - std::log(std::abs(
y)));
778 const R
i2 {std::abs(
y) >=
eps *
eps ?
y *
y : R {0}};
779 const R plus {R {1} + x};
780 const R minus {R {1} - x};
785 re = R {0.25} *std::log(f);
788 const R
four_x {R {4} *x};
792 const R
absx {std::max(std::abs(x), std::abs(
y))};
793 const R
absy {std::min(std::abs(x), std::abs(
y))};
799 else if (
absx >= R {1} || (
absx < R {0.75} &&
absy < R {0.5})) {
806 return {re, R {0.5} *std::atan2(
twice_y,
den)};
817 template <std::
floating_po
int R>
821 const R
eps {std::numeric_limits<R>::epsilon()};
822 const R
rx {std::abs(z.real())};
823 const R
ix {std::abs(z.imag())};
824 const R
sign {z.imag()};
828 const auto angle {[&](R a, R b) {
return adjust ? std::atan2(a, std::copysign(b,
sign)) : std::atan2(b, a); }};
831 y = {std::copysign(
y.imag(),
sign),
y.real()};
837 re =
res.real() + std::numbers::ln2_v<R>;
840 else if (
rx >= R {0.5} &&
ix <
eps / R {8}) {
841 const R s {std::hypot(R {1},
rx)};
842 re = std::log(
rx + s);
843 im =
adjust ? std::atan2(s, z.imag()) : std::atan2(
ix, s);
845 else if (
rx <
eps / R {8} &&
ix >= R {1.5}) {
846 const R s {std::sqrt((
ix + R {1}) * (
ix - R {1}))};
847 re = std::log(
ix + s);
850 else if (
ix > R {1} &&
ix < R {1.5} &&
rx < R {0.5}) {
851 const R
ix2m1 {(
ix + R {1}) * (
ix - R {1})};
853 const R s {std::sqrt(
ix2m1)};
863 const R
r1 {std::sqrt((
dm +
rx2) / R {2})};
869 else if (
ix == R {1} &&
rx < R {0.5}) {
870 if (
rx <
eps / R {8}) {
871 const R
root {std::sqrt(
rx)};
872 re = std::log1p(R {2} *(
rx +
root)) / R {2};
880 im =
angle(
rx + s1, R {1} + s2);
883 else if (
ix < R {1} &&
rx < R {0.5}) {
885 const R s {std::hypot(R {1},
rx)};
886 re = std::log1p(R {2} *
rx * (
rx + s)) / R {2};
887 im =
adjust ? std::atan2(s, z.imag()) : std::atan2(
ix, s);
891 const R s {std::sqrt(
onemix2)};
892 re = std::log1p(R {2} *
rx / s) / R {2};
893 im =
adjust ? std::atan2(s, z.imag()) : std::atan2(
ix, s);
902 const R
r1 {std::sqrt((
dp +
rx2) / R {2})};
914 return {std::copysign(re, z.real()), std::copysign(im,
adjust ? R {1} : z.imag())};
921 template <std::
floating_po
int R>
924 const R x {z.real()};
925 const R
y {z.imag()};
926 const R
nan {std::numeric_limits<R>::quiet_NaN()};
927 if (!std::isfinite(x) || !std::isfinite(
y)) {
929 const R
quarter {x == R {0} || std::isfinite(x) ? std::numbers::pi_v<R> / R {2} : std::numbers::pi_v<R> / R {4}};
930 return {std::copysign(std::numeric_limits<R>::infinity(), x), std::isnan(x) ?
nan : std::copysign(
quarter,
y)};
932 if (!std::isfinite(x)) {
933 return {x, ((std::isinf(x) && std::isfinite(
y)) ||
y == R {0}) ? std::copysign(R {0},
y) :
nan};
937 if (x == R {0} &&
y == R {0}) {
944 template <std::
floating_po
int R>
947 const R
nan {std::numeric_limits<R>::quiet_NaN()};
948 if (std::isnan(z.real()) || std::isnan(z.imag())) {
949 if (z.real() == R {0}) {
952 if (std::isinf(z.real()) || std::isinf(z.imag())) {
953 return {
nan, std::copysign(std::numeric_limits<R>::infinity(), z.imag())};
957 const std::complex<R>
y {
complex_asinh(std::complex<R> {-z.imag(), z.real()})};
958 return {
y.imag(), -
y.real()};
963 template <std::
floating_po
int R>
966 if (!std::isfinite(z.real()) || !std::isfinite(z.imag()) || (z.real() == R {0} && z.imag() == R {0})) {
968 const R re {(std::numbers::pi_v<R> / R {2}) -
y.real()};
969 return {re == R {0} ? R {0} : re, -
y.imag()};
971 const std::complex<R>
y {
kernel_asinh(std::complex<R> {-z.imag(), z.real()},
true)};
972 return {
y.imag(),
y.real()};
977 template <std::
floating_po
int R>
980 const R x {z.real()};
981 const R
y {z.imag()};
982 const R
nan {std::numeric_limits<R>::quiet_NaN()};
983 const R
inf {std::numeric_limits<R>::infinity()};
984 const R
half_pi {std::numbers::pi_v<R> / R {2}};
985 const R
quarter {std::numbers::pi_v<R> / R {4}};
986 if (!std::isfinite(x) || !std::isfinite(
y)) {
989 return {
inf, std::isnan(x) ?
nan : std::copysign(
angle,
y)};
992 return {
inf, std::isnan(
y) ?
nan : std::copysign(std::signbit(x) ? std::numbers::pi_v<R> : R {0},
y)};
996 if (x == R {0} &&
y == R {0}) {
997 return {R {0}, std::copysign(
half_pi,
y)};
999 const std::complex<R> w {
kernel_asinh(std::complex<R> {-
y, x},
true)};
1000 return std::signbit(
y) ? std::complex<R> {w.real(), -w.imag()} : std::complex<R> {-w.real(), w.imag()};
1009 template <std::
floating_po
int R>
1012 const R x {z.real()};
1013 const R
y {z.imag()};
1014 const R
half_pi {std::numbers::pi_v<R> / R {2}};
1015 const R
eps {std::numeric_limits<R>::epsilon()};
1016 const R
nan {std::numeric_limits<R>::quiet_NaN()};
1017 if (!std::isfinite(x) || !std::isfinite(
y)) {
1018 if (std::isinf(x)) {
1019 return {std::copysign(
half_pi, x), std::copysign(R {0},
y)};
1021 if (std::isinf(
y)) {
1022 return {std::isnan(x) ?
nan : std::copysign(
half_pi, x), std::copysign(R {0},
y)};
1024 return {
nan,
y == R {0} ? std::copysign(R {0},
y) :
nan};
1026 if (x == R {0} &&
y == R {0}) {
1029 if (std::abs(x) >= R {16} /
eps || std::abs(
y) >= R {16} /
eps) {
1031 if (std::abs(x) <= R {1}) {
1034 else if (std::abs(
y) <= R {1}) {
1038 const R h {std::hypot(x / R {2},
y / R {2})};
1039 im =
y / h / h / R {4};
1041 return {std::copysign(
half_pi, x), im};
1043 const R
absx {std::max(std::abs(x), std::abs(
y))};
1044 const R
absy {std::min(std::abs(x), std::abs(
y))};
1050 else if (
absx >= R {1} || (
absx < R {0.75} &&
absy < R {0.5})) {
1057 const R re {R {0.5} *std::atan2(
twice_x,
den)};
1059 if (std::abs(
y) == R {1} && std::abs(x) <
eps *
eps) {
1060 im = std::copysign(R {0.5},
y) * (std::numbers::ln2_v<R> - std::log(std::abs(x)));
1063 const R
r2 {std::abs(x) >=
eps *
eps ? x * x : R {0}};
1064 const R plus {
y + R {1}};
1065 const R minus {
y - R {1}};
1068 const R f {
num / below};
1070 im = R {0.25} *std::log(f);
1074 im = R {0.25} *std::log1p(
four_y / below);
1081 template <std::
floating_po
int R>
1085 return {w.real() * std::numbers::log2e_v<R>, w.imag() * std::numbers::log2e_v<R>};
1089 template <std::
floating_po
int R>
1093 return {w.real() * std::numbers::log10e_v<R>, w.imag() * std::numbers::log10e_v<R>};
1097 template <std::
floating_po
int R>
1100 return complex_exp(std::complex<R> {z.real() * std::numbers::ln2_v<R>, z.imag() * std::numbers::ln2_v<R>});
1109 template <std::
floating_po
int R>
1112 if (z.real() >
exp_bound<R> && std::isfinite(z.imag())) {
1114 return {w.real() - R {1}, w.imag()};
1116 const R a {std::sin(z.imag() / R {2})};
1118 const R half {R {2} *a * a};
1119 return {real - half, std::exp(z.real()) *
separate_sine(z.imag())};
1130 template <std::
floating_po
int R>
1133 const R x {z.real()};
1134 const R
y {z.imag()};
1135 if (x == R {0} &&
y == R {0}) {
1139 if (std::abs(x) <= R {3} && std::abs(
y) <= R {3}) {
1141 const R
xx_err {std::fma(x, x, -
xx)};
1144 const R
twice {R {2} *x};
1148 const R
sum {first +
xx};
1149 const R
cv {
sum - first};
1153 if (d >= R {-0.5}) {
1154 return {std::log1p(d) / R {2}, std::atan2(
y, x + R {1})};
1163 template <std::
floating_po
int R>
1166 const R x {z.real()};
1167 const R
y {z.imag()};
1168 const R
nan {std::numeric_limits<R>::quiet_NaN()};
1169 const R magnitude {std::hypot(x,
y)};
1170 if (std::isnan(magnitude)) {
1173 if (std::isinf(magnitude)) {
1174 if (std::isinf(x)) {
1175 return std::isinf(
y) ? std::complex<R> {
nan,
nan} : std::complex<R> {x > R {0} ? R {1} : R {-1}, R {0}};
1177 return {R {0},
y > R {0} ? R {1} : R {-1}};
1179 if (magnitude == R {0}) {
1180 return {R {0}, R {0}};
1182 return {x / magnitude,
y / magnitude};
#define NUMERICAL_OUT_OF_LINE
What every exception NumericAL throws is, beside its standard base (all but std::bad_alloc and std::s...
std::complex< R > complex_asinh(const std::complex< R > &z)
asinh(z), as glibc's casinh: C's Annex G for the infinities and NaNs, z itself at ±0 ± 0i,...
std::complex< R > complex_sign(const std::complex< R > &z)
z / |z|, as NumPy 2's sign of a complex number and the array API's: 0 for 0, NaN for a NaN modulus,...
constexpr R half_exp_bound
Half of it, past which tanh's parts are ±1 and a quotient by exp(2x): 354 for double,...
R separate_sine(R x)
std::sin of x in a call of its own, as NumPy's formulas take it: no build fuses it with a cos of the ...
R libm_add_product(R s, R a, R b)
s + a·b as the C library's build computes s += a·b: fused where it fuses, as libm_sum_of_products.
std::complex< R > complex_log(const std::complex< R > &z)
log(z): log|z| + i arg(z), log|z| as log1p(|z|² − 1) / 2 near the unit circle (|z|² − 1 exact or almo...
R libm_sum_of_products(R a, R b, R c, R d)
a·b + c·d as the C library's build computes it: fma(a, b, c·d) where it fuses (libm_fuses),...
std::complex< R > complex_sinh(const std::complex< R > &z)
sinh(z): sinh(re) cos(im) + i cosh(re) sin(im) of |re|, the sign of re carried by the cosine; past ex...
constexpr R exp_bound
The real part past which exp of it overflows, rounded down: 709 for double, 88 for float.
std::complex< R > complex_sin(const std::complex< R > &z)
sin(z): sin(re) cosh(im) + i cos(re) sinh(im) of |re|, the sign of re carried by the sine; an imagina...
T square(const T &x)
x · x, by the type's own product (a complex one's C Annex G, as the library's *).
std::complex< R > complex_tan(const std::complex< R > &z)
tan(z): (sin(re) cos(re) + i sinh(im) cosh(im)) / (cos(re)² + sinh(im)²); past half_exp_bound the ima...
std::complex< R > complex_exp2(const std::complex< R > &z)
2^z, as NumPy's: complex_exp of z times ln 2, each part.
std::complex< R > complex_log2(const std::complex< R > &z)
log2(z), as NumPy's: complex_log times log₂ e, each part.
std::complex< R > complex_log1p(const std::complex< R > &z)
log(1 + z): the imaginary part atan2(im, 1 + re), as NumPy's; the real part log|1 + z|,...
std::complex< R > complex_asin(const std::complex< R > &z)
asin(z), as glibc's casin: −i asinh(iz); a NaN part as Annex G has it.
double unit_circle_offset(double x, double y)
x² + y² − 1 for 0.5 ≤ x < 1, y ≤ x and x² + y² ≥ 0.5, almost exactly: each square split into its roun...
R separate_cosine(R x)
std::cos of x in a call of its own, as separate_sine.
std::complex< R > complex_acos(const std::complex< R > &z)
acos(z), as glibc's cacos: π/2 − asin(z) for the infinities, NaNs and zeros, else kernel_asinh of iz ...
std::complex< R > complex_expm1(const std::complex< R > &z)
exp(z) − 1, as NumPy's: expm1(re) cos(im) − 2 sin²(im/2) + i exp(re) sin(im), the sine and the cosine...
std::complex< R > complex_tanh(const std::complex< R > &z)
tanh(z): (sinh(re) cosh(re) + i sin(im) cos(im)) / (sinh(re)² + cos(im)²); past half_exp_bound the re...
std::complex< R > complex_cosh(const std::complex< R > &z)
cosh(z): cosh(re) cos(im) + i sinh(re) sin(im); past exp_bound, exp(|re|) / 2 times the cosine and th...
void small_sincos(R x, R &sine, R &cosine)
sin and cos of x (sine_and_cosine), or x and 1 for one under the smallest normal value (an exact sine...
std::complex< R > complex_exp(const std::complex< R > &z)
exp(z): exp(re) times cos(im) + i sin(im), a real part past exp_bound taken off in steps of it (each ...
bool unit_circle_squares_reach_half(R x, R y)
Whether x² + y² ≥ 0.5, as glibc's build computes the sum (libm_sum_of_products).
std::complex< R > complex_atanh(const std::complex< R > &z)
atanh(z), as glibc's catanh: C's Annex G for the infinities and NaNs; π/2 and the reciprocal past 16/...
void sine_and_cosine(double x, double &sine, double &cosine)
sin and cos of x together, as glibc's complex functions take them: its sincos under glibc (whose sing...
std::complex< R > complex_sqrt(const std::complex< R > &z)
The square root of a complex number, as the GNU C library's csqrt computes it (NumPy's on Linux,...
std::complex< R > kernel_asinh(const std::complex< R > &z, bool adjust)
asinh(z) of a finite nonzero z, as glibc's __kernel_casinh, its imaginary part taken from π/2 where a...
T root(const T &x)
The square root of a scalar, as std::sqrt gives it.
std::complex< R > complex_acosh(const std::complex< R > &z)
acosh(z), as glibc's cacosh: Annex G for the infinities, NaNs and zeros, else kernel_asinh of iz adju...
std::complex< R > complex_log10(const std::complex< R > &z)
log10(z), as NumPy's: complex_log times log₁₀ e, each part.
bool libm_fuses() noexcept
Whether the C library's complex functions fuse their sums of products, a·b + c·d as fma(a,...
std::complex< R > complex_cos(const std::complex< R > &z)
cos(z), as cosh(i z): complex_cosh of −im + i re.
std::complex< R > complex_atan(const std::complex< R > &z)
atan(z), as glibc's catan: C's Annex G for the infinities and NaNs; π/2 and the reciprocal past 16/ε;...
@ fused
std::fma, where every target has the instruction.
T sum(const tensor< T > &t)
The sum of every element, by pairwise halving (0 for an empty tensor), on the threads where they are ...