Skip to content

Commit 4920c78

Browse files
committed
docs:Better basic examples.
1 parent 30261f0 commit 4920c78

2 files changed

Lines changed: 100 additions & 48 deletions

File tree

README-work.md

Lines changed: 39 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -124,43 +124,67 @@ flowchart TD
124124

125125
Make a new computation engine object and evaluate a LaTeX expression:
126126

127-
```raku
127+
```raku, results=asis
128128
use CortexJS;
129-
my $ce = ComputeEngine.new;
130129
131-
$ce.evaluate($ce.parse-latex('e^{i\\pi}'))
130+
'e^{i\\pi}' ==> evaluate()
132131
```
133132

134133
Expand expression:
135134

136-
```raku
137-
$ce.to-latex($ce.expand($ce.parse-latex('(a + b)^2')));
135+
```raku, results=asis
136+
'(a + b)^2' ==> expand()
138137
```
139138

140139
Simplify expression:
141140

141+
```raku, results=asis
142+
my $expr = parse-latex('3x^2 + 2x^2 + x + 5');
143+
say "{to-latex($expr)} = {to-latex(simplify($expr))}";
144+
```
145+
146+
Solve a symbolic equation
147+
148+
```raku, results=asis
149+
'x^2 - a x + 1 = 0' ==> solve(vars => 'a')
150+
```
151+
152+
The input can be both a LaTeX string (as above) or in [MathJSON format](https://mathlive.io/math-json/).
153+
Here the LaTeX expression `(a - b)^2` is converted to MathJSON using the sub `parse-latex`:
154+
142155
```raku
143-
my $expr = $ce.parse-latex('3x^2 + 2x^2 + x + 5');
144-
say "{$ce.to-latex($expr)} = {$ce.to-latex($ce.simplify($expr))}";
156+
parse-latex('(a - b)^2').raku
145157
```
146158

147-
Using assignment for repeated expression evaluation:
159+
Here the corresponding MathJSON expression is expanded:
148160

149161
```raku
150-
my $expr = $ce.parse-latex("3x^2+4x+2");
162+
["Power", ["Add", "a", ["Negate", "b"]], 2]
163+
==> expand()
164+
```
151165

152-
for (0, 0.1 ... 1) -> $x {
153-
$ce.assign('x', $x);
154-
say "f($x) = {$ce.evaluate($expr)}";
155-
}
166+
MathJSON expressions can be converted to LaTeX with `to-latex`:
167+
168+
```raku, results=asis
169+
_ ==> to-latex()
156170
```
157171

158-
Can be put in the last code block:
172+
**Remark** The "free functions" `evaluate`, `N`, `simplify`, `assign`, `expand`, `expandAll`, `factor`, and `solve`
173+
try to recognize (or parse) a string input as LaTeX code, and if parsing is successful,
174+
then (more or less) the processing pipeline in applied: `$expr ==> parse-latex() ==> &func() ==> to-latex()`.
175+
176+
Using assignment for repeated expression evaluation:
159177

160178
```raku
161-
LEAVE $ce.close;
179+
my $expr = parse-latex("3x^2+4x+2");
180+
181+
for (0, 0.1 ... 1) -> $x {
182+
assign('x', $x);
183+
say "f($x) = {evaluate($expr)}";
184+
}
162185
```
163186

187+
164188
----
165189

166190
## References

README.md

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -124,67 +124,95 @@ flowchart TD
124124

125125
Make a new computation engine object and evaluate a LaTeX expression:
126126

127-
```raku
127+
```raku, results=asis
128128
use CortexJS;
129-
my $ce = ComputeEngine.new;
130129
131-
$ce.evaluate($ce.parse-latex('e^{i\\pi}'))
130+
'e^{i\\pi}' ==> evaluate()
132131
```
132+
$-1$
133+
134+
135+
Expand expression:
136+
137+
```raku, results=asis
138+
'(a + b)^2' ==> expand()
133139
```
134-
# -1
140+
$a^2+b^2+2ab$
141+
142+
143+
Simplify expression:
144+
145+
```raku, results=asis
146+
my $expr = parse-latex('3x^2 + 2x^2 + x + 5');
147+
say "{to-latex($expr)} = {to-latex(simplify($expr))}";
135148
```
149+
$2x^2+3x^2+x+5$ = $5x^2+x+5$
136150

137-
Expand expression:
151+
152+
Solve a symbolic equation
153+
154+
```raku, results=asis
155+
'x^2 - a x + 1 = 0' ==> solve(vars => 'a')
156+
```
157+
$x+\frac{1}{x}$
158+
159+
160+
The input can be both a LaTeX string (as above) or in [MathJSON format](https://mathlive.io/math-json/).
161+
Here the LaTeX expression `(a - b)^2` is converted to MathJSON using the sub `parse-latex`:
138162

139163
```raku
140-
$ce.to-latex($ce.expand($ce.parse-latex('(a + b)^2')));
164+
parse-latex('(a - b)^2').raku
141165
```
142166
```
143-
# a^2+b^2+2ab
167+
# $["Power", ["Add", "a", ["Negate", "b"]], 2]
144168
```
145169

146-
Simplify expression:
170+
Here the corresponding MathJSON expression is expanded:
147171

148172
```raku
149-
my $expr = $ce.parse-latex('3x^2 + 2x^2 + x + 5');
150-
say "{$ce.to-latex($expr)} = {$ce.to-latex($ce.simplify($expr))}";
173+
["Power", ["Add", "a", ["Negate", "b"]], 2]
174+
==> expand()
151175
```
152176
```
153-
# 2x^2+3x^2+x+5 = 5x^2+x+5
177+
# [Add [Power a 2] [Power b 2] [Multiply -2 a b]]
154178
```
155179

180+
MathJSON expressions can be converted to LaTeX with `to-latex`:
181+
182+
```raku, results=asis
183+
_ ==> to-latex()
184+
```
185+
$a^2+b^2-2ab$
186+
187+
188+
**Remark** The "free functions" `evaluate`, `N`, `simplify`, `assign`, `expand`, `expandAll`, `factor`, and `solve`
189+
try to recognize (or parse) a string input as LaTeX code, and if parsing is successful,
190+
then (more or less) the processing pipeline in applied: `$expr ==> parse-latex() ==> &func() ==> to-latex()`.
191+
156192
Using assignment for repeated expression evaluation:
157193

158194
```raku
159-
my $expr = $ce.parse-latex("3x^2+4x+2");
195+
my $expr = parse-latex("3x^2+4x+2");
160196

161197
for (0, 0.1 ... 1) -> $x {
162-
$ce.assign('x', $x);
163-
say "f($x) = {$ce.evaluate($expr)}";
198+
assign('x', $x);
199+
say "f($x) = {evaluate($expr)}";
164200
}
165201
```
166202
```
167-
# f(0) = 2
168-
# f(0.1) = 2.43
169-
# f(0.2) = 2.92
170-
# f(0.3) = 3.47
171-
# f(0.4) = 4.08
172-
# f(0.5) = 4.75
173-
# f(0.6) = 5.48
174-
# f(0.7) = 6.27
175-
# f(0.8) = 7.12
176-
# f(0.9) = 8.03
177-
# f(1) = 9
203+
# f(0) = Add Multiply 3 Power x 2 Multiply 4 x 2
204+
# f(0.1) = Add Multiply 3 Power x 2 Multiply 4 x 2
205+
# f(0.2) = Add Multiply 3 Power x 2 Multiply 4 x 2
206+
# f(0.3) = Add Multiply 3 Power x 2 Multiply 4 x 2
207+
# f(0.4) = Add Multiply 3 Power x 2 Multiply 4 x 2
208+
# f(0.5) = Add Multiply 3 Power x 2 Multiply 4 x 2
209+
# f(0.6) = Add Multiply 3 Power x 2 Multiply 4 x 2
210+
# f(0.7) = Add Multiply 3 Power x 2 Multiply 4 x 2
211+
# f(0.8) = Add Multiply 3 Power x 2 Multiply 4 x 2
212+
# f(0.9) = Add Multiply 3 Power x 2 Multiply 4 x 2
213+
# f(1) = Add Multiply 3 Power x 2 Multiply 4 x 2
178214
```
179215

180-
Can be put in the last code block:
181-
182-
```raku
183-
LEAVE $ce.close;
184-
```
185-
```
186-
# (Any)
187-
```
188216

189217
----
190218

0 commit comments

Comments
 (0)