find_terms {lazy.symbolic}R Documentation

Decompose an Expression into Additive Terms separated by + or -

Description

Decompose an Expression into Additive Terms separated by + or -

Usage

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

Arguments

expr

An expression string (scalar, vector or matrix)

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

The i-th term 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 term is oper[i].

The original expression can be recovered as:

res <- find_terms( expr )
expr <- paste( mapply( paste, res$oper, res$term, sep="" ), collapse="" )

or
expr <- paste( paste( res$oper, res$term, 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 terms can be sorted according to their names as follows:

res <- find_terms("c-a+d-b", sort=1)
exprs <- paste( paste( res$oper, res$term, sep="" ), collapse="" )

Value

A list consisting of
# of terms (nterms),
a vector of operators (oper),
a vector of terms (term),
location of each oper+term (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

res=find_terms( "-((a)) - c + ((a)+(b)) + (a-b) + 1 - (x-y)*(((z+1)))" )
paste( apply(res$tab,1, function(x){paste(x,collapse="")} ), collapse="")
find_terms("-a")
find_terms("x*a/(1-y)")
find_terms("-a+b-0")
find_terms("(A%p%B)%*%C-D%*%diag(X) %m% E")
find_terms(Simplify("-a+b-0"))

find_terms("(((b)+c")
find_terms(")*c+5")



[Package lazy.symbolic version 0.1.4 ]