Debian Codegraph @

packages/seacas/libraries/chaco/eigen/splarax.c

raw
1 /*
2 * Copyright(C) 1999-2020, 2023 National Technology & Engineering Solutions
3 * of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with
4 * NTESS, the U.S. Government retains certain rights in this software.
5 *
6 * See packages/seacas/LICENSE for details
7 */
8
9 #include "defs.h"
10 #include <stdio.h> // for NULL
11
12 /* Sparse linked A(matrix) times x(vector), double precision. */
13 void splarax(double *result, /* result of matrix vector multiplication */
14 struct vtx_data **mat, /* graph data structure */
15 int n, /* number of rows/columns in matrix */
16 double *vec, /* vector being multiplied by matrix */
17 double *vwsqrt, /* square roots of vertex weights */
18 double *work /* work vector from 1-n */
19 )
20 {
21 extern int PERTURB; /* perturb matrix? */
22 extern int NPERTURB; /* if so, number of edges to perturb */
23 extern double PERTURB_MAX; /* maximum value of perturbation */
24 struct vtx_data *mat_i; /* an entry in "mat" */
25 double sum; /* sums inner product of matrix-row & vector */
26 int *colpntr; /* loops through indices of nonzeros in a row */
27 float *wgtpntr; /* loops through values of nonzeros */
28 int i, j; /* loop counters */
29 double *wrkpntr; /* loops through indices of work vector */
30 double *vwsqpntr; /* loops through indices of vwsqrt */
31 double *vecpntr; /* loops through indices of vec */
32 double *respntr; /* loops through indices of result */
33 int last_edge; /* last edge in edge list */
34
35 if (vwsqrt == NULL) { /* No vertex weights */
36 if (mat[1]->ewgts == NULL) { /* No edge weights */
37 respntr = result;
38 for (i = 1; i <= n; i++) {
39 mat_i = mat[i];
40 colpntr = mat_i->edges;
41 last_edge = mat_i->nedges - 1;
42 sum = last_edge * vec[*colpntr++];
43 for (j = last_edge; j; j--) {
44 sum -= vec[*colpntr++];
45 }
46 *(++respntr) = sum;
47 }
48 }
49 else { /* Edge weights */
50 respntr = result;
51 for (i = 1; i <= n; i++) {
52 mat_i = mat[i];
53 colpntr = mat_i->edges;
54 wgtpntr = mat_i->ewgts;
55 sum = 0.0;
56 for (j = mat_i->nedges; j; j--) {
57 sum -= *wgtpntr++ * vec[*colpntr++];
58 }
59 *(++respntr) = sum; /* -sum if want -Ax */
60 }
61 }
62 if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) {
63 perturb(result, vec);
64 }
65 }
66 else { /* Vertex weights */
67 wrkpntr = work;
68 vecpntr = vec;
69 vwsqpntr = vwsqrt;
70 for (i = n; i; i--) {
71 *(++wrkpntr) = *(++vecpntr) / *(++vwsqpntr);
72 }
73
74 if (mat[1]->ewgts == NULL) { /* No edge weights. */
75 respntr = result;
76 for (i = 1; i <= n; i++) {
77 mat_i = mat[i];
78 colpntr = mat_i->edges;
79 last_edge = mat_i->nedges - 1;
80 sum = (last_edge)*work[*colpntr++];
81 for (j = last_edge; j; j--) {
82 sum -= work[*colpntr++];
83 }
84 *(++respntr) = sum;
85 }
86 }
87 else { /* Edge weights. */
88 respntr = result;
89 for (i = 1; i <= n; i++) {
90 mat_i = mat[i];
91 colpntr = mat_i->edges;
92 wgtpntr = mat_i->ewgts;
93 sum = 0.0;
94 for (j = mat_i->nedges; j; j--) {
95 sum -= *wgtpntr++ * work[*colpntr++];
96 }
97 *(++respntr) = sum; /* -sum if want -Ax */
98 }
99 }
100 if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) {
101 perturb(result, work);
102 }
103
104 respntr = result;
105 vwsqpntr = vwsqrt;
106 for (i = n; i; i--) {
107 *(++respntr) /= *(++vwsqpntr);
108 }
109 }
110 }
111
112 /* Sparse linked A(matrix) times x(vector)i, float version. */
113 void splarax_float(float *result, /* result of matrix vector multiplication */
114 struct vtx_data **mat, /* graph data structure */
115 int n, /* number of rows/columns in matrix */
116 float *vec, /* vector being multiplied by matrix */
117 float *vwsqrt, /* square roots of vertex weights */
118 float *work /* work vector from 1-n */
119 )
120 {
121 extern int PERTURB; /* perturb matrix? */
122 extern int NPERTURB; /* if so, number of edges to perturb */
123 extern double PERTURB_MAX; /* maximum value of perturbation */
124 struct vtx_data *mat_i; /* an entry in "mat" */
125 double sum; /* sums inner product of matrix-row & vector */
126 int *colpntr; /* loops through indices of nonzeros in a row */
127 float *wgtpntr; /* loops through values of nonzeros */
128 int i, j; /* loop counters */
129 float *wrkpntr; /* loops through indices of work vector */
130 float *vwsqpntr; /* loops through indices of vwsqrt */
131 float *vecpntr; /* loops through indices of vec */
132 float *respntr; /* loops through indices of result */
133 int last_edge; /* last edge in edge list */
134
135 if (vwsqrt == NULL) { /* No vertex weights */
136 if (mat[1]->ewgts == NULL) { /* No edge weights */
137 respntr = result;
138 for (i = 1; i <= n; i++) {
139 mat_i = mat[i];
140 colpntr = mat_i->edges;
141 last_edge = mat_i->nedges - 1;
142 sum = (last_edge)*vec[*colpntr++];
143 for (j = last_edge; j; j--) {
144 sum -= vec[*colpntr++];
145 }
146 *(++respntr) = sum;
147 }
148 }
149 else { /* Edge weights */
150 respntr = result;
151 for (i = 1; i <= n; i++) {
152 mat_i = mat[i];
153 colpntr = mat_i->edges;
154 wgtpntr = mat_i->ewgts;
155 sum = 0.0;
156 for (j = mat_i->nedges; j; j--) {
157 sum -= *wgtpntr++ * vec[*colpntr++];
158 }
159 *(++respntr) = sum; /* -sum if want -Ax */
160 }
161 }
162 if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) {
163 perturb_float(result, vec);
164 }
165 }
166 else { /* Vertex weights */
167 wrkpntr = work;
168 vecpntr = vec;
169 vwsqpntr = vwsqrt;
170 for (i = n; i; i--) {
171 *(++wrkpntr) = *(++vecpntr) / *(++vwsqpntr);
172 }
173
174 if (mat[1]->ewgts == NULL) { /* No edge weights. */
175 respntr = result;
176 for (i = 1; i <= n; i++) {
177 mat_i = mat[i];
178 colpntr = mat_i->edges;
179 last_edge = mat_i->nedges - 1;
180 sum = (last_edge)*work[*colpntr++];
181 for (j = last_edge; j; j--) {
182 sum -= work[*colpntr++];
183 }
184 *(++respntr) = sum;
185 }
186 }
187 else { /* Edge weights. */
188 respntr = result;
189 for (i = 1; i <= n; i++) {
190 mat_i = mat[i];
191 colpntr = mat_i->edges;
192 wgtpntr = mat_i->ewgts;
193 sum = 0.0;
194 for (j = mat_i->nedges; j; j--) {
195 sum -= *wgtpntr++ * work[*colpntr++];
196 }
197 *(++respntr) = sum; /* -sum if want -Ax */
198 }
199 }
200 if (PERTURB && NPERTURB > 0 && PERTURB_MAX > 0.0) {
201 perturb_float(result, work);
202 }
203
204 respntr = result;
205 vwsqpntr = vwsqrt;
206 for (i = n; i; i--) {
207 *(++respntr) /= *(++vwsqpntr);
208 }
209 }
210 }