XLL+ Class Library (6.3)

mtx_ptrs<T> Example

CopyC++
// Function:    MatrixAxisMean 
// Purpose:     Returns the mean of the values in one axis of a matrix 
 
//{{XLP_SRC(MatrixAxisMean) 
    // NOTE - the FunctionWizard will add and remove mapping code here. 
    //    DO NOT EDIT what you see in these blocks of generated code!
IMPLEMENT_XLLFN3(MatrixAxisMean, MatrixAxisMean_4, MatrixAxisMean_12, "RPI", 
    "UQI", L"MatrixAxisMean", 0, L"m,axis", 0, L"3", 0, L"Returns the mean of "
    L"the values in one axis of a matrix", 0, L"Square input matrix\0Axis - 0 "
    L"or 1\0", 0, 0, L"{MatrixAxisMean,,,Returns the mean of the values in one"
    L" axis of a matrix,3,1,128,U,{{0,{m,Double,2097162,,Square input matrix,,"
    L"{{{0,},{,}},{{0,},{,}}},,}},{0,{axis,Short Int,0,,Axis - 0 or 1,,,,}}},{"
    L"},3,,0,0}", 1)
CXlOper* MatrixAxisMean_Impl(CXlOper&, const CXlOper*, short int);

extern "C" __declspec(dllexport)
LPXLOPER12 MatrixAxisMean_12(LPXLOPER12 m, short int axis)
{
    XLL_FIX_STATE;
    CXlOper xloResult, m__port(m);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MatrixAxisMean_Impl(xloResult, &m__port, axis));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret12();
}
extern "C" __declspec(dllexport)
LPXLOPER4 MatrixAxisMean_4(LPXLOPER4 m, short int axis)
{
    XLL_FIX_STATE;
    CXlOper xloResult, m__port(m);
    try {
        CXlStructuredExceptionHandler _seh_;
        xloResult.HandleResult(MatrixAxisMean_Impl(xloResult, &m__port, axis));
    }
    catch(const CXlRuntimeException& ex) {
        CXllApp::Instance()->DisplayException(xloResult, ex);
    }
    return xloResult.Ret4();
}

CXlOper* MatrixAxisMean_Impl(CXlOper& xloResult, const CXlOper* m, short int 
    axis)
{
    // Input buffers
    ple::mtx_ptrs<double> matm;
    // Validate and translate inputs
    XlReadMatrix(*m, mtx_adapter(matm), L"m", XLA_TRUNC_ONEMPTY|
        XLA_TRUNC_ONBLANK|XLA_BOUND_UBOUND_AUTO, 0, 0, 0, 0);
    // End of generated code 
//}}XLP_SRC 
 
    // Check that the matrix is square & not empty 
    if (matm.size(0) != matm.size(1))
        return CXlOper::RetString("#Error: Expected a square matrix for m");
    if (matm.size(0) == 0)
        return CXlOper::RetString("#Error: Expected a non-empty square matrix for m");

    // Traverse the matrix 
    int i, n = matm.size(0);
    double dSum = 0.0;
    for (i = 0; i < n; i++)
        dSum += (axis == 0) ? matm[i][i] : matm[i][n - (1 + i)];
    dSum /= (double)n;

    // Return the result
    xloResult = dSum;
    return xloResult.Ret();
}

Uses

mtx_ptrs<T> | mtx_ptrs<T>::size | mtx_ptrs<T>::operator [] const