类decimal.InvalidOperation错误

时间:2017-02-15 18:28:14

标签: python matrix decimal multiplication

我将两个矩阵3x3和1x3相乘。

 {
  "sunrise": "2017-02-15 07:15",
  "lng": 11,
  "countryCode": "IT",
  "gmtOffset": 1,
  "rawOffset": 1,
  "sunset": "2017-02-15 17:45",
  "timezoneId": "Europe/Rome",
  "dstOffset": 2,
  "countryName": "Italy",
  "time": "2017-02-15 19:27",
  "lat": 44
}

代码可以工作但是在矩阵值的几次迭代之后,我得到了这个错误:

import decimal
dec = decimal.Decimal

dot = [0.0,0.0,0.0]
J = [1.0,2.0,3.0],[1.0,2.0,3.0],[1.0,2.0,3.0]
F = [0.0,1.0,2.0]

for i in range(3):
  dot[i] = dec(J[i][0])*dec(F[0])+dec(J[i][1])*dec(F[1])+dec(J[i][2])*dec(F[2])

这些是原始输入(来自先前执行的不同计算)

J(3x3)矩阵(原始)

<ipython-input-2-58067b4f3a4e> in DotProduct(J, F)
      9 def DotProduct(J,F):
     10     for i in range(3):
---> 11      dot[i] = dec(J[i][0]) * dec(F[0]) + dec(J[i][1]) 
               * dec(F[1]) + dec(J[i][2]) * dec(F[2])
     12     return dot
     13 

InvalidOperation: [<class 'decimal.InvalidOperation'>]

F(1x3)矩阵(原始)

([Decimal('0.1379312608303500019037507171'), 
Decimal('-0.1221574827527377810270231013'), 
Decimal('0E+30')], 
[Decimal('-0.03195193407702472679971126508'), 
Decimal('0.03257150032372060736589472552'), 
Decimal('-0E+30')], 
[Decimal('0E+30'), 
Decimal('-0E+30'),  
Decimal('0.001941217285022088082028050212')])

dot(1x3)矩阵 - J * F的结果

[ 0.33484765  0.37611522  5.50589724]

经过多次迭代:

J矩阵变为

[Decimal('0.00024067027981818560724074545'), 
Decimal('0.00155160691687072804459308826'), 
Decimal('0.01068814288621735926754972657')]

F Matrix变为

([Decimal('-2.017921396484441390463202074E-285'), 
Decimal('1.544790144766561273091342077E-337'), 
Decimal('-0')], 
[Decimal('-0.03736963129288584732273158647'), 
Decimal('0.03736963129288584732273158647'), 
Decimal('0')], 
[Decimal('0'), 
Decimal('-0'), 
Decimal('0.001941217285022090055061872254')])

点阵变为

[ -3.02323763e+229  -3.80160157e+281  -1.44050757e+308]

0 个答案:

没有答案