Skip to main content

cool_linear_solver

An easy going API to solver (see below) with user minimal effort

Installation

pip install cool-linear-solver

1) System of linear equations

from cool_linear_solver import Variable, quick_solve

x = Variable('x')
eqs = [x[0] + x[1] == 2, x[1] - x[0] == 3] #x[0] creates a new variable automatically you can also use thing like x['a'] 
sol = quick_solve(eqs)
print('x0, x1 =', sol[x[0]], sol[x[1]])

2) Solving the heat equation

This is a compact demonstration of using indexed Variable objects to build a small finite-difference-like system. This module uses sparse matrixes so the memory size increases only linearlly with the number of non-zero element in the system of equation.

import numpy as np
from cool_linear_solver import Variable, quick_solve

Nx = 50
Ny = 50
T = Variable('T')

x_coords = np.linspace(0, 1, Nx)
y_coords = np.linspace(0, 1, Ny)
dx = x_coords[1] - x_coords[0]
dy = y_coords[1] - y_coords[0]

eqs = []
for x in x_coords:
    for y in y_coords:
        # Simple Dirichlet boundary: set boundaries to 1 on x==0 and 0 elsewere on boundary
        # Interior to Laplace discretization
        if x == 0:
            eqs.append(T[x, y] == 1)
        elif y == 0 or y == 1 or x==1:
            eqs.append(T[x, y] == 0)
        else:
            eqs.append(T[x-dx, y] + T[x+dx, y] + T[x, y-dy] + T[x, y+dy] - 4*T[x, y] == 0) #floats can be used

sol = quick_solve(eqs)
# collect solution into array
Tarr = np.array([[sol[T[x, y]] for x in x_coords] for y in y_coords])

# 3d surface plot of result
from matplotlib import pyplot as plt
X, Y = np.meshgrid(x_coords, y_coords)
fig = plt.figure(figsize=(8, 6))
ax = fig.add_subplot(projection='3d')
surf = ax.plot_surface(X, Y, Tarr, cmap='viridis', edgecolor='none')
ax.view_init(elev=30, azim=-60)
ax.set_xlabel('x'); ax.set_ylabel('y'); ax.set_zlabel('T')
plt.colorbar(surf, ax=ax, shrink=0.6)
plt.tight_layout(); plt.show()

3) Least squares (unconstrained)

Create a least-squares objective by passing squared linear expressions.

from cool_linear_solver import Variable, quick_solve

x = Variable('x')
eqs = [ (x[0] + x[1] - 3)**2, (2*x[0] - x[1] + 1)**2 ]
sol = quick_solve(eqs)
print('solution:', sol[x[0]], sol[x[1]])

4) Constrained least squares

Add linear equalities / inequalities together with squared objectives.

from cool_linear_solver import Variable, quick_solve

x = Variable('x')
eqs = [ (x[0] + x[1] - 3)**2, (2*x[0] - x[1] + 1)**2, x[0] + x[1] == 2, x[0] >= 0 ]
sol = quick_solve(eqs)
print('solution:', sol[x[0]], sol[x[1]])

5) Quadratic problem

Minimize a quadratic objective (here: sum of squares plus linear term) with a linear equality.

from cool_linear_solver import Variable, quick_solve

x = Variable('x')
eqs = [ x[0]*x[0] + x[1]*x[1] + x[0] + x[1], x[0] + x[1] == 1 ]
sol = quick_solve(eqs)
print('solution:', sol[x[0]], sol[x[1]])

6) Linear program (LP)

Small LP: minimize a linear cost with linear constraints.

from cool_linear_solver import Variable, quick_solve

x = Variable('x')
eqs = [ x[0] + 2*x[1], x[0] + x[1] == 3, x[0] >= 0, x[1] >= 0 ]
sol = quick_solve(eqs)
print('x0, x1 =', sol[x[0]], sol[x[1]])

7) Mixed-integer program (MIP)

Mark variables as integer or binary using the Integer and Binary markers. quick_solve will route the problem to the MILP backend.

from cool_linear_solver import Variable, quick_solve, Integer, Binary

x = Variable('x')
eqs = [ 2*x[0] - 3*x[1] + x[2], x[0] == Integer, x[1] == Binary, x[0] >= 0, x[1] >= 0, x[2] >= 0, x[0] <= 5, x[1] <= 1 ]
sol = quick_solve(eqs)
print('x0, x1, x2 =', sol[x[0]], sol[x[1]], sol[x[2]])

Metadata

Release files for cool-linear-solver 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cool-linear-solver 0.2.1
File Size Uploaded Provenance Actions
cool_linear_solver-0.2.1.tar.gz 12.6 kB Without provenance Details for cool_linear_solver-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cool-linear-solver 0.2.1
File Interpreter ABI Platform Actions
cool_linear_solver-0.2.1-py3-none-any.whl Python 3 none any Details for cool_linear_solver-0.2.1-py3-none-any.whl

Total release size: 28.9 kB

Release files / cool_linear_solver-0.2.1.tar.gz

Download URL cool_linear_solver-0.2.1.tar.gz
Size 12.6 kB
Tags Source
SHA-256 checksum
How to use checksums
aad291112dbba72e2160ec3206980480bdac5bdf5fabaefb613896b75cb00d16
BLAKE2b-256 checksum
How to use checksums
c4f56de457e0dce241beabe5b0f96dfa3fe1d1bbb8fd3e20cb4e2b169582978b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.14

Release files / cool_linear_solver-0.2.1-py3-none-any.whl

Download URL cool_linear_solver-0.2.1-py3-none-any.whl
Size 16.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
168fc7d0205f9d42c1d36ce2b269230cc9b4e41c5b4cb37acbf0ba294eed9827
BLAKE2b-256 checksum
How to use checksums
b880243ee58dd94bac1501de8b51714a3c6120008161f1bce312fe6128b956a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.14

Security for cool-linear-solver 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page