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, isint, isscalar, len2

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

11 _0_0, _0_5, _1_0, _N_1_0, _1_3rd, _1_5, \

12 _2_0, _2_3rd, _3_0, isnear0, isnear1, \

13 _isfinite, _over, remainder as _remainder

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

15 _xError, _xkwds_get, _xkwds_pop

16 from pygeodesy.fsums import _2float, _Powers, Fsum, _fsum, fsum, fsum1_, \

17 _pow_op_, Fmt, unstr

18 from pygeodesy.interns import MISSING, _few_, _h_, _negative_, _not_scalar_, \

19 _too_

20 from pygeodesy.lazily import _ALL_LAZY, _sys_version_info2

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

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

23

24 from math import fabs, sqrt # pow

25 from operator import mul as _mul # in .triaxials

26

27__all__ = _ALL_LAZY.fmath

28__version__ = '23.08.17'

29

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

31_0_4142 = 0.414213562373095 # sqrt(_2_0) - _1_0

32

33

34 class Fdot(Fsum):

35 '''Precision dot product.

36 '''

37 def __init__(self, a, *b, **name):

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

39 for i=0..len(a))}.

40

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

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

43 @kwarg name: Optional name (C{str}).

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)

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):

59 '''New L{Fhorner} evaluation of the polynomial

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

61

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

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

64 instances), all positional.

65 @kwarg name: Optional name (C{str}).

66

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

68

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

70

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

72

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

74 '''

75 Fsum.__init__(self, *cs[-1:], **name)

76 if len(cs) > 1:

77 x = _2float(x=x)

78 _a = self._fadd # (other, op)

79 _f = self._finite # (other, op)

80 op = Fhorner.__name__

81 ps = self._ps

82 for c in reversed(cs[:-1]): # multiply-accumulate

83 ps[:] = [_f(p * x, op) for p in ps]

84 _a(c, op)

85 # assert self._ps is ps

86

87

88 class Fhypot(Fsum):

89 '''Precision hypotenuse of summation.

90 '''

91 def __init__(self, *xs, **power_name_RESIDUAL):

92 '''New L{Fhypot} hypotenuse of (the I{power} of) several

93 C{scalar} or C{Fsum} values.

94

95 @arg xs: One or more values to include (each C{scalar}

96 or an C{Fsum} instance).

97 @kwarg power_name_RESIDUAL: Optional exponent and root

98 order C{B{power}=2}, C{B{name}=NN} and

99 C{B{RESIDUAL}=None}, see L{Fsum.__init__}.

100 '''

101 try:

102 p = _xkwds_pop(power_name_RESIDUAL, power=2)

103 Fsum.__init__(self, **power_name_RESIDUAL)

104 if xs:

105 self._facc(_Powers(p, xs), up=False) # PYCHOK yield

106 self._fset(self._fpow(_1_0 / p, _pow_op_), asis=True)

107 except Exception as X:

108 raise self._ErrorX(X, xs, power=p)

109

110

111 class Fpolynomial(Fsum):

112 '''Precision polynomial evaluation.

113 '''

114 def __init__(self, x, *cs, **name):

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

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

117

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

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

120 positional.

121 @kwarg name: Optional name (C{str}).

122

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

124

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

126

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

128

129 @see: Function L{fpolynomial} and method L{Fsum.fadd}.

130 '''

131 Fsum.__init__(self, *cs[:1], **name)

132 n = len(cs) - 1

133 if n > 0:

134 self.fadd(_map_mul(cs[1:], fpowers(x, n), Fpolynomial))

135

136

137 class Fpowers(Fsum):

138 '''Precision summation or powers, optimized for C{power=2}.

139 '''

140 def __init__(self, power, *xs, **name_RESIDUAL):

141 '''New L{Fpowers} sum of (the I{power} of) several C{scalar}

142 or C{Fsum} values.

143

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

145 @arg xs: One or more values to include (each C{scalar}

146 or an C{Fsum} instance).

