Code Monkey home page Code Monkey logo

algebra-ring's Introduction

algebra-ring

defines an algebra ring structure

Installation | Example | API | License

NPM version Build Status JavaScript Style Guide

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 = require('algebra-ring')

// Define operators.
function contains (a) {
  // NaN, Infinity and -Infinity are not allowed
  return (typeof a === 'number' && isFinite(a))
}

function equality (a, b) { return a === b }

function addition (a, b) { return a + b }

function negation (a) { return -a }

function multiplication (a, b) { return a * b }

function inversion (a) { return 1 / a }

// Create a ring by defining its identities and operators.
const R = ring([0, 1], {
  equality: equality,
  contains: contains,
  addition: addition,
  negation: negation,
  multiplication: multiplication,
  inversion: inversion
})

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.contains(10) // true
R.contains(-1, 0.5, Math.PI, 5) // true
R.notContains(Infinity) // true

R.addition(1, 2) // 3
R.addition(2, 3, 5, 7) // 17

R.equality(R.negation(2), -2) // true

R.subtraction(2, 3) // -1

R.multiplication(2, 5) // 10
R.multiplication(2, 2, 2, 2) // 16

R.equality(R.inversion(10), 0.1) // true

R.division(1, 2) // 0.5

R.equality(R.addition(2, R.zero), 2) // true
R.equality(R.subtraction(2, 2), R.zero) // true

R.equality(R.multiplication(2, R.one), 2) // true
R.equality(R.division(2, 2), R.one) // true

R.division(1, 0) // will complain
R.inversion(R.zero) // will complain too

Boolean

It is possible to create a ring over the booleans.

const Boole = ring([false, true], {
  equality: (a, b) => (a === b),
  contains: (a) => (typeof a === 'boolean'),
  addition: (a, b) => (a || b),
  negation: (a) => (a),
  multiplication: (a, b) => (a && b),
  inversion: (a) => (a)
})

There are only two elements, you know, true and false.

Boole.contains(true, false) // true

Check that false is the neutral element of addition and true is the neutral element of multiplication.

Boole.addition(true, Boole.zero) // true
Boole.multiplication(true, Boole.one) // true

As usual, it is not allowed to divide by zero: the following code will throw.

Boole.division(true, false)
Boole.inversion(Bool.zero)

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

License

MIT

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.