You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/src/julia_functions.md
+128-2Lines changed: 128 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Creating Julia Functions from Expressions
2
2
3
-
Giac.jl lets you build symbolic expressions and then wrap them into regular Julia functions using [`substitute`](@ref) and [`to_julia`](@ref).
3
+
Giac.jl lets you build symbolic expressions and then wrap them into regular Julia functions. The recommended named entry point is [`build_function`](@ref); under the hood it composes [`substitute`](@ref) and [`to_julia`](@ref), and you can always drop down to those primitives directly.
4
4
5
5
## Basic Idea
6
6
@@ -20,6 +20,131 @@ f(0) # -1
20
20
f(-2) # 3
21
21
```
22
22
23
+
## Using `build_function`
24
+
25
+
`build_function(expr, vars...)` is the named convenience wrapper for exactly the pattern above. It returns a Julia callable that you can pass to `Plots.plot`, `Plots.surface`, broadcasting (`f.(xs)`), and matrix comprehensions — without writing the `substitute` + `to_julia` line every time.
26
+
27
+
```julia
28
+
using Giac
29
+
30
+
@giac_var x
31
+
f =build_function(x^2-1, x)
32
+
33
+
f(3) # 8
34
+
f.([0, 1, 2]) # [-1, 0, 3]
35
+
```
36
+
37
+
For multivariate expressions, list the variables in the desired positional order:
38
+
39
+
```julia
40
+
@giac_var x y
41
+
g =build_function(x^2+2*x*y - y^2, x, y)
42
+
43
+
g(1, 2) # 1
44
+
g(3, 1) # 14
45
+
```
46
+
47
+
`build_function` is intentionally a *thin* wrapper. It does not introduce any new substitution mechanism: the result of `build_function(expr, vars...)(vals...)` is always equal to `to_julia(substitute(expr, Pair.(vars, vals)...))`. If you need to insert a transformation between substitution and conversion (e.g., `simplify`, `expand`, `evalf` at custom precision), drop down to the primitives directly rather than expecting `build_function` to grow new keyword arguments for every variant.
|**Giac.jl**|`build_function`|`build_function(expr::GiacExpr, vars::GiacExpr...)`| Julia callable (`<: Function`) | Tier 1 wrapper over `substitute` + `to_julia`; each call goes through the Giac FFI. |
54
+
|**Symbolics.jl**|`build_function`|`build_function(expr, args...; kwargs...)`| Julia function (in-house codegen) | Walks the expression tree and emits native Julia code. SciML standard. |
55
+
|**SymPy.jl**|`lambdify`|`lambdify(expr, vars; fns=...)`| Julia function | Translates a SymPy expression into a Julia function via a Python intermediary. |
56
+
57
+
```@docs
58
+
build_function
59
+
```
60
+
61
+
### Choosing a backend
62
+
63
+
`build_function` accepts a `backend::Symbol` keyword that selects the engine
64
+
that evaluates the substituted expression. As of Giac.jl v0.14:
0 commit comments