Description
Firstly, I'm very fond of to_vec
-- it's served us well over the last couple of years. We know it has some shortcomings though, that we will need to address at some point.
Presently we have implementations of to_vec
like this instead of implementations that look like this.
IIRC the reason for this is that if you define things in the latter fashion then you'll find if you do something like
A = randn(5, 4)
A_vec, _ = to_vec(A)
At_vec, _ = to_vec(transpose(A))
then A_vec == At_vec
. My suspicion is that at some point in time, we were trying to compare the equality of two things based on their to_vec
representation and it came up that things weren't being compared properly. We do a similar thing with Diagonal
etc.
Unfortunately, it seems quite straightforward to construct examples which don't admit such a straightforward resolution as we employed for Adjoint
, Transpose
, and Diagonal
. For a trivial example, consider that
A = randn(5)
A == (A, ) # is false -- a `Tuple` is never equal to a `Vector`
However, since to_vec(::Tuple)
simply concatenates to_vec
of each of the fields, we'll find that
to_vec(A)[1] == to_vec((A, ))[1]
This leads me to believe that it's likely not possible to define to_vec
such that equality is preserved.
As such, I propose that we
- embrace, and explicitly document, that knowing whether
to_vec(x)[1] == to_vec(y)[1]
in general tells you nothing about whetherx == y
, and vice versa. We should point this out to explicitly warn people away from trying to check whether two things are equal based on theirto_vec
representation. - refactor some of the things (like
Tranpose
andAdjoint
) to use the struct representation, which is more computationally efficient. to_vec
should purely be thought of as a way to makeFiniteDifferences
' life easier. We might even consider ceasing to export it.
Now that @oxinabox has added some new approximate equality checking to ChainRulesTestUtils
, this ought to be less of a problem anyway, because it's now more straightforward to check whether things are approximately equal.
Of course, if anyone can see a different path forward, I'm all ears.