Learning about LU decompositions, and the textbook I'm using claimed that once the initial matrices L and U have been computed (assuming no pivoting matrix P is needed), it is much faster/more efficient (aka less flops) to solve Ly = b and then Ux = y than Ax = b from scratch. However, when I ran it on my system with a random A ⊆ R^(10 x 10) matrix and b ⊆ R^(10 x 1), it ended up being much slower (12.49 seconds vs 6.17 seconds).
Here's my code:
import numpy as np
from scipy.linalg import lu
from numpy.random import rand
from numpy.linalg import solve
from timeit import timeit as duration
A, b = rand(500, 500), rand(500, 1)
P,L,U = lu(A)
t_vanilla = duration("solve(A,b)", setup="from __main__ import solve, A, b")
print(t_vanilla)
t_decomps = duration("solve(U, solve(L,b))", setup="from __main__ import solve, L, U, b")
print(t_decomps)
Any ideas on why this might be the case? This is my first time using the timeit library, so I may well have messed something up haha. I'm running this on Mac OSX, python 3.8.
Also, I noticed that scipy.solve was WAY slower (over double the time) than numpy.solve for some reason ... any intuition as to why?
Thanks!!!