1

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

3

4 u'''Utilities using precision floating point summation.

5'''

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

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

8

9 from pygeodesy.basics import _copysign, copysign0, isbool, isint, isscalar, len2

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

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

12 _1_5, _copysign_0_0, _isfinite, _over, remainder

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

14 _xError, _xkwds_get, _xkwds_pop2

15 from pygeodesy.fsums import _2float, Fsum, fsum, fsum1_, _1primed, Fmt, unstr

16 from pygeodesy.interns import MISSING, _few_, _negative_, _not_scalar_, _too_

17 from pygeodesy.lazily import _ALL_LAZY, _sys_version_info2

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

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

20

21 from math import fabs, sqrt # pow

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

23

24__all__ = _ALL_LAZY.fmath

25__version__ = '24.04.24'

26

27# sqrt(2) <https://WikiPedia.org/wiki/Square_root_of_2>

28_0_4142 = 0.41421356237309504880 # ... sqrt(2) - 1

29_2_3rd = _1_3rd * 2

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

31

32

33 class Fdot(Fsum):

34 '''Precision dot product.

35 '''

36 def __init__(self, a, *b, **name_RESIDUAL):

37 '''New L{Fdot} precision dot product M{sum(a[i] * b[i]

38 for i=0..len(a)-1)}.

39

40 @arg a: Iterable, list, tuple, etc. (C{scalar}s).

41 @arg b: Other values (C{scalar}s), all positional.

42 @kwarg name_RESIDUAL: Optional C{B{name}=NN} and C{B{RESIDUAL}=None},

43 see L{Fsum<Fsum.__init__>}.

44

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

46

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

48

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

50 '''

51 Fsum.__init__(self, **name_RESIDUAL)

52 self.fadd(_map_mul(a, b, Fdot))

53

54

55 class Fhorner(Fsum):

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

57 '''

58 def __init__(self, x, *cs, **name_RESIDUAL):

59 '''New L{Fhorner} evaluation of polynomial M{sum(cs[i] * x**i

60 for i=0..len(cs)-1)}.

61

62 @arg x: Polynomial argument (C{scalar} or C{Fsum} instance).

63 @arg cs: Polynomial coeffients (C{scalar} or C{Fsum}

64 instances), all positional.

65 @kwarg name_RESIDUAL: Optional C{B{name}=NN} and C{B{RESIDUAL}=None},

66 see L{Fsum<Fsum.__init__>}.

67

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

69

70 @raise TypeError: Non-scalar B{C{x}}.

71

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

73

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

75 L{Fsum.fmul}.

76 '''

77 Fsum.__init__(self, **name_RESIDUAL)

78 if cs:

79 if isinstance(x, Fsum):

80 _mul = self._mul_Fsum

81 else:

82 _mul = self._mul_scalar

83 x = _2float(x=x)

84 op = Fhorner.__name__

85 if len(cs) > 1 and x:

86 for c in reversed(cs):

87 self._fset_ps(_mul(x, op))

88 self._fadd(c, op, up=False)

89 self._update()

90 else: # x == 0

91 self._fadd(cs[0], op)

92 else:

93 self._fset_ps(_0_0)

94

95

96 class Fhypot(Fsum):

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

98 '''

99 def __init__(self, *xs, **root_name_RESIDUAL_raiser):

100 '''New L{Fhypot} hypotenuse of (the I{root} of) several components.

101

102 @arg xs: One or more components (each a C{scalar} or an C{Fsum} instance).

103 @kwarg root_name_RESIDUAL_raiser: Optional, exponent and C{B{root}=2} order,

104 C{B{name}=NN}, C{B{RESIDUAL}=None} and C{B{raiser}=True}, see

105 class L{Fsum<Fsum.__init__>} and method L{root<Fsum.root>}.

106 '''

107 r = None # _xkwds_pop2 error

108 try:

109 r, kwds = _xkwds_pop2(root_name_RESIDUAL_raiser, root=2)

110 r, kwds = _xkwds_pop2(kwds, power=r) # for backward compatibility

111 raiser = _Fsum__init__(self, **kwds)

112 if xs:

113 self._facc_power(r, xs, Fhypot, **raiser)

114 self._fset(self.root(r, **raiser))

115 except Exception as X:

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

117

118

119 class Fpolynomial(Fsum):

120 '''Precision polynomial evaluation.

121 '''

