RobinHankin / mvp

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

replace coeffs #37

Closed RobinHankin closed 5 years ago

RobinHankin commented 5 years ago

The replacement method for coefficients requires that the replacement vector has length one. But look at this:

> (a <- rmvp(4))
mvp object algebraically equal to
4 a b^2 c^4 e^3 f^3  +  2 a^8 b^5 c f^5  +  3 b^9 c^4 e^7  +  c^4 e^12 f^6
> coeffs(a) <- coeffs(a) + 5
Error in `coeffs<-.mvp`(`*tmp*`, value = c(9, 7, 8, 6)) : 
  order of coefficients not defined.  Idiom "coeffs(x) <- value" is meaningful only if value is unchanged on reordering, here we require "value" to have length 1

The idiom coeffs(a) <- coeffs(a)+1 or indeed something like coeffs(a) <- coeffs(a) %%2 is meaningful. It means "add one to each coefficient". Currently this does not work.

Exactly the same issue happens in the mvp package:

https://github.com/RobinHankin/freealg/issues/7

but I'll deal with them separately.

RobinHankin commented 5 years ago

But we can't do this:

> a <- rmvp(6)
> a
mvp object algebraically equal to
4 a b^5 c^6 e^4  +  2 a c^5 d^4 e^7 f^3  +  a^2 b c d^5 e f^3  +  3 a^4 b^6 c^6 f^11  +  5 a^6 b^6 c^6 f^6  +  6 b d^6 e^5 f^6
> coeffs(a) <- pmax(coeffs(a),2)
Error in Ops.mvp_coeffs(mmm, each) : 
  operator '<' is not implemented for coeffs

and conceptually that should work: what we want to do is to replace each coefficient with the maximum of itself and 2. But of course < is not (and cannot be) implemented because coeffs(a) < 2 is a Boolean vector, which is not allowed because it imposes a possibly incorrect ordering.

RobinHankin commented 5 years ago

But now with the hashing system we can do this:

> linear(1:6)
mvp object algebraically equal to
a  +  2 b  +  3 c  +  4 d  +  5 e  +  6 f
> x <- linear(1:8)
> coeffs(x)[coeffs(x)>3] <- 3
> x
mvp object algebraically equal to
a  +  2 b  +  3 c  +  3 d  +  3 e  +  3 f  +  3 g  +  3 h
> 

Note that coeffs(x)<3 (intentionally) returns an error.