EVSL  1.1.0
EigenValues Slicing Library
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
vect.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <string.h>
3 #include "def.h"
4 #include "struct.h"
5 #include "internal_proto.h"
11 void rand_double(int n, double *v) {
12  int i;
13  double t = ((double) RAND_MAX)/2.0;
14  for (i=0; i<n; i++) {
15  v[i] = (rand() -t)/t;
16  }
17 }
18 
19 /*
20  * Generates a normally distributed random vector of length n
21  *
22  * Uses the Box-Muller transformation
23  * @param[out] v Vector to be populated
24  * @param[in] n Number of elements to generate (should be the length of v)
25  * */
26 void randn_double(int n, double *v) {
27  const double two_pi = 2.0 * 3.1415926535;
28  int i;
29  for(i = 0; i < n; i++) {
30  static double Z0;
31  static double Z1;
32  static int regen = 0;//A boolean
33  regen = !regen;
34  if(!regen)
35  {
36  v[i] = Z1;
37  }
38 
39  double U1 = rand() * (1.0 / RAND_MAX);
40  double U2 = rand() * (1.0 / RAND_MAX);
41 
42  Z0 = sqrt(-2.0 * log(U1)) * cos(two_pi * U2);
43  Z1 = sqrt(-2.0 * log(U1)) * sin(two_pi * U2);
44  v[i] = Z0;
45  }
46 }
47 
48 void vecset(int n, double t, double *v) {
49  int i;
50  for (i=0; i<n; i++)
51  v[i] = t;
52 }
53 
54 void linspace(double a, double b, int num, double *arr){
55  double h;
56  h = (num==1? 0: (b-a)/(num-1));
57  int i;
58  //-------------------- careful at the boundaries!
59  arr[0] = a;
60  arr[num-1] = b;
61  for (i=1; i<num-1; i++)
62  arr[i] = a+i*h;
63 
64 }
65 
66 int compare1(const void *a, const void *b) {
67  double *aa = (double*) a;
68  double *bb = (double*) b;
69  if (*aa < *bb) {
70  return -1;
71  } else if (*aa == *bb) {
72  return 0;
73  } else {
74  return 1;
75  }
76 }
77 typedef struct _doubleint {
78  int i;
79  double d;
80 } doubleint;
81 int compare2(const void *a, const void *b) {
82  const doubleint *aa = (doubleint*) a;
83  const doubleint *bb = (doubleint*) b;
84  if (aa->d < bb->d) {
85  return -1;
86  } else if (aa->d == bb->d) {
87  return 0;
88  } else {
89  return 1;
90  }
91 }
92 void sort_double(int n, double *v, int *ind) {
93  /* if sorting indices are not wanted */
94  if (ind == NULL) {
95  qsort(v, n, sizeof(double), compare1);
96  return;
97  }
98  doubleint *vv;
99  Malloc(vv, n, doubleint);
100  int i;
101  for (i=0; i<n; i++) {
102  vv[i].d = v[i];
103  vv[i].i = i;
104  }
105  qsort(vv, n, sizeof(doubleint), compare2);
106  for (i=0; i<n; i++) {
107  v[i] = vv[i].d;
108  ind[i] = vv[i].i;
109  }
110  free(vv);
111 }
112 
113 /* @brief y = x(p) */
114 void vec_perm(int n, int *p, double *x, double *y) {
115  if (!p) {
116  memcpy(y, x, n*sizeof(double));
117  } else {
118  int i;
119  for (i=0; i<n; i++) {
120  y[i] = x[p[i]];
121  }
122  }
123 }
124 
125 
126 /* @brief y(p) = x */
127 void vec_iperm(int n, int *p, double *x, double *y) {
128  if (!p) {
129  memcpy(y, x, n*sizeof(double));
130  } else {
131  int i;
132  for (i=0; i<n; i++) {
133  y[p[i]] = x[i];
134  }
135  }
136 }
struct _doubleint doubleint
defs in EVSL
void vecset(int n, double t, double *v)
Definition: vect.c:48
void rand_double(int n, double *v)
Definition: vect.c:11
void vec_iperm(int n, int *p, double *x, double *y)
Definition: vect.c:127
void vec_perm(int n, int *p, double *x, double *y)
Definition: vect.c:114
double d
Definition: vect.c:79
void sort_double(int n, double *v, int *ind)
Definition: vect.c:92
int compare1(const void *a, const void *b)
Definition: vect.c:66
This file contains function prototypes and constant definitions internally used in EVSL...
#define Malloc(base, nmem, type)
Definition: def.h:22
void randn_double(int n, double *v)
Definition: vect.c:26
structs used in evsl
int i
Definition: vect.c:78
int compare2(const void *a, const void *b)
Definition: vect.c:81
void linspace(double a, double b, int num, double *arr)
Definition: vect.c:54