Using the C++ Math Library    

Overview of the MATLAB C++ Math Library

The MATLAB C++ Math Library consists of approximately 400 MATLAB math functions. It includes the built-in MATLAB math functions and many of the math functions that are implemented as MATLAB M-files. The MATLAB C++ Math Library is layered on top of the MATLAB C Math Library. The major value added by this C++ layer is ease of use.

The MATLAB C++ Math Library is firmly rooted in the traditions of the MATLAB runtime environment. Programming with the MATLAB C++ Math Library is very much like writing M-files in MATLAB. While the C++ language imposes several differences, the syntax used by the MATLAB C++ Math Library is very similar to the syntax of the MATLAB language. Like MATLAB, the MATLAB C++ Math Library provides automatic memory management, which protects the programmer from memory leaks. For a detailed comparison between MATLAB and the MATLAB C++ Math Library, see Chapter 2.

An important goal of this product is to provide a library that feels natural to both C++ programmers and MATLAB users. Achieving this goal is difficult, because there is some tension between the natural C++ programming style and the natural MATLAB style. Where it was necessary to choose between the MATLAB or C++ way of doing things, the MATLAB method usually prevailed.

The MATLAB C++ Math Library defines a set of classes and functions for the development of linear algebraic algorithms. The most important class in the MATLAB C++ Math Library is mwArray. This class corresponds to MATLAB's array data type. The mwArray class supports most MATLAB operators and all of the mathematical functions. The only operators it does not support are
\, ./, .\, .*, and .^, which are not syntactically valid in C++. These operations are accessed via function calls.

MATLAB is known in programming-language theory as a "functional" language: neither functions nor operators have side effects. The MATLAB C++ Math Library preserves the functional nature of MATLAB. With one exception (see Using Indexing in Assignment Statements in Chapter 4), expressions do not modify the arrays they contain and functions do not modify their inputs. The only way to change the value of an array is by assignment to one or more elements of the array.

The functions and operators provided by the library are vectorized. This means that they contain loops to iterate over the elements of their inputs. As a consequence, code written using this library should contain very few loops over array elements; most programs will have none.

The interface to the library is divided into three parts:

The bulk of the interface consists of the MATLAB math functions.

When using this library, you most often call the MATLAB mathematical functions, the operators, and the mwArray constructors. The public methods of mwArray are for the most part used internally by the library.

In general, the library code indicates that an error has occurred by raising an exception. Exception objects are subclasses of mwException, and thus all types of exceptions can be caught with a single catch statement. Each exception has an associated error message, which can be printed by placing the exception into an output stream, for example, via cout.

We highly recommend that you use C++ exception handling when using this library. If you do not, the first error that occurs will cause your program to terminate with a cryptic error message, such as Unhandled exception, abnormal program termination.


 Getting Started Who Should Read This Book