📜  Julia 中的数组接口

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

Julia 中的数组接口

数组接口是 Julia 中数组必须遵循的语法约定。本文介绍了可用于在 Julia 中构造数组接口的各种方法。它还解释了如何在数组接口中执行索引以访问其元素。

可以通过采用标准数组并将自定义键与每个维度的索引链接起来来构建数组接口。这可以通过使用AxisIndices包来实现。

使用 AxisArray函数构造数组:

可以使用 Julia 的 AxisIndices 包中的AxisArray()函数创建数组。

例子:



Julia
# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using AxisArray function to return an array 
# with its dimension names and axis values
axis = AxisArray(arr, ["row1", "row2"], 
                      [:col1, :col2])


Julia
# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using AxisArray function to return an array 
# with its dimension names and axis values
axis = AxisArray{Int}(undef, ["row1", "row2"], 
                             [:col1, :col2]);
  
axis[:, :] = arr;
  
axis


Julia
# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using AxisArray function to return an array 
# with its dimension names and axis values
axis = AxisArray(arr, ["row1", "row2"], 
                      [:col1, :col2])
  
# Using NamedAxisArray function
# to return an array with named axes
axis_named = NamedAxisArray{(:xdim, :ydim)}(axis)


Julia
# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using MetaAxisArray function to return 
# an array with its dimension names, 
# axis values, metadata
axis_meta = MetaAxisArray(arr, (["row1", "row2"], 
                                [:col1, :col2]), 
                          metadata = "Array Interface")


Julia
# Julia program to perform indexing 
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function 
# to return an array with 
# its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return 
# an array with its dimension names 
# and axis values
axis = AxisArray(arr, ((1:2)m,
                ["col1", "col2", "col3"]))


Julia
# Julia program to perform indexing 
# in an array interface 
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function
# to return an array
# with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function
# to return an array 
# with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# displaying only the first row 
# and all the columns
axis[1,:]


Julia
# Julia program to perform indexing 
# in an array interface 
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return 
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return
# an array with its dimension names 
# and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# displaying the first two rows
# and the first two columns
axis[1:2, 1:2]


Julia
# Julia program to perform indexing
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return 
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return
# an array with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# displaying the first 3 elements of the array
axis[1:3]


Julia
# Julia program to perform indexing 
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return
# an array with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# accessing an element using its specified keys
axis[1m, "col1"]


Julia
# Julia program to perform indexing
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return 
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return 
# an array with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# Applying filters to the keys
axis[!=(1m), in(["col1", "col2"])]


输出:

数组接口也可以使用以下语法初始化:

句法:

Array{T}(undef, dims)

例子:

朱莉娅

# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using AxisArray function to return an array 
# with its dimension names and axis values
axis = AxisArray{Int}(undef, ["row1", "row2"], 
                             [:col1, :col2]);
  
axis[:, :] = arr;
  
axis

输出:



使用 NamedAxisArray()函数构造数组:

可以使用 Julia 的 AxisIndices 包中的 NamedAxisArray 为数组接口的每个维度或轴命名。

例子:

朱莉娅

# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using AxisArray function to return an array 
# with its dimension names and axis values
axis = AxisArray(arr, ["row1", "row2"], 
                      [:col1, :col2])
  
# Using NamedAxisArray function
# to return an array with named axes
axis_named = NamedAxisArray{(:xdim, :ydim)}(axis)

输出:

使用 MetaAxisArray()函数构造数组:

元数据也可以与数组接口链接。

例子:



朱莉娅

# Julia program to declare an array interface  
  
# Importing AxisIndices package
using AxisIndices
  
# Declare array
arr = [5 6; 7 8];
  
# Using MetaAxisArray function to return 
# an array with its dimension names, 
# axis values, metadata
axis_meta = MetaAxisArray(arr, (["row1", "row2"], 
                                [:col1, :col2]), 
                          metadata = "Array Interface")

输出:

数组索引

使用 Unitful 包在数组接口中建立索引:

数组接口可以像 Julia 中的标准数组一样被索引。这可以通过使用 Unitful 包来实现。

例子:

朱莉娅

# Julia program to perform indexing 
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function 
# to return an array with 
# its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return 
# an array with its dimension names 
# and axis values
axis = AxisArray(arr, ((1:2)m,
                ["col1", "col2", "col3"]))

输出:

例子:



朱莉娅

# Julia program to perform indexing 
# in an array interface 
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function
# to return an array
# with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function
# to return an array 
# with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# displaying only the first row 
# and all the columns
axis[1,:]

输出:

例子:

朱莉娅

# Julia program to perform indexing 
# in an array interface 
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return 
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return
# an array with its dimension names 
# and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# displaying the first two rows
# and the first two columns
axis[1:2, 1:2]

输出:

例子:

朱莉娅

# Julia program to perform indexing
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return 
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return
# an array with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# displaying the first 3 elements of the array
axis[1:3]

输出:



使用键在数组接口中建立索引:

数组接口可以在键的帮助下进行索引。

例子 :

朱莉娅

# Julia program to perform indexing 
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return
# an array with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# accessing an element using its specified keys
axis[1m, "col1"]

输出:

使用过滤函数在数组接口中建立索引:

数组接口也可以使用过滤键的函数来索引。

例子:

朱莉娅

# Julia program to perform indexing
# in an array interface
  
# Importing AxisIndices package
using AxisIndices
  
# Importing Unitful package
import Unitful: m
  
# Using reshape function to return 
# an array with its specified dimensions
arr = reshape(1:6, 2, 3);
  
# Using AxisArray function to return 
# an array with its dimension names and axis values
axis = AxisArray(arr, ((1:2)m, 
                ["col1", "col2", "col3"]))
  
# Applying filters to the keys
axis[!=(1m), in(["col1", "col2"])]

输出: