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