Best practice for avoiding line break bugs in long expressions

Thanks for the pointer to logsumpexp.

Would you compute it in the log space and then exp() it at the end?

Not sure how I would apply logsumexp() in this case, as all the terms have multiplicative factors against the exp() functions.

Would appreciate your coaching on this topic.

I would try to keep everything in logs, if possible. You can use

a e^b = e^{log(a) + b}

for the coefficients.

1 Like

Honestly, I strongly disagree with this not throwing an error.
Splitting equations in multiple lines and starting each line with a + or - is so prevalent (eg. see almost any long equation in a paper) that it should throw an error if it is done
Imo the fact that writing a long equation in julia in the most logical way (starting with + or -) is a silent bug is very wrong.

  • “Your editor should see this” is noncence imo because afaik julia doesn’t care about indentation (you can write the inner part of a function on the same indentation as the outer part). My editor doesn’t warn me here.
  • “You shouldn’t write such long equations anyway” is also noncence imo because long equations are something naturally happening in for example dynamic equations of physical systems. Why should they be avoided? Atleast an error should be thrown when it is done wrongly.

(not trying to diss here on julia, I strongly love julia)

Consider this in a repl section:

julia> a = 5
5
julia> + 3 - 1
2
julia> println(a)
5

Now write it in a script

a = 5
       + 3 - 1
println(a)

Do you want this to print 7? That adds a lot of parser complexity, and makes the code run differently in repl and as a script.

An editor warning is the correct level for something like this.

2 Likes

That is what brackets are also made for, for grouping:

a = (5
   + 3 - 1)
println(a)

Do not understand why @dfdx’s answer above was not marked as the solution.

6 Likes

I use Juno with Atom, and

x = 5 + 7
  + 8 + 9
  - 1 * 2
println(x)

Does not produce an editor warning. Should this be reported as an issue with Juno?

No, Juno is in maintenance mode. This is technically valid code… and I tried it in VS Code. It has a linter, I think (enabled by default?), and seems it would be an excellent addition to it. Rather file an an issue at VS Code and/or (its?) linter? Link many other sections, the one on it in the docs is empty:

https://www.julia-vscode.org/docs/stable/userguide/linter/#Linter

1 Like