
A2.5 Ox exported mathematics functions 41
printf("error: allocation failed!");
MatZero(m, 3, 3); /* set m to 0 */
MatZero(&v, 1, 3); /* set v to 0 */
for (i = 0; i < 3; ++i) /* set both to 1 */
{ for (j = 0; j < 3; ++j)
m[i][j] = 1;
v[i] = 1;
}
/* ... do more work */
MatFree(m2, 3, 3); /* done: free memory */
free(v);
Note that the memory of a matrix is owned by the original matrix. It is NOT safe to
exchange rowsby swapping pointers. Rows also cannot be exchangedbetween different
matrices; instead the elements must be copied from one row to the other. Columns have
to be done element by element as well.
As a final example, we show how to define a matrix which points to part of another
matrix. For example, to set up a matrix which points to the 2 by 2 lower right block in
m, allocate the pointers to rows:
MATRIX m2 = MatAlloc(2, 0);
m2[0] = &m[1][1];
m2[1] = &m[2][1];
// do work with m and m2, then free m2:
MatFree(m2, 2, 0);
Again note that the memory of the elements is still owned by m; deallocating m
deletes what m2 tries to point to.
When a language supports C-style DLLs, but not the pointer-to-pointer model used
in the MATRIX type, the following functions may be used to provide the necessary map-
ping:
MatAllocBlock function version of MatAlloc
MatCopyVecc store column-vectorized matrix in a MATRIX
MatCopyVecr store row-vectorized matrix in a MATRIX
MatFreeBlock function version of MatFree
MatGetAt get an element in a MATRIX
MatSetAt set an element in a MATRIX
VeccCopyMat store a MATRIX as a column vector
VecrCopyMat store a MATRIX as a row vector
Komentarze do niniejszej Instrukcji