4 ms·
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, m
by CapmCrackaWaka 5y ago
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.
- roenxi 5y agoHow 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.
- CapmCrackaWaka 5y ago> 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.
- roenxi 5y agoPretty 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.
- CapmCrackaWaka 5y agoBoth 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.