Skip to content

computationalabstraction/patNum.js

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

A Computational Mathematics Toolkit

patNum.js provides constructs and utilites to work with-

1. Statistics

2. Vectors

3. Matrices

4. Fractions

5. Polynomials

6. Complex Numbers

7. Waves

8. Geometry

9. Sets

10. Enumerative Combinatorics

Statistics

Min (Lowest Observation)

pn.Min( [ 13, 22, 26, 38, 36, 42 ] ); // Returns - 13

Max (Highest Observation)

pn.Max( [ 49, 50, 77, 81, 98, 110 ] ); // Returns - 110

Range (Span of Data)

// Range = Max - Min
pn.Range( [ 49, 50, 77, 81, 98, 110 ] ); // Returns - 61

AFrequency (Frequency of A Specific Number)

pn.AFrequency( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] , 2 );
// Returns - { Value: 2 , Occurance: 2 }

SomeFrequencies (Frequency of Some Numbers)

pn.SomeFrequencies( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] , [ 4, 2 ] );
// Returns - [ { Value: 4 , Occurance: 2 } , { Value: 2 , Occurance: 2 } ]

AllFrequencies (Frequency of All the Numbers)

pn.AllFrequencies( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - Frequencies of all the Numbers

Mean (Central Tendency)

pn.Mean( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 8.63

Medain (Central Tendency)

pn.Median( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 4

Mode (Central Tendency)

pn.Mode( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 11

SampleStandardDeviation (Deviation)

pn.SampleStandardDeviation( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 12.46

PopulationStandardDeviation (Deviation)

pn.PopulationStandardDeviation( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 11.88

MeanAbsoluteDeviation (Deviation)

pn.MeanAbsoluteDeviation( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 7.47

MedianAbsoluteDeviation (Deviation)

pn.MedianAbsoluteDeviation( [ 11, 2, 45, 5, 3, 2, 4, 11, 4, 3, 5 ] );
// Returns - 2

Other Operations

- GeometricMean

- HarmonicMean

- RootMeanSquare

- SampleVariance

- PopulationVariance

- BernoulliDistribution

Matrix

All Operations also work with Scalars

Matrix Creation

const m1 = pn.matrix([
    [1000,2000,3000,4000],
    [100,200,300,400]
]);

Matrix Addition

const m1 = pn.matrix([
    [1000,2000,3000,4000],
    [100,200,300,400]
]);

const m2 = pn.matrix([
    [100,200,300,400],
    [1000,2000,3000,4000]
]);

const m3 = m1.add(m2); // Returns a New Matrix

Matrix Subtraction

const m1 = pn.matrix([
    [10,20],
    [100,200]
]);

const m2 = pn.matrix([
    [100,200,300,400],
    [1000,2000,3000,4000]
]);

const m3 = m1.subtract(m2); // Returns a New Matrix

Matrix Hadamard Multiply

const m1 = pn.matrix([
    [1,2],
    [10,20]
]);

const m2 = pn.matrix([
    [5,10],
    [50,100]
]);

const m3 = m1.multiply(m2); // Returns a New Matrix

Matrix Dot Product (Matrix Linear Multiplication)

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
])

const m2 = pn.matrix([
    [10,20,30],
    [40,50,60]
])

const m3 = m1.dot(m2); // Returns a New Matrix

Matrix Divide

const m1 = pn.matrix([
    [1,2],
    [10,20]
]);

const m2 = pn.matrix([ 2 , 20 ]);

const m3 = m1.divide(m2); // Returns a New Matrix

Matrix Modify

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
]);

m1.at(1,1,100); // It will change 10 -> 100

Matrix Map

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
]);

const m2 = m1.map(e => e ** 2); // It will return a new Matrix

Matrix Transpose

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
]);

const m2 = m1.transpose(); // Returns a New Matrix

Matrix Clone

const m1 = pn.matrix([
    [1,2],
    [10,20]
]);

const m2 = m1.clone(); // Returns a New Matrix

Matrix Randomize

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
]);

m1.randomize(0,10); // Will choose a random value from 0 to 10 for all the elements of the Matrix
m1.randomize(); // By Default range is from -1 to 1

Matrix Flatten

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
]);

let am1 = m1.flatten(); // It will flatten the Matrix to [10,20,30,40,...] and return the Array

Matrix FlatMap

const m1 = pn.matrix([
    [10,20],
    [30,40],
    [50,60]
]);

let am1 = m1.flatMap( e => e * 2 );
// It will flatten [10,20,30,40,...] and then apply the operation to each element [20,40,60,80,...] and return the Array

Matrix Minor

const m1 = pn.matrix([
    [9,3,5],
    [-6,-9,7],
    [-1,-8,1]
]);

let min = m1.minor(1,1); // will return a minor matrix

Matrix Cofactors

const m1 = pn.matrix([
    [9,3,5],
    [-6,-9,7],
    [-1,-8,1]
]);

let cf = m1.cofactor(1,2); // will return the cofactor value

Matrix Determinant

const m1 = pn.matrix([
    [5,7],
    [10,20]
]);

let det = m1.determinant(); // will return the determinant

Matrix Inverse ( Type 1 )

const m1 = pn.matrix([
    [9,3,5],
    [-6,-9,7],
    [-1,-8,1]
]);

let inv = m1.inverse(); // will return and inverse matrix and may contain decimal values

Matrix Inverse ( Type 2 )

const m1 = pn.matrix([
    [9,3,5],
    [-6,-9,7],
    [-1,-8,1]
]);

let inv = m1.inverse2(); // will return and inverse matrix of fractions

Matrix toString

const m1 = pn.matrix([
    [9,3,5],
    [-6,-9,7],
    [-1,-8,1]
]);

console.log(m1.toString()); // will print the Matrix in a formatted fashion

