NEURON
givens.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 /*
30  Files for matrix computations
31 
32  Givens operations file. Contains routines for calculating and
33  applying givens rotations for/to vectors and also to matrices by
34  row and by column.
35 */
36 
37 /* givens.c 1.2 11/25/87 */
38 static char rcsid[] = "givens.c,v 1.1 1997/12/04 17:55:22 hines Exp";
39 
40 #include <stdio.h>
41 #include "matrix.h"
42 #include "matrix2.h"
43 #include <math.h>
44 
45 /* givens -- returns c,s parameters for Givens rotation to
46  eliminate y in the vector [ x y ]' */
47 void givens(x,y,c,s)
48 double x,y;
49 Real *c,*s;
50 {
51  Real norm;
52 
53  norm = sqrt(x*x+y*y);
54  if ( norm == 0.0 )
55  { *c = 1.0; *s = 0.0; } /* identity */
56  else
57  { *c = x/norm; *s = y/norm; }
58 }
59 
60 /* rot_vec -- apply Givens rotation to x's i & k components */
61 VEC *rot_vec(x,i,k,c,s,out)
62 VEC *x,*out;
63 u_int i,k;
64 double c,s;
65 {
66  Real temp;
67 
68  if ( x==VNULL )
69  error(E_NULL,"rot_vec");
70  if ( i >= x->dim || k >= x->dim )
71  error(E_RANGE,"rot_vec");
72  out = v_copy(x,out);
73 
74  /* temp = c*out->ve[i] + s*out->ve[k]; */
75  temp = c*v_entry(out,i) + s*v_entry(out,k);
76  /* out->ve[k] = -s*out->ve[i] + c*out->ve[k]; */
77  v_set_val(out,k,-s*v_entry(out,i)+c*v_entry(out,k));
78  /* out->ve[i] = temp; */
79  v_set_val(out,i,temp);
80 
81  return (out);
82 }
83 
84 /* rot_rows -- premultiply mat by givens rotation described by c,s */
85 MAT *rot_rows(mat,i,k,c,s,out)
86 MAT *mat,*out;
87 u_int i,k;
88 double c,s;
89 {
90  u_int j;
91  Real temp;
92 
93  if ( mat==(MAT *)NULL )
94  error(E_NULL,"rot_rows");
95  if ( i >= mat->m || k >= mat->m )
96  error(E_RANGE,"rot_rows");
97  if ( mat != out )
98  out = m_copy(mat,m_resize(out,mat->m,mat->n));
99 
100  for ( j=0; j<mat->n; j++ )
101  {
102  /* temp = c*out->me[i][j] + s*out->me[k][j]; */
103  temp = c*m_entry(out,i,j) + s*m_entry(out,k,j);
104  /* out->me[k][j] = -s*out->me[i][j] + c*out->me[k][j]; */
105  m_set_val(out,k,j, -s*m_entry(out,i,j) + c*m_entry(out,k,j));
106  /* out->me[i][j] = temp; */
107  m_set_val(out,i,j, temp);
108  }
109 
110  return (out);
111 }
112 
113 /* rot_cols -- postmultiply mat by givens rotation described by c,s */
114 MAT *rot_cols(mat,i,k,c,s,out)
115 MAT *mat,*out;
116 u_int i,k;
117 double c,s;
118 {
119  u_int j;
120  Real temp;
121 
122  if ( mat==(MAT *)NULL )
123  error(E_NULL,"rot_cols");
124  if ( i >= mat->n || k >= mat->n )
125  error(E_RANGE,"rot_cols");
126  if ( mat != out )
127  out = m_copy(mat,m_resize(out,mat->m,mat->n));
128 
129  for ( j=0; j<mat->m; j++ )
130  {
131  /* temp = c*out->me[j][i] + s*out->me[j][k]; */
132  temp = c*m_entry(out,j,i) + s*m_entry(out,j,k);
133  /* out->me[j][k] = -s*out->me[j][i] + c*out->me[j][k]; */
134  m_set_val(out,j,k, -s*m_entry(out,j,i) + c*m_entry(out,j,k));
135  /* out->me[j][i] = temp; */
136  m_set_val(out,j,i,temp);
137  }
138 
139  return (out);
140 }
141 
MAT * rot_rows(MAT *mat, u_int i, u_int k, double c, double s, MAT *out)
Definition: givens.c:85
#define Real
Definition: machine.h:189
MAT * rot_cols(MAT *mat, u_int i, u_int k, double c, double s, MAT *out)
Definition: givens.c:114
#define m_set_val(A, i, j, val)
Definition: matrix.h:277
static philox4x32_key_t k
Definition: nrnran123.cpp:11
#define m_copy(in, out)
Definition: matrix.h:403
Definition: matrix.h:67
#define v_copy(in, out)
Definition: matrix.h:404
uint32_t u_int
Definition: machine.h:38
#define E_RANGE
Definition: err.h:104
_CONST char * s
Definition: system.cpp:74
void givens(double x, double y, Real *c, Real *s)
Definition: givens.c:47
#define E_NULL
Definition: err.h:102
size_t j
#define v_set_val(x, i, val)
Definition: matrix.h:265
#define v_entry(x, i)
Definition: matrix.h:262
VEC * rot_vec(VEC *x, u_int i, u_int k, double c, double s, VEC *out)
Definition: givens.c:61
sqrt
Definition: extdef.h:3
static Object ** m_resize(void *v)
Definition: matrix.cpp:190
#define i
Definition: md1redef.h:12
#define c
#define error(err_num, fn_name)
Definition: err.h:73
#define VNULL
Definition: matrix.h:631
return NULL
Definition: cabcode.cpp:461
Definition: matrix.h:73
#define m_entry(A, i, j)
Definition: matrix.h:274
static char rcsid[]
Definition: givens.c:38