findSubexprs: Find common subexpressions

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/derivs.R

Description

This function finds common subexpressions in an expression vector so that duplicate computation can be avoided.

Usage

1
findSubexprs(expr, simplify = FALSE, tag = ".expr", verbose = FALSE, ...)

Arguments

expr

An expression vector or language object.

simplify

Whether to call nlsSimplify on each subexpression before looking for common subexpressions.

tag

The prefix to use for locally created variables.

verbose

If TRUE, diagnostics are printed as simplifications are recognized.

...

Additional parameters to pass to nlsSimplify. Used only if simplify is TRUE.

Details

This function identifies all repeated subexpressions in an expression vector, and stores them in locally created variables. It is used by fnDeriv to share common subexpressions between expression evaluations and gradient evaluations, for example.

If simplify is TRUE, the assumptions behind the simplifications done by nlsSimplify must be valid for the result to match the input. With the default simplifications, this means that all variables should take finite real values.

Value

A language object which evaluates to an expression vector which would evaluate to the same result as the original vector with less duplicated code but more storage of intermediate results.

Author(s)

Duncan Murdoch

See Also

deriv in the stats package, nlsSimplify

Examples

1
findSubexprs(expression(x^2, x-y, y^2-x^2))

Example output

{
    .expr1 <- x^2
    expression(.expr1, x - y, y^2 - .expr1)
}

nlsr documentation built on Nov. 23, 2021, 3:01 a.m.