147 @kwarg power_name_RESIDUAL: Optional exponent and root

148 order C{B{power}=2}, C{B{name}=NN} and

149 C{B{RESIDUAL}=None}, see L{Fsum.__init__}.

150 '''

151 try:

152 Fsum.__init__(self, **name_RESIDUAL)

153 if xs:

154 self._facc(_Powers(power, xs), up=False) # PYCHOK yield

155 except Exception as X:

156 raise self._ErrorX(X, xs, power=power)

157

158

159 class Fn_rt(Fsum):

160 '''Precision n-th root of summation.

161 '''

162 def __init__(self, root, *xs, **name_RESIDUAL):

163 '''New L{Fn_rt} root of the precision sum of several

164 C{scalar} or C{Fsum} values.

165

166 @arg root: The order (C{scalar} or C{Fsum}).

167 @arg xs: Values to include (each C{scalar} or an

168 C{Fsum} instance).

169 @kwarg name_RESIDUAL: See L{Fsum.__init__}.

170 '''

171 try:

172 Fsum.__init__(self, *xs, **name_RESIDUAL)

173 self._fset(self._fpow(_1_0 / root, _pow_op_), asis=True)

174 except Exception as X:

175 raise self._ErrorX(X, xs, root=root)

176

177

178 class Fcbrt(Fn_rt):

179 '''Precision cubic root of summation.

180 '''

181 def __init__(self, *xs, **name_RESIDUAL):

182 '''New L{Fcbrt} cubic root of the precision sum of

183 several C{scalar} or C{Fsum} values.

184

185 @arg xs: Values to include (each C{scalar} or an

186 C{Fsum} instance).

187 @kwarg name_RESIDUAL: See L{Fsum.__init__}.

188 '''

189 Fn_rt.__init__(self, _3_0, *xs, **name_RESIDUAL)

190

191

192 class Fsqrt(Fn_rt):

193 '''Precision square root of summation.

194 '''

195 def __init__(self, *xs, **name_RESIDUAL):

196 '''New L{Fsqrt} square root of the precision sum of

197 several C{scalar} or C{Fsum} values.

198

199 @arg xs: Values to include (each C{scalar} or an

200 C{Fsum} instance).

201 @kwarg name_RESIDUAL: See L{Fsum.__init__}.

202 '''

203 Fn_rt.__init__(self, _2_0, *xs, **name_RESIDUAL)

204

205

206 try:

207 from math import cbrt # Python 3.11+

208

209 def cbrt2(x):

210 '''Compute the cube root I{squared} M{x**(2/3)}.

211 '''

212 return cbrt(x)**2 # cbrt(-0.0*2) == -0.0

213

214 except ImportError: # Python 3.10-

215

216 def cbrt(x):

217 '''Compute the cube root M{x**(1/3)}.

218

219 @arg x: Value (C{scalar}).

220

221 @return: Cubic root (C{float}).

222

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

224 '''

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

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

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

228 return _copysign(pow(fabs(x), _1_3rd), x) # cbrt(-0.0) == -0.0

229

230 def cbrt2(x): # PYCHOK attr

231 '''Compute the cube root I{squared} M{x**(2/3)}.

232

233 @arg x: Value (C{scalar}).

234

235 @return: Cube root I{squared} (C{float}).

236

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

238 '''

239 return pow(fabs(x), _2_3rd) # XXX pow(fabs(x), _1_3rd)**2

240

241

242 def euclid(x, y):

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

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

245

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

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

248

249 @return: Appoximate norm (C{float}).

250

251 @see: Function L{euclid_}.

252 '''

253 x, y = fabs(x), fabs(y)

254 if x < y:

255 x, y = y, x

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

257

258

259 def euclid_(*xs):

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

261 by cascaded L{euclid}.

262

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

264

265 @return: Appoximate norm (C{float}).

266

267 @see: Function L{euclid}.

