What is _VectorMath protocol? Conforming to VectorArithmetic and AdditiveArithmetic

Context


While extending the Array type to conform to VectorArithmetic (for core animation experiment):

Code Block
extension Array: VectorArithmetic where Element: VectorArithmetic & AdditiveArithmetic { ... }

I get the following error:

Code Block
Referencing operator function '*' on '_VectorMath' requires that 'Element' conform to '_VectorMath'

and when calling the scale(by:) method:

Code Block
Referencing instance method 'scale(by:)' on 'Array' requires that 'Double' conform to '_VectorMath'



Workaround


Making type Element and Double conform to _VectorMath fixed the issue:

Code Block
extension Array: VectorArithmetic where Element: _VectorMath & VectorArithmetic & AdditiveArithmetic { ... }

Code Block swift
extension Double: _VectorMath { }


Questions


I tried looking for information on _VectorMath (note the underscore prefix) but I could not find anything in the official docs and not much on the internet.

What is _VectorMath protocol?
Where is it defined?
Is it ok to use it like this?
Or should I use a different approach?


What is _VectorMath protocol?

Not documented publicly.

Where is it defined?

Somewhere in the SwiftUI framework, maybe.
(Using _VectorMath causes error without import SwiftUI.)

Is it ok to use it like this?

NO. Considering the fact that it does not appear in the generated header of imported frameworks and that Xcode does not show any code completion of it, it is considered to be intentionally hidden. (For some reason, it cannot be private.)
So, you should better take it as a private API that Apple inhibits us to use.

Or should I use a different approach?

I think you should.


Just my curiosity, how are you trying to make Array conform to VectorArithmetic?
For example, what is your definition of +? Two Arrays may have different sizes.
What operations do you need to perform on vectors ?

Usual ones: add, multiply.

Or more advanced ?

Did you consider writing the extension by yourself ?

Proposed solution


I managed to get around the issue by using Element.scale(by:) in the extension's definition of Array.scale(by:) and not using the * operator.

Using * operator required Element and Double to conform to _VectorMath (which is what we wanted to avoid).



Follow-up on questions and remarks


Just my curiosity, how are you trying to make Array conform to VectorArithmetic?

I see them as coefficients of polynomials (as in the algebraic definition). If I am not mistaken, it can be a vector space.

For example, what is your definition of +? Two Arrays may have different sizes.

Add coefficients pairwise; and if one array is shorter than the other one, use AdditiveArithmetic.zero for the "missing" element.


> What is _VectorMath protocol?
Not documented publicly.

I did not see this sooner, but if I option-click on the _VectorMath, there's the quick help:

Summary

Adds the “vector space” numeric operations for any type that conforms to Animatable.

Declaration

protocol _VectorMath : Animatable
I am not sure what to make of this. But as you suggested I'd try not to use this "private" (?) API.


Proposed solution

Thanks for showing your solution. And that is the same as what I tried.
(Seems you already use Element.magnitudeSquared when implementing Array.magnitudeSquared.

Follow-up on questions and remarks

Thanks!

I see them as coefficients of polynomials (as in the algebraic definition). If I am not mistaken, it can be a vector space.

Coefficient space can make a vector space, but I'm not sure if it is a good example of VectorArithmetic.
As VectorArithmetic is not just a linear space, but also provides magnitudeSquared.

But, anyway, your description of + seems to be consistent.

I did not see this sooner, but if I option-click on the _VectorMath, there's the quick help:

Thanks, frankly, I was missing it.
What is _VectorMath protocol? Conforming to VectorArithmetic and AdditiveArithmetic
 
 
Q