Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about Complex{Irrational}? #21204

Closed
5 tasks done
giordano opened this issue Mar 28, 2017 · 13 comments
Closed
5 tasks done

What about Complex{Irrational}? #21204

giordano opened this issue Mar 28, 2017 · 13 comments
Labels
complex Complex numbers maths Mathematical functions

Comments

@giordano
Copy link
Contributor

giordano commented Mar 28, 2017

A Complex{Irrational} is allowed (e.g., Complex(pi, pi)), but there are a few functions that currently don't work with this type (e.g., exp, exp2, exp10; exp can be easily fixed if #21203 is merged, the other two functions can be fixed as part of the fix to #21200).

Should this type be kept (and non-working functions fixed) or automatically converted to, say, Complex{Float64}? I don't think this type is widely used (its odd representation in the REPL seems to confirm this) and any mathematical operation with it promotes it to another type, but I think that a decision should be taken about its status.

TODO list:

@giordano giordano changed the title What about Comlex{Irrational}? What about Complex{Irrational}? Mar 28, 2017
@ararslan
Copy link
Member

IMO we should add/widen methods to allow Complex{Irrational}. Seems silly to convert arguments when simply wrapping them in Complex.

@ararslan ararslan added maths Mathematical functions complex Complex numbers labels Mar 28, 2017
@giordano
Copy link
Contributor Author

giordano commented Mar 28, 2017

Seems silly to convert arguments when simply wrapping them in Complex.

I tend to agree. They can be promoted to Complex{BigFloat} by performing operations together with this type, but a method for big(::Complex{Irrational}) is currently missing:

big(z::Complex{<:Irrational}) = Complex{BigFloat}(z)

BTW, my fix for #21200 is to define

exp2(z::Complex{<:Real}) = exp2(float(z))

and change the current signature of exp2 to

function exp2(z::Complex{T}) where {T<:AbstractFloat}

Does it make sense?

@ararslan
Copy link
Member

Might make sense to define a big method for types that returns the Big* version, akin to what we do for float (e.g. float(Int64) == Float64). Then the method

big{T<:Real}(z::Complex{T}) = Complex{big(T)}(z)

could replace the separate ones for Integer and AbstractFloat.

@giordano
Copy link
Contributor Author

I'd like to work on this, but I'm unsure about what's the best way. I was going to define a method of big for each of Integer, Rational, Irrational and AbstractFloat (and automatically for their subtypes), but then noticed that float(T) is defined as

float{T<:Number}(::Type{T}) = typeof(float(zero(T)))

This is inefficient for Big* and doesn't handle Irrationals. Maybe define a method for big like this as fallback and then define a method for each type as I suggested above? What do you think?

@ararslan
Copy link
Member

big(::Type{<:Integer}) = BigInt
big(::Type{<:Union{AbstractFloat,Irrational}}) = BigFloat
big(::Type) = typeof(big(zero(T)))

should suffice I would think.

Backing up, how is big relevant to Complex{Irrational}?

@giordano
Copy link
Contributor Author

Backing up, how is big relevant to Complex{Irrational}?

Those methods allow promoting Complex{Irrational} to Complex{BigFloat} with the maximum precision. In general, Complex{Irrational} needs some love, that PR is part of this process ;-)

giordano added a commit to giordano/julia that referenced this issue Apr 27, 2017
@TotalVerb
Copy link
Contributor

I really don't see the purpose of Complex{Irrational{:π}} or similar singleton types. What is the significance of these values?

@giordano
Copy link
Contributor Author

giordano commented Apr 27, 2017

I don't have a particular purpose, but the type just exists, I don't see why making it impossible to use if it can be done with little effort (actually by improving other functions). In addition, complex(pi, pi) keeps the "irrationality" features, which can be sometimes useful, see above.

exp2(pi) doesn't work with x being Complex{Irrational} for the same reason for which doesn't work if x is Complex{Bool}. Fixing the functions listed in the first message can in turn make them wok for other custom types, this isn't limited to Complex{Irrational} at all.

@mschauer
Copy link
Contributor

Hm, there cannot be an irrational zero, I am pessimistic about the last bullet point.

@ararslan
Copy link
Member

You mean of allowing complex(π)? IMO that should be fine, just complex(π) = π + 0im.

@mschauer
Copy link
Contributor

mschauer commented Jul 23, 2017

You can achieve Complex{Float64}(0, pi) or even Complex{Real}(0, pi), but there cannot be a z::Complex{Irrational} which has real(z) == 0 because there is no irrational 0.

@ararslan
Copy link
Member

there cannot be a z::Complex{Irrational} which has real(z) = 0 because there is no irrational 0

Right. Though Complex{Irrational} could error on construction for rational input.

@giordano
Copy link
Contributor Author

giordano commented Jul 23, 2017

Each irrational number is a different type, given how Complex works now it's impossible to have real and imaginary parts with different types, even defining an "irrational zero".

The easiest fix is probably

complex(x::Irrational) = Complex{Float64}(x)

giordano added a commit to giordano/julia that referenced this issue Jul 23, 2017
`complex(x::Irrational)` cannot create a `Complex{Irrational}` instance, we have
to fall back on some other type, `Complex{Float64}` seems a reasonable option.

Closes JuliaLang#22878, see JuliaLang#21204.
giordano added a commit to giordano/julia that referenced this issue Jul 24, 2017
Create a `Complex{Real}` instance which preserves precision of the irrational
number.

Closes JuliaLang#22878, see JuliaLang#21204.
@giordano giordano closed this as completed Oct 1, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
complex Complex numbers maths Mathematical functions
Projects
None yet
Development

No branches or pull requests

4 participants