268 '''

269 e = _0_0

270 for x in sorted(map(fabs, xs)): # XXX not reverse=True

271 # e = euclid(x, e)

272 if e < x:

273 e, x = x, e

274 if x:

275 e += x * _0_4142

276 return e

277

278

279 def facos1(x):

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

281

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

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

284 '''

285 a = fabs(x)

286 if a < EPS0:

287 r = PI_2

288 elif a < EPS1:

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

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

291 if x < 0:

292 r = PI - r

293 else:

294 r = PI if x < 0 else _0_0

295 return r

296

297

298 def fasin1(x): # PYCHOK no cover

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

300

301 @see: L{facos1}.

302 '''

303 return PI_2 - facos1(x)

304

305

306 def fatan(x):

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

308 '''

309 a = fabs(x)

310 if a < _1_0:

311 r = fatan1(a) if a else _0_0

312 elif a > _1_0:

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

314 else:

315 r = PI_4

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

317 r = -r

318 return r

319

320

321 def fatan1(x):

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

323

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

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

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

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

328 IEEE Signal Processing Magazine, 111, May 2006.

329 '''

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

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

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

333 H = Fhorner(x, _0_0, 1.0300982, -0.1784, -0.0663)

334 return float(H)

335

336

337 def fatan2(y, x):

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

339

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

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

342 and L{fatan1}.

343 '''

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

345 if a < b:

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

347 elif b < a:

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

349 elif a: # == b != 0

350 r = PI_4

351 else: # a == b == 0

352 return _0_0

353 if x < 0:

354 r = PI - r

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

356 r = -r

357 return r

358

359

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

361 '''Return the average of two values.

362

363 @arg v1: One value (C{scalar}).

364 @arg v2: Other value (C{scalar}).

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

366

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

368 '''

369# @raise ValueError: Fraction out of range.

370# '''

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

372# raise _ValueError(fraction=f)

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

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

375

376

377 def fdot(a, *b):

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

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

380

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

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

383

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

385

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

387

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

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

390 '''

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

392

393

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

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

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

397

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

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

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

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

402

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

404

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

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

407

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

409 '''

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

411 return a * b * c

412

413 def _muly(a, b, c, start):

414 yield start

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

416 yield abc

417

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

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

420

421 return fsum(_muly(a, b, c, start) if start else map(_mul3, a, b, c))

422

423

424 def fhorner(x, *cs):

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

426 i=0..len(cs))} using the Horner form.

427

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

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

430

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

432

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

434

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

436

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

438

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

