@@ -116,6 +116,9 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
116116 Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N}}} ) where {Style, Axes, N} = ($ f)(bc. args... )
117117 Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N}}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
118118 end
119+ if T == Float32
120+ @eval Base. broadcasted (:: typeof ($ f), arg:: Union{Array{F,N},Base.Broadcast.Broadcasted} ) where {N,F<: Union{Float32,Float64} } = ($ f)(maybecopy (arg))
121+ end
119122 end
120123 for (f, fa) in (twoarg_funcs... ,(:pow ,:pow ))
121124 f! = Symbol (" $(f) !" )
@@ -124,14 +127,19 @@ for (T, suff) in ((Float64, ""), (Float32, "f"))
124127 Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N},Array{$T,N}}} ) where {Style, Axes, N} = ($ f)(bc. args... )
125128 Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T,N},Array{$T,N}}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
126129 end
130+ if T == Float32
131+ @eval Base. broadcasted (:: typeof ($ f), arg1:: Union{Array{F, N},Base.Broadcast.Broadcasted} , arg2:: Union{Array{F, N},Base.Broadcast.Broadcasted} ) where {N,F<: Union{Float32,Float64} } = ($ f)(maybecopy (arg1), maybecopy (arg2))
132+ end
127133 end
128134end
129135
130136# Functions over single vectors that return scalars/tuples
131137for (T, suff) in ((Float32, " " ), (Float64, " D" ))
132138
133139 for (f, fa) in ((:maximum , :maxv ), (:minimum , :minv ), (:mean , :meanv ),
134- (:meansqr , :measqv ), (:meanmag , :meamgv ), (:sum , :sve ))
140+ (:meanmag , :meamgv ), (:meansqr , :measqv ), (:meanssqr , :mvessq ),
141+ (:sum , :sve ), (:summag , :svemg ), (:sumsqr , :svesq ),
142+ (:sumssqr , :svs ))
135143 @eval begin
136144 function ($ f)(X:: Vector{$T} )
137145 val = Ref {$T} (0.0 )
@@ -192,7 +200,133 @@ for (T, suff) in ((Float32, ""), (Float64, "D"))
192200 return result
193201 end
194202 end
203+
204+ @eval begin
205+ # Broadcasting override such that f.(X) turns into f(X)
206+ Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N},Array{$T,N}}} ) where {Style, Axes, N} = ($ f)(bc. args... )
207+ Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T,N},Array{$T,N}}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
208+ Base. broadcasted (:: typeof ($ f), arg1:: Union{Array{$T, N},Base.Broadcast.Broadcasted} , arg2:: Union{Array{$T, N},Base.Broadcast.Broadcasted} ) where {N} = ($ f)(maybecopy (arg1), maybecopy (arg2))
209+ end
195210 end
196211end
197212
213+ # Element-wise operations over a vector and a scalar
214+ for (T, suff) in ((Float32, " " ), (Float64, " D" ))
215+
216+ for (f, name) in ((:vsadd , " addition" ), (:vsdiv , " division" ), (:vsmul , " multiplication" ))
217+ f! = Symbol (" $(f) !" )
218+
219+ @eval begin
220+ @doc """
221+ `$($ f!) (result::Vector{$($ T) }, X::Vector{$($ T) }, c::$($ T) )`
222+
223+ Implements vector-scalar **$($ name) ** over **Vector{$($ T) }** and $($ T) and overwrites
224+ the result vector with computed value. *Returns:* **Vector{$($ T) }** `result`
225+ """ ->
226+ function ($ f!)(result:: Vector{$T} , X:: Vector{$T} , c:: $T )
227+ ccall (($ (string (" vDSP_" , f, suff), libacc)), Cvoid,
228+ (Ptr{$ T}, Int64, Ptr{$ T}, Ptr{$ T}, Int64, UInt64),
229+ X, 1 , Ref (c), result, 1 , length (result))
230+ return result
231+ end
232+ end
233+
234+ @eval begin
235+ @doc """
236+ `$($ f) (X::Vector{$($ T) }, c::$($ T) )`
237+
238+ Implements vector-scalar **$($ name) ** over **Vector{$($ T) }** and $($ T) . Allocates
239+ memory to store result. *Returns:* **Vector{$($ T) }**
240+ """ ->
241+ function ($ f)(X:: Vector{$T} , c:: $T )
242+ result = similar (X)
243+ ($ f!)(result, X, c)
244+ return result
245+ end
246+ end
247+ end
248+ f = :vssub
249+ f! = Symbol (" $(f) !" )
250+
251+ @eval begin
252+ @doc """
253+ `$($ f!) (result::Vector{$($ T) }, X::Vector{$($ T) }, c::$($ T) )`
254+
255+ Implements vector-scalar **subtraction** over **Vector{$($ T) }** and $($ T) and overwrites
256+ the result vector with computed value. *Returns:* **Vector{$($ T) }** `result`
257+ """ ->
258+ function ($ f!)(result:: Vector{$T} , X:: Vector{$T} , c:: $T )
259+ ccall (($ (string (" vDSP_vsadd" , suff), libacc)), Cvoid,
260+ (Ptr{$ T}, Int64, Ptr{$ T}, Ptr{$ T}, Int64, UInt64),
261+ X, 1 , Ref (- c), result, 1 , length (result))
262+ return result
263+ end
264+ end
265+
266+ @eval begin
267+ @doc """
268+ `$($ f) (X::Vector{$($ T) }, c::$($ T) )`
269+
270+ Implements vector-scalar **subtraction** over **Vector{$($ T) }** and $($ T) . Allocates
271+ memory to store result. *Returns:* **Vector{$($ T) }**
272+ """ ->
273+ function ($ f)(X:: Vector{$T} , c:: $T )
274+ result = similar (X)
275+ ($ f!)(result, X, c)
276+ return result
277+ end
278+ end
279+
280+ f = :svsub
281+ f! = Symbol (" $(f) !" )
282+
283+ @eval begin
284+ @doc """
285+ `$($ f!) (result::Vector{$($ T) }, X::Vector{$($ T) }, c::$($ T) )`
286+
287+ Implements vector-scalar **subtraction** over $($ T) and **Vector{$($ T) }** and overwrites
288+ the result vector with computed value. *Returns:* **Vector{$($ T) }** `result`
289+ """ ->
290+ function ($ f!)(result:: Vector{$T} , X:: Vector{$T} , c:: $T )
291+ ccall (($ (string (" vDSP_vsadd" , suff), libacc)), Cvoid,
292+ (Ptr{$ T}, Int64, Ptr{$ T}, Ptr{$ T}, Int64, UInt64),
293+ - X, 1 , Ref (c), result, 1 , length (result))
294+ return result
295+ end
296+ end
297+
298+ @eval begin
299+ @doc """
300+ `$($ f) (X::Vector{$($ T) , c::$($ T) })`
301+
302+ Implements vector-scalar **subtraction** over $($ T) and **Vector{$($ T) }**. Allocates
303+ memory to store result. *Returns:* **Vector{$($ T) }**
304+ """ ->
305+ function ($ f)(X:: Vector{$T} , c:: $T )
306+ result = similar (X)
307+ ($ f!)(result, X, c)
308+ return result
309+ end
310+ end
311+
312+ for f in (:vsadd , :vssub , :vsdiv , :vsmul )
313+ f! = Symbol (" $(f) !" )
314+ @eval begin
315+ # Broadcasting override such that f.(X) turns into f(X)
316+ Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N},$T}} ) where {Style, Axes, N} = ($ f)(bc. args... )
317+ Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T,N},$T}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
318+ Base. broadcasted (:: typeof ($ f), arg1:: Union{Array{$T, N},Base.Broadcast.Broadcasted} , arg2:: $T ) where {N} = ($ f)(maybecopy (arg1), arg2)
319+ end
320+ end
321+
322+ f = :svsub
323+ f! = Symbol (" $(f) !" )
324+
325+ @eval begin
326+ # Broadcasting override such that f.(X) turns into f(X)
327+ Base. copy (bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T, N}, $T}} ) where {Style, Axes, N} = ($ f)(bc. args... )
328+ Base. copyto! (dest:: Array{$T, N} , bc:: Base.Broadcast.Broadcasted{Style, Axes, typeof($f), Tuple{Array{$T,N}, $T}} ) where {Style, Axes, N} = ($ f!)(dest, bc. args... )
329+ Base. broadcasted (:: typeof ($ f), arg1:: Union{Array{$T, N},Base.Broadcast.Broadcasted} , arg2:: $T ) where {N} = ($ f)(maybecopy (arg1), arg2)
330+ end
331+ end
198332
0 commit comments