SequenceM

SequenceM is a small JavaScript library for dealing with Sequences. SequenceM is great for dealing with arithmetic, geometric, and custom sequences.


Key Features:

If you want to define mathematical sequences easily and quickly check out the documentation. Or check out some cool examples of what SequenceM can do for you!
Feel free to use it however you like.


Installation

You can find all the source code here. The npm is here. If you are using npm, install it with
npm install sequencem
You can then load all the sequence types with
var {Sequence} = require("SequenceM");
var {ArithmeticSequence} = require("SequenceM");
var {GeometricSequence} = require("SequenceM");
Click here for Arithmetic Sequences
Click here for Geometric Sequences

Custom Sequences

Define a Sequence like this: var sequence = new Sequence() The Sequence function accepts 2 arguments, the function for determining every element in the sequence and extra options. The function must return a value. This value can be determined by any way you like. The function is given 3 arguments to use. The first argument is the position in the sequence that is being requested.
The next two elements are only provided if the "recursive" option is set to true. The second argument is the value of the previous element in the sequence. The third element is all the cached values of the sequence in order.
Options
When creating a Sequence, the second argument is the options object.

Options Object Properties:


"recursive"
Setting the recursive option to true will force the sequence to load all previous elements, it will also provide the main function with the value of the previous element and the list of all cached elements.
"inital"
The 0th element of the sequence will be cached as this value.
"values"
Values is a array. It initially sets the cache.
"preload"
The Sequence will cache all elements from 0 to the preload value.
"cache"
Setting cache to false will disable caching. I don't advise using this option because it can hurt performance, especially with recursive functions.

Sequence Methods:


.get()

Every sequence has the .get() function. It accepts 1 OR 2 arguments. If you provide 1 argument, it will return the value of that element. If you provide 2 arguments, it will return a array containing all the values from the first argument to the second argument (Inclusive to both arguments. The array contains empty values for all values from 0 to the first argument.)

.sum()

Every sequence also has the .sum() function. It accepts 1 OR 2 arguments. If you provide 1 argument, it will return the value of all elements from 0 too the argument (inclusive to 0 and the argument). If you provide 2 arguments, it will return the value of all elements from the first argument to the second argument (inclusive to both)

.set()

.set() does not work on Arithmetic or Geometric sequences, or sequences with caching disabled. It accepts 2 arguments. The first argument specifies the element in cache to set.(use a number) The second argument is the value to set the element to.

.load()

.load() does not work on Arithmetic or Geometric sequences, or sequences with caching disabled. It accepts 1 OR 2 arguments. If one argument is provided, the corresponding element is loaded into memory. If a second argument is proveded, all elements from the first element to the second element are loaded into memory (inclusive to both arguments)

Arithmetic and Geometric Sequences

Both Arithmetic and Geometric sequences are HIGHLY optimized. Whenever you can, use them. Their summation function is dramatically more efficient.
Arithmetic Sequences
They are created by using the ArithmeticSequence() function. Arithmetic sequences require 2 arguments, the base and the difference. The 0th element is the base and all elements are the difference more than the previous element.(subtraction works)
Geometric Sequences
They are created by using the GeometricSequence() function. Arithmetic sequences require 2 arguments, the base and the ratio. The 0th element is the base and all elements are the ratio times more than the previous element.(fractions work)

Examples:

//all whole numbers factorialized
var factorial=new Sequence(function(a,b){return a*b}, {recursive:true, initial:1})

//The Fibonacci Sequence
var fibonacci = new Sequence(function(a,b,c){return c[a-2]+b}, {values:[0,1], recursive:true})

//Lazy Caterer's sequence
var caterer=new Sequence(function(a){return (Math.pow(a,2)+a+2)/2})

//Tetration (hyper-4 operation) function (only works with whole numbers)
function tetrate(f,s) {return Math.pow(f, new Sequence(function(a,b){return Math.pow(b, f)},{recursive:true, initial:f}).get(s-2))}

//All natural numbers including zero
var natural=new ArithmeticSequence(0,1)


The Github page

The npm page