1#ifndef __JBXL_CPP_MATRIX_H_
2#define __JBXL_CPP_MATRIX_H_
28template <
typename T=
double>
class Matrix
46 void getm(
int nn,
int* size);
78 sz = (
int*)malloc(n*
sizeof(
int));
80 memset(sz, 0, n*
sizeof(
int));
84 va_start(argsptr, nn);
86 for (
int i=0; i<n; i++) {
87 sz[i] = (int)va_arg(argsptr,
int);
92 mx = (T*)malloc(r*
sizeof(T));
98 for (
int i=0; i<r; i++) mx[i] = (T)0;
115 if (sz!=NULL) ::free(sz);
116 if (mx!=NULL) ::free(mx);
123 sz = (
int*)malloc(n*
sizeof(
int));
124 if (sz==NULL)
return;
128 va_start(argsptr, nn);
130 for (
int i=0; i<n; i++) {
131 sz[i] = (int)va_arg(argsptr,
int);
136 mx = (T*)malloc(r*
sizeof(T));
142 for (
int i=0; i<r; i++) mx[i] = (T)0;
159 if (size==NULL)
return;
160 if (sz!=NULL) ::free(sz);
161 if (mx!=NULL) ::free(mx);
168 sz = (
int*)malloc(n*
sizeof(
int));
169 if (sz==NULL)
return;
172 for (
int i=0; i<n; i++) {
177 mx = (T*)malloc(r*
sizeof(T));
183 for (
int i=0; i<r; i++) mx[i] = (T)0;
209 args = (
int*)malloc(n*
sizeof(
int));
210 if (args==NULL)
return err;
212 va_start(argsptr, i);
214 for (
int m=1; m<n; m++) {
215 args[m] = (int)va_arg(argsptr,
int);
219 int dx = args[0] - 1;
220 for (
int d=1; d<n; d++) dx = dx*sz[d] + args[d] - 1;
223 if (dx>=r || dx<0)
return err;
238 for (
int i=0; i<a.
r; i++) {
239 fprintf(fp,
" %15lf", (
double)a.
mx[i]);
240 if ((i+1)%a.
sz[a.
n-1]==0) fprintf(fp,
"\n");
256 if (a.
mx!=NULL && mtx.
r==a.
r) {
257 for (
int i=0; i<a.
r; i++) {
287 int i, j, k, n, ii, aa, bb;
288 int *sz, *sa, *sb, *sc, *cx;
292 if (a.
mx==NULL || b.
mx==NULL)
return c;
293 if (a.
sz[a.
n-1]!=b.
sz[0])
return c;
296 sz = (
int*)malloc(n*
sizeof(
int));
297 if (sz==NULL)
return c;
298 sa = (
int*)malloc(a.
n*
sizeof(
int));
299 if (sa==NULL) {free(sz);
return c;}
300 sb = (
int*)malloc(b.
n*
sizeof(
int));
301 if (sb==NULL) {free(sz); free(sa);
return c;}
302 sc = (
int*)malloc(n*
sizeof(
int));
303 if (sc==NULL) {free(sz); free(sa); free(sb);
return c;}
304 cx = (
int*)malloc(n*
sizeof(
int));
305 if (cx==NULL) {free(sz); free(sa); free(sb); free(sc);
return c;}
307 for (i=0; i<a.
n-1; i++) sz[i] = a.
sz[i];
308 for (i=1; i<b.
n; i++) sz[a.
n-2+i] = b.
sz[i];
310 sa[a.
n-1] = sb[b.
n-1] = sc[n-1] = 1;
311 for (i=a.
n-2; i>=0; i--) sa[i] = sa[i+1]*a.
sz[i+1];
312 for (i=b.
n-2; i>=0; i--) sb[i] = sb[i+1]*b.
sz[i+1];
313 for (i=n-2; i>=0; i--) sc[i] = sc[i+1]*sz[i+1];
318 for (i=0; i<c.
r; i++) {
320 for (j=0; j<c.
n; j++) {
325 for (j=0; j<a.
n-1; j++) aa = aa + sa[j]*cx[j];
326 for (j=1; j<b.
n; j++) bb = bb + sb[j]*cx[j+a.
n-2];
329 for (k=0; k<b.
sz[0]; k++) st = st + a.
mx[k+aa]*b.
mx[bb+sb[0]*k];
367 if (c.
mx==NULL)
return vct;
381 if (c.
mx!=NULL)
for (
int i=0; i<a.
r; i++) c.
mx[i] = -a.
mx[i];
392 if (c.
mx!=NULL)
for (
int i=0; i<a.
r; i++) c.
mx[i] = a.
mx[i] + b.
mx[i];
403 if (c.
mx!=NULL)
for (
int i=0; i<a.
r; i++) c.
mx[i] = a.
mx[i] - b.
mx[i];
412 if (c.
mx!=NULL)
for (
int i=0; i<a.
r; i++) c.
mx[i] = (T)(d)*a.
mx[i];
421 if (c.
mx!=NULL)
for (
int i=0; i<a.
r; i++) c.
mx[i] = a.
mx[i]*(T)d;
430 if (c.
mx!=NULL)
for (
int i=0; i<a.
r; i++) c.
mx[i] = a.
mx[i]/(T)d;
438 for (
int i=0; i<v1.
r; i++)
if (v1.
mx[i]!=v2.
mx[i])
return false;
445 if (v1.
n!=v2.
n || v1.
r!=v2.
r)
return false;
446 for (
int i=0; i<v1.
n; i++)
if (v1.
sz[i]!=v2.
sz[i])
return false;
int * sz
各次元の要素数 sz[0] 〜 sz[n-1]
virtual ~Matrix()
関数にコピー渡しした場合に,関数内でディストラクトされても良い様に free() は使用しない.
void getm(int nn, int *size)
void free()
free() は手動で呼び出す.
int r
全要素数 sz[0]*sz[1]*...*sz[n-1]
Matrix< T > dup_Matrix(Matrix< T > a)
AffineTrans< T > operator*(AffineTrans< T > a, AffineTrans< T > b)
Matrix< T > operator+(const Matrix< T > a, const Matrix< T > b)
void print_Matrix(FILE *fp, Matrix< T > a)
bool isSameDimension(const Matrix< T > v1, const Matrix< T > v2)
Matrix< T > operator/(const Matrix< T > a, const R d)
bool operator==(const Matrix< T > v1, const Matrix< T > v2)
Matrix< T > operator-(const Matrix< T > a)