Skip to content

Commit 13582fa

Browse files
Merge pull request #804 from JuliaSymbolics/as/no-hvncat
fix: fix more bugs
2 parents bc20620 + c3d21ec commit 13582fa

File tree

16 files changed

+233
-219
lines changed

16 files changed

+233
-219
lines changed

docs/pages.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
pages = Any["index.md",
2-
"Manual" => Any["manual/representation.md",
2+
"Manual" => Any["manual/variants.md",
33
"manual/rewrite.md",
44
"manual/interface.md",
55
"manual/codegen.md",
6-
"manual/variants.md"],
6+
"manual/caching.md",
7+
"manual/recursive_utils.md",
8+
],
79
"api.md",
810
"upgrade.md"
911
]

docs/src/api.md

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -58,35 +58,3 @@ SymbolicUtils.simplify_fractions
5858
SymbolicUtils.quick_cancel
5959
SymbolicUtils.flatten_fractions
6060
```
61-
62-
## Code Generation
63-
64-
### Core Functions
65-
```@docs
66-
SymbolicUtils.Code.toexpr
67-
SymbolicUtils.Code.cse
68-
```
69-
70-
### Code Generation Types
71-
```@docs
72-
SymbolicUtils.Code.Assignment
73-
SymbolicUtils.Code.Let
74-
SymbolicUtils.Code.Func
75-
SymbolicUtils.Code.DestructuredArgs
76-
SymbolicUtils.Code.LiteralExpr
77-
SymbolicUtils.Code.ForLoop
78-
```
79-
80-
### Array Operations
81-
```@docs
82-
SymbolicUtils.Code.SetArray
83-
SymbolicUtils.Code.MakeArray
84-
SymbolicUtils.Code.MakeSparseArray
85-
SymbolicUtils.Code.MakeTuple
86-
```
87-
88-
### Parallelism
89-
```@docs
90-
SymbolicUtils.Code.SpawnFetch
91-
SymbolicUtils.Code.Multithreaded
92-
```

docs/src/manual/caching.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Caching recursive functions
2+
3+
Many functions benefit greatly from caching their results such that repeated calls with the same arguments
4+
can return cached values. As an example, `getindex` on symbolic arrays is a cached function since the operation can
5+
be expensive. SymbolicUtils.jl provides the `@cache` macro to allow easily caching such functions, with special
6+
benefits when the arguments are symbolic.
7+
8+
```@docs
9+
SymbolicUtils.@cache
10+
SymbolicUtils.associated_cache
11+
SymbolicUtils.get_limit
12+
SymbolicUtils.set_limit
13+
SymbolicUtils.get_retain_fractions
14+
SymbolicUtils.set_retain_fractions
15+
SymbolicUtils.toggle_caching
16+
SymbolicUtils.is_caching_enabled
17+
SymbolicUtils.clear_cache
18+
```

docs/src/manual/codegen.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ SymbolicUtils.Code.MakeArray
3535
SymbolicUtils.Code.MakeSparseArray
3636
SymbolicUtils.Code.MakeTuple
3737
SymbolicUtils.Code.LiteralExpr
38-
```
38+
SymbolicUtils.Code.ForLoop
39+
```
40+
41+
## Optimizations
42+
43+
### Common Subexpression Elimination (CSE)
44+
45+
SymbolicUtils can perform CSE on symbolic expressions, and codegen primitives composed of the above "Code Combinators".
46+
This ensures that common subexpressions in the expression are only computed once. Note that this assumes that all functions
47+
called within the expression are pure. SymbolicUtils can and will change the number and order of function calls.
48+
49+
```@docs
50+
SymbolicUtils.Code.cse
51+
SymbolicUtils.Code.cse_inside_expr
52+
```

docs/src/manual/recursive_utils.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Recursive utility functions
2+
3+
SymbolicUtils.jl provides several utility functions to perform common operations that
4+
require recursing over the expression tree.
5+
6+
```@docs
7+
SymbolicUtils.substitute
8+
SymbolicUtils.Substituter
9+
SymbolicUtils.default_substitute_filter
10+
SymbolicUtils.evaluate
11+
SymbolicUtils.query
12+
SymbolicUtils.search_variables
13+
SymbolicUtils.search_variables!
14+
SymbolicUtils.default_is_atomic
15+
SymbolicUtils.scalarize
16+
SymbolicUtils.scalarization_function
17+
```

docs/src/manual/representation.md

Lines changed: 0 additions & 90 deletions
This file was deleted.

docs/src/manual/variants.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ as `BasicSymbolic`. A `BasicSymbolic` is considered immutable. Mutating its fiel
99
behavior.
1010

1111
In SymbolicUtils v3, the type `T` in `BasicSymbolic{T}` was the type represented by the symbolic
12-
variable. In other words, `T` was the [`symtype`](@ref) of the variable.
12+
variable. In other words, `T` was the [`SymbolicUtils.symtype`](@ref) of the variable.
1313

1414
In SymbolicUtils v4, the `symtype` is not stored in the type, and is instead a field of the
1515
struct. This allows for greatly increased type-stability. The type `T` in `BasicSymbolic{T}`
@@ -26,6 +26,7 @@ A given expression must be pure in its `vartype`. In other words, no operation s
2626
of different `vartype`s.
2727

2828
!!! warning "A short note on (im-)mutability"
29+
2930
While `ismutabletype(BasicSymbolic)` returns `true`, symbolic types are IMMUTABLE.
3031
Any mutation is undefined behavior and can lead to very confusing and hard-to-debug issues.
3132
This includes internal mutation, such as mutating `AddMul.dict`. The arrays returned from
@@ -108,10 +109,10 @@ variant. This variant can be constructed using `Const{T}(val)` or `BSImpl.Const{
108109
where `T` is the appropriate `vartype`.
109110

110111
The `Const` constructors have an additional special behavior. If given an array of symbolics
111-
(or array of array of ... symbolics), it will return a `Term` (see below) with `hvncat` as
112-
the operation. This allows standard symbolic operations (such as [`substitute`](@ref)) to
113-
work on arrays of symbolics without excessive special-case handling and improved
114-
type-stability.
112+
(or array of array of ... symbolics), it will return a `Term` (see below) with
113+
[`SymbolicUtils.array_literal`](@ref) as the operation. This allows standard symbolic
114+
operations (such as [`substitute`](@ref)) to work on arrays of symbolics without excessive
115+
special-case handling and improved type-stability.
115116

116117
```julia
117118
struct Sym
@@ -294,6 +295,7 @@ range of indices over which they should iterate, in case such a range is explici
294295
provided.
295296

296297
!!! note
298+
297299
The common global index variable is printed as `_1`, `_2`, ... in arrayops. It is not
298300
a valid symbolic variable outside of an `ArrayOp`'s `expr`.
299301

@@ -336,8 +338,8 @@ to Base-like behavior:
336338
doing so requires the function(s) passed to `map` and `mapreduce` instead of their types
337339
or shapes.
338340
- Since `ndims` information is not present in the type, `eachindex`, `iterate`, `size`,
339-
`axes`, `ndims`, `collect` are type-unstable. [`safe_eachindex`](@ref) is useful as
340-
a type-stable iteration alternative.
341+
`axes`, `ndims`, `collect` are type-unstable. [`SymbolicUtils.stable_eachindex`](@ref) is
342+
useful as a type-stable iteration alternative.
341343
- `ifelse` requires that both the true and false cases have identical shape.
342344
- Symbolic arrays _only_ support cartesian indexing. For example, given `@syms x[1:3, 1:3]`
343345
accessing `x[4]` is invalid and `x[1, 2]` should be used. Valid indices are
@@ -385,7 +387,7 @@ words,
385387
Here, `f1(x)` is considered a symbolic function `f1` called with the argument `x` and
386388
`f2(x)` is considered a dependent variable that depends on `x`. The utilities
387389
[`SymbolicUtils.is_function_symbolic`](@ref), [`SymbolicUtils.is_function_symtype`](@ref),
388-
[`symbolicUtils.is_called_function_symbolic`](@ref) can be used to differentiate between
390+
[`SymbolicUtils.is_called_function_symbolic`](@ref) can be used to differentiate between
389391
these cases.
390392

391393
## API
@@ -409,6 +411,7 @@ unwrap_const
409411
### Inner constructors
410412

411413
```@docs
414+
SymbolicUtils.array_literal
412415
SymbolicUtils.BSImpl.Const
413416
SymbolicUtils.BSImpl.Sym
414417
SymbolicUtils.BSImpl.Term
@@ -457,9 +460,9 @@ SymbolicUtils.promote_shape
457460
### Symbolic array utilities
458461

459462
```@docs
460-
SymbolicUtils.safe_eachindex
461-
SymbolicUtils.SafeIndices
462-
SymbolicUtils.SafeIndex
463+
SymbolicUtils.stable_eachindex
464+
SymbolicUtils.StableIndices
465+
SymbolicUtils.StableIndex
463466
BS
464467
```
465468

src/SymbolicUtils.jl

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ using SymbolicIndexingInterface
1818
import Base: +, -, *, /, //, \, ^, ImmutableDict
1919
using ConstructionBase
2020
using TermInterface
21-
import TermInterface: iscall, isexpr, children,
22-
operation, arguments, metadata, maketerm, sorted_arguments
21+
import TermInterface: iscall, operation, arguments, metadata, maketerm, sorted_arguments
2322
# For ReverseDiffExt
2423
import ArrayInterface
2524
import ExproniconLite as EL

src/code.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -869,10 +869,9 @@ end
869869
"""
870870
$(TYPEDSIGNATURES)
871871
872-
Return `true` if CSE should descend inside `sym`, which has operation `f` and
873-
arguments `args...`.
872+
Return `true` if CSE should descend inside `sym`, which has operation `f`.
874873
"""
875-
function cse_inside_expr(sym, f, args...)
874+
function cse_inside_expr(sym, f)
876875
return true
877876
end
878877

@@ -1009,7 +1008,7 @@ function cse!(expr::BasicSymbolic{T}, state::CSEState) where {T}
10091008
if op isa BasicSymbolic{T}
10101009
SymbolicUtils.is_function_symbolic(op) || return expr
10111010
end
1012-
cse_inside_expr(expr, op, args...) || return expr
1011+
cse_inside_expr(expr, op) || return expr
10131012
args = map(Base.Fix2(cse!, state), args)
10141013
# use `term` instead of `maketerm` because we only care about the operation being performed
10151014
# and not the representation. This avoids issues with `newsym` symbols not having sizes, etc.

src/inspect.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ the expression.
3333
This function is used internally for printing via AbstractTrees.
3434
"""
3535
function AbstractTrees.children(x::BasicSymbolic)
36-
iscall(x) ? sorted_arguments(x) : isexpr(x) ? sorted_children(x) : ()
36+
iscall(x) ? sorted_arguments(x) : ()
3737
end
3838

3939
"""

0 commit comments

Comments
 (0)