JunkBox_Lib  1.10.2
matrix.h File Reference

マトリックス&ベクトルライブラリ ヘッダ More...

#include "common.h"
#include "mt.h"
#include <math.h>
#include <float.h>
#include <stdarg.h>
Include dependency graph for matrix.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  ivector
 
struct  vector
 
struct  quaternion
 
struct  imatrix
 
struct  matrix
 

Macros

#define in_vector(a, b)   ((a).x*(b).x+(a).y*(b).y+(a).z*(b).z)
 ベクトル a, bの内積をとる. More...
 
#define add_vector(a, b)   set_vector((a).x+(b).x,(a).y+(b).y,(a).z+(b).z)
 ベクトル a, bを足し算して, 結果を実数ベクトルで返す. More...
 
#define sub_vector(a, b)   set_vector((a).x-(b).x,(a).y-(b).y,(a).z-(b).z)
 ベクトル a, bを引き算して, 結果を実数ベクトルで返す. More...
 
#define add_ivector(a, b)   set_ivector((a).x+(b).x,(a).y+(b).y,(a).z+(b).z)
 ベクトル a, bを足し算して, 結果を整数ベクトルで返す. More...
 
#define sub_ivector(a, b)   set_ivector((a).x-(b).x,(a).y-(b).y,(a).z-(b).z)
 ベクトル a, bを引き算して, 結果を整数ベクトルで返す. More...
 
#define normal_vector(a)   (a).r = sqrt((double)((a).x*(a).x)+(a).y*(a).y+(a).z*(a).z);
 ベクトル aの大きさを計算する. More...
 
#define Vt(m, i)   ((m).mx[(i)-1])
 1次元マトリックスの i番目の要素を返す.1から数える. More...
 
#define Mx(m, i, j)   ((m).mx[(j)-1+(m).sz[1]*((i)-1)])
 2次元マトリックスの(i,j)要素を返す. 1から数える. More...
 
#define Mx1(m, i)   ((m).mx[(i)-1])
 1次元マトリックスの i番目の要素を返す.1から数える. More...
 
#define Mx2(m, i, j)   ((m).mx[(j)-1+(m).sz[1]*((i)-1)])
 2次元マトリックスの(i,j)要素を返す. 1から数える. More...
 
#define Mx3(m, i, j, k)   ((m).mx[(k)-1+(m).sz[2]*((j)-1)+(m).sz[1]*(m).sz[2]*((i)-1)])
 3次元マトリックスの(i,j,k)要素を返す. 1から数える. More...
 
#define Mx4(m, i, j, k, l)
 4次元マトリックスの(i,j,k,l)要素を返す.1から数える. More...
 

Functions

vector unit_vector (vector a)
 
vector unit_ivector (ivector a)
 
vector set_vector (double x, double y, double z)
 
ivector set_ivector (int x, int y, int z)
 
vector ex_vector (vector a, vector b)
 
ivector f2ivector (vector a)
 
vector i2vector (ivector a)
 
matrix make_matrix1 (int n)
 
matrix make_matrix2 (int n, int m)
 
matrix make_matrix (int n, int *sz)
 
imatrix make_imatrix1 (int n)
 
imatrix make_imatrix2 (int n, int m)
 
imatrix make_imatrix (int n, int *sz)
 
double * get_matrix (matrix a,...)
 
matrix add_matrix (matrix a, matrix b)
 
matrix sub_matrix (matrix a, matrix b)
 
matrix mlt_matrix (matrix a, matrix b)
 
void copy_matrix (matrix a, matrix b)
 
void free_matrix (matrix *a)
 
void clear_matrix (matrix *a)
 
void print_matrix (FILE *fp, matrix a)
 
int * get_imatrix (imatrix a,...)
 
imatrix add_imatrix (imatrix a, imatrix b)
 
imatrix sub_imatrix (imatrix a, imatrix b)
 
imatrix mlt_imatrix (imatrix a, imatrix b)
 
void copy_imatrix (imatrix a, imatrix b)
 
void free_imatrix (imatrix *a)
 
void clear_imatrix (imatrix *a)
 
void print_imatrix (FILE *fp, imatrix a)
 
matrix decompQR (matrix xx, imatrix col)
 
matrix trans_matrix (matrix a)
 
matrix minimum2 (matrix a, matrix x)
 
matrix invrU_matrix (matrix x)
 

Detailed Description

Author
Fumi.Iseki (C)

Definition in file matrix.h.

Macro Definition Documentation

◆ add_ivector

#define add_ivector (   a,
 
)    set_ivector((a).x+(b).x,(a).y+(b).y,(a).z+(b).z)

Definition at line 79 of file matrix.h.

◆ add_vector

#define add_vector (   a,
 
)    set_vector((a).x+(b).x,(a).y+(b).y,(a).z+(b).z)

Definition at line 77 of file matrix.h.

◆ in_vector

#define in_vector (   a,
 
)    ((a).x*(b).x+(a).y*(b).y+(a).z*(b).z)

Definition at line 76 of file matrix.h.

◆ Mx

#define Mx (   m,
  i,
 
)    ((m).mx[(j)-1+(m).sz[1]*((i)-1)])

Definition at line 84 of file matrix.h.

◆ Mx1

#define Mx1 (   m,
 
)    ((m).mx[(i)-1])

Definition at line 86 of file matrix.h.

◆ Mx2

#define Mx2 (   m,
  i,
 
)    ((m).mx[(j)-1+(m).sz[1]*((i)-1)])

Definition at line 87 of file matrix.h.

◆ Mx3

#define Mx3 (   m,
  i,
  j,
 
)    ((m).mx[(k)-1+(m).sz[2]*((j)-1)+(m).sz[1]*(m).sz[2]*((i)-1)])

Definition at line 88 of file matrix.h.

◆ Mx4

