Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@rgsoft/math

Package Overview
Dependencies
Maintainers
0
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@rgsoft/math - npm Package Compare versions

Comparing version 1.1.0 to 1.2.0

137

dist/index.d.ts

@@ -0,1 +1,38 @@

/**
* Converts an integer to its string representation on a given base
* @param { number } n Number
* @param { number } b Base
* @returns { string }
*/
declare function toBase(n: number, b: number): string;
/**
* Converts a string representing a number in a certain base to an integer
* @param { string } d Digits
* @param { number } b Base
* @returns { string }
*/
declare function fromBase(d: string, b: number): number;
/**
* Calculates the factorial of a positive integer
* @param { number } n Number to calculate from
* @param { number } m (Optional) Number to calculate to
* @returns { number }
*/
declare function factorial(n: number, m?: number): number;
/**
* Calculates the combination of n taking by k.
* @param { number } n
* @param { number } k
* @returns { number }
*/
declare function combination(n: number, k: number): number;
/**
* Calculates the permutation of n taking by k.
* @param { number } n
* @param { number } k
* @returns { number }
*/
declare function permutation(n: number, k: number): number;
declare class Complex {

@@ -35,2 +72,23 @@ readonly a: number;

/**
*
* @param { number } n Total experiments
* @param { number } p Success probability
* @param { number } x (Optional) Value to evaluate
*/
declare function binomial(n: number, p: number, x?: number): number | number[];
/**
*
* @param { number } l Average
* @param { number } k Value to evaluate
*/
declare function poisson(l: number, k: number): number | number[];
/**
*
* @param { number } r Successful experiments
* @param { number } p Success probability
* @param { number } x Value to evaluate
*/
declare function negativeBinomial(r: number, p: number, x: number): number | number[];
declare class Vector {

@@ -140,48 +198,2 @@ private _x;

declare class Segment {
readonly p: Vector;
readonly q: Vector;
constructor(p: Vector, q: Vector);
/**
*
* @param { Vector } r
* @returns { boolean }
*/
isOnSegment(r: Vector): boolean;
/**
* To find orientation of ordered triplet (p, q, r).
* The function returns following values
* 0 when p, q and r are collinear; -1 when clockwise, 1 counterclockwise
*
* @param { Vector } p
* @param { Vector } q
* @param { Vector } r
* @returns { number }
*/
private getOrientation;
/**
*
* Returns true if intersects with this segment
*
* @param { Segment } segment
* @returns { boolean }
*/
intersects(segment: Segment): boolean;
}
/**
* Converts an integer to its string representation on a given base
* @param { number } n Number
* @param { number } b Base
* @returns { string }
*/
declare function toBase(n: number, b: number): string;
/**
* Converts a string representing a number in a certain base to an integer
* @param { string } d Digits
* @param { number } b Base
* @returns { string }
*/
declare function fromBase(d: string, b: number): number;
declare function mod(n: number, m: number): number;

@@ -232,2 +244,33 @@ /**

export { Complex, Line, Segment, Vector, collatz, digitalRoots, factors, fromBase, gcd, lcm, mod, prime, toBase, totient };
declare class Segment {
readonly p: Vector;
readonly q: Vector;
constructor(p: Vector, q: Vector);
/**
*
* @param { Vector } r
* @returns { boolean }
*/
isOnSegment(r: Vector): boolean;
/**
* To find orientation of ordered triplet (p, q, r).
* The function returns following values
* 0 when p, q and r are collinear; -1 when clockwise, 1 counterclockwise
*
* @param { Vector } p
* @param { Vector } q
* @param { Vector } r
* @returns { number }
*/
private getOrientation;
/**
*
* Returns true if intersects with this segment
*
* @param { Segment } segment
* @returns { boolean }
*/
intersects(segment: Segment): boolean;
}
export { Complex, Line, Segment, Vector, binomial, collatz, combination, digitalRoots, factorial, factors, fromBase, gcd, lcm, mod, negativeBinomial, permutation, poisson, prime, toBase, totient };

@@ -27,4 +27,7 @@ "use strict";

Vector: () => Vector,
binomial: () => binomial,
collatz: () => collatz,
combination: () => combination,
digitalRoots: () => digitalRoots,
factorial: () => factorial,
factors: () => factors,

@@ -35,2 +38,5 @@ fromBase: () => fromBase,

mod: () => mod,
negativeBinomial: () => negativeBinomial,
permutation: () => permutation,
poisson: () => poisson,
prime: () => prime,

@@ -42,2 +48,108 @@ toBase: () => toBase,

// src/bases.ts
var alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
function toBase(n, b) {
if (!Number.isInteger(n) || n < 0) {
throw new Error("The number must be positive integer");
}
if (!Number.isInteger(b) || b <= 0) {
throw new Error("The base must be a positive integer");
}
if (b < 2) {
throw new Error("The min base is 2");
}
if (b > alphabet.length) {
throw new Error(`The max base allowed is ${alphabet.length}`);
}
let e = 1;
const digits = [];
while (n / (e * b) >= 1) {
e *= b;
}
do {
digits.push(alphabet[Math.floor(n / e)]);
n = n % e;
e /= b;
} while (e >= 1);
return digits.join("");
}
function fromBase(d, b) {
if (!Number.isInteger(b) || b <= 0) {
throw new Error("The base must be a positive integer");
}
if (b < 2) {
throw new Error("The min base is 2");
}
if (b > alphabet.length) {
throw new Error(`The max base allowed is ${alphabet.length}`);
}
const digits = d.split("").reverse();
return digits.reduce((prev, curr, e) => {
const n = alphabet.indexOf(curr);
if (n < 0) {
throw new Error(`Digit ${curr} not found`);
}
if (n >= b) {
throw new Error(`Invalid digit ${curr} for base ${b}`);
}
return prev + n * b ** e;
}, 0);
}
// src/combination.ts
function factorial(n, m = NaN) {
if (!Number.isInteger(n) || n < 0) {
throw new Error("The number must be a positive integer");
}
if (isNaN(m)) {
m = 1;
} else {
if (!Number.isInteger(m) || m < 0) {
throw new Error("The lower limit must be a positive integer");
}
if (n < m) {
throw new Error("The lower limit cannot be higher than the number");
}
}
if (n === 0) {
return 1;
}
let f = 1;
while (n >= m) {
f *= n;
n--;
}
return f;
}
function combination(n, k) {
if (!Number.isInteger(n) || n < 0) {
throw new Error("The number of elements must be a positive integer");
}
if (!Number.isInteger(k) || k < 0) {
throw new Error("The group quantity must be a positive integer");
}
if (k > n) {
throw new Error("The group quantity cannot be greater than the total elements");
}
if (k === 0 || k === n) {
return 1;
}
return factorial(n, Math.max(k + 1, n - k + 1)) / factorial(Math.min(k, n - k));
}
function permutation(n, k) {
if (!Number.isInteger(n) || n < 0) {
throw new Error("The number of elements must be a positive integer");
}
if (!Number.isInteger(k) || k < 0) {
throw new Error("The group quantity must be a positive integer");
}
if (k > n) {
throw new Error("The group quantity cannot be greater than the total elements");
}
if (k === 0) {
return 1;
}
return factorial(n, n - k + 1);
}
// src/complex.ts

@@ -151,2 +263,43 @@ var Complex = class _Complex {

// src/discrete.ts
function binomial(n, p, x = NaN) {
if (!Number.isInteger(n) || n < 1) {
throw new Error("The number of experiments must be a positive integer");
}
if (!(p >= 0 && p <= 1)) {
throw new Error("The success probability must be between 0 and 1");
}
if (!isNaN(x)) {
if (!Number.isInteger(x) || x < 0) {
throw new Error("The value must be a positive integer");
}
if (x > n) {
throw new Error("The value cannot ve larger than the experiments number");
}
return combination(n, x) * p ** x * (1 - p) ** (n - x);
}
return Array(n + 1).fill(0).map((_, i) => combination(n, i) * p ** i * (1 - p) ** (n - i));
}
function poisson(l, k) {
if (!Number.isInteger(l) || l < 0) {
throw new Error("The number of experiments must be a positive integer");
}
if (!Number.isInteger(k) || k < 0) {
throw new Error("The value must be a positive integer");
}
return l ** k * Math.exp(-l) / factorial(k);
}
function negativeBinomial(r, p, x) {
if (!Number.isInteger(r) || r < 1) {
throw new Error("The number of experiments must be a positive integer");
}
if (!(p >= 0 && p <= 1)) {
throw new Error("The success probability must be between 0 and 1");
}
if (!Number.isInteger(x) || x < 0) {
throw new Error("The value must be a positive integer");
}
return combination(r + x - 1, x) * p ** r * (1 - p) ** x;
}
// src/vector.ts

@@ -357,112 +510,2 @@ var Vector = class _Vector {

// src/segment.ts
var Segment = class {
constructor(p, q) {
this.p = p;
this.q = q;
}
/**
*
* @param { Vector } r
* @returns { boolean }
*/
isOnSegment(r) {
return (this.q.x - this.p.x) * (r.y - this.p.y) === (this.q.y - this.p.y) * (r.x - this.p.x);
}
/**
* To find orientation of ordered triplet (p, q, r).
* The function returns following values
* 0 when p, q and r are collinear; -1 when clockwise, 1 counterclockwise
*
* @param { Vector } p
* @param { Vector } q
* @param { Vector } r
* @returns { number }
*/
getOrientation(p, q, r) {
let val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return val > 0 ? -1 : 1;
}
/**
*
* Returns true if intersects with this segment
*
* @param { Segment } segment
* @returns { boolean }
*/
intersects(segment) {
let o1 = this.getOrientation(this.p, this.q, segment.p);
let o2 = this.getOrientation(this.p, this.q, segment.q);
let o3 = this.getOrientation(segment.p, segment.q, this.p);
let o4 = this.getOrientation(segment.p, segment.q, this.q);
if (o1 != o2 && o3 != o4) {
return true;
}
if (o1 == 0 && this.isOnSegment(segment.p)) {
return true;
}
if (o2 == 0 && this.isOnSegment(segment.q)) {
return true;
}
if (o3 == 0 && segment.isOnSegment(this.p)) {
return true;
}
if (o4 == 0 && segment.isOnSegment(this.q)) {
return true;
}
return false;
}
};
// src/bases.ts
var alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
function toBase(n, b) {
if (!Number.isInteger(n) || n < 0) {
throw new Error("The number must be positive integer");
}
if (!Number.isInteger(b) || b <= 0) {
throw new Error("The base must be a positive integer");
}
if (b < 2) {
throw new Error("The min base is 2");
}
if (b > alphabet.length) {
throw new Error(`The max base allowed is ${alphabet.length}`);
}
let e = 1;
const digits = [];
while (n / (e * b) >= 1) {
e *= b;
}
do {
digits.push(alphabet[Math.floor(n / e)]);
n = n % e;
e /= b;
} while (e >= 1);
return digits.join("");
}
function fromBase(d, b) {
if (!Number.isInteger(b) || b <= 0) {
throw new Error("The base must be a positive integer");
}
if (b < 2) {
throw new Error("The min base is 2");
}
if (b > alphabet.length) {
throw new Error(`The max base allowed is ${alphabet.length}`);
}
const digits = d.split("").reverse();
return digits.reduce((prev, curr, e) => {
const n = alphabet.indexOf(curr);
if (n < 0) {
throw new Error(`Digit ${curr} not found`);
}
if (n >= b) {
throw new Error(`Invalid digit ${curr} for base ${b}`);
}
return prev + n * b ** e;
}, 0);
}
// src/number.ts

@@ -589,2 +632,62 @@ function mod(n, m) {

}
// src/segment.ts
var Segment = class {
constructor(p, q) {
this.p = p;
this.q = q;
}
/**
*
* @param { Vector } r
* @returns { boolean }
*/
isOnSegment(r) {
return (this.q.x - this.p.x) * (r.y - this.p.y) === (this.q.y - this.p.y) * (r.x - this.p.x);
}
/**
* To find orientation of ordered triplet (p, q, r).
* The function returns following values
* 0 when p, q and r are collinear; -1 when clockwise, 1 counterclockwise
*
* @param { Vector } p
* @param { Vector } q
* @param { Vector } r
* @returns { number }
*/
getOrientation(p, q, r) {
let val = (q.y - p.y) * (r.x - q.x) - (q.x - p.x) * (r.y - q.y);
if (val == 0) return 0;
return val > 0 ? -1 : 1;
}
/**
*
* Returns true if intersects with this segment
*
* @param { Segment } segment
* @returns { boolean }
*/
intersects(segment) {
let o1 = this.getOrientation(this.p, this.q, segment.p);
let o2 = this.getOrientation(this.p, this.q, segment.q);
let o3 = this.getOrientation(segment.p, segment.q, this.p);
let o4 = this.getOrientation(segment.p, segment.q, this.q);
if (o1 != o2 && o3 != o4) {
return true;
}
if (o1 == 0 && this.isOnSegment(segment.p)) {
return true;
}
if (o2 == 0 && this.isOnSegment(segment.q)) {
return true;
}
if (o3 == 0 && segment.isOnSegment(this.p)) {
return true;
}
if (o4 == 0 && segment.isOnSegment(this.q)) {
return true;
}
return false;
}
};
// Annotate the CommonJS export names for ESM import in node:

@@ -596,4 +699,7 @@ 0 && (module.exports = {

Vector,
binomial,
collatz,
combination,
digitalRoots,
factorial,
factors,

@@ -604,2 +710,5 @@ fromBase,

mod,
negativeBinomial,
permutation,
poisson,
prime,

@@ -606,0 +715,0 @@ toBase,

{
"name": "@rgsoft/math",
"version": "1.1.0",
"version": "1.2.0",
"description": "Yet another JS math library",

@@ -5,0 +5,0 @@ "main": "./dist/index.js",

@@ -16,344 +16,8 @@ # math-js

## Complex Numbers
## Funcionalities
The complex number library has some methods for performing calculations on
complex numbers. To create a complex number of the form `a + bi`:
```js
const cpx = new Complex(a, b);
```
---
> 📝 Every complex number is inmutable; both, the real and the imaginary
> parts are readonly.
---
### Magnitude
```js
const c = new Complex(5, 12);
console.log(c.mag); // 13
```
### Conjugate
```js
const c = new Complex(2, 7);
console.log(`${c.conjugate()}`); // 2 - 7i
```
### Addition
#### Scalar Addition
```js
// r + (a + bi) = (a + r) + bi
const c1 = new Complex(5, 12);
const c2 = c1.add(4);
console.log(`${c2}`); // 9 + 12i
```
#### Complex Addition
```js
// (a + bi) + (c + di) = (a + c) + (b + d)i
const c1 = new Complex(4, -8);
const c2 = new Complex(-3, 6);
const c3 = c1.add(c2);
console.log(`${c3}`); // 1 + 2i
```
### Substraction
#### Scalar Substraction
```js
// (a + bi) - r = (a - r) + bi
const c1 = new Complex(9, 1);
const c2 = c1.sub(10);
console.log(`${c2}`); // -1 + 1i
```
#### Complex Substraction
```js
// (a + bi) - (c + di) = (a - c) + (b - d)i
const c1 = new Complex(4, -8);
const c2 = new Complex(-3, 6);
const c3 = c1.add(c2);
console.log(`${c3}`); // 1 + 2i
```
### Multiplication
#### Scalar Multiplication
```js
// r (a + bi) = ra + bi
const c1 = new Complex(4, -1);
const c2 = c1.mult(-5);
console.log(`${c2}`); // -20 + 5i
```
#### Complex Multiplication
```js
// (a + bi) (c + di) = (ac - bd) + (ad + bc)i
const c1 = new Complex(3, -1);
const c2 = new Complex(-2, 1);
const c3 = c2.mult(c2);
console.log(`${c3}`); // -5 + 5i
```
### Division
#### Scalar Division
```js
// (a + bi) / r = a / r + (b / r)i
const c1 = new Complex(4, -1);
const c2 = c1.div(-2);
console.log(`${c2}`); // -2 + 0.5i
```
#### Complex Division
```js
let c1 = new Complex(2, 1);
let c2 = new Complex(-1, -1);
let c3 = c1.div(c2);
console.log(`${c3}`); // -1.5 + 0.5i
```
### Square Root
```js
let c = new Complex(4, 0);
c = c.sqrt();
console.log(`${c}`); // 2 + 0i
```
## Vectors
Instantiation:
```js
const vector = new Vector(x, y);
```
### Angle
```js
const v = new Vector(1, 1);
console.log(v.angle); // (Math.PI * 0.5);
```
### Magnitude
```js
const v = new Vector(1, 1);
console.log(v.mag); // (Math.SQRT2);
```
### Normalization
```js
const v = new Vector(4, 4);
v.nomalize();
console.log(v.mag); // 1;
```
### Scalar Multiplication
```js
const v = new Vector(0, 3);
v.mult(4);
console.log(v.mag); // 12;
console.log(v.y); // 12;
```
### Addition
```js
const v = new Vector(-2, 3);
v.add(new Vector(3, -5));
console.log(v.x); // 1
console.log(v.y); // -2
```
### Substraction
```js
const v = new Vector(-2, 3);
v.sub(new Vector(3, -5));
console.log(v.x); // -5
console.log(v.y); // 8
```
### Distance to Another Vector
```js
const v = new Vector(7, 2);
console.log(v.dist(new Vector(3, -1))); // 5
```
### Angle to Another Vector
```js
const v1 = new Vector(0, 1);
const v2 = new Vector(1, 0);
console.log(v1.angleTo(v2)); // Math.PI * 0.5
```
### Create Vector from Angle
```js
let v = Vector.fromAngle(Math.PI * 0.5);
console.log(v.x); // 0
console.log(v.y); // 1
```
### Equality with Other Vector
```js
const v1 = new Vector(7, 2);
const v2 = new Vector(7, 2);
console.log(v1.equals(v2)); // true
```
### Copy Vector
```js
const v = new Vector(7, 2);
const cv = v.copy();
```
## Modulo
The `mod` function calculates the modular remainder of a number `n` modulo `m`.
The main diference between this function and the remainder operator `%` is that
the `mod` function applies the modular arithmetic strictly. For example
```javascript
console.log(-1 % 4);
```
will output `-1`. Whereas using the `mod`:
```javascript
console.log(mod(-1, 4));
```
we should get `3`.
The `mod` function will fail if it receives a negative modulo or any non-integer
number.
## Greatest Common Divisor
The `gcd` function calculates greatest common divisor between to positive
integers
```javascript
console.log(gcd(18, 4));
```
will ouput `2`.
## Least Common Multiple
The `lcm` function calculates least common multple between to positive integers
```javascript
console.log(lcm(18, 4));
```
will ouput `36`.
## Is a Number Prime
The `prime` checks if a positive integer is prime or not
```javascript
console.log(prime(17));
```
will ouput `true`.
## Prime Factorization
The `factors` function gets the prime factors with their respective exponents
```javascript
console.log(factors(48));
```
will ouput `[ [2, 4], [3, 1] ]`, where each element of the array is another
array with two numbers:
1. The prime factor
2. The exponent of the factor
In the example, this means that `48` is `2^4 + 3^1`.
## Number Totient
The `totient` function gets the [totient](https://en.wikipedia.org/wiki/Euler%27s_totient_function)
from a positive integer, that is, the number of prime numbers from 1 to `n-1`
for a given integer `n`.
```javascript
console.log(totient(100));
```
will ouput `40`.
## Collatz Sequence
The `collatz` function gets the [collatz](https://en.wikipedia.org/wiki/Collatz_conjecture)
sequence from a positive integer.
```javascript
console.log(collatz(5));
```
will ouput `[ 5, 16, 8, 4, 2, 1 ]`.
Optionally, it accepts a second parameter that limits the maximum length of the
resulting sequence.
```javascript
console.log(collatz(5, 5));
```
will ouput `[ 5, 16, 8, 4, 2 ]`.
## Digital Roots
The `digitalRoots` function gets the digital roots from a positive integer, that
is, the sum of all its digits.
```javascript
console.log(digitalRoots(19));
```
will ouput `1`.
## Converting to Other Bases
The `toBase` function gets the string representation of a given **positive integer**
in a certain base.
```javascript
console.log(toBase(255, 16), toBase(255, 2));
```
will ouput `FF 11111111`.
The inverse process is achieved with the `fromBase` function.
```javascript
console.log(fromBase('FF', 16), fromBase('11111111', 2));
```
outputs `255 255`.
- [Bases](docs/bases.md)
- [Combinations](docs/combinations.md)
- [Complex Numbers](docs/complex.md)
- [Number](docs/number.md)
- [Vectors](docs/vector.md)

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc