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

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);
@@ -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) {
@@ -731,7 +968,104 @@ var PowiainaNum = /*#__PURE__*/function () {
731
968
  }
732
969
  return res;
733
970
  }
971
+ }, {
972
+ key: "arr01",
973
+ get:
974
+ /**
975
+ * A property arary value for version 0.1.x PowiainaNum.
976
+ */
977
+ function get() {
978
+ var res = [0];
979
+ for (var i = 0; i < this.array.length; i++) {
980
+ if (i == 0) res[0] = this.array[i].repeat;else {
981
+ // @ts-ignore
982
+ res[i] = [0, 0, 0, 0];
983
+ // @ts-ignore
984
+ res[i][0] = this.array[i].arrow == Infinity ? "x" : this.array[i].arrow;
985
+ // @ts-ignore
986
+ res[i][1] = this.array[i].repeat;
987
+ // @ts-ignore
988
+ res[i][2] = this.array[i].expans == Infinity ? "x" : this.array[i].expans;
989
+ // @ts-ignore
990
+ res[i][3] = this.array[i].megota;
991
+ }
992
+ }
993
+ return res;
994
+ }
734
995
  }], [{
996
+ key: "add",
997
+ value: function add(t, other) {
998
+ return new PowiainaNum(t).add(other);
999
+ }
1000
+ }, {
1001
+ key: "sub",
1002
+ value: function sub(t, other) {
1003
+ return new PowiainaNum(t).sub(other);
1004
+ }
1005
+ }, {
1006
+ key: "mul",
1007
+ value: function mul(t, other) {
1008
+ return new PowiainaNum(t).mul(other);
1009
+ }
1010
+ }, {
1011
+ key: "div",
1012
+ value: function div(t, other) {
1013
+ return new PowiainaNum(t).div(other);
1014
+ }
1015
+ }, {
1016
+ key: "pow",
1017
+ value: function pow(t, other) {
1018
+ return new PowiainaNum(t).pow(other);
1019
+ }
1020
+ }, {
1021
+ key: "root",
1022
+ value: function root(t, other) {
1023
+ return new PowiainaNum(t).root(other);
1024
+ }
1025
+ }, {
1026
+ key: "sqrt",
1027
+ value: function sqrt(t) {
1028
+ return new PowiainaNum(t).sqrt();
1029
+ }
1030
+ }, {
1031
+ key: "cbrt",
1032
+ value: function cbrt(t) {
1033
+ return new PowiainaNum(t).cbrt();
1034
+ }
1035
+ }, {
1036
+ key: "log10",
1037
+ value: function log10(t) {
1038
+ return new PowiainaNum(t).log10();
1039
+ }
1040
+ }, {
1041
+ key: "log",
1042
+ value: function log(t) {
1043
+ var base = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : Math.E;
1044
+ return new PowiainaNum(t).log(base);
1045
+ }
1046
+ }, {
1047
+ key: "exp",
1048
+ value: function exp(x) {
1049
+ var y = new PowiainaNum(x);
1050
+ return y.pow_base(Math.E);
1051
+ }
1052
+ }, {
1053
+ key: "factorial",
1054
+ value: function factorial(x) {
1055
+ return new PowiainaNum(x).factorial();
1056
+ }
1057
+ }, {
1058
+ key: "gamma",
1059
+ value: function gamma(x) {
1060
+ return new PowiainaNum(x).gamma();
1061
+ }
1062
+ }, {
1063
+ key: "lambertw",
1064
+ value: function lambertw(x) {
1065
+ var principal = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
1066
+ return new PowiainaNum(x).lambertw(principal);
1067
+ }
1068
+ }, {
735
1069
  key: "fromNumber",
736
1070
  value: function fromNumber(x) {
737
1071
  var obj = new PowiainaNum(); // NaN
@@ -755,6 +1089,9 @@ var PowiainaNum = /*#__PURE__*/function () {
755
1089
  key: "fromString",
756
1090
  value: function fromString(input) {
757
1091
  var _a, _b, _c, _d, _e, _f;
1092
+ if (!isPowiainaNum.test(input)) {
1093
+ throw powiainaNumError + "malformed input: " + input;
1094
+ }
758
1095
  var x = new PowiainaNum();
759
1096
  var negateIt = false;
760
1097
  var recipIt = false;
@@ -976,6 +1313,13 @@ PowiainaNum.E_MSI_REC = new PowiainaNum({
976
1313
  layer: 0,
977
1314
  sign: 1
978
1315
  });
1316
+ PowiainaNum.TRITRI = new PowiainaNum({
1317
+ small: false,
1318
+ layer: 0,
1319
+ sign: 1,
1320
+ array: [newOperator(3638334640023.7783, 0, 1, 1), newOperator(7625587484984, 1, 1, 1)]
1321
+ });
1322
+ PowiainaNum.GRAHAMS_NUMBER = new PowiainaNum("(10{!})^63 10^^^(10^)^7625597484984 3638334640023.7783");
979
1323
  PowiainaNum.POSITIVE_INFINITY = new PowiainaNum(Infinity);
980
1324
  PowiainaNum.NEGATIVE_INFINITY = new PowiainaNum(-Infinity);
981
1325
  PowiainaNum.NaN = new PowiainaNum({