@@ -8,10 +8,15 @@ import Base: chop
8
8
@inline dot (M:: Int ,a:: Ptr{T} ,incx:: Int ,b:: Ptr{T} ,incy:: Int ) where {T<: Union{ComplexF64,ComplexF32} } =
9
9
BLAS. dotc (M,a,incx,b,incy)
10
10
11
- dotu (f:: StridedVector{T} ,g:: StridedVector{T} ) where {T<: Union{ComplexF32,ComplexF64} } =
11
+ dotu (f:: StridedVector{T} , g:: StridedVector{T} ) where {T<: Union{ComplexF32,ComplexF64} } =
12
12
BLAS. dotu (f,g)
13
- dotu (f:: AbstractVector{Complex{Float64}} ,g:: AbstractVector{N} ) where {N<: Real } = dot (conj (f),g)
14
- dotu (f:: AbstractVector{N} ,g:: AbstractVector{T} ) where {N<: Real ,T<: Number } = dot (f,g)
13
+ dotu (f:: AbstractVector{<:Complex} , g:: AbstractVector{<:Real} ) = dot (conj (f),g)
14
+ dotu (f:: AbstractVector{<:Real} , g:: AbstractVector{<:Real} ) = dot (f,g)
15
+ function dotu (f:: AbstractVector{<:Number} , g:: AbstractVector{<:Number} )
16
+ Base. require_one_based_indexing (f)
17
+ axes (f) == axes (g) || throw (ArgumentError (" vectors must have the same indices" ))
18
+ mapreduce (* , + , f, g)
19
+ end
15
20
16
21
17
22
normalize! (w:: AbstractVector ) = rmul! (w,inv (norm (w)))
100
105
scal! (n:: Integer ,cst:: BlasFloat ,ret:: DenseArray{T} ,k:: Integer ) where {T<: BlasFloat } =
101
106
BLAS. scal! (n,strictconvert (T,cst),ret,k)
102
107
103
- function scal! (n:: Integer ,cst:: Number ,ret:: AbstractArray ,k:: Integer )
104
- @assert k * n ≤ length (ret )
108
+ @inline function scal! (n:: Integer ,cst:: Number ,ret:: AbstractArray ,k:: Integer )
109
+ @boundscheck checkbounds (ret, 1 : (k * (n - 1 ) + 1 ) )
105
110
@simd for j= 1 : k: k* (n- 1 )+ 1
106
111
@inbounds ret[j] *= cst
107
112
end
@@ -115,6 +120,7 @@ scal!(cst::Number,v::AbstractArray) = scal!(length(v),cst,v,1)
115
120
# Helper routines
116
121
117
122
function reverseeven! (x:: AbstractVector )
123
+ Base. require_one_based_indexing (x)
118
124
n = length (x)
119
125
if iseven (n)
120
126
@inbounds @simd for k= 2 : 2 : n÷ 2
@@ -129,14 +135,15 @@ function reverseeven!(x::AbstractVector)
129
135
end
130
136
131
137
function negateeven! (x:: AbstractVector )
132
- @inbounds @simd for k = 2 : 2 : length (x)
133
- x[k] *= - 1
134
- end
138
+ Base . require_one_based_indexing (x)
139
+ v = view (x, 2 : 2 : length (x))
140
+ v .*= - 1
135
141
x
136
142
end
137
143
138
144
# checkerboard, same as applying negativeeven! to all rows then all columns
139
145
function negateeven! (X:: AbstractMatrix )
146
+ Base. require_one_based_indexing (X)
140
147
for j = 1 : 2 : size (X,2 )
141
148
@inbounds @simd for k = 2 : 2 : size (X,1 )
142
149
X[k,j] *= - 1
@@ -155,21 +162,15 @@ const alternatesign! = negateeven!
155
162
alternatesign (v:: AbstractVector ) = alternatesign! (copy (v))
156
163
157
164
function alternatingsum (v:: AbstractVector )
158
- ret = zero (eltype (v))
159
- s = 1
160
- @inbounds for k= 1 : length (v)
161
- ret+= s* v[k]
162
- s*= - 1
163
- end
164
-
165
- ret
165
+ sum (((a,b),) -> a* b, zip (v, Iterators. cycle ((1 ,- 1 ))))
166
166
end
167
167
168
168
# Sum Hadamard product of vectors up to minimum over lengths
169
169
function mindotu (a:: AbstractVector ,b:: AbstractVector )
170
- ret,m = zero (promote_type (eltype (a),eltype (b))),min (length (a),length (b))
171
- @inbounds @simd for i= m: - 1 : 1 ret += a[i]* b[i] end
172
- ret
170
+ Base. require_one_based_indexing (a)
171
+ Base. require_one_based_indexing (b)
172
+ m = min (length (a), length (b))
173
+ dotu (view (a, 1 : m), view (b, 1 : m))
173
174
end
174
175
175
176
@@ -240,25 +241,20 @@ function pad(A::AbstractMatrix,n::Integer,m::Integer)
240
241
Base. require_one_based_indexing (A)
241
242
T= eltype (A)
242
243
if n <= size (A,1 ) && m <= size (A,2 )
243
- A[1 : n,1 : m]
244
- elseif n== 0 || m== 0
245
- Matrix {T} (undef,n,m) # fixes weird julia bug when T==None
244
+ strictconvert (Matrix{T}, A[1 : n,1 : m])
246
245
else
247
246
ret = Matrix {T} (undef,n,m)
248
247
minn= min (n,size (A,1 ))
249
248
minm= min (m,size (A,2 ))
250
- for k= 1 : minn,j= 1 : minm
251
- @inbounds ret[k,j]= A[k,j]
252
- end
253
- for k= minn+ 1 : n,j= 1 : minm
254
- @inbounds ret[k,j]= zero (T)
255
- end
256
- for k= 1 : n,j= minm+ 1 : m
257
- @inbounds ret[k,j]= zero (T)
258
- end
259
- for k= minn+ 1 : n,j= minm+ 1 : m
260
- @inbounds ret[k,j]= zero (T)
261
- end
249
+
250
+ cinds = CartesianIndices ((1 : minn, 1 : minm))
251
+ copyto! (ret, cinds, A, cinds)
252
+
253
+ cinds = CartesianIndices ((minn+ 1 : n, 1 : minm))
254
+ ret[cinds] .= zero (T)
255
+
256
+ cinds = CartesianIndices ((axes (ret,1 ), minm+ 1 : m))
257
+ ret[cinds] .= zero (T)
262
258
263
259
ret
264
260
end
0 commit comments