Advertisement
Korotkodul

B. Сферические координаты

Feb 18th, 2025 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.25 KB | None | 0 0
  1. import numpy as np
  2.  
  3. class ShapeMismatchError(Exception):
  4.     pass
  5.  
  6. def convert_from_sphere(
  7.     distances: np.ndarray,
  8.     azimuth: np.ndarray,
  9.     inclination: np.ndarray,
  10. ) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
  11.     # ваш код
  12.     if not (distances.shape == azimuth.shape ==inclination.shape):
  13.         raise  ShapeMismatchError
  14.     abscissa = distances * np.sin(inclination) * np.cos(azimuth)
  15.     ordinates = distances * np.sin(inclination)  * np.sin(azimuth)
  16.     applicates = distances * np.cos(inclination)
  17.     return abscissa, ordinates, applicates
  18.  
  19.  
  20.  
  21.  
  22. def convert_to_sphere(
  23.     abscissa: np.ndarray,
  24.     ordinates: np.ndarray,
  25.     applicates: np.ndarray,
  26. ) -> tuple[np.ndarray, np.ndarray, np.ndarray]:
  27.     # ваш код
  28.     if not (abscissa.shape == ordinates.shape == applicates.shape):
  29.         raise ShapeMismatchError
  30.     distances = np.sqrt(abscissa**2 + ordinates**2 + applicates**2)
  31.     azimuth = np.full(abscissa.shape, np.pi / 2)
  32.     mask1 = abscissa != 0
  33.     azimuth[mask1] = np.arctan(ordinates[mask1] / abscissa[mask1])
  34.     mask2 = distances != 0
  35.     inclination = np.zeros(abscissa.shape)
  36.     inclination[mask2] = np.arccos(applicates[mask2] / distances[mask2])
  37.     return distances, azimuth, inclination
  38.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement