Description Usage Arguments Examples
We try to recursivelys simplify the expression expr
by trying to evaluate all of its sub-expressions. A sub-expression
that has been successfully evaluated is replaced with the evaluation
result.
expression.simplify
is somewhat similar to eval
,
with the difference that it can deal with expressions which are only
partially evaluatable. The body of the function function(x) { 5+4+x }
,
for instance, cannot be fully evaluated if the value of x
is unknown.
We can, however, replace the 5+4
with a 9
.
Since this function is still an early draft, there may be many scenarios where it does not simplify an expression as much as it could be possible. Hopefully, we can impove this in the future and make it more powerful.
Additionally, this function makes sure that identical sub-expressions will be replaced by the exactly same object. This may have advantages, e.g., making the expression more cache friendly when the object is a vector.
1 | expression.simplify(expr, envir)
|
expr |
the expression to be simplified |
envir |
the environment in which the expression is to be simplified |
1 2 3 4 5 6 7 8 | expression.simplify(5+3)
# [1] 8
k <- 8
expression.simplify( ((5+3*k)-1)/k - 2^tan(k) )
# [1] 3.491024
f <- function(x) { 5+3+x }
expression.simplify(body(f), envir=environment(f))
# 8 + x
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.