2015年11月17日火曜日

[Octave] broadcasting

Octaveにはbroadcastingという概念があるらしい。

これは例で言うと、下記のような演算ができるようになるもの

>A = [ 1 2 3; 4 5 6; 7 8 9]
>B = [1]
>C=  [ 10 20 30]
>bsxfun(@plus,A,B)
[2 3 4; 5 6 7; 8 9 10]
全部の要素に1が足されている 
 
>bsxfun(@plus,A,C)
[11 22 33; 14 25 26; 17 28 39]
一行目には10、二行目には20、三行目には30が足されている。

ちなみに、bsxfun関数を使わなくとも、A + BやA + Cをやっても同じ結果が返される。デフォルトで有効になっているということだろうか。(後で調べる)

OctaveやNumpyではbroadcastingと呼んでいるが、Matlabではbinary singleton expansion、Rではsingle instruction multiple dataとかreplicationと呼ばれたりするらしい。
A note on terminology: “broadcasting” is the term popularized by the Numpy numerical environment in the Python programming language. In other programming languages and environments, broadcasting may also be known as binary singleton expansion (BSX, in MATLAB, and the origin of the name of the bsxfun function), recycling (R programming language), single-instruction multiple data (SIMD), orreplication. 
 
GNU Octave 19.2 broadcasting
Mathworks bsxfun

単純に行列演算してる分にはとっつきやすい印象があったOctaveだが、ちょっと突っ込んで使い始めると意外にぱっと見わかりにくい機能が多い

0 件のコメント:

コメントを投稿