RobinHankin / mvp

https://robinhankin.github.io/mvp/
8 stars 3 forks source link

approximate equality #54

Open stla opened 2 years ago

stla commented 2 years ago

Hello,

I have two mvp polynomials, gmvpol and mvpol. If I run gmvpol == mvpol, I get FALSE. But that's only because there are very small differences between the coefficients. If I run all.equal(gmvpol[["coeffs"]], mvpol[["coeffs"]]), then I get TRUE. So I think it would be nice to have a function to test approximate equality, with a tolerance argument, as in all.equal.

RobinHankin commented 2 years ago

Hello, sorry it's taken me so long to get back to you. This function is proving somewhat more difficult than I thought. The coefficients of target and current are not consistent() so I can't just subtract one from the other because of disordR discipline. Writing a,b for the arguments, I can't just do all.equal(coeffs(a),coeffs(b)) for the same reason. And all.equal(elements(coeffs(a)),elements(coeffs(b))) is certainly wrong too.

What we want is for the coefficients of a-b to be "small" compared with the corresponding coefficients of a and the coefficients of b. But I can't quite see whether this can be accomplisehd by a call to all.equal.numeric() or whether a new function is needed. There must be an easy way to do this, but I just can't see it.

Best wishes

Robin

stla commented 2 years ago

Hi,

I saw you published disordR but I don't know yet what this package does. So for the moment I don't understand your reply. But then why this worked for me? (I don't exactly remember what I did).

RobinHankin commented 2 years ago

I am not at all sure that an implementation of all.equal.mvp() that directly uses base all.equal() is possible. Consider

`all.equal.mvp` <- function(target,current,...){
    target <- coeffs(target)
    current <- coeffs(current)
    stopifnot(consistent(target,current))
    all.equal.numeric(target,current,...)
}

which seems plausible but the consistency check is way too restrictive.