QR decomposition: Difference between revisions

Content added Content deleted
(Python solution)
m (→‎{{header|Python}}: Add note on Numpy qr function.)
Line 1,312: Line 1,312:
=={{header|Python}}==
=={{header|Python}}==
{{libheader|numpy}}
{{libheader|numpy}}
Numpy has a qr function but here is a reimplementation to show construction and use of the Householder reflections.
<lang python>#!/usr/bin/env python3
<lang python>#!/usr/bin/env python3


Line 1,338: Line 1,339:
( 6, 167, -68),
( 6, 167, -68),
(-4, 24, -41),
(-4, 24, -41),
)))
)), dtype=np.float64)


q, r = qr(a)
q, r = qr(a)
Line 1,353: Line 1,354:
return np.linalg.solve(r[:n, :], np.dot(q.T, b)[:n])
return np.linalg.solve(r[:n, :], np.dot(q.T, b)[:n])


x = np.array((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), dtype=np.float64)
x = np.array((0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
y = np.array((1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321), dtype=np.float64)
y = np.array((1, 6, 17, 34, 57, 86, 121, 162, 209, 262, 321))


print('\npolyfit:\n', polyfit(x, y, 2))</lang>
print('\npolyfit:\n', polyfit(x, y, 2))</lang>