Coverage for pygeodesy/auxilats/auxDST.py: 97%
104 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-09-15 09:43 -0400
« prev ^ index » next coverage.py v7.2.2, created at 2023-09-15 09:43 -0400
2# -*- coding: utf-8 -*-
4u'''Discrete Sine Transforms (AuxDST) in Python, transcoded from I{Karney}'s C++ class
5U{DST<https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1DST.html>}
6in I{GeographicLib version 2.2+}.
8Copyright (C) U{Charles Karney<mailto:Karney@Alum.MIT.edu>} (2022-2023) and licensed
9under the MIT/X11 License. For more information, see the U{GeographicLib
10<https://GeographicLib.SourceForge.io>} documentation.
12@note: Class L{AuxDST} requires package U{numpy<https://PyPI.org/project/numpy>} to be
13 installed, version 1.16 or newer and needed for I{exact} area calculations.
14'''
15# make sure int/int division yields float quotient, see .basics
16from __future__ import division as _; del _ # PYCHOK semicolon
18from pygeodesy.auxilats.auxily import _Dm
19from pygeodesy.basics import isodd, map2, neg, _reverange, _xnumpy
20from pygeodesy.constants import PI_2, PI_4, isfinite, _0_0, _0_5, _naninf
21# from pygeodesy.fsums import Fsum # from .karney
22from pygeodesy.karney import _2cos2x, _ALL_DOCS, Fsum, property_RO
23# from pygeodesy.lazily import _ALL_DOCS # from .karney
24# from pygeodesy.props import property_RO # from .karney
26__all__ = ()
27__version__ = '23.09.14'
30class AuxDST(object):
31 '''Discrete Sine Transforms (DST) for I{Auxiliary Latitudes}.
33 @see: I{Karney}'s C++ class U{DST
34 <https://GeographicLib.SourceForge.io/C++/doc/classGeographicLib_1_1DST.html>}.
35 '''
36 _N = 0
38 def __init__(self, N):
39 '''New L{AuxDST} instance.
41 @arg N: Size, number of points (C{int}).
42 '''
43 if N > 0:
44 self._N = int(N)
45 # kissfft(N, False) # size, inverse
47 @staticmethod
48 def evaluate(sinx, cosx, F, *N):
49 '''Evaluate the Fourier sum given the sine and cosine of the angle,
50 using precision I{Clenshaw} summation.
52 @arg sinx: The sin(I{sigma}) (C{float}).
53 @arg cosx: The cos(I{sigma}) (C{float}).
54 @arg F: The Fourier coefficients (C{float}[]).
55 @arg N: Optional, (smaller) number of terms to evaluate (C{int}).
57 @return: Precison I{Clenshaw} sum (C{float}).
59 @see: Methods C{AuxDST.integral} and C{AuxDST.integral2}.
60 '''
61 a = -_2cos2x(cosx, sinx)
62 if isfinite(a):
63 Y0, Y1 = Fsum(), Fsum()
64 n = _len_N(F, *N)
65 Fn = list(F[:n])
66 _F = Fn.pop
67 if isodd(n):
68 Y0 -= _F()
69 while Fn: # Y0, Y1 negated
70 Y1 -= Y0 * a + _F()
71 Y0 -= Y1 * a + _F()
72 r = float(_Dm(-Y0, Y1, sinx))
73 else:
74 r = _naninf(-a)
75 return r
77 @property_RO
78 def _fft_numpy(self):
79 '''(INTERNAL) Get the C{numpy.fft} module, I{once}.
80 '''
81 AuxDST._fft_numpy = fft = _xnumpy(AuxDST, 1, 16).fft # overwrite property_RO
82 return fft
84 def _fft_real(self, data):
85 '''(INTERNAL) NumPy's I{kissfft}-like C{transform_real} function,
86 taking C{float}[:N] B{C{data}} and returning C{complex}[:N*2].
87 '''
88 # <https://GitHub.com/mborgerding/kissfft/blob/master/test/testkiss.py>
89 return self._fft_numpy.rfftn(data)
91 def _ffts(self, data, cIV):
92 '''(INTERNAL) Compute the DST-III or DST-IV FFTransforms.
94 @arg data: Elements DST-III[0:N+1] or DST-IV[0:N] (C{float}[])
95 with DST_III[0] = 0.
96 @arg cIV: If C{True} DST-IV, otherwise DST-III.
98 @return: FFTransforms (C{float}[0:N]).
99 '''
100 t, N = (), self.N
101 if N > 0:
102 N2 = N * 2
103 d = tuple(data)
104 # assert len(d) == N + (0 if cIV else 1)
106 if cIV: # DST-IV
107 from cmath import exp as _cexp
109 def _cF(c, j, r=-PI_4 / N):
110 return c * _cexp(complex(0, r * j))
112 i = 0
113 else: # DST-III
114 i = 1
115 # assert d[0] == _0_0
117 def _cF(c, unused): # PYCHOK redef
118 return c
120 d += tuple(reversed(d[i:N])) # i == len(d) - N
121 d += tuple(map(neg, d[:N2]))
122 c = self._fft_real(d) # complex[0:N*2]
123 n2 = float(-N2)
124 t = tuple(_cF(c[j], j).imag / n2 for j in range(1, N2, 2))
125 return t
127 def _ffts2(self, data, F):
128 '''(INTERNAL) Doubled FFTransforms.
130 @arg data: Grid centers (C{float}[N]).
131 @arg F: The transforms (C{float}[N])
133 @return: Doubled FFTransforms (C{float}[N*2]).
134 '''
135 __2 = _0_5
137 def _dmF_2(d, F):
138 return (d - F) * __2
140 def _dpF_2(d, F):
141 return (d + F) * __2
143 N = self._N
144 # copy DST-IV order N transform to d[0:N]
145 d = self._ffts(data, True)
146 # assert len(d) >= N and len(F) >= N
147 # (DST-IV order N - DST-III order N) / 2
148 m = map2(_dmF_2, d[:N], F[:N])
149 # (DST-IV order N + DST-III order N) / 2
150 p = map2(_dpF_2, d[:N], F[:N])
151 return p + tuple(reversed(m))
153 @staticmethod
154 def integral(sinx, cosx, F, *N):
155 '''Evaluate the integral of Fourier sum given the sine and
156 cosine of the angle, using precision I{Clenshaw} summation.
158 @arg sinx: The sin(I{sigma}) (C{float}).
159 @arg cosx: The cos(I{sigma}) (C{float}).
160 @arg F: The Fourier coefficients (C{float}[]).
161 @arg N: Optional, C{len(B{F})} or a (smaller) number of
162 terms to evaluate (C{int}).
164 @return: Precison I{Clenshaw} intergral (C{float}).
166 @see: Methods C{AuxDST.evaluate} and C{AuxDST.integral2}.
167 '''
168 a = _2cos2x(cosx - sinx, cosx + sinx)
169 if isfinite(a):
170 Y0, Y1 = Fsum(), Fsum()
171 for r in _reverscaled(F, *N):
172 Y1 -= Y0 * a + r
173 Y1, Y0 = Y0, -Y1
174 r = float(_Dm(Y1, Y0, cosx))
175 else:
176 r = _naninf(a)
177 return r
179 @staticmethod
180 def integral2(sinx, cosx, siny, cosy, F, *N): # PYCHOK no cover
181 '''Compute the definite integral of Fourier sum given the
182 sine and cosine of the angles at the end points, using
183 precision I{Clenshaw} summation.
185 @arg sinx: The sin(I{sigma1}) (C{float}).
186 @arg cosx: The cos(I{sigma1}) (C{float}).
187 @arg siny: The sin(I{sigma2}) (C{float}).
188 @arg cosy: The cos(I{sigma2}) (C{float}).
189 @arg F: The Fourier coefficients (C{float}[]).
190 @arg N: Optional, C{len(B{F})} or a (smaller) number of
191 terms to evaluate (C{int}).
193 @return: Precison I{Clenshaw} integral (C{float}).
195 @see: Methods C{AuxDST.evaluate} and C{AuxDST.integral}.
196 '''
197 # 2 * cos(y - x) * cos(y + x) -> 2 * cos(2 * x)
198 c = _2cos2x(cosy * cosx, siny * sinx)
199 # -2 * sin(y - x) * sin(y + x) -> 0
200 s = -_2cos2x(siny * cosx, cosy * sinx)
201 if isfinite(c) and isfinite(s):
202 Y0, Y1 = Fsum(), Fsum()
203 Z0, Z1 = Fsum(), Fsum()
204 for r in _reverscaled(F, *N):
205 Y1 -= Y0 * c + Z0 * s + r
206 Z1 -= Y0 * s + Z0 * c
207 Y1, Y0 = Y0, -Y1
208 Z1, Z0 = Z0, -Z1
209 r = float(_Dm(Y1, Y0, cosy - cosx) +
210 _Dm(Z1, Z0, cosy + cosx))
211 else:
212 r = _naninf(c, s)
213 return r
215 @property_RO
216 def N(self):
217 '''Get this DST's size, number of points (C{int}).
218 '''
219 return self._N
221 def refine(self, f, F):
222 '''Refine the Fourier series by doubling the sampled points.
224 @arg f: Single-argument callable (C{B{f}(sigma)}).
225 @arg F: The initial Fourier series coefficients (C{float}[:N]).
227 @return: Fourier series coefficients (C{float}[:N*2]).
228 '''
229 def _data(_f, N): # [:N]
230 if N > 0:
231 r = PI_4 / N
232 for j in range(1, N*2, 2):
233 yield _f(r * j)
235 return self._ffts2(_data(f, self.N), F)
237 def reset(self, N):
238 '''Reset this DST.
240 @arg N: Size, number of points (C{int}).
242 @return: The new size (C{int}, non-negative).
243 '''
244 self._N = N = max(0, N)
245 # kissfft.assign(N*2, False) # "reset" size, inverse
246 return N
248 def transform(self, f):
249 '''Determine C{N + 1} terms in the Fourier series.
251 @arg f: Single-argument callable (C{B{f}(sigma)}).
253 @return: Fourier series coefficients (C{float}[:N+1],
254 leading 0).
255 '''
256 def _data(_f, N): # [:N + 1]
257 yield _0_0 # data[0] = 0
258 if N > 0:
259 r = PI_2 / N
260 for i in range(1, N + 1):
261 yield _f(r * i)
263 return self._ffts(_data(f, self.N), False)
266def _len_N(F, *N):
267 # Adjusted C{len(B{F})}.
268 return min(len(F), *N) if N else len(F)
271def _reverscaled(F, *N):
272 # Yield F[:N], reversed and scaled
273 for n in _reverange(_len_N(F, *N)):
274 yield F[n] / float(n * 2 + 1)
277__all__ += _ALL_DOCS(AuxDST)
279# **) MIT License
280#
281# Copyright (C) 2023-2023 -- mrJean1 at Gmail -- All Rights Reserved.
282#
283# Permission is hereby granted, free of charge, to any person obtaining a
284# copy of this software and associated documentation files (the "Software"),
285# to deal in the Software without restriction, including without limitation
286# the rights to use, copy, modify, merge, publish, distribute, sublicense,
287# and/or sell copies of the Software, and to permit persons to whom the
288# Software is furnished to do so, subject to the following conditions:
289#
290# The above copyright notice and this permission notice shall be included
291# in all copies or substantial portions of the Software.
292#
293# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
294# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
295# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
296# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
297# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
298# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
299# OTHER DEALINGS IN THE SOFTWARE.