122 def __init__(self, x, *cs, **name_RESIDUAL):

123 '''New L{Fpolynomial} evaluation of the polynomial

124 M{sum(cs[i] * x**i for i=0..len(cs)-1)}.

125

126 @arg x: Polynomial argument (C{scalar} or L{Fsum}).

127 @arg cs: Polynomial coeffients (each a C{scalar} or an L{Fsum} instance),

128 all positional.

129 @kwarg name_RESIDUAL: Optional C{B{name}=NN} and C{B{RESIDUAL}=None},

130 see L{Fsum<Fsum.__init__>}.

131

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

133

134 @raise TypeError: Non-scalar B{C{x}}.

135

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

137

138 @see: Class L{Fhorner}, function L{fpolynomial} and

139 method L{Fsum.fadd}.

140 '''

141 Fsum.__init__(self, *cs[:1], **name_RESIDUAL)

142 n = len(cs) - 1

143 if n > 0:

144 self.fadd(_1map_mul(cs[1:], _powers(x, n)))

145 elif n < 0:

146 self._fset_ps(_0_0)

147

148

149 class Fpowers(Fsum):

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

151 '''

152 def __init__(self, power, *xs, **name_RESIDUAL_raiser):

153 '''New L{Fpowers} sum of (the I{power} of) several values.

154

155 @arg power: The exponent (C{scalar} or L{Fsum}).

156 @arg xs: One or more values (each a C{scalar} or an C{Fsum} instance).

157 @kwarg name_RESIDUAL_raiser: Optional C{B{name}=NN}, C{B{RESIDUAL}=None} and

158 C{B{raiser}=True}, see L{Fsum<Fsum.__init__>} and L{fpow<Fsum.fpow>}.

159 '''

160 try:

161 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser)

162 if xs:

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

164 except Exception as X:

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

166

167

168 class Froot(Fsum):

169 '''The root of a precision summation.

170 '''

171 def __init__(self, root, *xs, **name_RESIDUAL_raiser):

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

173

174 @arg root: The order (C{scalar} or C{Fsum}), non-zero.

175 @arg xs: Values to summate (each a C{scalar} or an C{Fsum} instance).

176 @kwarg name_RESIDUAL_raiser: Optional C{B{name}=NN}, C{B{RESIDUAL}=None} and

177 C{B{raiser}=True}, see L{Fsum<Fsum.__init__>} and L{fpow<Fsum.fpow>}.

178 '''

179 try:

180 raiser = _Fsum__init__(self, **name_RESIDUAL_raiser)

181 if xs:

182 self.fadd(xs)

183 self._fset(self.root(root, **raiser))

184 except Exception as X:

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

186

187

188 class Fcbrt(Froot):

189 '''Cubic root of a precision summation.

190 '''

191 def __init__(self, *xs, **name_RESIDUAL_raiser):

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

193

194 @see: Class L{Froot} for further details.

195 '''

196 Froot.__init__(self, 3, *xs, **name_RESIDUAL_raiser)

197

198

199 class Fsqrt(Froot):

200 '''Square root of a precision summation.

201 '''

202 def __init__(self, *xs, **name_RESIDUAL_raiser):

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

204

205 @see: Class L{Froot} for further details.

206 '''

207 Froot.__init__(self, 2, *xs, **name_RESIDUAL_raiser)

208

209

210 def _Fsum__init__(inst, raiser=MISSING, **name_RESIDUAL):

211 '''(INTERNAL) Init an C{F...} instance above.

212 '''

213 Fsum.__init__(inst, **name_RESIDUAL) # PYCHOK self

214 inst._fset_ps(_0_0)

215 return {} if raiser is MISSING else dict(raiser=raiser)

216

217

218 def bqrt(x):

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

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

221

222 @arg x: Value (C{scalar} or L{Fsum} instance).

223

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

225

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

227

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

229 '''

230 return _root(x, _0_25, bqrt)

231

232

233 try:

234 from math import cbrt as _cbrt # Python 3.11+

235

236 except ImportError: # Python 3.10-

237

238 def _cbrt(x):

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

240 '''

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

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

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

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

245

246

247 def cbrt(x):

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

249

250 @arg x: Value (C{scalar} or L{Fsum} instance).

251

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

253

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

