basic_algebra.c
Go to the documentation of this file.
1 #include "basic_algebra.h"
2 
3 #include "defines.h"
4 #include "geom.h"
5 
6 #include <math.h>
7 
15 CBLAS_INLINE double GeneralDotProduct(unsigned int s,double *v1,double *v2)
16 {
17  #ifdef _CBLAS
18  return(cblas_ddot(s,v1,1,v2,1));
19  #else
20  unsigned int i;
21  double dp;
22 
23  dp=0.0;
24  for(i=0;i<s;i++)
25  dp+=(v1[i]*v2[i]);
26  return(dp);
27  #endif
28 }
29 
30 CBLAS_INLINE void ScaleVector(double f,unsigned int s,double *v)
31 {
32  #ifdef _CBLAS
33  cblas_dscal(s,f,v,1);
34  #else
35  unsigned int i;
36 
37  for(i=0;i<s;i++)
38  v[i]*=f;
39  #endif
40 }
41 
42 CBLAS_INLINE void ScaleVector2(double f,unsigned int s,double *v,double *vout)
43 {
44  #ifdef _CBLAS
45  cblas_dcopy(s,v,1,vout,1);
46  cblas_dscal(s,f,vout,1);
47  #else
48  unsigned int i;
49 
50  for(i=0;i<s;i++)
51  vout[i]=v[i]*f;
52  #endif
53 }
54 
55 CBLAS_INLINE void AccumulateVector(unsigned int s,double *v1,double *v2)
56 {
57  #ifdef _CBLAS
58  cblas_daxpy(s,1.0,v1,1,v2,1);
59  #else
60  unsigned int i;
61 
62  for(i=0;i<s;i++)
63  v2[i]+=v1[i];
64  #endif
65 }
66 
67 CBLAS_INLINE void SumVector(unsigned int s,double *v1,double *v2,double *v)
68 {
69  #ifdef _CBLAS
70  /* If v==v2 and we copy v1 on v, v2 will be lost */
71  if (v==v2)
72  cblas_daxpy(s,1.0,v1,1,v,1);
73  else
74  {
75  cblas_dcopy(s,v1,1,v,1);
76  cblas_daxpy(s,1.0,v2,1,v,1);
77  }
78  #else
79  unsigned int i;
80 
81  for(i=0;i<s;i++)
82  v[i]=(v1[i]+v2[i]);
83  #endif
84 }
85 
86 CBLAS_INLINE void SumVectorScale(unsigned int s,double *v1,double w,double *v2,double *v)
87 {
88  #ifdef _CBLAS
89  /* If v==v2 and we copy v1 on v, v2 will be lost */
90  if (v==v2)
91  {
92  cblas_dscal(s,w,v2,1);
93  cblas_daxpy(s,1.0,v1,1,v,1); /* v=v+v1=w*v2+v1 */
94  }
95  else
96  {
97  cblas_dcopy(s,v1,1,v,1);
98  cblas_daxpy(s,w,v2,1,v,1);
99  }
100  #else
101  unsigned int i;
102 
103  for(i=0;i<s;i++)
104  v[i]=(v1[i]+w*v2[i]);
105  #endif
106 }
107 
108 void CosVector(unsigned int s,double *v,double *co)
109 {
110  unsigned int i;
111 
112  for(i=0;i<s;i++)
113  co[i]=cos(v[i]);
114 }
115 
116 void SinVector(unsigned int s,double *v,double *si)
117 {
118  unsigned int i;
119 
120  for(i=0;i<s;i++)
121  si[i]=sin(v[i]);
122 }
123 
124 unsigned int MaxVectorElement(unsigned int m,double *v)
125 {
126  if (m==0)
127  return(NO_UINT);
128  else
129  {
130  unsigned int i;
131  unsigned int me;
132  double ma;
133 
134  me=0;
135  ma=v[0];
136  for(i=1;i<m;i++)
137  {
138  if (v[i]>ma)
139  {
140  ma=v[i];
141  me=i;
142  }
143  }
144  return(me);
145  }
146 }
147 
148 double MaxVector(unsigned int m,double *v)
149 {
150  if (m==0)
151  return(0.0);
152  else
153  {
154  unsigned int i;
155  double ma;
156 
157  ma=v[0];
158  for(i=1;i<m;i++)
159  {
160  if (v[i]>ma)
161  ma=v[i];
162  }
163  return(ma);
164  }
165 }
166 
167 unsigned int MinVectorElement(unsigned int m,double *v)
168 {
169  if (m==0)
170  return(NO_UINT);
171  else
172  {
173  unsigned int i;
174  unsigned int me;
175  double mi;
176 
177  me=0;
178  mi=v[0];
179  for(i=1;i<m;i++)
180  {
181  if (v[i]<mi)
182  {
183  mi=v[i];
184  me=i;
185  }
186  }
187  return(me);
188  }
189 }
190 
191 double MinVector(unsigned int m,double *v)
192 {
193  if (m==0)
194  return(0.0);
195  else
196  {
197  unsigned int i;
198  double mi;
199 
200  mi=v[0];
201  for(i=1;i<m;i++)
202  {
203  if (v[i]<mi)
204  mi=v[i];
205  }
206  return(mi);
207  }
208 }
209 
210 CBLAS_INLINE void SubtractVector(unsigned int s,double *v1,double *v2)
211 {
212  #ifdef _CBLAS
213  cblas_daxpy(s,-1.0,v2,1,v1,1);
214  #else
215  unsigned int i;
216 
217  for(i=0;i<s;i++)
218  v1[i]-=v2[i];
219  #endif
220 }
221 
222 /* We always inline DifferenceVector because it is used by other functions below */
223 inline void DifferenceVector(unsigned int s,double *v1,double *v2,double *v)
224 {
225  #ifdef _CBLAS
226  /* If v==v2 and we copy v1 on v, v2 will be lost */
227  if (v==v2)
228  {
229  cblas_dscal(s,-1.0,v2,1);
230  cblas_daxpy(s,1.0,v1,1,v,1); /* v=v+v1=-v2+v1 */
231  }
232  else
233  {
234  cblas_dcopy(s,v1,1,v,1);
235  cblas_daxpy(s,-1.0,v2,1,v,1);
236  }
237  #else
238  unsigned int i;
239 
240  for(i=0;i<s;i++)
241  v[i]=(v1[i]-v2[i]);
242  #endif
243 }
244 
245 void DifferenceVectorTopology(unsigned int s,unsigned int *tp,
246  double *v1,double *v2,double *v)
247 {
248  if (tp==NULL)
249  DifferenceVector(s,v1,v2,v);
250  else
251  {
252  unsigned int i;
253  double d;
254 
255  for(i=0;i<s;i++)
256  {
257  d=v1[i]-v2[i];
258  if (tp[i]==TOPOLOGY_S)
259  PI2PI(d);
260  v[i]=d;
261  }
262  }
263 }
264 
265 CBLAS_INLINE double Norm(unsigned int s,double *v)
266 {
267  #ifdef _CBLAS
268  return(cblas_dnrm2(s,v,1));
269  #else
270  unsigned int i;
271  double n;
272 
273  n=0.0;
274  for(i=0;i<s;i++)
275  n+=(v[i]*v[i]);
276  return(sqrt(n));
277  #endif
278 }
279 
280 CBLAS_INLINE double NormWithStride(unsigned int s,unsigned int st,double *v)
281 {
282  #ifdef _CBLAS
283  return(cblas_dnrm2(s,v,st));
284  #else
285  unsigned int i,j;
286  double n;
287 
288  n=0.0;
289  for(i=0,j=0;i<s;i++,j+=st)
290  n+=(v[j]*v[j]);
291  return(sqrt(n));
292  #endif
293 }
294 
295 /* We inline SquaredDistance in all the cases, not just if CBLAS is present
296  because it is used in functions below */
297 inline double SquaredDistance(unsigned int s,double *v1,double *v2)
298 {
299  double n;
300 
301  #ifdef _NO_CBLAS
302  /* Not clear if this is more efficient than the naive implementation.
303  So, this is not used right now. */
304  n=cblas_ddot(s,v1,1,v1,1)
305  -2.0*cblas_ddot(s,v1,1,v2,1)
306  +cblas_ddot(s,v2,1,v2,1);
307  #else
308  unsigned int i;
309  double v;
310 
311  n=0;
312  for(i=0;i<s;i++)
313  {
314  v=v1[i]-v2[i];
315  n+=(v*v);
316  }
317  #endif
318 
319  return(n);
320 }
321 
322 /* We inline Distance in all the cases, not just if CBLAS is present */
323 inline double Distance(unsigned int s,double *v1,double *v2)
324 {
325  return(sqrt(SquaredDistance(s,v1,v2)));
326 }
327 
328 double SquaredDistanceTopology(unsigned int s,unsigned int *tp,
329  double *v1,double *v2)
330 {
331  double n;
332 
333  /* We in-line the DistanceTopology function, for efficiency reasons. */
334  if (tp==NULL)
335  n=SquaredDistance(s,v1,v2);
336  else
337  {
338  unsigned int i;
339  double n,v;
340 
341  for(i=0;i<s;i++)
342  {
343  v=v1[i]-v2[i];
344  if ((tp[i]==TOPOLOGY_S)&&((v<-M_PI)||(v>M_PI)))
345  PI2PI(v);
346  n+=(v*v);
347  }
348  }
349  return(n);
350 }
351 
352 double SquaredDistanceTopologyMin(double t2,unsigned int s,unsigned int *tp,
353  double *v1,double *v2)
354 {
355  unsigned int i;
356  double n,v;
357 
358  n=0.0;
359  if (tp==NULL)
360  {
361  /* This is the Distance function inlined (and modified) here for
362  efficiency (avoid an additonal function call) */
363  for(i=0;((i<s)&&(n<t2));i++)
364  {
365  v=v1[i]-v2[i];
366  n+=(v*v);
367  }
368  }
369  else
370  {
371  for(i=0;((i<s)&&(n<t2));i++)
372  {
373  v=v1[i]-v2[i];
374  if ((tp[i]==TOPOLOGY_S)&&((v<-M_PI)||(v>M_PI)))
375  PI2PI(v);
376  n+=(v*v);
377  }
378  }
379  return(n);
380 }
381 
382 double DistanceTopology(unsigned int s,unsigned int *tp,
383  double *v1,double *v2)
384 {
385  double n;
386 
387  if (tp==NULL)
388  n=SquaredDistance(s,v1,v2);
389  else
390  {
391  unsigned int i;
392  double v;
393 
394  n=0.0;
395  for(i=0;i<s;i++)
396  {
397  v=v1[i]-v2[i];
398  if ((tp[i]==TOPOLOGY_S)&&((v<-M_PI)||(v>M_PI)))
399  PI2PI(v);
400  n+=(v*v);
401  }
402  }
403  return(sqrt(n));
404 }
405 
406 double DistanceTopologyMin(double t,unsigned int s,unsigned int *tp,
407  double *v1,double *v2)
408 {
409  unsigned int i;
410  double n,v,t2;
411 
412  n=0.0;
413  t2=t*t;
414  if (tp==NULL)
415  {
416  /* This is the SquaredDistance function inlined and modified for
417  efficiency (avoid an additonal function call) */
418  for(i=0;(i<s)&&(n<t2);i++)
419  {
420  v=v1[i]-v2[i];
421  n+=(v*v);
422  }
423  }
424  else
425  {
426  for(i=0;(i<s)&&(n<t2);i++)
427  {
428  v=v1[i]-v2[i];
429  if ((tp[i]==TOPOLOGY_S)&&((v<-M_PI)||(v>M_PI)))
430  PI2PI(v);
431  n+=(v*v);
432  }
433  }
434  return(sqrt(n));
435 }
436 
437 boolean CrossTopologyBorder(unsigned int s,unsigned int *tp,double *v1,double *v2)
438 {
439  return(Distance(s,v1,v2)>DistanceTopology(s,tp,v1,v2));
440 }
441 
442 CBLAS_INLINE void Normalize(unsigned int s,double *v)
443 {
444  double n;
445 
446  #ifdef _CBLAS
447  n=cblas_dnrm2(s,v,1);
448  if (n>1e-6)
449  cblas_dscal(s,1.0/n,v,1);
450  else
451  Error("Normalizing a null vector");
452  #else
453  unsigned int i;
454 
455  n=Norm(s,v);
456  if (n>1e-6) /* avoid division by (almost) zero */
457  {
458  for(i=0;i<s;i++)
459  v[i]/=n;
460  }
461  else
462  Error("Normalizing a null vector");
463  #endif
464 }
465 
466 double Mean(unsigned int s,double *v)
467 {
468  unsigned int i;
469  double m;
470 
471  m=0.0;
472  for(i=0;i<s;i++)
473  m+=v[i];
474 
475  return(m/((double)s));
476 }
477 
478 double StdDev(unsigned int s,double m,double *v)
479 {
480  unsigned int i;
481  double sd,d;
482 
483  sd=0.0;
484  for(i=0;i<s;i++)
485  {
486  d=v[i]-m;
487  sd+=(d*d);
488  }
489 
490  if (s>1)
491  return(sqrt(sd/((double)s-1)));
492  else
493  return(sqrt(sd));
494 }
495 
496 void ArrayPi2Pi(unsigned int n,unsigned int *t,double *a)
497 {
498  unsigned int i;
499  double *b;
500 
501  b=a;
502  for(i=0;i<n;i++,b++)
503  {
504  if (t[i]==TOPOLOGY_S)
505  PI2PI(*b);
506  }
507 }
508 
509 CBLAS_INLINE void GetRow(unsigned int k,unsigned int r,unsigned int c,double *m,double *v)
510 {
511  #ifdef _CBLAS
512  cblas_dcopy(c,&(m[RC2INDEX(k,0,r,c)]),(ROW_MAJOR?1:r),v,1);
513  #else
514  unsigned int i;
515 
516  for(i=0;i<c;i++)
517  v[i]=m[RC2INDEX(k,i,r,c)];
518  #endif
519 }
520 
521 CBLAS_INLINE void GetColumn(unsigned int k,unsigned int r,unsigned int c,double *m,double *v)
522 {
523  #ifdef _CBLAS
524  cblas_dcopy(r,&(m[RC2INDEX(0,k,r,c)]),(ROW_MAJOR?c:1),v,1);
525  #else
526  unsigned int i;
527 
528  for(i=0;i<r;i++)
529  v[i]=m[RC2INDEX(i,k,r,c)];
530  #endif
531 }
532 
533 CBLAS_INLINE void SetRow(double *v,unsigned int k,unsigned int r,unsigned int c,double *m)
534 {
535  #ifdef _CBLAS
536  cblas_dcopy(c,v,1,&(m[RC2INDEX(k,0,r,c)]),(ROW_MAJOR?1:r));
537  #else
538  unsigned int i;
539 
540  for(i=0;i<c;i++)
541  m[RC2INDEX(k,i,r,c)]=v[i];
542  #endif
543 }
544 
545 CBLAS_INLINE void SetColumn(double *v,unsigned int k,unsigned int r,unsigned int c,double *m)
546 {
547  #ifdef _CBLAS
548  cblas_dcopy(r,v,1,&(m[RC2INDEX(0,k,r,c)]),(ROW_MAJOR?c:1));
549  #else
550  unsigned int i;
551 
552  for(i=0;i<r;i++)
553  m[RC2INDEX(i,k,r,c)]=v[i];
554  #endif
555 }
556 
557 CBLAS_INLINE double RowSquaredNorm(unsigned int k,unsigned int r,unsigned int c,double *m)
558 {
559  double s;
560  unsigned int i;
561 
562  #ifdef _CBLAS
563  i=RC2INDEX(k,0,r,c);
564  s=cblas_ddot(c,&(m[i]),(ROW_MAJOR?1:r),&(m[i]),(ROW_MAJOR?1:r));
565  #else
566  double v;
567 
568  s=0.0;
569  for(i=0;i<c;i++)
570  {
571  v=m[RC2INDEX(k,i,r,c)];
572  s+=(v*v);
573  }
574  #endif
575  return(s);
576 }
577 
578 CBLAS_INLINE double ColumnSquaredNorm(unsigned int k,unsigned int r,unsigned int c,double *m)
579 {
580  double s;
581  unsigned int i;
582 
583  #ifdef _CBLAS
584  i=RC2INDEX(0,k,r,c);
585  s=cblas_ddot(r,&(m[i]),(ROW_MAJOR?c:1),&(m[i]),(ROW_MAJOR?c:1));
586  #else
587  double v;
588 
589  s=0.0;
590  for(i=0;i<r;i++)
591  {
592  v=m[RC2INDEX(i,k,r,c)];
593  s+=(v*v);
594  }
595  #endif
596  return(s);
597 }
598 
599 CBLAS_INLINE void MatrixVectorProduct(unsigned int r,unsigned int c,double *A,double *b,double *o)
600 {
601  #ifdef _CBLAS
602  cblas_dgemv((ROW_MAJOR?CblasRowMajor:CblasColMajor),CblasNoTrans,r,c,1.0,A,(ROW_MAJOR?c:r),b,1,0.0,o,1);
603  #else
604  unsigned int i,j;
605 
606  for(i=0;i<r;i++)
607  {
608  o[i]=0.0;
609  for(j=0;j<c;j++)
610  o[i]+=(A[RC2INDEX(i,j,r,c)]*b[j]);
611  }
612  #endif
613 }
614 
615 CBLAS_INLINE void TMatrixVectorProduct(unsigned int r,unsigned int c,double *A,double *b,double *o)
616 {
617  #ifdef _CBLAS
618  cblas_dgemv((ROW_MAJOR?CblasRowMajor:CblasColMajor),CblasTrans,r,c,1.0,A,(ROW_MAJOR?c:r),b,1,0.0,o,1);
619  #else
620  unsigned int i,j;
621 
622  for(i=0;i<c;i++)
623  {
624  o[i]=0.0;
625  for(j=0;j<r;j++)
626  o[i]+=(A[RC2INDEX(j,i,r,c)]*b[j]);
627  }
628  #endif
629 }
630 
631 CBLAS_INLINE void TMatrixVectorStrideProduct(unsigned int r,unsigned int c,double *A,unsigned int s,double *b,double *o)
632 {
633  #ifdef _CBLAS
634  cblas_dgemv((ROW_MAJOR?CblasRowMajor:CblasColMajor),CblasTrans,r,c,1.0,A,(ROW_MAJOR?c:r),b,s,0.0,o,1);
635  #else
636  unsigned int i,j;
637 
638  for(i=0;i<c;i++)
639  {
640  o[i]=0.0;
641  for(j=0;j<r;j++)
642  o[i]+=(A[RC2INDEX(j,i,r,c)]*b[j*s]);
643  }
644  #endif
645 }
646 
647 CBLAS_INLINE void MatrixMatrixProduct(unsigned int ra,unsigned int ca,double *A,
648  unsigned int cb,double *B,double *C)
649 {
650  #ifdef _CBLAS
651  /* A is ra x ca
652  B is ca x cb
653  C is ra x cb */
654  cblas_dgemm((ROW_MAJOR?CblasRowMajor:CblasColMajor),CblasNoTrans,CblasNoTrans,
655  ra,cb,ca,1.0,A,(ROW_MAJOR?ca:ra),B,(ROW_MAJOR?cb:ra),0.0,C,(ROW_MAJOR?cb:ca));
656  #else
657  unsigned int i,j,k;
658 
659  /* A * B */
660  for(i=0;i<ra;i++)
661  {
662  for(j=0;j<cb;j++)
663  {
664  C[RC2INDEX(i,j,ra,cb)]=0.0;
665  for(k=0;k<ca;k++)
666  C[RC2INDEX(i,j,ra,cb)]+=(A[RC2INDEX(i,k,ra,ca)]*B[RC2INDEX(k,j,ca,cb)]);
667  }
668  }
669  #endif
670 }
671 
672 CBLAS_INLINE void TMatrixMatrixProduct(unsigned int ra,unsigned int ca,double *A,
673  unsigned int cb,double *B,double *C)
674 {
675  #ifdef _CBLAS
676  /* A^t is ca x ra
677  B is ra x cb
678  C is ca x cb */
679  cblas_dgemm((ROW_MAJOR?CblasRowMajor:CblasColMajor),CblasTrans,CblasNoTrans,
680  ca,cb,ra,1.0,A,(ROW_MAJOR?ca:ra),B,(ROW_MAJOR?cb:ra),0.0,C,(ROW_MAJOR?cb:ca));
681  #else
682  unsigned int i,j,k;
683 
684  /* A^t * B */
685  for(i=0;i<ca;i++)
686  {
687  for(j=0;j<cb;j++)
688  {
689  C[RC2INDEX(i,j,ca,cb)]=0.0;
690  for(k=0;k<ra;k++)
691  C[RC2INDEX(i,j,ca,cb)]+=(A[RC2INDEX(k,i,ra,ca)]*B[RC2INDEX(k,j,ra,cb)]);
692  }
693  }
694  #endif
695 }
696 
697 double MinCosinusBetweenSubSpaces(unsigned int m,unsigned int k,double *T1,double *T2)
698 {
699  unsigned int i;
700  double c,cm;
701  double *proj;
702  #ifndef _CBLAS
703  unsigned int s,j;
704  #endif
705 
706  NEW(proj,k,double);
707 
708  cm=1; /*minimal cosinus*/
709  for(i=0;i<k;i++)
710  {
711  #ifdef _CBLAS
712  #if (ROW_MAJOR)
713  TMatrixVectorStrideProduct(m,k,T1,k,&(T2[i]),proj);
714  #else
715  TMatrixVectorProduct(m,k,T1,&(T2[i*m]),proj);
716  #endif
717  #else
718  for(j=0;j<k;j++)
719  {
720  proj[j]=0;
721  for(s=0;s<m;s++)
722  proj[j]+=(T1[RC2INDEX(s,j,m,k)]*T2[RC2INDEX(s,i,m,k)]);
723  }
724  #endif
725  /* Assuming vector of T1 and T2 orthonormal, c is the cosinus of
726  the i-th vector of basis T2 with respect to the subspace defined
727  by T1. Note that in the projection the angle can not be larger than
728  pi/2 and, therefore, the cosinus in in [0,1]*/
729  c=Norm(k,proj);
730 
731  /* We search for the minimum cosinus = maximum angle. This is a measure
732  of the difference between the two subspaces.*/
733  if (c<cm) cm=c;
734  }
735 
736  free(proj);
737 
738  return(cm);
739 }
740 
741 CBLAS_INLINE void SubMatrixFromMatrix(unsigned int nr1,unsigned int nc1,double *m1,
742  unsigned int nri,unsigned int nci,
743  unsigned int nr,unsigned int nc,double *m)
744 {
745  #ifdef _CBLAS
746  unsigned int i,j,k;
747  #if (ROW_MAJOR)
748  if (nr1<nc1)
749  {
750  /* copy row by row */
751  for(j=0,i=0,k=RC2INDEX(nri,nci,nr,nc);j<nr1;j++,i+=nc1,k+=nc)
752  cblas_dcopy(nc1,&(m1[i]),1,&(m[k]),1);
753  }
754  else
755  {
756  /* copy column by column */
757  for(i=0,k=RC2INDEX(nri,nci,nr,nc);i<nc1;i++,k++)
758  cblas_dcopy(nr1,&(m1[i]),nc1,&(m[k]),nc);
759  }
760  #else
761  if (nr1<nc1)
762  {
763  /* copy row by row */
764  for(i=0,k=RC2INDEX(nri,nci,nr,nc);i<nr1;i++,k++)
765  cblas_dcopy(nc1,&(m1[i]),nr1,&(m[k]),nr);
766  }
767  else
768  {
769  /* copy column by column */
770  for(j=0,i=0,k=RC2INDEX(nri,nci,nr,nc);j<nc1;j++,i+=nr1,k+=nr)
771  cblas_dcopy(nr1,&(m1[i]),1,&(m[k]),1);
772  }
773  #endif
774  #else
775  unsigned int i,j,k,l;
776 
777  for(i=0,k=nri;i<nr1;i++,k++)
778  {
779  for(j=0,l=nci;j<nc1;j++,l++)
780  m[RC2INDEX(k,l,nr,nc)]=m1[RC2INDEX(i,j,nr1,nc1)];
781  }
782  #endif
783 }
784 
785 CBLAS_INLINE void SubMatrixFromTMatrix(unsigned int nr1,unsigned int nc1,double *m1,
786  unsigned int nri,unsigned int nci,
787  unsigned int nr,unsigned int nc,double *m)
788 {
789  #ifdef _CBLAS
790  unsigned int i,j,k;
791  #if (ROW_MAJOR)
792  if (nc1<nr1)
793  {
794  /* copy row by row (of the transposed) */
795  for(i=0,k=RC2INDEX(nri,nci,nr,nc);i<nc1;i++,k+=nc)
796  cblas_dcopy(nr1,&(m1[i]),nc1,&(m[k]),1);
797  }
798  else
799  {
800  /* copy column by column (of the transposed) */
801  for(j=0,i=0,k=RC2INDEX(nri,nci,nr,nc);j<nr1;j++,i+=nc1,k++)
802  cblas_dcopy(nc1,&(m1[i]),1,&(m[k]),nc);
803  }
804  #else
805  if (nc1<nr1)
806  {
807  /* copy row by row (of the transposed) */
808  for(j=0,i=0,k=RC2INDEX(nri,nci,nr,nc);j<nc1;j++,i+=nr1,k++)
809  cblas_dcopy(nr1,&(m1[i]),1,&(m[k]),nr);
810  }
811  else
812  {
813  /* copy column by column (of the transposed) */
814  for(i=0,k=RC2INDEX(nri,nci,nr,nc);i<nr1;i++,k+=nr)
815  cblas_dcopy(nc1,&(m1[i]),nr1,&(m[k]),1);
816  }
817  #endif
818  #else
819  unsigned int i,j,k,l;
820 
821  for(i=0,k=nri;i<nc1;i++,k++)
822  {
823  for(j=0,l=nci;j<nr1;j++,l++)
824  m[RC2INDEX(k,l,nr,nc)]=m1[RC2INDEX(j,i,nr1,nc1)];
825  }
826  #endif
827 }
828 
829 void PrintVector(FILE *f,char *label,unsigned int n,double *v)
830 {
831  unsigned int i;
832 
833  if (label!=NULL)
834  fprintf(f,"%s=",label);
835  fprintf(f,"[ ");
836  for(i=0;i<n;i++)
837  fprintf(f,"%.16f ",v[i]);
838  fprintf(f,"];\n");
839 }
840 
841 void PrintMatrix(FILE *f,char *label,unsigned int r,unsigned int c,double *m)
842 {
843  unsigned int i,j;
844 
845  if (label!=NULL)
846  fprintf(f,"%s=",label);
847  fprintf(f,"[ ");
848  for(i=0;i<r;i++)
849  {
850  for(j=0;j<c;j++)
851  fprintf(f,"%.16f ",m[RC2INDEX(i,j,r,c)]);
852  fprintf(f,"\n");
853  }
854  fprintf(f,"];\n");
855 }
856 
857 void PrintTMatrix(FILE *f,char *label,unsigned int r,unsigned int c,double *m)
858 {
859  unsigned int i,j;
860 
861  if (label!=NULL)
862  fprintf(f,"%s=",label);
863  fprintf(f,"[ ");
864  for(i=0;i<c;i++)
865  {
866  for(j=0;j<r;j++)
867  fprintf(f,"%.16f ",m[RC2INDEX(j,i,r,c)]);
868  fprintf(f,"\n");
869  }
870  fprintf(f,"];\n");
871 }
double MaxVector(unsigned int m, double *v)
Value of the maximum element of a vector.
CBLAS_INLINE void ScaleVector(double f, unsigned int s, double *v)
Scales a vector.
Definition: basic_algebra.c:30
double SquaredDistanceTopologyMin(double t2, unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the squared distance of two points.
Definition of basic functions.
CBLAS_INLINE void SetRow(double *v, unsigned int k, unsigned int r, unsigned int c, double *m)
Sets a row of a matrix.
double DistanceTopology(unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the distance of two points.
CBLAS_INLINE void TMatrixVectorStrideProduct(unsigned int r, unsigned int c, double *A, unsigned int s, double *b, double *o)
Product of a transposed matrix and a vector.
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
#define RC2INDEX(i, j, nr, nc)
Index in a vector of a matrix element.
Definition: basic_algebra.h:81
void ArrayPi2Pi(unsigned int n, unsigned int *t, double *a)
Applies PI2PI to an array.
void SinVector(unsigned int s, double *v, double *si)
Sine on a vector.
double MinVector(unsigned int m, double *v)
Value of the minimum element of a vector.
CBLAS_INLINE void TMatrixMatrixProduct(unsigned int ra, unsigned int ca, double *A, unsigned int cb, double *B, double *C)
C = A^t * B.
CBLAS_INLINE double Norm(unsigned int s, double *v)
Computes the norm of a vector.
CBLAS_INLINE void SumVector(unsigned int s, double *v1, double *v2, double *v)
Adds two vectors.
Definition: basic_algebra.c:67
#define PI2PI(a)
Forces an angle go be in [-pi,pi].
Definition: defines.h:205
void DifferenceVectorTopology(unsigned int s, unsigned int *tp, double *v1, double *v2, double *v)
Substracts two vectors.
CBLAS_INLINE double RowSquaredNorm(unsigned int k, unsigned int r, unsigned int c, double *m)
Computes the squared norm of a row of a matrix.
CBLAS_INLINE void AccumulateVector(unsigned int s, double *v1, double *v2)
Adds a vector to another vectors.
Definition: basic_algebra.c:55
CBLAS_INLINE void Normalize(unsigned int s, double *v)
Normalizes a vector.
void Error(const char *s)
General error function.
Definition: error.c:80
double SquaredDistance(unsigned int s, double *v1, double *v2)
Computes the squared distance of two points.
#define CBLAS_INLINE
Inline BLAS-based functions.
Definition: basic_algebra.h:48
double Distance(unsigned int s, double *v1, double *v2)
Computes the distance of two points.
CBLAS_INLINE void ScaleVector2(double f, unsigned int s, double *v, double *vout)
Scales a vector.
Definition: basic_algebra.c:42
void PrintVector(FILE *f, char *label, unsigned int n, double *v)
Prints a vector.
unsigned int MaxVectorElement(unsigned int m, double *v)
Index of the maximum element of a vector.
CBLAS_INLINE double GeneralDotProduct(unsigned int s, double *v1, double *v2)
Computes the dot product of two general vectors.
Definition: basic_algebra.c:15
CBLAS_INLINE void MatrixVectorProduct(unsigned int r, unsigned int c, double *A, double *b, double *o)
Product of a matrix and a vector.
Definitions of constants and macros used in several parts of the cuik library.
void DifferenceVector(unsigned int s, double *v1, double *v2, double *v)
Substracts two vectors.
boolean CrossTopologyBorder(unsigned int s, unsigned int *tp, double *v1, double *v2)
Determines if the line between two points crosses the topology boder.
double MinCosinusBetweenSubSpaces(unsigned int m, unsigned int k, double *T1, double *T2)
Computes the cosinus of the maximum angle between two lineal sub-spaces.
#define M_PI
Pi.
Definition: defines.h:83
CBLAS_INLINE void TMatrixVectorProduct(unsigned int r, unsigned int c, double *A, double *b, double *o)
Product of a transposed matrix and a vector.
CBLAS_INLINE double ColumnSquaredNorm(unsigned int k, unsigned int r, unsigned int c, double *m)
Computes the squared norm of a column of a matrix.
double StdDev(unsigned int s, double m, double *v)
Computes the standard deviation.
#define NO_UINT
Used to denote an identifier that has not been initialized.
Definition: defines.h:435
CBLAS_INLINE void SumVectorScale(unsigned int s, double *v1, double w, double *v2, double *v)
Adds two vectors with a scale.
Definition: basic_algebra.c:86
void CosVector(unsigned int s, double *v, double *co)
Cosine on a vector.
CBLAS_INLINE void SubtractVector(unsigned int s, double *v1, double *v2)
Substracts a vector from another vector.
double SquaredDistanceTopology(unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the squared distance of two points.
CBLAS_INLINE void SubMatrixFromMatrix(unsigned int nr1, unsigned int nc1, double *m1, unsigned int nri, unsigned int nci, unsigned int nr, unsigned int nc, double *m)
Defines a submatrix in a matrix.
unsigned int MinVectorElement(unsigned int m, double *v)
Index of the minimum element of a vector.
double DistanceTopologyMin(double t, unsigned int s, unsigned int *tp, double *v1, double *v2)
Computes the distance of two points, if it is below a given threshold.
void PrintMatrix(FILE *f, char *label, unsigned int r, unsigned int c, double *m)
Prints a matrix.
CBLAS_INLINE void SubMatrixFromTMatrix(unsigned int nr1, unsigned int nc1, double *m1, unsigned int nri, unsigned int nci, unsigned int nr, unsigned int nc, double *m)
Defines a submatrix in a matrix.
CBLAS_INLINE double NormWithStride(unsigned int s, unsigned int st, double *v)
Computes the norm of a vector.
CBLAS_INLINE void MatrixMatrixProduct(unsigned int ra, unsigned int ca, double *A, unsigned int cb, double *B, double *C)
C = A * B.
CBLAS_INLINE void GetRow(unsigned int k, unsigned int r, unsigned int c, double *m, double *v)
Gets a row from a matrix.
void PrintTMatrix(FILE *f, char *label, unsigned int r, unsigned int c, double *m)
Prints a transposed matrix.
CBLAS_INLINE void SetColumn(double *v, unsigned int k, unsigned int r, unsigned int c, double *m)
Sets a column of a matrix.
double Mean(unsigned int s, double *v)
Computes the mean.
#define TOPOLOGY_S
One of the possible topologies.
Definition: defines.h:139
CBLAS_INLINE void GetColumn(unsigned int k, unsigned int r, unsigned int c, double *m, double *v)
Gets a column from a matrix.