Python 中的反余弦

发布于 2024-11-25 00:48:26 字数 126 浏览 2 评论 0原文

如果这是直截了当的,我很抱歉,但我没有在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

雨的味道风的声音 2024-12-02 00:48:26

我们有 acos 函数,它返回以弧度为单位的角度。

>>> import math
>>> math.acos(0)
1.5707963267948966
>>> _ * 2 - math.pi
0.0

We have the acos function, which returns the angle in radians.

>>> import math
>>> math.acos(0)
1.5707963267948966
>>> _ * 2 - math.pi
0.0
花桑 2024-12-02 00:48:26

为了增加使用 math.acos 的正确答案,还需要知道 cmath 中有适合复数的数学函数:

>>> import cmath
>>> cmath.acos(1j)
(1.5707963267948966-0.88137358701954294j)

坚持使用 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 in cmath:

>>> import cmath
>>> cmath.acos(1j)
(1.5707963267948966-0.88137358701954294j)

Stick with math.acos if you're only interested in real numbers,

笑着哭最痛 2024-12-02 00:48:26

math.acos() 的结果以弧度为单位。所以你需要将其转换为度数。

您可以这样做:

import math
res = math.degrees (math.acos (value))

The result of math.acos() is in radians. So you need to convert that to degrees.

Here is how you can do:

import math
res = math.degrees (math.acos (value))
若水微香 2024-12-02 00:48:26

为了响应通过 math.acos 使用反余弦找到返回角,只要角度 <= 90* 就可以了,一旦超过这个角度,Python 将无法区分你想要哪个角度。

观察。

>>> math.cos(5)
0.28366218546322625

上面,我要求 python 获取 5 弧度角的余弦,它给了我 0.28~ 太好了,下面我将要求 python 给我 0.28~ 余弦的弧度。应该是5吧?它只是告诉我确实如此。

>>> math.acos(0.28366218546322625)
1.2831853071795865

错误的! 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 或操作系统就无法确定您实际需要的两个角度中的哪一个角度的差异。或者,角度的切线。

1.28 Rad has  x cosine, y sine, z tan  (72*)
1.88 Rad has -x cosine, y sine, -z tan (108*)
4.39 Rad has -x cosine, -y sine, z tan (252*)
   5 Rad has  x cosine, -y sine, -z tan (288*)

或者,从笛卡尔角度来看,

                       negX,posY | posX,posY
                            -----+-----
                       negX,negY |  posX,negY

1.88 Rad has -x cosine, y sine (108) | 1.28 Rad has  x cosine, y sine (72*)
                                -----+-----
4.39 Rad has -x cosine, -y sine (252)|    5 Rad has  x cosine, -y sine (288)

因此,如果出于某种原因,我需要选择 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.

>>> math.cos(5)
0.28366218546322625

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.

>>> math.acos(0.28366218546322625)
1.2831853071795865

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.

1.28 Rad has  x cosine, y sine, z tan  (72*)
1.88 Rad has -x cosine, y sine, -z tan (108*)
4.39 Rad has -x cosine, -y sine, z tan (252*)
   5 Rad has  x cosine, -y sine, -z tan (288*)

or, viewed Cartesianly,

                       negX,posY | posX,posY
                            -----+-----
                       negX,negY |  posX,negY

1.88 Rad has -x cosine, y sine (108) | 1.28 Rad has  x cosine, y sine (72*)
                                -----+-----
4.39 Rad has -x cosine, -y sine (252)|    5 Rad has  x cosine, -y sine (288)

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.

滥情哥ㄟ 2024-12-02 00:48:26

您正在寻找 math.acos() 函数。

You're looking for the math.acos() function.

水溶 2024-12-02 00:48:26

您还可以使用 numpy 模块中的 arccos

>>> import numpy
>>> numpy.arccos(0.5)
1.0471975511965979

警告:对于标量,numpy.arccos() 函数非常重要比 math.acos 慢(约 10 倍)。

请参阅此处的帖子 numpy.arccos() 适合序列,而 math.acos 则不适合。 :)

>>> numpy.arccos([0.5, 0.3])
array([ 1.04719755,  1.26610367])

>>> math.acos([0.5, 0.3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required

You may also use arccos from the module numpy

>>> import numpy
>>> numpy.arccos(0.5)
1.0471975511965979

WARNING: For scalars the numpy.arccos() function is much slower (~ 10x) than math.acos. See post here

Nevertheless, the numpy.arccos() is suitable for sequences, while math.acos is not. :)

>>> numpy.arccos([0.5, 0.3])
array([ 1.04719755,  1.26610367])

but

>>> math.acos([0.5, 0.3])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: a float is required
瑾夏年华 2024-12-02 00:48:26

或者只是为 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文