| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | #include <string.h> |
| | #include <stdlib.h> |
| |
|
| | #ifdef HAVE_CONFIG_H |
| | #include "config.h" |
| | #endif |
| |
|
| | #include <pocketsphinx/err.h> |
| |
|
| | #include "util/clapack_lite.h" |
| | #include "util/matrix.h" |
| | #include "util/ckd_alloc.h" |
| |
|
| | void |
| | norm_3d(float32 ***arr, |
| | uint32 d1, |
| | uint32 d2, |
| | uint32 d3) |
| | { |
| | uint32 i, j, k; |
| | float64 s; |
| |
|
| | for (i = 0; i < d1; i++) { |
| | for (j = 0; j < d2; j++) { |
| |
|
| | |
| | for (k = 0, s = 0; k < d3; k++) { |
| | s += arr[i][j][k]; |
| | } |
| |
|
| | |
| | s = 1.0 / s; |
| |
|
| | |
| | for (k = 0; k < d3; k++) { |
| | arr[i][j][k] *= s; |
| | } |
| | } |
| | } |
| | } |
| | |
| | void |
| | accum_3d(float32 ***out, |
| | float32 ***in, |
| | uint32 d1, |
| | uint32 d2, |
| | uint32 d3) |
| | { |
| | uint32 i, j, k; |
| |
|
| | for (i = 0; i < d1; i++) { |
| | for (j = 0; j < d2; j++) { |
| | for (k = 0; k < d3; k++) { |
| | out[i][j][k] += in[i][j][k]; |
| | } |
| | } |
| | } |
| | } |
| |
|
| | void |
| | floor_nz_3d(float32 ***m, |
| | uint32 d1, |
| | uint32 d2, |
| | uint32 d3, |
| | float32 floor) |
| | { |
| | uint32 i, j, k; |
| |
|
| | for (i = 0; i < d1; i++) { |
| | for (j = 0; j < d2; j++) { |
| | for (k = 0; k < d3; k++) { |
| | if ((m[i][j][k] != 0) && (m[i][j][k] < floor)) |
| | m[i][j][k] = floor; |
| | } |
| | } |
| | } |
| | } |
| | void |
| | floor_nz_1d(float32 *v, |
| | uint32 d1, |
| | float32 floor) |
| | { |
| | uint32 i; |
| |
|
| | for (i = 0; i < d1; i++) { |
| | if ((v[i] != 0) && (v[i] < floor)) |
| | v[i] = floor; |
| | } |
| | } |
| |
|
| | void |
| | band_nz_1d(float32 *v, |
| | uint32 d1, |
| | float32 band) |
| | { |
| | uint32 i; |
| |
|
| | for (i = 0; i < d1; i++) { |
| | if (v[i] != 0) { |
| | if ((v[i] > 0) && (v[i] < band)) { |
| | v[i] = band; |
| | } |
| | else if ((v[i] < 0) && (v[i] > -band)) { |
| | v[i] = -band; |
| | } |
| | } |
| | } |
| | } |
| |
|
| | |
| | float64 |
| | determinant(float32 ** a, int32 n) |
| | { |
| | float32 **tmp_a; |
| | float64 det; |
| | char uplo; |
| | int32 info, i; |
| |
|
| | |
| | |
| | |
| | tmp_a = (float32 **)ckd_calloc_2d(n, n, sizeof(float32)); |
| | memcpy(tmp_a[0], a[0], n*n*sizeof(float32)); |
| |
|
| | uplo = 'L'; |
| | spotrf_(&uplo, &n, tmp_a[0], &n, &info); |
| | det = tmp_a[0][0]; |
| | |
| | for (i = 1; i < n; ++i) |
| | det *= tmp_a[i][i]; |
| | ckd_free_2d((void **)tmp_a); |
| | if (info > 0) |
| | return -1.0; |
| | else |
| | return det * det; |
| | } |
| |
|
| | int32 |
| | solve(float32 **a, |
| | float32 *b, |
| | float32 *out_x, |
| | int32 n) |
| | { |
| | char uplo; |
| | float32 **tmp_a; |
| | int32 info, nrhs; |
| |
|
| | |
| | |
| | |
| | tmp_a = (float32 **)ckd_calloc_2d(n, n, sizeof(float32)); |
| | memcpy(tmp_a[0], a[0], n*n*sizeof(float32)); |
| | memcpy(out_x, b, n*sizeof(float32)); |
| | uplo = 'L'; |
| | nrhs = 1; |
| | sposv_(&uplo, &n, &nrhs, tmp_a[0], &n, out_x, &n, &info); |
| | ckd_free_2d((void **)tmp_a); |
| |
|
| | if (info != 0) |
| | return -1; |
| | else |
| | return info; |
| | } |
| |
|
| | |
| | int32 |
| | invert(float32 ** ainv, float32 ** a, int32 n) |
| | { |
| | char uplo; |
| | float32 **tmp_a; |
| | int32 info, nrhs, i; |
| |
|
| | |
| | |
| | |
| | tmp_a = (float32 **)ckd_calloc_2d(n, n, sizeof(float32)); |
| | memcpy(tmp_a[0], a[0], n*n*sizeof(float32)); |
| |
|
| | |
| | memset(ainv[0], 0, sizeof(float32) * n * n); |
| | for (i = 0; i < n; i++) |
| | ainv[i][i] = 1.0; |
| |
|
| | uplo = 'L'; |
| | nrhs = n; |
| | sposv_(&uplo, &n, &nrhs, tmp_a[0], &n, ainv[0], &n, &info); |
| |
|
| | ckd_free_2d((void **)tmp_a); |
| |
|
| | if (info != 0) |
| | return -1; |
| | else |
| | return info; |
| | } |
| |
|
| | void |
| | matrixmultiply(float32 ** c, float32 ** a, float32 ** b, int32 n) |
| | { |
| | char side, uplo; |
| | float32 alpha; |
| |
|
| | side = 'L'; |
| | uplo = 'L'; |
| | alpha = 1.0; |
| | ssymm_(&side, &uplo, &n, &n, &alpha, a[0], &n, b[0], &n, &alpha, c[0], &n); |
| | } |
| |
|
| | void |
| | outerproduct(float32 ** a, float32 * x, float32 * y, int32 len) |
| | { |
| | int32 i, j; |
| |
|
| | for (i = 0; i < len; ++i) { |
| | a[i][i] = x[i] * y[i]; |
| | for (j = i + 1; j < len; ++j) { |
| | a[i][j] = x[i] * y[j]; |
| | a[j][i] = x[j] * y[i]; |
| | } |
| | } |
| | } |
| |
|
| | void |
| | scalarmultiply(float32 ** a, float32 x, int32 n) |
| | { |
| | int32 i, j; |
| |
|
| | for (i = 0; i < n; ++i) { |
| | a[i][i] *= x; |
| | for (j = i+1; j < n; ++j) { |
| | a[i][j] *= x; |
| | a[j][i] *= x; |
| | } |
| | } |
| | } |
| |
|
| | void |
| | matrixadd(float32 ** a, float32 ** b, int32 n) |
| | { |
| | int32 i, j; |
| |
|
| | for (i = 0; i < n; ++i) |
| | for (j = 0; j < n; ++j) |
| | a[i][j] += b[i][j]; |
| | } |
| |
|