Metadata-Version: 2.4
Name: symbolic-lp
Version: 0.1.2
Summary: Toolkit to express Linear Programs constraints in a symbolic and natural way and get parameterized matrix constraints.
Author: Alexis DAVID
Project-URL: Homepage, https://github.com/davidaleprog/symbolic_lp
Project-URL: Repository, https://github.com/davidaleprog/symbolic_lp
Project-URL: Issues, https://github.com/davidaleprog/symbolic_lp/issues
Keywords: linear-programming,optimization,symbolic,constraints,mathematical-optimization
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Mathematics
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.8
Classifier: Programming Language :: Python :: 3.9
Classifier: Programming Language :: Python :: 3.10
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Requires-Python: >=3.8
Description-Content-Type: text/markdown
Requires-Dist: numpy>=1.21.0
Requires-Dist: torch>=1.10.0
Provides-Extra: dev
Requires-Dist: pytest>=6.2.0; extra == "dev"
Requires-Dist: pytest-cov>=4.0.0; extra == "dev"
Requires-Dist: black; extra == "dev"
Requires-Dist: flake8; extra == "dev"
Requires-Dist: isort; extra == "dev"

# symbolic_lp

Toolkit to express linear program constraints in a symbolic and natural way and obtain parameterized matrix constraints.

## Installation

Install from PyPI:

```sh
pip install symbolic-lp
```

Or build locally:

```sh
python -m build
pip install dist/symbolic_lp-0.1.2-py3-none-any.whl
```

## Usage

```python
from symbolic_lp import Model

model = Model()

x = model.add_var("x", 2)
p = model.add_param("p", 2)

model.add_constraint(x[0] + p[0] <= 10)
model.add_constraint(2 * x[1] - p[1] >= 5)
model.add_constraint(x[0] + x[1] == 7)

# Inequality-only form: Ax <= b
A, b = model.get_ineq_matrices({"p": [3, 4]})

# Split form: Ax <= b and Cx = d
A, b, C, d = model.get_matrices({"p": [3, 4]})

print("A:", A)
print("b:", b)
print("C:", C)
print("d:", d)
```

`get_ineq_matrices()` returns the full inequality expansion. Equality constraints are converted into two inequalities.

`get_matrices()` returns the split matrix form:

- `A, b` for inequality constraints `Ax <= b`
- `C, d` for equality constraints `Cx = d`

## Batched Parameters

You can also pass batched parameter values with NumPy arrays or Torch tensors.

```python
import numpy as np
import torch

np_batched_p_values = np.random.randn(20, 2) + 4
torch_batched_p_values = torch.tensor(np_batched_p_values, dtype=torch.float32)

for batched_p_values in [np_batched_p_values, torch_batched_p_values]:
    A_batch, b_batch = model.get_ineq_matrices({"p": batched_p_values})
    print("A_batch shape:", A_batch.shape)
    print("b_batch shape:", b_batch.shape)
```
