Learn R Programming

polyMatrix (version 0.3.1)

predict.polynomial: Calculate the value of a ‘polynomial’ for real, complex and matrix argument

Description

The ‘polynom::predict.polynomial()’ method works for real, complex and matrix argument also. But in the case of matrix argument it uses the elementwise calculations instead the matrix multiplication. The ‘polyMatrix::predict.polynomial()’ calculate the result by the usual matrix product definition except the case, when the parameter meth equals by "as.in.the.polynom.package".

Usage

# S3 method for polynomial
predict(object,M,meth=c("as.matrix","as.in.the.polynom.package"),…)

Arguments

object

a numeric, matrix or polyMatrix class object

M

a polynomial class object

meth

If the value of this parameter as the default, "as.matrix" then calculate by matrix multiplications. Otherwise it calculate by elementwise multiplications.

additional arguments

Value

The is a numeric, numeric, matrix or polyMatrix class object depending on the class of the input

See Also

predict.polyMatrix, predict.charpn

Examples

Run this code
# NOT RUN {
p <- polynom::polynomial(1:3)
p # 1 + 2*x + 3*x^2

# predict a polynom for real values
predict(p,1) # 6

# predict a polynom for complex values
predict(p,1i) # -2+2i

# predict a polynom for matrices
M <- matrix(c(1,-1),2,2);
M
predict(p,M) # 3,2 \ -2, -1

# mimiking the elementwise calculation of the masked "polynom::predict" method
predict(p,M,meth="as.in.") # 6, 6 \ 2, 2

# predict a polynom for a polynomial matrix
pM <- polyMgen.d(2,2, rawData=ch2pn(c("1","x^2","x","0")))
pM # 1, x \ x^2, 0
predict(p,pM) # method: predict.polyMatrix()
# 6 + 3*z^3  5*z
# 5*z^2      1 + 3*z^3

# ---
# predict the result of a linear model
x <- c(1,3,4,1,0);
y <- c(27,81,54,27,18)
predict(lm(y~x)) # method: predict.lm()
# 31 57 70 31 18

# clean up
# rm(p, M, pM, x, y)
# }

Run the code above in your browser using DataLab