@rgsoft/math
Advanced tools
Comparing version 1.0.2 to 1.0.3
declare class Complex { | ||
private _a; | ||
private _b; | ||
readonly a: number; | ||
readonly b: number; | ||
private _mag?; | ||
constructor(_a: number, _b: number); | ||
constructor(a: number, b: number); | ||
/** | ||
@@ -27,2 +27,4 @@ * | ||
sqrt(): Complex; | ||
conjugate(): Complex; | ||
toString(): string; | ||
/** | ||
@@ -32,10 +34,2 @@ * @returns { number } | ||
get mag(): number; | ||
/** | ||
* @returns { number } | ||
*/ | ||
get b(): number; | ||
/** | ||
* @returns { number } | ||
*/ | ||
get a(): number; | ||
} | ||
@@ -133,2 +127,47 @@ | ||
export { Complex, Vector }; | ||
declare class Line { | ||
readonly m: number; | ||
readonly a: number; | ||
constructor(m: number, a: number); | ||
static fromPoints(p: Vector, q: Vector): Line; | ||
static mediatrix(p: Vector, q: Vector): Line; | ||
/** | ||
* Calculates a line intersection point with another | ||
* @param { Line } line | ||
* @returns { Vector } | ||
*/ | ||
intersectionPoint(line: Line): Vector; | ||
} | ||
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 }; |
@@ -24,2 +24,4 @@ "use strict"; | ||
Complex: () => Complex, | ||
Line: () => Line, | ||
Segment: () => Segment, | ||
Vector: () => Vector | ||
@@ -31,5 +33,5 @@ }); | ||
var Complex = class _Complex { | ||
constructor(_a, _b) { | ||
this._a = _a; | ||
this._b = _b; | ||
constructor(a, b) { | ||
this.a = a; | ||
this.b = b; | ||
return this; | ||
@@ -42,8 +44,8 @@ } | ||
add(n) { | ||
let { _a: a, _b: b } = this; | ||
let { a, b } = this; | ||
if (typeof n === "number") { | ||
a += n; | ||
} else if (n instanceof _Complex) { | ||
a += n._a; | ||
b += n._b; | ||
a += n.a; | ||
b += n.b; | ||
} | ||
@@ -57,8 +59,8 @@ return new _Complex(a, b); | ||
sub(n) { | ||
let { _a: a, _b: b } = this; | ||
let { a, b } = this; | ||
if (typeof n === "number") { | ||
a -= n; | ||
} else if (n instanceof _Complex) { | ||
a -= n._a; | ||
b -= n._b; | ||
a -= n.a; | ||
b -= n.b; | ||
} | ||
@@ -72,3 +74,3 @@ return new _Complex(a, b); | ||
mult(n) { | ||
let { _a: a, _b: b } = this; | ||
let { a, b } = this; | ||
if (typeof n === "number") { | ||
@@ -78,4 +80,4 @@ a *= n; | ||
} else if (n instanceof _Complex) { | ||
a = this._a * n._a - this._b * n._b; | ||
b = this._a * n._b + this._b * n._a; | ||
a = this.a * n.a - this.b * n.b; | ||
b = this.a * n.b + this.b * n.a; | ||
} | ||
@@ -89,3 +91,3 @@ return new _Complex(a, b); | ||
div(n) { | ||
let { _a: a, _b: b } = this; | ||
let { a, b } = this; | ||
if (typeof n === "number") { | ||
@@ -95,5 +97,5 @@ a /= n; | ||
} else if (n instanceof _Complex) { | ||
const d = n._a * n._a + n._b * n._b; | ||
a = (this._a * n._a + this._b * n._b) / d; | ||
b = (this._b * n._a - this._a * n._b) / d; | ||
const d = n.a * n.a + n.b * n.b; | ||
a = (this.a * n.a + this.b * n.b) / d; | ||
b = (this.b * n.a - this.a * n.b) / d; | ||
} | ||
@@ -103,5 +105,5 @@ return new _Complex(a, b); | ||
sqrt() { | ||
let { _a: a, _b: b } = this; | ||
let { a, b } = this; | ||
const m = Math.sqrt(this.mag); | ||
const phi = Math.atan2(this._b, this._a) * 0.5; | ||
const phi = Math.atan2(this.b, this.a) * 0.5; | ||
a = m * Math.cos(phi); | ||
@@ -111,2 +113,25 @@ b = m * Math.sin(phi); | ||
} | ||
conjugate() { | ||
return new _Complex(this.a, -1 * this.b); | ||
} | ||
toString() { | ||
let str = ""; | ||
if (this.a !== 0) { | ||
str += `${this.a}`; | ||
} | ||
if (this.b > 0) { | ||
if (this.b === 1) { | ||
str += ` + i`; | ||
} else { | ||
str += ` + ${this.b}i`; | ||
} | ||
} else if (this.b < 0) { | ||
if (this.b === -1) { | ||
str += ` - i`; | ||
} else { | ||
str += ` - ${Math.abs(this.b)}i`; | ||
} | ||
} | ||
return str.trim(); | ||
} | ||
/** | ||
@@ -117,18 +142,6 @@ * @returns { number } | ||
if (this._mag === void 0) { | ||
this._mag = Math.sqrt(this._a * this._a + this._b * this._b); | ||
this._mag = Math.sqrt(this.a * this.a + this.b * this.b); | ||
} | ||
return this._mag; | ||
} | ||
/** | ||
* @returns { number } | ||
*/ | ||
get b() { | ||
return this._b; | ||
} | ||
/** | ||
* @returns { number } | ||
*/ | ||
get a() { | ||
return this._a; | ||
} | ||
}; | ||
@@ -296,7 +309,113 @@ | ||
}; | ||
// src/line.ts | ||
var Line = class _Line { | ||
constructor(m, a) { | ||
this.m = m; | ||
this.a = a; | ||
} | ||
static fromPoints(p, q) { | ||
const m = p.x === q.x ? NaN : (p.y - q.y) / (p.x - q.x); | ||
const a = isNaN(m) ? p.x : -1 * m * p.x + p.y; | ||
return new _Line(m, a); | ||
} | ||
static mediatrix(p, q) { | ||
const a = q.copy(); | ||
a.sub(p); | ||
a.mult(0.5); | ||
const b = Vector.fromAngle(a.angle + Math.PI * 0.5); | ||
a.add(p); | ||
b.add(a); | ||
return _Line.fromPoints(a, b); | ||
} | ||
/** | ||
* Calculates a line intersection point with another | ||
* @param { Line } line | ||
* @returns { Vector } | ||
*/ | ||
intersectionPoint(line) { | ||
let x, y; | ||
if (this.m === line.m) { | ||
throw new Error("The slopes are equal"); | ||
} | ||
if (isNaN(this.m)) { | ||
x = this.a; | ||
y = line.m * x + line.a; | ||
} else if (isNaN(line.m)) { | ||
x = line.a; | ||
y = this.m * x + this.a; | ||
} else { | ||
x = (line.a - this.a) / (this.m - line.m); | ||
y = this.m * x + this.a; | ||
} | ||
return new Vector(x, y); | ||
} | ||
}; | ||
// 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: | ||
0 && (module.exports = { | ||
Complex, | ||
Line, | ||
Segment, | ||
Vector | ||
}); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@rgsoft/math", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Yet another JS math library", | ||
@@ -5,0 +5,0 @@ "main": "./dist/index.js", |
227
README.md
# math-js | ||
Yet another JS math library | ||
## Complex | ||
## Installation | ||
Complex numbers library | ||
```sh | ||
npm install @rgsoft/math | ||
``` | ||
## Tests | ||
```sh | ||
npm run test | ||
``` | ||
## Complex Numbers | ||
The complex number library has some methods for performing calculations on | ||
complex numbers. To create a complex number of the form `a + bi`: | ||
```js | ||
// 4 - 3i | ||
const cpx = new Complex(4, -3); | ||
``` | ||
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(); | ||
``` |
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
62428
944
229
0