#include #include #include "RMat.h" #if COMPILE_WITH_R #include #endif using namespace std; int main(int argc, char *argv[]) { //- Test element access (set value / get value). // RMat vMat1(2,3); int vCount = 0; for (int i=1; i<=2; i++) { for (int j=1; j<=3; j++) { vMat1(i,j) = ++vCount; } } vMat1.Print("vMat1 = \n"); cout << endl << "vMat1(2,2) = " << vMat1(2,2) << endl; //- Test special constructor for pre-allocated memory. // We'll use this when passing data from R to C++. // vMat2 should be a 2 by 3 matrix containing values between 3 and 8. // double *vValues; vValues = new double [6]; for (int i=0; i<6; i++) vValues[i] = i+3; RMat vMat2(vValues,2,3); vMat2.Print("\nvMat2 = \n"); //- Test assignment. // RMat vMat3; vMat3 = vMat1; vMat3.Print("\nvMat3 = \n"); //- Test copy constructor. // RMat vMat4(vMat3); vMat4.Print("\nvMat4 = \n"); //- Test transpose. // RMat vMat5 = vMat4.Transpose(); vMat5.Print("\nvMat5 = \n"); //- Test matrix multiplication. // RMat vMat6 = vMat1 * vMat5; vMat6.Print("\nvMat6 = \n"); //- Test setting all matrix elements to a scalar value. vMat6 = 0.0; vMat6.Print("\nNow, vMat6 = \n"); //- Note that we must manually deallocate memory // with the RMat class, since its destructor doesn't // do this. This is in anticipation of using the RMat // class with R. // vMat1.DeallocateMemory(); vMat2.DeallocateMemory(); vMat3.DeallocateMemory(); vMat4.DeallocateMemory(); vMat5.DeallocateMemory(); vMat6.DeallocateMemory(); #if COMPILE_WITH_R printf("\nUsing R math library to compute R_pow(2,3) = %g\n",R_pow(2,3)); #endif system("PAUSE"); return EXIT_SUCCESS; return 0; }