42.2 BLAS-like Primitives: AXPY, DOT, GEMV
Core building blocks for linear algebra mirror BLAS patterns: AXPY (y ← a·x + y), DOT (dot product), and GEMV (matrix–vector).
42.2.1 AXPY
void axpy(float a, float[] x, float[] y) {
for (int i = 0; i < x.length; i++) y[i] += a * x[i];
}
42.2.2 DOT (Dot Product)
float dot(float[] x, float[] y) {
float acc = 0f;
for (int i = 0; i < x.length; i++) acc += x[i] * y[i];
return acc;
}
Use Kahan summation to reduce floating‑point error when needed:
float dotKahan(float[] x, float[] y) {
float sum = 0f, c = 0f; // compensation
for (int i = 0; i < x.length; i++) {
float prod = x[i] * y[i];
float yk = prod - c;
float t = sum + yk;
c = (t - sum) - yk;
sum = t;
}
return sum;
}
42.2.3 GEMV (Matrix–Vector)
Row‑major matrix times vector:
void gemv(float[] A, int M, int N, float[] x, float[] y) {
for (int i = 0; i < M; i++) {
float acc = 0f;
int base = i * N;
for (int j = 0; j < N; j++) acc += A[base + j] * x[j];
y[i] = acc;
}
}
Transpose GEMV (A^T · x) for column‑wise operations:
void gemvT(float[] A, int M, int N, float[] x, float[] y) {
java.util.Arrays.fill(y, 0f);
for (int i = 0; i < M; i++) {
int base = i * N;
float xi = x[i];
for (int j = 0; j < N; j++) y[j] += A[base + j] * xi;
}
}