📜  Julia 中数组的数学运算

📅  最后修改于: 2022-05-13 01:55:44.338000             🧑  作者: Mango

Julia 中数组的数学运算

数学运算的帮助下,我们可以执行加法、减法、乘法、除法等等来计算两个矩阵或数组之间的结果。数学运算是任何高级编程语言的一个非常重要的部分。在 Julia 的世界中,为了与Python和Java等语言竞争,Julia 也启用了相同的功能但具有不同的语法。

加法运算

我们可以在+运算符的帮助下添加两个数组。

一维数组的示例:

# 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)

输出: