Julia 中数组的数学运算
在数学运算的帮助下,我们可以执行加法、减法、乘法、除法等等来计算两个矩阵或数组之间的结果。数学运算是任何高级编程语言的一个非常重要的部分。在 Julia 的世界中,为了与Python和Java等语言竞争,Julia 也启用了相同的功能但具有不同的语法。
Mathematical Operations :
- Addition: This operation helps to add two arrays.
[1 2 3] + [4 5 10] = [5 7 13]
- Subtration: This operation helps to subtract two arrays.
[1 2 3] – [4 5 10] = [-3 -3 -7]
- Multiplication: This operation helps to multiply two arrays.
[1 2 3] * [4; 5; 10] = [44]
- Division: This operation helps to divide two arrays.
[1 2 3] / [4; 5; 10] = [3.1428…]
加法运算
我们可以在+运算符的帮助下添加两个数组。
一维数组的示例:
# Define and declare the 1D arrays
A = [1 2 3] # Shape 1X3
B = [4 5 10] # Shape 1X3
# Adding two arrays
gfg = A + B
print(gfg)
输出:
二维数组示例:
# Define and declare the 2D arrays
A = [1 2; -1 -2] # Shape 2X2
B = [4 5; 10 12] # Shape 2X2
# Adding two arrays
gfg = A + B
print(gfg)
输出:
3D 数组示例:
# Define and declare the 3D arrays
A = cat([1 2 3], [-1 -2 -3], [2 1 4], dims=3) # Shape 3X3
B = cat([4 5 2], [10 12 -5], [-1 2 1], dims=3) # Shape 3X3
# Adding two arrays
gfg = A + B
print(gfg)
输出:
减法运算
我们可以借助– 运算符减去两个数组。
一维数组示例:
# Define and declare the 1D arrays
A = [1 2 3] # Shape 1X3
B = [4 5 10] # Shape 1X3
# Subtracting two arrays
gfg = A - B
print(gfg)
输出:
二维数组示例:
# Define and declare the 2D arrays
A = [1 2; -1 -2] # Shape 2X2
B = [4 5; 10 12] # Shape 2X2
# Subtracting two arrays
gfg = A - B
print(gfg)
输出:
3D 数组示例:
# Define and declare the 3D arrays
A = cat([1 2 3], [-1 -2 -3], [2 1 4], dims=3) # Shape 3X3
B = cat([4 5 2], [10 12 -5], [-1 2 1], dims=3) # Shape 3X3
# Subtracting two arrays
gfg = A - B
print(gfg)
输出:
乘法运算
我们可以借助*运算符将两个数组相乘。
一维数组示例:
# Define and declare the 1D arrays
A = [1 2 3] # Shape 1X3
B = [4; 5; 10] # Shape 3X1
# Multiplying two arrays
gfg = A * B
print(gfg)
输出:
二维数组示例:
# Define and declare the 2D arrays
A = [1 2; -1 -2] # Shape 2X2
B = [4 5; 10 12] # Shape 2X2
# Multiplying two arrays
gfg = A * B
print(gfg)
输出:
3D 数组示例:
# Define and declare the 3D arrays
A = cat([1 2 3], [-1 -2 -3], [2 1 4], dims=3) # Shape 3X3
B = cat([4 5 2], [10 12 -5], [-1 2 1], dims=3) # Shape 3X3
# Multiplying two arrays
gfg = A * B
print(gfg)
输出:
分工操作
我们可以在/运算符的帮助下划分两个数组。
一维数组示例:
# Define and declare the 1D arrays
B = [1 2 3] # Shape 1X3
A = [4 5 10] # Shape 1X3
# Dividing two arrays
gfg = A / B
print(gfg)
输出:
二维数组示例:
# Define and declare the 2D arrays
B = [1 2; 1 2] # Shape 2X2
A = [4 5; 10 12] # Shape 2X2
# Dividing two arrays
gfg = B / A
print(gfg)
输出:
3D 数组示例:
# Define and declare the 3D arrays
A = cat([1 2 3], [-1 -2 -3], [2 1 4], dims=3) # Shape 3X3
B = cat([4 5 2], [10 12 -5], [-1 2 1], dims=3) # Shape 3X3
# Dividing two arrays
gfg = B / A
print(gfg)
输出: