from __future__ import division
import numpy as np
import numpy.linalg as la
n = 5
A_dp = np.eye(n) + np.random.randn(n, n)
A_sp = A_dp.astype(np.float32)
x_dp = np.ones(n, np.float64)
b_dp = np.dot(A_dp, x_dp)
b_sp = b_dp.astype(np.float32)
Can we solve \(Ax=b\) to DP (double precision) accuracy using (almost) only SP matrix operations?
x0_sp = la.solve(A_sp, b_sp)
r0_dp = b_dp - np.dot(A_dp, x0_sp.astype(np.float64))
print la.norm(r0_dp)
z0_sp = la.solve(A_sp, r0_dp.astype(np.float32))
print la.norm(z0_sp)
x1_dp = (
x0_sp.astype(np.float64)
+ z0_sp.astype(np.float64)
)
print repr(float(x0_sp[0]))
print repr(float(z0_sp[0]))
print repr(float(x1_dp[0]))
la.norm(x0_sp-x_dp)
la.norm(x1_dp-x_dp)