255 '''

256 if isinstance(x, Fsum):

257 r = (-(-x).pow(_1_3rd)) if x < 0 else x.pow(_1_3rd)

258 else:

259 r = _cbrt(x)

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

261

262

263 def cbrt2(x): # PYCHOK attr

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

265

266 @arg x: Value (C{scalar} or L{Fsum} instance).

267

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

269

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

271 '''

272 return abs(x).pow(_2_3rd) if isinstance(x, Fsum) else _cbrt(x**2)

273

274

275 def euclid(x, y):

276 '''I{Appoximate} the norm M{sqrt(x**2 + y**2)} by

277 M{max(abs(x), abs(y)) + min(abs(x), abs(y)) * 0.4142...}.

278

279 @arg x: X component (C{scalar} or L{Fsum} instance).

280 @arg y: Y component (C{scalar} or L{Fsum} instance).

281

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

283

284 @see: Function L{euclid_}.

285 '''

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

287 if x < y:

288 x, y = y, x

289 return x + y * _0_4142 # XXX * _0_5 before 20.10.02

290

291

292 def euclid_(*xs):

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

294 cascaded L{euclid}.

295

296 @arg xs: X arguments, positional (C{scalar}s or L{Fsum} instances).

297

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

299

300 @see: Function L{euclid}.

301 '''

302 e = _0_0

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

304 # e = euclid(x, e)

305 if e < x:

306 e, x = x, e

307 if x:

308 e += x * _0_4142

309 return e

310

311

312 def facos1(x):

313 '''Fast approximation of L{pygeodesy.acos1}C{(B{x})}.

314

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

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

317 '''

318 a = fabs(x)

319 if a < EPS0:

320 r = PI_2

321 elif a < EPS1:

322 H = Fhorner(-a, 1.5707288, 0.2121144, 0.0742610, 0.0187293)

323 r = float(H * sqrt(_1_0 - a))

324 if x < 0:

325 r = PI - r

326 else:

327 r = PI if x < 0 else _0_0

328 return r

329

330

331 def fasin1(x): # PYCHOK no cover

332 '''Fast approximation of L{pygeodesy.asin1}C{(B{x})}.

333

334 @see: L{facos1}.

335 '''

336 return PI_2 - facos1(x)

337

338

339 def fatan(x):

340 '''Fast approximation of C{atan(B{x})}.

341 '''

342 a = fabs(x)

343 if a < _1_0:

344 r = fatan1(a) if a else _0_0

345 elif a > _1_0:

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

347 else:

348 r = PI_4

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

350 r = -r

351 return r

352

353

354 def fatan1(x):

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

356

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

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

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

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

361 IEEE Signal Processing Magazine, 111, May 2006.

362 '''

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

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

365 # x * (1.0300981633974482 + x * (-0.1784 - x * 0.0663))

366 H = Fhorner(x, _0_0, 1.0300981634, -0.1784, -0.0663)

367 return float(H)

368

369

370 def fatan2(y, x):

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

372

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

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

375 and L{fatan1}.

376 '''

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

378 if b > a:

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

380 elif a > b:

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

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

383 r = PI_4

384 else: # a == b == 0

385 return _0_0

386 if x < 0:

387 r = PI - r

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

389 r = -r

390 return r

391

392

393 def favg(v1, v2, f=_0_5):

394 '''Return the average of two values.

395

396 @arg v1: One value (C{scalar} or L{Fsum} instance).

397 @arg v2: Other value (C{scalar} or L{Fsum} instance).

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

399

400 @return: M{v1 + f * (v2 - v1)} (C{float}).

401 '''

402# @raise ValueError: Fraction out of range.

403# '''

404# if not 0 <= f <= 1: # XXX restrict fraction?

405# raise _ValueError(fraction=f)

406 # v1 + f * (v2 - v1) == v1 * (1 - f) + v2 * f

407 return fsum1_(v1, -f * v1, f * v2)

408

409

410 def fdot(a, *b):

411 '''Return the precision dot product M{sum(a[i] * b[i] for

412 i=0..len(a))}.

413

414 @arg a: Iterable, list, tuple, etc. (C{scalar}s).

415 @arg b: All positional arguments (C{scalar}s).

416

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

418

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

420

421 @see: Class L{Fdot} and U{Algorithm 5.10 B{DotK}

422 <https://www.TUHH.De/ti3/paper/rump/OgRuOi05.pdf>}.

423 '''

424 return fsum(_map_mul(a, b, fdot))

425

426

427 def fdot3(a, b, c, start=0):

