1

2# -*- coding: utf-8 -*-

3

4 u'''Utilities for precision floating point summation, multiplication,

5 C{fused-multiply-add}, polynomials, roots, etc.

6'''

7# make sure int/int division yields float quotient, see .basics

8 from __future__ import division as _; del _ # PYCHOK semicolon

9

10 from pygeodesy.basics import _copysign, copysign0, isbool, isint, isscalar, \

11 len2, map1, _xiterable, typename

12 from pygeodesy.constants import EPS0, EPS02, EPS1, NAN, PI, PI_2, PI_4, \

13 _0_0, _0_125, _1_6th, _0_25, _1_3rd, _0_5, _1_0, \

14 _1_5, _copysign_0_0, isfinite, remainder

15 from pygeodesy.errors import _IsnotError, LenError, _TypeError, _ValueError, \

16 _xError, _xkwds, _xkwds_pop2, _xsError

17 from pygeodesy.fsums import _2float, Fsum, fsum, _isFsum_2Tuple, Fmt, unstr

18# from pygeodesy.internals import typename # from .basics

19 from pygeodesy.interns import MISSING, _negative_, _not_scalar_

20 from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS

21# from pygeodesy.streprs import Fmt, unstr # from .fsums

22 from pygeodesy.units import Int_, _isHeight, _isRadius, Float_ # PYCHOK for .heights

23

24 from math import fabs, sqrt # pow

25 import operator as _operator # in .datums, .trf, .utm

26

27__all__ = _ALL_LAZY.fmath

28__version__ = '25.04.30'

29

30# sqrt(2) - 1 <https://WikiPedia.org/wiki/Square_root_of_2>

31_0_4142 = 0.41421356237309504880 # ~ 3_730_904_090_310_553 / 9_007_199_254_740_992

32_2_3rd = _1_3rd * 2

33_h_lt_b_ = 'abs(h) < abs(b)'

34

35

36 class Fdot(Fsum):

37 '''Precision dot product.

38 '''

39 def __init__(self, a, *b, **start_name_f2product_nonfinites_RESIDUAL):

40 '''New L{Fdot} precision dot product M{sum(a[i] * b[i] for i=0..len(a)-1)}.

41

42 @arg a: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

43 @arg b: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all

44 positional.

45 @kwarg start_name_f2product_nonfinites_RESIDUAL: Optional bias C{B{start}=0}

46 (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), C{B{name}=NN} (C{str})

47 and other settings, see class L{Fsum<Fsum.__init__>}.

48

49 @raise LenError: Unequal C{len(B{a})} and C{len(B{b})}.

50

51 @raise OverflowError: Partial C{2sum} overflow.

52

53 @raise TypeError: Invalid B{C{x}}.

54

55 @raise ValueError: Non-finite B{C{x}}.

56

57 @see: Function L{fdot} and method L{Fsum.fadd}.

58 '''

59 s, kwds = _xkwds_pop2(start_name_f2product_nonfinites_RESIDUAL, start=_0_0)

60 Fsum.__init__(self, **kwds)

61 self(s)

62

63 n = len(b)

64 if len(a) != n: # PYCHOK no cover

65 raise LenError(Fdot, a=len(a), b=n)

66 self._facc_dot(n, a, b, **kwds)

67

68

69 class Fdot_(Fdot):

70 '''Precision dot product.

71 '''

72 def __init__(self, *xys, **start_name_f2product_nonfinites_RESIDUAL):

73 '''New L{Fdot_} precision dot product M{sum(xys[i] * xys[i+1] for i in

74 range(0, len(xys), B{2}))}.

75

76 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),

77 all positional.

78

79 @see: Class L{Fdot<Fdot.__init__>} for further details.

80 '''

81 Fdot.__init__(self, xys[0::2], *xys[1::2], **start_name_f2product_nonfinites_RESIDUAL)

82

83

84 class Fhorner(Fsum):

85 '''Precision polynomial evaluation using the Horner form.

86 '''

87 def __init__(self, x, *cs, **incx_name_f2product_nonfinites_RESIDUAL):

88 '''New L{Fhorner} form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)}

89 with in- or decreasing exponent M{sum(... i=n..0)}, where C{n = len(cs) - 1}.

90

91 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

92 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),

93 all positional.

94 @kwarg incx_name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str}),

95 C{B{incx}=True} for in-/decreasing exponents (C{bool}) and other

96 settings, see class L{Fsum<Fsum.__init__>}.

97

98 @raise OverflowError: Partial C{2sum} overflow.

99

100 @raise TypeError: Invalid B{C{x}}.

101

102 @raise ValueError: Non-finite B{C{x}}.

103

104 @see: Function L{fhorner} and methods L{Fsum.fadd} and L{Fsum.fmul}.

105 '''

106 incx, kwds = _xkwds_pop2(incx_name_f2product_nonfinites_RESIDUAL, incx=True)

107 Fsum.__init__(self, **kwds)

108 self._fhorner(x, cs, Fhorner, incx=incx)

109

110

111 class Fhypot(Fsum):

112 '''Precision summation and hypotenuse, default C{root=2}.

113 '''

114 def __init__(self, *xs, **root_name_f2product_nonfinites_RESIDUAL_raiser):

