# @thi.ng/fuzzy

> Fuzzy logic operators & configurable rule inferencing engine

Latest version **2.1.144** (published 2026-08-25) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install @thi.ng/fuzzy
pnpm add @thi.ng/fuzzy
yarn add @thi.ng/fuzzy
bun add @thi.ng/fuzzy
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.1.144 |
| Published | 2026-08-25 |
| First published | 2020-12-22 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM |
| Node | >=18 |
| Dependencies | 2 |
| Unpacked size | 60.7 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Author | Karsten Schmidt |
| Maintainers | thi.ng |
| Keywords | agent, functional, fuzzy, inference, logic, math, rule, t-norm, typescript |

## Links

- npm: https://www.npmjs.com/package/@thi.ng/fuzzy
- Repository: https://codeberg.org/thi.ng/umbrella
- Homepage: https://thi.ng/fuzzy
- Funding: https://github.com/sponsors/postspectacular
- npm.io page: https://npm.io/package/@thi.ng/fuzzy

## Dependencies (2)

- [@thi.ng/api](https://npm.io/package/@thi.ng/api.md) ^8.12.28
- [@thi.ng/math](https://npm.io/package/@thi.ng/math.md) ^5.15.17

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 2.1.144 (latest) — 2026-08-25
- 2.1.143 — 2026-07-02
- 2.1.142 — 2026-06-05
- 2.1.141 — 2026-05-23
- 2.1.140 — 2026-05-20
- 2.1.138 — 2026-05-11
- 2.1.137 — 2026-05-08
- 2.1.136 — 2026-04-26
- 2.1.135 — 2026-04-18
- 2.1.134 — 2026-04-02
- 2.1.133 — 2026-03-19
- 2.1.132 — 2026-03-07
- 2.1.131 — 2026-03-07
- 2.1.130 — 2026-02-07
- 2.1.129 — 2026-01-23
- … 148 more at https://npm.io/package/@thi.ng/fuzzy/versions

## README

<!-- This file is generated - DO NOT EDIT! -->
<!-- Please see: https://codeberg.org/thi.ng/umbrella/src/branch/develop/CONTRIBUTING.md#changes-to-readme-files -->
# ![@thi.ng/fuzzy](https://codeberg.org/thi.ng/umbrella/media/branch/develop/assets/banners/thing-fuzzy.svg?1e243dac)

[![npm version](https://img.shields.io/npm/v/@thi.ng/fuzzy.svg)](https://www.npmjs.com/package/@thi.ng/fuzzy)
![npm downloads](https://img.shields.io/npm/dm/@thi.ng/fuzzy.svg)
[![Mastodon Follow](https://img.shields.io/mastodon/follow/109331703950160316?domain=https%3A%2F%2Fmastodon.thi.ng&style=social)](https://mastodon.thi.ng/@toxi)

> [!NOTE]

> This is one of 217 standalone projects. LLM-free, human-made and
> cared for software, maintained as part of the
> [@thi.ng/umbrella](https://codeberg.org/thi.ng/umbrella/) ecosystem and
> anti-framework.
>
> 🚀 Please help me to work full-time on these projects by [sponsoring
> me](https://codeberg.org/thi.ng/umbrella/src/branch/develop/CONTRIBUTING.md#donations).
> Thank you! ❤️

- [About](#about)
  - [Features](#features)
  - [References / Further reading](#references--further-reading)
- [Status](#status)
- [Support packages](#support-packages)
- [Installation](#installation)
- [Dependencies](#dependencies)
- [API](#api)
  - [Fuzzy set generators & combinators](#fuzzy-set-generators--combinators)
  - [Linguistic variables](#linguistic-variables)
  - [Rule creation & inferencing](#rule-creation--inferencing)
- [Authors](#authors)
- [License](#license)

## About

Fuzzy logic operators & configurable rule inferencing engine.

### Features

- Entirely declarative & functional approach
- Fully type checked
- Fuzzy set domain shaping & composition functions (incl. negated / inverse)
- Various [T-norms &
  S-norms](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy/src/tnorms.ts),
  incl. parametric versions
- Rules with multiple inputs/outputs and arbitrary term combinators (i.e.
  T-norms). Syntax sugar for common `and`/`or` rules.
- Defuzzification via customizable strategies and options to balance precision
  vs. performance
  - Maxima: First, Last, Mean
  - Center-of-Gravity (COG)
- Linguistic variable creation and term/set classification for given domain
  values
- Fuzzy set visualization (via
  [@thi.ng/fuzzy-viz](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy-viz))

### References / Further reading

- [Fuzzy Logic (Wikipedia)](https://en.wikipedia.org/wiki/Fuzzy_logic)
- [T-norm (Wikipedia)](https://en.wikipedia.org/wiki/T-Norm)
- [Fuzzy Logic - University of Western Australia](https://teaching.csse.uwa.edu.au/units/CITS7212/Lectures/Students/Fuzzy.pdf)
- [Introduction to Fuzzy Logic](https://www.researchgate.net/publication/267041266_Introduction_to_fuzzy_logic)
- [Defuzzification (Wikipedia)](https://en.wikipedia.org/wiki/Defuzzification)
- [Defuzzification methods](https://cse.iitkgp.ac.in/~dsamanta/courses/archive/sca/Archives/Chapter%205%20Defuzzification%20Methods.pdf)
- [Comparison of the COG Defuzzification Technique](https://arxiv.org/pdf/1612.00742.pdf)

## Status

**STABLE** - used in production

[Search or submit any issues for this package](https://codeberg.org/thi.ng/umbrella/issues?q=%5Bfuzzy%5D)

## Support packages

- [@thi.ng/fuzzy-viz](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy-viz) - Visualization, instrumentation & introspection utils for [@thi.ng/fuzzy](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy)

## Installation

```bash
yarn add @thi.ng/fuzzy
```

ESM import:

```ts
import * as fuzzy from "@thi.ng/fuzzy";
```

Browser ESM import:

```html
<script type="module" src="https://esm.run/@thi.ng/fuzzy"></script>
```

[JSDelivr documentation](https://www.jsdelivr.com/)

For Node.js REPL:

```js
const fuzzy = await import("@thi.ng/fuzzy");
```

Package sizes (brotli'd, pre-treeshake): ESM: 1.62 KB

## Dependencies

- [@thi.ng/api](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/api)
- [@thi.ng/math](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/math)

Note: @thi.ng/api is in _most_ cases a type-only import (not used at runtime)

## API

[Generated API docs](https://docs.thi.ng/umbrella/fuzzy/)

(See
[tests](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy/test) for
more usage examples).

### Fuzzy set generators & combinators

Generators:

- `constant()`
- `point()`
- `ramp()` / `invRamp()`
- `triangle()`
- `trapezoid()`
- `sigmoid()` / `invSigmoid()`
- `gaussian()`

Combinators:

- `negate()`
- `weighted()`
- `alphaCut()` / `invAlphaCut()`
- `compose()` / `intersect()` / `union()`

### Linguistic variables

Linguistic variables (short: **L-var**s) are groupings of named (and possibly
overlapping) fuzzy sets within a given value domain. The can be used standalone
or as inputs/outputs in rules (further below).

The
[@thi.ng/fuzzy-viz](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy-viz)
package provides utilities to visualize the fuzzy sets of an L-var.

![fuzzy set visualization of the example
l-var](https://codeberg.org/thi.ng/umbrella/media/branch/develop/assets/fuzzy/temperature-lvar-2.svg)

```ts
import {
    variable, invSigmoid, sigmoid, trapezoid,
    evaluate, classify
} from "@thi.ng/fuzzy";

// temperature sets (in celsius)
const temp = variable(
  // value domain
  [-20, 40],
  {
    freezing: invSigmoid(0, 2),
    cold: trapezoid(-1, 2, 16, 20),
    warm: trapezoid(15, 20, 30, 34),
    hot: sigmoid(32, 2)
  }
);

// evaluate all fuzzy sets for given domain value
evaluate(temp, 18)
// {
//   freezing: 2.220446049250313e-16,
//   cold: 0.5,
//   warm: 0.6,
//   hot: 6.914400106935423e-13
// }

evaluate(temp, 28)
// {
//   freezing: 0,
//   cold: 0,
//   warm: 1,
//   hot: 0.0003353501304664781
// }

// classify temperature (min confidence 33%, default: 50%)
classify(temp, 28, 0.33)
// "warm"
```

### Rule creation & inferencing

Example taken from Franck Dernoncourt's [Introduction to Fuzzy
Logic](https://www.researchgate.net/publication/267041266_Introduction_to_fuzzy_logic):

![fuzzy set illustration from F.Dernoncourt's
tutorial](https://codeberg.org/thi.ng/umbrella/media/branch/develop/assets/fuzzy/fuzzy-matrix-dernoncourt.png)

```ts
import {
    variable, ramp, invRamp, gaussian, triangle,
    or, defuzz, meanOfMaximaStrategy
} from "@thi.ng/fuzzy";

// define fuzzy input variables
const inputs = {
  food: variable([0, 10], {
    awful: invRamp(1, 3),
    delicious: ramp(7, 9),
  }),
  service: variable([0, 10], {
    poor: gaussian(0, 1.5),
    good: gaussian(5, 1.5),
    excellent: gaussian(10, 1.5),
  }),
};

const outputs = {
  tip: variable([0, 30], {
    low: triangle(0, 5, 10),
    medium: triangle(10, 15, 20),
    high: triangle(20, 25, 30),
  }),
};

// l-vars, rules and defuzzification are using generics for type safety
// we define these 2 type aliases for brevity
type I = typeof inputs;
type O = typeof outputs;

// rule definitions:
// if service is poor OR food is awful -> tip is low
// if service is normal -> tip is medium
// if service is excellent OR food is delicious -> tip is high
const rules = [
  or<I, O>({ food: "awful", service: "poor" }, { tip: "low" }),
  or<I, O>({ service: "good" }, { tip: "medium" }),
  or<I, O>({ food: "delicious", service: "excellent" }, { tip: "high" }),
];

// defuzzification using default center-of-gravity strategy
defuzz(
  inputs,
  outputs,
  rules,
  // input values
  { food: 7.32, service: 7.83 },
);
// { tip: 22.650000000000034 }

// defuzz with custom strategy (note: each has further config options)
defuzz(
  inputs,
  outputs,
  rules,
  // input values
  { food: 7.32, service: 7.83 },
  // custom defuzz strategy
  meanOfMaximaStrategy()
);
// { tip: 25.050000000000043 }
```

Note: The results are slightly different than those in the textbook example, due
to different `gaussian` fuzzy sets used for the `service` L-var.

Using `instrumentStrategy()` from the upcoming
[@thi.ng/fuzzy-viz](https://codeberg.org/thi.ng/umbrella/src/branch/develop/packages/fuzzy-viz)
package, we can also visualize the final, transformed fuzzy sets used to compute
crisp results and highlight the position of the crisp result value.

Here is the ASCII art output for the
[`centroidStrategy`](https://docs.thi.ng/umbrella/fuzzy/functions/centroidStrategy.html)
and using `tnormMin` (the default) to transform each rule's output set(s):

```ts
import { defuzz, centroidStrategy } from "@thi.ng/fuzzy";
import { instrumentStrategy, fuzzySetToAscii } from "@thi.ng/fuzzy-viz";

// wrap existing strategy
const strat = instrumentStrategy(centroidStrategy(), fuzzySetToAscii());

// call defuzz as normal
defuzz(inputs, outputs, rules, strat);

// obtain secondary results via deref()
console.log(strat.deref()[0]);
```

```text
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅▅.....
.......................................................................▁|██████████████████████▇....
......................................................................▁█|███████████████████████▇...
....................................▅▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▆▅....▂██|████████████████████████▇..
...................................▅█████████████████████████████▅..▂███|██████████████████████████.
..................................▅███████████████████████████████▅▂████|███████████████████████████
                                                                        ^ 21.52
```

Different results can be obtained by adjusting the
[T-norm](https://en.wikipedia.org/wiki/T-Norm) used to transform each rule's
output sets, here using `tnormHamacher(2)`.

```text
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|..........................
.........................................................................|.........▃▂...............
.........................................................................|.......▃███▆▂.............
.........................................................................|....▁▅███████▇▃...........
................................................▁▃▆▃▁....................|.▁▄▇████████████▆▃........
...........................................▁▂▄▆███████▆▄▂▁..............▂|▇██████████████████▆▃▁....
...................................▁▂▃▄▅▆▇█████████████████▇▆▅▄▃▂▁..▂▄▆██|███████████████████████▅▃▁
                                                                         ^ 21.84
```

...or using `tnormAczelAlsina(2)` (there're many more available):

```text
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|...........................
........................................................................|.....▂▃▄▅▅▅▅▅▄▄▃▁..........
........................................................................|.▂▅▇██████████████▆▄.......
........................................................................|▇████████████████████▆▂....
........................................▁▂▃▄▄▅▅▆▆▆▆▆▆▆▅▅▄▄▃▂▁.........▃█|███████████████████████▆▁..
....................................▂▄▇███████████████████████▇▄▂....▆██|█████████████████████████▄.
..................................▃▇█████████████████████████████▇▃▂████|██████████████████████████▇
                                                                        ^ 21.58
```

Just for illustration purposes (and using a different example), SVG output can
be obtained by merely switching to another instrumentation function (here
`fuzzySetToSvg()`):

![fuzzySetToSvg() visualization example](https://codeberg.org/thi.ng/umbrella/media/branch/develop/assets/fuzzy/strategy-viz.svg)

## Authors

- [Karsten Schmidt](https://thi.ng)

If this project contributes to an academic publication, please cite it as:

```bibtex
@misc{thing-fuzzy,
  title = "@thi.ng/fuzzy",
  author = "Karsten Schmidt",
  note = "https://thi.ng/fuzzy",
  year = 2020
}
```

## License

&copy; 2020 - 2026 Karsten Schmidt // Apache License 2.0

---
_Source: https://npm.io/package/@thi.ng/fuzzy · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
