Skip to content

Symbolic Arrays #197

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 120 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
120 commits
Select commit Hold shift + click to select a range
d87962e
Array basics
shashi Aug 16, 2020
c48e140
array tests
shashi Aug 16, 2020
df91050
Merge remote-tracking branch 'origin/master' into s/array-impl
shashi Feb 28, 2021
73fe6a7
fix and update tests
shashi Feb 28, 2021
0ac1b95
fix
shashi Mar 12, 2021
f7f6157
check in tensor-dsl
shashi Mar 19, 2021
277b8e0
wip
shashi Mar 22, 2021
2a134ad
implement broadcast
shashi Mar 22, 2021
242797e
okokok
shashi Mar 22, 2021
04e03d3
matvec
shashi Mar 22, 2021
d0718f6
Prolong life with propagate_* stuff
shashi Mar 22, 2021
47e309d
Clean up broadcast
shashi Mar 22, 2021
2a41a58
get rid of ArrayShape, fix stuff up
shashi Mar 23, 2021
352d7eb
matmul
shashi Mar 23, 2021
617b54d
fix a'a where a is a vector
shashi Mar 24, 2021
56f1808
map
shashi Mar 25, 2021
2d98822
map and reduce
shashi Mar 25, 2021
23abc1c
oops
shashi Mar 25, 2021
d22a108
sum prod
shashi Mar 25, 2021
0795082
some stuff
shashi Mar 29, 2021
2fe6149
arrayop macro experiment
shashi Mar 31, 2021
1d6f19e
cleanup
shashi Apr 1, 2021
2a9250e
maybe
shashi Apr 1, 2021
df74014
add custom reduce function
shashi Apr 4, 2021
e3db87c
mv oops
shashi Apr 4, 2021
41cc5e1
propagate_eltype and propagate_ndims for ArrayOp
shashi Apr 4, 2021
ed02581
Add eltype for getindex
shashi Apr 4, 2021
885f45f
broadcast
shashi Apr 6, 2021
c202e5d
commit from clean slate up to broadcast
shashi Apr 7, 2021
dca5b24
wip
shashi Apr 7, 2021
7399644
fix arrayop macro
shashi Apr 7, 2021
33201b5
remove redundant file
shashi Apr 7, 2021
8fdcaa8
migrate map and reduce
shashi Apr 8, 2021
fffc4a5
fix shape on ArrayOp, nicer printing
shashi Apr 8, 2021
ef38f1d
Eagerly evaluate shape, fix c'c case where c is Vector
shashi Apr 8, 2021
fdc90ab
a fuzzer and a bunch of robustness improvements
shashi Apr 8, 2021
d26d280
run fuzzer for longer
shashi Apr 8, 2021
433ed0c
add to runtests
shashi Apr 8, 2021
484cb7c
updates
shashi Apr 11, 2021
dcdbe0b
oops
shashi Apr 11, 2021
3b47d46
Merge remote-tracking branch 'origin/master' into s/arrays
shashi Apr 16, 2021
985acb2
use symbolic_wrapper macro fro Num
shashi Apr 16, 2021
89535df
wrap init
shashi Apr 22, 2021
323b251
Merge remote-tracking branch 'origin/master' into s/arrays
shashi Apr 23, 2021
a94edc4
implement arrays in at-variables
shashi Apr 23, 2021
5f2595b
fix wrapper and wrap output of getindex
shashi Apr 24, 2021
d0ebb7b
move wrapper type stuff here
shashi Apr 26, 2021
d12f259
move in the wrapper stuff from SymbolicUtils
shashi Apr 26, 2021
07c6c69
check in tests
shashi Apr 26, 2021
2e88835
don't be so quick to make eltype Any
shashi Apr 26, 2021
bba8dbb
dependency stuff
shashi Apr 26, 2021
5fd3cb6
include splitdef before
shashi Apr 26, 2021
aa231b1
fix printing
shashi Apr 26, 2021
637671e
Fix b'A to be an adjoint vector
shashi Apr 26, 2021
78402cc
Getindex Posthook
shashi Apr 28, 2021
b479ed3
sum prod all any with mapper function
shashi Apr 28, 2021
aee6018
allow default values on arrays to be propagated in getindex
shashi Apr 28, 2021
7c316c2
a single default value over an array
shashi Apr 29, 2021
221f6c1
toexpr
shashi Apr 29, 2021
a9bab2e
sketchy way to handle forcing the map call... eltype is wrong
shashi Apr 30, 2021
ecab3ef
scalarize
shashi May 1, 2021
3e7814b
substitute is not good enough, recursively scalarize
shashi May 1, 2021
cd618a5
encode getindex in ArrayOp
shashi May 2, 2021
fca214b
avoid descent into madness
shashi May 2, 2021
828e01e
allow specifying range in arrayop
shashi May 2, 2021
1b960f0
stop doing approx_eltype
shashi May 2, 2021
8ae260d
rm todo
shashi May 2, 2021
dbda544
try messing with the scalarizer
shashi May 3, 2021
4c28539
make the called macro syntax work
shashi May 4, 2021
3888841
Create an "identity" ArrayOp
shashi May 10, 2021
86e7d69
fix scalarize
shashi May 10, 2021
10a0c46
make tosymbol easy
shashi May 10, 2021
f029174
custom similarterm in unflatten
shashi May 16, 2021
0be613c
don't try to scalarize legit Arrays
shashi May 16, 2021
4909cdf
don't wrap length, convert is scalarize
shashi May 18, 2021
b2b2791
fixes
shashi May 26, 2021
3b06078
fixes
shashi May 28, 2021
ee475bb
allow getindex with CartesianIndex
shashi May 28, 2021
99f2f2d
Avoid piracy
shashi May 28, 2021
c417d2e
fix latexify
shashi May 28, 2021
6e5310b
Merge remote-tracking branch 'origin/master' into s/arrays
shashi May 28, 2021
0ef35ef
fix tests
shashi May 28, 2021
f90ef9d
fix calling with num
shashi May 28, 2021
8e09c6a
fix variables
shashi May 28, 2021
1ce5b5c
some more tests
shashi Jun 7, 2021
8e571d7
carefull with call2term when ref
shashi Jun 7, 2021
8b32642
fix : exprs
shashi Jun 7, 2021
8be5209
Merge remote-tracking branch 'origin/master' into s/arrays
shashi Jun 8, 2021
0e11224
Fix broadcast and scalarize on Num
shashi Jun 8, 2021
db473d8
fix broadcast on `@variables x[1:2](t)`
shashi Jun 8, 2021
8f45ede
fix tests
shashi Jun 8, 2021
de8589c
You can get pretty specific with the eltype, and things will work
shashi Jun 8, 2021
52c9a7a
Get det to work, start sneaking in better codegen for array-valued exprs
shashi Jun 8, 2021
2e2da33
fixes to toexpr
shashi Jun 9, 2021
2abc8e1
fix hash and lookup symbolify in toexpr of ArrayOp
shashi Jun 9, 2021
9b2dad5
make norm work by making mapreduce infer
shashi Jun 9, 2021
8848435
better inference of broadcast
shashi Jun 9, 2021
1c22c6e
scalarize recursively -- this is out of control
shashi Jun 9, 2021
4e82da3
test it with norm
shashi Jun 9, 2021
f6a9c63
revert change to broadcast, use term instead of calling function
shashi Jun 9, 2021
baa3373
C tests?
shashi Jun 9, 2021
a0ac8cd
Add first bit of docs
shashi Jun 9, 2021
578c6f9
update example
shashi Jun 9, 2021
69ae020
fix det
shashi Jun 9, 2021
9befb31
fix wrong test
shashi Jun 10, 2021
8885352
Fix CTarget build function
shashi Jun 10, 2021
7701add
fix?
shashi Jun 10, 2021
a5492f4
one point oh
shashi Jun 10, 2021
a182dc4
Merge branch 'master' into s/arrays
shashi Jun 10, 2021
d57a361
delete makesym
shashi Jun 11, 2021
a8eb4c0
Test defaultval stuff
shashi Jun 11, 2021
6b9fd94
delete dead code
shashi Jun 11, 2021
f4f9998
cover one more case
shashi Jun 11, 2021
b0d7662
test wrapper types
shashi Jun 11, 2021
0e21bbb
test a special build_function case
shashi Jun 11, 2021
880d9c9
fix
shashi Jun 11, 2021
b9ab78c
bump SU Version
shashi Jun 11, 2021
11f1a75
Don't fail fast
ChrisRackauckas Jun 11, 2021
b940a3e
Merge remote-tracking branch 'origin/s/arrays' into s/arrays
shashi Jun 11, 2021
06a76c0
remake x in test
shashi Jun 11, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
group:
- Core
Expand Down
7 changes: 5 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
name = "Symbolics"
uuid = "0c5d862f-8b57-4792-8d23-62f2024744c7"
authors = ["Shashi Gowda <[email protected]>"]
version = "0.1.33"
version = "1.0.0"

