numpy 任意精度线性代数 [英] numpy arbitrary precision linear algebra

查看:40
本文介绍了numpy 任意精度线性代数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 numpy 二维数组 [中型/大型 - 比如说 500x500].我想找到它的元素指数的特征值.问题是某些值非常负(-800、-1000 等),并且它们的指数下溢(意味着它们非常接近于零,因此 numpy 将它们视为零).无论如何在numpy中使用任意精度?

I have a numpy 2d array [medium/large sized - say 500x500]. I want to find the eigenvalues of the element-wise exponent of it. The problem is that some of the values are quite negative (-800,-1000, etc), and their exponents underflow (meaning they are so close to zero, so that numpy treats them as zero). Is there anyway to use arbitrary precision in numpy?

我梦想的方式:

import numpy as np

np.set_precision('arbitrary') # <--- Missing part
a = np.array([[-800.21,-600.00],[-600.00,-1000.48]])
ex = np.exp(a)  ## Currently warns about underflow
eigvals, eigvecs = np.linalg.eig(ex)

我已经用 gmpy 和 mpmath 搜索了一个解决方案,但无济于事.欢迎提出任何想法.

I have searched for a solution with gmpy and mpmath to no avail. Any idea will be welcome.

推荐答案

SymPy 可以计算任意精度:

SymPy can calculate arbitrary precision:

from sympy import exp, N, S
from sympy.matrices import Matrix

data = [[S("-800.21"),S("-600.00")],[S("-600.00"),S("-1000.48")]]
m = Matrix(data)
ex = m.applyfunc(exp).applyfunc(lambda x:N(x, 100))
vecs = ex.eigenvects()
print vecs[0][0] # eigen value
print vecs[1][0] # eigen value
print vecs[0][2] # eigen vect
print vecs[1][2] # eigen vect

输出:

-2.650396553004310816338679447269582701529092549943247237903254759946483528035516341807463648841185335e-261
2.650396553004310816338679447269582701529092549943247237903254759946483528035516341807466621962539464e-261
[[-0.9999999999999999999999999999999999999999999999999999999999999999999999999999999999999994391176386872]
[                                                                                                      1]]
[[1.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000560882361313]
[                                                                                                    1]]

您可以将 N(x, 100) 中的 100 更改为其他精度,但是,当我尝试 1000 时,特征向量的计算失败了.

you can change 100 in N(x, 100) to other precision, but, as I tried 1000, the calculation of eigen vect failed.

这篇关于numpy 任意精度线性代数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