-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.html
More file actions
299 lines (296 loc) · 82.5 KB
/
Copy pathindex.html
File metadata and controls
299 lines (296 loc) · 82.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
<!doctype html>
<html lang="en" dir="ltr" class="docs-wrapper plugin-docs plugin-id-default docs-version-current docs-doc-page docs-doc-id-compute-engine/guide-evaluate" data-has-hydrated="false">
<head>
<meta charset="UTF-8">
<meta name="generator" content="Docusaurus v3.10.2">
<title data-rh="true">Evaluation of Expressions · MathLive</title><meta data-rh="true" name="viewport" content="width=device-width,initial-scale=1"><meta data-rh="true" name="twitter:card" content="summary_large_image"><meta data-rh="true" property="og:image" content="https://mathlive.io/img/social-card.jpg"><meta data-rh="true" name="twitter:image" content="https://mathlive.io/img/social-card.jpg"><meta data-rh="true" property="og:url" content="https://mathlive.io/compute-engine/guides/evaluate/"><meta data-rh="true" property="og:locale" content="en"><meta data-rh="true" name="docusaurus_locale" content="en"><meta data-rh="true" name="docsearch:language" content="en"><meta data-rh="true" name="docusaurus_version" content="current"><meta data-rh="true" name="docusaurus_tag" content="docs-default-current"><meta data-rh="true" name="docsearch:version" content="current"><meta data-rh="true" name="docsearch:docusaurus_tag" content="docs-default-current"><meta data-rh="true" property="og:title" content="Evaluation of Expressions · MathLive"><meta data-rh="true" name="description" content="Evaluating an expression is the process of determining the value of the"><meta data-rh="true" property="og:description" content="Evaluating an expression is the process of determining the value of the"><link data-rh="true" rel="icon" href="/img/logo.webp"><link data-rh="true" rel="canonical" href="https://mathlive.io/compute-engine/guides/evaluate/"><link data-rh="true" rel="alternate" href="https://mathlive.io/compute-engine/guides/evaluate/" hreflang="en"><link data-rh="true" rel="alternate" href="https://mathlive.io/compute-engine/guides/evaluate/" hreflang="x-default"><link data-rh="true" rel="preconnect" href="https://Q23Y5RN3UQ-dsn.algolia.net" crossorigin="anonymous"><link data-rh="true" rel="alternate" type="text/markdown" href="/compute-engine/guides/evaluate.md"><script data-rh="true" type="application/ld+json">{"@context":"https://schema.org","@type":"BreadcrumbList","itemListElement":[{"@type":"ListItem","position":1,"name":"Evaluation of Expressions","item":"https://mathlive.io/compute-engine/guides/evaluate/"}]}</script><link rel="search" type="application/opensearchdescription+xml" title="MathLive" href="/opensearch.xml">
<link rel="stylesheet" src="https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.11/codemirror.min.css">
<script>!function(){var e,t;window.moduleMap||(window.moduleMap={}),window.moduleMap={...window.moduleMap,mathlive:"https://esm.run/mathlive","compute-engine":"https://esm.run/@cortex-js/compute-engine","@cortex-js/compute-engine":"https://esm.run/@cortex-js/compute-engine"},(e=["https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/codemirror.min.js","https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/javascript/javascript.min.js","https://cdnjs.cloudflare.com/ajax/libs/codemirror/5.65.16/mode/xml/xml.min.js","https://unpkg.com/@cortex-js/compute-engine","https://cdn.jsdelivr.net/npm/mathlive","https://cdn.jsdelivr.net/npm/@ui-js/code-playground@1.13.2/dist/code-playground.min.js@module"]).forEach((o,n)=>{const m=o.endsWith("@module"),c="script"+function(e,t=0){let o=3735928559^t,n=1103547991^t;for(let t=0;t<e.length;t++){const m=e.charCodeAt(t);o=Math.imul(o^m,2654435761),n=Math.imul(n^m,1597334677)}return o=Math.imul(o^o>>>16,2246822507),o^=Math.imul(n^n>>>13,3266489909),n=Math.imul(n^n>>>16,2246822507),n^=Math.imul(o^o>>>13,3266489909),4294967296*(2097151&n)+(o>>>0)}(o=o.replace("@module","")).toString(36);if(document.getElementById(c))return;const d=document.createElement("script");m&&(d.type="module"),d.id=c,d.src=o,d.async=!1,t&&n===e.length-1&&(d.onload=t),document.head.appendChild(d)})}()</script>
<link rel="preconnect" href="https://rsms.me/">
<link rel="stylesheet" href="https://rsms.me/inter/inter.css" media="print" onload="this.media='all'">
<link rel="preload" as="font" type="font/woff2" href="/fonts/berkeley-mono/BerkeleyMono-Regular.woff2" crossorigin="anonymous">
<link rel="preload" as="font" type="font/woff2" href="/fonts/berkeley-mono/BerkeleyMono-Bold.woff2" crossorigin="anonymous">
<link rel="preload" as="font" type="font/woff2" href="https://cdn.jsdelivr.net/npm/mathlive/fonts/KaTeX_Main-Regular.woff2" crossorigin="anonymous">
<link rel="preload" as="font" type="font/woff2" href="https://cdn.jsdelivr.net/npm/mathlive/fonts/KaTeX_Math-Italic.woff2" crossorigin="anonymous">
<link rel="preload" as="font" type="font/woff2" href="https://cdn.jsdelivr.net/npm/mathlive/fonts/KaTeX_AMS-Regular.woff2" crossorigin="anonymous">
<link rel="preload" as="font" type="font/woff2" href="https://cdn.jsdelivr.net/npm/mathlive/fonts/KaTeX_Size1-Regular.woff2" crossorigin="anonymous">
<link rel="preload" as="image" href="/img/hand-slugs.jpg" fetchpriority="high">
<link rel="preload" as="image" href="/img/hand-gears.jpg" fetchpriority="high">
<link rel="preload" as="image" href="/img/hand-cube2.jpg" fetchpriority="high"><link rel="stylesheet" href="/assets/css/styles.f0919c7c.css">
<script src="/assets/js/runtime~main.868c8a5e.js" defer="defer"></script>
<script src="/assets/js/main.8b66a134.js" defer="defer"></script>
</head>
<body>
<svg style="display: none;"><defs>
<symbol id="theme-svg-external-link" viewBox="0 0 24 24"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"/></symbol>
</defs></svg>
<script>document.documentElement.setAttribute("data-theme",window.matchMedia("(prefers-color-scheme: dark)").matches?"dark":"light"),document.documentElement.setAttribute("data-theme-choice","system"),function(){try{const c=new URLSearchParams(window.location.search).entries();for(var[t,e]of c)if(t.startsWith("docusaurus-data-")){var a=t.replace("docusaurus-data-","data-");document.documentElement.setAttribute(a,e)}}catch(t){}}()</script><div id="__docusaurus"><link rel="preload" as="image" href="/img/logo.webp"><div role="region" aria-label="Skip to main content"><a class="skipToContent_fXgn" href="#__docusaurus_skipToContent_fallback">Skip to main content</a></div><div class="shared-header"><nav aria-label="Main" class="theme-layout-navbar navbar navbar--fixed-top"><div class="navbar__inner"><div class="theme-layout-navbar-left navbar__items"><button aria-label="Toggle navigation bar" aria-expanded="false" class="navbar__toggle clean-btn" type="button"><svg width="30" height="30" viewBox="0 0 30 30" aria-hidden="true"><path stroke="currentColor" stroke-linecap="round" stroke-miterlimit="10" stroke-width="2" d="M4 7h22M4 15h22M4 23h22"></path></svg></button><a class="navbar__brand" href="/"><div class="navbar__logo"><img src="/img/logo.webp" alt="MathLive Logo" class="themedComponent_mlkZ themedComponent--light_NVdE" height="42" width="42"><img src="/img/logo.webp" alt="MathLive Logo" class="themedComponent_mlkZ themedComponent--dark_xIcU" height="42" width="42"></div><b class="navbar__title text--truncate">MathLive</b></a><a aria-current="page" class="navbar__item navbar__link navbar__link--active" href="/mathfield/">Mathfield</a><a aria-current="page" class="navbar__item navbar__link navbar__link--active" href="/compute-engine/">Compute Engine</a><a href="https://epsil.dev" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link">Epsil<svg width="13.5" height="13.5" aria-label="(opens in new tab)" class="iconExternalLink_nPIU"><use href="#theme-svg-external-link"></use></svg></a></div><div class="theme-layout-navbar-right navbar__items navbar__items--right"><a href="https://github.com/cortex-js" target="_blank" rel="noopener noreferrer" class="navbar__item navbar__link" icon="github">GitHub<svg width="13.5" height="13.5" aria-label="(opens in new tab)" class="iconExternalLink_nPIU"><use href="#theme-svg-external-link"></use></svg></a><div class="navbarSearchContainer_Bca1"><button type="button" class="DocSearch DocSearch-Button" aria-label="Search (Meta+k)" aria-keyshortcuts="Meta+k"><span class="DocSearch-Button-Container"><svg width="20" height="20" class="DocSearch-Search-Icon" viewBox="0 0 24 24" aria-hidden="true"><circle cx="11" cy="11" r="8" stroke="currentColor" fill="none" stroke-width="1.4"></circle><path d="m21 21-4.3-4.3" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"></path></svg><span class="DocSearch-Button-Placeholder">Search</span></span><span class="DocSearch-Button-Keys"></span></button></div></div></div><div role="presentation" class="navbar-sidebar__backdrop"></div></nav></div><div id="__docusaurus_skipToContent_fallback" class="theme-layout-main main-wrapper mainWrapper_z2l0"><div class="docsWrapper_hBAB"><button aria-label="Scroll back to top" class="clean-btn theme-back-to-top-button backToTopButton_sjWU" type="button"></button><div class="docRoot_UBD9"><aside class="theme-doc-sidebar-container docSidebarContainer_YfHR"><div class="sidebarViewport_aRkj"><div class="sidebar_njMd"><nav aria-label="Docs sidebar" class="menu thin-scrollbar menu_SIkG"><ul class="theme-doc-sidebar-menu menu__list"><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="categoryLink_byQd menu__link menu__link--sublist menu__link--sublist-caret" role="button" aria-expanded="false" href="/mathfield/"><span class="categoryLinkLabel_W154">Mathfield</span></a></div></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="categoryLink_byQd menu__link menu__link--sublist menu__link--sublist-caret menu__link--active" role="button" aria-expanded="true" href="/compute-engine/"><span class="categoryLinkLabel_W154">Compute Engine</span></a></div><ul class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item compass-icon"><a class="menu__link" tabindex="0" href="/compute-engine/"><span class="linkLabel_WmDU">Introduction</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item flask-icon"><a class="menu__link" tabindex="0" href="/compute-engine/demo/"><span class="linkLabel_WmDU">Demo</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2"><hr></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item changelog-icon"><a class="menu__link" tabindex="0" href="/compute-engine/changelog/"><span class="linkLabel_WmDU">Changelog</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2"><hr></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/expressions/"><span class="linkLabel_WmDU">Expressions</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link menu__link--active" aria-current="page" tabindex="0" href="/compute-engine/guides/evaluate/"><span class="linkLabel_WmDU">Evaluation of Expressions</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/symbolic-computing/"><span class="linkLabel_WmDU">Symbolic Computing</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/numeric-evaluation/"><span class="linkLabel_WmDU">Numeric Evaluation</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/canonical-form/"><span class="linkLabel_WmDU">Canonical Form</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/symbols/"><span class="linkLabel_WmDU">Symbols</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/augmenting/"><span class="linkLabel_WmDU">Custom Functions and Symbols</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/latex-syntax/"><span class="linkLabel_WmDU">LaTeX Syntax</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/types/"><span class="linkLabel_WmDU">Types</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/units/"><span class="linkLabel_WmDU">Units and Quantities</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/simplify/"><span class="linkLabel_WmDU">Simplify</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/compiling/"><span class="linkLabel_WmDU">Compiling Expressions</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/python-target/"><span class="linkLabel_WmDU">Python/NumPy Target</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/assumptions/"><span class="linkLabel_WmDU">Assumptions</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/patterns-and-rules/"><span class="linkLabel_WmDU">Patterns and Rules</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/identities/"><span class="linkLabel_WmDU">Extended Rules — The Identities Library</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/logic/"><span class="linkLabel_WmDU">Logic and First-Order Logic</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item guide-icon"><a class="menu__link" tabindex="0" href="/compute-engine/guides/linear-algebra/"><span class="linkLabel_WmDU">Linear Algebra</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2"><hr></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item sdk-icon"><a class="menu__link" tabindex="0" href="/math-json/"><span class="linkLabel_WmDU">MathJSON Format</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item sdk-icon"><a class="menu__link" tabindex="0" href="/compute-engine/api/"><span class="linkLabel_WmDU">API Reference</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2"><hr></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item compass-icon"><a class="menu__link" tabindex="0" href="/compute-engine/standard-library/"><span class="linkLabel_WmDU">MathJSON Standard Library</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/arithmetic/"><span class="linkLabel_WmDU">Arithmetic</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/calculus/"><span class="linkLabel_WmDU">Calculus</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/collections/"><span class="linkLabel_WmDU">Collections</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/colors/"><span class="linkLabel_WmDU">Colors</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/complex/"><span class="linkLabel_WmDU">Complex</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/control-structures/"><span class="linkLabel_WmDU">Control Structures</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/core/"><span class="linkLabel_WmDU">Core</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/functions/"><span class="linkLabel_WmDU">Functions</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/linear-algebra/"><span class="linkLabel_WmDU">Linear Algebra</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/logic/"><span class="linkLabel_WmDU">Logic</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/sets/"><span class="linkLabel_WmDU">Sets</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/special-functions/"><span class="linkLabel_WmDU">Special Functions</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/statistics/"><span class="linkLabel_WmDU">Statistics</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/combinatorics/"><span class="linkLabel_WmDU">Combinatorics</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/number-theory/"><span class="linkLabel_WmDU">Number Theory</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/strings/"><span class="linkLabel_WmDU">Strings and Text</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/trigonometry/"><span class="linkLabel_WmDU">Trigonometry</span></a></li><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item reference-icon"><a class="menu__link" tabindex="0" href="/compute-engine/reference/units/"><span class="linkLabel_WmDU">Units and Quantities</span></a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item"><div class="menu__list-item-collapsible"><a class="categoryLink_byQd menu__link"><span class="categoryLinkLabel_W154">Tutorials</span></a></div><ul class="menu__list"><li class="theme-doc-sidebar-item-link theme-doc-sidebar-item-link-level-2 menu__list-item tutorial-icon"><a class="menu__link" tabindex="0" href="/tutorials/simple-quiz/"><span class="linkLabel_WmDU">Simple Quiz</span></a></li></ul></li><li class="theme-doc-sidebar-item-category theme-doc-sidebar-item-category-level-1 menu__list-item menu__list-item--collapsed"><div class="menu__list-item-collapsible"><a class="categoryLink_byQd menu__link menu__link--sublist menu__link--sublist-caret" role="button" aria-expanded="false" href="/about/"><span class="categoryLinkLabel_W154">Additional Resources</span></a></div></li></ul></nav><button type="button" title="Collapse sidebar" aria-label="Collapse sidebar" class="button button--secondary button--outline collapseSidebarButton_PEFL"><svg width="20" height="20" aria-hidden="true" class="collapseSidebarButtonIcon_kv0_"><g fill="#7a7a7a"><path d="M9.992 10.023c0 .2-.062.399-.172.547l-4.996 7.492a.982.982 0 01-.828.454H1c-.55 0-1-.453-1-1 0-.2.059-.403.168-.551l4.629-6.942L.168 3.078A.939.939 0 010 2.528c0-.548.45-.997 1-.997h2.996c.352 0 .649.18.828.45L9.82 9.472c.11.148.172.347.172.55zm0 0"></path><path d="M19.98 10.023c0 .2-.058.399-.168.547l-4.996 7.492a.987.987 0 01-.828.454h-3c-.547 0-.996-.453-.996-1 0-.2.059-.403.168-.551l4.625-6.942-4.625-6.945a.939.939 0 01-.168-.55 1 1 0 01.996-.997h3c.348 0 .649.18.828.45l4.996 7.492c.11.148.168.347.168.55zm0 0"></path></g></svg></button></div></div></aside><main class="docMainContainer_TBSr"><div class="container padding-top--md padding-bottom--lg"><div class="row"><div class="col docItemCol_VOVn"><div class="docItemContainer_Djhp"><article><nav class="theme-doc-breadcrumbs breadcrumbsContainer_Z_bl" aria-label="Breadcrumbs"><ul class="breadcrumbs"><li class="breadcrumbs__item"><a aria-label="Home page" class="breadcrumbs__link" href="/"><svg viewBox="0 0 24 24" class="breadcrumbHomeIcon_YNFT"><path d="M10 19v-5h4v5c0 .55.45 1 1 1h3c.55 0 1-.45 1-1v-7h1.7c.46 0 .68-.57.33-.87L12.67 3.6c-.38-.34-.96-.34-1.34 0l-8.36 7.53c-.34.3-.13.87.33.87H5v7c0 .55.45 1 1 1h3c.55 0 1-.45 1-1z" fill="currentColor"></path></svg></a></li><li class="breadcrumbs__item"><span class="breadcrumbs__link">Compute Engine</span></li><li class="breadcrumbs__item breadcrumbs__item--active"><span class="breadcrumbs__link">Evaluation of Expressions</span></li></ul></nav><div class="tocCollapsible_ETCw theme-doc-toc-mobile tocMobile_ITEo"><button type="button" class="clean-btn tocCollapsibleButton_TO0P">On this page</button></div><div class="theme-doc-markdown markdown"><header><h1>Evaluation</h1></header>
<div class="intro_b9yo"><p>Evaluating an expression is the process of determining the value of the
expression. This involves looking up the definitions of symbols and functions,
evaluating the arguments of functions, and applying the function to the
arguments.</p></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="evaluation-methods">Evaluation Methods<a href="#evaluation-methods" class="hash-link" aria-label="Direct link to Evaluation Methods" title="Direct link to Evaluation Methods" translate="no"></a></h2>
<p><strong>To evaluate an expression</strong>, use the <code>evaluate()</code> function.</p>
<code-playground><div slot="javascript">// String expression
<!-- -->console.log(evaluate("3^2"));
<!-- -->
<!-- -->// Expression object
<!-- -->const expr = parse("2 + 2");
<!-- -->console.log(evaluate(expr));
</div></code-playground>
<div class="theme-admonition theme-admonition-info admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>Note</div><div class="admonitionContent_BuS1"><p>The <code>expr.value</code> property does not evaluate the expression. If the expression
is a literal, it returns the literal value. If the expression is a symbol, it
looks up the symbol in the current scope and returns its value.</p></div></div>
<code-playground><div slot="javascript">parse("x").value = 314;
<!-- -->console.info(parse('42').value)
<!-- -->console.info(parse('x').value)
<!-- -->console.info(parse('2 + 2').value)
</div></code-playground>
<p>The <code>N()</code> function provides a numeric evaluation of its argument.</p>
<code-playground><div slot="javascript">console.log(evaluate("2\\pi"));
<!-- -->console.log(N("2\\pi"));
</div></code-playground>
<div class="theme-admonition theme-admonition-info admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>Note</div><div class="admonitionContent_BuS1"><p>The <code>N()</code> and <code>evaluate()</code> functions are shorthands for the <code>expr.N()</code> and
<code>expr.evaluate()</code> methods. They use a common shared compute engine instance.</p><p>The <code>expr.N()</code> method is a shorthand for <code>expr.evaluate({numericApproximation: true})</code>.</p></div></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="compilation">Compilation<a href="#compilation" class="hash-link" aria-label="Direct link to Compilation" title="Direct link to Compilation" translate="no"></a></h3>
<p>An expression can be evaluated by compiling it to JavaScript using the <code>compile()</code> function.
The result includes a <code>run</code> function that can be called to evaluate the expression.</p>
<code-playground><div slot="javascript">// import { compile } from '@cortex-js/compute-engine';
<!-- -->const result = compile('2\\pi');
<!-- -->console.log(result.success ? 'compiled' : 'fallback');
<!-- -->console.log(result.run?.());
</div></code-playground>
<div class="readMore_KWn8"><a href="/compute-engine/guides/compiling/"><p>Read more about <strong>compiling expressions</strong> <svg class="icon_qj0s chevronRightBold_bbMj"><use xlink:href="/icons.svg#chevron-right-bold"></use></svg></p></a></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="asynchronous-evaluation">Asynchronous Evaluation<a href="#asynchronous-evaluation" class="hash-link" aria-label="Direct link to Asynchronous Evaluation" title="Direct link to Asynchronous Evaluation" translate="no"></a></h2>
<p>Some computations can be time-consuming. For example, computing a very large
factorial. To prevent the browser from freezing, the Compute Engine can
perform some operations asynchronously.</p>
<p><strong>To perform an asynchronous evaluation</strong>, use the <code>expr.evaluateAsync()</code> method.</p>
<div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-js codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token keyword" style="color:var(--base-0e)">try</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> fact </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> </span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'(70!)!'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> factResult </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">await</span><span class="token plain"> fact</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluateAsync</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">factResult</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token punctuation" style="color:var(--base-06)">}</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">catch</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">e</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">error</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">e</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token punctuation" style="color:var(--base-06)">}</span><br></div></code></pre></div></div>
<p>The <code>expr.evaluateAsync()</code> method returns a <code>Promise</code> that resolves to the result
of the evaluation. It accepts the same <code>numericApproximation</code> options as <code>expr.evaluate()</code>.</p>
<p>It is also possible to interrupt an evaluation, for example by providing the user
with a pause/cancel button.</p>
<p><strong>To make an evaluation interruptible</strong>, use an <code>AbortController</code>
object and a <code>signal</code>.</p>
<p>For example, to interrupt an evaluation after 500ms:</p>
<div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-js codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> abort </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">new</span><span class="token plain"> </span><span class="token class-name" style="color:var(--base-0a)">AbortController</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> signal </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> abort</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token plain">signal</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token function" style="color:var(--base-0a)">setTimeout</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token plain"> </span><span class="token operator" style="color:var(--base-06)">=></span><span class="token plain"> abort</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">abort</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">500</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token keyword" style="color:var(--base-0e)">try</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> fact </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> </span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'(70!)!'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> factResult </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">await</span><span class="token plain"> fact</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluateAsync</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"> signal </span><span class="token punctuation" style="color:var(--base-06)">}</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">factResult</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token punctuation" style="color:var(--base-06)">}</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">catch</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">e</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">error</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">e</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token punctuation" style="color:var(--base-06)">}</span><br></div></code></pre></div></div>
<code-playground><div slot="html"><div class="stack">
<!-- --> <!-- --><div class="row">
<!-- --> <!-- --><button id="evaluate-button">Evaluate</button>
<!-- --> <!-- --><button id="cancel-button" disabled>Cancel</button>
<!-- --> <!-- --></div>
<!-- -->
<!-- --> <!-- --><div id="output"></div>
<!-- --></div>
</div><div slot="javascript">const abort = new AbortController();
<!-- -->
<!-- -->document.getElementById('cancel-button').addEventListener('click',
<!-- --> <!-- -->() => abort.abort()
<!-- -->);
<!-- -->
<!-- -->document.getElementById('evaluate-button').addEventListener('click', async () => {
<!-- --> <!-- -->try {
<!-- --> <!-- -->document.getElementById('evaluate-button').disabled = true;
<!-- --> <!-- -->document.getElementById('cancel-button').disabled = false;
<!-- -->
<!-- --> <!-- -->const fact = ce.parse('(70!)!');
<!-- --> <!-- -->const factResult = await fact.evaluateAsync({ signal: abort.signal });
<!-- --> <!-- -->document.getElementById('output').textContent = factResult.toString();
<!-- --> <!-- -->
<!-- --> <!-- -->document.getElementById('evaluate-button').disabled = false;
<!-- --> <!-- -->document.getElementById('cancel-button').disabled = true;
<!-- --> <!-- -->} catch (e) {
<!-- --> <!-- -->document.getElementById('evaluate-button').disabled = false;
<!-- --> <!-- -->document.getElementById('cancel-button').disabled = true;
<!-- --> <!-- -->console.error(e);
<!-- --> <!-- -->}
<!-- -->});
<!-- -->
</div></code-playground>
<p><strong>To set a time limit for an operation</strong>, wrap it in a span with
<code>ce.withTimeLimit(limit, fn)</code>. The <code>limit</code> is either a number of milliseconds
or an object <code>{ ms, label }</code>; everything evaluated inside the callback shares
one deadline.</p>
<div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-js codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token keyword" style="color:var(--base-0e)">try</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> fact </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'(70!)!'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">const</span><span class="token plain"> result </span><span class="token operator" style="color:var(--base-06)">=</span><span class="token plain"> ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">withTimeLimit</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"> </span><span class="token literal-property property" style="color:var(--base-0c)">ms</span><span class="token operator" style="color:var(--base-06)">:</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">1000</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token literal-property property" style="color:var(--base-0c)">label</span><span class="token operator" style="color:var(--base-06)">:</span><span class="token plain"> </span><span class="token string" style="color:var(--base-0b)">'my-app:eval'</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">}</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token plain"> </span><span class="token operator" style="color:var(--base-06)">=></span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> fact</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">result</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token punctuation" style="color:var(--base-06)">}</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">catch</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">e</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"> console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">error</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">e</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token punctuation" style="color:var(--base-06)">}</span><br></div></code></pre></div></div>
<p>Spans nest by taking the tighter deadline, and a <code>CancellationError</code> reports
which span's budget expired through its <code>attribution</code> and <code>spans</code> fields. See
the <a class="" href="/compute-engine/guides/execution-constraints/">Execution Constraints</a>
guide for the full model, including the iteration and recursion budgets.
(The former <code>ce.timeLimit</code> property has been removed — spans are the only
way to arm a deadline.)</p>
<p>When an operation is canceled either because of a timeout or an abort, a
<code>CancellationError</code> is thrown. The class can be imported from the package
to distinguish cancellations from other errors:</p>
<div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-js codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token keyword" style="color:var(--base-0e)">import</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">{</span><span class="token plain"> CancellationError </span><span class="token punctuation" style="color:var(--base-06)">}</span><span class="token plain"> </span><span class="token keyword" style="color:var(--base-0e)">from</span><span class="token plain"> </span><span class="token string" style="color:var(--base-0b)">"@cortex-js/compute-engine"</span><span class="token punctuation" style="color:var(--base-06)">;</span><br></div></code></pre></div></div>
<p>The time limit is respected by long-running operations including iteration
over large or infinite collections, big operators (<code>Sum</code>, <code>Product</code>,
<code>Reduce</code>), number-theoretic functions (<code>Factorial</code>, <code>Totient</code>, <code>Sigma0</code>…),
and numeric limit extraction (<code>Limit</code>, <code>NLimit</code>).</p>
<p>Numerical integration is an exception: rather than throwing, Monte Carlo
integration (<code>NIntegrate</code>) returns the estimate computed from the samples
taken so far, with a correspondingly larger error bound.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="errors">Errors<a href="#errors" class="hash-link" aria-label="Direct link to Errors" title="Direct link to Errors" translate="no"></a></h2>
<p>An <a class="" href="/compute-engine/reference/core/#Error"><code>["Error"]</code></a> is a <strong>value</strong>, and it
propagates along the ordinary value path. If an operand that a function
evaluates strictly turns out to be an error, the whole expression evaluates to
that error — not to a frozen expression wrapped around it. The propagated
error carries an <code>["ErrorTrace"]</code> breadcrumb recording the operators it passed
through, so the failure site is still recoverable.</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-ts codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token builtin" style="color:var(--base-09)">console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'\\ln(\\text{a}) + 2'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token plain">json</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ➔ ["Error",</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ["ErrorCode", "'incompatible-type'", "'number'", "'string'"],</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ["ErrorTrace", ["ErrorFrame", "'Ln'", 1], ["ErrorFrame", "'Add'", 1]]]</span><br></div></code></pre></div></div>
<p>A <strong>collection</strong> is the exception: an error among its elements stays in place,
because a collection containing an error is still a well-formed collection.</p>
<p>An operand that is never evaluated cannot propagate anything. In a selection —
<code>["If"]</code>, <code>["Which"]</code> — or behind a short-circuit operator — <code>["And"]</code>,
<code>["Or"]</code> — the branch that is not taken is <strong>dead code</strong>, and an error inside it
never reaches the value:</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-ts codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token builtin" style="color:var(--base-09)">console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">box</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">[</span><span class="token string" style="color:var(--base-0b)">'If'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token string" style="color:var(--base-0b)">'True'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">5</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token punctuation" style="color:var(--base-06)">[</span><span class="token string" style="color:var(--base-0b)">'Divide'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token string" style="color:var(--base-0b)">'x'</span><span class="token punctuation" style="color:var(--base-06)">]</span><span class="token punctuation" style="color:var(--base-06)">]</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token plain">json</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ➔ 5</span><br></div></code></pre></div></div>
<p>The diagnostic is not lost, only unevaluated: the boxed expression still holds
<code>["If", "True", 5, ["Divide", "x", ["Error", "'missing'"]]]</code>, so a tool that
walks the expression still reports it.</p>
<p>Errors do not spread past the tools that inspect them. <code>Type</code> reports
<code>"error"</code>, <a class="" href="/compute-engine/reference/core/#IsError"><code>IsError</code></a> answers
<code>True</code>/<code>False</code>, and
<a class="" href="/compute-engine/reference/control-structures/#Match"><code>Match</code></a> decides on an
error subject — an <code>["Error", ...]</code> case destructures it, which is how a
failure is rescued.</p>
<p><code>NaN</code> is <strong>not</strong> an error. It is an ordinary IEEE numeric value — it has its
own type, <code>nan</code>, under <code>number</code> — so it does not propagate this way: a
function applied to <code>NaN</code> runs and receives it, and is free to inspect it.</p>
<p><code>NaN</code> is also <strong>inexact</strong>, so the exactness contract sends it down the numeric
branch: a numeric function of <code>NaN</code> numericizes under plain <code>evaluate()</code>,
without waiting for <code>.N()</code>, and the result is <code>NaN</code>.</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-ts codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token builtin" style="color:var(--base-09)">console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">box</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">[</span><span class="token string" style="color:var(--base-0b)">'Sin'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">NaN</span><span class="token punctuation" style="color:var(--base-06)">]</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token plain">json</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ➔ "NaN"</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token builtin" style="color:var(--base-09)">console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'\\ln(2)'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token plain">json</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ➔ ["Ln", 2] — an exact argument stays symbolic</span><br></div></code></pre></div></div>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="lexical-scopes-and-evaluation-contexts">Lexical Scopes and Evaluation Contexts<a href="#lexical-scopes-and-evaluation-contexts" class="hash-link" aria-label="Direct link to Lexical Scopes and Evaluation Contexts" title="Direct link to Lexical Scopes and Evaluation Contexts" translate="no"></a></h2>
<p>The Compute Engine supports
<a href="https://en.wikipedia.org/wiki/Scope_(computer_science)" target="_blank" rel="noopener noreferrer" class="">lexical scoping</a>.</p>
<p>A <strong>lexical scope</strong> is a region of the code where a symbol is defined.
Each scope has its own bindings table, which is a
mapping of symbols to their definitions. The definition includes the type
of the symbol, whether it is a constant and other properties.</p>
<p>An <strong>evaluation context</strong> is a snapshot of the current state of the Compute
Engine. It includes the values of all symbols currently in scope and
a chain of lexical scopes.</p>
<p>Evaluation contexts are arranged in a stack, with the current (top-most)
evaluation context available with <code>ce.context</code>.</p>
<p>Evaluation contexts are created automatically, for example when a new scope is
created, or each time a recursive function is called.</p>
<p>Some functions may create their own scope. These functions have the <code>scoped</code>
flag set to <code>true</code>. For example, the <code>Block</code> function creates a new scope
when it is called: any declarations made in the block are only visible within the
block. Similarly, the <code>Sum</code> function creates a new scope so that the index
variable is not visible outside the sum.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="binding">Binding<a href="#binding" class="hash-link" aria-label="Direct link to Binding" title="Direct link to Binding" translate="no"></a></h3>
<p><strong><a href="https://en.wikipedia.org/wiki/Name_binding" target="_blank" rel="noopener noreferrer" class="">Name Binding</a> is the process of
associating a symbol with a definition.</strong></p>
<p>Name Binding should not be confused with <strong>value binding</strong> which is the process
of associating a <strong>value</strong> to a symbol.</p>
<p>To bind a symbol to a definition, the Compute Engine looks up the bindings table
of the current scope. If the symbol is not found in the table, the parent
scope is searched, and so on until a definition is found.</p>
<p>If no definition is found, the symbol is declared with a type of
<code>unknown</code>, or a more specific type if the context allows it.</p>
<p>If the symbol is found, the definition record is used to evaluate the
expression.</p>
<p>The definition record contains information about the symbol, such as its
type, whether it is a constant, and how to evaluate it.</p>
<p>There are two kind of definition records:</p>
<ol>
<li class=""><strong>Value Definition Record</strong>: This record contains information about the
symbol, such as its type and whether it is a constant.</li>
<li class=""><strong>Operator Definition Record</strong>: This record contains information about the
operator, such as its signature and how to evaluate it.</li>
</ol>
<p>Name binding is done during canonicalization. If name binding failed, the
<code>isValid</code> property of the expression is <code>false</code>.</p>
<p><strong>To get a list of the errors in an expression</strong> use the <code>expr.errors</code> property.</p>
<div class="readMore_KWn8"><a href="/compute-engine/guides/expressions/#errors"> Read more about the
<strong>errors</strong> <svg class="icon_qj0s chevronRightBold_bbMj"><use xlink:href="/icons.svg#chevron-right-bold"></use></svg></a></div>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="bound-variables-and-assigned-values">Bound Variables and Assigned Values<a href="#bound-variables-and-assigned-values" class="hash-link" aria-label="Direct link to Bound Variables and Assigned Values" title="Direct link to Bound Variables and Assigned Values" translate="no"></a></h3>
<p>Some operators <strong>bind</strong> a variable of their own: <code>D</code>, <code>Integrate</code>, <code>Limit</code>,
<code>Sum</code>, <code>Product</code>, <code>Solve</code>, and <code>Function</code> (its parameters), among others. The
variable such an operator binds is a <strong>pure symbol</strong> — only its declared type
and any in-scope assumptions apply. A <strong>value</strong> assigned to a same-named symbol
does <strong>not</strong> apply to it, and the variable stays symbolic in the result.</p>
<div class="language-js codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-js codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">assign</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'x'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">5</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'\\frac{d}{dx} x^2'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">print</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ➔ 2x (not 10 — the bound "x" ignores its assigned value)</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain" style="display:inline-block"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">parse</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'\\int x^2 \\,dx'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">evaluate</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">print</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token comment" style="color:var(--base-05)">// ➔ x^3/3 (not 125/3)</span><br></div></code></pre></div></div>
<p>Every <strong>other</strong> symbol in the expression is <strong>free</strong>: its value always applies
under evaluation. So with <code>a := 3</code> as well, <code>\frac{d}{dx}(a\,x^2)</code> is <code>6x</code> —
the free <code>a</code> resolves, the bound <code>x</code> does not.</p>
<p>To use an assumption instead of a value (which <em>does</em> inform simplification and
solving), use <code>ce.assume()</code> rather than <code>ce.assign()</code>.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="default-scopes">Default Scopes<a href="#default-scopes" class="hash-link" aria-label="Direct link to Default Scopes" title="Direct link to Default Scopes" translate="no"></a></h3>
<p>The Compute Engine has a set of default scopes that are used to look up
symbols. These scopes are created automatically when the Compute Engine
is initialized. The default scopes include the <strong>system</strong> scope, and the <strong>global</strong> scope.</p>
<p>The <strong>system</strong> scope contains the definitions of all the built-in functions and
operators. The <strong>global</strong> scope is initially empty, but can be used to
store user-defined functions and symbols.</p>
<p>The <strong>global</strong> scope is the default scope used when the Compute Engine is
initialized.</p>
<p>Additional scopes can be created using the <code>ce.pushScope()</code> method.</p>
<h3 class="anchor anchorTargetStickyNavbar_Vzrq" id="creating-new-scopes">Creating New Scopes<a href="#creating-new-scopes" class="hash-link" aria-label="Direct link to Creating New Scopes" title="Direct link to Creating New Scopes" translate="no"></a></h3>
<p><strong>To add a new scope</strong> use <code>ce.pushScope()</code>.</p>
<div class="language-ts codeBlockContainer_Ckt0 theme-code-block" style="--prism-color:var(--console-color);--prism-background-color:var(--console-background)"><div class="codeBlockContent_QJqH"><pre tabindex="0" class="prism-code language-ts codeBlock_qGQc thin-scrollbar" style="color:var(--console-color);background-color:var(--console-background)"><code class="codeBlockLines_p187"><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">assign</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'x'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">100</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"> </span><span class="token comment" style="color:var(--base-05)">// "x" is defined in the current scope</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">pushScope</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">assign</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'x'</span><span class="token punctuation" style="color:var(--base-06)">,</span><span class="token plain"> </span><span class="token number" style="color:var(--base-09)">500</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"> </span><span class="token comment" style="color:var(--base-05)">// "x" is defined in the new scope</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token builtin" style="color:var(--base-09)">console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">expr</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'x'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"> </span><span class="token comment" style="color:var(--base-05)">// 500</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">popScope</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"></span><br></div><div class="token-line" style="color:var(--console-color)"><span class="token plain"></span><span class="token builtin" style="color:var(--base-09)">console</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">log</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token plain">ce</span><span class="token punctuation" style="color:var(--base-06)">.</span><span class="token function" style="color:var(--base-0a)">expr</span><span class="token punctuation" style="color:var(--base-06)">(</span><span class="token string" style="color:var(--base-0b)">'x'</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">)</span><span class="token punctuation" style="color:var(--base-06)">;</span><span class="token plain"> </span><span class="token comment" style="color:var(--base-05)">// 100</span><br></div></code></pre></div></div>
<p><strong>To exit a scope</strong> use <code>ce.popScope()</code>.</p>
<p>This will invalidate any definitions associated with the scope, and restore the
symbol table from previous scopes that may have been shadowed by the current
scope.</p>
<h2 class="anchor anchorTargetStickyNavbar_Vzrq" id="evaluation-loop">Evaluation Loop<a href="#evaluation-loop" class="hash-link" aria-label="Direct link to Evaluation Loop" title="Direct link to Evaluation Loop" translate="no"></a></h2>
<div class="theme-admonition theme-admonition-info admonition_xJq3 alert alert--info"><div class="admonitionHeading_Gvgb"><span class="admonitionIcon_Rf37"><svg viewBox="0 0 14 16"><path fill-rule="evenodd" d="M7 2.3c3.14 0 5.7 2.56 5.7 5.7s-2.56 5.7-5.7 5.7A5.71 5.71 0 0 1 1.3 8c0-3.14 2.56-5.7 5.7-5.7zM7 1C3.14 1 0 4.14 0 8s3.14 7 7 7 7-3.14 7-7-3.14-7-7-7zm1 3H6v5h2V4zm0 6H6v2h2v-2z"></path></svg></span>info</div><div class="admonitionContent_BuS1"><p>This is an advanced topic. You don't need to know the details of how the
evaluation loop works, unless you're interested in extending the standard
library and providing your own function definitions.</p></div></div>
<p>Each symbol is <strong>bound</strong> to a definition within a <strong>lexical scope</strong> during
canonicalization. This usually happens when calling <code>ce.expr()</code> or <code>ce.parse()</code>,
or if accessing the <code>.canonical</code> property of a
non-canonical expression.</p>
<p>When a function is evaluated, the following steps are followed:</p>
<ol>
<li class="">
<p>If the expression is not canonical, it cannot be evaluated and an error is
thrown. The expression must be canonicalized first.</p>
</li>
<li class="">
<p>Each argument of the function are evaluated, left to right, unless the
function has a <code>lazy</code> flag. If the function is lazy, the arguments are not
evaluated.</p>
<ol>
<li class="">
<p>An argument can be <strong>held</strong>, in which case it is not evaluated. Held
arguments can be useful when you need to pass a symbolic expression to a
function. If it wasn't held, the result of evaluating the expression would
be used, not the symbolic expression.</p>
<p>Alternatively, using the <code>Hold</code> function will prevent its argument from
being evaluated. Conversely, the <code>ReleaseHold</code> function will force an
evaluation.</p>
</li>
<li class="">
<p>If an argument is a <code>["Sequence"]</code> expression, treat each argument of the
sequence expression as if it was an argument of the function. If the
sequence is empty, ignore the argument.</p>
</li>
</ol>
</li>
<li class="">
<p>If the function is associative, flatten its arguments as necessary. $$$1$$</p>
</li>
<li class="">
<p>Apply the function to the arguments</p>
</li>
<li class="">
<p>Return the result in canonical form.</p>
</li>
</ol></div><footer class="theme-doc-footer docusaurus-mt-lg"><div class="theme-doc-footer-edit-meta-row row"><div class="col"></div><div class="col lastUpdated_VsjB"></div></div></footer></article><nav class="docusaurus-mt-lg pagination-nav" aria-label="Docs pages"><a class="pagination-nav__link pagination-nav__link--prev" href="/compute-engine/guides/expressions/"><div class="pagination-nav__sublabel">Previous</div><div class="pagination-nav__label">Expressions</div></a><a class="pagination-nav__link pagination-nav__link--next" href="/compute-engine/guides/symbolic-computing/"><div class="pagination-nav__sublabel">Next</div><div class="pagination-nav__label">Symbolic Computing</div></a></nav></div></div><div class="col col--3"><div class="tableOfContents_bqdL thin-scrollbar theme-doc-toc-desktop"><ul class="table-of-contents table-of-contents__left-border"><li><a href="#evaluation-methods" class="table-of-contents__link toc-highlight">Evaluation Methods</a><ul><li><a href="#compilation" class="table-of-contents__link toc-highlight">Compilation</a></li></ul></li><li><a href="#asynchronous-evaluation" class="table-of-contents__link toc-highlight">Asynchronous Evaluation</a></li><li><a href="#errors" class="table-of-contents__link toc-highlight">Errors</a></li><li><a href="#lexical-scopes-and-evaluation-contexts" class="table-of-contents__link toc-highlight">Lexical Scopes and Evaluation Contexts</a><ul><li><a href="#binding" class="table-of-contents__link toc-highlight">Binding</a></li><li><a href="#bound-variables-and-assigned-values" class="table-of-contents__link toc-highlight">Bound Variables and Assigned Values</a></li><li><a href="#default-scopes" class="table-of-contents__link toc-highlight">Default Scopes</a></li><li><a href="#creating-new-scopes" class="table-of-contents__link toc-highlight">Creating New Scopes</a></li></ul></li><li><a href="#evaluation-loop" class="table-of-contents__link toc-highlight">Evaluation Loop</a></li></ul></div></div></div></div></main></div></div></div><div class="shared-footer"><footer class="theme-layout-footer footer footer--dark"><div class="container container-fluid"><div class="row footer__links"><div class="theme-layout-footer-column col footer__col"><div class="footer__title">SDKs</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/mathfield/">Mathfield</a></li><li class="footer__item"><a class="footer__link-item" href="/compute-engine/">Compute Engine</a></li><li class="footer__item"><a class="footer__link-item" href="/math-json/">MathJSON</a></li><li class="footer__item"><a href="https://epsil.dev" target="_blank" rel="noopener noreferrer" class="footer__link-item">Epsil<svg width="13.5" height="13.5" aria-label="(opens in new tab)" class="iconExternalLink_nPIU"><use href="#theme-svg-external-link"></use></svg></a></li></ul></div><div class="theme-layout-footer-column col footer__col"><div class="footer__title">Community</div><ul class="footer__items clean-list"><li class="footer__item"><a href="https://github.com/cortex-js" target="_blank" rel="noopener noreferrer" class="footer__link-item">
<svg class="icon-one-rem"><use role="none" xlink:href="/icons.svg#github"></use></svg>
GitHub
<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg>
</a></li><li class="footer__item"><a href="https://discord.gg/yhmvVeJ4Hd" target="_blank" rel="noopener noreferrer" class="footer__link-item">
<svg class="icon-one-rem"><use role="none" xlink:href="/icons.svg#discord"></use></svg>
Discord
<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg>
</a></li><li class="footer__item"><a href="https://chatgpt.com/g/g-8YgEfR7ig-mathlive-gpt" target="_blank" rel="noopener noreferrer" class="footer__link-item">
<svg class="icon-one-rem"><use role="none" xlink:href="/icons.svg#openai"></use></svg>
MathLive GPT
<svg width="13.5" height="13.5" aria-hidden="true" viewBox="0 0 24 24" class="iconExternalLink_nPIU"><path fill="currentColor" d="M21 13v10h-21v-19h12v2h-10v15h17v-8h2zm3-12h-10.988l4.035 4-6.977 7.07 2.828 2.828 6.977-7.07 4.125 4.172v-11z"></path></svg>
</a></li></ul></div><div class="theme-layout-footer-column col footer__col"><div class="footer__title">More</div><ul class="footer__items clean-list"><li class="footer__item"><a class="footer__link-item" href="/about/">About Us</a></li></ul></div></div></div></footer></div></div>
</body>
</html>