Skip to content

disambiguate equality added; all cn float #19

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 6 additions & 1 deletion src/maps.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ applymap = Dict{String,Function}(
"geq" => x -> H(x[1] - x[2]),
"gt" => x -> H(x[1] - x[2] - ϵ),

# equal nodes are not part of the MathML specification and
# are generated by disambiguate_equality!
"equal" => x -> H(x[1] - x[2]) * H(x[2] - x[1]),
"neq" => x -> 1 - H(x[1] - x[2]) * H(x[2] - x[1]),

# "lt" => x -> Base.foldl(Base.:<, x),
# "leq" => x -> Base.foldl(Base.:≤, x),
# "geq" => x -> Base.foldl(Base.:≥, x),
Expand Down Expand Up @@ -114,4 +119,4 @@ tagmap = Dict{String,Function}(
"math" => x -> map(parse_node, elements(x)),
"vector" => x -> map(parse_node, elements(x)),
"lambda" => parse_lambda,
)
)
6 changes: 3 additions & 3 deletions src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ function parse_cn(node)
if haskey(node, "type") && elements(node) != EzXML.Node[]
parse_cn_w_sep(node)
else
Meta.parse(node.content)
Float64(Meta.parse(node.content)) # convert to Float64 for CellML compatibility
end
end

Expand Down Expand Up @@ -149,14 +149,14 @@ parse a <lambda> node
```xml
<lambda>
<bvar> x1 </bvar><bvar> xn </bvar>
expression-in-x1-xn
expression-in-x1-xn
</lambda>
```
"""
function parse_lambda(node)
es = elements(node)
vars = findall("//x:bvar | //bvar", node, ["x" => MathML.mathml_ns])
# vars2 = findall("//bvar", node) # works in tests
# vars2 = findall("//bvar", node) # works in tests
# vars = union(vars, vars2) # FIX

args = first.(parse_bvar.(vars))
Expand Down
28 changes: 21 additions & 7 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ const mathml_ns = "http://www.w3.org/1998/Math/MathML"
"""
mathml_to_nums()

given a filename or an `EzXML.Document` or `EzXML.Node`,
given a filename or an `EzXML.Document` or `EzXML.Node`,
finds all of the <ci>s and defines them as Symbolics.jl Nums
returns a Vector{Num}.
Note, the root namespace needs to be MathML
"""
function mathml_to_nums end
function mathml_to_nums end

function mathml_to_nums(fn::AbstractString)
doc = readxml(fn)
Expand All @@ -32,8 +32,8 @@ end
"""
extract_mathml()

given a filename, `EzXML.Document`, or `EzXML.Node`
returns all of the MathML nodes.
given a filename, `EzXML.Document`, or `EzXML.Node`
returns all of the MathML nodes.
"""
function extract_mathml end

Expand All @@ -46,9 +46,23 @@ function extract_mathml(doc::EzXML.Document)
end

function extract_mathml(node::EzXML.Node)
disambiguate_equality!(node)
findall("//x:math", node, ["x" => mathml_ns])
end

"""
@disambiguate_equality!

utility function to replace <eq> inside piecewise subtrees to
disambiguate from the assignement <eq>
"""
function disambiguate_equality!(node)
nodes = findall("//x:piecewise//x:eq", node, ["x" => mathml_ns])
for n in nodes
setnodename!(n, "equal")
end
end

"""
@xml_str(s)

Expand All @@ -58,7 +72,7 @@ macro xml_str(s)
parsexml(s).root
end

"""
"""
@MathML_str(s)

utility macro for parsing xml strings into symbolics
Expand All @@ -82,8 +96,8 @@ function check_ivs(node)
all(y -> y.content == x[1].content, x)
end

# conditional hack
H(x) = IfElse.ifelse(x > 0, one(x), zero(x))
# conditional and rounding hacks
H(x) = IfElse.ifelse(x >= 0, one(x), zero(x))
const ϵ = eps(Float64)
frac(x) = 0.5 - atan(cot(π * x)) / π
heaviside_or(x) = length(x) == 1 ? x[1] : x[1] + heaviside_or(x[2:end]) - x[1] * heaviside_or(x[2:end])