行列の各種演算を関数として以下のように実装する。
/* スカラーの行列への代入(初期化) */
void WH_Matrix__assign_s_OUT_whM
(double a,
int nRows_B, int nColumns_B, void *OUT__whM_B)
/*
入力引数:
a はスカラー
nRows_B は行列 OUT__whM_B の行数
nColumns_B は行列 OUT__whM_B の列数
出力引数:
OUT__whM_B は行列
*/
{
int i, j;
for (i = 0; i < nRows_B; i++) {
for (j = 0; j < nColumns_B; j++) {
WH_MATRIX__SET(OUT__whM_B, nRows_B, nColumns_B,
i, j, a);
}
}
}
/* 行列の行列への代入 */
void WH_Matrix__assign_whM_OUT_whM
(int nRows_A, int nColumns_A, void *whM_A,
int nRows_B, int nColumns_B, void *OUT__whM_B)
/*
入力引数:
nRows_A は行列 whM_A の行数
nColumns_A は行列 whM_A の列数
whM_A は行列
nRows_B は行列 OUT__whM_B の行数
nColumns_B は行列 OUT__whM_B の列数
出力引数:
OUT__whM_B は行列で、whM_A と同じもの
*/
{
int i, j;
assert(nRows_A == nRows_B);
assert(nColumns_A == nColumns_B);
for (i = 0; i < nRows_B; i++) {
for (j = 0; j < nColumns_B; j++) {
WH_MATRIX__AT(OUT__whM_B, nRows_B, nColumns_B, i, j)
= WH_MATRIX__AT(whM_A, nRows_A, nColumns_A, i, j);
}
}
}
/* 行列と行列との和 */
void WH_Matrix__whM_add_whM_OUT_whM
(int nRows_A, int nColumns_A, void *whM_A,
int nRows_B, int nColumns_B, void *whM_B,
int nRows_C, int nColumns_C, void *OUT__whM_C)
/*
入力引数:
nRows_A は行列 whM_A の行数
nColumns_A は行列 whM_A の列数
whM_A は行列
nRows_B は行列 whM_B の行数
nColumns_B は行列 whM_B の列数
whM_B は行列
nRows_C は行列 OUT__whM_C の行数
nColumns_C は行列 OUT__whM_C の列数
出力引数:
OUT__whM_C は行列で、whM_A + whM_B
*/
{
int i, j;
assert(nRows_A == nRows_C);
assert(nColumns_A == nColumns_C);
assert(nRows_B == nRows_C);
assert(nColumns_B == nColumns_C);
for (i = 0; i < nRows_C; i++) {
for (j = 0; j < nColumns_C; j++) {
WH_MATRIX__AT(OUT__whM_C, nRows_C, nColumns_C, i, j)
= WH_MATRIX__AT(whM_A, nRows_A, nColumns_A, i, j)
+ WH_MATRIX__AT(whM_B, nRows_B, nColumns_B, i, j);
}
}
}
/* 行列と行列との差 */
void WH_Matrix__whM_add_whM_OUT_whM
(int nRows_A, int nColumns_A, void *whM_A,
int nRows_B, int nColumns_B, void *whM_B,
int nRows_C, int nColumns_C, void *OUT__whM_C)
/*
入力引数:
nRows_A は行列 whM_A の行数
nColumns_A は行列 whM_A の列数
whM_A は行列
nRows_B は行列 whM_B の行数
nColumns_B は行列 whM_B の列数
whM_B は行列
nRows_C は行列 OUT__whM_C の行数
nColumns_C は行列 OUT__whM_C の列数
出力引数:
OUT__whM_C は行列で、whM_A - whM_B
*/
{
int i, j;
assert(nRows_A == nRows_C);
assert(nColumns_A == nColumns_C);
assert(nRows_B == nRows_C);
assert(nColumns_B == nColumns_C);
for (i = 0; i < nRows_C; i++) {
for (j = 0; j < nColumns_C; j++) {
WH_MATRIX__AT(OUT__whM_C, nRows_C, nColumns_C, i, j)
= WH_MATRIX__AT(whM_A, nRows_A, nColumns_A, i, j)
- WH_MATRIX__AT(whM_B, nRows_B, nColumns_B, i, j);
}
}
}
/* 行列とスカラーとの積 */
void WH_Matrix__whM_mul_s_OUT_whM
(int nRows_A, int nColumns_A, void *whM_A,
double b,
int nRows_C, int nColumns_C, void *OUT__whM_C)
/*
入力引数:
nRows_A は行列 whM_A の行数
nColumns_A は行列 whM_A の列数
whM_A は行列
b はスカラー
nRows_C は行列 OUT__whM_C の行数
nColumns_C は行列 OUT__whM_C の列数
出力引数:
OUT__whM_C は行列で、whM_A * b
*/
{
int i, j;
assert(nRows_A == nRows_C);
assert(nColumns_A == nColumns_C);
for (i = 0; i < nRows_C; i++) {
for (j = 0; j < nColumns_C; j++) {
WH_MATRIX__AT(OUT__whM_C, nRows_C, nColumns_C, i, j)
= WH_MATRIX__AT(whM_A, nRows_A, nColumns_A, i, j) * b;
}
}
}
/* スカラーと行列との積 */
void WH_Matrix__s_mul_whM_OUT_whM
(double a,
int nRows_B, int nColumns_B, void *whM_B,
int nRows_C, int nColumns_C, void *OUT__whM_C)
/*
入力引数:
a はスカラー
nRows_B は行列 whM_B の行数
nColumns_B は行列 whM_B の列数
whM_B は行列
nRows_C は行列 OUT__whM_C の行数
nColumns_C は行列 OUT__whM_C の列数
出力引数:
OUT__whM_C は行列で、a * whM_B
*/
{
int i, j;
assert(nRows_B == nRows_C);
assert(nColumns_B == nColumns_C);
for (i = 0; i < nRows_C; i++) {
for (j = 0; j < nColumns_C; j++) {
WH_MATRIX__AT(OUT__whM_C, nRows_C, nColumns_C, i, j)
= a * WH_MATRIX__AT(whM_B, nRows_B, nColumns_B, i, j);
}
}
}
/* 行列をスカラーで割る */
void WH_Matrix__whM_mul_s_OUT_whM
(int nRows_A, int nColumns_A, void *whM_A,
double b,
int nRows_C, int nColumns_C, void *OUT__whM_C)
/*
入力引数:
nRows_A は行列 whM_A の行数
nColumns_A は行列 whM_A の列数
whM_A は行列
b はスカラー
nRows_C は行列 OUT__whM_C の行数
nColumns_C は行列 OUT__whM_C の列数
出力引数:
OUT__whM_C は行列で、whM_A / b
*/
{
int i, j;
assert(nRows_A == nRows_C);
assert(nColumns_A == nColumns_C);
for (i = 0; i < nRows_C; i++) {
for (j = 0; j < nColumns_C; j++) {
WH_MATRIX__AT(OUT__whM_C, nRows_C, nColumns_C, i, j)
= WH_MATRIX__AT(whM_A, nRows_A, nColumns_A, i, j) / b;
}
}
}