Skip to content

lucabro81/math-helper

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

math-helper

Installation

npm install math-helper --save

Docs

Index

General

isPrime(n: number): boolean

import * as MH from 'math-helper';
 
let is_prime:boolean = MH.General.isPrime(13);
console.log(is_prime); // true
 
let is_not_prime:boolean = MH.General.isPrime(14);    
console.log(is_not_prime); // false

nextPrime(n: number): number

import * as MH from 'math-helper';
 
let next_prime:number = MH.General.nextPrime(12);
console.log(next_prime); // 13

isEven(num: number): boolean;

import * as MH from 'math-helper';
 
console.log(MH.General.isEven(1223)); // false
console.log(MH.General.isEven(1222)); // true

isEvenBitwise(num: number): boolean

Come la precedente ma utilizza operazioni bitwise, utile nel caso di grandi iterazioni

log(base: number, value: number): number

import * as MH from 'math-helper';
 
let log:number = MH.General.isEven(12, 144);
console.log(log); // 2

È possibile specificare una base arbitraria (N.B.: non sono ancora presenti controlli su base e argomento)

powerOfTwo(num: number): boolean

import * as MH from 'math-helper';
 
let result:number = MH.General.powerOfTwo(12); 
console.log(result); // 4096

Calcole le potenze di due specificando l'esponente, con operazioni bitwise

isDivisibleByPowOf2(num: number, divisor: number): boolea

import * as MH from 'math-helper';
 
let is_divisible: boolean = MH.General.isDivisibleByPowOf2(1234, 16);
console.log(is_divisible); // false

Controlla se un numero è divisibile per una potenza di 2 con operazioni bitwise, non funziona correttamente se si passa un divisore che non sia potenza di 2

isDivisible(num: number, divisor: number): boolean

Come il precedente ma usa solo l'operatore % e non ha limitazioni per i dati in ingresso

bitForNumberByBase(num: number, base: number, n_bits: number): number

import * as MH from 'math-helper';
 
let nbit:number = MH.General.bitForNumberByBase(1234, 16); // 1234 -> 0x4D2
console.log(nbit); // 3

Fornisce il numero di bit necessari a rappresentare il numero in ingresso in funzione dalla base fornita

factorial(num: number, tmp_fact?: number): number

import * as MH from 'math-helper';
 
let factorial:number = MH.General.factorial(17);
console.log(factorial); // 355687428096000

Calcolo con funzione tail recursive

Chaining (WIP)

chain(initial_value:any):GeneralChain

import * as MH from 'math-helper';
MH.General.chain(3);

Geometry

plane(a:number, b:number, c:number, d:number):Polynomial3D

import * as MH from 'math-helper';

let data:Array<Array<number>> = Array.from({length: 100}, () => [Math.floor(Math.random() * 100), Math.floor(Math.random() * 100)]);
let plane:Array<number> = MH.Geometry.plane(2, 3, 4)(data);

console.log(plane);

projectOnPlane(polynomial:Polynomial2D, plane:Polynomial3D):Polynomial3D

import * as MH from 'math-helper';

let data:Array<Array<number>> = Array.from({length: 100}, () => [Math.floor(Math.random() * 100), Math.floor(Math.random() * 100)]);

let plane:MH.Polynomial3D = MH.Geometry.plane(2, 3, 4);
let parabola:MH.Polynomial2D = MH.Geometry.parabola(2, 3, 4);

let projection:Array<number> = MH.Geometry.projectOnPlane(parabola, plane)(data);

console.log(projection);

parabola(a:number, b:number, c:number):Polynomial2D

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let parabola:Array<number> = MH.Geometry.parabola(2, 3, 4)(data);

console.log(parabola);

parabolaParametric(a:number, from:number, to:number, step:number):Array<Array>

import * as MH from 'math-helper';

let parabola:Array<number> = MH.Geometry.parabolaParametric(2, 0, 100, 0.01);
console.log(parabola);

ellipse(cx:number, cy:number, a:number, b:number):Polynomial2D

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let ellipse:Array<number> = MH.Geometry.ellipse(2, 3, 4, 5)(data);
console.log(ellipse);

ellipseParametric(cx:number, cy:number, a:number, b:number, rad:number): Array<Array>

import * as MH from 'math-helper';

