python - 具有复数的 Python interp2D
问题描述
我需要像 matlab 的 interp2 函数一样对 python 进行 interp2
我尝试使用与 matlabs inter2 相同的 scipy interp2d 函数
Matlab:interp2(x,y,yy,new_xx,new_yy)
x = 37、39、41
y = 2.5, 2.75, 3
yy = [[0.6 + 1.6j,0.6 + 1.6j,0.6 + 1.6j], [0.7 + 1.6j, 0.7 + 1.6j, 0.7 + 1.6j], [0.8 + 1.5j, 0.8 + 1.5j, 0.8 + 1.5j]] - 3x3 数组
new_xx = np.linspace(37,41,401)
new_yy = np.linspace(0,3,401)
'''
func = scipy.interpolate.interp2d(x,y,yy)
arr = func(new_xx,new_yy)
'''
运行 func = scipy.interpolate.interp2d(x,y,yy) "ComplexWarning: Casting complex values to real 会丢弃虚部"
如何与复数进行插值?
解决方案
一种解决方案是执行两种不同的插值:“如果 V 包含复数,则 interp2 分别对实部和虚部进行插值。” 来自interp2 matlab 文档。
import numpy as np
from scipy.interpolate import interp2d
x = np.array([37, 39, 41])
y = np.array([2.5, 2.75, 3])
z = np.array([[0.6 + 1.6j, 0.6 + 1.6j, 0.6 + 1.6j],
[0.7 + 1.6j, 0.7 + 1.6j, 0.7 + 1.6j],
[0.8 + 1.5j, 0.8 + 1.5j, 0.8 + 1.5j]])
# 2D grid interpolation
interpolator_real = interp2d(x, y, np.real(z))
interpolator_imag = interp2d(x, y, np.imag(z))
def interpolator_complex(x, y):
return interpolator_real(x, y) + 1j*interpolator_imag(x, y)
# test
new_x = np.linspace(37, 41, 6)
new_y = np.linspace(2.5, 3, 8)
interpolator_complex(new_x, new_y)
推荐阅读
- mongodb - mongodb聚合展开数组嵌套在数组中并计算每个唯一值的出现
- node.js - NodeJS 服务器到 NodeJS 客户端通知
- javascript - 如何调用多设置器 useState 反应钩子
- java - 如何在启动时设置 Cron Quartz 触发器
- android - Android + FTDI FT232H 兼容性
- javascript - Symfony - 自定义 FieldType form_widget 渲染
- unity3d - 旋转 - 偏航滚距
- python - 如何使用 urllib 发送请求并继续执行下一个命令而不等待 python 中的响应
- sql - 在 SQL 查询的 WHERE 子句中使用 R 列表
- angular - 如何在 Ionic 4 中将图片上传到 Firebase?