←back to thread

132 points zfnmxt | 3 comments | | HN request time: 0s | source
Show context
eigenspace ◴[] No.40713949[source]
I’d strongly suggest not going the Numpy route, and instead reading about broadcast in julia [1]. In many contexts, it’s very important to distinguish between the element-wise application of a function, and a function that is applied to an entire array. A classic example would be the matrix exponential[2], versus the element-wise exponential of a matrix.

In julia the former is `exp(M)`, and the latter is `exp.(M)`

[1] https://julialang.org/blog/2017/01/moredots/

[2] https://en.m.wikipedia.org/wiki/Matrix_exponential

replies(4): >>40714080 #>>40714083 #>>40714427 #>>40715920 #
cycomanic ◴[] No.40714427[source]
I actually think Julia's behaviour (partially) copied from matlab is bad. I had to review a lot of matlab/Julia simulation code write by PhD/grad students (as well as my own) and most of the time when things somehow are not working debugging becomes a task of "find the missing dot". If they would have decided on a more obvious symbol (the dot is really just way too easy to overlook) for broadcasting it would be much better, but I still much prefer that broadcast and matrix operation would use entirely different operators/named functions. Instead we have the case that a little annotation can dramatically change the behaviour of the code (the matrix exponential is in fact a great example).

That said I mainly work with fields and calculus so I might be biased because the majority of operations are broadcast and I only rarely use matrix operations.

replies(2): >>40715052 #>>40715192 #
tomsmeding ◴[] No.40715052[source]
The nice thing is that if this dot-approach (or some other syntax with a similar effect) were implemented in Futhark, then because Futhark is statically-typed, all mismatches would be compile-time errors. Those are very hard to overlook :)
replies(1): >>40715159 #
1. setopt ◴[] No.40715159[source]
In the very common case of square matrices, I think the dotted and non-dotted operations are generally both valid?
replies(1): >>40718678 #
2. iav ◴[] No.40718678[source]
They might be both valid but will generate a different output (int or range) leading to a compile time error downstream
replies(1): >>40721744 #
3. cycomanic ◴[] No.40721744[source]
I don't think that's generally the case, AFAIK the example of the matrix exponential example a bit further up would not be really distinguishable as a type from a regular matrix (with exponential elements).