diff --git a/base/linalg/matmul.jl b/base/linalg/matmul.jl index 64b49ff0016f1..e2bcbc6d96006 100644 --- a/base/linalg/matmul.jl +++ b/base/linalg/matmul.jl @@ -266,11 +266,83 @@ end # strides != 1 cases in libalg_blas.jl (*){T,S}(A::StridedMatrix{T}, B::StridedVector{S}) = generic_matvecmul('N', A, B) -arithtype(T) = T -arithtype(::Type{Bool}) = Int +*(::Type{Bool},::Type{Bool}) = Bool +*(::Type{Bool},::Type{Int}) = Int +*(::Type{Bool},::Type{Float32}) = Float32 +*(::Type{Bool},::Type{Float64}) = Float64 +*(::Type{Bool},::Type{Complex{Float32}}) = Complex{Float32} +*(::Type{Bool},::Type{Complex{Float64}}) = Complex{Float64} +*(::Type{Int},::Type{Bool}) = Int +*(::Type{Int},::Type{Int}) = Int +*(::Type{Int},::Type{Float32}) = Float32 +*(::Type{Int},::Type{Float64}) = Float64 +*(::Type{Int},::Type{Complex{Float32}}) = Complex{Float32} +*(::Type{Int},::Type{Complex{Float64}}) = Complex{Float64} +*(::Type{Float32},::Type{Bool}) = Float32 +*(::Type{Float32},::Type{Int}) = Float32 +*(::Type{Float32},::Type{Float32}) = Float32 +*(::Type{Float32},::Type{Float64}) = Float64 +*(::Type{Float32},::Type{Complex{Float32}}) = Complex{Float32} +*(::Type{Float32},::Type{Complex{Float64}}) = Complex{Float64} +*(::Type{Float64},::Type{Bool}) = Float64 +*(::Type{Float64},::Type{Int}) = Float64 +*(::Type{Float64},::Type{Float32}) = Float64 +*(::Type{Float64},::Type{Float64}) = Float64 +*(::Type{Float64},::Type{Complex{Float32}}) = Complex{Float64} +*(::Type{Float64},::Type{Complex{Float64}}) = Complex{Float64} +*(::Type{Complex{Float32}},::Type{Bool}) = Complex{Float32} +*(::Type{Complex{Float32}},::Type{Int}) = Complex{Float32} +*(::Type{Complex{Float32}},::Type{Float32}) = Complex{Float32} +*(::Type{Complex{Float32}},::Type{Float64}) = Complex{Float64} +*(::Type{Complex{Float32}},::Type{Complex{Float32}}) = Complex{Float32} +*(::Type{Complex{Float32}},::Type{Complex{Float64}}) = Complex{Float64} +*(::Type{Complex{Float64}},::Type{Bool}) = Complex{Float64} +*(::Type{Complex{Float64}},::Type{Int}) = Complex{Float64} +*(::Type{Complex{Float64}},::Type{Float32}) = Complex{Float64} +*(::Type{Complex{Float64}},::Type{Float64}) = Complex{Float64} +*(::Type{Complex{Float64}},::Type{Complex{Float32}}) = Complex{Float64} +*(::Type{Complex{Float64}},::Type{Complex{Float64}}) = Complex{Float64} +*{T<:Number,S,N}(::Type{T},::Type{Array{S,N}}) = Array{T*S,N} + +(+)(::Type{Bool},::Type{Bool}) = Int +(+)(::Type{Bool},::Type{Int}) = Int +(+)(::Type{Bool},::Type{Float32}) = Float32 +(+)(::Type{Bool},::Type{Float64}) = Float64 +(+)(::Type{Bool},::Type{Complex{Float32}}) = Complex{Float32} +(+)(::Type{Bool},::Type{Complex{Float64}}) = Complex{Float64} +(+)(::Type{Int},::Type{Bool}) = Int +(+)(::Type{Int},::Type{Int}) = Int +(+)(::Type{Int},::Type{Float32}) = Float32 +(+)(::Type{Int},::Type{Float64}) = Float64 +(+)(::Type{Int},::Type{Complex{Float32}}) = Complex{Float32} +(+)(::Type{Int},::Type{Complex{Float64}}) = Complex{Float64} +(+)(::Type{Float32},::Type{Bool}) = Float32 +(+)(::Type{Float32},::Type{Int}) = Float32 +(+)(::Type{Float32},::Type{Float32}) = Float32 +(+)(::Type{Float32},::Type{Float64}) = Float64 +(+)(::Type{Float32},::Type{Complex{Float32}}) = Complex{Float32} +(+)(::Type{Float32},::Type{Complex{Float64}}) = Complex{Float64} +(+)(::Type{Float64},::Type{Bool}) = Float64 +(+)(::Type{Float64},::Type{Int}) = Float64 +(+)(::Type{Float64},::Type{Float32}) = Float64 +(+)(::Type{Float64},::Type{Float64}) = Float64 +(+)(::Type{Float64},::Type{Complex{Float32}}) = Complex{Float64} +(+)(::Type{Float64},::Type{Complex{Float64}}) = Complex{Float64} +(+)(::Type{Complex{Float32}},::Type{Bool}) = Complex{Float32} +(+)(::Type{Complex{Float32}},::Type{Int}) = Complex{Float32} +(+)(::Type{Complex{Float32}},::Type{Float32}) = Complex{Float32} +(+)(::Type{Complex{Float32}},::Type{Float64}) = Complex{Float64} +(+)(::Type{Complex{Float32}},::Type{Complex{Float32}}) = Complex{Float32} +(+)(::Type{Complex{Float32}},::Type{Complex{Float64}}) = Complex{Float64} +(+)(::Type{Complex{Float64}},::Type{Bool}) = Complex{Float64} +(+)(::Type{Complex{Float64}},::Type{Int}) = Complex{Float64} +(+)(::Type{Complex{Float64}},::Type{Float32}) = Complex{Float64} +(+)(::Type{Complex{Float64}},::Type{Float64}) = Complex{Float64} +(+)(::Type{Complex{Float64}},::Type{Complex{Float32}}) = Complex{Float64} +(+)(::Type{Complex{Float64}},::Type{Complex{Float64}}) = Complex{Float64} function generic_matvecmul{T,S}(tA::Char, A::StridedMatrix{T}, B::StridedVector{S}) - C = Array(promote_type(arithtype(T),arithtype(S)), size(A, tA=='N' ? 1 : 2)) + C = Array(T*S + T*S, size(A, tA=='N' ? 1 : 2)) generic_matvecmul(C, tA, A, B) end @@ -323,7 +395,7 @@ end function generic_matmatmul{T,S}(tA, tB, A::StridedVecOrMat{T}, B::StridedMatrix{S}) mA, nA = lapack_size(tA, A) mB, nB = lapack_size(tB, B) - C = Array(promote_type(arithtype(T),arithtype(S)), mA, nB) + C = Array(T*S + T*S, mA, nB) generic_matmatmul(C, tA, tB, A, B) end diff --git a/base/operators.jl b/base/operators.jl index 018598b38201b..ead2ad71c15f1 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -143,8 +143,10 @@ At_ldiv_Bt(a,b) = transpose(a)\transpose(b) oftype{T}(::Type{T},c) = convert(T,c) oftype{T}(x::T,c) = convert(T,c) -zero(x) = oftype(x,0) -one(x) = oftype(x,1) +zero(x::Number) = oftype(x,0) +zero{T<:Number}(::Type{T}) = oftype(T,0) +one(x::Number) = oftype(x,1) +one{T<:Number}(::Type{T}) = oftype(T,1) sizeof(T::Type) = error(string("size of type ",T," unknown")) sizeof(T::DataType) = if isleaftype(T) T.size else error("type does not have a native size") end diff --git a/test/numbers.jl b/test/numbers.jl index 36e077d6950da..7fa772b98e4bb 100644 --- a/test/numbers.jl +++ b/test/numbers.jl @@ -1575,7 +1575,7 @@ approx_eq(a, b) = approx_eq(a, b, 1e-6) for T in [Int,BigInt], n = [1:1000,1000000] n = convert(T,n) f = factor(n) - @test n == prod([p^k for (p,k)=f]) + @test n == prod(T[p^k for (p,k)=f]) prime = n!=1 && length(f)==1 && get(f,n,0)==1 @test isprime(n) == prime