115 '''New L{Fhypot} hypotenuse of (the I{root} of) several components (raised

116 to the power I{root}).

117

118 @arg xs: Components (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all

119 positional.

120 @kwarg root_name_f2product_nonfinites_RESIDUAL_raiser: Optional, exponent

121 and C{B{root}=2} order (C{scalar}), C{B{name}=NN} (C{str}),

122 C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s and

123 other settings, see class L{Fsum<Fsum.__init__>} and method

124 L{root<Fsum.root>}.

125 '''

126 def _r_X_kwds(power=None, raiser=True, root=2, **kwds):

127 # DEPRECATED keyword argument C{power=2}, use C{root=2}

128 return (root if power is None else power), raiser, kwds

129

130 r = None # _xkwds_pop2 error

131 try:

132 r, X, kwds = _r_X_kwds(**root_name_f2product_nonfinites_RESIDUAL_raiser)

133 Fsum.__init__(self, **kwds)

134 self(_0_0)

135 if xs:

136 self._facc_power(r, xs, Fhypot, raiser=X)

137 self._fset(self.root(r, raiser=X))

138 except Exception as X:

139 raise self._ErrorXs(X, xs, root=r)

140

141

142 class Fpolynomial(Fsum):

143 '''Precision polynomial evaluation.

144 '''

145 def __init__(self, x, *cs, **name_f2product_nonfinites_RESIDUAL):

146 '''New L{Fpolynomial} evaluation of the polynomial M{sum(cs[i] * x**i for

147 i=0..len(cs)-1)}.

148

149 @arg x: Polynomial argument (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

150 @arg cs: Polynomial coeffients (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),

151 all positional.

152 @kwarg name_f2product_nonfinites_RESIDUAL: Optional C{B{name}=NN} (C{str})

153 and other settings, see class L{Fsum<Fsum.__init__>}.

154

155 @raise OverflowError: Partial C{2sum} overflow.

156

157 @raise TypeError: Invalid B{C{x}}.

158

159 @raise ValueError: Non-finite B{C{x}}.

160

161 @see: Class L{Fhorner}, function L{fpolynomial} and method L{Fsum.fadd}.

162 '''

163 Fsum.__init__(self, **name_f2product_nonfinites_RESIDUAL)

164 n = len(cs) - 1

165 self(_0_0 if n < 0 else cs[0])

166 self._facc_dot(n, cs[1:], _powers(x, n), **name_f2product_nonfinites_RESIDUAL)

167

168

169 class Fpowers(Fsum):

170 '''Precision summation of powers, optimized for C{power=2, 3 and 4}.

171 '''

172 def __init__(self, power, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):

173 '''New L{Fpowers} sum of (the I{power} of) several bases.

174

175 @arg power: The exponent (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

176 @arg xs: One or more bases (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all

177 positional.

178 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}

179 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s

180 and other settings, see class L{Fsum<Fsum.__init__>} and method

181 L{fpow<Fsum.fpow>}.

182 '''

183 try:

184 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)

185 Fsum.__init__(self, **kwds)

186 self(_0_0)

187 if xs:

188 self._facc_power(power, xs, Fpowers, raiser=X) # x**0 == 1

189 except Exception as X:

190 raise self._ErrorXs(X, xs, power=power)

191

192

193 class Froot(Fsum):

194 '''The root of a precision summation.

195 '''

196 def __init__(self, root, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):

197 '''New L{Froot} root of a precision sum.

198

199 @arg root: The order (C{scalar}, an L{Fsum} or L{Fsum2Tuple}), non-zero.

200 @arg xs: Items to summate (each a C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all

201 positional.

202 @kwarg name_f2product_nonfinites_RESIDUAL_raiser: Optional C{B{name}=NN}

203 (C{str}), C{B{raiser}=True} (C{bool}) for raising L{ResidualError}s

204 and other settings, see class L{Fsum<Fsum.__init__>} and method

205 L{fpow<Fsum.fpow>}.

206 '''

207 try:

208 X, kwds = _xkwds_pop2(name_f2product_nonfinites_RESIDUAL_raiser, raiser=True)

209 Fsum.__init__(self, **kwds)

210 self(_0_0)

211 if xs:

212 self.fadd(xs)

213 self(self.root(root, raiser=X))

214 except Exception as X:

215 raise self._ErrorXs(X, xs, root=root)

216

217

218 class Fcbrt(Froot):

219 '''Cubic root of a precision summation.

220 '''

221 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):

222 '''New L{Fcbrt} cubic root of a precision sum.

223

224 @see: Class L{Froot<Froot.__init__>} for further details.

225 '''

226 Froot.__init__(self, 3, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)

227

228

229 class Fsqrt(Froot):

230 '''Square root of a precision summation.

231 '''

232 def __init__(self, *xs, **name_f2product_nonfinites_RESIDUAL_raiser):

233 '''New L{Fsqrt} square root of a precision sum.

234

235 @see: Class L{Froot<Froot.__init__>} for further details.

236 '''

237 Froot.__init__(self, 2, *xs, **name_f2product_nonfinites_RESIDUAL_raiser)

238

239

240 def bqrt(x):

241 '''Return the 4-th, I{bi-quadratic} or I{quartic} root, M{x**(1 / 4)},

242 preserving C{type(B{x})}.

243

244 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

245

246 @return: I{Quartic} root (C{float} or an L{Fsum}).

247

248 @raise TypeeError: Invalid B{C{x}}.

249

250 @raise ValueError: Negative B{C{x}}.

251

252 @see: Functions L{zcrt} and L{zqrt}.

253 '''

