Title: | Multivariate Polynomials |
---|---|
Description: | Various utilities to manipulate multivariate polynomials. The package is almost completely superceded by the 'spray' and 'mvp' packages, which are much more efficient. |
Authors: | Robin K. S. Hankin [aut, cre] |
Maintainer: | Robin K. S. Hankin <[email protected]> |
License: | GPL |
Version: | 1.0-9 |
Built: | 2024-12-12 03:10:29 UTC |
Source: | https://github.com/robinhankin/multipol |
Various tools to manipulate and combine multivariate polynomials
Multidimensional arrays are interpreted in a natural way as multivariate polynomials.
Taking a matrix a
as an example, because this has two dimensions
it may be viewed as a bivariate polynomial with a[i,j]
being the
coefficient of . Note the off-by-one issue; see
?Extract
.
Multivariate polynomials of arbitrary arity are a straightforward generalization using appropriately dimensioned arrays.
Arithmetic operations “+
”,“-
”,
“*
”, “^
” operate as though their arguments
are multivariate polynomials.
Even quite small multipols are computationally intense; many coefficients have to be calculated and each is the sum of many terms.
The package is almost completely superceded by the spray and mvp packages, which use a sparse array system for efficiency.
Robin K. S. Hankin [aut, cre] (<https://orcid.org/0000-0001-5982-0415>)
Maintainer: Robin K. S. Hankin <[email protected]>
ones(2)*linear(c(1,-1)) # x^2-y^2 ones(2)*(ones(2,2)-uni(2)) # x^3+y^3 a <- as.multipol(matrix(1:12,3,4)) a a[1,1] <- 11 f <- as.function(a*a) f(c(1,pi))
ones(2)*linear(c(1,-1)) # x^2-y^2 ones(2)*(ones(2,2)-uni(2)) # x^3+y^3 a <- as.multipol(matrix(1:12,3,4)) a a[1,1] <- 11 f <- as.function(a*a) f(c(1,pi))
Coerce multipols to arrays; unclass
## S3 method for class 'multipol' as.array(x, ...)
## S3 method for class 'multipol' as.array(x, ...)
x |
multipol |
... |
Further arguments passed to |
Robin K. S. Hankin
a <- as.multipol(matrix(1,2,2)) as.array(a)
a <- as.multipol(matrix(1,2,2)) as.array(a)
Coerce a multipol to a function using environments
## S3 method for class 'multipol' as.function(x, ...)
## S3 method for class 'multipol' as.function(x, ...)
x |
A multipol |
... |
Further arguments, currently ignored |
Robin K. S. Hankin
a <- as.multipol(array (1:12, c(2,3,2))) f1 <- as.function(a) f2 <- as.function(a*a) x <- matrix(rnorm(15),ncol=3) f1(x)^2 - f2(x) #should be zero [non-trivial!]
a <- as.multipol(array (1:12, c(2,3,2))) f1 <- as.function(a) f2 <- as.function(a*a) x <- matrix(rnorm(15),ncol=3) f1(x)^2 - f2(x) #should be zero [non-trivial!]
Various useful multivariate polynomials such as homogeneous polynomials, linear polynomials, etc
constant(d) product(x) homog(d, n = d, value = 1) linear(x, power = 1) lone(d,x) single(d, e, power = 1) uni(d) zero(d)
constant(d) product(x) homog(d, n = d, value = 1) linear(x, power = 1) lone(d,x) single(d, e, power = 1) uni(d) zero(d)
d |
Integer giving the dimensionality (arity) of the result |
x |
A vector of integers |
n , e , power
|
Integers |
value |
Value for linear multivariate polynomial |
In the following, all multipols have their nonzero entries 1 unless otherwise stated.
Function constant(d)
returns the constant multivariate
polynomial of arity d
Function product(x)
returns a multipol of arity
length(x)
where all(dim(product(x))==x)
with all zero
entries except the one corresponding to
Function homog(d,n)
returns the homogeneous multipol of
arity d
and power n
. The coeffients are set to
value
(default 1); standard recycling is used
Function linear(x)
returns a multipol of arity
length(x)
which is linear in all its arguments and whose
coefficients are the elements of x
. Argument power
returns an equivalent multipol linear in x^power
Function lone(d,x)
returns a multipol of arity d
that is a product of variables x[i]
Function single(d,e,power)
returns a multipol of arity
d
with a single nonzero entry corresponding to dimension
e
raised to the power power
Function uni(d)
returns x1*x2*...*xd
[it is a
convenience wrapper for product(rep(1,d))
]
Function zero(d)
returns the zero multipol of arity
d
[it is a convenience wrapper for 0*constant(d)
]
Function ones(d)
returns x1+x2+...+xd
[it is a
convenience wrapper for linear(rep(1,d))
]
In many ways, the functions documented in this section are an adverisement for the inefficiency of dealing with multipols using arrays: sparse arrays would be the natural solution.
Robin K. S. Hankin
product(c(1,2,5)) # x * y^2 * z^5 uni(3) # xyz single(3,1) # x single(3,2) # y single(3,3) # z single(3,1,6) # x^6 single(3,2,6) # y^6 lone(3,1:2) # xy lone(3,c(1,3)) # xz linear(c(1,2,5)) # x + 2y + 5z ones(3) # x+y+z constant(3) # 1 + 0x + 0y + 0z zero(3) # 0 + 0x + 0y + 0z homog(3,2) # x^2 + y^2 + z^2 + xy + xz + yz # now some multivariate factorization: ones(2)*linear(c(1,-1)) # x^2-y^2 ones(2)*(linear(c(1,1),2)-uni(2)) # x^3+y^3 linear(c(1,-1))*homog(2,2) # x^3+y^3 again ones(2)*(ones(2,4)+uni(2)^2-product(c(1,3))-product(c(3,1))) # x^5+y^5 ones(2)*homog(2,4,c(1,-1,1,-1,1)) # x^5+y^5 again
product(c(1,2,5)) # x * y^2 * z^5 uni(3) # xyz single(3,1) # x single(3,2) # y single(3,3) # z single(3,1,6) # x^6 single(3,2,6) # y^6 lone(3,1:2) # xy lone(3,c(1,3)) # xz linear(c(1,2,5)) # x + 2y + 5z ones(3) # x+y+z constant(3) # 1 + 0x + 0y + 0z zero(3) # 0 + 0x + 0y + 0z homog(3,2) # x^2 + y^2 + z^2 + xy + xz + yz # now some multivariate factorization: ones(2)*linear(c(1,-1)) # x^2-y^2 ones(2)*(linear(c(1,1),2)-uni(2)) # x^3+y^3 linear(c(1,-1))*homog(2,2) # x^3+y^3 again ones(2)*(ones(2,4)+uni(2)^2-product(c(1,3))-product(c(3,1))) # x^5+y^5 ones(2)*homog(2,4,c(1,-1,1,-1,1)) # x^5+y^5 again
Partial differentiation with respect to any variable
## S3 method for class 'multipol' deriv(expr, i, derivative = 1, ...)
## S3 method for class 'multipol' deriv(expr, i, derivative = 1, ...)
expr |
A multipol |
i |
Dimension to differentiate with respect to |
derivative |
How many times to differentiate |
... |
Further arguments, currently ignored |
Robin K. S. Hankin
a <- as.multipol(matrix(1:12,3,4)) deriv(a,1) # standard usage: derivfferentiate WRT x1 deriv(a,2) # differentiate WRT x2 deriv(a,1,2) # second derivative deriv(a,1,3) # third derivative (zero multipol)
a <- as.multipol(matrix(1:12,3,4)) deriv(a,1) # standard usage: derivfferentiate WRT x1 deriv(a,2) # differentiate WRT x2 deriv(a,1,2) # second derivative deriv(a,1,3) # third derivative (zero multipol)
Extract or replace subsets of multipols
## S3 method for class 'multipol' x[...] ## S3 replacement method for class 'multipol' x[...] <- value
## S3 method for class 'multipol' x[...] ## S3 replacement method for class 'multipol' x[...] <- value
x |
A multipol |
... |
Indices to replace. Offset zero! See details section |
value |
replacement value |
Extraction and replacement operate with offset zero (using functions
taken from the Oarray package); see the examples section. This
is so that the index matches the power required (there is an
off-by-one issue. The first element corresponds to the
zeroth power. One wants index i
to extract/replace the
-th power and in particular one wants index
0
to
extract/replace the zeroth power).
Replacement operators return a multipol. Extraction returns an array. This is because it is often not clear exactly what multipol is desired from an extraction operation (it is also consistent with Oarray's behaviour).
Original code taken from the Oarray package by Jonty Rougier
Jonathan Rougier (2007). Oarray: Arrays with arbitrary offsets. R package version 1.4-2.
a <- as.multipol(matrix(1,4,6)) a[2,2] <- 100 a # coefficient of x1^2.x2^2 is 100 a[1:2,1:2] # a matrix. Note this corresponds to first and second powers # not zeroth and first (what multipol would you want here?) a[2,2] # 100 to match the "a[2,2] <- 100" assignment above
a <- as.multipol(matrix(1,4,6)) a[2,2] <- 100 a # coefficient of x1^2.x2^2 is 100 a[1:2,1:2] # a matrix. Note this corresponds to first and second powers # not zeroth and first (what multipol would you want here?) a[2,2] # 100 to match the "a[2,2] <- 100" assignment above
Is a multivariate polynomial constant or zero?
is.constant(a, allow.untrimmed = TRUE) is.zero(a, allow.untrimmed = TRUE)
is.constant(a, allow.untrimmed = TRUE) is.zero(a, allow.untrimmed = TRUE)
a |
A multipol |
allow.untrimmed |
Boolean with default |
Robin K. S. Hankin
is.zero(linear(c(1,1i))*linear(c(1,-1i)) - ones(2,2)) # factorize x^2+y^2
is.zero(linear(c(1,1i))*linear(c(1,-1i)) - ones(2,2)) # factorize x^2+y^2
Coerce and test for multipols
multipol(x) as.multipol(x) is.multipol(x)
multipol(x) as.multipol(x) is.multipol(x)
x |
Object to be coerced to multipol |
The usual case is to coerce an array to a multipol. A character
string may be given to as.multipol()
, which will attempt to
coerce to a multipol.
Subsets of a multipol are accessed and set using Oarray-style extraction with an offset of zero.
Robin K. S. Hankin
a <- as.multipol(array(1:12,c(2,3,2)))
a <- as.multipol(array(1:12,c(2,3,2)))
Uses Taylor's theorem to give one over one minus a multipol
ooom(n, a, maxorder=NULL)
ooom(n, a, maxorder=NULL)
n |
The order of the approximation; see details |
a |
A multipol |
maxorder |
A vector of integers giving the maximum order as per
|
The motivation for this function is the formal power series
. The way to
think about it is to observe that
,
even if
is a multivariate polynomial (one needs only power
associativity and a distributivity law, so this works for
polynomials). The right hand side is
if we neglect powers of
greater than the
-th, so the two terms on the left hand
side are multiplicative inverses of one another.
Argument n
specifies how many terms of the series to take.
The function uses an efficient array method when x
has only a single
non-zero entry. In other cases, a variant of Horner's method is
used.
Robin K. S. Hankin
I. J. Good 1976. “On the application of symmetric Dirichlet distributions and their mixtures to contingency tables”. The Annals of Statistics, volume 4, number 6, pp1159-1189; equation 5.6, p1166
ooom(4,homog(3,1)) # How many 2x2 contingency tables of nonnegative integers with rowsums = # c(2,2) and colsums = c(2,2) are there? Good gives: ( ooom(2,lone(4,c(1,3))) * ooom(2,lone(4,c(1,4))) * ooom(2,lone(4,c(2,3))) * ooom(2,lone(4,c(2,4))) )[2,2,2,2] # easier to use the aylmer package: ## Not run: library(aylmer) no.of.boards(matrix(1,2,2)) ## End(Not run)
ooom(4,homog(3,1)) # How many 2x2 contingency tables of nonnegative integers with rowsums = # c(2,2) and colsums = c(2,2) are there? Good gives: ( ooom(2,lone(4,c(1,3))) * ooom(2,lone(4,c(1,4))) * ooom(2,lone(4,c(2,3))) * ooom(2,lone(4,c(2,4))) )[2,2,2,2] # easier to use the aylmer package: ## Not run: library(aylmer) no.of.boards(matrix(1,2,2)) ## End(Not run)
Allows arithmetic operators to be used for multivariate polynomials such as addition, multiplication, and integer powers.
## S3 method for class 'multipol' Ops(e1, e2 = NULL) mprod(..., trim = TRUE , maxorder=NULL) mplus(..., trim = TRUE , maxorder=NULL) mneg(a, trim = TRUE , maxorder=NULL) mps(a, b, trim = TRUE , maxorder=NULL) mpow(a, n, trim = TRUE , maxorder=NULL)
## S3 method for class 'multipol' Ops(e1, e2 = NULL) mprod(..., trim = TRUE , maxorder=NULL) mplus(..., trim = TRUE , maxorder=NULL) mneg(a, trim = TRUE , maxorder=NULL) mps(a, b, trim = TRUE , maxorder=NULL) mpow(a, n, trim = TRUE , maxorder=NULL)
e1 , e2 , a
|
Multipols; scalars coerced |
b |
Scalar |
n |
Integer power |
... |
Multipols |
trim |
Boolean, with default |
maxorder |
Numeric vector indicating maximum orders of the output
[that is, the highest power retained in the multivariate Taylor
expansion about |
The function Ops.multipol()
passes unary and binary arithmetic
operators (“+
”, “-
”, “*
”, and
“^
”) to the appropriate specialist function.
In multipol.R
, these specialist functions all have formal names
such as .multipol.prod.scalar()
which follow a rigorous
pattern; they are not intended for the end user. They are not
exported from the namespace as they begin with a dot.
Five conveniently-named functions are provided in the package for the
end-user; these offer greater control than the arithmetic command-line
operations in that arguments trim
or maxorder
may be
set. They are:
mprod()
for products,
mplus()
for addition,
mneg()
for the negative,
mps()
for adding a scalar,
mpow()
for powers.
Addition and multiplication of multivariate polynomials is commutative and associative, to machine precision.
Robin K. S. Hankin
a <- as.multipol(matrix(1,4,5)) 100+a f <- as.function(a+1i) f(5:6) b <- as.multipol(array(rnorm(12),c(2,3,2))) f1 <- as.function(b) f2 <- as.function(b*b) f3 <- as.function(b^3) # could have said b*b*b x <- c(1,pi,exp(1)) f1(x)^2 - f2(x) #should be zero f1(x)^3 - f3(x) #should be zero x1 <- as.multipol(matrix(1:10,ncol=2)) x2 <- as.multipol(matrix(1:10,nrow=2)) x1+x2
a <- as.multipol(matrix(1,4,5)) 100+a f <- as.function(a+1i) f(5:6) b <- as.multipol(array(rnorm(12),c(2,3,2))) f1 <- as.function(b) f2 <- as.function(b*b) f3 <- as.function(b^3) # could have said b*b*b x <- c(1,pi,exp(1)) f1(x)^2 - f2(x) #should be zero f1(x)^3 - f3(x) #should be zero x1 <- as.multipol(matrix(1:10,ncol=2)) x2 <- as.multipol(matrix(1:10,nrow=2)) x1+x2
Gives an generalized outer product of two multipols
polyprod(m1, m2, overlap = 0)
polyprod(m1, m2, overlap = 0)
m1 , m2
|
multipols to be combined |
overlap |
Integer indicating how many variables are common to
|
Robin K. S. Hankin
a <- as.multipol(matrix(1,2,2)) # 1+x+y+xy polyprod(a,a) # (1+x+y+xy)*(1+z+t+zt) --- offset=0 polyprod(a,a,1) # (1+x+y+xy)*(1+y+z+yz) polyprod(a,a,2) # (1+x+y+xy)^2
a <- as.multipol(matrix(1,2,2)) # 1+x+y+xy polyprod(a,a) # (1+x+y+xy)*(1+z+t+zt) --- offset=0 polyprod(a,a,1) # (1+x+y+xy)*(1+y+z+yz) polyprod(a,a,2) # (1+x+y+xy)^2
Print methods for multipols
## S3 method for class 'multipol' print(x, ...) do_dimnames(a, include.square.brackets = getOption("isb"), varname = getOption("varname"), xyz = getOption("xyz")) ## S3 method for class 'multipol' as.character(x, ..., xyz = getOption("xyz"), varname = getOption("varname"))
## S3 method for class 'multipol' print(x, ...) do_dimnames(a, include.square.brackets = getOption("isb"), varname = getOption("varname"), xyz = getOption("xyz")) ## S3 method for class 'multipol' as.character(x, ..., xyz = getOption("xyz"), varname = getOption("varname"))
a , x
|
Multipol or array |
include.square.brackets |
Boolean with |
varname |
String to describe root variable name (eg
|
xyz |
Boolean with default |
... |
Further arguments (currently ignored) |
Function do_dimnames()
is a helper function that takes an array
and gives it dimnames appropriate for expression as a multipol. Default
behaviour is governed by options isb
, varname
, and
xyz
. The function might be useful but it is really intended to
be called by print.multipol()
.
The default behaviour of do_dimnames()
and as.character()
,
and hence the print method for multipols, may be modified by using the
options()
function. See examples section below.
Robin K. S. Hankin
ones(2,5) options("showchars" = TRUE) ones(2,5) options("xyz" = FALSE) ones(2,5) options("varname" = "fig") ones(2,5) options("showchars" = FALSE) ones(2,5) do_dimnames(matrix(0,2,3),varname="fig",include=TRUE)
ones(2,5) options("showchars" = TRUE) ones(2,5) options("xyz" = FALSE) ones(2,5) options("varname" = "fig") ones(2,5) options("showchars" = FALSE) ones(2,5) do_dimnames(matrix(0,2,3),varname="fig",include=TRUE)
Substitute a value for a variable and return a multipol of arity
d-1
put(a, i, value, keep = TRUE)
put(a, i, value, keep = TRUE)
a |
multipol |
i |
Dimension to substitute |
value |
value to substitute for |
keep |
Boolean with default |
Robin K. S. Hankin
a <- as.multipol(matrix(1:12,3,4)) put(a,1,pi) put(a,2,pi) b <- as.multipol(array(1:12,c(3,2,3))) put(b,2,pi,TRUE) put(b,2,pi,FALSE)
a <- as.multipol(matrix(1:12,3,4)) put(a,1,pi) put(a,2,pi) b <- as.multipol(array(1:12,c(3,2,3))) put(b,2,pi,TRUE) put(b,2,pi,FALSE)
Remove redundant entries from a multivariate polynomial: function
trim()
trims the array of non-significant zeroes as far as
possible without altering its value as a multipol; function
taylor()
returns the multivariate Taylor expansion to a
specified order.
trim(a) taylor(a,maxorder=NULL)
trim(a) taylor(a,maxorder=NULL)
a |
A multipol |
maxorder |
The multivariate order of the expansion returned;
default of |
Returns a multipol
If a
is a zero multipol (that is, a multivariate polynomial
with all entries zero) of any size, then trim(a)
is a zero
multipol of the same arity as a
but with extent 1 in each
direction.
Robin K. S. Hankin
a <- matrix(0,7,7) a[1:3,1:4] <- 1:12 a <- as.multipol(a) a trim(a) taylor(a,2)
a <- matrix(0,7,7) a[1:3,1:4] <- 1:12 a <- as.multipol(a) a trim(a) taylor(a,2)