c++blitz++

changing the output format in blitz


I use blitz library regularly in c++. It has quite a few nice facilities like to print a 2-dimensional array one just needs to

cout<<A<<endl;

However, the output comes out like (for 2x2 arrays)

2 x 2
[ 4  5 
  2  1]

Now, I wish to get rid of the 2x2 dimension and the brackets it places, because it sometimes creates problem when I wish to directly use the file to plot something. How do I do this?


Solution

  • You need to write your own output routine.

    Probably the simplest way to do that is to create a proxy class template:

    template <typename blitzarray>
    struct printer
    {
        printer (const blitzarray& ba_) : ba(ba_) {}
        const blitzarray& ba;
    };
    

    which outputs the array it has just the way you like:

    template <typename blitzarray, typename stream>
    stream& operator<< (stream& s, printer<blitzarray> pb)
    {
        // <-- print the array here <--
        return s;
    }
    

    and then have a little helper function that helps deducing the template argument:

    template <typename blitzarray>
    printer<blitzarray> myprint(const blitzarray& ba)
    {
        return printer<blitzarray>(ba);
    }
    

    The use is just:

    cout << myprint(A) << endl;
    

    You probably can borrow from the original blitz++ output routine.