Python 中的反余弦
如果这是直截了当的,我很抱歉,但我没有在 python 手册或谷歌中找到任何帮助。
我正在尝试使用 python 找到一个值的反余弦。
即 cos⁻1(x)
有谁知道该怎么做?
谢谢
Apologies if this is straight forward, but I have not found any help in the python manual or google.
I am trying to find the inverse cosine for a value using python.
i.e. cos⁻¹(x)
Does anyone know how to do this?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我们有
acos
函数,它返回以弧度为单位的角度。We have the
acos
function, which returns the angle in radians.为了增加使用
math.acos
的正确答案,还需要知道cmath
中有适合复数的数学函数:坚持使用
math.acos
如果您只对实数感兴趣,To augment the correct answers to use
math.acos
, it is also worth knowing that there are math functions suitable for complex numbers incmath
:Stick with
math.acos
if you're only interested in real numbers,math.acos()
的结果以弧度为单位。所以你需要将其转换为度数。您可以这样做:
The result of
math.acos()
is in radians. So you need to convert that to degrees.Here is how you can do:
为了响应通过 math.acos 使用反余弦找到返回角,只要角度 <= 90* 就可以了,一旦超过这个角度,Python 将无法区分你想要哪个角度。
观察。
上面,我要求 python 获取 5 弧度角的余弦,它给了我 0.28~ 太好了,下面我将要求 python 给我 0.28~ 余弦的弧度。应该是5吧?它只是告诉我确实如此。
错误的! Python 返回 1.28~ 弧度。从直观上看,原因很明显,1.28rad 与 5rad 具有相同的余弦,它们是反角。每个角度与另一个角度共享相同的正弦(并且与另外两个角度共享相同的正弦)。
即 5/175* 具有等效的正弦值。它们分别具有反比余弦 0.99~/-.99。它们的正弦表兄弟是 185 和 355。这里的关系模因是所有这些角度都具有相同的相对于水平轴的角度偏转。 5*。
python 返回 1.28 而不是 5 的原因是所有计算机/计算器都基于类似算盘的角度/弧度数据表,其正弦、余弦、正切等。所以当我 math.acos(x) 时,python 会询问内核在数据表中查找具有 x 余弦的角度,当找到它时,它会返回它出现的第一个条目。然后 python 给我这个角度。
由于这种共享的比例对称性,正余弦比经常重复。而且您可能会多次看到同一个数字。如果不执行额外的逻辑来考虑角度正弦的 -/+ 值,Python 或操作系统就无法确定您实际需要的两个角度中的哪一个角度的差异。或者,角度的切线。
或者,从笛卡尔角度来看,
因此,如果出于某种原因,我需要选择 5 个弧度(例如,对于矢量绘图或游戏来确定敌人来自玩家的各种矢量),我将不得不执行某种类型的 if/then比较正弦/正切的逻辑。
In response to using inverse cosine to find return angles via math.acos, it's all fine and dandy so long as the angle is <=90* once you go past that, python will have no way of differentiating which angle you wanted.
Observe.
Above, I asked python to fetch me the cosine of a 5 radian angle, and it gave me .28~ Great, below I'll ask python to give me the radian which has a .28~ cosine. Should be 5, right? It literally just told me it was.
Wrong! Python returns 1.28~ radians. The reason is obvious when plotted visually, 1.28rad has the same cosine as 5rad, they're inverse angles. Every angle shares the same sine with another angle (and -sine with two others).
i.e. 5/175* share an equivalent sine. They share inversely proportional cosines .99~/-.99 respectively. Their -sine cousins would be 185 and 355. The relationship meme here is that all these angles share the same angular deflection from the horizontal axis. 5*.
The reason python returns 1.28 and not 5 is that all computers/calculators are based on an abacus-like data table of an angle/radian, its sine, cos, tan etc etc. So when i math.acos(x), python asks the kernal to look through that data table for whichever angle has a cosine of x, and when it finds it, it returns the first entry it appears with. and then python gives that angle to me.
Due to this shared, proportional symmetry, sin/cos ratios repeat frequently. And you are likely to see the same figure, multiple times. There's no way for python, or the OS, to determine the difference of which of the two angles you actually require without doing additional logic that takes into account the -/+ value of the angle's sine. Or, the angle's tangent.
or, viewed Cartesianly,
So if, for whatever reason, i need 5 radians to be chosen (say for a vector drawing or game to determine the various vectors enemies are from the player), i would have to do some type of if/then logic comparing the sines/tangents.
您正在寻找
math.acos()
函数。You're looking for the
math.acos()
function.您还可以使用
numpy
模块中的arccos
警告:对于标量,
numpy.arccos()
函数非常重要比 math.acos 慢(约 10 倍)。请参阅此处的帖子
numpy.arccos()
适合序列,而math.acos
则不适合。 :)但
You may also use
arccos
from the modulenumpy
WARNING: For scalars the
numpy.arccos()
function is much slower (~ 10x) thanmath.acos
. See post hereNevertheless, the
numpy.arccos()
is suitable for sequences, whilemath.acos
is not. :)but
或者只是为 cos^{-1} 的 泰勒展开 编写一个
函数更耗时(并且运行速度可能更慢),但是更通用的方法
or just write a function of your own for the taylor expansion of cos^{-1}
this would be more time consuming (and maybe slower to run) but is the more general approach