mvp (version 1.0-14)

subs: Substitution

Description

Substitute symbols in an mvp object for numbers or other multivariate polynomials

Usage

subs(S, ..., lose = TRUE)
subsy(S, ..., lose = TRUE)
subvec(S, ...)
subsmvp(S,v,X)
varchange(S,...)
varchange_formal(S,old,new)
namechanger(x,old,new)

Value

Functions subs(), subsy() and subsmvp() return a multivariate polynomial unless lose is TRUE in which case a length one numeric vector is returned. Function

subvec() returns a numeric vector (sic! the output inherits its order from the arguments).

Arguments

S,X

Multivariate polynomials

...

named arguments corresponding to variables to substitute

lose

Boolean with default TRUE meaning to return a scalar (the constant) in place of a constant mvp object

v

A string corresponding to the variable to substitute

old,new,x

The old and new variable names respectively; x is a character vector

Author

Robin K. S. Hankin

Details

Function subs() substitutes variables for mvp objects, using a natural R idiom. Observe that this type of substitution is sensitive to order:


> p <- as.mvp("a b^2")
> subs(p,a="b",b="x")
mvp object algebraically equal to
x^3
> subs(p,b="x",a="b")  # same arguments, different order
mvp object algebraically equal to
b x^2

Functions subsy() and subsmvp() are lower-level functions, not really intended for the end-user. Function subsy() substitutes variables for numeric values (order matters if a variable is substituted more than once). Function subsmpv() takes a mvp object and substitutes another mvp object for a specific symbol.

Function subvec() substitutes the symbols of S with numerical values. It is vectorised in its ellipsis arguments with recycling rules and names behaviour inherited from cbind(). However, if the first element of ... is a matrix, then this is interpreted by rows, with symbol names given by the matrix column names; further arguments are ignored. Unlike subs(), this function is generally only useful if all symbols are given a value; unassigned symbols take a value of zero.

Function varchange() makes a formal variable substitution. It is useful because it can take non-standard variable names such as “(a-b)” or “?”, and is used in taylor(). Function varchange_formal() does the same task, but takes two character vectors, old and new, which might be more convenient than passing named arguments. Remember that non-standard names might need to be quoted; also you might need to escape some characters, see the examples. Function namechanger() is a low-level helper function that uses regular expression idiom to substitute variable names.

See Also

lose

Examples

Run this code
p <- rmvp(6,2,2,letters[1:3])
p
subs(p,a=1)
subs(p,a=1,b=2)

subs(p,a="1+b x^3",b="1-y")
subs(p,a=1,b=2,c=3,lose=FALSE)

do.call(subs,c(list(as.mvp("z")),rep(c(z="C+z^2"),5)))

subvec(p,a=1,b=2,c=1:5)   # supply a named list of vectors

M <- matrix(sample(1:3,26*3,replace=TRUE),ncol=26)
colnames(M) <- letters
rownames(M) <- c("Huey", "Dewie", "Louie")
subvec(kahle(r=3,p=1:3),M)  # supply a matrix

varchange(as.mvp("1+x+xy + x*y"),x="newx") # variable xy unchanged

kahle(5,3,1:3) %>% subs(a="a + delta")

varchange(p,a="]")  # nonstandard variable names OK

varchange_formal(p,"\\]","a")

Run the code above in your browser using DataLab