-
Notifications
You must be signed in to change notification settings - Fork 43
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
At vars #266
Open
jverzani
wants to merge
9
commits into
symengine:master
Choose a base branch
from
jverzani:at_vars
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
At vars #266
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4a07f2c
add SymbolicUtils extension
jverzani b177158
cleanup
jverzani fdfecfe
rework vars macro; wrap more functions
jverzani 0c8a982
d arc functions; test with repr
jverzani 3d1fc3e
remove extension commit
jverzani 9d71e57
add tests, clean up
jverzani 2e0b4e9
dropped this
jverzani 17aa888
Merge branch 'master' into at_vars
jverzani dc88761
Update LICENSE
isuruf File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
# !!! Note: | ||
# Many thanks to `@matthieubulte` for this contribution to `SymPy`. | ||
|
||
# The map_subscripts function is stolen from Symbolics.jl | ||
const IndexMap = Dict{Char,Char}( | ||
'-' => '₋', | ||
'0' => '₀', | ||
'1' => '₁', | ||
'2' => '₂', | ||
'3' => '₃', | ||
'4' => '₄', | ||
'5' => '₅', | ||
'6' => '₆', | ||
'7' => '₇', | ||
'8' => '₈', | ||
'9' => '₉') | ||
|
||
function map_subscripts(indices) | ||
str = string(indices) | ||
join(IndexMap[c] for c in str) | ||
end | ||
|
||
# Define a type hierarchy to describe a variable declaration. This is mainly for convenient pattern matching later. | ||
abstract type VarDecl end | ||
|
||
struct SymDecl <: VarDecl | ||
sym :: Symbol | ||
end | ||
|
||
struct NamedDecl <: VarDecl | ||
name :: String | ||
rest :: VarDecl | ||
end | ||
|
||
struct FunctionDecl <: VarDecl | ||
rest :: VarDecl | ||
end | ||
|
||
struct TensorDecl <: VarDecl | ||
ranges :: Vector{AbstractRange} | ||
rest :: VarDecl | ||
end | ||
|
||
struct AssumptionsDecl <: VarDecl | ||
assumptions :: Vector{Symbol} | ||
rest :: VarDecl | ||
end | ||
|
||
# Transform a Decl struct in an Expression that calls SymPy to declare the corresponding symbol | ||
function gendecl(x::VarDecl) | ||
asstokw(a) = Expr(:kw, esc(a), true) | ||
val = :($(ctor(x))($(name(x, missing)), $(map(asstokw, assumptions(x))...))) | ||
:($(esc(sym(x))) = $(genreshape(val, x))) | ||
end | ||
|
||
# Transform an expression in a Decl struct | ||
function parsedecl(expr) | ||
# @vars x | ||
if isa(expr, Symbol) | ||
return SymDecl(expr) | ||
|
||
# @vars x::assumptions, where assumption = assumptionkw | (assumptionkw...) | ||
#= no assumptions in SymEngine | ||
elseif isa(expr, Expr) && expr.head == :(::) | ||
symexpr, assumptions = expr.args | ||
assumptions = isa(assumptions, Symbol) ? [assumptions] : assumptions.args | ||
return AssumptionsDecl(assumptions, parsedecl(symexpr)) | ||
=# | ||
|
||
# @vars x=>"name" | ||
elseif isa(expr, Expr) && expr.head == :call && expr.args[1] == :(=>) | ||
length(expr.args) == 3 || parseerror() | ||
isa(expr.args[3], String) || parseerror() | ||
|
||
expr, strname = expr.args[2:end] | ||
return NamedDecl(strname, parsedecl(expr)) | ||
|
||
# @vars x() | ||
elseif isa(expr, Expr) && expr.head == :call && expr.args[1] != :(=>) | ||
length(expr.args) == 1 || parseerror() | ||
return FunctionDecl(parsedecl(expr.args[1])) | ||
|
||
# @vars x[1:5, 3:9] | ||
elseif isa(expr, Expr) && expr.head == :ref | ||
length(expr.args) > 1 || parseerror() | ||
ranges = map(parserange, expr.args[2:end]) | ||
return TensorDecl(ranges, parsedecl(expr.args[1])) | ||
else | ||
parseerror() | ||
end | ||
end | ||
|
||
function parserange(expr) | ||
range = eval(expr) | ||
isa(range, AbstractRange) || parseerror() | ||
range | ||
end | ||
|
||
sym(x::SymDecl) = x.sym | ||
sym(x::NamedDecl) = sym(x.rest) | ||
sym(x::FunctionDecl) = sym(x.rest) | ||
sym(x::TensorDecl) = sym(x.rest) | ||
sym(x::AssumptionsDecl) = sym(x.rest) | ||
|
||
ctor(::SymDecl) = :symbols | ||
ctor(x::NamedDecl) = ctor(x.rest) | ||
ctor(::FunctionDecl) = :SymFunction | ||
ctor(x::TensorDecl) = ctor(x.rest) | ||
ctor(x::AssumptionsDecl) = ctor(x.rest) | ||
|
||
assumptions(::SymDecl) = [] | ||
assumptions(x::NamedDecl) = assumptions(x.rest) | ||
assumptions(x::FunctionDecl) = assumptions(x.rest) | ||
assumptions(x::TensorDecl) = assumptions(x.rest) | ||
assumptions(x::AssumptionsDecl) = x.assumptions | ||
|
||
# Reshape is not used by most nodes, but TensorNodes require the output to be given | ||
# the shape matching the specification. For instance if @vars x[1:3, 2:6], we should | ||
# have size(x) = (3, 5) | ||
genreshape(expr, ::SymDecl) = expr | ||
genreshape(expr, x::NamedDecl) = genreshape(expr, x.rest) | ||
genreshape(expr, x::FunctionDecl) = genreshape(expr, x.rest) | ||
genreshape(expr, x::TensorDecl) = let | ||
shape = tuple(length.(x.ranges)...) | ||
:(reshape(collect($(expr)), $(shape))) | ||
end | ||
genreshape(expr, x::AssumptionsDecl) = genreshape(expr, x.rest) | ||
|
||
# To find out the name, we need to traverse in both directions to make sure that each node can get | ||
# information from parents and children about possible name. | ||
# This is done because the expr tree will always look like NamedDecl -> ... -> TensorDecl -> ... -> SymDecl | ||
# and the TensorDecl node will need to know if it should create names base on a NamedDecl parent or | ||
# based on the SymDecl leaf. | ||
name(x::SymDecl, parentname) = coalesce(parentname, String(x.sym)) | ||
name(x::NamedDecl, parentname) = coalesce(name(x.rest, x.name), x.name) | ||
name(x::FunctionDecl, parentname) = name(x.rest, parentname) | ||
name(x::AssumptionsDecl, parentname) = name(x.rest, parentname) | ||
name(x::TensorDecl, parentname) = let | ||
basename = name(x.rest, parentname) | ||
# we need to double reverse the indices to make sure that we traverse them in the natural order | ||
namestensor = map(Iterators.product(x.ranges...)) do ind | ||
sub = join(map(map_subscripts, ind), "_") | ||
string(basename, sub) | ||
end | ||
join(namestensor[:], ", ") | ||
end | ||
|
||
function parseerror() | ||
error("Incorrect @vars syntax. Try `@vars x=>\"x₀\" y() z[0:4]` for instance.") | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are the licenses for these codes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question, MIT (https://github.com/JuliaPy/SymPy.jl/blob/master/LICENSE.md)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and https://github.com/JuliaSymbolics/Symbolics.jl/blob/master/LICENSE.md