#include <slate.h>
//
// External function which takes an array as a pointer to a pointer, and
// adds 0.1 to each element. The function assumes all element indices
// begin at 0.
//
void tweak(double **mat, int nrows, int ncols) {
for (int i=0; i<nrows; i++)
for (int j=0; j<ncols; j++)
mat[i][j] += 0.1;
}
//
// Similar external function, but it takes a pointer to a contiguous block
// of values.
//
void tweak2(double *mat, int nrows, int ncols) {
for (int i=0; i<nrows*ncols; i++)
mat[i] += 0.1;
}
//
// Similar to tweak2(), but assumes a vector of data is input.
//
void tweak3(double *vec, int nelems) {
for (int i=0; i<nelems; i++)
vec[i] += 0.1;
}
//
// Generates a unit NxN matrix, in a pointer to data form.
//
void unit(double *u, int n) {
for (int i=0; i<n; i++)
for (int j=0; j<n; j++) {
if (i==j) u[i*n+j] = 1.0;
if (i!=j) u[i*n+j] = 0.0;
}
}
int main() {
//
// Start with a slate++ matrix. We're interfacing with external functions
// that expect indices starting at 0, so we need to specify slate Matrices
// with corresponding index bounds (i.e., all starting at 0).
//
Matrix<double> a(0,2,0,2,3.4);
cout << a << endl;
//
// Send it to the first function. The raw() method points directly to the
// pointer-to-pointer inside the Matrix, where data is stored. Anything
// done by tweak() to the data will be permanent.
//
tweak(a.raw(), a.row_size(), a.col_size());
cout << a << endl;
//
// Send it to the second function. There is nothing particularly different
// here, except that we must dereference the pointer-to-pointer once
// because tweak2() accepts a pointer to the data.
//
tweak2(*a.raw(), a.row_size(), a.col_size());
cout << a << endl;
//
// For Vectors, routines will likely accept a pointer to the data,
// which again is exposed through raw().
//
Vector<double> b(0,4,0.4);
cout << b << endl;
tweak3(b.raw(), b.size());
cout << b << endl;
//
// Recall that native C data can be copied into slate Vectors and Matrices
// either by reference ('=') or value ('copy()'). For example, we
// can use the external function unit() to generate a unit slate Matrix.
//
Matrix<double> m(1,4,1,4);
double *u = new double[16];
unit(u,4);
m = u;
cout << m;
}