muMECH  1.0
arrays.cpp
Go to the documentation of this file.
1 #include "arrays.h"
2 
3 #include "gelib.h"
4 
5 #include <stdlib.h>
6 #include <string.h>
7 #include <math.h>
8 
9 
10 namespace gelibspace {
11 
13 double give_copy_r2z (double a, double zero)
14 {
15  return (-zero < a && a < zero) ? 0.0 : a ;
16 }
17 double* give_copy_r2z (double *a, long n, double zero)
18 {
19  if (zero == 0.0) return a;
20  double *p = new double[n];
21  while (n--) p[n] = give_copy_r2z (a[n], zero);
22  return p;
23 }
24 
25 
26 // ********************************************************************************************
27 // *** *** *** *** CLASS MatriX *** *** *** ***
28 // ********************************************************************************************
30 void MatriX :: rotate (Dvctr *v) const
31 {
32  long vs = v->give_size();
33  if (vs != 3 && vs != 6) errol;
34 
35  ; this->rotate(v->give_ptr2val(0));
36  if (vs == 6) this->rotate(v->give_ptr2val(3));
37 }
38 
40 void MatriX :: rotate (double *v) const
41 {
42  double x = a[0] * v[0] + a[1] * v[1] + a[2] * v[2];
43  double y = a[3] * v[0] + a[4] * v[1] + a[5] * v[2];
44  double z = a[6] * v[0] + a[7] * v[1] + a[8] * v[2];
45 
46  v[0] = x;
47  v[1] = y;
48  v[2] = z;
49 }
50 
51 
52 //* ********************************************************************************************
53 //* *** *** *** *** CLASS ELEM3D *** *** *** ***
54 //* ********************************************************************************************
55 
56 //* ********************************************************************************************
57 //* *** *** *** *** CLASS POINT *** *** *** ***
58 //* ********************************************************************************************
60 void PoinT :: beRotatedPoint (const Dmtrx *rot, const PoinT *point)
61 {
62 #ifdef DEBUG
63  if (rot->give_crows() != 3 || rot->give_ccols() != 3) errol;
64 #endif
65  const double *r = rot->give_ptr2val();
66 
67  x = point->x * r[0] + point->y * r[1] + point->z * r[2];
68  y = point->x * r[3] + point->y * r[4] + point->z * r[5];
69  z = point->x * r[6] + point->y * r[7] + point->z * r[8];
70 }
72 void PoinT :: beRotatedBy (const double *r)
73 {
74  double xx = x * r[0] + y * r[1] + z * r[2];
75  double yy = x * r[3] + y * r[4] + z * r[5];
76  double zz = x * r[6] + y * r[7] + z * r[8];
77  x = xx;
78  y = yy;
79  z = zz;
80 }
82 void PoinT :: beRotatedBy2d (const double *r)
83 {
84  double xx = x * r[0] + y * r[1];
85  double yy = x * r[2] + y * r[3];
86  x = xx;
87  y = yy;
88  z = 0.0;
89 }
90 
91 //* ********************************************************************************************
92 //* *** *** *** *** CLASS DSCAL *** *** *** ***
93 //* ********************************************************************************************
94 void Dscal :: print (char *stream, int precision, double absZero) const
95 {
96  sprintf (stream, "% .*e", precision, (absZero != 0.0) ? give_copy_r2z (a, absZero) : a);
97  stream += 2+1+precision+2+NUM_DIGITS_IN_PRINTED_EXPONENT;
98 }
99 
100 
101 
102 double Xvctr :: give_zero (double abszero, double relzero) const
103 {
104  relzero *= this->give_lenght();
105  return (relzero > abszero ? relzero : abszero);
106 }
107 
108 //* ********************************************************************************************
109 //* *** *** *** *** CLASS LVCTR *** *** *** ***
110 //* ********************************************************************************************
111 
112 //* *** SET ***
114 void Lvctr :: realloc (long newsize)
115 {
116  if (newsize <= asize) return;
117 
118  asize = newsize;
119  long* newa = new long[asize];
120 
121  for (newsize=0; newsize<size; newsize++) *newa++ = *a++;
122 
123  delete [] (a-size);
124  a = newa-size;
125 }
126 
129 {
130  if (a) errol;
131  cnst = false;
132  exta = true;
133  size = s;
134  asize = 0;
135  a = array;
136  return this;
137 }
138 Lvctr* Lvctr :: assign_array (const long *array, long s)
139 {
140  if (a) errol;
141  cnst = true;
142  exta = true;
143  size = s;
144  asize = 0;
145  a = (long *)array;
146  return this;
147 }
149  if (a && !exta) delete [] a;
150  a = NULL;
151  exta = cnst = false;
152  shft = size = asize = 0;
153  return this;
154 }
155 
156 //* *** COMPUTE ***
157 double Lvctr :: give_lenght (void) const
158 {
159  double length = 0.0;
160  for (long i=0; i<size; i++)
161  length += a[i] * a[i];
162 
163  return sqrt(length);
164 }
165 long Lvctr :: give_sum (void) const { long sum = 0; for (long i=0; i<size; i++) sum += a[i]; return sum; }
166 long Lvctr :: give_number_of_nonzeros (void) const { long sum = 0; for (long i=0; i<size; i++) if ( a[i]) sum++; return sum; }
167 long Lvctr :: give_number_of_zeros (void) const { long sum = 0; for (long i=0; i<size; i++) if (!a[i]) sum++; return sum; }
168 
169 
170 //* ***
171 //* *** PRINT ***
172 //* ***
173 int Lvctr :: length_printed (int precision) const { return size * (25+1); }
174 int Lvctr :: length_printed_vector (int precision) const { return 3 * (25+1); }
175 int Lvctr :: length_printed_tensor (int precision) const { return 9 * (25+1); }
176 
177 //* *** VECTOR
178 #ifdef DEBUG
179 void Lvctr :: printYourself (bool force) const
181 {
182  if (size > 20 && !force) _errorr("Count of rows is > 20, to force printing set first parameter as true");
183 
184  fprintf (stdout, "Long vector [%ld]:\n", size);
185 
186  for (long i=0; i<size; i++) fprintf (stdout, "%ld ", a[i]);
187 
188  fprintf (stdout, "\n");
189 }
190 #endif
191 
193 void Lvctr :: print (char *stream, int precision, double absZero) const
194 {
195  if (size < 1) errol;
196 
197  int numlen = precision+1;
198 
199  ; sprintf (stream, "%ld", a[0]); stream += numlen-1;
200  for (long i=1; i<size; i++) { sprintf (stream, " %*ld", precision, a[i]); stream += numlen; }
201 }
203 void Lvctr :: print_vector (char *stream, int precision, double absZero, bool zerorest) const
204 {
205  if (zerorest) { if (size < 0) errol; }
206  else if (size < 3) errol;
207 
208  sprintf (stream, "%*ld %*ld %*ld",
209  precision, size>=1 ? a[0] : 0,
210  precision, size>=2 ? a[1] : 0,
211  precision, size>=3 ? a[2] : 0);
212 }
213 
214 //* *** TENSOR
216 void pprint_symtensor (char *stream, int precision, const long *a)
217 {
218  sprintf (stream, "%*ld %*ld %*ld\n", precision, a[0], precision, a[5], precision, a[4]); while (stream[0] != '\0') stream++;
219  sprintf (stream, "%*ld %*ld %*ld\n", precision, a[5], precision, a[1], precision, a[3]); while (stream[0] != '\0') stream++;
220  sprintf (stream, "%*ld %*ld %*ld\n", precision, a[4], precision, a[3], precision, a[2]);
221 }
222 void pprint_symtensor_zeroL (char *stream, int precision)
223 {
224  sprintf (stream, "%*d %*d %*d\n", precision, 0, precision, 0, precision, 0); while (stream[0] != '\0') stream++;
225  sprintf (stream, "%*d %*d %*d\n", precision, 0, precision, 0, precision, 0); while (stream[0] != '\0') stream++;
226  sprintf (stream, "%*d %*d %*d\n", precision, 0, precision, 0, precision, 0);
227 }
228 void pprint_symtensor (FILE *stream, int precision, const long *a)
229 {
230  fprintf (stream, "%*ld %*ld %*ld\n", precision, a[0], precision, a[5], precision, a[4]);
231  fprintf (stream, "%*ld %*ld %*ld\n", precision, a[5], precision, a[1], precision, a[3]);
232  fprintf (stream, "%*ld %*ld %*ld\n", precision, a[4], precision, a[3], precision, a[2]);
233 }
234 void pprint_symtensor_zeroL (FILE *stream, int precision)
235 {
236  fprintf (stream, "%*d %*d %*d\n", precision, 0, precision, 0, precision, 0);
237  fprintf (stream, "%*d %*d %*d\n", precision, 0, precision, 0, precision, 0);
238  fprintf (stream, "%*d %*d %*d\n", precision, 0, precision, 0, precision, 0);
239 }
240 
241 void Lvctr :: print_symtensor (char *stream, int precision, double absZero, bool zerorest) const
242 {
243  if (zerorest) { if (size != 0 && size != 6) errol; }
244  else if (size != 6) errol;
245 
246  if (size) pprint_symtensor (stream, precision, a);
247  else pprint_symtensor_zeroL (stream, precision);
248 }
249 void Lvctr :: print_symtensor (FILE *stream, int precision, double absZero, bool zerorest) const
250 {
251  if (zerorest) { if (size != 0 && size != 6) errol; }
252  else if (size != 6) errol;
253 
254  if (size) pprint_symtensor (stream, precision, a);
255  else pprint_symtensor_zeroL (stream, precision);
256 }
257 
258 
259 
260 //* ********************************************************************************************
261 //* *** *** *** *** CLASS DVCTR *** *** *** ***
262 //* ********************************************************************************************
263 //* *** SET ***
265 void Dvctr :: realloc (long newsize)
266 {
267  if (newsize <= asize) return;
268 
269  asize = newsize;
270  double* newa = new double[asize];
271 
272  for (newsize=0; newsize<size; newsize++) *newa++ = *a++;
273 
274  delete [] (a-size);
275  a = newa-size;
276 }
277 
280 {
281  if (a) errol;
282  cnst = false;
283  exta = true;
284  size = s;
285  asize = 0;
286  a = array;
287  return this;
288 }
289 Dvctr* Dvctr :: assign_array (const double *array, long s)
290 {
291  if (a)
292  errol;
293  cnst = true;
294  exta = true;
295  size = s;
296  asize = 0;
297  a = (double *)array;
298  return this;
299 }
301  if (a && !exta) delete [] a;
302  a = NULL;
303  exta = cnst = false;
304  shft = size = asize = 0;
305  return this;
306 }
307 
308 //* *** COMPUTE ***
309 double Dvctr :: give_lenght (void) const
310 {
311 # ifdef DEBUG
312  if (size == 0) _warningg ("size = 0 in give_length function");
313 # endif
314 
315  double length = 0.0;
316  for (long i=0; i<size; i++)
317  length += a[i] * a[i];
318 
319  return sqrt(length);
320 }
321 double Dvctr :: give_sum (void) const
322 {
323  double sum = 0;
324  for (long i=0; i<size; i++)
325  sum += a[i];
326 
327  return sum;
328 }
329 
330 //* *** EDIT ***
332 void Dvctr :: be_tnsr (const Dmtrx &src)
333 {
334  AVOID_CNST;
335  if ( src.give_crows() != 3 && src.give_ccols() != 3 ) _errorr ("size of mtrx != 3x3");
336 
337  const double *asrc = src.give_ptr2val(0,0);
338  this->resize_ignore_vals(6);
339 
340  a[0] = asrc[0];
341  a[1] = asrc[4];
342  a[2] = asrc[8];
343 
344  a[3] = asrc[5];
345  a[4] = asrc[2];
346  a[5] = asrc[1];
347 }
348 
350 void Dvctr :: be_vectproduct (const Dvctr &v1, const Dvctr &v2)
351 {
352  AVOID_CNST;
353  if ( v1.give_size() != 3 || v2.give_size() != 3 )
354  _errorr ("size of vectors is not equal to 3");
355 
356  this->resize_ignore_vals(3);
357 
358  const double *a1 = v1.give_ptr2val();
359  const double *a2 = v2.give_ptr2val();
360 
361  a[0] = a1[1] * a2[2] - a2[1] * a1[2];
362  a[1] = a1[2] * a2[0] - a2[2] * a1[0];
363  a[2] = a1[0] * a2[1] - a2[0] * a1[1];
364 }
365 
367 void Dvctr :: be_mean_of (const Dmtrx *src)
368 {
369  long n = src->give_crows();
370  long m = src->give_ccols();
371  const double *psrc = src->give_ptr2val();
372 
373  this->beCopyOf(psrc, m);
374 
375  if (n>1) {
376  psrc += m;
377  while (--n)
378  for (long i=0; i<m; i++)
379  a[i] += *(psrc++);
380 
381  this->dvdby(src->give_crows());
382  }
383 }
384 
386 double Dvctr :: give_dotproduct (const Dvctr &v) const
387 {
388  double prd = 0.0;
389  for (long i=0; i<this->size; i++)
390  prd += a[i] * v.a[i];
391 
392  return prd;
393 }
394 
395 
397 void Dvctr :: add (const Dvctr &src) { AVOID_CNST; if (size != src.give_size()) _errorr("size mismatch"); const double *asrc = src.give_ptr2val(); for (long i=0; i<size; i++) a[i] += asrc[i]; }
398 void Dvctr :: addtms (const Dvctr &src, double tms) { AVOID_CNST; if (size != src.give_size()) _errorr("size mismatch"); const double *asrc = src.give_ptr2val(); for (long i=0; i<size; i++) a[i] += tms * asrc[i]; }
399 void Dvctr :: sbt (const Dvctr &src) { AVOID_CNST; if (size != src.give_size()) _errorr("size mismatch"); const double *asrc = src.give_ptr2val(); for (long i=0; i<size; i++) a[i] -= asrc[i]; }
401 void Dvctr :: dvdby (double val)
402 {
403  AVOID_CNST;
404  for (long i=0; i<size; i++)
405  a[i] = a[i] / val;
406 }
408 void Dvctr :: tmsby (double val)
409 {
410  AVOID_CNST;
411  for (long i=0; i<size; i++)
412  a[i] = a[i] * val;
413 }
415 //void Dvctr :: tnsrRotWith1 (const Dmtrx &rot)
416 //{
417 // if ( size != 6 ) _errorr ("size of vectors is not equal to 6");
418 //
419 // Dmtrx sss, rotT;
420 //
421 // sss.be_tnsr(*this);
422 // rotT.be_transposition(rot);
423 //
424 // sss.tmsLeftBy (rot);
425 // sss.tmsRightBy (rotT);
426 //
427 // this->be_tnsr(sss);
428 //}
430 void Dvctr :: tnsrRotWith (const Dmtrx &rot)
431 {
432  AVOID_CNST;
433  if ( size != 6 ) _errorr ("size of vectors is not equal to 6");
434 
435  double aa[6];
436 
437  aa[0] = rot(0,0)*rot(0,0) * a[0] + rot(0,1)*rot(0,1) * a[1] + rot(0,2)*rot(0,2) * a[2] + 2*rot(0,0)*rot(0,1) * a[5] + 2*rot(0,0)*rot(0,2) * a[4] + 2*rot(0,1)*rot(0,2) * a[3];
438  aa[1] = rot(1,0)*rot(1,0) * a[0] + rot(1,1)*rot(1,1) * a[1] + rot(1,2)*rot(1,2) * a[2] + 2*rot(1,0)*rot(1,1) * a[5] + 2*rot(1,0)*rot(1,2) * a[4] + 2*rot(1,1)*rot(1,2) * a[3];
439  aa[2] = rot(2,0)*rot(2,0) * a[0] + rot(2,1)*rot(2,1) * a[1] + rot(2,2)*rot(2,2) * a[2] + 2*rot(2,0)*rot(2,1) * a[5] + 2*rot(2,0)*rot(2,2) * a[4] + 2*rot(2,1)*rot(2,2) * a[3];
440 
441  aa[5] = rot(0,0)*rot(1,0) * a[0] + rot(0,1)*rot(1,1) * a[1] + rot(0,2)*rot(1,2) * a[2] + (rot(0,0)*rot(1,1) + rot(0,1)*rot(1,0)) * a[5] + (rot(0,1)*rot(1,2) + rot(0,2)*rot(1,1)) * a[3] + (rot(0,0)*rot(1,2) + rot(0,2)*rot(1,0)) * a[4];
442  aa[3] = rot(1,0)*rot(2,0) * a[0] + rot(1,1)*rot(2,1) * a[1] + rot(1,2)*rot(2,2) * a[2] + (rot(1,0)*rot(2,1) + rot(1,1)*rot(2,0)) * a[5] + (rot(1,1)*rot(2,2) + rot(1,2)*rot(2,1)) * a[3] + (rot(1,0)*rot(2,2) + rot(1,2)*rot(2,0)) * a[4];
443  aa[4] = rot(0,0)*rot(2,0) * a[0] + rot(0,1)*rot(2,1) * a[1] + rot(0,2)*rot(2,2) * a[2] + (rot(0,0)*rot(2,1) + rot(0,1)*rot(2,0)) * a[5] + (rot(0,1)*rot(2,2) + rot(0,2)*rot(2,1)) * a[3] + (rot(0,0)*rot(2,2) + rot(0,2)*rot(2,0)) * a[4];
444 
445  this->beCopyOf(aa);
446 }
447 
449 //void Dvctr :: tnsrRotAxisZangle (double a)
450 //{
451 // Dmtrx rot(3,3);
452 // rot(0,0) = cos(a); rot(0,1) = -sin(a); rot(0,2) = 0;
453 // rot(1,0) = sin(a); rot(1,1) = cos(a); rot(1,2) = 0;
454 // rot(2,0) = 0; rot(2,1) = 0; rot(2,2) = 1;
455 //
456 // this->tnsrRotWith(rot);
457 //}
459 void Dvctr :: tnsrRotAxisZangle (double alpha)
460 {
461  AVOID_CNST;
462  if ( size != 6 ) _errorr ("size of vectors is not equal to 6");
463 
464  double c = cos(alpha);
465  double s = sin(alpha);
466 
467  double aa[6];
468 
469  aa[0] = c*c * a[0] + s*s * a[1] - 2*c*s * a[5] ;
470  aa[1] = s*s * a[0] + c*c * a[1] + 2*s*c * a[5] ;
471  aa[2] = a[2] ;
472 
473  aa[5] = c*s * a[0] - s*c * a[1] + (c*c - s*s) * a[5] ;
474  aa[3] = c * a[3] + s * a[4];
475  aa[4] = -s * a[3] + c * a[4];
476 
477  this->beCopyOf(aa);
478 }
479 
480 
481 
484 {
485  AVOID_CNST;
486  double length = this->give_lenght();
487  this->dvdby(length);
488 }
489 
491 double Dvctr :: compute_squared_norm (void) const
492 {
493  double *p = this->a;
494  int i = this->size;
495  double norm2 = 0.0;
496  while (i--) {
497  norm2 += (*p) * (*p);
498  p++;
499  }
500 
501  return norm2;
502 }
503 
505 //void Dvctr :: round2zero (double relZero, double absZero)
506 //{
507 // AVOID_CNST;
508 // double l;
509 // l = this->give_lenght();
510 // for (long i=0; i<size; i++)
511 // if (fabs(a[i]/l) < relZero || fabs(a[i]) < absZero) a[i] = 0.0;
512 //
513 //}
514 
515 
517 //void Dvctr :: round2zero (double relZero, double absZero)
518 //{
519 // AVOID_CNST;
520 // double l;
521 // l = this->give_lenght();
522 // for (long i=0; i<size; i++)
523 // if (fabs(a[i]/l) < relZero || fabs(a[i]) < absZero) a[i] = 0.0;
524 //
525 //}
526 
527 //if (zero < 0.0) {
528 // double l = 0.0;
529 // for (long i=0; i<n; i++) l += a[i] * a[i];
530 // l = sqrt(l) * zero;
531 //
532 // if ()
533 //
534 
535 
536 
537 
538 //* ***
539 //* *** PRINT ***
540 //* ***
541 int Dvctr :: length_printed (int precision) const { return size * (1+2+1+precision+2+NUM_DIGITS_IN_PRINTED_EXPONENT) + 1; }
542 int Dvctr :: length_printed_vector (int precision) const { return 3 * (1+2+1+precision+2+NUM_DIGITS_IN_PRINTED_EXPONENT) + 1; }
543 int Dvctr :: length_printed_tensor (int precision) const { return 9 * (1+2+1+precision+2+NUM_DIGITS_IN_PRINTED_EXPONENT) + 1; }
544 
545 //* *** VECTOR
546 #ifdef DEBUG
547 void Dvctr :: printYourself (bool force) const
549 {
550  if (size > 20 && !force) _errorr("Count of rows is > 20, to force printing set first parameter as true");
551 
552  fprintf (stdout, "Double vector [%ld]:\n", size);
553 
554  for (long i=0; i<size; i++) fprintf (stdout, "% .*e ", PYP, a[i]);
555 
556  fprintf (stdout, "\n");
557 }
558 #endif
559 
561 void Dvctr :: print (char *stream, int precision, double absZero) const
562 {
563  if (size < 1) errol;
564 
565  double *p = a;
566  if (absZero != 0.0) p = give_copy_r2z (a, size, absZero);
567 
568  int numlen = 1+2+1+precision+2+NUM_DIGITS_IN_PRINTED_EXPONENT;
569 
570  ; sprintf (stream, "% .*e", precision, p[0]); stream += numlen-1;
571  for (long i=1; i<size; i++) { sprintf (stream, " % .*e", precision, p[i]); stream += numlen; }
572 
573  if (p != a) delete [] p;
574 }
576 void Dvctr :: print_vector (char *stream, int precision, double absZero, bool zerorest) const
577 {
578  if (zerorest) { if (size < 0) errol; }
579  else if (size < 3) errol;
580 
581  double *p = a;
582  if (absZero != 0.0 && size) p = give_copy_r2z (a, 3, absZero);
583 
584  sprintf (stream, "% .*e % .*e % .*e",
585  precision, size>=1 ? p[0] : 0.0,
586  precision, size>=2 ? p[1] : 0.0,
587  precision, size>=3 ? p[2] : 0.0);
588 
589  if (p != a) delete [] p;
590 }
591 
592 //* *** TENSOR
593 
595 void pprint_tensor (FILE *stream, int precision, const double *a)
596 {
597  fprintf (stream, "% .*e % .*e % .*e\n", precision, a[0], precision, a[1], precision, a[2]);
598  fprintf (stream, "% .*e % .*e % .*e\n", precision, a[3], precision, a[4], precision, a[5]);
599  fprintf (stream, "% .*e % .*e % .*e\n", precision, a[6], precision, a[7], precision, a[8]);
600 }
601 void pprint_symtensor (char *stream, int precision, const double *a)
602 {
603  sprintf (stream, "% .*e % .*e % .*e\n", precision, a[0], precision, a[5], precision, a[4]); while (stream[0] != '\0') stream++;
604  sprintf (stream, "% .*e % .*e % .*e\n", precision, a[5], precision, a[1], precision, a[3]); while (stream[0] != '\0') stream++;
605  sprintf (stream, "% .*e % .*e % .*e\n", precision, a[4], precision, a[3], precision, a[2]);
606 }
607 void pprint_symtensor (FILE *stream, int precision, const double *a)
608 {
609  fprintf (stream, "% .*e % .*e % .*e\n", precision, a[0], precision, a[5], precision, a[4]);
610  fprintf (stream, "% .*e % .*e % .*e\n", precision, a[5], precision, a[1], precision, a[3]);
611  fprintf (stream, "% .*e % .*e % .*e\n", precision, a[4], precision, a[3], precision, a[2]);
612 }
613 
614 void pprint_tensor_zero (char *stream, int precision)
615 {
616  sprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0); while (stream[0] != '\0') stream++;
617  sprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0); while (stream[0] != '\0') stream++;
618  sprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
619 }
620 void pprint_tensor_zero (FILE *stream, int precision)
621 {
622  fprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
623  fprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
624  fprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
625 }
626 
627 
628 
629 void Dvctr :: print_symtensor (char *stream, int precision, double absZero, bool zerorest) const
630 {
631  if (zerorest) { if (size != 0 && size != 6) errol; }
632  else if (size != 6) errol;
633 
634  double *p = a;
635  if (absZero != 0.0 && size) p = give_copy_r2z (a, 6, absZero);
636 
637  if (size) pprint_symtensor (stream, precision, p);
638  else pprint_tensor_zero (stream, precision);
639 
640  if (p != a) delete [] p;
641 }
642 void Dvctr :: print_symtensor (FILE *stream, int precision, double absZero, bool zerorest) const
643 {
644  if (zerorest) { if (size != 0 && size != 6) errol; }
645  else if (size != 6) errol;
646 
647  double *p = a;
648  if (absZero != 0.0 && size) p = give_copy_r2z (a, 6, absZero);
649 
650  if (size) pprint_symtensor (stream, precision, p);
651  else pprint_tensor_zero (stream, precision);
652 
653  if (p != a) delete [] p;
654 }
655 
656 
658 //void Dvctr :: print_symtensor (char *stream, int precision) const
659 //{
660 // sprintf (stream, "% .*e % .*e % .*e\n", precision, a[0], precision, a[5], precision, a[4]); while (stream[0] != '\0') stream++;
661 // sprintf (stream, "% .*e % .*e % .*e\n", precision, a[5], precision, a[1], precision, a[3]); while (stream[0] != '\0') stream++;
662 // sprintf (stream, "% .*e % .*e % .*e\n", precision, a[4], precision, a[3], precision, a[2]);
663 //}
664 //void Dvctr :: print_symtensor_zero (char *stream, int precision) const
665 //{
666 // sprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0); while (stream[0] != '\0') stream++;
667 // sprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0); while (stream[0] != '\0') stream++;
668 // sprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
669 //}
671 //void Dvctr :: print_symtensor (FILE *stream, int precision) const
672 //{
673 // fprintf (stream, "% .*e % .*e % .*e\n", precision, a[0], precision, a[5], precision, a[4]);
674 // fprintf (stream, "% .*e % .*e % .*e\n", precision, a[5], precision, a[1], precision, a[3]);
675 // fprintf (stream, "% .*e % .*e % .*e\n", precision, a[4], precision, a[3], precision, a[2]);
676 //}
677 //void Dvctr :: print_symtensor_zero (FILE *stream, int precision) const
678 //{
679 // fprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
680 // fprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
681 // fprintf (stream, "% .*e % .*e % .*e\n", precision, 0.0, precision, 0.0, precision, 0.0);
682 //}
683 
684 
685 
686 
687 
688 //* ********************************************************************************************
689 //* *** *** *** *** CLASS LMTRX *** *** *** ***
690 //* ********************************************************************************************
691 
692 //* *************
693 //* *** SET ***
694 //* *************
696 void Lmtrx :: resize_ignore_vals (long r, long c)
697 {
698  if (crow == r && ccol == c) return;
699 
700  crow = r; ccol = c;
701 
702  if (asize < crow*ccol) {
703  if (a) delete [] a;
704  asize = crow*ccol;
705  a = new long[asize];
706  }
707 }
708 
709 
710 //* ***************
711 //* *** PRINT ***
712 //* ***************
714 #ifdef DEBUG
715 void Lmtrx :: printYourself (bool force) const
717 {
718  if (crow > 50) _warningg("count of rows is > 50");
719  if (ccol > 50) _warningg("count of collumns is > 50");
720 
721  fprintf (stdout, "Long matrix [%ld,%ld]:\n", crow, ccol);
722 
723  long i, j;
724  for (i=0; i<crow; i++) {
725  for (j=0; j<ccol; j++)
726  fprintf (stdout, "%ld", a[i*ccol + j]);
727 
728  fprintf (stdout, "\n");
729  }
730 }
731 #endif
732 
733 
734 
735 //* ********************************************************************************************
736 //* *** *** *** *** CLASS DMTRX *** *** *** ***
737 //* ********************************************************************************************
738 
739 //* *************
740 //* *** SET ***
741 //* *************
742 void Dmtrx :: resize_ignore_vals (long r, long c)
743 {
744  if (crow == r && ccol == c) return;
745 
746  crow = r; ccol = c;
747 
748  if (asize < crow*ccol) {
749  if (a) delete [] a;
750  asize = crow*ccol;
751  a = new double[asize];
752  }
753 }
754 
755 
756 //* **************
757 //* *** EDIT ***
758 //* **************
760 void Dmtrx :: be_tnsr (const Dvctr &src)
761 {
762  if ( src.give_size() != 6 ) _errorr ("size of vectors is not equal to 6");
763 
764  const double *asrc = src.give_ptr2val();
765  this->resize_ignore_vals(3,3);
766 
767  a[0] = asrc[0];
768  a[4] = asrc[1];
769  a[8] = asrc[2];
770 
771  a[5] = a[7] = asrc[3];
772  a[2] = a[6] = asrc[4];
773  a[1] = a[3] = asrc[5];
774 }
777 {
778  this->resize_ignore_vals(src.give_ccols(), src.give_crows());
779 
780  const double *asrc = src.give_ptr2val(0,0);
781  long i, j;
782  for (i=0; i<crow; i++)
783  for (j=0; j<ccol; j++)
784  a[i*ccol + j] = asrc[j*ccol + i];
785 
786 }
788 void Dmtrx :: be_mtrxMmtrx (const Dmtrx &A, const Dmtrx &B)
789 {
790  if ( A.give_ccols() != B.give_crows() ) _errorr("size mismatch");
791  long ccr = A.give_ccols();
792 
793  this->resize_ignore_vals(A.give_crows(), B.give_ccols());
794  this->zero();
795 
796  const double *aA = A.give_ptr2val(0,0);
797  const double *aB = B.give_ptr2val(0,0);
798 
799  long i, j, k;
800  for (i=0; i<crow; i++)
801  for (j=0; j<ccol; j++)
802  for (k=0; k<ccr; k++)
803  a[i*ccol + j] += aA[i*ccr+k] * aB[k*ccol+j];
804 
805 }
807 void Dmtrx :: tmsLeftBy (const Dmtrx &src)
808 {
809  Dmtrx mtrxXthis;
810  mtrxXthis.be_mtrxMmtrx (src, *this);
811 
812  this->beCopyOf(mtrxXthis);
813 }
815 void Dmtrx :: tmsRightBy (const Dmtrx &src)
816 {
817  Dmtrx thisXmtrx;
818  thisXmtrx.be_mtrxMmtrx (*this, src);
819 
820  this->beCopyOf(thisXmtrx);
821 }
822 
823 
824 //* *****************
825 //* *** COMPUTE ***
826 //* *****************
828 double Dmtrx :: give_determinant (void) const
829 {
830  if (crow == ccol && ccol == 2) return a[0] * a[3] - a[1] * a[2];
831  else if (crow == ccol && ccol == 3) return a[0] * a[4] * a[8] + a[3] * a[7] * a[2] +
832  a[6] * a[1] * a[5] - a[6] * a[4] * a[2] -
833  a[7] * a[5] * a[0] - a[8] * a[3] * a[1] ;
834  else _errorr3("cannot compute determinant of matrix %ld x %ld", crow, ccol);
835  return 0.0;
836 }
837 
842 bool Dmtrx :: GaussSolve (Dvctr &x, const Dvctr &rhs)
843 {
844 #ifdef DEBUG
845  if (crow != ccol) _errorr3 ("matrix is not square, %ld != %ld", crow, ccol);
846  if (crow != rhs.give_size()) _errorr3 ("matrix and rhs dimension differs, %ld != %ld", crow, rhs.give_size());
847 #endif
848 
849  int i, j, k, maxrow;
850  double tmp;
851 
852  x = rhs;
853 
854  for (i=0; i<crow-1; i++) {
855 
856  // Find the row with the largest first value
857  maxrow = i;
858  for (j=i+1; j<crow; j++)
859  if (fabs(a[j*ccol+i]) > fabs(a[maxrow*ccol+i]))
860  maxrow = j;
861 
862  // Singular matrix?
863  if (fabs(a[maxrow*ccol+i]) < 1.0e-15)
864  return(false);
865 
866  // Swap the maxrow and ith row
867  if (maxrow != i) {
868  for (j=i; j<crow; j++) {
869  tmp = a[i*ccol+j];
870  a[i*ccol+j] = a[maxrow*ccol+j];
871  a[maxrow*ccol+j] = tmp;
872  }
873 
874  tmp = x[i];
875  x[i] = x[maxrow];
876  x[maxrow] = tmp;
877  }
878 
879  // Eliminate the ith element of the jth row
880  for (j=i+1; j<crow; j++) {
881  tmp = a[j*ccol+i] / a[i*ccol+i];
882 
883  for (k=i; k<crow; k++)
884  a[j*ccol+k] -= a[i*ccol+k] * tmp;
885 
886  x[j] -= x[i] * tmp;
887  }
888  }
889 
890  // Singular matrix?
891  if (fabs(a[crow*ccol-1]) < 1.0e-15)
892  return(false);
893 
894  // Do the back substitution
895  for (i=crow-1; i>=0; i--) {
896  tmp = 0;
897  for (j=i+1; j<crow; j++)
898  tmp += a[i*ccol+j] * x[j];
899 
900  x[i] = (x[i] - tmp) / a[i*ccol+i];
901  }
902 
903  return true;
904 }
905 
906 
911 bool Dmtrx :: GaussSolve (Dvctr *x, const Dvctr *rhs, long nrhs)
912 {
913 #ifdef DEBUG
914  if (crow != ccol) _errorr3 ("matrix is not square, %ld != %ld", crow, ccol);
915  if (crow != rhs[0].give_size()) _errorr3 ("matrix and rhs dimension differs, %ld != %ld", crow, rhs[0].give_size());
916 #endif
917 
918  int i, j, k, n, maxrow;
919  double tmp;
920 
921  for (n=0; n<nrhs; n++) x[n].beCopyOf(rhs[n]);
922 
923  for (i=0; i<crow-1; i++) {
924 
925  // Find the row with the largest first value
926  maxrow = i;
927  for (j=i+1; j<crow; j++)
928  if (fabs(a[j*ccol+i]) > fabs(a[maxrow*ccol+i]))
929  maxrow = j;
930 
931  // Singular matrix?
932  if (fabs(a[maxrow*ccol+i]) < 1.0e-15)
933  return(false);
934 
935  // Swap the maxrow and ith row
936  if (maxrow != i) {
937  for (j=i; j<crow; j++) {
938  tmp = a[i*ccol+j];
939  a[i*ccol+j] = a[maxrow*ccol+j];
940  a[maxrow*ccol+j] = tmp;
941  }
942 
943  for (n=0; n<nrhs; n++) {
944  tmp = x[n][i];
945  x[n][i] = x[n][maxrow];
946  x[n][maxrow] = tmp;
947  }
948  }
949 
950  // Eliminate the ith element of the jth row
951  for (j=i+1; j<crow; j++) {
952  tmp = a[j*ccol+i] / a[i*ccol+i];
953 
954  for (k=i; k<crow; k++)
955  a[j*ccol+k] -= a[i*ccol+k] * tmp;
956 
957  for (n=0; n<nrhs; n++)
958  x[n][j] -= x[n][i] * tmp;
959  }
960  }
961 
962  // Singular matrix?
963  if (fabs(a[crow*ccol-1]) < 1.0e-15)
964  return(false);
965 
966  // Do the back substitution
967  for (i=crow-1; i>=0; i--) {
968  for (n=0; n<nrhs; n++) {
969  tmp = 0;
970  for (j=i+1; j<crow; j++)
971  tmp += a[i*ccol+j] * x[n][j];
972 
973  x[n][i] = (x[n][i] - tmp) / a[i*ccol+i];
974  }
975  }
976 
977  return true;
978 }
979 
980 
981 //* ***************
982 //* *** PRINT ***
983 //* ***************
985 #ifdef DEBUG
986 void Dmtrx :: printYourself (bool force) const
988 {
989  if (crow > 10) _errorr("count of rows is > 10");
990  if (ccol > 10) _errorr("count of collumns is > 10");
991 
992  fprintf (stdout, "Double matrix [%ld,%ld]:\n", crow, ccol);
993 
994  long i, j;
995  for (i=0; i<crow; i++) {
996  for (j=0; j<ccol; j++)
997  fprintf (stdout, "% .*e ", PYP, a[i*ccol + j]);
998 
999  fprintf (stdout, "\n");
1000  }
1001 }
1002 #endif
1003 
1004 
1005 //void Dmtrx :: print_tensor (FILE *stream, int precision) const
1006 //{
1007 // fprintf (stream, "% .*e % .*e % .*e\n", precision, a[0], precision, a[1], precision, a[2]);
1008 // fprintf (stream, "% .*e % .*e % .*e\n", precision, a[3], precision, a[4], precision, a[5]);
1009 // fprintf (stream, "% .*e % .*e % .*e\n", precision, a[6], precision, a[7], precision, a[8]);
1010 //}
1011 
1012 void Dmtrx :: print_tensor (FILE *stream, int precision, double absZero, bool zerorest) const
1013 {
1014  if (zerorest) { if (! ((crow == 0 && ccol == 0) || (crow == 3 && ccol == 3)) ) errol; }
1015  else if (crow != 3 || ccol != 3) errol;
1016 
1017  double *p = a;
1018  if (absZero != 0.0 && crow) p = give_copy_r2z (a, 9, absZero);
1019 
1020  if (crow) pprint_tensor (stream, precision, p);
1021  else pprint_tensor_zero (stream, precision);
1022 
1023  if (p != a) delete [] p;
1024 }
1025 
1026 
1027 } // namespace gelibspace
void print_symtensor(char *stream, int precision, double absZero=0.0, bool zerorest=false) const
Definition: arrays.cpp:241
double give_dotproduct(const Dvctr &v) const
Definition: arrays.cpp:386
#define _warningg(_1)
Definition: gelib.h:163
void print(char *stream, int precision, double absZero=0.0) const
Definition: arrays.cpp:561
void realloc(long newsize)
reallocate up receiver
Definition: arrays.cpp:265
General functions.
void be_mean_of(const Dmtrx *src)
Definition: arrays.cpp:367
virtual long give_size(void) const
Definition: arrays.h:414
double compute_squared_norm(void) const
Definition: arrays.cpp:491
virtual void print(char *stream, int precision, double absZero=0.0) const
Definition: arrays.cpp:94
void print(char *stream, int precision, double absZero=0.0) const
Definition: arrays.cpp:193
void pprint_tensor(FILE *stream, int precision, const double *a)
Definition: arrays.cpp:595
double give_lenght(void) const
Definition: arrays.cpp:157
void tmsby(double val)
Definition: arrays.cpp:408
void resize_ignore_vals(long r, long c)
print yourself
Definition: arrays.cpp:742
#define array(MAT, ROWS, COLS, ROW, COL)
Definition: meso3d.cpp:139
void rotate(Dvctr *v) const
Definition: arrays.cpp:30
void pprint_symtensor(char *stream, int precision, const long *a)
Definition: arrays.cpp:216
void pprint_tensor_zero(char *stream, int precision)
Definition: arrays.cpp:614
void beRotatedBy2d(const double *r)
Receiver will be rotated by 2d matrix &#39;rot&#39;. this = rot . this.
Definition: arrays.cpp:82
long give_ccols(void) const
Definition: arrays.h:758
#define NUM_DIGITS_IN_PRINTED_EXPONENT
Definition: gelib.h:41
void beRotatedBy(const double *r)
Receiver will be rotated by matrix &#39;rot&#39;. this = rot . this.
Definition: arrays.cpp:72
long give_sum(void) const
Definition: arrays.cpp:165
#define errol
Definition: gelib.h:151
int length_printed_tensor(int precision) const
Definition: arrays.cpp:175
Lvctr * assign_array(long *array, long s)
Definition: arrays.cpp:128
void be_tnsr(const Dmtrx &src)
Definition: arrays.cpp:332
void sbt(const Dvctr &src)
Definition: arrays.cpp:399
double give_lenght(void) const
Definition: arrays.cpp:309
void beRotatedPoint(const Dmtrx *rot, const PoinT *point)
Receiver will be point &#39;point&#39; rotated by matrix &#39;rot&#39;. this = rot . point.
Definition: arrays.cpp:60
#define AVOID_CNST
Definition: arrays.h:291
void be_tnsr(const Dvctr &src)
Definition: arrays.cpp:760
double give_determinant(void) const
Definition: arrays.cpp:828
void print_vector(char *stream, int precision, double absZero=0.0, bool zerorest=false) const
Definition: arrays.cpp:576
Structs Elem3D, PoinT and VectoR; classes Array, Array1d, Xscal, Dscal, Xvctr, Lvctr, Dvctr, Xmtrx, Lmtrx and Dmtrx.
long give_number_of_zeros(void) const
Definition: arrays.cpp:167
long give_number_of_nonzeros(void) const
Definition: arrays.cpp:166
void tmsRightBy(const Dmtrx &src)
Definition: arrays.cpp:815
double * a
Definition: arrays.h:586
void print_symtensor(char *stream, int precision, double absZero=0.0, bool zerorest=false) const
Definition: arrays.cpp:629
double * give_ptr2val(long r=0, long c=0)
Definition: arrays.h:822
void tmsLeftBy(const Dmtrx &src)
Definition: arrays.cpp:807
void tnsrRotWith(const Dmtrx &rot)
this = rot * this * rot^transp
Definition: arrays.cpp:430
int length_printed_vector(int precision) const
Definition: arrays.cpp:542
void print_tensor(FILE *stream, int precision, double absZero, bool zerorest) const
print yourself
Definition: arrays.cpp:1012
int length_printed_vector(int precision) const
Definition: arrays.cpp:174
void print_vector(char *stream, int precision, double absZero=0.0, bool zerorest=false) const
Definition: arrays.cpp:203
#define _errorr(_1)
Definition: gelib.h:160
#define _errorr3(_1, _2, _3)
Definition: gelib.h:155
void pprint_symtensor_zeroL(char *stream, int precision)
Definition: arrays.cpp:222
double give_zero(double abszero, double relzero) const
Definition: arrays.cpp:102
void be_mtrxMmtrx(const Dmtrx &A, const Dmtrx &B)
Definition: arrays.cpp:788
void tnsrRotAxisZangle(double a)
rotate coord system around axis z by angle
Definition: arrays.cpp:459
int length_printed(int precision) const
void round2zero (double zero, double absZero);
Definition: arrays.cpp:541
double give_sum(void) const
Definition: arrays.cpp:321
void realloc(long newsize)
reallocate up receiver
Definition: arrays.cpp:114
double give_copy_r2z(double a, double zero)
Definition: arrays.cpp:13
double a[9]
Definition: arrays.h:253
void normalize(void)
Definition: arrays.cpp:483
bool GaussSolve(Dvctr &x, const Dvctr &rhs)
gaussian eliminatio
Definition: arrays.cpp:842
Dvctr * assign_array(double *array, long s)
Definition: arrays.cpp:279
long give_crows(void) const
Definition: arrays.h:757
void dvdby(double val)
Definition: arrays.cpp:401
Lvctr * free(void)
Definition: arrays.cpp:148
double * give_ptr2val(long i=0)
return pointer to
Definition: arrays.h:645
void add(const Dvctr &src)
Definition: arrays.cpp:397
Dvctr * free(void)
Definition: arrays.cpp:300
void be_transposition(const Dmtrx &src)
Definition: arrays.cpp:776
int length_printed(int precision) const
Definition: arrays.cpp:173
void resize_ignore_vals(long r, long c)
Definition: arrays.cpp:696
#define PYP
Definition: arrays.h:21
int length_printed_tensor(int precision) const
Definition: arrays.cpp:543
void addtms(const Dvctr &src, double tms)
Definition: arrays.cpp:398
void e(int i)
void be_vectproduct(const Dvctr &v1, const Dvctr &v2)
Definition: arrays.cpp:350