
bzsubsect(Output formatting)

bzindex(persistence)
bzindex(Array!persistence)
bzindex(output formatting)
bzindex(Array!output formatting)
bzindex(saving arrays)
bzindex(writing arrays to output streams)
bzindex(Array!saving to output stream)
bzindex(Array!writing to output stream)

The current version of Blitz++ includes rudimentary output formatting
for arrays.  Here's an example:

bzexample(\
#include <blitz/array.h>

using namespace blitz;

int main()
{
    Array<int,2> A(4,5,FortranArray<2>());
    firstIndex i;
    secondIndex j;
    A = 10*i + j;

    cout << "A = " << A << endl;

    Array<float,1> B(20);
    B = exp(-i/100.);
    
    cout << "B = " << endl << B << endl;

    return 0;
})

And the output:

bzexample(\
A = 4 x 5
[       11        12        13        14        15
        21        22        23        24        25
        31        32        33        34        35
        41        42        43        44        45 ]

B = 20
[          1   0.99005  0.980199  0.970446  0.960789  0.951229  0.941765
    0.932394  0.923116  0.913931  0.904837  0.895834   0.88692  0.878095
    0.869358  0.860708  0.852144  0.843665   0.83527  0.826959 ]
)

bzsubsect(Inputting arrays)

bzindex(inputting arrays from an input stream)
bzindex(Array!inputting from istream)
bzindex(restoring arrays from an input stream)
bzindex(Array!restoring from istream)

Arrays may be restored from an istream using the tt(>>) operator.
bf(Note:) you must know the dimensionality of the array being
restored from the stream.  The tt(>>) operator expects an
array in the same input format as generated by the tt(<<) operator,
namely:

bzindex(Array!persistence format)

startit()
it() The size of the array, for example "32" for a 1-dimensional
array of 32 elements, "12 x 64 x 128" for a 3-dimensional array
of size 12x64x128.

it() The symbol tt('[') indicating the start of the array data

it() The array elements, listed in memory storage order

it() The symbol tt(']') indicating the end of the array data

endit()

The operator prototype is:

verb(
template<class T, int N>
istream& operator>>(istream&, Array<T,N>&);
)

Here is an example of saving and restoring arrays from files.
You can find this example in the Blitz++ distribution as
tt(examples/io.cpp).

bzexample(
#include <blitz/array.h>
#include <fstream.h>

BZ_USING_NAMESPACE(blitz)

const char* filename = "io.data";

void write_arrays()
{
    ofstream ofs(filename);
    if (ofs.bad())
    {
        cerr << "Unable to write to file: " << filename << endl;
        exit(1);
    }

    Array<float,3> A(3,4,5);
    A = 111 + tensor::i + 10 * tensor::j + 100 * tensor::k;
    ofs << A << endl;

    Array<float,2> B(3,4);
    B = 11 + tensor::i + 10 * tensor::j;
    ofs << B << endl;

    Array<float,1> C(4);
    C = 1 + tensor::i;
    ofs << C << endl;
}

int main()
{
    write_arrays();

    ifstream ifs(filename);
    if (ifs.bad())
    {
        cerr << "Unable to open file: " << filename << endl;
        exit(1);
    }

    Array<float,3> A;
    Array<float,2> B;
    Array<float,1> C;

    ifs >> A >> B >> C;

    cout << "Arrays restored from file: " << A << B << C << endl;

    return 0;
}
)


bf(Note:) The storage order and starting indices are not restored
from the input stream.  If you are restoring (for example) a
Fortran-style array, you must create a Fortran-style array, and
then restore it.  For example, this code restores a Fortran-style
array from the standard input stream:

verb(
    Array<float,2> B(fortranArray);
    cin >> B;
)