440 '''

441 H = Fhorner(x, *cs)

442 return float(H)

443

444

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

446 '''Interpolate using U{Inverse Distance Weighting

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

448

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

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

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

452

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

454

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

456

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

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

459

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

461 '''

462 n, xs = len2(xs)

463 d, ds = len2(ds)

464 if n != d or n < 1:

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

466

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

468 if d > EPS0 and n > 1:

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

470 if b < 0:

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

472 t = fsum(_map_mul1(xs, ws)) # fdot(xs, *ws)

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

474 else: # b == 0

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

476 elif d < 0: # PYCHOK no cover

477 n = Fmt.SQUARE(ds=ds.index(d))

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

479 return x

480

481

482 def fmean(xs):

483 '''Compute the accurate mean M{sum(xs[i] for

484 i=0..len(xs)) / len(xs)}.

485

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

487

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

489

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

491

492 @raise ValueError: No B{C{xs}} values.

493 '''

494 n, xs = len2(xs)

495 if n > 0:

496 return fsum(xs) / n # if n > 1 else _2float(index=0, xs=xs[0])

497 raise _ValueError(xs=xs)

498

499

500 def fmean_(*xs):

501 '''Compute the accurate mean M{sum(xs[i] for

502 i=0..len(xs)) / len(xs)}.

503

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

505

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

507

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

509

510 @raise ValueError: No B{C{xs}} values.

511 '''

512 return fmean(xs)

513

514

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

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

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

518

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

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

521 positional.

522 @kwarg over: Optional, final divisor (C{scalar}

523

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

525

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

527

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

529

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

531

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

533 '''

534 P = Fpolynomial(x, *cs)

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

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

537

538

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

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

541

542 @arg x: Value (C{scalar}).

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

544 @kwarg alts: Only alternating powers, starting with

545 this exponent (C{int}).

546

547 @return: Powers of B{C{x}} (C{float}s or C{int}s).

548

549 @raise TypeError: Non-scalar B{C{x}} or B{C{n}} not C{int}.

550

551 @raise ValueError: Non-finite B{C{x}} or non-positive B{C{n}}.

552 '''

553 if not isint(n):

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

555 elif n < 1:

556 raise _ValueError(n=n)

557

558 p = t = x if isint(x) else _2float(x=x)

559 ps = [p]

560 _a = ps.append

561 for _ in range(1, n):

562 p *= t

563 _a(p)

564

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

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

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

568

569 return ps

570

571

572 try:

573 from math import prod as fprod # Python 3.8

574 except ImportError:

575

576 def fprod(xs, start=1):

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

578

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

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

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

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

583

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

585

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

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

588 '''

589 return freduce(_mul, xs, start)

590

591

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

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

594

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

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

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

598

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

600

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

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

603 '''

604 if not isint(number):

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

606 for i in range(number):

607 yield start + i * step

608

609

610 try:

611 from functools import reduce as freduce

612 except ImportError:

613 try:

614 freduce = reduce # PYCHOK expected

615 except NameError: # Python 3+

616

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

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

619 '''

620 if start:

621 r = v = start[0]

622 else:

623 r, v = 0, MISSING

624 for v in xs:

625 r = f(r, v)

626 if v is MISSING:

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

628 return r

629

630

631 def fremainder(x, y):

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

633

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

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

636

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

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

639

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

641

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

643 project/geographiclib/>} and Python 3.7+

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

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

646 '''

647 # with Python 2.7.16 and 3.7.3 on macOS 10.13.6 and

648 # with Python 3.10.2 on macOS 12.2.1 M1 arm64 native

649 # fmod( 0, 360) == 0.0

650 # fmod( 360, 360) == 0.0

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

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

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

654 # however, using the % operator ...

655 # 0 % 360 == 0

656 # 360 % 360 == 0

657 # 360.0 % 360 == 0.0

658 # -0 % 360 == 0

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

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

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

662

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

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

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

666 if _isfinite(x):

667 try:

668 r = _remainder(x, y) if x else x

669 except Exception as e:

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

671 else: # handle x INF and NINF as NAN

672 r = NAN

673 return r

674

675

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

677 from math import hypot # OK in Python 3.7-

678

679 def hypot_(*xs):

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

681

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

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

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

685 handled differently.

686

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

688

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

690

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

692

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

694

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

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

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

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

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

700 '''

701 h, x2 = _h_x2(xs)

702 return (h * sqrt(x2)) if x2 else _0_0

703

704 elif _sys_version_info2 < (3, 10):

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

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

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

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

709

710 def hypot(x, y):

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

712

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

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

715

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

717 '''

718 if x:

719 h = sqrt(x**2 + y**2) if y else fabs(x)

720 elif y:

721 h = fabs(y)

722 else:

723 h = _0_0

724 return h

725

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

727 else:

728 from math import hypot # PYCHOK in Python 3.10+

729 hypot_ = hypot

730

731

732 def _h_x2(xs):

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

734 '''

735 if xs:

736 n, xs = len2(xs)

737 if n > 0:

738 h = float(max(map(fabs, xs)))

739 if h < EPS0:

740 x2 = _0_0

741 else: # math.fsum, see C{_hypot21_} below

742 x2 = _fsum(_x2_h2(_1_0, xs, h, _N_1_0))

743 return h, x2

744

745 raise _ValueError(xs=xs, txt=_too_(_few_))

746

747

748 def hypot1(x):

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

750

