如何从numpy.dot(A,A_inv)到达单位矩阵 [英] How to arrive at the unit matrix from numpy.dot(A, A_inv)
问题描述
import numpy $ numpy
b
$ b A = numpy.zeros((100,100))
E = numpy.zeros((100,100))
size = 100
对于我在范围(大小):
在范围内(大小)的
:
A [i] [j] + = numpy.random.randint(10)
if i == j :
E [i] [j] + = 1
A_inv = numpy.linalg.linalg.inv(A)
print numpy.dot(A,A_inv)
运行代码产生
[me] machine @ numeric $ python rand_diag.py
[[1.00000000e + 00 -7.99360578e-15 -1.14491749e-16 ...,3.81639165e-17
-4.42701431 e-15 1.17961196e-15]
[-5.55111512e-16 1.00000000e + 00 -2.22044605e-16 ...,-3.88578059e-16
1.33226763e-15 -8.32667268e-16]
显然结果是一个单位矩阵,但不是精确的,所以 print numpy.dot(A,A_inv)== E
很明显给出了 False
。我正在做这个练习线性代数,并试图找到我的机器到达极限的矩阵的大小。获得一个真实
将会在教学上有吸引力。
编辑:
设置 size = 10000
,内存不足
[me] machine @ numeric $ Python(794)malloc:
*** mmap(size = 800002048)失败(error code = 12)
***错误:can'''t allocate region
***在malloc_error_break中设置一个断点来调试
Traceback(最近的最后一次调用):
在< module>文件中的rand_diag.py A_inv = numpy.linalg.linalg.inv(A)
文件/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/numpy/linalg/linalg.py,第445行,在inv
返回换行(solve(a,identity(a.shape [0],dtype = a.dtype)))
File/Library/Frameworks/Python.framework/Versions/ 7.2 / lib / python2.7 / site-packages / numpy / linalg / linalg.py,第323行,解决了
a,b = _fastCopyAndTranspose(t,a,b)
文件/ Library / (_fastCT(a),)$($)中的第143行,创建了一个Frameworks / Python.framework / Versions / 7.2 / lib / python2.7 / site-packages / numpy / linalg / b $ b MemoryError
[1] + Exit 1 python rand_diag.py
如何分配更多的内存,如何并行运行(我有4个内核)?
解决方案code> True 在教学上会有吸引力,它也会脱离浮点运算的实际。
在处理浮点时,必须要准备好的不仅仅是不准确的结果,而且还要处理所有其他数字问题。
强烈建议阅读计算机科学家应该了解的有关Floating-点算术。
在您的特定情况下,要确保 A * inv(A)
是足够接近身份矩阵,你可以计算一个矩阵规范 numpy.dot(A,A_inv) - E
并确保它足够小。
请注意,您不必使用循环来填充 A
和 E
。相反,你可以使用
$ b $ pre code> A = numpy.random.randint(0,10,(size,size))
E = numpy.eye(size)
I prepare a matrix of random numbers, calculate its inverse and matrix multiply it with the original matrix. This, in theory, gives the unit matrix. How can I let numpy
do that for me?
import numpy
A = numpy.zeros((100,100))
E = numpy.zeros((100,100))
size = 100
for i in range(size):
for j in range(size):
A[i][j]+=numpy.random.randint(10)
if i == j:
E[i][j]+=1
A_inv = numpy.linalg.linalg.inv(A)
print numpy.dot(A, A_inv)
Running the code produces
[me]machine @ numeric $ python rand_diag.py
[[ 1.00000000e+00 -7.99360578e-15 -1.14491749e-16 ..., 3.81639165e-17
-4.42701431e-15 1.17961196e-15]
[ -5.55111512e-16 1.00000000e+00 -2.22044605e-16 ..., -3.88578059e-16
1.33226763e-15 -8.32667268e-16]
It's evident the result is a unit matrix, but not precisely, so print numpy.dot(A, A_inv) == E
evidently gives False
. I'm doing this for practicing linear algebra and trying to find the size of the matrix for which my machine arrives at its limits. Getting a True
would be didactically appealing.
Edit:
Setting size=10000
, I run out of memory
[me]machine @ numeric $ Python(794) malloc:
***mmap(size=800002048) failed (error code=12)
*** error: can\'t allocate region
*** set a breakpoint in malloc_error_break to debug
Traceback (most recent call last):
File "rand_diag.py", line 14, in <module> A_inv = numpy.linalg.linalg.inv(A)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 445, in inv
return wrap(solve(a, identity(a.shape[0], dtype=a.dtype)))
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 323, in solve
a, b = _fastCopyAndTranspose(t, a, b)
File "/Library/Frameworks/Python.framework/Versions/7.2/lib/python2.7/site-packages/numpy/linalg/linalg.py", line 143, in _fastCopyAndTranspose
cast_arrays = cast_arrays + (_fastCT(a),)
MemoryError
[1]+ Exit 1 python rand_diag.py
How can I allocate more memory and how can I run this in parallel (I have 4 cores)?
While getting True
would be didactically appealing, it would also be divorced from the realities of floating-point computations.
When dealing with the floating point, one necessarily has to be prepared not only for inexact results, but for all manner of other numerical issues that arise.
I highly recommend reading What Every Computer Scientist Should Know About Floating-Point Arithmetic.
In your particular case, to ensure that A * inv(A)
is close enough to the identity matrix, you could compute a matrix norm of numpy.dot(A, A_inv) - E
and ensure that it is small enough.
As a side note, you don't have to use a loop to populate A
and E
. Instead, you could just use
A = numpy.random.randint(0, 10, (size,size))
E = numpy.eye(size)
这篇关于如何从numpy.dot(A,A_inv)到达单位矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!