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