Python:计算矩阵内核时的计算精度

2024-10-03 13:23:35 发布

您现在位置:Python中文网/ 问答频道 /正文

问题如下: 我有一个方程:A*x=0,其中A是矩阵8x8,x是有8个元素的向量,0表示零向量。矩阵A的元素包含一个参数E,我必须找到它的值,这个方程的值是可解的,我已经用条件det(A)=0来做了。这就是我的问题的根源——det(A)对于找到的值,E不完全是0.0,而是非常非常接近0.0的值,例如9.5e-12。我把它解释为一个数值问题,但我可能错了? 下一步是找到x。我的概念是找到矩阵A的核,但是这里有一个非零det(A)返回的问题,因为我没有E可以解方程。有没有办法强迫Python使用近似值进行操作

总结:当A*x不完全是0.0,但值非常非常接近0.0时,我需要找到确定内核的方法

编辑: 矩阵值: [[6.60489454233399,-0.000899873003155720,-1111.26791946547,0,0,0,0],[8.4674802584912E-8+2.5809235665861e-7*I,-9.0806338985990E-10,0.00112138236223004,0,0,0,0], [0, 0.635021913463456, 1.57474880598360, -1.95244188971305, -0.512179135916290, 0, 0, 0], [0,6.40801701294518e-7,-1.58908171921515e-6,2.90298102267184e-6,-7.61531659204450e-7,0,0,0], [0, 0, 0, 0.512179135916290, 1.95244188971305, -1.57474880598360, -0.635021913463456, 0], [0,0,0,-7.61531659204450e-7,2.90298102267184e-6,-1.58908171921515e-6,6.40801701294518e-7,0], [0,0,0,0,0,784198.968204183,1.27518657961257e-6,-38.60530012011412], [0,0,0,0,0,0.791336522920740,-1.28679296313874e-12,-1.04862603277821e-5]])

代码示例:

from scipy import *
from numpy.linalg import *
from sympy import *
import sys
import numpy
import cmath
import math

a2= 65e5 #Ae-5
a3= 9e5 #Ae-5
a4= 130e5 #Ae-5

l = -a2-a3/2.0
t = -a3/2.0
f = a3/2.0
g = a4+a3/2.0

print 'Defined thickness'


V1= 0.74015 #eV fabs
V2= 1.1184 #eV fabs
V3= 0.74015 #eV fabs

m= 0.11*0.511e6/(2.99792458e+23)**2 #eV*s**2/A**2

hkr= 6.582119514e-16 #eV*s

x= 0.765705051154



print 'other symbols'

k1=(cmath.sqrt(2.0*(V1-x)*m))/hkr
k2=(cmath.sqrt(2.0*(V2-x)*m))/hkr
k3=(cmath.sqrt(-2.0*x*m))/hkr
k4=(cmath.sqrt(2.0*(V2-x)*m))/hkr
k5=(cmath.sqrt(2.0*(V3-x)*m))/hkr

print 'k-vectors'

a11 = cmath.exp(1.0j*k1*l)
a12 = -1.0*cmath.exp(k2*l)
a13 = -1.0*cmath.exp(-k2*l)

print '1st row'

a21 = 1.0j*k1*cmath.exp(k1*l)
a22 = -k2*cmath.exp(k2*l)
a23 = k2*cmath.exp(-k2*l)

print '2nd row'

a32 = cmath.exp(k2*t)
a33 = cmath.exp(-k2*t)
a34 = -1.0*cmath.exp(1.0j*k3*t)
a35 = -1.0*cmath.exp(-1.0j*k3*t)

print '3rd row'

a42 = k2*cmath.exp(k2*t)
a43 = -k2*cmath.exp(-k2*t)
a44 = -1.0j*k3*cmath.exp(1.0j*k3*t)
a45 = 1.0j*k3*cmath.exp(-1.0j*k3*t)

print '4th row'

a54 = cmath.exp(1.0j*k3*f)
a55 = cmath.exp(-1.0j*k3*f)
a56 = -1.0*cmath.exp(k4*f)
a57 = -1.0*cmath.exp(-k4*f)

print '5th row'

a64 = 1.0j*k3*cmath.exp(1.0j*k3*f)
a65 = -1.0j*k3*cmath.exp(-1.0j*k3*f)
a66 = -k4*cmath.exp(k4*f)
a67 = k4*cmath.exp(-k4*f)

print '6th row'

a76 = cmath.exp(k4*g)
a77 = cmath.exp(-k4*g)
a78 = -1.0*cmath.exp(-1.0j*k5*g)

print '7th row'

a86 = k4*cmath.exp(k4*g)
a87 = -k4*cmath.exp(-k4*g)
a88 = 1.0j*k5*cmath.exp(-1.0j*k5*g)
print '8th row'


M = Matrix([[a11,a12,a13,0,0,0,0,0],
        [a21,a22,a23,0,0,0,0,0],
        [0,a32,a33,a34,a35,0,0,0],      
        [0,a42,a43,a44,a45,0,0,0],
        [0,0,0,a54,a55,a56,a57,0],
        [0,0,0,a64,a65,a66,a67,0],            
        [0,0,0,0,0,a76,a77,a78],
        [0,0,0,0,0,a86,a87,a88]])


v=M.nullspace()
m=lcm([val.q for val in v])
PSI=m*v
print M
print PSI

Tags: import矩阵k2k1sqrta3cmathrow