1

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

3

4 u'''(INTERNAL) Private L{geodesicx} base class, functions and constants.

5

6 Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2008-2023)

7 and licensed under the MIT/X11 License. For more information, see the

8 U{GeographicLib<https://GeographicLib.SourceForge.io>} documentation.

9'''

10

11 from pygeodesy.basics import isodd, _MODS

12 from pygeodesy.constants import _0_0, _100_0

13 from pygeodesy.errors import _or, _xkwds_item2, _not_

14 from pygeodesy.fmath import hypot as _hypot

15# from pygeodesy.interns import _not_ # from .errors

16 from pygeodesy.karney import _CapsBase, GeodesicError, _2cos2x, _sum2_

17# from pygeodesy.lazily import _MODS, printf # .basics, _MODS

18

19 from math import ldexp as _ldexp

20

21__all__ = ()

22__version__ = '24.03.15'

23

24# valid C{nC4}s and C{C4order}s, see _xnC4 below

25_nC4s = {24: 2900, 27: 4032, 30: 5425}

26

27

28 class _GeodesicBase(_CapsBase): # in .geodsolve

29 '''(INTERNAL) Base class for C{[_]Geodesic*Exact}.

30 '''

31# def toRepr(self, prec=6, sep=_COMMASPACE_, **unused): # PYCHOK signature

32# '''Return this C{GeodesicExact*} items string.

33#

34# @kwarg prec: The C{float} precision, number of decimal digits (0..9).

35# Trailing zero decimals are stripped for B{C{prec}} values

36# of 1 and above, but kept for negative B{C{prec}} values.

37# @kwarg sep: Separator to join (C{str}).

38#

39# @return: C{GeodesicExact*} (C{str}).

40# '''

41# return Fmt.PAREN(self.named, self.toStr(prec=prec, sep=sep))

42 pass

43

44

45 class _Gfloats(dict):

46 '''(INTERNAL) Uniquify floats.

47 '''

48 n = 0 # total number of floats

49 nC4 = 0

50

51 def __init__(self, nC4): # PYCHOK signature

52 self.nC4 = nC4

53

54 def __call__(self, fs):

55 '''Return a tuple of "uniquified" floats.

56 '''

57 self.n += len(fs)

58 _f = self.setdefault

59 return tuple(_f(f, f) for f in map(float, fs)) # PYCHOK as attr

60

61 def prints(self):

62 n, u = self.n, len(self.keys())

63 d = (n - u) * _100_0 / n

64 _MODS.lazily.printf('_CX_%d: n=%d, u=%d, d=%.1f%%', self.nC4, n, u, d) # XXX

65

66

67 def _cosSeries(c4s, sx, cx): # PYCHOK shared .geodesicx.gx and -.gxline

68 '''(INTERNAL) I{Karney}'s cosine series expansion using U{Clenshaw

69 summation<https://WikiPedia.org/wiki/Clenshaw_algorithm>}.

70 '''

71 ar = _2cos2x(cx, sx)

72 y0 = t0 = y1 = t1 = _0_0

73 c4 = list(c4s)

74 _c4 = c4.pop

75 _s2 = _sum2_

76 if isodd(len(c4)):

77 y0 = _c4()

78 while c4:

79 # y1 = ar * y0 - y1 + c4.pop()

80 # y0 = ar * y1 - y0 + c4.pop()

81 y1, t1 = _s2(ar * y0, ar * t0, -y1, -t1, _c4())

82 y0, t0 = _s2(ar * y1, ar * t1, -y0, -t0, _c4())

83 # s = cx * (y0 - y1)

84 s, _ = _s2(cx * y0, _0_0, cx * t0, -cx * y1, -cx * t1)

85 return s

86

87

88_f = float # in _f2 and .geodesicx._C4_24, _27 and _30

89

90

91 def _f2(hi, lo): # in .geodesicx._C4_24, _27 and _30

92 '''(INTERNAL) For C{_coeffs}.

93 '''

94 return _ldexp(_f(hi), 52) + _f(lo)

95

96

97 def _sincos12(sin1, cos1, sin2, cos2, sineg0=False): # PYCHOK shared

98 '''(INTERNAL) Compute the sine and cosine of angle

99 M{ang12 = atan2(sin2, cos2) - atan2(sin1, cos1)}.

100

101 Negate C{sin1} to get C{sin12} and C{cos12} of the sum

102 M{ang12 = atan2(sin2, cos2) + atan2(sin1, cos1)}.

103

104 @kwarg sineg0: If C{True}, make negative C{sin12} zero (C{bool}).

105

106 @return: 2-Tuple C{(sin12, cos12)}.

107 '''

108 s = sin2 * cos1 - sin1 * cos2

109 c = cos2 * cos1 + sin1 * sin2

110 if sineg0 and s < 0:

111 s = _0_0 # max(s, _0_0) or NEG0?

112 return s, c

113

114

115 def _sin1cos2(sin1, cos1, sin2, cos2): # PYCHOK shared

116 '''(INTERNAL) Compute the C{sin1 * cos2} sine and its cosine.

117

118 @return: 2-Tuple C{(sin1 * cos2, hypot(sin1 * sin2, cos1)}.

119 '''

120 s = sin1 * cos2

121 c = _hypot(sin1 * sin2, cos1)

122 return s, c # _norm2(s, c)

123

124

125 def _xnC4(**name_nC4):

126 '''(INTERNAL) Validate C{C4order}.

127 '''

128 n, nC4 = _xkwds_item2(name_nC4)

129 if nC4 not in _nC4s or not isinstance(nC4, int):

130 raise GeodesicError(n, nC4, txt=_not_(_or(*map(str, _nC4s))))

131 return _nC4s[nC4]

132

133

134# **) MIT License

135#

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

137#

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

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

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

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

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

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

144#

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

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

147#

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

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

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

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

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

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

154# OTHER DEALINGS IN THE SOFTWARE.