diff --git a/.Rbuildignore b/.Rbuildignore index f92ee80..33dfb18 100644 --- a/.Rbuildignore +++ b/.Rbuildignore @@ -6,3 +6,4 @@ ^inst/trash$ ^inst/RDS$ ^cran-comments\.md$ +^CRAN-SUBMISSION$ diff --git a/DESCRIPTION b/DESCRIPTION index d7773bb..a67a93d 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,6 +1,6 @@ Package: symbolicQspray Title: Multivariate Polynomials with Symbolic Parameters in their Coefficients -Version: 1.0.0 +Version: 1.0.0.9000 Authors@R: person("Stéphane", "Laurent", , "laurent_step@outlook.fr", role = c("aut", "cre")) Description: Introduces the 'symbolicQspray' objects. Such an object @@ -16,9 +16,10 @@ Description: Introduces the 'symbolicQspray' objects. Such an object License: GPL-3 URL: https://github.com/stla/symbolicQspray BugReports: https://github.com/stla/symbolicQspray/issues +Remotes: stla/qspray, stla/ratioOfQsprays Depends: - qspray (>= 3.0.0), - ratioOfQsprays + qspray (>= 3.0.0.9000), + ratioOfQsprays (>= 1.0.0.9000) Imports: gmp, methods, diff --git a/NAMESPACE b/NAMESPACE index 8c25d00..ba32f85 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -24,6 +24,7 @@ exportMethods(changeVariables) exportMethods(compactSymmetricQspray) exportMethods(getCoefficient) exportMethods(getConstantTerm) +exportMethods(involvedVariables) exportMethods(isConstant) exportMethods(isQone) exportMethods(isQzero) @@ -48,6 +49,7 @@ importFrom(qspray,compactSymmetricQspray) importFrom(qspray,evalQspray) importFrom(qspray,getCoefficient) importFrom(qspray,getConstantTerm) +importFrom(qspray,involvedVariables) importFrom(qspray,isConstant) importFrom(qspray,isQone) importFrom(qspray,isQzero) diff --git a/R/internal.R b/R/internal.R index ee7ad7f..ce64b2a 100644 --- a/R/internal.R +++ b/R/internal.R @@ -115,8 +115,8 @@ arity <- function(qspray) { } powersMatrix <- function(qspray) { - n <- arity(qspray) - if(n == -Inf) { + n <- numberOfVariables(qspray) + if(n == 0L) { matrix(NA_integer_, 0L, 0L) } else { do.call(rbind, lapply(qspray@powers, grow, n = n)) diff --git a/R/queries.R b/R/queries.R index 048d978..68e3498 100644 --- a/R/queries.R +++ b/R/queries.R @@ -2,6 +2,7 @@ NULL setGeneric("numberOfVariables") +setGeneric("involvedVariables") setGeneric("numberOfTerms") setGeneric("getCoefficient") setGeneric("getConstantTerm") @@ -14,13 +15,14 @@ setGeneric("isQone") #' @aliases numberOfVariables,symbolicQspray-method #' @docType methods #' @importFrom qspray numberOfVariables -#' @title Number of variables in a 'symbolicQspray' polynomial +#' @title Number of variables of a 'symbolicQspray' polynomial #' @description Number of variables involved in a \code{symbolicQspray} object. #' #' @param x a \code{symbolicQspray} object #' #' @return An integer. #' @export +#' @seealso \code{\link{involvedVariables}}. #' @note The number of variables in the \code{symbolicQspray} object #' \code{Qlone(d)} is \code{d}, not \code{1}. setMethod( @@ -30,6 +32,39 @@ setMethod( } ) +#' @name involvedVariables +#' @aliases involvedVariables,symbolicQspray-method +#' @docType methods +#' @importFrom qspray involvedVariables +#' @title Variables involved in a 'symbolicQspray' polynomial +#' @description Variables involved in a \code{symbolicQspray} object. +#' +#' @param x a \code{symbolicQspray} object +#' +#' @return A vector of integers. Each integer represents the index of a +#' variable involved in \code{x}. +#' @export +#' @seealso \code{\link{numberOfVariables}}. +#' @examples +#' a1 <- qlone(1); a2 <- qlone(2) +#' X <- Qlone(1); Z <- Qlone(3) +#' Qspray <- (a1/a2)*X^2 + (a1/(a1+a2))*X*Z + a2^2/a1 +#' involvedVariables(Qspray) # should be c(1L, 3L) +setMethod( + "involvedVariables", "symbolicQspray", + function(x) { + if(isConstant(x)) { + integer(0L) + } else { + M <- powersMatrix(x) + tests <- apply(M, 2L, function(col) { + any(col != 0L) + }) + which(tests) + } + } +) + #' @name numberOfTerms #' @aliases numberOfTerms,symbolicQspray-method #' @docType methods @@ -54,27 +89,25 @@ setMethod( #' @docType methods #' @importFrom qspray getCoefficient #' @title Get a coefficient in a 'symbolicQspray' polynomial -#' @description Get the coefficient corresponding to the given sequence of -#' exponents. +#' @description Get the coefficient of the term with the given monomial. #' #' @param qspray a \code{symbolicQspray} object -#' @param exponents a vector of exponents +#' @param exponents a vector of exponents, thereby defining a monomial; +#' trailing zeros are ignored #' -#' @return The coefficient as a \code{ratioOfQsprays} object. +#' @return The coefficient, \code{ratioOfQsprays} object. #' @export -#' @importFrom gmp as.bigq -#' @importFrom ratioOfQsprays showRatioOfQspraysOption<- +#' @importFrom ratioOfQsprays showRatioOfQspraysOption<- as.ratioOfQsprays #' #' @examples -#' library(qspray) -#' x <- qlone(1) -#' y <- qlone(2) -#' p <- 2*x^2 + 3*y - 5 -#' getCoefficient(p, 2) # coefficient of x^2 +#' a1 <- qlone(1); a2 <- qlone(2) +#' X <- Qlone(1); Y <- Qlone(2) +#' p <- 2*(a1/a2)*X^2 + (a1/(a1+a2))*Y + a2^2/a1 +#' getCoefficient(p, 2) # coefficient of X^2 #' getCoefficient(p, c(2, 0)) # same as getCoefficient(p, 2) -#' getCoefficient(p, c(0, 1)) # coefficient of y (= x^0.y^1) +#' getCoefficient(p, c(0, 1)) # coefficient of Y (because Y=X^0.Y^1) #' getCoefficient(p, 0) # the constant term -#' getCoefficient(p, 3) # coefficient of x^3 +#' getCoefficient(p, 3) # coefficient of X^3 setMethod( "getCoefficient", c("symbolicQspray", "numeric"), function(qspray, exponents) { diff --git a/man/getCoefficient.Rd b/man/getCoefficient.Rd index b0f8f1f..5a95a2f 100644 --- a/man/getCoefficient.Rd +++ b/man/getCoefficient.Rd @@ -11,23 +11,22 @@ \arguments{ \item{qspray}{a \code{symbolicQspray} object} -\item{exponents}{a vector of exponents} +\item{exponents}{a vector of exponents, thereby defining a monomial; +trailing zeros are ignored} } \value{ -The coefficient as a \code{ratioOfQsprays} object. +The coefficient, \code{ratioOfQsprays} object. } \description{ -Get the coefficient corresponding to the given sequence of - exponents. +Get the coefficient of the term with the given monomial. } \examples{ -library(qspray) -x <- qlone(1) -y <- qlone(2) -p <- 2*x^2 + 3*y - 5 -getCoefficient(p, 2) # coefficient of x^2 +a1 <- qlone(1); a2 <- qlone(2) +X <- Qlone(1); Y <- Qlone(2) +p <- 2*(a1/a2)*X^2 + (a1/(a1+a2))*Y + a2^2/a1 +getCoefficient(p, 2) # coefficient of X^2 getCoefficient(p, c(2, 0)) # same as getCoefficient(p, 2) -getCoefficient(p, c(0, 1)) # coefficient of y (= x^0.y^1) +getCoefficient(p, c(0, 1)) # coefficient of Y (because Y=X^0.Y^1) getCoefficient(p, 0) # the constant term -getCoefficient(p, 3) # coefficient of x^3 +getCoefficient(p, 3) # coefficient of X^3 } diff --git a/man/involvedVariables.Rd b/man/involvedVariables.Rd new file mode 100644 index 0000000..b232379 --- /dev/null +++ b/man/involvedVariables.Rd @@ -0,0 +1,29 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/queries.R +\docType{methods} +\name{involvedVariables} +\alias{involvedVariables} +\alias{involvedVariables,symbolicQspray-method} +\title{Variables involved in a 'symbolicQspray' polynomial} +\usage{ +\S4method{involvedVariables}{symbolicQspray}(x) +} +\arguments{ +\item{x}{a \code{symbolicQspray} object} +} +\value{ +A vector of integers. Each integer represents the index of a + variable involved in \code{x}. +} +\description{ +Variables involved in a \code{symbolicQspray} object. +} +\examples{ +a1 <- qlone(1); a2 <- qlone(2) +X <- Qlone(1); Z <- Qlone(3) +Qspray <- (a1/a2)*X^2 + (a1/(a1+a2))*X*Z + a2^2/a1 +involvedVariables(Qspray) # should be c(1L, 3L) +} +\seealso{ +\code{\link{numberOfVariables}}. +} diff --git a/man/numberOfVariables.Rd b/man/numberOfVariables.Rd index 9e010ae..644029f 100644 --- a/man/numberOfVariables.Rd +++ b/man/numberOfVariables.Rd @@ -4,7 +4,7 @@ \name{numberOfVariables} \alias{numberOfVariables} \alias{numberOfVariables,symbolicQspray-method} -\title{Number of variables in a 'symbolicQspray' polynomial} +\title{Number of variables of a 'symbolicQspray' polynomial} \usage{ \S4method{numberOfVariables}{symbolicQspray}(x) } @@ -21,3 +21,6 @@ Number of variables involved in a \code{symbolicQspray} object. The number of variables in the \code{symbolicQspray} object \code{Qlone(d)} is \code{d}, not \code{1}. } +\seealso{ +\code{\link{involvedVariables}}. +}