矩阵类型
矩阵是一种数据类型,包含二维网格中的一到十六 标量 组件。 矩阵的每个组件必须具有相同的类型。
类型声明
可以使用矩阵内容的 标量类型 名称声明矩阵变量,其中包含的行数和列数:
TypeRowsCols Name
其中,Type
是每个组件的 标量类型,Rows
是一个常量整数表达式,介于 1 和 4 之间,表示行数,Cols
是一个常量整数表达式,指示列数,Name
是唯一标识变量名称的 ASCII 字符串。
例子:
int1x1 iMatrix; // integer matrix with 1 row, 1 column, 1 single component
int4x1 iMatrix; // integer matrix with 4 rows, 1 column, 4 total components
int1x4 iMatrix; // integer matrix with 1 row, 4 columns, 4 total components
double3x3 dMatrix; // double matrix with 3 rows, 3 columns, 9 total components
float3x2 fMatrix = { 0.0f, 0.1f, // row 1
2.1f, 2.2f, // row 2
4.1f, 4.2f // row 3
};
模板样式声明
备用声明语法使用 matrix
关键字和模板参数来指示标量类型、行数和列数:
matrix <Type=float, Rows=4, Cols=4> Name
其中 Type
是每个组件的 标量类型,Rows
是一个介于 1 和 4 之间的整数,表示行数,Cols
是一个介于 1 和 4 之间的整数,表示列数,但它们在模板样式尖括号中指定。
Name
是唯一标识变量名称的 ASCII 字符串。
请注意,模板参数默认值允许通过离开最后一个参数、从最后一个参数中离开给定类型的 4x4 矩阵或 4x4 浮点矩阵来指定给定类型的 4 列母体,或者通过关闭所有三个参数来指定 4 列浮点矩阵。
例子:
matrix <int, 1, 1> iMatrix = { 1 };
matrix <float, 2, 3> fMatrix = { 0.0f, 0.1f, 0.2f, // row 1
2.1f, 2.2f, 2.3f // row 2
};
matrix<int16_t, 1> sMatrix = { 1, 2, 3, 4 }; // Defaults to 1x4 int16 matrix
matrix<float16_t> hMatrix = { 0.0f, 0.1f, 0.2f, 0.3f, // Defaults to 4x4 float16 matrix
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
3.0f, 3.1f, 3.2f, 3.3f };
matrix fMatrix = { 0.0f, 0.1f, 0.2f, 0.3f, // Defaults to 4x4 float matrix
1.0f, 1.1f, 1.2f, 1.3f,
2.0f, 2.1f, 2.2f, 2.3f,
3.0f, 3.1f, 3.2f, 3.3f };