NEURON
norm.c
Go to the documentation of this file.
1 #include <../../nrnconf.h>
2 
3 /**************************************************************************
4 **
5 ** Copyright (C) 1993 David E. Steward & Zbigniew Leyk, all rights reserved.
6 **
7 ** Meschach Library
8 **
9 ** This Meschach Library is provided "as is" without any express
10 ** or implied warranty of any kind with respect to this software.
11 ** In particular the authors shall not be liable for any direct,
12 ** indirect, special, incidental or consequential damages arising
13 ** in any way from use of the software.
14 **
15 ** Everyone is granted permission to copy, modify and redistribute this
16 ** Meschach Library, provided:
17 ** 1. All copies contain this copyright notice.
18 ** 2. All modified copies shall carry a notice stating who
19 ** made the last modification and the date of such modification.
20 ** 3. No charge is made for this software or works derived from it.
21 ** This clause shall not be construed as constraining other software
22 ** distributed on the same medium as this software, nor is a
23 ** distribution fee considered a charge.
24 **
25 ***************************************************************************/
26 
27 
28 /*
29  A collection of functions for computing norms: scaled and unscaled
30 */
31 static char rcsid[] = "norm.c,v 1.1 1997/12/04 17:55:43 hines Exp";
32 
33 #include <stdio.h>
34 #include "matrix.h"
35 #include <math.h>
36 
37 
38 /* _v_norm1 -- computes (scaled) 1-norms of vectors */
39 double _v_norm1(x,scale)
40 VEC *x, *scale;
41 {
42  int i, dim;
43  Real s, sum;
44 
45  if ( x == (VEC *)NULL )
46  error(E_NULL,"_v_norm1");
47  dim = x->dim;
48 
49  sum = 0.0;
50  if ( scale == (VEC *)NULL )
51  for ( i = 0; i < dim; i++ )
52  sum += fabs(x->ve[i]);
53  else if ( scale->dim < dim )
54  error(E_SIZES,"_v_norm1");
55  else
56  for ( i = 0; i < dim; i++ )
57  { s = scale->ve[i];
58  sum += ( s== 0.0 ) ? fabs(x->ve[i]) : fabs(x->ve[i]/s);
59  }
60 
61  return sum;
62 }
63 
64 /* square -- returns x^2 */
65 double square(x)
66 double x;
67 { return x*x; }
68 
69 /* cube -- returns x^3 */
70 double cube(x)
71 double x;
72 { return x*x*x; }
73 
74 /* _v_norm2 -- computes (scaled) 2-norm (Euclidean norm) of vectors */
75 double _v_norm2(x,scale)
76 VEC *x, *scale;
77 {
78  int i, dim;
79  Real s, sum;
80 
81  if ( x == (VEC *)NULL )
82  error(E_NULL,"_v_norm2");
83  dim = x->dim;
84 
85  sum = 0.0;
86  if ( scale == (VEC *)NULL )
87  for ( i = 0; i < dim; i++ )
88  sum += square(x->ve[i]);
89  else if ( scale->dim < dim )
90  error(E_SIZES,"_v_norm2");
91  else
92  for ( i = 0; i < dim; i++ )
93  { s = scale->ve[i];
94  sum += ( s== 0.0 ) ? square(x->ve[i]) :
95  square(x->ve[i]/s);
96  }
97 
98  return sqrt(sum);
99 }
100 
101 #define max(a,b) ((a) > (b) ? (a) : (b))
102 
103 /* _v_norm_inf -- computes (scaled) infinity-norm (supremum norm) of vectors */
104 double _v_norm_inf(x,scale)
105 VEC *x, *scale;
106 {
107  int i, dim;
108  Real s, maxval, tmp;
109 
110  if ( x == (VEC *)NULL )
111  error(E_NULL,"_v_norm_inf");
112  dim = x->dim;
113 
114  maxval = 0.0;
115  if ( scale == (VEC *)NULL )
116  for ( i = 0; i < dim; i++ )
117  { tmp = fabs(x->ve[i]);
118  maxval = max(maxval,tmp);
119  }
120  else if ( scale->dim < dim )
121  error(E_SIZES,"_v_norm_inf");
122  else
123  for ( i = 0; i < dim; i++ )
124  { s = scale->ve[i];
125  tmp = ( s== 0.0 ) ? fabs(x->ve[i]) : fabs(x->ve[i]/s);
126  maxval = max(maxval,tmp);
127  }
128 
129  return maxval;
130 }
131 
132 /* m_norm1 -- compute matrix 1-norm -- unscaled */
133 double m_norm1(A)
134 MAT *A;
135 {
136  int i, j, m, n;
137  Real maxval, sum;
138 
139  if ( A == (MAT *)NULL )
140  error(E_NULL,"m_norm1");
141 
142  m = A->m; n = A->n;
143  maxval = 0.0;
144 
145  for ( j = 0; j < n; j++ )
146  {
147  sum = 0.0;
148  for ( i = 0; i < m; i ++ )
149  sum += fabs(A->me[i][j]);
150  maxval = max(maxval,sum);
151  }
152 
153  return maxval;
154 }
155 
156 /* m_norm_inf -- compute matrix infinity-norm -- unscaled */
157 double m_norm_inf(A)
158 MAT *A;
159 {
160  int i, j, m, n;
161  Real maxval, sum;
162 
163  if ( A == (MAT *)NULL )
164  error(E_NULL,"m_norm_inf");
165 
166  m = A->m; n = A->n;
167  maxval = 0.0;
168 
169  for ( i = 0; i < m; i++ )
170  {
171  sum = 0.0;
172  for ( j = 0; j < n; j ++ )
173  sum += fabs(A->me[i][j]);
174  maxval = max(maxval,sum);
175  }
176 
177  return maxval;
178 }
179 
180 /* m_norm_frob -- compute matrix frobenius-norm -- unscaled */
181 double m_norm_frob(A)
182 MAT *A;
183 {
184  int i, j, m, n;
185  Real sum;
186 
187  if ( A == (MAT *)NULL )
188  error(E_NULL,"m_norm_frob");
189 
190  m = A->m; n = A->n;
191  sum = 0.0;
192 
193  for ( i = 0; i < m; i++ )
194  for ( j = 0; j < n; j ++ )
195  sum += square(A->me[i][j]);
196 
197  return sqrt(sum);
198 }
199 
#define error(err_num, fn_name)
Definition: err.h:73
#define E_NULL
Definition: err.h:102
#define E_SIZES
Definition: err.h:95
#define Real
Definition: machine.h:189
#define i
Definition: md1redef.h:12
sqrt
Definition: extdef.h:3
fabs
Definition: extdef.h:3
#define A(i)
Definition: multisplit.cpp:63
double _v_norm1(VEC *x, VEC *scale)
Definition: norm.c:39
double m_norm_frob(MAT *A)
Definition: norm.c:181
double square(double x)
Definition: norm.c:65
double _v_norm2(VEC *x, VEC *scale)
Definition: norm.c:75
double cube(double x)
Definition: norm.c:70
double m_norm1(MAT *A)
Definition: norm.c:133
double m_norm_inf(MAT *A)
Definition: norm.c:157
static char rcsid[]
Definition: norm.c:31
double _v_norm_inf(VEC *x, VEC *scale)
Definition: norm.c:104
#define max(a, b)
Definition: norm.c:101
int const size_t const size_t n
Definition: nrngsl.h:11
size_t j
#define NULL
Definition: sptree.h:16
Definition: matrix.h:73
Definition: matrix.h:67