NEURON
nvector_nrnserial_ld.cpp
Go to the documentation of this file.
1 /*
2  * -----------------------------------------------------------------
3  * $Revision: 2212 $
4  * $Date: 2008-09-08 16:32:26 +0200 (Mon, 08 Sep 2008) $
5  * -----------------------------------------------------------------
6  * Programmer(s): Scott D. Cohen, Alan C. Hindmarsh, Radu Serban,
7  * and Aaron Collier @ LLNL
8  * -----------------------------------------------------------------
9  * Copyright (c) 2002, The Regents of the University of California.
10  * Produced at the Lawrence Livermore National Laboratory.
11  * All rights reserved.
12  * For details, see sundials/shared/LICENSE.
13  * -----------------------------------------------------------------
14  * This is the implementation file for a serial implementation
15  * of the NVECTOR package.
16  * -----------------------------------------------------------------
17  */
18 
19 #define USELONGDOUBLE 1
20 
21 #include <../../nrnconf.h>
22 #include <hocassrt.h>
23 #include <nrnassrt.h>
24 #if HAVE_POSIX_MEMALIGN
25 #define HAVE_MEMALIGN 1
26 #endif
27 #if HAVE_MEMALIGN
28 #undef _XOPEN_SOURCE /* avoid warnings about redefining this */
29 #define _XOPEN_SOURCE 600
30 #endif
31 
32 #include <stdio.h>
33 #include <stdlib.h>
34 
35 #include "nvector_nrnserial_ld.h"
36 #include "shared/sundialsmath.h"
37 #include "shared/sundialstypes.h"
38 
39 #define ZERO RCONST(0.0)
40 #define HALF RCONST(0.5)
41 #define ONE RCONST(1.0)
42 #define ONEPT5 RCONST(1.5)
43 
44 #if USELONGDOUBLE
45 #define ldrealtype long double
46 #else
47 #define ldrealtype realtype
48 #endif
49 
50 /* Private function prototypes */
51 /* z=x */
52 static void VCopy_NrnSerialLD(N_Vector x, N_Vector z);
53 /* z=x+y */
54 static void VSum_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z);
55 /* z=x-y */
56 static void VDiff_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z);
57 /* z=-x */
58 static void VNeg_NrnSerialLD(N_Vector x, N_Vector z);
59 /* z=c(x+y) */
60 static void VScaleSum_NrnSerialLD(realtype c, N_Vector x, N_Vector y, N_Vector z);
61 /* z=c(x-y) */
62 static void VScaleDiff_NrnSerialLD(realtype c, N_Vector x, N_Vector y, N_Vector z);
63 /* z=ax+y */
64 static void VLin1_NrnSerialLD(realtype a, N_Vector x, N_Vector y, N_Vector z);
65 /* z=ax-y */
66 static void VLin2_NrnSerialLD(realtype a, N_Vector x, N_Vector y, N_Vector z);
67 /* y <- ax+y */
68 static void Vaxpy_NrnSerialLD(realtype a, N_Vector x, N_Vector y);
69 /* x <- ax */
70 static void VScaleBy_NrnSerialLD(realtype a, N_Vector x);
71 
72 /*
73  * -----------------------------------------------------------------
74  * exported functions
75  * -----------------------------------------------------------------
76  */
77 
78 /* ----------------------------------------------------------------------------
79  * Function to create a new empty serial vector
80  */
81 
82 N_Vector N_VNewEmpty_NrnSerialLD(long int length) {
83  N_Vector v;
84  N_Vector_Ops ops;
86 
87  /* Create vector */
88  v = (N_Vector) malloc(sizeof *v);
89  if (v == NULL)
90  return (NULL);
91 
92  /* Create vector operation structure */
93  ops = (N_Vector_Ops) malloc(sizeof(struct _generic_N_Vector_Ops));
94  if (ops == NULL) {
95  free(v);
96  return (NULL);
97  }
98 
99  ops->nvclone = N_VClone_NrnSerialLD;
100  ops->nvdestroy = N_VDestroy_NrnSerialLD;
101  ops->nvspace = N_VSpace_NrnSerialLD;
102  ops->nvgetarraypointer = N_VGetArrayPointer_NrnSerialLD;
103  ops->nvsetarraypointer = N_VSetArrayPointer_NrnSerialLD;
104  ops->nvlinearsum = N_VLinearSum_NrnSerialLD;
105  ops->nvconst = N_VConst_NrnSerialLD;
106  ops->nvprod = N_VProd_NrnSerialLD;
107  ops->nvdiv = N_VDiv_NrnSerialLD;
108  ops->nvscale = N_VScale_NrnSerialLD;
109  ops->nvabs = N_VAbs_NrnSerialLD;
110  ops->nvinv = N_VInv_NrnSerialLD;
111  ops->nvaddconst = N_VAddConst_NrnSerialLD;
112  ops->nvdotprod = N_VDotProd_NrnSerialLD;
113  ops->nvmaxnorm = N_VMaxNorm_NrnSerialLD;
114  ops->nvwrmsnormmask = N_VWrmsNormMask_NrnSerialLD;
115  ops->nvwrmsnorm = N_VWrmsNorm_NrnSerialLD;
116  ops->nvmin = N_VMin_NrnSerialLD;
117  ops->nvwl2norm = N_VWL2Norm_NrnSerialLD;
118  ops->nvl1norm = N_VL1Norm_NrnSerialLD;
119  ops->nvcompare = N_VCompare_NrnSerialLD;
120  ops->nvinvtest = N_VInvTest_NrnSerialLD;
121  ops->nvconstrmask = N_VConstrMask_NrnSerialLD;
122  ops->nvminquotient = N_VMinQuotient_NrnSerialLD;
123 
124  /* Create content */
125  content = (N_VectorContent_NrnSerialLD) malloc(sizeof(struct _N_VectorContent_NrnSerialLD));
126  if (content == NULL) {
127  free(ops);
128  free(v);
129  return (NULL);
130  }
131 
132  content->length = length;
133  content->own_data = FALSE;
134  content->data = NULL;
135 
136  /* Attach content and ops */
137  v->content = content;
138  v->ops = ops;
139 
140  return (v);
141 }
142 
143 /* ----------------------------------------------------------------------------
144  * Function to create a new serial vector
145  */
146 
147 N_Vector N_VNew_NrnSerialLD(long int length) {
148  N_Vector v;
149  realtype* data;
150 
151  v = N_VNewEmpty_NrnSerialLD(length);
152  if (v == NULL)
153  return (NULL);
154 
155  /* Create data */
156  if (length > 0) {
157  /* Allocate memory */
158 #if HAVE_MEMALIGN
159  nrn_assert(posix_memalign((void**) &data, 64, length * sizeof(realtype)) == 0);
160 #else
161  data = (realtype*) malloc(length * sizeof(realtype));
162 #endif
163  if (data == NULL) {
165  return (NULL);
166  }
167 
168  /* Attach data */
170  NV_DATA_S_LD(v) = data;
171  }
172 
173  return (v);
174 }
175 
176 /* ----------------------------------------------------------------------------
177  * Function to clone from a template a new vector with empty (NULL) data array
178  */
179 
180 N_Vector N_VCloneEmpty_NrnSerialLD(N_Vector w) {
181  N_Vector v;
182  N_Vector_Ops ops;
184 
185  if (w == NULL)
186  return (NULL);
187 
188  /* Create vector */
189  v = (N_Vector) malloc(sizeof *v);
190  if (v == NULL)
191  return (NULL);
192 
193  /* Create vector operation structure */
194  ops = (N_Vector_Ops) malloc(sizeof(struct _generic_N_Vector_Ops));
195  if (ops == NULL) {
196  free(v);
197  return (NULL);
198  }
199 
200  ops->nvclone = w->ops->nvclone;
201  ops->nvdestroy = w->ops->nvdestroy;
202  ops->nvspace = w->ops->nvspace;
203  ops->nvgetarraypointer = w->ops->nvgetarraypointer;
204  ops->nvsetarraypointer = w->ops->nvsetarraypointer;
205  ops->nvlinearsum = w->ops->nvlinearsum;
206  ops->nvconst = w->ops->nvconst;
207  ops->nvprod = w->ops->nvprod;
208  ops->nvdiv = w->ops->nvdiv;
209  ops->nvscale = w->ops->nvscale;
210  ops->nvabs = w->ops->nvabs;
211  ops->nvinv = w->ops->nvinv;
212  ops->nvaddconst = w->ops->nvaddconst;
213  ops->nvdotprod = w->ops->nvdotprod;
214  ops->nvmaxnorm = w->ops->nvmaxnorm;
215  ops->nvwrmsnormmask = w->ops->nvwrmsnormmask;
216  ops->nvwrmsnorm = w->ops->nvwrmsnorm;
217  ops->nvmin = w->ops->nvmin;
218  ops->nvwl2norm = w->ops->nvwl2norm;
219  ops->nvl1norm = w->ops->nvl1norm;
220  ops->nvcompare = w->ops->nvcompare;
221  ops->nvinvtest = w->ops->nvinvtest;
222  ops->nvconstrmask = w->ops->nvconstrmask;
223  ops->nvminquotient = w->ops->nvminquotient;
224 
225  /* Create content */
226  content = (N_VectorContent_NrnSerialLD) malloc(sizeof(struct _N_VectorContent_NrnSerialLD));
227  if (content == NULL) {
228  free(ops);
229  free(v);
230  return (NULL);
231  }
232 
233  content->length = NV_LENGTH_S_LD(w);
234  content->own_data = FALSE;
235  content->data = NULL;
236 
237  /* Attach content and ops */
238  v->content = content;
239  v->ops = ops;
240 
241  return (v);
242 }
243 
244 /* ----------------------------------------------------------------------------
245  * Function to create a serial N_Vector with user data component
246  */
247 
248 N_Vector N_VMake_NrnSerialLD(long int length, realtype* v_data) {
249  N_Vector v;
250 
251  v = N_VNewEmpty_NrnSerialLD(length);
252  if (v == NULL)
253  return (NULL);
254 
255  if (length > 0) {
256  /* Attach data */
258  NV_DATA_S_LD(v) = v_data;
259  }
260 
261  return (v);
262 }
263 
264 /* ----------------------------------------------------------------------------
265  * Function to create an array of new serial vectors.
266  */
267 
268 N_Vector* N_VNewVectorArray_NrnSerialLD(int count, long int length) {
269  N_Vector* vs;
270  int j;
271 
272  if (count <= 0)
273  return (NULL);
274 
275  vs = (N_Vector*) malloc(count * sizeof(N_Vector));
276  if (vs == NULL)
277  return (NULL);
278 
279  for (j = 0; j < count; j++) {
280  vs[j] = N_VNew_NrnSerialLD(length);
281  if (vs[j] == NULL) {
283  return (NULL);
284  }
285  }
286 
287  return (vs);
288 }
289 
290 /* ----------------------------------------------------------------------------
291  * Function to create an array of new serial vectors with NULL data array.
292  */
293 
294 N_Vector* N_VNewVectorArrayEmpty_NrnSerialLD(int count, long int length) {
295  N_Vector* vs;
296  int j;
297 
298  if (count <= 0)
299  return (NULL);
300 
301  vs = (N_Vector*) malloc(count * sizeof(N_Vector));
302  if (vs == NULL)
303  return (NULL);
304 
305  for (j = 0; j < count; j++) {
306  vs[j] = N_VNewEmpty_NrnSerialLD(length);
307  if (vs[j] == NULL) {
309  return (NULL);
310  }
311  }
312 
313  return (vs);
314 }
315 
316 /* ----------------------------------------------------------------------------
317  * Function to free an array created with N_VNewVectorArray_NrnSerialLD
318  */
319 
320 void N_VDestroyVectorArray_NrnSerialLD(N_Vector* vs, int count) {
321  int j;
322 
323  for (j = 0; j < count; j++)
325 
326  free(vs);
327 }
328 
329 /* ----------------------------------------------------------------------------
330  * Function to print the a serial vector
331  */
332 
333 void N_VPrint_NrnSerialLD(N_Vector x) {
334  long int i, N;
335  realtype* xd;
336 
337  N = NV_LENGTH_S_LD(x);
338  xd = NV_DATA_S_LD(x);
339 
340  for (i = 0; i < N; i++) {
341 #if defined(SUNDIALS_EXTENDED_PRECISION)
342  printf("%11.8Lg\n", *xd++);
343 #elif defined(SUNDIALS_DOUBLE_PRECISION)
344  printf("%11.8lg\n", *xd++);
345 #else
346  printf("%11.8g\n", *xd++);
347 #endif
348  }
349  printf("\n");
350 }
351 
352 /*
353  * -----------------------------------------------------------------
354  * implementation of vector operations
355  * -----------------------------------------------------------------
356  */
357 
358 N_Vector N_VClone_NrnSerialLD(N_Vector w) {
359  N_Vector v;
360  realtype* data;
361  long int length;
362 
364  if (v == NULL)
365  return (NULL);
366 
367  length = NV_LENGTH_S_LD(w);
368 
369  /* Create data */
370  if (length > 0) {
371  /* Allocate memory */
372 #if HAVE_MEMALIGN
373  nrn_assert(posix_memalign((void**) &data, 64, length * sizeof(realtype)) == 0);
374 #else
375  data = (realtype*) malloc(length * sizeof(realtype));
376 #endif
377  if (data == NULL) {
379  return (NULL);
380  }
381 
382  /* Attach data */
384  NV_DATA_S_LD(v) = data;
385  }
386 
387  return (v);
388 }
389 
390 void N_VDestroy_NrnSerialLD(N_Vector v) {
391  if (NV_OWN_DATA_S_LD(v) == TRUE)
392  free(NV_DATA_S_LD(v));
393  free(v->content);
394  free(v->ops);
395  free(v);
396 }
397 
398 void N_VSpace_NrnSerialLD(N_Vector v, long int* lrw, long int* liw) {
399  *lrw = NV_LENGTH_S_LD(v);
400  *liw = 1;
401 }
402 
403 realtype* N_VGetArrayPointer_NrnSerialLD(N_Vector v) {
404  realtype* v_data;
405 
406  v_data = NV_DATA_S_LD(v);
407 
408  return (v_data);
409 }
410 
411 void N_VSetArrayPointer_NrnSerialLD(realtype* v_data, N_Vector v) {
412  if (NV_LENGTH_S_LD(v) > 0)
413  NV_DATA_S_LD(v) = v_data;
414 }
415 
416 void N_VLinearSum_NrnSerialLD(realtype a, N_Vector x, realtype b, N_Vector y, N_Vector z) {
417  long int i, N;
418  realtype c, *xd, *yd, *zd;
419  N_Vector v1, v2;
420  booleantype test;
421 
422  if ((b == ONE) && (z == y)) { /* BLAS usage: axpy y <- ax+y */
423  Vaxpy_NrnSerialLD(a, x, y);
424  return;
425  }
426 
427  if ((a == ONE) && (z == x)) { /* BLAS usage: axpy x <- by+x */
428  Vaxpy_NrnSerialLD(b, y, x);
429  return;
430  }
431 
432  /* Case: a == b == 1.0 */
433 
434  if ((a == ONE) && (b == ONE)) {
435  VSum_NrnSerialLD(x, y, z);
436  return;
437  }
438 
439  /* Cases: (1) a == 1.0, b = -1.0, (2) a == -1.0, b == 1.0 */
440 
441  if ((test = ((a == ONE) && (b == -ONE))) || ((a == -ONE) && (b == ONE))) {
442  v1 = test ? y : x;
443  v2 = test ? x : y;
444  VDiff_NrnSerialLD(v2, v1, z);
445  return;
446  }
447 
448  /* Cases: (1) a == 1.0, b == other or 0.0, (2) a == other or 0.0, b == 1.0 */
449  /* if a or b is 0.0, then user should have called N_VScale */
450 
451  if ((test = (a == ONE)) || (b == ONE)) {
452  c = test ? b : a;
453  v1 = test ? y : x;
454  v2 = test ? x : y;
455  VLin1_NrnSerialLD(c, v1, v2, z);
456  return;
457  }
458 
459  /* Cases: (1) a == -1.0, b != 1.0, (2) a != 1.0, b == -1.0 */
460 
461  if ((test = (a == -ONE)) || (b == -ONE)) {
462  c = test ? b : a;
463  v1 = test ? y : x;
464  v2 = test ? x : y;
465  VLin2_NrnSerialLD(c, v1, v2, z);
466  return;
467  }
468 
469  /* Case: a == b */
470  /* catches case both a and b are 0.0 - user should have called N_VConst */
471 
472  if (a == b) {
473  VScaleSum_NrnSerialLD(a, x, y, z);
474  return;
475  }
476 
477  /* Case: a == -b */
478 
479  if (a == -b) {
480  VScaleDiff_NrnSerialLD(a, x, y, z);
481  return;
482  }
483 
484  /* Do all cases not handled above:
485  (1) a == other, b == 0.0 - user should have called N_VScale
486  (2) a == 0.0, b == other - user should have called N_VScale
487  (3) a,b == other, a !=b, a != -b */
488 
489  N = NV_LENGTH_S_LD(x);
490  xd = NV_DATA_S_LD(x);
491  yd = NV_DATA_S_LD(y);
492  zd = NV_DATA_S_LD(z);
493 
494  for (i = 0; i < N; i++)
495  *zd++ = a * (*xd++) + b * (*yd++);
496 }
497 
498 void N_VConst_NrnSerialLD(realtype c, N_Vector z) {
499  long int i, N;
500  realtype* zd;
501 
502  N = NV_LENGTH_S_LD(z);
503  zd = NV_DATA_S_LD(z);
504 
505  for (i = 0; i < N; i++)
506  *zd++ = c;
507 }
508 
509 void N_VProd_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z) {
510  long int i, N;
511  realtype *xd, *yd, *zd;
512 
513  N = NV_LENGTH_S_LD(x);
514  xd = NV_DATA_S_LD(x);
515  yd = NV_DATA_S_LD(y);
516  zd = NV_DATA_S_LD(z);
517 
518  for (i = 0; i < N; i++)
519  *zd++ = (*xd++) * (*yd++);
520 }
521 
522 void N_VDiv_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z) {
523  long int i, N;
524  realtype *xd, *yd, *zd;
525 
526  N = NV_LENGTH_S_LD(x);
527  xd = NV_DATA_S_LD(x);
528  yd = NV_DATA_S_LD(y);
529  zd = NV_DATA_S_LD(z);
530 
531  for (i = 0; i < N; i++)
532  *zd++ = (*xd++) / (*yd++);
533 }
534 
535 void N_VScale_NrnSerialLD(realtype c, N_Vector x, N_Vector z) {
536  long int i, N;
537  realtype *xd, *zd;
538 
539  if (z == x) { /* BLAS usage: scale x <- cx */
541  return;
542  }
543 
544  if (c == ONE) {
545  VCopy_NrnSerialLD(x, z);
546  } else if (c == -ONE) {
547  VNeg_NrnSerialLD(x, z);
548  } else {
549  N = NV_LENGTH_S_LD(x);
550  xd = NV_DATA_S_LD(x);
551  zd = NV_DATA_S_LD(z);
552  for (i = 0; i < N; i++)
553  *zd++ = c * (*xd++);
554  }
555 }
556 
557 void N_VAbs_NrnSerialLD(N_Vector x, N_Vector z) {
558  long int i, N;
559  realtype *xd, *zd;
560 
561  N = NV_LENGTH_S_LD(x);
562  xd = NV_DATA_S_LD(x);
563  zd = NV_DATA_S_LD(z);
564 
565  for (i = 0; i < N; i++, xd++, zd++)
566  *zd = ABS(*xd);
567 }
568 
569 void N_VInv_NrnSerialLD(N_Vector x, N_Vector z) {
570  long int i, N;
571  realtype *xd, *zd;
572 
573  N = NV_LENGTH_S_LD(x);
574  xd = NV_DATA_S_LD(x);
575  zd = NV_DATA_S_LD(z);
576 
577  for (i = 0; i < N; i++)
578  *zd++ = ONE / (*xd++);
579 }
580 
581 void N_VAddConst_NrnSerialLD(N_Vector x, realtype b, N_Vector z) {
582  long int i, N;
583  realtype *xd, *zd;
584 
585  N = NV_LENGTH_S_LD(x);
586  xd = NV_DATA_S_LD(x);
587  zd = NV_DATA_S_LD(z);
588 
589  for (i = 0; i < N; i++)
590  *zd++ = (*xd++) + b;
591 }
592 
593 realtype N_VDotProd_NrnSerialLD(N_Vector x, N_Vector y) {
594  long int i, N;
595  realtype sum = ZERO, *xd, *yd;
596 
597  N = NV_LENGTH_S_LD(x);
598  xd = NV_DATA_S_LD(x);
599  yd = NV_DATA_S_LD(y);
600 
601  for (i = 0; i < N; i++)
602  sum += (*xd++) * (*yd++);
603 
604  return (sum);
605 }
606 
607 realtype N_VMaxNorm_NrnSerialLD(N_Vector x) {
608  long int i, N;
609  realtype max = ZERO, *xd;
610 
611  N = NV_LENGTH_S_LD(x);
612  xd = NV_DATA_S_LD(x);
613 
614  for (i = 0; i < N; i++, xd++) {
615  if (ABS(*xd) > max)
616  max = ABS(*xd);
617  }
618 
619  return (max);
620 }
621 
622 realtype N_VWrmsNorm_NrnSerialLD(N_Vector x, N_Vector w) {
623  long int i, N;
624  realtype prodi, *xd, *wd;
625  ldrealtype sum = ZERO;
626 
627  N = NV_LENGTH_S_LD(x);
628  xd = NV_DATA_S_LD(x);
629  wd = NV_DATA_S_LD(w);
630 
631  for (i = 0; i < N; i++) {
632  prodi = (*xd++) * (*wd++);
633  sum += prodi * prodi;
634  }
635 
636  return (RSqrt((realtype) sum / N));
637 }
638 
639 realtype N_VWrmsNormMask_NrnSerialLD(N_Vector x, N_Vector w, N_Vector id) {
640  long int i, N;
641  realtype prodi, *xd, *wd, *idd;
642  ldrealtype sum = ZERO;
643 
644  N = NV_LENGTH_S_LD(x);
645  xd = NV_DATA_S_LD(x);
646  wd = NV_DATA_S_LD(w);
647  idd = NV_DATA_S_LD(id);
648 
649  for (i = 0; i < N; i++) {
650  if (idd[i] > ZERO) {
651  prodi = xd[i] * wd[i];
652  sum += prodi * prodi;
653  }
654  }
655 
656  return (RSqrt((realtype) sum / N));
657 }
658 
659 realtype N_VMin_NrnSerialLD(N_Vector x) {
660  long int i, N;
661  realtype min, *xd;
662 
663  N = NV_LENGTH_S_LD(x);
664  xd = NV_DATA_S_LD(x);
665 
666  min = xd[0];
667 
668  xd++;
669  for (i = 1; i < N; i++, xd++) {
670  if ((*xd) < min)
671  min = *xd;
672  }
673 
674  return (min);
675 }
676 
677 realtype N_VWL2Norm_NrnSerialLD(N_Vector x, N_Vector w) {
678  long int i, N;
679  realtype prodi, *xd, *wd;
680  ldrealtype sum = ZERO;
681 
682  N = NV_LENGTH_S_LD(x);
683  xd = NV_DATA_S_LD(x);
684  wd = NV_DATA_S_LD(w);
685 
686  for (i = 0; i < N; i++) {
687  prodi = (*xd++) * (*wd++);
688  sum += prodi * prodi;
689  }
690 
691  return (RSqrt((realtype) sum));
692 }
693 
694 realtype N_VL1Norm_NrnSerialLD(N_Vector x) {
695  long int i, N;
696  realtype* xd;
697  ldrealtype sum = ZERO;
698 
699  N = NV_LENGTH_S_LD(x);
700  xd = NV_DATA_S_LD(x);
701 
702  for (i = 0; i < N; i++)
703  sum += ABS(xd[i]);
704 
705  return ((realtype) sum);
706 }
707 
708 void N_VOneMask_NrnSerialLD(N_Vector x) {
709  long int i, N;
710  realtype* xd;
711 
712  N = NV_LENGTH_S_LD(x);
713  xd = NV_DATA_S_LD(x);
714 
715  for (i = 0; i < N; i++, xd++) {
716  if (*xd != ZERO)
717  *xd = ONE;
718  }
719 }
720 
721 void N_VCompare_NrnSerialLD(realtype c, N_Vector x, N_Vector z) {
722  long int i, N;
723  realtype *xd, *zd;
724 
725  N = NV_LENGTH_S_LD(x);
726  xd = NV_DATA_S_LD(x);
727  zd = NV_DATA_S_LD(z);
728 
729  for (i = 0; i < N; i++, xd++, zd++) {
730  *zd = (ABS(*xd) >= c) ? ONE : ZERO;
731  }
732 }
733 
734 booleantype N_VInvTest_NrnSerialLD(N_Vector x, N_Vector z) {
735  long int i, N;
736  realtype *xd, *zd;
737 
738  N = NV_LENGTH_S_LD(x);
739  xd = NV_DATA_S_LD(x);
740  zd = NV_DATA_S_LD(z);
741 
742  for (i = 0; i < N; i++) {
743  if (*xd == ZERO)
744  return (FALSE);
745  *zd++ = ONE / (*xd++);
746  }
747 
748  return (TRUE);
749 }
750 
751 booleantype N_VConstrMask_NrnSerialLD(N_Vector c, N_Vector x, N_Vector m) {
752  long int i, N;
753  booleantype test;
754  realtype *cd, *xd, *md;
755 
756  N = NV_LENGTH_S_LD(x);
757  xd = NV_DATA_S_LD(x);
758  cd = NV_DATA_S_LD(c);
759  md = NV_DATA_S_LD(m);
760 
761  test = TRUE;
762 
763  for (i = 0; i < N; i++, cd++, xd++, md++) {
764  *md = ZERO;
765  if (*cd == ZERO)
766  continue;
767  if (*cd > ONEPT5 || (*cd) < -ONEPT5) {
768  if ((*xd) * (*cd) <= ZERO) {
769  test = FALSE;
770  *md = ONE;
771  }
772  continue;
773  }
774  if ((*cd) > HALF || (*cd) < -HALF) {
775  if ((*xd) * (*cd) < ZERO) {
776  test = FALSE;
777  *md = ONE;
778  }
779  }
780  }
781  return (test);
782 }
783 
784 realtype N_VMinQuotient_NrnSerialLD(N_Vector num, N_Vector denom) {
785  booleantype notEvenOnce;
786  long int i, N;
787  realtype *nd, *dd, min = 0.0;
788 
789  N = NV_LENGTH_S_LD(num);
790  nd = NV_DATA_S_LD(num);
791  dd = NV_DATA_S_LD(denom);
792 
793  notEvenOnce = TRUE;
794 
795  for (i = 0; i < N; i++, nd++, dd++) {
796  if (*dd == ZERO)
797  continue;
798  else {
799  if (notEvenOnce) {
800  min = *nd / *dd;
801  notEvenOnce = FALSE;
802  } else
803  min = MIN(min, (*nd) / (*dd));
804  }
805  }
806 
807  if (notEvenOnce || (N == 0))
808  min = BIG_REAL;
809 
810  return (min);
811 }
812 
813 /*
814  * -----------------------------------------------------------------
815  * private functions
816  * -----------------------------------------------------------------
817  */
818 
819 static void VCopy_NrnSerialLD(N_Vector x, N_Vector z) {
820  long int i, N;
821  realtype *xd, *zd;
822 
823  N = NV_LENGTH_S_LD(x);
824  xd = NV_DATA_S_LD(x);
825  zd = NV_DATA_S_LD(z);
826 
827  for (i = 0; i < N; i++)
828  *zd++ = *xd++;
829 }
830 
831 static void VSum_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z) {
832  long int i, N;
833  realtype *xd, *yd, *zd;
834 
835  N = NV_LENGTH_S_LD(x);
836  xd = NV_DATA_S_LD(x);
837  yd = NV_DATA_S_LD(y);
838  zd = NV_DATA_S_LD(z);
839 
840  for (i = 0; i < N; i++)
841  *zd++ = (*xd++) + (*yd++);
842 }
843 
844 static void VDiff_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z) {
845  long int i, N;
846  realtype *xd, *yd, *zd;
847 
848  N = NV_LENGTH_S_LD(x);
849  xd = NV_DATA_S_LD(x);
850  yd = NV_DATA_S_LD(y);
851  zd = NV_DATA_S_LD(z);
852 
853  for (i = 0; i < N; i++)
854  *zd++ = (*xd++) - (*yd++);
855 }
856 
857 static void VNeg_NrnSerialLD(N_Vector x, N_Vector z) {
858  long int i, N;
859  realtype *xd, *zd;
860 
861  N = NV_LENGTH_S_LD(x);
862  xd = NV_DATA_S_LD(x);
863  zd = NV_DATA_S_LD(z);
864 
865  for (i = 0; i < N; i++)
866  *zd++ = -(*xd++);
867 }
868 
869 static void VScaleSum_NrnSerialLD(realtype c, N_Vector x, N_Vector y, N_Vector z) {
870  long int i, N;
871  realtype *xd, *yd, *zd;
872 
873  N = NV_LENGTH_S_LD(x);
874  xd = NV_DATA_S_LD(x);
875  yd = NV_DATA_S_LD(y);
876  zd = NV_DATA_S_LD(z);
877 
878  for (i = 0; i < N; i++)
879  *zd++ = c * ((*xd++) + (*yd++));
880 }
881 
882 static void VScaleDiff_NrnSerialLD(realtype c, N_Vector x, N_Vector y, N_Vector z) {
883  long int i, N;
884  realtype *xd, *yd, *zd;
885 
886  N = NV_LENGTH_S_LD(x);
887  xd = NV_DATA_S_LD(x);
888  yd = NV_DATA_S_LD(y);
889  zd = NV_DATA_S_LD(z);
890 
891  for (i = 0; i < N; i++)
892  *zd++ = c * ((*xd++) - (*yd++));
893 }
894 
895 static void VLin1_NrnSerialLD(realtype a, N_Vector x, N_Vector y, N_Vector z) {
896  long int i, N;
897  realtype *xd, *yd, *zd;
898 
899  N = NV_LENGTH_S_LD(x);
900  xd = NV_DATA_S_LD(x);
901  yd = NV_DATA_S_LD(y);
902  zd = NV_DATA_S_LD(z);
903 
904  for (i = 0; i < N; i++)
905  *zd++ = a * (*xd++) + (*yd++);
906 }
907 
908 static void VLin2_NrnSerialLD(realtype a, N_Vector x, N_Vector y, N_Vector z) {
909  long int i, N;
910  realtype *xd, *yd, *zd;
911 
912  N = NV_LENGTH_S_LD(x);
913  xd = NV_DATA_S_LD(x);
914  yd = NV_DATA_S_LD(y);
915  zd = NV_DATA_S_LD(z);
916 
917  for (i = 0; i < N; i++)
918  *zd++ = a * (*xd++) - (*yd++);
919 }
920 
921 static void Vaxpy_NrnSerialLD(realtype a, N_Vector x, N_Vector y) {
922  long int i, N;
923  realtype *xd, *yd;
924 
925  N = NV_LENGTH_S_LD(x);
926  xd = NV_DATA_S_LD(x);
927  yd = NV_DATA_S_LD(y);
928 
929  if (a == ONE) {
930  for (i = 0; i < N; i++)
931  *yd++ += (*xd++);
932  return;
933  }
934 
935  if (a == -ONE) {
936  for (i = 0; i < N; i++)
937  *yd++ -= (*xd++);
938  return;
939  }
940 
941  for (i = 0; i < N; i++)
942  *yd++ += a * (*xd++);
943 }
944 
945 static void VScaleBy_NrnSerialLD(realtype a, N_Vector x) {
946  long int i, N;
947  realtype* xd;
948 
949  N = NV_LENGTH_S_LD(x);
950  xd = NV_DATA_S_LD(x);
951 
952  for (i = 0; i < N; i++)
953  *xd++ *= a;
954 }
#define c
#define TRUE
Definition: err.c:57
#define FALSE
Definition: err.c:56
#define ABS(x)
Definition: extras.c:201
#define MIN(a, b)
Definition: grids.h:36
#define min(a, b)
Definition: matrix.h:157
#define max(a, b)
Definition: matrix.h:154
#define v
Definition: md1redef.h:4
#define i
Definition: md1redef.h:12
#define printf
Definition: mwprefix.h:26
#define nrn_assert(ex)
Definition: nrnassrt.h:53
size_t j
void N_VCompare_NrnSerialLD(realtype c, N_Vector x, N_Vector z)
N_Vector N_VCloneEmpty_NrnSerialLD(N_Vector w)
realtype N_VWL2Norm_NrnSerialLD(N_Vector x, N_Vector w)
N_Vector N_VMake_NrnSerialLD(long int length, realtype *v_data)
void N_VScale_NrnSerialLD(realtype c, N_Vector x, N_Vector z)
N_Vector N_VNewEmpty_NrnSerialLD(long int length)
void N_VSpace_NrnSerialLD(N_Vector v, long int *lrw, long int *liw)
#define ONE
static void VScaleSum_NrnSerialLD(realtype c, N_Vector x, N_Vector y, N_Vector z)
void N_VLinearSum_NrnSerialLD(realtype a, N_Vector x, realtype b, N_Vector y, N_Vector z)
#define HALF
static void VDiff_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z)
realtype N_VWrmsNormMask_NrnSerialLD(N_Vector x, N_Vector w, N_Vector id)
booleantype N_VConstrMask_NrnSerialLD(N_Vector c, N_Vector x, N_Vector m)
static void VSum_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z)
realtype N_VDotProd_NrnSerialLD(N_Vector x, N_Vector y)
void N_VPrint_NrnSerialLD(N_Vector x)
static void VCopy_NrnSerialLD(N_Vector x, N_Vector z)
N_Vector N_VClone_NrnSerialLD(N_Vector w)
static void VLin2_NrnSerialLD(realtype a, N_Vector x, N_Vector y, N_Vector z)
booleantype N_VInvTest_NrnSerialLD(N_Vector x, N_Vector z)
realtype N_VWrmsNorm_NrnSerialLD(N_Vector x, N_Vector w)
static void VScaleBy_NrnSerialLD(realtype a, N_Vector x)
void N_VDiv_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z)
void N_VDestroyVectorArray_NrnSerialLD(N_Vector *vs, int count)
realtype N_VMaxNorm_NrnSerialLD(N_Vector x)
static void VLin1_NrnSerialLD(realtype a, N_Vector x, N_Vector y, N_Vector z)
realtype N_VL1Norm_NrnSerialLD(N_Vector x)
N_Vector * N_VNewVectorArray_NrnSerialLD(int count, long int length)
realtype N_VMin_NrnSerialLD(N_Vector x)
#define ldrealtype
void N_VOneMask_NrnSerialLD(N_Vector x)
static void Vaxpy_NrnSerialLD(realtype a, N_Vector x, N_Vector y)
static void VScaleDiff_NrnSerialLD(realtype c, N_Vector x, N_Vector y, N_Vector z)
realtype * N_VGetArrayPointer_NrnSerialLD(N_Vector v)
N_Vector N_VNew_NrnSerialLD(long int length)
static void VNeg_NrnSerialLD(N_Vector x, N_Vector z)
void N_VAbs_NrnSerialLD(N_Vector x, N_Vector z)
void N_VSetArrayPointer_NrnSerialLD(realtype *v_data, N_Vector v)
#define ZERO
void N_VDestroy_NrnSerialLD(N_Vector v)
#define ONEPT5
N_Vector * N_VNewVectorArrayEmpty_NrnSerialLD(int count, long int length)
void N_VInv_NrnSerialLD(N_Vector x, N_Vector z)
void N_VAddConst_NrnSerialLD(N_Vector x, realtype b, N_Vector z)
realtype N_VMinQuotient_NrnSerialLD(N_Vector num, N_Vector denom)
void N_VProd_NrnSerialLD(N_Vector x, N_Vector y, N_Vector z)
void N_VConst_NrnSerialLD(realtype c, N_Vector z)
struct _N_VectorContent_NrnSerialLD * N_VectorContent_NrnSerialLD
#define NV_OWN_DATA_S_LD(v)
#define NV_DATA_S_LD(v)
#define NV_LENGTH_S_LD(v)
#define data
Definition: rbtqueue.cpp:49
#define NULL
Definition: sptree.h:16