#define Mx4 (   m,
  i,
  j,
  k,
 
)
Value:
((m).mx[(l)-1+(m).sz[3]*((k)-1)+(m).sz[2]*(m).sz[3]*((j)-1) +\
(m).sz[1]*(m).sz[2]*(m).sz[3]*((i)-1)])

Definition at line 91 of file matrix.h.

◆ normal_vector

#define normal_vector (   a)    (a).r = sqrt((double)((a).x*(a).x)+(a).y*(a).y+(a).z*(a).z);

Definition at line 81 of file matrix.h.

◆ sub_ivector

#define sub_ivector (   a,
 
)    set_ivector((a).x-(b).x,(a).y-(b).y,(a).z-(b).z)

Definition at line 80 of file matrix.h.

◆ sub_vector

#define sub_vector (   a,
 
)    set_vector((a).x-(b).x,(a).y-(b).y,(a).z-(b).z)

Definition at line 78 of file matrix.h.

◆ Vt

#define Vt (   m,
 
)    ((m).mx[(i)-1])

Definition at line 83 of file matrix.h.

Function Documentation

◆ add_imatrix()

imatrix add_imatrix ( imatrix  a,
imatrix  b 
)

imatrix add_imatrix(imatrix a, imatrix b)

マトリックスの足し算. 整数マトリックス a, bを足し算して,結果の整数マトリックスを返す.

Parameters
a足されるマトリックス.
b足すマトリックス.
Returns
足し算結果の整数マトリックス.

Definition at line 594 of file matrix.c.

595 {
596  int i;
597  imatrix c;
598 
599  c.n = c.r = 0;
600  c.sz = NULL;
601  c.mx = NULL;
602  if (a.mx==NULL || b.mx==NULL) return c;
603  if (a.r != b.r) return c;
604 
605  c = make_imatrix(a.n, a.sz);
606  for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] + b.mx[i];
607  return c;
608 }
imatrix make_imatrix(int n, int *sz)
Definition: matrix.c:371
Definition: matrix.h:53
int * sz
各次元の要素数 sz[0]?sz[n-1]
Definition: matrix.h:56
int * mx
要素 mx[0]?mx[r-1]
Definition: matrix.h:57
int n
次元数
Definition: matrix.h:54
int r
全要素数 sz[0]+sz[1]+...+sz[n-1]
Definition: matrix.h:55

References make_imatrix(), imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

Here is the call graph for this function:

◆ add_matrix()

matrix add_matrix ( matrix  a,
matrix  b 
)

matrix add_matrix(matrix a, matrix b)

マトリックスの足し算. 実数マトリックス a, bを足し算して,結果の実数マトリックスを返す.

Parameters
a足されるマトリックス.
b足すマトリックス.
Returns
足し算結果のマトリックス.

Definition at line 567 of file matrix.c.

568 {
569  int i;
570  matrix c;
571 
572  c.n = c.r = 0;
573  c.sz = NULL;
574  c.mx = NULL;
575  if (a.mx==NULL || b.mx==NULL) return c;
576  if (a.r != b.r) return c;
577 
578  c = make_matrix(a.n, a.sz);
579  for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] + b.mx[i];
580  return c;
581 }
matrix make_matrix(int n, int *sz)
Definition: matrix.c:331
Definition: matrix.h:68
double * mx
要素 mx[0]?mx[r-1]
Definition: matrix.h:72
int * sz
各次元の要素数 sz[0]?sz[n-1]
Definition: matrix.h:71
int n
次元数
Definition: matrix.h:69
int r
全要素数 sz[0]+sz[1]+...+sz[n-1]
Definition: matrix.h:70

References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.

Here is the call graph for this function:

◆ clear_imatrix()

void clear_imatrix ( imatrix a)

◆ clear_matrix()

void clear_matrix ( matrix a)

◆ copy_imatrix()

void copy_imatrix ( imatrix  src,
imatrix  dst 
)

void copy_imatrix(imatrix src, imatrix dst)

整数マトリックスのコピー.srcの内容を dstへコピーする.
マトリックス全体のサイズが合わない場合は何もしない.
全体のサイズが合っていればコピーする.

Parameters
srcコピー元マトリックス.
dstコピー先マトリックス.

Definition at line 545 of file matrix.c.

