Skip to content

Instantly share code, notes, and snippets.

@Joshuaalbert
Created April 21, 2017 15:56
Show Gist options
  • Select an option

  • Save Joshuaalbert/0894af0edbf63571fdff3b6f293b7ff0 to your computer and use it in GitHub Desktop.

Select an option

Save Joshuaalbert/0894af0edbf63571fdff3b6f293b7ff0 to your computer and use it in GitHub Desktop.
Show how cho_solve gives wrong error and the proper way to do it.
from scipy.linalg import cho_solve
import numpy as np
def choSolve(L,y,lower=True):
x = np.copy(y)
if lower:
i = 0
while i < L.shape[0]:
x[i] /= L[i,i]
x[i+1:] -= L[i+1:,i]*x[i]
i += 1
return x
y = np.random.uniform(size=3)
a = np.random.uniform(size=[3,3])
a = a.T.dot(a)
L = np.linalg.cholesky(a)
print("Will solve L.x = y")
print("{} . x = {}".format(L,y))
x1 = cho_solve((L,True),y)
print("scipy.linalg.cho_solve result: {}".format(x1))
print("Verify L.x = y: {}".format(np.isclose(L.dot(x1),y)))
x2 = choSolve(L,y)
print("proper results: {}".format(x2))
print("Verify L.x = y: {}".format(np.isclose(L.dot(x2),y)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment