Suggestion for simple matrix/vector output

10-25-2009, 06:25 AM
I would suggest to add an output algorithm for matrices and vectors to the file nr3.h.

const int os_precision_m = 7;
const int os_width_m = 5;

std::ostream& operator<<(std::ostream &os, const MatDoub &m){
os << std::setprecision(os_precision_m);
for (int i=0;i<m.nrows();i++){
for (int j=0;j<m.ncols();j++){
os << std::setw(os_width_m) << m[i][j];
std::cout << std::endl;
return os;

Than you can write a matrix to standard output with

#include "nr3.h"
int main(){
MatDoub a(2,2);

a[0][0] = 1; a[0][1] = -1;
a[1][0] = 0; a[1][1] = 2;

cout << "a" << endl << a << endl;

What do you think about that idea?

Bill Press
10-26-2009, 08:54 AM
Yes, good idea. We were intentionally trying to keep nr3.h and its classes as minimal as possible, so as to be syntactically compatible with the maximum number of other vector-matrix class libraries out there. However, my sense now is that most NR3 users are using nr3.h, not something else. I wonder if it would be useful, now, to have a superset "nr3a.h" with a richer set of capabilities. For example, it would also be easy to overload the various arithmetic operators and provide "c=a+b" (etc.) capabilities for vectors and matrices.

Users, please give your views on this!

Bill P.