Array Multiplication and Division

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.

5

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.

1

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.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Array Multiplication and Division

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.

5

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.

1

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.

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật