Socket
Book a DemoInstallSign in
Socket

@m93a/arithmetic-types

Package Overview
Dependencies
Maintainers
1
Versions
12
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@m93a/arithmetic-types

Interfaces for mathematical data-types to implement

Source
npmnpm
Version
0.0.2
Version published
Weekly downloads
2
Maintainers
1
Weekly downloads
 
Created
Source

Arithmetic Types

Standardized interfaces for mathematical data types, such as Complex, Fraction and Matrix. This repository is a draft, the standard has not been adopted anywhere yet.

Overview

  • Every implementation of an Arithmetic Types interface must provide an Arithmetics object with static methods such as add(x, y) which accept arguments of a single data type and perform arithmetic operations on it.
  • The available interfaces are in scalar-arithmetic.ts and tensor-arithmetic.ts.
  • The methods of Arithmetics must treat the arguments as immutable and return a new instance where applicable.
  • Every instance of the data type must provide a x.clone() method and a reference to the Arithmetics object using x[Symbol.for('arithmetics')].
  • The instances may implement arithmetic methods such as x.add(y); if they do, these arithmetic methods should be mutating.
    • If the instance methods are mutating, then they must return this, ie. x.add(y) returns the mutated x.
    • If the instance methods are not mutating, then they must return the result as a new instance of the data type.

(The words must, should and may follow RFC2119)

Example usage

Static methods

This is how one can implement an arithmetic and geometric mean for fractions, decimals, complex numbers and quaternions etc., regardless of their implementation.

import { NormedDivisionRing, InstanceOf, symbols } from 'arithmetic-types'

const { Arithmetics } = symbols
type Numeric<T, F> = InstanceOf< NormedDivisionRing<T, F> >


function arithmeticMean<T, F>(first: Numeric<T, F>, ...args: Numeric<T, F>[])
{
  const arithmetics = first[Arithmetics]

  const sum = args.reduce( (a, b) => arithmetics.add(a, b), first )
  const count = args.length + 1

  return arithmetics.scale(sum, 1/count)
}


function geometricMean<T, F>(first: Numeric<T, F>, ...args: Numeric<T, F>[])
{
  const arithmetics = first[Arithmetics]
  if (!arithmetics.isCommutative) throw new TypeError('Geometric mean of non-commutative numbers is not supported.')

  const product = args.reduce( (a, b) => arithmetics.mul(a, b), first )
  const count = args.length + 1

  return arithmetics.pow(product, 1/count)
}

Instance methods

This is also an implementation of arithmetic and geometric mean, this time for the data types which support instance methods. This code is potentially faster than the previous one, because it doesn't have to create a new instance for every arithmetic operartion.

import { NormedDivisionRing, InstanceWithMethods } from 'arithmetic-types'

type Numeric<T, F> = InstanceWithMethods< T, NormedDivisionRing<T, F> >


function arithmeticMean<T, F>(first: Numeric<T, F>, ...args: Numeric<T, F>[])
{
  const sum = first.clone()

  args.forEach( a => sum = sum.add(a) )
  sum = sum.scale( 1/(args.length + 1) )

  return sum
}


function geometricMean<T, F>(first: Numeric<T, F>, ...args: Numeric<T, F>[])
{
  if (!first[Arithmetics].isCommutative) throw new TypeError('Geometric mean of non-commutative numbers is not supported.')

  const product = first.clone()

  args.forEach( a => product = product.mul(a) )
  product = product.pow( 1/(args.length + 1) )

  return product
}

FAQs

Package last updated on 16 May 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts