I'm working on a Swift package where I need to define operators for performing various arithmetic operations on vector and matrix structures. The operations are implemented by various functions in Accelerate for doing element-wise addition, matrix multiplication, etc. Operators for vector and matrix arithmetic tend to vary between different languages and packages. For example, element-wise matrix multiplication is performed with .*
or *
depending on the language or package. In the table below I tried to summarize some of the operators for NumPy, Julia, and MATLAB. In the last column I listed my preference for implementing these operators in a Swift package. I personally don't like the "dot" syntax because it clutters up the code with a bunch of dots. I also need to consider compound assignment operators for vectors and matrices such as *=
and +=
where I feel the dot syntax would be even more untidy, for example .+=
. However, I would like to get other peoples thoughts on these operators. So for a Swift package, what operator syntax would Swift users prefer for vector and matrix arithmetic? Are there other languages and/or packages that I should look at for inspiration?
Description | NumPy | Julia | MATLAB | Swift |
---|---|---|---|---|
Element-wise addition | + | .+ | + | + |
Element-wise subtraction | - | .- | - | - |
Element-wise multiplication | * | .* | .* | * |
Matrix multiplication | @ | * | * | ** |
Element-wise division | / | ./ | ./ | / |
Element-wise power | ** | .^ | .^ | ^ |