On Tue, 18 Oct 2005, Mathieu Bouchard wrote:

> What should happen after coercion? if i do 2**aMatrix, is it required that
> the left side gets promoted to a matrix (that is, the same type as the
> right-handed argument) ? Then is there any definition of matrix**matrix ?
> (i haven't ever used one). It would have to extend the semantics of
> scalar**matrix. Maybe I'm just lost too...
>
> But then, x**y = exp(log(x)*y), and since both exp and log are defined on
> matrices (through use of singular value decomposition), this gives us a
> natural definition for aMatrix**anotherMatrix.

I suppose it's only defined for square matrices. And indeed, through power 
series, about any analytic function can be defined on square matrices (for 
which the power series converges, of course).

But that's besides the point (of my problem), point is that Ruby as it is 
now precludes these kinds of things by doing the comparison before the 
coercion in some cases. This means that if I wanted to add matrix 
exponentials to Ruby, I'd need to redefine Integer#** instead of hooking 
into Ruby's coercion framework.

OTOH, coercion is indeed not always an option. Say I just wanted to define 
someInteger**someMatrix, then I've got only one option in Ruby's coercion 
framework, namely to coerce the integer to a matrix. And although it is 
possible to define someMatrix**someOtherMatrix, I may not want to go 
through all that trouble, even when someMatrix is just a 1x1 matrix in 
this case because it is overhead to build the matrix for nothing.

> The complex fields (like Q(sqrt(-1)) and C) are not in any way totally
> orderable. If you quotient the field in some way then you may obtain
> something totally orderable or not, but the best you can get with an
> ordinary complex field is a partial order. However Ruby doesn't have a
> Semicomparable interface (the name i wanted to give to partial ordering in
> Ruby), despite the Module class already supporting most of it in an
> implicit way: think about the meaning of "A<B" as "A is subclass of B" aka
> "A objects are a subset of B objects".
> 
> What do you think of this?

C can be totally ordered, for instance lexical order (with i = sqrt(-1)):

   a + b * i <= c + d * i   iff   a < c  or  a = c and b <= d

Is that a useable order? Well, that depends on what you need that order 
for. There are other orders possible though, but Ruby has only support for 
a single order. I wonder how hard it would be to have Ruby support 
multiple orders for the same kind of object. Given that we can use 
different < symbols, it would probably look like this:

   Complex.with_ordering(Complex::LexicalOrder) do
     Complex(1, 1) < Complex(1, 2) #=> true
   end

IMO though, we don't need a separate Semicomparable interface. #<=> 
already returns nil when two objects are not comparable. For instance
1 <=> true gives nil. Currently the comparison methods in Comparable raise 
an error when #<=> returns nil, but this could be changed to have these 
methods return false instead. Or nil, as Module#< does when two classes 
are not comparable, which would make things more consistent.

Peter