-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
make linear algebra math functions work for scalars #763
Comments
I'd suggest that your definition for norm is wrong: it should be norm(x::Number) = abs(x) |
I completely agree. On Apr 26, 2012, at 8:04, Michael van der Kolffreply@reply.github.com wrote:
|
What's the argument for having |
One of the main ones would be people coming from octave - people expect a 1x1 matrix to act as a scalar. |
That's a major misfeature of Matlab (and Octave by extension). Fixing mistakes like that is one the major reasons for a new language. |
The way I'd put it is that we're not going to be compatible with matlab or octave anyway, so we might as well fix little behaviors like that along the way. |
Yes, that's a better way to put it :-). Of course, we're not just creating a new language to fix little misfeatures like that. |
I disagree with your conclusion this thread. Scalars are vectors (they are vector spaces of dimension 1, and tensors of rank 0 for that matter). Norms and inner products of real and complex scalars are perfectly sensible operations, that (with the usual definitions) should give the absolute value and (conjugated) product respectively. Scalars are also perfectly good linear operators on the vector space of scalars, so operations like This is not a "misfeature" of Matlab. Linear algebra is not just for matrices in parentheses. |
I'm starting to come around to the view of identifying scalars with 0-tensors this way, but I still have some concerns about scalars that are also iterable, like strings. I'll have to sit down and figure out what whether they are actually a problem or not. For things like norm and dot, handling scalars is harmless. |
It's probably worth separating the Iterableness of things from their tensor rank. Strings aren't iterated using their indexing rules, for example. -- John On Dec 31, 2012, at 12:12 PM, Stefan Karpinski notifications@github.com wrote:
|
@stevengj: That's a perfectly valid mathematical argument for regular linear algebra, but it's not the whole story. What are scalars? The elements of arrays? Or non-arrays? (let's call those pure scalars) I would hate to introduce something into Julia that does exactly what you want most of the time, and then breaks things/introduces idiosyncrasies/hard to find bugs in less common cases; in this case arrays of arrays. That's the matlab fallacy to me. That said, I guess that a lot of the stuff that goes under the heading of treating scalars as 0-d arrays really comes down to cases when a function that always expects an array argument gets a non-array argument (pure scalar). Then it doesn't seem so dangerous to promote that pure scalar to be treated as a 0-d array. I guess that your suggestion about indexing into scalars goes under this heading; if you are indexing into it, you must have been expecting an array. Of course, I still didn't even say anything about indexable non-arrays like strings... |
@StefanKarpinski, when I say "scalar" I'm only talking about The argument is simple: if a documented Julia function @toivoh, I'm not sure what you're getting at here. I'm not arguing that all operations should work on both scalars and arrays, just the ones where it makes sense. Where is the "fallacy" or "idiosyncracy" introduced by defining (In fact, |
Oh, I definitely agree that we should have |
@stevengj Can you give an example of the advantage of having |
@stevengj: Ok, it seems that we're on the same page after all. I don't have anything in particular against |
@andreasnoackjensen, the advantage is that it makes it easier to write polymorphic routines that operate on either vectors or scalars. The more functions work for both and have consistent meanings, the fewer special cases you have to write manually. (Note that @toivoh, in the mathematical sense, real and complex scalars are vector spaces (and Hilbert spaces, and...); this viewpoint is totally standard and widespread. In the data-structure/computer-sciencey sense ("vector" == "array"), the two are somewhat different (although still very closely related). That's why it makes sense for nearly all linear-algebra functions to apply to numeric scalars as well as to arrays, whereas there might be other non-algebraic functions that make less sense or are ambiguous for scalars. |
Fixed by #1871. |
I was writing an optimization algorithm and wanted it to work with scalars and vectors. I noticed that
norm
anddot
aren't defined for scalar numbers.The definitions would be simple:
There might be more functions to consider defining for scalars as well.
The text was updated successfully, but these errors were encountered: