/* ex3.cpp
 * Peter Webb, Aug. 1995
 * Example 3 from the documentation: Calling Library Routines
 */

#include <stdio.h>
#include "matlab.h"

static double data[] = { 1, 3, 5, 7, 2, 4, 6, 8 };

main()
{
    /* Create the input matrix */

    Matrix *X = mxCreateFull(4, 2, 0);
    Matrix *U, *S, *V, *W = mlfScalar(0.0);
    memcpy(mxGetPr(X), data, 8 * sizeof(double));

    /* Compute the singular value decomposition and print it. */

    mlfPrintf("One input, one output:\nU = \n");
    mlfPrintMatrix(U=mlfSvd(0, 0, X, 0));
    mxFreeMatrix(U);

    /* Pass in extra output arguments */

    U = mlfSvd(&S, &V, X, 0);

    mlfPrintf("One input, three outputs:\n");
    mlfPrintf("U = \n"); mlfPrintMatrix(U); mxFreeMatrix(U);
    mlfPrintf("S = \n"); mlfPrintMatrix(S); mxFreeMatrix(S);
    mlfPrintf("V = \n"); mlfPrintMatrix(V); mxFreeMatrix(V);

    /* Pass in extra input arguments */

    U = mlfSvd(&S, &V, X, W);

    mlfPrintf("Two inputs, three outputs:\n");
    mlfPrintf("U = \n"); mlfPrintMatrix(U); mxFreeMatrix(U);
    mlfPrintf("S = \n"); mlfPrintMatrix(S); mxFreeMatrix(S);
    mlfPrintf("V = \n"); mlfPrintMatrix(V); mxFreeMatrix(V);

    mxFreeMatrix(X);
    mxFreeMatrix(W);
    exit(0);
}

