#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct  8 14:47:49 2024

@author: sam
"""

import numpy as np
from scipy import sparse as sp
from scipy.sparse import linalg as sla



A = sp.random(5000,5000,0.1) # creates 5000x5000 sparse matrix with density 0.1
b = sp.random(5000,1,0.1) # creates 5000x1 sparse vector with density 0.1

A2 = sp.csr_matrix(A) # converts matrix A to csr matrix form
b2 = sp.csr_matrix(b) # converts vector b to csr matrix form

x = sla.spsolve(A2,b) # obtains solution x for the linear equation Ax = b

c = (A @ x) - b.A.T # checks that Ax - b = 0

print(np.linalg.norm(c,2)) # prints the L2 norm of Ax - b