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

better names for norm functions? #1875

Closed
StefanKarpinski opened this issue Jan 2, 2013 · 26 comments
Closed

better names for norm functions? #1875

StefanKarpinski opened this issue Jan 2, 2013 · 26 comments
Labels
needs decision A decision on this change is needed speculative Whether the change will be implemented is speculative

Comments

@StefanKarpinski
Copy link
Sponsor Member

I've always found the convention of calling norm with a string or symbol argument indicating different norms both ugly and inconvenient and on the implementation side, checking various symbols is awful. It seems to me that vector and matrix norms deserve their own function names, so I'd like to propose better names, such as:

norm2(A)
norm1(A)
normp(p,A)
norminf(A)
normmax(A)
normfrob(A)
normscha(p,A)
normspec(A)
normnucl(A)

We can either get rid the Matlab-style norm function or leave it for compatibility define it by calling the various primitive norm functions listed here.

@johnmyleswhite
Copy link
Member

I actually really like the Matlab style norm function, but agree that its implementation is kind of awful. Why not just have norm(A, p) as the Lp norm be the default norm and only use extended names for the others?

@stevengj
Copy link
Member

stevengj commented Jan 2, 2013

It is pretty convenient to just be able to type norm(expression) to get a reasonable default norm of the expression regardless of type.

It's also not clear to me that matrix and vector norms should necessarily have different names, since the two are closely related. Two possibilities:

  • If normfoo(v) gives a vector norm, normfoo(M) could give the corresponding induced matrix norm. Problem: the induced norm may be hard to compute efficiently in general. (For the L2 norm you use the SVD, and for the L1 or Linf norm I guess you can get the induced norm by solving an LP, but for general Lp norms I'm not sure how you do it.)
  • Alternatively, if normfoo(v) computes a vector norm, normfoo(A) could simply compute the equivalent of normfoo(reshape(A,numel(A))), which is a valid matrix norm and can always be done in linear time. Other matrix norms could then have different names. Note that the Frobenius norm in this case is just an alias for norm2.

@toivoh
Copy link
Contributor

toivoh commented Jan 2, 2013

Actually, the induced 1-norm (inf-norm) of a matrix is even easier to compute: sum the absolute values along the columns (rows), then maximize over the sums. (This can also be used to overestimate the induced 2-norm, since norm2(A)^2 <= norm1(A)*norminf(A))

I have no idea about how to compute induced p-norms in general, though. I would still prefer for norm to compute induced matrix norms by default; I think that is what you most often want.

@ViralBShah
Copy link
Member

I think the proposed norm names are worse than what matlab does! I think that using symbols for stuff like Frobenius norm is a bit ugly, but a series of new names is uglier.

@StefanKarpinski
Copy link
Sponsor Member Author

I guess I'm alone on this one. I for one define frob(A) = norm(A,'frob') in all my Matlab codes. I'll leave this for a bit and see if anyone else chimes in, and if not, I'll just close it.

@timholy
Copy link
Sponsor Member

timholy commented Jan 2, 2013

I don't have strong preferences, but I confess to being sympathetic to this proposal. So you're not alone.

@toivoh
Copy link
Contributor

toivoh commented Jan 3, 2013

I'm with you too, Stefan. Those norms are not different versions of the same function, they are just different functions.

In matlab, the setup with a single norm function might possibly be useful for writing functions that use a norm specified by the user. In Julia, I think that such a thing would be done much better by accepting a norm function as an argument to the function.

@StefanKarpinski
Copy link
Sponsor Member Author

It seems like a good compromise here might be to give names to the different ways we're going to provide for computing norms – i.e. the different if/else cases in the norm function so that one can call them directly and then implement norm but checking the symbol parameter and calling the implementation functions.

@stevengj
Copy link
Member

stevengj commented Jan 3, 2013

I don't think implementing both styles is an improvement; better to pick one style and stick with it than have a lot of redundant functions.

I still think that norm(x, p) (defaulting to p=2) is a reasonable style at least for Lp norms on vectors and the corresponding induced norms (at least for p=1,2,inf) on matrices.

@StefanKarpinski
Copy link
Sponsor Member Author

The only redundant function then would be norm; the main change would be having individual entrypoints for specific norm functions instead of cramming them all into the body of norm. Even just as an internal implementation detail, it seems like better style. This would just be a matter of exposing those entrypoints in Base.

@StefanKarpinski
Copy link
Sponsor Member Author

Following Matlab, we would then have the following norm functions:

norm2(a)
norm1(a)
norminf(a)
normfrob(a)
normp(p,v)

@johnmyleswhite
Copy link
Member

And would lose norm? That seems like a mistake to me.

@StefanKarpinski
Copy link
Sponsor Member Author

No, that could stay.

@ViralBShah
Copy link
Member

I am not excited about having all these norm functions. The only ugly case is the Frobenius norm. In that case, I am happier having norm(a, :fro) as compared to 5 new normx functions.

@JeffBezanson
Copy link
Sponsor Member

The most discoverable name for that case is probably frobeniusnorm().

@johnmyleswhite
Copy link
Member

Why not just frobenius()?

@ViralBShah
Copy link
Member

I still think we just leave it as norm(a, :fro). Also, once we have keyword arguments, this will become a whole lot prettier.

@JeffBezanson
Copy link
Sponsor Member

I increasingly think things should be called What They Are Called. To get the frobenius norm you call frobeniusnorm(). The syntax norm(a, :fro) is not obvious to a matlab user either. I'm not sure what the keyword would be. norm(a, flavor=:fro)? If I'm being crazy here I'll just go back to my corner though.

@StefanKarpinski
Copy link
Sponsor Member Author

Obviously, I agree that norm(a, :fro) is not a good spelling. forbeniusnorm is a bit long-winded, but better.

@pao
Copy link
Member

pao commented Jan 11, 2013

Because this needs more opinions, I'm sympathetic to the argument for non-p norms, but prefer that the norm(a, p) be used for p-norms including the infinity-norm, defaulting to 2. Those really can all be expressed by parameterizing a single function, even if they're not implemented that way.

@johnmyleswhite
Copy link
Member

I agree with @pao that norm(a, p) is what norm should stand for. I think longer names for other functions is better, although I'm not sure why we can't have frobenius instead of frobeniusnorm.

@JeffBezanson
Copy link
Sponsor Member

I'm ok with frobenius. It's equally tab-completable.

@ViralBShah
Copy link
Member

I do not like frobenius as that does not even suggest that it is a norm. I suggest the following names, which are also tab-completable and discoverable in the REPL:

norm(x, p) # for 1,2,3... norms and Inf norm
normfro(x)

@nolta
Copy link
Member

nolta commented Jan 12, 2013

I think the problem w/ frobenius is that it could refer to either the norm or the number (http://en.wikipedia.org/wiki/Coin_problem).

I mildly prefer not splitting up norm, since even Mathematica follows Matlab's convention (e.g., Norm[x, "Frobenius"]). But frobeniusnorm is ok w/ me too.

@StefanKarpinski
Copy link
Sponsor Member Author

I like Viral's suggestion here: norm(x,p) for p = 1, 2, ..., Inf and normfro for the Frobenius norm. Other radically different norm functions can get their own function name and maybe be defined in a Norms package/module.

@ViralBShah
Copy link
Member

Ok, I will update the implementation accordingly. A Norms package for everything else is perfect.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs decision A decision on this change is needed speculative Whether the change will be implemented is speculative
Projects
None yet
Development

No branches or pull requests

9 participants