Metadata-Version: 2.4
Name: fuzlog
Version: 0.1.0
Summary: Simple and enjoyable Python library for fuzzy logic.
Keywords: fuzzy,fuzzy-logic,fuzzy-sets,fuzzy-inference,membership-functions,mamdani,sugeno
Author: coredegon
Author-email: coredegon <valikalik15@gmail.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Topic :: Scientific/Engineering
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Typing :: Typed
Requires-Dist: numpy>=2.0
Requires-Dist: matplotlib>=3.9 ; extra == 'plot'
Requires-Python: >=3.12
Project-URL: Repository, https://github.com/coredegon/fuzlog
Project-URL: Documentation, https://github.com/coredegon/fuzlog/tree/main/docs
Project-URL: Issues, https://github.com/coredegon/fuzlog/issues
Provides-Extra: plot
Description-Content-Type: text/markdown

# fuzlog

Fuzzy logic for Python: membership functions, linguistic variables, Mamdani and
Sugeno inference. Three small modules on top of NumPy, fully typed, Python 3.12+.

```sh
pip install fuzlog            # NumPy is the only dependency
pip install "fuzlog[plot]"    # adds matplotlib for the plots
```

## Example

How much to tip, given the service and the food:

```pycon
>>> import fuzlog as fz

>>> service = fz.partition("service", (0, 10), ["poor", "good", "excellent"])
>>> food = fz.Variable("food", (0, 10), rancid=fz.z_shape(1, 4), tasty=fz.s_shape(6, 9))
>>> tip = fz.partition("tip", (0, 30), cheap=5, average=15, generous=25)

>>> tipper = fz.Mamdani(
...     service.poor | food.rancid >> tip.cheap,
...     service.good >> tip.average,
...     service.excellent | food.tasty >> tip.generous,
... )
>>> round(tipper(service=3, food=8), 2)
17.03
>>> tipper(service=[1, 5, 9], food=[2, 5, 8]).round(2).tolist()
[8.2, 15.0, 21.8]
>>> print(*tipper.rules, sep="\n")
if service is poor or food is rancid then tip is cheap
if service is good then tip is average
if service is excellent or food is tasty then tip is generous
```

`tipper.plot(service=3, food=8)` and `tipper.surface("service", "food")` draw:

![The output set inferred for service 3 and food 8, and the tip over both inputs](https://raw.githubusercontent.com/coredegon/fuzlog/main/docs/img/tipper.png)

## What is inside

- **Sets.** 16 shapes: `triangle`, `trapezoid`, `rising`, `falling`, `rectangle`,
  `singleton`, `piecewise`, `gauss`, `gauss2`, `bell`, `sigmoid`, `dsigmoid`,
  `psigmoid`, `s_shape`, `z_shape`, `pi_shape`; any function of your own through
  `MF`. Sets combine with `&`, `|`, `~` and `**`.
- **Variables.** `Variable` names the terms of a quantity; `partition` builds terms
  that always sum to 1, also on periodic universes such as angles.
- **Rules.** `temp.hot & ~humid.dry >> fan.fast`, with weights and the hedges
  `very`, `extremely`, `somewhat`.
- **Inference.** `Mamdani` with any t-norm, s-norm, implication and aggregation and
  the defuzzifiers `centroid`, `bisector`, `mom`, `som`, `lom`; `Sugeno` with
  constant or functional conclusions.
- **Arrays.** A scalar gives a float, arrays give arrays, and inputs broadcast
  against each other.
- **Plots.** `variable.plot()`, `system.plot(...)`, `system.surface(x, y)`, in
  scripts and notebooks alike.

## Documentation

- [Guide](https://github.com/coredegon/fuzlog/blob/main/docs/guide.md): the library from sets to inference
- [Membership functions](https://github.com/coredegon/fuzlog/blob/main/docs/shapes.md): every shape, its definition and its name elsewhere
- [Reference](https://github.com/coredegon/fuzlog/blob/main/docs/reference.md): every public name on one page

## Development

```sh
uv sync                 # environment with the dev tools
uv run pytest           # tests, including every example in the documentation
uv run ruff check       # lint; `uv run ruff format` formats
uv run ty check         # types
```

MIT license.
