import numpy as np
X=np.array([[1.,2.],[1.,3.]]); beta=np.array([10.,5.])
pred=X@beta
assert pred.tolist()==[20.,25.]
print('prediction',pred.tolist())
A=np.array([[2.,1.],[1.,3.]]); b=np.array([5.,10.])
solution=np.linalg.solve(A,b)
assert np.allclose(solution,[1,3]) and np.allclose(A@solution,b)
print('solve',solution.tolist(),'condition',float(np.linalg.cond(A)))
x=np.array([0.,1.,2.]); y=np.array([1.,3.,5.])
design=np.column_stack([np.ones_like(x),x])
fitted,residuals,rank,singular=np.linalg.lstsq(design,y,rcond=None)
error=y-design@fitted
assert np.allclose(fitted,[1,2]) and np.allclose(error,0) and rank==2
print('lstsq',np.round(fitted,6).tolist(),'rank',rank,'squared error',float(error@error))
try:
    np.linalg.solve(np.array([[1.,2.],[2.,4.]]),np.array([3.,6.]))
except np.linalg.LinAlgError:
    print('singular system correctly rejected')
else:
    raise AssertionError('singular solve should fail')
