Octave支持多种不同的数组和矩阵类,其中大多数基于Array类。例外情况是下面单独讨论的稀疏矩阵类型。有三种基本矩阵类型:
MatrixdMatrix.h中定义的双精度矩阵类
ComplexMatrixCMatrix.h中定义的复数矩阵类
BoolMatrixboolMatrix.h中定义的布尔矩阵类
这些是Octave的基本二维矩阵类型。此外,还有许多多维数组类型,包括
NDArraydNDArray.h中定义的双精度数组类
ComplexNDarrayCNDArray.h中定义的复数数组类
boolNDArrayboolNDArray.h中定义的布尔数组类
int8NDArrayint16NDArrayint32NDArrayint64NDArrayint8NDArray.h、int16NDArray.h等中定义的8、16、32和64位有符号数组类
uint8NDArrayuint16NDArrayuint32NDArrayuint64NDArrayuint8NDArray.h、uint16NDArray.h等中定义的8、16、32和64位无符号数组类
构造矩阵或多维数组有几种基本方法。以类Matrix为例:
Matrix a;
这可用于所有矩阵和数组类型。
dim_vector定义矩阵或数组的维度,该dim_vector具有与size返回的向量相同的特性。例如:dim_vector dv (2, 3); // 2行,3列 Matrix a (dv);
这可用于所有矩阵和数组类型。
Matrix a (2, 2)
此构造函数只能用于矩阵类型。
这些类型都共享许多基本方法和运算符。其中许多与解释器中存在的函数相似。一系列有用的方法包括
T& operator () (octave_idx_type) ¶T& elem (octave_idx_type) ¶()运算符或elem方法允许读取或设置矩阵或数组的值。这些方法接受单个类型为octave_idx_type的参数,即矩阵或数组的索引。此外,矩阵类型还允许()运算符和elem方法的两个参数版本,用于给出要获取或设置的值的行索引和列索引。
请注意,这些函数会进行重要的错误检查,因此在某些情况下,用户可能更倾向于直接通过下面讨论的rwdata方法来访问数组或矩阵的数据。
octave_idx_type numel () const ¶矩阵或数组中元素的总数。
size_t byte_size () const ¶用于存储矩阵或数组的字节数。
dim_vector dims () const ¶以dim_vector类型值表示的矩阵或数组的维度。
int ndims () const ¶矩阵或数组的维数。矩阵总是二维的,但数组可以是N维的。
void resize (const dim_vector&) ¶void resize (nrows, ncols) ¶该方法接受一个类型为dim_vector的参数,或者对于矩阵,接受两个类型为octave_idx_type的参数,用于定义矩阵的行数和列数。
T * rwdata () ¶此方法返回一个指向矩阵或数组底层数据的指针,以便可以在Octave内部或通过外部库直接操作它。
运算符如+、-或*可用于大多数矩阵和数组类型。此外,还有许多仅对矩阵有意义的方法,例如transpose、hermitian、solve等。
从DEFUN_DLD函数的输入参数中提取矩阵或数组的典型方法如下:
#include <octave/oct.h>
DEFUN_DLD (addtwomatrices, args, , "Add A to B")
{
if (args.length () != 2)
print_usage ();
NDArray A = args(0).array_value ();
NDArray B = args(1).array_value ();
return octave_value (A + B);
}
为了避免段错误导致Octave中止,此函数在访问这些参数之前明确检查是否有足够的参数可用。然后它获取两个类型为NDArray的多维数组并将它们相加。请注意,这里调用array_value方法时没有使用is_matrix_type方法。如果在尝试提取值时发生错误,Octave将打印一条消息并抛出异常。推荐使用这种编码结构的原因是,参数可能是一种不是NDArray的类型,但将其转换为NDArray是有意义的。array_value方法允许在可能的情况下透明地执行此转换。如果您需要捕获这类错误并执行某种清理或其他操作,可以捕获octave_execution_error异常。
对两个NDArray对象进行A + B操作会返回一个NDArray,它在函数返回时被强制转换为octave_value。此演示函数的使用示例如下:
addtwomatrices (ones (2, 2), eye (2, 2))
⇒ 2 1
1 2
基本的Matrix和Array类型、从octave_value中提取这些类型的方法列表以及相关联的头文件如下所示。
| 类型 | 方法 | 源代码 |
|---|---|---|
RowVector |
row_vector_value |
dRowVector.h |
ComplexRowVector |
complex_row_vector_value |
CRowVector.h |
ColumnVector |
column_vector_value |
dColVector.h |
ComplexColumnVector |
complex_column_vector_value |
CColVector.h |
Matrix |
matrix_value |
dMatrix.h |
ComplexMatrix |
complex_matrix_value |
CMatrix.h |
boolMatrix |
bool_matrix_value |
boolMatrix.h |
charMatrix |
char_matrix_value |
chMatrix.h |
NDArray |
array_value |
dNDArray.h |
ComplexNDArray |
complex_array_value |
CNDArray.h |
boolNDArray |
bool_array_value |
boolNDArray.h |
charNDArray |
char_array_value |
charNDArray.h |
int8NDArray |
int8_array_value |
int8NDArray.h |
int16NDArray |
int16_array_value |
int16NDArray.h |
int32NDArray |
int32_array_value |
int32NDArray.h |
int64NDArray |
int64_array_value |
int64NDArray.h |
uint8NDArray |
uint8_array_value |
uint8NDArray.h |
uint16NDArray |
uint16_array_value |
uint16NDArray.h |
uint32NDArray |
uint32_array_value |
uint32NDArray.h |
uint64NDArray |
uint64_array_value |
uint64NDArray.h |
版权所有 © 2024-2026 Octave中文网
ICP备案/许可证号:黑ICP备2024030411号-2