dielines
Version:
Create dielines with pantograph APIs
1,442 lines • 238 kB
JavaScript
var ts = Object.defineProperty;
var es = (i, t, e) => t in i ? ts(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e;
var Et = (i, t, e) => es(i, typeof t != "symbol" ? t + "" : t, e);
var ns = Object.defineProperty, pi = (i) => {
throw TypeError(i);
}, is = (i, t, e) => t in i ? ns(i, t, { enumerable: !0, configurable: !0, writable: !0, value: e }) : i[t] = e, F = (i, t, e) => is(i, typeof t != "symbol" ? t + "" : t, e), tn = (i, t, e) => t.has(i) || pi("Cannot " + e), Pt = (i, t, e) => (tn(i, t, "read from private field"), e ? e.call(i) : t.get(i)), wi = (i, t, e) => t.has(i) ? pi("Cannot add the same private member more than once") : t instanceof WeakSet ? t.add(i) : t.set(i, e), qe = (i, t, e, n) => (tn(i, t, "write to private field"), t.set(i, e), e), Pn = (i, t, e) => (tn(i, t, "access private method"), e);
const Mn = Symbol.for("pantograph:BoundingBox");
function fe(i, t, e, n) {
return i <= n && t >= e;
}
let re = class Ht {
constructor(t = 1 / 0, e = 1 / 0, n = -1 / 0, s = -1 / 0) {
F(this, "xMin"), F(this, "yMin"), F(this, "xMax"), F(this, "yMax"), Object.defineProperty(this, Mn, { value: !0 }), this.xMin = t, this.yMin = e, this.xMax = n, this.yMax = s;
}
static isInstance(t) {
return !!t && t[Mn] === !0;
}
get width() {
return this.xMax - this.xMin;
}
get height() {
return this.yMax - this.yMin;
}
get center() {
return [(this.xMin + this.xMax) / 2, (this.yMin + this.yMax) / 2];
}
grow(t) {
return new Ht(
this.xMin - t,
this.yMin - t,
this.xMax + t,
this.yMax + t
);
}
contains(t) {
const [e, n] = t;
return fe(this.xMin, this.xMax, e, e) && fe(this.yMin, this.yMax, n, n);
}
overlaps(t) {
return fe(this.xMin, this.xMax, t.xMin, t.xMax) && fe(this.yMin, this.yMax, t.yMin, t.yMax);
}
addPoint(t) {
const [e, n] = t;
return new Ht(
Math.min(this.xMin, e),
Math.min(this.yMin, n),
Math.max(this.xMax, e),
Math.max(this.yMax, n)
);
}
merge(t) {
return new Ht(
Math.min(this.xMin, t.xMin),
Math.min(this.yMin, t.yMin),
Math.max(this.xMax, t.xMax),
Math.max(this.yMax, t.yMax)
);
}
intersection(t) {
return new Ht(
Math.max(this.xMin, t.xMin),
Math.max(this.yMin, t.yMin),
Math.min(this.xMax, t.xMax),
Math.min(this.yMax, t.yMax)
);
}
};
function en(i) {
return i.reduce((t, e) => t.addPoint(e), new re());
}
const yn = (i, t = 1e-9) => {
let e = i;
return Math.abs(i) < t && (e = 0), e.toFixed(-Math.log10(t));
};
function vt(i, t = 1e-9) {
return Array.from(
new Map(
i.map(([e, n]) => [
`[${yn(e, t)},${yn(n, t)}]`,
[e, n]
])
).values()
);
}
const Tt = Math.PI / 180, nn = 180 / Math.PI, it = (i) => `[${i[0]}, ${i[1]}]`, k = ([i, t], [e, n], s = 1e-9) => Math.abs(i - e) <= s && Math.abs(t - n) <= s, W = ([i, t], [e, n]) => [i + e, t + n], q = ([i, t], [e, n]) => [i - e, t - n], Mt = ([i, t]) => i * i + t * t, ss = ([i, t]) => Math.sqrt(Mt([i, t])), G = ([i, t], e) => [i * e, t * e], $t = ([i, t], [e, n] = [0, 0]) => (i - e) ** 2 + (t - n) ** 2, Z = (i, t = [0, 0]) => Math.sqrt($t(i, t));
function yt([i, t], [e, n]) {
return i * n - t * e;
}
function Ie([i, t], [e, n]) {
return i * e + t * n;
}
function rt([i, t]) {
const e = Z([i, t]);
return [i / e, t / e];
}
function xt(i, t) {
const e = Math.cos(t) * i, n = Math.sin(t) * i;
return [e, n];
}
function rs([i, t]) {
return Math.atan2(t, i);
}
function di(i) {
const t = Z(i), e = rs(i);
return [t, e];
}
function sn(i, t, e = 1e-9) {
const n = yt(i, t), s = Mt(i), r = Mt(t);
return n * n < s * r * e * e;
}
function nt(i) {
return [-i[1], i[0]];
}
function Zt(i) {
return [i[1], -i[0]];
}
function os(i, t, e) {
return k(e, i) || sn(q(e, i), t);
}
const bn = Symbol.for(
"pantograph:TransformationMatrix"
), Lt = (i, t) => {
const [e, n, s, r, o, a, h, l, c] = i, [u, m, w, g, p, M, f, P, b] = t;
return [
e * u + n * g + s * f,
e * m + n * p + s * P,
e * w + n * M + s * b,
r * u + o * g + a * f,
r * m + o * p + a * P,
r * w + o * M + a * b,
h * u + l * g + c * f,
h * m + l * p + c * P,
h * w + l * M + c * b
];
}, as = (i) => {
const [t, e, n, s, r, o, a, h, l] = i, c = t * (r * l - o * h) - e * (s * l - o * a) + n * (s * h - r * a);
return [
(r * l - o * h) / c,
(n * h - e * l) / c,
(e * o - n * r) / c,
(o * a - s * l) / c,
(t * l - n * a) / c,
(n * s - t * o) / c,
(s * h - r * a) / c,
(e * a - t * h) / c,
(t * r - e * s) / c
];
}, hs = (i) => {
const [t, e, n, s, r, o, a, h, l] = i;
return [t, s, a, e, r, h, n, o, l];
};
let ut = class Pi {
constructor(t) {
F(this, "_matrix", [1, 0, 0, 0, 1, 0, 0, 0, 1]), Object.defineProperty(this, bn, {
value: !0
}), t && (this._matrix = [...t]);
}
static isInstance(t) {
return !!t && t[bn] === !0;
}
clone() {
return new Pi(this._matrix);
}
transpose() {
return this._matrix = hs(this._matrix), this;
}
inverse() {
return this._matrix = as(this._matrix), this;
}
translate(t, e) {
return this._matrix = Lt([1, 0, t, 0, 1, e, 0, 0, 1], this._matrix), this;
}
rotate(t, e) {
const n = Math.cos(t), s = Math.sin(t), r = [n, -s, 0, s, n, 0, 0, 0, 1];
return e && this.translate(-e[0], -e[1]), this._matrix = Lt(r, this._matrix), e && this.translate(e[0], e[1]), this;
}
mirrorX() {
return this._matrix = Lt([1, 0, 0, 0, -1, 0, 0, 0, 1], this._matrix), this;
}
mirrorY() {
return this._matrix = Lt([-1, 0, 0, 0, 1, 0, 0, 0, 1], this._matrix), this;
}
mirrorLine(t, e) {
const [n, s] = t, r = Math.atan2(s, n);
return e && this.translate(-e[0], -e[1]), this.rotate(-r), this.mirrorX(), this.rotate(r), e && this.translate(e[0], e[1]), this;
}
mirrorCenter(t) {
return t && this.translate(-t[0], -t[1]), this._matrix = Lt([-1, 0, 0, 0, -1, 0, 0, 0, 1], this._matrix), t && this.translate(t[0], t[1]), this;
}
scale(t, e) {
return e && this.translate(-e[0], -e[1]), this._matrix = Lt([t, 0, 0, 0, t, 0, 0, 0, 1], this._matrix), e && this.translate(e[0], e[1]), this;
}
transform(t) {
const [e, n] = t, [s, r, o, a, h, l] = this._matrix;
return [s * e + r * n + o, a * e + h * n + l];
}
transformAngle(t) {
const [e, n] = this.transform([Math.cos(t), Math.sin(t)]), [s, r] = this.transform([0, 0]);
return Math.atan2(n - r, e - s);
}
keepsOrientation() {
const [t, , , , e] = this._matrix;
return t * e > 0;
}
scaleFactor() {
const [t, , , e] = this._matrix;
return Math.sqrt(t * t + e * e);
}
};
class _t {
translateX(t) {
const e = new ut().translate(t, 0);
return this.transform(e);
}
translateY(t) {
const e = new ut().translate(0, t);
return this.transform(e);
}
translate(t, e) {
const n = new ut().translate(t, e);
return this.transform(n);
}
translateTo([t, e]) {
const n = new ut().translate(t, e);
return this.transform(n);
}
rotate(t, e) {
const n = new ut().rotate(
t * Tt,
e
);
return this.transform(n);
}
scale(t, e) {
const n = new ut().scale(t, e);
return this.transform(n);
}
mirrorCenter(t) {
const e = new ut().mirrorCenter(t);
return this.transform(e);
}
mirror(t = "x", e) {
const n = new ut();
return t === "x" ? n.mirrorX() : t === "y" ? n.mirrorY() : n.mirrorLine(t, e), this.transform(n);
}
}
let oe = class extends _t {
constructor(t, e) {
super(), F(this, "precision", 1e-9), this.firstPoint = t, this.lastPoint = e, this.firstPoint = t, this.lastPoint = e;
}
get repr() {
return `${this.segmentType} ${it(this.firstPoint)} - ${it(
this.lastPoint
)}`;
}
get info() {
return this.repr;
}
[Symbol.for("nodejs.util.inspect.custom")]() {
return this.repr;
}
};
const xn = Symbol.for("pantograph:Line");
let U = class Vt extends oe {
constructor(t, e) {
super(t, e), F(this, "segmentType", "LINE"), F(this, "_V", null), F(this, "_slope", null), F(this, "_yIntercept", null), F(this, "_boundingBox", null), Object.defineProperty(this, xn, { value: !0 });
}
static isInstance(t) {
return !!t && t[xn] === !0;
}
isValidParameter(t) {
const e = this.length * this.precision;
return t >= -e && 1 - t >= -e;
}
paramPoint(t) {
return W(this.firstPoint, G(this.V, t));
}
get length() {
return Z(this.firstPoint, this.lastPoint);
}
get squareLength() {
return $t(this.firstPoint, this.lastPoint);
}
get V() {
return this._V === null && (this._V = q(this.lastPoint, this.firstPoint)), this._V;
}
get slope() {
if (this._slope === null) {
const [t, e] = this.V;
this._slope = e / t;
}
return this._slope;
}
get yIntercept() {
return this._yIntercept === null && (this._yIntercept = this.firstPoint[1] - this.slope * this.firstPoint[0]), this._yIntercept;
}
get midPoint() {
return W(this.firstPoint, G(this.V, 0.5));
}
isSame(t) {
return Vt.isInstance(t) ? k(this.firstPoint, t.firstPoint) && k(this.lastPoint, t.lastPoint) || k(this.lastPoint, t.firstPoint) && k(this.firstPoint, t.lastPoint) : !1;
}
clone() {
return new Vt(this.firstPoint, this.lastPoint);
}
reverse() {
return new Vt(this.lastPoint, this.firstPoint);
}
get boundingBox() {
return this._boundingBox === null && (this._boundingBox = new re(
Math.min(this.firstPoint[0], this.lastPoint[0]) - this.precision,
Math.min(this.firstPoint[1], this.lastPoint[1]) - this.precision,
Math.max(this.firstPoint[0], this.lastPoint[0]) + this.precision,
Math.max(this.firstPoint[1], this.lastPoint[1]) + this.precision
)), this._boundingBox;
}
distanceFrom(t) {
const e = q(t, this.firstPoint), n = Ie(e, this.V) / this.squareLength;
if (n < 0)
return Z(t, this.firstPoint);
if (n > 1)
return Z(t, this.lastPoint);
const s = this.paramPoint(n);
return Z(t, s);
}
isOnSegment(t) {
if (k(t, this.firstPoint, this.precision)) return !0;
const e = q(t, this.firstPoint);
if (!sn(this.V, e)) return !1;
const n = Ie(e, this.V) / this.squareLength;
return this.isValidParameter(n);
}
gradientAt(t) {
return this.V;
}
tangentAt(t) {
if (!this.isOnSegment(t)) throw new Error("Point is not on segment");
return rt(this.V);
}
get normalVector() {
return nt(rt(this.V));
}
get tangentAtFirstPoint() {
return rt(this.V);
}
get tangentAtLastPoint() {
return rt(this.V);
}
splitAt(t) {
let e;
if (Array.isArray(t) && t.length === 0)
return [this];
Array.isArray(t[0]) ? e = t : e = [t], e.forEach((h) => {
if (!this.isOnSegment(h))
throw new Error(
`Point ${it(h)} is not on segment ${this.repr}`
);
});
const n = [this.firstPoint, ...e, this.lastPoint], s = vt(n), r = this.lastPoint[0] - this.firstPoint[0];
let o = Math.sign(r), a = 0;
return Math.abs(r) < this.precision && (o = Math.sign(this.lastPoint[1] - this.firstPoint[1]), a = 1), s.sort(
(h, l) => o * (h[a] - l[a])
), s.flatMap((h, l) => l === s.length - 1 ? [] : new Vt(h, s[l + 1]));
}
transform(t) {
return new Vt(
t.transform(this.firstPoint),
t.transform(this.lastPoint)
);
}
};
function ls(i) {
return Array.from(Array(i).keys());
}
function dt(i) {
const t = Math.min(...i.map((e) => e.length));
return ls(t).map((e) => i.map((n) => n[e]));
}
function Ct(i, t = 1e-9) {
return i < 0 ? i + 2 * Math.PI : i >= 2 * Math.PI ? i % (2 * Math.PI) : i > 2 * Math.PI - t ? 0 : i;
}
function Se(i, t, e, n = 1e-9) {
let s = t - i;
return e && (s = -s), s < 0 && (s += 2 * Math.PI), s > 2 * Math.PI - n ? 0 : s;
}
const $e = (i, t, e) => {
const n = yt(i.V, t.V), s = Mt(i.V), r = Mt(t.V), o = e ? e * e : i.precision * t.precision;
if (n * n < s * r * o)
return "parallel";
const a = q(t.firstPoint, i.firstPoint), h = yt(a, t.V) / n, l = yt(a, i.V) / n;
return {
intersectionParam1: h,
intersectionParam2: l
};
};
function Mi(i, t, e = !1, n) {
const s = $e(i, t, n);
if (s === "parallel") {
if (!e) return null;
if (i.isSame(t)) return i;
const a = vt(
[
t.isOnSegment(i.firstPoint) ? i.firstPoint : null,
t.isOnSegment(i.lastPoint) ? i.lastPoint : null,
i.isOnSegment(t.firstPoint) ? t.firstPoint : null,
i.isOnSegment(t.lastPoint) ? t.lastPoint : null
].filter((h) => h !== null)
).sort((h, l) => h[0] - l[0]);
if (a.length === 0 || a.length === 1) return null;
if (a.length === 2) return new U(a[0], a[1]);
throw console.error(a), new Error(
"Unexpected number of points while intersecting parallel lines"
);
}
const { intersectionParam1: r, intersectionParam2: o } = s;
return !i.isValidParameter(r) || !t.isValidParameter(o) ? null : i.paramPoint(r);
}
const me = (i, t) => {
const e = q(i, t);
return di(e);
}, vn = Symbol.for("pantograph:Arc");
let D = class jt extends oe {
constructor(t, e, n, s = !1, { ignoreChecks: r = !1 } = {}) {
if (super(t, e), F(this, "segmentType", "ARC"), F(this, "center"), F(this, "clockwise"), F(this, "_coefficients", null), F(this, "_angularLength", null), F(this, "_radius", null), F(this, "_firstAngle", null), F(this, "_lastAngle", null), F(this, "_boundingBox", null), Object.defineProperty(this, vn, { value: !0 }), this.center = n, this.clockwise = s, !r) {
if (k(t, e))
throw new Error("Invalid arc, cannot be a full circle");
if (Math.abs(this.radius - Z(this.lastPoint, this.center)) > this.precision)
throw new Error(
`Invalid arc, radius does not match between ${it(
t
)} and ${it(e)}} (center ${it(n)})`
);
}
}
static isInstance(t) {
return !!t && t[vn] === !0;
}
get info() {
return `ARC(${it(this.firstPoint)}, ${it(
this.lastPoint
)}, ${it(this.center)}, ${this.clockwise ? "CW" : "CCW"})`;
}
get coefficients() {
if (this._coefficients === null) {
const [t, e] = this.center, n = this.radius * this.radius;
this._coefficients = {
x2: 1 / n,
xy: 0,
y2: 1 / n,
x: -(2 * t) / n,
y: -(2 * e) / n,
c: (t * t + e * e - n) / n
};
}
return this._coefficients;
}
isValidParameter(t) {
return 1 - t >= -this.precision && t >= -this.precision;
}
angleToParam(t) {
return Se(this.firstAngle, Ct(t), this.clockwise) / this.angularLength;
}
get angularLength() {
return this._angularLength || (this._angularLength = Se(
this.firstAngle,
this.lastAngle,
this.clockwise
)), this._angularLength;
}
paramPoint(t) {
return W(
this.center,
xt(
this.radius,
this.firstAngle + t * this.angularLength * (this.clockwise ? -1 : 1)
)
);
}
pointToParam(t) {
const [e, n] = me(t, this.center);
if (Math.abs(e - this.radius) > this.precision)
throw new Error(
`Point ${it(t)} is not on segment ${this.repr}`
);
const s = this.angleToParam(n);
if (!this.isValidParameter(s))
throw new Error(
`Point ${it(t)} is not on segment ${this.repr}`
);
return s;
}
get radius() {
return this._radius === null && (this._radius = Z(this.firstPoint, this.center)), this._radius;
}
get firstAngle() {
if (this._firstAngle === null) {
const [t, e] = q(this.firstPoint, this.center);
this._firstAngle = Ct(Math.atan2(e, t));
}
return this._firstAngle;
}
get lastAngle() {
if (this._lastAngle === null) {
const [t, e] = q(this.lastPoint, this.center);
this._lastAngle = Ct(Math.atan2(e, t));
}
return this._lastAngle;
}
get length() {
return this.radius * this.angularLength;
}
get squareLength() {
return this.length * this.length;
}
get midPoint() {
return this.paramPoint(0.5);
}
isSame(t) {
return !jt.isInstance(t) || !k(this.center, t.center) ? !1 : k(this.firstPoint, t.firstPoint) && k(this.lastPoint, t.lastPoint) && this.clockwise === t.clockwise || k(this.lastPoint, t.firstPoint) && k(this.firstPoint, t.lastPoint) && this.clockwise === !t.clockwise;
}
clone() {
return new jt(
this.firstPoint,
this.lastPoint,
this.center,
this.clockwise
);
}
reverse() {
return new jt(
this.lastPoint,
this.firstPoint,
this.center,
!this.clockwise
);
}
get boundingBox() {
if (this._boundingBox === null) {
const t = this.radius + this.precision, e = (n) => this.isValidParameter(this.angleToParam(n));
this._boundingBox = new re(
e(Math.PI) ? this.center[0] - t : Math.min(this.firstPoint[0], this.lastPoint[0]) - this.precision,
e(Math.PI * 1.5) ? this.center[1] - t : Math.min(this.firstPoint[1], this.lastPoint[1]) - this.precision,
e(0) ? this.center[0] + t : Math.max(this.firstPoint[0], this.lastPoint[0]) + this.precision,
e(Math.PI / 2) ? this.center[1] + t : Math.max(this.firstPoint[1], this.lastPoint[1]) + this.precision
);
}
return this._boundingBox;
}
distanceFrom(t) {
const [e, n] = me(t, this.center);
return this.isValidParameter(this.angleToParam(n)) ? Math.abs(e - this.radius) : Math.sqrt(
Math.min(
$t(t, this.firstPoint),
$t(t, this.lastPoint)
)
);
}
isOnSegment(t) {
if (k(t, this.firstPoint) || k(t, this.lastPoint))
return !0;
const [e, n] = me(t, this.center);
if (Math.abs(e - this.radius) > this.precision) return !1;
const s = this.angleToParam(n);
return this.isValidParameter(s);
}
gradientAt(t) {
const e = this.firstAngle + t * this.angularLength * (this.clockwise ? -1 : 1), n = this.radius * this.angularLength, s = -n * Math.sin(e), r = n * Math.cos(e);
return this.clockwise ? [-s, -r] : [s, r];
}
tangentAt(t) {
const [e, n] = me(t, this.center);
if (Math.abs(e - this.radius) > this.precision)
throw new Error("Point is not on the arc");
const s = this.angleToParam(n);
if (!this.isValidParameter(s))
throw new Error("Point is not on the arc");
const r = xt(1, n);
return (this.clockwise ? Zt : nt)(rt(r));
}
get tangentAtFirstPoint() {
const t = xt(1, this.firstAngle);
return (this.clockwise ? Zt : nt)(rt(t));
}
get tangentAtLastPoint() {
const t = xt(1, this.lastAngle);
return (this.clockwise ? Zt : nt)(rt(t));
}
splitAt(t) {
let e;
if (Array.isArray(t) && t.length === 0)
return [this];
Array.isArray(t[0]) ? e = t : e = [t];
const n = [0, 1, ...e.map((o) => this.pointToParam(o))], s = new Map(
dt([n, [this.firstPoint, this.lastPoint, ...e]])
);
n.sort((o, a) => o - a);
let r = null;
return n.flatMap((o, a) => {
if (a === n.length - 1) return [];
const h = n[a + 1];
if (h - o < this.precision)
return r === null && (r = o), [];
const l = r === null ? o : r, c = new jt(
s.get(l) || this.paramPoint(l),
s.get(h) || this.paramPoint(h),
this.center,
this.clockwise
);
return r = null, c;
});
}
transform(t) {
return new jt(
t.transform(this.firstPoint),
t.transform(this.lastPoint),
t.transform(this.center),
t.keepsOrientation() ? this.clockwise : !this.clockwise
);
}
};
function us(i, t, e) {
const n = new U(t, i), s = new U(t, e), r = nt(n.tangentAtFirstPoint), o = nt(s.tangentAtLastPoint), a = $e(
{ firstPoint: n.midPoint, V: r, precision: 1e-9 },
{ firstPoint: s.midPoint, V: o, precision: 1e-9 }
);
if (a === "parallel")
throw new Error("Cannot create an arc from three colinear points");
const h = yt(
q(i, t),
q(e, t)
) > 0;
return new D(
i,
e,
W(n.midPoint, G(r, a.intersectionParam1)),
h,
{ ignoreChecks: !0 }
);
}
function yi(i, t, e) {
const n = new U(i, t), s = nt(n.tangentAtFirstPoint), r = $e(
{ firstPoint: n.midPoint, V: s, precision: 1e-9 },
{
firstPoint: i,
V: nt(e),
precision: 1e-9
}
);
if (r === "parallel")
throw new Error("Cannot create an arc from three colinear points");
const o = W(
n.midPoint,
G(s, r.intersectionParam1)
), a = yt(
q(o, i),
q(o, W(i, e))
) < 0;
return new D(i, t, o, a, {
ignoreChecks: !0
});
}
const An = 1e-21, ge = 1.618034;
function En(i, t = 0, e = 1, n = 110, s = 1e3) {
let r, o, a, h, l, c, u, m, w, g, p, M, f;
for (o = i(t), a = i(e), o < a && ([t, e] = [e, t], [o, a] = [a, o]), f = e + ge * (e - t), h = i(f), l = 3, u = 0; h < a; ) {
if (m = (e - t) * (a - h), w = (e - f) * (a - o), g = w - m, Math.abs(g) < An ? r = 2 * An : r = 2 * g, p = e - ((e - f) * w - (e - t) * m) / r, M = e + n * (f - e), u > s)
throw new Error("Too many iterations.");
if (u += 1, (p - f) * (e - p) > 0) {
if (c = i(p), l += 1, c < h)
return t = e, e = p, o = a, a = c, [t, e, f, o, a, h, l];
if (c > a)
return f = p, h = c, [t, e, f, o, a, h, l];
p = f + ge * (f - e), c = i(p), l += 1;
} else
(p - M) * (M - f) >= 0 ? (p = M, c = i(p), l += 1) : (p - M) * (f - p) > 0 ? (c = i(p), l += 1, c < h && (e = f, f = p, p = f + ge * (f - e), a = h, h = c, c = i(p), l += 1)) : (p = f + ge * (f - e), c = i(p), l += 1);
t = e, e = f, f = p, o = a, a = h, h = c;
}
return [t, e, f, o, a, h, l];
}
const In = 0.381966, cs = 1e-11;
class fs {
constructor(t, e = 148e-10, n = 500) {
F(this, "xmin"), F(this, "fval"), F(this, "iter"), F(this, "funcalls"), F(this, "brack"), this.func = t, this.tol = e, this.maxiter = n, this.func = t, this.tol = e, this.maxiter = n, this.xmin = 1 / 0, this.fval = 1 / 0, this.iter = 0, this.funcalls = 0, this.brack = null;
}
setBracket(t = null) {
this.brack = t;
}
getBracketInfo() {
let t, e, n, s, r, o, a;
const h = this.func, l = this.brack;
if (l === null)
[r, o, a, t, e, n, s] = En(h);
else if (l.length === 2)
[r, o, a, t, e, n, s] = En(h, l[0], l[1]);
else if (l.length === 3) {
if ([r, o, a] = l, r > a && ([a, r] = [r, a]), !(r < o && o < a))
throw new Error("Not a bracketing interval.");
if (t = h(r), e = h(o), n = h(a), !(e < t && e < n))
throw new Error("Not a bracketing interval.");
s = 3;
} else
throw new Error(
"Bracketing interval must be length 2 or 3 sequence."
);
return [r, o, a, t, e, n, s];
}
optimize() {
const t = this.func;
let [e, n, s, , , , r] = this.getBracketInfo(), o = n, a = n, h = n, l = t(o), c = l, u = l, m, w;
e < s ? (m = e, w = s) : (m = s, w = e);
let g = 0;
r = 1;
let p = 0, M = 0, f;
for (; p < this.maxiter; ) {
const P = this.tol * Math.abs(o) + cs, b = 2 * P, I = 0.5 * (m + w);
if (Math.abs(o - I) < b - 0.5 * (w - m))
break;
if (Math.abs(g) <= P)
o >= I ? g = m - o : g = w - o, M = In * g;
else {
const V = (o - a) * (u - c);
let v = (o - h) * (u - l), S = (o - h) * v - (o - a) * V;
v = 2 * (v - V), v > 0 && (S = -S), v = Math.abs(v);
const O = g;
g = M, S > v * (m - o) && S < v * (w - o) && Math.abs(S) < Math.abs(0.5 * v * O) ? (M = S * 1 / v, f = o + M, (f - m < b || w - f < b) && (I - o >= 0 ? M = P : M = -P)) : (o >= I ? g = m - o : g = w - o, M = In * g);
}
Math.abs(M) < P ? M >= 0 ? f = o + P : f = o - P : f = o + M;
const C = t(f);
r += 1, C > u ? (f < o ? m = f : w = f, C <= l || a === o ? (h = a, a = f, c = l, l = C) : (C <= c || h === o || h === a) && (h = f, c = C)) : (f >= o ? m = o : w = o, h = a, a = o, o = f, c = l, l = u, u = C), p += 1;
}
this.xmin = o, this.fval = u, this.iter = p, this.funcalls = r;
}
results() {
return {
argMin: this.xmin,
fMin: this.fval,
iterations: this.iter,
funcCalls: this.funcalls
};
}
run() {
return this.optimize(), this.results();
}
}
function bi(i, t = 1e-8, e = 1e3) {
const n = new fs(i, t, e);
return n.setBracket([0, 1]), n.run();
}
function xi(i) {
if (i.__esModule) return i;
var t = i.default;
if (typeof t == "function") {
var e = function n() {
return this instanceof n ? Reflect.construct(t, arguments, this.constructor) : t.apply(this, arguments);
};
e.prototype = t.prototype;
} else e = {};
return Object.defineProperty(e, "__esModule", { value: !0 }), Object.keys(i).forEach(function(n) {
var s = Object.getOwnPropertyDescriptor(i, n);
Object.defineProperty(e, n, s.get ? s : {
enumerable: !0,
get: function() {
return i[n];
}
});
}), e;
}
var N = {};
const ms = Object.prototype.toString;
function ee(i) {
const t = ms.call(i);
return t.endsWith("Array]") && !t.includes("Big");
}
const gs = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
isAnyArray: ee
}, Symbol.toStringTag, { value: "Module" })), ps = /* @__PURE__ */ xi(gs);
function ws(i) {
var t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
if (!ee(i))
throw new TypeError("input must be an array");
if (i.length === 0)
throw new TypeError("input must not be empty");
var e = t.fromIndex, n = e === void 0 ? 0 : e, s = t.toIndex, r = s === void 0 ? i.length : s;
if (n < 0 || n >= i.length || !Number.isInteger(n))
throw new Error("fromIndex must be a positive integer smaller than length");
if (r <= n || r > i.length || !Number.isInteger(r))
throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");
for (var o = i[n], a = n + 1; a < r; a++)
i[a] > o && (o = i[a]);
return o;
}
function ds(i) {
var t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
if (!ee(i))
throw new TypeError("input must be an array");
if (i.length === 0)
throw new TypeError("input must not be empty");
var e = t.fromIndex, n = e === void 0 ? 0 : e, s = t.toIndex, r = s === void 0 ? i.length : s;
if (n < 0 || n >= i.length || !Number.isInteger(n))
throw new Error("fromIndex must be a positive integer smaller than length");
if (r <= n || r > i.length || !Number.isInteger(r))
throw new Error("toIndex must be an integer greater than fromIndex and at most equal to length");
for (var o = i[n], a = n + 1; a < r; a++)
i[a] < o && (o = i[a]);
return o;
}
function Ps(i) {
var t = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
if (ee(i)) {
if (i.length === 0)
throw new TypeError("input must not be empty");
} else throw new TypeError("input must be an array");
var e;
if (t.output !== void 0) {
if (!ee(t.output))
throw new TypeError("output option must be an array if specified");
e = t.output;
} else
e = new Array(i.length);
var n = ds(i), s = ws(i);
if (n === s)
throw new RangeError("minimum and maximum input values are equal. Cannot rescale a constant array");
var r = t.min, o = r === void 0 ? t.autoMinMax ? n : 0 : r, a = t.max, h = a === void 0 ? t.autoMinMax ? s : 1 : a;
if (o >= h)
throw new RangeError("min option must be smaller than max option");
for (var l = (h - o) / (s - n), c = 0; c < i.length; c++)
e[c] = (i[c] - n) * l + o;
return e;
}
const Ms = /* @__PURE__ */ Object.freeze(/* @__PURE__ */ Object.defineProperty({
__proto__: null,
default: Ps
}, Symbol.toStringTag, { value: "Module" })), ys = /* @__PURE__ */ xi(Ms);
Object.defineProperty(N, "__esModule", { value: !0 });
var ct = ps, Sn = ys;
const pe = " ".repeat(2), vi = " ".repeat(4);
function bs() {
return Ai(this);
}
function Ai(i, t = {}) {
const {
maxRows: e = 15,
maxColumns: n = 10,
maxNumSize: s = 8,
padMinus: r = "auto"
} = t;
return `${i.constructor.name} {
${pe}[
${vi}${xs(i, e, n, s, r)}
${pe}]
${pe}rows: ${i.rows}
${pe}columns: ${i.columns}
}`;
}
function xs(i, t, e, n, s) {
const { rows: r, columns: o } = i, a = Math.min(r, t), h = Math.min(o, e), l = [];
if (s === "auto") {
s = !1;
t: for (let c = 0; c < a; c++)
for (let u = 0; u < h; u++)
if (i.get(c, u) < 0) {
s = !0;
break t;
}
}
for (let c = 0; c < a; c++) {
let u = [];
for (let m = 0; m < h; m++)
u.push(vs(i.get(c, m), n, s));
l.push(`${u.join(" ")}`);
}
return h !== o && (l[l.length - 1] += ` ... ${o - e} more columns`), a !== r && l.push(`... ${r - t} more rows`), l.join(`
${vi}`);
}
function vs(i, t, e) {
return (i >= 0 && e ? ` ${kn(i, t - 1)}` : kn(i, t)).padEnd(t);
}
function kn(i, t) {
let e = i.toString();
if (e.length <= t) return e;
let n = i.toFixed(t);
if (n.length > t && (n = i.toFixed(Math.max(0, t - (n.length - t)))), n.length <= t && !n.startsWith("0.000") && !n.startsWith("-0.000"))
return n;
let s = i.toExponential(t);
return s.length > t && (s = i.toExponential(Math.max(0, t - (s.length - t)))), s.slice(0);
}
function As(i, t) {
i.prototype.add = function(e) {
return typeof e == "number" ? this.addS(e) : this.addM(e);
}, i.prototype.addS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) + e);
return this;
}, i.prototype.addM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) + e.get(n, s));
return this;
}, i.add = function(e, n) {
return new t(e).add(n);
}, i.prototype.sub = function(e) {
return typeof e == "number" ? this.subS(e) : this.subM(e);
}, i.prototype.subS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) - e);
return this;
}, i.prototype.subM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) - e.get(n, s));
return this;
}, i.sub = function(e, n) {
return new t(e).sub(n);
}, i.prototype.subtract = i.prototype.sub, i.prototype.subtractS = i.prototype.subS, i.prototype.subtractM = i.prototype.subM, i.subtract = i.sub, i.prototype.mul = function(e) {
return typeof e == "number" ? this.mulS(e) : this.mulM(e);
}, i.prototype.mulS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) * e);
return this;
}, i.prototype.mulM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) * e.get(n, s));
return this;
}, i.mul = function(e, n) {
return new t(e).mul(n);
}, i.prototype.multiply = i.prototype.mul, i.prototype.multiplyS = i.prototype.mulS, i.prototype.multiplyM = i.prototype.mulM, i.multiply = i.mul, i.prototype.div = function(e) {
return typeof e == "number" ? this.divS(e) : this.divM(e);
}, i.prototype.divS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) / e);
return this;
}, i.prototype.divM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) / e.get(n, s));
return this;
}, i.div = function(e, n) {
return new t(e).div(n);
}, i.prototype.divide = i.prototype.div, i.prototype.divideS = i.prototype.divS, i.prototype.divideM = i.prototype.divM, i.divide = i.div, i.prototype.mod = function(e) {
return typeof e == "number" ? this.modS(e) : this.modM(e);
}, i.prototype.modS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) % e);
return this;
}, i.prototype.modM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) % e.get(n, s));
return this;
}, i.mod = function(e, n) {
return new t(e).mod(n);
}, i.prototype.modulus = i.prototype.mod, i.prototype.modulusS = i.prototype.modS, i.prototype.modulusM = i.prototype.modM, i.modulus = i.mod, i.prototype.and = function(e) {
return typeof e == "number" ? this.andS(e) : this.andM(e);
}, i.prototype.andS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) & e);
return this;
}, i.prototype.andM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) & e.get(n, s));
return this;
}, i.and = function(e, n) {
return new t(e).and(n);
}, i.prototype.or = function(e) {
return typeof e == "number" ? this.orS(e) : this.orM(e);
}, i.prototype.orS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) | e);
return this;
}, i.prototype.orM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) | e.get(n, s));
return this;
}, i.or = function(e, n) {
return new t(e).or(n);
}, i.prototype.xor = function(e) {
return typeof e == "number" ? this.xorS(e) : this.xorM(e);
}, i.prototype.xorS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) ^ e);
return this;
}, i.prototype.xorM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) ^ e.get(n, s));
return this;
}, i.xor = function(e, n) {
return new t(e).xor(n);
}, i.prototype.leftShift = function(e) {
return typeof e == "number" ? this.leftShiftS(e) : this.leftShiftM(e);
}, i.prototype.leftShiftS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) << e);
return this;
}, i.prototype.leftShiftM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) << e.get(n, s));
return this;
}, i.leftShift = function(e, n) {
return new t(e).leftShift(n);
}, i.prototype.signPropagatingRightShift = function(e) {
return typeof e == "number" ? this.signPropagatingRightShiftS(e) : this.signPropagatingRightShiftM(e);
}, i.prototype.signPropagatingRightShiftS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) >> e);
return this;
}, i.prototype.signPropagatingRightShiftM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) >> e.get(n, s));
return this;
}, i.signPropagatingRightShift = function(e, n) {
return new t(e).signPropagatingRightShift(n);
}, i.prototype.rightShift = function(e) {
return typeof e == "number" ? this.rightShiftS(e) : this.rightShiftM(e);
}, i.prototype.rightShiftS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) >>> e);
return this;
}, i.prototype.rightShiftM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) >>> e.get(n, s));
return this;
}, i.rightShift = function(e, n) {
return new t(e).rightShift(n);
}, i.prototype.zeroFillRightShift = i.prototype.rightShift, i.prototype.zeroFillRightShiftS = i.prototype.rightShiftS, i.prototype.zeroFillRightShiftM = i.prototype.rightShiftM, i.zeroFillRightShift = i.rightShift, i.prototype.not = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, ~this.get(e, n));
return this;
}, i.not = function(e) {
return new t(e).not();
}, i.prototype.abs = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.abs(this.get(e, n)));
return this;
}, i.abs = function(e) {
return new t(e).abs();
}, i.prototype.acos = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.acos(this.get(e, n)));
return this;
}, i.acos = function(e) {
return new t(e).acos();
}, i.prototype.acosh = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.acosh(this.get(e, n)));
return this;
}, i.acosh = function(e) {
return new t(e).acosh();
}, i.prototype.asin = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.asin(this.get(e, n)));
return this;
}, i.asin = function(e) {
return new t(e).asin();
}, i.prototype.asinh = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.asinh(this.get(e, n)));
return this;
}, i.asinh = function(e) {
return new t(e).asinh();
}, i.prototype.atan = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.atan(this.get(e, n)));
return this;
}, i.atan = function(e) {
return new t(e).atan();
}, i.prototype.atanh = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.atanh(this.get(e, n)));
return this;
}, i.atanh = function(e) {
return new t(e).atanh();
}, i.prototype.cbrt = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.cbrt(this.get(e, n)));
return this;
}, i.cbrt = function(e) {
return new t(e).cbrt();
}, i.prototype.ceil = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.ceil(this.get(e, n)));
return this;
}, i.ceil = function(e) {
return new t(e).ceil();
}, i.prototype.clz32 = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.clz32(this.get(e, n)));
return this;
}, i.clz32 = function(e) {
return new t(e).clz32();
}, i.prototype.cos = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.cos(this.get(e, n)));
return this;
}, i.cos = function(e) {
return new t(e).cos();
}, i.prototype.cosh = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.cosh(this.get(e, n)));
return this;
}, i.cosh = function(e) {
return new t(e).cosh();
}, i.prototype.exp = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.exp(this.get(e, n)));
return this;
}, i.exp = function(e) {
return new t(e).exp();
}, i.prototype.expm1 = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.expm1(this.get(e, n)));
return this;
}, i.expm1 = function(e) {
return new t(e).expm1();
}, i.prototype.floor = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.floor(this.get(e, n)));
return this;
}, i.floor = function(e) {
return new t(e).floor();
}, i.prototype.fround = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.fround(this.get(e, n)));
return this;
}, i.fround = function(e) {
return new t(e).fround();
}, i.prototype.log = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.log(this.get(e, n)));
return this;
}, i.log = function(e) {
return new t(e).log();
}, i.prototype.log1p = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.log1p(this.get(e, n)));
return this;
}, i.log1p = function(e) {
return new t(e).log1p();
}, i.prototype.log10 = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.log10(this.get(e, n)));
return this;
}, i.log10 = function(e) {
return new t(e).log10();
}, i.prototype.log2 = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.log2(this.get(e, n)));
return this;
}, i.log2 = function(e) {
return new t(e).log2();
}, i.prototype.round = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.round(this.get(e, n)));
return this;
}, i.round = function(e) {
return new t(e).round();
}, i.prototype.sign = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.sign(this.get(e, n)));
return this;
}, i.sign = function(e) {
return new t(e).sign();
}, i.prototype.sin = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.sin(this.get(e, n)));
return this;
}, i.sin = function(e) {
return new t(e).sin();
}, i.prototype.sinh = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.sinh(this.get(e, n)));
return this;
}, i.sinh = function(e) {
return new t(e).sinh();
}, i.prototype.sqrt = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.sqrt(this.get(e, n)));
return this;
}, i.sqrt = function(e) {
return new t(e).sqrt();
}, i.prototype.tan = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.tan(this.get(e, n)));
return this;
}, i.tan = function(e) {
return new t(e).tan();
}, i.prototype.tanh = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.tanh(this.get(e, n)));
return this;
}, i.tanh = function(e) {
return new t(e).tanh();
}, i.prototype.trunc = function() {
for (let e = 0; e < this.rows; e++)
for (let n = 0; n < this.columns; n++)
this.set(e, n, Math.trunc(this.get(e, n)));
return this;
}, i.trunc = function(e) {
return new t(e).trunc();
}, i.pow = function(e, n) {
return new t(e).pow(n);
}, i.prototype.pow = function(e) {
return typeof e == "number" ? this.powS(e) : this.powM(e);
}, i.prototype.powS = function(e) {
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) ** e);
return this;
}, i.prototype.powM = function(e) {
if (e = t.checkMatrix(e), this.rows !== e.rows || this.columns !== e.columns)
throw new RangeError("Matrices dimensions must be equal");
for (let n = 0; n < this.rows; n++)
for (let s = 0; s < this.columns; s++)
this.set(n, s, this.get(n, s) ** e.get(n, s));
return this;
};
}
function gt(i, t, e) {
let n = e ? i.rows : i.rows - 1;
if (t < 0 || t > n)
throw new RangeError("Row index out of range");
}
function pt(i, t, e) {
let n = e ? i.columns : i.columns - 1;
if (t < 0 || t > n)
throw new RangeError("Column index out of range");
}
function Ot(i, t) {
if (t.to1DArray && (t = t.to1DArray()), t.length !== i.columns)
throw new RangeError(
"vector size must be the same as the number of columns"
);
return t;
}
function qt(i, t) {
if (t.to1DArray && (t = t.to1DArray()), t.length !== i.rows)
throw new RangeError("vector size must be the same as the number of rows");
return t;
}
function rn(i, t) {
if (!ct.isAnyArray(t))
throw new TypeError("row indices must be an array");
for (let e = 0; e < t.length; e++)
if (t[e] < 0 || t[e] >= i.rows)
throw new RangeError("row indices are out of range");
}
function on(i, t) {
if (!ct.isAnyArray(t))
throw new TypeError("column indices must be an array");
for (let e = 0; e < t.length; e++)
if (t[e] < 0 || t[e] >= i.columns)
throw new RangeError("column indices are out of range");
}
function Ye(i, t, e, n, s) {
if (arguments.length !== 5)
throw new RangeError("expected 4 arguments");
if (we("startRow", t), we("endRow", e), we("startColumn", n), we("endColumn", s), t > e || n > s || t < 0 || t >= i.rows || e < 0 || e >= i.rows || n < 0 || n >= i.columns || s < 0 || s >= i.columns)
throw new RangeError("Submatrix indices are out of range");
}
function _e(i, t = 0) {
let e = [];
for (let n = 0; n < i; n++)
e.push(t);
return e;
}
function we(i, t) {
if (typeof t != "number")
throw new TypeError(`${i} must be a number`);
}
function Bt(i) {
if (i.isEmpty())
throw new Error("Empty matrix has no elements to index");
}
function Es(i) {
let t = _e(i.rows);
for (let e = 0; e < i.rows; ++e)
for (let n = 0; n < i.columns; ++n)
t[e] += i.get(e, n);
return t;
}
function Is(i) {
let t = _e(i.columns);
for (let e = 0; e < i.rows; ++e)
for (let n = 0; n < i.columns; ++n)
t[n] += i.get(e, n);
return t;
}
function Ss(i) {
let t = 0;
for (let e = 0; e < i.rows; e++)
for (let n = 0; n < i.columns; n++)
t += i.get(e, n);
return t;
}
function ks(i) {
let t = _e(i.rows, 1);
for (let e = 0; e < i.rows; ++e)
for (let n = 0; n < i.columns; ++n)
t[e] *= i.get(e, n);
return t;
}
function Cs(i) {
let t = _e(i.columns, 1);
for (let e = 0; e < i.rows; ++e)
for (let n = 0; n < i.columns; ++n)
t[n] *= i.get(e, n);
return t;
}
function Rs(i) {
let t = 1;
for (let e = 0; e < i.rows; e++)
for (let n = 0; n < i.columns; n++)
t *= i.get(e, n);
return t;
}
function Ts(i, t, e) {
const n = i.rows, s = i.columns, r = [];
for (let o = 0; o < n; o++) {
let a = 0, h = 0, l = 0;
for (let c = 0; c < s; c++)
l = i.get(o, c) - e[o], a += l, h += l * l;
t ? r.push((h - a * a / s) / (s - 1)) : r.push((h - a * a / s) / s);
}
return r;
}
function $s(i, t, e) {
const n = i.rows, s = i.columns, r = [];
for (let o = 0; o < s; o++) {
let a = 0, h = 0, l = 0;
for (let c = 0; c < n; c++)
l = i.get(c, o) - e[o], a += l, h += l * l;
t ? r.push((h - a * a / n) / (n - 1)) : r.push((h - a * a / n) / n);
}
return r;
}
function _s(i, t, e) {
const n = i.rows, s = i.columns, r = n * s;
let o = 0, a = 0, h = 0;
for (let l = 0; l < n; l++)
for (let c = 0; c < s; c++)
h = i.get(l, c) - e, o += h, a += h * h;
return t ? (a - o * o / r) / (r - 1) : (a - o * o / r) / r;
}
function Fs(i, t) {
for (let e = 0; e < i.rows; e++)
for (let n = 0; n < i.columns; n++)
i.set(e, n, i.get(e, n) - t[e]);
}
function Ls(i, t) {
for (let e = 0; e < i.rows; e++)
for (let n = 0; n < i.columns; n++)
i.set(e, n, i.get(e, n) - t[n]);
}
function Bs(i, t) {
for (let e = 0; e < i.rows; e++)
for (let n = 0; n < i.columns; n++)
i.set(e, n, i.get(e, n) - t);
}
function Vs(i) {
const t = [];
for (let e = 0; e < i.rows; e++) {
let n = 0;
for (let s = 0; s < i.columns; s++)
n += i.get(e, s) ** 2 / (i.columns - 1);
t.push(Math.sqrt(n));
}
return t;
}
function js(i, t) {
for (let e = 0; e < i.rows; e++)
for (let n = 0; n < i.columns; n++)
i.set(e, n, i.get(e, n) / t[e]);
}
functi