1+ import math
12import numpy as np
23
34from matplotlib import _api , rcParams
@@ -52,8 +53,8 @@ def cla(self):
5253
5354 self .grid (rcParams ['axes.grid' ])
5455
55- Axes .set_xlim (self , - np .pi , np .pi )
56- Axes .set_ylim (self , - np .pi / 2.0 , np .pi / 2.0 )
56+ Axes .set_xlim (self , - math .pi , math .pi )
57+ Axes .set_ylim (self , - math .pi / 2.0 , math .pi / 2.0 )
5758
5859 def _set_lim_and_transforms (self ):
5960 # A (possibly non-linear) projection on the (already scaled) data
@@ -88,7 +89,7 @@ def _set_lim_and_transforms(self):
8889 Affine2D ().translate (0 , - 4 )
8990
9091 # This is the transform for latitude ticks.
91- yaxis_stretch = Affine2D ().scale (np .pi * 2 , 1 ).translate (- np .pi , 0 )
92+ yaxis_stretch = Affine2D ().scale (math .pi * 2 , 1 ).translate (- math .pi , 0 )
9293 yaxis_space = Affine2D ().scale (1 , 1.1 )
9394 self ._yaxis_transform = \
9495 yaxis_stretch + \
@@ -108,8 +109,8 @@ def _set_lim_and_transforms(self):
108109
109110 def _get_affine_transform (self ):
110111 transform = self ._get_core_transform (1 )
111- xscale , _ = transform .transform ((np .pi , 0 ))
112- _ , yscale = transform .transform ((0 , np .pi / 2 ))
112+ xscale , _ = transform .transform ((math .pi , 0 ))
113+ _ , yscale = transform .transform ((0 , math .pi / 2 ))
113114 return Affine2D () \
114115 .scale (0.5 / xscale , 0.5 / yscale ) \
115116 .translate (0.5 , 0.5 )
@@ -287,7 +288,7 @@ def inverted(self):
287288 return AitoffAxes .AitoffTransform (self ._resolution )
288289
289290 def __init__ (self , * args , ** kwargs ):
290- self ._longitude_cap = np .pi / 2.0
291+ self ._longitude_cap = math .pi / 2
291292 super ().__init__ (* args , ** kwargs )
292293 self .set_aspect (0.5 , adjustable = 'box' , anchor = 'C' )
293294 self .cla ()
@@ -305,11 +306,11 @@ class HammerTransform(_GeoTransform):
305306 def transform_non_affine (self , ll ):
306307 # docstring inherited
307308 longitude , latitude = ll .T
308- half_long = longitude / 2.0
309+ half_long = longitude / 2
309310 cos_latitude = np .cos (latitude )
310- sqrt2 = np . sqrt ( 2.0 )
311+ sqrt2 = 2 ** ( 1 / 2 )
311312 alpha = np .sqrt (1.0 + cos_latitude * np .cos (half_long ))
312- x = (2.0 * sqrt2 ) * (cos_latitude * np .sin (half_long )) / alpha
313+ x = (2 * sqrt2 ) * (cos_latitude * np .sin (half_long )) / alpha
313314 y = (sqrt2 * np .sin (latitude )) / alpha
314315 return np .column_stack ([x , y ])
315316
@@ -324,15 +325,15 @@ def transform_non_affine(self, xy):
324325 x , y = xy .T
325326 z = np .sqrt (1 - (x / 4 ) ** 2 - (y / 2 ) ** 2 )
326327 longitude = 2 * np .arctan ((z * x ) / (2 * (2 * z ** 2 - 1 )))
327- latitude = np .arcsin (y * z )
328+ latitude = np .arcsin (y * z )
328329 return np .column_stack ([longitude , latitude ])
329330
330331 def inverted (self ):
331332 # docstring inherited
332333 return HammerAxes .HammerTransform (self ._resolution )
333334
334335 def __init__ (self , * args , ** kwargs ):
335- self ._longitude_cap = np .pi / 2.0
336+ self ._longitude_cap = math .pi / 2
336337 super ().__init__ (* args , ** kwargs )
337338 self .set_aspect (0.5 , adjustable = 'box' , anchor = 'C' )
338339 self .cla ()
@@ -351,18 +352,18 @@ def transform_non_affine(self, ll):
351352 # docstring inherited
352353 def d (theta ):
353354 delta = (- (theta + np .sin (theta ) - pi_sin_l )
354- / (1 + np .cos (theta )))
355+ / (1.0 + np .cos (theta )))
355356 return delta , np .abs (delta ) > 0.001
356357
357358 longitude , latitude = ll .T
358-
359- clat = np . pi / 2 - np .abs (latitude )
359+ pi_half = math . pi / 2
360+ clat = pi_half - np .abs (latitude )
360361 ihigh = clat < 0.087 # within 5 degrees of the poles
361362 ilow = ~ ihigh
362363 aux = np .empty (latitude .shape , dtype = float )
363364
364365 if ilow .any (): # Newton-Raphson iteration
365- pi_sin_l = np .pi * np .sin (latitude [ilow ])
366+ pi_sin_l = math .pi * np .sin (latitude [ilow ])
366367 theta = 2.0 * latitude [ilow ]
367368 delta , large_delta = d (theta )
368369 while np .any (large_delta ):
@@ -372,12 +373,13 @@ def d(theta):
372373
373374 if ihigh .any (): # Taylor series-based approx. solution
374375 e = clat [ihigh ]
375- d = 0.5 * ( 3 * np .pi * e ** 2 ) ** ( 1.0 / 3 )
376- aux [ihigh ] = (np . pi / 2 - d ) * np .sign (latitude [ihigh ])
376+ d = np . cbrt ( 3.0 * math .pi * e ** 2 ) / 2
377+ aux [ihigh ] = (pi_half - d ) * np .sign (latitude [ihigh ])
377378
378379 xy = np .empty (ll .shape , dtype = float )
379- xy [:, 0 ] = (2.0 * np .sqrt (2.0 ) / np .pi ) * longitude * np .cos (aux )
380- xy [:, 1 ] = np .sqrt (2.0 ) * np .sin (aux )
380+ sqrt2 = 2 ** (1 / 2 )
381+ xy [:, 0 ] = (sqrt2 / pi_half ) * longitude * np .cos (aux )
382+ xy [:, 1 ] = sqrt2 * np .sin (aux )
381383
382384 return xy
383385
@@ -392,17 +394,18 @@ def transform_non_affine(self, xy):
392394 x , y = xy .T
393395 # from Equations (7, 8) of
394396 # https://mathworld.wolfram.com/MollweideProjection.html
395- theta = np .arcsin (y / np .sqrt (2 ))
396- longitude = (np .pi / (2 * np .sqrt (2 ))) * x / np .cos (theta )
397- latitude = np .arcsin ((2 * theta + np .sin (2 * theta )) / np .pi )
397+ sqrt2 = 2 ** (1 / 2 )
398+ theta = np .arcsin (y / sqrt2 )
399+ longitude = (math .pi / (2.0 * sqrt2 )) * x / np .cos (theta )
400+ latitude = np .arcsin ((2.0 * theta + np .sin (2.0 * theta )) / math .pi )
398401 return np .column_stack ([longitude , latitude ])
399402
400403 def inverted (self ):
401404 # docstring inherited
402405 return MollweideAxes .MollweideTransform (self ._resolution )
403406
404407 def __init__ (self , * args , ** kwargs ):
405- self ._longitude_cap = np .pi / 2.0
408+ self ._longitude_cap = math .pi / 2
406409 super ().__init__ (* args , ** kwargs )
407410 self .set_aspect (0.5 , adjustable = 'box' , anchor = 'C' )
408411 self .cla ()
@@ -434,15 +437,17 @@ def transform_non_affine(self, ll):
434437 clat = self ._center_latitude
435438 cos_lat = np .cos (latitude )
436439 sin_lat = np .sin (latitude )
440+ cos_clat = np .cos (clat )
441+ sin_clat = np .sin (clat )
437442 diff_long = longitude - clong
438443 cos_diff_long = np .cos (diff_long )
439444
440445 inner_k = np .maximum ( # Prevent divide-by-zero problems
441- 1 + np . sin ( clat ) * sin_lat + np . cos ( clat ) * cos_lat * cos_diff_long ,
446+ 1.0 + sin_clat * sin_lat + cos_clat * cos_lat * cos_diff_long ,
442447 1e-15 )
443- k = np .sqrt (2 / inner_k )
448+ k = np .sqrt (2.0 / inner_k )
444449 x = k * cos_lat * np .sin (diff_long )
445- y = k * (np . cos ( clat ) * sin_lat - np . sin ( clat ) * cos_lat * cos_diff_long )
450+ y = k * (cos_clat * sin_lat - sin_clat * cos_lat * cos_diff_long )
446451
447452 return np .column_stack ([x , y ])
448453
@@ -466,7 +471,7 @@ def transform_non_affine(self, xy):
466471 clong = self ._center_longitude
467472 clat = self ._center_latitude
468473 p = np .maximum (np .hypot (x , y ), 1e-9 )
469- c = 2 * np .arcsin (0.5 * p )
474+ c = 2.0 * np .arcsin (0.5 * p )
470475 sin_c = np .sin (c )
471476 cos_c = np .cos (c )
472477
@@ -485,7 +490,7 @@ def inverted(self):
485490 self ._resolution )
486491
487492 def __init__ (self , * args , center_longitude = 0 , center_latitude = 0 , ** kwargs ):
488- self ._longitude_cap = np .pi / 2
493+ self ._longitude_cap = math .pi / 2
489494 self ._center_longitude = center_longitude
490495 self ._center_latitude = center_latitude
491496 super ().__init__ (* args , ** kwargs )
0 commit comments