Multi-dimensional Integers, Variables, and Expressions
Defining multi-dimensional variables
QUBO++ supports multi-dimensional variables (or qbpp::Var objects) and multi-dimensional integer variables (or qbpp::VarInt objects) of arbitrary depth using the functions qbpp::var() and qbpp::var_int(), respectively. Their basic usage is as follows:
qbpp::var("name",s1,s2,...,sd): Creates an array ofqbpp::Varobjects with the givennameand shape $s1\times s2\times \cdots\times sd$.l <= qbpp::var_int("name",s1,s2,...,sd) <= u: Creates an array ofqbpp::VarIntobjects with the specified range and shape $s1\times s2\times \cdots\times sd$.
The following QUBO++ program creates a binary variable and an integer variable, each with dimension $2\times 3\times 4$.
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3, 4);
auto y = 1 <= qbpp::var_int("y", 2, 3, 4) <= 8;
std::cout << "x : " << x << std::endl;
std::cout << "y : " << y << std::endl;
}
This program outputs the following:
x : {{{x[0][0][0],x[0][0][1],x[0][0][2],x[0][0][3]},{x[0][1][0],x[0][1][1],x[0][1][2],x[0][1][3]},{x[0][2][0],x[0][2][1],x[0][2][2],x[0][2][3]}},{{x[1][0][0],x[1][0][1],x[1][0][2],x[1][0][3]},{x[1][1][0],x[1][1][1],x[1][1][2],x[1][1][3]},{x[1][2][0],x[1][2][1],x[1][2][2],x[1][2][3]}}}
y : {{{1 +y[0][0][0][0] +2*y[0][0][0][1] +4*y[0][0][0][2],1 +y[0][0][1][0] +2*y[0][0][1][1] +4*y[0][0][1][2],1 +y[0][0][2][0] +2*y[0][0][2][1] +4*y[0][0][2][2],1 +y[0][0][3][0] +2*y[0][0][3][1] +4*y[0][0][3][2]},{1 +y[0][1][0][0] +2*y[0][1][0][1] +4*y[0][1][0][2],1 +y[0][1][1][0] +2*y[0][1][1][1] +4*y[0][1][1][2],1 +y[0][1][2][0] +2*y[0][1][2][1] +4*y[0][1][2][2],1 +y[0][1][3][0] +2*y[0][1][3][1] +4*y[0][1][3][2]},{1 +y[0][2][0][0] +2*y[0][2][0][1] +4*y[0][2][0][2],1 +y[0][2][1][0] +2*y[0][2][1][1] +4*y[0][2][1][2],1 +y[0][2][2][0] +2*y[0][2][2][1] +4*y[0][2][2][2],1 +y[0][2][3][0] +2*y[0][2][3][1] +4*y[0][2][3][2]}},{{1 +y[1][0][0][0] +2*y[1][0][0][1] +4*y[1][0][0][2],1 +y[1][0][1][0] +2*y[1][0][1][1] +4*y[1][0][1][2],1 +y[1][0][2][0] +2*y[1][0][2][1] +4*y[1][0][2][2],1 +y[1][0][3][0] +2*y[1][0][3][1] +4*y[1][0][3][2]},{1 +y[1][1][0][0] +2*y[1][1][0][1] +4*y[1][1][0][2],1 +y[1][1][1][0] +2*y[1][1][1][1] +4*y[1][1][1][2],1 +y[1][1][2][0] +2*y[1][1][2][1] +4*y[1][1][2][2],1 +y[1][1][3][0] +2*y[1][1][3][1] +4*y[1][1][3][2]},{1 +y[1][2][0][0] +2*y[1][2][0][1] +4*y[1][2][0][2],1 +y[1][2][1][0] +2*y[1][2][1][1] +4*y[1][2][1][2],1 +y[1][2][2][0] +2*y[1][2][2][1] +4*y[1][2][2][2],1 +y[1][2][3][0] +2*y[1][2][3][1] +4*y[1][2][3][2]}}}
Each qbpp::Var object in x can be accessed as x[i][j][k]. Each qbpp::VarInt object in y can be accessed as y[i][j][k], which is internally represented by three binary variables:
y[i][j][k][0]y[i][j][k][1]y[i][j][k][2]
corresponding to the binary encoding of integers in the specified range.
Creating integer variable arrays with individual ranges
When defining a multi-dimensional array of integer variables, all elements created by l <= qbpp::var_int("name", s1, s2, ...) <= u share the same range $[l, u]$. In many practical problems, however, each element may need a different range.
In such cases, you can first create a placeholder array using qbpp::var_int("name", s1, s2, ...) == 0, and then assign individual ranges to each element in a loop:
#include <qbpp/qbpp.hpp>
int main() {
auto max_vals = qbpp::int_array({3, 7, 15, 5});
auto x = qbpp::var_int("x", max_vals.size()) == 0;
for (size_t i = 0; i < max_vals.size(); ++i) {
x[i] = 0 <= qbpp::var_int() <= max_vals[i];
}
for (size_t i = 0; i < max_vals.size(); ++i) {
std::cout << "x[" << i << "] = " << x[i] << std::endl;
}
}
In this program, qbpp::var_int("x", 4) == 0 creates an array of 4 constant-zero VarInt objects as placeholders. Each element is then reassigned with its own range using 0 <= qbpp::var_int() <= max_vals[i].
This technique is commonly used in problems such as the Cutting Stock Problem, where the upper bound of each variable differs.
NOTE The
== 0syntax creates aVarIntwithmin_val = max_val = 0(i.e., a constant zero). It does not create an equality constraint. Any integer constant can be used, e.g.,qbpp::var_int("x", 4) == 5creates constant-five placeholders.
Defining multi-dimensional expressions
QUBO++ allows you to define multi-dimensional expressions (or qbpp::Expr objects) with arbitrary depth using the function qbpp::expr() as follows:
qbpp::expr(s1,s2,...,sd): Creates a multi-dimensional array ofqbpp::Exprobjects with shape $s1\times s2\times \cdots\times sd$.
The following program defines a 3-dimensional array x of qbpp::Var objects with shape $2\times 3\times 4$ and a 2-dimensional array f of size $2\times 3$. Then, using a triple for-loop, each f[i][j] accumulates the sum of x[i][j][0], x[i][j][1], x[i][j][2], and x[i][j][3]:
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3, 4);
auto f = qbpp::expr(2, 3);
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t k = 0; k < 4; ++k) {
f[i][j] += x[i][j][k];
}
}
}
f.simplify_as_binary();
std::cout << "f = " << f << std::endl;
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
std::cout << "f[" << i << "][" << j << "] = " << f[i][j] << std::endl;
}
}
}
Note that the simplify_as_binary() member function can be applied to a multi-dimensional array of qbpp::Expr objects. When called on such an array, it applies simplify_as_binary() to each element individually (element-wise).
This program produces the following output:
f = {{x[0][0][0] +x[0][0][1] +x[0][0][2] +x[0][0][3],x[0][1][0] +x[0][1][1] +x[0][1][2] +x[0][1][3],x[0][2][0] +x[0][2][1] +x[0][2][2] +x[0][2][3]},{x[1][0][0] +x[1][0][1] +x[1][0][2] +x[1][0][3],x[1][1][0] +x[1][1][1] +x[1][1][2] +x[1][1][3],x[1][2][0] +x[1][2][1] +x[1][2][2] +x[1][2][3]}}
f[0][0] = x[0][0][0] +x[0][0][1] +x[0][0][2] +x[0][0][3]
f[0][1] = x[0][1][0] +x[0][1][1] +x[0][1][2] +x[0][1][3]
f[0][2] = x[0][2][0] +x[0][2][1] +x[0][2][2] +x[0][2][3]
f[1][0] = x[1][0][0] +x[1][0][1] +x[1][0][2] +x[1][0][3]
f[1][1] = x[1][1][0] +x[1][1][1] +x[1][1][2] +x[1][1][3]
f[1][2] = x[1][2][0] +x[1][2][1] +x[1][2][2] +x[1][2][3]
Creating an array of expressions by auto type deduction
An array of qbpp::Expr objects can be created without explicitly calling qbpp::expr(). When a function call or an arithmetic operation yields an array-shaped result, an array of qbpp::Expr objects with the same shape can be defined using auto type deduction.
The following QUBO++ program creates an array f of qbpp::Expr objects with the same dimensions as an array x of qbpp::Var objects:
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3);
auto f = x + 1;
f += x - 2;
f.simplify_as_binary();
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
std::cout << "f[" << i << "][" << j << "] = " << f[i][j] << std::endl;
}
}
}
In this program, x is defined as a $2 \times 3$ array of qbpp::Var objects. The expression x + 1 produces a $2 \times 3$ array of qbpp::Expr objects, which is used to initialize f via auto type deduction. After that, the expression x - 2 is added element-wise to f.
This program outputs:
f[0][0] = -1 +2*x[0][0]
f[0][1] = -1 +2*x[0][1]
f[0][2] = -1 +2*x[0][2]
f[1][0] = -1 +2*x[1][0]
f[1][1] = -1 +2*x[1][1]
f[1][2] = -1 +2*x[1][2]
The qbpp::Array class
qbpp::Array is a multi-dimensional array class that supports arrays of any dimension, including 1D. It provides multi-dimensional indexing via operator[] chaining (e.g., x[i][j][k]) and element-wise arithmetic operations. The element type (Var, Expr, integer, etc.) is determined internally and does not need to be specified as a template parameter.
Arrays are created using the following factory functions:
qbpp::var("name", s1, s2, ...): Array of binary variables.qbpp::expr(s1, s2, ...): Array of zero-initialized expressions.qbpp::int_array({v1, v2, ...}): Array of integer constants.qbpp::int_array(s1, s2, ...): Zero-initialized integer array with given shape.
The qbpp::Array class provides the following member functions:
size(): Returns the size of the outermost dimension.total(): Returns the total number of elements.ndim(): Returns the number of dimensions.empty(): Returnstrueif the array has no elements.operator[]: Returns an element (1D) or a sub-array proxy (multi-dimensional).begin()/end(): Iterators for range-basedforloops.
In addition, qbpp::Array supports the following operators for element-wise operations:
+: Element-wise addition of two arrays, or an array and a scalar.-: Element-wise subtraction of two arrays, or an array and a scalar.*: Element-wise multiplication of two arrays, or an array and a scalar.- unary
-: Negates all elements in the array. - unary
~: Negates all variable literals in the array.
Multi-dimensional arrays are implemented as a single flat array with shape information. For example, x in the following code is a 2D array of variables with shape (2, 3):
auto x = qbpp::var("x", 2, 3);
The element-wise operations described above are supported for multi-dimensional arrays only when the operands have the same shape.
Multi-dimensional arrays can be accessed using index-based for loops with operator[]:
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3);
auto f = x + 1;
f += x - 2;
f.simplify_as_binary();
std::cout << "total = " << f.total() << std::endl;
std::cout << "ndim = " << f.ndim() << std::endl;
std::cout << "shape = (" << f.shape(0) << ", " << f.shape(1) << ")" << std::endl;
for (size_t i = 0; i < f.size(); ++i) {
for (size_t j = 0; j < f[i].size(); ++j) {
std::cout << "(" << f[i][j] << ")";
}
std::cout << std::endl;
}
}
Here, f.total() returns the total number of elements, f.ndim() the number of dimensions, and f.shape(d) returns the size of dimension d. f[i][j] accesses the element at row i, column j.
This program outputs:
(-1 +2*x[0][0])(-1 +2*x[0][1])(-1 +2*x[0][2])
(-1 +2*x[1][0])(-1 +2*x[1][1])(-1 +2*x[1][2])
Sub-array access and operations
To extract a sub-array (e.g., a row or column) from a multi-dimensional array, use the row(), col(), and slice() methods. For slicing along arbitrary axes and concatenation, see Slice and Concat Functions.
多次元の整数、変数および式
多次元変数の定義
QUBO++は、関数qbpp::var()およびqbpp::var_int()を使用して、任意の深さの多次元変数(qbpp::Varオブジェクト)および多次元整数変数(qbpp::VarIntオブジェクト)をサポートしています。 基本的な使い方は次のとおりです:
qbpp::var("name",s1,s2,...,sd): 指定されたnameと形状$s1\times s2\times \cdots\times sd$を持つqbpp::Varオブジェクトの配列を作成します。l <= qbpp::var_int("name",s1,s2,...,sd) <= u: 指定された範囲と形状$s1\times s2\times \cdots\times sd$を持つqbpp::VarIntオブジェクトの配列を作成します。
以下のQUBO++プログラムは、それぞれ次元$2\times 3\times 4$のバイナリ変数と整数変数を作成します。
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3, 4);
auto y = 1 <= qbpp::var_int("y", 2, 3, 4) <= 8;
std::cout << "x : " << x << std::endl;
std::cout << "y : " << y << std::endl;
}
このプログラムの出力は次のとおりです:
x : {{{x[0][0][0],x[0][0][1],x[0][0][2],x[0][0][3]},{x[0][1][0],x[0][1][1],x[0][1][2],x[0][1][3]},{x[0][2][0],x[0][2][1],x[0][2][2],x[0][2][3]}},{{x[1][0][0],x[1][0][1],x[1][0][2],x[1][0][3]},{x[1][1][0],x[1][1][1],x[1][1][2],x[1][1][3]},{x[1][2][0],x[1][2][1],x[1][2][2],x[1][2][3]}}}
y : {{{1 +y[0][0][0][0] +2*y[0][0][0][1] +4*y[0][0][0][2],1 +y[0][0][1][0] +2*y[0][0][1][1] +4*y[0][0][1][2],1 +y[0][0][2][0] +2*y[0][0][2][1] +4*y[0][0][2][2],1 +y[0][0][3][0] +2*y[0][0][3][1] +4*y[0][0][3][2]},{1 +y[0][1][0][0] +2*y[0][1][0][1] +4*y[0][1][0][2],1 +y[0][1][1][0] +2*y[0][1][1][1] +4*y[0][1][1][2],1 +y[0][1][2][0] +2*y[0][1][2][1] +4*y[0][1][2][2],1 +y[0][1][3][0] +2*y[0][1][3][1] +4*y[0][1][3][2]},{1 +y[0][2][0][0] +2*y[0][2][0][1] +4*y[0][2][0][2],1 +y[0][2][1][0] +2*y[0][2][1][1] +4*y[0][2][1][2],1 +y[0][2][2][0] +2*y[0][2][2][1] +4*y[0][2][2][2],1 +y[0][2][3][0] +2*y[0][2][3][1] +4*y[0][2][3][2]}},{{1 +y[1][0][0][0] +2*y[1][0][0][1] +4*y[1][0][0][2],1 +y[1][0][1][0] +2*y[1][0][1][1] +4*y[1][0][1][2],1 +y[1][0][2][0] +2*y[1][0][2][1] +4*y[1][0][2][2],1 +y[1][0][3][0] +2*y[1][0][3][1] +4*y[1][0][3][2]},{1 +y[1][1][0][0] +2*y[1][1][0][1] +4*y[1][1][0][2],1 +y[1][1][1][0] +2*y[1][1][1][1] +4*y[1][1][1][2],1 +y[1][1][2][0] +2*y[1][1][2][1] +4*y[1][1][2][2],1 +y[1][1][3][0] +2*y[1][1][3][1] +4*y[1][1][3][2]},{1 +y[1][2][0][0] +2*y[1][2][0][1] +4*y[1][2][0][2],1 +y[1][2][1][0] +2*y[1][2][1][1] +4*y[1][2][1][2],1 +y[1][2][2][0] +2*y[1][2][2][1] +4*y[1][2][2][2],1 +y[1][2][3][0] +2*y[1][2][3][1] +4*y[1][2][3][2]}}}
x内の各qbpp::Varオブジェクトはx[i][j][k]としてアクセスできます。 y内の各qbpp::VarIntオブジェクトはy[i][j][k]としてアクセスでき、 内部的には以下の3つのバイナリ変数で表現されます:
y[i][j][k][0]y[i][j][k][1]y[i][j][k][2]
これらは指定された範囲の整数のバイナリエンコーディングに対応しています。
個別の範囲を持つ整数変数配列の作成
多次元整数変数配列を定義する場合、l <= qbpp::var_int("name", s1, s2, ...) <= u で作成された全要素は同じ範囲 $[l, u]$ を共有します。 しかし実際の問題では、各要素に異なる範囲が必要な場合が多くあります。
このような場合、まず qbpp::var_int("name", s1, s2, ...) == 0 でプレースホルダ配列を作成し、ループ内で各要素に個別の範囲を割り当てます:
#include <qbpp/qbpp.hpp>
int main() {
auto max_vals = qbpp::int_array({3, 7, 15, 5});
auto x = qbpp::var_int("x", max_vals.size()) == 0;
for (size_t i = 0; i < max_vals.size(); ++i) {
x[i] = 0 <= qbpp::var_int() <= max_vals[i];
}
for (size_t i = 0; i < max_vals.size(); ++i) {
std::cout << "x[" << i << "] = " << x[i] << std::endl;
}
}
このプログラムでは、qbpp::var_int("x", 4) == 0 がプレースホルダとして定数ゼロの VarInt オブジェクト4個の配列を作成します。 各要素は 0 <= qbpp::var_int() <= max_vals[i] で個別の範囲に再代入されます。
このテクニックは、各変数の上限が異なる切り出し問題などでよく使われます。
NOTE
== 0の構文はmin_val = max_val = 0(定数ゼロ)のVarIntを作成するものであり、等号制約を作成するものではありません。 任意の整数定数を使用でき、例えばqbpp::var_int("x", 4) == 5は定数5のプレースホルダを作成します。
多次元式の定義
QUBO++では、関数qbpp::expr()を使用して任意の深さの多次元式(qbpp::Exprオブジェクト)を定義できます:
qbpp::expr(s1,s2,...,sd): 形状$s1\times s2\times \cdots\times sd$のqbpp::Exprオブジェクトの多次元配列を作成します。
以下のプログラムは、形状$2\times 3\times 4$のqbpp::Varオブジェクトの3次元配列xと、サイズ$2\times 3$の2次元配列fを定義します。 次に、3重forループを使用して、各f[i][j]にx[i][j][0]、x[i][j][1]、x[i][j][2]、x[i][j][3]の和を蓄積します:
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3, 4);
auto f = qbpp::expr(2, 3);
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
for (size_t k = 0; k < 4; ++k) {
f[i][j] += x[i][j][k];
}
}
}
f.simplify_as_binary();
std::cout << "f = " << f << std::endl;
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
std::cout << "f[" << i << "][" << j << "] = " << f[i][j] << std::endl;
}
}
}
simplify_as_binary()メンバ関数はqbpp::Exprオブジェクトの多次元配列に対しても適用できます。 このような配列に対して呼び出された場合、各要素に対して個別に(要素ごとに)simplify_as_binary()が適用されます。
このプログラムの出力は次のとおりです:
f = {{x[0][0][0] +x[0][0][1] +x[0][0][2] +x[0][0][3],x[0][1][0] +x[0][1][1] +x[0][1][2] +x[0][1][3],x[0][2][0] +x[0][2][1] +x[0][2][2] +x[0][2][3]},{x[1][0][0] +x[1][0][1] +x[1][0][2] +x[1][0][3],x[1][1][0] +x[1][1][1] +x[1][1][2] +x[1][1][3],x[1][2][0] +x[1][2][1] +x[1][2][2] +x[1][2][3]}}
f[0][0] = x[0][0][0] +x[0][0][1] +x[0][0][2] +x[0][0][3]
f[0][1] = x[0][1][0] +x[0][1][1] +x[0][1][2] +x[0][1][3]
f[0][2] = x[0][2][0] +x[0][2][1] +x[0][2][2] +x[0][2][3]
f[1][0] = x[1][0][0] +x[1][0][1] +x[1][0][2] +x[1][0][3]
f[1][1] = x[1][1][0] +x[1][1][1] +x[1][1][2] +x[1][1][3]
f[1][2] = x[1][2][0] +x[1][2][1] +x[1][2][2] +x[1][2][3]
auto型推論による式配列の作成
qbpp::Exprオブジェクトの配列は、qbpp::expr()を明示的に呼び出さずに作成できます。 関数呼び出しや算術演算が配列形状の結果を返す場合、auto型推論を使用して同じ形状のqbpp::Exprオブジェクトの配列を定義できます。
以下のQUBO++プログラムは、qbpp::Varオブジェクトの配列xと同じ次元のqbpp::Exprオブジェクトの配列fを作成します:
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3);
auto f = x + 1;
f += x - 2;
f.simplify_as_binary();
for (size_t i = 0; i < 2; ++i) {
for (size_t j = 0; j < 3; ++j) {
std::cout << "f[" << i << "][" << j << "] = " << f[i][j] << std::endl;
}
}
}
このプログラムでは、xは$2 \times 3$のqbpp::Varオブジェクトの配列として定義されています。 式x + 1は$2 \times 3$のqbpp::Exprオブジェクトの配列を生成し、auto型推論を通じてfの初期化に使用されます。 その後、式x - 2がfに要素ごとに加算されます。
このプログラムの出力は次のとおりです:
f[0][0] = -1 +2*x[0][0]
f[0][1] = -1 +2*x[0][1]
f[0][2] = -1 +2*x[0][2]
f[1][0] = -1 +2*x[1][0]
f[1][1] = -1 +2*x[1][1]
f[1][2] = -1 +2*x[1][2]
qbpp::Array クラス
qbpp::Array は、1次元を含む任意の次元の多次元配列をサポートするクラスです。 operator[] のチェーン(例: x[i][j][k])による多次元インデックスアクセスと、要素ごとの算術演算を提供します。 要素型(Var, Expr, 整数など)は内部で管理されるため、テンプレートパラメータとして指定する必要はありません。
配列は以下のファクトリ関数で作成します:
qbpp::var("name", s1, s2, ...): バイナリ変数の配列。qbpp::expr(s1, s2, ...): ゼロ初期化された式の配列。qbpp::int_array({v1, v2, ...}): 整数定数の配列。qbpp::int_array(s1, s2, ...): ゼロ初期化された整数配列。
qbpp::Array クラスは以下のメンバ関数を提供しています:
size(): 最外次元のサイズを返します。total(): 全要素数を返します。ndim(): 次元数を返します。empty(): 配列が空の場合trueを返します。operator[]: 要素(1次元)またはサブ配列プロキシ(多次元)を返します。begin()/end(): range-basedforループ用イテレータ。
さらに、qbpp::Array は要素ごとの演算のために以下の演算子をサポートしています:
+: 2つの配列、または配列とスカラーの要素ごとの加算。-: 2つの配列、または配列とスカラーの要素ごとの減算。*: 2つの配列、または配列とスカラーの要素ごとの乗算。- 単項
-: 配列内のすべての要素を否定します。 - 単項
~: 配列内のすべての変数リテラルを否定します。
多次元配列は、形状情報を持つ単一のフラット配列として実装されています。 例えば、以下のコードにおける x は shape (2, 3) の変数の2次元配列です:
auto x = qbpp::var("x", 2, 3);
上記の要素ごとの演算は、オペランドが同じ形状を持つ場合にのみ多次元配列でサポートされます。
多次元配列は operator[] を使ったインデックスベースの for ループでアクセスできます:
#include <qbpp/qbpp.hpp>
int main() {
auto x = qbpp::var("x", 2, 3);
auto f = x + 1;
f += x - 2;
f.simplify_as_binary();
std::cout << "total = " << f.total() << std::endl;
std::cout << "ndim = " << f.ndim() << std::endl;
std::cout << "shape = (" << f.shape(0) << ", " << f.shape(1) << ")" << std::endl;
for (size_t i = 0; i < f.size(); ++i) {
for (size_t j = 0; j < f[i].size(); ++j) {
std::cout << "(" << f[i][j] << ")";
}
std::cout << std::endl;
}
}
ここで、f.total()は全要素数、f.ndim()は次元数、f.shape(d)は次元dのサイズを返します。 f[i][j]は行i、列jの要素にアクセスします。
このプログラムの出力は次のとおりです:
(-1 +2*x[0][0])(-1 +2*x[0][1])(-1 +2*x[0][2])
(-1 +2*x[1][0])(-1 +2*x[1][1])(-1 +2*x[1][2])
サブ配列アクセスと操作
多次元配列から行や列などのサブ配列を取得するには、row()、col()、slice() メソッドを使います。 任意の軸に沿ったスライスや連結については スライス関数と連結関数 を参照してください。