Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In what way is the distinction between scalars and vectors useful in APL?

Tags:

apl

I'm an experienced software engineer beginning to study APL, and in order to get a better feel for the language early on, I would like to understand why the language exposes to the coder the difference between scalars and vectors. As far as I can tell this far into my studies, this only limits flexibility without yielding any benefit to compensate for it. The result is APL code cluttered with otherwise-needless workarounds such as ravels, encloses, and discloses.

Since APL is such a bizarre and hard-to-read language, I'm building my own helper library in APL to make my own APL-ish interface, and getting away from using wonky "idioms" in raw APL to do everyday tasks. Before I build in abstraction of the scalar/vector distinction throughout my helper library, should I be aware of any utility I may be sacrificing by doing so?

Thanks!

like image 910
Erik Midtskogen Avatar asked Oct 28 '22 21:10

Erik Midtskogen


1 Answers

If you're writing a function that requires a vector, I'd suggest you to forget about the possibility of the input being a scalar (consider it undefined behavior), and force the caller to ravel the argument to a vector. It should by far be the minority case (and if it isn't, you're doing something wrong). Just like if you wrote a function taking a matrix, you wouldn't expect it to be given a vector. Using a scalar in a vectors place is as strange as using a vector in a matrices place.

It is indeed strange that 1 2 3 and 1 2 are vectors, but 1 alone is a scalar, so if you want consistency, don't create vectors with A B C notation (aka strand notation) and forget that arrays can be created like that completely. Of course, I don't actually expect you to do that, and I would like if there was a notation that could create any size vectors all in the same way.

Also, don't forget that APL supports arrays with rank > 1, using which correctly are a major part of writing APL well. Scalars are rank 0 (0-dimensional), vectors - 1 (1D), matrices - 2 (2D), etc.

like image 65
dzaima Avatar answered Dec 28 '22 15:12

dzaima