428 '''Return the precision dot product M{start +

429 sum(a[i] * b[i] * c[i] for i=0..len(a)-1)}.

430

431 @arg a: Iterable, list, tuple, etc. (C{scalar}s).

432 @arg b: Iterable, list, tuple, etc. (C{scalar}s).

433 @arg c: Iterable, list, tuple, etc. (C{scalar}s).

434 @kwarg start: Optional bias (C{scalar}).

435

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

437

438 @raise LenError: Unequal C{len(B{a})}, C{len(B{b})}

439 and/or C{len(B{c})}.

440

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

442 '''

443 def _mul3(a, b, c): # map function

444 return a * b * c

445

446 def _mul3_(a, b, c, start):

447 yield start

448 for abc in map(_mul3, a, b, c):

449 yield abc

450

451 if not len(a) == len(b) == len(c):

452 raise LenError(fdot3, a=len(a), b=len(b), c=len(c))

453

454 return fsum(_mul3_(a, b, c, start) if start else map(_mul3, a, b, c))

455

456

457 def fhorner(x, *cs):

458 '''Evaluate the polynomial M{sum(cs[i] * x**i for

459 i=0..len(cs)-1)} using the Horner form.

460

461 @arg x: Polynomial argument (C{scalar}).

462 @arg cs: Polynomial coeffients (C{scalar}s).

463

464 @return: Horner value (C{float}).

465

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

467

468 @raise TypeError: Non-scalar B{C{x}}.

469

470 @raise ValueError: No B{C{cs}} coefficients or B{C{x}} is not finite.

471

472 @see: Function L{fpolynomial} and class L{Fhorner}.

473 '''

474 H = Fhorner(x, *cs)

475 return float(H)

476

477

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

479 '''Interpolate using U{Inverse Distance Weighting

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

481

482 @arg xs: Known values (C{scalar}s).

483 @arg ds: Non-negative distances (C{scalar}s).

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

485

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

487

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

489

490 @raise ValueError: Invalid B{C{beta}}, negative B{C{ds}} value,

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

492

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

494 '''

495 n, xs = len2(xs)

496 d, ds = len2(ds)

497 if n != d or n < 1:

498 raise LenError(fidw, xs=n, ds=d)

499

500 d, x = min(zip(ds, xs))

501 if d > EPS0 and n > 1:

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

503 if b < 0:

504 ws = tuple(float(d)**b for d in ds)

505 t = fsum(_1map_mul(xs, ws)) # Fdot(xs, *ws)

506 x = _over(t, fsum(ws, floats=True))

507 else: # b == 0

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

509 elif d < 0: # PYCHOK no cover

510 n = Fmt.SQUARE(distance=ds.index(d))

511 raise _ValueError(n, d, txt=_negative_)

512 return x

513

514

515 def fmean(xs):

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

517

518 @arg xs: Values (C{scalar} or L{Fsum} instances).

519

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

521

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

523

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

525 '''

526 n, xs = len2(xs)

527 if n < 1:

528 raise LenError(fmean, xs=xs)

529 return Fsum(*xs).fover(n) if n > 1 else _2float(index=0, xs=xs[0])

530

531

532 def fmean_(*xs):

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

534

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

536 '''

537 return fmean(xs)

538

539

540 def fpolynomial(x, *cs, **over):

541 '''Evaluate the polynomial M{sum(cs[i] * x**i for

542 i=0..len(cs)) [/ over]}.

543

544 @arg x: Polynomial argument (C{scalar}).

545 @arg cs: Polynomial coeffients (C{scalar}s), all

546 positional.

547 @kwarg over: Optional final, I{non-zero} divisor

548 (C{scalar}).

549

550 @return: Polynomial value (C{float}).

551

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

553

554 @raise TypeError: Non-scalar B{C{x}}.

555

556 @raise ValueError: No B{C{cs}} coefficients or B{C{x}} is not finite.

557

558 @see: Function L{fhorner} and class L{Fpolynomial}.

559 '''

560 P = Fpolynomial(x, *cs)

561 d = _xkwds_get(over, over=0) if over else 0

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

563

564

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

566 '''Return a series of powers M{[x**i for i=1..n]}.

567

568 @arg x: Value (C{scalar} or L{Fsum}).

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

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

571 exponent (C{int}).

572

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

574

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

576

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

578 '''

579 if not isint(n):

580 raise _IsnotError(int.__name__, n=n)

