Git Product home page Git Product logo

Comments (8)

odow avatar odow commented on June 10, 2024 1

I think we can accept a PR for:

Base.complex(x::AbstractJuMPScalar, y::AbstractJuMPScalar) = x + im * y
Base.complex(x, y::AbstractJuMPScalar) = x + im * y
Base.complex(x::AbstractJuMPScalar, y) = x + im * y

but I'm not going to support Base.Complex. That's a constructor and we can't return an instance of the type.

from jump.jl.

odow avatar odow commented on June 10, 2024

I think defining Complex(a, b) to return something other than ::Complex is legal, but bad form in Julia, so I would prefer not to do this.

Do you mean complex(r, i)?

help?> complex
search: complex Complex ComplexF64 ComplexF32 ComplexF16 ComplexPlane ComplexVariable precompile __precompile__ Highs_compilationDate

  complex(r, [i])

  Convert real numbers or arrays to complex. i defaults to zero.

  Examples
  ≡≡≡≡≡≡≡≡

  julia> complex(7)
  7 + 0im
  
  julia> complex([1, 2, 3])
  3-element Vector{Complex{Int64}}:
   1 + 0im
   2 + 0im
   3 + 0im

  ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

  complex(T::Type)

  Return an appropriate type which can represent a value of type T as a complex number. Equivalent to typeof(complex(zero(T))).

  Examples
  ≡≡≡≡≡≡≡≡

  julia> complex(Complex{Int})
  Complex{Int64}
  
  julia> complex(Int)
  Complex{Int64}

julia> complex(1, 2)
1 + 2im

Is this really an improvement on a + im * b? Do you have a benchmark on a realistic instance? I don't really see how this could make a difference.

from jump.jl.

araujoms avatar araujoms commented on June 10, 2024

I really do mean Complex(a,b). No, I don't have a benchmark, but this is a function used deep inside almost every cone in Hypatia, so every microsecond matters.

@chriscoey I believe you're the one who wrote the original function with Complex(a,b) instead of a + im*b, care to comment?

from jump.jl.

odow avatar odow commented on June 10, 2024

Why does code in Hypatia need to work for JuMP variables?

In your own code, can't you just do:

my_complex(a, b) = Complex(a, b)
my_complex(a::AbstractJuMPScalar, b::AbstractJuMPScalar) = a + im * b

Complex(a,b) instead of a + im*b

a + im*b does

tmp = (im * b)::ComplexF64
(a + tmp)::ComplexF64

so it introduces an additional temporary.

from jump.jl.

araujoms avatar araujoms commented on June 10, 2024

Because several of the examples use JuMP.

And no, I'm not defining a my_complex function in order to add a method to JuMP. That's hideous and makes the code harder to read. Moreover, Complex is a really basic function, I'm certain that such a method will also be useful for other people.

from jump.jl.

chriscoey avatar chriscoey commented on June 10, 2024

I did Complex(a, b) in my original code for the reason Oscar mentioned (avoid a temp), and personally I think it's just as readable. Hypatia doesn't use JuMP internally, but some of the Hypatia examples do in order to help build the MOI models.

from jump.jl.

araujoms avatar araujoms commented on June 10, 2024

Great, that works for me, I'll do a PR. Shouldn't it be Union{GenericVariableRef{<:Real}, GenericAffExpr{<:Real}, GenericQuadExpr{<:Real}} instead of AbstractJuMPScalar, though?

from jump.jl.

odow avatar odow commented on June 10, 2024

I guess so. I assumed complex would just do the equivalent, but it throws a MethodError, so I guess we should match.

julia> complex(1+2im, 3)
ERROR: MethodError: no method matching complex(::Complex{Int64}, ::Int64)

Closest candidates are:
  complex(::Real, ::Real)
   @ Base complex.jl:175
  complex(::Complex)
   @ Base complex.jl:173

Stacktrace:
 [1] top-level scope
   @ REPL[150]:1

julia> complex(1+2im, 0)
ERROR: MethodError: no method matching complex(::Complex{Int64}, ::Int64)

Closest candidates are:
  complex(::Real, ::Real)
   @ Base complex.jl:175
  complex(::Complex)
   @ Base complex.jl:173

Stacktrace:
 [1] top-level scope
   @ REPL[151]:1

julia> complex(1+2im, 0im)
ERROR: MethodError: no method matching complex(::Complex{Int64}, ::Complex{Int64})

Closest candidates are:
  complex(::Complex)
   @ Base complex.jl:173

Stacktrace:
 [1] top-level scope
   @ REPL[152]:1

from jump.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.