23 r = a.
x*a.
x+a.
y*a.
y+a.
z*a.
z;
54 r = a.
x*a.
x+a.
y*a.
y+a.
z*a.
z;
89 c.
n = sqrt(x*x + y*y + z*z);
111 c.
n = sqrt((
double)x*x + y*y + z*z);
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);
152 c.
n = sqrt(c.
x*c.
x + c.
y*c.
y + c.
z*c.
z);
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);
195 a.
sz = (
int*)malloc(
sizeof(
int));
196 if (a.
sz==NULL)
return a;
199 a.
mx = (
double*)malloc(n*
sizeof(
double));
208 for (i=0; i<n; i++) a.
mx[i] = 0.0;
229 a.
sz = (
int*)malloc(
sizeof(
int));
230 if (a.
sz==NULL)
return a;
233 a.
mx = (
int*)malloc(n*
sizeof(
int));
242 for (i=0; i<n; i++) a.
mx[i] = 0;
264 a.
sz = (
int*)malloc(2*
sizeof(
int));
265 if (a.
sz==NULL)
return a;
270 a.
mx = (
double*)malloc(s*
sizeof(
double));
279 for (i=0; i<s; i++) a.
mx[i] = 0.0;
301 a.
sz = (
int*)malloc(2*
sizeof(
int));
302 if (a.
sz==NULL)
return a;
307 a.
mx = (
int*)malloc(s*
sizeof(
int));
316 for (i=0; i<s; i++) a.
mx[i] = 0;
337 a.
sz = (
int*)malloc(n*
sizeof(
int));
342 for (s=1,i=0; i<n; i++) {
347 a.
mx = (
double*)malloc(s*
sizeof(
double));
356 for (i=0; i<s; i++) a.
mx[i] = 0.0;
377 a.
sz = (
int*)malloc(n*
sizeof(
int));
382 for (s=1,i=0; i<n; i++) {
387 a.
mx = (
int*)malloc(s*
sizeof(
int));
396 for (i=0; i<s; i++) a.
mx[i] = 0;
410 if(a->
sz!=NULL) free(a->
sz);
411 if(a->
mx!=NULL) free(a->
mx);
427 if(a->
sz!=NULL) free(a->
sz);
428 if(a->
mx!=NULL) free(a->
mx);
455 if (mtx.
n<1)
return NULL;
456 args = (
int*)malloc(mtx.
n*
sizeof(
int));
457 if (args==NULL)
return NULL;
459 va_start(argsptr, mtx);
460 for (m=0; m<mtx.
n; m++) {
461 args[m] = (int)va_arg(argsptr,
int);
465 int dx = args[0] - 1;
466 for (d=1; d<mtx.
n; d++) dx = dx*mtx.
sz[d] + args[d] - 1;
469 if (dx>=mtx.
r || dx<0)
return NULL;
494 if (mtx.
n<1)
return NULL;
495 args = (
int*)malloc(mtx.
n*
sizeof(
int));
496 if (args==NULL)
return NULL;
498 va_start(argsptr, mtx);
499 for (m=0; m<mtx.
n; m++) {
500 args[m] = (int)va_arg(argsptr,
int);
504 int dx = args[0] - 1;
505 for (d=1; d<mtx.
n; d++) dx = dx*mtx.
sz[d] + args[d] - 1;
508 if (dx>=mtx.
r || dx<0)
return NULL;
527 if (src.
mx==NULL || dst.
mx==NULL)
return;
528 if ((src.
r!=dst.
r)||(dst.
n!=dst.
n))
return;
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];
549 if (src.
mx==NULL || dst.
mx==NULL)
return;
550 if ((src.
r!=dst.
r)||(dst.
n!=dst.
n))
return;
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];
575 if (a.
mx==NULL || b.
mx==NULL)
return c;
576 if (a.
r != b.
r)
return c;
579 for (i=0; i<c.
r; i++) c.
mx[i] = a.
mx[i] + b.
mx[i];
602 if (a.
mx==NULL || b.
mx==NULL)
return c;
603 if (a.
r != b.
r)
return c;
606 for (i=0; i<c.
r; i++) c.
mx[i] = a.
mx[i] + b.
mx[i];
629 if (a.
mx==NULL || b.
mx==NULL)
return c;
630 if (a.
r != b.
r)
return c;
633 for (i=0; i<c.
r; i++) c.
mx[i] = a.
mx[i] - b.
mx[i];
656 if (a.
mx==NULL || b.
mx==NULL)
return c;
657 if (a.
r != b.
r)
return c;
660 for (i=0; i<c.
r; i++) c.
mx[i] = a.
mx[i] - b.
mx[i];
677 int i, j, k, n, ii, aa, bb;
678 int *sz, *sa, *sb, *sc, *cx;
685 if (a.
mx==NULL || b.
mx==NULL)
return c;
686 if (a.
sz[a.
n-1]!=b.
sz[0])
return c;
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;}
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));
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];
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];
716 for (i=0; i<c.
r; i++) {
718 for (j=0; j<c.
n; j++) {
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];
727 for (k=0; k<b.
sz[0]; k++) st = st + a.
mx[k+aa]*b.
mx[bb+sb[0]*k];
753 int i, j, k, n, ii, aa, bb, st;
754 int *sz, *sa, *sb, *sc, *cx;
760 if (a.
mx==NULL || b.
mx==NULL)
return c;
761 if (a.
sz[a.
n-1]!=b.
sz[0])
return c;
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;}
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));
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];
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];
791 for (i=0; i<c.
r; i++) {
793 for (j=0; j<c.
n; j++) {
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];
802 for (k=0; k<b.
sz[0]; k++) st = st + a.
mx[k+aa]*b.
mx[bb+sb[0]*k];
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");
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");
850 fprintf(stdout,
"\n");
871 int i, j, r, n, m, sz[2];
878 if (xx.
mx==NULL || col.
mx==NULL)
return a;
879 if (xx.
n!=2 || (xx.
sz[1]!=col.
sz[0]))
return a;
881 nsq.
mx = isq.
mx = x.
mx = NULL;
889 if (nsq.
mx==NULL || isq.
mx==NULL || x.
mx==NULL || a.
mx==NULL) {
896 for (i=0; i<xx.
r; i++) x.
mx[i] = xx.
mx[i];
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);
901 Vt(isq, i) = ((
Vt(nsq, i)!=0) ?
Vt(nsq,i) : -1.0);
905 for (r=1; r<=m; r++) {
908 for (i=r; i<=m; i++) {
909 t =
Vt(nsq,i)/
Vt(isq,i);
910 if (t>u) { u = t; j = i; }
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++) {
917 Mx(x,i,j) =
Mx(x,i,r);
922 for (u=0.0,i=r; i<=n; i++) u = u +
Mx(x,i,r)*
Mx(x,i,r);
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);
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);
936 for (i=1; i<=m; i++) {
937 for (j=1; j<=m; j++)
Mx(a,i,j) =
Mx(x,i,j);
971 matrix rx, rt, rr, aa, bb, cc;
976 if (y.
mx==NULL || x.
mx==NULL)
return cc;
977 if (y.
sz[0]!=x.
sz[0])
return cc;
982 if (cl.
mx==NULL)
return cc;
992 if (bb.
mx!=NULL)
for (i=0; i<m; i++) cc.
mx[cl.
mx[i]-1] = bb.
mx[i];
1022 if (a.
mx==NULL || a.
n!=2)
return c;
1028 for (k=0; k<c.
r; k++) {
1031 c.
mx[j*sz[1]+i] = a.
mx[k];
1054 if (x.
mx==NULL)
return a;
1055 if (x.
sz[0]!=x.
sz[1])
return a;
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;
1066 if (a.
mx==NULL)
return a;
1068 for (i=0; i<a.
r; i++) a.
mx[i] = x.
mx[i];
1070 for (k=1; k<=n; k++) {
1073 for (i=1; i<=n; i++)
Mx(a,i,k) =
Mx(a,i,k)/t;
1076 for (j=1; j<=n; 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;
vector unit_vector(vector a)
void print_imatrix(FILE *fp, imatrix a)
void free_imatrix(imatrix *a)
matrix make_matrix1(int n)
vector unit_ivector(ivector a)
matrix add_matrix(matrix a, matrix b)
matrix sub_matrix(matrix a, matrix b)
matrix minimum2(matrix y, matrix x)
matrix make_matrix2(int n, int m)
ivector f2ivector(vector a)
ivector set_ivector(int x, int y, int z)
vector i2vector(ivector a)
vector ex_vector(vector a, vector b)
vector set_vector(double x, double y, double z)
double * get_matrix(matrix mtx,...)
void copy_imatrix(imatrix src, imatrix dst)
matrix decompQR(matrix xx, imatrix col)
imatrix sub_imatrix(imatrix a, imatrix b)
imatrix add_imatrix(imatrix a, imatrix b)
void free_matrix(matrix *a)
imatrix mlt_imatrix(imatrix a, imatrix b)
imatrix make_imatrix2(int n, int m)
matrix mlt_matrix(matrix a, matrix b)
matrix make_matrix(int n, int *sz)
void copy_matrix(matrix src, matrix dst)
matrix invrU_matrix(matrix x)
imatrix make_imatrix(int n, int *sz)
int * get_imatrix(imatrix mtx,...)
matrix trans_matrix(matrix a)
void print_matrix(FILE *fp, matrix a)
imatrix make_imatrix1(int n)
#define Mx(m, i, j)
2次元マトリックスの(i,j)要素を返す. 1から数える.
#define Vt(m, i)
1次元マトリックスの i番目の要素を返す.1から数える.
int * sz
各次元の要素数 sz[0]?sz[n-1]
int r
全要素数 sz[0]+sz[1]+...+sz[n-1]
double * mx
要素 mx[0]?mx[r-1]
int * sz
各次元の要素数 sz[0]?sz[n-1]
int r
全要素数 sz[0]+sz[1]+...+sz[n-1]