Julia: scalar + I => scalar

Created on 23 Oct 2017  路  4Comments  路  Source: JuliaLang/julia

Now that we've deprecated matrix + scalar, we can revisit the old A + I + 1 associativity chesnut. If we make scalar + I => scalar then we have (A + I) + 1 and A + (I + 1) both being the same error for matrix + scalar. This also would allow us to write generic polynomials that work for both scalars and square matrices since e.g. x^2 + 2x + I would evaluate to a scalar for scalar x and to a matrix for matrix x. Strictly this isn't breaking, so this need not be done for 1.0, but we may want to at least try it out to see if there are any things we need to break to make this work.

ref #17083, cc @eveydee, @dlfivefifty

linear algebra

Most helpful comment

Oh hey, we already do this:

julia> 1 + I
2

All 4 comments

For generic polynomial it needs to be written as x^2 + 2x + I(x), where I(::Int) = 1 and I(m::Matrix) = eye(m)? To handle zero order polynomials and to be type stable (in the reduction variable) when you would implement a function that takes a vector of coefficients and the point to evaluate the polynomial at.

Edit: Alternatively, you could write it as x^2 + 2x^1 + x^0. No need to use I at all. I actually think this is the right way to do it.

That's an additional concern, but only for writing very generic polynomial code 鈥撀爁or a specific polynomial with positive order, just having scalar + I => scalar is sufficient. In any case, the fact that there are additional concerns doesn't make this not a good change. For generic polynomial code, we can use x^2 + 2x + x^0 or something like that.

Oh hey, we already do this:

julia> 1 + I
2

Fixed by #23923.

Was this page helpful?
0 / 5 - 0 ratings