Solving linear equations by different methods

Solving linear equations by different methods

Randall Romero Aguilar, PhD

This demo is based on the original Matlab demo accompanying the Computational Economics and Finance 2001 textbook by Mario Miranda and Paul Fackler.

Original (Matlab) CompEcon file: demlin01.m

Running this file requires the Python version of CompEcon. This can be installed with pip by running

!pip install compecon --upgrade

Last updated: 2022-Ago-07


import numpy as np
import pandas as pd
from numpy.linalg import solve, inv
from timeit import default_timer as timer
from numba import jit

Make a function to time

tic = lambda: timer()
toc = lambda t: 1000* (timer() - t)  # ellapsed milliseconds

Milliseconds required to solve n by n linear equation \(Ax = b\)

m times using solve(A, b) and dot(inv(A), b), computing inverse only once.

mvalues = [1, 100]
nvalues = [50, 500]

cases = pd.MultiIndex.from_product([mvalues, nvalues], names=['m','n'])
results0 = pd.DataFrame(index=cases, columns=['solve(A,b)', 'inv(A) @ b'])

for m, n in cases:
    A = np.random.rand(n, n)
    b = np.random.rand(n, 1)

    tt = tic()
    for j in range(m):
        x = solve(A, b)

    results0.loc[(m, n), 'solve(A,b)'] = toc(tt)

    tt = tic()
    Ainv = inv(A)
    for j in range(m):
        x = Ainv @ b

    results0.loc[(m, n), 'inv(A) @ b'] = toc(tt)
@jit
def using_solve(A, b, m):
    for j in range(m):
        x = solve(A, b)

@jit
def using_inv(Ainv, b, m):    
    for j in range(m):
        x = Ainv @ b

#run once to compile
using_solve(A, b, m)
using_inv(Ainv, b, m)
results1 = pd.DataFrame(index=cases, columns=['solve(A,b)', 'inv(A) @ b'])

for m, n in cases:
    A = np.random.rand(n, n)
    b = np.random.rand(n, 1)

    tt = tic()
    using_solve(A, b, m)

    results1.loc[(m, n), 'solve(A,b)'] = toc(tt)

    tt = tic()
    Ainv = inv(A)
    using_inv(Ainv, b, m)

    results1.loc[(m, n), 'inv(A) @ b'] = toc(tt)
pd.concat([results0, results1], keys=['without jit', 'using jit'], axis=1).style.highlight_min(axis=1)
    without jit using jit
    solve(A,b) inv(A) @ b solve(A,b) inv(A) @ b
m n        
1 50 7.494000 0.731900 0.083900 0.309500
500 3.313000 9.448000 3.277700 8.304200
100 50 7.089400 0.838400 4.628700 0.391200
500 777.569700 19.509600 345.602300 13.260500