Row Vector

const m1 = pn.vector([ 10, 20, 30 ]);

Column Vector

const m1 = pn.colVector([ 10, 20, 30 ]);
Vector is internally a Matrix and supports all the operations which are supported on Matrix

Fractions

Creating Fractions

const f1 = pn.fraction(2,7);

Printing Fractions

const f1 = pn.fraction(3,9);
console.log(f1.toString());

Adding Fractions

const f1 = pn.fraction(5,2);
const f2 = pn.fraction(7,3);
const f3 = f1.add(f2);
console.log(f3.toString());

Substracting Fractions

const f1 = pn.fraction(4,3);
const f2 = pn.fraction(8,9);
const f3 = f1.substract(f2);
console.log(f3.toString());

Multiplying Fractions

const f1 = pn.fraction(5,2);
const f2 = pn.fraction(4,8);
const f3 = f1.multiply(f2);
console.log(f3.toString());

Dividing Fractions

const f1 = pn.fraction(5,10);
const f2 = pn.fraction(2,5);
const f3 = f1.divide(f2);
console.log(f3.toString());

Reducing Fractions

const f1 = pn.fraction(25,50);
console.log(f1.reduce().toString());

Representing Recurring Number in Fraction Form

const rf = recurringToFraction(1,undefined,3); // represents 1.33333333...
// First parameter is digits before the dot
// Second parameter is non-recurring digits after the dot if not pass undefined
// Third parameter is recurring digits after the dot
console.log(rf.toString());

Complex Numbers

Complex Number Creation

const c1 = pn.complex(10,2); // 10(Real) , 2(Coefficient of i)

Complex toString

const c1 = pn.complex(10,2);
console.log(c1.toString()); // -> 10 + 2i

Note: Any Operation which transforms Complex Number in any way returns a new Complex Object

Complex add

const c1 = pn.complex(10,2);
const c2 = pn.complex(5,3);
console.log(c1.add(c2).toString()); 

Complex substract

const c1 = pn.complex(10,2);
const c2 = pn.complex(5,3);
console.log(c1.substract(c2).toString()); 

Complex multiply

const c1 = pn.complex(5,7);
const c2 = pn.complex(-6);
console.log(c1.multiply(c2).toString());

Complex divide

const c1 = pn.complex(5,10);
console.log(c1.divide(2).toString());

Complex pow

const c1 = pn.complex(5,10);
console.log(c1.pow(2).toString());

Complex sqrt

const c1 = pn.complex(4,4);
console.log(c1.sqrt().toString());

Complex exp

const c1 = pn.complex(4,4);
console.log(c1.exp().toString());

Complex log

const c1 = pn.complex(10,10);
console.log(c1.log().toString());

Complex sin

const c1 = pn.complex(8,9);
console.log(c1.sin().toString());

Complex cos

const c1 = pn.complex(1,7);
console.log(c1.cos().toString());

Complex sinh

const c1 = pn.complex(8,9);
console.log(c1.sinh().toString());

Complex cosh

const c1 = pn.complex(1,7);
console.log(c1.cosh().toString());

Complex angle (Angle of Complex Number in Degrees)

const c1 = pn.complex(6,2);
console.log(c1.angle());

Complex mod

const c1 = pn.complex(-5,10);
console.log(c1.mod().toString());

Complex conjugate

const c1 = pn.complex(5,10);
console.log(c1.conjugate().toString());

Complex addinv (Additive Inverse)

const c1 = pn.complex(5,10);
console.log(c1.addinv().toString());

Complex mulinv (Multiplicative Inverse)

const c1 = pn.complex(5,10);
console.log(c1.mulinv().toString());

Complex toArray

const c1 = pn.complex(5,10);
console.log(c1.toArray());

Waves

Simple Harmonic Motion Wave

const y = pn.oscillatorWave({ 
    amplitude:0.5,
    period:3.2
});
console.log(y.of(4)); 

General Wave (One Dimensional Harmonic Motion Wave which Propogates)

const z = pn.wave({
    amplitude:3,
    wavelength:4,
    period:8,
    start:3
});
console.log(z.of(2,1)); 

Superpostion of General Waves

const z = pn.wave({
    amplitude:3,
    wavelength:4,
    period:8,
    start:3
});

// You can pass variable number of waves as paramenters while calling superimpose
const sz = z.superimpose(z); 
// superimpose method returns a Superposition Object which will be internally an amalgamation of all the waves imposed on the original wave

// Superposition Object internally keeps a list with references to all the waves of which then combine to create Superposition. Superposition follows the same protocol/interface as Wave so it can be used in a composite fashion shown in the next example

// Will amplify the original wave by the factor of 2
console.log(sz.of(2,1)); 

Composite Superpostion

const x = pn.wave({
    amplitude:3,
    wavelength:4,
    period:8,
    start:3
});

const sx = x.superimpose(x); // Will amplify the original wave by the factor of 2

const z = pn.wave({
    amplitude:5,
    wavelength:2,
    period:4,
    start:5
});

const sz = z.superimpose(z,sx); // Can also pass Superpositions Instances 

console.log(sz.of(2,1)); 

Composite Superpostion example with phase shift

const w1 = pn.wave({
    amplitude:3,
    wavelength:4,
    period:8,
    start:1
});

const w2 = pn.wave({
    amplitude:3,
    wavelength:4,
    period:8,
    start:1,
    shift:180
});

const sw = w1.superimpose(w1,w1,w2);

const w3 = pn.wave({
    amplitude:5,
    wavelength:2,
    period:3,
    start:1
});

const ssw = w3.superimpose(w3,sw);

console.log(ssw.of(2,0)); // It will be 3 + 3 + 3 - 3 + 5 + 5 = 16 ( Adding the Amplitudes )