[R] Dispatching on 2 arguments?
Leonard Mada
|eo@m@d@ @end|ng |rom @yon|c@eu
Sun Nov 7 06:54:31 CET 2021
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
More information about the R-help
mailing list