One vs 1 in algebra

I am writing functions which are basically algebra coded up (from an economic model), with code like

(1-βᵢ)*(1-αᵢ)*ωᵢ / ((1-βᵢ + γᵢ*βᵢ)*(1-αᵢ)*ωᵢ + γᵢβₖ*(1-αₖ)*ωₖ)

and it gets worse. They should work with Float64, Rational{Int64} (for unit tests!), and ForwardDiff.Dual.

Out of habit (following suggestions in the manual), I use one(...) for 1, but now I have come to question this, because the compiler seems to produce the same code for the literal 1 (which is neatly promoted) and one(...). MWE:

with1(x) = 1+x
withone(x) = one(x) + x

eg @code_warntype is the same.

So is it an OK habit to use 1 in formulas? Or is there a downside?

PS: I of course understand that a standalone application for a generic type where no promotion applies would require one(...) for type stability.

Yeah this is completely fine. Since you’re not storing the 1 in a variable or anything that could change type, the flow of types is unambiguous.

2 Likes

Talking about writing math, I sometimes wish there was a nice symbol for fractions that doesn’t require parenthesis. I haven’t really found anything very natural though.

∷(a,b) = a/b

f(x) = x+1 ∷ x-1

#vs

f(x) = (x+1) / (x-1)