In order to handle the dimensions of a matrix in a coherent manner a class Dim is defined. Objects of this class are used when constructing matrices and enquiring about their rank and index sizes.
Dim(i1, i2, i3...in)
#include "Qdos.h" Dim dVec(3); Dim dRotMatx(3,3); // sets up the dimension for a 3-vector and a 3x3 rotn matrix
size())
n_elems = dRotMatx.size();
rank())
rank = dRotMatx.rank();
[]
#include "Qdos.h"
Dim dRotMatx(3,3);
iSize = dRotMatx[0];
jSize = dRotMatx[1];
for(i=0; i<iSize; i++)
printf("\n", i, j);
for (j=0; j<jSize; j++)
printf("%d,%d ", i, j);
The above example prints
QdRMatrix(const Dim &dim)
#include "Qdos.h" QdRMatrix_var Rmatx = new QdRMatrix (const Dim(3,3) );This example creates a var pointer to a 3x3 matrix (of doubles).
QdRMatrix( Dim &dim, double value)
#include "Qdos.h" QdRMatrix_var Rmatx = new QdRMatrix ( Dim(3,3), 0.0 );This example creates a var pointer to a 3x3 matrix and initialises all elements to the value 0.
[i1]...[in]
#include "Qdos.h"
QdRMatrix_var mat = new QdRMatrix(Dim(10,5));
QdRMatrix_var dif = new QdRMatrix(Dim(10));
for(i=0; i<10; i++)
(*dif)[i] = (*mat)[i][0] - (*mat)[i][4];
Note we dereference the QdRMatrix_var pointer to the QdRMatrix using
(*matrix) before accessing the elements using [n]. This example finds the
difference between the last and first colums in a 10 x 5 matrix.
dimdata()
#include "Qdos.h"
QdRMatrix_var mat = QdRMatrix_var::narrow(obj);
// handle 2D matrix iSize x kSize
Dim mDim = mat->dimdata();
if ( !mat.is_nil() && mDim.rank() != 2 ) {
// handle 2D array
}
else{
QplugAppendTextDisplay("Input object not a 2D Matrix\n");
return QPLUG_FAILURE;
}
dims()
#include "Qdos.h"
QdRMatrix_var mat = QdRMatrix_var::narrow(obj);
// handle 2D matrix iSize x kSize
if ( !mat.is_nil() && (mat->dimdata()).rank() != 2 ) {
iSize = (mat->dims())[0];
kSize = (mat->dims())[1];
double sumk[iSize];
for(i=0; i<iSize; i++){
sumk[i] = 0;
for(k=0; k<kSize; k++)
sumk[i] += mat[i][k];
}
}
else{
QplugAppendTextDisplay("Input object not a 2D Matrix\n");
return QPLUG_FAILURE;
}
The above example finds the sizes of the two indices of a 2D
matrix and them sums over the second index.
rank()
if ( !mat.is_nil() && (mat->dimdata()).rank() != 2 ){ if ( !mat.is_nil() && mat->rank() != 2 ){
QdRMatrixSeq()
#include "Qdos.h" QdRMatrixSeq_var out_seq = new QdRMatrixSeq();The above example creates a new empty matrix sequence.
QdRMatrixSeq(int size, const Dim & dim)
#include "Qdos.h" QdRMatrixSeq_var in_seq = QdRMatrixSeq_var::narrow(dobj); int sz = in_seq->sequence_size(); QdRMatrixSeq_var out_seq = new QdRMatrixSeq(sz, Dim(3));The above example creates a new sequence with the same number of entries as in_seq, but with dimension Dim(3).
[]
#include "Qdos.h"
QdRMatrixSeq_var in_seq = QdRMatrixSeq_var::narrow(dobj);
int sz = in_seq->sequence_size();
QdRMatrixSeq_var out_seq = new QdRMatrixSeq();
for (int i=0; i<sz; i++)
if( UserTest(in_seq[i]) ) out_seq->push_back(in_seq[i]);
The above example creates a new empty sequence and appends each
QdRMatrix object from the input QdRMatrixSeq sequence that
satisfies the user created function UserTest.
The first [i] dereference on a QdRMatrixSeq_var accesses the
QdRMatrix at index i, but subsequent sets of braces will extract the
corresponding element inside the matrix, e.g.
#include "Qdos.h"
QdRMatrixSeq_var in_seq = QdRMatrixSeq_var::narrow(dobj);
if( (in_seq[r]).rank() == 2){
double val_ij = in_seq[r][i][j];
}
The above example sets val_ij equal to the value of the i,j element
in the 2D array for the rNote that QdRMatrixSeq_var objects share the same generic methods as other QdObject_var objects and sequences, such as Factory::clone() and sequence_size(), etc.