JunkBox_Lib++ (for Windows) 1.10.1
Loading...
Searching...
No Matches
Matrix++.h
Go to the documentation of this file.
1#ifndef __JBXL_CPP_MATRIX_H_
2#define __JBXL_CPP_MATRIX_H_
3
11#include "tools++.h"
12#include <math.h>
13#include "Vector.h"
14
15
16//
17namespace jbxl {
18
19
28template <typename T=double> class Matrix
29{
30public:
31 int n;
32 int r;
33 int d;
34 int* sz;
35 T* mx;
37
38public:
39 Matrix() { init();}
40 Matrix(int nn, ...);
41 virtual ~Matrix() {}
42
43 void init() { n = r = d = 0; err = (T)0; sz = NULL; mx = NULL;}
44 void init(int nn, ...);
45
46 void getm(int nn, int* size);
47 T& element(int i, ...);
48 void clear(T v=T(0)) { for(int i=0;i<r;i++) mx[i]=v;}
49 void dup(Matrix<T> a) { *this = dup_Matrix(a);}
50
52 void free() { if(sz!=NULL) ::free(sz); if(mx!=NULL) ::free(mx); init();}
53};
54
55
71template <typename T> Matrix<T>::Matrix(int nn, ...)
72{
73 r = 0;
74 n = nn;
75 d = 0;
76 err = (T)0;
77 mx = NULL;
78 sz = (int*)malloc(n*sizeof(int));
79 if (sz==NULL) return;
80 memset(sz, 0, n*sizeof(int));
81
82 va_list argsptr;
83
84 va_start(argsptr, nn);
85 r = 1;
86 for (int i=0; i<n; i++) {
87 sz[i] = (int)va_arg(argsptr, int);
88 r = r*sz[i];
89 }
90 va_end(argsptr);
91
92 mx = (T*)malloc(r*sizeof(T));
93 if (mx==NULL) {
94 ::free(sz);
95 sz = NULL;
96 return;
97 }
98 for (int i=0; i<r; i++) mx[i] = (T)0;
99
100 return;
101}
102
103
113template <typename T> void Matrix<T>::init(int nn, ...)
114{
115 if (sz!=NULL) ::free(sz);
116 if (mx!=NULL) ::free(mx);
117
118 r = 0;
119 n = nn;
120 d = 0;
121 err = (T)0;
122 mx = NULL;
123 sz = (int*)malloc(n*sizeof(int));
124 if (sz==NULL) return;
125
126 va_list argsptr;
127
128 va_start(argsptr, nn);
129 r = 1;
130 for (int i=0; i<n; i++) {
131 sz[i] = (int)va_arg(argsptr, int);
132 r = r*sz[i];
133 }
134 va_end(argsptr);
135
136 mx = (T*)malloc(r*sizeof(T));
137 if (mx==NULL) {
138 ::free(sz);
139 sz = NULL;
140 return;
141 }
142 for (int i=0; i<r; i++) mx[i] = (T)0;
143
144 return;
145}
146
147
157template <typename T> void Matrix<T>::getm(int nn, int* size)
158{
159 if (size==NULL) return;
160 if (sz!=NULL) ::free(sz);
161 if (mx!=NULL) ::free(mx);
162
163 r = 0;
164 n = nn;
165 d = 0;
166 err = (T)0;
167 mx = NULL;
168 sz = (int*)malloc(n*sizeof(int));
169 if (sz==NULL) return;
170
171 r = 1;
172 for (int i=0; i<n; i++) {
173 sz[i] = size[i];
174 r = r*sz[i];
175 }
176
177 mx = (T*)malloc(r*sizeof(T));
178 if (mx==NULL) {
179 ::free(sz);
180 sz = NULL;
181 return;
182 }
183 for (int i=0; i<r; i++) mx[i] = (T)0;
184
185 return;
186}
187
188
204template <typename T> T& Matrix<T>::element(int i, ...)
205{
206 int* args;
207 va_list argsptr;
208
209 args = (int*)malloc(n*sizeof(int));
210 if (args==NULL) return err;
211
212 va_start(argsptr, i);
213 args[0] = i;
214 for (int m=1; m<n; m++) {
215 args[m] = (int)va_arg(argsptr, int);
216 }
217 va_end(argsptr);
218
219 int dx = args[0] - 1;
220 for (int d=1; d<n; d++) dx = dx*sz[d] + args[d] - 1;
221 ::free(args);
222
223 if (dx>=r || dx<0) return err;
224 return mx[dx];
225}
226
227
236template <typename T> void print_Matrix(FILE* fp, Matrix<T> a)
237{
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");
241 }
242}
243
244
250template <typename T> Matrix<T> dup_Matrix(Matrix<T> a)
251{
252 Matrix<T> mtx;
253
254 if (a.sz!=NULL) {
255 mtx.getm(a.n, a.sz);
256 if (a.mx!=NULL && mtx.r==a.r) {
257 for (int i=0; i<a.r; i++) {
258 mtx.mx[i] = a.mx[i];
259 }
260 }
261 }
262
263 mtx.d = a.d;
264 mtx.err = (T)(1);
265 //
266 return mtx;
267}
268
269
270
272// オペレータ
273
285template <typename T> Matrix<T> operator * (const Matrix<T> a, const Matrix<T> b)
286{
287 int i, j, k, n, ii, aa, bb;
288 int *sz, *sa, *sb, *sc, *cx;
289 T st;
290 Matrix<T> c;
291
292 if (a.mx==NULL || b.mx==NULL) return c;
293 if (a.sz[a.n-1]!=b.sz[0]) return c;
294
295 n = a.n + b.n - 2;
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;}
306
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];
309
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];
314
315 c.getm(n, sz);
316
317 if (c.sz!=NULL) {
318 for (i=0; i<c.r; i++) {
319 ii = i;
320 for (j=0; j<c.n; j++) {
321 cx[j] = ii / sc[j];
322 ii = ii % sc[j];
323 }
324 aa = bb = 0;
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];
327
328 st = (T)0;
329 for (k=0; k<b.sz[0]; k++) st = st + a.mx[k+aa]*b.mx[bb+sb[0]*k];
330 c.mx[i] = st;
331 }
332 }
333
334 free(sz);
335 free(sa);
336 free(sb);
337 free(sc);
338 free(cx);
339
340 return c;
341}
342
343
355template <typename T> Vector<T> operator * (const Matrix<T> a, const Vector<T> v)
356{
357 Vector<T> vct(0.0, 0.0, 0.0, 0.0, -1.0);
358
359 Matrix<T> b(1, 3);
360 b.mx[0] = v.x;
361 b.mx[1] = v.y;
362 b.mx[2] = v.z;
363
364 Matrix<T> c = a*b;
365 b.free();
366
367 if (c.mx==NULL) return vct;
368 vct.x = c.mx[0];
369 vct.y = c.mx[1];
370 vct.z = c.mx[2];
371 vct.c = v.c;
372 c.free();
373
374 return vct;
375}
376
377
378template <typename T> inline Matrix<T> operator - (const Matrix<T> a)
379{
380 Matrix<T> c(a.n, a.sz);
381 if (c.mx!=NULL) for (int i=0; i<a.r; i++) c.mx[i] = -a.mx[i];
382 return c;
383}
384
385
386template <typename T> inline Matrix<T> operator + (const Matrix<T> a, const Matrix<T> b)
387{
388 Matrix<T> c;
389 if (!isSameDimension(a, b)) return c;
390
391 c.getm(a.n, a.sz);
392 if (c.mx!=NULL) for (int i=0; i<a.r; i++) c.mx[i] = a.mx[i] + b.mx[i];
393 return c;
394}
395
396
397template <typename T> inline Matrix<T> operator - (const Matrix<T> a, const Matrix<T> b)
398{
399 Matrix<T> c;
400 if (!isSameDimension(a, b)) return c;
401
402 c.getm(a.n, a.sz);
403 if (c.mx!=NULL) for (int i=0; i<a.r; i++) c.mx[i] = a.mx[i] - b.mx[i];
404 return c;
405}
406
407
408//
409template <typename T, typename R> inline Matrix<T> operator * (const R d, const Matrix<T> a)
410{
411 Matrix<T> c(a.n, a.sz);
412 if (c.mx!=NULL) for (int i=0; i<a.r; i++) c.mx[i] = (T)(d)*a.mx[i];
413 return c;
414}
415
416
417//
418template <typename T, typename R> inline Matrix<T> operator * (const Matrix<T> a, const R d)
419{
420 Matrix<T> c(a.n, a.sz);
421 if (c.mx!=NULL) for (int i=0; i<a.r; i++) c.mx[i] = a.mx[i]*(T)d;
422 return c;
423}
424
425
426//
427template <typename T, typename R> inline Matrix<T> operator / (const Matrix<T> a, const R d)
428{
429 Matrix<T> c(a.n, a.sz);
430 if (c.mx!=NULL) for (int i=0; i<a.r; i++) c.mx[i] = a.mx[i]/(T)d;
431 return c;
432}
433
434
435template <typename T> inline bool operator == (const Matrix<T> v1, const Matrix<T> v2)
436{
437 if (!isSameDimension(v1, v2)) return false;
438 for (int i=0; i<v1.r; i++) if (v1.mx[i]!=v2.mx[i]) return false;
439 return true;
440}
441
442
443template <typename T> inline bool isSameDimension(const Matrix<T> v1, const Matrix<T> v2)
444{
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;
447 return true;
448}
449
450
451} // namespace
452
453#endif
ベクトルライブラリ for C++
void init()
Definition Matrix++.h:43
void dup(Matrix< T > a)
Definition Matrix++.h:49
int * sz
各次元の要素数 sz[0] 〜 sz[n-1]
Definition Matrix++.h:34
virtual ~Matrix()
関数にコピー渡しした場合に,関数内でディストラクトされても良い様に free() は使用しない.
Definition Matrix++.h:41
T & element(int i,...)
Definition Matrix++.h:204
int d
汎用
Definition Matrix++.h:33
int n
次元数
Definition Matrix++.h:31
void init(int nn,...)
Definition Matrix++.h:113
T err
エラー時にこれを参照用として返す.
Definition Matrix++.h:36
void getm(int nn, int *size)
Definition Matrix++.h:157
Matrix(int nn,...)
Definition Matrix++.h:71
void clear(T v=T(0))
Definition Matrix++.h:48
void free()
free() は手動で呼び出す.
Definition Matrix++.h:52
T * mx
要素 mx[0] 〜 mx[r-1]
Definition Matrix++.h:35
int r
全要素数 sz[0]*sz[1]*...*sz[n-1]
Definition Matrix++.h:32
double c
信頼度
Definition Vector.h:63
Definition Brep.h:29
Matrix< T > dup_Matrix(Matrix< T > a)
Definition Matrix++.h:250
AffineTrans< T > operator*(AffineTrans< T > a, AffineTrans< T > b)
Matrix< T > operator+(const Matrix< T > a, const Matrix< T > b)
Definition Matrix++.h:386
void print_Matrix(FILE *fp, Matrix< T > a)
Definition Matrix++.h:236
bool isSameDimension(const Matrix< T > v1, const Matrix< T > v2)
Definition Matrix++.h:443
Matrix< T > operator/(const Matrix< T > a, const R d)
Definition Matrix++.h:427
bool operator==(const Matrix< T > v1, const Matrix< T > v2)
Definition Matrix++.h:435
Matrix< T > operator-(const Matrix< T > a)
Definition Matrix++.h:378
ツールライブラリ ヘッダ for C++