find_a_term {lazy.symbolic}R Documentation

Find a term separated by a multiplicative operator

Description

Find a term separated by a multiplicative operator

Usage

find_a_term(expr, pattern = NULL, complete = 1)

Arguments

expr

A string containing an expression

pattern

A pattern to be used to identify a variable
The default pattern is "([[:digit:]]+|[[:alpha:]][[:alnum:]_]*)".

complete

= 0 to find the first variable, not the first term

Details

This function finds the first term of expr. The term is defined as the product (including division) of variables. A variable is defined as the string consisting of valid root characters trailed by subscripts in [ and ], if any. Those characters in a balanced parens can be regarded as a variable as well as a function reference such as f(args). Therefore, expr such as A[a,b] or f(args) are treated equally.

When complete=0, the function returns the first variable. WHen complete=1, the function returns the first term.

This function searches for a variable in expr from left to right. If a variable is found and it is trailed by a multiplicative operator, the search continues to the right again.

Value

If complete=1, a list of

  term  The term found w/o trailing and preceeding signs.
  loc   The starting location of the term in expr.
  len   The length of the term
  pre   The preceeding string to the left of the term, if any.
  post  The trailing string to the right of the term, if any.

term will be a valid expression but depending on the input, pre and post may not be valid expressions.

From expr the term can be obtained as

res <- find_a_term( expr )
term <- substr(expr,res$loc,res$loc+res$len-1)

See the example below.

Examples

find_a_term( "-a*b+c")
find_a_term( "-f(a*b)+c")
find_a_term( "a*F(b+c)")

find_a_term( "-a*b+c", complete=0)

expr="-b[i,j]*a+c(a,b))-f(x)+a2"
res=find_a_term( expr )
Print(res)
Print(substr(expr,res$loc,res$loc+res$len-1), res$term)


[Package lazy.symbolic version 0.1.4 ]