find_factors {lazy.symbolic}R Documentation

Decompose a Term into Multiplicative Factors separated by *, /, %*%, %\@%.

Description

Decompose a Term into Multiplicative Factors separated by *, /, %*%, %\@%.

Usage

find_factors(expr, markso = 1, sort = 0, debug = 0)

Arguments

expr

An expression string (scalar, vector or matrix)
Each element of expr must not contain multiple terms.

markso

= 0 not to mark the special opeartors

sort

= 1 to sort the factors according to their names

debug

= 1 to print the intermediate result

Details

It is assumed that there is just one character in between % and %.
The i-th factor with the preceding operator of expr is substr( expr0, fromto[i,1], fromto[i,2] ) where expr0 is the blank compressed expr.
The prefix operator associated with the i-th factor is oper[i].

The original expression can be recovered as:

res <- find_factors( expr )
expr <- paste( mapply( paste, res$oper, res$factor, sep="" ), collapse="" )

or

expr <- paste( paste( res$oper, res$factor, sep="" ), collapse="" )

or
#' expr <- paste( apply(res$tab,1, function(x){paste(x,collapse="")} ) , collapse="")
The unbalanced parens, if any, will be neglected but recorded in valid flag.

The factors can be sorted according to their names as follows:

res <- find_factors("A*d/a/c", sort=1)
exprs <- paste( paste( res$oper, res$factor, sep="" ), collapse="" )

res <- find_factors("A*c/(x+y)/d", sort=1)
exprs <- paste( paste( res$oper, res$factor, sep="" ), collapse="" )

Value

A list consisting of
# of factors (nfactors),
a vector of operators (oper),
a vector of factors (factor),
location of each oper+factor (fromto),
a matrix of operators and terms (tab) ,
a vector of validity flag of each term (valid),
ok flag (ok=1 if the input expr is equal to the reconstructed one.)

Examples

find_factors( "a*b/c*(d-e)*f(n)" )
find_factors( c("a*b/c*(d-e)*f(n)", "-2.13*a") )
find_factors( "a%*%b/c%*%(d-e)%*%f(n)" )
find_factors( "ab%*%cd*(b%p%q+a%m%c)/c*d/a%T%b" )
# error
find_factors( "a+b" )


[Package lazy.symbolic version 0.1.4 ]