muMECH  1.0
elementaryFunctions.h
Go to the documentation of this file.
1 //********************************************************************************************************
2 // code: ### ### ##### #### ## ##
3 // ## ## ######## ## ## ## ## ##
4 // ## ## ## ## ## ### ## ######
5 // ## ## ## ## ## ## ## ## ##
6 // ##### ## ## ##### #### ## ##
7 // ##
8 //
9 // name: elementaryFunctions.h
10 // author(s): Jan Novak
11 // language: C, C++
12 // license: This program is free software; you can redistribute it and/or modify
13 // it under the terms of the GNU Lesser General Public License as published by
14 // the Free Software Foundation; either version 2 of the License, or
15 // (at your option) any later version.
16 //
17 // This program is distributed in the hope that it will be useful,
18 // but WITHOUT ANY WARRANTY; without even the implied warranty of
19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 // GNU Lesser General Public License for more details.
21 //
22 // You should have received a copy of the GNU Lesser General Public License
23 // along with this program; if not, write to the Free Software
24 // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 //********************************************************************************************************
32 #ifndef MUMECH_ELEMENTARY_FUNCTIONS_H
33 #define MUMECH_ELEMENTARY_FUNCTIONS_H
34 
35 #include <time.h>
36 #include <stdio.h>
37 #include <string.h>
38 
39 
40 // print flag
41 #define _MATHEMATICA_SYNTAX_ 0
42 #define _STANDARD_SYNTAX_ 1
43 
44 
45 namespace mumech {
46 
47 
48 //macros and type definitions
49 //---------------------------
50 
51 //open file manipulation macros
52 typedef enum { _READ_, _WRITE_, _ADD_ } file_mode;
53 
54 
55 
56 // *******************************************************************************************************
57 // /** Operations with arrays of chars **/
58 // /** @name *** VARIOUS FUNCTIONS *** **/
59 // ***************************************************|***************************************************
60 // @{
61 
62 //********************************************************************************************************
63 //function gives the position of given 'double' @number in given @column of 2D array.
64 //Function explores the numbers in column from 0-zero until @rozsah increment
65 //int GetPosition( double ** array, double number, int column, int rozsah );
66 //********************************************************************************************************
67 
68 //********************************************************************************************************
69 //function prints the iteration progress to console in 'per-cent'
70 void PrintProgress( double procento, int incr );
71 //********************************************************************************************************
72 //function prints the iteration progress to console, function prints a mark '\/'
73 void PrintProgress( clock_t * p_time );
74 
77 double giveVolumeEllipsoid (double a, double b, double c);
78 
80 double give_curvature (double x, double a, double b);
81 
82 // Inverse hyperbolic cosine
83 double acosh(double x);
84 
85 // @}
86 
87 
88 // *******************************************************************************************************
89 // /** Operations with file descriptors **/
90 // /** @name *** FILE PROCESSING *** **/
91 // ***************************************************|***************************************************
92 // @{
93 
95 void OpenFile( FILE **file, char *name, const char mode[] );
97 void OpenFile( FILE* &file, char name[], int mode );
99 void CloseFile( FILE *file );
100 
102 void GoToEndOfLine( FILE * fr );
103 
105 void LatexPreambule( FILE * fw );
107 void LatexEnd( FILE * fw );
108 
111 //file_descriptor WriteTmp( char * buffer, size_t length )
112 
113 // @}
114 
115 
116 // *******************************************************************************************************
117 // /** Operations with arrays of chars **/
118 // /** @name *** STRING PROCESSING *** **/
119 // ***************************************************|***************************************************
120 // @{
121 
123 int GetRangeOfFileRows(char * p_name);
125 int GetRangeOfFileColumns(char * p_name);
127 int GetRangeOfFileColumns_ii(char * p_name);
128 
129 // @}
130 
131 
132 // *******************************************************************************************************
133 // /** Operations with 1d arrays of various length **/
134 // /** @name *** GENERAL VECTOR OPERATIONS *** **/
135 // ***************************************************|***************************************************
136 // @{
137 
139 inline bool* Allocate1Dbz (long d1) { bool *a = new bool [d1]; memset (a, 0, d1*sizeof(bool )); return a; }
141 inline bool** Allocate1Dbp (long d1) { bool **a = new bool* [d1]; memset (a, 0, d1*sizeof(bool* )); return a; }
142 
143 
145 inline int* Allocate1Di (long d1) { int *a = new int [d1]; return a; }
146 
148 inline double* Allocate1Dd (long d1) { double *a = new double [d1]; return a; }
150 inline double* Allocate1Ddz (long d1) { double *a = new double [d1]; memset (a, 0, d1*sizeof(double )); return a; }
152 inline double** Allocate1Ddp (long d1) { return (double**) memset (new double* [d1], 0, d1*sizeof(double* )); }
154 inline double*** Allocate1Ddpp (long d1) { double ***a = new double**[d1]; memset (a, 0, d1*sizeof(double**)); return a; }
155 
156 
158 inline void CleanVector (double *a, long n) { memset (a, 0, n * sizeof(double)); }
160 inline void CleanVector (int *a, long n) { memset (a, 0, n * sizeof(int) ); }
162 inline void CleanVector (long *a, long n) { memset (a, 0, n * sizeof(long) ); }
163 
164 //void CleanString( char * p, int n ) { zkontrolovat, jestli nemuzu pouzit memset p_string_name += n; while (n--) p[n] = '\0';}
165 
167 inline double GiveSumMembersVector (const double *a, long n) { double s=0.0; while (n--) s+= *(a++); return s; }
168 
169 
171 inline void CopyVector (const double *src, double *dest, long n) { if(src==dest) return; memcpy(dest, src, n * sizeof(double)); }
173 inline void CopyVector (const long *src, long *dest, long n) { if(src==dest) return; memcpy(dest, src, n * sizeof(long )); }
175 inline void CopyVector (const int *src, int *dest, long n) { if(src==dest) return; memcpy(dest, src, n * sizeof(int )); }
176 
178 inline void AddVector (const double *a, double *b, long n) { while (n--) b[n] += a[n]; }
180 inline void AddVector (const int *a, int *b, long n) { while (n--) b[n] += a[n]; }
181 
183 inline void SubtractVector (const double *a, double *b, long n) { while (n--) b[n] -= a[n]; }
184 
186 inline void SubtractTwoVectors (const double *v1, const double *v2, double *answer, long n) { while (n--) answer[n] = v1[n] - v2[n]; }
187 
189 inline void AddScalarToVector (long s, long *v, long n) { while (n--) v[n] += s; }
190 
192 inline void SumTwoVectors (const double *v1, const double *v2, double *answer, long n) { while (n--) answer[n] = v1[n] + v2[n]; }
193 
194 
196 inline void AddVectorMultipledBy (double mlt, const double *a, double *b, long n) { while (n--) b[n] += mlt * a[n]; }
197 
199 inline void DivideVector (double *a, long n, double val) { while (n--) a[n] /= val; }
200 
202 inline void MultiplyVector (double *a, long n, double val) { while (n--) a[n] *= val; }
203 
205 double give_VectorVectorProduct (const double *v1, const double *v2, long n);
206 
208 double give_quadNormTwoVectors (const double *v1, const double *v2, int n);
209 
210 //function initializes a 2D 'double' array from a given file
211 void ReadDataToArray( double ** p_array, char * p_file_name );
212 //function initializes a 3D 'double' array by values from a given file
213 void ReadDataTo3DArray( double *** p_array, char * p_file_name, int no_fields, int no_rows,
214  int no_columns );
215 //function initializes a 3D 'int' array by values from a given file
216 void ReadDataToArray( int ** p_array, char * p_file_name );
217 //function initializes an 'int' vector by values from a given file
218 void ReadDataToVector( int * p_array, char * p_file_name );
219 
220 
226 double giveVectorLength (double *point_1, double *point_2);
227 
234 double giveVectorLength (double rx, double ry, double rz);
235 
237 bool isNumberPresentInVector( int number, int * vector, int range );
238 
239 
240 // @}
241 
242 
243 // *******************************************************************************************************
244 // /** Operations with 2d, 3d, 4d arrays of various sizes **/
245 // /** @name *** GENERAL ARRAY OPERATIONS *** **/
246 // ***************************************************|***************************************************
247 // @{
248 
250 double** AllocateArray2D (long d1, long d2);
252 void AllocateArray2D (double **&array, long d1, long d2);
253 
254 // /// Function allocates a 2D 'double' d1xd2 array by 'new' command. First dimension is 'all', second dim only one i1-th.
255 // double** AllocateArray2Dao (long d1, long d2, long i1);
256 // /// Function allocates a 2D 'double' array by 'new' command.
257 // void AllocateArray2Dao (double **&array, long d1, long d2, long i1);
258 
260 inline void DeleteArray2D (bool **array, long d1) { if (array == NULL) return; while (d1--) delete [] array[d1]; delete [] array; array = NULL; }
262 inline void DeleteArray2D (double **array, long d1) { if (array == NULL) return; while (d1--) delete [] array[d1]; delete [] array; array = NULL; }
264 inline void DeleteArray3D (double ***array, long d1, long d2) { if (array == NULL) return; while (d1--) DeleteArray2D(array[d1], d2); delete [] array; array = NULL; }
265 
266 
268 inline void CleanArray2D (int **a, long rows, long columns) { while (rows--) memset (a[rows], 0, columns * sizeof(int )); }
270 inline void CleanArray2d (double **a, long rows, long columns) { while (rows--) memset (a[rows], 0, columns * sizeof(double)); }
271 
273 //void CopyArray2D (const double **src, double **dest, int d1, int d2) { true;}
275 void CopyArray2D (const double* const* src, double** dest, long d1, long d2);
276 
277 
279 void CopyArray3D (const double ***src, double ***dest, long d1, long d2, long d3);
280 
282 void AddArray2D (const double* const* src, double** dest, long d1, long d2);
284 void AddArray3D (double ***src, double ***dest, long d1, long d2, long d3);
285 
287 void DivideArray2D (double** a, double val, long d1, long d2);
288 
289 
290 
292 void SubtractTwoArrays2D (const double* const* a1, const double* const* a2, double **answer, long d1, long d2);
293 
294 
296 void giveInverseMatrix2x2 (double *answer, const double *m);
298 void giveInverseMatrix3x3 (double *answer, const double *m);
299 
301 void giveInverseMatrix3x3to5 (double *answer, const double *m);
303 void giveInverseMatrix6x6to12 (double *answer, const double *m);
304 
305 
307 inline double GiveSumMembersArray2D (const double **a, long d1, long d2) { double s=0.0; while (d1--) s += GiveSumMembersVector(*(a++), d2); return s; }
308 
315 inline void copyMatrix_1Dto2D (const double *v, double **m, long n) { for (long i=0; i<n; i++) memcpy (m[i], v+i*n, n * sizeof(double)); }
316 
323 inline void copyMatrix_2Dto1D (const double* const* m, double *v, long n) { for (long i=0; i<n; i++) memcpy (v+i*n, m[i], n * sizeof(double)); }
324 
326 inline void giveUnitMatrix2x2 (double *m) { m[1] = m[2] = 0.0; m[0] = m[3] = 1.0; }
328 inline void giveUnitMatrix3x3 (double *m) { m[1] = m[2] = m[3] = m[5] = m[6] = m[7] = 0.0; m[0] = m[4] = m[8] = 1.0; }
329 
330 
331 
334 void giveMatrixVectorProduct (const double *mtrx, const double *vect, double *resVect, long n);
335 
338 void giveMatrixMatrixProduct (const double *mtrx1, const double *mtrx2, double *resMtrx, long n);
339 
340 
342 void giveTransposedMatrix( const double * mtrx, double * T_mtrx, int n );
343 
345 void printMatrix( double * mtrx, int n, const char * notice );
346 
348 void printMatrix( double ** mtrx, int n, const char * notice );
349 
351 void printVector( double * vect, int n, const char * notice );
352 
354 void printVector( int * vect, int n, const char * notice );
355 
357 void printVectorRowForm( double * vect, int n, const char * notice );
358 
360 void printVectorRowForm( int * vect, int n, const char * notice );
361 
362 
363 // @}
364 
365 
366 } // end of namespace mumech
367 
368 #endif
369 
370 /*end of file*/
double give_curvature(double x, double a, double b)
double GiveSumMembersVector(const double *a, long n)
Function returns sum of all member of &#39;array&#39;.
double ** Allocate1Ddp(long d1)
Function allocates (&#39;new&#39; command) a 1d array of double* pointers set to NULL.
void CopyArray3D(const double ***src, double ***dest, long d1, long d2, long d3)
Function copy two 3D double arrays with dimensions d1xd2xd3 from &#39;src&#39; to &#39;dest&#39;. ...
double * Allocate1Dd(long d1)
Function allocates (&#39;new&#39; command) a 1d array of double.
double *** Allocate1Ddpp(long d1)
Function allocates (&#39;new&#39; command) a 1d array of double** pointers set to NULL.
void ReadDataToVector(int *p_array, char *p_file_name)
description: function initializes an &#39;int&#39; vector by values from a given file
double give_quadNormTwoVectors(const double *v1, const double *v2, int n)
Function returns the quadratic norm of two vectors of length &#39;n&#39;.
void GoToEndOfLine(FILE *fr)
description: functions moves the cursor the the end of line (EOLN) in a text file last edit: 1...
void AddVectorMultipledBy(double mlt, const double *a, double *b, long n)
Function add given number of components multiplied by &#39;mlt&#39; from vector &#39;a&#39; to vector &#39;b&#39;...
void CleanArray2D(int **a, long rows, long columns)
Function cleans an 2d &#39;int&#39; array, initialize each value being 0-zero.
void CloseFile(FILE *file)
description: function closes a previously opened file and tests the succes of the closing note: funct...
void giveUnitMatrix3x3(double *m)
Function sets 3X3 matrix to unit matrix.
#define array(MAT, ROWS, COLS, ROW, COL)
Definition: meso3d.cpp:139
void giveInverseMatrix2x2(double *answer, const double *m)
Function computes inverse of the row-wise saved 2x2 matrix &#39;m&#39;.
void CopyVector(const double *src, double *dest, long n)
Function copy given number of components from vector, &#39;a&#39; to vector &#39;b&#39;.
void giveInverseMatrix6x6to12(double *answer, const double *m)
Function computes inverse of the eshelby-like 3x3 matrix &#39;m&#39; saved in reduced 6x6to12 form...
int GetRangeOfFileRows(char *p_name)
Funkce zapise LENGTH bytu z BUFFER do docasneho souboru.
int GetRangeOfFileColumns_ii(char *p_name)
Function gives the number columns of a file structured as a table.
void giveInverseMatrix3x3to5(double *answer, const double *m)
Function computes inverse of the eshelby-like 3x3 matrix &#39;m&#39; saved in reduced 3x3to5 form...
void CopyArray2D(const double *const *src, double **dest, long d1, long d2)
Function copy two 2D double arrays with dimensions d1xd2 from &#39;src&#39; to &#39;dest&#39;.
void giveMatrixVectorProduct(const double *mtrx, const double *vect, double *resVect, long n)
Function gives the product of a regular matrix with a vector.
void AddScalarToVector(long s, long *v, long n)
Function add given number of components from vector &#39;a&#39; to vector &#39;b&#39;.
int * Allocate1Di(long d1)
Function allocates (&#39;new&#39; command) a 1d array of int.
void printVectorRowForm(double *vect, int n, const char *notice)
Function prints the vector of double values as row.
void AddArray2D(const double *const *src, double **dest, long d1, long d2)
Function add 2D double array with dimensions d1xd2 from &#39;src&#39; to &#39;dest&#39;.
void SubtractTwoVectors(const double *v1, const double *v2, double *answer, long n)
Function subtracts two vectors of entries. answer = v1 - v2.
void printVector(double *vect, int n, const char *notice)
Function prints the vector of double values as column.
void SubtractTwoArrays2D(const double *const *a1, const double *const *a2, double **answer, long d1, long d2)
Function subtracts two 2D double arrays with dimensions d1xd2. answer = v1 - v2.
double * Allocate1Ddz(long d1)
Function allocates (&#39;new&#39; command) a 1d array of double set to zero.
void CleanArray2d(double **a, long rows, long columns)
Function cleans an 2d &#39;double&#39; array, initialize each value being 0-zero.
void DeleteArray3D(double ***array, long d1, long d2)
Function deletes a 3D &#39;double&#39; array of dimension d1 x d2 x ??, allocated by new. ...
double acosh(double x)
Inverse hyperbolic cosine.
void SumTwoVectors(const double *v1, const double *v2, double *answer, long n)
Function sums two vectors of entries. resVec = vec1 - vec2.
void SubtractVector(const double *a, double *b, long n)
Function subtracts given number of components vector &#39;a&#39; from vector &#39;b&#39;; b -= a. ...
void copyMatrix_2Dto1D(const double *const *m, double *v, long n)
Function copies 2D matrix saved in 2d array m[n][n] to row-wise vector v[n * n].
void DivideVector(double *a, long n, double val)
Function divides &#39;n&#39; components of field &#39;a&#39; by value &#39;val&#39;.
void giveMatrixMatrixProduct(const double *mtrx1, const double *mtrx2, double *resMtrx, long n)
Function gives the product of two regular matrices.
void MultiplyVector(double *a, long n, double val)
Function multiplies &#39;n&#39; components of field &#39;a&#39; by value &#39;val&#39;.
void ReadDataToArray(double **p_array, char *p_file_name)
description: function initializes a 2D &#39;double&#39; array from a given file
void giveInverseMatrix3x3(double *answer, const double *m)
Function computes inverse of the row-wise saved 3x3 matrix &#39;m&#39;.
void PrintProgress(double procento, int incr)
Operations with arrays of chars.
void printMatrix(double *mtrx, int n, const char *notice)
Function prints a matrix which are saved in &#39;row-by-row&#39; C arrangement as 1D arrays.
double GiveSumMembersArray2D(const double **a, long d1, long d2)
Function returns sum of all member of array &#39;a&#39;.
bool ** Allocate1Dbp(long d1)
Function allocates (&#39;new&#39; command) a 1d array of bool* pointers set to NULL.
void DeleteArray2D(bool **array, long d1)
Function deletes a 2D &#39;bool&#39; array of dimension d1 x ??, allocated by new.
bool * Allocate1Dbz(long d1)
Function allocates (&#39;new&#39; command) a 1d array of bool set to zero.
void ReadDataTo3DArray(double ***p_array, char *p_file_name, int no_fields, int no_rows, int no_columns)
description: function initializes a 3D &#39;double&#39; array by values from a given file ...
double ** AllocateArray2D(long d1, long d2)
Operations with 2d, 3d, 4d arrays of various sizes.
void AddArray3D(double ***src, double ***dest, long d1, long d2, long d3)
Function add 3D double array with dimensions d1xd2xd3 from &#39;src&#39; to &#39;dest&#39;.
double giveVectorLength(const double *point_1, const double *point_2)
void LatexPreambule(FILE *fw)
description: function prints a simple preamble of a LaTEX document last edit: 16.05.2007
void OpenFile(FILE **file, char *name, const char mode[])
Operations with file descriptors.
void giveUnitMatrix2x2(double *m)
Function sets 3x3 matrix to unit matrix.
void copyMatrix_1Dto2D(const double *v, double **m, long n)
Function copies 2D matrix saved in row-wise vector v[n * n] to 2d array m[n][n].
double give_VectorVectorProduct(const double *v1, const double *v2, long n)
Operations with 1d arrays of various length.
double giveVolumeEllipsoid(double a, double b, double c)
Function gives the volume of an allipsoid defined by its semiaxes a, b, c.
void CleanVector(double *a, long n)
Functin cleans a &#39;double&#39; vector, initialize each value being 0-zero.
void LatexEnd(FILE *fw)
description: function prints the end (closing part) of LaTEX document last edit: 16.05.2007
void AddVector(const double *a, double *b, long n)
Function add given number of components from vector &#39;a&#39; to vector &#39;b&#39;, b += a.
bool isNumberPresentInVector(int number, int *vect, int range)
description: function returns TRUE/FALSE if a occurs/doesn&#39;t occurs in a , respectively ...
void DivideArray2D(double **a, double val, long d1, long d2)
Function divides 2D double array &#39;a&#39; with dimensions d1xd2 by value &#39;val&#39;.
void giveTransposedMatrix(const double *mtrx, double *T_mtrx, int n)
Function gives the trasposition of a matrix saved in row-by-row C arrangement.
int GetRangeOfFileColumns(char *p_name)
Function gives the number columns of the file such as is not necessarilly.