Title: | Manipulation of Rational Functions |
---|---|
Description: | Functions to manipulate rational functions, including basic arithmetic operators, derivatives, and integrals with EXPLICIT forms. |
Authors: | Yixuan Qiu <[email protected]> |
Maintainer: | Yixuan Qiu <[email protected]> |
License: | GPL |
Version: | 0.1-1 |
Built: | 2024-11-01 02:52:15 UTC |
Source: | https://github.com/yixuan/rationalfun |
This function converts an object of class "rationalfun" to a character string.
## S3 method for class 'rationalfun' as.character(x, ...)
## S3 method for class 'rationalfun' as.character(x, ...)
x |
an object of class "rationalfun" |
... |
not used in this function |
A character string representing the rational function.
r <- rationalfun(c(1, 1), c(3, 2, 1)) as.character(r)
r <- rationalfun(c(1, 1), c(3, 2, 1)) as.character(r)
This function converts an object of class "rationalfun" to a function.
## S3 method for class 'rationalfun' as.function(x, ...)
## S3 method for class 'rationalfun' as.function(x, ...)
x |
an object of class "rationalfun" |
... |
not used in this function |
A function with one argument which could be a real or complex vector.
r <- rationalfun(c(1, 1), c(3, 2, 1)) r f <- as.function(r) f f(1:10) f(1:10 + (0+2i))
r <- rationalfun(c(1, 1), c(3, 2, 1)) r f <- as.function(r) f f(1:10) f(1:10 + (0+2i))
Calculate the derivative of a rational function. The returned value result is still an object of class "rationalfun".
## S3 method for class 'rationalfun' deriv(expr, ...)
## S3 method for class 'rationalfun' deriv(expr, ...)
expr |
an object of class "rationalfun" |
... |
not used in this function |
An object of class "rationalfun" representing the derivative of the original rational function.
# (x + 1) / (x^2 + x + 1) r <- rationalfun(c(1, 1), c(1, 1, 1)) deriv(r)
# (x + 1) / (x^2 + x + 1) r <- rationalfun(c(1, 1), c(1, 1, 1)) deriv(r)
Convert a function call to a function in R. In this
package, the function is typically used to convert the
result of integral.rationalfun()
to a
function with one argument.
int2fun(expr)
int2fun(expr)
expr |
a function call, typically returned by
|
A function with one argument which could be a real or complex vector.
x <- rationalfun(c(-6, -1, -8, 15, -1, 8, -9, 2), c(8, 12, 16, 4, 4)) int <- integral(x) fun <- int2fun(int) fun(c(0, 1))
x <- rationalfun(c(-6, -1, -8, 15, -1, 8, -9, 2), c(8, 12, 16, 4, 4)) int <- integral(x) fun <- int2fun(int) fun(c(0, 1))
Calculate the integral of a rational function. See "Details".
## S3 method for class 'rationalfun' integral(expr, ...)
## S3 method for class 'rationalfun' integral(expr, ...)
expr |
an object of class "rationalfun" |
... |
not used in this function |
The returned value is a function call with argument named
"x". That is, the integral is an expression in R with an
explicit form, which could be evaluated directly by
calling eval()
, or indirectly using the
int2fun()
function.
The algorithm is based on the Hermite-Ostrogradski formula which is discussed in the reference. See the article for more details.
A function call representing the explicit form of the integral.
T. N. Subramaniam, and Donald E. G. Malm, How to Integrate Rational Functions, The American Mathematical Monthly, Vol. 99, No.8 (1992), 762-772.
# (x + 1) / (x^2 + x + 1) r <- rationalfun(c(1, 1), c(1, 1, 1)) expr <- integral(r) # Evaluate the call directly eval(expr, list(x = 2)) # Use int2fun() f <- int2fun(expr) f(2)
# (x + 1) / (x^2 + x + 1) r <- rationalfun(c(1, 1), c(1, 1, 1)) expr <- integral(r) # Evaluate the call directly eval(expr, list(x = 2)) # Use int2fun() f <- int2fun(expr) f(2)
Basic arithmetic operators for rational functions.
## S3 method for class 'rationalfun' Ops(e1, e2)
## S3 method for class 'rationalfun' Ops(e1, e2)
e1 |
an object of class "rationalfun" |
e2 |
for |
A new object of "rationalfun" class.
r1 <- rationalfun(c(1, 2), c(1, 2, 1)) r2 <- rationalfun(c(1, 1), c(1, -2, 1)) r1 + r2 r1 * r2 r1^2
r1 <- rationalfun(c(1, 2), c(1, 2, 1)) r2 <- rationalfun(c(1, 1), c(1, -2, 1)) r1 + r2 r1 * r2 r1^2
Evaluate a rational function at a real or complex vector.
## S3 method for class 'rationalfun' predict(object, newdata, ...)
## S3 method for class 'rationalfun' predict(object, newdata, ...)
object |
an object of class "rationalfun" |
newdata |
a vector at which evaluation is requested. |
... |
not used in this function Both real and complex vectors are accepted. |
A vector of evaluated results.
r <- rationalfun(c(1, 1), c(3, 2, 1)) predict(r, 1:10)
r <- rationalfun(c(1, 1), c(3, 2, 1)) predict(r, 1:10)
Print a rational function in a fraction form.
## S3 method for class 'rationalfun' print(x, ...)
## S3 method for class 'rationalfun' print(x, ...)
x |
an object of class "rationalfun" |
... |
not used in this function |
Invisible, the object itself.
r <- rationalfun(c(1, 1), c(3, 2, 1)) print(r)
r <- rationalfun(c(1, 1), c(3, 2, 1)) print(r)
Construction of rational functions.
rationalfun(numer = c(0, 1), denom = c(1, 1, 1)) rfun(numer = c(0, 1), denom = c(1, 1, 1)) rationalfun.poly(numer = polynomial(c(0, 1)), denom = polynomial(c(1, 1, 1))) rfun.poly(numer = polynomial(c(0, 1)), denom = polynomial(c(1, 1, 1)))
rationalfun(numer = c(0, 1), denom = c(1, 1, 1)) rfun(numer = c(0, 1), denom = c(1, 1, 1)) rationalfun.poly(numer = polynomial(c(0, 1)), denom = polynomial(c(1, 1, 1))) rfun.poly(numer = polynomial(c(0, 1)), denom = polynomial(c(1, 1, 1)))
numer |
in |
denom |
similar to |
A rational function object could be constructed either by
calling rationalfun()
or by calling
rationalfun.poly()
.
rationalfun()
constructs a rational function from
the coefficient vectors of the numerator and the
denominator. For example, consider a rational function
where
and
, you may call
rationalfun(p[1:k], q[1:m])
to build the object.
For rationalfun.poly()
, it receives two objects of
class "polynomial" from the polynom package,
representing the polynomials of the numerator and the
denominator respectively. Use this function if you
already have objects of "polynomial" class, typically by
calling polynomial()
,
poly.calc()
or
poly.orth()
.
rfun()
and rfun.poly()
are aliases of
rationalfun()
and rationalfun.poly()
in
order to type fewer letters.
The value returned by rationalfun()
and
rationalfun.poly()
is an object of class
"rationalfun". You can coerce the object to a function,
by calling as.function.rationalfun()
, or to
a character string, by calling
as.character.rationalfun()
.
Objects of "ratioanlfun" class support basic operators
including "+"
, "-"
, "*"
, "/"
and "^"
. To evaluate a rational function at a
given vector, use predict.rationalfun()
. To
compute the derivative and integral in explicit
form, call deriv.rationalfun()
and
integral.rationalfun()
respectively.
An object of class "rationalfun".
polynomial
,
poly.calc
,
poly.orth
# (x + 1) / (x^2 + 2 * x + 3) r1 <- rationalfun(c(1, 1), c(3, 2, 1)) print(r1) # Construct from objects of 'polynomial' class if (require(polynom)) { p1 <- poly.calc(c(1, 2)) p2 <- polynomial(rep(1, 5)) r2 <- rfun.poly(p1, p2) print(r2) }
# (x + 1) / (x^2 + 2 * x + 3) r1 <- rationalfun(c(1, 1), c(3, 2, 1)) print(r1) # Construct from objects of 'polynomial' class if (require(polynom)) { p1 <- poly.calc(c(1, 2)) p2 <- polynomial(rep(1, 5)) r2 <- rfun.poly(p1, p2) print(r2) }
Simplify a rational function by dropping terms whose coefficients are close to zero, and then reducing it to an irreducible form.
simplify(x, ...)
simplify(x, ...)
x |
an object of class "rationalfun" |
... |
currently not used in this function |
A new object of class "rationalfun"representing the simplified rational function.
# (x + 1) / (x^2 + 2 * x + 1) ==> 1 / (x + 1) r <- rationalfun(c(1, 1), c(1, 2, 1)) simplify(r)
# (x + 1) / (x^2 + 2 * x + 1) ==> 1 / (x + 1) r <- rationalfun(c(1, 1), c(1, 2, 1)) simplify(r)