@mathzetta/inlinemath 0.4.1 → 0.4.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,2 +1,2 @@
1
- /* Inline Math 0.4.1 — MIT license; includes mathzetta (and @mathzetta/cas, for the CAS) */
2
- const e=-1,t={"+":{prec:50,assoc:"left",arity:2,fixity:"infix"},"-":{prec:50,assoc:"left",arity:2,fixity:"infix"},"±":{prec:50,assoc:"left",arity:2,fixity:"infix"},"*":{prec:70,assoc:"left",arity:2,fixity:"infix"},"/":{prec:70,assoc:"left",arity:2,fixity:"infix"},"^":{prec:80,assoc:"right",arity:2,fixity:"infix"},mod:{prec:45,assoc:"left",arity:2,fixity:"infix"},neg:{prec:75,assoc:"none",arity:1,fixity:"prefix"},pm:{prec:75,assoc:"none",arity:1,fixity:"prefix"},"!":{prec:90,assoc:"none",arity:1,fixity:"postfix"},_:{prec:100,assoc:"none",arity:2,fixity:"infix"},sqrt:s(1),abs:s(1),floor:s(1),ceil:s(1),det:s(1),ln:s(1),loglog:s(1),logloglog:s(1),expm1:s(1),sin:s(1),cos:s(1),tan:s(1),csc:s(1),sec:s(1),cot:s(1),sinh:s(1),cosh:s(1),tanh:s(1),arcsin:s(1),arccos:s(1),arctan:s(1),arcsec:s(1),arccsc:s(1),arccot:s(1),arcsinh:s(1),arccosh:s(1),arctanh:s(1),random:s(0),log:s(2),root:s(2),atan2:s(2),round:s(2),fixed:s(2),perm:s(2),comb:s(2),der:s(2),evaluated:s(4),setbuilder:s(2),max:s(e),min:s(e),gcd:s(e),mean:s(e),sum:s(e),prod:s(e),int:s(e),lim:s(e),"\\sumidx":s(e),"\\prodidx":s(e),pmatrix:s(e),bmatrix:s(e),vmatrix:s(e),"()":n(),"[]":n(),"{}":n(),"(]":n(),"[)":n(),"=":{prec:30,assoc:"left",arity:2,fixity:"infix"},"===":{prec:30,assoc:"left",arity:2,fixity:"infix"},"≠":{prec:30,assoc:"left",arity:2,fixity:"infix"},"≠=":{prec:30,assoc:"left",arity:2,fixity:"infix"},"<":{prec:34,assoc:"left",arity:2,fixity:"infix"},">":{prec:34,assoc:"left",arity:2,fixity:"infix"},"≤":{prec:34,assoc:"left",arity:2,fixity:"infix"},"≥":{prec:34,assoc:"left",arity:2,fixity:"infix"},"∈":{prec:32,assoc:"left",arity:2,fixity:"infix"},"∉":{prec:32,assoc:"left",arity:2,fixity:"infix"},"∪":{prec:33,assoc:"left",arity:2,fixity:"infix"},"∩":{prec:33,assoc:"left",arity:2,fixity:"infix"},"≈":{prec:30,assoc:"left",arity:2,fixity:"infix"},"≡":{prec:30,assoc:"left",arity:2,fixity:"infix"},pmod:{prec:2,assoc:"left",arity:2,fixity:"infix"},"→":{prec:38,assoc:"left",arity:2,fixity:"infix"},"\\not":{prec:85,assoc:"none",arity:1,fixity:"prefix"},"&":{prec:20,assoc:"left",arity:2,fixity:"infix"},"\\xor":{prec:19,assoc:"left",arity:2,fixity:"infix"},"|":{prec:18,assoc:"left",arity:2,fixity:"infix"},"\\nor":{prec:17,assoc:"left",arity:2,fixity:"infix"},"\\nand":{prec:17,assoc:"left",arity:2,fixity:"infix"},"\\implies":{prec:15,assoc:"right",arity:2,fixity:"infix"},"\\iff":{prec:14,assoc:"left",arity:2,fixity:"infix"},"?:":{prec:10,assoc:"right",arity:3,fixity:"infix"}};function s(e){return{prec:100,assoc:"none",arity:e,fixity:"prefix"}}function n(){return{prec:5,assoc:"left",arity:e,fixity:"none",group:!0}}const r="\\:",i=e=>"string"==typeof e&&e.startsWith(r)&&e.length>2;function o(e){return Object.prototype.hasOwnProperty.call(t,e)?t[e]:i(e)?a:void 0}const a=s(e),l=e=>o(e)?.prec;function c(e,t,s){if("mod"===e&&t)return l(t)<=70;if("mod"===t&&["*","/"].includes(e))return!0;if("mod"===t&&["+","-","±"].includes(e))return"right"===s;const n=l(e),r=l(t);if(!n||!r)return!1;if(r!==n)return r<n;const i=o(e).assoc;return"left"===i?"right"===s:"right"===i&&"left"===s}const h=100,u="",p="",f={[u]:"dot",[p]:"cross"},d="",g={ln:h,log:h,loglog:h,logloglog:h,sqrt:h,sqrtpow:h,pow:h,expm1:h,der:h,lim:h,int:h,round:h,fixed:h,floor:h,ceil:h,abs:h,random:h,sum:h,prod:h,"\\sumidx":h,"\\prodidx":h,perm:h,comb:h,"\\perm":h,"\\comb":h,"\\binom":h,max:h,min:h,gcd:h,mean:h,pmatrix:h,bmatrix:h,vmatrix:h,det:h,sin:h,cos:h,tan:h,csc:h,sec:h,cot:h,sinh:h,cosh:h,tanh:h,evaluated:h,setbuilder:h,arcsin:h,arccos:h,arctan:h,arcsec:h,arccsc:h,arccot:h,arcsinh:h,arccosh:h,arctanh:h,atan2:h,_:h,"!":90,"\\not":85,"^":80,"*":70,"/":70,frac:70,[u]:70,[p]:70,mod:45,"+":50,"-":50,"±":50,"→":38,"<":34,">":34,"≤":34,"≥":34,"∈":32,"∉":32,"∪":33,"∩":33,"=":30,"==":30,"===":30,"≠":30,"≠=":30,"!==":30,"≈":30,"≡":30,pmod:2,"&":20,"\\xor":19,"|":18,"∨":18,"\\nor":17,"\\nand":17,"\\implies":15,"\\iff":14,"?:":10,"?":9,",":5},m=e=>Object.prototype.hasOwnProperty.call(g,e),$=new Set(["^","?:","?","log"]),x=["(","{","["],y=[")","}","]"];function b(e,t=!1){return"string"==typeof e&&(t?v:w).test(e)}const w=/^(?:\d+\.?\d*|\.\d+|\d*\.\d+\.\.)$/,v=/^-?(?:\d+\.?\d*|\.\d+|\d*\.\d+\.\.)$/;for(const e of[g])Object.setPrototypeOf(e,null);const k=e=>({type:"number",value:String(e)}),A=e=>({type:"symbol",value:e}),E=(e,t)=>({type:"op",op:e,args:t}),_=['"',"'","`"];function M(e){return b(e,!0)?k(e):_.includes(e[0])?{type:"string",value:e}:A(e)}const O={"0-":"neg",sqrtpow:"root",pow:"^","==":"=","!==":"≠="};function q(t){if(!Array.isArray(t)||0===t.length)throw new Error("Empty postfix");const s=[],n=[];if(t.forEach((t,r)=>{const a=O[t]??String(t),l=o(a);if(!l)return s.push({...M(a),id:r}),void n.push(1);let c=l.arity;if(c===e){const e=s.pop();if(n.pop(),c=e&&"number"===e.type?Number(e.value):NaN,!Number.isInteger(c)||c<0)throw new Error(`'${a}' takes a variable number of arguments, so it must follow a count`);if(0===c&&!l.group&&!i(a))throw new Error(`'${a}' needs at least one argument`)}if(s.length<c)throw new Error(`'${a}' needs ${c} operand${1===c?"":"s"}`);const h=1+Math.max(0,...n.splice(n.length-c,c));if(h>1e3)throw new Error("Nested too deeply (more than 1000 levels)");n.push(h),s.push({...E(a,s.splice(s.length-c,c)),id:r})}),1!==s.length)throw new Error(`Postfix leaves ${s.length} values instead of 1`);return s[0]}function S(t,s){const n=[];return function t(r){"op"===r.type?(r.args.forEach(t),o(r.op)?.arity===e&&n.push(String(r.args.length)),s&&r.written&&(s[n.length]=r.written),n.push(r.op)):n.push(r.value)}(t),n}function z(e){if(Array.isArray(e))return q(e);if(e&&"object"==typeof e&&e.type)return e;throw new Error("Expected a postfix array or an expression tree")}function N(e,t,s=null){t(e,s),"op"===e.type&&e.args.forEach(s=>N(s,t,e))}function L(e){let t="";return N(e,e=>{!t&&"symbol"===e.type&&1===e.value.length&&/[a-zA-Zʹ-Ͽ]/.test(e.value)&&(t=e.value)}),t}for(const e of[O])Object.setPrototypeOf(e,null);function Z(e){const t=new Map;for(const[s,n,r]of e)if(s&&""!==s.trim()&&(t.set(s,n),!r))for(const e of R(s))t.get(e)||t.set(e,n);const s=new Map;for(const e of t.keys()){let t=s;for(let s=0;s<e.length;s++)t.has(e[s])||t.set(e[s],new Map),t=t.get(e[s]);t.end=!0}return e=>{let n="",r=0;for(;r<e.length;){const i=C(s,e,r);i?(n+=t.get(e.slice(r,r+i)),r+=i):n+=e[r++]}return n}}function C(e,t,s){let n=e,r=0;for(let e=s;e<t.length&&(n=n.get(t[e]),n);e++)n.end&&(r=e-s+1);return r}function R(e){const t=new Set([e.toLowerCase(),e.toUpperCase(),I(e)]);return t.delete(e),t}function I(e){let t="",s=!1;for(const n of e){const e=n>="a"&&n<="z",r=n>="A"&&n<="Z";t+=!s&&e?n.toUpperCase():n,s=e||r}return t}const j=[["Math.atan2","atan2"],["Math.atan2(","atan2("],["atan2","atan2"],["\\atan2","atan2"],["Math.random()","random(1)"],["random.random()","random(1)"],["Math.log2(","log_{2}("],["Math.log10(","log_{10}("],["Math.log2","log_{2}"],["Math.log10","log_{10}"],["Math.log_{","log_{"],["Math.log","log_{e}"],["Math.ln","log_{e}"],["Math.abs(","abs("],["Math.fabs(","abs("],["Math.gcd(","gcd("],["Math.comb(","comb("],["Math.perm(","perm("],["Math.fmod(","mod("],["Math.exp(","(e)^("],["Math.exp2(","(2)^("],["Math.expm1(","expm1("],["Math.pow(","pow("],["**","^"],["Math.floor(","floor("],["Math.ceil(","ceil("],["Math.round(","round("],["Math.max(","max("],["Math.min(","min("],["Math.pi","π"],["Math.PI","π"],["math.Pi","π"],["Math.e","e"],["math.E","e"],["Math.sqrt2","sqrt(2)"],["Math.sqrt(","sqrt("],["Math.SQRT1_2","sqrt(1/2)"],["Math.SQRT1_{2}","sqrt(1/2)"],["Math.SQRT2","sqrt(2)"],["cbrt","sqrt[3]"],["Math.cbrt","sqrt[3]"],["Sqrt","sqrt"],["Root","sqrt"],["\\sqrt{","sqrt{"],["\\frac","frac"],["\\dfrac","frac"],["\\tfrac","frac"],["\\cfrac","frac"],["\\lim","lim"],["\\sum","\\sumidx"],["\\prod","\\prodidx"],["\\displaystyle",""],["\\textstyle",""],["\\,",""],["\\;",""],["\\!",""],["\\quad",""],["\\qquad",""],["\\lvert","|"],["\\rvert","|"],["\\vert","|"],["^\\prime","′"],["^{\\prime}","′"],["^{\\prime\\prime}","′′"],["\\prime","′"],["\\approx","≈"],["\\varepsilon","ε"],["\\vartheta","θ"],["\\varphi","φ"],["\\cup","∪"],["\\mathbb{R}","ℝ",!0],["\\mathbb{N}","ℕ",!0],["\\mathbb{Z}","ℤ",!0],["\\mathbb{Q}","ℚ",!0],["\\mathbb{C}","ℂ",!0],["\\cap","∩"],["\\%","%"],["\\barwedge","\\nand"],["∧","&"],["¬","!"],["↑","\\nand"],["↓","\\nor"],["∫","\\int"],["∑","\\sum"],["∏","\\prod"],["asin(","arcsin("],["acos(","arccos("],["atan(","arctan("],["asinh(","arcsinh("],["acosh(","arccosh("],["atanh(","arctanh("],["asec(","arcsec("],["acsc(","arccsc("],["acot(","arccot("],["\\sqrt*","sqrt"],["Log","log"],["Max","max"],["Min","min"],["Floor","floor"],["Ceil","ceil"],["Round","round"],["Abs","abs"],["Sum","sum"],["Prod","prod"],["Mean","mean"],["Avg","mean"],["Average","mean"],["Gcd","gcd"],["Math.LOG2e","log_{2}(e)"],["Math.LOG10e","log_{10}(e)"],["Math.LN2","log_{e}(2)"],["Math.LN10","log_{e}(10)"],["Math.tau","τ"],["infinity","∞"],["\\infty","∞"],["\\cdot",u],["\\times",p],["×",p],["·",u],["÷","/"],["->","→"],["\\to","→"],["\\max","max"],["\\min","min"],["\\det","det"],["^{\\top}","^{T}",!0],["^\\top","^T",!0],["^{\\intercal}","^{T}",!0],["^\\intercal","^T",!0],["^{\\mathsf{T}}","^{T}",!0],["^{\\mathrm{T}}","^{T}",!0],["product(","prod("],["log[","log_["],["\\log[","log_["],["\\log_","log_"],["\\log","log_{e}"],["\\ln","log_{e}"],["log(","log_{e}("],["ln(","log_{e}("],...["","\\"].flatMap(e=>["(","{"].flatMap(t=>[2,3,4,5,6,7,8,9,10].map(s=>[`${e}log${s}${t}`,`log_{${s}}${t}`]))),["\\log_10","log_{10}"],["log_10","log_{10}"],["\\log\\log","\\loglog"],["\\log\\log\\log","\\logloglog"],["\\loglog","\\loglog"],["\\logloglog","\\logloglog"],["loglog(","loglog("],["\\exp","e^"],["exp(","e^("],["\\div","/"],["\\int","\\int"],["\\in","∈"],["\\not\\in","∉"],["\\notin","∉"],["\\nor"," \\nor "],["\\xor"," \\xor "],["\\nand"," \\nand "],["\\Longrightarrow"," \\implies "],["\\Rightarrow"," \\implies "],["\\implies"," \\implies "],["⇒"," \\implies "],["⟹"," \\implies "],["\\Longleftrightarrow"," \\iff "],["\\Leftrightarrow"," \\iff "],["\\iff"," \\iff "],["⇔"," \\iff "],["⟺"," \\iff "],["\\uparrow"," \\nand "],["\\downarrow"," \\nor "],["\\oplus"," \\xor "],["\\lor","∨"],["\\land","&"],["Not"," \\not "],["&&","&"],["\\neg"," \\not "],["\\not"," \\not "],["\\rightarrow","→"],["\\limits_","_"],["\\pmod"," pmod "],["\\equiv","≡"],["\\mod"," mod "],["\\bmod"," mod "],["\\pm","±"],["\\plusmn","±"],["\\neq","≠"],["\\ne","≠"],["!==","≠="],["==","="],["=...=","==="],["!=","≠"],["\\gt",">"],["\\lt","<"],["\\ge","≥"],["\\geq","≥"],[">=","≥"],["\\le","≤"],["\\leq","≤"],["<=","≤"],["<>","≠"],["\\lparen","("],["\\rparen",")"],["\\left(","("],["\\right)",")"],["\\left[","["],["\\right]","]"],["\\left{","{"],["\\right}","}"],["\\left\\{","{"],["\\{","{"],["\\}","}"],["\\right\\}","}"],["\\left.",d],["\\left|","|"],["\\right|","|"],["\\lfloor","⌊"],["\\rfloor","⌋"],["\\lceil","⌈"],["\\rceil","⌉"],["ceiling(","ceil("],["√","sqrt"],["²","^2"],["³","^3"],["⁴","^4"],["⁵","^5"],["⁶","^6"],["⁷","^7"],["⁸","^8"],["⁹","^9"],["⁰","^0"],["¹","^1"],["ⁿ","^n"],["⁻¹","^{-1}"],["⁻²","^{-2}"],["⁻³","^{-3}"],["∛","sqrt[3]"],["∜","sqrt[4]"],["½","(1/2)"],["⅓","(1/3)"],["⅔","(2/3)"],["¼","(1/4)"],["¾","(3/4)"],["⅛","(1/8)"],["⅜","(3/8)"],["⅝","(5/8)"],["⅞","(7/8)"],["⅕","(1/5)"],["⅖","(2/5)"],["⅗","(3/5)"],["⅘","(4/5)"],["⅙","(1/6)"],["⅚","(5/6)"],["⅐","(1/7)"],["⅑","(1/9)"],["⅒","(1/10)"],["⊕"," \\xor "],["∞","∞"],["⋅",u],["−","-"],["\\wedge","&"],["∨","∨"],["\\vee","∨"],["\\Pi","Π",!0],["\\Alpha","Α",!0],["\\Beta","Β",!0],["\\Gamma","Γ",!0],["\\Delta","Δ",!0],["\\Delta ","Δ",!0],["\\Epsilon","Ε",!0],["\\Zeta","Ζ",!0],["\\Eta","Η",!0],["\\Theta","Θ",!0],["\\Iota","Ι",!0],["\\Kappa","Κ",!0],["\\Lambda","Λ",!0],["\\Mu","Μ",!0],["\\Nu","Ν",!0],["\\Xi","Ξ",!0],["\\Omicron","Ο",!0],["\\Rho","Ρ",!0],["\\Sigma","Σ",!0],["\\Tau","Τ",!0],["\\Upsilon","Υ",!0],["\\Phi","Φ",!0],["\\Chi","Χ",!0],["\\Psi","Ψ",!0],["\\Omega","Ω",!0],["\\pi","π",!0],["\\alpha","α",!0],["\\beta","β",!0],["\\gamma","γ",!0],["\\delta","δ",!0],["\\epsilon","ε",!0],["\\zeta","ζ",!0],["\\eta","η",!0],["\\theta","θ",!0],["\\iota","ι",!0],["\\kappa","κ",!0],["\\lambda","λ",!0],["\\mu","μ",!0],["\\nu","ν",!0],["\\xi","ξ",!0],["\\omicron","ο",!0],["\\rho","ρ",!0],["\\sigma","σ",!0],["\\tau","τ",!0],["\\upsilon","υ",!0],["\\phi","φ",!0],["\\chi","χ",!0],["\\psi","ψ",!0],["\\omega","ω",!0]];const T=e=>" "===e||"\t"===e||"\n"===e,F="({[",P=")}]";const W="ⅆ",B=new RegExp(String.raw`(\\(?:mathrm|operatorname|text)\{d\}\s*|d)([A-Za-zα-ω](?![A-Za-z0-9])|\\(?:${"alpha|beta|gamma|delta|epsilon|varepsilon|zeta|eta|theta|vartheta|iota|kappa|lambda|mu|nu|xi|rho|sigma|tau|upsilon|phi|varphi|chi|psi|omega"})(?![A-Za-z]))`,"g");function U(e){const t=[String.raw`\left\{`,String.raw`\left\lbrace`,String.raw`\{`,String.raw`\lbrace`,"{"],s=[String.raw`\right\}`,String.raw`\right\rbrace`,String.raw`\}`,String.raw`\rbrace`,"}"],n=/\\[A-Za-z{]/.test(e);for(let r=0,i=0;r<50;r++){let r=-1,o="";for(let s=i;s<e.length&&r<0;s++){const i=t.find(t=>e.startsWith(t,s));i&&("{"===i&&(n||/[\\^_A-Za-z]$/.test(e.slice(0,s)))||("{"!==i||"{"!==e[s+1]?(r=s,o=i):s++))}if(r<0)return e;let a=0,l=null,c=null,h=-1,u=0;for(let t=r+o.length;t<e.length;t++){const r=s.find(s=>e.startsWith(s,t));if(0===a&&r&&("{"===o?"}"===r:"}"!==r)){h=t,o+="|"+r;break}if(!"([{".includes(e[t])||"{"===e[t]&&"\\"===e[t-1]?")]}".includes(e[t])&&"\\"!==e[t-1]&&a--:a++,0===a&&"?"===e[t]&&u++,0===a&&!l){const s=/^(?:\\middle\s*\||\\mid(?![A-Za-z])|:(?!=)|\|)/.exec(e.slice(t));s&&":"===s[0]&&u?u--:s&&"|"!==s[0]?l={at:t,length:s[0].length}:s&&n&&!c&&!/\\left\s*$|\\right\s*$|\\middle\s*$/.test(e.slice(0,t))&&(c={at:t,length:1})}}if(l??=c,h<0||!l){i=r+1;continue}const p=e.slice(r+o.split("|")[0].length,l.at).trim(),f=e.slice(l.at+l.length,h).trim();if(!p||!f){i=r+1;continue}const d=o.split("|")[1];e=`${e.slice(0,r)}setbuilder((${p}),(${f}))${e.slice(h+d.length)}`,i=r}return e}const D=String.raw`\\(?:[Bb]igg?|right)[lrm]?`,G=new RegExp(`(?:${D}\\s*([\\]|])|(\\]))\\s*(?=[_^])`,"g");function H(e,t){let s=t;for(;" "===e[s];)s++;if("{"===e[s]){let t=0;for(let n=s;n<e.length;n++)if("{"===e[n])t++;else if("}"===e[n]&&0===--t)return{value:e.slice(s+1,n).trim(),end:n+1};return null}const n=/^\\[A-Za-z]+/.exec(e.slice(s));return n?{value:n[0],end:s+n[0].length}:"-"===e[s]&&/\d/.test(e[s+1]??"")?{value:e.slice(s,s+2),end:s+2}:e[s]?{value:e[s],end:s+1}:null}function Q(e,t){let s=t;for(;" "===e[s];)s++;const n=e[s];if("_"!==n&&"^"!==n)return null;const r=H(e,s+1);if(!r)return null;let i=r.end;for(;" "===e[i];)i++;const o="_"===n?"^":"_";if(e[i]!==o)return null;const a=H(e,i+1);return a?"_"===n?{lo:r.value,hi:a.value,end:a.end}:{lo:a.value,hi:r.value,end:a.end}:null}function V(e,t,s){let n=0;for(let r=t-1;r>=0;r--){if(s){if("]"===e[r])n++;else if("["===e[r]){if(0===n)return{start:r,end:r+1};n--}continue}const i=e.slice(0,r+1),o=/\\right\s*[\]|)}.]$|\\[Bb]igg?r?\s*[\]|]$/.exec(i),a=/\\left\s*(?:[[(.]|\\\{)$|\\[Bb]igg?l?\s*\[$/.exec(i);if(o&&r+1<t)n++,r-=o[0].length-1;else if(a){if(0===n)return{start:r+1-a[0].length,end:r+1};n--,r-=a[0].length-1}}return null}function X(e,t){let s=0;for(let n=t-1;n>=0;n--){const t=e[n];if(")]}".includes(t))s++;else if("([{".includes(t)){if(0===s)return n+1;s--}else{if(0===s&&/[=<>&,]/.test(t))return n+1;if(0===s&&/\\(?:approx|leq?|geq?|neq?|to|quad|qquad)$/.test(e.slice(0,n+1)))return n+1}}return 0}function J(e){for(let t=0;t<50;t++){G.lastIndex=0;let t=null;for(let s=G.exec(e);s;s=G.exec(e)){const n=Q(e,s.index+s[0].length);if(!n)continue;const r=Boolean(s[2]),i="|"===(s[1]??s[2]),o=V(e,s.index,r);if((o||i&&!/^\\right/.test(s[0]))&&(!o||!/\\left\s*\|$/.test(e.slice(o.start,o.end)))){t={m:s,limits:n,opener:o};break}}if(!t)return e;const{m:s,limits:n,opener:r}=t,i=r?r.start:X(e,s.index),o=e.slice(r?r.end:i,s.index).trim(),a=/^\s*([A-Za-zα-ωΑ-Ω]|\\[A-Za-z]+)\s*=\s*([\s\S]+)$/.exec(n.lo),l=n.hi.replace(/^\s*(?:[A-Za-zα-ωΑ-Ω]|\\[A-Za-z]+)\s*=\s*/,""),c=a?`evaluated((${o}),${a[1]},${a[2]},${l})`:`evaluated((${o}),${n.lo},${l})`;e=`${e.slice(0,i)}${i>0&&/[^\s(=]/.test(e[i-1])?" ":""}${c}${e.slice(n.end)}`}return e}const K={__proto__:null,sine:"sin",cosine:"cos",tangent:"tan",secant:"sec",cosecant:"csc",cotangent:"cot"};const Y=100;const ee={__proto__:null},te={__proto__:null};[..."⁰¹²³⁴⁵⁶⁷⁸⁹"].forEach((e,t)=>{ee[e]=String(t)}),[..."₀₁₂₃₄₅₆₇₈₉"].forEach((e,t)=>{te[e]=String(t)}),Object.assign(ee,{"⁺":"+","⁻":"-","⁼":"=","⁽":"(","⁾":")","ᵃ":"a","ᵇ":"b","ᶜ":"c","ᵈ":"d","ᵉ":"e","ᶠ":"f","ᵍ":"g","ʰ":"h","ⁱ":"i","ʲ":"j","ᵏ":"k","ˡ":"l","ᵐ":"m","ⁿ":"n","ᵒ":"o","ᵖ":"p","ʳ":"r","ˢ":"s","ᵗ":"t","ᵘ":"u","ᵛ":"v","ʷ":"w","ˣ":"x","ʸ":"y","ᶻ":"z"}),Object.assign(te,{"₊":"+","₋":"-","₌":"=","₍":"(","₎":")","ₐ":"a","ₑ":"e","ₕ":"h","ᵢ":"i","ⱼ":"j","ₖ":"k","ₗ":"l","ₘ":"m","ₙ":"n","ₒ":"o","ₚ":"p","ᵣ":"r","ₛ":"s","ₜ":"t","ᵤ":"u","ᵥ":"v","ₓ":"x"});const se=new RegExp(`[${Object.keys(ee).join("")}]+|[${Object.keys(te).join("")}]+`,"g");const ne={__proto__:null,"½":"1/2","⅓":"1/3","⅔":"2/3","¼":"1/4","¾":"3/4","⅕":"1/5","⅖":"2/5","⅗":"3/5","⅘":"4/5","⅙":"1/6","⅚":"5/6","⅐":"1/7","⅛":"1/8","⅜":"3/8","⅝":"5/8","⅞":"7/8","⅑":"1/9","⅒":"1/10"},re=new RegExp(`(?<![\\w.,^_])(\\d+)[ \\t]?([${Object.keys(ne).join("")}])`,"g"),ie=/(?<![\w.,^_/])(\d+)[ \t]+([1-9]\d*)\/(\d+)(?![\d./^!])/g,oe=/(?<![\w.,^_])(\d+)\s*\\[dtc]?frac\s*(?:\{\s*(\d+)\s*\}\s*\{\s*(\d+)\s*\}|(\d)(\d)(?!\d))/g;function ae(e){const t=e.replace(/^\s*(?:\\(?:text|textrm|mathrm)\s*\{\s*(?:if|for|when|where)\s*\}|\b(?:if|for|when|where)\b)\s*,?/,"").replace(/[\s,.;]+$/,"").trim();return/^(?:\\(?:text|textrm|mathrm)\s*\{\s*)?(?:otherwise|else)\s*\}?$/.test(t)?"":t}function le(e,t){const s=[];let n=0,r=0;for(let i=0;i<e.length;i++)F.includes(e[i])?n++:P.includes(e[i])?n--:0===n&&e.startsWith(t,i)&&(s.push(e.slice(r,i)),i+=t.length-1,r=i+1);return s.push(e.slice(r)),s}function ce(e,t,s){let n=0;const r=new RegExp(`\\b${s}\\b`,"iy");for(let s=t;s<e.length;s++)if(F.includes(e[s]))n++;else if(P.includes(e[s])){if(--n<0)return-1}else if(0===n&&(r.lastIndex=s,r.test(e)))return s;return-1}function he(e,t){let s=0;for(let n=t;n<e.length;n++)if(F.includes(e[n]))s++;else if(P.includes(e[n])&&--s<0)return n;return e.length}function ue(e,t){let s=1;for(let n=t+1;n<e.length;n++)if("?"===e[n])s++;else if(":"===e[n]&&0===--s)return e.slice(0,t+1)+"("+e.slice(t+1,n)+")?:"+e.slice(n+1);return e}function pe(e){for(let t=2;t<e.length;t++)if("g"===e[t]&&"log"===e.slice(t-2,t+1))if("_"===e[t+1]){if(F.includes(e[t+2]))continue;const s=/^(?:\d+(?:\.\d+)?|[A-Za-z\u0370-\u03FF])/.exec(e.slice(t+2))?.[0];if(!s)continue;const n=t+2+s.length;e=e.slice(0,t+2)+"{"+s+"}"+e.slice(n)}else if("("===e[t+1]){let s=1,n=0,r="",i="",o=0;for(let a=t+2;a<e.length;a++){if(")]".includes(e[a])?s--:"([".includes(e[a])&&s++,0===s){o=a;break}1===s&&","===e[a]?n++:0===n?r+=e[a]:i+=e[a]}1===n&&o>0&&(e=e.slice(0,t+1)+"_{"+i+"}("+r+")"+e.slice(o+1))}return e}function fe(e){const t="⌈"===e[0]?"ceil":"floor";let s=1;for(let n=1;n<e.length;n++)if("⌊"===e[n]||"⌈"===e[n]?s++:"⌋"!==e[n]&&"⌉"!==e[n]||s--,0===s)return t+"("+e.slice(1,n)+")"+e.slice(n+1);return e}function de(e){let t=1;for(let s=0;s<e.length;s++){if(")]".includes(e[s])?t--:"([".includes(e[s])&&t++,0!==t)continue;const n=e.slice(0,s),r=e[s+1];if("{"===r||"("===r)return r+n+","+e.slice(s+2);const i=/^[A-Za-z0-9.\u0370-\u03FF]+/.exec(e.slice(s+1))?.[0]??"";return"{"+n+","+i+"}"+e.slice(s+1+i.length)}return e}const ge=new Set(["pmatrix","bmatrix","vmatrix"]);function me(e){const t=e.indexOf("}"),s=e.indexOf("{");if(t<0||s>=0&&s<t)return!1;const n=e.slice(0,t);if(!n.includes("matrix"))return!1;const r=e.indexOf("\\end{"+n+"}"),i=e.indexOf("\\begin{"+n+"}",t+1);if(r<0||-1!==i&&i<r)return!1;const o=e.slice(t+1,r).trim().split("\\\\").map(e=>e.trim()).filter(e=>e.length).map(e=>"["+e.replace(/&/g,", ")+"]"),a=e.slice(r+6+n.length);return ge.has(n)?n+"("+o.join(", ")+")"+a:"["+o.join(", ")+"]"+a}function $e(e,t){if("{"!==e[t+1])return[e[t+1],t+2];let s=1,n="";for(let r=t+2;r<e.length;r++){if("{"===e[r])s++;else if("}"===e[r]&&0===--s)return[n,r+1];n+=e[r]}return[n,e.length]}function xe(e){const{lower:t,upper:s,next:n}=function(e){let t="",s="",n=0;return"_"===e[0]&&([t,n]=$e(e,0),"^"===e[n]&&([s,n]=$e(e,n))),{lower:t,upper:s,next:n}}(e),r=e.slice(n),i=e=>e.replace(/\\,/g,"").replace(/\\!/g,"");let o,a="",l="",c=r.indexOf("\\mathrm{d}");if(c>=0){o=r.slice(0,c);const e=/^\{([^{}]*)\}/.exec(r.slice(c+10));a=e?e[1]:r[c+10],l=r.slice(c+10+(e?e[0].length:1))}else(c=function(e){let t=0;for(let s=0;s<e.length;s++)if(F.includes(e[s]))t++;else if(P.includes(e[s]))t--;else if(0===t&&"d"===e[s]&&/[A-Za-z\u0370-\u03FF]/.test(e[s+1]??"")&&!/[A-Za-z]/.test(e[s+2]??""))return s;return-1}(r))>=0?(o=r.slice(0,c),a=r[c+1],l=r.slice(c+2)):o=r;if(!a){const e=/frac\{d([A-Za-z])\}/.exec(o);e&&(a=e[1],o=o.replace(e[0],"frac{1}"))}if(!a&&/^[A-Za-z]$/.test(t)){a=[...i(o).matchAll(/(?<![A-Za-z\\])([A-Za-z])(?![A-Za-z(])/g)].map(e=>e[1]).find(e=>e!==t&&e!==s)??"x"}let h="int("+(i(o).trim()?i(o):"1");return a&&(h+=","+a),(t||s)&&(h+=","+t+","+s),h+")"+l}function ye(e){const t=e[0];if(!(t>="A"&&t<="Z"||t>="a"&&t<="z"||t>="α"&&t<="ω"))return!1;const s=e.slice(2);let n=1;for(let e=0;e<s.length;e++)if(P.includes(s[e])?n--:F.includes(s[e])&&n++,0===n)return"("+s.slice(0,e)+","+t+")"+s.slice(e+1);return!1}function be(e){return"{"===e[0]||e.length<2?e:"{"+e[0]+"}{"+e[1]+"}"+e.slice(2)}const we=/[A-Za-z\u0370-\u03FF]/;function ve(e,t){if("{"!==e[t])return null;let s=0;for(let n=t;n<e.length;n++)if("{"===e[n])s++;else if("}"===e[n]&&0===--s)return[e.slice(t+1,n),n+1];return null}function ke(e,t){const s=/^\^(?:\{(\d+)\}|(\d))/.exec(e.slice(t));return s?[Number(s[1]??s[2]),t+s[0].length]:[1,t]}function Ae(e,t){const s=e.replace(/\\mathrm\{d\}/g,"d"),n=t.replace(/\\mathrm\{d\}/g,"d"),r=/^d(?:\^\{?(\d+)\}?)?([A-Za-z\u0370-\u03FF]?)$/.exec(s),i=/^d([A-Za-z\u0370-\u03FF])(?:\^\{?(\d+)\}?)?$/.exec(n);return r&&i?{n:Number(r[1]??1),of:r[2],by:i[1]}:null}function Ee(e,t){if(F.includes(e[t])){let s=0;for(let n=t;n<e.length;n++)if(F.includes(e[n]))s++;else if(P.includes(e[n])&&0===--s)return[e.slice(t,n+1),n+1];return[e.slice(t),e.length]}let s=0;for(let n=t;n<e.length;n++)if(F.includes(e[n]))s++;else{if(P.includes(e[n])&&--s<0)return[e.slice(t,n),n];if(0===s&&("="===e[n]||","===e[n]))return[e.slice(t,n),n]}return[e.slice(t),e.length]}const _e=(e,t,s)=>`der(${e},${t}${s>1?","+s:""})`;const Me="strtktk",Oe="ABCDEFGHIJ",qe=e=>Me+[...String(e)].map(e=>Oe[e]).join(""),Se=new RegExp(`^${Me}[${Oe}]+$`);function ze(e){let t="";for(let s=0;s<e.length;s++){const n=e[s];if('"'===n||"`"===n){const n=Ne(e,s);t+=e.slice(s,n+1),s=n}else if("'"===n)if(/[A-Za-z0-9)\]′\u0370-\u03FF]/.test(t[t.length-1]??""))t+="′";else{const n=Ne(e,s);t+=e.slice(s,n+1),s=n}else t+=n}return t}function Ne(e,t){let s=!1;for(let n=t+1;n<e.length;n++)if(s)s=!1;else if("\\"===e[n])s=!0;else if(e[n]===e[t])return n;return e.length-1}const Le=new RegExp(`${Me}[${Oe}]+|§\\d+§`,"g");function Ze(e,t){return t&&0!==t.size?e.map(e=>"string"!=typeof e?e:e.replace(Le,e=>{const s=t.get(e);if(void 0!==s)return s;for(let s=e.length-1;s>7;s--){const n=t.get(e.slice(0,s));if(void 0!==n)return n+e.slice(s)}return e})):e}const Ce={factorial:"((n) => { if (!Number.isInteger(n) || n < 0) return NaN; if (n > 170) return Infinity; let r = 1; for (let i = 2; i <= n; i++) r *= i; return r; })",perm:"((n, k) => { if (!Number.isInteger(n) || !Number.isInteger(k) || k < 0 || k > n) return NaN; let r = 1; for (let i = n - k + 1; i <= n && r !== Infinity; i++) r *= i; return r; })",comb:"((n, k) => { if (!Number.isInteger(n) || !Number.isInteger(k) || k < 0 || k > n) return NaN; k = Math.min(k, n - k); let r = 1; for (let i = 1; i <= k && r !== Infinity; i++) r = r * (n - k + i) / i; return Math.round(r); })",gcd:"((args)=>{let gcd = function(a,b) { return (!b)?a:gcd(b,a%b); }; return Math.abs(args.reduce(gcd));})",det:"((m) => { const a = m.map((r) => r.slice()); const n = a.length; let d = 1; for (let i = 0; i < n; i++) { let p = i; for (let r = i + 1; r < n; r++) if (Math.abs(a[r][i]) > Math.abs(a[p][i])) p = r; if (a[p][i] === 0) return 0; if (p !== i) { const t = a[p]; a[p] = a[i]; a[i] = t; d = -d; } d *= a[i][i]; for (let r = i + 1; r < n; r++) { const f = a[r][i] / a[i][i]; for (let c = i; c < n; c++) a[r][c] -= f * a[i][c]; } } return d; })",times:"((a, b) => { const L = Array.isArray; const m = (v) => L(v) && v.length > 0 && v.every((r) => L(r) && r.length > 0 && r.length === v[0].length && !r.some(L)) && (v.length > 1 || v[0].length > 1); const l = (v) => L(v) && v.length > 0 && !v.some(L); const p = (A, B) => (A[0].length === B.length ? A.map((r) => B[0].map((_, j) => r.reduce((s, x, k) => s + x * B[k][j], 0))) : NaN); const e = (x, y) => (L(x) && L(y) ? (x.length === y.length ? x.map((t, i) => e(t, y[i])) : NaN) : L(x) ? x.map((t) => e(t, y)) : L(y) ? y.map((t) => e(x, t)) : x * y); if (m(a) && m(b)) return p(a, b); if (m(a) && l(b)) { const c = p(a, b.map((y) => [y])); return L(c) ? c.map((r) => r[0]) : c; } if (l(a) && m(b)) { const c = p([a], b); return L(c) ? c[0] : c; } return e(a, b); })",same:"((a, b) => { const s = (x, y) => (Array.isArray(x) && Array.isArray(y) ? x.length === y.length && x.every((v, i) => s(v, y[i])) : x == y); return s(a, b); })",union:"((a, b) => [...new Set([].concat(a, b))])",intersection:"((a, b) => [].concat(a).filter((x) => [].concat(b).includes(x)))",round:"((x, d) => { const p = Math.pow(10, d); return (x < 0 ? -Math.round(-x * p) : Math.round(x * p)) / p; })",mod:"((a, n) => ((a % n) + n) % n)",root:"((n, x) => (x < 0 && Math.abs(n % 2) === 1 ? -Math.pow(-x, 1 / n) : Math.pow(x, 1 / n)))",contains:"((s, t) => { const f = (x) => (x == null ? String() : String(x)).replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32)); return f(s).includes(f(t)); })",startswith:"((s, t) => { const f = (x) => (x == null ? String() : String(x)).replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32)); return f(s).startsWith(f(t)); })"},Re="lambda x: str().join(chr(ord(c) + 32) if 65 <= ord(c) <= 90 else c for c in (str() if x is None or x != x else str(int(x)) if isinstance(x, float) and x.is_integer() else str(x)))",Ie=`lambda s, t, f=${Re}: f(t) in f(s)`,je=`lambda s, t, f=${Re}: f(s).startswith(f(t))`,Te={contains:`(${Ie})`,startswith:`(${je})`},Fe={contains:`np.vectorize(${Ie}, otypes=[bool])`,startswith:`np.vectorize(${je}, otypes=[bool])`};for(const e of[Ce,Te,Fe])Object.setPrototypeOf(e,null);const Pe="([{",We=")]}";function Be(e,t){let s=0;for(let n=t;n<e.length;n++)if(Pe.includes(e[n]))s++;else if(We.includes(e[n])&&0===--s)return n;return-1}function Ue(e){if(!e.trim())return[];const t=[];let s=0,n=0;for(let r=0;r<e.length;r++)Pe.includes(e[r])?s++:We.includes(e[r])?s--:","===e[r]&&0===s&&(t.push(e.slice(n,r).trim()),n=r+1);return t.push(e.slice(n).trim()),t}function De(e,t,s){const n=[...e.matchAll(t)];for(let t=n.length-1;t>=0;t--){const r=n[t],i=r.index+r[0].length-1,o=Be(e,i);if(o<0)continue;const a=s(Ue(e.slice(i+1,o)),r,e.slice(o+1));if(null==a)continue;const[l,c=0]=Array.isArray(a)?a:[a];if((e=e.slice(0,r.index)+l+e.slice(o+1+c)).length>1e5)throw new Error("The expression expands too much when read")}return e}const Ge=e=>new RegExp(`(?<![\\w.])(${e})\\s*\\(`,"g"),He=e=>`(${e})`,Qe=e=>/^\[.*\]$/s.test(e.trim())?`{${e.trim().slice(1,-1)}}`:e,Ve=[...Object.entries(Ce),...Object.entries(Fe),...Object.entries(Te)];const Xe={IF:e=>e.length>=2&&`((${e[0]}) ? (${e[1]}) : (${e[2]??"false"}))`,AND:e=>e.length&&`(${e.map(He).join(" & ")})`,OR:e=>e.length&&`(${e.map(He).join(" ∨ ")})`,NOT:e=>1===e.length&&`(!(${e[0]}))`,XOR:e=>2===e.length&&`((${e[0]}) \\xor (${e[1]}))`,LOG:e=>2===e.length?`log_{${e[1]}}(${e[0]})`:1===e.length&&`log_{10}(${e[0]})`,ATAN2:e=>2===e.length&&`atan2(${e[1]}, ${e[0]})`,INT:e=>1===e.length&&`floor(${e[0]})`,FIXED:e=>(1===e.length||2===e.length)&&`fixed(${e[0]}, ${e[1]??2})`,"FLOOR.MATH":e=>1===e.length&&`floor(${e[0]})`,"CEILING.MATH":e=>1===e.length&&`ceil(${e[0]})`,FACT:e=>1===e.length&&`((${e[0]})!)`,COMBIN:e=>2===e.length&&`comb(${e.join(", ")})`,PERMUT:e=>2===e.length&&`perm(${e.join(", ")})`,AVERAGE:e=>`mean(${e.join(", ")})`,SUM:e=>`sum(${e.join(", ")})`,PRODUCT:e=>`prod(${e.join(", ")})`,MAX:e=>`max(${e.join(", ")})`,MIN:e=>`min(${e.join(", ")})`,MEDIAN:e=>`median(${e.join(", ")})`,"STDEV.P":e=>`stdev(${e.join(", ")})`,STDEVP:e=>`stdev(${e.join(", ")})`,CEILING:e=>(1===e.length||2===e.length&&"1"===e[1].trim())&&`ceil(${e[0]})`,RAND:e=>!e.length&&"random()",ISNUMBER:e=>1===e.length&&`((${e[0]}) ∈ ℝ)`,LOG10:e=>1===e.length&&`log_{10}(${e[0]})`,MMULT:e=>2===e.length&&`((${e[0]}) * (${e[1]}))`};function Je(e,t){for(let s=t,n=0;s>=0;s--)if(")"===e[s])n++;else if("("===e[s]&&0===--n)return s;return-1}function Ke(e){const t="(?:[\\p{L}_][\\p{L}\\p{N}_.]*|\\d+(?:\\.\\d+)?|§\\d+§)";return e=function(e,t){const s=new RegExp(`^(?:${t})`,"u"),n=new RegExp(`(?:${t})$`,"u"),r=/[\p{L}_][\p{L}\p{N}_.]*$/u;for(let t=e.indexOf("^");t>=0;t=e.indexOf("^",t+1)){let i,o=t+1;for(;/\s/.test(e[o]??"");)o++;if("("===e[o])i=Be(e,o)+1;else{const t=s.exec(e.slice(o));i=t?o+t[0].length:0,t&&"("===e[i]&&(i=Be(e,i)+1)}if(i<=o||!/^\s*\^/.test(e.slice(i)))continue;let a,l=t;for(;l>0&&/\s/.test(e[l-1]);)l--;if(")"===e[l-1]){a=Je(e,l-1);const t=a>0&&r.exec(e.slice(Math.max(0,a-64),a));t&&(a-=t[0].length)}else{const t=n.exec(e.slice(Math.max(0,l-64),l));a=t?l-t[0].length:-1}a<0||(e=`${e.slice(0,a)}(${e.slice(a,i)})${e.slice(i)}`,t=i+1)}return e}(e=(e=(e=e.replace(/^\s*=/,"").replace(/(?<![\w.])_xlfn\./g,"")).replace(/&/g,"+")).replace(new RegExp(`(^|[(,=<>*/+^-]\\s*)-\\s*(${t})(?=\\s*\\^)`,"gu"),"$1(-$2)"),t),De(e,/(?<![\w.])([A-Z][A-Z0-9]*(?:\.[A-Z]+)?)\s*\(/g,(e,t)=>Xe[t[1]]?.(e)||null)}const Ye={POWER:"pow",SQRT:"sqrt",ABS:"abs",LN:"ln",EXP:"exp",FLOOR:"floor",CEIL:"ceil",CEILING:"ceil",ROUND:"round",GREATEST:"max",LEAST:"min",MOD:"mod",ATAN2:"atan2",FACTORIAL:"factorial",SIN:"sin",COS:"cos",TAN:"tan",SEC:"sec",CSC:"csc",COT:"cot",ASIN:"asin",ACOS:"acos",ATAN:"atan",SINH:"sinh",COSH:"cosh",TANH:"tanh",ASINH:"asinh",ACOSH:"acosh",ATANH:"atanh",COMB:"comb",PERM:"perm",GCD:"gcd",DERIVATIVE:"derivative",LIMIT:"limit",UNION:"union",EXPM1:"expm1",TRUNC:"trunc",SUMMATION:"sum"},et="strtktk[A-J]+";function tt(e){for(let t=!0;t;){const s=e;t=(e=e.replace(new RegExp(`(${et})\\s*\\|\\|\\s*(${et})`,"g"),"$1 + $2"))!==s}const t=new RegExp(`^\\(typeof\\(([^()]*)\\)\\s+IN\\s+\\(${et},\\s*${et}\\)\\s+AND\\s+(.*)\\)$`,"s");for(let s=e.lastIndexOf("(typeof(");s>=0;s=s>0?e.lastIndexOf("(typeof(",s-1):-1){const n=Be(e,s),r=n>0&&t.exec(e.slice(s,n+1));if(!r)continue;const[,i,o]=r,a=new Map([[`${i} = TRUNC(${i})`,"ℤ"],[`${i} = TRUNC(${i}) AND ${i} > 0`,"ℕ"],[`ABS(${i}) < ∞`,"ℝ"]]).get(o.trim());a&&(e=`${e.slice(0,s)}((${i}) ∈ ${a})${e.slice(n+1)}`)}e=De(e,/(?<![\w.])CAST\s*\(/gi,e=>{const t=1===e.length&&/^(.*)\s+AS\s+(?:REAL|FLOAT|DOUBLE(?:\s+PRECISION)?|NUMERIC|DECIMAL|INTEGER|INT)\s*$/is.exec(e[0]);return t?`(${t[1]})`:null}),e=(e=(e=De(e=function(e){for(let t=0;t<50;t++){const t=[...e.matchAll(/\bCASE\s+WHEN\b/gi)];if(!t.length)return e;const s=t[t.length-1].index,n=/\bEND\b/i.exec(e.slice(s));if(!n)return e;const r=e.slice(s+4,s+n.index).split(/\b(WHEN|THEN|ELSE)\b/i).map(e=>e.trim()),i=[];let o="null";for(let e=1;e<r.length;e+=2){const t=r[e].toUpperCase();"WHEN"===t?i.push([r[e+1],r[e+3]]):"ELSE"===t&&(o=r[e+1])}const a=i.reduceRight((e,[t,s])=>`((${t}) ? (${s}) : ${e})`,He(o));e=e.slice(0,s)+a+e.slice(s+n.index+3)}return e}(e=e.replace(/(?<![\w.])ARRAY\s*\[/g,"[")),/(?<![\w.])LOG\s*\(/g,e=>2===e.length?`log_{${e[0]}}(${e[1]})`:`log_{10}(${e[0]})`)).replace(/(?<![\w.])PI\s*\(\s*\)/g,"π")).replace(/(?<![\w.])([A-Z][A-Z0-9]*)(?=\s*\()/g,e=>Ye[e]??e);for(const[t,s]of[[/\bNOT\s+IN\s*\(/g,"∉"],[/\bIN\s*\(/g,"∈"]])for(let n=t.exec(e);n;n=t.exec(e)){const r=n.index+n[0].length-1,i=Be(e,r);if(i<0)break;e=`${e.slice(0,n.index)}${s} {${e.slice(r+1,i)}}${e.slice(i+1)}`,t.lastIndex=0}return e.replace(/<>/g," ≠ ")}const st={sin:"sin",cos:"cos",tan:"tan",sinh:"sinh",cosh:"cosh",tanh:"tanh",arcsin:"asin",arccos:"acos",arctan:"atan",arcsinh:"asinh",arccosh:"acosh",arctanh:"atanh",arctan2:"atan2",sqrt:"sqrt",cbrt:"cbrt",exp:"exp",expm1:"expm1",log:"log",log2:"log2",log10:"log10",floor:"floor",ceil:"ceil",round:"round",power:"pow",abs:"abs",absolute:"abs",fabs:"abs",pi:"PI",e:"E",gcd:"gcd"},nt={maximum:"max",minimum:"min",nanmax:"max",nanmin:"min",nansum:"sum",nanprod:"prod",nanmean:"mean",nanmedian:"median",nanstd:"stdev",mod:"mod",fmod:"mod",where:"where",isin:"isin",isclose:"isclose",isfinite:"isfinite",logical_and:"logical_and",logical_or:"logical_or",logical_not:"logical_not",logical_xor:"logical_xor",prod:"prod",sum:"sum",trunc:"trunc",union1d:"union",intersect1d:"intersection",dot:"np_times",matmul:"np_times",multiply:"np_times",array_equal:"np_equal"};const rt=e=>e.replace(/(?<![\w.])set\s*\(\s*\)/g,"∅"),it={inf:"∞",nan:"NaN"};function ot(e){return(e=(e=function(e){return e=e.replace(/(?<![\w.])sympy\.Interval\.(open|Lopen|Ropen)\s*\(/g,(e,t)=>`sympy_interval_${t}(`),e=e.replace(/(?<![\w.])sympy\.S\.(\w+)/g,(e,t)=>lt[t]??e),e=e.replace(/(?<![\w.])sympy\.(Reals|Integers|Naturals0?|Rationals|Complexes)\b/g,(e,t)=>lt[t]),e.replace(/(?<![\w.])sympy\.(\w+)/g,(e,t)=>ct[t]??at[t]??e)}(e=(e=(e=(e=(e=(e=(e=e.replace(/(?<![\w.])(?:np|numpy)\.random\.random\(\s*\)|(?<![\w.])random\.random\(\s*\)/g,"Math.random()")).replace(/(?<![\w.])(?:np|numpy)\.linalg\.(det|inv)\b/g,"$1")).replace(/(?<![\w.])(?:np|numpy)\.array\s*\(/g,"(")).replace(/(?<![\w.])(?:np|numpy)\.(\w+)/g,(e,t)=>st[t]?`Math.${st[t]}`:nt[t]?nt[t]:it[t]??e)).replace(/(?<![\w.])(?:math|Math)\.(factorial|isfinite|isclose|prod|inf|nan|abs|trunc)\b/g,(e,t)=>it[t]??t)).replace(/(?<![\w.])Number\.(isInteger|isFinite)\b/g,(e,t)=>t.toLowerCase())).replace(/(?<![\w.])scipy\.(?:special|integrate)\.(\w+)/g,"$1"))).replace(/\bMZ_INFINITY\b/g,"∞")).replace(/(?<![\w.])mz_(?=\w+\s*\()/g,"")}const at={pi:"π",E:"e",oo:"∞",true:"true",false:"false",nan:"NaN",I:"i"},lt={Reals:"ℝ",Integers:"ℤ",Naturals:"ℕ",Naturals0:"ℕ",Rationals:"ℚ",Complexes:"ℂ"},ct={sqrt:"Math.sqrt",exp:"Math.exp",sin:"Math.sin",cos:"Math.cos",tan:"Math.tan",sinh:"Math.sinh",cosh:"Math.cosh",tanh:"Math.tanh",asin:"Math.asin",acos:"Math.acos",atan:"Math.atan",asinh:"Math.asinh",acosh:"Math.acosh",atanh:"Math.atanh",atan2:"atan2",csc:"csc",sec:"sec",cot:"cot",log:"log",Abs:"abs",floor:"floor",ceiling:"ceil",Max:"max",Min:"min",factorial:"factorial",binomial:"comb",ff:"perm",gcd:"gcd",gcd_list:"sympy_list_gcd",Mod:"mod",root:"sympy_root",real_root:"sympy_root",det:"det",Rational:"sympy_rational",Integer:"sympy_value",Float:"sympy_value",Eq:"sympy_eq",Ne:"sympy_ne",And:"sympy_and",Or:"sympy_or",Not:"sympy_not",Xor:"sympy_xor",Nand:"sympy_nand",Nor:"sympy_nor",Implies:"sympy_implies",Equivalent:"sympy_iff",Piecewise:"sympy_piecewise",FiniteSet:"sympy_set",Interval:"sympy_interval",Contains:"sympy_contains",Union:"union",Intersection:"intersection",diff:"derivative",integrate:"sympy_integrate",limit:"sympy_limit",summation:"sympy_summation",product:"sympy_product",expm1:"expm1",Matrix:""},ht={open:"()",Lopen:"(]",Ropen:"[)"};const ut=e=>{const t=/^\((.*)\)$/s.exec(e.trim());return t?Ue(t[1]):null},pt=e=>t=>2===t.length&&`${e[0]}${t[0]}, ${t[1]}${e[1]}`,ft=e=>ut(e)??[e],dt={where:e=>3===e.length&&`((${e[0]}) ? (${e[1]}) : (${e[2]}))`,both:e=>2===e.length&&`((${e[0]}) & (${e[1]}))`,either:e=>2===e.length&&`((${e[0]}) ∨ (${e[1]}))`,np_times:e=>2===e.length&&`((${e[0]}) * (${e[1]}))`,np_equal:e=>2===e.length&&`((${e[0]}) = (${e[1]}))`,logical_and:e=>2===e.length&&`((${e[0]}) & (${e[1]}))`,logical_or:e=>2===e.length&&`((${e[0]}) ∨ (${e[1]}))`,logical_xor:e=>2===e.length&&`((${e[0]}) \\xor (${e[1]}))`,logical_not:e=>1===e.length&&`(!(${e[0]}))`,isin:e=>2===e.length&&`((${e[0]}) ∈ ${Qe(e[1])})`,contains:e=>2===e.length&&`((${e[1]}) ∈ ${Qe(e[0])})`,isclose:e=>e.length>=2&&`((${e[0]}) ≈ (${e[1]}))`,isfinite:e=>1===e.length&&`((${e[0]}) ∈ ℝ)`,isinteger:e=>1===e.length&&`((${e[0]}) ∈ ℤ)`,factorial:e=>1===e.length&&`((${e[0]})!)`,union:e=>2===e.length&&`((${e[0]}) ∪ (${e[1]}))`,union_:e=>2===e.length&&`((${e[0]}) ∪ (${e[1]}))`,intersection:e=>2===e.length&&`((${e[0]}) ∩ (${e[1]}))`,sq:e=>1===e.length&&`((${e[0]})^2)`,cube:e=>1===e.length&&`((${e[0]})^3)`,fract:e=>1===e.length&&`((${e[0]}) - floor(${e[0]}))`,inversesqrt:e=>1===e.length&&`(1/sqrt(${e[0]}))`,radians:e=>1===e.length&&`((${e[0]})°)`,degrees:e=>1===e.length&&`((${e[0]})/°)`,mod:e=>2===e.length&&`((${e[0]}) \\mod (${e[1]}))`,atan:e=>2===e.length&&`atan2(${e[0]}, ${e[1]})`,exp2:e=>1===e.length&&`((2)^(${e[0]}))`,trunc:e=>1===e.length&&`((${e[0]}) < 0 ? (ceil(${e[0]})) : floor(${e[0]}))`,...Object.fromEntries([2,3,4].map(e=>[`mat${e}`,t=>t.length===e*e&&`[${Array.from({length:e},(s,n)=>`[${Array.from({length:e},(s,r)=>t[r*e+n]).join(", ")}]`).join(", ")}]`])),vec2:e=>`(${e.join(", ")})`,vec3:e=>`(${e.join(", ")})`,vec4:e=>`(${e.join(", ")})`,derivative:e=>e.length>=2&&`der(${e.join(", ")})`,sympy_rational:e=>2===e.length&&`((${e[0]})/(${e[1]}))`,sympy_value:e=>1===e.length&&`(${e[0]})`,sympy_eq:e=>2===e.length&&`((${e[0]}) = (${e[1]}))`,sympy_ne:e=>2===e.length&&`((${e[0]}) ≠ (${e[1]}))`,sympy_and:e=>e.length&&`(${e.map(He).join(" & ")})`,sympy_or:e=>e.length&&`(${e.map(He).join(" ∨ ")})`,sympy_not:e=>1===e.length&&`(!(${e[0]}))`,sympy_xor:e=>2===e.length&&`((${e[0]}) \\xor (${e[1]}))`,sympy_nand:e=>2===e.length&&`((${e[0]}) \\nand (${e[1]}))`,sympy_implies:e=>2===e.length&&`((${e[0]}) \\implies (${e[1]}))`,sympy_iff:e=>2===e.length&&`((${e[0]}) \\iff (${e[1]}))`,sympy_nor:e=>2===e.length&&`((${e[0]}) \\nor (${e[1]}))`,sympy_piecewise:e=>{const t=e.map(ut);return!t.length||t.some(e=>!e||2!==e.length)?null:t.reduceRight((e,[t,s])=>null===e||/^True$|^true$/.test(s.trim())&&void 0===e?`(${t})`:`((${s}) ? (${t}) : ${e??"(null)"})`,void 0)},sympy_set:e=>`{${e.join(", ")}}`,sympy_interval:pt("[]"),sympy_interval_open:pt("()"),sympy_interval_Lopen:pt("(]"),sympy_interval_Ropen:pt("[)"),sympy_contains:e=>2===e.length&&`((${e[0]}) ∈ ${e[1]})`,sympy_list_gcd:e=>1===e.length&&`gcd(${e[0].trim().replace(/^\[|\]$/g,"")})`,sympy_root:e=>2===e.length&&`root(${e[1]}, ${e[0]})`,sympy_integrate:e=>{if(2!==e.length)return null;const t=ft(e[1]);return 3===t.length?`int(${e[0]}, ${t.join(", ")})`:1===t.length?`int(${e[0]}, ${t[0]})`:null},sympy_limit:e=>e.length>=3&&`lim(${e.slice(0,3).join(", ")}${e[3]?`, ${e[3]}`:""})`,sympy_summation:e=>{if(3===e.length&&/^\s*[A-Za-z]\w*\s*==/.test(e[1]))return`sum(${e[0]}, ${e[1].replace("==","=")}, ${e[2]})`;const t=2===e.length&&ft(e[1]);return 3===t?.length&&`(\\sum_{${t[0]}=${t[1]}}^{${t[2]}} (${e[0]}))`},sympy_product:e=>{if(3===e.length&&/^\s*[A-Za-z]\w*\s*==/.test(e[1]))return`prod(${e[0]}, ${e[1].replace("==","=")}, ${e[2]})`;const t=2===e.length&&ft(e[1]);return 3===t?.length&&`(\\prod_{${t[0]}=${t[1]}}^{${t[2]}} (${e[0]}))`},limit:e=>e.length>=3&&`lim(${e.join(", ")})`,summation:e=>3===e.length&&/^\s*[A-Za-z]\w*\s*==/.test(e[1])&&`sum(${e[0]}, ${e[1].replace("==","=")}, ${e[2]})`,sum:gt("sum"),prod:gt("prod")};function gt(e){return t=>{const s=1===t.length&&/^(.*?)\s+for\s+(\w+)\s+in\s+range\((.*)\)\s*$/s.exec(t[0]);if(!s)return null;const n=Ue(s[3]),[r,i]=1===n.length?["0",n[0]]:n,o=/\s*\+\s*1$/.test(i)?i.replace(/\s*\+\s*1$/,""):`(${i}) - 1`;return`(\\${e}_{${s[2]}=${r}}^{${o}} (${s[1]}))`}}function mt(e){e=function(e){for(let t=e.lastIndexOf("(lambda");t>=0;t=t>0?e.lastIndexOf("(lambda",t-1):-1){const s=Be(e,t);if(s<0||"("!==e[s+1])continue;const n=Be(e,s+1),r=/^\(lambda\s+([\w\s,]+?)\s*:\s*([\s\S]*)\)$/.exec(e.slice(t,s+1));if(n<0||!r)continue;const i=r[1].split(",").map(e=>e.trim()),o=Ue(e.slice(s+2,n));if(o.length!==i.length||!i.every(e=>/^[A-Za-z_]\w*$/.test(e)))continue;const a=new Map(i.map((e,t)=>[e,o[t]])),l=r[2].replace(new RegExp(`(?<![\\w.])(${i.join("|")})(?!\\w)`,"g"),e=>`(${a.get(e)})`);if((e=e.slice(0,t)+`(${l})`+e.slice(n+1)).length>1e5)throw new Error("The expression expands too much when read")}return e}(e),e=De(e,Ge("quad"),(e,t,s)=>{const n=/^lambda\s+(\w+)\s*:\s*(.*)$/s.exec(e[0]??"");if(!n||3!==e.length)return null;const r=/^\s*\[\s*0\s*\]/.exec(s);return[`int(${n[2]}, ${n[1]}, ${e[1]}, ${e[2]})`,r?r[0].length:0]}),e=De(e,Ge(Object.keys(dt).join("|")),(e,t)=>dt[t[1]](e)||null);for(let t=(e=De(e,Ge("float"),(e,t,s)=>{if(1!==e.length)return null;const n=/^\s*\.is_integer\(\s*\)/.exec(s);return n?[`((${e[0]}) ∈ ℤ)`,n[0].length]:`(${e[0]})`})).lastIndexOf(".includes(");t>=0;t=t>0?e.lastIndexOf(".includes(",t-1):-1){const s=t+9,n=Be(e,s);let r=t;if(We.includes(e[t-1])){let s=0;for(r=t-1;r>=0;r--)if(We.includes(e[r]))s++;else if(Pe.includes(e[r])&&0===--s)break}else for(;r>0&&/[\w.]/.test(e[r-1]);)r--;n<0||r<0||r===t||(e=`${e.slice(0,r)}((${e.slice(s+1,n)}) ∈ ${Qe(e.slice(r,t))})${e.slice(n+1)}`)}return e}for(const e of[Xe,Ye,st,nt,at,lt,ct,ht,dt,it])Object.setPrototypeOf(e,null);const $t=String.raw`(?:\([^()]*\)|\[[^\[\]]*\]|\{[^{}]*\}|(?<![\w.′^])[\w.′^]+)`,xt=e=>e.trim().split(/\s+/).join(String.raw`\s+`),yt=e=>new RegExp(String.raw`(?<![\w.])${xt(e)}(?![\w.])`,"gi"),bt=[["is greater than or equal to","≥"],["is more than or equal to","≥"],["is at least","≥"],["is no less than","≥"],["is not less than","≥"],["is less than or equal to","≤"],["is at most","≤"],["is no more than","≤"],["is not more than","≤"],["is not greater than","≤"],["is greater than",">"],["is more than",">"],["is bigger than",">"],["is larger than",">"],["is above",">"],["exceeds",">"],["is less than","<"],["is smaller than","<"],["is fewer than","<"],["is below","<"],["is not equal to","≠"],["does not equal","≠"],["doesn't equal","≠"],["is different from","≠"],["is approximately equal to","≈"],["is approximately","≈"],["is about","≈"],["is roughly","≈"],["is close to","≈"],["is equal to","="],["is the same as","="],["equals","="],["greater than or equal to","≥"],["less than or equal to","≤"],["at least","≥"],["at most","≤"],["greater than",">"],["more than",">"],["less than","<"],["not equal to","≠"],["equal to","="]],wt="[<>≤≥=≠≈]",vt={second:2,third:3,fourth:4,fifth:5,sixth:6,seventh:7,eighth:8,ninth:9,tenth:10};Object.setPrototypeOf(vt,null);const kt=Object.entries({sine:"sin",cosine:"cos",tangent:"tan",secant:"sec",cosecant:"csc",cotangent:"cot","natural log":"ln","natural logarithm":"ln",arcsine:"arcsin",arccosine:"arccos",arctangent:"arctan",arcsecant:"arcsec",arccosecant:"arccsc",arccotangent:"arccot","hyperbolic sine":"sinh","hyperbolic cosine":"cosh","hyperbolic tangent":"tanh",floor:"floor",ceiling:"ceil"}).sort((e,t)=>t[0].length-e[0].length),At=[["is non-negative","≥ 0"],["is nonnegative","≥ 0"],["is not negative","≥ 0"],["is non-positive","≤ 0"],["is nonpositive","≤ 0"],["is not positive","≤ 0"],["is positive","> 0"],["is negative","< 0"],["is non-zero","≠ 0"],["is nonzero","≠ 0"],["is not zero","≠ 0"],["is zero","= 0"],["is an integer","∈ ℤ"],["is not an integer","∉ ℤ"],["is a natural number","∈ ℕ"],["is a real number","∈ ℝ"],["is real","∈ ℝ"],["is a rational number","∈ ℚ"],["is rational","∈ ℚ"],...[["the integers","ℤ"],["the natural numbers","ℕ"],["the real numbers","ℝ"],["the rational numbers","ℚ"],["the complex numbers","ℂ"]].flatMap(([e,t])=>[[`is not in ${e}`,`∉ ${t}`],[`is in ${e}`,`∈ ${t}`]])],Et=/(?<![A-Za-z])(?:is|between|exceeds|equals?|does|doesn't|least|most|than|divides|and|or|of|to|squared|cubed|twice|percent|by|over|times|plus|minus|modulo)(?![A-Za-z])/i;bt.map(([e])=>e),At.map(([e])=>e),kt.map(([e])=>`${e} of`);const _t=Z([[" And "," & "],[" Or "," ∨ "],[" Not In "," ∉ "],[" Nor "," \\nor "],[" Xor "," \\xor "],[" Nand "," \\nand "],[" In "," ∈ "],[" Mod "," \\mod "],[" % ","%"],[" Perm ","\\perm "],[" P ","\\perm",!0],[" Comb ","\\comb "],[" C ","\\comb",!0],[" choose ","\\comb "],["! ","!_"],["\\in ","∈"],["\\notin ","∉"],["\\not\\in ","∉"],["\\to ","→"],["===","=...="]]),Mt=Z([...j,...function(){const e=[],t=(t,s)=>{e.push([t,s],[t+"(",s+"("])};for(const s of["sin","cos","tan","cot","csc","sec"]){e.push(["Math."+s+"(",s+"("],["\\"+s+"(",s+"("],["\\"+s,s]);for(const e of["sin","cos","tan"].includes(s)?["","h"]:[""]){const n=s+e,r="arc"+s+e;e&&(t("Math."+n,n),t("\\"+n,n));for(const s of["^-1","^{-1}","-1"])t("\\"+n+s,r),t(n+s,r),e||t("Math."+n+s,r);e||t("\\a"+n,r),t("\\arc"+n,r),t("Math.a"+n,r),t("Math.arc"+n,r)}}return e}()]),Ot=[["code",function(e){return/^\s*=|[(.%^<_eE]|\|\||\bif\b|\b(?:True|False|None|TRUE|FALSE|NULL)\b/.test(e)?(/^\s*=(?!=)/.test(e)&&(e=Ke(e)),(e=(e=(e=function(e){for(let t=0;t<50;t++){let t=!0;for(const s of e.matchAll(/\bif\b/g)){const n=s.index;let r=0,i=-1,o=e.length;for(let t=n+2;t<e.length;t++)if(Pe.includes(e[t]))r++;else{if(We.includes(e[t])&&--r<0){o=t;break}if(0===r&&","===e[t]){o=t;break}if(0===r&&/^then\b/.test(e.slice(t))&&/\W/.test(e[t-1])){i=-2;break}0===r&&i<0&&/^else\b/.test(e.slice(t))&&/\W/.test(e[t-1])&&(i=t)}if(i<0)continue;r=0;let a=0;for(let t=n-1;t>=0;t--)if(We.includes(e[t]))r++;else{if(Pe.includes(e[t])&&--r<0){a=t+1;break}if(0===r&&","===e[t]){a=t+1;break}}const l=e.slice(a,n).trim();if(!l)continue;const c=`((${e.slice(n+2,i).trim()}) ? (${l}) : (${e.slice(i+4,o).trim()}))`;e=e.slice(0,a)+c+e.slice(o),t=!1;break}if(t)return e}return e}(e=mt(e=function(e){return e.includes("set")?rt(De(e,/(?<![\w.])sorted\s*\(/g,e=>{if(1!==e.length)return null;const t=e[0];for(let e=0,s=0;e<t.length;e++)if("([{".includes(t[e]))s++;else if(")]}".includes(t[e]))s--;else if(0===s&&("|"===t[e]||"&"===t[e])){const s=e=>{const t=e.trim(),s=/^set\s*\(/.exec(t);return s&&Be(t,s[0].length-1)===t.length-1?t.slice(s[0].length,-1):null},[n,r]=[s(t.slice(0,e)),s(t.slice(e+1))];return null!==n&&null!==r?`((${n}) ${"|"===t[e]?"∪":"∩"} (${r}))`:null}return null})):e}(e=ot(e=tt(e=(e=(e=function(e){for(const[t,s]of Ve)for(let n=e.lastIndexOf(s);n>=0;n=n>0?e.lastIndexOf(s,n-1):-1){const r=n+s.length,i="("===e[r]?Be(e,r):-1;if(i<0)continue;let o=e.slice(r+1,i);"gcd"===t&&(o=o.trim().replace(/^\[|\]$/g,""));const a="times"===t||"same"===t?Ue(o):null,l="factorial"===t?`((${o})!)`:2===a?.length?`((${a[0]}) ${"times"===t?"*":"="} (${a[1]}))`:`${t}(${o})`;e=e.slice(0,n)+l+e.slice(i+1)}for(let t=e.lastIndexOf(").toFixed(");t>=0;t=t>0?e.lastIndexOf(").toFixed(",t-1):-1){const s=t+10-1,n=Be(e,s),r=e.lastIndexOf("Number(",t);n<0||r<0||Be(e,r+7-1)!==t||(e=e.slice(0,r)+`fixed(${e.slice(r+7,t)}, ${e.slice(s+1,n)})`+e.slice(n+1))}return e.replace(/\(\(\)=>\{let (sum|prod) = [01]; let loopCount = 0; for \(let (\w+)=(.*?);\2<=(.*?);\2\+\+\) \{if \(loopCount\+\+ > 1000000\)\{break\};\1 [+*]= (.*?);\}; return \1;\}\)\(\)/g,(e,t,s,n,r,i)=>`(\\${"sum"===t?"sum":"prod"}_{${s}=${n}}^{${r}} (${i}))`)}(e)).replace(/(?<![\w.])9e999\b/g,"∞")).replace(/(?<![\w.])(\d+(?:\.\d+)?)[eE]([+-]?\d+)(?![\w.])/g,"($1*10^{$2})"))))))).replace(/\b(?:True|TRUE)\b/g,"true").replace(/\b(?:False|FALSE)\b/g,"false").replace(/\b(?:None|NULL)\b/g,"null")).replace(/\^\^/g," \\xor ")).replace(/%(?=\s*(?:[(-]|[A-Za-z_][\w.]*\s*\())/g," \\mod ")):e}],["english",function(e){if(!Et.test(e))return e;let t=e;const s=(e,s,n,r="∈")=>{t=t.replace(new RegExp(String.raw`(?<![\w.])${xt(e)}\s+(${$t})\s+(?:and|to)\s+(${$t})(?:\s+(inclusive|exclusive))?(?![\w.])`,"gi"),(e,t,i,o)=>{const[a,l]="exclusive"===o?.toLowerCase()?["(",")"]:"inclusive"===o?.toLowerCase()?["[","]"]:[s,n];return` ${r} ${a}${t}, ${i}${l} `})};s("is not strictly between","(",")","∉"),s("is not between","[","]","∉"),s("is strictly between","(",")"),s("is between","[","]"),s("between","[","]"),t=t.replace(new RegExp(String.raw`(?<![\w.])is\s+(not\s+)?in\s+the\s+(open|closed|half-open)\s+interval\s+from\s+(${$t})\s+to\s+(${$t})`,"gi"),(e,t,s,n,r)=>{const[i,o]={open:["(",")"],closed:["[","]"],"half-open":["[",")"]}[s.toLowerCase()];return` ${t?"∉":"∈"} ${i}${n}, ${r}${o} `});const n=String.raw`(${$t})\s+`;t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?within\s+(${$t})\s*(%|\s+percent)?\s+of\s+(${$t})`,"gi"),(e,t,s,n,r,i)=>`(abs((${t}) - (${i})) ${s?">":"≤"} ${r?`(${n})/100 * ${/^\d+(\.\d+)?$/.test(i)?i:`abs(${i})`}`:`(${n})`})`),t=t.replace(new RegExp(n+String.raw`is\s+(?:a\s+)?multiple\s+of\s+(${$t})`,"gi"),"(($1) mod ($2) = 0)"),t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?divisible\s+by\s+(${$t})`,"gi"),(e,t,s,n)=>`((${t}) mod (${n}) ${s?"≠":"="} 0)`),t=t.replace(new RegExp(String.raw`(${$t})\s+divides\s+(${$t})`,"gi"),"(($2) mod ($1) = 0)"),t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?(even|odd)(?![\w.])`,"gi"),(e,t,s,n)=>`((${t}) mod 2 ${"even"===n.toLowerCase()!==Boolean(s)?"=":"≠"} 0)`),t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?a\s+whole\s+number(?![\w.])`,"gi"),(e,t,s)=>s?`((${t}) ∉ ℤ ∨ (${t}) < 0)`:`((${t}) ∈ ℤ & (${t}) ≥ 0)`);for(const[e,s]of At)t=t.replace(yt(e),` ${s} `);t=t.replace(yt("is not in")," ∉ ").replace(yt("is in")," ∈ ").replace(yt("is an element of")," ∈ ");for(const[e,s]of bt)t=t.replace(yt(e),` ${s} `);t=t.replace(yt("is not")," ≠ "),t=t.replace(/(?<=[\w.)\]}′]\s+)is(?=\s+[\w.(\[{-])/gi," = ");for(let e=!0;e;){const s=t;t=t.replace(new RegExp(String.raw`(${$t})\s*(${wt})\s*(${$t})\s+(and|or)\s+(${wt})`,"gi"),"$1 $2 $3 $4 $1 $5"),e=t!==s}t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?absolute\s+value\s+of\s+(${$t})`,"gi"),"abs($1)"),t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?square\s+root\s+of\s+(${$t})`,"gi"),"sqrt($1)"),t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?cube\s+root\s+of\s+(${$t})`,"gi"),"root(3, $1)"),t=t.replace(new RegExp(String.raw`(${$t})\s+to\s+the\s+power\s+of\s+(${$t})`,"gi"),"($1)^($2)"),t=t.replace(new RegExp(String.raw`(${$t})\s+to\s+the\s+(${Object.keys(vt).join("|")}|(\d+|n)(?:st|nd|rd|th))(?:\s+power)?(?![\w.])`,"gi"),(e,t,s,n)=>`(${t})^(${vt[s.toLowerCase()]??n})`),t=t.replace(new RegExp(String.raw`(${$t})\s+to\s+the\s+(${$t})(?![\w.])`,"gi"),"($1)^($2)");for(const[e,s]of kt)t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?${xt(e)}\s+of\s+(${$t})`,"gi"),`${s}($1)`);t=t.replace(new RegExp(String.raw`(${$t})\s+squared(?![\w.])`,"gi"),"($1)^2"),t=t.replace(new RegExp(String.raw`(${$t})\s+cubed(?![\w.])`,"gi"),"($1)^3"),t=t.replace(new RegExp(String.raw`(?<![\w.])half\s+of\s+(${$t})`,"gi"),"($1)/2"),t=t.replace(new RegExp(String.raw`(?<![\w.])twice\s+(${$t})`,"gi"),"2*($1)"),t=t.replace(new RegExp(String.raw`(\d+(?:\.\d+)?)\s+percent(?![\w.])`,"gi"),"$1%");const r=e=>new RegExp(String.raw`(?<=[\w.′)\]}]\s+)${xt(e)}(?=\s+[\w.(\[{-])`,"gi");for(const[e,s]of[["divided by","/"],["over","/"],["multiplied by","*"],["times","*"],["plus","+"],["minus","-"],["modulo","mod"]])t=t.replace(r(e),` ${s} `);return t}],["words",_t],["spaced",function(e){e=function(e){if(/\\[io]*int|∫|(?<![A-Za-z])int\(|\\partial|∂|(?<![A-Za-z])d\s*\/\s*d|frac\s*\{\s*(?:\\mathrm\s*\{\s*d\s*\}|\\operatorname\s*\{\s*d\s*\}|d)/.test(e))return e;const t=[];for(const s of e.matchAll(B)){const n=e.slice(0,s.index),r=e.slice(s.index+s[0].length),i="d"!==s[1];if(!i&&(/[A-Za-z0-9\\^_/]$|frac\s*\{$/.test(n)||/^\s*[/}]/.test(r)&&!/\{\s*$/.test(n)))continue;const o=i||/\\[,;:! ]\s*$/.test(n);t.push({m:s,strong:o})}const s=t.length>=2?t:t.filter(e=>e.strong);for(const{m:t}of s.reverse())e=e.slice(0,t.index)+W+t[2]+e.slice(t.index+t[0].length);return e}(e=function(e){return e.replace(/(?:\{\s*\}|(?<![\w}\])′]))_\s*(?:\{\s*([^{}]+?)\s*\}|([A-Za-z0-9]+?))\s*(?:\\mathrm\{([CP])\}|([CP]))\s*_\s*(?:\{\s*([^{}]+?)\s*\}|([A-Za-z0-9]))/g,(e,t,s,n,r,i,o)=>`${"C"===(n??r)?"comb":"perm"}(${t??s},${i??o})`)}(function(e){return e.replace(/(?<![A-Za-z\\])([A-Za-z])\^\s*\{?\s*\(\s*([1-9]\d*)\s*\)\s*\}?(?=\s*\()/g,(e,t,s)=>{if(s.length>3||Number(s)>Y)throw new Error(`A derivative f^{(n)} is read up to n = ${Y}`);return t+"′".repeat(Number(s))})}(function(e){return e.replace(/(?<![A-Za-z\\])(arc)?(sine|cosine|tangent|secant|cosecant|cotangent)(?![A-Za-z])(?=\s*\(|\s+(?:[A-Za-z](?![A-Za-z])|\d|\\))/gi,(e,t,s)=>(t?"arc":"")+K[s.toLowerCase()])}(e=function(e){for(let t=e.lastIndexOf("\\sqrt");t>=0;t=t>0?e.lastIndexOf("\\sqrt",t-1):-1){let s=t+5;if(/[A-Za-z]/.test(e[s]??""))continue;for(;T(e[s]);)s++;const n=e.slice(s);let r=/^(?:§\d+§|-?(?:\d+(?:\.\d+)?|\.\d+)|-?[A-Za-zͰ-Ͽ])/.exec(n)?.[0];if(!r&&/^\\[A-Za-z]/.test(n)&&!n.startsWith("\\left")){let e=/^\\[A-Za-z]+/.exec(n)[0].length;if(/^\d\d/.exec(n.slice(e))&&/frac$/.test(n.slice(0,e)))e+=2;else{const t=/^\s*\[[^\[\]{}]*\]/.exec(n.slice(e));for(t&&(e+=t[0].length);;){let t=e;for(;T(n[t]);)t++;if("{"!==n[t])break;let s=0,r=-1;for(let e=t;e<n.length;e++)if("{"===n[e])s++;else if("}"===n[e]&&0===--s){r=e;break}if(r<0)break;e=r+1}}r=n.slice(0,e)}r&&(e=`${e.slice(0,t)}\\sqrt{${r}}${e.slice(s+r.length)}`)}return e}(function(e){return e=e.replace(re,(e,t,s)=>`(${t}+${ne[s]})`),e=e.replace(ie,(e,t,s,n)=>Number(s)<Number(n)&&3!==s.length?`(${t}+${s}/${n})`:e),e.replace(oe,(e,t,s,n,r,i)=>{const[o,a]=[s??r,n??i];return Number(o)<Number(a)?`(${t}+\\frac{${o}}{${a}})`:e})}(function(e){return e.replace(se,e=>{const t=e[0]in ee;return`${t?"^":"_"}{${[...e].map(e=>(t?ee:te)[e]).join("")}}`})}(e))))))),/\\/.test(e)||(e=U(e)),e=pe(e=function(e){for(let t=0;t<e.length;t++){const s=s=>e.slice(Math.max(0,t-s),t+1);if("t"===e[t]&&"\\text"===s(4)&&"{"===e[t+1]){let s=1,n=t+2,r="";for(;s>0&&n<e.length;)"{"===e[n]?s++:"}"===e[n]?s--:r+=e[n],n++;e=e.slice(0,t-4)+r+e.slice(n)}}return e}(e=(e=(e=function(e){for(let t=0;t<100;t++){const t=[...e.matchAll(/\bif\b/gi)];if(!t.length)return e;const s=t[t.length-1].index,n=ce(e,s+2,"then");if(n<0)return e;const r=e.slice(s+2,n),i=ce(e,n+4,"else"),o=he(e,i<0?n+4:i+4),a=e.slice(n+4,i<0?o:i),l=i<0?"unchanged":e.slice(i+4,o).trim();e=`${e.slice(0,s)}((${r.trim()}) ? (${a.trim()}) : (${l}))${e.slice(o)}`}return e}(e=e.replace(/(?<![.\w=])\.\.\.(?![.\w=])/g,"⋯").replace(/∅/g,"\\{\\}"))).replace(/\(\s*\\?b?mod\s+([^()\s](?:[^()]*[^()\s])?)\s*\)/g," \\pmod{$1}")).replace(/(^|[^A-Za-z\\])lim\s+([^\s_{}]+?)\s*(->|→|\\to)\s*([^\s]+)\s+/g,"$1lim_{$2→$4} ")));for(let t=0;t<e.length-1;t++)if("?"===e[t]&&":"!==e[t+1])e=ue(e,t);else if("_"===e[t]||"^"===e[t]){const s=/^§\d+§/.exec(e.slice(t+1));if(s){e=`${e.slice(0,t+1)}{${s[0]}}${e.slice(t+1+s[0].length)}`;continue}const n=("^"===e[t]||/[A-Za-z}]/.test(e[t-1]??""))&&/^\\[A-Za-z]+(?![A-Za-z{\d])/.exec(e.slice(t+1));if(n){e=`${e.slice(0,t+1)}{${n[0]}}${e.slice(t+1+n[0].length)}`;continue}let r=t+1;for("-"===e[r]&&r++;r<e.length&&".0123456789".includes(e[r]);)r++;if(r===t+2&&"-"===e[t+1]&&(r=t+1),"-"===e[t+1]&&/^\s*[\^!]/.test(e.slice(r,r+8)))continue;r>t+1&&(e=e.slice(0,t+1)+"{"+e.slice(t+1,r)+"}"+e.slice(r))}return e}],["squeeze",function(e){return(e=e.replace(/(?<![\d.])\d{1,3}(?:[ \u00A0\u202F]\d{3})+(?![\d])/g,e=>e.replace(/[ \u00A0\u202F]/g,""))).replace(/\s+/g,(t,s)=>{const n=e[s-1]??"",r=e[s+t.length]??"";return"Δ"===n?"":/[A-Za-z\u0370-\u03FF′]/.test(n)&&/[A-Za-z\u0370-\u03FF]/.test(r)||/[A-Za-z\u0370-\u03FF′]/.test(n)&&/^\\[A-Za-z]/.test(e.slice(s+t.length,s+t.length+2))&&!/\\[A-Za-z]+$/.test(e.slice(0,s))||/[A-Za-z\u0370-\u03FF′]/.test(n)&&/\d/.test(r)&&!/\\[A-Za-z]+$/.test(e.slice(0,s))?" ":/\d/.test(n)&&/\d/.test(r)&&!/\\[A-Za-z]+\s*\d*$/.test(e.slice(0,s))?"*":""})}],["symbols",Mt],["bar",function(e){let t=0;for(let s=e.indexOf(d,t);s>=0;s=e.indexOf(d,t)){let n=0,r=-1;for(let t=s+1;t<e.length;t++)if("([{".includes(e[t]))n++;else if(")]}".includes(e[t]))n--;else if(0===n&&e.startsWith("|_",t)){r=t;break}r<0&&qt(),e=`${e.slice(0,s+1)}(${e.slice(s+1,r)})${e.slice(r)}`,t=s+1}return e}],["compact",function(e){if(e=function(e){for(let t=0;t<e.length;t++){const s=t>0&&we.test(e[t-1]);let n,r=null,i=t;if(e.startsWith("frac{",t)){const s=ve(e,t+4),n=s&&ve(e,s[1]);r=n&&Ae(s[0],n[0]),r&&(i=n[1])}else if("d"===e[t]&&!s){let[s,n]=ke(e,t+1),o="";if(we.test(e[n]??"")&&"/"===e[n+1]&&(o=e[n++]),"/"===e[n]&&"d"===e[n+1]&&we.test(e[n+2]??"")){const t=e[n+2],[,a]=ke(e,n+3);r={n:s,of:o,by:t},i=a}}if(r){if(r.of)n=_e(r.of,r.by,r.n);else{const[t,s]=Ee(e,i);if(!t)continue;n=_e(t,r.by,r.n),i=s}e=e.slice(0,t)+n+e.slice(i),t+=3}}return e}(e=function(e){for(let t=e.indexOf(")′");t>=0;t=e.indexOf(")′",t+1)){let s=0,n=-1;for(let r=t;r>=0;r--)if(")"===e[r])s++;else if("("===e[r]&&0===--s){n=r;break}if(n<0)continue;for(;n>0&&/[A-Za-z\u0370-\u03FF′]/.test(e[n-1]);)n--;let r=0;for(;"′"===e[t+1+r];)r++;e=e.slice(0,n)+_e(e.slice(n,t+1),"x",r)+e.slice(t+1+r),t=n}return e}(e=(e=pe(e)).replace(/(?<![A-Za-z])frac(\d)(\d)/g,"frac{$1}{$2}"))),e.split("\\int").length>101)throw new Error("More than 100 integrals");for(let t=0;t<e.length;t++){const s=e[t],n=s=>e.slice(Math.max(0,t-s),t),r=s=>e.slice(Math.max(0,t-s),t+1);if("{"===s){if("\\begin"===n(6)){const s=me(e.slice(t+1));s&&(e=e.slice(0,t-6)+s)}}else if("["===s)"\\sqrt"===n(5)?e=e.slice(0,t-5)+"sqrt"+de(e.slice(t+1)):"sqrt"===n(4)&&(e=e.slice(0,t-4)+"sqrt"+de(e.slice(t+1)));else if("⌊"===s||"⌈"===s)e=e.slice(0,t)+fe(e.slice(t));else if("t"===s)"\\int"===r(3)&&(e=e.slice(0,t-3)+xe(e.slice(t+1)));else if("m"===s)"\\binom"===r(5)&&(e=e.slice(0,t-5)+"\\binom"+be(e.slice(t+1)));else if("d"===s&&"dd"===r(1)&&"("===e[t+2]){const s=ye(e.slice(t+1));s&&(e=e.slice(0,t-1)+"der"+s)}}return(e=e.replace(/\\(?:text|textrm|textit|mathrm|mathit|mathbf|mathsf|operatorname)\{([A-Za-z])\}/g,"$1")).replace(/\\log/g,"log")}],["bars",function(e){for(let t=e.indexOf(d);t>=0;t=e.indexOf(d)){const s=qt;e.startsWith("(der(",t+1)||s();const n=St(e,t+1,"(",")"),r=St(e,t+5,"(",")");(r<0||n!==r+1)&&s();const i=zt(e.slice(t+6,r));(i.length<2||i.length>3||!e.startsWith("|_",n+1))&&s();const o=n;let a,l;"{"===e[o+3]?(l=St(e,o+3,"{","}"),l<0&&s(),a=e.slice(o+4,l)):(l=o+3,a=e[l]??"");const[c,h,u]=i;a.trim()||s();const p=a.includes("=")?a:`${h}=${a}`;e=`${e.slice(0,t)}der(${c},${p}${u?`,${u}`:""})${e.slice(l+1)}`}return e}],["tidy",e=>e.replace(/ {2,}/g," ").trim()]],qt=()=>{throw new Error("A bar with one limit (\\left. … \\right|_{x = a}) is read only after a derivative, \\left.\\frac{d}{dx} f\\right|_{x=a}; with two, \\left. F \\right|_a^b, it is F at b minus F at a")};function St(e,t,s,n){let r=0;for(let i=t;i<e.length;i++)if(e[i]===s)r++;else if(e[i]===n&&0===--r)return i;return-1}function zt(e){const t=[];let s=0,n=0;for(let r=0;r<e.length;r++)"([{".includes(e[r])?s++:")]}".includes(e[r])?s--:","===e[r]&&0===s&&(t.push(e.slice(n,r)),n=r+1);return t.push(e.slice(n)),t}function Nt(e,t={}){if("string"!=typeof e)throw new Error("Expression must be a string");const{text:s,strings:n}=function(e){const t=new Map;let s="",n=0;for(;n<e.length;){const r=e[n];if('"'!==r&&"'"!==r&&"`"!==r){s+=e[n++];continue}let i=n+1,o="",a=!1;for(;i<e.length&&(a||e[i]!==r);)a=!a&&"\\"===e[i],o+=e[i++];const l=qe(t.size);t.set(l,r+o+r),s+=l,n=i+1}return{text:s,strings:t}}(ze(e));let r=s.replace(/\t/g," ");t.evaluate&&(r=function(e,t,s){const n=/\\[A-Za-z]/.test(e);let r=0;for(;;){let i=e.indexOf("{{",r);if(i<0)return e;let o=!1;const a=n&&Lt.test(e.slice(0,i));if(Zt(e,i)||a&&"{"===e[i+2]?i+=1:o=a,!e.startsWith("{{",i)){r=i;continue}const l=Ct(e,i);if(l<0)return e;let c=e.slice(i+2,l).trim();for(const[e,s]of t)c=c.split(e).join(s);try{let n=s(c);if(/^["'`]/.test(n)){const e=qe(t.size);t.set(e,n),n=e}const a=e.slice(0,i).trimEnd(),h=e.slice(l+2).trimStart();/^[\d.]/.test(n)&&(/[\d.]$/.test(a)||/^[\d.]/.test(h))&&(n=`(${n})`),o&&(n=`{${n}}`),e=e.slice(0,i)+" "+n+" "+e.slice(l+2),r=i+1+n.length}catch{r=l+2}}}(r,n,t.evaluate));const i=[["strings",r]];for(const[e,t]of Ot){if(r=t(r),r.length>1e5)throw new Error("The expression expands too much when read");i.push([e,r])}return{cleaned:r,stringMap:n,steps:i}}const Lt=/(?:\\[A-Za-z]+\*?|[\^_}\]])$/;function Zt(e,t){let s=0;for(;"\\"===e[t-1-s];)s++;return s%2==1}function Ct(e,t){let s=0;for(let n=t+2;n<e.length;n++){if(e.startsWith("}}",n)&&0===s)return n;"{"===e[n]?s++:"}"===e[n]&&s--}return-1}const Rt={alpha:"α",beta:"β",gamma:"γ",delta:"δ",epsilon:"ε",zeta:"ζ",eta:"η",theta:"θ",iota:"ι",kappa:"κ",lambda:"λ",mu:"μ",nu:"ν",xi:"ξ",omicron:"ο",pi:"π",rho:"ρ",sigma:"σ",tau:"τ",upsilon:"υ",phi:"φ",chi:"χ",psi:"ψ",omega:"ω",Alpha:"Α",Beta:"Β",Gamma:"Γ",Delta:"Δ",Epsilon:"Ε",Zeta:"Ζ",Eta:"Η",Theta:"Θ",Iota:"Ι",Kappa:"Κ",Lambda:"Λ",Mu:"Μ",Nu:"Ν",Xi:"Ξ",Omicron:"Ο",Pi:"Π",Rho:"Ρ",Sigma:"Σ",Tau:"Τ",Upsilon:"Υ",Phi:"Φ",Chi:"Χ",Psi:"Ψ",Omega:"Ω"};function It(e){for(;"op"===e.type&&e.args.length;)e=e.args[0];return"op"===e.type?"":e.value}function jt(e,t,s){const n=e=>"symbol"===e.type?e.value===t?s:e:"op"!==e.type?e:Tt.has(e.op)&&function(e,t){if("setbuilder"===e.op){const s=e.args[0];return("symbol"===s.type?s.value:s.args?.[0]?.value)===t}const s=e.args[1];return"symbol"===s?.type?s.value===t:"op"===s?.type&&["=","→"].includes(s.op)&&"symbol"===s.args[0]?.type&&s.args[0].value===t}(e,t)?{...e,id:void 0,args:e.args.map((t,s)=>function(e,t,s,n){if("setbuilder"===e)return 0===s&&"op"===t.type&&"∈"===t.op?{...t,id:void 0,args:[t.args[0],n(t.args[1])]}:t;if(0===s)return t;if(1===s)return"op"!==t.type||"="!==t.op&&"→"!==t.op?t:{...t,id:void 0,args:[t.args[0],...t.args.slice(1).map(n)]};return n(t)}(e.op,t,s,n))}:{...e,id:void 0,args:e.args.map(n)};return n(e)}const Tt=new Set(["der","int","lim","\\sumidx","\\prodidx","evaluated","setbuilder"]);function Ft(e){if("op"!==e.type||"∈"!==e.op&&"∉"!==e.op)return null;const[t,s]=e.args;if("op"!==s.type||"setbuilder"!==s.op)return null;let[n,r]=s.args,i=null;if("op"===n.type&&"∈"===n.op&&"symbol"===n.args[0].type&&([n,i]=n.args),"symbol"!==n.type)return null;const o=jt(r,n.value,t),a=i?{type:"op",op:"&",args:[{type:"op",op:"∈",args:[t,i]},o]}:o;return"∉"===e.op?{type:"op",op:"\\not",args:[a]}:a}function Pt(e){const t=function(e){const t=/^(-?)(\d*)\.(\d+)\.\.$/.exec(e);if(!t)return null;const[,s,n,r]=t;let i=1;for(let e=1;2*e<=r.length;e++)if(r.slice(-e)===r.slice(-2*e,-e)){i=e;break}const o=r.slice(-i);let a=r;for(;a.endsWith(o);)a=a.slice(0,-i);return{sign:s,whole:n||"0",prefix:a,block:o}}(e);if(!t)return e.replace(/\.+$/,"");const{sign:s,whole:n,prefix:r,block:i}=t;return`${s}${n}.${(r+i.repeat(Math.ceil(20/i.length)+1)).slice(0,Math.max(r.length+i.length,17))}`}function Wt(e){const t="string"==typeof e&&/^(-?)(\d*)(?:\.(\d*))?%$/.exec(e);if(!t||!t[2]&&!t[3])return null;const s=(t[2]||"0").padStart(3,"0")+(t[3]??""),n=s.length-(t[3]??"").length-2,r=`${s.slice(0,n).replace(/^0+(?=\d)/,"")}.${s.slice(n)}`.replace(/\.?0+$/,"");return t[1]+(""===r?"0":r)}function Bt(e){if("*"!==e.op)return!1;const[t,s]=e.args;return"number"===t.type&&"-1"===t.value&&"-1"!==It(s)}function Ut(e){return"op"!==e.type||o(e.op)?.group?null:"sum"===e.op?"+":"prod"===e.op?"*":Bt(e)?"neg":e.op}const Dt=new Set(["=","===","≠","≠=","<",">","≤","≥","≈","≡","→"]),Gt=(e,t,s)=>("op"!==t.type||"?:"!==t.op||!Dt.has(e.op))&&c(e.op,Ut(t),s);function Ht(e,t){const s=Ut(t),n=s&&o(s)?.prec;return n?"!"===e?n<=o("!").prec:"\\not"===e?n<o("\\not").prec:n<70||es(t)||"\\not"===s:"\\not"!==e&&es(t)}function Qt(e,t,s={}){const n=s.left?"bracket":Kt(e),r=s.right?"bracket":Jt(t);return"digit"!==r&&"sign"!==r&&"word"!==n&&"word"!==r&&"other"!==n&&("frac"!==r||"digit"!==n&&"frac"!==n)}function Vt(e){return 1===e.length||Boolean(Rt[e])||/^Δ.$/.test(e)||/^d[A-Za-z\u03B1-\u03C9]$/.test(e)||/^.′+$/.test(e)}function Xt(e){return"op"===e.type&&!o(e.op)?.group&&(o(e.op)?.prec??100)<70&&!["sum","prod"].includes(e.op)}function Jt(e){if("number"===e.type)return e.value.startsWith("-")?"sign":"digit";if("op"!==e.type)return"symbol"===e.type&&Vt(e.value)?"name":"word";const t=e.op;return"neg"===t||"pm"===t||Bt(e)?"sign":"/"===t?"frac":o(t)?.group||Xt(e)?"bracket":Yt(e)||["der","int","lim","comb"].includes(t)?"call":["^","!","_"].includes(t)?Xt(e.args[0])?"bracket":Jt(e.args[0]):["*","sum","prod","perm"].includes(t)?Jt(e.args[0]):"other"}function Kt(e){if("number"===e.type)return"digit";if("op"!==e.type)return"symbol"===e.type&&Vt(e.value)?"name":"word";const t=e.op;return"/"===t?"frac":"^"===t||"_"===t?"script":o(t)?.group||Xt(e)||Yt(e)||["der","int","lim","comb","perm"].includes(t)?"bracket":["*","neg","pm","sum","prod"].includes(t)?Kt(e.args[e.args.length-1]):"other"}function Yt(e){if("op"!==e.type)return!1;const t=o(e.op);return"prefix"===t?.fixity&&100===t.prec}function es(e){if("number"===e.type)return e.value.startsWith("-");if("op"!==e.type)return!1;if("neg"===e.op||Bt(e))return!0;const t=o(e.op);return"infix"===t?.fixity&&!Gt(e,e.args[0],"left")&&es(e.args[0])}const ts=e=>"symbol"===e.type&&1===e.value.length&&/[a-zA-Zʹ-Ͽ]/.test(e.value),ss=e=>e.slice(2),ns=new Set(["{}","()","[]","(]","[)","setbuilder","∪","∩"]);function rs(e){if("op"!==e.type||"∈"!==e.op&&"∉"!==e.op||2!==e.args.length)return null;const[t,s]=e.args;return"string"===s.type||e.textSearch?{text:t,within:s}:"string"!==t.type||("op"===s.type?ns.has(s.op):"symbol"===s.type&&/^[ℝℤℕℚℂ∅]$/u.test(s.value))?null:{text:t,within:s}}const is=e=>"string"==typeof e&&e.startsWith("\\:")&&"startswith"===e.slice(2).toLowerCase(),os=e=>(null==e?"":String(e)).replace(/[A-Z]/g,e=>String.fromCharCode(e.charCodeAt(0)+32));function as(e){if(2!==e.args.length)throw new Error('startswith takes a value and the text it starts with: startswith(name, "Mc")');return e.args}function ls(e){const t="op"===e.args[1].type&&"="===e.args[1].op?e.args[1]:null,s=t?t.args[0]:e.args[1],n=t?t.args[1]:null;let r=1,i=e.args[0];for(;"op"===i.type&&"der"===i.op&&cs(i.args[1],s);)r++,i=i.args[0];return{n:r,body:i,by:s,at:n,variable:"symbol"===i.type&&Vt(i.value)}}const cs=(e,t)=>e.type===t.type&&e.value===t.value&&"op"!==e.type,hs=new Set(["<",">","≤","≥","=","===","≠","≠=","&","|","\\not","\\xor","\\nand","\\nor","\\implies","\\iff","∈","∉","≈","≡"]);function us(e){return"op"===e.type&&(!!("="===e.op&&(t=e.args[1],"op"===t.type&&hs.has(t.op)||"symbol"===t.type&&("true"===t.value||"false"===t.value)))||e.args.some(us));var t}const ps=new Set(["<",">","≤","≥","=","≠"]);function fs(e){if("op"!==e.type||!ps.has(e.op)||2!==e.args.length)return null;const[t,s]=e.args;if("op"!==t.type||!ps.has(t.op)||2!==t.args.length)return null;return{type:"op",op:"&",args:[fs(t)??t,{type:"op",op:e.op,args:[t.args[1],s]}]}}for(const e of[Rt])Object.setPrototypeOf(e,null);function ds(e){if(e.length>1e6)throw new Error("The output would be too long");return e}const gs={pmatrix:["(",")"],bmatrix:["[","]"],vmatrix:["|","|"]};function ms(e){if(!e||"op"!==e.type)return null;if(Object.prototype.hasOwnProperty.call(gs,e.op))return e.args.map(e=>"op"===e.type&&"[]"===e.op?e.args:[e]);if("[]"!==e.op||!e.args.length||!e.args.every(e=>"op"===e.type&&"[]"===e.op))return null;const t=e.args[0].args.length;return!t||!e.args.every(e=>e.args.length===t)||e.args.length<2&&t<2?null:e.args.map(e=>e.args)}const $s=Object.keys(g).filter(e=>100===g[e]&&/^[a-z]{2,}$/.test(e)),xs=["frac"],ys=[...Object.keys(Rt),"pi","tau","inf","infinity","exp"],bs="\n cost costs coast cosine cosines cosy sine sines since sincere sinus single\n tank tanks tango stand stank\n point points print prints paint paints pint pints mint hint hints lint tint saint faint joint into\n spin spine pipe pipes pine pines pick picks pink pity pie pies pilot pivot pixel pizza piano\n order orders under wonder border render older elder ladder cider rider wider bidder\n logic logo logs blog clog login slog\n model models mode modes modal modest modern module modules\n limb limbs slim lime limo limit limits climb\n maxi minute minutes mince mind mine mines\n sums sumo summit rounds floors ceils means meant sect sects secs expo kiln\n meta metal metals taxi must mute muse nut nuts\n".trim().split(/\s+/),ws=new Set([...ys,...bs,"simplify","expand","factor","cancel","diff","integrate","solve","approx","limit","true","false","null","undefined","unchanged","deg","mod","pmod","NaN","Infinity"]),vs=/^[a-zA-ZͰ-Ͽ]$/,ks=new Set(["f","g","h","u","v","F","G","H"]),As=e=>$s.includes(e)||xs.includes(e),Es=[...$s,...xs,...ys].sort((e,t)=>t.length-e.length),_s=[...$s,...xs,"pi"].sort((e,t)=>t.length-e.length);function Ms(e,t=Es){const s=[];let n=0;for(;n<e.length;){const r=t.find(t=>e.startsWith(t,n))??e[n];s.push(r),n+=r.length}return s}function Os(e){if(As(e))return!0;const t=Ms(e,_s);return t.length>1&&As(t[t.length-1])&&!ws.has(e)}function qs(e,{mode:t="plain",followed:s=!0}={}){if(e.includes("′")&&/^[a-zA-Z′]+$/.test(e)&&!/^.′+$/.test(e))return e.match(/[a-zA-Z]′*/g);if(!((n=e).length>0&&[...n].every(e=>vs.test(e)))||1===e.length||ws.has(e)||As(e))return[e];var n;if("Δ"===e[0]&&2===e.length)return[e];if("latex"===t)return Ss(Ms(e),s);if(e.length<=2)return[...e];const r=Ss(Ms(e,_s),s),i=r.findIndex(e=>e.length>1);if(i<0)return[e];if("pi"===r[i]&&!r.some(e=>$s.includes(e)))return 3===e.length?r:[e];const o=i;let a=0,l=0;for(const e of r.slice(i))a=1===e.length?a+1:0,l=Math.max(l,a);return o<=1&&l<=2?r:[e]}function Ss(e,t){const s=e[e.length-1];return t||!$s.includes(s)?e:[...e.slice(0,-1),...s]}const zs="§",Ns=/[A-Za-zͰ-Ͽ′]/,Ls=e=>/^[A-Za-zͰ-Ͽ][A-Za-zͰ-Ͽ′]*$/.test(e);function Zs(e,t="plain"){if("string"!=typeof e)throw new Error("Tokenizer input must be a string");const s=[];let n="";const i=e=>{n&&(Ls(n)?s.push(...function(e,t,s){if((e=>Se.test(e))(e))return[e];if(x.includes(t))return function(e,t){if(/^[a-zA-Z]′+$/.test(e))return[r+e];if("if"===e.toLowerCase())return["\\:if"];if(1===e.length)return ks.has(e)?[r+e]:[e];if(m(e))return[e];const s=qs(e,{mode:t,followed:!0});return s.length>1?s:e.length<=2?[...e]:[r+e]}(e,s);const n=void 0!==t&&!/[+\-*/=<>),\]}|!≤≥≠]/.test(t);return qs(e,{mode:s,followed:n})}(n,e,t)):s.push(n),n="")};for(let t=0;t<e.length;t++){const o=e[t];if(o===zs){i(o);const n=e.indexOf(zs,t+1);if(n<0)throw new Error(`An unmatched ${zs}`);const a=e.slice(t,n+1);s.push(x.includes(e[n+1])?r+a:a),t=n;continue}if("ⅆ"===o&&/[A-Za-z\u0370-\u03FF]/.test(e[t+1]??""))n&&i(o),n="",s.push("d"+e[t+1]),t+=1;else{if(Ls(n)&&/[0-9]/.test(o)&&m(n+o)){const{op:r,end:i}=Cs(e,t,n+o);s.push(r),n="",t=i;continue}if(Ns.test(o)&&"′"!==o&&b(n)&&(s.push(n),n=""),Ns.test(o)&&(""===n?"′"!==o:Ls(n)))n+=o;else if(/[0-9]/.test(o)&&Ls(n)&&!Os(n))n+=o;else if(Ls(n)&&i(" "===o?e[t+1]:o),"(){}[]".includes(o))n&&s.push(...Rs(n,o)),n="",s.push(o);else if("_"!==o&&" "!==o){if(m(n+o)){const{op:r,end:i}=Cs(e,t,n+o);s.push(r),n="",t=i;continue}if(m(o)){i(o);const{op:n,end:r}=Cs(e,t,o);s.push(n),t=r;continue}n+=o}else i(e[t+1]),"_"===o&&s.push("_")}}if(i(void 0),s.some(e=>/^:/.test(e)))throw new Error("Unmatched ':'");return s}function Cs(e,t,s){let n=t;for(;n+1<e.length&&m(s+e[n+1]);)s+=e[n+1],n++;return"!"===s&&"_"===e[n+1]&&n++,{op:s,end:n}}function Rs(e,t){if(!x.includes(t))return[e];if("\\"===e[0]){const t=e.slice(1);return[m(t)?t:r+t]}return b(e)?[e,"*"]:m(e)||As(e)||b(e[0])?[e]:[r+e]}const Is=70,js=100,Ts="%mod",Fs=/^d[A-Za-z\u0370-\u03FF]$/,Ps=e=>x.includes(e),Ws=e=>y.includes(e),Bs=e=>i(e)||g[e]===js&&"_"!==e,Us=new Set(["\\comb","\\perm","comb","perm"]),Ds={frac:"/","\\binom":"\\binom"},Gs=new Set(["<",">","≤","≥","=","==","===","≠","≠=","!=","!==","&","|","\\not","\\xor","\\implies","\\iff","∈","∉","true","false"]),Hs={"\\comb":"comb","\\perm":"perm",deg:"°","π":"pi","∨":"|"};class Qs{constructor(e){this.tokens=e,this.pos=0,this.out=[],this.absDepth=0,this.frames=[]}peek(e=0){return this.tokens[this.pos+e]}next(){return this.tokens[this.pos++]}emit(...e){this.out.push(...e)}parseAll(){if(!this.tokens.length)throw new Error("Empty expression");this.parseExpression(0);const e=this.peek();if(void 0!==e)throw new Error(Ws(e)?`Unmatched '${e}'`:`Unexpected '${e}'`);return this.out}parseExpression(e,t=!1){this.parseOperand(),this.absorb(e,t)}absorb(e,t=!1){for(;;){const s=this.peek();if(void 0===s||Ws(s))return;const n=this.operatorAt(s,t);if(!n||n.lbp<=e)return;n.run()}}operatorAt(e,t){if("|"===e)return this.pipe();if(","===e)return{lbp:5,run:()=>{this.next(),this.frames.length&&(this.frames[this.frames.length-1].comma=!0),this.parseExpression(5),this.emit(",")}};if("?"===e)return{lbp:9,run:()=>this.ternary()};if("mod"===e)return{lbp:g.mod,run:()=>this.modulo()};if(e===Ts)return{lbp:Is,run:()=>{this.next(),this.parseExpression(Is),this.emit("mod")}};if("pmod"===e)return{lbp:g.pmod,run:()=>{this.next(),this.parseScript(),this.emit("pmod")}};if("?:"===e)return{lbp:10,run:()=>{throw new Error("Unmatched ':'")}};if("!"===e)return{lbp:90,run:()=>{this.next(),this.emit("!")}};if("_"===e)return{lbp:js,run:()=>{this.next(),this.parseScript(),this.emit("_")}};if(Us.has(e))return{lbp:js,run:()=>{this.next(),this.parseExpression(js),this.emit(e)}};const s=g[e];if("="===e&&void 0!==s)return{lbp:s,run:()=>this.equals(s)};if(void 0!==s&&!Bs(e)&&"\\not"!==e&&!(e in Ds)){const t=$.has(e)?s-1:s;return{lbp:s,run:()=>{this.next(),this.parseExpression(t),this.emit(e)}}}return this.startsOperand(e)?this.implicit(e,t):null}implicit(e,t){return{lbp:!t||Bs(e)||Fs.test(e)?Is:71,run:()=>{this.parseExpression(Is),this.emit("*")}}}startsOperand(e){return void 0===e||Ws(e)||"|"===e?"|"===e:e!==Ts&&(!!(Ps(e)||Bs(e)||e in Ds)||!m(e))}pipe(){if(this.absDepth>0)return null;let e=0;for(let t=this.pos;t<this.tokens.length;t++)"|"===this.tokens[t]&&e++;const t="|"===this.peek(1);return e%2==0&&!t&&this.startsOperand(this.peek(1))?this.implicit("|",!1):{lbp:g["|"],run:()=>{this.next(),t&&e%2==0&&this.next(),this.parseExpression(g["|"]),this.emit("|")}}}modulo(){this.next(),this.parseExpression(Is,!0),this.emit("mod")}equals(e){this.next(),this.parseExpression(e),Gs.has(this.out[this.out.length-1])&&this.questionAhead()&&this.absorb(8),this.emit("=")}questionAhead(){let e=0;for(let t=this.pos;t<this.tokens.length;t++){const s=this.tokens[t];if(Ps(s))e++;else if(Ws(s)){if(0===e--)return!1}else{if(0===e&&","===s)return!1;if(0===e&&"?"===s)return!0}}return!1}ternary(){if(this.next(),this.parseExpression(10),"?:"!==this.next())throw new Error("Unmatched '?'");this.parseExpression(8),this.emit("?:")}parseOperand(){const e=this.next(),t=this.tokens[this.pos-2],s=this.peek();if(void 0===e)throw new Error("An operator is missing an operand at the end");if(Ps(e))return this.bracket(e,"{"===e&&("∈"===t||"∉"===t));if("|"===e)return this.absolute();if("-"===e||"+"===e){return"^"===t&&(void 0===s||Ws(s))||Ps(t)&&Ws(s)?this.emit("\\"+e):"+"===e?this.parseExpression(75):this.negate()}if("±"===e)return this.parseExpression(75),this.emit("pm");if("!"===e||"\\not"===e)return this.parseExpression(85),this.emit("\\not");if(e in Ds)return this.parseScript(),this.parseScript(),this.emit(Ds[e]);if("log"===e)return this.logarithm();if("lim"===e&&"_"===s)return this.limit();if(("\\sumidx"===e||"\\prodidx"===e)&&"_"===s)return this.bigOperator(e);if(Bs(e))return this.functionCall(e);if(e===Ts)throw new Error("'%' is missing an operand");if(Ws(e)||m(e))throw new Error(`'${e}' is missing an operand`);this.emit(e)}negate(){const e=this.out.length;this.parseExpression(75);const t=this.out.slice(e);1!==t.length||!b(t[0])&&"∞"!==t[0]?this.emit("neg"):this.out[e]="-"+t[0]}bracket(e,t=!1){if(Ws(this.peek()))return void this.emit("∅",e+this.next());this.frames.push({comma:!1}),this.parseExpression(0);const{comma:s}=this.frames.pop(),n=this.peek();Ws(n)&&(this.next(),s?this.emit(e+n):t&&this.emit("{}"))}absolute(){this.absDepth++,this.parseExpression(0),this.absDepth--,"|"===this.peek()&&this.next(),this.emit("abs")}parseScript(){const e=this.peek();if(Ps(e))return this.parseOperand();if("-"===e||"+"===e)return this.parseOperand();if(Bs(e))return this.parseOperand();if(this.next(),void 0===e||Ws(e)||m(e))throw new Error("Missing a sub- or superscript");this.emit(e)}logarithm(){"_"===this.peek()?(this.next(),this.parseScript()):this.emit("e"),this.argument(),this.emit("log")}limit(){this.next();const e=this.out.length;this.parseScript();const t=this.out.splice(e);this.parseExpression(g["+"],!0),this.emit(...t,",","()","lim")}bigOperator(e){this.next();const t=this.out.length;this.parseScript();let s=this.out.splice(t);"^"===this.peek()&&(this.next(),this.parseScript(),s=[...s,...this.out.splice(t),","]),this.parseExpression(g["+"],!0),this.emit(...s,",","()",e)}functionCall(e){if(void 0===this.peek())return this.emit(e);let t=null;if("^"===this.peek()){this.next();const e=this.out.length;this.parseScript(),t=this.out.splice(e)}if(0===o(e)?.arity&&!Ps(this.peek()))return this.emit(e);this.argument(),this.emit(e),t&&this.emit(...t,"^")}argument(){const e=this.peek();if(Ps(e))return this.next(),this.bracket(e);this.parseExpression(Is,!0)}}function Vs(e,t){e=function(e){const t=[];for(let s=0;s<e.length;s++){const n=e[s],i=e=>"string"==typeof e&&/^[a-zA-Z]$/.test(e);if(ks.has(n)&&"^ { - 1 } ("===e.slice(s+1,s+7).join(" ")){t.push(r+n+"⁻¹"),s+=5;continue}const[o,a,l]=e.slice(s+1,s+4),c=i(a)?a!==n:b(a);i(n)&&"e"!==n&&"("===o&&c&&")"===l?t.push(r+n):t.push(n)}return t}(e);const s=[];for(const n of e)m(n)||Ps(n)||Ws(n)||"_"===n||i(n)||b(n)?s.push(n):s.push(...Xs(n,t));return s}function Xs(e,t){if("%"===e)return[Ts];if(!e.includes("%"))return Js(e,t);const s=e.split("%");s.length>1&&""===s[s.length-1]&&(s.pop(),s[s.length-1]+="%");const n=[];return s.forEach((e,s)=>{s>0&&n.push(Ts),""!==e&&n.push(...Js(e,t))}),n}function Js(e,t){let s=0;for(;s<e.length&&b(e.slice(0,s+1));)s++;if(0===s||s===e.length)return[e];const n=e.slice(s);return"%"===n?[e]:[e.slice(0,s),...(r=n,Vs(Zs(r,t),t))];var r}for(const e of[Ds,Hs])Object.setPrototypeOf(e,null);const Ks=new Set(["()","[]","{}","(]","[)"]),Ys={pow:"^",sqrtpow:"root","\\binom":"comb","\\comb":"comb","\\perm":"perm",frac:"/","==":"=","!==":"≠=",...Object.fromEntries(Object.keys(f).map(e=>[e,"*"]))},en=new Set(["round","fixed","der"]),tn=e=>i(e)||100===g[e];function sn(t){const s=[],n=e=>{if(!s.length)throw new Error(`'${e}' is missing an operand`);return s.pop()},r=e=>e.list?e.list:[e],i=(e,t)=>{if(e.list)throw new Error(`A list of values needs brackets around it (near '${t}')`);return e};if(t.forEach((a,l)=>{if("∅"===a)return void s.push({list:[]});if(","===a){const e=n(a),t=n(a);return void s.push({list:[...r(t),...r(e)]})}if(Ks.has(a)){const e=r(n(a));if(!e.length&&"{}"!==a&&!tn(t[l+1]))throw new Error("Empty brackets");return void s.push(E(a,e))}const c=Ys[a]??a,h=o(c);if(!h){if("?"===a||"abs("===a)throw new Error(`Unmatched '${a[0]}'`);return void s.push(M(a))}let u;if(tn(a)&&Ks.has(t[l-1])&&s[s.length-1]?.op===t[l-1])u=s.pop().args;else{let t=h.arity===e||en.has(c)?1:h.arity;if(s.length<t)throw new Error(`'${a}' needs ${t} operand${1===t?"":"s"}`);u=s.splice(s.length-t,t).map(e=>i(e,a))}const p=function(e,t){switch(e){case"sqrt":if(2===t.length)return E("root",t);break;case"round":if(1===t.length)return E("round",[t[0],k(0)]);break;case"sum":case"prod":{const s=t[1];if("op"===s?.type&&"symbol"===s.args[0]?.type&&(3===t.length&&"="===s.op||2===t.length&&"∈"===s.op))return E("sum"===e?"\\sumidx":"\\prodidx",t);break}case"evaluated":if(3===t.length)return E("evaluated",[t[0],A(L(t[0])||"x"),t[1],t[2]]);break;case"det":if(t.length>1&&t.every(e=>"op"===e.type&&"[]"===e.op))return E("det",[E("[]",t)]);break;case"fixed":if(1===t.length)return E("fixed",[t[0],k(2)]);break;case"der":if(1===t.length)return E("der",[t[0],A("x")]);if(3===t.length)return function([e,t,s]){const n="number"===s.type?Number(s.value):NaN;if(!Number.isInteger(n)||n<1||n>20)throw new Error("der(f, x, n) needs n to be a whole number from 1 to 20");const r="op"===t.type&&"="===t.op?t:null,i=r?r.args[0]:t;let o=e;for(let e=0;e<n;e++)o=E("der",[o,r&&e===n-1?r:i]);return o}(t);break;case"log":if(2===t.length&&"symbol"===t[0].type&&"e"===t[0].value)return E("ln",[t[1]]);break;case"lim":return rn(e,t),E("lim",function(e){const t=e=>{if("op"!==e.type)return null;if("^"===e.op&&"symbol"===e.args[1].type&&nn[e.args[1].value])return{point:e.args[0],side:nn[e.args[1].value]};const s=e.args.length?t(e.args[e.args.length-1]):null;return s&&{point:{...e,args:[...e.args.slice(0,-1),s.point]},side:s.side}},[s,n,r]=e;if(2===e.length&&"op"===n.type&&"→"===n.op){const[e,r]=n.args,i=t(r);if(i)return[s,e,i.point,A(i.side)]}const i=3===e.length&&t(r);return i?[s,n,i.point,A(i.side)]:e}(t));case"\\:if":if(2===t.length||3===t.length)return E("?:",[t[0],t[1],t[2]??A("unchanged")]);throw new Error("if() takes a condition, a value, and optionally an alternative")}return rn(e,t),E(e,t)}(c,u);f[a]&&"*"===p.op&&(p.written=f[a]),s.push(p)}),1!==s.length)throw new Error(s.length?"Some values are not connected by an operator":"Empty expression");const a=["=","<",">","≤","≥","≠","≈","≡","∈","∉"],l=s[0];return l.list&&l.list.length>1&&l.list.every(e=>"op"===e.type&&a.includes(e.op))?l.list.reduce((e,t)=>E("&",[e,t])):i(s[0],"end")}const nn={"\\+":"r","\\-":"l"};function rn(t,s){const{arity:n}=o(t);if(n!==e){if(s.length!==n){throw new Error(`'${t}' takes ${n} argument${1===n?"":"s"}, not ${s.length}`)}}else if(0===s.length&&!i(t))throw new Error(`'${t}' needs at least one argument`)}for(const e of[Ys,nn])Object.setPrototypeOf(e,null);const on=new RegExp(`[${Object.keys(f).join("")}]`,"g"),an=e=>e.replace(on,"*"),ln=/\\(?:text|textrm|textit|mathrm|mathit|operatorname)\{\s*([A-Za-z][A-Za-z0-9 ]*[A-Za-z0-9])\s*\}/g;const cn=e=>/^[a-z][a-z0-9]*$/.test(e)&&m(e);function hn(e){return e.includes("\\")?(e=function(e){for(let t=0;t<50;t++){const t=e.lastIndexOf("\\begin{cases}"),s=t<0?-1:e.indexOf("\\end{cases}",t);if(s<0)return e;const n=le(e.slice(t+13,s),"\\\\").map(e=>le(e,"&")).filter(e=>e.join("").trim());let r=null;const i=/=|<|>|\\(?:le|ge|leq|geq|neq|ne|lt|gt)(?![A-Za-z])/;if(n.length>1&&n.every(e=>1===e.length&&i.test(e[0])))r=n.map(e=>`(${e[0].trim().replace(/\s*[,;.]$/,"")})`).join(" \\land "),e=e.slice(0,t)+`(${r})`+e.slice(s+11);else{for(let e=n.length-1;e>=0;e--){const t=n[e][0].trim().replace(/\s*[,;]$/,""),s=ae(n[e].slice(1).join("&"));r=null!==r||s?`((${s||"true"}) ? (${t}) : ${r??"(unchanged)"})`:`(${t})`}e=e.slice(0,t)+(r??"")+e.slice(s+11)}}return e}(e),(e=function(e){return e=J(e),e=U(e),e=e.replace(/\\(?:[Bb]igg?)[lrm]?(?![A-Za-z])\s*/g,""),e=e.replace(/\^\s*(?:\\circ\b|\{\s*\\circ\s*\})|\\degree\b|\\textdegree\b/g,"°"),e=e.replace(/\\(?:cdots|ldots|dotsb|dotsc|dots)(?![A-Za-z])/g,"⋯"),e=e.replace(/(\d*)\.(\d*)\\overline\s*\{\s*(\d+)\s*\}/g,(e,t,s,n)=>`${t||"0"}.${s}${n}${n}..`),e=e.replace(/\\(?:emptyset|varnothing)(?![A-Za-z])/g,"\\{\\}"),e=e.replace(/\(\s*([A-Za-z])\s*\\circ\s*([A-Za-z])\s*\)\s*\(([^()]*)\)/g,"$1($2($3))"),e}(e)).replace(/\{\}\^\{([^{}]*)\}\s*\\mathrm\{P\}\s*_\{([^{}]*)\}/g,"\\perm($1,$2)")):/[\]|]\s*_/.test(e)?J(e):e}function un(e){pn=e}let pn=null;function fn(e,t={}){const s={original:e};try{if("string"!=typeof e)throw new Error("Expression must be a string");const n=t.maxLength??5e3;if(e.length>n)throw new Error(`Longer than ${n} characters (options.maxLength)`);if(e.includes(""))throw new Error("The character U+E000 can't be read: MathZetta uses it to hold § in strings");if((e=function(e,t,s){if(!e.includes(t))return e;const n=ze(e);let r="";for(let i=0;i<n.length;i++){const o=n[i];if('"'===o||"`"===o||"'"===o){const o=Ne(n,i);r+=e.slice(i,o+1).replaceAll(t,s),i=o}else r+=e[i]}return r}(e,zs,"")).includes(zs))throw new Error(`The character ${zs} can't be read: MathZetta uses it to mark names`);const r=/^\s*=(?!=)/.test(e);r&&(e=e.replace(/("(?:[^"]|"")*")|\$(?=[A-Za-z]{0,3}\d|[A-Za-z]{1,3}\$?\d)/g,(e,t)=>t??""));const i=r?[...new Set(e.replace(/"(?:[^"]|"")*"/g,"").match(/(?<![\w.])[A-Za-z]{1,3}\d+(?::[A-Za-z]{1,3}\d+)?(?![\w.(])/g)??[])]:[],{text:o,protectedNames:a}=function(e,t=[],s=[]){const n=new Map,r=new Map,i=e=>{let t=r.get(e);return void 0===t&&(t=`${zs}${n.size}${zs}`,n.set(t,e),r.set(e,t)),t};e=e.replace(ln,(e,t)=>cn(t.trim())?` ${t.trim()}`:i(t.trim()));for(const s of[...t].sort((e,t)=>t.length-e.length)){const t=s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");e=e.replace(new RegExp(`(?<![A-Za-z_])${t}(?![A-Za-z0-9_])`,"g"),()=>i(s))}for(const t of[...s].sort((e,t)=>t.length-e.length)){const s=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");e=e.replace(new RegExp(`(?<![A-Za-z_])${s}(?![A-Za-z0-9_])(\\s*)(\\(?)`,"g"),(e,s,n)=>i(t)+(n?" * (":s))}return{text:e,protectedNames:n}}(hn(e),t.names,[...t.values??[],...i]),l=(e=>/\\[A-Za-z]/.test(e)?"latex":"plain")(o),{cleaned:c,stringMap:h,steps:u}=function(e,t={}){const s=void 0===t.blocks?pn:t.blocks;return Nt(e,{evaluate:s?e=>s(e,t.scope):void 0})}(o,{blocks:t.blocks,scope:t.scope}),p=e=>Ze(Ze(e,h),a).map(e=>"string"==typeof e?e.replaceAll("",zs):e);s.mode=l,s.cleaned=an(p([c])[0]),s.cleanSteps=u.map(([e,t])=>[e,an(t)]);const f=Zs(c,l);s.tokens=f.map(an);const d=p(function(e,t="plain"){return new Qs(Vs(e,t)).parseAll().map(e=>Hs[e]??e)}(f,l));s.rawPostfix=d.map(an);const g={},m=S(sn(d),g);s.postfix=m,s.written=g,s.tree=q(m),Object.keys(g).length&&N(s.tree,e=>{g[e.id]&&(e.written=g[e.id])})}catch(e){s.error=e instanceof RangeError?"Nested too deeply":e.message}return s}const dn=e=>"string"==typeof e&&e.length>80?`${e.slice(0,77)}…`:e;function gn(e,t={}){const s=fn(e,t);if(t.keepIntermediateSteps)return s;if(!s.error)return s.postfix;if(t.allowUnvalidated&&s.rawPostfix)return s.rawPostfix;throw new Error(`Failed to parse expression "${dn(e)}": ${s.error}`)}function mn(e,t={}){const s=fn(e,t);if(s.error)throw new Error(`Failed to parse expression "${dn(e)}": ${s.error}`);return s.tree}const $n=(e,t)=>"string"==typeof e?mn(e,t):z(e),xn=e=>Array.isArray(e)&&0===e.length,yn=e=>"op"===e.type&&i(e.op)&&["vec","hat"].includes(ss(e.op)),bn={plus:"plus",minus:"minus",times:"times",over:"over",negative:"negative",plusOrMinus:"plus or minus",quantity:"the quantity",of:"of",and:"and",or:"or",not:"not",xor:"exclusive or",nand:"nand",nor:"nor",implies:"implies",iff:"if and only if",squared:"squared",cubed:"cubed",toThe:"to the",toThePowerOf:"to the power of",power:"power",sub:"sub",factorial:"factorial",prime:"prime",doublePrime:"double prime",triplePrime:"triple prime",inverse:"inverse",mod:"mod",percent:"percent",degrees:"degrees",dot:"dot",cross:"cross",vector:"vector",hat:"hat",at:"at",equals:"equals",notEqual:"is not equal to",lessThan:"is less than",greaterThan:"is greater than",lessOrEqual:"is less than or equal to",greaterOrEqual:"is greater than or equal to",approx:"is approximately",congruent:"is congruent to",approaches:"approaches",tendsTo:"approaches",isIn:"is in",isNotIn:"is not in",union:"union",intersect:"intersect",squareRoot:"the square root of",cubeRoot:"the cube root of",nthRoot:e=>`the ${e} root of`,absoluteValue:"the absolute value of",floor:"the floor of",ceiling:"the ceiling of",logBase:"log base",naturalLog:"the natural log of",choose:"choose",permute:"permute",derivative:"the derivative",secondDerivative:"the second derivative",nthDerivative:e=>`the ${e} derivative`,withRespectTo:"with respect to",d:"d",integralOf:"the integral of",integralFrom:"the integral from",to:"to",evaluatedFrom:"evaluated from",setOf:"the set of",suchThat:"such that",limitAs:"the limit as",fromTheRight:"from the right",fromTheLeft:"from the left",sumFrom:"the sum from",sumOver:"the sum over",sumOf:"the sum of",productFrom:"the product from",productOver:"the product over",productOf:"the product of",matrix:(e,t)=>`the ${e} by ${t} matrix`,row:e=>`row ${e}`,determinantOf:"the determinant of",theSet:"the set",theList:"the list",openInterval:"the open interval from",closedInterval:"the closed interval from",halfOpenInterval:"the half-open interval from",comma:",",if:"if",then:"then",otherwise:"otherwise",infinity:"infinity",pi:"pi",e:"e",delta:"delta",randomNumber:"a random number",rounded:"rounded to",decimalPlaces:"decimal places",nearestWholeNumber:"the nearest whole number",writtenTo:"written to",asWholeNumber:"written as a whole number",reals:"the real numbers",integers:"the integers",naturals:"the natural numbers",rationals:"the rational numbers",complexes:"the complex numbers",repeating:"repeating",andSoOn:"dot dot dot",emptySet:"the empty set",eToThe:"e to the",endQuantity:"end quantity",endFraction:"end fraction",endRoot:"end root",endExponent:"end exponent",endSub:"end sub",endAbsoluteValue:"end absolute value",endFloor:"end floor",endCeiling:"end ceiling",endLimit:"end limit",endSum:"end sum",endMatrix:"end matrix",endProduct:"end product",endDerivative:"end derivative",half:"half",halves:"halves",ordinals:["","","half","third","quarter","fifth","sixth","seventh","eighth","ninth","tenth"],numerators:["","one","two","three","four","five","six","seven","eight","nine"],powers:["","first","squared","cubed","fourth","fifth","sixth","seventh","eighth","ninth","tenth"]},wn={sin:"sine",cos:"cosine",tan:"tangent",csc:"cosecant",sec:"secant",cot:"cotangent",sinh:"hyperbolic sine",cosh:"hyperbolic cosine",tanh:"hyperbolic tangent",arcsin:"inverse sine",arccos:"inverse cosine",arctan:"inverse tangent",arcsec:"inverse secant",arccsc:"inverse cosecant",arccot:"inverse cotangent",arcsinh:"inverse hyperbolic sine",arccosh:"inverse hyperbolic cosine",arctanh:"inverse hyperbolic tangent",ln:"the natural log",log:"log",loglog:"the log of the log",logloglog:"the log of the log of the log",expm1:"e to the power, minus 1, of",max:"the maximum",min:"the minimum",gcd:"the greatest common divisor",mean:"the mean",det:"the determinant",atan2:"the arctangent",round:"round",fixed:"fixed",random:"random"},vn=Object.fromEntries(Object.entries(Rt).map(([e,t])=>[t,e]));function kn(e,t={}){return En(function(e,t={}){return An(e,t,!0)}(e,t))}function An(e,t,s){return function(e){let t=e.length;for(;t>0&&(e[t-1].close||!e[t-1].text.replace(/,/g,"").trim());)t--;const s=[];for(const{text:n,id:r}of e.slice(0,t)){const e=n.trim();if(!e)continue;const t=e.startsWith(","),i=t?e.slice(1).trim():e;t&&s.length&&!s[s.length-1].text.endsWith(",")&&(s[s.length-1].text+=","),i&&s.push({text:i,id:r})}s.length&&(s[s.length-1].text=s[s.length-1].text.replace(/,$/,""));return s}(new On({...bn,...t.words},{...wn,...t.names},t.say||{},s).speak(e))}function En(e){return e.map(e=>e.text).join(" ").replace(/\s+/g," ").replace(/ ,/g,",").replace(/,(\s*,)+/g,",").replace(/^,\s*/,"").replace(/,\s*$/,"").trim()}const _n={"=":"equals","===":"equals","≠":"notEqual","≠=":"notEqual","<":"lessThan",">":"greaterThan","≤":"lessOrEqual","≥":"greaterOrEqual","≈":"approx","≡":"congruent","→":"tendsTo","∈":"isIn","∉":"isNotIn","∪":"union","∩":"intersect","±":"plusOrMinus","&":"and","|":"or","\\xor":"xor","\\nand":"nand","\\nor":"nor","\\implies":"implies","\\iff":"iff"},Mn={"ℝ":"reals","ℤ":"integers","ℕ":"naturals","ℚ":"rationals","ℂ":"complexes"};class On{constructor(e,t,s,n=!1){this.w=e,this.names=t,this.say=s,this.audio=n}speak(e){const t=this.say["op"===e.type?e.op:e.type];if(t){return[{text:t("op"===e.type?e.args.map(e=>En(this.speak(e))):[e.value],e,e=>En(this.speak(e))),id:e.id}]}return"number"===e.type?[this.seg(e,this.number(e.value))]:"string"===e.type?[this.seg(e,e.value.slice(1,-1))]:"symbol"===e.type?[this.seg(e,this.symbol(e.value))]:this.operation(e)}seg(e,t){return{text:t,id:e.id}}number(e){const t=this.w;return e.endsWith("..")?`${e.replace(/\.+$/,"")} ${t.repeating}`:e.startsWith("-")?`${t.negative} ${e.slice(1)}`:e}symbol(e){const t=this.w;if("∞"===e)return t.infinity;if("-∞"===e)return`${t.negative} ${t.infinity}`;if("pi"===e||"π"===e)return t.pi;if("tau"===e||"τ"===e)return"tau";if("\\+"===e)return t.plus;if("\\-"===e)return t.minus;if("°"===e)return t.degrees;if("⋯"===e)return t.andSoOn;if(Mn[e])return t[Mn[e]];if(vn[e])return vn[e].toLowerCase();if(/^Δ.+$/.test(e))return`${t.delta} ${this.symbol(e.slice(1))}`;if(/^d[A-Za-z\u03B1-\u03C9]$/.test(e))return`d ${this.symbol(e[1])}`;const s=/′+$/.exec(e);return s?`${this.symbol(e.slice(0,-s[0].length))} ${this.primes(s[0].length)}`:e.endsWith("⁻¹")?`${this.symbol(e.slice(0,-2))} ${t.inverse}`:e}primes(e){const t=this.w;return[t.prime,t.doublePrime,t.triplePrime][e-1]??`${e} ${t.prime}`}isCompound(e){if("op"!==e.type)return!1;if("sum"===e.op||"?:"===e.op)return!0;const t=l(e.op);return void 0!==t&&t<=50&&!o(e.op).group}quantity(e,t="endQuantity"){return this.isCompound(e)?[this.seg(e,this.w.quantity),...this.speak(e),...this.closer(e,t)]:this.speak(e)}grouped(e,t,s){return this.isCompound(e)||this.audio&&s?[this.seg(e,this.w.quantity),...this.speak(e),...this.closer(e,t)]:this.speak(e)}closer(e,t){const s=this.seg(e,this.w.comma);return this.audio&&t?[s,{text:this.w[t],id:e.id,close:!0},s]:[s]}contents(e,t){return this.audio&&this.isCompound(e)?this.quantity(e,t):[...this.speak(e),...this.pauseAfter(e)]}argument(e){return this.quantity(e)}operation(e){const{op:t,args:s}=e,[n,r]=s,o=this.w,a=t=>this.seg(e,t),l=()=>s.flatMap((e,t)=>t?[a(o.comma),...this.listed(e)]:this.listed(e));if(_n[t])return[...this.speak(n),a(o[_n[t]]),...this.speak(r)];if(i(t)){if(1===s.length&&"symbol"===n.type&&"vec"===ss(t))return[a(o.vector),...this.speak(n)];if(1===s.length&&"symbol"===n.type&&"hat"===ss(t))return[...this.speak(n),a(o.hat)];const e=this.symbol(ss(t));return s.length?[a(`${e} ${o.of}`),...this.arguments(s)]:[a(e)]}if(this.names[t]&&!["log","round","fixed","atan2","random","expm1"].includes(t))return[a(`${this.names[t]} ${o.of}`),...this.arguments(s)];switch(t){case"+":return Bt(r)?[...this.leftSide(n),a(o.minus),...this.quantityIfSum(r.args[1])]:[...this.leftSide(n),a(o.plus),...this.speak(r)];case"-":return Bt(r)?[...this.leftSide(n),a(o.plus),...this.speak(r.args[1])]:[...this.leftSide(n),a(o.minus),...this.quantityIfSum(r)];case"*":return this.product(e);case"/":return this.fraction(e);case"^":return this.power(e);case"_":return[...this.speak(n),a(o.sub),...this.audio?this.grouped(r,"endSub","op"===r.type):this.speak(r)];case"neg":return this.isCompound(n)||"op"===n.type&&"neg"===n.op?[a(`${o.negative} ${o.quantity}`),...this.speak(n),...this.closer(n,"endQuantity")]:[a(o.negative),...this.speak(n)];case"pm":return[a(o.plusOrMinus),...this.speak(n)];case"!":return[...this.grouped(n,"endQuantity","op"===n.type||"number"===n.type&&n.value.startsWith("-")),a(o.factorial)];case"\\not":return[a(o.not),...this.quantity(n)];case"sqrt":return[a(o.squareRoot),...this.contents(n,"endRoot")];case"root":{const e="number"===n.type?Number(n.value):NaN,t=3===e?o.cubeRoot:2===e?o.squareRoot:o.nthRoot(e?this.ordinal(e):En(this.speak(n))+"th");return[a(t),...this.contents(r,"endRoot")]}case"abs":return[a(o.absoluteValue),...this.contents(n,"endAbsoluteValue")];case"floor":return[a(o.floor),...this.contents(n,"endFloor")];case"ceil":return[a(o.ceiling),...this.contents(n,"endCeiling")];case"log":return"symbol"===n.type&&"e"===n.value?[a(`${this.names.ln} ${o.of}`),...this.argument(r)]:[a(`${o.logBase}`),...this.speak(n),a(o.of),...this.argument(r)];case"mod":return[...this.quantity(n),a(o.mod),...this.quantity(r)];case"pmod":return[...this.speak(n),a(`${o.comma} ${o.mod}`),...this.speak(r)];case"perm":return[...this.quantity(n),a(o.permute),...this.quantity(r)];case"comb":return[...this.quantity(n),a(o.choose),...this.quantity(r)];case"round":{const e=0===("number"===r.type?Number(r.value):NaN)?o.nearestWholeNumber:`${En(this.speak(r))} ${o.decimalPlaces}`;return[...this.speak(n),a(`${o.comma} ${o.rounded} ${e}`)]}case"fixed":return 0===("number"===r.type?Number(r.value):NaN)?[...this.speak(n),a(`${o.comma} ${o.asWholeNumber}`)]:[...this.speak(n),a(`${o.comma} ${o.writtenTo}`),...this.speak(r),a(o.decimalPlaces)];case"atan2":return[a(`${this.names.atan2} ${o.of}`),...this.speak(n),a(o.over),...this.speak(r)];case"random":return[a(o.randomNumber)];case"expm1":return[a(o.eToThe),...this.speak(n),a(`${o.comma} ${o.minus} 1`)];case"?:":{const e=[a(o.if),...this.speak(n),a(`${o.comma} ${o.then}`),...this.speak(r)];return(e=>"symbol"===e.type&&"unchanged"===e.value)(s[2])?e:[...e,a(`${o.comma} ${o.otherwise}`),...this.speak(s[2])]}case"sum":return s.flatMap((e,t)=>t?[a(o.plus),...this.speak(e)]:this.speak(e));case"prod":return s.flatMap((e,t)=>t?[a(o.times),...this.quantity(e)]:this.quantity(e));case"mean":case"max":case"min":case"gcd":return[a(`${this.names[t]} ${o.of}`),...l()];case"()":return 2===s.length?[a(o.openInterval),...this.speak(n),a(o.to),...this.speak(r)]:l();case"pmatrix":case"bmatrix":return this.matrix(e);case"vmatrix":return[a(o.determinantOf),...this.matrix(e)];case"[]":return ms(e)?this.matrix(e):2===s.length?[a(o.closedInterval),...this.speak(n),a(o.to),...this.speak(r)]:[a(o.theList),...l()];case"(]":case"[)":return[a(o.halfOpenInterval),...this.speak(n),a(o.to),...this.speak(r)];case"{}":return s.length?[a(o.theSet),...l()]:[a(o.emptySet)];case"setbuilder":return[a(o.setOf),..."op"===s[0].type&&"∈"===s[0].op?[...this.speak(s[0].args[0]),a("in"),...this.speak(s[0].args[1])]:this.speak(s[0]),a(o.suchThat),...this.speak(s[1])];case"evaluated":return[...this.speak(s[0]),a(`${o.comma} ${o.evaluatedFrom}`),...this.speak(s[2]),a(o.to),...this.speak(s[3])];case"der":return this.derivative(e);case"int":return this.integral(e);case"lim":return this.limit(e);case"\\sumidx":return this.bigOperator(e,o.sumFrom,o.sumOver,o.sumOf,"endSum");case"\\prodidx":return this.bigOperator(e,o.productFrom,o.productOver,o.productOf,"endProduct")}throw new Error(`No speech for '${t}'`)}matrix(e){const t=this.w,s=ms(e),n=[this.seg(e,t.matrix(s.length,s[0].length))];return s.forEach((s,r)=>{const i=e.args[r];n.push(this.seg(e,t.comma),this.seg("op"===i.type&&"[]"===i.op?i:e,`${t.row(r+1)}:`)),s.forEach((s,r)=>n.push(...r?[this.seg(e,t.comma)]:[],...this.speak(s)))}),[...n,...this.closer(e,"endMatrix")]}arguments(e){return 1===e.length?this.argument(e[0]):e.flatMap((e,t)=>t?[{text:this.w.comma,id:e.id},...this.listed(e)]:this.listed(e))}listed(e){return this.audio?this.quantity(e):this.speak(e)}pauseAfter(e){return this.isCompound(e)?[this.seg(e,this.w.comma)]:[]}leftSide(e){return this.audio&&"op"===e.type&&"?:"===e.op?[this.seg(e,this.w.quantity),...this.speak(e),...this.closer(e,"endQuantity")]:this.speak(e)}quantityIfSum(e){return this.quantity(e)}product(e){const[t,s]=e.args,n=this.w;if(Bt(e))return this.speak({...e,op:"neg",args:[s]});if("symbol"===s.type&&"°"===s.value)return[...this.speak(t),this.seg(e,n.degrees)];const r=this.audio&&"op"===t.type&&"/"===t.op&&!t.args.every(e=>"number"===e.type&&/^\d$|^10$/.test(e.value))?[this.seg(t,n.quantity),...this.speak(t),...this.closer(t,"endFraction")]:this.quantity(t);this.audio&&"op"===t.type&&"^"===t.op&&"number"!==t.args[1].type&&r.push(...this.closer(t.args[1],"endExponent"));const i=this.quantity(s);if(e.written&&[t,s].some(yn))return[...r,this.seg(e,n[e.written]),...i];return Boolean(e.written)||this.isCompound(t)||this.isCompound(s)||this.isWord(t)||this.isWord(s)||this.startsWithNumber(s)||"op"===s.type&&"/"===s.op||this.isCall(t)||i[0].text.startsWith("the ")?[...r,this.seg(e,n.times),...i]:[...r,...i]}isWord(e){return"symbol"===e.type&&/^[A-Za-z]{2,}$/.test(e.value)&&!Rt[e.value]&&!["pi","tau"].includes(e.value)}isCall(e){return"op"===e.type&&(i(e.op)||"prefix"===o(e.op)?.fixity&&100===l(e.op))}startsWithNumber(e){let t=e;for(;"op"===t.type&&t.args.length;)t=t.args[0];return"number"===t.type}fraction(e){const[t,s]=e.args,n=this.w,r=t=>this.seg(e,t);if("number"===t.type&&"number"===s.type&&/^[1-9]$/.test(t.value)&&/^([2-9]|10)$/.test(s.value)){const e=Number(t.value),i=Number(s.value),o=2===i?1===e?n.half:n.halves:n.ordinals[i]+(1===e?"":"s");return[r(`${n.numerators[e]} ${o}`)]}const i=(e,...t)=>"op"===e.type&&t.includes(e.op)&&!Bt(e),o=this.grouped(t,null,i(t,"/")),a=this.isCall(t)?[r(n.comma)]:[],l=this.speak(s),c=i(s,"/")||this.audio&&i(s,"*")&&l.some(e=>e.id===s.id&&e.text===n.times),h=this.isCompound(s)||this.audio&&c;return[...o,...a,r(n.over),...h?[this.seg(s,n.quantity),...l,...this.closer(s,"endFraction")]:l]}power(e){const[t,s]=e.args,n=this.w,r=t=>this.seg(e,t),i=this.isCompound(t)||this.isCall(t)||"number"===t.type&&t.value.startsWith("-")||"op"===t.type&&"/"===t.op||this.audio&&"op"===t.type&&("^"===t.op||"neg"===t.op||Bt(t))?[r(n.quantity),...this.speak(t),...this.closer(t,"endQuantity")]:this.speak(t);if(this.isCall(t)&&this.names[t.op]&&"number"===s.type&&/^[2-3]$/.test(s.value)&&1===t.args.length){const e=2===Number(s.value)?n.squared:n.cubed;return[r(`${this.names[t.op]} ${e} ${n.of}`),...this.argument(t.args[0])]}if("number"===s.type&&/^(?:[1-9]|10)$/.test(s.value)){const e=Number(s.value);return 2===e||3===e?[...i,r(n.powers[e])]:[...i,r(`${n.toThe} ${n.powers[e]}`)]}if("number"===s.type&&/^\d+$/.test(s.value))return[...i,r(`${n.toThe} ${this.ordinal(Number(s.value))}`)];if(!(this.isCompound(s)||"op"===s.type&&"/"===s.op&&this.isCompound(s.args[0]))){const e=this.speak(s),t=e[0]&&e[0].text.startsWith(n.quantity);return[...i,r(t?n.toThePowerOf:n.toThe),...e]}return this.audio?[...i,r(n.toThePowerOf),r(n.quantity),...this.speak(s),...this.closer(s,"endExponent")]:[...i,r(n.toThePowerOf),...this.speak(s),r(n.comma)]}ordinal(e){if(e<=10&&e>=1)return"squared"===this.w.powers[e]?"second":"cubed"===this.w.powers[e]?"third":this.w.powers[e];return`${e}${e%100>=11&&e%100<=13?"th":["th","st","nd","rd"][e%10]??"th"}`}derivative(e){const{n:t,body:s,by:n,at:r,variable:i}=ls(e),o=this.w,a=t=>this.seg(e,t),l=r?[a(`${o.comma} ${o.at}`),...this.speak(n),a(o.equals),...this.speak(r)]:[];if(i){return[...[a(1===t?o.d:`${o.d} ${2===t?o.squared:3===t?o.cubed:`${o.toThe} ${this.ordinal(t)}`}`),...this.speak(s),a(o.d),...this.speak(n),a(1===t?"":2===t?o.squared:3===t?o.cubed:`${o.toThe} ${this.ordinal(t)}`)],...l]}return[a(`${1===t?o.derivative:2===t?o.secondDerivative:o.nthDerivative(this.ordinal(t))} ${o.withRespectTo}`),...this.speak(n),a(o.of),...this.contents(s,"endDerivative"),...l]}integral(e){const{args:t}=e,[s,n,r,i]=t,o=this.w,a=t=>this.seg(e,t),l=t.length>=2&&"symbol"===n.type&&1===n.value.length,c=l?this.speak(n):[a(L(s)||"x")];let h=[];4===t.length?h=[r,i]:3===t.length?h=l?[r]:[n,r]:2!==t.length||l||(h=[n]);return[...2===h.length?[a(o.integralFrom),...this.speak(h[0]),a(o.to),...this.speak(h[1]),a(o.of)]:1===h.length?[a(o.integralFrom),...this.speak(h[0]),a(o.of)]:[a(o.integralOf)],...this.speak(s),a(`${o.comma} ${o.d}`),...c]}limit(e){const{args:t}=e,[s,n,r,i]=t,o=this.w,a=t=>this.seg(e,t);let l;if(t.length>=3){if(l=[...this.speak(n),a(o.approaches),...this.speak(r)],4===t.length){const e=String(i.value??"").toLowerCase().startsWith("r");l.push(a(e?o.fromTheRight:o.fromTheLeft))}}else if(2===t.length&&"op"===n.type&&"→"===n.op)l=[...this.speak(n.args[0]),a(o.approaches),...this.speak(n.args[1])];else{if(2!==t.length)return[a(`${o.limitAs.replace(/ as$/,"")} ${o.of}`),...this.speak(s)];{const e=L(s);l=e?[a(e),a(o.approaches),...this.speak(n)]:this.speak(n)}}return this.audio&&"op"===s.type&&"/"===s.op?[a(o.limitAs),...l,a(o.of),...this.grouped(s,"endLimit",!0)]:[a(o.limitAs),...l,a(o.of),...this.contents(s,"endLimit")]}bigOperator(e,t,s,n,r){const{args:i}=e,[o,a,l]=i,c=this.w,h=t=>this.seg(e,t);let u;return u=3===i.length?[h(t),...this.speak(a),h(c.to),...this.speak(l),h(c.of)]:2===i.length?[h(s),...this.speak(a),h(c.of)]:[h(n)],[...u,...this.contents(o,r)]}}for(const e of[bn,wn,_n,Mn,vn])Object.setPrototypeOf(e,null);function qn(e,t={}){return xn(e)?[]:function(e,t={}){return An(e,t,!1)}($n(e),t)}function Sn(e,t={}){return xn(e)?"":kn($n(e),t)}const zn={"(":{fill:"M0.92,0 C0.1,0.2 0.1,0.8 0.92,1 L1,0.975 C0.48,0.75 0.48,0.25 1,0.025 Z"},")":{fill:"M0.08,0 C0.9,0.2 0.9,0.8 0.08,1 L0,0.975 C0.52,0.75 0.52,0.25 0,0.025 Z"},"{":{fill:"M1,0 C0.45,0 0.5,0.05 0.5,0.2 L0.5,0.38 C0.5,0.46 0.35,0.49 0.05,0.5 C0.35,0.51 0.5,0.54 0.5,0.62 L0.5,0.8 C0.5,0.95 0.45,1 1,1 L1,0.985 C0.68,0.985 0.64,0.95 0.64,0.8 L0.64,0.62 C0.64,0.53 0.55,0.505 0.3,0.5 C0.55,0.495 0.64,0.47 0.64,0.38 L0.64,0.2 C0.64,0.05 0.68,0.015 1,0.015 Z"},"}":{fill:"M0,0 C0.55,0 0.5,0.05 0.5,0.2 L0.5,0.38 C0.5,0.46 0.65,0.49 0.95,0.5 C0.65,0.51 0.5,0.54 0.5,0.62 L0.5,0.8 C0.5,0.95 0.55,1 0,1 L0,0.985 C0.32,0.985 0.36,0.95 0.36,0.8 L0.36,0.62 C0.36,0.53 0.45,0.505 0.7,0.5 C0.45,0.495 0.36,0.47 0.36,0.38 L0.36,0.2 C0.36,0.05 0.32,0.015 0,0.015 Z"},sqrt:{stroke:"M0,0.62 L0.18,0.55 L0.45,0.97 L0.98,0"},cancel:{stroke:"M0,1 L1,0"},int:{stroke:"M0.95,0.09 C0.9,0.01 0.72,0 0.66,0.12 C0.6,0.25 0.42,0.75 0.34,0.88 C0.28,0.99 0.1,0.99 0.05,0.91"},sum:{fill:"M0.04,0 L0.94,0 L0.98,0.24 L0.94,0.24 C0.9,0.1 0.8,0.07 0.62,0.07 L0.24,0.07 L0.58,0.5 L0.2,0.93 L0.64,0.93 C0.82,0.93 0.92,0.9 0.95,0.76 L0.99,0.76 L0.94,1 L0.02,1 L0.47,0.47 Z"},prod:{fill:"M0,0 L1,0 L1,0.04 L0.88,0.05 L0.88,0.95 L1,0.96 L1,1 L0.62,1 L0.62,0.96 L0.74,0.95 L0.74,0.06 L0.26,0.06 L0.26,0.95 L0.38,0.96 L0.38,1 L0,1 L0,0.96 L0.12,0.95 L0.12,0.05 L0,0.04 Z"}};function Nn(e,t,s=""){const n=zn[e];return`<svg class="${t}" viewBox="0 0 1 1" preserveAspectRatio="none" aria-hidden="true"${s?` style="${s}"`:""}>${n.fill?`<path d="${n.fill}" fill="currentColor"/>`:`<path d="${n.stroke}" fill="none" stroke="currentColor" vector-effect="non-scaling-stroke"/>`}</svg>`}const Ln=.8,Zn=.2,Cn=.25,Rn=.7,In="−",jn={"+":"add","-":"sub","*":"mul","/":"frac","^":"pow",_:"subscript",neg:"neg",pm:"pm","!":"factorial","=":"eq","===":"eq","≠":"neq","≠=":"neq","<":"lt",">":"gt","≤":"le","≥":"ge","≈":"approx","≡":"equiv","∈":"in","∉":"notin","∪":"union","∩":"intersect","→":"to","±":"pm","&":"and","|":"or","\\not":"not","\\xor":"xor","\\nand":"nand","\\nor":"nor","\\implies":"implies","\\iff":"iff","?:":"cases","()":"tuple","[]":"list","{}":"set","(]":"interval","[)":"interval","\\sumidx":"sum","\\prodidx":"prod",mod:"mod",pmod:"pmod",pmatrix:"matrix",bmatrix:"matrix",vmatrix:"matrix"},Tn={"=":"=","===":"=","≠":"≠","≠=":"≠","<":"<",">":">","≤":"≤","≥":"≥","∈":"∈","∉":"∉","→":"→","≈":"≈","≡":"≡","&":"∧","|":"∨","\\xor":"⊕","\\nand":"↑","\\nor":"↓","\\implies":"⇒","\\iff":"⇔"},Fn={"±":"±","∪":"∪","∩":"∩",mod:"mod"},Pn=["sin","cos","tan","csc","sec","cot","sinh","cosh","tanh"],Wn={arcsin:"sin",arccos:"cos",arctan:"tan",arcsec:"sec",arccsc:"csc",arccot:"cot",arcsinh:"sinh",arccosh:"cosh",arctanh:"tanh"},Bn=[...Pn,...Object.keys(Wn),"ln","det"],Un={atan2:"atan2",max:"max",min:"min",gcd:"gcd",mean:"mean",round:"round",fixed:"fixed",expm1:"expm1",random:"random"},Dn={"()":["(",")"],"[]":["[","]"],"{}":["{","}"],"(]":["(","]"],"[)":["[",")"]},Gn=["int","lim","\\sumidx","\\prodidx"],Hn={"\\partial":"∂","\\nabla":"∇","\\ell":"ℓ","\\infty":"∞","\\emptyset":"∅"},Qn={vec:"→",hat:"ˆ",widehat:"ˆ",tilde:"˜",dot:"˙",ddot:"¨"},Vn=/^[acegmnopqrsuvwxyzαγεημνοπρστυχω]$/,Xn=e=>"symbol"===e?.type&&Vn.test(Rt[e.value]??e.value),Jn=["boxed","overline","bar","underline","cancel",...Object.keys(Qn)],Kn={prefix:"mv",display:!0,fnParens:"auto",trigPowers:!0,inverseTrig:"arc",derivativeBrackets:"[",scriptFractions:"slash",cases:!0,label:!0,mathml:null,attributes:null,style:null},Yn=e=>String(e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;"),er=e=>+e.toFixed(3)+"em",tr=(e,t=Ln,s=Zn)=>({html:e,h:t,d:s});function sr(...e){const t=e.flat().filter(Boolean);return{html:t.map(e=>e.html).join(""),h:Math.max(Ln,...t.map(e=>e.h)),d:Math.max(Zn,...t.map(e=>e.d))}}function nr(e,t={}){const s=new rr({...Kn,...t}),n=s.render(e,{level:0}),r=`${s.p}${s.o.display?` ${s.p}-display`:""}`,i="function"==typeof s.o.mathml?function(e,t,s,n){try{return String(e(t)).replace(/ id="mz-\d+"/g,"").replace(/^<math>/,`<math class="${s}-sr-math"${n?' display="block"':""}>`)}catch{return""}}(s.o.mathml,e,s.p,s.o.display):"",o=i?null:function(e,t){if(!1===t||null===t)return null;if("string"==typeof t)return t;try{return"function"==typeof t?t(e):Sn(e)}catch{return null}}(e,s.o.label),a=null!==o||i?n.html.replace(/^<span /,'<span aria-hidden="true" '):n.html;return{html:`<span class="${r}"${null===o?"":` role="math" aria-label="${Yn(o)}"`}>${a}${i}</span>`,h:n.h,d:n.d}}class rr{constructor(e){this.o=e,this.p=e.prefix}el(e,t,s="",n=""){return`<span class="${e.split(" ").map(e=>`${this.p}-${e}`).join(" ")}"${s?` style="${s}"`:""}${n}>${t}</span>`}text(e,t){const s=tr(this.el(e,Yn(t)));return s.leaf={kind:e,content:Yn(t)},s}bin(e){return this.text("bin",e)}rel(e){return this.text("rel",e)}punct(e){return this.text("punct",e)}word(e){return this.text("word",e)}wrap(e,t,s){let n="";void 0!==e.id&&(n+=` data-node="${e.id}"`),"op"===e.type&&(n+=` data-op="${Yn(e.op)}"`);const r=this.o.attributes&&this.o.attributes(e)||{};for(const[e,t]of Object.entries(r))"class"!==e&&"style"!==e&&/^[A-Za-z_:][\w:.-]*$/.test(e)&&(n+=` ${e}="${Yn(t)}"`);const i=[r.style,this.o.style&&this.o.style(e)].filter(Boolean).join(";"),o=(s.leaf?["n",...t,s.leaf.kind]:["n",...t]).map(e=>`${this.p}-${e}`).concat(r.class?[Yn(r.class)]:[]).join(" "),a=s.leaf?s.leaf.content:s.html;return{html:`<span class="${o}"${i?` style="${Yn(i)}"`:""}${n}>${a}</span>`,h:s.h,d:s.d}}render(e,t){return"number"===e.type?this.number(e):"string"===e.type?this.wrap(e,["text"],this.text("text",e.value.replace(/^["'`]|["'`]$/g,""))):"symbol"===e.type?this.wrap(e,["symbol"],this.symbol(e.value)):this.operation(e,t)}number(e){const t=/^(-?\d*\.)(\d*)\.\.$/.exec(e.value);if(t){const[,s,n]=t;let r=1;for(let e=1;2*e<=n.length;e++)if(n.slice(-e)===n.slice(-2*e,-e)){r=e;break}let i=n;for(;i.endsWith(n.slice(-r))&&i.length>=r;)i=i.slice(0,-r);const o=this.text("num",(s+i).replace(/^-/,In)),a=this.text("num",n.slice(-r));return this.wrap(e,["number"],sr(o,tr(this.el("over",a.html),a.h+.14,a.d)))}return this.wrap(e,["number"],this.text("num",e.value.replace(/^-/,In)))}symbol(e){return Rt[e]&&(e=Rt[e]),Hn[e]?this.text("ord",Hn[e]):"\\+"===e||"\\-"===e?this.text("ord",e[1]):"-∞"===e?this.text("ord","−∞"):/^[A-Za-z]$/.test(e)||/^[α-ω]$/.test(e)?this.text("var",e):/^[A-Za-z]′+$/.test(e)?tr(this.el("var",Yn(e[0]))+this.el("prime",Yn(e.slice(1)))):/^Δ[A-Za-z]$/.test(e)?tr(this.el("ord","Δ")+this.el("var",e[1])):/^d[A-Za-zα-ω]$/.test(e)?tr(this.el("ord","d")+this.el("var",e[1])):e.endsWith("⁻¹")?this.script(this.symbol(e.slice(0,-2)),this.text("num","−1"),{level:0}):/^\\[A-Za-z]+$/.test(e)?this.text("name",e.slice(1)):/^[A-Za-z]{2,}$/.test(e)?this.text("name",e):this.text("ord",e)}operation(e,t){const{op:s,args:n}=e,[r,o]=n,a=(e,s={})=>this.render(e,{...t,...s,forceParens:s.forceParens||!1,before:s.before||!1,leftFactor:!1}),c=jn[s]??(/^[a-z0-9]+$/i.test(s)?s:"other"),h=t=>this.wrap(e,["op",`op-${c}`],t),u=(t,s)=>Gt(e,t,s)&&"/"!==t.op?this.fence(a(t)):a(t);if(Bn.includes(s)||"log"===s)return h(this.named(e,t));if(Un[s])return h(sr(this.word(Un[s]),this.fence(this.list(n,t))));if(i(s))return h(this.custom(e,t));const p=ms(e);if(p)return h(this.matrix(e,p,t));if("{}"===s&&!n.length)return h(this.text("op","∅"));if(Dn[s])return h(this.fence(this.list(n,t),...Dn[s]));if(("|"===s||"&"===s)&&n.length>=2&&n.every(ar)){const e=e=>"|"!==e.op&&"&"!==e.op||e.op===s?a(e):this.fence(a(e));return h(sr(n.map((t,n)=>n?[this.text("conj","|"===s?"or":"and"),e(t)]:[e(t)]).flat()))}if(Tn[s]&&2===n.length)return h(sr(u(r,"left"),this.rel(Tn[s]),u(o,"right")));if(Fn[s]&&2===n.length)return h(sr(u(r,"left"),this.bin(Fn[s]),u(o,"right")));switch(s){case"+":if(Bt(o)){const e=o.args[1],t=Gt({op:"-"},e,"right")?this.fence(a(e)):a(e);return h(sr(u(r,"left"),this.bin(In),this.wrap(o,["op","op-mul"],t)))}return"number"===o.type&&o.value.startsWith("-")?h(sr(u(r,"left"),this.bin(In),this.wrap(o,["number"],this.text("num",o.value.slice(1))))):h(sr(u(r,"left"),this.bin("+"),this.signed(o,u(o,"right"))));case"-":return h(sr(u(r,"left"),this.bin(In),this.signed(o,u(o,"right"))));case"*":return h(this.product(e,t));case"/":return h(this.fraction(e,t));case"^":return h(this.power(e,t));case"_":{const e=Ut(r)&&l(Ut(r))<100;return h(this.script(e?this.fence(a(r)):a(r,{forceParens:!0}),null,t,a(o,{level:t.level+1})))}case"neg":return h(sr(this.text("sign",In),Ht(s,r)?this.fence(a(r)):a(r,{before:t.before})));case"pm":return h(sr(this.text("sign","±"),Ht(s,r)?this.fence(a(r)):a(r)));case"!":return h(sr(Ht(s,r)?this.fence(a(r)):a(r,{forceParens:!0}),this.text("ord","!")));case"\\not":return h(sr(this.text("sign","¬"),Ht(s,r)?this.fence(a(r)):a(r)));case"pmod":return h(sr(a(r),this.fence(sr(this.word("mod"),this.text("space",""),a(o)))));case"sqrt":return h(this.radical(a(r)));case"root":return h(this.radical(a(o),this.render(r,{level:t.level+2})));case"abs":return h(this.fence(a(r),"|","|"));case"floor":return h(this.fence(a(r),"⌊","⌋"));case"ceil":return h(this.fence(a(r),"⌈","⌉"));case"loglog":return h(sr(this.word("ln"),this.fence(sr(this.word("ln"),this.fence(a(r))))));case"logloglog":return h(sr(this.word("ln"),this.fence(sr(this.word("ln"),this.fence(sr(this.word("ln"),this.fence(a(r))))))));case"perm":return h(this.script(this.script(tr(""),this.render(r,{level:t.level+1}),t),null,t,this.render(o,{level:t.level+1}),this.word("P")));case"comb":return h(this.fence(this.stack(a(r),a(o),!1)));case"?:":return h(this.o.cases?this.cases(e,t):sr(a(r),this.bin("?"),a(o),this.bin(":"),a(n[2])));case"sum":case"prod":{const e={op:"sum"===s?"+":"*"},t=(t,s)=>Gt(e,t,s?"right":"left")?this.fence(a(t)):a(t);return h(sr(n.map((e,n)=>n?[this.bin("sum"===s?"+":"·"),t(e,n)]:[t(e,n)]).flat()))}case"setbuilder":return h(this.fence(sr(a(r),this.bin("|"),a(o)),"{","}"));case"evaluated":{const e=e=>this.render(e,{...t,level:t.level+1});return h(this.script(this.fence(a(r),"[","]"),e(n[3]),t,e(n[2])))}case"der":return h(this.derivative(e,t));case"int":return h(this.integral(n,t));case"lim":return h(this.limit(n,t));case"\\sumidx":return h(this.bigOperator("sum",n,t));case"\\prodidx":return h(this.bigOperator("prod",n,t))}throw new Error(`Inline Math cannot draw '${s}'`)}signed(e,t){return("number"===e.type?e.value.startsWith("-"):"neg"===e.op||"pm"===e.op)&&!t.fenced?this.fence(t):t}list(e,t){return sr(e.map((e,s)=>s?[this.punct(","),this.render(e,t)]:[this.render(e,t)]).flat())}product(e,t){const[s,n]=e.args;if(Bt(e))return sr(this.text("sign",In),Ht("neg",n)?this.fence(this.render(n,t)):this.render(n,t));const r=function(e){const[t,s]=e.args;return{left:Gt(e,t,"left")&&"/"!==t.op,right:Gt(e,s,"right")&&"/"!==s.op}}(e);Gn.includes(s.op)&&(r.left=!0);const o=!e.written&&Qt(s,n,r),a=function(e){for(;"op"===e.type;){if(Bn.includes(e.op)||"log"===e.op||Un[e.op]||i(e.op))return!0;if("^"!==e.op&&"*"!==e.op)return!1;e=e.args[0]}return!1}(n),l=r.left?this.fence(this.render(s,t)):this.render(s,{...t,before:o&&!a,leftFactor:!0}),c=r.right?this.fence(this.render(n,t)):this.render(n,{...t,before:t.before});return sr(l,o?tr(this.el(a?"fnspace":"times","")):this.bin("cross"===e.written?"×":"·"),c)}named(e,t,s=null){const{op:n,args:r}=e,i="log"===n?r[1]:r[0];let o;o=Wn[n]&&"power"===this.o.inverseTrig?this.script(this.word(Wn[n]),this.text("num","−1"),t):this.word(n),s&&(o=this.script(o,s,t)),"log"===n&&(o=this.script(o,null,t,this.render(r[0],{level:t.level+1})));const a="auto"===this.o.fnParens&&!t.forceParens&&!t.before&&ir(i),l=this.render(i,{...t,forceParens:!1,before:!1});return sr(o,a?sr(tr(this.el("fnspace","")),l):this.fence(l))}custom(e,t){const s=ss(e.op),n=e.args;if(Jn.includes(s)&&1===n.length)return this.decorate(s,this.render(n[0],t),n[0]);return sr(/^[A-Za-z]′*(⁻¹)?$/.test(s)?this.symbol(s):this.word(s),tr(this.el("callgap","")),this.fence(this.list(n,t)))}decorate(e,t,s){switch(e){case"boxed":return tr(this.el("boxed",t.html),t.h+.22,t.d+.22);case"cancel":return tr(this.el("cancel",t.html+Nn("cancel",`${this.p}-cancel-line`)),t.h,t.d);case"underline":return tr(this.el("underline",t.html),t.h,t.d+.12);case"overline":case"bar":return tr(this.el("over"+(Xn(s)?" over-low":""),t.html),t.h+.14,t.d);default:{const n=Xn(s);return tr(this.el("accent"+(n?" accent-low":""),this.el(`accent-mark accent-${e}`,Qn[e])+t.html),t.h+.3,t.d)}}}fraction(e,t){const[s,n]=e.args;if(t.level>0&&"slash"===this.o.scriptFractions&&ir(s,!0)&&ir(n,!0)){const e={...t,leftFactor:!1},r=(t,s)=>"op"!==t.type||ir(t)&&(!s||"*"!==t.op||Bt(t))?this.render(t,e):this.fence(this.render(t,e)),i=sr("op"===s.type&&"neg"===s.op?this.render(s,e):r(s,!1),this.text("ord","/"),r(n,!0));return t.leftFactor?this.fence(i):i}const r=t.level>0||t.inFraction||!this.o.display?.8:1,i={...t,inFraction:!0,forceParens:!1,before:!1};return this.stack(this.render(s,i),this.render(n,i),!0,r)}stack(e,t,s=!0,n=1){const r=e=>1===n?e.html:this.el("scaled",e.html,`font-size:${er(n)}`),i=s?.06:0,o=Cn+i/2+.1+Math.max(Zn,n*e.d),a=this.el("frac"+(s?"":" frac-nobar"),this.el("numer",r(e))+this.el("denom",r(t)),`vertical-align:${er(o)}`);return tr(a,o+Math.max(Ln,n*e.h),Math.max(Ln,n*t.h)+Math.max(Zn,n*t.d)+.1+i/2-Cn)}power(e,t){const[s,n]=e.args,r={...t,level:t.level+1,forceParens:!1,before:!1,inFraction:!1,leftFactor:!1},i=()=>this.render(n,r);if(this.o.trigPowers&&Pn.includes(s.op)&&cr(n))return this.wrap(s,["op",`op-${s.op}`],this.named(s,t,i()));if("_"===s.op){const e=this.render(s.args[1],r);return this.wrap(s,["op","op-subscript"],this.script(this.render(s.args[0],{...t,forceParens:!0}),i(),t,e))}const o=Gt(e,s,"left")||"number"===s.type&&s.value.startsWith("-")||"/"===s.op||Bn.includes(s.op)||Gn.includes(s.op)?this.fence(this.render(s,t)):this.render(s,{...t,forceParens:!0});return this.script(o,i(),t)}script(e,t,s,n=null,r=null){const i=s.level>0?.8:Rn,o=r?sr(e,r):e;let a=t?Math.max(.42,e.h-.38,i*t.d+.12):0,l=n?Math.max(.2,e.d-.05,i*n.h-.45):0;if(t&&n){const e=.1-(a-i*t.d-(i*n.h-l));e>0&&(l+=e)}const c=e=>this.el("script",e.html,`font-size:${er(i)}`);if(t&&!n)return tr(e.html+this.el("sup",t.html,`font-size:${er(i)};vertical-align:${er(a/i)}`),Math.max(o.h,a+i*t.h),Math.max(o.d,i*t.d-a));if(n&&!t)return tr(o.html+this.el("sub",n.html,`font-size:${er(i)};vertical-align:${er(-l/i)}`),Math.max(o.h,i*n.h-l),Math.max(o.d,l+i*n.d));const h=a+i*t.h,u=l+i*n.d,p=this.el("subsup",c(t)+c(n),`vertical-align:${er(a)};height:${er(h+u)}`);return tr(o.html+p,Math.max(o.h,h),Math.max(o.d,u))}radical(e,t=null){const s=Math.max(e.h,Ln)+.1+.06,n=Math.max(e.d,Zn),r=s+n,i=.5+.06*(r-1),o=Nn("sqrt",`${this.p}-hook`,`width:${er(i)};left:${er(-i)}`);let a="";if(t){const e=.8*Rn;a=this.el("rootindex",t.html,`font-size:${er(e)};vertical-align:${er((.55*r-n)/e)}`)}return tr(this.el("sqrt",a+this.el("radicand",o+e.html,`margin-left:${er(i+.05)}`)),s,n)}fence(e,t="(",s=")"){const n=Math.max(e.h,Ln)+Math.max(e.d,Zn),r=n>1.25,i=r?.1:0,o=(e,t)=>{if(!e)return{html:"",w:0};const s={"(":.26,")":.26,"{":.36,"}":.36}[e];if(s){const i=s+(r?Math.min(.2,.05*(n-1)):0);return{html:Nn(e,`${this.p}-delim ${this.p}-delim-${t}`,`width:${er(i)}`),w:i+.1}}const i={"[":"lbrack","]":"rbrack","|":"vert","⌊":"lfloor","⌋":"rfloor","⌈":"lceil","⌉":"rceil"}[e];return{html:this.el(`delim ${i} delim-${t}`,""),w:"|"===e?.18:.3}},a=o(t,"l"),l=o(s,"r");if(!r){const t=tr(this.el("fence fence-line",a.html+e.html+l.html),Math.max(e.h,Ln),Math.max(e.d,Zn));return t.fenced=!0,t}const c=`padding:${er(i)} ${er(l.w)} ${er(i)} ${er(a.w)}`,h=tr(this.el("fence",a.html+e.html+l.html,c),Math.max(e.h,Ln)+i,Math.max(e.d,Zn)+i);return h.fenced=!0,h}derivative(e,t){const s=this.leibniz(e,t),{by:n,at:r}=ls(e);if(!r)return s;const i=e.args[1],o=this.render({...i,args:[{...n,id:void 0},r]},{...t,level:t.level+1});return this.script(this.fence(s,"","|"),null,t,o)}leibniz(e,t){const{n:s,body:n,by:r,variable:i}=ls(e),o={...t,level:t.level+1},a=()=>this.text("d","d"),c=()=>s>1?this.render({type:"number",value:String(s)},o):null,h={...t,inFraction:!0},u=sr(a(),c()?this.script(this.render(r,h),c(),h):this.render(r,h)),p=e=>sr(c()?this.script(a(),c(),h):a(),e||[]),f=t.level>0||t.inFraction||!this.o.display?.8:1;if(i)return this.stack(p(this.render(n,h)),u,!0,f);const d=Ut(n)&&l(Ut(n))<100,[g,m]="("===this.o.derivativeBrackets?["(",")"]:["[","]"],$=d?this.fence(this.render(n,t),g,m):this.render(n,t);return sr(this.stack(p(),u,!0,f),$)}integral(e,t){const[s,n,r,i]=e,o=e=>this.render(e,t),a=e=>e?this.render(e,{...t,level:t.level+1}):null,l=()=>this.text("var",L(s)||"x");let c,h=null,u=null;switch(e.length){case 1:c=l();break;case 2:ts(n)?c=o(n):(c=l(),h=a(n));break;case 3:ts(n)?(c=o(n),h=a(r)):(c=l(),h=a(n),u=a(r));break;default:c=o(n),h=a(r),u=a(i)}const p=o(s),f=tr(this.el("dx",this.el("d","d")+c.html),c.h,c.d);return sr(this.bigSign("int",p,h,u,t),p,f)}bigSign(e,t,s,n,r){const i=!this.o.display||r.level>0,o=Math.max(i?.6:.95,Math.max(t.h-Cn,t.d+Cn)+.12),a=Nn(e,`${this.p}-bigsign ${this.p}-${e}sign`,`height:${er(2*o)};width:${er(i?.38:.5)};vertical-align:${er(Cn-o)}`);if(!s&&!n)return tr(a,Cn+o,o-Cn);const l=Rn,c=n||tr(""),h=s||tr(""),u=Cn+o-l*c.h;h.d;const p=this.el("limits",this.el("script",c.html,`font-size:${er(l)}`)+this.el("script lower",h.html,`font-size:${er(l)}`),`vertical-align:${er(u)};height:${er(2*o)}`);return tr(a+p,Cn+o,o-Cn)}limit(e,t){const[s,n,r,i]=e,o={...t,level:t.level+1},a=e=>this.render(e,o),l=()=>this.rel("→");let c;switch(e.length){case 1:return sr(this.word("lim"),tr(this.el("fnspace","")),this.body(s,t));case 2:{const e=L(s),t="number"===n.type||"symbol"===n.type&&n.value.includes("∞");c=e&&t?sr(this.text("var",e),l(),a(n)):a(n);break}case 3:c=sr(a(n),l(),a(r));break;default:{const e=It(i).toLowerCase().startsWith("r")?"+":In;c=sr(a(n),l(),this.script(a(r),this.wrap(i,["symbol"],this.text("ord",e)),o))}}return sr(this.under(this.word("lim"),c,t),this.body(s,t))}under(e,t,s){return!this.o.display||s.level>0?this.script(e,null,s,t):tr(this.el("under",e.html+this.el("script",t.html,`font-size:${er(.7)}`)),e.h,Math.max(e.d,Zn)+.05+.7*(t.h+t.d))}body(e,t){const s=Ut(e);return s&&l(s)<l("*")?this.fence(this.render(e,t)):this.render(e,t)}bigOperator(e,t,s){const[n,r,i]=t,o={...s,level:s.level+1},a=r?this.render(r,o):null,l=i?this.render(i,o):null,c=this.body(n,s);if(!this.o.display||s.level>0){const t=.55,n=Nn(e,`${this.p}-bigsign`,`height:${er(2*t)};width:${er(.8)};vertical-align:${er(Cn-t)}`);return sr(this.script(tr(n,Cn+t,t-Cn),l,s,a),c)}const h=.68,u=Rn,p=Nn(e,`${this.p}-bigsign`,`height:${er(1.36)};width:${er("sum"===e?1:1.05)}`),f=l?1.01+u*l.d:Cn-h,d=e=>this.el("script",e.html,`font-size:${er(u)}`),g=this.el("bigop",(l?d(l):"")+p+(a?d(a):""),`vertical-align:${er(f)}`);return sr(tr(g,.93+(l?.08+u*(l.h+l.d):0),h-Cn+(a?.08+u*(a.h+a.d):0)),c)}matrix(e,t,s){const n=t.map(e=>e.map(e=>this.render(e,s))),r=n.map(e=>({h:Math.max(Ln,...e.map(e=>e.h)),d:Math.max(Zn,...e.map(e=>e.d))})),i=r.reduce((e,t)=>e+t.h+t.d,0)+.3*(t.length-1),o=Cn+i/2-r[0].h,a=n.map((t,s)=>{const n=t.map(e=>this.el("matrix-cell",e.html)).join(""),r=e.args[s];return r&&"op"===r.type&&"[]"===r.op?this.wrap(r,["op","op-list","matrix-row"],tr(n)).html:n}).join(""),l=t[0].length,c=tr(this.el("matrix",a,`grid-template-columns:repeat(${l},auto);vertical-align:${er(o)}`),Cn+i/2,i/2-Cn),[h,u]=gs[e.op]??["[","]"];return this.fence(c,h,u)}cases(e,t){const s=[];let n=e;for(;"op"===n.type&&"?:"===n.op;)s.push([this.render(n.args[1],t),sr(this.word("if"),this.text("space",""),this.render(n.args[0],t))]),n=n.args[2];"symbol"===n.type&&"unchanged"===n.value||s.push([this.render(n,t),this.word("otherwise")]);const r=s.map(([e,t])=>({h:Math.max(e.h,t.h),d:Math.max(e.d,t.d)})),i=r.reduce((e,t)=>e+t.h+t.d,0)+.25*(s.length-1),o=Cn+i/2-r[0].h,a=s.map(([e,t])=>this.el("case-value",e.html)+this.el("case-cond",t.html)).join(""),l=tr(this.el("cases",a,`vertical-align:${er(o)}`),Cn+i/2,i/2-Cn);return this.fence(l,"{","")}}function ir(e,t=!1){return"number"===e.type?!e.value.startsWith("-")||t:"symbol"===e.type||"op"===e.type&&("abs"===e.op||ms(e)?!t:!t||"neg"!==e.op&&"^"!==e.op?"*"===e.op&&!Bt(e)&&e.args.every(e=>"number"===e.type&&!e.value.startsWith("-")||lr(e)||"*"===e.op&&ir(e,t)):ir(e.args[0],!0)&&("neg"===e.op||ir(e.args[1],!0)))}const or=["=","===","≠","≠=","<",">","≤","≥","≈","≡","∈","∉"],ar=e=>"op"===e.type&&e.args.length>=2&&(2===e.args.length&&or.includes(e.op)||("|"===e.op||"&"===e.op)&&e.args.every(ar)),lr=e=>"symbol"===e.type&&(1===e.value.length||/^[α-ωΑ-Ω]$/.test(e.value)||Boolean(Rt[e.value])),cr=e=>"number"===e.type&&/^[1-9]\d*$/.test(e.value)&&Number(e.value)<10;for(const e of[jn,Tn,Fn,Wn,Un,Dn,Hn,Qn])Object.setPrototypeOf(e,null);function hr(e){const{id:t,from:s,...n}=e;return n.args&&(n.args=n.args.map(hr)),n}const ur=e=>S(e).join(" ");function pr(e,t){const s=new Set;fr(e,t,s);const n=[];return N(e,e=>{s.has(e)&&n.push(e)}),n}function fr(e,t,s){if(null!=t)if(Array.isArray(t))t.forEach(t=>fr(e,t,s));else if("number"!=typeof t)if("function"!=typeof t){if("string"==typeof t){const n=ur(mn(t));return void N(e,e=>{ur(e)===n&&s.add(e)})}if(t.path){const n=dr(e,t.path);return void(n&&s.add(n))}if(t.type){const n=ur(t);N(e,e=>{e!==t&&ur(e)!==n||s.add(e)})}}else N(e,(e,n)=>{t(e,n)&&s.add(e)});else N(e,e=>{e.id===t&&s.add(e)})}function dr(e,t){let s=e;for(const e of t){if("op"!==s.type||!s.args[e])return null;s=s.args[e]}return s}function gr(e,t){const s=[],n=e=>{if(e.id===t)return!0;if("op"!==e.type)return!1;for(let t=0;t<e.args.length;t++){if(s.push(t),n(e.args[t]))return!0;s.pop()}return!1};return n(e)?s:null}function mr(t){let s=0;const n=t=>{"op"===t.type&&(t.args.forEach(n),o(t.op)?.arity===e&&s++),t.id=s++};return n(t),t}function $r(e,t={}){const{blocks:s}=t;if("string"==typeof e)return mn(e,s?{blocks:e=>s(e,t.scope)}:{});if(Array.isArray(e))return q(e);if(e&&"object"==typeof e&&e.type){if(void 0!==e.id)return e;const t=structuredClone(e);return N(t,e=>{"op"!==e.type&&void 0!==e.value&&(e.value=String(e.value))}),mr(t)}throw new Error("Inline Math needs an expression, standard postfix or a tree")}function xr(e,t={}){const s=$r(e,t);return nr(s,yr(s,t)).html}function yr(e,t){const{colors:s,highlight:n,scope:r,blocks:i,...o}=t,a=new Map;for(const[t,n]of function(e){return e?Array.isArray(e)?e:Object.entries(e).map(([e,t])=>[/^\d+$/.test(e)?Number(e):e,t]):[]}(s))if(br(n))for(const s of pr(e,t))a.set(s,`color:${n}`);const l=new Set(void 0===n?[]:pr(e,n));if(!a.size&&!l.size)return o;const c=o.prefix||"mv";return{...o,style:e=>[a.get(e),o.style&&o.style(e)].filter(Boolean).join(";"),attributes:e=>{const t={...o.attributes?o.attributes(e):{}};return l.has(e)&&(t.class=[t.class,`${c}-hl`].filter(Boolean).join(" ")),t}}}function br(e){return"string"==typeof e&&/^[#\w\s.,%()/+-]+$/.test(e)&&!/url\s*\(|expression/i.test(e)}export{un as A,ds as B,Bt as C,Kn as D,ms as E,i as F,ss as G,ls as H,c as I,Ut as J,us as K,Rt as L,gs as M,It as N,ts as O,zn as P,Ht as Q,xn as R,gn as S,o as T,q as U,yr as a,qn as b,ur as c,mr as d,xr as e,$r as f,hr as g,L as h,S as i,$n as j,Wt as k,nr as l,fs as m,dr as n,Ft as o,gr as p,rs as q,Pt as r,pr as s,Sn as t,os as u,is as v,as as w,mn as x,z as y,N as z};
1
+ /* Inline Math 0.4.2 — MIT license; includes mathzetta (and @mathzetta/cas, for the CAS) */
2
+ const e=-1,t={"+":{prec:50,assoc:"left",arity:2,fixity:"infix"},"-":{prec:50,assoc:"left",arity:2,fixity:"infix"},"±":{prec:50,assoc:"left",arity:2,fixity:"infix"},"*":{prec:70,assoc:"left",arity:2,fixity:"infix"},"/":{prec:70,assoc:"left",arity:2,fixity:"infix"},"^":{prec:80,assoc:"right",arity:2,fixity:"infix"},mod:{prec:45,assoc:"left",arity:2,fixity:"infix"},neg:{prec:75,assoc:"none",arity:1,fixity:"prefix"},pm:{prec:75,assoc:"none",arity:1,fixity:"prefix"},"!":{prec:90,assoc:"none",arity:1,fixity:"postfix"},_:{prec:100,assoc:"none",arity:2,fixity:"infix"},sqrt:s(1),abs:s(1),floor:s(1),ceil:s(1),det:s(1),ln:s(1),loglog:s(1),logloglog:s(1),expm1:s(1),sin:s(1),cos:s(1),tan:s(1),csc:s(1),sec:s(1),cot:s(1),sinh:s(1),cosh:s(1),tanh:s(1),arcsin:s(1),arccos:s(1),arctan:s(1),arcsec:s(1),arccsc:s(1),arccot:s(1),arcsinh:s(1),arccosh:s(1),arctanh:s(1),random:s(0),log:s(2),root:s(2),atan2:s(2),round:s(2),fixed:s(2),perm:s(2),comb:s(2),der:s(2),evaluated:s(4),setbuilder:s(2),max:s(e),min:s(e),gcd:s(e),mean:s(e),sum:s(e),prod:s(e),int:s(e),lim:s(e),"\\sumidx":s(e),"\\prodidx":s(e),pmatrix:s(e),bmatrix:s(e),vmatrix:s(e),"()":n(),"[]":n(),"{}":n(),"(]":n(),"[)":n(),"=":{prec:30,assoc:"left",arity:2,fixity:"infix"},"===":{prec:30,assoc:"left",arity:2,fixity:"infix"},"≠":{prec:30,assoc:"left",arity:2,fixity:"infix"},"≠=":{prec:30,assoc:"left",arity:2,fixity:"infix"},"<":{prec:34,assoc:"left",arity:2,fixity:"infix"},">":{prec:34,assoc:"left",arity:2,fixity:"infix"},"≤":{prec:34,assoc:"left",arity:2,fixity:"infix"},"≥":{prec:34,assoc:"left",arity:2,fixity:"infix"},"∈":{prec:32,assoc:"left",arity:2,fixity:"infix"},"∉":{prec:32,assoc:"left",arity:2,fixity:"infix"},"∪":{prec:33,assoc:"left",arity:2,fixity:"infix"},"∩":{prec:33,assoc:"left",arity:2,fixity:"infix"},"≈":{prec:30,assoc:"left",arity:2,fixity:"infix"},"≡":{prec:30,assoc:"left",arity:2,fixity:"infix"},pmod:{prec:2,assoc:"left",arity:2,fixity:"infix"},"→":{prec:38,assoc:"left",arity:2,fixity:"infix"},"\\not":{prec:85,assoc:"none",arity:1,fixity:"prefix"},"&":{prec:20,assoc:"left",arity:2,fixity:"infix"},"\\xor":{prec:19,assoc:"left",arity:2,fixity:"infix"},"|":{prec:18,assoc:"left",arity:2,fixity:"infix"},"\\nor":{prec:17,assoc:"left",arity:2,fixity:"infix"},"\\nand":{prec:17,assoc:"left",arity:2,fixity:"infix"},"\\implies":{prec:15,assoc:"right",arity:2,fixity:"infix"},"\\iff":{prec:14,assoc:"left",arity:2,fixity:"infix"},"?:":{prec:10,assoc:"right",arity:3,fixity:"infix"}};function s(e){return{prec:100,assoc:"none",arity:e,fixity:"prefix"}}function n(){return{prec:5,assoc:"left",arity:e,fixity:"none",group:!0}}const r="\\:",i=e=>"string"==typeof e&&e.startsWith(r)&&e.length>2;function o(e){return Object.prototype.hasOwnProperty.call(t,e)?t[e]:i(e)?a:void 0}const a=s(e),l=e=>o(e)?.prec;function c(e,t,s){if("mod"===e&&t)return l(t)<=70;if("mod"===t&&["*","/"].includes(e))return!0;if("mod"===t&&["+","-","±"].includes(e))return"right"===s;const n=l(e),r=l(t);if(!n||!r)return!1;if(r!==n)return r<n;const i=o(e).assoc;return"left"===i?"right"===s:"right"===i&&"left"===s}const h=100,u="",p="",f={[u]:"dot",[p]:"cross"},d="",g={ln:h,log:h,loglog:h,logloglog:h,sqrt:h,sqrtpow:h,pow:h,expm1:h,der:h,lim:h,int:h,round:h,fixed:h,floor:h,ceil:h,abs:h,random:h,sum:h,prod:h,"\\sumidx":h,"\\prodidx":h,perm:h,comb:h,"\\perm":h,"\\comb":h,"\\binom":h,max:h,min:h,gcd:h,mean:h,pmatrix:h,bmatrix:h,vmatrix:h,det:h,sin:h,cos:h,tan:h,csc:h,sec:h,cot:h,sinh:h,cosh:h,tanh:h,evaluated:h,setbuilder:h,arcsin:h,arccos:h,arctan:h,arcsec:h,arccsc:h,arccot:h,arcsinh:h,arccosh:h,arctanh:h,atan2:h,_:h,"!":90,"\\not":85,"^":80,"*":70,"/":70,frac:70,[u]:70,[p]:70,mod:45,"+":50,"-":50,"±":50,"→":38,"<":34,">":34,"≤":34,"≥":34,"∈":32,"∉":32,"∪":33,"∩":33,"=":30,"==":30,"===":30,"≠":30,"≠=":30,"!==":30,"≈":30,"≡":30,pmod:2,"&":20,"\\xor":19,"|":18,"∨":18,"\\nor":17,"\\nand":17,"\\implies":15,"\\iff":14,"?:":10,"?":9,",":5},m=e=>Object.prototype.hasOwnProperty.call(g,e),$=new Set(["^","?:","?","log"]),x=["(","{","["],y=[")","}","]"];function b(e,t=!1){return"string"==typeof e&&(t?v:w).test(e)}const w=/^(?:\d+\.?\d*|\.\d+|\d*\.\d+\.\.)$/,v=/^-?(?:\d+\.?\d*|\.\d+|\d*\.\d+\.\.)$/;for(const e of[g])Object.setPrototypeOf(e,null);const k=e=>({type:"number",value:String(e)}),A=e=>({type:"symbol",value:e}),E=(e,t)=>({type:"op",op:e,args:t}),_=['"',"'","`"];function M(e){return b(e,!0)?k(e):_.includes(e[0])?{type:"string",value:e}:A(e)}const O={"0-":"neg",sqrtpow:"root",pow:"^","==":"=","!==":"≠="};function q(t){if(!Array.isArray(t)||0===t.length)throw new Error("Empty postfix");const s=[],n=[];if(t.forEach((t,r)=>{const a=O[t]??String(t),l=o(a);if(!l)return s.push({...M(a),id:r}),void n.push(1);let c=l.arity;if(c===e){const e=s.pop();if(n.pop(),c=e&&"number"===e.type?Number(e.value):NaN,!Number.isInteger(c)||c<0)throw new Error(`'${a}' takes a variable number of arguments, so it must follow a count`);if(0===c&&!l.group&&!i(a))throw new Error(`'${a}' needs at least one argument`)}if(s.length<c)throw new Error(`'${a}' needs ${c} operand${1===c?"":"s"}`);const h=1+Math.max(0,...n.splice(n.length-c,c));if(h>1e3)throw new Error("Nested too deeply (more than 1000 levels)");n.push(h),s.push({...E(a,s.splice(s.length-c,c)),id:r})}),1!==s.length)throw new Error(`Postfix leaves ${s.length} values instead of 1`);return s[0]}function S(t,s){const n=[];return function t(r){"op"===r.type?(r.args.forEach(t),o(r.op)?.arity===e&&n.push(String(r.args.length)),s&&r.written&&(s[n.length]=r.written),n.push(r.op)):n.push(r.value)}(t),n}function z(e){if(Array.isArray(e))return q(e);if(e&&"object"==typeof e&&e.type)return e;throw new Error("Expected a postfix array or an expression tree")}function N(e,t,s=null){t(e,s),"op"===e.type&&e.args.forEach(s=>N(s,t,e))}function L(e){let t="";return N(e,e=>{!t&&"symbol"===e.type&&1===e.value.length&&/[a-zA-Zʹ-Ͽ]/.test(e.value)&&(t=e.value)}),t}for(const e of[O])Object.setPrototypeOf(e,null);function Z(e){const t=new Map;for(const[s,n,r]of e)if(s&&""!==s.trim()&&(t.set(s,n),!r))for(const e of R(s))t.get(e)||t.set(e,n);const s=new Map;for(const e of t.keys()){let t=s;for(let s=0;s<e.length;s++)t.has(e[s])||t.set(e[s],new Map),t=t.get(e[s]);t.end=!0}return e=>{let n="",r=0;for(;r<e.length;){const i=C(s,e,r);i?(n+=t.get(e.slice(r,r+i)),r+=i):n+=e[r++]}return n}}function C(e,t,s){let n=e,r=0;for(let e=s;e<t.length&&(n=n.get(t[e]),n);e++)n.end&&(r=e-s+1);return r}function R(e){const t=new Set([e.toLowerCase(),e.toUpperCase(),I(e)]);return t.delete(e),t}function I(e){let t="",s=!1;for(const n of e){const e=n>="a"&&n<="z",r=n>="A"&&n<="Z";t+=!s&&e?n.toUpperCase():n,s=e||r}return t}const j=[["Math.atan2","atan2"],["Math.atan2(","atan2("],["atan2","atan2"],["\\atan2","atan2"],["Math.random()","random(1)"],["random.random()","random(1)"],["Math.log2(","log_{2}("],["Math.log10(","log_{10}("],["Math.log2","log_{2}"],["Math.log10","log_{10}"],["Math.log_{","log_{"],["Math.log","log_{e}"],["Math.ln","log_{e}"],["Math.abs(","abs("],["Math.fabs(","abs("],["Math.gcd(","gcd("],["Math.comb(","comb("],["Math.perm(","perm("],["Math.fmod(","mod("],["Math.exp(","(e)^("],["Math.exp2(","(2)^("],["Math.expm1(","expm1("],["Math.pow(","pow("],["**","^"],["Math.floor(","floor("],["Math.ceil(","ceil("],["Math.round(","round("],["Math.max(","max("],["Math.min(","min("],["Math.pi","π"],["Math.PI","π"],["math.Pi","π"],["Math.e","e"],["math.E","e"],["Math.sqrt2","sqrt(2)"],["Math.sqrt(","sqrt("],["Math.SQRT1_2","sqrt(1/2)"],["Math.SQRT1_{2}","sqrt(1/2)"],["Math.SQRT2","sqrt(2)"],["cbrt","sqrt[3]"],["Math.cbrt","sqrt[3]"],["Sqrt","sqrt"],["Root","sqrt"],["\\sqrt{","sqrt{"],["\\frac","frac"],["\\dfrac","frac"],["\\tfrac","frac"],["\\cfrac","frac"],["\\lim","lim"],["\\sum","\\sumidx"],["\\prod","\\prodidx"],["\\displaystyle",""],["\\textstyle",""],["\\,",""],["\\;",""],["\\!",""],["\\quad",""],["\\qquad",""],["\\lvert","|"],["\\rvert","|"],["\\vert","|"],["^\\prime","′"],["^{\\prime}","′"],["^{\\prime\\prime}","′′"],["\\prime","′"],["\\approx","≈"],["\\varepsilon","ε"],["\\vartheta","θ"],["\\varphi","φ"],["\\cup","∪"],["\\mathbb{R}","ℝ",!0],["\\mathbb{N}","ℕ",!0],["\\mathbb{Z}","ℤ",!0],["\\mathbb{Q}","ℚ",!0],["\\mathbb{C}","ℂ",!0],["\\cap","∩"],["\\%","%"],["\\barwedge","\\nand"],["∧","&"],["¬","!"],["↑","\\nand"],["↓","\\nor"],["∫","\\int"],["∑","\\sum"],["∏","\\prod"],["asin(","arcsin("],["acos(","arccos("],["atan(","arctan("],["asinh(","arcsinh("],["acosh(","arccosh("],["atanh(","arctanh("],["asec(","arcsec("],["acsc(","arccsc("],["acot(","arccot("],["\\sqrt*","sqrt"],["Log","log"],["Max","max"],["Min","min"],["Floor","floor"],["Ceil","ceil"],["Round","round"],["Abs","abs"],["Sum","sum"],["Prod","prod"],["Mean","mean"],["Avg","mean"],["Average","mean"],["Gcd","gcd"],["Math.LOG2e","log_{2}(e)"],["Math.LOG10e","log_{10}(e)"],["Math.LN2","log_{e}(2)"],["Math.LN10","log_{e}(10)"],["Math.tau","τ"],["infinity","∞"],["\\infty","∞"],["\\cdot",u],["\\times",p],["×",p],["·",u],["÷","/"],["->","→"],["\\to","→"],["\\max","max"],["\\min","min"],["\\det","det"],["^{\\top}","^{T}",!0],["^\\top","^T",!0],["^{\\intercal}","^{T}",!0],["^\\intercal","^T",!0],["^{\\mathsf{T}}","^{T}",!0],["^{\\mathrm{T}}","^{T}",!0],["product(","prod("],["log[","log_["],["\\log[","log_["],["\\log_","log_"],["\\log","log_{e}"],["\\ln","log_{e}"],["log(","log_{e}("],["ln(","log_{e}("],...["","\\"].flatMap(e=>["(","{"].flatMap(t=>[2,3,4,5,6,7,8,9,10].map(s=>[`${e}log${s}${t}`,`log_{${s}}${t}`]))),["\\log_10","log_{10}"],["log_10","log_{10}"],["\\log\\log","\\loglog"],["\\log\\log\\log","\\logloglog"],["\\loglog","\\loglog"],["\\logloglog","\\logloglog"],["loglog(","loglog("],["\\exp","e^"],["exp(","e^("],["\\div","/"],["\\int","\\int"],["\\in","∈"],["\\not\\in","∉"],["\\notin","∉"],["\\nor"," \\nor "],["\\xor"," \\xor "],["\\nand"," \\nand "],["\\Longrightarrow"," \\implies "],["\\Rightarrow"," \\implies "],["\\implies"," \\implies "],["⇒"," \\implies "],["⟹"," \\implies "],["\\Longleftrightarrow"," \\iff "],["\\Leftrightarrow"," \\iff "],["\\iff"," \\iff "],["⇔"," \\iff "],["⟺"," \\iff "],["\\uparrow"," \\nand "],["\\downarrow"," \\nor "],["\\oplus"," \\xor "],["\\lor","∨"],["\\land","&"],["Not"," \\not "],["&&","&"],["\\neg"," \\not "],["\\not"," \\not "],["\\rightarrow","→"],["\\limits_","_"],["\\pmod"," pmod "],["\\equiv","≡"],["\\mod"," mod "],["\\bmod"," mod "],["\\pm","±"],["\\plusmn","±"],["\\neq","≠"],["\\ne","≠"],["!==","≠="],["==","="],["=...=","==="],["!=","≠"],["\\gt",">"],["\\lt","<"],["\\ge","≥"],["\\geq","≥"],[">=","≥"],["\\le","≤"],["\\leq","≤"],["<=","≤"],["<>","≠"],["\\lparen","("],["\\rparen",")"],["\\left(","("],["\\right)",")"],["\\left[","["],["\\right]","]"],["\\left{","{"],["\\right}","}"],["\\left\\{","{"],["\\{","{"],["\\}","}"],["\\right\\}","}"],["\\left.",d],["\\left|","|"],["\\right|","|"],["\\lfloor","⌊"],["\\rfloor","⌋"],["\\lceil","⌈"],["\\rceil","⌉"],["ceiling(","ceil("],["√","sqrt"],["²","^2"],["³","^3"],["⁴","^4"],["⁵","^5"],["⁶","^6"],["⁷","^7"],["⁸","^8"],["⁹","^9"],["⁰","^0"],["¹","^1"],["ⁿ","^n"],["⁻¹","^{-1}"],["⁻²","^{-2}"],["⁻³","^{-3}"],["∛","sqrt[3]"],["∜","sqrt[4]"],["½","(1/2)"],["⅓","(1/3)"],["⅔","(2/3)"],["¼","(1/4)"],["¾","(3/4)"],["⅛","(1/8)"],["⅜","(3/8)"],["⅝","(5/8)"],["⅞","(7/8)"],["⅕","(1/5)"],["⅖","(2/5)"],["⅗","(3/5)"],["⅘","(4/5)"],["⅙","(1/6)"],["⅚","(5/6)"],["⅐","(1/7)"],["⅑","(1/9)"],["⅒","(1/10)"],["⊕"," \\xor "],["∞","∞"],["⋅",u],["−","-"],["\\wedge","&"],["∨","∨"],["\\vee","∨"],["\\Pi","Π",!0],["\\Alpha","Α",!0],["\\Beta","Β",!0],["\\Gamma","Γ",!0],["\\Delta","Δ",!0],["\\Delta ","Δ",!0],["\\Epsilon","Ε",!0],["\\Zeta","Ζ",!0],["\\Eta","Η",!0],["\\Theta","Θ",!0],["\\Iota","Ι",!0],["\\Kappa","Κ",!0],["\\Lambda","Λ",!0],["\\Mu","Μ",!0],["\\Nu","Ν",!0],["\\Xi","Ξ",!0],["\\Omicron","Ο",!0],["\\Rho","Ρ",!0],["\\Sigma","Σ",!0],["\\Tau","Τ",!0],["\\Upsilon","Υ",!0],["\\Phi","Φ",!0],["\\Chi","Χ",!0],["\\Psi","Ψ",!0],["\\Omega","Ω",!0],["\\pi","π",!0],["\\alpha","α",!0],["\\beta","β",!0],["\\gamma","γ",!0],["\\delta","δ",!0],["\\epsilon","ε",!0],["\\zeta","ζ",!0],["\\eta","η",!0],["\\theta","θ",!0],["\\iota","ι",!0],["\\kappa","κ",!0],["\\lambda","λ",!0],["\\mu","μ",!0],["\\nu","ν",!0],["\\xi","ξ",!0],["\\omicron","ο",!0],["\\rho","ρ",!0],["\\sigma","σ",!0],["\\tau","τ",!0],["\\upsilon","υ",!0],["\\phi","φ",!0],["\\chi","χ",!0],["\\psi","ψ",!0],["\\omega","ω",!0]];const T=e=>" "===e||"\t"===e||"\n"===e,F="({[",P=")}]";const W="ⅆ",B=new RegExp(String.raw`(\\(?:mathrm|operatorname|text)\{d\}\s*|d)([A-Za-zα-ω](?![A-Za-z0-9])|\\(?:${"alpha|beta|gamma|delta|epsilon|varepsilon|zeta|eta|theta|vartheta|iota|kappa|lambda|mu|nu|xi|rho|sigma|tau|upsilon|phi|varphi|chi|psi|omega"})(?![A-Za-z]))`,"g");function U(e){const t=[String.raw`\left\{`,String.raw`\left\lbrace`,String.raw`\{`,String.raw`\lbrace`,"{"],s=[String.raw`\right\}`,String.raw`\right\rbrace`,String.raw`\}`,String.raw`\rbrace`,"}"],n=/\\[A-Za-z{]/.test(e);for(let r=0,i=0;r<50;r++){let r=-1,o="";for(let s=i;s<e.length&&r<0;s++){const i=t.find(t=>e.startsWith(t,s));i&&("{"===i&&(n||/[\\^_A-Za-z]$/.test(e.slice(0,s)))||("{"!==i||"{"!==e[s+1]?(r=s,o=i):s++))}if(r<0)return e;let a=0,l=null,c=null,h=-1,u=0;for(let t=r+o.length;t<e.length;t++){const r=s.find(s=>e.startsWith(s,t));if(0===a&&r&&("{"===o?"}"===r:"}"!==r)){h=t,o+="|"+r;break}if(!"([{".includes(e[t])||"{"===e[t]&&"\\"===e[t-1]?")]}".includes(e[t])&&"\\"!==e[t-1]&&a--:a++,0===a&&"?"===e[t]&&u++,0===a&&!l){const s=/^(?:\\middle\s*\||\\mid(?![A-Za-z])|:(?!=)|\|)/.exec(e.slice(t));s&&":"===s[0]&&u?u--:s&&"|"!==s[0]?l={at:t,length:s[0].length}:s&&n&&!c&&!/\\left\s*$|\\right\s*$|\\middle\s*$/.test(e.slice(0,t))&&(c={at:t,length:1})}}if(l??=c,h<0||!l){i=r+1;continue}const p=e.slice(r+o.split("|")[0].length,l.at).trim(),f=e.slice(l.at+l.length,h).trim();if(!p||!f){i=r+1;continue}const d=o.split("|")[1];e=`${e.slice(0,r)}setbuilder((${p}),(${f}))${e.slice(h+d.length)}`,i=r}return e}const D=String.raw`\\(?:[Bb]igg?|right)[lrm]?`,G=new RegExp(`(?:${D}\\s*([\\]|])|(\\]))\\s*(?=[_^])`,"g");function H(e,t){let s=t;for(;" "===e[s];)s++;if("{"===e[s]){let t=0;for(let n=s;n<e.length;n++)if("{"===e[n])t++;else if("}"===e[n]&&0===--t)return{value:e.slice(s+1,n).trim(),end:n+1};return null}const n=/^\\[A-Za-z]+/.exec(e.slice(s));return n?{value:n[0],end:s+n[0].length}:"-"===e[s]&&/\d/.test(e[s+1]??"")?{value:e.slice(s,s+2),end:s+2}:e[s]?{value:e[s],end:s+1}:null}function Q(e,t){let s=t;for(;" "===e[s];)s++;const n=e[s];if("_"!==n&&"^"!==n)return null;const r=H(e,s+1);if(!r)return null;let i=r.end;for(;" "===e[i];)i++;const o="_"===n?"^":"_";if(e[i]!==o)return null;const a=H(e,i+1);return a?"_"===n?{lo:r.value,hi:a.value,end:a.end}:{lo:a.value,hi:r.value,end:a.end}:null}function V(e,t,s){let n=0;for(let r=t-1;r>=0;r--){if(s){if("]"===e[r])n++;else if("["===e[r]){if(0===n)return{start:r,end:r+1};n--}continue}const i=e.slice(0,r+1),o=/\\right\s*[\]|)}.]$|\\[Bb]igg?r?\s*[\]|]$/.exec(i),a=/\\left\s*(?:[[(.]|\\\{)$|\\[Bb]igg?l?\s*\[$/.exec(i);if(o&&r+1<t)n++,r-=o[0].length-1;else if(a){if(0===n)return{start:r+1-a[0].length,end:r+1};n--,r-=a[0].length-1}}return null}function X(e,t){let s=0;for(let n=t-1;n>=0;n--){const t=e[n];if(")]}".includes(t))s++;else if("([{".includes(t)){if(0===s)return n+1;s--}else{if(0===s&&/[=<>&,]/.test(t))return n+1;if(0===s&&/\\(?:approx|leq?|geq?|neq?|to|quad|qquad)$/.test(e.slice(0,n+1)))return n+1}}return 0}function J(e){for(let t=0;t<50;t++){G.lastIndex=0;let t=null;for(let s=G.exec(e);s;s=G.exec(e)){const n=Q(e,s.index+s[0].length);if(!n)continue;const r=Boolean(s[2]),i="|"===(s[1]??s[2]),o=V(e,s.index,r);if((o||i&&!/^\\right/.test(s[0]))&&(!o||!/\\left\s*\|$/.test(e.slice(o.start,o.end)))){t={m:s,limits:n,opener:o};break}}if(!t)return e;const{m:s,limits:n,opener:r}=t,i=r?r.start:X(e,s.index),o=e.slice(r?r.end:i,s.index).trim(),a=/^\s*([A-Za-zα-ωΑ-Ω]|\\[A-Za-z]+)\s*=\s*([\s\S]+)$/.exec(n.lo),l=n.hi.replace(/^\s*(?:[A-Za-zα-ωΑ-Ω]|\\[A-Za-z]+)\s*=\s*/,""),c=a?`evaluated((${o}),${a[1]},${a[2]},${l})`:`evaluated((${o}),${n.lo},${l})`;e=`${e.slice(0,i)}${i>0&&/[^\s(=]/.test(e[i-1])?" ":""}${c}${e.slice(n.end)}`}return e}const K={__proto__:null,sine:"sin",cosine:"cos",tangent:"tan",secant:"sec",cosecant:"csc",cotangent:"cot"};const Y=100;const ee={__proto__:null},te={__proto__:null};[..."⁰¹²³⁴⁵⁶⁷⁸⁹"].forEach((e,t)=>{ee[e]=String(t)}),[..."₀₁₂₃₄₅₆₇₈₉"].forEach((e,t)=>{te[e]=String(t)}),Object.assign(ee,{"⁺":"+","⁻":"-","⁼":"=","⁽":"(","⁾":")","ᵃ":"a","ᵇ":"b","ᶜ":"c","ᵈ":"d","ᵉ":"e","ᶠ":"f","ᵍ":"g","ʰ":"h","ⁱ":"i","ʲ":"j","ᵏ":"k","ˡ":"l","ᵐ":"m","ⁿ":"n","ᵒ":"o","ᵖ":"p","ʳ":"r","ˢ":"s","ᵗ":"t","ᵘ":"u","ᵛ":"v","ʷ":"w","ˣ":"x","ʸ":"y","ᶻ":"z"}),Object.assign(te,{"₊":"+","₋":"-","₌":"=","₍":"(","₎":")","ₐ":"a","ₑ":"e","ₕ":"h","ᵢ":"i","ⱼ":"j","ₖ":"k","ₗ":"l","ₘ":"m","ₙ":"n","ₒ":"o","ₚ":"p","ᵣ":"r","ₛ":"s","ₜ":"t","ᵤ":"u","ᵥ":"v","ₓ":"x"});const se=new RegExp(`[${Object.keys(ee).join("")}]+|[${Object.keys(te).join("")}]+`,"g");const ne={__proto__:null,"½":"1/2","⅓":"1/3","⅔":"2/3","¼":"1/4","¾":"3/4","⅕":"1/5","⅖":"2/5","⅗":"3/5","⅘":"4/5","⅙":"1/6","⅚":"5/6","⅐":"1/7","⅛":"1/8","⅜":"3/8","⅝":"5/8","⅞":"7/8","⅑":"1/9","⅒":"1/10"},re=new RegExp(`(?<![\\w.,^_])(\\d+)[ \\t]?([${Object.keys(ne).join("")}])`,"g"),ie=/(?<![\w.,^_/])(\d+)[ \t]+([1-9]\d*)\/(\d+)(?![\d./^!])/g,oe=/(?<![\w.,^_])(\d+)\s*\\[dtc]?frac\s*(?:\{\s*(\d+)\s*\}\s*\{\s*(\d+)\s*\}|(\d)(\d)(?!\d))/g;function ae(e){const t=e.replace(/^\s*(?:\\(?:text|textrm|mathrm)\s*\{\s*(?:if|for|when|where)\s*\}|\b(?:if|for|when|where)\b)\s*,?/,"").replace(/[\s,.;]+$/,"").trim();return/^(?:\\(?:text|textrm|mathrm)\s*\{\s*)?(?:otherwise|else)\s*\}?$/.test(t)?"":t}function le(e,t){const s=[];let n=0,r=0;for(let i=0;i<e.length;i++)F.includes(e[i])?n++:P.includes(e[i])?n--:0===n&&e.startsWith(t,i)&&(s.push(e.slice(r,i)),i+=t.length-1,r=i+1);return s.push(e.slice(r)),s}function ce(e,t,s){let n=0;const r=new RegExp(`\\b${s}\\b`,"iy");for(let s=t;s<e.length;s++)if(F.includes(e[s]))n++;else if(P.includes(e[s])){if(--n<0)return-1}else if(0===n&&(r.lastIndex=s,r.test(e)))return s;return-1}function he(e,t){let s=0;for(let n=t;n<e.length;n++)if(F.includes(e[n]))s++;else if(P.includes(e[n])&&--s<0)return n;return e.length}function ue(e,t){let s=1;for(let n=t+1;n<e.length;n++)if("?"===e[n])s++;else if(":"===e[n]&&0===--s)return e.slice(0,t+1)+"("+e.slice(t+1,n)+")?:"+e.slice(n+1);return e}function pe(e){for(let t=2;t<e.length;t++)if("g"===e[t]&&"log"===e.slice(t-2,t+1))if("_"===e[t+1]){if(F.includes(e[t+2]))continue;const s=/^(?:\d+(?:\.\d+)?|[A-Za-z\u0370-\u03FF])/.exec(e.slice(t+2))?.[0];if(!s)continue;const n=t+2+s.length;e=e.slice(0,t+2)+"{"+s+"}"+e.slice(n)}else if("("===e[t+1]){let s=1,n=0,r="",i="",o=0;for(let a=t+2;a<e.length;a++){if(")]".includes(e[a])?s--:"([".includes(e[a])&&s++,0===s){o=a;break}1===s&&","===e[a]?n++:0===n?r+=e[a]:i+=e[a]}1===n&&o>0&&(e=e.slice(0,t+1)+"_{"+i+"}("+r+")"+e.slice(o+1))}return e}function fe(e){const t="⌈"===e[0]?"ceil":"floor";let s=1;for(let n=1;n<e.length;n++)if("⌊"===e[n]||"⌈"===e[n]?s++:"⌋"!==e[n]&&"⌉"!==e[n]||s--,0===s)return t+"("+e.slice(1,n)+")"+e.slice(n+1);return e}function de(e){let t=1;for(let s=0;s<e.length;s++){if(")]".includes(e[s])?t--:"([".includes(e[s])&&t++,0!==t)continue;const n=e.slice(0,s),r=e[s+1];if("{"===r||"("===r)return r+n+","+e.slice(s+2);const i=/^[A-Za-z0-9.\u0370-\u03FF]+/.exec(e.slice(s+1))?.[0]??"";return"{"+n+","+i+"}"+e.slice(s+1+i.length)}return e}const ge=new Set(["pmatrix","bmatrix","vmatrix"]);function me(e){const t=e.indexOf("}"),s=e.indexOf("{");if(t<0||s>=0&&s<t)return!1;const n=e.slice(0,t);if(!n.includes("matrix"))return!1;const r=e.indexOf("\\end{"+n+"}"),i=e.indexOf("\\begin{"+n+"}",t+1);if(r<0||-1!==i&&i<r)return!1;const o=e.slice(t+1,r).trim().split("\\\\").map(e=>e.trim()).filter(e=>e.length).map(e=>"["+e.replace(/&/g,", ")+"]"),a=e.slice(r+6+n.length);return ge.has(n)?n+"("+o.join(", ")+")"+a:"["+o.join(", ")+"]"+a}function $e(e,t){if("{"!==e[t+1])return[e[t+1],t+2];let s=1,n="";for(let r=t+2;r<e.length;r++){if("{"===e[r])s++;else if("}"===e[r]&&0===--s)return[n,r+1];n+=e[r]}return[n,e.length]}function xe(e){const{lower:t,upper:s,next:n}=function(e){let t="",s="",n=0;return"_"===e[0]&&([t,n]=$e(e,0),"^"===e[n]&&([s,n]=$e(e,n))),{lower:t,upper:s,next:n}}(e),r=e.slice(n),i=e=>e.replace(/\\,/g,"").replace(/\\!/g,"");let o,a="",l="",c=r.indexOf("\\mathrm{d}");if(c>=0){o=r.slice(0,c);const e=/^\{([^{}]*)\}/.exec(r.slice(c+10));a=e?e[1]:r[c+10],l=r.slice(c+10+(e?e[0].length:1))}else(c=function(e){let t=0;for(let s=0;s<e.length;s++)if(F.includes(e[s]))t++;else if(P.includes(e[s]))t--;else if(0===t&&"d"===e[s]&&/[A-Za-z\u0370-\u03FF]/.test(e[s+1]??"")&&!/[A-Za-z]/.test(e[s+2]??""))return s;return-1}(r))>=0?(o=r.slice(0,c),a=r[c+1],l=r.slice(c+2)):o=r;if(!a){const e=/frac\{d([A-Za-z])\}/.exec(o);e&&(a=e[1],o=o.replace(e[0],"frac{1}"))}if(!a&&/^[A-Za-z]$/.test(t)){a=[...i(o).matchAll(/(?<![A-Za-z\\])([A-Za-z])(?![A-Za-z(])/g)].map(e=>e[1]).find(e=>e!==t&&e!==s)??"x"}let h="int("+(i(o).trim()?i(o):"1");return a&&(h+=","+a),(t||s)&&(h+=","+t+","+s),h+")"+l}function ye(e){const t=e[0];if(!(t>="A"&&t<="Z"||t>="a"&&t<="z"||t>="α"&&t<="ω"))return!1;const s=e.slice(2);let n=1;for(let e=0;e<s.length;e++)if(P.includes(s[e])?n--:F.includes(s[e])&&n++,0===n)return"("+s.slice(0,e)+","+t+")"+s.slice(e+1);return!1}function be(e){return"{"===e[0]||e.length<2?e:"{"+e[0]+"}{"+e[1]+"}"+e.slice(2)}const we=/[A-Za-z\u0370-\u03FF]/;function ve(e,t){if("{"!==e[t])return null;let s=0;for(let n=t;n<e.length;n++)if("{"===e[n])s++;else if("}"===e[n]&&0===--s)return[e.slice(t+1,n),n+1];return null}function ke(e,t){const s=/^\^(?:\{(\d+)\}|(\d))/.exec(e.slice(t));return s?[Number(s[1]??s[2]),t+s[0].length]:[1,t]}function Ae(e,t){const s=e.replace(/\\mathrm\{d\}/g,"d"),n=t.replace(/\\mathrm\{d\}/g,"d"),r=/^d(?:\^\{?(\d+)\}?)?([A-Za-z\u0370-\u03FF]?)$/.exec(s),i=/^d([A-Za-z\u0370-\u03FF])(?:\^\{?(\d+)\}?)?$/.exec(n);return r&&i?{n:Number(r[1]??1),of:r[2],by:i[1]}:null}function Ee(e,t){if(F.includes(e[t])){let s=0;for(let n=t;n<e.length;n++)if(F.includes(e[n]))s++;else if(P.includes(e[n])&&0===--s)return[e.slice(t,n+1),n+1];return[e.slice(t),e.length]}let s=0;for(let n=t;n<e.length;n++)if(F.includes(e[n]))s++;else{if(P.includes(e[n])&&--s<0)return[e.slice(t,n),n];if(0===s&&("="===e[n]||","===e[n]))return[e.slice(t,n),n]}return[e.slice(t),e.length]}const _e=(e,t,s)=>`der(${e},${t}${s>1?","+s:""})`;const Me="strtktk",Oe="ABCDEFGHIJ",qe=e=>Me+[...String(e)].map(e=>Oe[e]).join(""),Se=new RegExp(`^${Me}[${Oe}]+$`);function ze(e){let t="";for(let s=0;s<e.length;s++){const n=e[s];if('"'===n||"`"===n){const n=Ne(e,s);t+=e.slice(s,n+1),s=n}else if("'"===n)if(/[A-Za-z0-9)\]′\u0370-\u03FF]/.test(t[t.length-1]??""))t+="′";else{const n=Ne(e,s);t+=e.slice(s,n+1),s=n}else t+=n}return t}function Ne(e,t){let s=!1;for(let n=t+1;n<e.length;n++)if(s)s=!1;else if("\\"===e[n])s=!0;else if(e[n]===e[t])return n;return e.length-1}const Le=new RegExp(`${Me}[${Oe}]+|§\\d+§`,"g");function Ze(e,t){return t&&0!==t.size?e.map(e=>"string"!=typeof e?e:e.replace(Le,e=>{const s=t.get(e);if(void 0!==s)return s;for(let s=e.length-1;s>7;s--){const n=t.get(e.slice(0,s));if(void 0!==n)return n+e.slice(s)}return e})):e}const Ce={factorial:"((n) => { if (!Number.isInteger(n) || n < 0) return NaN; if (n > 170) return Infinity; let r = 1; for (let i = 2; i <= n; i++) r *= i; return r; })",perm:"((n, k) => { if (!Number.isInteger(n) || !Number.isInteger(k) || k < 0 || k > n) return NaN; let r = 1; for (let i = n - k + 1; i <= n && r !== Infinity; i++) r *= i; return r; })",comb:"((n, k) => { if (!Number.isInteger(n) || !Number.isInteger(k) || k < 0 || k > n) return NaN; k = Math.min(k, n - k); let r = 1; for (let i = 1; i <= k && r !== Infinity; i++) r = r * (n - k + i) / i; return Math.round(r); })",gcd:"((args)=>{let gcd = function(a,b) { return (!b)?a:gcd(b,a%b); }; return Math.abs(args.reduce(gcd));})",det:"((m) => { const a = m.map((r) => r.slice()); const n = a.length; let d = 1; for (let i = 0; i < n; i++) { let p = i; for (let r = i + 1; r < n; r++) if (Math.abs(a[r][i]) > Math.abs(a[p][i])) p = r; if (a[p][i] === 0) return 0; if (p !== i) { const t = a[p]; a[p] = a[i]; a[i] = t; d = -d; } d *= a[i][i]; for (let r = i + 1; r < n; r++) { const f = a[r][i] / a[i][i]; for (let c = i; c < n; c++) a[r][c] -= f * a[i][c]; } } return d; })",times:"((a, b) => { const L = Array.isArray; const m = (v) => L(v) && v.length > 0 && v.every((r) => L(r) && r.length > 0 && r.length === v[0].length && !r.some(L)) && (v.length > 1 || v[0].length > 1); const l = (v) => L(v) && v.length > 0 && !v.some(L); const p = (A, B) => (A[0].length === B.length ? A.map((r) => B[0].map((_, j) => r.reduce((s, x, k) => s + x * B[k][j], 0))) : NaN); const e = (x, y) => (L(x) && L(y) ? (x.length === y.length ? x.map((t, i) => e(t, y[i])) : NaN) : L(x) ? x.map((t) => e(t, y)) : L(y) ? y.map((t) => e(x, t)) : x * y); if (m(a) && m(b)) return p(a, b); if (m(a) && l(b)) { const c = p(a, b.map((y) => [y])); return L(c) ? c.map((r) => r[0]) : c; } if (l(a) && m(b)) { const c = p([a], b); return L(c) ? c[0] : c; } return e(a, b); })",same:"((a, b) => { const s = (x, y) => (Array.isArray(x) && Array.isArray(y) ? x.length === y.length && x.every((v, i) => s(v, y[i])) : x == y); return s(a, b); })",union:"((a, b) => [...new Set([].concat(a, b))])",intersection:"((a, b) => [].concat(a).filter((x) => [].concat(b).includes(x)))",round:"((x, d) => { const p = Math.pow(10, d); return (x < 0 ? -Math.round(-x * p) : Math.round(x * p)) / p; })",mod:"((a, n) => ((a % n) + n) % n)",root:"((n, x) => (x < 0 && Math.abs(n % 2) === 1 ? -Math.pow(-x, 1 / n) : Math.pow(x, 1 / n)))",contains:"((s, t) => { const f = (x) => (x == null ? String() : String(x)).replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32)); return f(s).includes(f(t)); })",startswith:"((s, t) => { const f = (x) => (x == null ? String() : String(x)).replace(/[A-Z]/g, (c) => String.fromCharCode(c.charCodeAt(0) + 32)); return f(s).startsWith(f(t)); })"},Re="lambda x: str().join(chr(ord(c) + 32) if 65 <= ord(c) <= 90 else c for c in (str() if x is None or x != x else str(int(x)) if isinstance(x, float) and x.is_integer() else str(x)))",Ie=`lambda s, t, f=${Re}: f(t) in f(s)`,je=`lambda s, t, f=${Re}: f(s).startswith(f(t))`,Te={contains:`(${Ie})`,startswith:`(${je})`},Fe={contains:`np.vectorize(${Ie}, otypes=[bool])`,startswith:`np.vectorize(${je}, otypes=[bool])`};for(const e of[Ce,Te,Fe])Object.setPrototypeOf(e,null);const Pe="([{",We=")]}";function Be(e,t){let s=0;for(let n=t;n<e.length;n++)if(Pe.includes(e[n]))s++;else if(We.includes(e[n])&&0===--s)return n;return-1}function Ue(e){if(!e.trim())return[];const t=[];let s=0,n=0;for(let r=0;r<e.length;r++)Pe.includes(e[r])?s++:We.includes(e[r])?s--:","===e[r]&&0===s&&(t.push(e.slice(n,r).trim()),n=r+1);return t.push(e.slice(n).trim()),t}function De(e,t,s){const n=[...e.matchAll(t)];for(let t=n.length-1;t>=0;t--){const r=n[t],i=r.index+r[0].length-1,o=Be(e,i);if(o<0)continue;const a=s(Ue(e.slice(i+1,o)),r,e.slice(o+1));if(null==a)continue;const[l,c=0]=Array.isArray(a)?a:[a];if((e=e.slice(0,r.index)+l+e.slice(o+1+c)).length>1e5)throw new Error("The expression expands too much when read")}return e}const Ge=e=>new RegExp(`(?<![\\w.])(${e})\\s*\\(`,"g"),He=e=>`(${e})`,Qe=e=>/^\[.*\]$/s.test(e.trim())?`{${e.trim().slice(1,-1)}}`:e,Ve=[...Object.entries(Ce),...Object.entries(Fe),...Object.entries(Te)];const Xe={IF:e=>e.length>=2&&`((${e[0]}) ? (${e[1]}) : (${e[2]??"false"}))`,AND:e=>e.length&&`(${e.map(He).join(" & ")})`,OR:e=>e.length&&`(${e.map(He).join(" ∨ ")})`,NOT:e=>1===e.length&&`(!(${e[0]}))`,XOR:e=>2===e.length&&`((${e[0]}) \\xor (${e[1]}))`,LOG:e=>2===e.length?`log_{${e[1]}}(${e[0]})`:1===e.length&&`log_{10}(${e[0]})`,ATAN2:e=>2===e.length&&`atan2(${e[1]}, ${e[0]})`,INT:e=>1===e.length&&`floor(${e[0]})`,FIXED:e=>(1===e.length||2===e.length)&&`fixed(${e[0]}, ${e[1]??2})`,"FLOOR.MATH":e=>1===e.length&&`floor(${e[0]})`,"CEILING.MATH":e=>1===e.length&&`ceil(${e[0]})`,FACT:e=>1===e.length&&`((${e[0]})!)`,COMBIN:e=>2===e.length&&`comb(${e.join(", ")})`,PERMUT:e=>2===e.length&&`perm(${e.join(", ")})`,AVERAGE:e=>`mean(${e.join(", ")})`,SUM:e=>`sum(${e.join(", ")})`,PRODUCT:e=>`prod(${e.join(", ")})`,MAX:e=>`max(${e.join(", ")})`,MIN:e=>`min(${e.join(", ")})`,MEDIAN:e=>`median(${e.join(", ")})`,"STDEV.P":e=>`stdev(${e.join(", ")})`,STDEVP:e=>`stdev(${e.join(", ")})`,CEILING:e=>(1===e.length||2===e.length&&"1"===e[1].trim())&&`ceil(${e[0]})`,RAND:e=>!e.length&&"random()",ISNUMBER:e=>1===e.length&&`((${e[0]}) ∈ ℝ)`,LOG10:e=>1===e.length&&`log_{10}(${e[0]})`,MMULT:e=>2===e.length&&`((${e[0]}) * (${e[1]}))`};function Je(e,t){for(let s=t,n=0;s>=0;s--)if(")"===e[s])n++;else if("("===e[s]&&0===--n)return s;return-1}function Ke(e){const t="(?:[\\p{L}_][\\p{L}\\p{N}_.]*|\\d+(?:\\.\\d+)?|§\\d+§)";return e=function(e,t){const s=new RegExp(`^(?:${t})`,"u"),n=new RegExp(`(?:${t})$`,"u"),r=/[\p{L}_][\p{L}\p{N}_.]*$/u;for(let t=e.indexOf("^");t>=0;t=e.indexOf("^",t+1)){let i,o=t+1;for(;/\s/.test(e[o]??"");)o++;if("("===e[o])i=Be(e,o)+1;else{const t=s.exec(e.slice(o));i=t?o+t[0].length:0,t&&"("===e[i]&&(i=Be(e,i)+1)}if(i<=o||!/^\s*\^/.test(e.slice(i)))continue;let a,l=t;for(;l>0&&/\s/.test(e[l-1]);)l--;if(")"===e[l-1]){a=Je(e,l-1);const t=a>0&&r.exec(e.slice(Math.max(0,a-64),a));t&&(a-=t[0].length)}else{const t=n.exec(e.slice(Math.max(0,l-64),l));a=t?l-t[0].length:-1}a<0||(e=`${e.slice(0,a)}(${e.slice(a,i)})${e.slice(i)}`,t=i+1)}return e}(e=(e=(e=e.replace(/^\s*=/,"").replace(/(?<![\w.])_xlfn\./g,"")).replace(/&/g,"+")).replace(new RegExp(`(^|[(,=<>*/+^-]\\s*)-\\s*(${t})(?=\\s*\\^)`,"gu"),"$1(-$2)"),t),De(e,/(?<![\w.])([A-Z][A-Z0-9]*(?:\.[A-Z]+)?)\s*\(/g,(e,t)=>Xe[t[1]]?.(e)||null)}const Ye={POWER:"pow",SQRT:"sqrt",ABS:"abs",LN:"ln",EXP:"exp",FLOOR:"floor",CEIL:"ceil",CEILING:"ceil",ROUND:"round",GREATEST:"max",LEAST:"min",MOD:"mod",ATAN2:"atan2",FACTORIAL:"factorial",SIN:"sin",COS:"cos",TAN:"tan",SEC:"sec",CSC:"csc",COT:"cot",ASIN:"asin",ACOS:"acos",ATAN:"atan",SINH:"sinh",COSH:"cosh",TANH:"tanh",ASINH:"asinh",ACOSH:"acosh",ATANH:"atanh",COMB:"comb",PERM:"perm",GCD:"gcd",DERIVATIVE:"derivative",LIMIT:"limit",UNION:"union",EXPM1:"expm1",TRUNC:"trunc",SUMMATION:"sum"},et="strtktk[A-J]+";function tt(e){for(let t=!0;t;){const s=e;t=(e=e.replace(new RegExp(`(${et})\\s*\\|\\|\\s*(${et})`,"g"),"$1 + $2"))!==s}const t=new RegExp(`^\\(typeof\\(([^()]*)\\)\\s+IN\\s+\\(${et},\\s*${et}\\)\\s+AND\\s+(.*)\\)$`,"s");for(let s=e.lastIndexOf("(typeof(");s>=0;s=s>0?e.lastIndexOf("(typeof(",s-1):-1){const n=Be(e,s),r=n>0&&t.exec(e.slice(s,n+1));if(!r)continue;const[,i,o]=r,a=new Map([[`${i} = TRUNC(${i})`,"ℤ"],[`${i} = TRUNC(${i}) AND ${i} > 0`,"ℕ"],[`ABS(${i}) < ∞`,"ℝ"]]).get(o.trim());a&&(e=`${e.slice(0,s)}((${i}) ∈ ${a})${e.slice(n+1)}`)}e=De(e,/(?<![\w.])CAST\s*\(/gi,e=>{const t=1===e.length&&/^(.*)\s+AS\s+(?:REAL|FLOAT|DOUBLE(?:\s+PRECISION)?|NUMERIC|DECIMAL|INTEGER|INT)\s*$/is.exec(e[0]);return t?`(${t[1]})`:null}),e=(e=(e=De(e=function(e){for(let t=0;t<50;t++){const t=[...e.matchAll(/\bCASE\s+WHEN\b/gi)];if(!t.length)return e;const s=t[t.length-1].index,n=/\bEND\b/i.exec(e.slice(s));if(!n)return e;const r=e.slice(s+4,s+n.index).split(/\b(WHEN|THEN|ELSE)\b/i).map(e=>e.trim()),i=[];let o="null";for(let e=1;e<r.length;e+=2){const t=r[e].toUpperCase();"WHEN"===t?i.push([r[e+1],r[e+3]]):"ELSE"===t&&(o=r[e+1])}const a=i.reduceRight((e,[t,s])=>`((${t}) ? (${s}) : ${e})`,He(o));e=e.slice(0,s)+a+e.slice(s+n.index+3)}return e}(e=e.replace(/(?<![\w.])ARRAY\s*\[/g,"[")),/(?<![\w.])LOG\s*\(/g,e=>2===e.length?`log_{${e[0]}}(${e[1]})`:`log_{10}(${e[0]})`)).replace(/(?<![\w.])PI\s*\(\s*\)/g,"π")).replace(/(?<![\w.])([A-Z][A-Z0-9]*)(?=\s*\()/g,e=>Ye[e]??e);for(const[t,s]of[[/\bNOT\s+IN\s*\(/g,"∉"],[/\bIN\s*\(/g,"∈"]])for(let n=t.exec(e);n;n=t.exec(e)){const r=n.index+n[0].length-1,i=Be(e,r);if(i<0)break;e=`${e.slice(0,n.index)}${s} {${e.slice(r+1,i)}}${e.slice(i+1)}`,t.lastIndex=0}return e.replace(/<>/g," ≠ ")}const st={sin:"sin",cos:"cos",tan:"tan",sinh:"sinh",cosh:"cosh",tanh:"tanh",arcsin:"asin",arccos:"acos",arctan:"atan",arcsinh:"asinh",arccosh:"acosh",arctanh:"atanh",arctan2:"atan2",sqrt:"sqrt",cbrt:"cbrt",exp:"exp",expm1:"expm1",log:"log",log2:"log2",log10:"log10",floor:"floor",ceil:"ceil",round:"round",power:"pow",abs:"abs",absolute:"abs",fabs:"abs",pi:"PI",e:"E",gcd:"gcd"},nt={maximum:"max",minimum:"min",nanmax:"max",nanmin:"min",nansum:"sum",nanprod:"prod",nanmean:"mean",nanmedian:"median",nanstd:"stdev",mod:"mod",fmod:"mod",where:"where",isin:"isin",isclose:"isclose",isfinite:"isfinite",logical_and:"logical_and",logical_or:"logical_or",logical_not:"logical_not",logical_xor:"logical_xor",prod:"prod",sum:"sum",trunc:"trunc",union1d:"union",intersect1d:"intersection",dot:"np_times",matmul:"np_times",multiply:"np_times",array_equal:"np_equal"};const rt=e=>e.replace(/(?<![\w.])set\s*\(\s*\)/g,"∅"),it={inf:"∞",nan:"NaN"};function ot(e){return(e=(e=function(e){return e=e.replace(/(?<![\w.])sympy\.Interval\.(open|Lopen|Ropen)\s*\(/g,(e,t)=>`sympy_interval_${t}(`),e=e.replace(/(?<![\w.])sympy\.S\.(\w+)/g,(e,t)=>lt[t]??e),e=e.replace(/(?<![\w.])sympy\.(Reals|Integers|Naturals0?|Rationals|Complexes)\b/g,(e,t)=>lt[t]),e.replace(/(?<![\w.])sympy\.(\w+)/g,(e,t)=>ct[t]??at[t]??e)}(e=(e=(e=(e=(e=(e=(e=e.replace(/(?<![\w.])(?:np|numpy)\.random\.random\(\s*\)|(?<![\w.])random\.random\(\s*\)/g,"Math.random()")).replace(/(?<![\w.])(?:np|numpy)\.linalg\.(det|inv)\b/g,"$1")).replace(/(?<![\w.])(?:np|numpy)\.array\s*\(/g,"(")).replace(/(?<![\w.])(?:np|numpy)\.(\w+)/g,(e,t)=>st[t]?`Math.${st[t]}`:nt[t]?nt[t]:it[t]??e)).replace(/(?<![\w.])(?:math|Math)\.(factorial|isfinite|isclose|prod|inf|nan|abs|trunc)\b/g,(e,t)=>it[t]??t)).replace(/(?<![\w.])Number\.(isInteger|isFinite)\b/g,(e,t)=>t.toLowerCase())).replace(/(?<![\w.])scipy\.(?:special|integrate)\.(\w+)/g,"$1"))).replace(/\bMZ_INFINITY\b/g,"∞")).replace(/(?<![\w.])mz_(?=\w+\s*\()/g,"")}const at={pi:"π",E:"e",oo:"∞",true:"true",false:"false",nan:"NaN",I:"i"},lt={Reals:"ℝ",Integers:"ℤ",Naturals:"ℕ",Naturals0:"ℕ",Rationals:"ℚ",Complexes:"ℂ"},ct={sqrt:"Math.sqrt",exp:"Math.exp",sin:"Math.sin",cos:"Math.cos",tan:"Math.tan",sinh:"Math.sinh",cosh:"Math.cosh",tanh:"Math.tanh",asin:"Math.asin",acos:"Math.acos",atan:"Math.atan",asinh:"Math.asinh",acosh:"Math.acosh",atanh:"Math.atanh",atan2:"atan2",csc:"csc",sec:"sec",cot:"cot",log:"log",Abs:"abs",floor:"floor",ceiling:"ceil",Max:"max",Min:"min",factorial:"factorial",binomial:"comb",ff:"perm",gcd:"gcd",gcd_list:"sympy_list_gcd",Mod:"mod",root:"sympy_root",real_root:"sympy_root",det:"det",Rational:"sympy_rational",Integer:"sympy_value",Float:"sympy_value",Eq:"sympy_eq",Ne:"sympy_ne",And:"sympy_and",Or:"sympy_or",Not:"sympy_not",Xor:"sympy_xor",Nand:"sympy_nand",Nor:"sympy_nor",Implies:"sympy_implies",Equivalent:"sympy_iff",Piecewise:"sympy_piecewise",FiniteSet:"sympy_set",Interval:"sympy_interval",Contains:"sympy_contains",Union:"union",Intersection:"intersection",diff:"derivative",integrate:"sympy_integrate",limit:"sympy_limit",summation:"sympy_summation",product:"sympy_product",expm1:"expm1",Matrix:""},ht={open:"()",Lopen:"(]",Ropen:"[)"};const ut=e=>{const t=/^\((.*)\)$/s.exec(e.trim());return t?Ue(t[1]):null},pt=e=>t=>2===t.length&&`${e[0]}${t[0]}, ${t[1]}${e[1]}`,ft=e=>ut(e)??[e],dt={where:e=>3===e.length&&`((${e[0]}) ? (${e[1]}) : (${e[2]}))`,both:e=>2===e.length&&`((${e[0]}) & (${e[1]}))`,either:e=>2===e.length&&`((${e[0]}) ∨ (${e[1]}))`,np_times:e=>2===e.length&&`((${e[0]}) * (${e[1]}))`,np_equal:e=>2===e.length&&`((${e[0]}) = (${e[1]}))`,logical_and:e=>2===e.length&&`((${e[0]}) & (${e[1]}))`,logical_or:e=>2===e.length&&`((${e[0]}) ∨ (${e[1]}))`,logical_xor:e=>2===e.length&&`((${e[0]}) \\xor (${e[1]}))`,logical_not:e=>1===e.length&&`(!(${e[0]}))`,isin:e=>2===e.length&&`((${e[0]}) ∈ ${Qe(e[1])})`,contains:e=>2===e.length&&`((${e[1]}) ∈ ${Qe(e[0])})`,isclose:e=>e.length>=2&&`((${e[0]}) ≈ (${e[1]}))`,isfinite:e=>1===e.length&&`((${e[0]}) ∈ ℝ)`,isinteger:e=>1===e.length&&`((${e[0]}) ∈ ℤ)`,factorial:e=>1===e.length&&`((${e[0]})!)`,union:e=>2===e.length&&`((${e[0]}) ∪ (${e[1]}))`,union_:e=>2===e.length&&`((${e[0]}) ∪ (${e[1]}))`,intersection:e=>2===e.length&&`((${e[0]}) ∩ (${e[1]}))`,sq:e=>1===e.length&&`((${e[0]})^2)`,cube:e=>1===e.length&&`((${e[0]})^3)`,fract:e=>1===e.length&&`((${e[0]}) - floor(${e[0]}))`,inversesqrt:e=>1===e.length&&`(1/sqrt(${e[0]}))`,radians:e=>1===e.length&&`((${e[0]})°)`,degrees:e=>1===e.length&&`((${e[0]})/°)`,mod:e=>2===e.length&&`((${e[0]}) \\mod (${e[1]}))`,atan:e=>2===e.length&&`atan2(${e[0]}, ${e[1]})`,exp2:e=>1===e.length&&`((2)^(${e[0]}))`,trunc:e=>1===e.length&&`((${e[0]}) < 0 ? (ceil(${e[0]})) : floor(${e[0]}))`,...Object.fromEntries([2,3,4].map(e=>[`mat${e}`,t=>t.length===e*e&&`[${Array.from({length:e},(s,n)=>`[${Array.from({length:e},(s,r)=>t[r*e+n]).join(", ")}]`).join(", ")}]`])),vec2:e=>`(${e.join(", ")})`,vec3:e=>`(${e.join(", ")})`,vec4:e=>`(${e.join(", ")})`,derivative:e=>e.length>=2&&`der(${e.join(", ")})`,sympy_rational:e=>2===e.length&&`((${e[0]})/(${e[1]}))`,sympy_value:e=>1===e.length&&`(${e[0]})`,sympy_eq:e=>2===e.length&&`((${e[0]}) = (${e[1]}))`,sympy_ne:e=>2===e.length&&`((${e[0]}) ≠ (${e[1]}))`,sympy_and:e=>e.length&&`(${e.map(He).join(" & ")})`,sympy_or:e=>e.length&&`(${e.map(He).join(" ∨ ")})`,sympy_not:e=>1===e.length&&`(!(${e[0]}))`,sympy_xor:e=>2===e.length&&`((${e[0]}) \\xor (${e[1]}))`,sympy_nand:e=>2===e.length&&`((${e[0]}) \\nand (${e[1]}))`,sympy_implies:e=>2===e.length&&`((${e[0]}) \\implies (${e[1]}))`,sympy_iff:e=>2===e.length&&`((${e[0]}) \\iff (${e[1]}))`,sympy_nor:e=>2===e.length&&`((${e[0]}) \\nor (${e[1]}))`,sympy_piecewise:e=>{const t=e.map(ut);return!t.length||t.some(e=>!e||2!==e.length)?null:t.reduceRight((e,[t,s])=>null===e||/^True$|^true$/.test(s.trim())&&void 0===e?`(${t})`:`((${s}) ? (${t}) : ${e??"(null)"})`,void 0)},sympy_set:e=>`{${e.join(", ")}}`,sympy_interval:pt("[]"),sympy_interval_open:pt("()"),sympy_interval_Lopen:pt("(]"),sympy_interval_Ropen:pt("[)"),sympy_contains:e=>2===e.length&&`((${e[0]}) ∈ ${e[1]})`,sympy_list_gcd:e=>1===e.length&&`gcd(${e[0].trim().replace(/^\[|\]$/g,"")})`,sympy_root:e=>2===e.length&&`root(${e[1]}, ${e[0]})`,sympy_integrate:e=>{if(2!==e.length)return null;const t=ft(e[1]);return 3===t.length?`int(${e[0]}, ${t.join(", ")})`:1===t.length?`int(${e[0]}, ${t[0]})`:null},sympy_limit:e=>e.length>=3&&`lim(${e.slice(0,3).join(", ")}${e[3]?`, ${e[3]}`:""})`,sympy_summation:e=>{if(3===e.length&&/^\s*[A-Za-z]\w*\s*==/.test(e[1]))return`sum(${e[0]}, ${e[1].replace("==","=")}, ${e[2]})`;const t=2===e.length&&ft(e[1]);return 3===t?.length&&`(\\sum_{${t[0]}=${t[1]}}^{${t[2]}} (${e[0]}))`},sympy_product:e=>{if(3===e.length&&/^\s*[A-Za-z]\w*\s*==/.test(e[1]))return`prod(${e[0]}, ${e[1].replace("==","=")}, ${e[2]})`;const t=2===e.length&&ft(e[1]);return 3===t?.length&&`(\\prod_{${t[0]}=${t[1]}}^{${t[2]}} (${e[0]}))`},limit:e=>e.length>=3&&`lim(${e.join(", ")})`,summation:e=>3===e.length&&/^\s*[A-Za-z]\w*\s*==/.test(e[1])&&`sum(${e[0]}, ${e[1].replace("==","=")}, ${e[2]})`,sum:gt("sum"),prod:gt("prod")};function gt(e){return t=>{const s=1===t.length&&/^(.*?)\s+for\s+(\w+)\s+in\s+range\((.*)\)\s*$/s.exec(t[0]);if(!s)return null;const n=Ue(s[3]),[r,i]=1===n.length?["0",n[0]]:n,o=/\s*\+\s*1$/.test(i)?i.replace(/\s*\+\s*1$/,""):`(${i}) - 1`;return`(\\${e}_{${s[2]}=${r}}^{${o}} (${s[1]}))`}}function mt(e){e=function(e){for(let t=e.lastIndexOf("(lambda");t>=0;t=t>0?e.lastIndexOf("(lambda",t-1):-1){const s=Be(e,t);if(s<0||"("!==e[s+1])continue;const n=Be(e,s+1),r=/^\(lambda\s+([\w\s,]+?)\s*:\s*([\s\S]*)\)$/.exec(e.slice(t,s+1));if(n<0||!r)continue;const i=r[1].split(",").map(e=>e.trim()),o=Ue(e.slice(s+2,n));if(o.length!==i.length||!i.every(e=>/^[A-Za-z_]\w*$/.test(e)))continue;const a=new Map(i.map((e,t)=>[e,o[t]])),l=r[2].replace(new RegExp(`(?<![\\w.])(${i.join("|")})(?!\\w)`,"g"),e=>`(${a.get(e)})`);if((e=e.slice(0,t)+`(${l})`+e.slice(n+1)).length>1e5)throw new Error("The expression expands too much when read")}return e}(e),e=De(e,Ge("quad"),(e,t,s)=>{const n=/^lambda\s+(\w+)\s*:\s*(.*)$/s.exec(e[0]??"");if(!n||3!==e.length)return null;const r=/^\s*\[\s*0\s*\]/.exec(s);return[`int(${n[2]}, ${n[1]}, ${e[1]}, ${e[2]})`,r?r[0].length:0]}),e=De(e,Ge(Object.keys(dt).join("|")),(e,t)=>dt[t[1]](e)||null);for(let t=(e=De(e,Ge("float"),(e,t,s)=>{if(1!==e.length)return null;const n=/^\s*\.is_integer\(\s*\)/.exec(s);return n?[`((${e[0]}) ∈ ℤ)`,n[0].length]:`(${e[0]})`})).lastIndexOf(".includes(");t>=0;t=t>0?e.lastIndexOf(".includes(",t-1):-1){const s=t+9,n=Be(e,s);let r=t;if(We.includes(e[t-1])){let s=0;for(r=t-1;r>=0;r--)if(We.includes(e[r]))s++;else if(Pe.includes(e[r])&&0===--s)break}else for(;r>0&&/[\w.]/.test(e[r-1]);)r--;n<0||r<0||r===t||(e=`${e.slice(0,r)}((${e.slice(s+1,n)}) ∈ ${Qe(e.slice(r,t))})${e.slice(n+1)}`)}return e}for(const e of[Xe,Ye,st,nt,at,lt,ct,ht,dt,it])Object.setPrototypeOf(e,null);const $t=String.raw`(?:\([^()]*\)|\[[^\[\]]*\]|\{[^{}]*\}|(?<![\w.′^])[\w.′^]+)`,xt=e=>e.trim().split(/\s+/).join(String.raw`\s+`),yt=e=>new RegExp(String.raw`(?<![\w.])${xt(e)}(?![\w.])`,"gi"),bt=[["is greater than or equal to","≥"],["is more than or equal to","≥"],["is at least","≥"],["is no less than","≥"],["is not less than","≥"],["is less than or equal to","≤"],["is at most","≤"],["is no more than","≤"],["is not more than","≤"],["is not greater than","≤"],["is greater than",">"],["is more than",">"],["is bigger than",">"],["is larger than",">"],["is above",">"],["exceeds",">"],["is less than","<"],["is smaller than","<"],["is fewer than","<"],["is below","<"],["is not equal to","≠"],["does not equal","≠"],["doesn't equal","≠"],["is different from","≠"],["is approximately equal to","≈"],["is approximately","≈"],["is about","≈"],["is roughly","≈"],["is close to","≈"],["is equal to","="],["is the same as","="],["equals","="],["greater than or equal to","≥"],["less than or equal to","≤"],["at least","≥"],["at most","≤"],["greater than",">"],["more than",">"],["less than","<"],["not equal to","≠"],["equal to","="]],wt="[<>≤≥=≠≈]",vt={second:2,third:3,fourth:4,fifth:5,sixth:6,seventh:7,eighth:8,ninth:9,tenth:10};Object.setPrototypeOf(vt,null);const kt=Object.entries({sine:"sin",cosine:"cos",tangent:"tan",secant:"sec",cosecant:"csc",cotangent:"cot","natural log":"ln","natural logarithm":"ln",arcsine:"arcsin",arccosine:"arccos",arctangent:"arctan",arcsecant:"arcsec",arccosecant:"arccsc",arccotangent:"arccot","hyperbolic sine":"sinh","hyperbolic cosine":"cosh","hyperbolic tangent":"tanh",floor:"floor",ceiling:"ceil"}).sort((e,t)=>t[0].length-e[0].length),At=[["is non-negative","≥ 0"],["is nonnegative","≥ 0"],["is not negative","≥ 0"],["is non-positive","≤ 0"],["is nonpositive","≤ 0"],["is not positive","≤ 0"],["is positive","> 0"],["is negative","< 0"],["is non-zero","≠ 0"],["is nonzero","≠ 0"],["is not zero","≠ 0"],["is zero","= 0"],["is an integer","∈ ℤ"],["is not an integer","∉ ℤ"],["is a natural number","∈ ℕ"],["is a real number","∈ ℝ"],["is real","∈ ℝ"],["is a rational number","∈ ℚ"],["is rational","∈ ℚ"],...[["the integers","ℤ"],["the natural numbers","ℕ"],["the real numbers","ℝ"],["the rational numbers","ℚ"],["the complex numbers","ℂ"]].flatMap(([e,t])=>[[`is not in ${e}`,`∉ ${t}`],[`is in ${e}`,`∈ ${t}`]])],Et=/(?<![A-Za-z])(?:is|between|exceeds|equals?|does|doesn't|least|most|than|divides|and|or|of|to|squared|cubed|twice|percent|by|over|times|plus|minus|modulo)(?![A-Za-z])/i;bt.map(([e])=>e),At.map(([e])=>e),kt.map(([e])=>`${e} of`);const _t=Z([[" And "," & "],[" Or "," ∨ "],[" Not In "," ∉ "],[" Nor "," \\nor "],[" Xor "," \\xor "],[" Nand "," \\nand "],[" In "," ∈ "],[" Mod "," \\mod "],[" % ","%"],[" Perm ","\\perm "],[" P ","\\perm",!0],[" Comb ","\\comb "],[" C ","\\comb",!0],[" choose ","\\comb "],["! ","!_"],["\\in ","∈"],["\\notin ","∉"],["\\not\\in ","∉"],["\\to ","→"],["===","=...="]]),Mt=Z([...j,...function(){const e=[],t=(t,s)=>{e.push([t,s],[t+"(",s+"("])};for(const s of["sin","cos","tan","cot","csc","sec"]){e.push(["Math."+s+"(",s+"("],["\\"+s+"(",s+"("],["\\"+s,s]);for(const e of["sin","cos","tan"].includes(s)?["","h"]:[""]){const n=s+e,r="arc"+s+e;e&&(t("Math."+n,n),t("\\"+n,n));for(const s of["^-1","^{-1}","-1"])t("\\"+n+s,r),t(n+s,r),e||t("Math."+n+s,r);e||t("\\a"+n,r),t("\\arc"+n,r),t("Math.a"+n,r),t("Math.arc"+n,r)}}return e}()]),Ot=[["code",function(e){return/^\s*=|[(.%^<_eE]|\|\||\bif\b|\b(?:True|False|None|TRUE|FALSE|NULL)\b/.test(e)?(/^\s*=(?!=)/.test(e)&&(e=Ke(e)),(e=(e=(e=function(e){for(let t=0;t<50;t++){let t=!0;for(const s of e.matchAll(/\bif\b/g)){const n=s.index;let r=0,i=-1,o=e.length;for(let t=n+2;t<e.length;t++)if(Pe.includes(e[t]))r++;else{if(We.includes(e[t])&&--r<0){o=t;break}if(0===r&&","===e[t]){o=t;break}if(0===r&&/^then\b/.test(e.slice(t))&&/\W/.test(e[t-1])){i=-2;break}0===r&&i<0&&/^else\b/.test(e.slice(t))&&/\W/.test(e[t-1])&&(i=t)}if(i<0)continue;r=0;let a=0;for(let t=n-1;t>=0;t--)if(We.includes(e[t]))r++;else{if(Pe.includes(e[t])&&--r<0){a=t+1;break}if(0===r&&","===e[t]){a=t+1;break}}const l=e.slice(a,n).trim();if(!l)continue;const c=`((${e.slice(n+2,i).trim()}) ? (${l}) : (${e.slice(i+4,o).trim()}))`;e=e.slice(0,a)+c+e.slice(o),t=!1;break}if(t)return e}return e}(e=mt(e=function(e){return e.includes("set")?rt(De(e,/(?<![\w.])sorted\s*\(/g,e=>{if(1!==e.length)return null;const t=e[0];for(let e=0,s=0;e<t.length;e++)if("([{".includes(t[e]))s++;else if(")]}".includes(t[e]))s--;else if(0===s&&("|"===t[e]||"&"===t[e])){const s=e=>{const t=e.trim(),s=/^set\s*\(/.exec(t);return s&&Be(t,s[0].length-1)===t.length-1?t.slice(s[0].length,-1):null},[n,r]=[s(t.slice(0,e)),s(t.slice(e+1))];return null!==n&&null!==r?`((${n}) ${"|"===t[e]?"∪":"∩"} (${r}))`:null}return null})):e}(e=ot(e=tt(e=(e=(e=function(e){for(const[t,s]of Ve)for(let n=e.lastIndexOf(s);n>=0;n=n>0?e.lastIndexOf(s,n-1):-1){const r=n+s.length,i="("===e[r]?Be(e,r):-1;if(i<0)continue;let o=e.slice(r+1,i);"gcd"===t&&(o=o.trim().replace(/^\[|\]$/g,""));const a="times"===t||"same"===t?Ue(o):null,l="factorial"===t?`((${o})!)`:2===a?.length?`((${a[0]}) ${"times"===t?"*":"="} (${a[1]}))`:`${t}(${o})`;e=e.slice(0,n)+l+e.slice(i+1)}for(let t=e.lastIndexOf(").toFixed(");t>=0;t=t>0?e.lastIndexOf(").toFixed(",t-1):-1){const s=t+10-1,n=Be(e,s),r=e.lastIndexOf("Number(",t);n<0||r<0||Be(e,r+7-1)!==t||(e=e.slice(0,r)+`fixed(${e.slice(r+7,t)}, ${e.slice(s+1,n)})`+e.slice(n+1))}return e.replace(/\(\(\)=>\{let (sum|prod) = [01]; let loopCount = 0; for \(let (\w+)=(.*?);\2<=(.*?);\2\+\+\) \{if \(loopCount\+\+ > 1000000\)\{break\};\1 [+*]= (.*?);\}; return \1;\}\)\(\)/g,(e,t,s,n,r,i)=>`(\\${"sum"===t?"sum":"prod"}_{${s}=${n}}^{${r}} (${i}))`)}(e)).replace(/(?<![\w.])9e999\b/g,"∞")).replace(/(?<![\w.])(\d+(?:\.\d+)?)[eE]([+-]?\d+)(?![\w.])/g,"($1*10^{$2})"))))))).replace(/\b(?:True|TRUE)\b/g,"true").replace(/\b(?:False|FALSE)\b/g,"false").replace(/\b(?:None|NULL)\b/g,"null")).replace(/\^\^/g," \\xor ")).replace(/%(?=\s*(?:[(-]|[A-Za-z_][\w.]*\s*\())/g," \\mod ")):e}],["english",function(e){if(!Et.test(e))return e;let t=e;const s=(e,s,n,r="∈")=>{t=t.replace(new RegExp(String.raw`(?<![\w.])${xt(e)}\s+(${$t})\s+(?:and|to)\s+(${$t})(?:\s+(inclusive|exclusive))?(?![\w.])`,"gi"),(e,t,i,o)=>{const[a,l]="exclusive"===o?.toLowerCase()?["(",")"]:"inclusive"===o?.toLowerCase()?["[","]"]:[s,n];return` ${r} ${a}${t}, ${i}${l} `})};s("is not strictly between","(",")","∉"),s("is not between","[","]","∉"),s("is strictly between","(",")"),s("is between","[","]"),s("between","[","]"),t=t.replace(new RegExp(String.raw`(?<![\w.])is\s+(not\s+)?in\s+the\s+(open|closed|half-open)\s+interval\s+from\s+(${$t})\s+to\s+(${$t})`,"gi"),(e,t,s,n,r)=>{const[i,o]={open:["(",")"],closed:["[","]"],"half-open":["[",")"]}[s.toLowerCase()];return` ${t?"∉":"∈"} ${i}${n}, ${r}${o} `});const n=String.raw`(${$t})\s+`;t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?within\s+(${$t})\s*(%|\s+percent)?\s+of\s+(${$t})`,"gi"),(e,t,s,n,r,i)=>`(abs((${t}) - (${i})) ${s?">":"≤"} ${r?`(${n})/100 * ${/^\d+(\.\d+)?$/.test(i)?i:`abs(${i})`}`:`(${n})`})`),t=t.replace(new RegExp(n+String.raw`is\s+(?:a\s+)?multiple\s+of\s+(${$t})`,"gi"),"(($1) mod ($2) = 0)"),t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?divisible\s+by\s+(${$t})`,"gi"),(e,t,s,n)=>`((${t}) mod (${n}) ${s?"≠":"="} 0)`),t=t.replace(new RegExp(String.raw`(${$t})\s+divides\s+(${$t})`,"gi"),"(($2) mod ($1) = 0)"),t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?(even|odd)(?![\w.])`,"gi"),(e,t,s,n)=>`((${t}) mod 2 ${"even"===n.toLowerCase()!==Boolean(s)?"=":"≠"} 0)`),t=t.replace(new RegExp(n+String.raw`is\s+(not\s+)?a\s+whole\s+number(?![\w.])`,"gi"),(e,t,s)=>s?`((${t}) ∉ ℤ ∨ (${t}) < 0)`:`((${t}) ∈ ℤ & (${t}) ≥ 0)`);for(const[e,s]of At)t=t.replace(yt(e),` ${s} `);t=t.replace(yt("is not in")," ∉ ").replace(yt("is in")," ∈ ").replace(yt("is an element of")," ∈ ");for(const[e,s]of bt)t=t.replace(yt(e),` ${s} `);t=t.replace(yt("is not")," ≠ "),t=t.replace(/(?<=[\w.)\]}′]\s+)is(?=\s+[\w.(\[{-])/gi," = ");for(let e=!0;e;){const s=t;t=t.replace(new RegExp(String.raw`(${$t})\s*(${wt})\s*(${$t})\s+(and|or)\s+(${wt})`,"gi"),"$1 $2 $3 $4 $1 $5"),e=t!==s}t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?absolute\s+value\s+of\s+(${$t})`,"gi"),"abs($1)"),t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?square\s+root\s+of\s+(${$t})`,"gi"),"sqrt($1)"),t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?cube\s+root\s+of\s+(${$t})`,"gi"),"root(3, $1)"),t=t.replace(new RegExp(String.raw`(${$t})\s+to\s+the\s+power\s+of\s+(${$t})`,"gi"),"($1)^($2)"),t=t.replace(new RegExp(String.raw`(${$t})\s+to\s+the\s+(${Object.keys(vt).join("|")}|(\d+|n)(?:st|nd|rd|th))(?:\s+power)?(?![\w.])`,"gi"),(e,t,s,n)=>`(${t})^(${vt[s.toLowerCase()]??n})`),t=t.replace(new RegExp(String.raw`(${$t})\s+to\s+the\s+(${$t})(?![\w.])`,"gi"),"($1)^($2)");for(const[e,s]of kt)t=t.replace(new RegExp(String.raw`(?<![\w.])(?:the\s+)?${xt(e)}\s+of\s+(${$t})`,"gi"),`${s}($1)`);t=t.replace(new RegExp(String.raw`(${$t})\s+squared(?![\w.])`,"gi"),"($1)^2"),t=t.replace(new RegExp(String.raw`(${$t})\s+cubed(?![\w.])`,"gi"),"($1)^3"),t=t.replace(new RegExp(String.raw`(?<![\w.])half\s+of\s+(${$t})`,"gi"),"($1)/2"),t=t.replace(new RegExp(String.raw`(?<![\w.])twice\s+(${$t})`,"gi"),"2*($1)"),t=t.replace(new RegExp(String.raw`(\d+(?:\.\d+)?)\s+percent(?![\w.])`,"gi"),"$1%");const r=e=>new RegExp(String.raw`(?<=[\w.′)\]}]\s+)${xt(e)}(?=\s+[\w.(\[{-])`,"gi");for(const[e,s]of[["divided by","/"],["over","/"],["multiplied by","*"],["times","*"],["plus","+"],["minus","-"],["modulo","mod"]])t=t.replace(r(e),` ${s} `);return t}],["words",_t],["spaced",function(e){e=function(e){if(/\\[io]*int|∫|(?<![A-Za-z])int\(|\\partial|∂|(?<![A-Za-z])d\s*\/\s*d|frac\s*\{\s*(?:\\mathrm\s*\{\s*d\s*\}|\\operatorname\s*\{\s*d\s*\}|d)/.test(e))return e;const t=[];for(const s of e.matchAll(B)){const n=e.slice(0,s.index),r=e.slice(s.index+s[0].length),i="d"!==s[1];if(!i&&(/[A-Za-z0-9\\^_/]$|frac\s*\{$/.test(n)||/^\s*[/}]/.test(r)&&!/\{\s*$/.test(n)))continue;const o=i||/\\[,;:! ]\s*$/.test(n);t.push({m:s,strong:o})}const s=t.length>=2?t:t.filter(e=>e.strong);for(const{m:t}of s.reverse())e=e.slice(0,t.index)+W+t[2]+e.slice(t.index+t[0].length);return e}(e=function(e){return e.replace(/(?:\{\s*\}|(?<![\w}\])′]))_\s*(?:\{\s*([^{}]+?)\s*\}|([A-Za-z0-9]+?))\s*(?:\\mathrm\{([CP])\}|([CP]))\s*_\s*(?:\{\s*([^{}]+?)\s*\}|([A-Za-z0-9]))/g,(e,t,s,n,r,i,o)=>`${"C"===(n??r)?"comb":"perm"}(${t??s},${i??o})`)}(function(e){return e.replace(/(?<![A-Za-z\\])([A-Za-z])\^\s*\{?\s*\(\s*([1-9]\d*)\s*\)\s*\}?(?=\s*\()/g,(e,t,s)=>{if(s.length>3||Number(s)>Y)throw new Error(`A derivative f^{(n)} is read up to n = ${Y}`);return t+"′".repeat(Number(s))})}(function(e){return e.replace(/(?<![A-Za-z\\])(arc)?(sine|cosine|tangent|secant|cosecant|cotangent)(?![A-Za-z])(?=\s*\(|\s+(?:[A-Za-z](?![A-Za-z])|\d|\\))/gi,(e,t,s)=>(t?"arc":"")+K[s.toLowerCase()])}(e=function(e){for(let t=e.lastIndexOf("\\sqrt");t>=0;t=t>0?e.lastIndexOf("\\sqrt",t-1):-1){let s=t+5;if(/[A-Za-z]/.test(e[s]??""))continue;for(;T(e[s]);)s++;const n=e.slice(s);let r=/^(?:§\d+§|-?(?:\d+(?:\.\d+)?|\.\d+)|-?[A-Za-zͰ-Ͽ])/.exec(n)?.[0];if(!r&&/^\\[A-Za-z]/.test(n)&&!n.startsWith("\\left")){let e=/^\\[A-Za-z]+/.exec(n)[0].length;if(/^\d\d/.exec(n.slice(e))&&/frac$/.test(n.slice(0,e)))e+=2;else{const t=/^\s*\[[^\[\]{}]*\]/.exec(n.slice(e));for(t&&(e+=t[0].length);;){let t=e;for(;T(n[t]);)t++;if("{"!==n[t])break;let s=0,r=-1;for(let e=t;e<n.length;e++)if("{"===n[e])s++;else if("}"===n[e]&&0===--s){r=e;break}if(r<0)break;e=r+1}}r=n.slice(0,e)}r&&(e=`${e.slice(0,t)}\\sqrt{${r}}${e.slice(s+r.length)}`)}return e}(function(e){return e=e.replace(re,(e,t,s)=>`(${t}+${ne[s]})`),e=e.replace(ie,(e,t,s,n)=>Number(s)<Number(n)&&3!==s.length?`(${t}+${s}/${n})`:e),e.replace(oe,(e,t,s,n,r,i)=>{const[o,a]=[s??r,n??i];return Number(o)<Number(a)?`(${t}+\\frac{${o}}{${a}})`:e})}(function(e){return e.replace(se,e=>{const t=e[0]in ee;return`${t?"^":"_"}{${[...e].map(e=>(t?ee:te)[e]).join("")}}`})}(e))))))),/\\/.test(e)||(e=U(e)),e=pe(e=function(e){for(let t=0;t<e.length;t++){const s=s=>e.slice(Math.max(0,t-s),t+1);if("t"===e[t]&&"\\text"===s(4)&&"{"===e[t+1]){let s=1,n=t+2,r="";for(;s>0&&n<e.length;)"{"===e[n]?s++:"}"===e[n]?s--:r+=e[n],n++;e=e.slice(0,t-4)+r+e.slice(n)}}return e}(e=(e=(e=function(e){for(let t=0;t<100;t++){const t=[...e.matchAll(/\bif\b/gi)];if(!t.length)return e;const s=t[t.length-1].index,n=ce(e,s+2,"then");if(n<0)return e;const r=e.slice(s+2,n),i=ce(e,n+4,"else"),o=he(e,i<0?n+4:i+4),a=e.slice(n+4,i<0?o:i),l=i<0?"unchanged":e.slice(i+4,o).trim();e=`${e.slice(0,s)}((${r.trim()}) ? (${a.trim()}) : (${l}))${e.slice(o)}`}return e}(e=e.replace(/(?<![.\w=])\.\.\.(?![.\w=])/g,"⋯").replace(/∅/g,"\\{\\}"))).replace(/\(\s*\\?b?mod\s+([^()\s](?:[^()]*[^()\s])?)\s*\)/g," \\pmod{$1}")).replace(/(^|[^A-Za-z\\])lim\s+([^\s_{}]+?)\s*(->|→|\\to)\s*([^\s]+)\s+/g,"$1lim_{$2→$4} ")));for(let t=0;t<e.length-1;t++)if("?"===e[t]&&":"!==e[t+1])e=ue(e,t);else if("_"===e[t]||"^"===e[t]){const s=/^§\d+§/.exec(e.slice(t+1));if(s){e=`${e.slice(0,t+1)}{${s[0]}}${e.slice(t+1+s[0].length)}`;continue}const n=("^"===e[t]||/[A-Za-z}]/.test(e[t-1]??""))&&/^\\[A-Za-z]+(?![A-Za-z{\d])/.exec(e.slice(t+1));if(n){e=`${e.slice(0,t+1)}{${n[0]}}${e.slice(t+1+n[0].length)}`;continue}let r=t+1;for("-"===e[r]&&r++;r<e.length&&".0123456789".includes(e[r]);)r++;if(r===t+2&&"-"===e[t+1]&&(r=t+1),"-"===e[t+1]&&/^\s*[\^!]/.test(e.slice(r,r+8)))continue;r>t+1&&(e=e.slice(0,t+1)+"{"+e.slice(t+1,r)+"}"+e.slice(r))}return e}],["squeeze",function(e){return(e=e.replace(/(?<![\d.])\d{1,3}(?:[ \u00A0\u202F]\d{3})+(?![\d])/g,e=>e.replace(/[ \u00A0\u202F]/g,""))).replace(/\s+/g,(t,s)=>{const n=e[s-1]??"",r=e[s+t.length]??"";return"Δ"===n?"":/[A-Za-z\u0370-\u03FF′]/.test(n)&&/[A-Za-z\u0370-\u03FF]/.test(r)||/[A-Za-z\u0370-\u03FF′]/.test(n)&&/^\\[A-Za-z]/.test(e.slice(s+t.length,s+t.length+2))&&!/\\[A-Za-z]+$/.test(e.slice(0,s))||/[A-Za-z\u0370-\u03FF′]/.test(n)&&/\d/.test(r)&&!/\\[A-Za-z]+$/.test(e.slice(0,s))?" ":/\d/.test(n)&&/\d/.test(r)&&!/\\[A-Za-z]+\s*\d*$/.test(e.slice(0,s))?"*":""})}],["symbols",Mt],["bar",function(e){let t=0;for(let s=e.indexOf(d,t);s>=0;s=e.indexOf(d,t)){let n=0,r=-1;for(let t=s+1;t<e.length;t++)if("([{".includes(e[t]))n++;else if(")]}".includes(e[t]))n--;else if(0===n&&e.startsWith("|_",t)){r=t;break}r<0&&qt(),e=`${e.slice(0,s+1)}(${e.slice(s+1,r)})${e.slice(r)}`,t=s+1}return e}],["compact",function(e){if(e=function(e){for(let t=0;t<e.length;t++){const s=t>0&&we.test(e[t-1]);let n,r=null,i=t;if(e.startsWith("frac{",t)){const s=ve(e,t+4),n=s&&ve(e,s[1]);r=n&&Ae(s[0],n[0]),r&&(i=n[1])}else if("d"===e[t]&&!s){let[s,n]=ke(e,t+1),o="";if(we.test(e[n]??"")&&"/"===e[n+1]&&(o=e[n++]),"/"===e[n]&&"d"===e[n+1]&&we.test(e[n+2]??"")){const t=e[n+2],[,a]=ke(e,n+3);r={n:s,of:o,by:t},i=a}}if(r){if(r.of)n=_e(r.of,r.by,r.n);else{const[t,s]=Ee(e,i);if(!t)continue;n=_e(t,r.by,r.n),i=s}e=e.slice(0,t)+n+e.slice(i),t+=3}}return e}(e=function(e){for(let t=e.indexOf(")′");t>=0;t=e.indexOf(")′",t+1)){let s=0,n=-1;for(let r=t;r>=0;r--)if(")"===e[r])s++;else if("("===e[r]&&0===--s){n=r;break}if(n<0)continue;for(;n>0&&/[A-Za-z\u0370-\u03FF′]/.test(e[n-1]);)n--;let r=0;for(;"′"===e[t+1+r];)r++;e=e.slice(0,n)+_e(e.slice(n,t+1),"x",r)+e.slice(t+1+r),t=n}return e}(e=(e=pe(e)).replace(/(?<![A-Za-z])frac(\d)(\d)/g,"frac{$1}{$2}"))),e.split("\\int").length>101)throw new Error("More than 100 integrals");for(let t=0;t<e.length;t++){const s=e[t],n=s=>e.slice(Math.max(0,t-s),t),r=s=>e.slice(Math.max(0,t-s),t+1);if("{"===s){if("\\begin"===n(6)){const s=me(e.slice(t+1));s&&(e=e.slice(0,t-6)+s)}}else if("["===s)"\\sqrt"===n(5)?e=e.slice(0,t-5)+"sqrt"+de(e.slice(t+1)):"sqrt"===n(4)&&(e=e.slice(0,t-4)+"sqrt"+de(e.slice(t+1)));else if("⌊"===s||"⌈"===s)e=e.slice(0,t)+fe(e.slice(t));else if("t"===s)"\\int"===r(3)&&(e=e.slice(0,t-3)+xe(e.slice(t+1)));else if("m"===s)"\\binom"===r(5)&&(e=e.slice(0,t-5)+"\\binom"+be(e.slice(t+1)));else if("d"===s&&"dd"===r(1)&&"("===e[t+2]){const s=ye(e.slice(t+1));s&&(e=e.slice(0,t-1)+"der"+s)}}return(e=e.replace(/\\(?:text|textrm|textit|mathrm|mathit|mathbf|mathsf|operatorname)\{([A-Za-z])\}/g,"$1")).replace(/\\log/g,"log")}],["bars",function(e){for(let t=e.indexOf(d);t>=0;t=e.indexOf(d)){const s=qt;e.startsWith("(der(",t+1)||s();const n=St(e,t+1,"(",")"),r=St(e,t+5,"(",")");(r<0||n!==r+1)&&s();const i=zt(e.slice(t+6,r));(i.length<2||i.length>3||!e.startsWith("|_",n+1))&&s();const o=n;let a,l;"{"===e[o+3]?(l=St(e,o+3,"{","}"),l<0&&s(),a=e.slice(o+4,l)):(l=o+3,a=e[l]??"");const[c,h,u]=i;a.trim()||s();const p=a.includes("=")?a:`${h}=${a}`;e=`${e.slice(0,t)}der(${c},${p}${u?`,${u}`:""})${e.slice(l+1)}`}return e}],["tidy",e=>e.replace(/ {2,}/g," ").trim()]],qt=()=>{throw new Error("A bar with one limit (\\left. … \\right|_{x = a}) is read only after a derivative, \\left.\\frac{d}{dx} f\\right|_{x=a}; with two, \\left. F \\right|_a^b, it is F at b minus F at a")};function St(e,t,s,n){let r=0;for(let i=t;i<e.length;i++)if(e[i]===s)r++;else if(e[i]===n&&0===--r)return i;return-1}function zt(e){const t=[];let s=0,n=0;for(let r=0;r<e.length;r++)"([{".includes(e[r])?s++:")]}".includes(e[r])?s--:","===e[r]&&0===s&&(t.push(e.slice(n,r)),n=r+1);return t.push(e.slice(n)),t}function Nt(e,t={}){if("string"!=typeof e)throw new Error("Expression must be a string");const{text:s,strings:n}=function(e){const t=new Map;let s="",n=0;for(;n<e.length;){const r=e[n];if('"'!==r&&"'"!==r&&"`"!==r){s+=e[n++];continue}let i=n+1,o="",a=!1;for(;i<e.length&&(a||e[i]!==r);)a=!a&&"\\"===e[i],o+=e[i++];const l=qe(t.size);t.set(l,r+o+r),s+=l,n=i+1}return{text:s,strings:t}}(ze(e));let r=s.replace(/\t/g," ");t.evaluate&&(r=function(e,t,s){const n=/\\[A-Za-z]/.test(e);let r=0;for(;;){let i=e.indexOf("{{",r);if(i<0)return e;let o=!1;const a=n&&Lt.test(e.slice(0,i));if(Zt(e,i)||a&&"{"===e[i+2]?i+=1:o=a,!e.startsWith("{{",i)){r=i;continue}const l=Ct(e,i);if(l<0)return e;let c=e.slice(i+2,l).trim();for(const[e,s]of t)c=c.split(e).join(s);try{let n=s(c);if(/^["'`]/.test(n)){const e=qe(t.size);t.set(e,n),n=e}const a=e.slice(0,i).trimEnd(),h=e.slice(l+2).trimStart();/^[\d.]/.test(n)&&(/[\d.]$/.test(a)||/^[\d.]/.test(h))&&(n=`(${n})`),o&&(n=`{${n}}`),e=e.slice(0,i)+" "+n+" "+e.slice(l+2),r=i+1+n.length}catch{r=l+2}}}(r,n,t.evaluate));const i=[["strings",r]];for(const[e,t]of Ot){if(r=t(r),r.length>1e5)throw new Error("The expression expands too much when read");i.push([e,r])}return{cleaned:r,stringMap:n,steps:i}}const Lt=/(?:\\[A-Za-z]+\*?|[\^_}\]])$/;function Zt(e,t){let s=0;for(;"\\"===e[t-1-s];)s++;return s%2==1}function Ct(e,t){let s=0;for(let n=t+2;n<e.length;n++){if(e.startsWith("}}",n)&&0===s)return n;"{"===e[n]?s++:"}"===e[n]&&s--}return-1}const Rt={alpha:"α",beta:"β",gamma:"γ",delta:"δ",epsilon:"ε",zeta:"ζ",eta:"η",theta:"θ",iota:"ι",kappa:"κ",lambda:"λ",mu:"μ",nu:"ν",xi:"ξ",omicron:"ο",pi:"π",rho:"ρ",sigma:"σ",tau:"τ",upsilon:"υ",phi:"φ",chi:"χ",psi:"ψ",omega:"ω",Alpha:"Α",Beta:"Β",Gamma:"Γ",Delta:"Δ",Epsilon:"Ε",Zeta:"Ζ",Eta:"Η",Theta:"Θ",Iota:"Ι",Kappa:"Κ",Lambda:"Λ",Mu:"Μ",Nu:"Ν",Xi:"Ξ",Omicron:"Ο",Pi:"Π",Rho:"Ρ",Sigma:"Σ",Tau:"Τ",Upsilon:"Υ",Phi:"Φ",Chi:"Χ",Psi:"Ψ",Omega:"Ω"};function It(e){for(;"op"===e.type&&e.args.length;)e=e.args[0];return"op"===e.type?"":e.value}function jt(e,t,s){const n=e=>"symbol"===e.type?e.value===t?s:e:"op"!==e.type?e:Tt.has(e.op)&&function(e,t){if("setbuilder"===e.op){const s=e.args[0];return("symbol"===s.type?s.value:s.args?.[0]?.value)===t}const s=e.args[1];return"symbol"===s?.type?s.value===t:"op"===s?.type&&["=","→"].includes(s.op)&&"symbol"===s.args[0]?.type&&s.args[0].value===t}(e,t)?{...e,id:void 0,args:e.args.map((t,s)=>function(e,t,s,n){if("setbuilder"===e)return 0===s&&"op"===t.type&&"∈"===t.op?{...t,id:void 0,args:[t.args[0],n(t.args[1])]}:t;if(0===s)return t;if(1===s)return"op"!==t.type||"="!==t.op&&"→"!==t.op?t:{...t,id:void 0,args:[t.args[0],...t.args.slice(1).map(n)]};return n(t)}(e.op,t,s,n))}:{...e,id:void 0,args:e.args.map(n)};return n(e)}const Tt=new Set(["der","int","lim","\\sumidx","\\prodidx","evaluated","setbuilder"]);function Ft(e){if("op"!==e.type||"∈"!==e.op&&"∉"!==e.op)return null;const[t,s]=e.args;if("op"!==s.type||"setbuilder"!==s.op)return null;let[n,r]=s.args,i=null;if("op"===n.type&&"∈"===n.op&&"symbol"===n.args[0].type&&([n,i]=n.args),"symbol"!==n.type)return null;const o=jt(r,n.value,t),a=i?{type:"op",op:"&",args:[{type:"op",op:"∈",args:[t,i]},o]}:o;return"∉"===e.op?{type:"op",op:"\\not",args:[a]}:a}function Pt(e){const t=function(e){const t=/^(-?)(\d*)\.(\d+)\.\.$/.exec(e);if(!t)return null;const[,s,n,r]=t;let i=1;for(let e=1;2*e<=r.length;e++)if(r.slice(-e)===r.slice(-2*e,-e)){i=e;break}const o=r.slice(-i);let a=r;for(;a.endsWith(o);)a=a.slice(0,-i);return{sign:s,whole:n||"0",prefix:a,block:o}}(e);if(!t)return e.replace(/\.+$/,"");const{sign:s,whole:n,prefix:r,block:i}=t;return`${s}${n}.${(r+i.repeat(Math.ceil(20/i.length)+1)).slice(0,Math.max(r.length+i.length,17))}`}function Wt(e){const t="string"==typeof e&&/^(-?)(\d*)(?:\.(\d*))?%$/.exec(e);if(!t||!t[2]&&!t[3])return null;const s=(t[2]||"0").padStart(3,"0")+(t[3]??""),n=s.length-(t[3]??"").length-2,r=`${s.slice(0,n).replace(/^0+(?=\d)/,"")}.${s.slice(n)}`.replace(/\.?0+$/,"");return t[1]+(""===r?"0":r)}function Bt(e){if("*"!==e.op)return!1;const[t,s]=e.args;return"number"===t.type&&"-1"===t.value&&"-1"!==It(s)}function Ut(e){return"op"!==e.type||o(e.op)?.group?null:"sum"===e.op?"+":"prod"===e.op?"*":Bt(e)?"neg":e.op}const Dt=new Set(["=","===","≠","≠=","<",">","≤","≥","≈","≡","→"]),Gt=(e,t,s)=>("op"!==t.type||"?:"!==t.op||!Dt.has(e.op))&&c(e.op,Ut(t),s);function Ht(e,t){const s=Ut(t),n=s&&o(s)?.prec;return n?"!"===e?n<=o("!").prec:"\\not"===e?n<o("\\not").prec:n<70||es(t)||"\\not"===s:"\\not"!==e&&es(t)}function Qt(e,t,s={}){const n=s.left?"bracket":Kt(e),r=s.right?"bracket":Jt(t);return"digit"!==r&&"sign"!==r&&"word"!==n&&"word"!==r&&"other"!==n&&("frac"!==r||"digit"!==n&&"frac"!==n)}function Vt(e){return 1===e.length||Boolean(Rt[e])||/^Δ.$/.test(e)||/^d[A-Za-z\u03B1-\u03C9]$/.test(e)||/^.′+$/.test(e)}function Xt(e){return"op"===e.type&&!o(e.op)?.group&&(o(e.op)?.prec??100)<70&&!["sum","prod"].includes(e.op)}function Jt(e){if("number"===e.type)return e.value.startsWith("-")?"sign":"digit";if("op"!==e.type)return"symbol"===e.type&&Vt(e.value)?"name":"word";const t=e.op;return"neg"===t||"pm"===t||Bt(e)?"sign":"/"===t?"frac":o(t)?.group||Xt(e)?"bracket":Yt(e)||["der","int","lim","comb"].includes(t)?"call":["^","!","_"].includes(t)?Xt(e.args[0])?"bracket":Jt(e.args[0]):["*","sum","prod","perm"].includes(t)?Jt(e.args[0]):"other"}function Kt(e){if("number"===e.type)return"digit";if("op"!==e.type)return"symbol"===e.type&&Vt(e.value)?"name":"word";const t=e.op;return"/"===t?"frac":"^"===t||"_"===t?"script":o(t)?.group||Xt(e)||Yt(e)||["der","int","lim","comb","perm"].includes(t)?"bracket":["*","neg","pm","sum","prod"].includes(t)?Kt(e.args[e.args.length-1]):"other"}function Yt(e){if("op"!==e.type)return!1;const t=o(e.op);return"prefix"===t?.fixity&&100===t.prec}function es(e){if("number"===e.type)return e.value.startsWith("-");if("op"!==e.type)return!1;if("neg"===e.op||Bt(e))return!0;const t=o(e.op);return"infix"===t?.fixity&&!Gt(e,e.args[0],"left")&&es(e.args[0])}const ts=e=>"symbol"===e.type&&1===e.value.length&&/[a-zA-Zʹ-Ͽ]/.test(e.value),ss=e=>e.slice(2),ns=new Set(["{}","()","[]","(]","[)","setbuilder","∪","∩"]);function rs(e){if("op"!==e.type||"∈"!==e.op&&"∉"!==e.op||2!==e.args.length)return null;const[t,s]=e.args;return"string"===s.type||e.textSearch?{text:t,within:s}:"string"!==t.type||("op"===s.type?ns.has(s.op):"symbol"===s.type&&/^[ℝℤℕℚℂ∅]$/u.test(s.value))?null:{text:t,within:s}}const is=e=>"string"==typeof e&&e.startsWith("\\:")&&"startswith"===e.slice(2).toLowerCase(),os=e=>(null==e?"":String(e)).replace(/[A-Z]/g,e=>String.fromCharCode(e.charCodeAt(0)+32));function as(e){if(2!==e.args.length)throw new Error('startswith takes a value and the text it starts with: startswith(name, "Mc")');return e.args}function ls(e){const t="op"===e.args[1].type&&"="===e.args[1].op?e.args[1]:null,s=t?t.args[0]:e.args[1],n=t?t.args[1]:null;let r=1,i=e.args[0];for(;"op"===i.type&&"der"===i.op&&cs(i.args[1],s);)r++,i=i.args[0];return{n:r,body:i,by:s,at:n,variable:"symbol"===i.type&&Vt(i.value)}}const cs=(e,t)=>e.type===t.type&&e.value===t.value&&"op"!==e.type,hs=new Set(["<",">","≤","≥","=","===","≠","≠=","&","|","\\not","\\xor","\\nand","\\nor","\\implies","\\iff","∈","∉","≈","≡"]);function us(e){return"op"===e.type&&(!!("="===e.op&&(t=e.args[1],"op"===t.type&&hs.has(t.op)||"symbol"===t.type&&("true"===t.value||"false"===t.value)))||e.args.some(us));var t}const ps=new Set(["<",">","≤","≥","=","≠"]);function fs(e){if("op"!==e.type||!ps.has(e.op)||2!==e.args.length)return null;const[t,s]=e.args;if("op"!==t.type||!ps.has(t.op)||2!==t.args.length)return null;return{type:"op",op:"&",args:[fs(t)??t,{type:"op",op:e.op,args:[t.args[1],s]}]}}for(const e of[Rt])Object.setPrototypeOf(e,null);function ds(e){if(e.length>1e6)throw new Error("The output would be too long");return e}const gs={pmatrix:["(",")"],bmatrix:["[","]"],vmatrix:["|","|"]};function ms(e){if(!e||"op"!==e.type)return null;if(Object.prototype.hasOwnProperty.call(gs,e.op))return e.args.map(e=>"op"===e.type&&"[]"===e.op?e.args:[e]);if("[]"!==e.op||!e.args.length||!e.args.every(e=>"op"===e.type&&"[]"===e.op))return null;const t=e.args[0].args.length;return!t||!e.args.every(e=>e.args.length===t)||e.args.length<2&&t<2?null:e.args.map(e=>e.args)}const $s=Object.keys(g).filter(e=>100===g[e]&&/^[a-z]{2,}$/.test(e)),xs=["frac"],ys=[...Object.keys(Rt),"pi","tau","inf","infinity","exp"],bs="\n cost costs coast cosine cosines cosy sine sines since sincere sinus single\n tank tanks tango stand stank\n point points print prints paint paints pint pints mint hint hints lint tint saint faint joint into\n spin spine pipe pipes pine pines pick picks pink pity pie pies pilot pivot pixel pizza piano\n order orders under wonder border render older elder ladder cider rider wider bidder\n logic logo logs blog clog login slog\n model models mode modes modal modest modern module modules\n limb limbs slim lime limo limit limits climb\n maxi minute minutes mince mind mine mines\n sums sumo summit rounds floors ceils means meant sect sects secs expo kiln\n meta metal metals taxi must mute muse nut nuts\n".trim().split(/\s+/),ws=new Set([...ys,...bs,"simplify","expand","factor","cancel","diff","integrate","solve","approx","limit","true","false","null","undefined","unchanged","deg","mod","pmod","NaN","Infinity"]),vs=/^[a-zA-ZͰ-Ͽ]$/,ks=new Set(["f","g","h","u","v","F","G","H"]),As=e=>$s.includes(e)||xs.includes(e),Es=[...$s,...xs,...ys].sort((e,t)=>t.length-e.length),_s=[...$s,...xs,"pi"].sort((e,t)=>t.length-e.length);function Ms(e,t=Es){const s=[];let n=0;for(;n<e.length;){const r=t.find(t=>e.startsWith(t,n))??e[n];s.push(r),n+=r.length}return s}function Os(e){if(As(e))return!0;const t=Ms(e,_s);return t.length>1&&As(t[t.length-1])&&!ws.has(e)}function qs(e,{mode:t="plain",followed:s=!0}={}){if(e.includes("′")&&/^[a-zA-Z′]+$/.test(e)&&!/^.′+$/.test(e))return e.match(/[a-zA-Z]′*/g);if(!((n=e).length>0&&[...n].every(e=>vs.test(e)))||1===e.length||ws.has(e)||As(e))return[e];var n;if("Δ"===e[0]&&2===e.length)return[e];if("latex"===t)return Ss(Ms(e),s);if(e.length<=2)return[...e];const r=Ss(Ms(e,_s),s),i=r.findIndex(e=>e.length>1);if(i<0)return[e];if("pi"===r[i]&&!r.some(e=>$s.includes(e)))return 3===e.length?r:[e];const o=i;let a=0,l=0;for(const e of r.slice(i))a=1===e.length?a+1:0,l=Math.max(l,a);return o<=1&&l<=2?r:[e]}function Ss(e,t){const s=e[e.length-1];return t||!$s.includes(s)?e:[...e.slice(0,-1),...s]}const zs="§",Ns=/[A-Za-zͰ-Ͽ′]/,Ls=e=>/^[A-Za-zͰ-Ͽ][A-Za-zͰ-Ͽ′]*$/.test(e);function Zs(e,t="plain"){if("string"!=typeof e)throw new Error("Tokenizer input must be a string");const s=[];let n="";const i=e=>{n&&(Ls(n)?s.push(...function(e,t,s){if((e=>Se.test(e))(e))return[e];if(x.includes(t))return function(e,t){if(/^[a-zA-Z]′+$/.test(e))return[r+e];if("if"===e.toLowerCase())return["\\:if"];if(1===e.length)return ks.has(e)?[r+e]:[e];if(m(e))return[e];const s=qs(e,{mode:t,followed:!0});return s.length>1?s:e.length<=2?[...e]:[r+e]}(e,s);const n=void 0!==t&&!/[+\-*/=<>),\]}|!≤≥≠]/.test(t);return qs(e,{mode:s,followed:n})}(n,e,t)):s.push(n),n="")};for(let t=0;t<e.length;t++){const o=e[t];if(o===zs){i(o);const n=e.indexOf(zs,t+1);if(n<0)throw new Error(`An unmatched ${zs}`);const a=e.slice(t,n+1);s.push(x.includes(e[n+1])?r+a:a),t=n;continue}if("ⅆ"===o&&/[A-Za-z\u0370-\u03FF]/.test(e[t+1]??""))n&&i(o),n="",s.push("d"+e[t+1]),t+=1;else{if(Ls(n)&&/[0-9]/.test(o)&&m(n+o)){const{op:r,end:i}=Cs(e,t,n+o);s.push(r),n="",t=i;continue}if(Ns.test(o)&&"′"!==o&&b(n)&&(s.push(n),n=""),Ns.test(o)&&(""===n?"′"!==o:Ls(n)))n+=o;else if(/[0-9]/.test(o)&&Ls(n)&&!Os(n))n+=o;else if(Ls(n)&&i(" "===o?e[t+1]:o),"(){}[]".includes(o))n&&s.push(...Rs(n,o)),n="",s.push(o);else if("_"!==o&&" "!==o){if(m(n+o)){const{op:r,end:i}=Cs(e,t,n+o);s.push(r),n="",t=i;continue}if(m(o)){i(o);const{op:n,end:r}=Cs(e,t,o);s.push(n),t=r;continue}n+=o}else i(e[t+1]),"_"===o&&s.push("_")}}if(i(void 0),s.some(e=>/^:/.test(e)))throw new Error("Unmatched ':'");return s}function Cs(e,t,s){let n=t;for(;n+1<e.length&&m(s+e[n+1]);)s+=e[n+1],n++;return"!"===s&&"_"===e[n+1]&&n++,{op:s,end:n}}function Rs(e,t){if(!x.includes(t))return[e];if("\\"===e[0]){const t=e.slice(1);return[m(t)?t:r+t]}return b(e)?[e,"*"]:m(e)||As(e)||b(e[0])?[e]:[r+e]}const Is=70,js=100,Ts="%mod",Fs=/^d[A-Za-z\u0370-\u03FF]$/,Ps=e=>x.includes(e),Ws=e=>y.includes(e),Bs=e=>i(e)||g[e]===js&&"_"!==e,Us=new Set(["\\comb","\\perm","comb","perm"]),Ds={frac:"/","\\binom":"\\binom"},Gs=new Set(["<",">","≤","≥","=","==","===","≠","≠=","!=","!==","&","|","\\not","\\xor","\\implies","\\iff","∈","∉","true","false"]),Hs={"\\comb":"comb","\\perm":"perm",deg:"°","π":"pi","∨":"|"};class Qs{constructor(e){this.tokens=e,this.pos=0,this.out=[],this.absDepth=0,this.frames=[]}peek(e=0){return this.tokens[this.pos+e]}next(){return this.tokens[this.pos++]}emit(...e){this.out.push(...e)}parseAll(){if(!this.tokens.length)throw new Error("Empty expression");this.parseExpression(0);const e=this.peek();if(void 0!==e)throw new Error(Ws(e)?`Unmatched '${e}'`:`Unexpected '${e}'`);return this.out}parseExpression(e,t=!1){this.parseOperand(),this.absorb(e,t)}absorb(e,t=!1){for(;;){const s=this.peek();if(void 0===s||Ws(s))return;const n=this.operatorAt(s,t);if(!n||n.lbp<=e)return;n.run()}}operatorAt(e,t){if("|"===e)return this.pipe();if(","===e)return{lbp:5,run:()=>{this.next(),this.frames.length&&(this.frames[this.frames.length-1].comma=!0),this.parseExpression(5),this.emit(",")}};if("?"===e)return{lbp:9,run:()=>this.ternary()};if("mod"===e)return{lbp:g.mod,run:()=>this.modulo()};if(e===Ts)return{lbp:Is,run:()=>{this.next(),this.parseExpression(Is),this.emit("mod")}};if("pmod"===e)return{lbp:g.pmod,run:()=>{this.next(),this.parseScript(),this.emit("pmod")}};if("?:"===e)return{lbp:10,run:()=>{throw new Error("Unmatched ':'")}};if("!"===e)return{lbp:90,run:()=>{this.next(),this.emit("!")}};if("_"===e)return{lbp:js,run:()=>{this.next(),this.parseScript(),this.emit("_")}};if(Us.has(e))return{lbp:js,run:()=>{this.next(),this.parseExpression(js),this.emit(e)}};const s=g[e];if("="===e&&void 0!==s)return{lbp:s,run:()=>this.equals(s)};if(void 0!==s&&!Bs(e)&&"\\not"!==e&&!(e in Ds)){const t=$.has(e)?s-1:s;return{lbp:s,run:()=>{this.next(),this.parseExpression(t),this.emit(e)}}}return this.startsOperand(e)?this.implicit(e,t):null}implicit(e,t){return{lbp:!t||Bs(e)||Fs.test(e)?Is:71,run:()=>{this.parseExpression(Is),this.emit("*")}}}startsOperand(e){return void 0===e||Ws(e)||"|"===e?"|"===e:e!==Ts&&(!!(Ps(e)||Bs(e)||e in Ds)||!m(e))}pipe(){if(this.absDepth>0)return null;let e=0;for(let t=this.pos;t<this.tokens.length;t++)"|"===this.tokens[t]&&e++;const t="|"===this.peek(1);return e%2==0&&!t&&this.startsOperand(this.peek(1))?this.implicit("|",!1):{lbp:g["|"],run:()=>{this.next(),t&&e%2==0&&this.next(),this.parseExpression(g["|"]),this.emit("|")}}}modulo(){this.next(),this.parseExpression(Is,!0),this.emit("mod")}equals(e){this.next(),this.parseExpression(e),Gs.has(this.out[this.out.length-1])&&this.questionAhead()&&this.absorb(8),this.emit("=")}questionAhead(){let e=0;for(let t=this.pos;t<this.tokens.length;t++){const s=this.tokens[t];if(Ps(s))e++;else if(Ws(s)){if(0===e--)return!1}else{if(0===e&&","===s)return!1;if(0===e&&"?"===s)return!0}}return!1}ternary(){if(this.next(),this.parseExpression(10),"?:"!==this.next())throw new Error("Unmatched '?'");this.parseExpression(8),this.emit("?:")}parseOperand(){const e=this.next(),t=this.tokens[this.pos-2],s=this.peek();if(void 0===e)throw new Error("An operator is missing an operand at the end");if(Ps(e))return this.bracket(e,"{"===e&&("∈"===t||"∉"===t));if("|"===e)return this.absolute();if("-"===e||"+"===e){return"^"===t&&(void 0===s||Ws(s))||Ps(t)&&Ws(s)?this.emit("\\"+e):"+"===e?this.parseExpression(75):this.negate()}if("±"===e)return this.parseExpression(75),this.emit("pm");if("!"===e||"\\not"===e)return this.parseExpression(85),this.emit("\\not");if(e in Ds)return this.parseScript(),this.parseScript(),this.emit(Ds[e]);if("log"===e)return this.logarithm();if("lim"===e&&"_"===s)return this.limit();if(("\\sumidx"===e||"\\prodidx"===e)&&"_"===s)return this.bigOperator(e);if(Bs(e))return this.functionCall(e);if(e===Ts)throw new Error("'%' is missing an operand");if(Ws(e)||m(e))throw new Error(`'${e}' is missing an operand`);this.emit(e)}negate(){const e=this.out.length;this.parseExpression(75);const t=this.out.slice(e);1!==t.length||!b(t[0])&&"∞"!==t[0]?this.emit("neg"):this.out[e]="-"+t[0]}bracket(e,t=!1){if(Ws(this.peek()))return void this.emit("∅",e+this.next());this.frames.push({comma:!1}),this.parseExpression(0);const{comma:s}=this.frames.pop(),n=this.peek();Ws(n)&&(this.next(),s?this.emit(e+n):t&&this.emit("{}"))}absolute(){this.absDepth++,this.parseExpression(0),this.absDepth--,"|"===this.peek()&&this.next(),this.emit("abs")}parseScript(){const e=this.peek();if(Ps(e))return this.parseOperand();if("-"===e||"+"===e)return this.parseOperand();if(Bs(e))return this.parseOperand();if(this.next(),void 0===e||Ws(e)||m(e))throw new Error("Missing a sub- or superscript");this.emit(e)}logarithm(){"_"===this.peek()?(this.next(),this.parseScript()):this.emit("e"),this.argument(),this.emit("log")}limit(){this.next();const e=this.out.length;this.parseScript();const t=this.out.splice(e);this.parseExpression(g["+"],!0),this.emit(...t,",","()","lim")}bigOperator(e){this.next();const t=this.out.length;this.parseScript();let s=this.out.splice(t);"^"===this.peek()&&(this.next(),this.parseScript(),s=[...s,...this.out.splice(t),","]),this.parseExpression(g["+"],!0),this.emit(...s,",","()",e)}functionCall(e){if(void 0===this.peek())return this.emit(e);let t=null;if("^"===this.peek()){this.next();const e=this.out.length;this.parseScript(),t=this.out.splice(e)}if(0===o(e)?.arity&&!Ps(this.peek()))return this.emit(e);this.argument(),this.emit(e),t&&this.emit(...t,"^")}argument(){const e=this.peek();if(Ps(e))return this.next(),this.bracket(e);this.parseExpression(Is,!0)}}function Vs(e,t){e=function(e){const t=[];for(let s=0;s<e.length;s++){const n=e[s],i=e=>"string"==typeof e&&/^[a-zA-Z]$/.test(e);if(ks.has(n)&&"^ { - 1 } ("===e.slice(s+1,s+7).join(" ")){t.push(r+n+"⁻¹"),s+=5;continue}const[o,a,l]=e.slice(s+1,s+4),c=i(a)?a!==n:b(a);i(n)&&"e"!==n&&"("===o&&c&&")"===l?t.push(r+n):t.push(n)}return t}(e);const s=[];for(const n of e)m(n)||Ps(n)||Ws(n)||"_"===n||i(n)||b(n)?s.push(n):s.push(...Xs(n,t));return s}function Xs(e,t){if("%"===e)return[Ts];if(!e.includes("%"))return Js(e,t);const s=e.split("%");s.length>1&&""===s[s.length-1]&&(s.pop(),s[s.length-1]+="%");const n=[];return s.forEach((e,s)=>{s>0&&n.push(Ts),""!==e&&n.push(...Js(e,t))}),n}function Js(e,t){let s=0;for(;s<e.length&&b(e.slice(0,s+1));)s++;if(0===s||s===e.length)return[e];const n=e.slice(s);return"%"===n?[e]:[e.slice(0,s),...(r=n,Vs(Zs(r,t),t))];var r}for(const e of[Ds,Hs])Object.setPrototypeOf(e,null);const Ks=new Set(["()","[]","{}","(]","[)"]),Ys={pow:"^",sqrtpow:"root","\\binom":"comb","\\comb":"comb","\\perm":"perm",frac:"/","==":"=","!==":"≠=",...Object.fromEntries(Object.keys(f).map(e=>[e,"*"]))},en=new Set(["round","fixed","der"]),tn=e=>i(e)||100===g[e];function sn(t){const s=[],n=e=>{if(!s.length)throw new Error(`'${e}' is missing an operand`);return s.pop()},r=e=>e.list?e.list:[e],i=(e,t)=>{if(e.list)throw new Error(`A list of values needs brackets around it (near '${t}')`);return e};if(t.forEach((a,l)=>{if("∅"===a)return void s.push({list:[]});if(","===a){const e=n(a),t=n(a);return void s.push({list:[...r(t),...r(e)]})}if(Ks.has(a)){const e=r(n(a));if(!e.length&&"{}"!==a&&!tn(t[l+1]))throw new Error("Empty brackets");return void s.push(E(a,e))}const c=Ys[a]??a,h=o(c);if(!h){if("?"===a||"abs("===a)throw new Error(`Unmatched '${a[0]}'`);return void s.push(M(a))}let u;if(tn(a)&&Ks.has(t[l-1])&&s[s.length-1]?.op===t[l-1])u=s.pop().args;else{let t=h.arity===e||en.has(c)?1:h.arity;if(s.length<t)throw new Error(`'${a}' needs ${t} operand${1===t?"":"s"}`);u=s.splice(s.length-t,t).map(e=>i(e,a))}const p=function(e,t){switch(e){case"sqrt":if(2===t.length)return E("root",t);break;case"round":if(1===t.length)return E("round",[t[0],k(0)]);break;case"sum":case"prod":{const s=t[1];if("op"===s?.type&&"symbol"===s.args[0]?.type&&(3===t.length&&"="===s.op||2===t.length&&"∈"===s.op))return E("sum"===e?"\\sumidx":"\\prodidx",t);break}case"evaluated":if(3===t.length)return E("evaluated",[t[0],A(L(t[0])||"x"),t[1],t[2]]);break;case"det":if(t.length>1&&t.every(e=>"op"===e.type&&"[]"===e.op))return E("det",[E("[]",t)]);break;case"fixed":if(1===t.length)return E("fixed",[t[0],k(2)]);break;case"der":if(1===t.length)return E("der",[t[0],A("x")]);if(3===t.length)return function([e,t,s]){const n="number"===s.type?Number(s.value):NaN;if(!Number.isInteger(n)||n<1||n>20)throw new Error("der(f, x, n) needs n to be a whole number from 1 to 20");const r="op"===t.type&&"="===t.op?t:null,i=r?r.args[0]:t;let o=e;for(let e=0;e<n;e++)o=E("der",[o,r&&e===n-1?r:i]);return o}(t);break;case"log":if(2===t.length&&"symbol"===t[0].type&&"e"===t[0].value)return E("ln",[t[1]]);break;case"lim":return rn(e,t),E("lim",function(e){const t=e=>{if("op"!==e.type)return null;if("^"===e.op&&"symbol"===e.args[1].type&&nn[e.args[1].value])return{point:e.args[0],side:nn[e.args[1].value]};const s=e.args.length?t(e.args[e.args.length-1]):null;return s&&{point:{...e,args:[...e.args.slice(0,-1),s.point]},side:s.side}},[s,n,r]=e;if(2===e.length&&"op"===n.type&&"→"===n.op){const[e,r]=n.args,i=t(r);if(i)return[s,e,i.point,A(i.side)]}const i=3===e.length&&t(r);return i?[s,n,i.point,A(i.side)]:e}(t));case"\\:if":if(2===t.length||3===t.length)return E("?:",[t[0],t[1],t[2]??A("unchanged")]);throw new Error("if() takes a condition, a value, and optionally an alternative")}return rn(e,t),E(e,t)}(c,u);f[a]&&"*"===p.op&&(p.written=f[a]),s.push(p)}),1!==s.length)throw new Error(s.length?"Some values are not connected by an operator":"Empty expression");const a=["=","<",">","≤","≥","≠","≈","≡","∈","∉"],l=s[0];return l.list&&l.list.length>1&&l.list.every(e=>"op"===e.type&&a.includes(e.op))?l.list.reduce((e,t)=>E("&",[e,t])):i(s[0],"end")}const nn={"\\+":"r","\\-":"l"};function rn(t,s){const{arity:n}=o(t);if(n!==e){if(s.length!==n){throw new Error(`'${t}' takes ${n} argument${1===n?"":"s"}, not ${s.length}`)}}else if(0===s.length&&!i(t))throw new Error(`'${t}' needs at least one argument`)}for(const e of[Ys,nn])Object.setPrototypeOf(e,null);const on=new RegExp(`[${Object.keys(f).join("")}]`,"g"),an=e=>e.replace(on,"*"),ln=/\\(?:text|textrm|textit|mathrm|mathit|operatorname)\{\s*([A-Za-z][A-Za-z0-9 ]*[A-Za-z0-9])\s*\}/g;const cn=e=>/^[a-z][a-z0-9]*$/.test(e)&&m(e);function hn(e){return e.includes("\\")?(e=function(e){for(let t=0;t<50;t++){const t=e.lastIndexOf("\\begin{cases}"),s=t<0?-1:e.indexOf("\\end{cases}",t);if(s<0)return e;const n=le(e.slice(t+13,s),"\\\\").map(e=>le(e,"&")).filter(e=>e.join("").trim());let r=null;const i=/=|<|>|\\(?:le|ge|leq|geq|neq|ne|lt|gt)(?![A-Za-z])/;if(n.length>1&&n.every(e=>1===e.length&&i.test(e[0])))r=n.map(e=>`(${e[0].trim().replace(/\s*[,;.]$/,"")})`).join(" \\land "),e=e.slice(0,t)+`(${r})`+e.slice(s+11);else{for(let e=n.length-1;e>=0;e--){const t=n[e][0].trim().replace(/\s*[,;]$/,""),s=ae(n[e].slice(1).join("&"));r=null!==r||s?`((${s||"true"}) ? (${t}) : ${r??"(unchanged)"})`:`(${t})`}e=e.slice(0,t)+(r??"")+e.slice(s+11)}}return e}(e),(e=function(e){return e=J(e),e=U(e),e=e.replace(/\\(?:[Bb]igg?)[lrm]?(?![A-Za-z])\s*/g,""),e=e.replace(/\^\s*(?:\\circ\b|\{\s*\\circ\s*\})|\\degree\b|\\textdegree\b/g,"°"),e=e.replace(/\\(?:cdots|ldots|dotsb|dotsc|dots)(?![A-Za-z])/g,"⋯"),e=e.replace(/(\d*)\.(\d*)\\overline\s*\{\s*(\d+)\s*\}/g,(e,t,s,n)=>`${t||"0"}.${s}${n}${n}..`),e=e.replace(/\\(?:emptyset|varnothing)(?![A-Za-z])/g,"\\{\\}"),e=e.replace(/\(\s*([A-Za-z])\s*\\circ\s*([A-Za-z])\s*\)\s*\(([^()]*)\)/g,"$1($2($3))"),e}(e)).replace(/\{\}\^\{([^{}]*)\}\s*\\mathrm\{P\}\s*_\{([^{}]*)\}/g,"\\perm($1,$2)")):/[\]|]\s*_/.test(e)?J(e):e}function un(e){pn=e}let pn=null;function fn(e,t={}){const s={original:e};try{if("string"!=typeof e)throw new Error("Expression must be a string");const n=t.maxLength??5e3;if(e.length>n)throw new Error(`Longer than ${n} characters (options.maxLength)`);if(e.includes(""))throw new Error("The character U+E000 can't be read: MathZetta uses it to hold § in strings");if((e=function(e,t,s){if(!e.includes(t))return e;const n=ze(e);let r="";for(let i=0;i<n.length;i++){const o=n[i];if('"'===o||"`"===o||"'"===o){const o=Ne(n,i);r+=e.slice(i,o+1).replaceAll(t,s),i=o}else r+=e[i]}return r}(e,zs,"")).includes(zs))throw new Error(`The character ${zs} can't be read: MathZetta uses it to mark names`);const r=/^\s*=(?!=)/.test(e);r&&(e=e.replace(/("(?:[^"]|"")*")|\$(?=[A-Za-z]{0,3}\d|[A-Za-z]{1,3}\$?\d)/g,(e,t)=>t??""));const i=r?[...new Set(e.replace(/"(?:[^"]|"")*"/g,"").match(/(?<![\w.])[A-Za-z]{1,3}\d+(?::[A-Za-z]{1,3}\d+)?(?![\w.(])/g)??[])]:[],{text:o,protectedNames:a}=function(e,t=[],s=[]){const n=new Map,r=new Map,i=e=>{let t=r.get(e);return void 0===t&&(t=`${zs}${n.size}${zs}`,n.set(t,e),r.set(e,t)),t};e=e.replace(ln,(e,t)=>cn(t.trim())?` ${t.trim()}`:i(t.trim()));for(const s of[...t].sort((e,t)=>t.length-e.length)){const t=s.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");e=e.replace(new RegExp(`(?<![A-Za-z_])${t}(?![A-Za-z0-9_])`,"g"),()=>i(s))}for(const t of[...s].sort((e,t)=>t.length-e.length)){const s=t.replace(/[.*+?^${}()|[\]\\]/g,"\\$&");e=e.replace(new RegExp(`(?<![A-Za-z_])${s}(?![A-Za-z0-9_])(\\s*)(\\(?)`,"g"),(e,s,n)=>i(t)+(n?" * (":s))}return{text:e,protectedNames:n}}(hn(e),t.names,[...t.values??[],...i]),l=(e=>/\\[A-Za-z]/.test(e)?"latex":"plain")(o),{cleaned:c,stringMap:h,steps:u}=function(e,t={}){const s=void 0===t.blocks?pn:t.blocks;return Nt(e,{evaluate:s?e=>s(e,t.scope):void 0})}(o,{blocks:t.blocks,scope:t.scope}),p=e=>Ze(Ze(e,h),a).map(e=>"string"==typeof e?e.replaceAll("",zs):e);s.mode=l,s.cleaned=an(p([c])[0]),s.cleanSteps=u.map(([e,t])=>[e,an(t)]);const f=Zs(c,l);s.tokens=f.map(an);const d=p(function(e,t="plain"){return new Qs(Vs(e,t)).parseAll().map(e=>Hs[e]??e)}(f,l));s.rawPostfix=d.map(an);const g={},m=S(sn(d),g);s.postfix=m,s.written=g,s.tree=q(m),Object.keys(g).length&&N(s.tree,e=>{g[e.id]&&(e.written=g[e.id])})}catch(e){s.error=e instanceof RangeError?"Nested too deeply":e.message}return s}const dn=e=>"string"==typeof e&&e.length>80?`${e.slice(0,77)}…`:e;function gn(e,t={}){const s=fn(e,t);if(t.keepIntermediateSteps)return s;if(!s.error)return s.postfix;if(t.allowUnvalidated&&s.rawPostfix)return s.rawPostfix;throw new Error(`Failed to parse expression "${dn(e)}": ${s.error}`)}function mn(e,t={}){const s=fn(e,t);if(s.error)throw new Error(`Failed to parse expression "${dn(e)}": ${s.error}`);return s.tree}const $n=(e,t)=>"string"==typeof e?mn(e,t):z(e),xn=e=>Array.isArray(e)&&0===e.length,yn=e=>"op"===e.type&&i(e.op)&&["vec","hat"].includes(ss(e.op)),bn={plus:"plus",minus:"minus",times:"times",over:"over",negative:"negative",plusOrMinus:"plus or minus",quantity:"the quantity",of:"of",and:"and",or:"or",not:"not",xor:"exclusive or",nand:"nand",nor:"nor",implies:"implies",iff:"if and only if",squared:"squared",cubed:"cubed",toThe:"to the",toThePowerOf:"to the power of",power:"power",sub:"sub",factorial:"factorial",prime:"prime",doublePrime:"double prime",triplePrime:"triple prime",inverse:"inverse",mod:"mod",percent:"percent",degrees:"degrees",dot:"dot",cross:"cross",vector:"vector",hat:"hat",at:"at",equals:"equals",notEqual:"is not equal to",lessThan:"is less than",greaterThan:"is greater than",lessOrEqual:"is less than or equal to",greaterOrEqual:"is greater than or equal to",approx:"is approximately",congruent:"is congruent to",approaches:"approaches",tendsTo:"approaches",isIn:"is in",isNotIn:"is not in",union:"union",intersect:"intersect",squareRoot:"the square root of",cubeRoot:"the cube root of",nthRoot:e=>`the ${e} root of`,absoluteValue:"the absolute value of",floor:"the floor of",ceiling:"the ceiling of",logBase:"log base",naturalLog:"the natural log of",choose:"choose",permute:"permute",derivative:"the derivative",secondDerivative:"the second derivative",nthDerivative:e=>`the ${e} derivative`,withRespectTo:"with respect to",d:"d",integralOf:"the integral of",integralFrom:"the integral from",to:"to",evaluatedFrom:"evaluated from",setOf:"the set of",suchThat:"such that",limitAs:"the limit as",fromTheRight:"from the right",fromTheLeft:"from the left",sumFrom:"the sum from",sumOver:"the sum over",sumOf:"the sum of",productFrom:"the product from",productOver:"the product over",productOf:"the product of",matrix:(e,t)=>`the ${e} by ${t} matrix`,row:e=>`row ${e}`,determinantOf:"the determinant of",theSet:"the set",theList:"the list",openInterval:"the open interval from",closedInterval:"the closed interval from",halfOpenInterval:"the half-open interval from",comma:",",if:"if",then:"then",otherwise:"otherwise",infinity:"infinity",pi:"pi",e:"e",delta:"delta",randomNumber:"a random number",rounded:"rounded to",decimalPlaces:"decimal places",nearestWholeNumber:"the nearest whole number",writtenTo:"written to",asWholeNumber:"written as a whole number",reals:"the real numbers",integers:"the integers",naturals:"the natural numbers",rationals:"the rational numbers",complexes:"the complex numbers",repeating:"repeating",andSoOn:"dot dot dot",emptySet:"the empty set",eToThe:"e to the",endQuantity:"end quantity",endFraction:"end fraction",endRoot:"end root",endExponent:"end exponent",endSub:"end sub",endAbsoluteValue:"end absolute value",endFloor:"end floor",endCeiling:"end ceiling",endLimit:"end limit",endSum:"end sum",endMatrix:"end matrix",endProduct:"end product",endDerivative:"end derivative",half:"half",halves:"halves",ordinals:["","","half","third","quarter","fifth","sixth","seventh","eighth","ninth","tenth"],numerators:["","one","two","three","four","five","six","seven","eight","nine"],powers:["","first","squared","cubed","fourth","fifth","sixth","seventh","eighth","ninth","tenth"]},wn={sin:"sine",cos:"cosine",tan:"tangent",csc:"cosecant",sec:"secant",cot:"cotangent",sinh:"hyperbolic sine",cosh:"hyperbolic cosine",tanh:"hyperbolic tangent",arcsin:"inverse sine",arccos:"inverse cosine",arctan:"inverse tangent",arcsec:"inverse secant",arccsc:"inverse cosecant",arccot:"inverse cotangent",arcsinh:"inverse hyperbolic sine",arccosh:"inverse hyperbolic cosine",arctanh:"inverse hyperbolic tangent",ln:"the natural log",log:"log",loglog:"the log of the log",logloglog:"the log of the log of the log",expm1:"e to the power, minus 1, of",max:"the maximum",min:"the minimum",gcd:"the greatest common divisor",mean:"the mean",det:"the determinant",atan2:"the arctangent",round:"round",fixed:"fixed",random:"random"},vn=Object.fromEntries(Object.entries(Rt).map(([e,t])=>[t,e]));function kn(e,t={}){return En(function(e,t={}){return An(e,t,!0)}(e,t))}function An(e,t,s){return function(e){let t=e.length;for(;t>0&&(e[t-1].close||!e[t-1].text.replace(/,/g,"").trim());)t--;const s=[];for(const{text:n,id:r}of e.slice(0,t)){const e=n.trim();if(!e)continue;const t=e.startsWith(","),i=t?e.slice(1).trim():e;t&&s.length&&!s[s.length-1].text.endsWith(",")&&(s[s.length-1].text+=","),i&&s.push({text:i,id:r})}s.length&&(s[s.length-1].text=s[s.length-1].text.replace(/,$/,""));return s}(new On({...bn,...t.words},{...wn,...t.names},t.say||{},s).speak(e))}function En(e){return e.map(e=>e.text).join(" ").replace(/\s+/g," ").replace(/ ,/g,",").replace(/,(\s*,)+/g,",").replace(/^,\s*/,"").replace(/,\s*$/,"").trim()}const _n={"=":"equals","===":"equals","≠":"notEqual","≠=":"notEqual","<":"lessThan",">":"greaterThan","≤":"lessOrEqual","≥":"greaterOrEqual","≈":"approx","≡":"congruent","→":"tendsTo","∈":"isIn","∉":"isNotIn","∪":"union","∩":"intersect","±":"plusOrMinus","&":"and","|":"or","\\xor":"xor","\\nand":"nand","\\nor":"nor","\\implies":"implies","\\iff":"iff"},Mn={"ℝ":"reals","ℤ":"integers","ℕ":"naturals","ℚ":"rationals","ℂ":"complexes"};class On{constructor(e,t,s,n=!1){this.w=e,this.names=t,this.say=s,this.audio=n}speak(e){const t=this.say["op"===e.type?e.op:e.type];if(t){return[{text:t("op"===e.type?e.args.map(e=>En(this.speak(e))):[e.value],e,e=>En(this.speak(e))),id:e.id}]}return"number"===e.type?[this.seg(e,this.number(e.value))]:"string"===e.type?[this.seg(e,e.value.slice(1,-1))]:"symbol"===e.type?[this.seg(e,this.symbol(e.value))]:this.operation(e)}seg(e,t){return{text:t,id:e.id}}number(e){const t=this.w;return e.endsWith("..")?`${e.replace(/\.+$/,"")} ${t.repeating}`:e.startsWith("-")?`${t.negative} ${e.slice(1)}`:e}symbol(e){const t=this.w;if("∞"===e)return t.infinity;if("-∞"===e)return`${t.negative} ${t.infinity}`;if("pi"===e||"π"===e)return t.pi;if("tau"===e||"τ"===e)return"tau";if("\\+"===e)return t.plus;if("\\-"===e)return t.minus;if("°"===e)return t.degrees;if("⋯"===e)return t.andSoOn;if(Mn[e])return t[Mn[e]];if(vn[e])return vn[e].toLowerCase();if(/^Δ.+$/.test(e))return`${t.delta} ${this.symbol(e.slice(1))}`;if(/^d[A-Za-z\u03B1-\u03C9]$/.test(e))return`d ${this.symbol(e[1])}`;const s=/′+$/.exec(e);return s?`${this.symbol(e.slice(0,-s[0].length))} ${this.primes(s[0].length)}`:e.endsWith("⁻¹")?`${this.symbol(e.slice(0,-2))} ${t.inverse}`:e}primes(e){const t=this.w;return[t.prime,t.doublePrime,t.triplePrime][e-1]??`${e} ${t.prime}`}isCompound(e){if("op"!==e.type)return!1;if("sum"===e.op||"?:"===e.op)return!0;const t=l(e.op);return void 0!==t&&t<=50&&!o(e.op).group}quantity(e,t="endQuantity"){return this.isCompound(e)?[this.seg(e,this.w.quantity),...this.speak(e),...this.closer(e,t)]:this.speak(e)}grouped(e,t,s){return this.isCompound(e)||this.audio&&s?[this.seg(e,this.w.quantity),...this.speak(e),...this.closer(e,t)]:this.speak(e)}closer(e,t){const s=this.seg(e,this.w.comma);return this.audio&&t?[s,{text:this.w[t],id:e.id,close:!0},s]:[s]}contents(e,t){return this.audio&&this.isCompound(e)?this.quantity(e,t):[...this.speak(e),...this.pauseAfter(e)]}argument(e){return this.quantity(e)}operation(e){const{op:t,args:s}=e,[n,r]=s,o=this.w,a=t=>this.seg(e,t),l=()=>s.flatMap((e,t)=>t?[a(o.comma),...this.listed(e)]:this.listed(e));if(_n[t])return[...this.speak(n),a(o[_n[t]]),...this.speak(r)];if(i(t)){if(1===s.length&&"symbol"===n.type&&"vec"===ss(t))return[a(o.vector),...this.speak(n)];if(1===s.length&&"symbol"===n.type&&"hat"===ss(t))return[...this.speak(n),a(o.hat)];const e=this.symbol(ss(t));return s.length?[a(`${e} ${o.of}`),...this.arguments(s)]:[a(e)]}if(this.names[t]&&!["log","round","fixed","atan2","random","expm1"].includes(t))return[a(`${this.names[t]} ${o.of}`),...this.arguments(s)];switch(t){case"+":return Bt(r)?[...this.leftSide(n),a(o.minus),...this.quantityIfSum(r.args[1])]:[...this.leftSide(n),a(o.plus),...this.speak(r)];case"-":return Bt(r)?[...this.leftSide(n),a(o.plus),...this.speak(r.args[1])]:[...this.leftSide(n),a(o.minus),...this.quantityIfSum(r)];case"*":return this.product(e);case"/":return this.fraction(e);case"^":return this.power(e);case"_":return[...this.speak(n),a(o.sub),...this.audio?this.grouped(r,"endSub","op"===r.type):this.speak(r)];case"neg":return this.isCompound(n)||"op"===n.type&&"neg"===n.op?[a(`${o.negative} ${o.quantity}`),...this.speak(n),...this.closer(n,"endQuantity")]:[a(o.negative),...this.speak(n)];case"pm":return[a(o.plusOrMinus),...this.speak(n)];case"!":return[...this.grouped(n,"endQuantity","op"===n.type||"number"===n.type&&n.value.startsWith("-")),a(o.factorial)];case"\\not":return[a(o.not),...this.quantity(n)];case"sqrt":return[a(o.squareRoot),...this.contents(n,"endRoot")];case"root":{const e="number"===n.type?Number(n.value):NaN,t=3===e?o.cubeRoot:2===e?o.squareRoot:o.nthRoot(e?this.ordinal(e):En(this.speak(n))+"th");return[a(t),...this.contents(r,"endRoot")]}case"abs":return[a(o.absoluteValue),...this.contents(n,"endAbsoluteValue")];case"floor":return[a(o.floor),...this.contents(n,"endFloor")];case"ceil":return[a(o.ceiling),...this.contents(n,"endCeiling")];case"log":return"symbol"===n.type&&"e"===n.value?[a(`${this.names.ln} ${o.of}`),...this.argument(r)]:[a(`${o.logBase}`),...this.speak(n),a(o.of),...this.argument(r)];case"mod":return[...this.quantity(n),a(o.mod),...this.quantity(r)];case"pmod":return[...this.speak(n),a(`${o.comma} ${o.mod}`),...this.speak(r)];case"perm":return[...this.quantity(n),a(o.permute),...this.quantity(r)];case"comb":return[...this.quantity(n),a(o.choose),...this.quantity(r)];case"round":{const e=0===("number"===r.type?Number(r.value):NaN)?o.nearestWholeNumber:`${En(this.speak(r))} ${o.decimalPlaces}`;return[...this.speak(n),a(`${o.comma} ${o.rounded} ${e}`)]}case"fixed":return 0===("number"===r.type?Number(r.value):NaN)?[...this.speak(n),a(`${o.comma} ${o.asWholeNumber}`)]:[...this.speak(n),a(`${o.comma} ${o.writtenTo}`),...this.speak(r),a(o.decimalPlaces)];case"atan2":return[a(`${this.names.atan2} ${o.of}`),...this.speak(n),a(o.over),...this.speak(r)];case"random":return[a(o.randomNumber)];case"expm1":return[a(o.eToThe),...this.speak(n),a(`${o.comma} ${o.minus} 1`)];case"?:":{const e=[a(o.if),...this.speak(n),a(`${o.comma} ${o.then}`),...this.speak(r)];return(e=>"symbol"===e.type&&"unchanged"===e.value)(s[2])?e:[...e,a(`${o.comma} ${o.otherwise}`),...this.speak(s[2])]}case"sum":return s.flatMap((e,t)=>t?[a(o.plus),...this.speak(e)]:this.speak(e));case"prod":return s.flatMap((e,t)=>t?[a(o.times),...this.quantity(e)]:this.quantity(e));case"mean":case"max":case"min":case"gcd":return[a(`${this.names[t]} ${o.of}`),...l()];case"()":return 2===s.length?[a(o.openInterval),...this.speak(n),a(o.to),...this.speak(r)]:l();case"pmatrix":case"bmatrix":return this.matrix(e);case"vmatrix":return[a(o.determinantOf),...this.matrix(e)];case"[]":return ms(e)?this.matrix(e):2===s.length?[a(o.closedInterval),...this.speak(n),a(o.to),...this.speak(r)]:[a(o.theList),...l()];case"(]":case"[)":return[a(o.halfOpenInterval),...this.speak(n),a(o.to),...this.speak(r)];case"{}":return s.length?[a(o.theSet),...l()]:[a(o.emptySet)];case"setbuilder":return[a(o.setOf),..."op"===s[0].type&&"∈"===s[0].op?[...this.speak(s[0].args[0]),a("in"),...this.speak(s[0].args[1])]:this.speak(s[0]),a(o.suchThat),...this.speak(s[1])];case"evaluated":return[...this.speak(s[0]),a(`${o.comma} ${o.evaluatedFrom}`),...this.speak(s[2]),a(o.to),...this.speak(s[3])];case"der":return this.derivative(e);case"int":return this.integral(e);case"lim":return this.limit(e);case"\\sumidx":return this.bigOperator(e,o.sumFrom,o.sumOver,o.sumOf,"endSum");case"\\prodidx":return this.bigOperator(e,o.productFrom,o.productOver,o.productOf,"endProduct")}throw new Error(`No speech for '${t}'`)}matrix(e){const t=this.w,s=ms(e),n=[this.seg(e,t.matrix(s.length,s[0].length))];return s.forEach((s,r)=>{const i=e.args[r];n.push(this.seg(e,t.comma),this.seg("op"===i.type&&"[]"===i.op?i:e,`${t.row(r+1)}:`)),s.forEach((s,r)=>n.push(...r?[this.seg(e,t.comma)]:[],...this.speak(s)))}),[...n,...this.closer(e,"endMatrix")]}arguments(e){return 1===e.length?this.argument(e[0]):e.flatMap((e,t)=>t?[{text:this.w.comma,id:e.id},...this.listed(e)]:this.listed(e))}listed(e){return this.audio?this.quantity(e):this.speak(e)}pauseAfter(e){return this.isCompound(e)?[this.seg(e,this.w.comma)]:[]}leftSide(e){return this.audio&&"op"===e.type&&"?:"===e.op?[this.seg(e,this.w.quantity),...this.speak(e),...this.closer(e,"endQuantity")]:this.speak(e)}quantityIfSum(e){return this.quantity(e)}product(e){const[t,s]=e.args,n=this.w;if(Bt(e))return this.speak({...e,op:"neg",args:[s]});if("symbol"===s.type&&"°"===s.value)return[...this.speak(t),this.seg(e,n.degrees)];const r=this.audio&&"op"===t.type&&"/"===t.op&&!t.args.every(e=>"number"===e.type&&/^\d$|^10$/.test(e.value))?[this.seg(t,n.quantity),...this.speak(t),...this.closer(t,"endFraction")]:this.quantity(t);this.audio&&"op"===t.type&&"^"===t.op&&"number"!==t.args[1].type&&r.push(...this.closer(t.args[1],"endExponent"));const i=this.quantity(s);if(e.written&&[t,s].some(yn))return[...r,this.seg(e,n[e.written]),...i];return Boolean(e.written)||this.isCompound(t)||this.isCompound(s)||this.isWord(t)||this.isWord(s)||this.startsWithNumber(s)||"op"===s.type&&"/"===s.op||this.isCall(t)||i[0].text.startsWith("the ")?[...r,this.seg(e,n.times),...i]:[...r,...i]}isWord(e){return"symbol"===e.type&&/^[A-Za-z]{2,}$/.test(e.value)&&!Rt[e.value]&&!["pi","tau"].includes(e.value)}isCall(e){return"op"===e.type&&(i(e.op)||"prefix"===o(e.op)?.fixity&&100===l(e.op))}startsWithNumber(e){let t=e;for(;"op"===t.type&&t.args.length;)t=t.args[0];return"number"===t.type}fraction(e){const[t,s]=e.args,n=this.w,r=t=>this.seg(e,t);if("number"===t.type&&"number"===s.type&&/^[1-9]$/.test(t.value)&&/^([2-9]|10)$/.test(s.value)){const e=Number(t.value),i=Number(s.value),o=2===i?1===e?n.half:n.halves:n.ordinals[i]+(1===e?"":"s");return[r(`${n.numerators[e]} ${o}`)]}const i=(e,...t)=>"op"===e.type&&t.includes(e.op)&&!Bt(e),o=this.grouped(t,null,i(t,"/")),a=this.isCall(t)?[r(n.comma)]:[],l=this.speak(s),c=i(s,"/")||this.audio&&i(s,"*")&&l.some(e=>e.id===s.id&&e.text===n.times),h=this.isCompound(s)||this.audio&&c;return[...o,...a,r(n.over),...h?[this.seg(s,n.quantity),...l,...this.closer(s,"endFraction")]:l]}power(e){const[t,s]=e.args,n=this.w,r=t=>this.seg(e,t),i=this.isCompound(t)||this.isCall(t)||"number"===t.type&&t.value.startsWith("-")||"op"===t.type&&"/"===t.op||this.audio&&"op"===t.type&&("^"===t.op||"neg"===t.op||Bt(t))?[r(n.quantity),...this.speak(t),...this.closer(t,"endQuantity")]:this.speak(t);if(this.isCall(t)&&this.names[t.op]&&"number"===s.type&&/^[2-3]$/.test(s.value)&&1===t.args.length){const e=2===Number(s.value)?n.squared:n.cubed;return[r(`${this.names[t.op]} ${e} ${n.of}`),...this.argument(t.args[0])]}if("number"===s.type&&/^(?:[1-9]|10)$/.test(s.value)){const e=Number(s.value);return 2===e||3===e?[...i,r(n.powers[e])]:[...i,r(`${n.toThe} ${n.powers[e]}`)]}if("number"===s.type&&/^\d+$/.test(s.value))return[...i,r(`${n.toThe} ${this.ordinal(Number(s.value))}`)];if(!(this.isCompound(s)||"op"===s.type&&"/"===s.op&&this.isCompound(s.args[0]))){const e=this.speak(s),t=e[0]?e[0].text.split(" ")[0]:"",o=e[0]&&(e[0].text.startsWith(n.quantity)||t===n.toThe.split(" ").pop());return[...i,r(o?n.toThePowerOf:n.toThe),...e]}return this.audio?[...i,r(n.toThePowerOf),r(n.quantity),...this.speak(s),...this.closer(s,"endExponent")]:[...i,r(n.toThePowerOf),...this.speak(s),r(n.comma)]}ordinal(e){if(e<=10&&e>=1)return"squared"===this.w.powers[e]?"second":"cubed"===this.w.powers[e]?"third":this.w.powers[e];return`${e}${e%100>=11&&e%100<=13?"th":["th","st","nd","rd"][e%10]??"th"}`}derivative(e){const{n:t,body:s,by:n,at:r,variable:i}=ls(e),o=this.w,a=t=>this.seg(e,t),l=r?[a(`${o.comma} ${o.at}`),...this.speak(n),a(o.equals),...this.speak(r)]:[];if(i){return[...[a(1===t?o.d:`${o.d} ${2===t?o.squared:3===t?o.cubed:`${o.toThe} ${this.ordinal(t)}`}`),...this.speak(s),a(o.d),...this.speak(n),a(1===t?"":2===t?o.squared:3===t?o.cubed:`${o.toThe} ${this.ordinal(t)}`)],...l]}return[a(`${1===t?o.derivative:2===t?o.secondDerivative:o.nthDerivative(this.ordinal(t))} ${o.withRespectTo}`),...this.speak(n),a(o.of),...this.contents(s,"endDerivative"),...l]}integral(e){const{args:t}=e,[s,n,r,i]=t,o=this.w,a=t=>this.seg(e,t),l=t.length>=2&&"symbol"===n.type&&1===n.value.length,c=l?this.speak(n):[a(L(s)||"x")];let h=[];4===t.length?h=[r,i]:3===t.length?h=l?[r]:[n,r]:2!==t.length||l||(h=[n]);return[...2===h.length?[a(o.integralFrom),...this.speak(h[0]),a(o.to),...this.speak(h[1]),a(o.of)]:1===h.length?[a(o.integralFrom),...this.speak(h[0]),a(o.of)]:[a(o.integralOf)],...this.speak(s),a(`${o.comma} ${o.d}`),...c]}limit(e){const{args:t}=e,[s,n,r,i]=t,o=this.w,a=t=>this.seg(e,t);let l;if(t.length>=3){if(l=[...this.speak(n),a(o.approaches),...this.speak(r)],4===t.length){const e=String(i.value??"").toLowerCase().startsWith("r");l.push(a(e?o.fromTheRight:o.fromTheLeft))}}else if(2===t.length&&"op"===n.type&&"→"===n.op)l=[...this.speak(n.args[0]),a(o.approaches),...this.speak(n.args[1])];else{if(2!==t.length)return[a(`${o.limitAs.replace(/ as$/,"")} ${o.of}`),...this.speak(s)];{const e=L(s);l=e?[a(e),a(o.approaches),...this.speak(n)]:this.speak(n)}}return this.audio&&"op"===s.type&&"/"===s.op?[a(o.limitAs),...l,a(o.of),...this.grouped(s,"endLimit",!0)]:[a(o.limitAs),...l,a(o.of),...this.contents(s,"endLimit")]}bigOperator(e,t,s,n,r){const{args:i}=e,[o,a,l]=i,c=this.w,h=t=>this.seg(e,t);let u;return u=3===i.length?[h(t),...this.speak(a),h(c.to),...this.speak(l),h(c.of)]:2===i.length?[h(s),...this.speak(a),h(c.of)]:[h(n)],[...u,...this.contents(o,r)]}}for(const e of[bn,wn,_n,Mn,vn])Object.setPrototypeOf(e,null);function qn(e,t={}){return xn(e)?[]:function(e,t={}){return An(e,t,!1)}($n(e),t)}function Sn(e,t={}){return xn(e)?"":kn($n(e),t)}const zn={"(":{fill:"M0.92,0 C0.1,0.2 0.1,0.8 0.92,1 L1,0.975 C0.48,0.75 0.48,0.25 1,0.025 Z"},")":{fill:"M0.08,0 C0.9,0.2 0.9,0.8 0.08,1 L0,0.975 C0.52,0.75 0.52,0.25 0,0.025 Z"},"{":{fill:"M1,0 C0.45,0 0.5,0.05 0.5,0.2 L0.5,0.38 C0.5,0.46 0.35,0.49 0.05,0.5 C0.35,0.51 0.5,0.54 0.5,0.62 L0.5,0.8 C0.5,0.95 0.45,1 1,1 L1,0.985 C0.68,0.985 0.64,0.95 0.64,0.8 L0.64,0.62 C0.64,0.53 0.55,0.505 0.3,0.5 C0.55,0.495 0.64,0.47 0.64,0.38 L0.64,0.2 C0.64,0.05 0.68,0.015 1,0.015 Z"},"}":{fill:"M0,0 C0.55,0 0.5,0.05 0.5,0.2 L0.5,0.38 C0.5,0.46 0.65,0.49 0.95,0.5 C0.65,0.51 0.5,0.54 0.5,0.62 L0.5,0.8 C0.5,0.95 0.55,1 0,1 L0,0.985 C0.32,0.985 0.36,0.95 0.36,0.8 L0.36,0.62 C0.36,0.53 0.45,0.505 0.7,0.5 C0.45,0.495 0.36,0.47 0.36,0.38 L0.36,0.2 C0.36,0.05 0.32,0.015 0,0.015 Z"},sqrt:{stroke:"M0,0.62 L0.18,0.55 L0.45,0.97 L0.98,0"},cancel:{stroke:"M0,1 L1,0"},int:{stroke:"M0.95,0.09 C0.9,0.01 0.72,0 0.66,0.12 C0.6,0.25 0.42,0.75 0.34,0.88 C0.28,0.99 0.1,0.99 0.05,0.91"},sum:{fill:"M0.04,0 L0.94,0 L0.98,0.24 L0.94,0.24 C0.9,0.1 0.8,0.07 0.62,0.07 L0.24,0.07 L0.58,0.5 L0.2,0.93 L0.64,0.93 C0.82,0.93 0.92,0.9 0.95,0.76 L0.99,0.76 L0.94,1 L0.02,1 L0.47,0.47 Z"},prod:{fill:"M0,0 L1,0 L1,0.04 L0.88,0.05 L0.88,0.95 L1,0.96 L1,1 L0.62,1 L0.62,0.96 L0.74,0.95 L0.74,0.06 L0.26,0.06 L0.26,0.95 L0.38,0.96 L0.38,1 L0,1 L0,0.96 L0.12,0.95 L0.12,0.05 L0,0.04 Z"}};function Nn(e,t,s=""){const n=zn[e];return`<svg class="${t}" viewBox="0 0 1 1" preserveAspectRatio="none" aria-hidden="true"${s?` style="${s}"`:""}>${n.fill?`<path d="${n.fill}" fill="currentColor"/>`:`<path d="${n.stroke}" fill="none" stroke="currentColor" vector-effect="non-scaling-stroke"/>`}</svg>`}const Ln=.8,Zn=.2,Cn=.25,Rn=.7,In="−",jn={"+":"add","-":"sub","*":"mul","/":"frac","^":"pow",_:"subscript",neg:"neg",pm:"pm","!":"factorial","=":"eq","===":"eq","≠":"neq","≠=":"neq","<":"lt",">":"gt","≤":"le","≥":"ge","≈":"approx","≡":"equiv","∈":"in","∉":"notin","∪":"union","∩":"intersect","→":"to","±":"pm","&":"and","|":"or","\\not":"not","\\xor":"xor","\\nand":"nand","\\nor":"nor","\\implies":"implies","\\iff":"iff","?:":"cases","()":"tuple","[]":"list","{}":"set","(]":"interval","[)":"interval","\\sumidx":"sum","\\prodidx":"prod",mod:"mod",pmod:"pmod",pmatrix:"matrix",bmatrix:"matrix",vmatrix:"matrix"},Tn={"=":"=","===":"=","≠":"≠","≠=":"≠","<":"<",">":">","≤":"≤","≥":"≥","∈":"∈","∉":"∉","→":"→","≈":"≈","≡":"≡","&":"∧","|":"∨","\\xor":"⊕","\\nand":"↑","\\nor":"↓","\\implies":"⇒","\\iff":"⇔"},Fn={"±":"±","∪":"∪","∩":"∩",mod:"mod"},Pn=["sin","cos","tan","csc","sec","cot","sinh","cosh","tanh"],Wn={arcsin:"sin",arccos:"cos",arctan:"tan",arcsec:"sec",arccsc:"csc",arccot:"cot",arcsinh:"sinh",arccosh:"cosh",arctanh:"tanh"},Bn=[...Pn,...Object.keys(Wn),"ln","det"],Un={atan2:"atan2",max:"max",min:"min",gcd:"gcd",mean:"mean",round:"round",fixed:"fixed",expm1:"expm1",random:"random"},Dn={"()":["(",")"],"[]":["[","]"],"{}":["{","}"],"(]":["(","]"],"[)":["[",")"]},Gn=["int","lim","\\sumidx","\\prodidx"],Hn={"\\partial":"∂","\\nabla":"∇","\\ell":"ℓ","\\infty":"∞","\\emptyset":"∅"},Qn={vec:"→",hat:"ˆ",widehat:"ˆ",tilde:"˜",dot:"˙",ddot:"¨"},Vn=/^[acegmnopqrsuvwxyzαγεημνοπρστυχω]$/,Xn=e=>"symbol"===e?.type&&Vn.test(Rt[e.value]??e.value),Jn=["boxed","overline","bar","underline","cancel",...Object.keys(Qn)],Kn={prefix:"mv",display:!0,fnParens:"auto",trigPowers:!0,inverseTrig:"arc",derivativeBrackets:"[",scriptFractions:"slash",cases:!0,label:!0,mathml:null,attributes:null,style:null},Yn=e=>String(e).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;"),er=e=>+e.toFixed(3)+"em",tr=(e,t=Ln,s=Zn)=>({html:e,h:t,d:s});function sr(...e){const t=e.flat().filter(Boolean);return{html:t.map(e=>e.html).join(""),h:Math.max(Ln,...t.map(e=>e.h)),d:Math.max(Zn,...t.map(e=>e.d))}}function nr(e,t={}){const s=new rr({...Kn,...t}),n=s.render(e,{level:0}),r=`${s.p}${s.o.display?` ${s.p}-display`:""}`,i="function"==typeof s.o.mathml?function(e,t,s,n){try{return String(e(t)).replace(/ id="mz-\d+"/g,"").replace(/^<math>/,`<math class="${s}-sr-math"${n?' display="block"':""}>`)}catch{return""}}(s.o.mathml,e,s.p,s.o.display):"",o=i?null:function(e,t){if(!1===t||null===t)return null;if("string"==typeof t)return t;try{return"function"==typeof t?t(e):Sn(e)}catch{return null}}(e,s.o.label),a=null!==o||i?n.html.replace(/^<span /,'<span aria-hidden="true" '):n.html;return{html:`<span class="${r}"${null===o?"":` role="math" aria-label="${Yn(o)}"`}>${a}${i}</span>`,h:n.h,d:n.d}}class rr{constructor(e){this.o=e,this.p=e.prefix}el(e,t,s="",n=""){return`<span class="${e.split(" ").map(e=>`${this.p}-${e}`).join(" ")}"${s?` style="${s}"`:""}${n}>${t}</span>`}text(e,t){const s=tr(this.el(e,Yn(t)));return s.leaf={kind:e,content:Yn(t)},s}bin(e){return this.text("bin",e)}rel(e){return this.text("rel",e)}punct(e){return this.text("punct",e)}word(e){return this.text("word",e)}wrap(e,t,s){let n="";void 0!==e.id&&(n+=` data-node="${e.id}"`),"op"===e.type&&(n+=` data-op="${Yn(e.op)}"`);const r=this.o.attributes&&this.o.attributes(e)||{};for(const[e,t]of Object.entries(r))"class"!==e&&"style"!==e&&/^[A-Za-z_:][\w:.-]*$/.test(e)&&(n+=` ${e}="${Yn(t)}"`);const i=[r.style,this.o.style&&this.o.style(e)].filter(Boolean).join(";"),o=(s.leaf?["n",...t,s.leaf.kind]:["n",...t]).map(e=>`${this.p}-${e}`).concat(r.class?[Yn(r.class)]:[]).join(" "),a=s.leaf?s.leaf.content:s.html;return{html:`<span class="${o}"${i?` style="${Yn(i)}"`:""}${n}>${a}</span>`,h:s.h,d:s.d}}render(e,t){return"number"===e.type?this.number(e):"string"===e.type?this.wrap(e,["text"],this.text("text",e.value.replace(/^["'`]|["'`]$/g,""))):"symbol"===e.type?this.wrap(e,["symbol"],this.symbol(e.value)):this.operation(e,t)}number(e){const t=/^(-?\d*\.)(\d*)\.\.$/.exec(e.value);if(t){const[,s,n]=t;let r=1;for(let e=1;2*e<=n.length;e++)if(n.slice(-e)===n.slice(-2*e,-e)){r=e;break}let i=n;for(;i.endsWith(n.slice(-r))&&i.length>=r;)i=i.slice(0,-r);const o=this.text("num",(s+i).replace(/^-/,In)),a=this.text("num",n.slice(-r));return this.wrap(e,["number"],sr(o,tr(this.el("over",a.html),a.h+.14,a.d)))}return this.wrap(e,["number"],this.text("num",e.value.replace(/^-/,In)))}symbol(e){return Rt[e]&&(e=Rt[e]),Hn[e]?this.text("ord",Hn[e]):"\\+"===e||"\\-"===e?this.text("ord",e[1]):"-∞"===e?this.text("ord","−∞"):/^[A-Za-z]$/.test(e)||/^[α-ω]$/.test(e)?this.text("var",e):/^[A-Za-z]′+$/.test(e)?tr(this.el("var",Yn(e[0]))+this.el("prime",Yn(e.slice(1)))):/^Δ[A-Za-z]$/.test(e)?tr(this.el("ord","Δ")+this.el("var",e[1])):/^d[A-Za-zα-ω]$/.test(e)?tr(this.el("ord","d")+this.el("var",e[1])):e.endsWith("⁻¹")?this.script(this.symbol(e.slice(0,-2)),this.text("num","−1"),{level:0}):/^\\[A-Za-z]+$/.test(e)?this.text("name",e.slice(1)):/^[A-Za-z]{2,}$/.test(e)?this.text("name",e):this.text("ord",e)}operation(e,t){const{op:s,args:n}=e,[r,o]=n,a=(e,s={})=>this.render(e,{...t,...s,forceParens:s.forceParens||!1,before:s.before||!1,leftFactor:!1}),c=jn[s]??(/^[a-z0-9]+$/i.test(s)?s:"other"),h=t=>this.wrap(e,["op",`op-${c}`],t),u=(t,s)=>Gt(e,t,s)&&"/"!==t.op?this.fence(a(t)):a(t);if(Bn.includes(s)||"log"===s)return h(this.named(e,t));if(Un[s])return h(sr(this.word(Un[s]),this.fence(this.list(n,t))));if(i(s))return h(this.custom(e,t));const p=ms(e);if(p)return h(this.matrix(e,p,t));if("{}"===s&&!n.length)return h(this.text("op","∅"));if(Dn[s])return h(this.fence(this.list(n,t),...Dn[s]));if(("|"===s||"&"===s)&&n.length>=2&&n.every(ar)){const e=e=>"|"!==e.op&&"&"!==e.op||e.op===s?a(e):this.fence(a(e));return h(sr(n.map((t,n)=>n?[this.text("conj","|"===s?"or":"and"),e(t)]:[e(t)]).flat()))}if(Tn[s]&&2===n.length)return h(sr(u(r,"left"),this.rel(Tn[s]),u(o,"right")));if(Fn[s]&&2===n.length)return h(sr(u(r,"left"),this.bin(Fn[s]),u(o,"right")));switch(s){case"+":if(Bt(o)){const e=o.args[1],t=Gt({op:"-"},e,"right")?this.fence(a(e)):a(e);return h(sr(u(r,"left"),this.bin(In),this.wrap(o,["op","op-mul"],t)))}return"number"===o.type&&o.value.startsWith("-")?h(sr(u(r,"left"),this.bin(In),this.wrap(o,["number"],this.text("num",o.value.slice(1))))):h(sr(u(r,"left"),this.bin("+"),this.signed(o,u(o,"right"))));case"-":return h(sr(u(r,"left"),this.bin(In),this.signed(o,u(o,"right"))));case"*":return h(this.product(e,t));case"/":return h(this.fraction(e,t));case"^":return h(this.power(e,t));case"_":{const e=Ut(r)&&l(Ut(r))<100;return h(this.script(e?this.fence(a(r)):a(r,{forceParens:!0}),null,t,a(o,{level:t.level+1})))}case"neg":return h(sr(this.text("sign",In),Ht(s,r)?this.fence(a(r)):a(r,{before:t.before})));case"pm":return h(sr(this.text("sign","±"),Ht(s,r)?this.fence(a(r)):a(r)));case"!":return h(sr(Ht(s,r)?this.fence(a(r)):a(r,{forceParens:!0}),this.text("ord","!")));case"\\not":return h(sr(this.text("sign","¬"),Ht(s,r)?this.fence(a(r)):a(r)));case"pmod":return h(sr(a(r),this.fence(sr(this.word("mod"),this.text("space",""),a(o)))));case"sqrt":return h(this.radical(a(r)));case"root":return h(this.radical(a(o),this.render(r,{level:t.level+2})));case"abs":return h(this.fence(a(r),"|","|"));case"floor":return h(this.fence(a(r),"⌊","⌋"));case"ceil":return h(this.fence(a(r),"⌈","⌉"));case"loglog":return h(sr(this.word("ln"),this.fence(sr(this.word("ln"),this.fence(a(r))))));case"logloglog":return h(sr(this.word("ln"),this.fence(sr(this.word("ln"),this.fence(sr(this.word("ln"),this.fence(a(r))))))));case"perm":return h(this.script(this.script(tr(""),this.render(r,{level:t.level+1}),t),null,t,this.render(o,{level:t.level+1}),this.word("P")));case"comb":return h(this.fence(this.stack(a(r),a(o),!1)));case"?:":return h(this.o.cases?this.cases(e,t):sr(a(r),this.bin("?"),a(o),this.bin(":"),a(n[2])));case"sum":case"prod":{const e={op:"sum"===s?"+":"*"},t=(t,s)=>Gt(e,t,s?"right":"left")?this.fence(a(t)):a(t);return h(sr(n.map((e,n)=>n?[this.bin("sum"===s?"+":"·"),t(e,n)]:[t(e,n)]).flat()))}case"setbuilder":return h(this.fence(sr(a(r),this.bin("|"),a(o)),"{","}"));case"evaluated":{const e=e=>this.render(e,{...t,level:t.level+1});return h(this.script(this.fence(a(r),"[","]"),e(n[3]),t,e(n[2])))}case"der":return h(this.derivative(e,t));case"int":return h(this.integral(n,t));case"lim":return h(this.limit(n,t));case"\\sumidx":return h(this.bigOperator("sum",n,t));case"\\prodidx":return h(this.bigOperator("prod",n,t))}throw new Error(`Inline Math cannot draw '${s}'`)}signed(e,t){return("number"===e.type?e.value.startsWith("-"):"neg"===e.op||"pm"===e.op)&&!t.fenced?this.fence(t):t}list(e,t){return sr(e.map((e,s)=>s?[this.punct(","),this.render(e,t)]:[this.render(e,t)]).flat())}product(e,t){const[s,n]=e.args;if(Bt(e))return sr(this.text("sign",In),Ht("neg",n)?this.fence(this.render(n,t)):this.render(n,t));const r=function(e){const[t,s]=e.args;return{left:Gt(e,t,"left")&&"/"!==t.op,right:Gt(e,s,"right")&&"/"!==s.op}}(e);Gn.includes(s.op)&&(r.left=!0);const o=!e.written&&Qt(s,n,r),a=function(e){for(;"op"===e.type;){if(Bn.includes(e.op)||"log"===e.op||Un[e.op]||i(e.op))return!0;if("^"!==e.op&&"*"!==e.op)return!1;e=e.args[0]}return!1}(n),l=r.left?this.fence(this.render(s,t)):this.render(s,{...t,before:o&&!a,leftFactor:!0}),c=r.right?this.fence(this.render(n,t)):this.render(n,{...t,before:t.before});return sr(l,o?tr(this.el(a?"fnspace":"times","")):this.bin("cross"===e.written?"×":"·"),c)}named(e,t,s=null){const{op:n,args:r}=e,i="log"===n?r[1]:r[0];let o;o=Wn[n]&&"power"===this.o.inverseTrig?this.script(this.word(Wn[n]),this.text("num","−1"),t):this.word(n),s&&(o=this.script(o,s,t)),"log"===n&&(o=this.script(o,null,t,this.render(r[0],{level:t.level+1})));const a="auto"===this.o.fnParens&&!t.forceParens&&!t.before&&ir(i),l=this.render(i,{...t,forceParens:!1,before:!1});return sr(o,a?sr(tr(this.el("fnspace","")),l):this.fence(l))}custom(e,t){const s=ss(e.op),n=e.args;if(Jn.includes(s)&&1===n.length)return this.decorate(s,this.render(n[0],t),n[0]);return sr(/^[A-Za-z]′*(⁻¹)?$/.test(s)?this.symbol(s):this.word(s),tr(this.el("callgap","")),this.fence(this.list(n,t)))}decorate(e,t,s){switch(e){case"boxed":return tr(this.el("boxed",t.html),t.h+.22,t.d+.22);case"cancel":return tr(this.el("cancel",t.html+Nn("cancel",`${this.p}-cancel-line`)),t.h,t.d);case"underline":return tr(this.el("underline",t.html),t.h,t.d+.12);case"overline":case"bar":return tr(this.el("over"+(Xn(s)?" over-low":""),t.html),t.h+.14,t.d);default:{const n=Xn(s);return tr(this.el("accent"+(n?" accent-low":""),this.el(`accent-mark accent-${e}`,Qn[e])+t.html),t.h+.3,t.d)}}}fraction(e,t){const[s,n]=e.args;if(t.level>0&&"slash"===this.o.scriptFractions&&ir(s,!0)&&ir(n,!0)){const e={...t,leftFactor:!1},r=(t,s)=>"op"!==t.type||ir(t)&&(!s||"*"!==t.op||Bt(t))?this.render(t,e):this.fence(this.render(t,e)),i=sr("op"===s.type&&"neg"===s.op?this.render(s,e):r(s,!1),this.text("ord","/"),r(n,!0));return t.leftFactor?this.fence(i):i}const r=t.level>0||t.inFraction||!this.o.display?.8:1,i={...t,inFraction:!0,forceParens:!1,before:!1};return this.stack(this.render(s,i),this.render(n,i),!0,r)}stack(e,t,s=!0,n=1){const r=e=>1===n?e.html:this.el("scaled",e.html,`font-size:${er(n)}`),i=s?.06:0,o=Cn+i/2+.1+Math.max(Zn,n*e.d),a=this.el("frac"+(s?"":" frac-nobar"),this.el("numer",r(e))+this.el("denom",r(t)),`vertical-align:${er(o)}`);return tr(a,o+Math.max(Ln,n*e.h),Math.max(Ln,n*t.h)+Math.max(Zn,n*t.d)+.1+i/2-Cn)}power(e,t){const[s,n]=e.args,r={...t,level:t.level+1,forceParens:!1,before:!1,inFraction:!1,leftFactor:!1},i=()=>this.render(n,r);if(this.o.trigPowers&&Pn.includes(s.op)&&cr(n))return this.wrap(s,["op",`op-${s.op}`],this.named(s,t,i()));if("_"===s.op){const e=this.render(s.args[1],r);return this.wrap(s,["op","op-subscript"],this.script(this.render(s.args[0],{...t,forceParens:!0}),i(),t,e))}const o=Gt(e,s,"left")||"number"===s.type&&s.value.startsWith("-")||"/"===s.op||Bn.includes(s.op)||Gn.includes(s.op)?this.fence(this.render(s,t)):this.render(s,{...t,forceParens:!0});return this.script(o,i(),t)}script(e,t,s,n=null,r=null){const i=s.level>0?.8:Rn,o=r?sr(e,r):e;let a=t?Math.max(.42,e.h-.38,i*t.d+.12):0,l=n?Math.max(.2,e.d-.05,i*n.h-.45):0;if(t&&n){const e=.1-(a-i*t.d-(i*n.h-l));e>0&&(l+=e)}const c=e=>this.el("script",e.html,`font-size:${er(i)}`);if(t&&!n)return tr(e.html+this.el("sup",t.html,`font-size:${er(i)};vertical-align:${er(a/i)}`),Math.max(o.h,a+i*t.h),Math.max(o.d,i*t.d-a));if(n&&!t)return tr(o.html+this.el("sub",n.html,`font-size:${er(i)};vertical-align:${er(-l/i)}`),Math.max(o.h,i*n.h-l),Math.max(o.d,l+i*n.d));const h=a+i*t.h,u=l+i*n.d,p=this.el("subsup",c(t)+c(n),`vertical-align:${er(a)};height:${er(h+u)}`);return tr(o.html+p,Math.max(o.h,h),Math.max(o.d,u))}radical(e,t=null){const s=Math.max(e.h,Ln)+.1+.06,n=Math.max(e.d,Zn),r=s+n,i=.5+.06*(r-1),o=Nn("sqrt",`${this.p}-hook`,`width:${er(i)};left:${er(-i)}`);let a="";if(t){const e=.8*Rn;a=this.el("rootindex",t.html,`font-size:${er(e)};vertical-align:${er((.55*r-n)/e)}`)}return tr(this.el("sqrt",a+this.el("radicand",o+e.html,`margin-left:${er(i+.05)}`)),s,n)}fence(e,t="(",s=")"){const n=Math.max(e.h,Ln)+Math.max(e.d,Zn),r=n>1.25,i=r?.1:0,o=(e,t)=>{if(!e)return{html:"",w:0};const s={"(":.26,")":.26,"{":.36,"}":.36}[e];if(s){const i=s+(r?Math.min(.2,.05*(n-1)):0);return{html:Nn(e,`${this.p}-delim ${this.p}-delim-${t}`,`width:${er(i)}`),w:i+.1}}const i={"[":"lbrack","]":"rbrack","|":"vert","⌊":"lfloor","⌋":"rfloor","⌈":"lceil","⌉":"rceil"}[e];return{html:this.el(`delim ${i} delim-${t}`,""),w:"|"===e?.18:.3}},a=o(t,"l"),l=o(s,"r");if(!r){const t=tr(this.el("fence fence-line",a.html+e.html+l.html),Math.max(e.h,Ln),Math.max(e.d,Zn));return t.fenced=!0,t}const c=`padding:${er(i)} ${er(l.w)} ${er(i)} ${er(a.w)}`,h=tr(this.el("fence",a.html+e.html+l.html,c),Math.max(e.h,Ln)+i,Math.max(e.d,Zn)+i);return h.fenced=!0,h}derivative(e,t){const s=this.leibniz(e,t),{by:n,at:r}=ls(e);if(!r)return s;const i=e.args[1],o=this.render({...i,args:[{...n,id:void 0},r]},{...t,level:t.level+1});return this.script(this.fence(s,"","|"),null,t,o)}leibniz(e,t){const{n:s,body:n,by:r,variable:i}=ls(e),o={...t,level:t.level+1},a=()=>this.text("d","d"),c=()=>s>1?this.render({type:"number",value:String(s)},o):null,h={...t,inFraction:!0},u=sr(a(),c()?this.script(this.render(r,h),c(),h):this.render(r,h)),p=e=>sr(c()?this.script(a(),c(),h):a(),e||[]),f=t.level>0||t.inFraction||!this.o.display?.8:1;if(i)return this.stack(p(this.render(n,h)),u,!0,f);const d=Ut(n)&&l(Ut(n))<100,[g,m]="("===this.o.derivativeBrackets?["(",")"]:["[","]"],$=d?this.fence(this.render(n,t),g,m):this.render(n,t);return sr(this.stack(p(),u,!0,f),$)}integral(e,t){const[s,n,r,i]=e,o=e=>this.render(e,t),a=e=>e?this.render(e,{...t,level:t.level+1}):null,l=()=>this.text("var",L(s)||"x");let c,h=null,u=null;switch(e.length){case 1:c=l();break;case 2:ts(n)?c=o(n):(c=l(),h=a(n));break;case 3:ts(n)?(c=o(n),h=a(r)):(c=l(),h=a(n),u=a(r));break;default:c=o(n),h=a(r),u=a(i)}const p=o(s),f=tr(this.el("dx",this.el("d","d")+c.html),c.h,c.d);return sr(this.bigSign("int",p,h,u,t),p,f)}bigSign(e,t,s,n,r){const i=!this.o.display||r.level>0,o=Math.max(i?.6:.95,Math.max(t.h-Cn,t.d+Cn)+.12),a=Nn(e,`${this.p}-bigsign ${this.p}-${e}sign`,`height:${er(2*o)};width:${er(i?.38:.5)};vertical-align:${er(Cn-o)}`);if(!s&&!n)return tr(a,Cn+o,o-Cn);const l=Rn,c=n||tr(""),h=s||tr(""),u=Cn+o-l*c.h;h.d;const p=this.el("limits",this.el("script",c.html,`font-size:${er(l)}`)+this.el("script lower",h.html,`font-size:${er(l)}`),`vertical-align:${er(u)};height:${er(2*o)}`);return tr(a+p,Cn+o,o-Cn)}limit(e,t){const[s,n,r,i]=e,o={...t,level:t.level+1},a=e=>this.render(e,o),l=()=>this.rel("→");let c;switch(e.length){case 1:return sr(this.word("lim"),tr(this.el("fnspace","")),this.body(s,t));case 2:{const e=L(s),t="number"===n.type||"symbol"===n.type&&n.value.includes("∞");c=e&&t?sr(this.text("var",e),l(),a(n)):a(n);break}case 3:c=sr(a(n),l(),a(r));break;default:{const e=It(i).toLowerCase().startsWith("r")?"+":In;c=sr(a(n),l(),this.script(a(r),this.wrap(i,["symbol"],this.text("ord",e)),o))}}return sr(this.under(this.word("lim"),c,t),this.body(s,t))}under(e,t,s){return!this.o.display||s.level>0?this.script(e,null,s,t):tr(this.el("under",e.html+this.el("script",t.html,`font-size:${er(.7)}`)),e.h,Math.max(e.d,Zn)+.05+.7*(t.h+t.d))}body(e,t){const s=Ut(e);return s&&l(s)<l("*")?this.fence(this.render(e,t)):this.render(e,t)}bigOperator(e,t,s){const[n,r,i]=t,o={...s,level:s.level+1},a=r?this.render(r,o):null,l=i?this.render(i,o):null,c=this.body(n,s);if(!this.o.display||s.level>0){const t=.55,n=Nn(e,`${this.p}-bigsign`,`height:${er(2*t)};width:${er(.8)};vertical-align:${er(Cn-t)}`);return sr(this.script(tr(n,Cn+t,t-Cn),l,s,a),c)}const h=.68,u=Rn,p=Nn(e,`${this.p}-bigsign`,`height:${er(1.36)};width:${er("sum"===e?1:1.05)}`),f=l?1.01+u*l.d:Cn-h,d=e=>this.el("script",e.html,`font-size:${er(u)}`),g=this.el("bigop",(l?d(l):"")+p+(a?d(a):""),`vertical-align:${er(f)}`);return sr(tr(g,.93+(l?.08+u*(l.h+l.d):0),h-Cn+(a?.08+u*(a.h+a.d):0)),c)}matrix(e,t,s){const n=t.map(e=>e.map(e=>this.render(e,s))),r=n.map(e=>({h:Math.max(Ln,...e.map(e=>e.h)),d:Math.max(Zn,...e.map(e=>e.d))})),i=r.reduce((e,t)=>e+t.h+t.d,0)+.3*(t.length-1),o=Cn+i/2-r[0].h,a=n.map((t,s)=>{const n=t.map(e=>this.el("matrix-cell",e.html)).join(""),r=e.args[s];return r&&"op"===r.type&&"[]"===r.op?this.wrap(r,["op","op-list","matrix-row"],tr(n)).html:n}).join(""),l=t[0].length,c=tr(this.el("matrix",a,`grid-template-columns:repeat(${l},auto);vertical-align:${er(o)}`),Cn+i/2,i/2-Cn),[h,u]=gs[e.op]??["[","]"];return this.fence(c,h,u)}cases(e,t){const s=[];let n=e;for(;"op"===n.type&&"?:"===n.op;)s.push([this.render(n.args[1],t),sr(this.word("if"),this.text("space",""),this.render(n.args[0],t))]),n=n.args[2];"symbol"===n.type&&"unchanged"===n.value||s.push([this.render(n,t),this.word("otherwise")]);const r=s.map(([e,t])=>({h:Math.max(e.h,t.h),d:Math.max(e.d,t.d)})),i=r.reduce((e,t)=>e+t.h+t.d,0)+.25*(s.length-1),o=Cn+i/2-r[0].h,a=s.map(([e,t])=>this.el("case-value",e.html)+this.el("case-cond",t.html)).join(""),l=tr(this.el("cases",a,`vertical-align:${er(o)}`),Cn+i/2,i/2-Cn);return this.fence(l,"{","")}}function ir(e,t=!1){return"number"===e.type?!e.value.startsWith("-")||t:"symbol"===e.type||"op"===e.type&&("abs"===e.op||ms(e)?!t:!t||"neg"!==e.op&&"^"!==e.op?"*"===e.op&&!Bt(e)&&e.args.every(e=>"number"===e.type&&!e.value.startsWith("-")||lr(e)||"*"===e.op&&ir(e,t)):ir(e.args[0],!0)&&("neg"===e.op||ir(e.args[1],!0)))}const or=["=","===","≠","≠=","<",">","≤","≥","≈","≡","∈","∉"],ar=e=>"op"===e.type&&e.args.length>=2&&(2===e.args.length&&or.includes(e.op)||("|"===e.op||"&"===e.op)&&e.args.every(ar)),lr=e=>"symbol"===e.type&&(1===e.value.length||/^[α-ωΑ-Ω]$/.test(e.value)||Boolean(Rt[e.value])),cr=e=>"number"===e.type&&/^[1-9]\d*$/.test(e.value)&&Number(e.value)<10;for(const e of[jn,Tn,Fn,Wn,Un,Dn,Hn,Qn])Object.setPrototypeOf(e,null);function hr(e){const{id:t,from:s,...n}=e;return n.args&&(n.args=n.args.map(hr)),n}const ur=e=>S(e).join(" ");function pr(e,t){const s=new Set;fr(e,t,s);const n=[];return N(e,e=>{s.has(e)&&n.push(e)}),n}function fr(e,t,s){if(null!=t)if(Array.isArray(t))t.forEach(t=>fr(e,t,s));else if("number"!=typeof t)if("function"!=typeof t){if("string"==typeof t){const n=ur(mn(t));return void N(e,e=>{ur(e)===n&&s.add(e)})}if(t.path){const n=dr(e,t.path);return void(n&&s.add(n))}if(t.type){const n=ur(t);N(e,e=>{e!==t&&ur(e)!==n||s.add(e)})}}else N(e,(e,n)=>{t(e,n)&&s.add(e)});else N(e,e=>{e.id===t&&s.add(e)})}function dr(e,t){let s=e;for(const e of t){if("op"!==s.type||!s.args[e])return null;s=s.args[e]}return s}function gr(e,t){const s=[],n=e=>{if(e.id===t)return!0;if("op"!==e.type)return!1;for(let t=0;t<e.args.length;t++){if(s.push(t),n(e.args[t]))return!0;s.pop()}return!1};return n(e)?s:null}function mr(t){let s=0;const n=t=>{"op"===t.type&&(t.args.forEach(n),o(t.op)?.arity===e&&s++),t.id=s++};return n(t),t}function $r(e,t={}){const{blocks:s}=t;if("string"==typeof e)return mn(e,s?{blocks:e=>s(e,t.scope)}:{});if(Array.isArray(e))return q(e);if(e&&"object"==typeof e&&e.type){if(void 0!==e.id)return e;const t=structuredClone(e);return N(t,e=>{"op"!==e.type&&void 0!==e.value&&(e.value=String(e.value))}),mr(t)}throw new Error("Inline Math needs an expression, standard postfix or a tree")}function xr(e,t={}){const s=$r(e,t);return nr(s,yr(s,t)).html}function yr(e,t){const{colors:s,highlight:n,scope:r,blocks:i,...o}=t,a=new Map;for(const[t,n]of function(e){return e?Array.isArray(e)?e:Object.entries(e).map(([e,t])=>[/^\d+$/.test(e)?Number(e):e,t]):[]}(s))if(br(n))for(const s of pr(e,t))a.set(s,`color:${n}`);const l=new Set(void 0===n?[]:pr(e,n));if(!a.size&&!l.size)return o;const c=o.prefix||"mv";return{...o,style:e=>[a.get(e),o.style&&o.style(e)].filter(Boolean).join(";"),attributes:e=>{const t={...o.attributes?o.attributes(e):{}};return l.has(e)&&(t.class=[t.class,`${c}-hl`].filter(Boolean).join(" ")),t}}}function br(e){return"string"==typeof e&&/^[#\w\s.,%()/+-]+$/.test(e)&&!/url\s*\(|expression/i.test(e)}export{un as A,ds as B,Bt as C,Kn as D,ms as E,i as F,ss as G,ls as H,c as I,Ut as J,us as K,Rt as L,gs as M,It as N,ts as O,zn as P,Ht as Q,xn as R,gn as S,o as T,q as U,yr as a,qn as b,ur as c,mr as d,xr as e,$r as f,hr as g,L as h,S as i,$n as j,Wt as k,nr as l,fs as m,dr as n,Ft as o,gr as p,rs as q,Pt as r,pr as s,Sn as t,os as u,is as v,as as w,mn as x,z as y,N as z};