> On Jan 25, 2017, at 1:58 PM, David Sweeris via swift-evolution 
> <[email protected]> wrote:
> 
> 
>> On Jan 25, 2017, at 07:59, Anton Mironov via swift-evolution 
>> <[email protected]> wrote:
>> 
>> Hi everyone,
>> 
>> I want to suggest a tiny extension to an Arithmetic protocol. It would be 
>> nice to have an additive identity and a multiplicative identity constants. 
>> Basically zero and one.
>> 
>> ```
>> protocol Arithmetic {
>>  /* ... */
>>  static var zero: Self { get }  // additive identity: (value + .zero) == 
>> value
>>  static var one: Self { get }   // multiplicative identity: (value * .one) 
>> == value
>> }
>> ```
>> 
>> These constants will ease implementation of math structures: vectors, 
>> matrices and etc.
>> I’m sorry if I’m duplicating someone’s suggestion. It is really hard to 
>> search for something in a long thread.
> 
> Vectors, matrices, etc can't conform to this protocol anyway because of the * 
> and / requirements. And while it is true that it's not uncommon to reference 
> an all-zero matrix or vector as "0", that doesn't work for any other 
> number... for them I think the spelling should be "additiveIdentity". It'll 
> be easy enough to just say
> protocol Addable {
>    static var additiveIdentity: Self {get}
>    ...
> }
> extension Addable where Self: Arithmetic {
>    static var additiveIdentity: Self {return Self.zero}
> }
> 
> IMHO, anyway.

1. Square matrices of fixed size can absolutely conform to this protocol 
(except for ‘/‘, as discussed earlier in the thread).

2. It’s pretty common to call the multiplicative identity matrix 1 (though “I” 
is more common). The rest of the integers map to square matrices in the obvious 
way n —> n*1 (i.e. the matrix with n on the main diagonal, and zeros elsewhere).

– Steve
_______________________________________________
swift-evolution mailing list
[email protected]
https://lists.swift.org/mailman/listinfo/swift-evolution

Reply via email to