let ellipse:Array<number> = MH.Geometry.ellipseParametric(2, 3, 4, 5, 0.1);
console.log(ellipse);

Chaining (WIP)

chain(initial_value:any):GeometryChain

import * as MH from 'math-helper';
MH.Geometry.chain(3);

Statistic

arithmeticMean(data:Array):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let mean:Array<number> = MH.Statistic.arithmeticMean(data);

console.log(mean);

arithmeticWeightedMean(data:Array<Array>):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let mean:Array<number> = MH.Statistic.arithmeticWeightedMean(data);

console.log(mean);

linearRegression(data:Array<Array>):Array<Array>

import * as MH from 'math-helper';

let data:Array<Array<number>> = Array.from({length: 100}, () => [Math.floor(Math.random() * 100), Math.floor(Math.random() * 100)]);
let regression:Array<Array<number>> = MH.Statistic.linearRegression(data);

console.log(regression);

binomialCoefficient(n:number, m:number):number

import * as MH from 'math-helper';

let bc:Array<Array<number>> = MH.Statistic.binomialCoefficient(5, 2);

console.log(bc); // 10

howManyPermutationsR(data:Array):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let permutations:number = MH.Statistic.howManyPermutationsR(data);

console.log(permutations);

Permutazioni con ripetizione

howManyPermutations(data:Array):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let permutations:number = MH.Statistic.howManyPermutations(data);

console.log(permutations);

howManyDispositionsR(data:Array, m:number):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let dispositions:number = MH.Statistic.howManyDispositionsR(data);

console.log(dispositions);

Disposizioni con ripetizione

howManyDispositions(data:Array, m:number):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let dispositions:number = MH.Statistic.howManyDispositions(data);

console.log(dispositions);

howManyCombinationsR(data:Array, m:number):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let combinations:number = MH.Statistic.howManyCombinationsR(data);

console.log(combinations);

Combinazioni con ripetizione

howManyCombinations(data:Array, m:number):number

import * as MH from 'math-helper';

let data:Array<number> = Array.from({length: 100}, () => Math.floor(Math.random() * 100));
let combinations:number = MH.Statistic.howManyCombinations(data);

console.log(combinations);

Chaining (WIP)

chain(initial_value:any):StatisticChain

import * as MH from 'math-helper';
MH.Statistic.chain(3);

Trigonometry

sinWithAmplitude(rad:number, amplitude:number):number

import * as MH from 'math-helper';

let sin:number = MH.Trigonometry.sinWithAmplitude(Math.PI/6, 3);
console.log(sin);

sinWithShift(rad:number, shift:number):number

import * as MH from 'math-helper';

let sin:number = MH.Trigonometry.sinWithShift(Math.PI/6, 3);
console.log(sin);

cosWithAmplitude(rad:number, amplitude:number):number

import * as MH from 'math-helper';

let cos:number = MH.Trigonometry.cosWithAmplitude(Math.PI/6, 3);
console.log(cos);

cosWithShift(rad:number, shift:number):number

import * as MH from 'math-helper';

let cos:number = MH.Trigonometry.cosWithShift(Math.PI/6, 3);
console.log(cos);

Chaining (WIP)

chain(initial_value:any):TrigonometryChain

import * as MH from 'math-helper';
MH.Trigonometry.chain(3);

Common (WIP)

chainWith<T extends any, O extends any>(class_ref:T, curr_value?:any):O

import * as MH from 'math-helper';
MH.General
  .chain(3)
  .bitForNumberByBase(6, 8)
  .chainWith<MH.Geometry, MH.GeometryChain>(MH.Geometry, Math.PI/12)
  .ellipseParametric(3, 2,4,5)
  .done()

switchTo(class_ref:T): T

import * as MH from 'math-helper';
MH.General
  .chain(3)
  .bitForNumberByBase(6, 8)
  .switchTo(MH.Geometry)
  .chain(Math.PI/12)
  .ellipseParametric(3, 2,4,5)
  .done()

done():any

import * as MH from 'math-helper';
MH.General
  .chain(3)
  .bitForNumberByBase(6, 8)
  .switchTo(MH.Geometry)
  .chain(Math.PI/12)
  .ellipseParametric(3, 2,4,5)
  .done()

About

little math helper for ts projects

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published