Skip to content

Commit 8751279

Browse files
committed
doc
1 parent 19ce347 commit 8751279

File tree

2 files changed

+100
-86
lines changed

2 files changed

+100
-86
lines changed

docs/compute-engine/01-introduction.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ To use the Compute Engine, you must write JavaScript or TypeScript code. This
2424
guide assumes familiarity with one of these programming languages.
2525
:::
2626

27-
<div style={{height:"2rem"}}></div>
27+
<div style={{height:"1rem"}}></div>
2828

2929
```live
3030
console.log("exp(i*pi) =", ce.parse("e^{i\\pi}").evaluate());
@@ -37,8 +37,8 @@ ce.box(["Expand", expr]).evaluate().print();
3737

3838

3939
```live
40-
const lhs = ce.parse("2x^2 + 3x + 1");
41-
const rhs = ce.parse("1 + 2x + x + 2x^2");
40+
const lhs = ce.parse("1 + x(1 + 2x) + 2x");
41+
const rhs = ce.parse("2x^2 + 3x + 1");
4242
console.log(lhs, lhs.isEqual(rhs) ? "=" : "≠", rhs);
4343
```
4444

@@ -72,8 +72,13 @@ The Compute Engine can:
7272
In this guide, functions such as `ce.box()` and `ce.parse()` require a
7373
`ComputeEngine` instance which is denoted by the `ce.` prefix.
7474

75+
**To create a new `ComputeEngine` instance:**, use `ce = new ComputeEngine()`
76+
7577
Functions that apply to a boxed expression, such as `expr.simplify()` are denoted with the
7678
`expr.` prefix.
79+
80+
**To create a new boxed expression:**, use `expr = ce.parse()` or `expr = ce.box()`
81+
7782
:::
7883

7984

@@ -85,10 +90,13 @@ Try the **interactive demo** now<Icon name="chevron-right-bold" />
8590
## Getting Started
8691

8792
The easiest way to get started is to load the Compute Engine JavaScript module
88-
from a CDN, then instantiate a `ComputeEngine` object.
93+
from a CDN, then create a `ComputeEngine` instance.
8994

9095
### Using JavaScript Modules
9196

97+
JavaScript modules are the modern way to load JavaScript code. You can load the
98+
Compute Engine module from a CDN using an `import` statement.
99+
92100
```html
93101
<script type="module">
94102
import { ComputeEngine } from

docs/compute-engine/03-guide-expressions.md

Lines changed: 88 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -44,45 +44,6 @@ The input of `ce.box()` can be:
4444

4545
The result is an instance of a `BoxedExpression`.
4646

