UNPKG

@stemcmicro/core

Version:

Computer Algebra System in TypeScript

1 lines 291 kB
import{native_sym as t,Native as e,is_native as n,log as r,multiply as i,real as s,exp as o}from"@stemcmicro/native";export{NATIVE_MAX,NATIVE_MIN,Native,code_from_native_sym,is_native_sym,native_sym}from"@stemcmicro/native";import{create_sym as a,is_boo as u,Flt as c,negOne as l,is_num as f,is_rat as h,imu as d,is_flt as p,Rat as m,bigInt as g,is_tensor as x,zero as w,one as y,Tensor as v,create_int as E,BigInteger as b,is_sym as S,is_str as O,is_err as q,is_uom as M,is_blade as _,is_hyp as F,is_keyword as L,is_imu as k,Err as D,Cell as A,Map as P,create_flt as T,create_str as N,create_tensor as I,et as $,booT as B,epsilon as C,Str as U,piAsFlt as H,eAsFlt as R,two as z,four as V,half as X,negFour as K,three as Z,nine as j,third as J,eight as G,Sym as Y,Boo as Q,assert_tensor as W,Hyp as tt,create_tensor_elements_diagonal as et,is_map as nt,is_tag as rt,is_jsobject as it,create_boo as st,JsObject as ot,assert_jsobject as at,assert_sym as ut,booU as ct,Uom as lt,QQ as ft,create_rat as ht,zeroAsFlt as dt,is_cell as pt,assert_str as mt,assert_map as gt,assert_cell as xt,assert_flt as wt,create_hyp as yt,booF as vt,assert_rat as Et,Keyword as bt,create_tensor_elements as St,oneAsFlt as Ot}from"@stemcmicro/atoms";import{diagnostic as qt,Diagnostics as Mt,is_localizable as _t,Localizable as Ft}from"@stemcmicro/diagnostics";import{str_to_string as Lt,isone as kt,is_add as Dt,is_multiply as At,is_power as Pt,guess as Tt,is_num_and_negative as Nt,is_rat_and_integer as It,is_num_and_eq_rational as $t,is_cons_opr_eq_power as Bt,is_num_and_eq_number as Ct,is_num_and_eq_one_half as Ut,multiply_items as Ht,negate as Rt,subst as zt,add as Vt,divide as Xt,is_mul_2_any_any as Kt,num_to_number as Zt,multiply as jt,power as Jt,inverse as Gt,subtract as Yt,iszero as Qt,is_negative as Wt,is_cons_opr_eq_multiply as te,compare_num_num as ee,is_cons_opr_eq_add as ne,is_cons_opr_eq_sym as re,is_outer as ie,is_inner_or_dot as se,lt_num_num as oe,is_cons_opr_eq_inv as ae,is_base_of_natural_logarithm as ue,is_pi as ce,is_num_and_eq_two as le,count_factors as fe,contains_single_blade as he,abs as de,is_factorial as pe,is_rat_and_fraction as me,convertMetricToNative as ge,convert_tensor_to_strings as xe,create_algebra_as_tensor as we,predicate_return_value as ye,prolog_eval_varargs as ve,float as Ee,is_safe_integer_range as be,is_opr_2_any_any as Se}from"@stemcmicro/helpers";export{create_algebra_as_blades}from"@stemcmicro/helpers";import{is_atom as Oe,nil as qe,is_nil as Me,is_cons as _e,cadr as Fe,caddr as Le,cadnr as ke,items_to_cons as De,cdr as Ae,car as Pe,cons as Te,assert_cons as Ne,assert_U as Ie,assert_cons_or_nil as $e,cddr as Be,cdddr as Ce,caadr as Ue,cadadr as He,is_cons2 as Re,cadddr as ze,is_singleton as Ve}from"@stemcmicro/tree";import{Lambda as Xe,SIGN_GT as Ke,SIGN_LT as Ze,SIGN_EQ as je,is_lambda as Je}from"@stemcmicro/context";import{Directive as Ge}from"@stemcmicro/directive";import{StackU as Ye,Stack as Qe}from"@stemcmicro/stack";import{scan_meta as We}from"@stemcmicro/em-parse";import{complexity as tn,conjfunc as en,inner as nn,push_rational as rn,power as sn,make_stack_draw as on,value_of as an,stopf as un,pop_integer as cn,multiply as ln,expfunc as fn,add as hn,sqrtfunc as dn,subtract as pn,negate as mn,push_integer as gn,multiply_factors as xn,power_e_expo as wn,stack_infix as yn,stack_kronecker as vn,stack_log as En,stack_mag as bn,stack_mod as Sn,stack_noexpand as On,stack_outer as qn,stack_rank as Mn,stack_arccosh as _n,stack_arcsin as Fn,stack_arcsinh as Ln,stack_arctan as kn,stack_arctanh as Dn,stack_arg as An,stack_binding as Pn,stack_circexp as Tn,stack_clock as Nn,stack_cos as In,stack_cosh as $n,stack_exp as Bn,stack_expcos as Cn,stack_expcosh as Un,stack_expsin as Hn,stack_expsinh as Rn,stack_exptan as zn,stack_exptanh as Vn,stack_floor as Xn,stack_hadamard as Kn,stack_imag as Zn,stack_index as jn,stack_inv as Jn,stack_minor as Gn,stack_minormatrix as Yn,stack_nroots as Qn,stack_polar as Wn,stack_rationalize as tr,stack_real as er,stack_rect as nr,stack_sin as rr,stack_sinh as ir,stack_sqrt as sr,stack_testlt as or,stack_testge as ar,stack_testgt as ur,stack_transpose as cr,stack_unit as lr}from"@stemcmicro/eigenmath";const fr=-1,hr=0,dr=1,pr=0,mr=1;function gr(t){return(t&mr)===mr}const xr=["Blade","Boo","Cell","Flt","Imu","Map","Rat","Sym","Tensor","Uom"];function wr(t){return"boolean"==typeof t&&t?1:0}const yr=[1,2];let vr=class{extension;constructor(t){this.extension=t}create(t){return new this.extension(t)}};function Er(t){return new vr(t)}function br(t,e){return t.equals(e)?(e.addRef(),[pr,e]):(t.addRef(),[mr,t])}class Sr extends Error{constructor(t){super(),this.name="ProgrammingError","string"==typeof t&&(this.message=t)}}class Or{handler;#t;#e;constructor(t,e,n){this.handler=t,this.#t=e,this.#e=n}get hash(){return this.#t}get name(){return this.#t}iscons(){return!1}operator(){throw new Sr}isKind(t){return!!Oe(t)&&this.#e(t)}toHumanString(n,r){return Lt(this.handler.dispatch(n,t(e.human),qe,r))}toInfixString(n,r){return Lt(this.handler.dispatch(n,t(e.infix),qe,r))}toLatexString(n,r){return Lt(this.handler.dispatch(n,t(e.latex),qe,r))}toListString(n,r){return Lt(this.handler.dispatch(n,t(e.sexpr),qe,r))}evaluate(t,e,n){throw new Error("evaluate method not implemented.")}transform(t,e){const n=this.valueOf(t,e);try{return br(n,t)}finally{n.release()}}valueOf(t,e){return this.dispatch(t,a("valueof"),qe,e)}binL(t,e,n,r){return this.handler.binL(t,e,n,r)}binR(t,e,n,r){return this.handler.binR(t,e,n,r)}dispatch(t,e,n,r){const i=this.handler.dispatch(t,e,n,r);return Me(i)?qt(Mt.Property_0_does_not_exist_on_type_1,e,a(t.type)):i}subst(t,e,n,r){throw new Error("subst method not implemented.")}test(t,e,n){const r=this.handler.dispatch(t,e,qe,n);if(u(r))return r.isTrue();throw qt(Mt.Property_0_does_not_exist_on_type_1,e,a(t.type))}}class qr{builder;type;guard;constructor(t,e,n){this.builder=t,this.type=e,this.guard=n}create(t){return new Or(this.builder.create(),this.type,this.guard)}}class Mr{handler;constructor(t){this.handler=t}get hash(){throw new Error("Method not implemented.")}get name(){throw new Error("Method not implemented.")}phases;dependencies;iscons(){throw new Error("Method not implemented.")}operator(){throw new Error("Method not implemented.")}isKind(t,e){throw new Error("Method not implemented.")}subst(t,e,n,r){throw new Error("Method not implemented.")}toHumanString(t,e){throw new Error("Method not implemented.")}toInfixString(t,e){throw new Error("Method not implemented.")}toLatexString(t,e){throw new Error("Method not implemented.")}toListString(t,e){throw new Error("Method not implemented.")}evaluate(t,e,n){throw new Error("Method not implemented.")}transform(t,e){throw new Error("Method not implemented.")}valueOf(t,e){throw new Error("Method not implemented.")}test(t,e,n){return this.handler.test(t,e,n)}binL(t,e,n,r){return this.handler.binL(t,e,n,r)}binR(t,e,n,r){return this.handler.binR(t,e,n,r)}dispatch(t,e,n,r){return this.handler.dispatch(t,e,n,r)}}function _r(t,e,n){return t===Fr.d?Fr:t===Lr.d?Lr:t===Ar.d?Ar:t===kr.d?kr:t===Pr.d?Pr:new c(t,e,n)}const Fr=new c(0),Lr=new c(1),kr=new c(2),Dr=new c(Math.PI);new c(1e-6),new c(Math.E);const Ar=new c(-1),Pr=new c(-2),Tr=t(e.add),Nr=t(e.cos),Ir=t(e.tan);t(e.subtract);const $r=t(e.multiply);t(e.divide);const Br=t(e.pow),Cr=t(e.outer),Ur=t(e.inner),Hr=t(e.inv),Rr=t(e.lco),zr=t(e.rco),Vr=t(e.sin);t(e.succ),t(e.pred),t(e.E);const Xr=t(e.PI),Kr=t(e.factorial);t(e.iszero),t(e.testlt),t(e.testgt),t(e.testle),a(":"),t(e.tau);const Zr=t(e.IMU);var jr;!function(t){t[t.Ascii=0]="Ascii",t[t.Human=1]="Human",t[t.Infix=2]="Infix",t[t.LaTeX=3]="LaTeX",t[t.SExpr=4]="SExpr",t[t.EcmaScript=5]="EcmaScript"}(jr||(jr={}));const Jr=new class{constructor(){}tos=0;stack=[]};function Gr(t){throw Yr(0),new Error(`Stop: ${t}`)}function Yr(t){if(Jr.tos<=t)Jr.tos=t;else for(;Jr.tos>t;)Jr.stack[Jr.tos]=null,Jr.tos--}function Qr(t,e,n){n.pushDirective(Ge.expanding,0);try{return t(e,n)}finally{n.popDirective()}}function Wr(t,e,n){n.pushDirective(Ge.expanding,1);try{return t(e,n)}finally{n.popDirective()}}function ti(t,e,n,r){r.pushDirective(Ge.expanding,1);try{return t(e,n,r)}finally{r.popDirective()}}class ei{static NegOne(t){return t.getDirective(Ge.evaluatingAsFloat)?Ar:l}static PI(t){return t.getDirective(Ge.evaluatingAsFloat)?Dr:Xr}}function ni(t){return _e(t)?[...t].length:0}const ri=!0,ii=t(e.add);a("adj"),a("algebra");const si=a("and"),oi=a("approxratio"),ai=a("arccos"),ui=a("arccosh"),ci=a("arcsin"),li=a("arcsinh"),fi=a("arctan"),hi=a("arctanh");a("atomize");const di=a("besselj"),pi=a("bessely");a("binding");const mi=a("binomial"),gi=a("ceiling"),xi=a("check"),wi=a("choose");t(e.circexp);const yi=a("clear");a("clearall"),a("clearpatterns");const vi=a("coeff");a("cofactor"),a("compare"),a("compare-factors"),a("compare-terms");const Ei=t(e.component),bi=t(e.condense),Si=a("contract"),Oi=Nr,qi=a("cosh");a("decomp");const Mi=a("defint");a("denominator");const _i=a("det"),Fi=a("dim"),Li=a("dirac");a("divide"),a("divisors");const ki=a("do"),Di=a("dot");a("draw"),a("dsolve");const Ai=a("eigen"),Pi=a("eigenval"),Ti=a("eigenvec");a("equal");const Ni=a("erf"),Ii=a("erfc"),$i=a("eval"),Bi=t(e.exp),Ci=t(e.expand);a("expcos"),t(e.factor);const Ui=Kr;a("factorpoly");const Hi=t(e.float),Ri=a("floor"),zi=t(e.fn),Vi=a("for"),Xi=t(e.function),Ki=a("gamma"),Zi=t(e.gcd),ji=t(e.hermite);t(e.hilbert),a("if");const Ji=t(e.imag);t(e.inner);const Gi=t(e.integral),Yi=Hr;a("invg"),a("isinteger");const Qi=t(e.isprime),Wi=a("laguerre");a("laplace");const ts=a("lcm");t(e.lco);const es=a("legendre"),ns=a("let"),rs=t(e.log),is=$r;t(e.not),a("nroots"),a("number"),a("numerator");const ss=a("operator"),os=a("or");t(e.outer);const as=a("pattern");t(e.polar);const us=Br,cs=a("printLeaveEAlone"),ls=a("printLeaveXAlone");a("print");const fs=a("product"),hs=a("quote");a("quotient");const ds=a("rank");t(e.rco);const ps=t(e.real),ms=t(e.isreal),gs=a("round");t(e.rect),a("roots");const xs=t(e.assign),ws=a("sgn"),ys=Vr,vs=a("sinh");a("shape");const Es=a("sqrt");a("stop"),t(e.subst),a("-");const bs=a("sum"),Ss=Ir,Os=a("tanh"),qs=t(e.taylor),Ms=t(e.test),_s=t(e.testge),Fs=t(e.testgt),Ls=t(e.testle),ks=t(e.testlt),Ds=a("transpose"),As=a("unit"),Ps=a("uom");t(e.zero),a("lastPrint"),a("lastAsciiPrint"),a("lastInfixPrint"),a("lastLatexPrint"),a("lastSexprPrint"),a("lastHumanPrint"),a("autoexpand"),a("autofactor");const Ts=a("bake");a("trace");const Ns=a("$METAA"),Is=a("$METAB"),$s=a("$METAX"),Bs=a("$SECRETX");a("version"),a("a"),a("b"),a("c");const Cs=a("d"),Us=a("i"),Hs=a("j");a("n"),a("r");const Rs=a("s"),zs=a("t"),Vs=a("x"),Xs=a("y"),Ks=a("z"),Zs=a("I");a("a_"),a("b_"),a("x_");a("$C1"),a("$C2"),a("$C3"),a("$C4"),a("$C5"),a("$C6");const js=1e4,Js=24,Gs=function(){const t=[2];let e=3;for(;t.length<js;){let n=0;const r=Math.sqrt(e);for(;n<t.length&&t[n]<=r;){if(e%t[n]==0){n=-1;break}n++}-1!==n&&t.push(e),e+=2}return t[js]=0,t}();function Ys(t){return t.isZero()?0:t.isPositive()?1:-1}function Qs(t,e){return t.equals(e)}function Ws(t){return h(t)?1===Ys(t.a):!!p(t)&&t.d>0}function to(t){return!!f(t)&&t.isMinusOne()}function eo(t,e){return kt(t,e)||to(t)}function no(t,e){return null==e&&(e=Tt(t)),!(!function(t,e){return function(t,e){return At(t)?t.tail().every((t=>ro(t,e))):ro(t,e)}(t,e)}(t,e)||1!==function(...t){return t.reduce(((t,e)=>t+Number(e)),0)}(t.contains(Vs),t.contains(Xs),t.contains(Ks)))&&e}function ro(t,e){if(Pt(t)){const n=t.base,r=t.expo;return h(r)&&r.isPositiveInteger()&&so(n,e)}return so(t,e)}function io(t,e){return!!t.contains(e)&&so(t,e)}function so(t,e){return Dt(t)?t.tail().every((t=>oo(t,e))):oo(t,e)}function oo(t,e){return At(t)?t.tail().every((t=>ao(t,e))):ao(t,e)}function ao(t,e){if(t.equals(e))return!0;if(Pt(t)&&t.base.equals(e)){const e=t.expo;return h(e)&&e.isPositiveInteger()}return!t.contains(e)}function uo(t){if(Pt(t)){const e=t.expo;if(h(e)&&Nt(t.base))return!0}return!1}function co(t,e){return At(t)&&3===ni(t)&&p(ke(t,1))&&uo(ke(t,2))||e.equals(t,d)}function lo(t){if(At(t)&&3===ni(t)){if(f(Fe(t))&&Le(t).equals(d))return!0;if(t.equals(d))return!0;if(uo(Le(t)))return!0}return!1}function fo(t){if(Dt(t)){if(3===ni(t)){const e=Fe(t);if(f(e)&&(lo(Le(t))||lo(t)))return!0}}return!1}function ho(t){return It(t)&&t.a.isEven()}function po(t){return h(t)&&t.isFraction()}function mo(t){return $t(t,-1,2)}function go(t){return!(!_e(t)||!Bt(t))&&(Pt(t)&&Ct(Fe(t),2)&&$t(Le(t),-1,2))}function xo(t){return Pt(t)&&Ct(Fe(t),3)&&Ut(Le(t))}function wo(t){return!(!p(t)&&!Hi.equals(t))||!!_e(t)&&[...t].some(wo)}function yo(t,e,n){return r=function(t,e,n,r){const i=De(t,e,n);try{return r.valueOf(i)}finally{i.release()}}($r,t,e,n),r;var r}function vo(t,e,n){return function(t,e,n,r){r.pushDirective(Ge.expanding,0);try{return t(e,n,r)}finally{r.popDirective()}}(yo,t,e,n)}function Eo(t,e){e.pushDirective(Ge.factoring,1);try{return Ht(t,e)}finally{e.popDirective()}}function bo(t,e){return t.multiply(e)}function So(t,e){return t.divide(e)}function Oo(t,e){return t.mod(e)}function qo(t,e){const n=t.divmod(e);return[n.quotient,n.remainder]}function Mo(t){return g(t)}function _o(t,e){if(t.isPositive()){if(e.isNegative())return t.multiply(g(-1))}else if(e.isPositive())return t.multiply(g(-1));return t}function Fo(t,e){return t.isPositive()?t:t.multiply(g(-1))}function Lo(t){const e=So(t.a,t.b);return new m(e,g.one)}function ko(t,e){return new m(g(t),g(e))}function Do(t){return h(t)||p(t)?t.toNumber():0}function Ao(t){return new m(function(t){let e;if(0===t||1===t)return e=g(1),e;e=g(2);let n=g(0);if(3<=t)for(let r=3;r<=t;r++)n=g(r),e=bo(e,n);return e}(t),g.one)}function Po(t,e){const n=Rt(e,d),r=zt(t,d,n,e);return e.valueOf(r)}const To=t(e.testeq);function No(t,e,n){const r=De(To,t,e);try{return n.valueOf(r)}finally{r.release()}}const Io=t(e.rect);function $o(t,e){const n=De(Io,t);try{return e.valueOf(n)}finally{n.release()}}function Bo(t,e,n){const r=Rt(n,e);return Vt(n,t,r)}function Co(t,e){t=Ae(t);let n=e.valueOf(Pe(t));return _e(t)&&(n=t.tail().reduce(((t,n)=>Uo(t,e.valueOf(n),e)),n)),n}function Uo(t,e,n){return function(t,e,n){if(Dt(t))return function(t,e,n){return t.tail().reduce(((t,r)=>n.add(t,Uo(r,e,n))),w)}(t,e,n);if(x(t))return function(t,e,n){return t.map((t=>Uo(t,e,n)))}(t,e,n);if(t.contains(e))return w;return t}(t,e,n)}function Ho(t,e,n){n.pushDirective(Ge.expanding,1);try{return Xt(t,e,n)}finally{n.popDirective()}}function Ro(t,e,n){const r=new Ye;let i=t;for(;;){const t=n.valueOf(zt(i,e,w,n));if(r.push(t),i=i.equals(t)?w:n.valueOf(Bo(i,t,n)),i.equals(w))try{return r.elements}finally{r.release()}i.equals(e)?i=y:_e(i)&&zo(i,e)?i=i.lhs:(i=Ho(i,e,n),i=n.valueOf(i))}}function zo(t,e){return!!Kt(t)&&t.rhs.equals(e)}function Vo(n,r){const i=Xo(n,r),s=i.length;return new v([s],function(n,r){const i=n.map((function(t,e){return{value:t,index:e}}));i.sort((function(n,i){const s=r.compareFn(t(e.multiply))(n.value,i.value);return r.isscalar(n.value)||r.isscalar(i.value)?s:n.index-i.index}));const s=i.map((function(t){return t.value}));return s}(i,r))}function Xo(t,e){const n=[];if(f(t))n.push(...ba(Zt(t)));else if(_e(t)&&Dt(t))n.push(...function(t,e){const n=_e(t)?t.tail().reduce((function(t,n){return Sa(t,n,e)})):Pe(t),r=[];let i=n;if(kt(i,e))return r.push(t,y),r;if(f(i))r.push(...ba(Zt(i)));else if(At(i)){const t=Ae(i);f(Pe(t))?r.push(...ba(Zt(Pe(t)))):r.push(Pe(t),y),_e(t)&&t.tail().forEach((t=>r.push(t,y)))}else r.push(i,y);i=Gt(i,e);const s=_e(t)?t.tail().reduce(((t,n)=>Vt(e,t,jt(e,i,n))),w):Ae(t);return r.push(s,y),r}(t,e));else if(At(t)){let e=Ae(t);if(f(Pe(e))&&(n.push(...ba(Zt(Pe(e)))),e=Ae(e)),_e(e)){const t=[...e].map((t=>Pt(t)?[Fe(t),Le(t)]:[t,y]));n.push(...t.flat())}}else Pt(t)?n.push(Fe(t),Le(t)):n.push(t,y);const r=n.length;return n.push(y),Ko(n,0,r,e),n.slice(r)}function Ko(t,e,n,r){const i=t.pop();if(e===n)return void t.push(i);const s=t[e+0],o=t[e+1],a=Zt(o);if(!isNaN(a))for(let o=0;o<=Math.abs(a);o++)t.push(jt(r,i,Jt(r,s,E(((u=a)<0?-1:u>0?1:0)*o)))),Ko(t,e+2,n,r);var u}function Zo(t,e,n){return ti(jo,t,e,n)}function jo(t,e,n){const r=Sa(t,e,n),i=Xt(r,t,n),s=Xt(i,e,n);return Gt(s,n)}function Jo(t,e){const n=e.valueOf(t.item1),r=e.valueOf(t.item2),i=e.valueOf(t.item3);return i.isnil?Go(n,r,Vs,e):Go(n,r,i,e)}function Go(t,e,n,r){const i=Ro(t,n,r);let s=i.length-1;const o=Ro(e,n,r),a=o.length-1;let u=s-a,c=w;for(;u>=0;){const t=Xt(i[s],o[a],r);for(let e=0;e<=a;e++)i[u+e]=Yt(r,i[u+e],jt(r,o[e],t));c=Vt(r,c,jt(r,t,Jt(r,n,E(u)))),s--,u--}return c}class Yo extends Error{name="SystemError";constructor(t){super(t),console.warn(t,(new Error).stack)}}function Qo(t){if(void 0===t)throw new Error("expr must be defined.");Jr.stack[Jr.tos++]=t}function Wo(){if(0===Jr.tos)throw new Yo("stack underflow");const t=Jr.stack[--Jr.tos];return Jr.stack[Jr.tos]=null,t}function ta(t){for(;t.length>0;)Qo(t.shift())}function ea(t,e,n){const r=function(t,e){return t};if(wo(t))throw new Error("floating point numbers in polynomial");const i=Ro(t,e,n);for(let t=0;t<i.length;t++);const s=function(t,e){let n=y;for(const r of t)n=Zo(Na(r,e),n,e);for(let r=0;r<t.length;r++)t[r]=jt(e,n,t[r]);const r=Gt(n,e);return r}(i,n);for(let t=0;t<i.length;t++);let o,a,u,c="ℝ",l=null,f=i.length-1,h=s;for(;f>0;){let t=!1,s=!1;if(Qt(i[0],n)?(o=y,a=w):"ℝ"===c?[s,o,a]=na(i,f,e,o,a,n):"ℂ"===c&&([t,o]=ra(l,i,f,n)),"ℝ"===c){if(!1===s){c="ℂ";continue}{for(u=Vt(n,jt(n,o,e),a),h=vo(h,u,n),ia(o,a,i,f,n);f&&Qt(i[f],n);)f--;let t=w;for(let r=0;r<=f;r++)t=Vt(n,t,jt(n,i[r],Jt(n,e,E(r))));l=t}}else if("ℂ"===c){if(!1===t)break;{const t=Yt(n,o,e),s=Yt(n,Po(o,n),e);u=jt(n,t,s);const a=h;if(h=vo(h,u,n),null==l){let t=w;for(let r=0;r<=f;r++)t=Vt(n,t,jt(n,i[r],Jt(n,e,E(r))));l=t}const c=l;l=Go(c,u,e,n);if(!No(jt(n,l,u),c,n)){Qo(a);const t=Qr(ka,c,n);return r(vo(Wo(),t,n))}for(let t=0;t<=f;t++)i.pop();i.push(...Ro(l,e,n)),f-=2}}}let d=w;for(let t=0;t<=f;t++)d=Vt(n,d,jt(n,i[t],Jt(n,e,E(t))));const p=Qr(ka,d,n);return f>0&&Wt(i[f])?r(vo(Qr((function(t,e){return Rt(e,t)}),h,n),Rt(n,p),n)):r(vo(h,p,n))}function na(t,e,n,r,i,s){const o=Jr.tos,a=Jr.tos;ta(Xo(t[e],s));const u=Jr.tos-a,c=Jr.tos;ta(Xo(t[0],s));const l=Jr.tos-c;for(let n=0;n<u;n++)for(let u=0;u<l;u++){r=Jr.stack[a+n],i=Jr.stack[c+u];const l=Rt(s,Xt(i,r,s)),f=sa(l,t,e,s);if(Qt(f,s))return Yr(o),[!0,r,i];i=Rt(s,i);const h=sa(Rt(s,l),t,e,s);if(Qt(h,s))return Yr(o),[!0,r,i]}return Yr(o),[!1,r,i]}function ra(t,e,n,r){let i,s,o;if(n<=2)return[!1,i];const a=Jr.tos;if(i=$o(Jt(r,l,ko(2,3)),r),s=i,Qo(s),o=sa(s,e,n,r),Qt(o,r))return Yr(a),[!0,i];if(i=$o(Jt(r,y,ko(2,3)),r),s=i,Qo(s),o=sa(s,e,n,r),Qt(o,r))return Yr(a),[!0,i];for(let t=-10;t<=10;t++)for(let s=1;s<=5;s++){i=$o(Vt(r,E(t),jt(r,E(s),d)),r);const o=i;Qo(o);const u=sa(o,e,n,r);if(Qt(u,r))return Yr(a),[!0,i]}return Yr(a),[!1,i]}function ia(t,e,n,r,i){let s=w;for(let o=r;o>0;o--){const r=Xt(n[o],t,i);n[o]=s,s=r,n[o-1]=Yt(i,n[o-1],jt(i,s,e))}n[0]=s}function sa(t,e,n,r){!function(t){const e=function(t){if(Number.isInteger(t))return t;throw new Sr}(t);if(e>=0)return e;throw new Sr}(n);let i=w;for(let s=n;s>=0;s--)i=Vt(r,jt(r,i,t),e[s]);return i}const oa=9007199254740992;function aa(t){return-oa<t&&t<oa}function ua(t){return t>0?Math.floor(t):Math.ceil(t)}const ca=1<<30;function la(t){if(t instanceof b){const e=t.value|BigInt(ca);return e&-e}throw new Error}function fa(t,e){return t=ha(t),e=ha(e),t.lesser(e)?t:e}function ha(t){return"number"==typeof t?function(t){return new b(BigInt(t))}(t):"string"==typeof t?function(t){if(aa(+t)){const e=+t;if(e===ua(e))return new b(BigInt(e));throw new Error("Invalid integer: "+t)}const e="-"===t[0];e&&(t=t.slice(1));const n=t.split(/e/i);if(n.length>2)throw new Error("Invalid integer: "+n.join("e"));if(2===n.length){let e=n[1];"+"===e[0]&&(e=e.slice(1));let r=+e;if(r!==ua(r)||!aa(r))throw new Error("Invalid integer: "+r+" is not a valid exponent.");let i=n[0];const s=i.indexOf(".");if(s>=0&&(r-=i.length-s-1,i=i.slice(0,s)+i.slice(s+1)),r<0)throw new Error("Cannot include negative exponent part for integers");i+=new Array(r+1).join("0"),t=i}if(!/^([0-9][0-9]*)$/.test(t))throw new Error("Invalid integer: "+t);return new b(BigInt(e?"-"+t:t))}(t):"bigint"==typeof t?new b(t):t}const da=[];for(let t=0;t<1e3;t++)da[t]=ha(t),t>0&&(da[-t]=ha(-t));function pa(t,e){return function(t,e){if(t=ha(t).abs(),e=ha(e).abs(),t.equals(e))return t;if(t.isZero())return e;if(e.isZero())return t;let n,r=da[1];for(;t.isEven()&&e.isEven();)n=fa(la(t),la(e)),t=t.divide(n),e=e.divide(n),r=r.multiply(n);for(;t.isEven();)t=t.divide(la(t));do{let n;for(;e.isEven();)e=e.divide(la(e));t.greater(e)&&(n=e,e=t,t=n),e=e.subtract(t)}while(!e.isZero());return r.isUnit()?t:t.multiply(r)}(t,e)}let ma=g(0);function ga(t){if(t.isZero()||t.isOne()||t.isMinusOne())return t;ma=t.a;const e=function(){const t=[];ma.isNegative()&&(ma=Fo(ma),t.push(l));for(let e=0;e<1e4;e++)if(t.push(...xa(e)),0===ma.compare(1))return t;return t.push(...function(){const t=[],e=Mo(1);let n=Mo(5),r=Mo(2),i=1,s=1;for(;;){if(ma.isProbablePrime())return t.push(wa(ma,1)),t;for(;;){let o=r.subtract(n);o=Fo(o);const a=pa(o,ma);if(!a.equals(1)){if(t.push(wa(a,1)),0===a.compare(ma))return t;o=So(ma,a),ma=o,o=Oo(n,ma),n=o,o=Oo(r,ma),r=o;break}0==--i&&(r=n,s*=2,i=s),o=bo(n,n),n=o.add(e),o=Oo(n,ma),n=o}}}()),t}();return e.length,1===e.length?e[0]:e.length>1?Te(is,De(...e)):qe}function xa(t){const e=[];let n;const r=Mo(Gs[t]);let i=0;for(;;){if(0===ma.compare(1))return i&&e.push(wa(r,i)),e;let t;if([n,t]=Array.from(qo(ma,r)),!t.isZero())break;i++,ma=n}return i&&e.push(wa(r,i)),-1===n.compare(r)&&(e.push(wa(ma,1)),ma=Mo(1)),e}function wa(t,e){let n=new m(t,g.one);return e>1&&(n=De(us,n,new m(Mo(e),g.one))),n}function ya(t,e,n,r){const i=Ea(e,n,r);_e(i)&&te(i)?t.push(...i.tail()):t.push(i)}function va(t,e,n){return h(t)&&t.isInteger()?ga(t):Ea(t,e,n)}function Ea(t,e,n){return t.contains(e)&&io(t,e)&&S(e)?ea(t,e,n):t}function ba(t){if(isNaN(t))throw new Error("number too big to factor");const e=[];t<0&&(t=-t);for(let n=0;n<js;n++){const r=Gs[n];if(r>t/r)break;let i=0;for(;t%r==0;)t/=r,i++;i&&(e.push(E(r)),e.push(E(i)))}return t>1&&(e.push(E(t)),e.push(y)),e}function Sa(t,e,n){return ti(Oa,t,e,n)}function Oa(t,e,n){if(t.equals(e))return t;if(h(t)&&h(e))return t.gcd(e);const r=qa(t,e);return r?function(t,e,n,r){let i=Ea(t,n,r),s=Ea(e,n,r);(At(i)||At(s))&&(At(i)||(i=De(is,i,y)),At(s)||(s=De(is,s,y)));if(At(i)&&At(s))return Ma(i,s,r);return _a(i,s,r)}(t,e,r,n):Dt(t)&&Dt(e)?function(t,e,n){if(Ne(t),Ne(e),ni(t)!==ni(e))return y;const r=_e(t)?t.tail().reduce((function(t,e){return Sa(t,e,n)})):Pe(Ae(t)),i=_e(e)?e.tail().reduce((function(t,e){return Sa(t,e,n)})):Pe(Ae(e)),s=Xt(t,r,n),o=Xt(e,i,n);if(s.equals(o))return jt(n,s,Sa(r,i,n));return y}(t,e,n):(Dt(t)&&(t=Fa(t,n)),Dt(e)&&(e=Fa(e,n)),At(t)?function(t,e,n){return t.tail().reduce(((t,r)=>jt(n,t,Sa(r,e,n))),y)}(t,e,n):At(e)?function(t,e,n){return e.tail().reduce(((e,r)=>jt(n,e,Sa(t,r,n))),y)}(t,e,n):At(t)&&At(e)?Ma(t,e,n):_a(t,e,n))}function qa(t,e){const n=no(t,null);return!!n&&(!!no(e,n)&&n)}function Ma(t,e,n){const r=t.rest,i=e.rest;return[...r].reduce(((t,e)=>jt(n,t,[...i].reduce(((t,r)=>jt(n,t,Sa(e,r,n))),y))),y)}function _a(t,e,n){let r,i;if(Pt(t)?(r=Le(t),t=Fe(t)):r=y,Pt(e)?(i=Le(e),e=Fe(e)):i=y,!t.equals(e))return y;if(f(r)&&f(i)){const e=ee(r,i)<0?r:i;return Jt(n,t,e)}const s=Xt(r,i,n);if(f(s)){const e=Fe(r),s=At(r)&&f(e)?e:y,o=Fe(i),a=At(i)&&f(o)?o:y,u=ee(s,a)<0?r:i;return Jt(n,t,u)}const o=Yt(n,r,i);if(!f(o))return y;const a=Nt(o)?r:i;return Jt(n,t,a)}function Fa(t,e){return t.tail().reduce((function(t,n){return Sa(t,n,e)}))}function La(t,e){return Qr(ka,t,e)}function ka(t,e){if(!Dt(t))return t;const n=t.tail().reduce((function(t,n){return Sa(t,n,e)})),r=Gt(n,e),i=function(t,e){return Wr((function(t){return e.valueOf(t)}),t,e)}(t.tail().reduce(((t,n)=>Vt(e,t,vo(r,n,e))),w),e);return Xt(i,r,e)}function Da(t,e){e.pushDirective(Ge.factoring,1);try{return function(t,e){if(x(t))return function(t,e){if(!x(t))return t;const n=t.mapElements((function(t){return Da(t,e)}));return t.withElements(n)}(t,e);if(!Dt(t))return t;const n=function(t,e){if(Dt(t))return t.tail().reduce(((t,n)=>Aa(n,t,e)),y);return Aa(t,y,e)}(t,e);let r=w;_e(t)&&(r=t.tail().reduce(((t,r)=>Vt(e,t,jt(e,n,r))),r));const i=La(r,e),s=Xt(i,n,e);return s}(t,e)}finally{e.popDirective()}}function Aa(t,e,n){return At(t)?t.tail().reduce(((t,e)=>Pa(e,t,n)),e):Pa(t,e,n)}function Pa(t,e,n){if(!Pt(t))return e;const r=t;return t=Le(t),Nt(t)||At(t)&&Nt(Fe(t))?Ta(e,Gt(r,n),n):e}function Ta(t,e,n){return Xt(jt(n,t,e),Sa(t,e,n),n)}function Na(t,e){if(h(t))return t.denom();if(_e(t)&&Dt(t)&&(t=Da(t,e)),_e(t)){const n=t.cdr;if(At(t)&&!kt(Pe(n),e)){const n=function(t,e){const n=function(t){return function(e){return Na(e,t)}}(e),r=t.map(n);return r}(t.tail(),e);return Ht(n,e)}if(Pt(t)&&Wt(t.expo))return Gt(t,e)}return y}function Ia(t,e){const n=t.argList;try{const t=n.head;try{const n=e.valueOf(t);try{return $a(n,e)}finally{n.release()}}finally{t.release()}}finally{n.release()}}function $a(t,e){return _e(t)&&ne(t)&&(t=Da(t,e)),_e(t)&&te(t)&&!kt(Pe(Ae(t)),e)?Ht(t.tail().map((function(t){return $a(t,e)})),e):h(t)?t.numer():_e(t)&&Bt(t)&&Wt(t.expo)?y:t}const Ba=t(e.abs);function Ca(t){return _e(t)&&re(t,Ba)}function Ua(t){return _e(t)&&ne(t)}function Ha(t){return _e(t)&&re(t,$r)}function Ra(t){return _e(t)&&S(t.opr)&&n(t.opr,e.pow)}function za(t){return _e(t)&&re(t,Kr)}function Va(t){return _e(t)&&re(t,Ds)}function Xa(t){return!(!f(t)&&!Zs.equals(t))||!!x(t)&&t.everyElement(Xa)}function Ka(t){return Zs.equals(t)}const Za=a("last"),ja=a("tty");function Ja(t){return Pe(Pe(t))}function Ga(t){return Pe(Ae(t))}function Ya(t){return Ae(Ae(t))}function Qa(t){return Pe(Pe(Ae(t)))}function Wa(t){return Pe(Ae(Ae(t)))}function tu(t){return Pe(Ae(Ae(Ae(t))))}function eu(t){return Pe(Ae(Ae(Ae(Ae(t)))))}function nu(t,e){let n;if(e.getDirective(Ge.printMode)===jr.EcmaScript||e.getDirective(Ge.renderFloatAsEcmaScript))return`${t}`;if(e.getDirective(Ge.forceFixedPrintout)){const r=e.getDirective(Ge.maxFixedPrintoutDigits);n=t.toFixed(r),n=n.replace(/(\.\d*?[1-9])0+$/gm,"$1"),n=n.replace(/\.0+$/gm,""),-1===n.indexOf(".")&&(n+=".0"),parseFloat(n)!==t&&(n=t.toFixed(r)+"...")}else n=""+t,n=e.getDirective(Ge.printMode)===jr.LaTeX?/\d*\.\d*e.*/gm.test(n)?n.replace(/e(.*)/gm,"\\mathrm{e}{$1}"):n.replace(/(\d+)e(.*)/gm,"$1.0\\mathrm{e}{$2}"):/\d*\.\d*e.*/gm.test(n)?n.replace(/e(.*)/gm,"*10^($1)"):n.replace(/(\d+)e(.*)/gm,"$1.0*10^($2)");return n}function ru(t,e,n){if(h(t)){let r="",i=t.a.toString();if(e||"-"===i[0]&&(i=i.substring(1)),n.getDirective(Ge.printMode)===jr.LaTeX&&t.isFraction()&&(i="\\frac{"+i+"}{"),r+=i,t.isFraction()){n.getDirective(Ge.printMode)!==jr.LaTeX&&(r+="/");let e=t.b.toString();n.getDirective(Ge.printMode)===jr.LaTeX&&(e+="}"),r+=e}return r}if(p(t)){const r=nu(t.d,n);return e?r:"-"===r[0]?r.substring(1):r}throw new Error(`print_number(p = ${t})`)}function iu(n,r){if(_e(n)){let t="";for(t+="(",t+=iu(Pe(n),r),n=Ae(n);_e(n);)t+=" ",t+=iu(Pe(n),r),n=Ae(n);return n!==qe&&(t+=" . ",t+=iu(n,r)),t+=")",t}if(!Oe(n)){if(n.isnil){const i=r.handlerFor(n);return Lt(i.dispatch(n,t(e.sexpr),qe,r))}throw new Error}{const i=r.handlerFor(n).dispatch(n,t(e.sexpr),qe,r);try{return Lt(i)}finally{i.release()}}}const su=t(e.E),ou=t(e.IMU),au=t(e.PI),uu=t(e.testeq),cu=t(e.component);function lu(t,e){return function(t){if(h(t)&&(t.isNegative()||t.isFraction()))return!0;if(p(t)&&t.isNegative())return!0;if(Ua(t))return!0;if(Ha(t))return!0;if(Ra(t))return!0;return!1}(t)?`(${du(t,e)})`:du(t,e)}function fu(t,e){return h(t)&&t.isFraction()||Ua(t)||Ha(t)||Ra(t)?`(${du(t,e)})`:du(t,e)}function hu(t,e,n){let r="";const i=Ga(t);let s=Wa(t);return to(s)?(r+=lu(i,n),r):(1===e&&(r+="("),s=Rt(n,s),r+=Ou(i,s,n),1===e&&(r+=")"),r)}function du(t,e){return Ie(t),function(t,e){Ie(t);let n="";if(Ua(t))for(t=Ae(t),"-"===pu(Pe(t))&&(n+="-"),n+=gu(Pe(t),e),t=Ae(t);_e(t);)"+"===pu(Pe(t))?e.getDirective(Ge.printMode)===jr.Human?n+=" + ":n+="+":e.getDirective(Ge.printMode)===jr.Human?n+=" - ":n+="-",n+=gu(Pe(t),e),t=Ae(t);else"-"===pu(t)&&(n+="-"),n+=gu(t,e);return n}(t,e)}function pu(t){if(_e(t)&&Ha(t)){const e=Pe(t.cdr);if(f(e)&&e.isNegative())return"-"}return f(t)&&t.isNegative()?"-":"+"}function mu(t,e){let n="",r="",i=Ae(t);to(Pe(i))&&(i=Ae(i));let s=!1;f(Pe(i))&&(s=!0);let o=!1;e.getDirective(Ge.printMode)===jr.LaTeX&&_e(Ae(i))&&function(t){return h(t)&&t.isFraction()&&Qs(t.a.abs(),1)}(Pe(i))&&(o=!0,n=Pe(i).b.toString());let a="";for(o?(r=a,a=""):a+=xu(Pe(i),!1,!1,e),i=Ae(i);_e(i);)e.getDirective(Ge.printMode)===jr.LaTeX&&s&&Ja(i).equals(us)&&f(Pe(Ae(Pe(i))))&&(po(Pe(Ae(Ae(Pe(i)))))||(a+=" \\cdot ")),a+=Mu(e),a+=xu(Pe(i),!1,!0,e),s=!1,f(Pe(i))&&(s=!0),i=Ae(i);return o&&(a=r+"\\frac{"+a+"}{"+n+"}"),a}function gu(t,e){return Ie(t),_e(t)&&Ha(t)?function(t){let e=t.cdr;for(;_e(e);){if(_u(e.car))return!0;e=e.cdr}return!1}(t)?function(t,e){let n,r,i="",s=0,o=0,a=0,u=t.cdr,c=Pe(u);for(h(c)?(n=c.numer().abs(),r=c.denom(),kt(n,e)||o++,kt(r,e)||a++,u=Ae(u)):(n=y,r=y);_e(u);)c=Pe(u),_u(c)?a++:o++,u=Ae(u);if(e.getDirective(Ge.printMode)===jr.LaTeX&&(i+="\\frac{"),0===o)i+="1";else for(s=0,u=Ae(t),h(Pe(u))&&(u=Ae(u)),kt(n,e)||(i+=qu(n,!1,!1,e),s=1);_e(u);)c=Pe(u),_u(c)||(s&&(i+=Mu(e)),i+=qu(c,!1,!1,e),s=1),u=Ae(u);for(e.getDirective(Ge.printMode)===jr.LaTeX?i+="}{":e.getDirective(Ge.printMode)===jr.Human?i+=" / ":i+="/",a>1&&e.getDirective(Ge.printMode)!==jr.LaTeX&&(i+="("),s=0,u=Ae(t),h(Pe(u))&&(u=Ae(u)),kt(r,e)||(i+=qu(r,!1,!1,e),s=1);_e(u);)c=Pe(u),_u(c)&&(s&&(i+=Mu(e)),i+=hu(c,a,e),s=1),u=Ae(u);return a>1&&e.getDirective(Ge.printMode)!==jr.LaTeX&&(i+=")"),e.getDirective(Ge.printMode)===jr.LaTeX&&(i+="}"),i}(t,e):mu(t,e):xu(t,!1,!1,e)}function xu(t,e,n,r){if(Ie(t),_e(t)&&ie(t)){let e=t.argList;if(_e(e)){let t=yu(e.car,!1,!1,r);for(e=e.cdr;_e(e);)t+=wu(r),t+=yu(Pe(e),!1,!0,r),e=e.cdr;return t}throw new Error}return yu(t,e,n,r)}function wu(t){return t.getDirective(Ge.printMode)===jr.LaTeX?" \\wedge ":t.getDirective(Ge.printMode)===jr.Human?" ^ ":"^"}function yu(t,e,n,r){if(Ie(t),_e(t)&&se(t)){let e=t.argList;if(_e(e)){let t=qu(e.car,!1,!1,r);for(e=e.cdr;_e(e);)t+=vu(r),t+=qu(Pe(e),!1,!0,r),e=e.cdr;return t}throw new Error}if(_e(t)&&function(t){return _e(t)&&re(t,Rr)}(t)){let e=t.argList;if(_e(e)){let t=qu(e.car,!1,!1,r);for(e=e.cdr;_e(e);)t+=Eu(r),t+=qu(Pe(e),!1,!0,r),e=e.cdr;return t}throw new Error}if(_e(t)&&function(t){return _e(t)&&re(t,zr)}(t)){let e=t.argList;if(_e(e)){let t=qu(e.car,!1,!1,r);for(e=e.cdr;_e(e);)t+=bu(r),t+=qu(Pe(e),!1,!0,r),e=e.cdr;return t}throw new Error}return qu(t,e,n,r)}function vu(t){return t.getDirective(Ge.printMode)===jr.LaTeX?" \\mid ":t.getDirective(Ge.printMode)===jr.Human?" | ":"|"}function Eu(t){return t.getDirective(Ge.printMode)===jr.LaTeX?" \\ll ":t.getDirective(Ge.printMode)===jr.Human?" << ":"<<"}function bu(t){return t.getDirective(Ge.printMode)===jr.LaTeX?" \\gg ":t.getDirective(Ge.printMode)===jr.Human?" >> ":">>"}function Su(t,e){let n="";return n+="(",n+=du(t,e),n+=")",n}function Ou(t,e,n){let r="";if(Ut(e))if(Ct(t,2)){if(n.getDirective(Ge.printMode)===jr.EcmaScript)return r+="Math.SQRT2",r}else{if(n.getDirective(Ge.printMode)===jr.LaTeX)return r+="\\sqrt{",r+=du(t,n),r+="}",r;if(n.getDirective(Ge.printMode)===jr.EcmaScript)return r+="Math.sqrt(",r+=du(t,n),r+=")",r}if(Ct(n.getBinding(cs,qe),1)&&ue(t))return n.getDirective(Ge.printMode)===jr.EcmaScript?(r+="Math.exp(",r+=fu(e,n),r+=")",r):(n.getDirective(Ge.printMode)===jr.LaTeX?(r+="e^{",r+=du(e,n),r+="}"):(r+="exp(",r+=du(e,n),r+=")"),r);if(n.getDirective(Ge.printMode)===jr.EcmaScript)return r+="Math.pow(",r+=lu(t,n),r+=", ",r+=fu(e,n),r+=")",r;if(function(t,e){if(S(t)){if(t.equals(a("x"))){const t=ls,n=e.getBinding(t,qe);return!(!n.equals(t)&&!n.isnil&&Ct(n,1))}return!0}return!0}(t,n)){if(!ue(t)){if(f(e)&&e.isMinusOne())return n.getDirective(Ge.printMode)===jr.LaTeX?r+="\\frac{1}{":n.getDirective(Ge.printMode)===jr.Human?r+="1 / ":r+="1/",_e(t)&&n.getDirective(Ge.printMode)!==jr.LaTeX?(r+="(",r+=du(t,n),r+=")"):r+=du(t,n),n.getDirective(Ge.printMode)===jr.LaTeX&&(r+="}"),r;if(Wt(e)){n.getDirective(Ge.printMode)===jr.LaTeX?r+="\\frac{1}{":n.getDirective(Ge.printMode)===jr.Human?r+="1 / ":r+="1/";const i=Rt(n,e);return _e(t)&&n.getDirective(Ge.printMode)!==jr.LaTeX?(r+="(",r+=Ou(t,i,n),r+=")"):r+=Ou(t,i,n),n.getDirective(Ge.printMode)===jr.LaTeX&&(r+="}"),r}}if(po(e)&&n.getDirective(Ge.printMode)===jr.LaTeX){r+="\\sqrt";const i=Na(e,n);return le(i)||(r+="[",r+=du(i,n),r+="]"),r+="{",r+=Ou(t,e=$a(e,n),n),r+="}",r}}return n.getDirective(Ge.printMode)===jr.LaTeX&&kt(e,n)?r+=du(t,n):(Ra(t)?n.getDirective(Ge.printMode)!==jr.LaTeX?(r+="(",r+=qu(t,!0,!1,n),r+=")"):r+=qu(t,!0,!1,n):Nt(t)||Ua(t)?(r+="(",r+=du(t,n),r+=")"):Ha(t)?n.getDirective(Ge.printMode)!==jr.LaTeX?(r+="(",r+=qu(t,!0,!1,n),r+=")"):r+=qu(t,!0,!1,n):ie(t)||se(t)?(r+="(",r+=du(t,n),r+=")"):f(t)&&(oe(t,w)||po(t))?(r+="(",r+=qu(t,!1,!1,n),r+=")"):r+=qu(t,!1,!1,n),n.getDirective(Ge.printMode)===jr.Human?n.getDirective(Ge.useCaretForExponentiation)?r+="^":r+="**":n.getDirective(Ge.printMode)===jr.LaTeX||n.getDirective(Ge.useCaretForExponentiation)?r+="^":r+="**",n.getDirective(Ge.printMode)===jr.LaTeX?du(e,n).length>1?(r+="{",r+=du(e,n),r+="}"):r+=du(e,n):_e(e)||po(e)||f(e)&&oe(e,w)?(r+="(",r+=du(e,n),r+=")"):r+=qu(e,!1,!1,n)),r}function qu(r,i=!1,s=!1,o){Ie(r);const a=i;if(f(r)){if(s&&oe(r,w)){let t="";return t+="(",t+=ru(r,s,o),t+=")",t}return ru(r,s,o)}if(Oe(r)){const n=o.handlerFor(r);switch(o.getDirective(Ge.printMode)){case jr.Human:return Lt(n.dispatch(r,t(e.human),qe,o));case jr.Infix:{const i=n.dispatch(r,t(e.infix),qe,o);if(O(i))return Lt(i);throw q(i)?i:new Sr}case jr.LaTeX:return Lt(n.dispatch(r,t(e.latex),qe,o));case jr.SExpr:return Lt(n.dispatch(r,t(e.sexpr),qe,o));default:throw new Error(`${o.getDirective(Ge.printMode)}`)}}if(_e(r)&&Ha(r)){let t="";return a||"-"!==pu(r)&&o.getDirective(Ge.printMode)===jr.LaTeX||(o.getDirective(Ge.printMode)===jr.LaTeX?t+=" \\left (":t+="("),t+=du(r,o),a||"-"!==pu(r)&&o.getDirective(Ge.printMode)===jr.LaTeX||(o.getDirective(Ge.printMode)===jr.LaTeX?t+=" \\right ) ":t+=")"),t}if(Ua(r)){let t="";return a||(t+="("),t+=du(r,o),a||(t+=")"),t}if(Ra(r))return Ou(r.base,r.expo,o);if(_e(r)){const t=r.opr,e=r.argList;try{if(t.equals(zi)){let t="";const n=e.item(1);try{if(o.getDirective(Ge.printMode)!==jr.EcmaScript){const n=e.item(0);try{t+="fn ";t+=iu(n,o),t+=" -> "}finally{n.release()}}return t+=du(n,o),t}finally{n.release()}}else if(t.equals(Xi)){let t="";const n=e.item(0);try{if(o.getDirective(Ge.printMode)!==jr.EcmaScript){const n=e.item(1);try{t+="function ";t+=iu(n,o),t+=" -> "}finally{n.release()}}return t+=du(n,o),t}finally{n.release()}}}finally{t.release(),e.release()}}if(Pe(r).equals(as)){let t="";return t+=du(Qa(r),o),o.getDirective(Ge.printMode)===jr.LaTeX?t+=" \\rightarrow ":o.getDirective(Ge.printMode)===jr.Human?t+=" -> ":t+="->",t+=du(Pe(Ae(Ga(r))),o),t}if(Pe(r).equals(cu)&&S(Ga(r))){let t="";return t+=function(t,e){let n="";if(Ja(t=Ae(t)).equals(ii)||Ja(t).equals(is)||Ja(t).equals(us)||Ja(t).equals(Ui)?n+=Su(Pe(t),e):n+=du(Pe(t),e),n+="[",t=Ae(t),_e(t))for(n+=du(Pe(t),e),t=Ae(t);_e(t);)n+=",",n+=du(Pe(t),e),t=Ae(t);return n+="]",n}(r,o),t}if(_e(r)&&za(r))return function(t,e){let n="";return po(t=Ga(t))||Ua(t)||Ha(t)||Ra(t)||za(t)?n+=Su(t,e):n+=du(t,e),n+="!",n}(r,o);if(_e(r)&&Ca(r))switch(o.getDirective(Ge.printMode)){case jr.Human:case jr.Infix:return function(t,e){let n="";return n+="abs(",n+=du(t.argList.head,e),n+=")",n}(r,o);case jr.LaTeX:return function(t,e){let n="";return n+="\\left |",n+=du(t.argList.head,e),n+=" \\right |",n}(r,o);default:throw new Sr}else{if(Pe(r).equals(Es)&&o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="";return n+="\\sqrt{",n+=du(Ga(t),e),n+="} ",n}(r,o),t}if(Va(r)){if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="";return n+="{",_e(Ga(t))&&(n+="("),n+=du(Ga(t),e),_e(Ga(t))&&(n+=")"),n+="}",n+="^T",n}(r,o),t}if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="";return n+="transpose(",n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(As)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="";return n+="identity(",n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(_e(r)&&ae(r)){if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="";return n+="{",_e(Ga(t))&&(n+="("),n+=du(Ga(t),e),_e(Ga(t))&&(n+=")"),n+="}",n+="^{-1}",n}(r,o),t}if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="";return n+="inv(",n+=du(Ga(t),e),n+=")",n}(r,o),t}}else{if(Pe(r).equals(mi)&&o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="";return n+="\\binom{",n+=du(Ga(t),e),n+="}{",n+=du(Wa(t),e),n+="} ",n}(r,o),t}if(Pe(r).equals(Mi)&&o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="";const r=Pe(Ae(t)),i=t=Ae(t);let s=0;for(;_e(Ae(Ae(t)));){s++;const r=Ae(Ae(t));n+="\\int^{",n+=du(Pe(Ae(r)),e),n+="}_{",n+=du(Pe(r),e),n+="} \\! ",t=Ae(r)}n+=du(r,e),n+=" \\,",t=i;for(let r=1;r<=s;r++){const i=Ae(t);n+=" \\mathrm{d} ",n+=du(Pe(i),e),r<s&&(n+=" \\, "),t=Ae(Ae(i))}return n}(r,o),t}if(se(r)){if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="";return n+=du(Ga(t),e),n+=" \\cdot ",n+=du(Wa(t),e),n}(r,o),t}if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="dot(";return n+=du(Ga(t),e),n+=", ",n+=du(Wa(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(ys)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="Math.sin(";return n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(Oi)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="Math.cos(";return n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(Ss)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="Math.tan(";return n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(ci)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="Math.asin(";return n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(ai)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="Math.acos(";return n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(fi)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="Math.atan(";return n+=du(Ga(t),e),n+=")",n}(r,o),t}}else if(Pe(r).equals(bs)){if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="\\sum_{";return n+=du(Wa(t),e),n+="=",n+=du(tu(t),e),n+="}^{",n+=du(eu(t),e),n+="}{",n+=du(Ga(t),e),n+="}",n}(r,o),t}if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){const n=Ga(t),r=Wa(t),i=tu(t),s=eu(t);return"(function(){ var "+du(r,e)+"; var holderSum = 0; var lowerlimit = "+du(i,e)+"; var upperlimit = "+du(s,e)+"; for ("+du(r,e)+" = lowerlimit; "+du(r,e)+" < upperlimit; "+du(r,e)+"++) { holderSum += "+du(n,e)+"; } return holderSum;})()"}(r,o),t}}else if(Pe(r).equals(fs)){if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="\\prod_{";return n+=du(Wa(t),e),n+="=",n+=du(tu(t),e),n+="}^{",n+=du(eu(t),e),n+="}{",n+=du(Ga(t),e),n+="}",n}(r,o),t}if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){const n=Ga(t),r=Wa(t),i=tu(t),s=eu(t);return"(function(){ var "+du(r,e)+"; var holderProduct = 1; var lowerlimit = "+du(i,e)+"; var upperlimit = "+du(s,e)+"; for ("+du(r,e)+" = lowerlimit; "+du(r,e)+" < upperlimit; "+du(r,e)+"++) { holderProduct *= "+du(n,e)+"; } return holderProduct;})()"}(r,o),t}}else if(Pe(r).equals(Vi)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){const n=Ga(t),r=Wa(t),i=tu(t),s=eu(t);return"(function(){ var "+r+"; var lowerlimit = "+du(i,e)+"; var upperlimit = "+du(s,e)+"; for ("+r+" = lowerlimit; "+r+" < upperlimit; "+r+"++) { "+du(n,e)+" } })()"}(r,o),t}}else if(Pe(r).equals(ki)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="";for(t=Ae(t);_e(t);)n+=du(Pe(t),e),t=Ae(t);return n}(r,o),t}}else if(Pe(r).equals(Ms)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="(function(){";t=Ae(t);let r=0;for(;_e(t);){if(Ae(t).isnil){n+="else {",n+="return ("+du(Pe(t),e)+");,$",n+="}";break}r&&(n+=" else "),n+="if ("+du(Pe(t),e)+"){,$",n+="return ("+du(Ga(t),e)+");,$",n+="}",r++,t=Ya(t)}return n+="})()",n}(r,o),t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="\\left\\{ \\begin{array}{ll}";for(t=Ae(t);_e(t);){if(Ae(t).isnil){n+="{",n+=du(Pe(t),e),n+="} & otherwise ",n+=" \\\\\\\\";break}n+="{",n+=du(Ga(t),e),n+="} & if & ",n+=du(Pe(t),e),n+=" \\\\\\\\",t=Ya(t)}return n=n.substring(0,n.length-4),n+"\\end{array} \\right."}(r,o),t}}else if(Pe(r).equals(ks)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="(("+du(Ga(r),o)+") < ("+du(Wa(r),o)+"))",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="{";return n+=du(Ga(t),e),n+="}",n+=" < ",n+="{",n+=du(Wa(t),e),n+"}"}(r,o),t}}else if(Pe(r).equals(Ls)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="(("+du(Ga(r),o)+") <= ("+du(Wa(r),o)+"))",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="{";return n+=du(Ga(t),e),n+="}",n+=" \\leq ",n+="{",n+=du(Wa(t),e),n+"}"}(r,o),t}}else if(Pe(r).equals(Fs)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="(("+du(Ga(r),o)+") > ("+du(Wa(r),o)+"))",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="{";return n+=du(Ga(t),e),n+="}",n+=" > ",n+="{",n+=du(Wa(t),e),n+"}"}(r,o),t}}else if(Pe(r).equals(_s)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="(("+du(Ga(r),o)+") >= ("+du(Wa(r),o)+"))",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="{";return n+=du(Ga(t),e),n+="}",n+=" \\geq ",n+="{",n+=du(Wa(t),e),n+"}"}(r,o),t}}else if(_e(r)&&r.opr.equals(uu)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="(("+du(Ga(r),o)+") === ("+du(Wa(r),o)+"))",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=function(t,e){let n="{";return n+=du(t.lhs,e),n+="}",n+=" = ",n+="{",n+=du(t.rhs,e),n+"}"}(r,o),t}}else if(Pe(r).equals(Ri)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="Math.floor("+du(Ga(r),o)+"),$",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=" \\lfloor {"+du(Ga(r),o)+"} \\rfloor ,$",t}}else if(Pe(r).equals(gi)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="Math.ceiling("+du(Ga(r),o)+"),$",t}if(o.getDirective(Ge.printMode)===jr.LaTeX){let t="";return t+=" \\lceil {"+du(Ga(r),o)+"} \\rceil ,$",t}}else if(Pe(r).equals(gs)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+="Math.round("+du(Ga(r),o)+"),$",t}}else if(Pe(r).equals(xs)){if(o.getDirective(Ge.printMode)===jr.EcmaScript){let t="";return t+=function(t,e){let n="";return n+=du(Ga(t),e),n+=" = ",n+=du(Wa(t),e),n+="; ",n}(r,o),t}{let t="";return t+=du(Ga(r),o),t+="=",t+=du(Wa(r),o),t}}}}return function(r,i,s){if(Ie(r),_e(r)){let t="";if(t+=qu(r.car,!1,!1,s),(r=r.cdr).isnil)return t;if(i||(t+="("),_e(r))for(t+=du(Pe(r),s),r=Ae(r);_e(r);)t+=",",t+=du(Pe(r),s),r=Ae(r);return i||(t+=")"),t}if(Oe(r))if(q(r));else if(!_t(r))throw new Sr(`${r}`);if(M(r)){let t="";return t+=r.toString(),t}if(_(r)){let t="";return t+=r.toString(),t}if(S(r)&&n(r,e.derivative))return"d";if(ue(r))return s.getDirective(Ge.printMode)===jr.EcmaScript?"Math.E":s.getDirective(Ge.printMode)===jr.LaTeX?"e":s.getSymbolPrintName(su);if(ce(r))return s.getDirective(Ge.printMode)===jr.LaTeX?"\\pi":s.getSymbolPrintName(au);if(S(r)){const n=s.handlerFor(r);return s.getDirective(Ge.printMode)===jr.Infix?Lt(n.dispatch(r,t(e.infix),qe,s)):s.getDirective(Ge.printMode)===jr.LaTeX?Lt(n.dispatch(r,t(e.latex),qe,s)):r.key()}if(F(r))return r.printname;if(L(r)){const n=s.handlerFor(r);return s.getDirective(Ge.printMode)===jr.Infix?Lt(n.dispatch(r,t(e.infix),qe,s)):s.getDirective(Ge.printMode)===jr.LaTeX?Lt(n.dispatch(r,t(e.latex),qe,s)):r.key()}if(q(r)){const n=s.handlerFor(r);return Lt(n.dispatch(r,t(e.infix),qe,s))}if(k(r))return s.getDirective(Ge.printMode)===jr.LaTeX?"i":s.getSymbolPrintName(ou);if(Oe(r)){switch(s.getDirective(Ge.printMode)){case jr.Ascii:case jr.Human:case jr.Infix:case jr.LaTeX:case jr.SExpr:}throw new Sr(`${r}: ${r.type}`)}if(r.isnil)return s.getSymbolPrintName(t(e.NIL));throw new Sr(`${r} ??? is_cons: ${_e(r)} is_atom: ${Oe(r)} is_nil: ${Me(r)}`)}(r,a,o)}function Mu(t){return t.getDirective(Ge.printMode)===jr.LaTeX?"":t.getDirective(Ge.printMode)===jr.Human?" ":"*"}function _u(t){if(_e(t)&&Ra(t)){const e=t.cdr;if(_e(e)){const t=e.car;if(!ue(t)){const t=e.cdr;if(_e(t)){const e=t.car;return Wt(e)}}}}return!1}function Fu(t,e){e.pushDirective(Ge.printMode,jr.Infix);try{return du(t,e).replace(/_/g,"\\_")}finally{e.popDirective()}}function Lu(t){if(u(t))return t.isTrue();if(h(t)){if(t.isOne())return!0;if(t.isZero())return!1;throw new Sr(`${t}`)}throw new Sr(`${t}`)}function ku(t){if(t instanceof Du)return t.context;throw new Sr((new Error).stack)}class Du{context;#n=1;constructor(t){if(!t)throw new Sr("ctxt MUST be defined.");this.context=t,t.addRef()}addRef(){this.#n++}release(){this.#n--,0===this.#n&&this.context.release()}hasState(t){return this.context.hasState(t)}getState(t){return this.context.getState(t)}setState(t,e){this.context.setState(t,e)}handlerFor(t){return this.context.handlerFor(t)}addAtomListener(t){throw new Error("Method not implemented.")}removeAtomListener(t){throw new Error("Method not implemented.")}getCellHost(){throw new Error("Method not implemented.")}setCellHost(t){throw new Error("Method not implemented.")}getProlog(){throw new Error("Method not implemented.")}getPrintHandler(){throw new Error("Method not implemented.")}setPrintHandler(t){throw new Error("Method not implemented.")}abs(n){const r=De(t(e.arg),n);try{return this.context.valueOf(r)}finally{r.release()}}algebra(t,e){throw new Error("Method not implemented.")}add(...n){const r=De(t(e.add),...n);try{return this.context.valueOf(r)}finally{r.release()}}arccos(n){const r=De(t(e.arccos),n);try{return this.context.valueOf(r)}finally{r.release()}}arcsin(n){const r=De(t(e.arcsin),n);try{return this.context.valueOf(r)}finally{r.release()}}arctan(n){const r=De(t(e.arctan),n);try{return this.context.valueOf(r)}finally{r.release()}}arg(n){const r=De(t(e.arg),n);try{return this.context.valueOf(r)}finally{r.release()}}clock(n){const r=De(t(e.clock),n);try{return this.context.valueOf(r)}finally{r.release()}}conj(n){const r=De(t(e.conj),n);try{return this.context.valueOf(r)}finally{r.release()}}cos(n){const r=De(t(e.cos),n);try{return this.context.valueOf(r)}finally{r.release()}}clearBindings(){throw new Error("Method not implemented.")}clearOperators(){throw new Error("Method not implemented.")}compareFn(t){return this.context.compareFn(t)}component(n,r){const i=De(t(e.component),n,r);try{return this.context.valueOf(i)}finally{i.release()}}defineEvalFunction(t,e){throw new Error("Method not implemented.")}defineFunction(t,e){throw new Error("Method not implemented.")}defineStackFunction(t,e){throw new Error("Method not implemented.")}defineExtension(t){throw new Error("Method not implemented.")}defineUserSymbol(t){throw new Error("Method not implemented.")}derivedEnv(){throw new Error("Method not implemented.")}divide(n,r){const i=De(t(e.divide),n,r);try{return this.context.valueOf(i)}finally{i.release()}}equals(n,r){const i=De(t(e.testeq),n,r);try{const t=ku(this).valueOf(i);try{return Lu(t)}finally{t.release()}}finally{i.release()}}evaluate(t,...e){throw new Error("Method not implemented.")}exp(n){const r=De(t(e.exp),n);try{return this.context.valueOf(r)}finally{r.release()}}factor(n){const r=De(t(e.factor),n);try{return this.context.valueOf(r)}finally{r.release()}}factorize(t,e){throw new Error("Method not implemented.")}float(n){const r=De(t(e.float),n);try{return this.context.valueOf(r)}finally{r.release()}}getDirective(t){return this.context.getDirective(t)}getSymbolPredicates(t){return{algebraic:!1,antihermitian:!1,commutative:!1,complex:!1,extended_negative:!1,extended_nonnegative:!1,extended_nonpositive:!1,extended_nonzero:!1,extended_positive:!1,finite:!1,hermitian:!1,hypercomplex:!1,hyperreal:!1,imaginary:!1,infinite:!1,infinitesimal:!1,integer:!1,irrational:!1,negative:!1,noninteger:!1,nonnegative:!1,nonpositive:!1,nonzero:!1,positive:!1,rational:!1,real:!1,transcendental:!1,zero:!1}}getSymbolPrintName(t){throw new Error("Method not implemented.")}getSymbolUsrFunc(t){throw new Error("Method not implemented.")}getSymbolsInfo(){throw new Error("Method not implemented.")}buildOperators(){throw new Error("Method not implemented.")}im(n){const r=De(t(e.imag),n);try{return this.context.valueOf(r)}finally{r.release()}}inner(n,r){const i=De(t(e.inner),n,r);try{return this.context.valueOf(i)}finally{i.release()}}is(t,e){throw new Error("Method not implemented.")}iscomplex(n){const r=De(t(e.iscomplex),n);try{const t=this.context.valueOf(r);try{return Lu(t)}finally{t.release()}}finally{r.release()}}isExpanding(){return this.context.getDirective(Ge.expanding)>0}isFactoring(){throw new Error("Method not implemented.")}isimag(n){const r=De(t(e.isimag),n);try{const t=this.context.valueOf(r);try{return Lu(t)}finally{t.release()}}finally{r.release()}}isinfinite(n){const r=De(t(e.isinfinite),n);try{const t=this.context.valueOf(r);try{return Lu(t)}finally{t.release()}}finally{r.release()}}isinfinitesimal(n){const r=De(t(e.isinfinitesimal),n);try{const t=this.context.valueOf(r);try{return Lu(t)}finally{t.release()}}finally{r.release()}}isminusone(t){throw new Error("Method not implemented.")}isnegative(t){th