Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
3  
I'm fairly sure for many of these, Ruby is just providing reasonably guessable operator-looking functions for set arithmetic that exists under other names (+ is a union, - is an intersect, etc). Not all possible mathematics operators necessarily make sense as an overload for values on a set. –  KChaloux Jun 19 '13 at 19:09
3  
You are thinking about sets. Set operations sometimes use arithmetic operators for convince, but union, intersect, minus, and difference might confuse people if you are trying to maintain some conceptual relations between * 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 –  MichaelT Jun 19 '13 at 19:09
2  
There was an assignment back in my days of academic C++ where we had to use some number of overloading operators in some code (even [] was overloaded). Then we were to swap code and write what our partner's code did in english. So while a + 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. –  MichaelT Jun 19 '13 at 19:19
    
MichaelT - I am thinking about sets, but I'm asking about arrays. That is, because arrays can have repeated elements and order matters, some of the set operations don't make as much sense –  Narfanator Jun 19 '13 at 20:02
    
MichaelT - I don't want to overload unless it makes enough sense for you to guess what it does before you know, and for you to go "of course it does that!" after you know. If there's multiple options and none are a clear winner, I'd just not use the * 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. –  Narfanator Jun 19 '13 at 20:06

2 Answers 2

Ruby’s model is provided more for convenience than correctness, and is inconsistent:

  • array + array is array concatenation, allowing duplicates, but array - array is set difference, removing duplicates: [1, 1] - [1] is [], not [1].

  • - is not the inverse of +, because it’s not the case that a + b - c == a for all Array instances a, b, and c: take [1] + [1] - [1].

  • array * fixnum is defined as iterated array concatenation, but fixnum * 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.

share|improve this answer
    
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 Jun 20 '13 at 5:50
    
The first sentance is the right answer. I wonder why it is still not accepted. –  user40989 Jan 7 at 15:31

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.

share|improve this answer
    
In Ruby, [1, 2, 3] * 2 => [1,2,3,1,2,3] –  Narfanator Jun 20 '13 at 5:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.