47-
### String Representation
48-
49-
The `expr.toString()` method returns a [AsciiMath](https://asciimath.org/) string representation of the expression.
50-
51-
```live
52-
let expr = ce.parse("3x^2+\\sqrt{2}");
53-
console.log(expr.toString());
54-
```
55-
56-
When used in a context where a string is expected, the `expr.toString()` method
57-
is called automatically.
58-
59-
```live
60-
let expr = ce.parse("3x^2+\\sqrt{2}");
61-
console.log(expr);
62-
```
63-
64-
**To output an AsciiMath representation of the expressio to the console** use
65-
`expr.print()`.
66-
67-
```live
68-
let expr = ce.parse("3x^2+\\sqrt{2}");
69-
expr.print();
70-
```
71-
72-
**To obtain a LaTeX representation of the expression** use `expr.latex` or
73-
`expr.toLatex()` for additional formatting options.
74-
75-
```live
76-
let expr = ce.parse("3x^2+\\sqrt{2}");
77-
console.log(expr.latex);
78-
```
79-
80-
### Canonical Expressions
81-
82-
By default, `ce.box()` returns a canonical expression. See
83-
[Canonical Expressions](#canonical) for more info.
84-
85-
8647
```js
8748
let expr = ce.box(1.729e3);
8849
console.log(expr.re);
@@ -113,9 +74,6 @@ console.log(expr.json);
11374
// ➔ ["Add", 3, "x", "y"]
11475
```
11576

116-
By default, `ce.parse()` returns a canonical expression. See
117-
[Canonical Expressions](#canonical-expressions) for more info.
118-
11977
**To get a Boxed Expression representing the content of a mathfield**
12078
use the `mf.expression` property:
12179

@@ -127,42 +85,6 @@ console.log(expr.evaluate());
12785
// ➔ 2
12886
```
12987

130-
## Unboxing
131-
132-
**To access the MathJSON expression of a boxed expression as plain JSON**, use
133-
the `expr.json` property. This property is an "unboxed" version of the
134-
expression.
135-
136-
```js
137-
const expr = ce.box(["Add", 3, "x"]);
138-
console.log(expr.json);
139-
// ➔ ["Add", 3, "x"]
140-
```
141-
142-
**To customize the format of the MathJSON expression returned by `expr.json`**
143-
use the `ce.toMathJson()` method.
144-
145-
Use this option to control:
146-
147-
- which metadata, if any, should be included
148-
- whether to use shorthand notation
149-
- to exclude some functions.
150-
151-
See [JsonSerializationOptions](/compute-engine/api#jsonserializationoptions)
152-
for more info about the formatting options available.
153-
154-
```live
155-
const expr = ce.parse("2 + \\frac{q}{p}");
156-
console.log("expr.json:", expr.json);
157-
158-
console.log("expr.toMathJson():", expr.toMathJson({
159-
exclude: ["Divide"], // Don't use `Divide` functions,
160-
// use `Multiply`/`Power` instead
161-
shorthands: [], // Don't use any shorthands
162-
}));
163-
```
164-
165-
16688
## Canonical Expressions
16789

16890
The **canonical form** of an expression is a conventional way of writing an
@@ -216,15 +138,20 @@ By default, `ce.box()` and `ce.parse()` produce a canonical expression.
216138
**To get a non-canonical expression instead**, use
217139
`ce.box(expr, {canonical: false})` or `ce.parse(latex, {canonical: false})`.
218140

219-
The non-canonical form sticks closer to the original LaTeX input.
141+
When using `ce.parse()`, the non-canonical form sticks closer to the original
142+
LaTeX input. When using `ce.box()`, the non-canonical form matches the
143+
input MathJSON.
220144

221145
```js
222-
const expr = "\\frac{30}{-50}";
146+
const latex = "\\frac{30}{-50}";
223147

224-
ce.parse(expr);
148+
ce.parse(latex);
225149
// canonical form ➔ ["Rational", -3, 5]
226150

227-
ce.parse(expr, { canonical: false });
151+
ce.parse(latex, { canonical: false });
152+
// non-canonical form ➔ ["Divide", 30, -50]
153+
154+
ce.box(["Divide", 30, -50], { canonical: false });
228155
// non-canonical form ➔ ["Divide", 30, -50]
229156
```
230157

@@ -249,6 +176,85 @@ only possible syntax errors, but also semantic errors (incorrect number or
249176
type of arguments, etc...).
250177

251178

179+
180+
181+
## String Representation
182+
183+
The `expr.toString()` method returns a [AsciiMath](https://asciimath.org/) string representation of the expression.
184+
185+
```live
186+
let expr = ce.parse("3x^2+\\sqrt{2}");
187+
console.log(expr.toString());
188+
```
189+
190+
When used in a context where a string is expected, the `expr.toString()` method
191+
is called automatically.
192+
193+
```live
194+
let expr = ce.parse("3x^2+\\sqrt{2}");
195+
console.log(expr);
196+
```
197+
198+
**To output an AsciiMath representation of the expression to the console** use
199+
`expr.print()`.
200+
201+
```live
202+
let expr = ce.parse("\\frac{1+\\sqrt{5}}{2}");
203+
expr.print();
204+
```
205+
206+
**To obtain a LaTeX representation of the expression** use `expr.latex` or
207+
`expr.toLatex()` for additional formatting options.
208+
209+
```live
210+
let expr = ce.parse("3x^2+\\sqrt{2}");
211+
console.log(expr.latex);
212+
```
213+
214+
215+
216+
217+
218+
219+
220+
## Unboxing
221+
222+
**To access the MathJSON expression of a boxed expression as plain JSON**, use
223+
the `expr.json` property. This property is an "unboxed" version of the
224+
expression.
225+
226+
```js
227+
const expr = ce.box(["Add", 3, "x"]);
228+
console.log(expr.json);
229+
// ➔ ["Add", 3, "x"]
230+
```
231+
232+
**To customize the format of the MathJSON expression returned by `expr.json`**
233+
use the `ce.toMathJson()` method.
234+
235+
Use this option to control:
236+
237+
- which metadata, if any, should be included
238+
- whether to use shorthand notation
239+
- to exclude some functions.
240+
241+
See [JsonSerializationOptions](/compute-engine/api#jsonserializationoptions)
242+
for more info about the formatting options available.
243+
244+
```live
245+
const expr = ce.parse("2 + \\frac{q}{p}");
246+
console.log("expr.json:", expr.json);
247+
248+
console.log("expr.toMathJson():", expr.toMathJson({
249+
exclude: ["Divide"], // Don't use `Divide` functions,
250+
// use `Multiply`/`Power` instead
251+
shorthands: [], // Don't use any shorthands
252+
}));
253+
```
254+
255+
256+
257+
252258
## Mutability
253259

254260
Unless otherwise specified, expressions are immutable.

0 commit comments

Comments
 (0)