Xmipp  v3.23.11-Nereus
lib_vec.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 * L I B _ V E C *
3 **********************************************************************
4 * Library is part of the Situs package URL: situs.biomachina.org *
5 * (c) Pablo Chacon, John Heumann, and Willy Wriggers, 2001-2015 *
6 **********************************************************************
7 * *
8 * Creating, resetting, copying arrays and other data structures. *
9 * *
10 **********************************************************************
11 * See legal statement for terms of distribution *
12 *********************************************************************/
13 
14 #include "situs.h"
15 #include "lib_vec.h"
16 #include "lib_err.h"
17 
18 /*
19  * Allocation and de-allocation of C arrays and matrices
20  * All arrays and matrices are initialized to zero when created
21  */
22 
23 /****** Following routines are for vectors and arrays of doubles ******/
24 
25 /*====================================================================*/
26 void zero_vect(double *vect, unsigned long len)
27 {
28  memset(vect, 0, len * sizeof(double));
29 }
30 
31 /*====================================================================*/
32 void do_vect(double **pvect, unsigned long len)
33 {
34  *pvect = (double *) alloc_vect(len, sizeof(double));
35 }
36 
37 /*====================================================================*/
38 void zero_mat(double **mat, unsigned long len_i, unsigned long len_j)
39 {
40  memset(mat[0], 0, len_i * len_j * sizeof(double));
41 }
42 
43 /*====================================================================*/
44 void cp_vect(double **vect1, double **vect2, unsigned long len)
45 {
46  memcpy(*vect1, *vect2, len * sizeof(double));
47 }
48 
49 /*====================================================================*/
50 /* destroys memory allocated to vect2 after copying */
51 void cp_vect_destroy(double **pvect1, double **pvect2, unsigned long len)
52 {
53  if (*pvect1)
54  free(*pvect1);
55  do_vect(pvect1, len);
56  cp_vect(pvect1, pvect2, len);
57  free_vect_and_zero_ptr((void**)pvect2);
58 }
59 
60 /*====================================================================*/
61 void add_scaled_vect(double *to_vect, double *from_vect, double scalar, unsigned long len)
62 {
63  unsigned long i;
64  for (i = 0; i < len; ++i)
65  to_vect[i] += scalar * from_vect[i];
66 }
67 
68 /****** Following routines are for arbitrary vectors and arrays ******/
69 
70 /*====================================================================*/
71 void *alloc_vect(unsigned int n, size_t elem_size)
72 {
73  void *pvect;
74 
75  pvect = calloc(n, elem_size);
76  if (!pvect) {
77  error_memory_allocation(99901, "lib_cvq");
78  }
79  return pvect;
80 };
81 
82 /*====================================================================*/
83 void free_vect_and_zero_ptr(void **pvect)
84 {
85  if (*pvect) {
86  free(*pvect);
87  *pvect = 0;
88  }
89 }
90 
91 /*====================================================================*/
92 void free_mat_and_zero_ptr(void ***pmat)
93 {
94  if (*pmat) {
95  if (**pmat)
96  free(**pmat);
97  free(*pmat);
98  *pmat = 0;
99  }
100 }
void zero_mat(double **mat, unsigned long len_i, unsigned long len_j)
Definition: lib_vec.cpp:38
void cp_vect_destroy(double **pvect1, double **pvect2, unsigned long len)
Definition: lib_vec.cpp:51
void free_vect_and_zero_ptr(void **pvect)
Definition: lib_vec.cpp:83
void free_mat_and_zero_ptr(void ***pmat)
Definition: lib_vec.cpp:92
void add_scaled_vect(double *to_vect, double *from_vect, double scalar, unsigned long len)
Definition: lib_vec.cpp:61
#define i
free((char *) ob)
#define len
void do_vect(double **pvect, unsigned long len)
Definition: lib_vec.cpp:32
void * alloc_vect(unsigned int n, size_t elem_size)
Definition: lib_vec.cpp:71
void cp_vect(double **vect1, double **vect2, unsigned long len)
Definition: lib_vec.cpp:44
void zero_vect(double *vect, unsigned long len)
Definition: lib_vec.cpp:26
int * n
void error_memory_allocation(int error_number, const char *program)
Definition: lib_err.cpp:50