Skip to content

Commit 1af9e0d

Browse files
authored
Merge pull request #21 from anandijain/aj/fix2
#19 with test fixes
2 parents d3f91df + 6aa5d01 commit 1af9e0d

File tree

4 files changed

+33
-13
lines changed

4 files changed

+33
-13
lines changed

src/maps.jl

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ applymap = Dict{String,Function}(
1818
"geq" => x -> H(x[1] - x[2]),
1919
"gt" => x -> H(x[1] - x[2] - ϵ),
2020

21+
# equal nodes are not part of the MathML specification and
22+
# are generated by disambiguate_equality!
23+
"equal" => x -> H(x[1] - x[2]) * H(x[2] - x[1]),
24+
"neq" => x -> 1 - H(x[1] - x[2]) * H(x[2] - x[1]),
25+
2126
# "lt" => x -> Base.foldl(Base.:<, x),
2227
# "leq" => x -> Base.foldl(Base.:≤, x),
2328
# "geq" => x -> Base.foldl(Base.:≥, x),
@@ -114,4 +119,4 @@ tagmap = Dict{String,Function}(
114119
"math" => x -> map(parse_node, elements(x)),
115120
"vector" => x -> map(parse_node, elements(x)),
116121
"lambda" => parse_lambda,
117-
)
122+
)

src/parse.jl

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ function parse_cn(node)
3333
if haskey(node, "type") && elements(node) != EzXML.Node[]
3434
parse_cn_w_sep(node)
3535
else
36-
Meta.parse(node.content)
36+
Float64(Meta.parse(node.content)) # convert to Float64 for CellML compatibility
3737
end
3838
end
3939

@@ -136,6 +136,7 @@ parse a <diff>
136136
"""
137137
function parse_diff(a)
138138
(iv, deg), x = a
139+
deg = trunc(Int, deg)
139140
# num = Num(Symbolics.Sym{Symbolics.FnType{Tuple{Real},Real}}(Symbol(x))(iv))
140141
D = Differential(iv)^deg
141142
D(x)
@@ -149,14 +150,14 @@ parse a <lambda> node
149150
```xml
150151
<lambda>
151152
<bvar> x1 </bvar><bvar> xn </bvar>
152-
expression-in-x1-xn
153+
expression-in-x1-xn
153154
</lambda>
154155
```
155156
"""
156157
function parse_lambda(node)
157158
es = elements(node)
158159
vars = findall("//x:bvar | //bvar", node, ["x" => MathML.mathml_ns])
159-
# vars2 = findall("//bvar", node) # works in tests
160+
# vars2 = findall("//bvar", node) # works in tests
160161
# vars = union(vars, vars2) # FIX
161162

162163
args = first.(parse_bvar.(vars))

src/utils.jl

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@ const mathml_ns = "http://www.w3.org/1998/Math/MathML"
33
"""
44
mathml_to_nums()
55
6-
given a filename or an `EzXML.Document` or `EzXML.Node`,
6+
given a filename or an `EzXML.Document` or `EzXML.Node`,
77
finds all of the <ci>s and defines them as Symbolics.jl Nums
88
returns a Vector{Num}.
99
Note, the root namespace needs to be MathML
1010
"""
11-
function mathml_to_nums end
11+
function mathml_to_nums end
1212

1313
function mathml_to_nums(fn::AbstractString)
1414
doc = readxml(fn)
@@ -32,8 +32,8 @@ end
3232
"""
3333
extract_mathml()
3434
35-
given a filename, `EzXML.Document`, or `EzXML.Node`
36-
returns all of the MathML nodes.
35+
given a filename, `EzXML.Document`, or `EzXML.Node`
36+
returns all of the MathML nodes.
3737
"""
3838
function extract_mathml end
3939

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

4848
function extract_mathml(node::EzXML.Node)
49+
disambiguate_equality!(node)
4950
findall("//x:math", node, ["x" => mathml_ns])
5051
end
5152

53+
"""
54+
@disambiguate_equality!
55+
56+
utility function to replace <eq> inside piecewise subtrees to
57+
disambiguate from the assignement <eq>
58+
"""
59+
function disambiguate_equality!(node)
60+
nodes = findall("//x:piecewise//x:eq", node, ["x" => mathml_ns])
61+
for n in nodes
62+
setnodename!(n, "equal")
63+
end
64+
end
65+
5266
"""
5367
@xml_str(s)
5468
@@ -58,7 +72,7 @@ macro xml_str(s)
5872
parsexml(s).root
5973
end
6074

61-
"""
75+
"""
6276
@MathML_str(s)
6377
6478
utility macro for parsing xml strings into symbolics
@@ -82,8 +96,8 @@ function check_ivs(node)
8296
all(y -> y.content == x[1].content, x)
8397
end
8498

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

test/parse.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ str = """
9999
"""
100100
@test isequal(MathML.parse_str(str),
101101
IfElse.ifelse(
102-
IfElse.ifelse(1 - t > 0, 1, 0) > 0.5,
103-
x * (y + a * z) * ((1 - (b * z))^-1),
102+
IfElse.ifelse(1. - t >= 0, 1, 0) > 0.5,
103+
x * (y + a * z) * ((1.0 - (b * z))^-1),
104104
x * y))
105105

106106
# factorial

0 commit comments

Comments
 (0)