-
Notifications
You must be signed in to change notification settings - Fork 71
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
abstract type #2
Comments
I would go so far as to have a separate micro-API-ish package module AbstractIntervals
export AbstractInterval, LoHiInterval
abstract type AbstractInterval{T} end
struct LoHiInterval{T} <: AbstractInterval{T}
lo::T
hi::T
end
end # module as that will help get other interval packages all conformant |
In principle I certainly agree. However, currently we need to have intervals be a subtype of |
are you referring to dual.jl or another file? |
Yes, to be able to create julia v0.5> using ValidatedNumerics, ForwardDiff
julia v0.5> f(x) = x^2 - 2;
julia v0.5> X = 3..4 # Interval
[3, 4]
julia v0.5> ForwardDiff.derivative(f, X)
[6, 8] to get an enclosure of the derivative of |
And that restriction on Of course, we could certainly do abstract type AbstractInterval{T} <: Real end but I'm not sure if Tim will be happy with that for |
abstract type AbstractInterval{T, Orderedness<TotallyOrdered, e.g. linearly connected>} <: Real end maybe you have a better fitting second param (to become trait, probably) |
@timholy (why can we not already pre-furgate mathematical abstractions without tangling the numerical hierarchy?) |
In principle, I don't think an interval should be a subtype of julia> Factorization <: AbstractArray
false even though a More practically, I think (hope) I understand the bind this puts you in. Could one relax the requirement on $ git diff
diff --git a/src/dual.jl b/src/dual.jl
index dfed492..0b56063 100644
--- a/src/dual.jl
+++ b/src/dual.jl
@@ -4,7 +4,7 @@ const ExternalReal = Union{subtypes(Real)...}
# Dual #
########
-immutable Dual{N,T<:Real} <: Real
+immutable Dual{N,T}
value::T
partials::Partials{N,T}
end
@@ -13,7 +13,7 @@ end
# Constructors #
################
-Dual{N,T}(value::T, partials::Partials{N,T}) = Dual{N,T}(value, partials)
+# Dual{N,T}(value::T, partials::Partials{N,T}) = Dual{N,T}(value, partials)
function Dual{N,A,B}(value::A, partials::Partials{N,B})
T = promote_type(A, B)
@@ -144,11 +144,11 @@ end
isconstant(n::Dual) = iszero(partials(n))
-@ambiguous Base.isequal{N}(a::Dual{N}, b::Dual{N}) = isequal(value(a), value(b))
-@ambiguous Base.:(==){N}(a::Dual{N}, b::Dual{N}) = value(a) == value(b)
-@ambiguous Base.isless{N}(a::Dual{N}, b::Dual{N}) = value(a) < value(b)
-@ambiguous Base.:<{N}(a::Dual{N}, b::Dual{N}) = isless(a, b)
-@ambiguous Base.:(<=){N}(a::Dual{N}, b::Dual{N}) = <=(value(a), value(b))
+Base.isequal{N}(a::Dual{N}, b::Dual{N}) = isequal(value(a), value(b))
+Base.:(==){N}(a::Dual{N}, b::Dual{N}) = value(a) == value(b)
+Base.isless{N}(a::Dual{N}, b::Dual{N}) = value(a) < value(b)
+Base.:<{N}(a::Dual{N}, b::Dual{N}) = isless(a, b)
+Base.:(<=){N}(a::Dual{N}, b::Dual{N}) = <=(value(a), value(b))
for T in (AbstractFloat, Irrational, Real)
Base.isequal(n::Dual, x::T) = isequal(value(n), x)
@@ -210,11 +210,11 @@ Base.float{N,T}(n::Dual{N,T}) = Dual{N,promote_type(T, Float16)}(n)
# Addition/Subtraction #
#----------------------#
-@ambiguous @inline Base.:+{N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) + value(n2), partials(n1) + partials(n2))
+@inline Base.:+{N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) + value(n2), partials(n1) + partials(n2))
@ambiguous @inline Base.:+(n::Dual, x::Real) = Dual(value(n) + x, partials(n))
@ambiguous @inline Base.:+(x::Real, n::Dual) = n + x
-@ambiguous @inline Base.:-{N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) - value(n2), partials(n1) - partials(n2))
+@inline Base.:-{N}(n1::Dual{N}, n2::Dual{N}) = Dual(value(n1) - value(n2), partials(n1) - partials(n2))
@ambiguous @inline Base.:-(n::Dual, x::Real) = Dual(value(n) - x, partials(n))
@ambiguous @inline Base.:-(x::Real, n::Dual) = Dual(x - value(n), -(partials(n)))
@inline Base.:-(n::Dual) = Dual(-(value(n)), -(partials(n)))
@@ -225,7 +225,7 @@ Base.float{N,T}(n::Dual{N,T}) = Dual{N,promote_type(T, Float16)}(n)
@inline Base.:*(n::Dual, x::Bool) = x ? n : (signbit(value(n))==0 ? zero(n) : -zero(n))
@inline Base.:*(x::Bool, n::Dual) = n * x
-@ambiguous @inline function Base.:*{N}(n1::Dual{N}, n2::Dual{N})
+@inline function Base.:*{N}(n1::Dual{N}, n2::Dual{N})
v1, v2 = value(n1), value(n2)
return Dual(v1 * v2, _mul_partials(partials(n1), partials(n2), v2, v1))
end
@@ -236,7 +236,7 @@ end
# Division #
#----------#
-@ambiguous @inline function Base.:/{N}(n1::Dual{N}, n2::Dual{N})
+@inline function Base.:/{N}(n1::Dual{N}, n2::Dual{N})
v1, v2 = value(n1), value(n2)
return Dual(v1 / v2, _div_partials(partials(n1), partials(n2), v1, v2))
end
@@ -254,7 +254,7 @@ end
for f in (:(Base.:^), :(NaNMath.pow))
@eval begin
- @ambiguous @inline function ($f){N}(n1::Dual{N}, n2::Dual{N})
+ @inline function ($f){N}(n1::Dual{N}, n2::Dual{N})
v1, v2 = value(n1), value(n2)
expv = ($f)(v1, v2)
powval = v2 * ($f)(v1, v2 - 1) It doesn't pass |
Though one should get in touch with @jrevels before going down this road too far. If just removing the type restriction won't fly, one can still control dispatch even without "official" traits. One can use SimpleTraits.jl or just write them out manually:
|
In Julia at the moment, there simply isn't any way of saying that an interval (or a dual number, for that matter) is "something number-like" (e.g. is something like a ring, in the abstract algebra sense), without making it a subtype of In that sense, currently, I was discussing this offline with @jrevels, who may wish to comment about |
We may have had a "race condition" in our comments, but in case not: |
The docs on the AbstractArray interface may be a helpful model showing how |
Thanks @timholy, that's a very nice idea. |
I want to look both ways -- making it work best with the ways open through v0.6 (as this has been focused) and getting as clear and pithy as possible on what would allow us the requisite expressiveness within the sense of Julia as developing to write it right. The above @timholy is nice. To get nice and clean requires deeper support and, it feels, renewed attention now-ish. Any ideas welcome. I'd like to hear from Bill Hart on this too (email sent), he has spent a good deal of energy around computing the mathy abstract. |
@jrevels says that the restriction on the type of thing allowed inside a Nonetheless, he pointed out that an EDIT: This is the same reason that requires that Since we do not have multiple dispatch in Julia, it is thus not possible to inherit from an |
Are packages now using |
I guess there are many packages that restrict arguments of functions to |
I often use |
Can you give an example where you use the total order?
…On 8 Apr 2017 2:03 p.m., "Tim Holy" ***@***.***> wrote:
I often use Real when I'm assuming the existence of a total order, which
an interval doesn't support.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AALtTojYoZrpUnHsYsgfJLAkpo1K9VeRks5rt9oPgaJpZM4M12DS>
.
|
If I want to sort something? In other words, if I'm restricting the types allowed to be in a specific container, we don't currently have a |
Good point.
But I expect (though I don't currently have evidence) that there are
numerical functions whose arguments are specified as Real in order to
exclude eg Complex, and that would work with intervals.
…On 8 Apr 2017 2:15 p.m., "Tim Holy" ***@***.***> wrote:
If I want to sort something? In other words, if I'm restricting the types
allowed to be in a specific container, we don't currently have a
HasTotalOrder(T) trait, so I often use Real as a surrogate.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#2 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AALtTmVrW-wOIEP0o9pmv5e4Ksx68X_Dks5rt9y7gaJpZM4M12DS>
.
|
There are sorts of intervals over which a total ordering is available. Subsegments of a line segment are an example. You are constraining the nature of interval more than is necessary. How about preceeding_finitely_represented_value? My initial interest in coding Julia with interval types was to explore geometries and intervals with interval-valued bounds. |
@timholy "DualWorthy would allow you to opt-in for any type that you are willing to support the necessary operations for Dual" -- I would expect the author of the type |
Is there something that comes with @JeffBezanson type-system-bettering and its triangular dispatch that lets a type parameter itself be parametrized (directly)? |
But the answer to
The whole point of traits is to define multiple behaviors. Look more carefully at how |
3/4 out of two is not bad :) (a) should have been clearer, I use |
We should follow Tim Holy and use
parameterizing the underlying type
that fits well with an emerging pattern
AbstractTime{T}
etc.The text was updated successfully, but these errors were encountered: