python3.6.3でnumpyを使っています。
Decimal(0.295535291)このような数値をfloat64にcastし、np.linalg.solveを使い、結果を出したいのですが、
TypeError: No loop matching the specified signature and casting
was found for ufunc solve1
とエラーが出てしまいます。
cast時に数値が、-0.70446470900000002このようになってしまっています。これが原因でしょうか。
質問は、このエラーはなぜ発生してしまっているのか、Decimalからfloat64へcast時、正しい値へcastするにはどうしたらいいか。の二つです。
発生している問題・エラーメッセージ
TypeError: No loop matching the specified signature and casting was found for ufunc solve1
該当のソースコード
val1 = np.array([ [Decimal(-0.704464709), Decimal(0.295535291), Decimal(0.295535291), Decimal(0.295535291), Decimal(0.295535291), Decimal(0.295535291), Decimal(0.295535291), Decimal(1)], [Decimal(0.0000822818198462602), Decimal(-1), Decimal(0), Decimal(0), Decimal(0), Decimal(0), Decimal(0), Decimal(1)], [Decimal(0.063900458), Decimal(0.06398274), Decimal(-0.93601726), Decimal(0.06398274), Decimal(0.06398274), Decimal(0.06398274), Decimal(0.06398274), Decimal(1)], [Decimal(0.185757364), Decimal(0.185757364), Decimal(0.185757364), Decimal(-0.814242636), Decimal(0.185757364), Decimal(0.185757364), Decimal(0.185757364), Decimal(1)], [Decimal(0.030625048), Decimal(0), Decimal(0), Decimal(0), Decimal(-1), Decimal(0), Decimal(0), Decimal(1)], [Decimal(0.420372803), Decimal(0.450997851), Decimal(0.450997851), Decimal(0.450997851), Decimal(0.450997851), Decimal(-0.549002149), Decimal(0.450997851), Decimal(1)], [Decimal(0.003726755), Decimal(0.003726755), Decimal(0.003726755), Decimal(0.003726755), Decimal(0.003726755), Decimal(0.003726755), Decimal(-0.996273245), Decimal(1)], [Decimal(1), Decimal(1), Decimal(1), Decimal(1), Decimal(1), Decimal(1), Decimal(1), Decimal(0)] ]) val2 = np.array( [0, 0, 0, 0, 0, 0, 0, 1] ) for i, data in enumerate(val1): val1[i] = [np.float64(x) for x in data] ans = np.linalg.solve(val1, val2) print(ans)
試したこと
Decimalをfloat64にcastしたとき、桁数が多くなっていることが原因かと思い、roundを使い四捨五入してみましたが同じエラーでした。
型が違うのかと思い、四捨五入後にfloat32にcast、float64にcastなども試しましたが同じエラーでした。
val1 = np.array([ [-0.704464709, 0.295535291, 0.295535291, 0.295535291, 0.295535291, 0.295535291, 0.295535291, 1], [8.23E-05, -1, 0, 0, 0, 0, 0, 1], [0.063900458, 0.06398274, -0.93601726, 0.06398274, 0.06398274, 0.06398274, 0.06398274, 1], [0.185757364, 0.185757364, 0.185757364, -0.814242636, 0.185757364, 0.185757364, 0.185757364, 1], [0.030625048, 0, 0, 0, -1, 0, 0, 1], [0.420372803, 0.450997851, 0.450997851, 0.450997851, 0.450997851, -0.549002149, 0.450997851, 1], [0.003726755, 0.003726755, 0.003726755, 0.003726755, 0.003726755, 0.003726755, -0.996273245, 1], [1, 1, 1, 1, 1, 1, 1, 0] ]) val2 = np.array( [0, 0, 0, 0, 0, 0, 0, 1] ) ans = np.linalg.solve(val1, val2)
最初から、np.arrayに入力した場合はエラーにはなりませんでした。
回答1件
あなたの回答
tips
プレビュー