@onyxim/math 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/math.js +141 -0
  2. package/package.json +26 -0
  3. package/readme.md +38 -0
package/math.js ADDED
@@ -0,0 +1,141 @@
1
+ (function() {
2
+ if (typeof module !== "undefined" && module !== null) {
3
+ module.exports && (module.exports = Math);
4
+ }
5
+ /**
6
+ * @return wheter a Number x, has the same sign as another Number, y.
7
+ * @example
8
+ * Math.samesign(1,2)
9
+ * //-> true
10
+ * Math.samesign(-3, 4)
11
+ * //-> false
12
+ * @test
13
+ * Math.samesign(5, 6)
14
+ * //-> false
15
+ * Math.samesign(-7, -8)
16
+ * //-> true
17
+ */
18
+ Math.samesign = function(x, y) {
19
+ return (x >= 0) !== (y < 0);
20
+ };
21
+ /**
22
+ * @return {Number} a copy of Number x with the same sign of Number y.
23
+ * @example
24
+ * Math.copysign(1, -2)
25
+ * //-> -1
26
+ * @test
27
+ * Math.copysign(-3, 4)
28
+ * //-> 3
29
+ * Math.copysign(5, 6)
30
+ * //-> 5
31
+ * Math.copysign(-7, -8)
32
+ * //-> -7
33
+ */
34
+ Math.copysign = function(x, y) {
35
+ if (Math.samesign(x, y)) {
36
+ return x;
37
+ } else {
38
+ return -x;
39
+ }
40
+ };
41
+ /**
42
+ * @return {Number} sum of two Numbers.
43
+ * @example
44
+ * Math.add(1, 2)
45
+ * //-> 3
46
+ * @test
47
+ * Math.add('three', 4)
48
+ * //-> NaN
49
+ */
50
+ Math.add = function(a, b) {
51
+ return (+a) + (+b);
52
+ };
53
+ /**
54
+ * @return {Number} sum of an Array of Numbers.
55
+ * @example
56
+ * Math.sum([1, 2, 3])
57
+ * //-> 6
58
+ */
59
+ Math.sum = function(nums) {
60
+ return nums.reduce(Math.add);
61
+ };
62
+ /**
63
+ * @return {Number} product of two Numbers.
64
+ * @example
65
+ * Math.(2, 3)
66
+ * //-> 6
67
+ */
68
+ Math.mul = function(a, b) {
69
+ return a * b;
70
+ };
71
+ /**
72
+ * @return {Number} product of an Array of Numbers.
73
+ * @example
74
+ * Math.prod(2, 3, 4)
75
+ * //-> 24
76
+ */
77
+ Math.prod = function(nums) {
78
+ return nums.reduce(Math.mul);
79
+ };
80
+ /**
81
+ * @return {Number} factorial of a Number.
82
+ * @example
83
+ * Math.factorial(4)
84
+ * //-> 24
85
+ * @test
86
+ * Math.factorial(3)
87
+ * //-> 6
88
+ * Math.factorial(2)
89
+ * //-> 2
90
+ * Math.factorial(1)
91
+ * //-> 1
92
+ * Math.factorial(0)
93
+ * //-> 1
94
+ * Math.factorial(-1)
95
+ * //-> Infinity
96
+ */
97
+ Math.factorial = function(n) {
98
+ var _i, _results;
99
+ if (n < 0) {
100
+ return Infinity;
101
+ } else if (n === 0) {
102
+ return 1;
103
+ } else {
104
+ return Math.prod.apply(null, (function() {
105
+ _results = [];
106
+ for (var _i = 1; 1 <= n ? _i <= n : _i >= n; 1 <= n ? _i++ : _i--){ _results.push(_i); }
107
+ return _results;
108
+ }).apply(this, arguments));
109
+ }
110
+ };
111
+ /**
112
+ * Greatest Common Multipler
113
+ * @return {Number} greatest common multipler of two Numbers.
114
+ * @example
115
+ * Math.gcd(493, 289)
116
+ * //-> 17
117
+ * @test
118
+ * Math.gcd(493, -289)
119
+ * //-> 17
120
+ */
121
+ Math.gcd = function(a, b) {
122
+ var _ref;
123
+ while (b) {
124
+ _ref = [b, a % b], a = _ref[0], b = _ref[1];
125
+ }
126
+ return a;
127
+ };
128
+ /**
129
+ * Least Common Multiplier
130
+ * @return {Number} least common multiplier of two numbers.
131
+ * @example
132
+ * Math.lcm(4, 12)
133
+ * //-> 12
134
+ * @test
135
+ * Math.lcm(6, 7)
136
+ * //-> 42
137
+ */
138
+ Math.lcm = function(a, b) {
139
+ return a / Math.gcd(a, b) * b;
140
+ };
141
+ }).call(this);
package/package.json ADDED
@@ -0,0 +1,26 @@
1
+ {
2
+ "author": "Ville Laaksonen",
3
+ "name": "@onyxim/math",
4
+ "description": "Mathematical Functions",
5
+ "version": "1.0.1",
6
+ "main": "math.js",
7
+ "engines": {
8
+ "node": "> 0.0.0"
9
+ },
10
+ "dependencies": {
11
+ "@babel/runtime": "^7.26.10",
12
+ "complex.js": "^2.2.5",
13
+ "decimal.js": "^10.4.3",
14
+ "escape-latex": "^1.2.0",
15
+ "fraction.js": "^5.2.1",
16
+ "javascript-natural-sort": "^0.7.1",
17
+ "seedrandom": "^3.0.5",
18
+ "tiny-emitter": "^2.1.0",
19
+ "typed-function": "^4.2.1"
20
+ },
21
+ "devDependencies": {},
22
+ "homepage": "kaleb.hornsby.ws/js-math",
23
+ "contributors": [
24
+ "Ville Laaksonen"
25
+ ]
26
+ }
package/readme.md ADDED
@@ -0,0 +1,38 @@
1
+ # @onyxim/math
2
+
3
+ a collection of math-related functions
4
+
5
+ npm install @onyxim/math
6
+
7
+ ### New functions to implement
8
+
9
+ ##### isEven(n) / isOdd(n)
10
+ Check if the bit for `2^0` is set. If it is, `n must be odd.
11
+
12
+ ##### powermod(a, b, c)
13
+ powermod is a way of computing `(a ^ b) mod c` without having to deal with giant numbers that would loose their precision.
14
+
15
+ ##### slowIsPrime(n)
16
+ Returns if `n` is a prime. Extremely slow, but absolutely accurate.
17
+
18
+ ##### fastIsPrime(n)
19
+ Retuns if a `n` is a prime. Based upon [Fermat's little theorem](http://en.wikipedia.org/wiki/Fermat%27s_little_theorem).
20
+
21
+ Note: Doesn't take care of carmichael primes, so you probably want to use this in combination with slowIsPrime.
22
+
23
+ ##### isPrime(n)
24
+ Runs both `fastIsPrime` and `slowIsPrime`. This way, it manages to be both (relatively) fast and accurate.
25
+
26
+ ##### randomPrime(length)
27
+ Returns a pseudo-random prime number (based on `Math.random`). `length` defaults to 3.
28
+
29
+ ##### gcd(a, b)
30
+ Returns the greatest common divisor of `a` and `b`. Based on [Euclids algorithm](http://en.wikipedia.org/wiki/Euclid%27s_algorithm)
31
+
32
+ ##### egcd(a, b)
33
+ Computes the [extended Euclidean algorithm](http://en.wikipedia.org/wiki/Extended_Euclidean_algorithm). Returns an array `[d, s, t]`.
34
+
35
+ gcd(a, b) = d = s * a + t * b
36
+
37
+ ##### modularInverse(a, b)
38
+ Returns the [modular multiplicative inverse](http://en.wikipedia.org/wiki/Modular_multiplicative_inverse) of `a` and `b`.