通过atan实现atan2函数

这篇博客探讨了Python中torch库中atan和atan2函数的使用,特别是当只有atan函数可用时如何模拟atan2的效果。通过示例代码展示了如何通过atan调整象限以得到正确的角度,并对比了atan2的直接输出。内容涵盖了复数的幅角计算和角度在不同象限的校正,对于理解和应用这两个函数具有指导意义。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

参考MATLAB函数——atan(x)和atan2(x)的区别

在有的情况下只提供了atan的内置函数而没有atan2,先要判断象限再调整。

import torch
import math

pi = math.pi
real = torch.tensor([ 0.12,  -1.1, -0.1, 1])
imag = torch.tensor([ 0.22,  1.2, -1.2, -1.2])
atan = torch.atan(imag/real)
print(atan)
atan[1] = atan[1] + pi
atan[2] = atan[2] - pi
print(atan)
atan2 = torch.atan2(imag,real)
print(atan2)

output

tensor([ 1.0714, -0.8288, 1.4877, -0.8761])
tensor([ 1.0714, 2.3127, -1.6539, -0.8761])
tensor([ 1.0714, 2.3127, -1.6539, -0.8761])

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值