An easy going contrained linear solver with sparse methods and minimal effort
Project description
cool_linear_solver: An easy going (contrained) linear solver with sparse methods and minimal effort
usage:
from cool_linear_solver import Variable, System_of_linear_eqs
T = Variable(name='T')
print(T.coefs)
print(T)
eq1 = T + T + T + T[1] + 1*T[3.5]==1
eq2 = T + T + T[1] - 4*T[3.5]==4
eq3 = T + T[1]/10+ 0*T[3.5]==-10.5
print(eq1)
print(eq2)
print(eq3)
sys = System_of_linear_eqs()
sys.add_equation(eq1)
sys.add_equation(eq2)
sys.add_equation(eq3) #or use sys.add_equations([eq1,eq2,eq3]) or even `sys = quicksolver([eq1,eq2,eq3])`
sys.solve() #solve using the pushed equations
print('T=',sys[T]) #evaluated T on the solution
print('T[1]=',sys[T[1]])
print('T[3.5]=',sys[T[3.5]])
# print('T[2.5]=',sys[T[2.5]]) #this will throw an error for it was not present in the source equations
print('eq1=',sys[eq1]) #you can also evaluate expressions
As can be seen you can use non-integer indexes and the notation is quite simple.
Heat Equation Solving example
It uses a sparse solver so you can used it as a PDE solver such as solving the heat equation
import numpy as np
N = 200 #creates and 200**2 by 200**2 sparse matrix
Ny = Nx = N
dx, dy = 1/(Nx-1), 1/(Ny-1)
yar = np.linspace(0,1,num=Ny)
xar = np.linspace(0,1,num=Nx)
eqs = System_of_linear_eqs()
T = Variable(name='T')
for yi in range(Ny):
for xi in range(Nx):
x,y = xar[xi], yar[yi]
if x==0:
if 0.25<y<0.75:
eqs.add_equation(T[x,y]==1)
else:
eqs.add_equation(T[x,y]==0)
elif y==0 or x==1 or y==1:
eqs.add_equation(T[x,y]==0)
else:
#domain:
eqs.add_equation(T[x,y]==0.25*(T[xar[xi+1],y] + T[xar[xi-1],y] + T[x,yar[yi+1]] + T[x,yar[yi-1]]))
print(eqs.get_sparse_matrix().__repr__()) # a sparse matrix is automaticly created
eqs.solve()
Tar = []
for yi in range(Ny):
Trow = []
for xi in range(Nx):
x,y = xar[xi], yar[yi]
Trow.append(eqs[T[x,y]])
Tar.append(Trow)
from matplotlib import pyplot as plt
plt.contourf(xar,yar,Tar)
plt.colorbar()
plt.show()
It can be quite slow in constructing the equations but that is not the goal of this module.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for cool_linear_solver-0.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6662284d37cce423e4eb96275a75e7ece1448ee95a03a42b4970e0253e37cfa9 |
|
MD5 | 1323ea076c4ca8f5923de64e7901ea54 |
|
BLAKE2b-256 | c162636519162e686530a658d4ed33308a54d28763fd60974db2a3c4813b246b |