Skip to content
This repository
branch: master
file 223 lines (193 sloc) 5.731 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

export Symbolic, AbstractVariable, SymbolicVariable, BasicVariable, processExpr, @sexpr
export SymbolParameter, simplify
import Base.show, Base.(==)

#################################################################
#
# Symbolic types
# Symbolic - top of the tree
# AbstractVariable - inherit from this for custom symbolic
# variable types
# SymbolicVariable - use this in method argument typing
#
#################################################################

abstract Symbolic
abstract AbstractVariable <: Symbolic
typealias SymbolicVariable Union(Symbol, AbstractVariable)


#################################################################
#
# BasicVariable - an example to use during testing
#
#################################################################

type BasicVariable <: AbstractVariable
    sym::Symbol
end
# The following is probably too plain.
show(io::IO, x::BasicVariable) = print(io, x.sym)
(==)(x::BasicVariable, y::BasicVariable) = x.sym == y.sym


#################################################################
#
# @sexpr - return an Expr with variables spliced in
# processExpr - do the Expr splicing
#
#################################################################

function processExpr(x::Expr)
    if x.head == :call
        quoted = Expr(:quote,x.args[1])
        code = :(Expr(:call,$quoted))
        for y in x.args[2:end]
            push!(code.args,processExpr(y))
        end
        return code
    else
        return x
    end
end

processExpr(x::Any) = x

macro sexpr(x)
    esc(processExpr(x))
end


#################################################################
#
# SymbolParameter
# used to be able to dispatch on the symbol representing a
# function
#
#################################################################

type SymbolParameter{T}
end
SymbolParameter(s::Symbol) = SymbolParameter{s}()



#################################################################
#
# simplify()
#
#################################################################

isnumber(a) = isa(a, Number)

# Numbers and symbols can't be simplified further
simplify(x) = x
simplify(n::Number) = n
simplify(s::SymbolicVariable) = s

# The default is just to simplify arguments.
simplify{T}(x::SymbolParameter{T}, args) = Expr(:call, T, map(simplify, args)...)

function simplify(ex::Expr)
    if ex.head != :call
        return ex
    end
    if all(map(isnumber, ex.args[2:end]))
        return eval(ex)
    end
    new_ex = simplify(SymbolParameter(ex.args[1]), ex.args[2:end])
    while new_ex != ex
        new_ex, ex = simplify(new_ex), new_ex
    end
    return new_ex
end

function sum_numeric_args(args)
    sum = 0
    sym_args = {}
    for arg in args
        if isnumber(arg)
            sum += arg
        else
            push!(sym_args, arg)
        end
    end
    (sum, sym_args)
end

function mul_numeric_args(args)
    prod = 1
    sym_args = {}
    for arg in args
        if isnumber(arg)
            prod *= arg
        else
            push!(sym_args, arg)
        end
    end
    (prod, sym_args)
end

# Handle `args` of all lengths
function simplify(::SymbolParameter{:+}, args)
    # Remove any 0's in a sum
    args = map(simplify, filter(x -> x != 0, args))
    if length(args) == 0
        return 0
    # Special Case: simplify(:(+x)) == x
    elseif length(args) == 1
        return args[1]
    else
        (sum, sym_args) = sum_numeric_args(args)
        args = sum==0 ? sym_args : [sum, sym_args]
        return Expr(:call, :+, args...)
    end
end

isminus(ex::Expr) = ex.head == :call && ex.args[1] == :- && length(ex.args) == 2
isminus(ex) = false

# Assume length(args) == 3
function simplify(::SymbolParameter{:-}, args)
    # Remove any 0's in a subtraction
    args = map(simplify, filter(x -> x != 0, args))
    if length(args) == 0
        return 0
    # Special Case: simplify(:(x - x)) == 0
    elseif length(args) == 2 && args[1] == args[2]
        return 0
    # Special Case: simplify(:(x - (-y))) == x + y
    elseif length(args) == 2 && isminus(args[2])
        return Expr(:call, :+, args[1], args[2].args[2])
    else
        return Expr(:call, :-, args...)
    end
end

# Handle `args` of all lengths
function simplify(::SymbolParameter{:*}, args)
    # Remove any 1's in a product
    args = map(simplify, filter(x -> x != 1, args))
    if length(args) == 0
        return 1
    # Special Case: simplify(:(*(x))) == x
    elseif length(args) == 1
        return args[1]
    # Special Case: simplify(:(x * y * z * 0)) == 0
    elseif any(args .== 0)
        return 0
    # Special Case: simplify(:(*(-1,x))) == -x
    elseif length(args) == 2 && args[1] == -1
        return Expr(:call, :-, args[2])
    else
        (prod, sym_args) = mul_numeric_args(args)
        args = prod==1 ? sym_args : [prod, sym_args]
        return Expr(:call, :*, args...)
    end
end

# Assume length(args) == 3
function simplify(::SymbolParameter{:/}, args)
    args = map(simplify, args)
    # Special Case: simplify(:(x / 1)) == x
    if args[2] == 1
        return args[1]
    # Special Case: simplify(:(0 / x)) == 0
    elseif args[1] == 0
        return 0
    else
        return Expr(:call, :/, args...)
    end
end

# Assume length(args) == 3
function simplify(::SymbolParameter{:^}, args)
    args = map(simplify, args)
    # Special Case: simplify(:(x ^ 0)) == 1
    if args[2] == 0
        return 1
    # Special Case: simplify(:(x ^ 1)) == x
    elseif args[2] == 1
        return args[1]
    # Special Case: simplify(:(0 ^ x)) == 0
    elseif args[1] == 0
        return 0
    # Special Case: simplify(:(1 ^ x)) == 1
    elseif args[1] == 1
        return 1
    else
        return Expr(:call, :^, args...)
    end
end
Something went wrong with that request. Please try again.