Learn R Programming

genscore (version 1.0.0)

read_exponential: Parses the integer coefficient in an exponential term.

Description

Parses the integer coefficient in an exponential term.

Usage

read_exponential(s, has_index)

Arguments

s

A string that starts with one of the following forms: exp(x), exp(-x), exp(2x), exp(-2x), exp(12*x), exp(-123*x), followed by other terms. If has_index == TRUE, the first term should be rewritten in x with an index (e.g. exp(x1), exp(-2*x2)).

has_index

A logical, indicates whether the term is written in a component (e.g. x1, x2) as opposed to a uniform term (i.e. x).

Value

A list containing the following elements:

power_numer

An integer, the integer coefficient inside the first exponential term.

idx

An integer, the index of the term matched (e.g. 3 for exp(2*x3)). NULL if has_index == FALSE.

s

A string, the rest of the unparsed string.

If parsing is unsuccessful, NULL is returned.

Details

Parses the coefficient in the first exponential term and returns the rest of the terms.

Examples

Run this code
# NOT RUN {
# Unsuccessful parsing, not starting with exponential, returns \code{NULL}.
read_exponential("x", FALSE)
# Unsuccessful parsing, not starting with exponential, returns \code{NULL}.
read_exponential("x1^2+exp(2x2)", TRUE)
read_exponential("exp(x)", FALSE)
read_exponential("exp(x1)", TRUE)
read_exponential("exp(-x)", FALSE)
read_exponential("exp(-x1)+x2^2", TRUE)
read_exponential("exp(2x)", FALSE)
read_exponential("exp(2x1)+x2^(-2/3)", TRUE)
read_exponential("exp(-2x)", FALSE)
read_exponential("exp(-2x1)+exp(x3)", TRUE)
read_exponential("exp(12x)", FALSE)
read_exponential("exp(12x2)+x3^(-3)+x4^2", TRUE)
read_exponential("exp(-12x)", FALSE)
read_exponential("exp(-12x3)+x1^(2/5)+log(x2)", TRUE)
read_exponential("exp(123*x)", FALSE)
read_exponential("exp(123*x1)+x2^4", TRUE)
read_exponential("exp(-123*x)", FALSE)
read_exponential("exp(-123*x4)+exp(2*x3)", TRUE)
# }

Run the code above in your browser using DataLab