72 lines
2.5 KiB
Julia
72 lines
2.5 KiB
Julia
|
module ExpressionProcessing
|
||
|
|
||
|
export construct_symtable
|
||
|
export replace_variables!
|
||
|
export expr_to_postfix
|
||
|
export SymbolTable64
|
||
|
|
||
|
const SymbolTable64 = Dict{Tuple{Expr, Symbol},Float64}
|
||
|
|
||
|
# Maybe switch from Array{String} to Array{Union{Float64, String}}?
|
||
|
function expr_to_postfix(expr::Expr)::Array{String}
|
||
|
postfix = Array{String,1}()
|
||
|
operator = String(expr.args[1])
|
||
|
|
||
|
push!(postfix, expr.args[2], expr.args[3], operator)
|
||
|
for i in 4:length(expr.args)
|
||
|
arg = expr.args[i]
|
||
|
if typeof(arg) === Expr
|
||
|
push!(postfix, expr_to_postfix(ex))
|
||
|
else
|
||
|
push!(postfix, string(arg), operator)
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
"Replaces all the variables and parameters of the given expression with their corresponding value stored in the symtable
|
||
|
# Arguments
|
||
|
- `symtable::SymbolTable64`: Contains the values of all variables for each expression
|
||
|
- `originalExpr::Expr`: Contains a deep copy of the original expression. It is used to link the expression and variables to their according value stored in the symtable
|
||
|
"
|
||
|
function replace_variables!(ex::Expr, symtable::SymbolTable64, originalExpr::Expr)
|
||
|
for i in 1:length(ex.args)
|
||
|
arg = ex.args[i]
|
||
|
if typeof(arg) === Expr
|
||
|
replace_variables!(arg, symtable, originalExpr)
|
||
|
elseif haskey(symtable, (originalExpr,arg)) # We found a variable/parameter and can replace it with the actual value
|
||
|
println("Replacing")
|
||
|
ex.args[i] = symtable[(originalExpr,arg)]
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
# TODO: Completly rewrite this function because I misunderstood it. Not every column is linked to an expression. therefore all other functions need to be reworked as well. Probably can't replace the variables in julia anymore, look into this. (see ExpressionExecutorCuda.jl for more info)
|
||
|
# Before rewriting, proceed with just creating a postfix notation and sending the variable matrix as well as the parameter "matrix" to the GPU to perform first calculations
|
||
|
function construct_symtable(expressions::Vector{Expr}, mat::Matrix{Float64}, params::Vector{Vector{Float64}})::SymbolTable64
|
||
|
symtable = SymbolTable64()
|
||
|
|
||
|
for i in eachindex(expressions)
|
||
|
expr = expressions[i]
|
||
|
values = mat[i,:]
|
||
|
parameters = params[i]
|
||
|
|
||
|
fill_symtable!(expr, symtable, values, "x")
|
||
|
fill_symtable!(expr, symtable, parameters, "p")
|
||
|
end
|
||
|
|
||
|
return symtable
|
||
|
end
|
||
|
|
||
|
function fill_symtable!(expr::Expr, symtable::SymbolTable64, values::Vector{Float64}, symbolPrefix::String)
|
||
|
varIndex = 1
|
||
|
for j in eachindex(values)
|
||
|
val = values[j]
|
||
|
sym = Symbol(symbolPrefix, varIndex)
|
||
|
|
||
|
symtable[expr,sym] = val
|
||
|
varIndex += 1
|
||
|
end
|
||
|
end
|
||
|
|
||
|
end
|