546 {
547  int i;
548 
549  if (src.mx==NULL || dst.mx==NULL) return;
550  if ((src.r!=dst.r)||(dst.n!=dst.n)) return;
551 
552  for (i=0; i<src.n; i++) dst.sz[i] = src.sz[i];
553  for (i=0; i<src.r; i++) dst.mx[i] = src.mx[i];
554 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

◆ copy_matrix()

void copy_matrix ( matrix  src,
matrix  dst 
)

void copy_matrix(matrix src, matrix dst)

マトリックスのコピー.srcの内容を dstへコピーする.
マトリックス全体のサイズが合わない場合は何もしない.
全体のサイズが合っていればコピーする.

Parameters
srcコピー元マトリックス.
dstコピー先マトリックス.

Definition at line 523 of file matrix.c.

524 {
525  int i;
526 
527  if (src.mx==NULL || dst.mx==NULL) return;
528  if ((src.r!=dst.r)||(dst.n!=dst.n)) return;
529 
530  for (i=0; i<src.n; i++) dst.sz[i] = src.sz[i];
531  for (i=0; i<src.r; i++) dst.mx[i] = src.mx[i];
532 }

References matrix::mx, matrix::n, matrix::r, and matrix::sz.

◆ decompQR()

matrix decompQR ( matrix  xx,
imatrix  col 
)

matrix decompQR(matrix xx, imatrix col)

2次元行列 xxのQR分解を行い,R行列を返す.

xx = Q・R

  • xxのQR分解を行った後,係数のマトリックス(上三角行列 R)を返す.
  • ピボット操作(列の入れ換え)をした場合は整数ベクトル colに Qの 基底ベクトルが入る.
  • colは予め make_imatrix()等によって確保されていなければならない.
Parameters
xxQR分解を行う2次元行列.
colメモリを確保し,初期化しておく.Qの基底ベクトルが入る.
Returns
上三角行列 R

Definition at line 869 of file matrix.c.

870 {
871  int i, j, r, n, m, sz[2];
872  double s, t, u;
873  matrix nsq, isq, x, a;
874 
875  a.n = a.r = 0;
876  a.sz = NULL;
877  a.mx = NULL;
878  if (xx.mx==NULL || col.mx==NULL) return a;
879  if (xx.n!=2 || (xx.sz[1]!=col.sz[0])) return a;
880 
881  nsq.mx = isq.mx = x.mx = NULL;
882  n = xx.sz[0];
883  m = xx.sz[1];
884  sz[0] = sz[1] = m;
885  nsq = make_matrix(1, &m);
886  isq = make_matrix(1, &m);
887  x = make_matrix(xx.n, xx.sz);
888  a = make_matrix(xx.n, sz);
889  if (nsq.mx==NULL || isq.mx==NULL || x.mx==NULL || a.mx==NULL) {
890  free_matrix(&nsq);
891  free_matrix(&isq);
892  free_matrix(&x);
893  return a;
894  }
895 
896  for (i=0; i<xx.r; i++) x.mx[i] = xx.mx[i];
897 
898  for (i=1; i<=m; i++) {
899  for (s=0.0,j=1; j<=n; j++) s = s + Mx(x, j, i)*Mx(x, j, i);
900  Vt(nsq, i) = s;
901  Vt(isq, i) = ((Vt(nsq, i)!=0) ? Vt(nsq,i) : -1.0);
902  Vt(col, i) = i;
903  }
904 
905  for (r=1; r<=m; r++) {
906  if (r!=1) {
907  j = r; u = 0.0;
908  for (i=r; i<=m; i++) {
909  t = Vt(nsq,i)/Vt(isq,i);
910  if (t>u) { u = t; j = i; }
911  }
912  i = Vt(col,j); Vt(col,j) = Vt(col,r); Vt(col,r) = i;
913  t = Vt(nsq,j); Vt(nsq,j) = Vt(nsq,r); Vt(nsq,r) = t;
914  t = Vt(isq,j); Vt(isq,j) = Vt(isq,r); Vt(isq,r) = t;
915  for (i=1; i<=n; i++) {
916  t = Mx(x,i,j);
917  Mx(x,i,j) = Mx(x,i,r);
918  Mx(x,i,r) = t;
919  }
920  }
921 
922  for (u=0.0,i=r; i<=n; i++) u = u + Mx(x,i,r)*Mx(x,i,r);
923  u = sqrt(u);
924  if (Mx(x,r,r)<0.0) u = -u;
925  Mx(x, r, r) = Mx(x,r,r) + u;
926  t = 1.0/(Mx(x,r,r)*u);
927 
928  for (j=1; j<=r-1; j++) Mx(x,r,j)=0.0;
929  for (j=r+1; j<=m; j++) {
930  for (s=0.0,i=r; i<=n; i++) s = s + Mx(x,i,r)*Mx(x,i,j);
931  for (i=r; i<=n; i++) Mx(x,i,j) = Mx(x,i,j) - s*t*Mx(x,i,r);
932  }
933  Mx(x,r,r) = -u;
934  }
935 
936  for (i=1; i<=m; i++) {
937  for (j=1; j<=m; j++) Mx(a,i,j) = Mx(x,i,j);
938  }
939 
940  free_matrix(&nsq);
941  free_matrix(&isq);
942  free_matrix(&x);
943 
944  return a;
945 }
void free_matrix(matrix *a)
Definition: matrix.c:408
#define Mx(m, i, j)
2次元マトリックスの(i,j)要素を返す. 1から数える.
Definition: matrix.h:84
#define Vt(m, i)
1次元マトリックスの i番目の要素を返す.1から数える.
Definition: matrix.h:83

References free_matrix(), make_matrix(), imatrix::mx, matrix::mx, Mx, matrix::n, matrix::r, imatrix::sz, matrix::sz, and Vt.

Referenced by minimum2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ex_vector()

vector ex_vector ( vector  a,
vector  b 
)

vector ex_vector(vector a, vector b)

ベクトルの外積. 実数ベクトル a,b の外積ベクトルを計算し,それを返す.

Parameters
a外積を計算するベクトル.
b外積を計算するベクトル.
Returns
外積ベクトル.

Definition at line 167 of file matrix.c.

168 {
169  vector c;
170 
171  c.x = a.y*b.z - a.z*b.y;
172  c.y = a.z*b.x - a.x*b.z;
173  c.z = a.x*b.y - a.y*b.x;
174  c.n = sqrt(c.x*c.x+c.y*c.y+c.z*c.z);
175  return c;
176 }
Definition: matrix.h:29
double z
z方向成分
Definition: matrix.h:32
double y
y方向成分
Definition: matrix.h:31
double n
ベクトルの大きさ
Definition: matrix.h:33
double x
x方向成分
Definition: matrix.h:30

References vector::n, vector::x, vector::y, and vector::z.

◆ f2ivector()

ivector f2ivector ( vector  a)

ivector f2ivector(vector a)

実数ベクトル a から整数ベクトルをつくり出し,それを返す. 各要素は四捨五入される.

Parameters
a変換する実数ベクトル.
Returns
変換された整数ベクトル.

Definition at line 125 of file matrix.c.

126 {
127  ivector c;
128 
129  c.x = (int)(a.x + 0.5);
130  c.y = (int)(a.y + 0.5);
131  c.z = (int)(a.z + 0.5);
132  c.n = sqrt((double)c.x*c.x + c.y*c.y + c.z*c.z);
133  return c;
134 }
Definition: matrix.h:21
int y
y方向成分
Definition: matrix.h:23
int z
z方向成分
Definition: matrix.h:24
int x
x方向成分
Definition: matrix.h:22
double n
ベクトルの大きさ
Definition: matrix.h:25

References ivector::n, ivector::x, vector::x, ivector::y, vector::y, ivector::z, and vector::z.

◆ free_imatrix()

void free_imatrix ( imatrix a)

void free_imatrix(imatrix* a)

整数マトリックスのバッファ部を開放する.

Parameters
a開放するバッファ部を持った整数マトリックスへのポインタ.

Definition at line 425 of file matrix.c.

426 {
427  if(a->sz!=NULL) free(a->sz);
428  if(a->mx!=NULL) free(a->mx);
429  a->sz = NULL;
430  a->mx = NULL;
431  a->n = a->r = 0;
432 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

Referenced by minimum2().

Here is the caller graph for this function:

◆ free_matrix()

void free_matrix ( matrix a)

void free_matrix(matrix* a)

マトリックスのバッファ部を開放をする.

Parameters
a開放するバッファ部を持ったマトリックスへのポインタ.

Definition at line 408 of file matrix.c.

409 {
410  if(a->sz!=NULL) free(a->sz);
411  if(a->mx!=NULL) free(a->mx);
412  a->sz = NULL;
413  a->mx = NULL;
414  a->n = a->r = 0;
415 }

References matrix::mx, matrix::n, matrix::r, and matrix::sz.

Referenced by decompQR(), and minimum2().

Here is the caller graph for this function:

◆ get_imatrix()

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 488 of file matrix.c.

489 {
490  int m, d;
491  int* args;
492  va_list argsptr;
493 
494  if (mtx.n<1) return NULL;
495  args = (int*)malloc(mtx.n*sizeof(int));
496  if (args==NULL) return NULL;
497 
498  va_start(argsptr, mtx);
499  for (m=0; m<mtx.n; m++) {
500  args[m] = (int)va_arg(argsptr, int);
501  }
502  va_end(argsptr);
503 
504  int dx = args[0] - 1;
505  for (d=1; d<mtx.n; d++) dx = dx*mtx.sz[d] + args[d] - 1;
506  free(args);
507 
508  if (dx>=mtx.r || dx<0) return NULL;
509  return &mtx.mx[dx];
510 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

◆ get_matrix()

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 449 of file matrix.c.

450 {
451  int m, d;
452  int* args;
453  va_list argsptr;
454 
455  if (mtx.n<1) return NULL;
456  args = (int*)malloc(mtx.n*sizeof(int));
457  if (args==NULL) return NULL;
458 
459  va_start(argsptr, mtx);
460  for (m=0; m<mtx.n; m++) {
461  args[m] = (int)va_arg(argsptr, int);
462  }
463  va_end(argsptr);
464 
465  int dx = args[0] - 1;
466  for (d=1; d<mtx.n; d++) dx = dx*mtx.sz[d] + args[d] - 1;
467  free(args);
468 
469  if (dx>=mtx.r || dx<0) return NULL;
470  return &mtx.mx[dx];
471 }

References matrix::mx, matrix::n, matrix::r, and matrix::sz.

◆ i2vector()

vector i2vector ( ivector  a)

vector i2vector(ivector a)

整数ベクトル a から実数ベクトルをつくり出し,それを返す.

Parameters
a変換する整数ベクトル.
Returns
変換された実数ベクトル.

Definition at line 145 of file matrix.c.

146 {
147  vector c;
148 
149  c.x = (double)a.x;
150  c.y = (double)a.y;
151  c.z = (double)a.z;
152  c.n = sqrt(c.x*c.x + c.y*c.y + c.z*c.z);
153  return c;
154 }

References vector::n, ivector::x, vector::x, ivector::y, vector::y, ivector::z, and vector::z.

◆ invrU_matrix()

matrix invrU_matrix ( matrix  x)

matrix invrU_matrix(matrix x)

2次元の上三角行列 xの逆行列を求める.

Parameters
x操作対象行列(上三角行列).
Returns
xの逆行列.

Definition at line 1045 of file matrix.c.

1046 {
1047  int i, j, k, n;
1048  double t, u, det;
1049  matrix a;
1050 
1051  a.n = a.r = 0;
1052  a.sz = NULL;
1053  a.mx = NULL;
1054  if (x.mx==NULL) return a;
1055  if (x.sz[0]!=x.sz[1]) return a;
1056 
1057  n = x.sz[0]; // 上三角行列のチェック
1058  for (j=1; j<n; j++) {
1059  for (i=j+1; i<=n; i++) {
1060  if (Mx(x,i,j) != 0.0) return a;
1061  }
1062  }
1063 
1064  det = 1.0;
1065  a = make_matrix(x.n, x.sz);
1066  if (a.mx==NULL) return a;
1067 
1068  for (i=0; i<a.r; i++) a.mx[i] = x.mx[i];
1069 
1070  for (k=1; k<=n; k++) {
1071  t = Mx(a,k,k);
1072  det = det*t;
1073  for (i=1; i<=n; i++) Mx(a,i,k) = Mx(a,i,k)/t;
1074  Mx(a,k,k) = 1.0/t;
1075 
1076  for (j=1; j<=n; j++) {
1077  if (j!=k) {
1078  u = Mx(a,k,j);
1079  for (i=1; i<=n; i++) {
1080  if (i!=k) Mx(a,i,j) = Mx(a,i,j) - Mx(a,i,k)*u;
1081  else Mx(a,i,j) = - u/t;
1082  }
1083  }
1084  }
1085  }
1086  return a;
1087 }

References make_matrix(), matrix::mx, Mx, matrix::n, matrix::r, and matrix::sz.

Referenced by minimum2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ make_imatrix()

imatrix make_imatrix ( int  n,
int *  sz 
)

imatrix make_imatrix(int n, int* sz)

任意(n)次元の整数マトリックスのバッファ部をつくり出す. 要素自体は 0に初期化される.

Parameters
nマトリックスの次元数.
szsz[0]〜sz[n-1]: 各次元の要素数.
Returns
作成された整数マトリックス.

Definition at line 371 of file matrix.c.

372 {
373  int i, s;
374  imatrix a;
375 
376  a.n = a.r = 0;
377  a.sz = (int*)malloc(n*sizeof(int));
378  if (a.sz==NULL) {
379  a.mx = NULL;
380  return a;
381  }
382  for (s=1,i=0; i<n; i++) {
383  a.sz[i] = sz[i];
384  s = s*sz[i];
385  }
386 
387  a.mx = (int*)malloc(s*sizeof(int));
388  if (a.mx==NULL) {
389  free(a.sz);
390  a.sz = NULL;
391  return a;
392  }
393 
394  a.n = n;
395  a.r = s;
396  for (i=0; i<s; i++) a.mx[i] = 0;
397  return a;
398 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

Referenced by add_imatrix(), minimum2(), mlt_imatrix(), and sub_imatrix().

Here is the caller graph for this function:

◆ make_imatrix1()

imatrix make_imatrix1 ( int  n)

imatrix make_imatrix1(int n)

1次元の整数行列のバッファ部をつくり出す. 要素自体は 0に初期化される.

Parameters
n1次元行列の大きさ.
Returns
作成された1次元整数行列

Definition at line 222 of file matrix.c.

223 {
224  int i;
225  imatrix a;
226 
227  a.n = a.r = 0;
228  a.mx = NULL;
229  a.sz = (int*)malloc(sizeof(int));
230  if (a.sz==NULL) return a;
231  a.sz[0] = n;
232 
233  a.mx = (int*)malloc(n*sizeof(int));
234  if (a.mx==NULL) {
235  free(a.sz);
236  a.sz = NULL;
237  return a;
238  }
239 
240  a.n = 1;
241  a.r = n;
242  for (i=0; i<n; i++) a.mx[i] = 0;
243  return a;
244 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

◆ make_imatrix2()

imatrix make_imatrix2 ( int  n,
int  m 
)

imatrix make_imatrix2(int n, int m)

2次元の整数行列のバッファ部をつくり出す. 要素自体は 0に初期化される.

Parameters
n2次元行列の行の数.
m2次元行列の列の数.
Returns
作成された2次元整数行列

Definition at line 294 of file matrix.c.

295 {
296  int i, s;
297  imatrix a;
298 
299  a.n = a.r = 0;
300  a.mx = NULL;
301  a.sz = (int*)malloc(2*sizeof(int));
302  if (a.sz==NULL) return a;
303  a.sz[0] = n;
304  a.sz[1] = m;
305  s = n*m;
306 
307  a.mx = (int*)malloc(s*sizeof(int));
308  if (a.mx==NULL) {
309  free(a.sz);
310  a.sz = NULL;
311  return a;
312  }
313 
314  a.n = 2;
315  a.r = s;
316  for (i=0; i<s; i++) a.mx[i] = 0;
317  return a;
318 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

◆ make_matrix()

matrix make_matrix ( int  n,
int *  sz 
)

matrix make_matrix(int n, int* sz)

任意(n)次元の実数マトリックスのバッファ部をつくり出す. 要素自体は 0.0に初期化される.

Parameters
nマトリックスの次元数.
szsz[0]〜sz[n-1]: 各次元の要素数.
Returns
作成されたマトリックス.

Definition at line 331 of file matrix.c.

332 {
333  int i, s;
334  matrix a;
335 
336  a.n = a.r = 0;
337  a.sz = (int*)malloc(n*sizeof(int));
338  if (a.sz==NULL) {
339  a.mx = NULL;
340  return a;
341  }
342  for (s=1,i=0; i<n; i++) {
343  a.sz[i] = sz[i];
344  s = s*sz[i];
345  }
346 
347  a.mx = (double*)malloc(s*sizeof(double));
348  if (a.mx==NULL) {
349  free(a.sz);
350  a.sz = NULL;
351  return a;
352  }
353 
354  a.n = n;
355  a.r = s;
356  for (i=0; i<s; i++) a.mx[i] = 0.0;
357  return a;
358 }

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().

Here is the caller graph for this function:

◆ make_matrix1()

matrix make_matrix1 ( int  n)

matrix make_matrix1(int n)

1次元の実数行列のバッファ部をつくり出す. 要素自体は 0.0に初期化される.

Parameters
n1次元行列の大きさ.
Returns
作成された1次元行列

Definition at line 188 of file matrix.c.

189 {
190  int i;
191  matrix a;
192 
193  a.n = a.r = 0;
194  a.mx = NULL;
195  a.sz = (int*)malloc(sizeof(int));
196  if (a.sz==NULL) return a;
197  a.sz[0] = n;
198 
199  a.mx = (double*)malloc(n*sizeof(double));
200  if (a.mx==NULL) {
201  free(a.sz);
202  a.sz = NULL;
203  return a;
204  }
205 
206  a.n = 1;
207  a.r = n;
208  for (i=0; i<n; i++) a.mx[i] = 0.0;
209  return a;
210 }

References matrix::mx, matrix::n, matrix::r, and matrix::sz.

◆ make_matrix2()

matrix make_matrix2 ( int  n,
int  m 
)

matrix make_matrix2(int n, int m)

2次元の実数行列のバッファ部をつくり出す. 要素自体は 0.0に初期化される.

Parameters
n2次元行列の行の数.
m2次元行列の列の数.
Returns
作成された2次元行列

Definition at line 257 of file matrix.c.

258 {
259  int i, s;
260  matrix a;
261 
262  a.n = a.r = 0;
263  a.mx = NULL;
264  a.sz = (int*)malloc(2*sizeof(int));
265  if (a.sz==NULL) return a;
266  a.sz[0] = n;
267  a.sz[1] = m;
268  s = n*m;
269 
270  a.mx = (double*)malloc(s*sizeof(double));
271  if (a.mx==NULL) {
272  free(a.sz);
273  a.sz = NULL;
274  return a;
275  }
276 
277  a.n = 2;
278  a.r = s;
279  for (i=0; i<s; i++) a.mx[i] = 0.0;
280  return a;
281 }

References matrix::mx, matrix::n, matrix::r, and matrix::sz.

◆ minimum2()

matrix minimum2 ( matrix  y,
matrix  x 
)

matrix minimum2(matrix y, matrix x)

最小2乗法で方程式の近似解を解き,結果を返す. ただし, x,yは2次元行列のみ.

Parameters
y連立方程式の結果の行列 (例を見よ)
x連立方程式の変数の行列 (例を見よ)
Returns
連立方程式の係数の行列 (例を見よ)
以下の場合 A = minimum2(Y, X) で解く(a1,a2を求める).
Y = X・A
y1 = x11*a1 + x12*a2
y2 = x21*a1 + x22*a2
y3 = x31*a1 + x32*a2

Definition at line 967 of file matrix.c.

968 {
969  int i, m;
970  imatrix cl;
971  matrix rx, rt, rr, aa, bb, cc;
972 
973  cc.n = cc.r = 0;
974  cc.sz = NULL;
975  cc.mx = NULL;
976  if (y.mx==NULL || x.mx==NULL) return cc;
977  if (y.sz[0]!=x.sz[0]) return cc;
978 
979  //n = x.sz[0];
980  m = x.sz[1]; /* n×m 行列 n>=m */
981  cl = make_imatrix(1, &m);
982  if (cl.mx==NULL) return cc;
983  cc = make_matrix (1, &m);
984  if (cc.mx==NULL) {free_imatrix(&cl); return cc;}
985 
986  rx = decompQR(x, cl);
987  rr = invrU_matrix(rx),
988  rt = mlt_matrix(rr, trans_matrix(rr));
989  aa = mlt_matrix(trans_matrix(x), y),
990  bb = mlt_matrix(rt, aa);
991 
992  if (bb.mx!=NULL) for (i=0; i<m; i++) cc.mx[cl.mx[i]-1] = bb.mx[i];
993 
994  free_imatrix(&cl);
995  free_matrix(&rx);
996  free_matrix(&rr);
997  free_matrix(&rt);
998  free_matrix(&aa);
999  free_matrix(&bb);
1000 
1001  return cc;
1002 }
void free_imatrix(imatrix *a)
Definition: matrix.c:425
matrix decompQR(matrix xx, imatrix col)
Definition: matrix.c:869
matrix mlt_matrix(matrix a, matrix b)
Definition: matrix.c:675
matrix invrU_matrix(matrix x)
Definition: matrix.c:1045
matrix trans_matrix(matrix a)
Definition: matrix.c:1013

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().

Here is the call graph for this function:

◆ mlt_imatrix()

imatrix mlt_imatrix ( imatrix  a,
imatrix  b 
)

imatrix mlt_imatrix(imatrix a, imatrix b)

マトリックスのかけ算. 整数マトリックス a, bをかけ算して,結果の整数マトリックスを返す.

Parameters
aかけられるマトリックス.
bかけるマトリックス.
Returns
かけ算結果の整数マトリックス.

Definition at line 751 of file matrix.c.

752 {
753  int i, j, k, n, ii, aa, bb, st;
754  int *sz, *sa, *sb, *sc, *cx;
755  imatrix c;
756 
757  c.n = c.r = 0;
758  c.sz = NULL;
759  c.mx = NULL;
760  if (a.mx==NULL || b.mx==NULL) return c;
761  if (a.sz[a.n-1]!=b.sz[0]) return c;
762 
763  n = a.n + b.n - 2;
764  sz = (int*)malloc(n*sizeof(int));
765  if (sz==NULL) return c;
766  sa = (int*)malloc(a.n*sizeof(int));
767  if (sa==NULL) {free(sz); return c;}
768  sb = (int*)malloc(b.n*sizeof(int));
769  if (sb==NULL) {free(sz); free(sa); return c;}
770  sc = (int*)malloc(n*sizeof(int));
771  if (sc==NULL) {free(sz); free(sa); free(sb); return c;}
772  cx = (int*)malloc(n*sizeof(int));
773  if (cx==NULL) {free(sz); free(sa); free(sb); free(sc); return c;}
774 
775  memset(sz, 0, n*sizeof(int));
776  memset(sa, 0, a.n*sizeof(int));
777  memset(sb, 0, b.n*sizeof(int));
778  memset(sc, 0, n*sizeof(int));
779  memset(cx, 0, n*sizeof(int));
780 
781  for (i=0; i<a.n-1; i++) sz[i] = a.sz[i];
782  for (i=1; i<b.n; i++) sz[a.n-2+i] = b.sz[i];
783 
784  sa[a.n-1] = sb[b.n-1] = sc[n-1] = 1;
785  for (i=a.n-2; i>=0; i--) sa[i] = sa[i+1]*a.sz[i+1];
786  for (i=b.n-2; i>=0; i--) sb[i] = sb[i+1]*b.sz[i+1];
787  for (i=n-2; i>=0; i--) sc[i] = sc[i+1]*sz[i+1];
788 
789  c = make_imatrix(n, sz);
790 
791  for (i=0; i<c.r; i++) {
792  ii = i;
793  for (j=0; j<c.n; j++) {
794  cx[j] = ii / sc[j];
795  ii = ii % sc[j];
796  }
797  aa = bb = 0;
798  for (j=0; j<a.n-1; j++) aa = aa + sa[j]*cx[j];
799  for (j=1; j<b.n; j++) bb = bb + sb[j]*cx[j+a.n-2];
800 
801  st = 0;
802  for (k=0; k<b.sz[0]; k++) st = st + a.mx[k+aa]*b.mx[bb+sb[0]*k];
803  c.mx[i] = st;
804  }
805 
806  free(sz);
807  free(sa);
808  free(sb);
809  free(sc);
810  free(cx);
811 
812  return c;
813 }

References make_imatrix(), imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

Here is the call graph for this function:

◆ mlt_matrix()

matrix mlt_matrix ( matrix  a,
matrix  b 
)

matrix mlt_matrix(matrix a, matrix b)

マトリックスのかけ算. 実数マトリックス a, bをかけ算して,結果の実数マトリックスを返す.

Parameters
aかけられるマトリックス.
bかけるマトリックス.
Returns
かけ算結果のマトリックス.

Definition at line 675 of file matrix.c.

676 {
677  int i, j, k, n, ii, aa, bb;
678  int *sz, *sa, *sb, *sc, *cx;
679  double st;
680  matrix c;
681 
682  c.n = c.r = 0;
683  c.sz = NULL;
684  c.mx = NULL;
685  if (a.mx==NULL || b.mx==NULL) return c;
686  if (a.sz[a.n-1]!=b.sz[0]) return c;
687 
688  n = a.n + b.n - 2;
689  sz = (int*)malloc(n*sizeof(int));
690  if (sz==NULL) return c;
691  sa = (int*)malloc(a.n*sizeof(int));
692  if (sa==NULL) {free(sz); return c;}
693  sb = (int*)malloc(b.n*sizeof(int));
694  if (sb==NULL) {free(sz); free(sa); return c;}
695  sc = (int*)malloc(n*sizeof(int));
696  if (sc==NULL) {free(sz); free(sa); free(sb); return c;}
697  cx = (int*)malloc(n*sizeof(int));
698  if (cx==NULL) {free(sz); free(sa); free(sb); free(sc); return c;}
699 
700  memset(sz, 0, n*sizeof(int));
701  memset(sa, 0, a.n*sizeof(int));
702  memset(sb, 0, b.n*sizeof(int));
703  memset(sc, 0, n*sizeof(int));
704  memset(cx, 0, n*sizeof(int));
705 
706  for (i=0; i<a.n-1; i++) sz[i] = a.sz[i];
707  for (i=1; i<b.n; i++) sz[a.n-2+i] = b.sz[i];
708 
709  sa[a.n-1] = sb[b.n-1] = sc[n-1] = 1;
710  for (i=a.n-2; i>=0; i--) sa[i] = sa[i+1]*a.sz[i+1];
711  for (i=b.n-2; i>=0; i--) sb[i] = sb[i+1]*b.sz[i+1];
712  for (i=n-2; i>=0; i--) sc[i] = sc[i+1]*sz[i+1];
713 
714  c = make_matrix(n, sz);
715 
716  for (i=0; i<c.r; i++) {
717  ii = i;
718  for (j=0; j<c.n; j++) {
719  cx[j] = ii / sc[j];
720  ii = ii % sc[j];
721  }
722  aa = bb = 0;
723  for (j=0; j<a.n-1; j++) aa = aa + sa[j]*cx[j];
724  for (j=1; j<b.n; j++) bb = bb + sb[j]*cx[j+a.n-2];
725 
726  st = 0.0;
727  for (k=0; k<b.sz[0]; k++) st = st + a.mx[k+aa]*b.mx[bb+sb[0]*k];
728  c.mx[i] = st;
729  }
730 
731  free(sz);
732  free(sa);
733  free(sb);
734  free(sc);
735  free(cx);
736 
737  return c;
738 }

References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.

Referenced by minimum2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ print_imatrix()

void print_imatrix ( FILE *  fp,
imatrix  a 
)

void print_imatrix(FILE* fp, imatrix a)

整数マトリックスの要素を標準出力に書き出す.

Parameters
fp出力先のファイル記述子
aプリントするマトリックス.

Definition at line 843 of file matrix.c.

844 {
845  int i;
846  for (i=0; i<a.r; i++) {
847  fprintf(fp, " %6d", a.mx[i]);
848  if ((i+1)%a.sz[a.n-1]==0) fprintf(fp, "\n");
849  }
850  fprintf(stdout,"\n");
851 }

References imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

◆ print_matrix()

void print_matrix ( FILE *  fp,
matrix  a 
)

void print_matrix(FILE* fp, matrix a)

実数マトリックスの要素を標準出力に書き出す.

Parameters
fp出力先のファイル記述子
aプリントするマトリックス.

Definition at line 824 of file matrix.c.

825 {
826  int i;
827  for (i=0; i<a.r; i++) {
828  fprintf(fp, " %15lf", a.mx[i]);
829  if ((i+1)%a.sz[a.n-1]==0) fprintf(fp, "\n");
830  }
831 // fprintf(stdout,"\n");
832 }

References matrix::mx, matrix::n, matrix::r, and matrix::sz.

◆ set_ivector()

ivector set_ivector ( int  x,
int  y,
int  z 
)

ivector set_ivector(int x, int y, int z)

整数ベクトルの作成.x方向成分, y方向成分, z方向成分から整数ベクトルを作り,それを返す.

Parameters
xベクトルの x成分.
yベクトルの y成分.
zベクトルの z成分.
Returns
作成された整数ベクトル.

Definition at line 104 of file matrix.c.

105 {
106  ivector c;
107 
108  c.x = x;
109  c.y = y;
110  c.z = z;
111  c.n = sqrt((double)x*x + y*y + z*z);
112  return c;
113 }

References ivector::n, ivector::x, ivector::y, and ivector::z.

◆ set_vector()

vector set_vector ( double  x,
double  y,
double  z 
)

vector set_vector(double x, double y, double z)

ベクトルの作成.x方向成分, y方向成分, z方向成分から実数ベクトルを作り,それを返す.

Parameters
xベクトルの x成分.
yベクトルの y成分.
zベクトルの z成分.
Returns
作成されたベクトル.

Definition at line 82 of file matrix.c.

83 {
84  vector c;
85 
86  c.x = x;
87  c.y = y;
88  c.z = z;
89  c.n = sqrt(x*x + y*y + z*z);
90  return c;
91 }

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().

Here is the caller graph for this function:

◆ sub_imatrix()

imatrix sub_imatrix ( imatrix  a,
imatrix  b 
)

imatrix sub_imatrix(imatrix a, imatrix b)

マトリックスの引き算. 整数マトリックス a, bを引き算して,結果の整数マトリックスを返す.

Parameters
a引かれるマトリックス.
b引くマトリックス.
Returns
引き算結果の整数マトリックス.

Definition at line 648 of file matrix.c.

649 {
650  int i;
651  imatrix c;
652 
653  c.n = c.r = 0;
654  c.sz = NULL;
655  c.mx = NULL;
656  if (a.mx==NULL || b.mx==NULL) return c;
657  if (a.r != b.r) return c;
658 
659  c = make_imatrix(a.n, a.sz);
660  for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] - b.mx[i];
661  return c;
662 }

References make_imatrix(), imatrix::mx, imatrix::n, imatrix::r, and imatrix::sz.

Here is the call graph for this function:

◆ sub_matrix()

matrix sub_matrix ( matrix  a,
matrix  b 
)

matrix sub_matrix(matrix a, matrix b)

マトリックスの引き算. 実数マトリックス a, bを引き算して,結果の実数マトリックスを返す.

Parameters
a引かれるマトリックス.
b引くマトリックス.
Returns
引き算結果のマトリックス.

Definition at line 621 of file matrix.c.

622 {
623  int i;
624  matrix c;
625 
626  c.n = c.r = 0;
627  c.sz = NULL;
628  c.mx = NULL;
629  if (a.mx==NULL || b.mx==NULL) return c;
630  if (a.r != b.r) return c;
631 
632  c = make_matrix(a.n, a.sz);
633  for (i=0; i<c.r; i++) c.mx[i] = a.mx[i] - b.mx[i];
634  return c;
635 }

References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.

Here is the call graph for this function:

◆ trans_matrix()

matrix trans_matrix ( matrix  a)

matrix trans_matrix(matrix a)

2次元行列 aの転置行列を返す.

Parameters
a操作対象行列.
Returns
aの転置行列.

Definition at line 1013 of file matrix.c.

1014 {
1015  int i, j, k;
1016  matrix c;
1017  int sz[2];
1018 
1019  c.n = c.r = 0;
1020  c.sz = NULL;
1021  c.mx = NULL;
1022  if (a.mx==NULL || a.n!=2) return c;
1023 
1024  sz[0] = a.sz[1];
1025  sz[1] = a.sz[0];
1026  c = make_matrix(a.n, sz);
1027 
1028  for (k=0; k<c.r; k++) {
1029  i = k/a.sz[1];
1030  j = k%a.sz[1];
1031  c.mx[j*sz[1]+i] = a.mx[k];
1032  }
1033  return c;
1034 }

References make_matrix(), matrix::mx, matrix::n, matrix::r, and matrix::sz.

Referenced by minimum2().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ unit_ivector()

vector unit_ivector ( ivector  a)

vector unit_ivector(ivector a)

整数ベクトル aの単位ベクトルを返す.返されるベクトルは実数ベクトル.

Parameters
a対象整数ベクトル.
Returns
a の単位ベクトル(実数ベクトル).

Definition at line 49 of file matrix.c.

50 {
51  vector c;
52  double r;
53 
54  r = a.x*a.x+a.y*a.y+a.z*a.z;
55  if (Xabs(r)<EPS*EPS) {
56  c.x = 0.0;
57  c.y = 0.0;
58  c.z = 0.0;
59  c.n = 1.0;
60  }
61  else {
62  r = sqrt(r);
63  c.x = a.x/r;
64  c.y = a.y/r;
65  c.z = a.z/r;
66  c.n = 1.0;
67  }
68  return c;
69 }
#define Xabs(x)
Definition: common.h:257
double EPS
Definition: mt.c:9

References EPS, vector::n, ivector::x, vector::x, Xabs, ivector::y, vector::y, ivector::z, and vector::z.

◆ unit_vector()

vector unit_vector ( vector  a)

vector unit_vector(vector a)

ベクトル aの単位ベクトルを返す.

Parameters
a対象ベクトル.
Returns
a の単位ベクトル.

Definition at line 18 of file matrix.c.

19 {
20  vector c;
21  double r;
22 
23  r = a.x*a.x+a.y*a.y+a.z*a.z;
24  if (Xabs(r)<EPS*EPS) {
25  c.x = 0.0;
26  c.y = 0.0;
27  c.z = 0.0;
28  c.n = 1.0;
29  }
30  else {
31  r = sqrt(r);
32  c.x = a.x/r;
33  c.y = a.y/r;
34  c.z = a.z/r;
35  c.n = 1.0;
36  }
37  return c;
38 }

References EPS, vector::n, vector::x, Xabs, vector::y, and vector::z.

Referenced by circle3d(), pool(), torus(), vfNabra(), and vNabra().

Here is the caller graph for this function: