Newmat - FAQ - frequently asked questions

Last updated 29 October, 2015


Where can I find the latest version of newmat?

Look at my site .

Where can I find a version of newmat for Unix?

Newmat works with a variety of Unix systems including Linux. Download newmat10.tar.gz.

Where is the documentation for newmat? (Not really an FAQ but I sometimes think it should be!)

Newmat10 has only an html version of the documentation, which is also included with the program. Also see the bugs and notes page.

I have Microsoft Visual C++ 5 or 6. How do I use newmat with it? (This really is a genuine FAQ!)

Look at my page on Visual C++.

Is there a function for doing a vector cross-product?

This is included in the latest version of newmat11 beta.



ostream conflict in newmatio.h

You may need to replace (answer supplied by a user)

ostream& operator<<(ostream&, const BaseMatrix&);
ostream& operator<<(ostream&, const GeneralMatrix&);


std::ostream& operator<<(std::ostream&, const BaseMatrix&);
std::ostream& operator<<(std::ostream&, const GeneralMatrix&);

I get the following error message when compiling with G++.

newmat.h --> ISO C++ forbids declaration of 'nTypes' ... 

Upgrade to the latest version of newmat10 or newmat11.

I am using your makefile to compile newmat (or newran etc) with Linux and it won't recognise any of your files.

This can happen if you have downloaded the .zip version of the program rather than the .tar.gz since the cases of the filenames will be wrong. There may be some other problems as well. Download the .tar.gz version for use with a UNIX or Linux system or Cygwin under Windows.

I am using VC++ and getting compile errors like

d:\trynewmat\svd.cpp(20) : warning C4005: 'REPORT' : macro redefinition
   d:\trynewmat\svd.cpp(18) : see previous definition of 'REPORT'
d:\trynewmat\svd.cpp(228) : fatal error C1010: unexpected end of file
   while looking for precompiled header directive

Either turn off the pre-compiled headers option or put #include "stdafx.h" at the beginning of every .cpp file.

I am using VC++ with the MFC and getting lots of link errors

It is important to compile all your files with the same options. This applies particularly to the options about the MFC. The library file generated with my make file will probably not work with programs generated with the IDE.

I am using Borland Builder C++ 5. I get a compile error when I compile newmatio.h with an error message Multiple declaration for 'ostream'.

Turn off the pre-compiled headers option.

The following program won't compile

   ColumnVector cv(10); RowVector rv(10);
   .... put values in cv, rv ....
   Real r = rv * cv;

A 1x1 matrix isn't automatically converted to a Real. Use

Real r = (rv * cv).AsScalar();

I have compiled a simple program using newmat with Borland C++. The code file is 1.3 megabytes. Why is it so big?

Turn off debugging in Borland's options.

I get a warning in each of newmat4.cpp and newmat5.cpp about unreachable code.

You get these when you are using built-in exceptions. It is nothing to worry about.

I get a mass of compile errors when I try your test programs.

Check that you have edited include.h as described in the documentation. In particular the line "#define bool_LIB 0" must be active if and only if you compiler recognises bool.

Gnu G++ 2.4.5 gives compile errors - upgrade to a more recent version like 2.95.

I am building newmat using the IDE with Borland or Microsoft and I get a mass of link errors.

You haven't added all the source files to the project, or you haven't added the library file [if you have already compiled newmat to a library file] or you have got some object files left over from a previous version of the project.

If you are a new user to the Borland or Microsoft IDEs, find out how to setup your job as a project. You can't just open the file containing your main program in the file menu and expect it to find all the other source files.

Here is the list of source files for newmat (excluding the test and demonstration files)

Basic newmat

  • bandmat.cpp
  • myexcept.cpp
  • newmat1.cpp
  • newmat2.cpp
  • newmat3.cpp
  • newmat4.cpp
  • newmat5.cpp
  • newmat6.cpp
  • newmat7.cpp
  • newmat8.cpp
  • newmat9.cpp
  • newmatex.cpp
  • newmatrm.cpp
  • submat.cpp

Factorisations etc

  • cholesky.cpp
  • evalue.cpp
  • fft.cpp
  • hholder.cpp
  • jacobi.cpp
  • sort.cpp
  • svd.cpp


  • newfft.cpp
  • nm_misc.cpp

Nonlinear routines

  • newmatnl.cpp
  • solution.cpp

I am using newmat08 and have just upgraded to the latest version of my compiler. I get compile errors.

These probably arise from the changes in the C++ language. Upgrade to newmat10 or newmat11.



Newmat crashes with a very unhelpful message such as abnormal termination (or possibly no message).

If you are using compiler supported exceptions, make sure you catch any exception thrown by newmat and print the error message. See the section on catching exceptions in the latest version of the newmat documentation. If you are using simulated exceptions you should get the error message automatically if you are using a console version of the program. If you are using a GUI version follow the instructions on catching exceptions and replace the cout statements with an error message box.

I am loading a symmetric matrix as follows and getting wrong answers.

   SymmetricMatrix m2(3);
   m2[0][0] = 6; m2[0][1] = 5; m2[0][2] = 4;
                 m2[1][1] = 3; m2[1][2] = 2;
                               m2[2][2] = 1;

Symmetric matrices are stored as lower triangular matrices. Load the values as a lower triangular matrix. [But loading as an upper triangular matrix will work if you use the indices of the form m2(1,1).]

I am loading a symmetric matrix as follows and getting wrong answers.

   SymmetricMatrix A(2);
   Real a[] = {0.616556,0.61544,0.61544,0.716556};
   A << a;

Load only the values actually stored in the matrix:

     SymmetricMatrix A(2);
     Real a[] = {0.616556,0.61544,0.716556};
     A << a;

The following code gives a runtime error.

   Matrix A(10,10);
   .... put values in A ....
   Matrix B(10,10);
   B += A;

You must initialise B with B = 0.0; Newmat does not automatically initialise newly constructed matrices.

The following code fails at runtime. Is multiplication of symmetric matrices not implemented?

   SymmetricMatrix A, B;
   .... put values in A, B ....
   SymmetricMatrix C = A * B;

The product of symmetric matrices is not necessarily symmetric. Use

Matrix C = A * B;

or, if you know the product will be symmetric, use

SymmetricMatrix C; C << A * B;

I have compiled your test_exc program and I get a stream of error messages when I run it.

You are meant to. The test_exc program is testing the exception and error message system. Look at the file test_exc.txt to see the output you should be getting.

I have compiled newmat as a library file with Borland C++ version 5 and I get runtime crashes when I use this with my programs.

The options you compiled the library with are not compatible with those used in your program. I don't know a reliable way of avoiding this problem and I tend not to use library files with Borland.

I have got your message Cannot resolve memory linked list. See notes in myexcept.cpp for details.

You may have both the compiler supported exceptions and my simulated exceptions turned on. Edit my file include.h to use the compiler supported exceptions or change your compiler options to turn off the inbuilt exceptions. As a last resort, edit include.h to disable exceptions.

Your SVD routine gives a different answer from the SVD routine in Matlab.

The SVD decomposition is not completely unique. Columns in the U matrix can be swapped, if those in the V matrix and the elements in the diagonal matrix, D, are swapped in the same way. The signs of the elements in a column of U may be reversed if the signs in the corresponding column in V are reversed. If a number of the singular values are identical one can apply an orthogonal transformation to the corresponding columns of U and the corresponding columns of V. Note that older versions of my SVD do not sort the singular values.