Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Borrow MATLAB's ability to add a scalar to a vector #21

Open
PoochyEXE opened this issue Jun 16, 2022 · 0 comments
Open

Borrow MATLAB's ability to add a scalar to a vector #21

PoochyEXE opened this issue Jun 16, 2022 · 0 comments

Comments

@PoochyEXE
Copy link

MATLAB will happily let you add a scalar to a vector, or even a scalar to a matrix. If a is a matrix or vector and b is a scalar value, then a + b will return a copy of a with b added to every element, and likewise b + a, instead of doing the sensible thing and raising a type error.

Of course, we can generalize this a bit for BS. If a is an array, b is a non-array type, and op is an operator -- literally any operator in the whole language -- such that a op b would otherwise be invalid, then instead a op b returns an array with the same length of a such that for each element a_i in the original array a, the returned array has a_i op b in the same position. Likewise for b op a returning an array of b op a_i for each a_i in a.

This also implies that if a is an array of arrays, b is recursively applied to every array in a to produce the output array.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant