Skip to content

Commit 61b6dd7

Browse files
Merge pull request #25 from magistere/deparse
Make 'deparse' function correctly handle parentheses
2 parents 903e80d + 0460d1d commit 61b6dd7

File tree

3 files changed

+32
-20
lines changed

3 files changed

+32
-20
lines changed

src/check_derivative.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
function check_derivative(f::Function, g::Function, x::Number)
22
auto_g = derivative(f)
3-
return max(abs(g(x) - auto_g(x)))
3+
return maximum(abs(g(x) - auto_g(x)))
44
end
55

66
function check_gradient{T <: Number}(f::Function, g::Function, x::Vector{T})
77
auto_g = gradient(f)
8-
return max(abs(g(x) - auto_g(x)))
8+
return maximum(abs(g(x) - auto_g(x)))
99
end
1010

1111
function check_second_derivative(f::Function, h::Function, x::Number)
1212
auto_h = second_derivative(f)
13-
return max(abs(h(x) - auto_h(x)))
13+
return maximum(abs(h(x) - auto_h(x)))
1414
end
1515

1616
function check_hessian{T <: Number}(f::Function, h::Function, x::Vector{T})
1717
auto_h = hessian(f)
18-
return max(abs(h(x) - auto_h(x)))
18+
return maximum(abs(h(x) - auto_h(x)))
1919
end

src/deparse.jl

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,30 @@
1+
const infix_ops = [:+, :-, :*, :/, :^]
2+
const op_priority = {:+ => 1, :- => 1, :* => 2, :/ => 2, :^ => 3}
3+
4+
isinfix(ex::Expr) = ex.head == :call && ex.args[1] in infix_ops
5+
isinfix(other) = false
6+
17
function deparse(ex::Expr)
28
if ex.head != :call
39
return string(ex)
4-
else
5-
if ex.args[1] in [:+, :-, :*, :/, :^]
6-
if length(ex.args) == 2
7-
return string(ex.args[1], deparse(ex.args[2]))
8-
else
9-
return join(map(x -> deparse(x), ex.args[2:end]),
10-
string(" ", string(ex.args[1]), " "))
11-
end
10+
end
11+
op = ex.args[1]
12+
args = ex.args[2:end]
13+
if !(op in infix_ops)
14+
return string(op, "(", join(map(deparse, args), ", "), ")")
15+
end
16+
if length(args) == 1
17+
return string(op, deparse(args[1]))
18+
end
19+
str = {}
20+
for subexpr in args
21+
if isinfix(subexpr) && op_priority[subexpr.args[1]] <= op_priority[op]
22+
push!(str, string("(", deparse(subexpr), ")"))
1223
else
13-
return string(ex.args[1],
14-
"(",
15-
join(map(x -> deparse(x), ex.args[2:end]), ", "),
16-
")")
24+
push!(str, deparse(subexpr))
1725
end
1826
end
27+
return join(str, string(" ", string(op), " "))
1928
end
20-
deparse(other::Any) = string(other)
2129

22-
# TODO: Examine string contents of inputs, insert parentheses if added:
23-
# + (CONTAINS * OR /)
24-
# - (CONTAINS * OR /)
30+
deparse(other) = string(other)

test/deparse.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,8 @@
11
@assert isequal(deparse(:(cos(x) + sin(x))), "cos(x) + sin(x)")
22
@assert isequal(deparse(:(cos(x) + sin(x) + exp(-x))), "cos(x) + sin(x) + exp(-x)")
3+
@assert isequal(deparse(parse("x+y*z")), "x + y * z")
4+
@assert isequal(deparse(parse("(x+y)*z")), "(x + y) * z")
5+
@assert isequal(deparse(parse("1/(x/y)")), "1 / (x / y)")
6+
@assert isequal(deparse(parse("1/(x*y)")), "1 / (x * y)")
7+
@assert isequal(deparse(parse("z^(x+y)")), "z ^ (x + y)")
8+
@assert isequal(deparse(parse("z^(x*y)")), "z ^ (x * y)")

0 commit comments

Comments
 (0)