Skip to content

Commit cf8d062

Browse files
committed
Update documentation for JuliaFunc and StdStr
1 parent 15975e6 commit cf8d062

1 file changed

Lines changed: 52 additions & 5 deletions

File tree

docs/src/usage.md

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,13 @@ x₃x³A¹₆ + 2A¹₅x⁵x₆
110110

111111
See [Derivatives in Standard Notation](@ref) for more examples.
112112

113-
## Converting Tensor Expression to Standard Notation
113+
## Converting Tensor Expressions to Standard Notation
114114

115115
An expression in Ricci-notation can be converted back to standard notation using [`to_std`](@ref). `DiffMatic` can output a standard expression in string format or as a Julia function.
116116

117117
### String Output
118118

119-
An expression can be converted to standard matrix notation and retrieved as a string by passing [`StdStr`](@ref) as a keyword argument to [`to_std`](@ref):
119+
An expression can be converted to standard matrix notation and retrieved as a string by passing a [`StdStr`](@ref) instance as a keyword argument `format` to [`to_std`](@ref):
120120

121121
```jldoctest usage
122122
to_std(J; format = StdStr())
@@ -128,6 +128,7 @@ to_std(J; format = StdStr())
128128

129129
Special notation used in the output:
130130
- "⊙": Element-wise multiplication.
131+
- "⊘": Element-wise division.
131132
- "diag(x)": Diagonal matrix with "x" on the diagonal.
132133
- "vec(1)": Vector consisting of 1:s.
133134
- "sgn(x)": The signum function applied element-wise.
@@ -136,7 +137,7 @@ Special notation used in the output:
136137

137138
### Julia Function
138139

139-
A Julia function is generated by passing [`JuliaFunc`](@ref) as a keyword argument to [`to_std`](@ref). The function is returned in the form of an [expression](https://docs.julialang.org/en/v1/manual/metaprogramming/#Expressions-and-evaluation):
140+
A Julia function is generated by passing a [`JuliaFunc`](@ref) instance as a keyword argument `format` to [`to_std`](@ref). The function is returned in the form of an [expression](https://docs.julialang.org/en/v1/manual/metaprogramming/#Expressions-and-evaluation):
140141

141142
```julia
142143
to_std(g; format = JuliaFunc())
@@ -145,10 +146,10 @@ to_std(g; format = JuliaFunc())
145146

146147
quote
147148
#= ... =#
148-
function (B, A, x)
149+
function (A, B, x)
149150
#= ... =#
150151
#= ... =#
151-
return 2 * (transpose(B) * (transpose(A) * x)) + 2 * ((A * B) * x)
152+
return 2 * ((transpose(B) * transpose(A)) * x) + 2 * (A * (B * x))
152153
end
153154
end
154155
```
@@ -172,5 +173,51 @@ g_fun(An, Bn, xn)
172173
450.0
173174
```
174175

176+
The `JuliaFunc` generator may use functions and primitives defined in `LinearAlgebra`, such as e.g. `diagm` and `I`. These functions need to be explicitly imported by the user prior to usage:
177+
178+
```jldoctest usage
179+
g = gradient(log.(A * x)' * x, x)
180+
g_fun = eval(to_std(g; format = JuliaFunc()))
181+
182+
An = exp(3) * ones(3,3)
183+
xn = Float64[1; 0; 0]
184+
185+
g_fun(An, xn)
186+
187+
# output
188+
189+
ERROR: UndefVarError: `diagm` not defined
190+
```
191+
192+
```jldoctest usage
193+
using LinearAlgebra: diagm
194+
195+
g_fun(An, xn)
196+
197+
# output
198+
199+
3-element Vector{Float64}:
200+
4.0
201+
4.0
202+
4.0
203+
```
204+
205+
The order of the arguments of the generated function can be specified through `JuliaFunc`:
206+
```julia
207+
to_std(g; format = JuliaFunc([x, B, A]))
208+
209+
# output
210+
211+
quote
212+
#= ... =#
213+
function (x, B, A)
214+
#= ... =#
215+
#= ... =#
216+
return 2 * ((transpose(B) * transpose(A)) * x) + 2 * (A * (B * x))
217+
end
218+
end
219+
220+
```
221+
175222
```@bibliography
176223
```

0 commit comments

Comments
 (0)