gpc_utils.c
Go to the documentation of this file.
1 #include "gpc.h"
2 #include "gpc_utils.h"
3 
4 void m_trans(const gsl_matrix*A, gsl_matrix*A_t){
5  gsl_matrix_transpose_memcpy(A_t,A);
6 }
7 
8 void m_mult(const gsl_matrix*A, const gsl_matrix*B, gsl_matrix*AB){
9  gsl_blas_dgemm(CblasNoTrans,CblasNoTrans,1.0,A,B,0.0,AB);
10 }
11 
12 void m_add_to(const gsl_matrix*A, gsl_matrix*B){
13  gsl_matrix_add(B, A);
14 }
15 
16 void m_scale(double m, gsl_matrix*A){
17  gsl_matrix_scale(A,m);
18 }
19 
20 void m_add (const gsl_matrix*A, const gsl_matrix*B, gsl_matrix*ApB){
21  gsl_matrix_memcpy(ApB,A);
22  gsl_matrix_add(ApB,B);
23 }
24 
25 void m_inv(const gsl_matrix*A, gsl_matrix*invA) {
26  unsigned int n = A->size1;
27  gsl_matrix * m = gsl_matrix_alloc(n,n);
28  gsl_matrix_memcpy(m,A);
29  gsl_permutation * perm = gsl_permutation_alloc (n);
30  /* Make LU decomposition of matrix m */
31  int s;
32  gsl_linalg_LU_decomp (m, perm, &s);
33  /* Invert the matrix m */
34  gsl_linalg_LU_invert (m, perm, invA);
35  gsl_permutation_free(perm);
36  gsl_matrix_free(m);
37 }
38 
39 double m_det(const gsl_matrix*A) {
40  unsigned int n = A->size1;
41  gsl_matrix * m = gsl_matrix_alloc(n,n);
42  gsl_matrix_memcpy(m,A);
43  gsl_permutation * perm = gsl_permutation_alloc (n);
44  int sign;
45  gsl_linalg_LU_decomp (m, perm, &sign);
46  double det = gsl_linalg_LU_det(m, sign);
47 
48  gsl_permutation_free(perm);
49  gsl_matrix_free(m);
50  return det;
51 }
52 
53 double m_dot(const gsl_matrix*A,const gsl_matrix*B) {
54  double sum = 0;
55  unsigned int j;
56  for(j=0;j<A->size2;j++)
57  sum += gmg(A,0,j)*gmg(B,j,0);
58  return sum;
59 }
60 
61 int poly_real_roots(unsigned int n, const double*a, double *roots) {
62  double z[(n-1)*2];
63  gsl_poly_complex_workspace * w = gsl_poly_complex_workspace_alloc(n);
64  if(GSL_SUCCESS != gsl_poly_complex_solve (a, n, w, z)) {
65  return 0;
66  }
67  gsl_poly_complex_workspace_free (w);
68 
69  unsigned int i=0;
70  for(i=0;i<n-1;i++) {
71  roots[i] = z[2*i];
72  }
73  return 1;
74 }
75 
76 
77 int poly_greatest_real_root(unsigned int n, const double*a, double *root) {
78  double z[(n-1)*2];
79  gsl_poly_complex_workspace * w = gsl_poly_complex_workspace_alloc(n);
80  if(GSL_SUCCESS != gsl_poly_complex_solve (a, n, w, z)) {
81  return 0;
82  }
83  gsl_poly_complex_workspace_free (w);
84  if(TRACE_ALGO) {
85  printf("Solving the equation\n a = [");
86  for(unsigned int i=0;i<n;i++) {
87  printf("%lf ", a[i]);
88  }
89  printf("]\n");
90  }
91 
92  unsigned int i;
93  double lambda = 0; int assigned = 0;
94  for (i = 0; i < n-1; i++) {
95  if(TRACE_ALGO) {
96  fprintf (stderr, "root z%d = %+.18f + %+.18f i \n", i, z[2*i], z[2*i+1]);
97  }
98 /* XXX ==0 is bad */
99  if( z[2*i+1]==0 ) /* real root */
100  if(!assigned || (z[2*i]>lambda)) {
101  assigned = 1;
102  lambda = z[2*i];
103  }
104  }
105  if(TRACE_ALGO)
106  fprintf (stderr, "lambda = %+.18f \n", lambda);
107  if(!assigned) {
108  fprintf(stderr, "poly_greatest_real_root: Could not find real root for polynomial.\n");
109  fprintf(stderr, "polynomial coefficients : ");
110  for (i = 0; i < n; i++)
111  fprintf(stderr, " %lf ", a[i]);
112  fprintf(stderr, "\nRoots:\n");
113 
114  for (i = 0; i < n-1; i++)
115  fprintf (stderr, "root z%d = %+.18f + %+.18f i \n", i, z[2*i], z[2*i+1]);
116 
117  return 0;
118  }
119 
120  *root = lambda;
121  return 1;
122 }
123 
124 void m_display(const char*str, gsl_matrix*m) {
125  printf("%s= \n", str);
126  unsigned int i,j;
127  for(i=0;i<m->size1;i++) {
128  printf(" ");
129  for(j=0;j<m->size2;j++)
130  printf("%e ", gmg(m,i,j));
131  printf("\n");
132  }
133 }
134 
void m_trans(const gsl_matrix *A, gsl_matrix *A_t)
Definition: gpc_utils.c:4
void m_scale(double m, gsl_matrix *A)
Definition: gpc_utils.c:16
void m_display(const char *str, gsl_matrix *m)
Definition: gpc_utils.c:124
int poly_greatest_real_root(unsigned int n, const double *a, double *root)
Definition: gpc_utils.c:77
#define gmg
Definition: gpc_utils.h:12
#define m(v1, v2)
Definition: egsl_macros.h:13
void m_add(const gsl_matrix *A, const gsl_matrix *B, gsl_matrix *ApB)
Definition: gpc_utils.c:20
void m_inv(const gsl_matrix *A, gsl_matrix *invA)
Definition: gpc_utils.c:25
#define TRACE_ALGO
Definition: gpc.h:42
#define sum(v1, v2)
Definition: egsl_macros.h:10
double m_det(const gsl_matrix *A)
Definition: gpc_utils.c:39
void m_mult(const gsl_matrix *A, const gsl_matrix *B, gsl_matrix *AB)
Definition: gpc_utils.c:8
void m_add_to(const gsl_matrix *A, gsl_matrix *B)
Definition: gpc_utils.c:12
int poly_real_roots(unsigned int n, const double *a, double *roots)
Definition: gpc_utils.c:61
double m_dot(const gsl_matrix *A, const gsl_matrix *B)
Definition: gpc_utils.c:53


csm
Author(s): Andrea Censi
autogenerated on Tue May 11 2021 02:18:23