Skip to content

Commit

Permalink
promotion in min/max. closes #212.
Browse files Browse the repository at this point in the history
add isless for tuples
  • Loading branch information
JeffBezanson committed Mar 16, 2012
1 parent d5a6602 commit 1bf9937
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 17 deletions.
5 changes: 3 additions & 2 deletions jl/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -627,8 +627,9 @@ end
function isless(A::AbstractArray, B::AbstractArray)
nA, nB = numel(A), numel(B)
for i = 1:min(nA, nB)
if !isequal(A[i], B[i])
return isless(A[i], B[i])
a, b = A[i], B[i]
if !isequal(a, b)
return isless(a, b)
end
end
return nA < nB
Expand Down
17 changes: 2 additions & 15 deletions jl/operators.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
## types ##

<:(T,S) = subtype(T,S)
>:(T,S) = subtype(S,T)
const (<:) = subtype

super(T::Union(CompositeKind,BitsKind,AbstractKind)) = T.super

Expand All @@ -21,7 +20,7 @@ isequal(x,y) = is(x,y)
isequal(x::Number, y::Number) = x==y
isless(x::Real, y::Real) = x<y

max(x,y) = x > y ? x : y
max(x,y) = y < x ? x : y
min(x,y) = x < y ? x : y

## definitions providing basic traits of arithmetic operators ##
Expand Down Expand Up @@ -159,18 +158,6 @@ end

macro vectorize_2arg(S,f)
quote
# function ($f){T<:$S}(x::T, y::AbstractArray{T,1})
# [ ($f)(x,y[i]) | i=1:length(y) ]
# end
# function ($f){T<:$S}(x::AbstractArray{T,1}, y::T)
# [ ($f)(x[i],y) | i=1:length(x) ]
# end
# function ($f){T<:$S}(x::T, y::AbstractArray{T,2})
# [ ($f)(x,y[i,j]) | i=1:size(y,1), j=1:size(y,2) ]
# end
# function ($f){T<:$S}(x::AbstractArray{T,2}, y::T)
# [ ($f)(x[i,j],y) | i=1:size(x,1), j=1:size(x,2) ]
# end
function ($f){T1<:$S, T2<:$S}(x::T1, y::AbstractArray{T2})
reshape([ ($f)(x, y[i]) | i=1:numel(y) ], size(y))
end
Expand Down
6 changes: 6 additions & 0 deletions jl/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ mod(x::Real, y::Real) = mod(promote(x,y)...)
mod1(x::Real, y::Real) = mod1(promote(x,y)...)
cmp(x::Real, y::Real) = cmp(promote(x,y)...)

max(x::Real, y::Real) = max(promote(x,y)...)
min(x::Real, y::Real) = min(promote(x,y)...)

## catch-alls to prevent infinite recursion when definitions are missing ##

no_op_err(name, T) = error(name," not defined for ",T)
Expand All @@ -74,3 +77,6 @@ no_op_err(name, T) = error(name," not defined for ",T)

=={T<:Number}(x::T, y::T) = no_op_err("==", T)
<{T<:Real}(x::T, y::T) = no_op_err("<", T)

max{T<:Real}(x::T, y::T) = y < x ? x : y
min{T<:Real}(x::T, y::T) = x < y ? x : y
11 changes: 11 additions & 0 deletions jl/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,17 @@ function isequal(t1::Tuple, t2::Tuple)
return true
end

function isless(t1::Tuple, t2::Tuple)
n1, n2 = length(t1), length(t2)
for i = 1:min(n1, n2)
a, b = t1[i], t2[i]
if !isequal(a, b)
return isless(a, b)
end
end
return n1 < n2
end

## functions ##

copy(x::Tuple) = map(copy, x)
Expand Down

0 comments on commit 1bf9937

Please sign in to comment.