Theory.js Documentation and Change Log

Documentation for Theory.js

  • Abstract Algebra

    • isFiniteGroup - checks if the set, operation, and identity define a finite group

      isFiniteGroup(set, operation, identity)

      in: set = elements in the group
      operation = binary operation on elements of the set
      identity = identity element, where a binary operation on identity and any other element results in the same value
      out: true/false if the set, operation, and identity make up a finite group

      Example:

      0 to 9, mod 10 addition

      true

    • isAbelian - tests if a possible group is an Abelian group

      isAbelian(set, operation, identity)

      in: set = elements in the group
      operation = binary operation on elements of the set
      identity = identity element, where a binary operation on identity and any other element results in the same value
      out: true/false if this is an Abelian group

      Example:

      0 to 9, mod 10 addition

      true

    • isFiniteSubgroup - determines if a subset of a group is a subgroup

      isFiniteSubgroup(set, operation, identity, subset)

      in: set = elements of a group
      operation = closed binary operation on the group
      identity = identity element, where a binary operation on identity and any other element results in the same value
      subset = a subset of the group's set of elements
      out: true/false if the subset is a subgroup

      Example:

      Group = 0 to 9 modulo 10 addition

      Possible subgroup = 0, 2, 4, 6, 8 modulo 10 addition

      true (subset is a subgroup)

    • groupCenter - computes the center of a group

      groupCenter(set, operation, identity)

      in: set = elements of a group
      operation = closed binary operation on the group
      identity = identity element, where a binary operation on identity and any other element results in the same value
      out: The center of the group

      Example:

      Group: N10, 0 to 9 addition modulo 10

      Result: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9

    • centralizer - returns the elements that commute with the passed in element

      centralizer(set, operation, identity, elem)

      in: set = elements of a group
      operation = closed binary operation on the group
      identity = identity element, where a binary operation on identity and any other element results in the same value
      elem = element to find centralizer set for
      out: returns the centralizer set for the element

      Example:

      Group: N10, 0 to 9 addition modulo 10

      element to find centralizer for: 5

      0, 1, 2, 3, 4, 5, 6, 7, 8, 9

    • operationExp - turns addition into multiplication, multiplication into exponential

      operationExp(operation, elem, startElem, power)

      in: operation = a binary operation
      elem = starting element, change on recursion
      startElem = starting element, doesn't change
      power = power of the calculation
      out: the computed result

      Example:

      addition operation, element 5, power 4

      Result: 20

      multiplication operation, element 5, power 2 25

      Result: 25

    • orderOfElem - determines the order of an element in a group (distance to the identity)

      orderOfElem(set, operation, identity, elem)

      in: set: set of the group
      operation: closed binary operation on the group's elements
      identity: number by which any other element in the set combines with and the result is the other element
      elem: the element to determine the order of
      out: order of the element

      Example:

      N10, set 0 to 9 modulo 10 addition, element 5

      Result: 2

      1 to 6, mod 7 multiplication, element 4

      Result: 3

    • cyclicSubgroup - a cyclic subgroup generated from an element in the parent group

      cyclicSubgroup(set, operation, identity, elem)

      in: set: set of the group
      operation: closed binary operation on the group's elements
      identity: number by which any other element in the set combines with and the result is the other element
      elem: the element that generates the cyclic subgroup
      out: the resulting cyclic subgroup

      Example:

      N10, set 0 to 9 modulo 10 addition, element 3

      Result: 3,6,9,2,5,8,1,4,7,0

      1 to 6, mod 7 multiplication, element 2

      2,4,1

  • Combinatorics

    • fib - Fibonacci sequence generator

      fib(size)

      in: size = number of elements of the sequence to return
      out: a Fibonacci sequence

      Example:

      fib(5);
      1,1,2,3,5

    • factorial - Calculates factorial of a number

      factorial(num)

      in: num = integer to compute factorial for
      out: result of factorial computation

      Example:

      factorial(5);
      120

      factorial(10);
      3628800

    • partialPerm - Calculates the partial permutations of a set

      partialPerm(k, n)

      in: k = number of selected items
      n = number of items
      out: partial permutations (ordering matters)

      Example:

      partialPerm(3, 5)
      (5 objects, 3 selected)
      60

    • combination - Calculates the number of combinations of items, where order doesn't matter

      combination(k, n)

      in: k = number of selected items
      n = number of items
      out: combinations (ordering doesn't matter)

      Example:

      combination(3, 5)
      (5 objects, 3 selected)
      10

  • Core Functions

    • sequence - Generate a wide range of sequences

      sequence(seedArr, size, seqFunc)

      in: seedArr = starting values of sequence, as an array
      size = total size of the sequence returned, including the seed
      seqFunc = function to create the next value from the preceding value - must return a number
      (the entire array of current results is sent to this function on each loop through)
      out: the generated sequence

      Example:

      "fib" uses this function internally:

      var result = sequence([1, 1], size, function(a) {
      var al = a.length;
      return a[al - 1] + a[al - 2];
      });

    • reduce - perform a function to combine the values in a list into a single value

      reduce(list, reduceFunc)

      in: list = the list to reduce
      reduceFunc = the function that condenses the list, arguments "item" and "total"
      out: total of the values derived from the list

      Example:

      "factorial" uses this function internally:

      var result = reduce(range, function(i, total) {
      return total * i;
      });

  • Number Theory

    • factors - find the factors of a given number

      factors(num)

      in: num = the number to factor
      out: a list of prime factors

      Example:

      factors(28)

      2,7

    • factorize - find the factors and their powers for a number

      factorize(num)

      in: num = number to factorize
      out: factors and their powers

      Example:

      factorize(28)

      [[2, 2], [7, 1]]

      (represents 2^2 + 7^1)

    • GCD - calculates the greatest common divisor for two numbers

      GCD(a, b)

      in: a = number 1
      b = number 2
      out: GCD for a and b

      Example:

      GCD(2124, 54)

      18

    • LCM - calculates the least commmon multiple for two numbers

      in: a = number 1
      b = number 2 out: the least common multiple of a and b

      Example:

      LCM(8, 42)

      168

  • Set Theory

    • isUnique - returns true/false if the set has all unique elements

      isUnique(a)

      in: one numerical array
      out: true/false if all values are unique

      Example:

      isUnique([1, 1, 2, 2, 4]);

      false

      isUnique([1, 2, 3, 5]);

      true

    • makeUnique - eliminates duplicates from a set

      makeUnique(a)

      in: a numerical array
      out: returns an array with all values unique

      Example:

      makeUnique([1, 1, 2, 2, 3, 4, 4, 4, 5]);

      1,2,3,4,5

    • intersect - intersection of two sets

      intersect(a, b)

      in: Takes two numerical arrays
      out: returns one array of the intersection of the two arrays - unique values

      Example:

      intersect([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 4, 8, 16, 32]);

      [1,2,4,8]

    • setUnion - union of two sets, unique values

      setUnion(a, b)

      in: two arrays to compute the union of
      out: the unique values in one, the other, or both arrays

      Example:

      setUnion([1, 2, 2, 3, 3, 4], [1, 2, 4, 4, 8]);

      [1,2,3,4,8]

    • cartesian - the Cartesian Product of two sets

      cartesian(a, b)

      in: two arrays (sets)
      out: Cartesian product of the two sets

      Example:

      cartesian([1, 2, 3, 4], [8, 16, 32, 64]);

      [[1,8],[1,16],[1,32],[1,64],[2,8],[2,16],[2,32],[2,64],[3,8],[3,16],[3,32],[3,64],[4,8],[4,16],[4,32],[4,64]]

    • isSubset - true/false if set a is a subset of set b

      isSubset(a, b)

      in: a = possible subset
      b = possibly same set or superset of a
      out: true/false if a is a subset of b

      Example:

      isSubset([1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7, 8]);

      true

      isSubset([1, 2, 4, 8, 16, 32], [1, 2, 4, 8, 16, 32]);

      true

    • isProperSubset - true/false if set a is a proper subset of set b

      isProperSubset(a, b)

      in: a = possible subset of b
      b = any set
      out: true/false is a is a proper subset of b

      Example:

      isProperSubset([1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7, 8]);

      true

      isProperSubset([1, 2, 4, 8, 16, 32], [1, 2, 4, 8, 16, 32]);

      false

    • isDisjoint - true/false if a and b are disjoint sets

      isDisjoint(a, b)

      in:a = set 1
      b = set 2
      out: true/false is a and b are disjoint sets

      Example:

      isDisjoint([1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7, 8]);

      false

      isDisjoint([1, 2, 3, 4, 5], [1, 2, 3, 4, 5, 6, 7, 8]);

      true

    • setDiff - returns the set difference from a to b

      setDiff(a, b)

      in:a = set to calculate difference to b
      b = set 2
      out:set difference of a to b

      Example:

      setDiff([1, 2, 4, 8, 16], [1, 2, 3, 4, 5]);

      8,16

      setDiff([1, 2, 4, 8, 16], [1, 2, 4, 8, 16, 32, 64, 128]);

      (empty array)

    • complement - returns set difference of a to b where b is a subset of a

      complement(a, b)

      in: a = set to compute difference of
      b = subset of a
      out: if b is not a subset of a, null. Otherwise, set difference of a to b

      Example:

      complement([1, 2, 4, 8, 16], [1, 2, 3, 4, 5]);

      null

      complement([1, 2, 4, 8, 16], [1, 2, 4]);

      8,16

    • symDiff - Symmetric difference; items in either a or b but not both

      symDiff(a, b)

      in: a = set one
      b = set two
      out:items in a or b but not both

      Example:

      symDiff([1, 2, 4, 8, 16], [1, 2, 3, 4, 5]);

      8,16,3,5

  • Statistics

    • mean - calculates the average (mean) of the numbers in the provided list

      mean(list)

      in: one numerical array
      out: average (mean) of the values in the provided list

      Example:

      mean([1, 4, 7, 8, 22, 31]);

      12.166666666666666

    • median - calculates the middle value (median) of the numbers in the provided list

      median(list)

      in: one numerical array
      out: middle value (median) of the values in the provided list

      Example:

      median([1, 4, 7, 8, 22, 31]);

      7.5

    • mode - calculates the most frequently occuring value(s) (mode) of the numbers in the provided list

      mode(list)

      in: one numerical array
      out: mode value(s) for the items in the provided list

      Example:

      mode([1, 3, 3, 7, 9, 21]);

      3

      mode([1, 1, 2, 2, 5, 7, 7]);

      1, 2, 7

    • max - calculates the highest value in the provided list

      max(list)

      in: one numerical array
      out: maximum value in the list

      Example:

      max([1, 4, 88, 2, 30, 101, 30]);

      101

    • min - calculates the lowest value in the provided list

      min(list)

      in: one numerical array
      out: minimum value in the list

      Example:

      min([15, 0, -5, 200, 30, 10, 30]);

      -5

    • range - calculates the difference between the highest value and the lowest value in the provided list

      range(list)

      in: one numerical array
      out: range for the list's values

      Example:

      range([15, 0, -5, 200, 30, 10, 30]);

      205

    • quartiles - calculates the 1st and 3rd quartiles, Q1 and Q3

      quartiles(list)

      in: one numerical array
      out: the Q1 and Q3 values in the list

      Example:

      quartiles([1, 4, 7, 14, 22, 40, 60, 70, 80]);

      7, 60

    • IQR - calculates the Interquartile Range, between Q1 and Q3

      IQR(list)

      in: one numerical array
      out: the difference between the Q1 and Q3 values in the list

      Example:

      IQR([1, 4, 7, 14, 22, 40, 60, 70, 80]);

      53

    • outliers - returns numbers 1.5 IQR or more beyond Q1 or Q3

      outliers(list)

      in: one numerical array
      out: any outliers in the list

      Example:

      outliers([-10, 5, 15, 22, 28, 30, 36, 60, 97]);

      97

    • variance - returns the variance in the list - in units squared

      variance(list)

      in: one numerical array
      out:variance in the list in units squared

      Example:

      variance([5, 15, 12, 23, 15, 14, 3]);

      45.285714285714285

    • stdDeviation - returns the standard deviation in the list

      stdDeviation(list)

      in: one numerical array
      out: standard deviation in the list

      Example:

      stdDeviation([5, 15, 12, 23, 15, 14, 3]);

      6.729466121893644

Change Log for Theory.js

  • 0.1.2

    Added a number of statistics functions including mean, mode, median, variance, standard deviation, quartiles, max, min, range, Interquartile Range, and outliers.

  • 0.1.1

    Significant additions.

    Adds functions for number theory and abstract algebra. Specifically, GCD, LCM, factors, and factorization for number theory, and a handful of group theory functions.

  • 0.1.0

    Initial release.

    Introduces functions for set theory and combinatorics, plus core functions for sequences and a reduce operation.