提问人:Pooyan Rezaeipour 提问时间:11/12/2023 最后编辑:Pooyan Rezaeipour 更新时间:11/12/2023 访问量:118
在 python 中更改除法精度 [已关闭]
Changing the precision of division in python [closed]
问:
我想在Python中更改除法的精度。
实际上,我正在Matlab中处理“col2im”指令,我想用Python编写它的等效形式。
例如,对于相同的输入,在 Matlab 中输出为
在 python 中我得到了
输出之间略有不同,但在特定应用中,获得完全相同的结果非常重要。
我认为问题是因为我使用的 Python 中的除法形式。我尝试了“ / , // , divide( , ) ”,但事实并非如此。
有没有其他方法可以在 python 中划分 2 个值?
我使用了以下源代码,并进行了轻微的修改,如 https://github.com/Mullahz/Python-programs-for-MATLAB-in-built-functions/blob/main/col2im.py 所示:
import numpy as np
def col2im(blk_mtx, img, blk_size):
image_size = img.shape
imgw = img.shape[0]
imgh = img.shape[1]
p, q = [blk_size, blk_size]
result = np.zeros(image_size)
weight = np.zeros(image_size)
col = 0
for i in range(1, imgw, blk_size):
for j in range(1, imgh, blk_size):
result[j-1:j + p-1, i-1:i + q-1] += blk_mtx[:, col].reshape((blk_size, blk_size), order='F')
weight[j-1:j + p-1, i-1:i + q-1] += np.ones((blk_size, blk_size))
col += 1
final_res = np.divide(result, weight)
return final_res
答: 暂无答案
评论