Ne.app NeANT
The Ne.app NeANT User module of the NeANT distribution.
gslice.cpp
Go to the documentation of this file.
1
29#include <iostream> // fo cout, endl
30#include <valarray.h> // Includes valarray and provides stream inserter.
31#include <examples.h> // Generic header for all examples.
32
33typedef std::valarray<int> valarray_t;
34typedef std::valarray<std::size_t> selector_t;
35
36int main(void) {
37
38 // Create a valarray of ints.
39 valarray_t::value_type ibuf[27] = { 0, 1, 2, 3, 4, 5, 6, 7, 8,
40 9, 10, 11, 12, 13, 14, 15, 16, 17,
41 18, 19, 20, 21, 22, 23, 24, 25, 26 };
42 valarray_t vi(ibuf, 27);
43
44 // Create length and stride valarray
45 selector_t::value_type length_buf[3] = {3,3,3};
46 selector_t::value_type stride_buf[3] = {9,3,1};
47 selector_t length_val(length_buf, 3);
48 selector_t stride_val(stride_buf, 3);
49
50 // Print out the valarray<int>.
51 std::cout << "original valarray vi\n\n" << vi << "\n\n";
52
53 // Print out all three dimensions (the entire valarray).
54 std::cout << "vi[gslice(0,{3,3,3},{9,3,1})]\n\n"
55 << vi[std::gslice(0,length_val,stride_val)] << "\n\n";
56
57 // Print a two dimensional slice out of the middle.
58 selector_t::value_type length_buf2[] = {3, 3};
59 selector_t::value_type stride_buf2[] = {3, 1};
60 selector_t length_val2(length_buf2, 2);
61 selector_t stride_val2(stride_buf2, 2);
62
63 std::cout << "vi[gslice(9,{3,3},{3,1})]\n\n"
64 << vi[std::gslice(9,length_val2,stride_val2)] << "\n\n";
65
66 // Print another two dimensional slice out of the middle but
67 // orthoganal to one we just did.
68 stride_val2[0] = 9;
69 stride_val2[1] = 1;
70
71 std::cout << "vi[gslice(3,{3,3},{9,1})]\n\n"
72 << vi[std::gslice(3,length_val2,stride_val2)] << "\n\n";
73
74 // Print out the last plane in the middle -- orthoganal to both
75 // of the previous ones.
76 stride_val2[0] = 3;
77 stride_val2[1] = 9;
78
79 std::cout << "vi[gslice(1,{3,3},{3,9})]\n\n"
80 << vi[std::gslice(1,length_val2,stride_val2)] << "\n\n";
81
82 // Now how about a diagonal slice, upper left front to lower right
83 // back.
84 stride_val2[0] = 3;
85 stride_val2[1] = 10;
86
87 std::cout << "vi[gslice(0,{3,3},{3,10})]\n\n"
88 << vi[std::gslice(0,length_val2,stride_val2)] << "\n\n";
89
90 return 0;
91}
std::valarray< std::size_t > selector_t
Definition gslice.cpp:34
int main(void)
Definition gslice.cpp:36
std::valarray< int > valarray_t
Definition gslice.cpp:33