Hacker News new | past | comments | ask | show | jobs | submit login

For instance, if you wanted to add 1 to every other element in the vector (1,2,3,4,5,6). To someone who has no programming experience, this may be a daunting task. But simply doing (1,0) + (1,2,3,4,5,6) works in R.



The only problem with this is that I don't personally find it obvious/intuitive that (1,0) + (1,2,3,4,5,6) = (2,2,4,4,6,6).

In fact, I would probably expect the output to be (1,0,1,2,3,4,5,6).


Indeed, no mathematician or statistician would think of `+` as concatenation. List concatenation isn't a relevant problem to most mathematicians. This is where the above comments about prior knowledge and context come into play. Likewise, when I multiply two vectors together, or multiply a scalar with a vector, I have a definite idea what I "want" out of that multiplication. For many programmers, they think of this more as an exercise in data-types than an expression of linear algebra.


Statisticians might see it differently, but I still don’t think it’s obvious that (1,0) represents effectively a repeating pattern that’s extended to the size of the other vector.

For instance, for the statement (a,b,c,d,e) + (a,b,c,d,e,f) are we really all saying that it’s clear, obvious and unambiguous that this means (a+a, b+b, c+c, d+d, e+e, f+a).

Personally if you showed me that and asked me to describe what would happen before reading this thread, I would have said it would either throw an error, concatenate the vectors or only add the matching elements. I wouldn’t personally guess that the first vector would be treated as a repeating sequence, but different people might make different assumptions - I just don’t think it’s particularly clear, and I struggle to believe it’s clear, unambiguous and obvious to statisticians too.


> I would have said it would either throw an error, concatenate the vectors or only add the matching elements

Heh this itself is common evidence of an (experienced) programmer's way of thinking. Remember, when dealing with mathematics, there's no machine (or runtime or similar abstraction) there lurking in the background, enforcing conditions, or even lending a physical reality. Operations in mathematics are defined. Notation in mathematics is just an operation encoded as an "arbitrary" symbol. Which leads to

> I just don’t think it’s particularly clear, and I struggle to believe it’s clear, unambiguous and obvious to statisticians too.

This happens to be convention in both a subset of textbooks and many programming environments. It's mostly an artifact of the notation.

If you want to explore what math notation "feels like" a bit more without learning math (which I _wholeheartedly_ recommend as it's incredibly useful), try out the APL programming language a bit. It evokes a similar atmosphere of notation conveying the idea of well-defined operations.


I guess that's the difference between people who prefer R vs. people who use more traditional languages. '+' means addition in the scientific world, if I'm trying to figure out how to add numbers the first thing I try is '+'. To me, those vectors are just that - vectors of numbers, not instances of a class (even though that is kind of true under the hood). I have a problem I want to solve, and R does a good job of giving me what I want.


In what branch of maths or science is (1,0) + (1,2,3,4,5,6) even defined? That operation shouldn't make sense to anyone; it is adding vectors of different dimensions.

The result should probably be vector promotion then (2,2,3,4,5,6). (2,2,4,4,6,6) is not a good answer.


Sure we can argue about the syntax and what ‘+’ should do but the point is that many people find the build in behavior of R to be intuitive and easy to learn, myself included.


How can something be intuitive if it maps to a totally arbitrary abstract concept? Does that operation even have a name?

That is the plus symbol and it is in the context of two vectors. The intuitive thing to do is to fail with an error if it is a mathematics operation, concatenate if it is a programming context or treat the two vectors as the same length by appending 0s to the shorter one if the goal is to be unhelpfully helpful.

Repeating a vector until the dimensions match then element-wise adding them may be convenient for you, and you may like it. Maybe even lots of people like it. But it is a tough sell for me to believe it is intuitive.


> Does that operation even have a name.

numpy calls it 'broadcasting', although this particular operation doesn't work. For example, this is valid in numpy:

`np.array([1,2]) > np.array([[1],[2]])`

The idea is that an attempt is made to handle the operation even if the arrays / matrices are incompatible. Extending the operations of matrices and vectors in this way allows for extremely concise operations that would otherwise be a pain, like in the original R example.


Pretty sure it isn't broadcasting, because

  > c(1, 0) + c(1, 2, 3, 4, 5, 6)
  [1] 2 2 4 4 6 6
succeeds but in numpy

  >>> numpy.array([1,0]) + numpy.array([1, 2, 3, 4, 5, 6])
  Traceback (most recent call last):
    File "<stdin>", line 1, in <module>
  ValueError: operands could not be broadcast together with shapes (2,) (6,)
ie, numpy correctly recognises that totally incompatible dimensions can't be added.


Both languages allow for incompatible arrays to be operated on in this manner though, just because it fails in numpy just means that numpy hasn't implemented that type of broadcasting. For instance, this operation:

np.array([1,2]) + np.array([[1],[2]])

is not broadly defined and understood in pure mathematics. Looking at it, it's not immediately obvious what it would do. You are adding a (1,2) matrix to a (2,1) matrix. It turns out, numpy extends the rules of matrix multiplication to matrix addition through the rule of broadcasting.

R is just doing the same thing, in a different way. These types of implementations are common in scientific computing because it creates easy ways to do these operations, programming robustness and rigidity be damned.




Consider applying for YC's Summer 2025 batch! Applications are open till May 13

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: