マトリックス&ベクトルライブラリ More...
#include "matrix.h"
Go to the source code of this file.
Definition in file matrix.c.
imatrix add_imatrix(imatrix a, imatrix b)
マトリックスの足し算. 整数マトリックス a, bを足し算して,結果の整数マトリックスを返す.
a | 足されるマトリックス. | |
b | 足すマトリックス. |
Definition at line 610 of file matrix.c.
References make_imatrix(), imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
00611 { 00612 int i; 00613 imatrix c; 00614 00615 c.n = c.r = 0; 00616 c.sz = NULL; 00617 c.mx = NULL; 00618 if (a.mx==NULL || b.mx==NULL) return c; 00619 if (a.r != b.r) return c; 00620 00621 c = make_imatrix(a.n, a.sz); 00622 for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] + b.mx[i]; 00623 return c; 00624 }
matrix add_matrix(matrix a, matrix b)
マトリックスの足し算. 実数マトリックス a, bを足し算して,結果の実数マトリックスを返す.
a | 足されるマトリックス. | |
b | 足すマトリックス. |
Definition at line 582 of file matrix.c.
References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.
00583 { 00584 int i; 00585 matrix c; 00586 00587 c.n = c.r = 0; 00588 c.sz = NULL; 00589 c.mx = NULL; 00590 if (a.mx==NULL || b.mx==NULL) return c; 00591 if (a.r != b.r) return c; 00592 00593 c = make_matrix(a.n, a.sz); 00594 for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] + b.mx[i]; 00595 return c; 00596 }
void copy_imatrix(imatrix src, imatrix dst)
整数マトリックスのコピー.srcの内容を dstへコピーする.
マトリックス全体のサイズが合わない場合は何もしない.
全体のサイズが合っていればコピーする.
src | コピー元マトリックス. | |
dst | コピー先マトリックス. |
Definition at line 559 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
void copy_matrix(matrix src, matrix dst)
マトリックスのコピー.srcの内容を dstへコピーする.
マトリックス全体のサイズが合わない場合は何もしない.
全体のサイズが合っていればコピーする.
src | コピー元マトリックス. | |
dst | コピー先マトリックス. |
Definition at line 536 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
matrix decompQR(matrix xx, imatrix col)
2次元行列 xxのQR分解を行い,R行列を返す.
xx = Q・R
xx | QR分解を行う2次元行列. | |
col | メモリを確保し,初期化しておく.Qの基底ベクトルが入る. |
Definition at line 881 of file matrix.c.
References free_matrix(), make_matrix(), Mx, imatrix::mx, matrix::mx, matrix::n, matrix::r, imatrix::sz, matrix::sz, and Vt.
Referenced by minimum2().
00882 { 00883 int i, j, r, n, m, sz[2]; 00884 double s, t, u; 00885 matrix nsq, isq, x, a; 00886 00887 a.n = a.r = 0; 00888 a.sz = NULL; 00889 a.mx = NULL; 00890 if (xx.mx==NULL || col.mx==NULL) return a; 00891 if (xx.n!=2 || (xx.sz[1]!=col.sz[0])) return a; 00892 00893 nsq.mx = isq.mx = x.mx = NULL; 00894 n = xx.sz[0]; 00895 m = xx.sz[1]; 00896 sz[0] = sz[1] = m; 00897 nsq = make_matrix(1, &m); 00898 isq = make_matrix(1, &m); 00899 x = make_matrix(xx.n, xx.sz); 00900 a = make_matrix(xx.n, sz); 00901 if (nsq.mx==NULL || isq.mx==NULL || x.mx==NULL || a.mx==NULL) { 00902 free_matrix(&nsq); 00903 free_matrix(&isq); 00904 free_matrix(&x); 00905 return a; 00906 } 00907 00908 for (i=0; i<xx.r; i++) x.mx[i] = xx.mx[i]; 00909 00910 for (i=1; i<=m; i++) { 00911 for (s=0.0,j=1; j<=n; j++) s = s + Mx(x, j, i)*Mx(x, j, i); 00912 Vt(nsq, i) = s; 00913 Vt(isq, i) = ((Vt(nsq, i)!=0) ? Vt(nsq,i) : -1.0); 00914 Vt(col, i) = i; 00915 } 00916 00917 for (r=1; r<=m; r++) { 00918 if (r!=1) { 00919 j = r; u = 0.0; 00920 for (i=r; i<=m; i++) { 00921 t = Vt(nsq,i)/Vt(isq,i); 00922 if (t>u) { u = t; j = i; } 00923 } 00924 i = Vt(col,j); Vt(col,j) = Vt(col,r); Vt(col,r) = i; 00925 t = Vt(nsq,j); Vt(nsq,j) = Vt(nsq,r); Vt(nsq,r) = t; 00926 t = Vt(isq,j); Vt(isq,j) = Vt(isq,r); Vt(isq,r) = t; 00927 for (i=1; i<=n; i++) { 00928 t = Mx(x,i,j); 00929 Mx(x,i,j) = Mx(x,i,r); 00930 Mx(x,i,r) = t; 00931 } 00932 } 00933 00934 for (u=0.0,i=r; i<=n; i++) u = u + Mx(x,i,r)*Mx(x,i,r); 00935 u = sqrt(u); 00936 if (Mx(x,r,r)<0.0) u = -u; 00937 Mx(x, r, r) = Mx(x,r,r) + u; 00938 t = 1.0/(Mx(x,r,r)*u); 00939 00940 for (j=1; j<=r-1; j++) Mx(x,r,j)=0.0; 00941 for (j=r+1; j<=m; j++) { 00942 for (s=0.0,i=r; i<=n; i++) s = s + Mx(x,i,r)*Mx(x,i,j); 00943 for (i=r; i<=n; i++) Mx(x,i,j) = Mx(x,i,j) - s*t*Mx(x,i,r); 00944 } 00945 Mx(x,r,r) = -u; 00946 } 00947 00948 for (i=1; i<=m; i++) { 00949 for (j=1; j<=m; j++) Mx(a,i,j) = Mx(x,i,j); 00950 } 00951 00952 free_matrix(&nsq); 00953 free_matrix(&isq); 00954 free_matrix(&x); 00955 00956 return a; 00957 }
vector ex_vector(vector a, vector b)
ベクトルの外積. 実数ベクトル a,b の外積ベクトルを計算し,それを返す.
a | 外積を計算するベクトル. | |
b | 外積を計算するベクトル. |
実数ベクトル a から整数ベクトルをつくり出し,それを返す. 各要素は四捨五入される.
a | 変換する実数ベクトル. |
Definition at line 131 of file matrix.c.
References ivector::n, vector::x, ivector::x, vector::y, ivector::y, vector::z, and ivector::z.
void free_imatrix | ( | imatrix * | a | ) |
整数マトリックスのバッファ部を開放する.
a | 開放するバッファ部を持った整数マトリックスへのポインタ. |
Definition at line 435 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
Referenced by minimum2().
00436 { 00437 if(a->sz!=NULL) free(a->sz); 00438 if(a->mx!=NULL) free(a->mx); 00439 a->sz = NULL; 00440 a->mx = NULL; 00441 a->n = a->r = 0; 00442 }
void free_matrix | ( | matrix * | a | ) |
マトリックスのバッファ部を開放をする.
a | 開放するバッファ部を持ったマトリックスへのポインタ. |
Definition at line 417 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
Referenced by decompQR(), and minimum2().
00418 { 00419 if(a->sz!=NULL) free(a->sz); 00420 if(a->mx!=NULL) free(a->mx); 00421 a->sz = NULL; 00422 a->mx = NULL; 00423 a->n = a->r = 0; 00424 }
int* get_imatrix | ( | imatrix | mtx, | |
... | ||||
) |
int* get_imatrix(imatrix mtx, ...)
Matrix の要素を返す.次元数に制限はない.インデックスは1から数える(0からではない).
参考:1次元配列へのアクセスインデックス 1次元: (i) (i-1) 2次元: (i,j) (j-1) + sz[1]*(i-1) 3次元: (i,j,k) (k-1) + sz[2]*(j-1) + sz[1]*sz[2]*(i-1) 4次元: (i,j,k,l) (l-1) + sz[3]*(k-1) + sz[2]*sz[3]*(j-1) + sz[1]*sz[2]*sz[3]*(i-1) ...................
Definition at line 500 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
00501 { 00502 int m, d; 00503 int* args; 00504 va_list argsptr; 00505 00506 if (mtx.n<1) return NULL; 00507 args = (int*)malloc(mtx.n*sizeof(int)); 00508 if (args==NULL) return NULL; 00509 00510 va_start(argsptr, mtx); 00511 for (m=0; m<mtx.n; m++) { 00512 args[m] = (int)va_arg(argsptr, int); 00513 } 00514 va_end(argsptr); 00515 00516 int dx = args[0] - 1; 00517 for (d=1; d<mtx.n; d++) dx = dx*mtx.sz[d] + args[d] - 1; 00518 free(args); 00519 00520 if (dx>=mtx.r || dx<0) return NULL; 00521 return &mtx.mx[dx]; 00522 }
double* get_matrix | ( | matrix | mtx, | |
... | ||||
) |
double* get_matrix(matrix mtx, ...)
Matrix の要素を返す.次元数に制限はない.インデックスは1から数える(0からではない).
参考:1次元配列へのアクセスインデックス 1次元: (i) (i-1) 2次元: (i,j) (j-1) + sz[1]*(i-1) 3次元: (i,j,k) (k-1) + sz[2]*(j-1) + sz[1]*sz[2]*(i-1) 4次元: (i,j,k,l) (l-1) + sz[3]*(k-1) + sz[2]*sz[3]*(j-1) + sz[1]*sz[2]*sz[3]*(i-1) ...................
Definition at line 460 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
00461 { 00462 int m, d; 00463 int* args; 00464 va_list argsptr; 00465 00466 if (mtx.n<1) return NULL; 00467 args = (int*)malloc(mtx.n*sizeof(int)); 00468 if (args==NULL) return NULL; 00469 00470 va_start(argsptr, mtx); 00471 for (m=0; m<mtx.n; m++) { 00472 args[m] = (int)va_arg(argsptr, int); 00473 } 00474 va_end(argsptr); 00475 00476 int dx = args[0] - 1; 00477 for (d=1; d<mtx.n; d++) dx = dx*mtx.sz[d] + args[d] - 1; 00478 free(args); 00479 00480 if (dx>=mtx.r || dx<0) return NULL; 00481 return &mtx.mx[dx]; 00482 }
整数ベクトル a から実数ベクトルをつくり出し,それを返す.
a | 変換する整数ベクトル. |
Definition at line 152 of file matrix.c.
References vector::n, ivector::x, vector::x, ivector::y, vector::y, ivector::z, and vector::z.
2次元の上三角行列 xの逆行列を求める.
x | 操作対象行列(上三角行列). |
Definition at line 1060 of file matrix.c.
References make_matrix(), Mx, matrix::mx, matrix::n, matrix::r, and matrix::sz.
Referenced by minimum2().
01061 { 01062 int i, j, k, n; 01063 double t, u, det; 01064 matrix a; 01065 01066 a.n = a.r = 0; 01067 a.sz = NULL; 01068 a.mx = NULL; 01069 if (x.mx==NULL) return a; 01070 if (x.sz[0]!=x.sz[1]) return a; 01071 01072 n = x.sz[0]; // 上三角行列のチェック 01073 for (j=1; j<n; j++) { 01074 for (i=j+1; i<=n; i++) { 01075 if (Mx(x,i,j) != 0.0) return a; 01076 } 01077 } 01078 01079 det = 1.0; 01080 a = make_matrix(x.n, x.sz); 01081 if (a.mx==NULL) return a; 01082 01083 for (i=0; i<a.r; i++) a.mx[i] = x.mx[i]; 01084 01085 for (k=1; k<=n; k++) { 01086 t = Mx(a,k,k); 01087 det = det*t; 01088 for (i=1; i<=n; i++) Mx(a,i,k) = Mx(a,i,k)/t; 01089 Mx(a,k,k) = 1.0/t; 01090 01091 for (j=1; j<=n; j++) { 01092 if (j!=k) { 01093 u = Mx(a,k,j); 01094 for (i=1; i<=n; i++) { 01095 if (i!=k) Mx(a,i,j) = Mx(a,i,j) - Mx(a,i,k)*u; 01096 else Mx(a,i,j) = - u/t; 01097 } 01098 } 01099 } 01100 } 01101 return a; 01102 }
imatrix make_imatrix | ( | int | n, | |
int * | sz | |||
) |
imatrix make_imatrix(int n, int* sz)
任意(n)次元の整数マトリックスのバッファ部をつくり出す. 要素自体は 0に初期化される.
n | マトリックスの次元数. | |
sz | sz[0]〜sz[n-1]: 各次元の要素数. |
Definition at line 380 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
Referenced by add_imatrix(), minimum2(), mlt_imatrix(), and sub_imatrix().
00381 { 00382 int i, s; 00383 imatrix a; 00384 00385 a.n = a.r = 0; 00386 a.sz = (int*)malloc(n*sizeof(int)); 00387 if (a.sz==NULL) { 00388 a.mx = NULL; 00389 return a; 00390 } 00391 for (s=1,i=0; i<n; i++) { 00392 a.sz[i] = sz[i]; 00393 s = s*sz[i]; 00394 } 00395 00396 a.mx = (int*)malloc(s*sizeof(int)); 00397 if (a.mx==NULL) { 00398 free(a.sz); 00399 return a; 00400 } 00401 00402 a.n = n; 00403 a.r = s; 00404 for (i=0; i<s; i++) a.mx[i] = 0; 00405 return a; 00406 }
imatrix make_imatrix1 | ( | int | n | ) |
1次元の整数行列のバッファ部をつくり出す. 要素自体は 0に初期化される.
n | 1次元行列の大きさ. |
Definition at line 231 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
00232 { 00233 int i; 00234 imatrix a; 00235 00236 a.n = a.r = 0; 00237 a.mx = NULL; 00238 a.sz = (int*)malloc(sizeof(int)); 00239 if (a.sz==NULL) return a; 00240 a.sz[0] = n; 00241 00242 a.mx = (int*)malloc(n*sizeof(int)); 00243 if (a.mx==NULL) { 00244 free(a.sz); 00245 return a; 00246 } 00247 00248 a.n = 1; 00249 a.r = n; 00250 for (i=0; i<n; i++) a.mx[i] = 0; 00251 return a; 00252 }
imatrix make_imatrix2 | ( | int | n, | |
int | m | |||
) |
imatrix make_imatrix2(int n, int m)
2次元の整数行列のバッファ部をつくり出す. 要素自体は 0に初期化される.
n | 2次元行列の行の数. | |
m | 2次元行列の列の数. |
Definition at line 303 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
00304 { 00305 int i, s; 00306 imatrix a; 00307 00308 a.n = a.r = 0; 00309 a.mx = NULL; 00310 a.sz = (int*)malloc(2*sizeof(int)); 00311 if (a.sz==NULL) return a; 00312 a.sz[0] = n; 00313 a.sz[1] = m; 00314 s = n*m; 00315 00316 a.mx = (int*)malloc(s*sizeof(int)); 00317 if (a.mx==NULL) { 00318 free(a.sz); 00319 return a; 00320 } 00321 00322 a.n = 2; 00323 a.r = s; 00324 for (i=0; i<s; i++) a.mx[i] = 0; 00325 return a; 00326 }
matrix make_matrix | ( | int | n, | |
int * | sz | |||
) |
matrix make_matrix(int n, int* sz)
任意(n)次元の実数マトリックスのバッファ部をつくり出す. 要素自体は 0.0に初期化される.
n | マトリックスの次元数. | |
sz | sz[0]〜sz[n-1]: 各次元の要素数. |
Definition at line 340 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
Referenced by add_matrix(), decompQR(), invrU_matrix(), minimum2(), mlt_matrix(), sub_matrix(), and trans_matrix().
00341 { 00342 int i, s; 00343 matrix a; 00344 00345 a.n = a.r = 0; 00346 a.sz = (int*)malloc(n*sizeof(int)); 00347 if (a.sz==NULL) { 00348 a.mx = NULL; 00349 return a; 00350 } 00351 for (s=1,i=0; i<n; i++) { 00352 a.sz[i] = sz[i]; 00353 s = s*sz[i]; 00354 } 00355 00356 a.mx = (double*)malloc(s*sizeof(double)); 00357 if (a.mx==NULL) { 00358 free(a.sz); 00359 return a; 00360 } 00361 00362 a.n = n; 00363 a.r = s; 00364 for (i=0; i<s; i++) a.mx[i] = 0.0; 00365 return a; 00366 }
matrix make_matrix1 | ( | int | n | ) |
1次元の実数行列のバッファ部をつくり出す. 要素自体は 0.0に初期化される.
n | 1次元行列の大きさ. |
Definition at line 197 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
00198 { 00199 int i; 00200 matrix a; 00201 00202 a.n = a.r = 0; 00203 a.mx = NULL; 00204 a.sz = (int*)malloc(sizeof(int)); 00205 if (a.sz==NULL) return a; 00206 a.sz[0] = n; 00207 00208 a.mx = (double*)malloc(n*sizeof(double)); 00209 if (a.mx==NULL) { 00210 free(a.sz); 00211 return a; 00212 } 00213 00214 a.n = 1; 00215 a.r = n; 00216 for (i=0; i<n; i++) a.mx[i] = 0.0; 00217 return a; 00218 }
matrix make_matrix2 | ( | int | n, | |
int | m | |||
) |
matrix make_matrix2(int n, int m)
2次元の実数行列のバッファ部をつくり出す. 要素自体は 0.0に初期化される.
n | 2次元行列の行の数. | |
m | 2次元行列の列の数. |
Definition at line 266 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
00267 { 00268 int i, s; 00269 matrix a; 00270 00271 a.n = a.r = 0; 00272 a.mx = NULL; 00273 a.sz = (int*)malloc(2*sizeof(int)); 00274 if (a.sz==NULL) return a; 00275 a.sz[0] = n; 00276 a.sz[1] = m; 00277 s = n*m; 00278 00279 a.mx = (double*)malloc(s*sizeof(double)); 00280 if (a.mx==NULL) { 00281 free(a.sz); 00282 return a; 00283 } 00284 00285 a.n = 2; 00286 a.r = s; 00287 for (i=0; i<s; i++) a.mx[i] = 0.0; 00288 return a; 00289 }
matrix minimum2(matrix y, matrix x)
最小2乗法で方程式の近似解を解き,結果を返す. ただし, x,yは2次元行列のみ.
y | 連立方程式の結果の行列 (例を見よ) | |
x | 連立方程式の変数の行列 (例を見よ) |
Y = X・A y1 = x11*a1 + x12*a2 y2 = x21*a1 + x22*a2 y3 = x31*a1 + x32*a2
Definition at line 980 of file matrix.c.
References decompQR(), free_imatrix(), free_matrix(), invrU_matrix(), make_imatrix(), make_matrix(), mlt_matrix(), imatrix::mx, matrix::mx, matrix::n, matrix::r, matrix::sz, and trans_matrix().
00981 { 00982 int i, m; 00983 imatrix cl; 00984 matrix rx, rt, rr, aa, bb, cc; 00985 00986 cc.n = cc.r = 0; 00987 cc.sz = NULL; 00988 cc.mx = NULL; 00989 if (y.mx==NULL || x.mx==NULL) return cc; 00990 if (y.sz[0]!=x.sz[0]) return cc; 00991 00992 //n = x.sz[0]; 00993 m = x.sz[1]; /* n×m 行列 n>=m */ 00994 cl = make_imatrix(1, &m); 00995 if (cl.mx==NULL) return cc; 00996 cc = make_matrix (1, &m); 00997 if (cc.mx==NULL) {free_imatrix(&cl); return cc;} 00998 00999 rx = decompQR(x, cl); 01000 rr = invrU_matrix(rx), 01001 rt = mlt_matrix(rr, trans_matrix(rr)); 01002 aa = mlt_matrix(trans_matrix(x), y), 01003 bb = mlt_matrix(rt, aa); 01004 01005 if (bb.mx!=NULL) for (i=0; i<m; i++) cc.mx[cl.mx[i]-1] = bb.mx[i]; 01006 01007 free_imatrix(&cl); 01008 free_matrix(&rx); 01009 free_matrix(&rr); 01010 free_matrix(&rt); 01011 free_matrix(&aa); 01012 free_matrix(&bb); 01013 01014 return cc; 01015 }
imatrix mlt_imatrix(imatrix a, imatrix b)
マトリックスのかけ算. 整数マトリックス a, bをかけ算して,結果の整数マトリックスを返す.
a | かけられるマトリックス. | |
b | かけるマトリックス. |
Definition at line 765 of file matrix.c.
References make_imatrix(), imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
00766 { 00767 int i, j, k, n, ii, aa, bb, st; 00768 int *sz, *sa, *sb, *sc, *cx; 00769 imatrix c; 00770 00771 c.n = c.r = 0; 00772 c.sz = NULL; 00773 c.mx = NULL; 00774 if (a.mx==NULL || b.mx==NULL) return c; 00775 if (a.sz[a.n-1]!=b.sz[0]) return c; 00776 00777 n = a.n + b.n - 2; 00778 sz = (int*)malloc(n*sizeof(int)); 00779 if (sz==NULL) return c; 00780 sa = (int*)malloc(a.n*sizeof(int)); 00781 if (sa==NULL) {free(sz); return c;} 00782 sb = (int*)malloc(b.n*sizeof(int)); 00783 if (sb==NULL) {free(sz); free(sa); return c;} 00784 sc = (int*)malloc(n*sizeof(int)); 00785 if (sc==NULL) {free(sz); free(sa); free(sb); return c;} 00786 cx = (int*)malloc(n*sizeof(int)); 00787 if (cx==NULL) {free(sz); free(sa); free(sb); free(sc); return c;} 00788 00789 00790 for (i=0; i<a.n-1; i++) sz[i] = a.sz[i]; 00791 for (i=1; i<b.n; i++) sz[a.n-2+i] = b.sz[i]; 00792 00793 sa[a.n-1] = sb[b.n-1] = sc[n-1] = 1; 00794 for (i=a.n-2; i>=0; i--) sa[i] = sa[i+1]*a.sz[i+1]; 00795 for (i=b.n-2; i>=0; i--) sb[i] = sb[i+1]*b.sz[i+1]; 00796 for (i=n-2; i>=0; i--) sc[i] = sc[i+1]*sz[i+1]; 00797 00798 c = make_imatrix(n, sz); 00799 00800 for (i=0; i<c.r; i++) { 00801 ii = i; 00802 for (j=0; j<c.n; j++) { 00803 cx[j] = ii / sc[j]; 00804 ii = ii % sc[j]; 00805 } 00806 aa = bb = 0; 00807 for (j=0; j<a.n-1; j++) aa = aa + sa[j]*cx[j]; 00808 for (j=1; j<b.n; j++) bb = bb + sb[j]*cx[j+a.n-2]; 00809 00810 st = 0; 00811 for (k=0; k<b.sz[0]; k++) st = st + a.mx[k+aa]*b.mx[bb+sb[0]*k]; 00812 c.mx[i] = st; 00813 } 00814 00815 free(sz); 00816 free(sa); 00817 free(sb); 00818 free(sc); 00819 free(cx); 00820 00821 return c; 00822 }
matrix mlt_matrix(matrix a, matrix b)
マトリックスのかけ算. 実数マトリックス a, bをかけ算して,結果の実数マトリックスを返す.
a | かけられるマトリックス. | |
b | かけるマトリックス. |
Definition at line 694 of file matrix.c.
References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.
Referenced by minimum2().
00695 { 00696 int i, j, k, n, ii, aa, bb; 00697 int *sz, *sa, *sb, *sc, *cx; 00698 double st; 00699 matrix c; 00700 00701 c.n = c.r = 0; 00702 c.sz = NULL; 00703 c.mx = NULL; 00704 if (a.mx==NULL || b.mx==NULL) return c; 00705 if (a.sz[a.n-1]!=b.sz[0]) return c; 00706 00707 n = a.n + b.n - 2; 00708 sz = (int*)malloc(n*sizeof(int)); 00709 if (sz==NULL) return c; 00710 sa = (int*)malloc(a.n*sizeof(int)); 00711 if (sa==NULL) {free(sz); return c;} 00712 sb = (int*)malloc(b.n*sizeof(int)); 00713 if (sb==NULL) {free(sz); free(sa); return c;} 00714 sc = (int*)malloc(n*sizeof(int)); 00715 if (sc==NULL) {free(sz); free(sa); free(sb); return c;} 00716 cx = (int*)malloc(n*sizeof(int)); 00717 if (cx==NULL) {free(sz); free(sa); free(sb); free(sc); return c;} 00718 00719 for (i=0; i<a.n-1; i++) sz[i] = a.sz[i]; 00720 for (i=1; i<b.n; i++) sz[a.n-2+i] = b.sz[i]; 00721 00722 sa[a.n-1] = sb[b.n-1] = sc[n-1] = 1; 00723 for (i=a.n-2; i>=0; i--) sa[i] = sa[i+1]*a.sz[i+1]; 00724 for (i=b.n-2; i>=0; i--) sb[i] = sb[i+1]*b.sz[i+1]; 00725 for (i=n-2; i>=0; i--) sc[i] = sc[i+1]*sz[i+1]; 00726 00727 c = make_matrix(n, sz); 00728 00729 for (i=0; i<c.r; i++) { 00730 ii = i; 00731 for (j=0; j<c.n; j++) { 00732 cx[j] = ii / sc[j]; 00733 ii = ii % sc[j]; 00734 } 00735 aa = bb = 0; 00736 for (j=0; j<a.n-1; j++) aa = aa + sa[j]*cx[j]; 00737 for (j=1; j<b.n; j++) bb = bb + sb[j]*cx[j+a.n-2]; 00738 00739 st = 0.0; 00740 for (k=0; k<b.sz[0]; k++) st = st + a.mx[k+aa]*b.mx[bb+sb[0]*k]; 00741 c.mx[i] = st; 00742 } 00743 00744 free(sz); 00745 free(sa); 00746 free(sb); 00747 free(sc); 00748 free(cx); 00749 00750 return c; 00751 }
void print_imatrix | ( | FILE * | fp, | |
imatrix | a | |||
) |
void print_imatrix(FILE* fp, imatrix a)
整数マトリックスの要素を標準出力に書き出す.
fp | 出力先のファイル記述子 | |
a | プリントするマトリックス. |
Definition at line 854 of file matrix.c.
References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
void print_matrix | ( | FILE * | fp, | |
matrix | a | |||
) |
void print_matrix(FILE* fp, matrix a)
実数マトリックスの要素を標準出力に書き出す.
fp | 出力先のファイル記述子 | |
a | プリントするマトリックス. |
Definition at line 834 of file matrix.c.
References matrix::mx, matrix::n, matrix::r, and matrix::sz.
ivector set_ivector | ( | int | x, | |
int | y, | |||
int | z | |||
) |
ivector set_ivector(int x, int y, int z)
整数ベクトルの作成.x方向成分, y方向成分, z方向成分から整数ベクトルを作り,それを返す.
x | ベクトルの x成分. | |
y | ベクトルの y成分. | |
z | ベクトルの z成分. |
Definition at line 109 of file matrix.c.
References ivector::n, ivector::x, ivector::y, and ivector::z.
vector set_vector | ( | double | x, | |
double | y, | |||
double | z | |||
) |
vector set_vector(double x, double y, double z)
ベクトルの作成.x方向成分, y方向成分, z方向成分から実数ベクトルを作り,それを返す.
x | ベクトルの x成分. | |
y | ベクトルの y成分. | |
z | ベクトルの z成分. |
Definition at line 86 of file matrix.c.
References vector::n, vector::x, vector::y, and vector::z.
Referenced by circle3d(), curvature(), curvature3D(), make_VSGraph(), new_VSGraph(), pool(), read_shape_main_file(), torus(), vfNabra(), and vNabra().
00087 { 00088 vector c; 00089 00090 c.x = x; 00091 c.y = y; 00092 c.z = z; 00093 c.n = sqrt(x*x + y*y + z*z); 00094 return c; 00095 }
imatrix sub_imatrix(imatrix a, imatrix b)
マトリックスの引き算. 整数マトリックス a, bを引き算して,結果の整数マトリックスを返す.
a | 引かれるマトリックス. | |
b | 引くマトリックス. |
Definition at line 666 of file matrix.c.
References make_imatrix(), imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.
00667 { 00668 int i; 00669 imatrix c; 00670 00671 c.n = c.r = 0; 00672 c.sz = NULL; 00673 c.mx = NULL; 00674 if (a.mx==NULL || b.mx==NULL) return c; 00675 if (a.r != b.r) return c; 00676 00677 c = make_imatrix(a.n, a.sz); 00678 for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] - b.mx[i]; 00679 return c; 00680 }
matrix sub_matrix(matrix a, matrix b)
マトリックスの引き算. 実数マトリックス a, bを引き算して,結果の実数マトリックスを返す.
a | 引かれるマトリックス. | |
b | 引くマトリックス. |
Definition at line 638 of file matrix.c.
References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.
00639 { 00640 int i; 00641 matrix c; 00642 00643 c.n = c.r = 0; 00644 c.sz = NULL; 00645 c.mx = NULL; 00646 if (a.mx==NULL || b.mx==NULL) return c; 00647 if (a.r != b.r) return c; 00648 00649 c = make_matrix(a.n, a.sz); 00650 for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] - b.mx[i]; 00651 return c; 00652 }
2次元行列 aの転置行列を返す.
a | 操作対象行列. |
Definition at line 1027 of file matrix.c.
References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.
Referenced by minimum2().
01028 { 01029 int i, j, k; 01030 matrix c; 01031 int sz[2]; 01032 01033 c.n = c.r = 0; 01034 c.sz = NULL; 01035 c.mx = NULL; 01036 if (a.mx==NULL || a.n!=2) return c; 01037 01038 sz[0] = a.sz[1]; 01039 sz[1] = a.sz[0]; 01040 c = make_matrix(a.n, sz); 01041 01042 for (k=0; k<c.r; k++) { 01043 i = k/a.sz[1]; 01044 j = k%a.sz[1]; 01045 c.mx[j*sz[1]+i] = a.mx[k]; 01046 } 01047 return c; 01048 }
vector unit_ivector(ivector a)
整数ベクトル aの単位ベクトルを返す.返されるベクトルは実数ベクトル.
a | 対象整数ベクトル. |
Definition at line 52 of file matrix.c.
References EPS, vector::n, vector::x, ivector::x, Xabs, vector::y, ivector::y, vector::z, and ivector::z.
00053 { 00054 vector c; 00055 double r; 00056 00057 r = a.x*a.x+a.y*a.y+a.z*a.z; 00058 if (Xabs(r)<EPS*EPS) { 00059 c.x = 0.0; 00060 c.y = 0.0; 00061 c.z = 0.0; 00062 c.n = 1.0; 00063 } 00064 else { 00065 r = sqrt(r); 00066 c.x = a.x/r; 00067 c.y = a.y/r; 00068 c.z = a.z/r; 00069 c.n = 1.0; 00070 } 00071 return c; 00072 }
ベクトル aの単位ベクトルを返す.
a | 対象ベクトル. |
Definition at line 20 of file matrix.c.
References EPS, vector::n, vector::x, Xabs, vector::y, and vector::z.
Referenced by circle3d(), pool(), torus(), vfNabra(), and vNabra().
00021 { 00022 vector c; 00023 double r; 00024 00025 r = a.x*a.x+a.y*a.y+a.z*a.z; 00026 if (Xabs(r)<EPS*EPS) { 00027 c.x = 0.0; 00028 c.y = 0.0; 00029 c.z = 0.0; 00030 c.n = 1.0; 00031 } 00032 else { 00033 r = sqrt(r); 00034 c.x = a.x/r; 00035 c.y = a.y/r; 00036 c.z = a.z/r; 00037 c.n = 1.0; 00038 } 00039 return c; 00040 }