File: arrays-io.yo

package info (click to toggle)
blitz%2B%2B 1%3A1.0.1%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,016 kB
  • sloc: cpp: 56,889; python: 1,939; fortran: 1,510; f90: 852; makefile: 828; sh: 309
file content (156 lines) | stat: -rw-r--r-- 3,537 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

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;
)