powiaina_num.js 0.2.0-alpha.2.2 → 0.2.0-alpha.2.4

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.
@@ -36,10 +36,12 @@ function _typeof(o) {
36
36
  }, _typeof(o);
37
37
  }
38
38
 
39
+ var powiainaNumError = "[PowiainaNum 0.2 error]";
39
40
  var MSI = 9007199254740991;
40
41
  var MSI_LOG10 = 15.954589770191003;
41
42
  var MSI_REC = 1.1102230246251568e-16;
42
43
  var LONG_STRING_MIN_LENGTH = 17;
44
+ var isPowiainaNum = /^[-\+]*(Infinity|NaN|(10(\^+|\{([1-9]\d*|!)(,([1-9]\d*|!))?(,[1-9]\d*)?\})|\(10(\^+|\{([1-9]\d*|!)(,([1-9]\d*|!))?(,[1-9]\d*)?\})\)\^[1-9]\d* )*((\d+(\.\d*)?|\d*\.\d+)?([Ee][-\+]*))*(0|\d+(\.\d*)?|\d*\.\d+))$/;
43
45
  function newOperator(r) {
44
46
  var a = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
45
47
  var e = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 1;
@@ -67,6 +69,125 @@ function compareTuples() {
67
69
  function log10LongString(str) {
68
70
  return Math.log10(Number(str.substring(0, LONG_STRING_MIN_LENGTH))) + (str.length - LONG_STRING_MIN_LENGTH);
69
71
  }
72
+ // Code from break_eternity.js
73
+ function f_gamma(n) {
74
+ if (!isFinite(n)) {
75
+ return n;
76
+ }
77
+ if (n < -50) {
78
+ if (n === Math.trunc(n)) {
79
+ return Number.NEGATIVE_INFINITY;
80
+ }
81
+ return 0;
82
+ }
83
+ var scal1 = 1;
84
+ while (n < 10) {
85
+ scal1 = scal1 * n;
86
+ ++n;
87
+ }
88
+ n -= 1;
89
+ var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI)
90
+ l = l + (n + 0.5) * Math.log(n);
91
+ l = l - n;
92
+ var n2 = n * n;
93
+ var np = n;
94
+ l = l + 1 / (12 * np);
95
+ np = np * n2;
96
+ l = l - 1 / (360 * np);
97
+ np = np * n2;
98
+ l = l + 1 / (1260 * np);
99
+ np = np * n2;
100
+ l = l - 1 / (1680 * np);
101
+ np = np * n2;
102
+ l = l + 1 / (1188 * np);
103
+ np = np * n2;
104
+ l = l - 691 / (360360 * np);
105
+ np = np * n2;
106
+ l = l + 7 / (1092 * np);
107
+ np = np * n2;
108
+ l = l - 3617 / (122400 * np);
109
+ return Math.exp(l) / scal1;
110
+ }
111
+ var OMEGA = 0.56714329040978387299997; // W(1, 0)
112
+ //from https://math.stackexchange.com/a/465183
113
+ // The evaluation can become inaccurate very close to the branch point
114
+ // Evaluates W(x, 0) if principal is true, W(x, -1) if principal is false
115
+ function f_lambertw(z) {
116
+ var tol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1e-10;
117
+ var principal = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
118
+ var w;
119
+ var wn;
120
+ if (!Number.isFinite(z)) {
121
+ return z;
122
+ }
123
+ if (principal) {
124
+ if (z === 0) {
125
+ return z;
126
+ }
127
+ if (z === 1) {
128
+ return OMEGA;
129
+ }
130
+ if (z < 10) {
131
+ w = 0;
132
+ } else {
133
+ w = Math.log(z) - Math.log(Math.log(z));
134
+ }
135
+ } else {
136
+ if (z === 0) return -Infinity;
137
+ if (z <= -0.1) {
138
+ w = -2;
139
+ } else {
140
+ w = Math.log(-z) - Math.log(-Math.log(-z));
141
+ }
142
+ }
143
+ for (var i = 0; i < 100; ++i) {
144
+ wn = (z * Math.exp(-w) + w * w) / (w + 1);
145
+ if (Math.abs(wn - w) < tol * Math.abs(wn)) {
146
+ return wn;
147
+ } else {
148
+ w = wn;
149
+ }
150
+ }
151
+ throw Error("Iteration failed to converge: ".concat(z.toString())); //return Number.NaN;
152
+ }
153
+ //from https://github.com/scipy/scipy/blob/8dba340293fe20e62e173bdf2c10ae208286692f/scipy/special/lambertw.pxd
154
+ // The evaluation can become inaccurate very close to the branch point
155
+ // at ``-1/e``. In some corner cases, `lambertw` might currently
156
+ // fail to converge, or can end up on the wrong branch.
157
+ // Evaluates W(x, 0) if principal is true, W(x, -1) if principal is false
158
+ function d_lambertw(z) {
159
+ var tol = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 1e-10;
160
+ var principal = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
161
+ var w;
162
+ var ew, wewz, wn;
163
+ if (z.isInfinite()) return z;
164
+ if (principal) {
165
+ if (z.eq(PowiainaNum.ZERO)) {
166
+ return PowiainaNum.ZERO.clone();
167
+ }
168
+ if (z.eq(PowiainaNum.ONE)) {
169
+ //Split out this case because the asymptotic series blows up
170
+ return PowiainaNum.fromNumber(OMEGA);
171
+ } //Get an initial guess for Halley's method
172
+ w = z.log();
173
+ } else {
174
+ if (z.eq(PowiainaNum.ZERO)) {
175
+ return PowiainaNum.NEGATIVE_INFINITY.clone();
176
+ } //Get an initial guess for Halley's method
177
+ w = z.neg().log();
178
+ } //Halley's method; see 5.9 in [1]
179
+ for (var i = 0; i < 100; ++i) {
180
+ ew = w.neg().exp();
181
+ wewz = w.sub(z.mul(ew));
182
+ wn = w.sub(wewz.div(w.add(1).sub(w.add(2).mul(wewz).div(w.mul(2).add(2)))));
183
+ if (wn.sub(w).abs().lt(wn.abs().mul(tol))) {
184
+ return wn;
185
+ } else {
186
+ w = wn;
187
+ }
188
+ }
189
+ throw Error("Iteration failed to converge: ".concat(z.toString())); //return Decimal.dNaN;
190
+ }
70
191
  var PowiainaNum = /*#__PURE__*/function () {
71
192
  function PowiainaNum(arg1) {
72
193
  _classCallCheck(this, PowiainaNum);
@@ -97,6 +218,7 @@ var PowiainaNum = /*#__PURE__*/function () {
97
218
  return _createClass(PowiainaNum, [{
98
219
  key: "add",
99
220
  value: function add(other) {
221
+ var _a, _b, _c, _d;
100
222
  var x = this.clone();
101
223
  var y = new PowiainaNum(other);
102
224
  // inf + -inf = nan
@@ -130,11 +252,11 @@ var PowiainaNum = /*#__PURE__*/function () {
130
252
  a = y;
131
253
  }
132
254
  var mult = 1;
133
- if (!a.small && !b.small && !a.getOperator(1) && !b.getOperator(1) && a.sign == b.sign) {
134
- return new PowiainaNum((a.getOperator(0) + b.getOperator(0)) * a.sign);
255
+ if (!a.small && !b.small && !((_a = a.array[1]) === null || _a === void 0 ? void 0 : _a.repeat) && !((_b = b.array[1]) === null || _b === void 0 ? void 0 : _b.repeat) && a.sign == b.sign) {
256
+ return new PowiainaNum((a.array[0].repeat + b.array[0].repeat) * a.sign);
135
257
  }
136
- var alog10 = (a.small ? -1 : 1) * (a.getOperator(1) ? a.getOperator(0) : Math.log10(a.getOperator(0)));
137
- var blog10 = (b.small ? -1 : 1) * (b.getOperator(1) ? b.getOperator(0) : Math.log10(b.getOperator(0)));
258
+ var alog10 = (a.small ? -1 : 1) * (((_c = a.array[1]) === null || _c === void 0 ? void 0 : _c.repeat) ? a.array[0].repeat : Math.log10(a.array[0].repeat));
259
+ var blog10 = (b.small ? -1 : 1) * (((_d = b.array[1]) === null || _d === void 0 ? void 0 : _d.repeat) ? b.array[0].repeat : Math.log10(b.array[0].repeat));
138
260
  if (alog10 - blog10 > MSI_LOG10) return a;
139
261
  var offset = -Math.floor(alog10); //a number can make a+off in [0,1)
140
262
  var r,
@@ -150,10 +272,9 @@ var PowiainaNum = /*#__PURE__*/function () {
150
272
  r = new PowiainaNum();
151
273
  r.sign = 1;
152
274
  if (l > MSI_LOG10 || l < -MSI_LOG10) {
153
- r.setOperator(1, 1);
154
- r.setOperator(Math.log10(Math.abs(l)), 1);
275
+ r.array = [newOperator(l, 0), newOperator(1, 1)];
155
276
  } else {
156
- r.setOperator(Math.pow(10, Math.abs(l)), 0);
277
+ r.array = [newOperator(Math.pow(10, l), 0)];
157
278
  }
158
279
  r.small = l < 0 ? true : false;
159
280
  r.sign *= mult;
@@ -284,11 +405,17 @@ var PowiainaNum = /*#__PURE__*/function () {
284
405
  }
285
406
  }, {
286
407
  key: "log",
287
- value: function log(base) {
408
+ value: function log() {
409
+ var base = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : Math.E;
288
410
  // log_a b = log_x b / log_x a;
289
411
  var other = new PowiainaNum(base);
290
412
  return this.log10().div(other.log10());
291
413
  }
414
+ }, {
415
+ key: "exp",
416
+ value: function exp() {
417
+ return this.pow_base(Math.E);
418
+ }
292
419
  }, {
293
420
  key: "mod",
294
421
  value: function mod(x) {
@@ -296,6 +423,116 @@ var PowiainaNum = /*#__PURE__*/function () {
296
423
  var division = this.div(other);
297
424
  return division.sub(division.floor()).mul(other);
298
425
  }
426
+ /**
427
+ * For positive integers, X factorial (written as X!) equals X * (X - 1) * (X - 2) *... * 3 * 2 * 1. 0! equals 1.
428
+ * This can be extended to real numbers (except for negative integers) via the gamma function, which is what this function does.
429
+ */
430
+ //[Code from break_eternity.js]
431
+ }, {
432
+ key: "factorial",
433
+ value: function factorial() {
434
+ if (this.abs().lt(MSI)) {
435
+ return this.add(1).gamma();
436
+ } else if (this.abs().lt(PowiainaNum.E_MSI)) {
437
+ return PowiainaNum.exp(this.mul(this.log10().sub(1)));
438
+ } else {
439
+ return PowiainaNum.exp(this);
440
+ }
441
+ }
442
+ }, {
443
+ key: "gamma",
444
+ value:
445
+ /**
446
+ * The gamma function extends the idea of factorials to non-whole numbers using some calculus.
447
+ * Gamma(x) is defined as the integral of t^(x-1) * e^-t dt from t = 0 to t = infinity,
448
+ * and gamma(x) = (x - 1)! for nonnegative integer x, so the factorial for non-whole numbers is defined using the gamma function.
449
+ */
450
+ //[Code from break_eternity.js]
451
+ //from HyperCalc source code
452
+ function gamma() {
453
+ if (this.small) {
454
+ return this.rec();
455
+ } else if (this.lte(MSI)) {
456
+ if (this.lt(24)) {
457
+ return PowiainaNum.fromNumber(f_gamma(this.sign * this.getOperator(0)));
458
+ }
459
+ var t = this.getOperator(0) - 1;
460
+ var l = 0.9189385332046727; //0.5*Math.log(2*Math.PI)
461
+ l = l + (t + 0.5) * Math.log(t);
462
+ l = l - t;
463
+ var n2 = t * t;
464
+ var np = t;
465
+ var lm = 12 * np;
466
+ var adj = 1 / lm;
467
+ var l2 = l + adj;
468
+ if (l2 === l) {
469
+ return PowiainaNum.exp(l);
470
+ }
471
+ l = l2;
472
+ np = np * n2;
473
+ lm = 360 * np;
474
+ adj = 1 / lm;
475
+ l2 = l - adj;
476
+ if (l2 === l) {
477
+ return PowiainaNum.exp(l);
478
+ }
479
+ l = l2;
480
+ np = np * n2;
481
+ lm = 1260 * np;
482
+ var lt = 1 / lm;
483
+ l = l + lt;
484
+ np = np * n2;
485
+ lm = 1680 * np;
486
+ lt = 1 / lm;
487
+ l = l - lt;
488
+ return PowiainaNum.exp(l);
489
+ } else if (this.gt(MSI)) {
490
+ return PowiainaNum.exp(this.mul(this.log().sub(1)));
491
+ } else {
492
+ return PowiainaNum.exp(this);
493
+ }
494
+ }
495
+ }, {
496
+ key: "lambertw",
497
+ value:
498
+ /**
499
+ * The Lambert W function, also called the omega function or product logarithm, is the solution W(x) === x*e^x.
500
+ * https://en.wikipedia.org/wiki/Lambert_W_function
501
+ *
502
+ * This is a multi-valued function in the complex plane, but only two branches matter for real numbers: the "principal branch" W0, and the "non-principal branch" W_-1.
503
+ * W_0 works for any number >= -1/e, but W_-1 only works for nonpositive numbers >= -1/e.
504
+ * The "principal" parameter, which is true by default, decides which branch we're looking for: W_0 is used if principal is true, W_-1 is used if principal is false.
505
+ */
506
+ //Code from break_eternity.js
507
+ //Some special values, for testing: https://en.wikipedia.org/wiki/Lambert_W_function#Special_values
508
+ function lambertw() {
509
+ var principal = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
510
+ if (this.lt(-0.3678794411710499)) {
511
+ return PowiainaNum.NaN.clone(); //complex
512
+ } else if (principal) {
513
+ if (this.abs().lt("1e-300")) return new PowiainaNum(this);else if (this.small) {
514
+ return PowiainaNum.fromNumber(f_lambertw(this.toNumber()));
515
+ } else if (this.layer === 0) {
516
+ return PowiainaNum.fromNumber(f_lambertw(this.sign * this.getOperator(0)));
517
+ } else if (this.lt("eee15")) {
518
+ return d_lambertw(this);
519
+ } else {
520
+ // Numbers this large would sometimes fail to converge using d_lambertw, and at this size this.ln() is close enough
521
+ return this.log();
522
+ }
523
+ } else {
524
+ if (this.sign === 1) {
525
+ return PowiainaNum.NaN.clone(); //complex
526
+ }
527
+ if (this.layer === 0) {
528
+ return PowiainaNum.fromNumber(f_lambertw(this.sign * this.getOperator(0), 1e-10, false));
529
+ } else if (this.layer == 1) {
530
+ return d_lambertw(this, 1e-10, false);
531
+ } else {
532
+ return this.neg().rec().lambertw().neg();
533
+ }
534
+ }
535
+ }
299
536
  }, {
300
537
  key: "max",
301
538
  value: function max(x) {
@@ -492,13 +729,14 @@ var PowiainaNum = /*#__PURE__*/function () {
492
729
  if (this.abs().gte(Math.pow(2, 52))) return true;
493
730
  return false;
494
731
  }
732
+ }, {
733
+ key: "normalize",
734
+ value:
495
735
  /**
496
736
  * Normalize functions will make this number convert into standard format.(it also change `this`, like [].sort)
497
737
  * @returns normalized number
498
738
  */
499
- }, {
500
- key: "normalize",
501
- value: function normalize() {
739
+ function normalize() {
502
740
  //TODO: normalize
503
741
  var renormalize = true;
504
742
  var x = this;
@@ -729,7 +967,109 @@ var PowiainaNum = /*#__PURE__*/function () {
729
967
  }
730
968
  return res;
731
969
  }
970
+ }, {
971
+ key: "arr01",
972
+ get:
973
+ /**
974
+ * A property arary value for version 0.1.x PowiainaNum.
975
+ */
976
+ function get() {
977
+ var res = [0];
978
+ for (var i = 0; i < this.array.length; i++) {
979
+ if (i == 0) res[0] = this.array[i].repeat;else {
980
+ // @ts-ignore
981
+ res[i] = [0, 0, 0, 0];
982
+ // @ts-ignore
983
+ res[i][0] = this.array[i].arrow == Infinity ? "x" : this.array[i].arrow;
984
+ // @ts-ignore
985
+ res[i][1] = this.array[i].repeat;
986
+ // @ts-ignore
987
+ res[i][2] = this.array[i].expans == Infinity ? "x" : this.array[i].expans;
988
+ // @ts-ignore
989
+ res[i][3] = this.array[i].megota;
990
+ }
991
+ }
992
+ return res;
993
+ }
732
994
  }], [{
995
+ key: "add",
996
+ value: function add(t, other) {
997
+ return new PowiainaNum(t).add(other);
998
+ }
999
+ }, {
1000
+ key: "sub",
1001
+ value: function sub(t, other) {
1002
+ return new PowiainaNum(t).sub(other);
1003
+ }
1004
+ }, {
1005
+ key: "mul",
1006
+ value: function mul(t, other) {
1007
+ return new PowiainaNum(t).mul(other);
1008
+ }
1009
+ }, {
1010
+ key: "div",
1011
+ value: function div(t, other) {
1012
+ return new PowiainaNum(t).div(other);
1013
+ }
1014
+ }, {
1015
+ key: "pow",
1016
+ value: function pow(t, other) {
1017
+ return new PowiainaNum(t).pow(other);
1018
+ }
1019
+ }, {
1020
+ key: "root",
1021
+ value: function root(t, other) {
1022
+ return new PowiainaNum(t).root(other);
1023
+ }
1024
+ }, {
1025
+ key: "sqrt",
1026
+ value: function sqrt(t) {
1027
+ return new PowiainaNum(t).sqrt();
1028
+ }
1029
+ }, {
1030
+ key: "cbrt",
1031
+ value: function cbrt(t) {
1032
+ return new PowiainaNum(t).cbrt();
1033
+ }
1034
+ }, {
1035
+ key: "log10",
1036
+ value: function log10(t) {
1037
+ return new PowiainaNum(t).log10();
1038
+ }
1039
+ }, {
1040
+ key: "log",
1041
+ value: function log(t) {
1042
+ var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Math.E;
1043
+ return new PowiainaNum(t).log(base);
1044
+ }
1045
+ }, {
1046
+ key: "exp",
1047
+ value: function exp(x) {
1048
+ var y = new PowiainaNum(x);
1049
+ return y.pow_base(Math.E);
1050
+ }
1051
+ }, {
1052
+ key: "factorial",
1053
+ value: function factorial(x) {
1054
+ return new PowiainaNum(x).factorial();
1055
+ }
1056
+ }, {
1057
+ key: "gamma",
1058
+ value: function gamma(x) {
1059
+ return new PowiainaNum(x).gamma();
1060
+ }
1061
+ }, {
1062
+ key: "lambertw",
1063
+ value: function lambertw(x) {
1064
+ var principal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
1065
+ return new PowiainaNum(x).lambertw(principal);
1066
+ }
1067
+ }, {
1068
+ key: "isNaN",
1069
+ value: function isNaN(x) {
1070
+ return new PowiainaNum(x).isNaN();
1071
+ }
1072
+ }, {
733
1073
  key: "fromNumber",
734
1074
  value: function fromNumber(x) {
735
1075
  var obj = new PowiainaNum(); // NaN
@@ -738,22 +1078,35 @@ var PowiainaNum = /*#__PURE__*/function () {
738
1078
  var y = Math.abs(x);
739
1079
  if (y >= MSI_REC && y < 1) {
740
1080
  obj.small = true;
741
- obj.setOperator(1 / y, 0);
1081
+ obj.array = [newOperator(1 / y, 0)];
742
1082
  } else if (y < MSI_REC) {
743
1083
  obj.small = true;
744
- obj.setOperator(-Math.log10(y), 0);
745
- obj.setOperator(1, 1);
1084
+ obj.array = [newOperator(-Math.log10(y), 0), newOperator(1, 1)];
1085
+ } else if (y <= MSI) {
1086
+ obj.array = [newOperator(y, 0)];
746
1087
  } else {
747
- obj.setOperator(y, 0);
1088
+ obj.setOperator(Math.log10(y), 0);
1089
+ obj.array = [newOperator(Math.log10(y), 0), newOperator(1, 1)];
748
1090
  }
749
- obj.normalize();
750
1091
  return obj;
751
1092
  }
752
1093
  }, {
753
1094
  key: "fromString",
754
1095
  value: function fromString(input) {
755
1096
  var _a, _b, _c, _d, _e, _f;
1097
+ if (!isPowiainaNum.test(input)) {
1098
+ throw powiainaNumError + "malformed input: " + input;
1099
+ }
756
1100
  var x = new PowiainaNum();
1101
+ // Judge the string was a number
1102
+ // @ts-ignore
1103
+ if (!isNaN(Number(input))) {
1104
+ // @ts-ignore
1105
+ if (isFinite(Number(input))) {
1106
+ x.resetFromObject(PowiainaNum.fromNumber(Number(input)));
1107
+ return x;
1108
+ }
1109
+ }
757
1110
  var negateIt = false;
758
1111
  var recipIt = false;
759
1112
  if (input[0] == "-" || input[0] == "+") {
@@ -974,6 +1327,13 @@ PowiainaNum.E_MSI_REC = new PowiainaNum({
974
1327
  layer: 0,
975
1328
  sign: 1
976
1329
  });
1330
+ PowiainaNum.TRITRI = new PowiainaNum({
1331
+ small: false,
1332
+ layer: 0,
1333
+ sign: 1,
1334
+ array: [newOperator(3638334640023.7783, 0, 1, 1), newOperator(7625587484984, 1, 1, 1)]
1335
+ });
1336
+ PowiainaNum.GRAHAMS_NUMBER = new PowiainaNum("(10{!})^63 10^^^(10^)^7625597484984 3638334640023.7783");
977
1337
  PowiainaNum.POSITIVE_INFINITY = new PowiainaNum(Infinity);
978
1338
  PowiainaNum.NEGATIVE_INFINITY = new PowiainaNum(-Infinity);
979
1339
  PowiainaNum.NaN = new PowiainaNum({