Array Multiplication and Division

Posted by Narfanator on Programmers See other posts from Programmers or by Narfanator
Published on 2013-06-19T17:59:43Z Indexed on 2013/06/24 16:36 UTC
Read the original article Hit count: 378

Filed under:
|
|
|

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.

© Programmers or respective owner

Related posts about math

Related posts about theory