Boolean tests [was Re: Attack a sacred Python Cow]

Terry Reedy tjreedy at udel.edu
Mon Aug 4 20:47:01 EDT 2008



Erik Max Francis wrote:
> Antoon Pardon wrote:
[responding to me]
>> Maybe I'm going to be pedantic here, but I fear that your code won't
>> work with matrices. The problem is that multiplication is not
>> commutative with matrices. This means that matrices have two divisions 
>> a right
>> and a left division. A far as I know the "/" operator usaly implements
>> the left division while solving is done by using a right division.
>>
>> So your code will probably fail when applied to matrices.

Your remarks are correct as far as they go, but..

> You're right in your general point, but usually division between 
> matrices isn't defined at all because of this ambiguity.  However the 
> general point can still exist between `solveLeft` and `solveRight` 
> functions which do something along the lines of a*b**-1 and a**-1*b.  A 
> single `solve`, of course, would choose one of these, and syntactically 
> it might be quite reasonable to define matrix division that does one of 
> these and have a single `solve` function that accomplishes it.
> 
> Therefore, the general point about polymorphism still stands.

as Eric suggests, I was assuming that details would be defined to make 
my example work.  The context was the challenge to find an example 
where, for instance, 'if x:' would work but something more specific like 
'if x != 0:' would not.  My abstract answer was "number-like class with 
division that is not valid for the class's null object".  The concrete 
answer was an incomplete instantiation of that for matrices.  Given the 
critique, polynomial division might be a better example since polynomial 
multiplication is commutative while  both ()(if allowed) and (0,) as 
polyomials might well be defined as != to numeric 0.  I suspect there 
are other algebra classes that work for this or other examples, but it 
has been a loooooong time since I took abstract algebra.

Actually I sort of worked too hard ;-).
The decimal module breaks the transitivity of equality!

 >>> import decimal as d
 >>> z=d.Decimal('0.0')
 >>> z==0
True
 >>> z==0.0
False
 >>> 0==0.0
True

So if someone wrote 'if x == 0.0:' instead of 'if not x:' (perhaps to 
raise an exception with explanation for unusable input), the former 
would not work.

tjr




More information about the Python-list mailing list