compute.ts
v2.0.3
Published
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
Downloads
10
Maintainers
Readme
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 project provides several libraries that each comes with dozens of operators. Please note the cross-compatibility of the libraries.
- @compute.ts/boolean
- @compute.ts/number
- @compute.ts/string
- @compute.ts/date
- @compute.ts/dictionary
- @compute.ts/function
- @compute.ts/math
- @compute.ts/structural
Usage
Imports
The engine library permits you to import the operators in a very simple way.
import {/* required operators */} from "@compute.ts/{library}" ;
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.
import {number} from "@compute.ts/number" ;
const x = number();
x.affect(12);
Expression
The library 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.
import {sum, number} from "@compute.ts/number";
const x = number();
const y = number();
const fx = sum(x, y);
fx.eval()
toString
The library allows you to visualize a node in a very convinient way.
import {sum, number} from "@compute.ts/number";
const x0 = number(12);
const x1 = number(12);
const x2 = number(12);
const y0 = sum(x0, x1);
const y1 = minus(x1, x2);
const z = divide(y0, y1);
z.toString();
// ☐ divide
// ├─ ☐ sum
// │ ├─ ☑ 2
// │ └─ ☑ ∅
// └─ ☐ minus
// ├─ ☑ ∅
// └─ ☑ ∅
//
// ☐ = the expression need to be evaluated
// ☑ = the expression has been evaluated
// ∅ = the expression value is undefined
x1.affect(4);
x2.affect(8);
z.eval();
z.toString();
// ☑ divide(6, -4) ➔ -1.5
// ├─ ☑ sum(2, 4) ➔ 6
// │ ├─ ☑ 2
// │ └─ ☑ 4
// └─ ☑ minus(4, 8) ➔ -4
// ├─ ☑ 4
// └─ ☑ 8
//
// ➔ = current value of the expression
// ☑ = the expression has been evaluated
alias
If you create an alias for a given expression.
import {sum, number} from "@compute.ts/number";
const x0 = number(12);
const x1 = number(4);
const x2 = number(8);
const y0 = sum(x0, x1);
const y1 = minus(x1, x2).alias('y1');
const z = divide(y0, y1);
z.eval();
z.toString();
// ☑ divide ➔ -1.5
// ├─ ☑ sum ➔ 6
// │ ├─ ☑ 2
// │ └─ ☑ 4
// └─ ☑ -4 [y1]
describe
If you create a description for a given expression.
import {sum, number} from "@compute.ts/number";
const x0 = number(12);
const x1 = number(4);
const x2 = number(8);
const x3 = number(4);
const y0 = sum(x0, x1);
const z = divide(y0, x3).alias('z').describe('The final result');
z.eval();
z.toString();
// ☑ divide ➔ -1.5 [z: The final result]
// ├─ ☑ sum ➔ 6
// │ ├─ ☑ 2
// │ └─ ☑ 4
// └─ ☑ -4
Example
Polynom computation
import {number} from "@compute.ts/number";
import {x2} from "@compute.ts/math";
const x = number();
const a = number(3);
const b = number(5);
const c = number(7);
const fx = a.times(x2(x)).plus(b.times(x)).plus(c);
x.affect(1)
fx.eval(); // = 15
Polynom resolution
import {number, zero} from "@compute.ts/number";
import {x2, sqrt} from "@compute.ts/math";
const a = number();
const b = number();
const c = number();
const delta = x2(b).minus(times(4, a, c));
const hasSolution = delta.greaterOrEqualThan(zero);
const x1 = opposite(b).minus(sqrt(delta)).divideBy(times(2, a));
const x2 = opposite(b).sum(sqrt(delta)).divideBy(times(2, a));
a.affect(1);
b.affect(2);
c.affect(3);
hasSolution.eval() // = true
x1.eval(); // = 2/3
x2.eval(); // = 1
About the author
I am a software developer with 4 years of project specializing in the development of web solutions. Digital Nomad, I work while traveling. After 3 years into the french industry, I've started to work as a freelance software architect or fullstack developer.
Based on state-of-the-art web technologies, I offer you to architect & develop the best version of your project. My experience in the web app development assure you to build a nice looking, performant and stable product.
Minimalist, I like to travel, to meet people, learn new things and handmade stuff. Scuba & sky diving licenced. I like also hamburgers, Kinder chocolate and crepes. Karate black belt.
https://berthellemy.com/