254 return _root(x, _0_25, bqrt)

255

256

257 try:

258 from math import cbrt as _cbrt # Python 3.11+

259

260 except ImportError: # Python 3.10-

261

262 def _cbrt(x):

263 '''(INTERNAL) Compute the I{signed}, cube root M{x**(1/3)}.

264 '''

265 # <https://archive.lib.MSU.edu/crcmath/math/math/r/r021.htm>

266 # simpler and more accurate than Ken Turkowski's CubeRoot, see

267 # <https://People.FreeBSD.org/~lstewart/references/apple_tr_kt32_cuberoot.pdf>

268 return _copysign(pow(fabs(x), _1_3rd), x) # to avoid complex

269

270

271 def cbrt(x):

272 '''Compute the cube root M{x**(1/3)}, preserving C{type(B{x})}.

273

274 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

275

276 @return: Cubic root (C{float} or L{Fsum}).

277

278 @see: Functions L{cbrt2} and L{sqrt3}.

279 '''

280 if _isFsum_2Tuple(x):

281 r = abs(x).fpow(_1_3rd)

282 if x.signOf() < 0:

283 r = -r

284 else:

285 r = _cbrt(x)

286 return r # cbrt(-0.0) == -0.0

287

288

289 def cbrt2(x): # PYCHOK attr

290 '''Compute the cube root I{squared} M{x**(2/3)}, preserving C{type(B{x})}.

291

292 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

293

294 @return: Cube root I{squared} (C{float} or L{Fsum}).

295

296 @see: Functions L{cbrt} and L{sqrt3}.

297 '''

298 return abs(x).fpow(_2_3rd) if _isFsum_2Tuple(x) else _cbrt(x**2)

299

300

301 def euclid(x, y):

302 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by M{max(abs(x),

303 abs(y)) + min(abs(x), abs(y)) * 0.4142...}.

304

305 @arg x: X component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

306 @arg y: Y component (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

307

308 @return: Appoximate norm (C{float} or L{Fsum}).

309

310 @see: Function L{euclid_}.

311 '''

312 x, y = abs(x), abs(y) # NOT fabs!

313 return (x + y * _0_4142) if x > y else \

314 (y + x * _0_4142) # * _0_5 before 20.10.02

315

316

317 def euclid_(*xs):

318 '''I{Appoximate} the norm M{sqrt(sum(x**2 for x in xs))} by cascaded

319 L{euclid}.

320

321 @arg xs: X arguments (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}),

322 all positional.

323

324 @return: Appoximate norm (C{float} or L{Fsum}).

325

326 @see: Function L{euclid}.

327 '''

328 e = _0_0

329 for x in sorted(map(abs, xs)): # NOT fabs, reverse=True!

330 # e = euclid(x, e)

331 if e < x:

332 e, x = x, e

333 if x:

334 e += x * _0_4142

335 return e

336

337

338 def facos1(x):

339 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}, scalar.

340

341 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/

342 ShaderFastLibs/blob/master/ShaderFastMathLib.h>}.

343 '''

344 a = fabs(x)

345 if a < EPS0:

346 r = PI_2

347 elif a < EPS1:

348 r = _fast(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)

349 r *= sqrt(_1_0 - a)

350 if x < 0:

351 r = PI - r

352 else:

353 r = PI if x < 0 else _0_0

354 return r

355

356

357 def fasin1(x): # PYCHOK no cover

358 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}, scalar.

359

360 @see: L{facos1}.

361 '''

362 return PI_2 - facos1(x)

363

364

365 def _fast(x, *cs):

366 '''(INTERNAL) Horner form for C{facos1} and C{fatan1}.

367 '''

368 h = 0

369 for c in reversed(cs):

370 h = _fma(x, h, c) if h else c

371 return h

372

373

374 def fatan(x):

375 '''Fast approximation of C{atan(B{x})}, scalar.

376 '''

377 a = fabs(x)

378 if a < _1_0:

379 r = fatan1(a) if a else _0_0

380 elif a > _1_0:

381 r = PI_2 - fatan1(_1_0 / a) # == fatan2(a, _1_0)

382 else:

383 r = PI_4

384 if x < 0: # copysign0(r, x)

385 r = -r

386 return r

387

388

389 def fatan1(x):

390 '''Fast approximation of C{atan(B{x})} for C{0 <= B{x} < 1}, I{unchecked}.

391

392 @see: U{ShaderFastLibs.h<https://GitHub.com/michaldrobot/ShaderFastLibs/

393 blob/master/ShaderFastMathLib.h>} and U{Efficient approximations

394 for the arctangent function<http://www-Labs.IRO.UMontreal.CA/

395 ~mignotte/IFT2425/Documents/EfficientApproximationArctgFunction.pdf>},

396 IEEE Signal Processing Magazine, 111, May 2006.

397 '''

398 # Eq (9): PI_4 * x - x * (abs(x) - 1) * (0.2447 + 0.0663 * abs(x)), for -1 < x < 1

399 # == PI_4 * x - (x**2 - x) * (0.2447 + 0.0663 * x), for 0 < x < 1

400 # == x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))

401 return _fast(x, _0_0, 1.0300981634, -0.1784, -0.0663)

402

403

404 def fatan2(y, x):

405 '''Fast approximation of C{atan2(B{y}, B{x})}, scalar.

406

