XCSF 1.4.8
XCSF learning classifier system
Loading...
Searching...
No Matches
blas.c
Go to the documentation of this file.
1/*
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation, either version 3 of the License, or
5 * (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 */
15
24#include "blas.h"
25
26static void
27gemm_nn(const int M, const int N, const int K, const double ALPHA,
28 const double *A, const int lda, const double *B, const int ldb,
29 double *C, const int ldc)
30{
31 for (int i = 0; i < M; ++i) {
32 for (int k = 0; k < K; ++k) {
33 const double A_PART = ALPHA * A[i * lda + k];
34 for (int j = 0; j < N; ++j) {
35 C[i * ldc + j] += A_PART * B[k * ldb + j];
36 }
37 }
38 }
39}
40
41static void
42gemm_nt(const int M, const int N, const int K, const double ALPHA,
43 const double *A, const int lda, const double *B, const int ldb,
44 double *C, const int ldc)
45{
46 for (int i = 0; i < M; ++i) {
47 for (int j = 0; j < N; ++j) {
48 double sum = 0;
49 for (int k = 0; k < K; ++k) {
50 sum += ALPHA * A[i * lda + k] * B[j * ldb + k];
51 }
52 C[i * ldc + j] += sum;
53 }
54 }
55}
56
57static void
58gemm_tn(const int M, const int N, const int K, const double ALPHA,
59 const double *A, const int lda, const double *B, const int ldb,
60 double *C, const int ldc)
61{
62 for (int i = 0; i < M; ++i) {
63 for (int k = 0; k < K; ++k) {
64 const double A_PART = ALPHA * A[k * lda + i];
65 for (int j = 0; j < N; ++j) {
66 C[i * ldc + j] += A_PART * B[k * ldb + j];
67 }
68 }
69 }
70}
71
72static void
73gemm_tt(const int M, const int N, const int K, const double ALPHA,
74 const double *A, const int lda, const double *B, const int ldb,
75 double *C, const int ldc)
76{
77 for (int i = 0; i < M; ++i) {
78 for (int j = 0; j < N; ++j) {
79 double sum = 0;
80 for (int k = 0; k < K; ++k) {
81 sum += ALPHA * A[i + k * lda] * B[k + j * ldb];
82 }
83 C[i * ldc + j] += sum;
84 }
85 }
86}
87
107void
108blas_gemm(const int TA, const int TB, const int M, const int N, const int K,
109 const double ALPHA, const double *A, const int lda, const double *B,
110 const int ldb, const double BETA, double *C, const int ldc)
111{
112 for (int i = 0; i < M; ++i) {
113 for (int j = 0; j < N; ++j) {
114 C[i * ldc + j] *= BETA;
115 }
116 }
117 if (!TA && !TB) {
118 gemm_nn(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
119 } else if (TA && !TB) {
120 gemm_tn(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
121 } else if (!TA && TB) {
122 gemm_nt(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
123 } else {
124 gemm_tt(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
125 }
126}
127
137void
138blas_axpy(const int N, const double ALPHA, const double *X, const int INCX,
139 double *Y, const int INCY)
140{
141 if (ALPHA != 1) {
142 for (int i = 0; i < N; ++i) {
143 Y[i * INCY] += ALPHA * X[i * INCX];
144 }
145 } else {
146 for (int i = 0; i < N; ++i) {
147 Y[i * INCY] += X[i * INCX];
148 }
149 }
150}
151
159void
160blas_scal(const int N, const double ALPHA, double *X, const int INCX)
161{
162 if (ALPHA != 0) {
163 for (int i = 0; i < N; ++i) {
164 X[i * INCX] *= ALPHA;
165 }
166 } else {
167 for (int i = 0; i < N; ++i) {
168 X[i * INCX] = 0;
169 }
170 }
171}
172
180void
181blas_fill(const int N, const double ALPHA, double *X, const int INCX)
182{
183 for (int i = 0; i < N; ++i) {
184 X[i * INCX] = ALPHA;
185 }
186}
187
197double
198blas_dot(const int N, const double *X, const int INCX, const double *Y,
199 const int INCY)
200{
201 double dot = 0;
202 for (int i = 0; i < N; ++i) {
203 dot += X[i * INCX] * Y[i * INCY];
204 }
205 return dot;
206}
207
216void
217blas_mul(const int N, const double *X, const int INCX, double *Y,
218 const int INCY)
219{
220 for (int i = 0; i < N; ++i) {
221 Y[i * INCY] *= X[i * INCX];
222 }
223}
224
231double
232blas_sum(const double *X, const int N)
233{
234 double sum = 0;
235 for (int i = 0; i < N; ++i) {
236 sum += X[i];
237 }
238 return sum;
239}
static void gemm_nn(const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, double *C, const int ldc)
Definition blas.c:27
static void gemm_tn(const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, double *C, const int ldc)
Definition blas.c:58
void blas_fill(const int N, const double ALPHA, double *X, const int INCX)
Fills the vector X with the value ALPHA.
Definition blas.c:181
void blas_scal(const int N, const double ALPHA, double *X, const int INCX)
Scales vector X by the scalar ALPHA and overwrites it with the result.
Definition blas.c:160
double blas_sum(const double *X, const int N)
Returns the sum of the vector X.
Definition blas.c:232
double blas_dot(const int N, const double *X, const int INCX, const double *Y, const int INCY)
Computes the dot product of two vectors.
Definition blas.c:198
static void gemm_nt(const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, double *C, const int ldc)
Definition blas.c:42
static void gemm_tt(const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, double *C, const int ldc)
Definition blas.c:73
void blas_axpy(const int N, const double ALPHA, const double *X, const int INCX, double *Y, const int INCY)
Multiplies vector X by the scalar ALPHA and adds it to the vector Y.
Definition blas.c:138
void blas_mul(const int N, const double *X, const int INCX, double *Y, const int INCY)
Multiplies vector X by the vector Y and stores the result in vector Y.
Definition blas.c:217
void blas_gemm(const int TA, const int TB, const int M, const int N, const int K, const double ALPHA, const double *A, const int lda, const double *B, const int ldb, const double BETA, double *C, const int ldc)
Performs the matrix-matrix multiplication: .
Definition blas.c:108
Basic linear algebra functions.