581 elif n < 1:

582 raise _ValueError(n=n)

583

584 p = x if isint(x) or isinstance(x, Fsum) else _2float(x=x)

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

586

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

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

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

590

591 return ps

592

593

594 try:

595 from math import prod as fprod # Python 3.8

596 except ImportError:

597

598 def fprod(xs, start=1):

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

600

601 @arg xs: Terms to be multiplied, an iterable, list,

602 tuple, etc. (C{scalar}s).

603 @kwarg start: Initial term, also the value returned

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

605

606 @return: The product (C{float}).

607

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

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

610 '''

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

612

613

614 def frandoms(n, seeded=None):

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

616

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

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

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

620

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

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

623 '''

624 from random import gauss, random, seed, shuffle

625

626 if seeded is None:

627 pass

628 elif seeded and isbool(seeded):

629 from time import localtime

630 seed(localtime().tm_yday)

631 elif isscalar(seeded):

632 seed(seeded)

633

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

635 for _ in range(n):

636 s = 0

637 t = list(c)

638 _a = t.append

639 for _ in range(n * 8):

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

641 _a(v)

642 s += v

643 shuffle(t)

644 yield t

645

646

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

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

649

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

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

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

653

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

655

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

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

658 '''

659 if not isint(number):

660 raise _IsnotError(int.__name__, number=number)

661 for i in range(number):

662 yield start + (step * i)

663

664

665 try:

666 from functools import reduce as freduce

667 except ImportError:

668 try:

669 freduce = reduce # PYCHOK expected

670 except NameError: # Python 3+

671

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

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

674 '''

675 if start:

676 r = v = start[0]

677 else:

678 r, v = 0, MISSING

679 for v in xs:

680 r = f(r, v)

681 if v is MISSING:

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

683 return r

684

685

686 def fremainder(x, y):

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

688

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

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

691

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

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

694

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

696

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

698 project/geographiclib/>} and Python 3.7+

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

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

701 '''

702 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and

703 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native

704 # fmod( 0, 360) == 0.0

705 # fmod( 360, 360) == 0.0

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

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

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

709 # however, using the % operator ...

710 # 0 % 360 == 0

711 # 360 % 360 == 0

712 # 360.0 % 360 == 0.0

713 # -0 % 360 == 0

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

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

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

717

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

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

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

721 if _isfinite(x):

722 try:

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

724 except Exception as e:

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

726 else: # handle x INF and NINF as NAN

727 r = NAN

728 return r

729

730

731 if _sys_version_info2 < (3, 8): # PYCHOK no cover

732 from math import hypot # OK in Python 3.7-

733

734 def hypot_(*xs):

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

736

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

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

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

740 handled differently.

741

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

743

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

745

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

747

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

749

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

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

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

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

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

755 '''

756 _, R = _h_xs2(xs, True, hypot_)

757 return float(R)

758

759 elif _sys_version_info2 < (3, 10):

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

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

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

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

764

765 def hypot(x, y):

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

767

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

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

770

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

772 '''

773 return (float(Fhypot(x, y, raiser=False)) if y else

774 fabs(x)) if x else fabs(y)

775

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

777 else:

778 from math import hypot # PYCHOK in Python 3.10+

779 hypot_ = hypot

780

781

782 def _h_xs2(xs, pot_, which):

783 '''(INTERNAL) Helper for L{hypot_} and L{hypot2_}.

784 '''

785 n, xs = len2(xs)

786 if n > 0:

787 h = float(max(map(abs, xs))) # NOT fabs!

788 if h < EPS0:

789 R = _0_0

790 elif n > 1:

791 if pot_:

792 if h != _1_0:

793 xs = ((x / h) for x in xs)

794 R = Fhypot(*xs, raiser=False) * h

795 else:

796 R = Fpowers(2, *xs)

797 else:

798 R = h if pot_ else (h**2)

799 return h, R

800

801 raise _ValueError(unstr(which, xs), txt=_too_(_few_))

802

803

804 def hypot1(x):

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

806

807 @arg x: Argument (C{scalar} or L{Fsum} instance).

808

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

810 '''

811 return hypot(_1_0, x) if x else _1_0

812

813

814 def hypot2(x, y):

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

816

817 @arg x: X argument (C{scalar} or L{Fsum} instance).

818 @arg y: Y argument (C{scalar} or L{Fsum} instance).

819

