Skip to content

Commit bcb61a6

Browse files
hayatoikomaGaika
authored andcommitted
Flexible interface for linspace and range (#196)
1 parent 4ce4008 commit bcb61a6

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/array.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -95,17 +95,17 @@ length(a::AFArray) = prod(size(a))
9595
inv{T<:Complex,N}(X::AFArray{T,N})::AFArray{T,N} = inverse(X,AF_MAT_NONE)
9696
inv{T<:Real,N}(X::AFArray{T,N})::AFArray{T,N} = inverse(X,AF_MAT_NONE)
9797
range{T}(::Type{AFArray{T}}, a::Integer, b::Integer) = range(1, [b], 0, T) + T(a)
98-
function range{T1, T2}(::Type{AFArray{T1}}, a::T2, b::T2, c::Integer)
99-
x = b .* ones(AFArray{T1}, c)
100-
x[1] = a
98+
function range{T}(::Type{AFArray{T}}, start, step, len::Integer)
99+
x = step .* ones(AFArray{T}, len)
100+
x[1] = start
101101
cumsum(x)
102102
end
103-
function linspace{T}(::Type{AFArray}, a::T, b::T, c::Integer)
104-
a_fl = Float64(a)
105-
b_fl = Float64(b)
106-
dx = (b_fl - a_fl)/(Float64(c) - 1.0)
107-
range(AFArray{Float64}, a_fl, dx, c)
103+
range(::Type{AFArray}, a::Integer, b::Integer, c::Integer) = range(AFArray{Float64}, a, b, c)
104+
function linspace{T}(::Type{AFArray{T}}, start, stop, len::Integer)
105+
dx = T((stop - start)/(len - 1))
106+
range(AFArray{T}, start, dx, len)
108107
end
108+
linspace(::Type{AFArray}, start::Integer, stop::Integer, len::Integer) = linspace(AFArray{Float64}, start, stop, len)
109109
function Base.sum(A::AFArray, ndims)
110110
B = A
111111
for i in ndims

test/array.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,8 @@
1717
x = linspace(AFArray, 2, 3, 10)
1818
@test Array(x) collect(linspace(2, 3, 10))
1919
@test typeof(x) == AFArray{Float64, 1}
20+
x = linspace(AFArray{Float32}, 2, 3, 10)
21+
@test Array(x) collect(linspace(2, 3, 10))
22+
@test typeof(x) == AFArray{Float32, 1}
2023
end
2124
end

0 commit comments

Comments
 (0)