std::gslice
ヘッダ <valarray> で定義 | ||
class gslice; | ||
std::gslice
はストライドとサイズの多段の集合によって定義される std::valarray のインデックスの部分集合を表すセレクタクラスです。 std::gslice
型のオブジェクトは、例えば valarray
として表された多次元配列のカラムを選択するために、 valarray の operator[]
でインデックスとして使用できます。
開始値 s、ストライドのリスト i
j およびサイズのリスト d
j が与えられたとき、これらの値から構築された std::gslice
はインデックスの集合 k
j=s+Σ
j(i
jd
j) を選択します。
例えば、開始インデックス 3
、ストライド {19,4,1
} および長さ {2,4,3}
は以下のようなインデックスの集合を生成します。
3+0*19+0*4+0*1=3,
3+0*19+0*4+1*1=4,
3+0*19+0*4+2*1=5,
3+0*19+1*4+0*1=7,
3+0*19+1*4+1*1=8,
...
3+1*19+3*4+2*1=36
同じインデックスを複数回選択する std::gslice
オブジェクトを構築することもできます。 上の例でストライド {1,1,1}
を使用した場合、インデックスは {3, 4, 5, 4, 5, 6, ...}
のようになります。 そのような gslice は std::valarray::operator[]
の const バージョンへの引数としてのみ使用することができ、そうでなければ動作は未定義です。
目次 |
[編集]メンバ関数
コンストラクタ | 一般スライスを構築します (パブリックメンバ関数) |
startsizestride | スライスのパラメータを返します (パブリックメンバ関数) |
std::gslice::gslice
gslice() | ||
gslice(std::size_t start, conststd::valarray<std::size_t>& sizes, conststd::valarray<std::size_t>& strides ); | ||
gslice(const gslice& other ); | ||
新しい一般スライスを構築します。
start
、 sizes
、 strides
を持つ新しいスライスを構築します。other
のコピーを構築します。引数
start | - | 最初の要素の位置 |
sizes | - | 各次元の要素数を定義する配列 |
strides | - | 各次元の連続する要素間の位置数を定義する配列 |
other | - | コピーする別のスライス |
std::slice::start, size, stride
std::size_t start()const; | (1) | |
std::valarray<std::size_t> size()const; | (2) | |
std::valarray<std::size_t> stride()const; | (3) | |
構築時にスライスに渡されたパラメータ start、 sizes、 strides をそれぞれ返します。
引数
(なし)
戻り値
スライスのパラメータ start、 sizes、 strides。
計算量
一定。
[編集]例
三次元配列のカラムを指定する gslice の使用方法をデモンストレーションします。
#include <iostream>#include <valarray>void test_print(std::valarray<int>& v, int rows, int cols, int planes){for(int r=0; r<rows;++r){for(int c=0; c<cols;++c){for(int z=0; z<planes;++z)std::cout<< v[r*cols*planes + c*planes + z]<<' ';std::cout<<'\n';}std::cout<<'\n';}}int main(){std::valarray<int> v =// 3d array: 2 x 4 x 3 elements{111,112,113 , 121,122,123 , 131,132,133 , 141,142,143, 211,212,213 , 221,222,223 , 231,232,233 , 241,242,243};// int ar3d[2][4][3]std::cout<<"Initial 2x4x3 array:\n"; test_print(v, 2, 4, 3); // update every value in the first columns of both planes v[std::gslice(0, {2, 4}, {4*3, 3})]=1;// two level one strides of 12 elements// then four level two strides of 3 elements // subtract the third column from the second column in the 1st plane v[std::gslice(1, {1, 4}, {4*3, 3})]-= v[std::gslice(2, {1, 4}, {4*3, 3})]; std::cout<<"After column operations: \n"; test_print(v, 2, 4, 3);}
出力:
Initial 2x4x3 array: 111 112 113 121 122 123 131 132 133 141 142 143 211 212 213 221 222 223 231 232 233 241 242 243 After column operations: 1 -1 113 1 -1 123 1 -1 133 1 -1 143 1 212 213 1 222 223 1 232 233 1 242 243
[編集]関連項目
valarray の要素、スライス、マスクを取得または設定します (パブリックメンバ関数) | |
BLAS ライクな valarray のスライス: 開始位置、長さ、ストライド (クラス) | |
gslice 適用後の valarray のサブセットへのプロキシ (クラステンプレート) |