NEURON
submat.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 /* 1.2 submat.c 11/25/87 */
29 
30 #include <stdio.h>
31 #include "matrix.h"
32 
33 static char rcsid[] = "submat.c,v 1.1 1997/12/04 17:55:55 hines Exp";
34 
35 
36 /* get_col -- gets a specified column of a matrix and retruns it as a vector */
37 VEC *get_col(mat,col,vec)
38 u_int col;
39 MAT *mat;
40 VEC *vec;
41 {
42  u_int i;
43 
44  if ( mat==(MAT *)NULL )
45  error(E_NULL,"get_col");
46  if ( col >= mat->n )
47  error(E_RANGE,"get_col");
48  if ( vec==(VEC *)NULL || vec->dim<mat->m )
49  vec = v_resize(vec,mat->m);
50 
51  for ( i=0; i<mat->m; i++ )
52  vec->ve[i] = mat->me[i][col];
53 
54  return (vec);
55 }
56 
57 /* get_row -- gets a specified row of a matrix and retruns it as a vector */
58 VEC *get_row(mat,row,vec)
59 u_int row;
60 MAT *mat;
61 VEC *vec;
62 {
63  u_int i;
64 
65  if ( mat==(MAT *)NULL )
66  error(E_NULL,"get_row");
67  if ( row >= mat->m )
68  error(E_RANGE,"get_row");
69  if ( vec==(VEC *)NULL || vec->dim<mat->n )
70  vec = v_resize(vec,mat->n);
71 
72  for ( i=0; i<mat->n; i++ )
73  vec->ve[i] = mat->me[row][i];
74 
75  return (vec);
76 }
77 
78 /* _set_col -- sets column of matrix to values given in vec (in situ) */
79 MAT *_set_col(mat,col,vec,i0)
80 MAT *mat;
81 VEC *vec;
82 u_int col,i0;
83 {
84  u_int i,lim;
85 
86  if ( mat==(MAT *)NULL || vec==(VEC *)NULL )
87  error(E_NULL,"_set_col");
88  if ( col >= mat->n )
89  error(E_RANGE,"_set_col");
90  lim = min(mat->m,vec->dim);
91  for ( i=i0; i<lim; i++ )
92  mat->me[i][col] = vec->ve[i];
93 
94  return (mat);
95 }
96 
97 /* _set_row -- sets row of matrix to values given in vec (in situ) */
98 MAT *_set_row(mat,row,vec,j0)
99 MAT *mat;
100 VEC *vec;
101 u_int row,j0;
102 {
103  u_int j,lim;
104 
105  if ( mat==(MAT *)NULL || vec==(VEC *)NULL )
106  error(E_NULL,"_set_row");
107  if ( row >= mat->m )
108  error(E_RANGE,"_set_row");
109  lim = min(mat->n,vec->dim);
110  for ( j=j0; j<lim; j++ )
111  mat->me[row][j] = vec->ve[j];
112 
113  return (mat);
114 }
115 
116 /* sub_mat -- returns sub-matrix of old which is formed by the rectangle
117  from (row1,col1) to (row2,col2)
118  -- Note: storage is shared so that altering the "new"
119  matrix will alter the "old" matrix */
120 MAT *sub_mat(old,row1,col1,row2,col2,new)
121 MAT *old,*new;
122 u_int row1,col1,row2,col2;
123 {
124  u_int i;
125 
126  if ( old==(MAT *)NULL )
127  error(E_NULL,"sub_mat");
128  if ( row1 > row2 || col1 > col2 || row2 >= old->m || col2 >= old->n )
129  error(E_RANGE,"sub_mat");
130  if ( new==(MAT *)NULL || new->m < row2-row1+1 )
131  {
132  new = NEW(MAT);
133  new->me = NEW_A(row2-row1+1,Real *);
134  if ( new==(MAT *)NULL || new->me==(Real **)NULL )
135  error(E_MEM,"sub_mat");
136  else if (mem_info_is_on()) {
137  mem_bytes(TYPE_MAT,0,sizeof(MAT)+
138  (row2-row1+1)*sizeof(Real *));
139  }
140 
141  }
142  new->m = row2-row1+1;
143 
144  new->n = col2-col1+1;
145 
146  new->base = (Real *)NULL;
147 
148  for ( i=0; i < new->m; i++ )
149  new->me[i] = (old->me[i+row1]) + col1;
150 
151  return (new);
152 }
153 
154 
155 /* sub_vec -- returns sub-vector which is formed by the elements i1 to i2
156  -- as for sub_mat, storage is shared */
157 VEC *sub_vec(old,i1,i2,new)
158 VEC *old, *new;
159 int i1, i2;
160 {
161  if ( old == (VEC *)NULL )
162  error(E_NULL,"sub_vec");
163  if ( i1 > i2 || old->dim < i2 )
164  error(E_RANGE,"sub_vec");
165 
166  if ( new == (VEC *)NULL )
167  new = NEW(VEC);
168  if ( new == (VEC *)NULL )
169  error(E_MEM,"sub_vec");
170  else if (mem_info_is_on()) {
171  mem_bytes(TYPE_VEC,0,sizeof(VEC));
172  }
173 
174 
175  new->dim = i2 - i1 + 1;
176  new->ve = &(old->ve[i1]);
177 
178  return new;
179 }
#define TYPE_VEC
Definition: meminfo.h:52
#define min(a, b)
Definition: matrix.h:157
MAT * sub_mat(MAT *old, u_int row1, u_int col1, u_int row2, u_int col2, MAT *new)
Definition: submat.c:120
#define Real
Definition: machine.h:189
static Object ** v_resize(void *v)
Definition: ivocvect.cpp:1297
#define NEW_A(num, type)
Definition: matrix.h:139
MAT * _set_col(MAT *mat, u_int col, VEC *vec, u_int i0)
Definition: submat.c:79
static char rcsid[]
Definition: submat.c:33
u_int n
Definition: matrix.h:74
Real * ve
Definition: matrix.h:69
VEC * get_col(MAT *mat, u_int col, VEC *vec)
Definition: submat.c:37
int mem_info_is_on(void)
Definition: meminfo.c:221
Definition: matrix.h:67
uint32_t u_int
Definition: machine.h:38
#define E_RANGE
Definition: err.h:104
u_int dim
Definition: matrix.h:68
#define mem_bytes(type, old_size, new_size)
Definition: meminfo.h:136
#define E_NULL
Definition: err.h:102
size_t j
VEC * sub_vec(VEC *old, int i1, int i2, VEC *new)
Definition: submat.c:157
VEC * get_row(MAT *mat, u_int row, VEC *vec)
Definition: submat.c:58
static unsigned row
Definition: nonlin.cpp:89
MAT * _set_row(MAT *mat, u_int row, VEC *vec, u_int j0)
Definition: submat.c:98
#define i
Definition: md1redef.h:12
#define error(err_num, fn_name)
Definition: err.h:73
u_int m
Definition: matrix.h:74
#define TYPE_MAT
Definition: meminfo.h:49
return NULL
Definition: cabcode.cpp:461
Real ** me
Definition: matrix.h:76
#define NEW(type)
Definition: matrix.h:136
Definition: matrix.h:73
#define E_MEM
Definition: err.h:97