algebra-ring
defines an algebra ring structure
Installation | Example | API | License
Installation
With npm do
npm install algebra-ring
Example
All code in the examples below is intended to be contained into a single file.
Real
Create a ring structure over real numbers.
const ring = // Define operators. { // NaN, Infinity and -Infinity are not allowed return typeof a === 'number' && } { return a === b } { return a + b } { return -a } { return a * b } { return 1 / a } // Create a ring by defining its identities and operators.const R =
You get a Ring that is a Group with multiplication operator. The multiplication operator must be closed respect the underlying set; its inverse operator is division.
This is the list of ring operators:
- contains
- notContains
- equality
- disequality
- addition
- subtraction
- negation
- multiplication
- division
- inversion
The neutral element for addition and multiplication are, as usual, called zero and one respectively.
R // trueR // trueR // true R // 3R // 17 R // true R // -1 R // 10R // 16 R // true R // 0.5 R // trueR // true R // trueR // true R // will complainR // will complain too
Boolean
It is possible to create a ring over the booleans.
const Boole =
There are only two elements, you know, true
and false
.
Boole // true
Check that false
is the neutral element of addition and true
is the
neutral element of multiplication.
Boole // trueBoole // true
As usual, it is not allowed to divide by zero: the following code will throw.
BooleBoole
API
ring(identities, operator)
- @param
{Array}
identities - @param
{*}
identities[0] a.k.a zero - @param
{*}
identities1 a.k.a one - @param
{Object}
operator - @param
{Function}
operator.contains - @param
{Function}
operator.equality - @param
{Function}
operator.addition - @param
{Function}
operator.negation - @param
{Function}
operator.multiplication - @param
{Function}
operator.inversion - @returns
{Object}
ring
ring.error
An object exposing the following error messages:
- cannotDivideByZero
- doesNotContainIdentity
- identityIsNotNeutral