関数random_matrix マニュアル

(The documentation of function random_matrix)

Last Update: 2021/12/6


◆機能・用途(Purpose)

各成分の値を乱数で与えた行列を作成する。
Create a matrix whose values of components are given by random values.


◆形式(Format)

#include <matrix/initialize.h>
inline struct matrix random_matrix(const int rowmax,const int columnmax)


◆引数(Arguments)

rowmax 作成する行列の行数。
The number of rows of the matrix to create.
columnmax 作成する行列の列数。
The number of columns of the matrix to create.


◆戻り値(Return value)

各成分の値を乱数で与えたrowmax×columnmaxの行列を表す構造体。 戻り値の各メンバの値は以下のようになる。
A structure which represents a matrix of size rowmax×columnmax, whose components are given by random values. The values of members of the return value are as follows.

戻り値のメンバ
Member of the return value
値
Value
rowmax 引数rowmaxの値。
The value of argument rowmax.
columnmax 引数columnmaxの値。
The value of argument columnmax.
main rowmax×columnmaxの2次元配列。 各配列要素の値は乱数で与える。詳しくは下記「詳細」参照。
A 2-D array of rowmax×columnmax. The values of array components are given by random values; see “Detail” below.
allocated ’y’


◆使用例(Example)

struct matrix A=random_matrix(2,3);


◆詳細(Detail)

この関数では乱数値を以下の方法で生成する。
In this function, random values are generated by the procedure below.
  1. 関数randを用いて0-RAND_MAXの間の整数値乱数を取得する。 0が得られた場合はノンゼロの乱数値が得られるまで繰り返す。
    An integer random value in a range 0-RAND_MAX is obtained by function rand. If the result was 0, then the same procedure is repeated until a non-zero random value is obtained.
  2. 上で取得した整数値乱数の逆数を計算する。
    The inverse of the integer random value obtained above is calculated.
  3. ステップ1で取得した整数値が奇数ならば ステップ2の計算結果に\(-1\)を掛ける。
    A factor \(-1\) is multiplied with the value obtained by the step 2 if the integer obtained by the step 1 was an odd number.
このステップ3で得られた値が行列要素の値として採用される。 したがって全ての値が整数値の逆数であり、値の範囲は\([-1,1]\)である。 0に近い値ほど高確率となる。 また、正の数と負の数を取る確率はほぼ同じである。
The value obtained by the step 3 is adopted as the value of each matrix component. Thus all the values are an inverse of an integer and varies in the range \([-1,1]\). The closer the value is to 0, the higher the probability. Positive and negative values appear at almost same probability.