在 Julia 中通过集合广播一个函数——broadcast() 和 broadcast!() 方法
broadcast()
是 julia 中的内置函数,用于在指定的数组、元组或集合上广播函数f 。
Syntax: broadcast(f, As…)
Parameters:
- f: Specified function.
- As: Specified arrays, tuples or collections.
Returns: It returns the results of the process of broadcasting the function over the specified arrays, tuples or collections.
例子:
# Julia program to illustrate
# the use of broadcast() method
# Getting the results of the process
# of broadcasting the function over
# the specified arrays, tuples or collections
A = [1 3; 5 7; 9 11; 13 15; 17 19]
B = [2, 4, 6, 8, 10]
println(broadcast(+, A, B))
broadcast(-, A, B)
输出:
播送!()
broadcast!()
是 julia 中的一个内置函数,它与 broadcast()函数相同,但将 broadcast(f, As...) 的结果存储在指定的 dest 数组中。
Syntax: broadcast!(f, dest, As…)
Parameters:
- f: Specified function.
- dest: Specified destination array.
- As: Specified arrays, tuples or collections.
Returns: It returns the results of the process of broadcasting the function over the specified arrays, tuples or collections.
例子:
# Julia program to illustrate
# the use of broadcast !() method
# Getting the results of the process
# of broadcasting the function over
# the specified arrays, tuples or collections
A = [1; 2];
B = [3; 4];
broadcast !(+, B, A)
println(B)
println(A)
broadcast !(+, B, A, (5, 10))
println(B)
println(A)
输出: