Using the C++ Math Library    

Example Program: Calling Library Functions (ex2.cpp)

This example demonstrates how to call different versions of the same library function and how to pass optional input and output arguments. The example uses the svd() function.

You can find the code for this example in the
<matlab>/extern/examples/cppmath directory on UNIX systems and in the <matlab>\extern\examples\cppmath directory on PCs, where <matlab> represents the top-level directory of your installation. See Building C++ Applicationsfor information about building and running the example program.

In MATLAB, there is one svd() function that you can call with varying numbers of input and output arguments. The MATLAB version of svd() counts the number of arguments passed to it and performs a different calculation for each valid combination of input and output arguments.

An ordinary C++ function cannot count its arguments. It always requires the same number of inputs and outputs each time you call it. However, you can declare multiple C++ functions with the same name as long as the argument lists for the functions are different. This is called overloading a function. Argument lists differ if they contain different numbers of arguments or if the types of the arguments are different.

There are three ways to call the svd() function in MATLAB. Therefore there are three overloaded svd() functions in C++, each corresponding to one of the ways you can call svd() in MATLAB. This example demonstrates how to call each of the overloaded svd() functions.

Refer to the online MATLAB C++ Math Library Reference for an explanation of svd(). Accessing Online Reference Documentation describes how to access the Help Desk.

In this example, note the following:

The numbered items in the list below correspond to the numbered comments in the code example:

  1. Include header files. matlab.hpp declares the MATLAB C++ Math Library's data types and functions. matlab.hpp includes iostream.h, which declares the input and output streams cin and cout.  stdlib.h contains the definition of EXIT_SUCCESS.
  2. Declare the static C++ array that initializes the svd() input matrix in the main() routine. The MATLAB C++ Math Library requires that the numbers in this array be in column-major order. See Example Program: Creating Arrays and Array I/O (ex1.cpp) in Chapter 3 for more information.
  3. Call a C++ constructor to declare and initialize the matrix X to a four-row, two-column matrix. In your code, use C++ constructors whenever possible, as they are more efficient than a declaration followed by an assignment.
  4. Call the simplest of the three svd() functions. This function takes one input matrix and produces one output matrix. Because C++ allows multiple functions with the same name to co-exist as long as their argument lists are different (this is called overloading a function), calling the simplest form of svd() does not require passing in extra NULL arguments as it would in the MATLAB C Math Library.
  5. Call the second svd() function. This function takes one input and produces three outputs. Because C++, unlike MATLAB, does not allow multiple return values, you pass the extra outputs into the svd() function as output arguments. Output arguments are modified by the function to contain the appropriate results.

    Just as input arguments are uniformly passed as const mwArray references, output arguments are always passed as pointers to mwArray objects. In C++, applying the & (address-of) operator to an object produces a pointer to that object.

  1. Call the third svd() function, which takes two inputs and produces three outputs. The second input is an optional argument. Note again the convenience of C++ function overloading. Without overloading, this optional input argument would have appeared as a NULL value in the argument lists of the other calls to svd().

Output

The program produces the following output. See Using Array Stream I/O in Chapter 8 for details on the array input and output format.


 Summary of Library Calling Conventions How to Call Operators