Skip to content

Commit 26b3a2f

Browse files
author
Joe Petviashvili
committed
add ifelse and isfinite to fix #180
1 parent 7025824 commit 26b3a2f

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/array.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ import Base: identity, ifft, imag, isinf, isnan, iszero, join, lgamma, log, log1
5858
import Base: minimum, mod, norm, prod, qr, randn, range, rank, real, rem, replace, round, select, show, inv
5959
import Base: sign, signbit, sin, sinh, sort, sortperm, std, sqrt, sum, svd, tan, tanh, transpose, trunc, var, any, all
6060
import Base: cat, hcat, vcat, conv, max, min, sizeof, similar, length, sizeof, vecnorm, linspace
61-
import Base: diag, diagm
61+
import Base: diag, diagm, isfinite, ifelse
6262

6363
similar(a::AFArray) = zeros(a)
6464
similar{T}(a::AFArray, ::Type{T}) = zeros(AFArray{T}, size(a))
@@ -102,6 +102,7 @@ function linspace{T}(::Type{AFArray}, a::T, b::T, c::Integer)
102102
dx = (b_fl - a_fl)/(Float64(c) - 1.0)
103103
range(AFArray{Float64}, a_fl, dx, c)
104104
end
105+
isfinite(a::AFArray) = !isinf(a) & !isnan(a)
105106

106107
import Base: /, *, +, -, ^, ==, <, >, <=, >=, !, !=, &, |, <<, >>, xor
107108

src/util.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Base: RefValue, @pure, show, clamp, find
22
import Base: cumsum, cumprod, cummin, cummax, chol, abs2
33

4-
export constant, select, get_last_error, err_to_string, sort_index, fir, iir
4+
export constant, get_last_error, err_to_string, sort_index, fir, iir
55
export mean_weighted, var_weighted, set_array_indexer, set_seq_param_indexer
66
export afeval
77

@@ -262,23 +262,23 @@ function constant{N}(val::UInt,sz::NTuple{N,Int})
262262
AFArray{UInt,N}(arr[])
263263
end
264264

265-
function select{T1,N1,T2,N2}(cond::AFArray{Bool},a::AFArray{T1,N1},b::AFArray{T2,N2})
265+
function ifelse{T1,N1,T2,N2}(cond::AFArray{Bool},a::AFArray{T1,N1},b::AFArray{T2,N2})
266266
out = RefValue{af_array}(0)
267267
_error(ccall((:af_select,af_lib),af_err,
268268
(Ptr{af_array},af_array,af_array,af_array),
269269
out,cond.arr,a.arr,b.arr))
270270
AFArray{typed(T1,T2),batched(N1,N2)}(out[])
271271
end
272272

273-
function select{T1,N1,T2<:Real}(cond::AFArray{Bool},a::AFArray{T1,N1},b::T2)
273+
function ifelse{T1,N1,T2<:Real}(cond::AFArray{Bool},a::AFArray{T1,N1},b::T2)
274274
out = RefValue{af_array}(0)
275275
_error(ccall((:af_select_scalar_r,af_lib),af_err,
276276
(Ptr{af_array},af_array,af_array,Cdouble),
277277
out,cond.arr,a.arr,Cdouble(b)))
278278
AFArray{typed(T1,T2),N1}(out[])
279279
end
280280

281-
function select{T1,T2,N2}(cond::AFArray{Bool},a::T1,b::AFArray{T2,N2})
281+
function ifelse{T1,T2,N2}(cond::AFArray{Bool},a::T1,b::AFArray{T2,N2})
282282
out = RefValue{af_array}(0)
283283
_error(ccall((:af_select_scalar_l,af_lib),af_err,
284284
(Ptr{af_array},af_array,Cdouble,af_array),

test/math.jl

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -266,13 +266,16 @@ for op in [:maximum, :minimum, :sum, :prod, :sizeof, :var, :std, :mean, :median,
266266
end
267267
end
268268

269-
a = AFArray([true false true])
270-
b = AFArray([1 2 3])
271-
c = AFArray([4 5 6; 7 8 9])
269+
aa = [true false true]
270+
bb = [1 2 3]
271+
cc = [4 5 6; 7 8 9]
272+
a = AFArray(aa)
273+
b = AFArray(bb)
274+
c = AFArray(cc)
272275

273-
@test Array(select(a, b, c)) == [1 5 3; 1 8 3]
274-
@test Array(select(a, 0, b)) == [0 2 0]
275-
@test Array(select(a, b, 0)) == [1 0 3]
276+
@test Array(ifelse(a, b, c)) == ifelse.(aa, bb, cc)
277+
@test Array(ifelse(a, 0, b)) == ifelse.(aa, 0, bb)
278+
@test Array(ifelse(a, b, 0)) == ifelse.(aa, bb, 0)
276279

277280
@test size(reshape(c, 6)) == (6, )
278281
@test size(reshape(c, (3,2))) == (3, 2)
@@ -301,3 +304,8 @@ ss[1] = 10
301304
@test s[1] != 10f0 && ss[1] == 10f0
302305
ssa = similar(s, Float64)
303306
@test typeof(ssa) == AFArray{Float64,1}
307+
308+
aa = [NaN Inf 3]
309+
a = AFArray(aa)
310+
311+
@test Array(@inferred isfinite(a)) == isfinite.(aa)

0 commit comments

Comments
 (0)