407 @see: U{fastApproximateAtan(x, y)<https://GitHub.com/CesiumGS/cesium/blob/

408 master/Source/Shaders/Builtin/Functions/fastApproximateAtan.glsl>}

409 and L{fatan1}.

410 '''

411 a, b = fabs(x), fabs(y)

412 if b > a:

413 r = (PI_2 - fatan1(a / b)) if a else PI_2

414 elif a > b:

415 r = fatan1(b / a) if b else _0_0

416 elif a: # a == b != 0

417 r = PI_4

418 else: # a == b == 0

419 return _0_0

420 if x < 0:

421 r = PI - r

422 if y < 0: # copysign0(r, y)

423 r = -r

424 return r

425

426

427 def favg(a, b, f=_0_5, nonfinites=True):

428 '''Return the precise average of two values.

429

430 @arg a: One (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

431 @arg b: Other (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

432 @kwarg f: Optional fraction (C{float}).

433 @kwarg nonfinites: Optional setting, see function L{fma}.

434

435 @return: M{a + f * (b - a)} (C{float}).

436 '''

437 F = fma(f, (b - a), a, nonfinites=nonfinites)

438 return float(F)

439

440

441 def fdot(xs, *ys, **start_f2product_nonfinites):

442 '''Return the precision dot product M{sum(xs[i] * ys[i] for i in range(len(xs)))}.

443

444 @arg xs: Iterable of values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

445 @arg ys: Other values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.

446 @kwarg start_f2product_nonfinites: Optional bias C{B{start}=0} (C{scalar}, an

447 L{Fsum} or L{Fsum2Tuple}) and settings C{B{f2product}=None} (C{bool})

448 and C{B{nonfinites=True}} (C{bool}), see class L{Fsum<Fsum.__init__>}.

449

450 @return: Dot product (C{float}).

451

452 @raise LenError: Unequal C{len(B{xs})} and C{len(B{ys})}.

453

454 @see: Class L{Fdot}, U{Algorithm 5.10 B{DotK}

455 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>} and function

456 C{math.sumprod} in Python 3.12 and later.

457 '''

458 D = Fdot(xs, *ys, **_xkwds(start_f2product_nonfinites, nonfinites=True))

459 return float(D)

460

461

462 def fdot_(*xys, **start_f2product_nonfinites):

463 '''Return the (precision) dot product M{sum(xys[i] * xys[i+1] for i in range(0, len(xys), B{2}))}.

464

465 @arg xys: Pairwise values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all positional.

466

467 @see: Function L{fdot} for further details.

468

469 @return: Dot product (C{float}).

470 '''

471 D = Fdot_(*xys, **_xkwds(start_f2product_nonfinites, nonfinites=True))

472 return float(D)

473

474

475 def fdot3(xs, ys, zs, **start_f2product_nonfinites):

476 '''Return the (precision) dot product M{start + sum(xs[i] * ys[i] * zs[i] for i in range(len(xs)))}.

477

478 @arg xs: X values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

479 @arg ys: Y values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

480 @arg zs: Z values iterable (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

481

482 @see: Function L{fdot} for further details.

483

484 @return: Dot product (C{float}).

485

486 @raise LenError: Unequal C{len(B{xs})}, C{len(B{ys})} and/or C{len(B{zs})}.

487 '''

488 n = len(xs)

489 if not n == len(ys) == len(zs):

490 raise LenError(fdot3, xs=n, ys=len(ys), zs=len(zs))

491

492 D = Fdot((), **_xkwds(start_f2product_nonfinites, nonfinites=True))

493 kwds = dict(f2product=D.f2product(), nonfinites=D.nonfinites())

494 _f = Fsum(**kwds)

495 D = D._facc(_f(x).f2mul_(y, z, **kwds) for x, y, z in zip(xs, ys, zs))

496 return float(D)

497

498

499 def fhorner(x, *cs, **incx):

500 '''Horner form evaluation of polynomial M{sum(cs[i] * x**i for i=0..n)} as

501 in- or decreasing exponent M{sum(... i=n..0)}, where C{n = len(cs) - 1}.

502

503 @return: Horner sum (C{float}).

504

505 @see: Class L{Fhorner<Fhorner.__init__>} for further details.

506 '''

507 H = Fhorner(x, *cs, **incx)

508 return float(H)

509

510

511 def fidw(xs, ds, beta=2):

512 '''Interpolate using U{Inverse Distance Weighting

513 <https://WikiPedia.org/wiki/Inverse_distance_weighting>} (IDW).

514

515 @arg xs: Known values (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

516 @arg ds: Non-negative distances (each C{scalar}, an L{Fsum} or

517 L{Fsum2Tuple}).

518 @kwarg beta: Inverse distance power (C{int}, 0, 1, 2, or 3).

519

520 @return: Interpolated value C{x} (C{float}).

521

522 @raise LenError: Unequal or zero C{len(B{ds})} and C{len(B{xs})}.

523

524 @raise TypeError: An invalid B{C{ds}} or B{C{xs}}.

525

526 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} or

527 weighted B{C{ds}} below L{EPS}.

528

529 @note: Using C{B{beta}=0} returns the mean of B{C{xs}}.

530 '''

531 n, xs = len2(xs)

532 if n > 1:

533 b = -Int_(beta=beta, low=0, high=3)

534 if b < 0:

535 try: # weighted

536 _d, W, X = (Fsum() for _ in range(3))

537 for i, d in enumerate(_xiterable(ds)):

538 x = xs[i]

539 D = _d(d)

540 if D < EPS0:

541 if D < 0:

542 raise ValueError(_negative_)

543 x = float(x)

544 i = n

545 break

546 if D.fpow(b):

547 W += D

548 X += D.fmul(x)

549 else:

550 x = X.fover(W, raiser=False)

551 i += 1 # len(xs) >= len(ds)

552 except IndexError:

553 i += 1 # len(xs) < i < len(ds)

554 except Exception as X:

555 _I = Fmt.INDEX

556 raise _xError(X, _I(xs=i), x,

557 _I(ds=i), d)

558 else: # b == 0

559 x = fsum(xs) / n # fmean(xs)

560 i = n

561 elif n:

562 x = float(xs[0])

563 i = n

564 else:

565 x = _0_0

566 i, _ = len2(ds)

567 if i != n:

568 raise LenError(fidw, xs=n, ds=i)

569 return x

570

571

572 try:

573 from math import fma as _fma # in .resections

574 except ImportError: # PYCHOK DSPACE!

575

576 def _fma(x, y, z): # no need for accuracy

577 return x * y + z

578

579

580 def fma(x, y, z, **nonfinites): # **raiser

581 '''Fused-multiply-add, using C{math.fma(x, y, z)} in Python 3.13+

582 or an equivalent implementation.

583

584 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

585 @arg y: Multiplier (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

586 @arg z: Addend (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

587 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False},

588 to override default L{nonfiniterrors}

589 (C{bool}), see method L{Fsum.fma}.

590

591 @return: C{(x * y) + z} (C{float} or L{Fsum}).

592 '''

593 F, raiser = _Fm2(x, **nonfinites)

594 return F.fma(y, z, **raiser).as_iscalar

595

596

597 def _Fm2(x, nonfinites=None, **raiser):

598 '''(INTERNAL) Handle C{fma} and C{f2mul} DEPRECATED C{raiser=False}.

599 '''

600 return Fsum(x, nonfinites=nonfinites), raiser

601

602

603 def fmean(xs):

604 '''Compute the accurate mean M{sum(xs) / len(xs)}.

605

606 @arg xs: Values (each C{scalar}, or L{Fsum} or L{Fsum2Tuple}).

607

608 @return: Mean value (C{float}).

609

610 @raise LenError: No B{C{xs}} values.

611

612 @raise OverflowError: Partial C{2sum} overflow.

613 '''

614 n, xs = len2(xs)

615 if n < 1:

616 raise LenError(fmean, xs=xs)

617 M = Fsum(*xs, nonfinites=True)

618 return M.fover(n) if n > 1 else float(M)

619

620

621 def fmean_(*xs, **nonfinites):

622 '''Compute the accurate mean M{sum(xs) / len(xs)}.

623

624 @see: Function L{fmean} for further details.

625 '''

626 return fmean(xs, **nonfinites)

627

628

629 def f2mul_(x, *ys, **nonfinites): # **raiser

630 '''Cascaded, accurate multiplication C{B{x} * B{y} * B{y} ...} for all B{C{ys}}.

631

632 @arg x: Multiplicand (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

633 @arg ys: Multipliers (each C{scalar}, an L{Fsum} or L{Fsum2Tuple}), all

634 positional.

635 @kwarg nonfinites: Use C{B{nonfinites}=True} or C{=False}, to override default

636 L{nonfiniterrors} (C{bool}), see method L{Fsum.f2mul_}.

637

638 @return: The cascaded I{TwoProduct} (C{float}, C{int} or L{Fsum}).

639

640 @see: U{Equations 2.3<https://www.TUHH.De/ti3/paper/rump/OzOgRuOi06.pdf>}

641 '''

642 F, raiser = _Fm2(x, **nonfinites)

643 return F.f2mul_(*ys, **raiser).as_iscalar

644

645

646 def fpolynomial(x, *cs, **over_f2product_nonfinites):

647 '''Evaluate the polynomial M{sum(cs[i] * x**i for i=0..len(cs)) [/ over]}.

648

649 @kwarg over_f2product_nonfinites: Optional final divisor C{B{over}=None}

650 (I{non-zero} C{scalar}) and other settings, see class

651 L{Fpolynomial<Fpolynomial.__init__>}.

652

653 @return: Polynomial value (C{float} or L{Fpolynomial}).

654 '''

655 d, kwds = _xkwds_pop2(over_f2product_nonfinites, over=0)

656 P = Fpolynomial(x, *cs, **kwds)

657 return P.fover(d) if d else float(P)

658

659

660 def fpowers(x, n, alts=0):

661 '''Return a series of powers M{[x**i for i=1..n]}, note I{1..!}

662

663 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

664 @arg n: Highest exponent (C{int}).

665 @kwarg alts: Only alternating powers, starting with this

666 exponent (C{int}).

667

668 @return: Tuple of powers of B{C{x}} (each C{type(B{x})}).

669

670 @raise TypeError: Invalid B{C{x}} or B{C{n}} not C{int}.

671

672 @raise ValueError: Non-finite B{C{x}} or invalid B{C{n}}.

673 '''

674 if not isint(n):

675 raise _IsnotError(typename(int), n=n)

676 elif n < 1:

677 raise _ValueError(n=n)

678

679 p = x if isscalar(x) or _isFsum_2Tuple(x) else _2float(x=x)

680 ps = tuple(_powers(p, n))

681

682 if alts > 0: # x**2, x**4, ...

683 # ps[alts-1::2] chokes PyChecker

684 ps = ps[slice(alts-1, None, 2)]

685

686 return ps

687

688

689 try:

690 from math import prod as fprod # Python 3.8

691 except ImportError:

692

693 def fprod(xs, start=1):

694 '''Iterable product, like C{math.prod} or C{numpy.prod}.

695

696 @arg xs: Iterable of values to be multiplied (each

697 C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

698 @kwarg start: Initial value, also the value returned

699 for an empty B{C{xs}} (C{scalar}).

700

701 @return: The product (C{float} or L{Fsum}).

702

703 @see: U{NumPy.prod<https://docs.SciPy.org/doc/

704 numpy/reference/generated/numpy.prod.html>}.

705 '''

706 return freduce(_operator.mul, xs, start)

707

708

709 def frandoms(n, seeded=None):

710 '''Generate C{n} (long) lists of random C{floats}.

711

712 @arg n: Number of lists to generate (C{int}, non-negative).

713 @kwarg seeded: If C{scalar}, use C{random.seed(B{seeded})} or

714 if C{True}, seed using today's C{year-day}.

715

716 @see: U{Hettinger<https://GitHub.com/ActiveState/code/tree/master/recipes/

717 Python/393090_Binary_floating_point_summatiaccurate_full/recipe-393090.py>}.

718 '''

719 from random import gauss, random, seed, shuffle

720

721 if seeded is None:

722 pass

723 elif seeded and isbool(seeded):

724 from time import localtime

725 seed(localtime().tm_yday)

726 elif isscalar(seeded):

727 seed(seeded)

728

729 c = (7, 1e100, -7, -1e100, -9e-20, 8e-20) * 7

730 for _ in range(n):

731 s = 0

732 t = list(c)

733 _a = t.append

734 for _ in range(n * 8):

735 v = gauss(0, random())**7 - s

736 _a(v)

737 s += v

738 shuffle(t)

739 yield t

740

741

742 def frange(start, number, step=1):

743 '''Generate a range of C{float}s.

744

745 @arg start: First value (C{float}).

746 @arg number: The number of C{float}s to generate (C{int}).

747 @kwarg step: Increment value (C{float}).

748

749 @return: A generator (C{float}s).

750

751 @see: U{NumPy.prod<https://docs.SciPy.org/doc/

752 numpy/reference/generated/numpy.arange.html>}.

753 '''

754 if not isint(number):

755 raise _IsnotError(typename(int), number=number)

756 for i in range(number):

757 yield start + (step * i)

758

759

760 try:

761 from functools import reduce as freduce

762 except ImportError:

763 try:

764 freduce = reduce # PYCHOK expected

765 except NameError: # Python 3+

766

767 def freduce(f, xs, *start):

768 '''For missing C{functools.reduce}.

769 '''

770 if start:

771 r = v = start[0]

772 else:

773 r, v = 0, MISSING

774 for v in xs:

775 r = f(r, v)

776 if v is MISSING:

777 raise _TypeError(xs=(), start=MISSING)

778 return r

779

780

781 def fremainder(x, y):

782 '''Remainder in range C{[-B{y / 2}, B{y / 2}]}.

783

784 @arg x: Numerator (C{scalar}).

785 @arg y: Modulus, denominator (C{scalar}).

786

787 @return: Remainder (C{scalar}, preserving signed

788 0.0) or C{NAN} for any non-finite B{C{x}}.

789

790 @raise ValueError: Infinite or near-zero B{C{y}}.

791

792 @see: I{Karney}'s U{Math.remainder<https://PyPI.org/

793 project/geographiclib/>} and Python 3.7+

794 U{math.remainder<https://docs.Python.org/3/

795 library/math.html#math.remainder>}.

796 '''

797 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and

798 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native

799 # fmod( 0, 360) == 0.0

800 # fmod( 360, 360) == 0.0

801 # fmod(-0, 360) == 0.0

802 # fmod(-0.0, 360) == -0.0

803 # fmod(-360, 360) == -0.0

804 # however, using the % operator ...

805 # 0 % 360 == 0

806 # 360 % 360 == 0

807 # 360.0 % 360 == 0.0

808 # -0 % 360 == 0

809 # -360 % 360 == 0 == (-360) % 360

810 # -0.0 % 360 == 0.0 == (-0.0) % 360

811 # -360.0 % 360 == 0.0 == (-360.0) % 360

812

813 # On Windows 32-bit with python 2.7, math.fmod(-0.0, 360)

814 # == +0.0. This fixes this bug. See also Math::AngNormalize

815 # in the C++ library, Math.sincosd has a similar fix.

816 if isfinite(x):

817 try:

818 r = remainder(x, y) if x else x

819 except Exception as e:

820 raise _xError(e, unstr(fremainder, x, y))

821 else: # handle x INF and NINF as NAN

822 r = NAN

823 return r

824

825

826 if _MODS.sys_version_info2 < (3, 8): # PYCHOK no cover

827 from math import hypot # OK in Python 3.7-

828

829 def hypot_(*xs):

830 '''Compute the norm M{sqrt(sum(x**2 for x in xs))}.

831

832 Similar to Python 3.8+ n-dimension U{math.hypot

833 <https://docs.Python.org/3.8/library/math.html#math.hypot>},

834 but exceptions, C{nan} and C{infinite} values are

835 handled differently.

836

837 @arg xs: X arguments (C{scalar}s), all positional.

838

839 @return: Norm (C{float}).

840

841 @raise OverflowError: Partial C{2sum} overflow.

842

843 @raise ValueError: Invalid or no B{C{xs}} values.

844

845 @note: The Python 3.8+ Euclidian distance U{math.dist

846 <https://docs.Python.org/3.8/library/math.html#math.dist>}

847 between 2 I{n}-dimensional points I{p1} and I{p2} can be

848 computed as M{hypot_(*((c1 - c2) for c1, c2 in zip(p1, p2)))},

849 provided I{p1} and I{p2} have the same, non-zero length I{n}.

850 '''

851 return float(_Hypot(*xs))

852

853 elif _MODS.sys_version_info2 < (3, 10): # PYCHOK no cover

854 # In Python 3.8 and 3.9 C{math.hypot} is inaccurate, see

855 # U{agdhruv<https://GitHub.com/geopy/geopy/issues/466>},

856 # U{cffk<https://Bugs.Python.org/issue43088>} and module

857 # U{geomath.py<https://PyPI.org/project/geographiclib/1.52>}

858

859 def hypot(x, y):

860 '''Compute the norm M{sqrt(x**2 + y**2)}.

861

862 @arg x: X argument (C{scalar}).

863 @arg y: Y argument (C{scalar}).

864

865 @return: C{sqrt(B{x}**2 + B{y}**2)} (C{float}).

866 '''

867 return float(_Hypot(x, y))

868

869 from math import hypot as hypot_ # PYCHOK in Python 3.8 and 3.9

870 else:

871 from math import hypot # PYCHOK in Python 3.10+

872 hypot_ = hypot

873

874

875 def _Hypot(*xs):

876 '''(INTERNAL) Substitute for inaccurate C{math.hypot}.

877 '''

878 return Fhypot(*xs, nonfinites=True, raiser=False) # f2product=True

879

880

881 def hypot1(x):

882 '''Compute the norm M{sqrt(1 + x**2)}.

883

884 @arg x: Argument (C{scalar} or L{Fsum} or L{Fsum2Tuple}).

885

886 @return: Norm (C{float} or L{Fhypot}).

887 '''

888 h = _1_0

889 if x:

890 if _isFsum_2Tuple(x):

891 h = _Hypot(h, x)

892 h = float(h)

893 else:

894 h = hypot(h, x)

895 return h

896

897

898 def hypot2(x, y):

899 '''Compute the I{squared} norm M{x**2 + y**2}.

900

901 @arg x: X (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

902 @arg y: Y (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

903

904 @return: C{B{x}**2 + B{y}**2} (C{float}).

905 '''

906 x, y = map1(abs, x, y) # NOT fabs!

907 if y > x:

908 x, y = y, x

909 h2 = x**2

910 if h2 and y:

911 h2 *= (y / x)**2 + _1_0

912 return float(h2)

913

914

915 def hypot2_(*xs):

916 '''Compute the I{squared} norm C{fsum(x**2 for x in B{xs})}.

917

918 @arg xs: Components (each C{scalar}, an L{Fsum} or

919 L{Fsum2Tuple}), all positional.

920

921 @return: Squared norm (C{float}).

922

923 @see: Class L{Fpowers} for further details.

924 '''

925 h2 = float(max(map(abs, xs))) if xs else _0_0

926 if h2: # and isfinite(h2)

927 _h = _1_0 / h2

928 xs = ((x * _h) for x in xs)

929 H2 = Fpowers(2, *xs, nonfinites=True) # f2product=True

930 h2 = H2.fover(_h**2)

931 return h2

932

933

934 def norm2(x, y):

935 '''Normalize a 2-dimensional vector.

936

937 @arg x: X component (C{scalar}).

938 @arg y: Y component (C{scalar}).

939

940 @return: 2-Tuple C{(x, y)}, normalized.

941

942 @raise ValueError: Invalid B{C{x}} or B{C{y}}

943 or zero norm.

944 '''

945 try:

946 h = None

947 h = hypot(x, y)

948 if h:

949 x, y = (x / h), (y / h)

950 else:

951 x = _copysign_0_0(x) # pass?

952 y = _copysign_0_0(y)

953 except Exception as e:

954 raise _xError(e, x=x, y=y, h=h)

955 return x, y

956

957

958 def norm_(*xs):

959 '''Normalize the components of an n-dimensional vector.

960

961 @arg xs: Components (each C{scalar}, an L{Fsum} or

962 L{Fsum2Tuple}), all positional.

963

964 @return: Yield each component, normalized.

965

966 @raise ValueError: Invalid or insufficent B{C{xs}}

967 or zero norm.

968 '''

969 try:

970 i = h = None

971 x = xs

972 h = hypot_(*xs)

973 _h = (_1_0 / h) if h else _0_0

974 for i, x in enumerate(xs):

975 yield x * _h

976 except Exception as X:

977 raise _xsError(X, xs, i, x, h=h)

978

979

980 def _powers(x, n):

981 '''(INTERNAL) Yield C{x**i for i=1..n}.

982 '''

983 p = 1 # type(p) == type(x)

984 for _ in range(n):

985 p *= x

986 yield p

987

988

989 def _root(x, p, where):

990 '''(INTERNAL) Raise C{x} to power C{0 < p < 1}.

991 '''

992 try:

993 if x > 0:

994 r = Fsum(f2product=True, nonfinites=True)(x)

995 return r.fpow(p).as_iscalar

996 elif x < 0:

997 raise ValueError(_negative_)

998 except Exception as X:

999 raise _xError(X, unstr(where, x))

1000 return _0_0 if p else _1_0

1001

1002

1003 def sqrt0(x, Error=None):

1004 '''Return the square root C{sqrt(B{x})} iff C{B{x} > }L{EPS02},

1005 preserving C{type(B{x})}.

1006

1007 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

1008 @kwarg Error: Error to raise for negative B{C{x}}.

1009

1010 @return: Square root (C{float} or L{Fsum}) or C{0.0}.

1011

1012 @raise TypeeError: Invalid B{C{x}}.

1013

1014 @note: Any C{B{x} < }L{EPS02} I{including} C{B{x} < 0}

1015 returns C{0.0}.

1016 '''

1017 if Error and x < 0:

1018 raise Error(unstr(sqrt0, x))

1019 return _root(x, _0_5, sqrt0) if x > EPS02 else (

1020 _0_0 if x < EPS02 else EPS0)

1021

1022

1023 def sqrt3(x):

1024 '''Return the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)},

1025 preserving C{type(B{x})}.

1026

1027 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

1028

1029 @return: Square root I{cubed} (C{float} or L{Fsum}).

1030

1031 @raise TypeeError: Invalid B{C{x}}.

1032

1033 @raise ValueError: Negative B{C{x}}.

1034

1035 @see: Functions L{cbrt} and L{cbrt2}.

1036 '''

1037 return _root(x, _1_5, sqrt3)

1038

1039

1040 def sqrt_a(h, b):

1041 '''Compute C{I{a}} side of a right-angled triangle from

1042 C{sqrt(B{h}**2 - B{b}**2)}.

1043

1044 @arg h: Hypotenuse or outer annulus radius (C{scalar}).

1045 @arg b: Triangle side or inner annulus radius (C{scalar}).

1046

1047 @return: C{copysign(I{a}, B{h})} or C{unsigned 0.0} (C{float}).

1048

1049 @raise TypeError: Non-scalar B{C{h}} or B{C{b}}.

1050

1051 @raise ValueError: If C{abs(B{h}) < abs(B{b})}.

1052

1053 @see: Inner tangent chord B{I{d}} of an U{annulus

1054 <https://WikiPedia.org/wiki/Annulus_(mathematics)>}

1055 and function U{annulus_area<https://People.SC.FSU.edu/

1056 ~jburkardt/py_src/geometry/geometry.py>}.

1057 '''

1058 try:

1059 if not (_isHeight(h) and _isRadius(b)):

1060 raise TypeError(_not_scalar_)

1061 c = fabs(h)

1062 if c > EPS0:

1063 s = _1_0 - (b / c)**2

1064 if s < 0:

1065 raise ValueError(_h_lt_b_)

1066 a = (sqrt(s) * c) if 0 < s < 1 else (c if s else _0_0)

1067 else: # PYCHOK no cover

1068 b = fabs(b)

1069 d = c - b

1070 if d < 0:

1071 raise ValueError(_h_lt_b_)

1072 d *= c + b

1073 a = sqrt(d) if d else _0_0

1074 except Exception as x:

1075 raise _xError(x, h=h, b=b)

1076 return copysign0(a, h)

1077

1078

1079 def zcrt(x):

1080 '''Return the 6-th, I{zenzi-cubic} root, M{x**(1 / 6)},

1081 preserving C{type(B{x})}.

1082

1083 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

1084

1085 @return: I{Zenzi-cubic} root (C{float} or L{Fsum}).

1086

1087 @see: Functions L{bqrt} and L{zqrt}.

1088

1089 @raise TypeeError: Invalid B{C{x}}.

1090

1091 @raise ValueError: Negative B{C{x}}.

1092 '''

1093 return _root(x, _1_6th, zcrt)

1094

1095

1096 def zqrt(x):

1097 '''Return the 8-th, I{zenzi-quartic} or I{squared-quartic} root,

1098 M{x**(1 / 8)}, preserving C{type(B{x})}.

1099

1100 @arg x: Value (C{scalar}, an L{Fsum} or L{Fsum2Tuple}).

1101

1102 @return: I{Zenzi-quartic} root (C{float} or L{Fsum}).

1103

1104 @see: Functions L{bqrt} and L{zcrt}.

1105

1106 @raise TypeeError: Invalid B{C{x}}.

1107

1108 @raise ValueError: Negative B{C{x}}.

1109 '''

1110 return _root(x, _0_125, zqrt)

1111

1112# **) MIT License

1113#

1114# Copyright (C) 2016-2025 -- mrJean1 at Gmail -- All Rights Reserved.

1115#

1116# Permission is hereby granted, free of charge, to any person obtaining a

1117# copy of this software and associated documentation files (the "Software"),

1118# to deal in the Software without restriction, including without limitation

1119# the rights to use, copy, modify, merge, publish, distribute, sublicense,

1120# and/or sell copies of the Software, and to permit persons to whom the

1121# Software is furnished to do so, subject to the following conditions:

1122#

1123# The above copyright notice and this permission notice shall be included

1124# in all copies or substantial portions of the Software.

1125#

1126# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS

1127# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

1128# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL

1129# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR

1130# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,

1131# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR

1132# OTHER DEALINGS IN THE SOFTWARE.