820 @return: C{B{x}**2 + B{y}**2} (C{float} or L{Fsum}).

821 '''

822 if abs(x) < abs(y): # NOT fabs!

823 x, y = y, x

824 if x:

825 h2 = x**2

826 if y:

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

828 else:

829 h2 = _0_0

830 return h2

831

832

833 def hypot2_(*xs):

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

835

836 @arg xs: X arguments (C{scalar}s or L{Fsum} instances),

837 all positional.

838

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

840

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

842

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

844

845 @see: Function L{hypot_}.

846 '''

847 _, R = _h_xs2(xs, False, hypot2_)

848 return float(R)

849

850

851 def _map_mul(a, b, where):

852 '''(INTERNAL) Yield each B{C{a * b}}.

853 '''

854 n = len(b)

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

856 raise LenError(where, a=len(a), b=n)

857 return _1map_mul(a, b) if n < 4 else map(_operator.mul, a, b)

858

859

860 def _1map_mul(a, b):

861 '''(INTERNAL) Yield each B{C{a * b}}, 1-primed.

862 '''

863 return _1primed(map(_operator.mul, a, b))

864

865

866 def norm2(x, y):

867 '''Normalize a 2-dimensional vector.

868

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

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

871

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

873

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

875 or zero norm.

876 '''

877 try:

878 h = hypot(x, y)

879 if h:

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

881 else:

882 x = _copysign_0_0(x) # pass?

883 y = _copysign_0_0(y)

884 except Exception as e:

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

886 return x, y

887

888

889 def norm_(*xs):

890 '''Normalize all n-dimensional vector components.

891

892 @arg xs: Components (C{scalar}s), all positional.

893

894 @return: Yield each component, normalized.

895

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

897 or zero norm.

898 '''

899 try:

900 i = x = h = None

901 h = hypot_(*xs)

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

903 for i, x in enumerate(xs):

904 yield x * _h

905 except Exception as X:

906 raise _xError(X, Fmt.SQUARE(xs=i), x, h=h)

907

908

909 def _powers(x, n):

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

911 '''

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

913 for _ in range(n):

914 p *= x

915 yield p

916

917

918 def _root(x, p, where):

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

920 '''

921 try:

922 if x > 0:

923 return pow(x, p)

924 elif x < 0:

925 raise ValueError(_negative_)

926 except Exception as X:

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

928 return _0_0

929

930

931 def sqrt0(x, Error=None):

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

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

934

935 @arg x: Value (C{scalar} or L{Fsum} instance).

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

937

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

939

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

941 returns C{0.0}.

942 '''

943 if Error and x < 0:

944 raise Error(unstr(sqrt0, x))

945 return _root(x, _0_5, sqrt0) if x > EPS02 else (_0_0 if x < EPS02 else EPS0)

946

947

948 def sqrt3(x):

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

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

951

952 @arg x: Value (C{scalar} or L{Fsum} instance).

953

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

955

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

957

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

959 '''

960 return _root(x, _1_5, sqrt3)

961

962

963 def sqrt_a(h, b):

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

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

966

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

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

969

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

971

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

973

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

975

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

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

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

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

980 '''

981 try:

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

983 raise TypeError(_not_scalar_)

984 c = fabs(h)

985 if c > EPS0:

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

987 if s < 0:

988 raise ValueError(_h_lt_b_)

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

990 else: # PYCHOK no cover

991 b = fabs(b)

992 d = c - b

993 if d < 0:

994 raise ValueError(_h_lt_b_)

995 d *= c + b

996 a = sqrt(d) if d else _0_0

997 except Exception as x:

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

999 return copysign0(a, h)

1000

1001

1002 def zcrt(x):

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

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

1005

1006 @arg x: Value (C{scalar} or L{Fsum} instance).

1007

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

1009

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

1011

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

1013 '''

1014 return _root(x, _1_6th, zcrt)

1015

1016

1017 def zqrt(x):

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

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

1020

1021 @arg x: Value (C{scalar} or L{Fsum} instance).

1022

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

1024

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

1026

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

1028 '''

1029 return _root(x, _0_125, zqrt)

1030

1031# **) MIT License

1032#

1033# Copyright (C) 2016-2024 -- mrJean1 at Gmail -- All Rights Reserved.

1034#

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

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

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

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

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

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

1041#

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

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

1044#

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

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

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

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

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

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

1051# OTHER DEALINGS IN THE SOFTWARE.