@@ -124,67 +124,95 @@ flowchart TD
124124
125125Make a new computation engine object and evaluate a LaTeX expression:
126126
127- ``` raku
127+ ``` raku, results=asis
128128use 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+
156192Using 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
161197for (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