751 @arg x: Argument (C{scalar}).

752

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

754 '''

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

756

757

758 def hypot2(x, y):

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

760

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

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

763

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

765 '''

766 if x:

767 if y:

768 if fabs(x) < fabs(y):

769 x, y = y, x

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

771 else:

772 h2 = x**2

773 elif y:

774 h2 = y**2

775 else:

776 h2 = _0_0

777 return h2

778

779

780 def hypot2_(*xs):

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

782

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

784

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

786

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

788

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

790

791 @see: Function L{hypot_}.

792 '''

793 h, x2 = _h_x2(xs)

794 return (h**2 * x2) if x2 else _0_0

795

796

797 def _map_mul(a, b, where):

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

799 '''

800 n = len(b)

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

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

803 return map(_mul, a, b) if n > 3 else _map_mul1(a, b)

804

805

806 def _map_mul1(a, b):

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

808 '''

809 yield _1_0

810 for ab in map(_mul, a, b):

811 if ab:

812 yield ab

813 yield _N_1_0

814

815

816 def norm2(x, y):

817 '''Normalize a 2-dimensional vector.

818

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

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

821

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

823

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

825 or zero norm.

826 '''

827 h = hypot(x, y)

828 if not h:

829 x = y = _0_0 # pass?

830 elif not isnear1(h):

831 try:

832 x, y = x / h, y / h

833 except Exception as e:

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

835 return x, y

836

837

838 def norm_(*xs):

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

840

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

842

843 @return: Yield each component, normalized.

844

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

846 or zero norm.

847 '''

848 h = hypot_(*xs)

849 if h:

850 try:

851 for i, x in enumerate(xs):

852 yield x / h

853 except Exception as e:

854 raise _xError(e, Fmt.SQUARE(xs=i), x, _h_, h)

855 else:

856 for _ in xs:

857 yield _0_0

858

859

860 def sqrt0(x):

861 '''Return the square root iff C{B{x} >} L{EPS02}.

862

863 @arg x: Value (C{scalar}).

864

865 @return: Square root (C{float}) or C{0.0}.

866

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

868 returns C{0.0}.

869 '''

870 return sqrt(x) if x > EPS02 else (_0_0 if x < EPS02 else EPS0)

871

872

873 def sqrt3(x):

874 '''Compute the square root, I{cubed} M{sqrt(x)**3} or M{sqrt(x**3)}.

875

876 @arg x: Value (C{scalar}).

877

878 @return: Square root I{cubed} (C{float}).

879

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

881

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

883 '''

884 if x < 0:

885 raise _ValueError(x=x, txt=_negative_)

886 return pow(x, _1_5) if x else _0_0

887

888

889 def sqrt_a(h, b):

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

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

892

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

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

895

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

897

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

899

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

901

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

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

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

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

906 '''

907 try:

908 if not (isscalar(h) and isscalar(b)):

909 raise TypeError(_not_scalar_)

910 elif isnear0(h): # PYCHOK no cover

911 c, b = fabs(h), fabs(b)

912 d = c - b

913 if d < 0:

914 raise ValueError('abs(h) < abs(b)')

915 a = copysign0(sqrt((c + b) * d), h) if d > 0 else _0_0

916 else:

917 c = float(h)

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

919 if s < 0:

920 raise ValueError('abs(h) < abs(b)')

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

922 except Exception as x:

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

924 return a

925

926

927 def _x2_h2(s, xs, h, e):

928 '''(INTERNAL) Yield M{(x / h)**2 for x in xs}.

929 '''

930 yield s

931 if h in (_0_0, _1_0):

932 for x in xs:

933 if x:

934 yield x**2

935 else:

936 for x in xs:

937 if x:

938 yield (x / h)**2

939 yield e

940

941# **) MIT License

942#

943# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved.

944#

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

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

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

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

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

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

951#

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

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

954#

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

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

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

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

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

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

961# OTHER DEALINGS IN THE SOFTWARE.