muMECH  1.0
arrays.h
Go to the documentation of this file.
1 #ifndef GELIB_ARRAYS_H
2 #define GELIB_ARRAYS_H
3 
4 #include "gelib.h"
5 #include "mathlib.h"
6 #include "librw.h"
7 
8 #include <stdio.h>
9 #include <string.h>
10 #include <math.h>
11 
12 
19 namespace gelibspace {
20 
21 #define PYP 3
22 
23 struct VectoR;
24 struct PoinT;
25 
26 class Dmtrx;
27 
28 // ********************************************************************************************
29 // *** *** *** *** CLASS ELEM3D *** *** *** ***
30 // ********************************************************************************************
31 struct Elem3D
32 {
33  double x, y, z;
34 
35  Elem3D() { x=y=z = 0.0; }
36  virtual ~Elem3D() { }
37 
38  // *** GET ***
40  double operator[] (int i) const {
41 #ifdef DEBUG
42  if (i<0 || i>2) _errorr ("i out of the range");
43 #endif
44  return ((i==0) ? x : ((i==1) ? y : z));
45  }
47  double& operator[] (int i) {
48 #ifdef DEBUG
49  if (i<0 || i>2) _errorr ("i out of the range");
50 #endif
51  return ((i==0) ? x : ((i==1) ? y : z));
52  }
53 
54  // *** FIND OUT ***
55  bool is_identical_to (const Elem3D *p, double zero) const { return isZero(zero, x - p->x) && isZero(zero, y - p->y) && isZero(zero, z - p->z); }
56  bool is_identical_to (const Elem3D *p) const { return ( x == p->x && y == p->y && z == p->z); }
57 
58  // *** EDIT ***
59  Elem3D* tms (double val) { x *= val; y *= val; z *= val; return this; }
60  Elem3D* dvd (double val) { x /= val; y /= val; z /= val; return this; }
61  Elem3D* add (const Elem3D *p) { x += p->x; y += p->y; z += p->z; return this; }
62  Elem3D* sub (const Elem3D *p) { x -= p->x; y -= p->y; z -= p->z; return this; }
63  Elem3D* add (const double *p) { x += p[0]; y += p[1]; z += p[2]; return this; }
64  Elem3D* copy (const Elem3D *p) { x = p->x; y = p->y; z = p->z; return this; }
65  Elem3D* zero (void) { x = y = z = 0.0; return this; }
66  Elem3D* round2abszero (double zr) { if (fabs(x)<zr) x=0.0; if (fabs(y)<zr) y=0.0; if (fabs(z)<zr) z=0.0; return this; }
67 
68  // *** SET ***
69  bool scan_x (FILE *stream) { return ( fscanf (stream, "%lf", &x) == 1); }
70  bool scan_y (FILE *stream) { return ( fscanf (stream, "%lf", &y) == 1); }
71  bool scan_z (FILE *stream) { return ( fscanf (stream, "%lf", &z) == 1); }
72  bool scan_xyz (FILE *stream) { return ( fscanf (stream, "%lf %lf %lf", &x, &y, &z) == 3); }
73  bool scan_xyz (const char *&src) { sscanf (src, "%lf %lf %lf", &x, &y, &z); return (SP_skip_word(src) && SP_skip_word(src) && SP_skip_word(src)); }
74  bool scan_xyz (const double *src) { x = src[0]; y = src[1]; z = src[2]; return true; }
75 
76  // *** GET ***
77  void copy_to (double *dest) const { dest[0] = x; dest[1] = y; dest[2] = z; }
78 
79  // *** COMPUTE ***
81  double giveScalProduct (const Elem3D *v) const { return x*v->x + y*v->y + z*v->z; }
83  double give_sum (void) const { return x + y + z; }
84 
85  // *** PRINT ***
86 #ifdef DEBUG
87  virtual void printYourself (bool force=false) const { fprintf (stdout, "PoinT [3]:\n% .*e % .*e % .*e\n", PYP, x, PYP, y, PYP, z); }
88 #endif
89 
90 };
91 
92 
93 // ********************************************************************************************
94 // *** *** *** *** CLASS POINT *** *** *** ***
95 // ********************************************************************************************
96 
97 struct PoinT : public Elem3D
98 {
99  // *** EDIT ***
100  PoinT* copy (const PoinT *p) { Elem3D::copy(p); return this; }
101 
102  double dist_to (const PoinT *p) const { return sqrt( this->dist2_to(p) ); }
103  double dist2_to (const PoinT *p) const { return ((x-p->x)*(x-p->x) + (y-p->y)*(y-p->y) + (z-p->z)*(z-p->z)); }
104 
106  inline double dist_to_line (const PoinT *p1, const PoinT *p2) const;
108  void bePointAtAbscissa (const PoinT *p1, const PoinT *p2, double ksi)
109  {
110  x = 0.5 * (p1->x + p2->x + ksi * ( p2->x - p1->x ));
111  y = 0.5 * (p1->y + p2->y + ksi * ( p2->y - p1->y ));
112  z = 0.5 * (p1->z + p2->z + ksi * ( p2->z - p1->z ));
113  }
114 
117  bool give_ksiAtAbscissa (double zero, double norm, const PoinT *p1, const PoinT *p2, double &ksi) const
118  {
119  if ( dist_to_line (p1,p2) > norm*zero ) return false;
120 
122  long i;
123  if (fabs(p1->x - p2->x) > fabs(p1->y - p2->y)) if (fabs(p1->x - p2->x) > fabs(p1->z - p2->z)) i = 0; else i = 2;
124  else if (fabs(p1->y - p2->y) > fabs(p1->z - p2->z)) i = 1; else i = 2;
125 
126  ksi = 2.0 * ( (*this)[i] - (*p1)[i] ) / ( (*p2)[i] - (*p1)[i] ) - 1.0 ;
127 
128  return ( fabs(ksi) < (1.0+zero) );
129  }
130 
132  void beRotatedPoint (const Dmtrx *rot, const PoinT *point);
134  void beRotatedBy (const double *r);
136  void beRotatedBy2d (const double *r);
137 
138  double* d() const
139  {
140  double *a = new double[3];
141  a[0] = x; a[1] = y; a[2] = z;
142  return a;
143  }
144 
145 };
146 
147 
148 // ********************************************************************************************
149 // *** *** *** *** CLASS VectoR *** *** *** ***
150 // ********************************************************************************************
151 
152 struct VectoR : public Elem3D
153 {
154  // *** EDIT ***
155  VectoR* copy (const VectoR *p) { Elem3D::copy(p); return this; }
156 
158  VectoR* beP2P (const PoinT *p1, const PoinT *p2) {
159  x = p2->x - p1->x;
160  y = p2->y - p1->y;
161  z = p2->z - p1->z;
162  return this;
163  }
165  VectoR* beVectProduct (const VectoR *v1, const VectoR *v2) {
166  x = v1->y * v2->z - v2->y * v1->z;
167  y = v1->z * v2->x - v2->z * v1->x;
168  z = v1->x * v2->y - v2->x * v1->y;
169  return this;
170  }
172  VectoR* beVectProduct (const PoinT *p1, const PoinT *p2, const PoinT *p3)
173  { // beVectProduct (p1, p2, p1, p3);
174  x = p2->y*p3->z - p3->y*p2->z + p1->y*(p2->z-p3->z) - p1->z*(p2->y-p3->y);
175  y = p2->z*p3->x - p3->z*p2->x + p1->z*(p2->x-p3->x) - p1->x*(p2->z-p3->z);
176  z = p2->x*p3->y - p3->x*p2->y + p1->x*(p2->y-p3->y) - p1->y*(p2->x-p3->x);
177  return this;
178  }
180  VectoR* beVectProduct (const PoinT *p1, const PoinT *p2, const PoinT *p3, const PoinT *p4)
181  {
182  VectoR v1, v2;
183  v1.beP2P(p1, p2);
184  v2.beP2P(p3, p4);
185 
186  this->beVectProduct (&v1, &v2);
187  return this;
188  }
190  VectoR* normalize (void) { this->dvd( this->give_length() ); return this; }
191 
192  // *** COMPUTE ***
193  double give_length (void) const { return sqrt( x*x + y*y + z*z ); }
194  double give_length_2d (void) const { return sqrt( x*x + y*y ); }
195  double give_angle_2d (void) const { return atan(y/x); }
196  VectoR* be_rot_by_angle_2d (const VectoR *p, double a)
197  {
198  double cs = cos(a);
199  double sn = sin(a);
200 
201  x = p->x * cs - p->y * sn;
202  y = p->x * sn + p->y * cs;
203  z = p->z;
204  return this;
205  }
206  VectoR* be_rotSS_by_angle_2d (const VectoR *p, double a)
207  {
208  double cs = cos(a);
209  double sn = sin(a);
210 
211  x = p->x * cs + p->y * sn;
212  y = - p->x * sn + p->y * cs;
213  z = p->z;
214  return this;
215  }
216 
217  // *** INFO ***
218  bool is_parallel_with (const VectoR *that, double relZero) const
219  {
220  double l1 = this->give_length();
221  double l2 = that->give_length();
222  if (l1/l2 < relZero || l2/l1 < relZero) _warningg("Lengths of vectors are not proportional");
223 
224  double v1x = this->x/l1; if (fabs(v1x) < relZero) v1x = 0.0;
225  double v1y = this->y/l1; if (fabs(v1y) < relZero) v1y = 0.0;
226  double v1z = this->z/l1; if (fabs(v1z) < relZero) v1z = 0.0;
227 
228  double v2x = that->x/l2; if (fabs(v2x) < relZero) v2x = 0.0;
229  double v2y = that->y/l2; if (fabs(v2y) < relZero) v2y = 0.0;
230  double v2z = that->z/l2; if (fabs(v2z) < relZero) v2z = 0.0;
231 
232  int sign;
233  if (v1x*v2x < 0.0 || v1y*v2y < 0.0 || v1z*v2z < 0.0) sign = -1;
234  else sign = 1;
235 
236  return (fabs(v1x-sign*v2x) < relZero && fabs(v1y-sign*v2y) < relZero && fabs(v1z-sign*v2z) < relZero);
237  }
238 };
239 
241 double PoinT :: dist_to_line (const PoinT *p1, const PoinT *p2) const
242 {
243  VectoR a;
244  a.beVectProduct(p1, p2, this);
245  return ( sqrt((a.x*a.x + a.y*a.y + a.z*a.z) / p1->dist2_to(p2) ) );
246 }
247 
248 // ********************************************************************************************
249 // *** *** *** *** CLASS MatriX *** *** *** ***
250 // ********************************************************************************************
251 struct MatriX
252 {
253  double a[9];
254 
255  MatriX() { }
256  virtual ~MatriX() { }
257 
258  double& at11() { return a[0]; }
259  double& at12() { return a[1]; }
260  double& at13() { return a[2]; }
261  double& at21() { return a[3]; }
262  double& at22() { return a[4]; }
263  double& at23() { return a[5]; }
264  double& at31() { return a[6]; }
265  double& at32() { return a[7]; }
266  double& at33() { return a[8]; }
267 
268  void rotate (Dvctr *v) const;
269  void rotate (double *v) const;
270 
271  void copy_to_row_1 (const VectoR *v) { a[0] = v->x; a[1] = v->y; a[2] = v->z; }
272  void copy_to_row_2 (const VectoR *v) { a[3] = v->x; a[4] = v->y; a[5] = v->z; }
273  void copy_to_row_3 (const VectoR *v) { a[6] = v->x; a[7] = v->y; a[8] = v->z; }
274 
275  void copy_to_col_1 (const VectoR *v) { a[0] = v->x; a[3] = v->y; a[6] = v->z; }
276  void copy_to_col_2 (const VectoR *v) { a[1] = v->x; a[4] = v->y; a[7] = v->z; }
277  void copy_to_col_3 (const VectoR *v) { a[2] = v->x; a[5] = v->y; a[8] = v->z; }
278 };
279 
280 
281 // ********************************************************************************************
282 // *** *** *** *** CLASS ARRAY *** *** *** ***
283 // ********************************************************************************************
284 
285 #ifdef DEBUG
286 #define SRCCHCK if (src==NULL) _errorr("source == NULL");
287 #define AVOID_CNST if (cnst) _errorr("cnst")
288 #define AVOID_EXTA if (exta) _errorr("exta")
289 #else
290 #define SRCCHCK
291 #define AVOID_CNST
292 #define AVOID_EXTA
293 #endif
294 
295 
298 
299 class Dmtrx;
300 
301 class Array
302 {
303  public:
305  Array () { }
307  virtual ~Array () { }
308 
309  // give type def
310  virtual arrayTypedef give_arrayTypedef (void) const = 0;
311 
312  // *** PRINT ***
313 #ifdef DEBUG
314  virtual void printYourself (bool force=false) const = 0;
315 #endif
316 
317 };
318 
319 
320 
321 // ********************************************************************************************
322 // *** *** *** *** CLASS ARRAY1D *** *** *** ***
323 // ********************************************************************************************
324 class Array1d : public Array
325 {
326  public:
328  Array1d () : Array() { }
330  virtual ~Array1d() { }
331 
332  virtual long give_size (void) const = 0;
333 
334  // *** PRINT ***
335  virtual int length_printed (int precision) const = 0;
336  virtual void print (char *stream, int precision, double absZero=0.0) const = 0;
337 };
338 
339 
340 // ********************************************************************************************
341 // *** *** *** *** CLASS XSCAL *** *** *** ***
342 // ********************************************************************************************
343 class Xscal : public Array1d
344 {
345  public:
347  Xscal () : Array1d() { }
349  virtual ~Xscal() { }
350 
351  long give_size (void) const { return 1; }
352 };
353 
354 // ********************************************************************************************
355 // *** *** *** *** CLASS DSCAL *** *** *** ***
356 // ********************************************************************************************
357 class Dscal : public Xscal
358 {
359  public:
360  double a; // value
361 
363  Dscal (void) : Xscal () { a = 0; }
364  Dscal (double val) : Xscal () { a = val; }
366  virtual ~Dscal() { }
367 
369  arrayTypedef give_arrayTypedef (void) const { return ATdouble; }
370 
371  // *** OPERATORS ***
372  double operator() (void) const { return a; }
373  double& operator() (void) { return a; }
374 
375  // *** PRINT ***
376 #ifdef DEBUG
377  virtual void printYourself (bool force=false) const { fprintf (stdout, "Double scalar: % .*e\n", PYP, a); }
378 #endif
379  virtual int length_printed (int precision) const { return 1+2+1+precision+2+NUM_DIGITS_IN_PRINTED_EXPONENT + 1; }
380  virtual void print (char *stream, int precision, double absZero=0.0) const;
381 };
382 
383 
384 
385 // ********************************************************************************************
386 // *** *** *** *** CLASS XVCTR *** *** *** ***
387 // ********************************************************************************************
388 class Xvctr : public Array1d
389 {
390  protected:
391  bool exta; // external allocation, we cannot delete "a"
392  bool cnst; // constant, values can not be changed
393  long shft; // a is shifted
394 
395  long size; // length of array
396  long asize; // length of the allocated array
397 
398 
399  public:
401  Xvctr (long s) : Array1d() { exta = cnst = false; shft = 0; size = asize = s; }
403  virtual ~Xvctr() { }
404 
405  virtual arrayClassType give_classid() const = 0;
406 
407  // *** SET ***
408  virtual void cpat (long i, const Xvctr *p, long j) = 0;
409  virtual void shift (long val) = 0;
410  void deshift (void) { this->shift(-shft); }
411  virtual bool scan (const char *&src) = 0;
412 
413  // *** GET ***
414  virtual long give_size (void) const { return size; }
415 
416  // *** COMPUTE ***
417  virtual double give_lenght (void) const = 0;
419  double give_zero (double abszero, double relzero) const;
420 
421  // *** PRINT ***
422  //virtual int length_printed (int precision) const = 0;
423  virtual int length_printed_vector (int precision) const = 0;
424  virtual int length_printed_tensor (int precision) const = 0;
425  //virtual void print (char *stream, int precision, double absZero=0.0 ) const = 0;
426  virtual void print_vector (char *stream, int precision, double absZero=0.0, bool zerorest=false) const = 0;
427  virtual void print_symtensor (char *stream, int precision, double absZero=0.0, bool zerorest=false) const = 0;
428  virtual void print_symtensor (FILE *stream, int precision, double absZero=0.0, bool zerorest=false) const = 0;
429  //virtual void print_member (char *stream, int precision, long i) const = 0;
430 };
431 
432 // ********************************************************************************************
433 // *** *** *** *** CLASS LVCTR *** *** *** ***
434 // ********************************************************************************************
435 class Lvctr : public Xvctr
436 {
438  long *a;
439 
440  public:
442  Lvctr (void) : Xvctr (0) { a = NULL; }
443  Lvctr (long s) : Xvctr (s) { a = new long[asize]; }
444  Lvctr ( const Lvctr *p) : Xvctr (p->size) { a = new long[asize]; memcpy (a, p->a, size*sizeof(long)); }
445  Lvctr (long s, const long *p) : Xvctr (s) { a = new long[asize]; memcpy (a, p, size*sizeof(long)); }
447  virtual ~Lvctr() { deshift(); if (a && !exta) delete [] a; }
448 
451  arrayTypedef give_arrayTypedef (void) const { return ATlong; }
452 
453  // *** OPERATORS ***
455  long operator[] (long i) const {
456 #ifdef DEBUG
457  if (i<0 || i>=size) _errorr ("i out of the range");
458 #endif
459  return a[i];
460  }
461 
463  long& operator[] (long i) {
464 #ifdef DEBUG
465  if (i<0 || i>=size)
466  _errorr ("i out of the range");
467 #endif
468  return a[i];
469  }
470 
472  Lvctr& operator= (const Lvctr &src) {
474  if ( this == &src ) _errorr("oneself assignment");
475 
476  size = 0;
477  this->realloc (src.size);
478  size = src.size;
479 
480  memcpy (a, src.a, size*sizeof(long));
481 
482  return * this;
483  }
484 
485  // *** SET ***
487  void add (long val) { resize_preserve_vals(size+1); a[size-1] = val; }
489  void add_unique (long val) { if (! this->is_member(val)) this->add(val); }
491  bool is_member (long val) const { return is_member_of_array (val, size, a); }
493  Lvctr* free (void);
495  void realloc (long newsize);
497  Lvctr* resize_preserve_vals (long newsize) { AVOID_CNST; AVOID_EXTA; this->realloc (newsize); size = newsize; return this; }
499  Lvctr* resize_ignore_vals (long newsize) { AVOID_CNST; AVOID_EXTA; size = 0; this->realloc (newsize); size = newsize; return this; }
501  Lvctr* resize_to_asize (void) { AVOID_CNST; AVOID_EXTA; size = asize; return this; }
503  Lvctr* assign_array ( long *array, long s);
504  Lvctr* assign_array (const long *array, long s);
506  void cpat (long i, const Xvctr *p, long j) {
507  const Lvctr *pt = dynamic_cast<const Lvctr *>(p);
508  if (!pt) _errorr("");
509  a[i] = pt->a[j];
510  }
511  void shift (long val) { AVOID_CNST; if (a) { shft += val; a += val; } }
512  bool scan (const char *&src) { return SP_scan_array (src, size, a); }
514  void fillYourselfBy (int val) { memset (a, val, size*sizeof(long)); }
515 
516  // *** GET ***
518  long* give_ptr2val (long i=0) { AVOID_CNST; return a + i; }
519  const long* give_ptr2val (long i=0) const { return a + i; }
520 
521 
522  // *** COMPUTE ***
523  double give_lenght (void) const;
524  long give_sum (void) const;
525  long give_number_of_nonzeros (void) const;
526  long give_number_of_zeros (void) const;
527 
528 
529  // *** EDIT ***
531  void zero (void) { AVOID_CNST; memset (a,0,asize*sizeof(long)); }
533  //
534  //
535  //
536  //
537 
539  //
540  //
541  //
542  //
543  //
544 
550  //void addtms (const Dvctr &src, double tms);
552  //void tmsby (double val);
554  //void dvdby (double val);
555 
557 
558  // *** PRINT ***
559 #ifdef DEBUG
560  virtual void printYourself (bool force) const;
561 #endif
562  //
563  int length_printed (int precision) const;
564  int length_printed_vector (int precision) const;
565  int length_printed_tensor (int precision) const;
566  void print (char *stream, int precision, double absZero=0.0 ) const;
567  void print_vector (char *stream, int precision, double absZero=0.0, bool zerorest=false) const;
568  void print_symtensor (char *stream, int precision, double absZero=0.0, bool zerorest=false) const;
569  void print_symtensor (FILE *stream, int precision, double absZero=0.0, bool zerorest=false) const;
570  //void print_member (char *stream, int precision, long i) const { sprintf (stream, "%ld", a[i]); }
571  //
572  // void print_symtensor (FILE *stream) const;
573  // void print_r2z_symtensor (FILE *stream, double relZero, double absZero) const;
574  // void print_symtensor (char *stream) const;
575  // void print_r2z_symtensor (char *stream, double relZero, double absZero) const;
576 };
577 
578 
579 // ********************************************************************************************
580 // *** *** *** *** CLASS DVCTR *** *** *** ***
581 // ********************************************************************************************
582 
583 class Dvctr : public Xvctr
584 {
585  public:
586  double *a; // array of values
587 
589  Dvctr (void) : Xvctr (0) { a = NULL; }
590  Dvctr (long s) : Xvctr (s) { a = new double[asize]; }
591  Dvctr ( const Dvctr *src) : Xvctr (src->size) { SRCCHCK; a = new double[asize]; memcpy (a, src->a, size*sizeof(double)); }
592  Dvctr (long s, const double *src) : Xvctr (s) { SRCCHCK; a = new double[asize]; memcpy (a, src, size*sizeof(double)); }
593  Dvctr ( const VectoR *src) : Xvctr (3) { SRCCHCK; a = new double[asize]; a[0]=src->x; a[1]=src->y; a[2]=src->z; }
595  virtual ~Dvctr() { deshift(); if (a && !exta) delete [] a; }
596 
598  arrayTypedef give_arrayTypedef (void) const { return ATdouble; }
599 
600  // *** OPERATORS ***
601  double operator[] (long i) const {
602 #ifdef DEBUG
603  if (i<0 || i>=size) _errorr ("i out of the range");
604 #endif
605  return a[i];
606  }
607  double& operator[] (long i) {
608 #ifdef DEBUG
609  if (i<0 || i>=size)
610  _errorr ("i out of the range");
611 #endif
612  return a[i];
613  }
614 
615 
616  // *** SET ***
617  Dvctr* free (void);
618  void realloc (long newsize);
619  // resize, keep values
620  Dvctr* resize_preserve_vals (long newsize) { AVOID_CNST; AVOID_EXTA; this->realloc (newsize); size = newsize; return this; }
621  // resize, ignore values
623  //AVOID_EXTA;
624  if (exta)
625  _errorr("exta");
626  size = 0; this->realloc (newsize); size = newsize; return this; }
627  // resize, ignore values
628  Dvctr* resize_to_asize (void) { AVOID_CNST; AVOID_EXTA; size = asize; return this; }
629  //Dvctr* resizeup (long newsize) { AVOID_CNST; AVOID_EXTA; this->reallocup (newsize); size = newsize; }
630  //Dvctr* resizedown (long newsize) { AVOID_CNST; AVOID_EXTA; this->reallocdown(newsize); size = newsize; }
631 
632  Dvctr* assign_array ( double *array, long s);
633  Dvctr* assign_array (const double *array, long s);
635  void cpat (long i, const Xvctr *p, long j) {
636  const Dvctr *pt = dynamic_cast<const Dvctr *>(p);
637  if (!pt) _errorr("");
638  a[i] = pt->a[j];
639  }
640  void shift (long val) { AVOID_CNST; if (a) { shft += val; a += val; } }
641  bool scan (const char *&src) { return SP_scan_array (src, size, a); }
642 
643  // *** GET ***
645  double* give_ptr2val (long i=0) { AVOID_CNST; return a + i; }
646  const double* give_ptr2val (long i=0) const { return a + i; }
647 
648 
649  // *** COMPUTE ***
650  double give_lenght (void) const;
651  double give_sum (void) const;
652  long give_number_of_nonzeros (void) const;
653 
654 
655  // *** EDIT ***
657  void zero (void) { AVOID_CNST; memset (a,0,size*sizeof(double)); }
659  void beCopyOf (const PoinT &src) { this->resize_ignore_vals(3); a[0] = src.x; a[1] = src.y; a[2] = src.z; }
660  void beCopyOf (const double *src) { AVOID_CNST; memcpy (a, src, size*sizeof(double)); }
661  void beCopyOf (const double *src, long s) { this->resize_ignore_vals(s ); memcpy (a, src, size*sizeof(double)); }
662  void beCopyOf (const Dvctr &src) { this->resize_ignore_vals(src. size); memcpy (a, src.a, size*sizeof(double)); }
663  void beCopyOf (const Dvctr *src) { this->resize_ignore_vals(src->size); memcpy (a, src->a, size*sizeof(double)); }
664 
665  void copy_to (double *dest) const { memcpy (dest, a, size*sizeof(double)); }
666 
668  void append (const Dvctr *src) {
669  AVOID_CNST;
670  this->resize_preserve_vals(size + src->size);
671  memcpy (a+size-src->size, src->a, (size-src->size)*sizeof(double));
672  }
673 
675  void be_tnsr (const Dmtrx &src);
677  void be_vectproduct (const Dvctr &v1, const Dvctr &v2);
679  void be_mean_of (const Dmtrx *src);
681  double give_dotproduct (const Dvctr &v) const;
682 
683 
687  void add (const Dvctr &src); // this += src
688  void addtms (const Dvctr &src, double tms); // this += tms * src
689  void sbt (const Dvctr &src); // this -= src
690  void tmsby (double val);
691  void dvdby (double val);
692 
694  void tnsrRotWith (const Dmtrx &rot);
695  void tnsrRotWith1 (const Dmtrx &rot);
696  void tnsrRotWith2 (const Dmtrx &rot);
698  void tnsrRotAxisZangle (double a);
700  void normalize (void);
702  double compute_squared_norm(void) const;
704  double give_zero (double abszero, double relzero) const;
707 
708  // *** PRINT ***
709 #ifdef DEBUG
710  virtual void printYourself (bool force) const;
711 #endif
712  //
713  int length_printed (int precision) const;
714  int length_printed_vector (int precision) const;
715  int length_printed_tensor (int precision) const;
716  void print (char *stream, int precision, double absZero=0.0 ) const;
717  void print_vector (char *stream, int precision, double absZero=0.0, bool zerorest=false) const;
718  void print_symtensor (char *stream, int precision, double absZero=0.0, bool zerorest=false) const;
719  void print_symtensor (FILE *stream, int precision, double absZero=0.0, bool zerorest=false) const;
720 
721 // void print_vector (char *stream, int precision) const;
722 // void print_vector_or_zero (char *stream, int precision) const;
723 // void print_vector_zerorest (char *stream, int precision) const;
724 // void print_member (char *stream, int precision, long i) const { sprintf (stream, "% .*e", precision, a[i]); }
725 // //
726 // void print_symtensor (FILE *stream, int precision) const;
727 // void print_symtensor_zero (FILE *stream, int precision) const;
728 // void print_r2z_symtensor (FILE *stream, int precision, double relZero, double absZero) const;
729 // void print_symtensor (char *stream, int precision) const;
730 // void print_symtensor_zero (char *stream, int precision) const;
731 // void print_r2z_symtensor (char *stream, int precision, double relZero, double absZero) const;
732 };
733 
734 
735 
736 
737 
738 // ********************************************************************************************
739 // *** *** *** *** CLASS XMTRX *** *** *** ***
740 // ********************************************************************************************
741 class Xmtrx : public Array
742 {
743  protected:
744  long crow; // count of rows
745  long ccol; // count of collumns
746  long asize; // length of the allocated array
747 
748  public:
750  Xmtrx (void) : Array() { crow = 0; ccol = 0; asize = crow*ccol; }
751  Xmtrx (long r, long c) : Array() { crow = r; ccol = c; asize = crow*ccol; }
753  virtual ~Xmtrx() { }
754 
755  // *** SET ***
756  // *** GET ***
757  long give_crows (void) const { return crow; }
758  long give_ccols (void) const { return ccol; }
759 };
760 
761 
762 
763 // ********************************************************************************************
764 // *** *** *** *** CLASS LMTRX *** *** *** ***
765 // ********************************************************************************************
766 class Lmtrx : public Xmtrx
767 {
768  public:
769  long *a; // array of values
770 
772  Lmtrx (void) : Xmtrx() { a = NULL; }
773  Lmtrx (long r, long c) : Xmtrx(r,c) { a = new long[asize]; }
775  virtual ~Lmtrx() { if (a) delete [] a; }
776 
777  arrayTypedef give_arrayTypedef (void) const { return ATlong; }
778 
779  // *** SET ***
780  void resize_ignore_vals (long r, long c);
781 
782  // *** GET ***
783  long &operator()(long r, long c) { return a[r*ccol + c]; }
784  const long &operator()(long r, long c) const { return a[r*ccol + c]; }
786  long* give_ptr2val (long r=0, long c=0) { return a + r*ccol + c; }
787  const long* give_ptr2val (long r=0, long c=0) const { return a + r*ccol + c; }
788 
789  // *** PRINT ***
790 #ifdef DEBUG
791  virtual void printYourself (bool force=false) const;
792 #endif
793 
794 };
795 
796 
797 // ********************************************************************************************
798 // *** *** *** *** CLASS DMTRX *** *** *** ***
799 // ********************************************************************************************
800 class Dmtrx : public Xmtrx
801 {
802  protected:
803  double *a; // array of values
804 
805  public:
807  Dmtrx (void) : Xmtrx() { a = NULL; }
808  Dmtrx (long r, long c) : Xmtrx(r,c) { a = new double[asize]; }
809  Dmtrx ( const Dmtrx *src) : Xmtrx(src->crow,src->ccol) { SRCCHCK; a = new double[asize]; memcpy (a, src->a, crow*ccol*sizeof(double)); }
811  virtual ~Dmtrx() { delete [] a; }
812 
813  arrayTypedef give_arrayTypedef (void) const { return ATdouble; }
814 
815  // *** SET ***
816  void resize_ignore_vals (long r, long c);
817 
818  // *** GET ***
819  double &operator()(long r, long c) { return a[r*ccol + c]; }
820  const double &operator()(long r, long c) const { return a[r*ccol + c]; }
822  double* give_ptr2val (long r=0, long c=0) { return a + r*ccol + c; }
823  const double* give_ptr2val (long r=0, long c=0) const { return a + r*ccol + c; }
824 
825 
826  // *** EDIT ***
828  void zero (void) { memset (a, 0, crow*ccol*sizeof(double)); }
830 # ifndef DEBUG
831  void copy_row (long r, const Dvctr &v) { if (ccol != v.give_size()) _errorr("size mismatch"); memcpy (a + r*ccol, v.give_ptr2val(), ccol*sizeof(double)); }
832 # else
833  void copy_row (long r, const Dvctr &v) { memcpy (a + r*ccol, v.give_ptr2val(), ccol*sizeof(double)); }
834 # endif
835  void copy_row (long r, const Elem3D *v) { a[r*ccol] = v->x; a[r*ccol+1] = v->y; a[r*ccol+2] = v->z; }
837  void beCopyOf (const double *src) { memcpy (a, src, crow*ccol*sizeof(double)); }
838  void beCopyOf (const Dmtrx &src) { this->beCopyOf(src. give_ptr2val(0,0)); }
839  void beCopyOf (const Dmtrx *src) { this->beCopyOf(src->give_ptr2val(0,0)); }
841  void be_tnsr (const Dvctr &src);
842  void be_transposition (const Dmtrx &src);
843  void be_mtrxMmtrx (const Dmtrx &A, const Dmtrx &B);
844  void tmsLeftBy (const Dmtrx &src);
845  void tmsRightBy (const Dmtrx &src);
846 
847  // *** COMPUTE ***
848  double give_determinant (void) const;
850  bool GaussSolve (Dvctr &x, const Dvctr &rhs);
851  bool GaussSolve (Dvctr *x, const Dvctr *rhs, long nrhs);
852 
853  // *** PRINT ***
854 #ifdef DEBUG
855  virtual void printYourself (bool force=false) const;
856 #endif
857  //void print_tensor (FILE *stream, int precision) const;
858  void print_tensor (FILE *stream, int precision, double absZero, bool zerorest) const;
859 
860 };
861 
862 
863 } // namespace gelibspace
864 
865 #endif // GELIB_ARRAYS_H
double & at33()
Definition: arrays.h:266
VectoR * normalize(void)
Definition: arrays.h:190
virtual ~Array()
DESTRUCTOR.
Definition: arrays.h:307
Array1d()
CONSTRUCTOR.
Definition: arrays.h:328
const long * give_ptr2val(long r=0, long c=0) const
Definition: arrays.h:787
#define _warningg(_1)
Definition: gelib.h:163
bool give_ksiAtAbscissa(double zero, double norm, const PoinT *p1, const PoinT *p2, double &ksi) const
compute natural coordinate ksi of receiver at abscissa p1p2 answer: 1(0) = point lays on(out of) absc...
Definition: arrays.h:117
long * a
Array of values.
Definition: arrays.h:438
double & at12()
Definition: arrays.h:259
Dvctr(long s)
Definition: arrays.h:590
double & at22()
Definition: arrays.h:262
Dvctr * resize_ignore_vals(long newsize)
Definition: arrays.h:622
virtual ~Lmtrx()
DESTRUCTOR.
Definition: arrays.h:775
VectoR * beP2P(const PoinT *p1, const PoinT *p2)
Receiver is set to vector from p1 to p2, i.e. &#39;this = p2 - p1&#39;.
Definition: arrays.h:158
long give_size(void) const
Definition: arrays.h:351
void beCopyOf(const double *src)
Definition: arrays.h:837
bool scan_z(FILE *stream)
Definition: arrays.h:71
Xmtrx(long r, long c)
Definition: arrays.h:751
Lvctr * resize_preserve_vals(long newsize)
resize, keep values
Definition: arrays.h:497
void append(const Dvctr *src)
Definition: arrays.h:668
virtual ~Xmtrx()
DESTRUCTOR.
Definition: arrays.h:753
void beCopyOf(const Dvctr *src)
Definition: arrays.h:663
bool scan_y(FILE *stream)
Definition: arrays.h:70
General functions.
void beCopyOf(const Dvctr &src)
Definition: arrays.h:662
virtual ~Array1d()
DESTRUCTOR.
Definition: arrays.h:330
virtual long give_size(void) const
Definition: arrays.h:414
Input / output function.
Xmtrx(void)
CONSTRUCTOR.
Definition: arrays.h:750
Lvctr(long s)
Definition: arrays.h:443
double give_length(void) const
Definition: arrays.h:193
#define array(MAT, ROWS, COLS, ROW, COL)
Definition: meso3d.cpp:139
Dmtrx(const Dmtrx *src)
Definition: arrays.h:809
Dvctr(void)
CONSTRUCTOR.
Definition: arrays.h:589
arrayTypedef give_arrayTypedef(void) const
Definition: arrays.h:777
double & at11()
Definition: arrays.h:258
bool SP_scan_array(const char *&src, int n, ArgType *a)
... array of numbers
Definition: librw.h:253
double give_length_2d(void) const
Definition: arrays.h:194
void fillYourselfBy(int val)
Set all elements of the array to the given value.
Definition: arrays.h:514
arrayTypedef give_arrayTypedef(void) const
Definition: arrays.h:451
Lmtrx(void)
CONSTRUCTOR.
Definition: arrays.h:772
Array()
CONSTRUCTOR.
Definition: arrays.h:305
void zero(void)
Definition: arrays.h:828
long give_ccols(void) const
Definition: arrays.h:758
bool scan(const char *&src)
Definition: arrays.h:512
double & operator()(long r, long c)
Definition: arrays.h:819
void beCopyOf(const Dmtrx *src)
Definition: arrays.h:839
#define NUM_DIGITS_IN_PRINTED_EXPONENT
Definition: gelib.h:41
long * give_ptr2val(long r=0, long c=0)
Definition: arrays.h:786
Dmtrx(long r, long c)
Definition: arrays.h:808
Dscal(void)
CONSTRUCTOR.
Definition: arrays.h:363
Dvctr(const VectoR *src)
Definition: arrays.h:593
Xscal()
CONSTRUCTOR.
Definition: arrays.h:347
void add_unique(long val)
add value to size++ position if unique
Definition: arrays.h:489
double giveScalProduct(const Elem3D *v) const
scalar product this * e
Definition: arrays.h:81
bool is_member(long val) const
Definition: arrays.h:491
Elem3D * copy(const Elem3D *p)
Definition: arrays.h:64
const long * give_ptr2val(long i=0) const
Definition: arrays.h:519
virtual ~Elem3D()
Definition: arrays.h:36
void bePointAtAbscissa(const PoinT *p1, const PoinT *p2, double ksi)
receiver will be point at abscissa p1p2 with natural coord ksi
Definition: arrays.h:108
void copy_to_row_1(const VectoR *v)
Definition: arrays.h:271
long & operator()(long r, long c)
Definition: arrays.h:783
VectoR * beVectProduct(const VectoR *v1, const VectoR *v2)
vector product v1 x v2 (cross product)
Definition: arrays.h:165
arrayTypedef give_arrayTypedef(void) const
Definition: arrays.h:598
void copy_row(long r, const Elem3D *v)
Definition: arrays.h:835
bool scan_xyz(const char *&src)
Definition: arrays.h:73
long is_member_of_array(ArgType val, long n, const ArgType *array)
check out "val" is member of "array"
Definition: gelib.h:320
#define AVOID_CNST
Definition: arrays.h:291
Dvctr * resize_preserve_vals(long newsize)
Definition: arrays.h:620
PoinT * copy(const PoinT *p)
Definition: arrays.h:100
const double * give_ptr2val(long i=0) const
Definition: arrays.h:646
#define SRCCHCK
Definition: arrays.h:290
Dvctr(long s, const double *src)
Definition: arrays.h:592
void beCopyOf(const PoinT &src)
Definition: arrays.h:659
void copy_to(double *dest) const
Definition: arrays.h:77
arrayClassType give_classid() const
Definition: arrays.h:368
Elem3D * round2abszero(double zr)
Definition: arrays.h:66
virtual ~MatriX()
Definition: arrays.h:256
double & at23()
Definition: arrays.h:263
Dscal(double val)
Definition: arrays.h:364
arrayTypedef
Definition: arrays.h:297
Lvctr(const Lvctr *p)
Definition: arrays.h:444
VectoR * beVectProduct(const PoinT *p1, const PoinT *p2, const PoinT *p3, const PoinT *p4)
vector product p1p2 x p3p4
Definition: arrays.h:180
#define AVOID_EXTA
Definition: arrays.h:292
Lmtrx(long r, long c)
Definition: arrays.h:773
bool scan_xyz(FILE *stream)
Definition: arrays.h:72
VectoR * be_rotSS_by_angle_2d(const VectoR *p, double a)
Definition: arrays.h:206
void beCopyOf(const double *src)
Definition: arrays.h:660
double operator[](int i) const
Definition: arrays.h:40
arrayClassType
Definition: arrays.h:296
Elem3D * zero(void)
Definition: arrays.h:65
double * a
Definition: arrays.h:586
void copy_to_col_3(const VectoR *v)
Definition: arrays.h:277
double * a
Definition: arrays.h:803
double * give_ptr2val(long r=0, long c=0)
Definition: arrays.h:822
void beCopyOf(const double *src, long s)
Definition: arrays.h:661
double * d() const
Definition: arrays.h:138
void add(long val)
add value to size++ position
Definition: arrays.h:487
double & at21()
Definition: arrays.h:261
Lvctr * resize_ignore_vals(long newsize)
resize, ignore values
Definition: arrays.h:499
void deshift(void)
Definition: arrays.h:410
const double & operator()(long r, long c) const
Definition: arrays.h:820
void copy_row(long r, const Dvctr &v)
Definition: arrays.h:831
void copy_to_row_2(const VectoR *v)
Definition: arrays.h:272
arrayClassType give_classid() const
Definition: arrays.h:450
#define _errorr(_1)
Definition: gelib.h:160
void zero(void)
Definition: arrays.h:657
Elem3D * sub(const Elem3D *p)
Definition: arrays.h:62
double & at31()
Definition: arrays.h:264
Elem3D * tms(double val)
Definition: arrays.h:59
Dmtrx(void)
CONSTRUCTOR.
Definition: arrays.h:807
bool scan(const char *&src)
Definition: arrays.h:641
Mathematic functions.
Elem3D * add(const double *p)
Definition: arrays.h:63
void zero(void)
Definition: arrays.h:531
bool is_parallel_with(const VectoR *that, double relZero) const
Definition: arrays.h:218
double & at13()
Definition: arrays.h:260
const double * give_ptr2val(long r=0, long c=0) const
Definition: arrays.h:823
void cpat(long i, const Xvctr *p, long j)
Definition: arrays.h:635
VectoR * beVectProduct(const PoinT *p1, const PoinT *p2, const PoinT *p3)
vector product p1p2 x p1p3
Definition: arrays.h:172
void copy_to_col_1(const VectoR *v)
Definition: arrays.h:275
double dist_to_line(const PoinT *p1, const PoinT *p2) const
give distance from receiver to line defined by p1 and p2
Definition: arrays.h:241
void beCopyOf(const Dmtrx &src)
Definition: arrays.h:838
double dist_to(const PoinT *p) const
Definition: arrays.h:102
double give_angle_2d(void) const
Definition: arrays.h:195
long * give_ptr2val(long i=0)
return pointer to
Definition: arrays.h:518
Xvctr(long s)
CONSTRUCTOR.
Definition: arrays.h:401
virtual ~Xscal()
DESTRUCTOR.
Definition: arrays.h:349
arrayClassType give_classid() const
Definition: arrays.h:597
arrayTypedef give_arrayTypedef(void) const
Definition: arrays.h:813
void copy_to_col_2(const VectoR *v)
Definition: arrays.h:276
long give_crows(void) const
Definition: arrays.h:757
Lvctr(long s, const long *p)
Definition: arrays.h:445
bool isZero(double zero, double a)
Definition: mathlib.cpp:308
virtual ~Lvctr()
DESTRUCTOR.
Definition: arrays.h:447
void copy_to(double *dest) const
Definition: arrays.h:665
VectoR * copy(const VectoR *p)
Definition: arrays.h:155
bool is_identical_to(const Elem3D *p) const
Definition: arrays.h:56
virtual ~Dvctr()
DESTRUCTOR.
Definition: arrays.h:595
virtual ~Dscal()
DESTRUCTOR.
Definition: arrays.h:366
Elem3D * add(const Elem3D *p)
Definition: arrays.h:61
bool scan_x(FILE *stream)
Definition: arrays.h:69
bool scan_xyz(const double *src)
Definition: arrays.h:74
Elem3D * dvd(double val)
Definition: arrays.h:60
double dist2_to(const PoinT *p) const
Definition: arrays.h:103
double * give_ptr2val(long i=0)
return pointer to
Definition: arrays.h:645
Dvctr * resize_to_asize(void)
Definition: arrays.h:628
void cpat(long i, const Xvctr *p, long j)
Definition: arrays.h:506
VectoR * be_rot_by_angle_2d(const VectoR *p, double a)
Definition: arrays.h:196
virtual ~Dmtrx()
DESTRUCTOR.
Definition: arrays.h:811
Dvctr(const Dvctr *src)
Definition: arrays.h:591
bool SP_skip_word(const char *&src, int n)
... word and space before
Definition: librw.cpp:591
double & at32()
Definition: arrays.h:265
Lvctr(void)
CONSTRUCTOR.
Definition: arrays.h:442
virtual int length_printed(int precision) const
Definition: arrays.h:379
void copy_to_row_3(const VectoR *v)
Definition: arrays.h:273
#define PYP
Definition: arrays.h:21
Lvctr * resize_to_asize(void)
resize to asize
Definition: arrays.h:501
virtual ~Xvctr()
DESTRUCTOR.
Definition: arrays.h:403
const long & operator()(long r, long c) const
Definition: arrays.h:784
arrayTypedef give_arrayTypedef(void) const
Definition: arrays.h:369
void shift(long val)
Definition: arrays.h:640
void shift(long val)
Definition: arrays.h:511
double give_sum(void) const
sum of components
Definition: arrays.h:83
bool is_identical_to(const Elem3D *p, double zero) const
Definition: arrays.h:55