在 Julia 中跨数组广播
Julia 是一种具有高级语法的高性能动态编程语言。每个人都可以免费使用并且易于学习。 Julia 通常用于数据分析和可视化目的。
包含多维对象的网格称为多维数组。它可以包含任何类型的对象,但对于大多数计算目的,必须存在Int或Float类型的对象。在 Julia 中实现数组比其他计算语言更容易。数组可以以易于理解的方式编写在代码中,编译器负责实现性能和时间效率。在 Julia 中,也可以通过继承AbstractArray来定义自定义数组类型。
跨阵列广播
有时仅对数组的某些元素应用操作非常有用,例如将两个元素的向量添加到数组中。但是,如果通过将向量的大小增加到数组的大小来完成,则可能效率低下,尤其是对于具有大量元素和更多维度的数组。
Julia
# Creating two arrays with random elements
a = rand(2, 1);
b = rand(2, 4);
# Repeat function repeats the elements,
# specified number of times.
# Adding vector 'a' to 'b' by increasing the size of 'a'
repeat(a, 1, 4) + b
Julia
# Creating two 1D arrays of different sizes
A = [1];
B = [5,6,7,8];
# Adding 'A' to 'B' by increasing it's size
# We use the addition operator in the broadcast function
broadcast(+, A, B)
Julia
# Applying the broadcast function
# to perform addition on a 2D array
A = [1 2];
B = [5 6; 7 8; 9 10; 11 12];
broadcast(+, A, B)
Julia
# Applying the broadcast function
# to perform addition on a 3D array
A = [2 3];
B = cat([4 5;6 7], [8 9;10 11],
[12 13;14 15], dims = 3);
broadcast(+, A, B)
Julia
# Equating all elements of the 1D array to the element '7'
[1, 2, 3, 4].= [7]
Julia
# Implementing addition, multiplication and
# subtraction operations to broadcast to a 2D array
[1,2].+[1 2 3; 4 5 6]
[1,2].*[1 2 3; 4 5 6]
[1,2].-[1 2 3; 4 5 6]
Julia
# Implementing addition operator to broadcast to a 3D array
cat([4 5; 6 7] , [8 9; 10 11] , [12 13; 14 15] , dims = 3).+[1, 2]
Julia
# Broadcasting to create strings with additional elements to a 1D array
string.(1:3, ".", ["ONE", "TWO", "THREE"])
Julia
# Broadcasting to convert elements
# of a 2D array to the type Float32
convert.(Float32, [1 2 3; 4 5 6])
Julia
# Broadcasting to calculate ceiling
# of the decimal elements in a 3D array
ceil.((UInt8,), cat([4 5; 6 7] ,
[8 9; 10 11] ,
[12 13; 14 15] , dims = 3))
输出:
为了解决这个问题,Julia 提供了broadcast()函数,该函数将单维数组的大小增加到我们要操作的数组并应用我们提供的函数,而不使用任何额外的内存。因此,对于大型数组,不会浪费内存。该函数不仅对数组有效,对其他结构也有效。
我们可以在broadcast()函数中传递的参数如下:
- 运算符/函数
- 要添加的数组
- 要添加的数组
在一维数组上应用函数:
朱莉娅
# Creating two 1D arrays of different sizes
A = [1];
B = [5,6,7,8];
# Adding 'A' to 'B' by increasing it's size
# We use the addition operator in the broadcast function
broadcast(+, A, B)
将函数应用于二维数组:
朱莉娅
# Applying the broadcast function
# to perform addition on a 2D array
A = [1 2];
B = [5 6; 7 8; 9 10; 11 12];
broadcast(+, A, B)
将函数应用于 3D 数组:
朱莉娅
# Applying the broadcast function
# to perform addition on a 3D array
A = [2 3];
B = cat([4 5;6 7], [8 9;10 11],
[12 13;14 15], dims = 3);
broadcast(+, A, B)
使用点运算符
我们可以使用点运算符(.+、.-、.*、.=)来实现与广播()函数相同的操作
在一维数组上使用点运算符:
朱莉娅
# Equating all elements of the 1D array to the element '7'
[1, 2, 3, 4].= [7]
在二维数组上使用点运算符:
朱莉娅
# Implementing addition, multiplication and
# subtraction operations to broadcast to a 2D array
[1,2].+[1 2 3; 4 5 6]
[1,2].*[1 2 3; 4 5 6]
[1,2].-[1 2 3; 4 5 6]
在 3D 数组上使用点运算符:
朱莉娅
# Implementing addition operator to broadcast to a 3D array
cat([4 5; 6 7] , [8 9; 10 11] , [12 13; 14 15] , dims = 3).+[1, 2]
Julia 为我们提供了许多可以在数组上广播、应用各种操作的函数。我们可以使用点运算符来实现这些功能,如下所示。
朱莉娅
# Broadcasting to create strings with additional elements to a 1D array
string.(1:3, ".", ["ONE", "TWO", "THREE"])
朱莉娅
# Broadcasting to convert elements
# of a 2D array to the type Float32
convert.(Float32, [1 2 3; 4 5 6])
朱莉娅
# Broadcasting to calculate ceiling
# of the decimal elements in a 3D array
ceil.((UInt8,), cat([4 5; 6 7] ,
[8 9; 10 11] ,
[12 13; 14 15] , dims = 3))