[deps]
AbstractAlgebra = "c3fe647b-3220-5bb0-a1ea-a7954cac585d"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
DiffRules = "b552c78f-8df3-52c6-915a-8e097449b14b"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
DocStringExtensions = "ffbed154-4ef7-542d-bbb7-c09d3a79fcae"
Expand All @@ -23,6 +24,7 @@ SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46"
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
SymbolicUtils = "d1185830-fcd6-423d-90d6-eec64667417b"
TreeViews = "a2a6695c-b41b-5b7d-aed9-dbfdeacea5d7"

Expand All @@ -45,7 +47,8 @@ SafeTestsets = "0"
SciMLBase = "1.8"
Setfield = "0.7"
SpecialFunctions = "0.7, 0.8, 0.9, 0.10, 1.0"
SymbolicUtils = "0.10.1, 0.11"
StaticArrays = "1.1"
SymbolicUtils = "0.12"
TreeViews = "0.3"
julia = "1.5"

Expand Down
130 changes: 130 additions & 0 deletions docs/src/manual/arrays.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
# Symbolic arrays

Symbolic array-valued expressions (symbolic arrays) are supported by Symbolics. Symbolic array expressions propagate useful metadata that depends on input arrays: array dimension, element type and shape.

You can create a symbolic array variable with the following syntax:

