I came across a question that (eventually) landed me wondering about array arithmetic. I'm thinking specifically in Ruby, but I think the concepts are language independent.
So, addition and subtraction are defined, in Ruby, as such:
[1,6,8,3,6] + [5,6,7] == [1,6,8,3,6,5,6,7] # All the elements of the first, then all the elements of the second
[1,6,8,3,6] - [5,6,7] == [1,8,3] # From the first, remove anything found in the second
and array * scalar is defined:
[1,2,3] * 2 == [1,2,3,1,2,3]
But
What, conceptually, should the following be? None of these are (as far as I can find) defined:
- Array x Array:
[1,2,3] * [1,2,3] #=> ?
- Array / Scalar:
[1,2,3,4,5] / 2 #=> ?
- Array / Scalar:
[1,2,3,4,5] % 2 #=> ?
- Array / Array:
[1,2,3,4,5] / [1,2] #=> ?
- Array / Array:
[1,2,3,4,5] % [1,2] #=> ?
I've found some mathematical descriptions of these operations for set theory, but I couldn't really follow them, and sets don't have duplicates (arrays do).
Edit: Note, I do not mean vector (matrix) arithmetic, which is completely defined.
Edit2: If this is the wrong stack exchange, tell me which is the right one and I'll move it.
Edit 3: Add mod operators to the list.
Edit 4:
I figure array / scalar
is derivable from array * scalar
:
a * b = c
=> a = b / c
[1,2,3] * 3 = [1,2,3]+[1,2,3]+[1,2,3] = [1,2,3,1,2,3,1,2,3]
=> [1,2,3] = [1,2,3,1,2,3,1,2,3] / 3
Which, given that programmer's division ignore the remained and has modulus:
[1,2,3,4,5] / 2 = [[1,2], [3,4]]
[1,2,3,4,5] % 2 = [5]
Except that these are pretty clearly non-reversible operations (not that modulus ever is), which is non-ideal.
Edit: I asked a question over on Math that led me to Multisets. I think maybe extensible arrays are "multisets", but I'm not sure yet.
*
for sets and scalars. Some prefer the operators of ⊆ or ∪ (thats not a 'u') to describe these operations more succinctly and with less ambiguity. See sets and set operations[]
was overloaded). Then we were to swap code and write what our partner's code did in english. So whilea + b
returned one thing,b + a
returned something else. We learned that trying to make things too convenient and "elegant" through overloading made it difficult for someone who didn't expect it to work that way to read it. The law of least astonishment should be paramount in overloading.*
or/
characters, but still provide the functionality. But that still doesn't tell me what that functionality is. Most I've got so far is that maybe[1,2,3,4,5] / 2 == [[1,2], [3,4]]
(and thus[1,2,3,4,5] % 3 == [4,5]
), but I dunno if that's the right choice.