Skip to content

Commit

Permalink
ndarray: type convertion of _minus_scalar (#354)
Browse files Browse the repository at this point in the history
Ref: #353
  • Loading branch information
iblislin committed Dec 7, 2017
1 parent ce0e237 commit f8d4f62
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/ndarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -598,13 +598,14 @@ broadcast_(::typeof(+), x::Real, y::NDArray) = x + y
Subtract a bunch of arguments from `dst`. Inplace updating.
"""
function sub_from!(dst::NDArray{T}, arg::NDArrayOrReal) where T
function sub_from!(dst::NDArray, arg::NDArrayOrReal)
@assert dst.writable
if isa(arg, Real)
_minus_scalar(dst, scalar = convert(T, arg), out = dst)
_minus_scalar(dst, scalar = arg, out = dst)
else
_minus!(dst, arg)
end
dst
end

import Base: -
Expand Down
5 changes: 5 additions & 0 deletions test/unittest/ndarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,11 @@ function test_minus()
@test t6 - scalar_small copy(a6 .- scalar_small)
@test t6 - scalar_large copy(a6 .- scalar_large)

info("NDArray::minus::scalar::type convert")
let x = mx.NDArray([1, 2, 3])
@test copy(x .- π) [-2, -1, 0]
end

info("NDArray::minus::type stablility")
let x = mx.zeros(dims), y = mx.ones(dims)
@inferred x - y
Expand Down

0 comments on commit f8d4f62

Please sign in to comment.