NEURON
hsehldr.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  Files for matrix computations
30 
31  Householder transformation file. Contains routines for calculating
32  householder transformations, applying them to vectors and matrices
33  by both row & column.
34 */
35 
36 /* hsehldr.c 1.3 10/8/87 */
37 static char rcsid[] = "hsehldr.c,v 1.1 1997/12/04 17:55:24 hines Exp";
38 
39 #include <stdio.h>
40 #include "matrix.h"
41 #include "matrix2.h"
42 #include <math.h>
43 
44 
45 /* hhvec -- calulates Householder vector to eliminate all entries after the
46  i0 entry of the vector vec. It is returned as out. May be in-situ */
47 VEC *hhvec(vec,i0,beta,out,newval)
48 VEC *vec,*out;
49 u_int i0;
50 Real *beta,*newval;
51 {
52  Real norm;
53 
54  out = _v_copy(vec,out,i0);
55  norm = sqrt(_in_prod(out,out,i0));
56  if ( norm <= 0.0 )
57  {
58  *beta = 0.0;
59  return (out);
60  }
61  *beta = 1.0/(norm * (norm+fabs(out->ve[i0])));
62  if ( out->ve[i0] > 0.0 )
63  *newval = -norm;
64  else
65  *newval = norm;
66  out->ve[i0] -= *newval;
67 
68  return (out);
69 }
70 
71 /* hhtrvec -- apply Householder transformation to vector -- may be in-situ */
72 VEC *hhtrvec(hh,beta,i0,in,out)
73 VEC *hh,*in,*out; /* hh = Householder vector */
74 u_int i0;
75 double beta;
76 {
77  Real scale;
78  /* u_int i; */
79 
80  if ( hh==(VEC *)NULL || in==(VEC *)NULL )
81  error(E_NULL,"hhtrvec");
82  if ( in->dim != hh->dim )
83  error(E_SIZES,"hhtrvec");
84  if ( i0 > in->dim )
85  error(E_BOUNDS,"hhtrvec");
86 
87  scale = beta*_in_prod(hh,in,i0);
88  out = v_copy(in,out);
89  __mltadd__(&(out->ve[i0]),&(hh->ve[i0]),-scale,(int)(in->dim-i0));
90  /************************************************************
91  for ( i=i0; i<in->dim; i++ )
92  out->ve[i] = in->ve[i] - scale*hh->ve[i];
93  ************************************************************/
94 
95  return (out);
96 }
97 
98 /* hhtrrows -- transform a matrix by a Householder vector by rows
99  starting at row i0 from column j0 -- in-situ */
100 MAT *hhtrrows(M,i0,j0,hh,beta)
101 MAT *M;
102 u_int i0, j0;
103 VEC *hh;
104 double beta;
105 {
106  Real ip, scale;
107  int i /*, j */;
108 
109  if ( M==(MAT *)NULL || hh==(VEC *)NULL )
110  error(E_NULL,"hhtrrows");
111  if ( M->n != hh->dim )
112  error(E_RANGE,"hhtrrows");
113  if ( i0 > M->m || j0 > M->n )
114  error(E_BOUNDS,"hhtrrows");
115 
116  if ( beta == 0.0 ) return (M);
117 
118  /* for each row ... */
119  for ( i = i0; i < M->m; i++ )
120  { /* compute inner product */
121  ip = __ip__(&(M->me[i][j0]),&(hh->ve[j0]),(int)(M->n-j0));
122  /**************************************************
123  ip = 0.0;
124  for ( j = j0; j < M->n; j++ )
125  ip += M->me[i][j]*hh->ve[j];
126  **************************************************/
127  scale = beta*ip;
128  if ( scale == 0.0 )
129  continue;
130 
131  /* do operation */
132  __mltadd__(&(M->me[i][j0]),&(hh->ve[j0]),-scale,
133  (int)(M->n-j0));
134  /**************************************************
135  for ( j = j0; j < M->n; j++ )
136  M->me[i][j] -= scale*hh->ve[j];
137  **************************************************/
138  }
139 
140  return (M);
141 }
142 
143 
144 /* hhtrcols -- transform a matrix by a Householder vector by columns
145  starting at row i0 from column j0 -- in-situ */
146 MAT *hhtrcols(M,i0,j0,hh,beta)
147 MAT *M;
148 u_int i0, j0;
149 VEC *hh;
150 double beta;
151 {
152  /* Real ip, scale; */
153  int i /*, k */;
154  static VEC *w = VNULL;
155 
156  if ( M==(MAT *)NULL || hh==(VEC *)NULL )
157  error(E_NULL,"hhtrcols");
158  if ( M->m != hh->dim )
159  error(E_SIZES,"hhtrcols");
160  if ( i0 > M->m || j0 > M->n )
161  error(E_BOUNDS,"hhtrcols");
162 
163  if ( beta == 0.0 ) return (M);
164 
165  w = v_resize(w,M->n);
167  v_zero(w);
168 
169  for ( i = i0; i < M->m; i++ )
170  if ( hh->ve[i] != 0.0 )
171  __mltadd__(&(w->ve[j0]),&(M->me[i][j0]),hh->ve[i],
172  (int)(M->n-j0));
173  for ( i = i0; i < M->m; i++ )
174  if ( hh->ve[i] != 0.0 )
175  __mltadd__(&(M->me[i][j0]),&(w->ve[j0]),-beta*hh->ve[i],
176  (int)(M->n-j0));
177  return (M);
178 }
179 
#define TYPE_VEC
Definition: meminfo.h:52
#define Real
Definition: machine.h:189
static Object ** v_resize(void *v)
Definition: ivocvect.cpp:1297
Real * ve
Definition: matrix.h:69
static char rcsid[]
Definition: hsehldr.c:37
Definition: matrix.h:67
#define v_copy(in, out)
Definition: matrix.h:404
#define E_SIZES
Definition: err.h:95
uint32_t u_int
Definition: machine.h:38
#define E_RANGE
Definition: err.h:104
void __mltadd__(Real *dp1, Real *dp2, double s, int len)
Definition: machine.c:76
u_int dim
Definition: matrix.h:68
VEC * hhtrvec(VEC *hh, double beta, u_int i0, VEC *in, VEC *out)
Definition: hsehldr.c:72
MAT * hhtrcols(MAT *M, u_int i0, u_int j0, VEC *hh, double beta)
Definition: hsehldr.c:146
#define E_BOUNDS
Definition: err.h:96
VEC * _v_copy(VEC *in, VEC *out, u_int i0)
Definition: copy.c:58
#define E_NULL
Definition: err.h:102
VEC * hhvec(VEC *vec, u_int i0, Real *beta, VEC *out, Real *newval)
Definition: hsehldr.c:47
double __ip__(Real *dp1, Real *dp2, int len)
Definition: machine.c:40
VEC * v_zero(VEC *x)
Definition: init.c:40
sqrt
Definition: extdef.h:3
#define MEM_STAT_REG(var, type)
Definition: meminfo.h:133
#define i
Definition: md1redef.h:12
#define error(err_num, fn_name)
Definition: err.h:73
fabs
Definition: extdef.h:3
#define VNULL
Definition: matrix.h:631
double _in_prod(VEC *x, VEC *y, u_int i0)
return NULL
Definition: cabcode.cpp:461
Definition: matrix.h:73
MAT * hhtrrows(MAT *M, u_int i0, u_int j0, VEC *hh, double beta)
Definition: hsehldr.c:100