colibri-engine

The engine is based on incremental computation algorithms. When a calculation is submitted to the engine, all the computed values are memorized. So, if you change some variable and query an evaluation, the engine is able to compute the result very fast be

Usage no npm install needed!

<script type="module">
  import colibriEngine from 'https://cdn.skypack.dev/colibri-engine';
</script>

README

Colibri-Engine

Presentation

The engine is based on incremental computation algorithms. When a calculation is submitted to the engine, all the computed values are memorized. So, if you change some variable and query an evaluation, the engine is able to compute the result very fast because it recomputes only what has changed.

Libraries

The API provides several libraries that each comes with dozens of operators.

Imports

Typescript

import {ColibriEngine} from 'colibri-engine';

Javascript

const {ColibriEngine} = require('colibri-engine');

Usage

Create a model

Variable

The engine API permits you to define some special scalars called variables. There are special because once the model is loaded into the engine, you are able to change its values with the affect operation.

const {number} = new ColibriNumberLibrary();
const x = number();
// Now the x object can be used to build expressions

Expression

The API engine allows you to create expressions such as "and", "sum", "isLessThan". An expression represents a step of the computation of the calculation. There, take a set of nodes (variables or expressions) and return a single value—the expressions value.

const {sum, number} = new ColibriNumberLibrary();
const x = number();
const y = number();
const fx = sum(x, y);
// Now the fx is an expression that evaluates the sum of x & y

Alias

The API engine allows you to create aliases. The creation of an alias permits you to refer to an expression through an easy-to-read name.

const {number} = new ColibriNumberLibrary();
number().alias('x');
// Now the model includes a variable known as 'x'

Query the model

Affectation

engine.affect({alias1: value1, alias2: value2, ..., aliasN: valueN});

The engine permits you to affect values to variables of the model according to given aliases.

const {ColibriEngine} = new ColibriEngine();

const engine = new ColibriEngine();
engine.affect({'x': 12, 'y': 'hello_world', 'z': true});

Evaluation

engine.eval([alias1, alias2, ..., aliasn]);

The engine permits you to evaluate expressions of the model according to given aliases.

const {ColibriEngine} = new ColibriEngine();

const engine = new ColibriEngine();
const {values} = engine.eval(['x', 'y', 'z']);

Example

Polynom computation

// imports the operators
const {number} = new ColibriNumberLibrary();
const {x2} = new ColibriMathLibrary();

// create the model
const x = number().alias('x');
const a = number(3).alias('a');
const b = number(5).alias('b');
const c = number(7).alias('c');
const fx = a.times(x2(x)).plus(b.times(x)).plus(c).alias('fx');

// Query the engine
const engine = new ColibriEngine();
engine.affect({'x': 1});
const {values} = engine.eval(['fx']);
// values = {'fx': 21}

Polynom resolution

// Imports the operators
const {number, zero} = new ColibriNumberLibrary();
const {x2, sqrt} = new ColibriMathLibrary();

// Create the model
const a = number().alias('a');
const b = number().alias('b');
const c = number().alias('c');
const delta = x2(b).minus(times(4, a, c)).alias('delta');
const hasSolution = delta.greaterOrEqualThan(zero).alias('hasSolution');
const x = opposite(b).divide(times(2, a)).alias('x');
opposite(b).minus(sqrt(delta)).divide(times(2, a)).alias('x1');
opposite(b).sum(sqrt(delta)).divide(times(2, a)).alias('x2');

// Query the engine
const engine = new ColibriEngine();
engine.affect({'a': 1, 'b': 2, 'c': 3});
const {values} = engine.eval(['x1', 'x2']);
// values = {'x1': 2/3, 'x2': 1}