Unlimited learning, half price | 50% off

Last chance! 50% off unlimited learning

Sale ends in


hpa (version 1.3.3)

polynomialIndex: Multivariate Polynomial Representation

Description

Function polynomialIndex provides matrix which allows to iterate through the elements of multivariate polynomial being aware of these elements powers. So (i, j)-th element of the matrix is power of j-th variable in i-th multivariate polynomial element.

Function printPolynomial prints multivariate polynomial given its degrees (pol_degrees) and coefficients (pol_coefficients) vectors.

Usage

polynomialIndex(pol_degrees = numeric(0), is_validation = TRUE)

printPolynomial(pol_degrees, pol_coefficients, is_validation = TRUE)

Value

Function polynomialIndex

returns matrix which rows are responsible for variables while columns are related to powers. So (i,j)-th element of this matrix corresponds to the power ij of the xj variable in i-th polynomial element. Therefore i-th column of this matrix contains vector of powers (i1,...,im) for the i-th polynomial element. So the function transforms m-dimensional elements indexing to one-dimensional.

Function printPolynomial returns the string which contains polynomial symbolic representation.

Arguments

pol_degrees

non-negative integer vector of polynomial degrees (orders).

is_validation

logical value indicating whether function input arguments should be validated. Set it to FALSE for slight performance boost (default value is TRUE).

pol_coefficients

numeric vector of polynomial coefficients.

Details

Multivariate polynomial of degrees (K1,...,Km) (pol_degrees) has the form: a(0,...,0)x10...xm0+...+a(K1,...,Km)x1K1...xmKm, where a(i1,...,im) are polynomial coefficients, while polynomial elements are: a(i1,...,im)x1i1...xmim, where (i1,...,im) are polynomial element's powers corresponding to variables (x1,...,xm) respectively. Note that ij{0,...,Kj}.

Function printPolynomial removes polynomial elements which coefficients are zero and variables which powers are zero. Output may contain long coefficients representation as they are not rounded.

Examples

Run this code
## Get polynomial indexes matrix for the polynomial 
## which degrees are (1, 3, 5)

polynomialIndex(c(1, 3, 5))
# \donttest{
## Consider multivariate polynomial of degrees (2, 1) such that coefficients
## for elements which powers sum is even are 2 and for those which powers sum
## is odd are 5. So the polynomial is 2+5x2+5x1+2x1x2+2x1^2+5x1^2x2 where
## x1 and x2 are polynomial variables.

# Create variable to store polynomial degrees
pol_degrees <- c(2, 1)

# Let's represent its powers (not coefficients) in a matrix form
pol_matrix <- polynomialIndex(pol_degrees)

# Calculate polynomial elements' powers sums
pol_powers_sum <- pol_matrix[1, ] + pol_matrix[2, ]

# Let's create polynomial coefficients vector filling it
# with NA values
pol_coefficients <- rep(NA, (pol_degrees[1] + 1) * (pol_degrees[2] + 1))

# Now let's fill coefficients vector with correct values
pol_coefficients[pol_powers_sum %% 2 == 0] <- 2
pol_coefficients[pol_powers_sum %% 2 != 0] <- 5

# Finally, let's check that correspondence is correct
printPolynomial(pol_degrees, pol_coefficients)
# }
## Let's represent polynomial 0.3+0.5x2-x2^2+2x1+1.5x1x2+x1x2^2

pol_degrees <- c(1, 2)
pol_coefficients <- c(0.3, 0.5, -1, 2, 1.5, 1)

printPolynomial(pol_degrees, pol_coefficients)

Run the code above in your browser using DataLab