void SnnsCLib::RbfMulMatrix(RbfFloatMatrix *m1, RbfFloatMatrix *m2, RbfFloatMatrix *m3) { int dest_c; int dest_r; int count; #ifdef DEBUG_MODE if (m1 -> rows != m2 -> rows || m1 -> columns != m3 -> columns || m2 -> columns != m3 -> rows) { ErrMess("RbfMulMatrix: incompatible matrixes.\n"); } #endif /* This seems to be a strange way to multiply two matrices but */ /* it prevents the swapper from trashing: */ RbfClearMatrix(m1, 0.0); for (dest_r = 0; dest_r < m1 -> rows; dest_r++) { for (count = 0; count < m2 -> columns; count++) { for (dest_c = 0; dest_c < m1 -> columns; dest_c++) RbfMatrixSetValue(m1, dest_r, dest_c, RbfMatrixGetValue(m2, dest_r, count) * RbfMatrixGetValue(m3, count, dest_c) + RbfMatrixGetValue(m1, dest_r, dest_c)); } } }
void SnnsCLib::RbfMulTranspMatrix(RbfFloatMatrix *m1, RbfFloatMatrix *m2) { int dest_c; int dest_r; int count; register float scalar_product; #ifdef DEBUG_MODE if (m2 -> rows != m1 -> rows || m2 -> rows != m1 -> columns) { ErrMess("RbfMulTranspMatrix: incompatible matrixes.\n"); } #endif /* notice that m2*m2T is a symmetric matrix! */ for (dest_r = 0; dest_r < m1 -> rows; dest_r++) { for (dest_c = dest_r; dest_c < m1 -> rows; dest_c++) { scalar_product = 0.0; for (count = 0; count < m2 -> columns; count++) { scalar_product += RbfMatrixGetValue(m2, dest_r, count) * RbfMatrixGetValue(m2, dest_c, count); } RbfMatrixSetValue(m1, dest_r, dest_c, scalar_product); if (dest_r != dest_c) RbfMatrixSetValue(m1, dest_c, dest_r, scalar_product); } } }
int RbfInvMatrix(RbfFloatMatrix *m) { register int i, j; register int *indx; register float *b; register int tmp_err; RbfFloatMatrix help; #ifdef DEBUG_MODE if (m -> rows != m -> columns) ErrMess("RbfInvMatrix: matrix not of form N x N.\n"); #endif if (!RbfAllocMatrix(m -> rows, m -> rows, &help) || (indx = (int *) malloc(m -> rows * sizeof(int))) == NULL || (b = (float *) malloc(m -> rows * sizeof(float))) == NULL) { ErrMess("RbfInvMatrix: impossible to allocate helpmatrix.\n"); return KRERR_INSUFFICIENT_MEM; } RbfSetMatrix(&help, m); if ((tmp_err = RbfLUDcmp(&help, indx)) != 1) { free(b); free(indx); RbfFreeMatrix(&help); return tmp_err; } for (j = 0; j < m -> rows; j++) { for (i = 0; i < m -> rows; i++) b[i] = 0.0; b[j] = 1.0; RbfLUBksb(&help, indx, b); for (i = 0; i < m -> rows; i++) RbfMatrixSetValue(m, i, j, b[i]); } free(b); free(indx); RbfFreeMatrix(&help); return 1; }
void RbfTranspMatrix(RbfFloatMatrix *m1, RbfFloatMatrix *m2) { int r; int c; #ifdef DEBUG_MODE if (m1 -> rows != m2 -> columns || m1 -> columns != m2 -> rows) ErrMess("RbfTranspMatrix: incompatible matrixes.\n"); #endif /* make m1 to become m2 transposed */ for (r = 0; r < m2 -> rows; r++) for (c = 0; c < m2 -> columns; c++) RbfMatrixSetValue(m1, c, r, RbfMatrixGetValue(m2, r, c)); }
void SnnsCLib::RbfAddMatrix(RbfFloatMatrix *m1, RbfFloatMatrix *m2, RbfFloatMatrix *m3) { int dest_c; int dest_r; #ifdef DEBUG_MODE if (!(m1 -> rows == m2 -> rows && m2 -> rows == m3 -> rows) || !(m1 -> columns == m2 -> columns && m2 -> columns == m3 -> columns) ) { ErrMess("RbfAddMatrix: incompatible matrixes.\n"); } #endif for (dest_r = 0; dest_r < m1 -> rows; dest_r++) for (dest_c = 0; dest_c < m1 -> columns; dest_c++) RbfMatrixSetValue(m1, dest_r, dest_c, RbfMatrixGetValue(m2, dest_r, dest_c) + RbfMatrixGetValue(m3, dest_r, dest_c)); }
void RbfSetMatrix(RbfFloatMatrix *m1, RbfFloatMatrix *m2) { int count; float *ptofrom; float *ptoto; #ifdef DEBUG_MODE if (m1 -> rows != m2 -> rows || m1 -> columns != m2 -> columns) { ErrMess("RbfSetMatrix: incompatible matrixes.\n"); } #endif count = (m2 -> rows)*(m2 -> columns); ptofrom = m2 -> field; ptoto = m1 -> field; while(count--) { *ptoto++ = *ptofrom++; } }
static int RbfLUDcmp(RbfFloatMatrix *m, int *indx) { register float sum; register float dum; register float big; register int i, j, k, imax; register float temp; register float *vv; if ((vv = (float *) malloc(m -> rows * sizeof(float))) == NULL) { ErrMess("RbfLUDcmp: impossible to allocate helpmatrix.\n"); return KRERR_INSUFFICIENT_MEM; } for (i = 0; i < m -> rows; i++) { big = 0.0; for (j = 0; j < m -> rows; j++) { if ((temp = fabs(RbfMatrixGetValue(m, i, j))) > big) big = temp; } if (big == 0.0) { free(vv); return 0; } vv[i] = 1.0/big; } for (j = 0; j < m -> rows; j++) { for (i = 0; i < j; i++) { sum = RbfMatrixGetValue(m, i, j); for (k = 0; k < i; k++) sum -= RbfMatrixGetValue(m, i, k) * RbfMatrixGetValue(m, k, j); RbfMatrixSetValue(m, i, j, sum); } big = 0.0; for (i = j; i < m -> rows; i++) { sum = RbfMatrixGetValue(m, i, j); for (k = 0; k < j; k++) sum -= RbfMatrixGetValue(m, i, k) * RbfMatrixGetValue(m, k, j); RbfMatrixSetValue(m, i, j, sum); if ((dum = vv[i] * fabs(sum)) >= big) { big = dum; imax = i; } } if (j != imax) { for (k = 0; k < m -> rows; k++) { dum = RbfMatrixGetValue(m, imax, k); RbfMatrixSetValue(m, imax, k, RbfMatrixGetValue(m, j, k)); RbfMatrixSetValue(m, j, k, dum); } dum = vv[imax]; vv[imax] = vv[j]; vv[j] = dum; } indx[j] = imax; if (RbfMatrixGetValue(m, j, j) == 0.0) { fprintf(stderr,"RbfLUDcmp: seems to be a singular matrix\n"); free(vv); return 0; } if (j != (m -> rows - 1)) { dum = 1.0/RbfMatrixGetValue(m, j, j); for (i = j+1; i < m -> rows; i++) RbfMatrixSetValue(m, i, j, RbfMatrixGetValue(m, i, j) * dum); } } free(vv); return 1; }
int SnnsCLib::RbfInvMatrix(RbfFloatMatrix *m) { RbfFloatMatrix help; int i, j, r, k, n; float max, hr, hi; #ifdef DEBUG_MODE if (m -> rows != m -> columns) ErrMess(const_cast<char*>("RbfInvMatrix: matrix not of form N x N.\n")); #endif n = m -> rows; /* n = dimension of matrix */ /* allocate helpmatrix: 0. row: field p[N] of algorithm */ /* 1. row: field hv[N] of algorithm */ if (!RbfAllocMatrix(2, n, &help)) { ErrMess(const_cast<char*>("RbfInvMatrix: impossible to allocate helpmatrix.\n")); return KRERR_INSUFFICIENT_MEM; } /* initialize permutation field: */ for (j = 0; j < n; j++) RbfMatrixSetValue(&help, 0, j, (float) j); /* invert matrix: */ /* notation in comments: m[r,c] means element at row r column c */ for (j = 0; j < n; j++) { /* looking for pivot: */ /* find row r, where r >= j and m[r,j] is maximal */ max = fabs(RbfMatrixGetValue(m, j, j)); r = j; for (i = j+1; i < n; i++) { hi = fabs(RbfMatrixGetValue(m, j, i)); if (hi > max) { max = hi; r = i; } } /* test if matrix is singulary: */ /* if true, then return directly and report errorcode */ if (max == 0.0) { RbfFreeMatrix(&help); return 0; } /* if r != j (r > j) then switch row r with row j and mark */ /* this in helpmatrix: */ if (r > j) { for (k = 0; k < n; k++) { hr = RbfMatrixGetValue(m, j, k); RbfMatrixSetValue(m, j, k, RbfMatrixGetValue(m, r, k)); RbfMatrixSetValue(m, r, k, hr); } hi = RbfMatrixGetValue(&help, 0, j); RbfMatrixSetValue(&help, 0, j, RbfMatrixGetValue(&help, 0, r)); RbfMatrixSetValue(&help, 0, r, hi); } /* do the transformation: */ hr = 1.0 / RbfMatrixGetValue(m, j, j); for (i = 0; i < n; i++) { RbfMatrixSetValue(m, i, j, hr * RbfMatrixGetValue(m, i, j)); } RbfMatrixSetValue(m, j, j, hr); for (k = 0; k < n; k++) if (k != j) { for (i = 0; i < n; i++) if (i != j) { RbfMatrixSetValue(m, i, k, RbfMatrixGetValue(m, i, k) - RbfMatrixGetValue(m, i, j) * RbfMatrixGetValue(m, j, k)); } RbfMatrixSetValue(m, j, k, -hr * RbfMatrixGetValue(m, j, k)); } } /* now switch back the columns: */ for (i = 0; i < n; i++) { for (k = 0; k < n; k++) RbfMatrixSetValue(&help, 1, (int) RbfMatrixGetValue(&help, 0, k), RbfMatrixGetValue(m, i, k)); for (k = 0; k < n; k++) RbfMatrixSetValue(m, i, k, RbfMatrixGetValue(&help, 1, k)); } /* report success: */ RbfFreeMatrix(&help); return 1; }