Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将任意角度转换为间隔[-pi,pi]_Python_Numpy_Trigonometry_Angle - Fatal编程技术网

Python 将任意角度转换为间隔[-pi,pi]

Python 将任意角度转换为间隔[-pi,pi],python,numpy,trigonometry,angle,Python,Numpy,Trigonometry,Angle,如何将一个任意角度的值x,以弧度为单位,从区间]-无限,无限[转换为区间[-pi,pi]中的等效角度 此类转换的示例,以度为单位: 45度=>45度 180度=>180度 181度=>-179度 -200度=>160度 380度=>20度 一种可能的方法: import numpy as np np.arctan2(np.sin(x), np.cos(x)) # converts x to [-np.pi, np.pi] ……和: np.arctan2(np.sin(x), np.cos

如何将一个任意角度的值
x
,以弧度为单位,从区间]-无限,无限[转换为区间[-pi,pi]中的等效角度

此类转换的示例,以度为单位:

  • 45度=>45度
  • 180度=>180度
  • 181度=>-179度
  • -200度=>160度
  • 380度=>20度
    • 一种可能的方法:

      import numpy as np
      
      np.arctan2(np.sin(x), np.cos(x))  # converts x to [-np.pi, np.pi]
      
      ……和:

      np.arctan2(np.sin(x), np.cos(x)) + np.pi  # converts x to [0, 2*np.pi]
      

      如果您至少可以访问Python的3.7版,那么
      math
      模块有一个函数,它可以在单个函数调用中实现您想要的功能。只需使用
      math.rements(my_angle,2*math.pi)
      (或者为了好玩,使用而不是
      2*math.pi

      例如:

      >>> from math import remainder, tau
      >>> math.remainder(2.7, tau)
      2.7
      >>> math.remainder(3.7, tau)  # note wraparound to 3.7 - 2*pi
      -2.583185307179586
      >>> math.remainder(1000.0, tau)
      0.9735361584457891