RobinHankin / disordR

https://robinhankin.github.io/disordR/
1 stars 0 forks source link

pmin() seems to work #27

Closed RobinHankin closed 2 years ago

RobinHankin commented 3 years ago

Look:

> a <- rdis()
> b <- a*2-0.5
> pmin(a,b)
A disord object with hash 91ae489c424fb97f79660fb26d7e55fd7540a5c2 and elements
[1]  0.75251960 -0.03254402  0.98480714
[4]  0.72937949  0.60269093  0.14233462
[7]  0.51988598  0.95873991  0.31990565
(in some order)
> 

I guess it is because of the new lapply(), but either way we can lose that pmindis() and pmaxdis() stuff.

RobinHankin commented 2 years ago

But the functionality is not entirely present:

> a <- runif(10)
> pmax(a,0.5)
 [1] 0.5000000 0.5000000 0.5000000 0.5000000 0.5000000 0.5000000 0.5111702
 [8] 0.5000000 0.7380832 0.5000000

But pmax() does not work for disord objects and length-one vectors:

> a <- rdis()
> a
A disord object with hash c9b49b1566b6d38ecc77c5774bf61681c684692b and elements
[1] 0.08796295 0.13858344 0.80037993 0.37455733 0.18298922 0.03930161 0.68088122
[8] 0.33788059 0.42316731
(in some order)
> pmax(a,0.5)
Error in mmm < each : length(e2) == 1 is not TRUE
> 
RobinHankin commented 2 years ago

Function base::pmax() fails at the line if (isS4(change <- mmm < each) && (nS4 || !isS4(each))) change <- as(change, "logical")

and that fails because comparisons [here "<"] do not work between disord objects and vectors all of which are the same entry:

> disord(1:10) < rep(5,10)
Error in disord(1:10) < rep(5, 10) : length(e2) == 1 is not TRUE
> 
RobinHankin commented 2 years ago

Commit cf9a3641fea617beb1cfa865db fixed the "<" issue but not pmax(); the error message has changed:


> pmax(disord(1:10),4)
Error in .local(x, i, j = j, ..., value) : 
  cannot use a regular index to extract, only a disord object
> 
RobinHankin commented 2 years ago

The problem is now here:

if (isS4(change <- mmm < each) && (nS4 || !isS4(each))) 
                change <- as(change, "logical")

and the error is caused by this:

> jj <- (disord(1:10)>5)
> as(jj,"logical")
 [1] FALSE FALSE FALSE FALSE FALSE  TRUE  TRUE  TRUE  TRUE  TRUE
> 

The problem is that as(jj,"logical") should give a disord object, but the hash is lost. And this is definitely wrong because losing the hash means that an order is imposed.

RobinHankin commented 2 years ago

We need

setAs("disord","logical",function(from){disord(as.logical(elements(from)),hash(from))})

(which incidentally seems to make pmax() work) but there are other coercion methods to do and it would be better to do them all at once.