algebra-group
defines an algebra group structure
Table Of Contents
Installation
With npm do
npm install algebra-group
Examples
All code in the examples below is intended to be contained into a single file.
Integer additive group
Create the Integer additive group.
const algebraGroup = // Define identity element.const zero = 0 // Define operators. { // NaN, Infinity and -Infinity are not allowed return typeof n === 'number' && && n % 1 === 0} { return a === b } { return a + b } { return -a } // Create Integer additive group a.k.a (Z, +).const Z =
You get a group object with zero as identity and the following group operators:
- contains
- notContains
- equality
- disequality
- addition
- subtraction
- negation
Z // trueZ // falseZ // falseZ // trueZ // trueZ // trueZ // false, 4.5 is not an integer Z // 3Z // 10 Z // -5 Z // 4Z // 0 Z // true
R\{0}
multiplicative group
Consider R\{0}
, the set of Real numbers minus 0, with multiplication as composition law.
It is necessary to remove 0, otherwise there is an element which inverse does not belong to the group, which breaks group laws.
It makes sense to customize group props, which defaults to additive group naming.
{ // NaN, Infinity and -Infinity are not allowed return typeof n === 'number' && n !== 0 && } { return a * b } { return 1 / a } { // Consider // // 0.1 + 0.2 === 0.3 // // It evaluates to false. Actually the expression // // 0.1 + 0.2 // // will return // // 0.30000000000000004 // // Hence we need to approximate equality with an epsilon. return Math < NumberEPSILON} // Create Real multiplicative group a.k.a (R, *). const R =
You get a group object with one as identity and the following group operators:
- contains
- notContains
- equality
- disequality
- multiplication
- division
- inversion
R // trueR // trueR // true R // 0.5 // 2 * 3 * 5 = 30 = 60 / 2R // true
R+
multiplicative group
Create the multiplicative group of positive real numbers (0,∞)
.
It is a well defined group, since
- it has an indentity
- it is close respect to its composition law
- for every element, its inverse belongs to the set
Let's customize group props, with a shorter naming.
{ // NaN, Infinity are not allowed return typeof n === 'number' && n > 0 && } const Rp =
You get a group object with one identity and the following group operators:
- contains
- notContains
- eq
- ne
- mul
- div
- inv
Rp // trueRp // trueRp // trueRp // 8
API
algebraGroup(identity, operator)
- @param
{Object}
given identity and operators - @param
{*}
given.identity a.k.a. neutral element - @param
{Function}
given.contains - @param
{Function}
given.equality - @param
{Function}
given.compositionLaw - @param
{Function}
given.inversion - @param
{Object}
[naming] - @param
{String}
[naming.identity=zero] - @param
{String}
[naming.contains=contains] - @param
{String}
[naming.equality=equality] - @param
{String}
[naming.compositionLaw=addition] - @param
{String}
[naming.inversion=negation] - @param
{String}
[naming.inverseCompositionLaw=subtraction] - @param
{String}
[naming.notContains=notContains] - @returns
{Object}
groups
algebraGroup.errors
An object exposing the following errors:
- ArgumentIsNotInGroupError
- EqualityIsNotReflexiveError
- IdentityIsNotInGroupError
- IdentityIsNotNeutralError
const ArgumentIsNotInGroupError EqualityIsNotReflexiveError IdentityIsNotInGroupError IdentityIsNotNeutralError } = algebraGrouperrors
You can then do something like this
try // Some code that could raise an error. catch error
For example, the following snippets will throw the corresponding error.
ArgumentIsNotInGroupError
R // 0 is not in group R\{0}Rp // -1 is not in R+
EqualityIsNotReflexiveError
IdentityIsNotInGroupError
IdentityIsNotNeutralError