Skip to content

Commit

Permalink
refactored some code into new package
Browse files Browse the repository at this point in the history
  • Loading branch information
chakravala committed Apr 15, 2018
1 parent acb5659 commit 623a45e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function linefilter(e::Expr)
end

"""
treefork!(::Expr)
treecombine!(::Expr)
Recursively simplifies out extra edges from Expr objects
"""
Expand Down
34 changes: 17 additions & 17 deletions src/rexpr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function rtrim(r::Array{Compat.String,1})
h = 1
l = length(r)
while h l
isempty(r[h]) ? (deleteat!(n,h); l -= 1) : (h += 1)
isempty(n[h]) ? (deleteat!(n,h); l -= 1) : (h += 1)
end
return n
end
Expand Down Expand Up @@ -244,6 +244,17 @@ ColCheck = ( () -> begin
return (tf=gs)->(gstf && (gs=tf); return gs)
end)()

"""
Reduce.DisplayLog(::Bool)
Toggle whether to display the log of REDUCE commands
"""
DisplayLog = ( () -> begin
gs = false
return (tf=gs)->(gstf && (gs=tf); return gs)
end)()


@inline function SubReplace(sym::Symbol,str::String)
a = matchall(r"([^ ()+*\^\/-]+|[()+*\^\/-])",str)
for s 1:length(a)
Expand Down Expand Up @@ -356,10 +367,12 @@ julia> R\"int(sin(x), x)\" |> RExpr |> rcall
!(o == :factor) && write(offs,"off $o\$ ")
!(o in [offlist;[:factor]]) && write(offr,"; on $o")
end
write(rs,String(UInt8[take!(ons)...,take!(offs)...]) *
wrs = String(UInt8[take!(ons)...,take!(offs)...]) *
string(r) *
String(UInt8[take!(onr)...,take!(offr)...]))
mode ? (sp = readsp(rs)) : (sp = read(rs))
String(UInt8[take!(onr)...,take!(offr)...])
DisplayLog() && println(wrs)
write(rs,wrs)
sp = mode ? readsp(rs) : read(rs)
expo && rcall(R"off exp")
mode && for h 1:length(sp)
sp[h] = replace(sp[h],'\n' => "")
Expand Down Expand Up @@ -416,19 +429,6 @@ end

#getindex(r::RExpr, i) = "$r($i)" |> rcall

function callcount(expr)
c = 0
if typeof(expr) == Expr
if expr.head == :call
c += 1
end
for arg expr.args
c += callcount(arg)
end
end
return c
end

#= regroup parens * add feature
@noinline function squash(expr)
Expand Down
58 changes: 0 additions & 58 deletions src/switch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,61 +59,3 @@ for fun in switches
$fun(expr;be=0) = expr
end
end

horner(x,a) = horner(x,a,1)
horner(x,a::Array{<:Any,1},k) = k == length(a) ? a[k] : a[k] + x*horner(x,a,k+1)

factor(x,a) = factor(x,a,1)
factor(x,a::Array{<:Any,1},k) = k == length(a) ? x-a[k] : (x-a[k])*factor(x,a,k+1)

expand(x,a) = expand(x,a,length(a))
expand(x,a::Array{<:Any,1},k) = k == 1 ? a[k] : a[k]*x^(k-1) + expand(x,a,k-1)

function expravg(expr)
cs = 0
s = 0.0
cp = 0
p = 0.0
if typeof(expr) == Expr
if expr.head == :call && expr.args[1] == :^ &&
expr.args[3] |> typeof <: Number
cp += 1
p += abs(expr.args[3])
(cst,st,cpt,pt) = expravg(expr.args[2])
cs += cst
s += cst*st
cp += cpt
p += cpt*pt
else
for arg expr.args
(cst,st,cpt,pt) = expravg(arg)
cs += cst
s += cst*st
cp += cpt
p += cpt*pt
end
end
elseif typeof(expr) <: Number
cs += 1
s += log(abs(expr))
end
return (cs,cs == 0 ? 0.0 : s/cs,cp,cp == 0 ? 1.0 : p/cp)
end

function exprval(expr)
val = expravg(expr)
val[2]*val[4]*callcount(expr)
end

export optimal

function optimal(expr)
h = horner(expr)
f = factor(h)
if exprval(h) exprval(f)
return h
else
return f
end
end

0 comments on commit 623a45e

Please sign in to comment.