首页 > 解决方案 > 复数的离散积分会产生错误的结果

问题描述

我目前正在尝试为复数傅立叶变换实现对复数的离散积分。
但是,我在 go 中所做的实现会产生错误的结果。

这是我的实现:

n = input() //It is a function argument
cumulativeValue := 0 + 0i
currentTime := 0.00
const deltaTime = 0.001
const precision = 0.0001
for cmpFloat(currentTime, 1.00, precision) < 0 {

    currentPoint := findPointForTime(currentTime)
    cumulativeValue += complex(float64(currentPoint.X), float64(currentPoint.Y)) * cmplx.Exp(complex(0.0, -2.0*math.Pi*float64(n)*currentTime))

    currentTime += deltaTime
}

cumulativeValue *= deltaTime

该算法返回傅立叶级数的元素。但是当我尝试从意式中取回该功能时,它与原始输入相去甚远。
我尝试在 Python 中实现相同的算法,没有进行转换,并且它完美地工作。


调查

我首先认为问题与findPointForTime功能有关,但我已经确认它没有。

然后,在调试时,我发现前几个值在 Python 中是相当等价的,但在第 500 次加法(中间算法)之后就不那么等价了。

那么问题可能是 go 的复数的精度不够,或者 cmplx.Exp 不正确(这似乎不太可能)。

那么,有没有办法提高 go 中 complex128 的精度呢?


TLDR:问题可能是 go 的浮点精度。有没有办法改进它?


编辑

currentTime = 0
deltaTime = 0.001
cumulativeValue = complex(0)
while currentTime < 1.0:
    point = getPointforTime(currentTime)
    cumulativeValue += point * cmath.exp(-2.0j*cmath.pi*n*currentTime)*deltaTime

    currentTime += deltatime

标签: go

解决方案


推荐阅读