Substitute symbols in an mvp object for numbers or other multivariate polynomials
subs(S,..., drop =TRUE)subsy(S,..., drop =TRUE)subvec(S,...)subsmvp(S,v,X)varchange(S,...)varchange_formal(S,old,new)namechanger(x,old,new)
Arguments
S,X: Multivariate polynomials
...: named arguments corresponding to variables to substitute
drop: 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
Returns
Functions subs(), subsy() and subsmvp() return a multivariate polynomial unless drop 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).
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.
Author(s)
Robin K. S. Hankin
See Also
drop
Examples
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,drop=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 vectorsM <- 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 matrixvarchange(as.mvp("1+x+xy + x*y"),x="newx")# variable xy unchangedkahle(5,3,1:3)|> subs(a="a + delta")varchange(p,a="]")# nonstandard variable names OKvarchange_formal(p,"\\]","a")