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.
2 Answers 2
Ruby’s model is provided more for convenience than correctness, and is inconsistent:
array + array
is array concatenation, allowing duplicates, butarray - array
is set difference, removing duplicates:[1, 1] - [1]
is[]
, not[1]
.-
is not the inverse of+
, because it’s not the case thata + b - c == a
for allArray
instancesa
,b
, andc
: take[1] + [1] - [1]
.array * fixnum
is defined as iterated array concatenation, butfixnum * array
is not defined at all.
For purely array-based operations, I would expect +
and -
to be inverses:
[1, 2] + [3, 1] == [1, 2, 3, 1]
[1, 2, 3, 1] - [3, 1] == [1, 2]
-
would remove elements from the tail just as +
added them. Similarly for *
and /
:
[1, 2] * 3 == [1, 2, 1, 2, 1, 2]
[1, 2, 1, 2, 1, 2] / 3 == [1, 2]
[5, 1, 2, 1, 2] / 2 == [1, 2]
/
would first discard elements from the left until a.size % b == 0
. Why from the left? Well, I would expect an array modulus operator to satisfy the law:
a % b == a - (b * (a / b))
And that rule seems to work if you go through a few examples:
[1, 1] % 2 == [1, 1] - (2 * ([1, 1] / 2)) == []
[5, 1, 1] % 2 == [5, 1, 1] - (2 * ([5, 1, 1] / 2)) == [5]
This is basically defining division as iterated subtraction.
There are a couple of consistent and reasonably intuitive interpretations of array ♦ array
:
Cartesian product:
[1, 2] ♦ [3, 4] == [1 ♦ 3, 1 ♦ 4, 2 ♦ 3, 2 ♦ 4]
Pairwise product:
[1, 2] ♦ [3, 4] == [1 ♦ 3, 2 ♦ 4]
With a Cartesian product, the size of the result is the product of the size of the inputs. This is how list comprehensions and the list monad work in Haskell:
[x ♦ y | x <- [1, 2], y <- [3, 4]]
do
x <- [1, 2]
y <- [3, 4]
return (x ♦ y)
A pairwise product also makes sense, in that ([x1, y1, z1] * [x2, y2, z2]).reduce(:+)
would be the dot product of the vectors [x1, y1, z1]
and [x2, y2, z2]
. Of course, you would need to define the result when the inputs are of different lengths; in Haskell, the zipWith
function takes the shorter of the two input lists:
zipWith (♦) [1, 2] [3, 4, 5]
== zipWith (♦) [1, 2] [3, 4]
So the answer is that there are several possible interpretations, the choice of which is up to the designers of languages and libraries. As long as they’re self-consistent, none of them is strictly more "right" or "intuitive" than any other. The established convention in array languages is for array * array
to refer to pairwise product, because this generalises well to higher dimensions of array, and from promoting scalars to arrays of appropriate dimension.
-
Thanks! Informative and detailed explanation. I'm going to keep thinking this over, because it sounds like Ruby's concept of Array doesn't fit into the Vector or Set concept, and I don't want to make it.Narfanator– Narfanator2013年06月20日 05:50:50 +00:00Commented Jun 20, 2013 at 5:50
In my opinion, there is no reason that arithmetic operators ought to apply to arrays. Attempting to force arrays to have meaningful semantics with arithmetic operators is confusing, and confusion is the source of bugs. Even the Ruby semantics you name are not as obvious as you might think.
For example, notice the behavior of multiplication by a scalar. It might be reasonable to assume that multiplication by an integer will be equivalent to repeated addition, but that isn't the case:
[1, 2, 3] * 2 == [2, 4, 6]
[1, 2, 3] + [1, 2, 3] == [1, 2, 3, 1, 2, 3]
(I haven't checked this, but am going on what you posted.) As such, it can be argued that multiplication by a scalar behaves non-intuitively. (On the other hand, it behaves exactly like vector multiplication by a scalar, so in that sense it is intuitive. Still, notice that vector multiplication by a scalar is not an arithmetic operation.)
What should the behavior of these other operators be? In my opinion, it was a mistake to provide operator+
etc. for arrays, precisely because these operators cannot behave similarly to the usual arithmetic operators - it would have been better to either expand the set of available operators (to define unique operators that make sense with arrays -- to borrow an example, Haskell uses ++
for array concatenation), OR to use non-operator functions to implement these semantics (for example, [1, 2, 3].append [4, 5, 6]
may behave similarly to [1, 2, 3] + [4, 5, 6]
).
In any case, I would not overload the meanings of operator symbols across unrelated types like this.
-
1In Ruby,
[1, 2, 3] * 2 => [1,2,3,1,2,3]
Narfanator– Narfanator2013年06月20日 05:40:55 +00:00Commented Jun 20, 2013 at 5:40 -
"On the other hand, it behaves exactly like vector multiplication by a scalar, so in that sense it is intuitive." I disagree with this sentence. In mathematics, if
k
is a scalar andv
is a vector, thenk v
is scalar-vector multiplication, butv k
is undefined. It's always scalar-vector multiplication, and never vector-scalar multiplication.Stef– Stef2024年01月31日 18:48:56 +00:00Commented Jan 31, 2024 at 18:48
*
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.