[R] Dispatching on 2 arguments?
Andrew Simmons
@kw@|mmo @end|ng |rom gm@||@com
Sun Nov 7 07:13:03 CET 2021
If you want to use the 'methods' package, you could do something like:
replace <- function (p1, p2, ...)
{
stop(gettextf("cannot 'replace' with arguments of class %s and %s",
sQuote(class(p1)[1L]), sQuote(class(p2)[1L])))
}
methods::setGeneric("replace")
methods::setMethod("replace", signature = c(p1 = "pm", p2 = "numeric"),
definition = replace.pm.numeric)
methods::setMethod("replace", signature = c(p1 = "pm", p2 = "character"),
definition = replace.pm.character)
First, we make the default function. I'm not sure what you want that to
look like, but I've made an example above.
Second, you make it generic so that you can provide methods for it.
Last, you add the methods you want. You specify the name of the generic
function, "replace", the classes of arguments it will accept, and then its
function definition.
You can use ?methods::setGeneric and ?methods::setMethod for help with
either of these functions. I hope this helps!
On Sun, Nov 7, 2021 at 1:55 AM Leonard Mada via R-help <r-help using r-project.org>
wrote:
> Dear List-members,
>
>
> I would like to experiment with dispatching on 2 arguments and have a
> few questions.
>
>
> p1 = data.frame(x=1:3, coeff=1)
> class(p1) = c("pm", class(p1));
>
> I want to replace variables in a polynomial with either:
> another polynomial, or another variable (character) or with a specific
> value.
>
>
> 1.) Can I dispatch on 2 arguments?
>
>
> replace.pm.? = function(p1, p2, ...) {...}
> or classic:
> replace.pm = function(p1, p2, ...) {
> if(is.numeric(p2) || is.complex(p2)) return(replace.pm.numeric(p1,
> p2, ...));
> if(is.character(p2)) return(replace.pm.character(p1, p2=p2, ...));
> else ...
>
> }
>
>
> I will provide some realistic examples below.
>
>
> 2.) Advantages / Disadvantages of each method
> What are the advantages or disadvantages to each of these methods?
> I do not yet understand what should be the best design.
>
>
> Real example:
>
>
> ### Quintic
> p1 = toPoly.pm("x^5 - 5*K*x^3 - 5*(K^2 + K)*x^2 - 5*K^3*x - K^4 - 6*K^3
> + 5*K^2 - K")
> # fractional powers: [works as well]
> r = toPoly.pm("K^(4/5) + K^(3/5) + K^(1/5)")
> # - we just found a root of a non-trivial quintic!
> # all variables/monomials got cancelled;
> replace.pm(p1, r, "x", pow=1)
>
> # more formal
> r = toPoly.pm("k^4*m^4 + k^3*m^3 + k*m")
> # m^5 = 1; # m = any of the 5 roots of unity of order 5;
> pR = p1;
> pR = replace.pm(pR, r, xn="x") # poly
> pR = replace.pm(pR, "K", xn="k", pow=5) # character
> pR = replace.pm(pR, 1, xn="m", pow=5) # value
> pR # the roots worked! [no remaining rows]
> # - we just found ALL 5 roots!
>
>
> The code is on Github (see below).
>
>
> Sincerely,
>
>
> Leonard
>
> =========
>
> # very experimental code
> # some names & arguments may change;
>
> source("Polynomials.Helper.R")
> # also required, but are loaded automatically if present in wd;
> # source("Polynomials.Helper.Parser.R")
> # source("Polynomials.Helper.Format.R")
> ### not necessary for this Test (just loaded)
> # source("Polynomials.Helper.D.R")
> # source("Polynomials.Helper.Factorize.R")
> # the libraries pracma & polynom are not really required for this test
> either;
>
> ### Github:
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.R
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Parser.R
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Format.R
> # not necessary for this Test
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.D.R
>
> https://github.com/discoleo/R/blob/master/Math/Polynomials.Helper.Factorize.R
>
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list