```julia
@variables A[1:5, 1:3] b[1:3]
```

Here `A` is a symbolic matrix of size `(5, 3)` and `b` is a symbolic vector of length 3.

```julia
julia> size(A)
(5, 3)

julia> size(b)
(3,)

julia> ndims(A)
2

julia> ndims(b)
1

julia> eltype(A)
Real

julia> eltype(b)
Real
```

## Array operations

Operations on symbolic arrays return symbolic array expressions:

```julia
julia> c = A * b
(A*b)[1:5]

julia> size(c)
(5,)

julia> eltype(c)
Real
```

Adjoints, matrix-matrix, and matrix-vector multiplications are supported. Dot product returns a scalar-valued expression:

```julia
julia> b'b
adjoint(b)*b

julia> size(b'b)
()
```

Outer product returns a matrix:

```julia
julia> b * b'
(b*adjoint(b))[1:3,1:3]

julia> size(b*b')
(3, 3)
```

### Broadcast, map and reduce


```julia
julia> A .* b'
(broadcast(*, A, adjoint(b))[1:5,1:3]
```

```julia
julia> map(asin, (A*b))
(map(asin, A*b))[1:5]
```

``julia
julia> sum(A)
...

julia> typeof(sum(A))
Num # it's a scalar!

julia> typeof(sum(A, dims=2))
Arr{Real, 2} # it's a vector
```

### Indexing and delayed computation

Indexing array expressions is fairly flexible in Symbolics. Let's go through all the possible ways to index arrays.

#### Scalar indexing and scalarization

```julia
julia> AAt = A*A'
(A*adjoint(A))[1:5,1:5]

julia> AAt[2,3]
(A*adjoint(A))[2,3]
```

Here we indexed for the element (2,3), but we got back a symbolic indexing expression. You may want to force the element to be computed in terms of the elements of A. This can be done, using `scalarize` function.

```julia
julia> Symbolics.scalarize(AAt[2,3])
A[2, 1]*A[3, 1] + A[2, 2]*A[3, 2] + A[2, 3]*A[3, 3]

julia> @syms i::Int j::Int
(i, j)

julia> Symbolics.scalarize(AAt[i,j])
A[i, 1]*A[j, 1] + A[i, 2]*A[j, 2] + A[i, 3]*A[j, 3]
```

In general any scalar expression which is derived from array expressions can be scalarized.

```julia
julia> sum(A[:,1]) + sum(A[2,:])
Symbolics._mapreduce(identity, +, A[Colon(), 1], Colon(), (:init => false,)) + Symbolics._mapreduce(identity, +, A[2, Colon()], Colon(), (:init => false,))

julia> Symbolics.scalarize(sum(A[:,1]) + sum(A[2,:]))
A[1, 1] + A[2, 2] + A[2, 3] + A[4, 1] + A[5, 1] + 2A[2, 1] + A[3, 1]

```

## How arrays work
1 change: 0 additions & 1 deletion docs/src/manual/expression_manipulation.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ Other additional manipulation functions are given below.
```@docs
Symbolics.get_variables
Symbolics.tosymbol
Symbolics.makesym
Symbolics.diff2term
Symbolics.solve_for
```
13 changes: 10 additions & 3 deletions src/Symbolics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ export simplify, substitute

using SciMLBase, IfElse
export Num
using MacroTools
import MacroTools: splitdef, combinedef, postwalk, striplines
include("wrapper-types.jl")

include("num.jl")

export Equation, ConstrainedEquation
Expand All @@ -33,8 +37,9 @@ include("equations.jl")
include("utils.jl")
export degree

using MacroTools
import MacroTools: splitdef, combinedef, postwalk, striplines
using ConstructionBase
include("arrays.jl")

export @register
include("register.jl")

Expand All @@ -52,13 +57,15 @@ export Differential, expand_derivatives

include("diff.jl")

include("array-lib.jl")

include("linear_algebra.jl")

import Libdl
include("build_function.jl")
export build_function

using Distributions
import Distributions
include("extra_functions.jl")

using Latexify
Expand Down
Loading