【发布时间】:2019-10-11 10:00:00
【问题描述】:
有没有办法在python中将复数矩阵转换为整数矩阵? Solution in Wolfram Mathematica 。 比如
import numpy as np
A=np.zeros((6,6),dtype=complex)
A[1,1]= 1.+1j
A[3,2]= 2.1-1j
print(A)
返回
[[0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
[0. +0.j 1. +1.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
[0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
[0. +0.j 0. +0.j 2.1-1.j 0. +0.j 0. +0.j 0. +0.j]
[0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]
[0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j 0. +0.j]]
但我想要类似的东西
[[0 0 ...
[0 1+j ...
我试过了
A.astype(np.int64)
但返回
... ComplexWarning: Casting complex values to real discards the imaginary part
Entry point for launching an IPython kernel.
array([[0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 2, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0]])
【问题讨论】:
-
嗨,欢迎来到 SO。对于一个好问题,您应该发布您尝试过的代码。您至少可以提供您创建的矩阵以及您已经尝试过的库。
标签: python type-conversion complex-numbers