📄 nnmisc.c
字号:
/* Check whether vectors has the same length. */
if (ptv1->row != ptv2->row)
merror("Dimension mismatch error in sprod!");
#endif
/* Calculate scalar product of the vectors. */
elements = ptv1->row;
for ( i=0; i<elements; i++ ) {
prod += (cvget(ptv1,i))*(cvget(ptv2,i));
}
return prod;
}
/*
* MAKEINDEXVEC
* ------------
* Create an index vector.
*
* INPUTS: start - First element
* end - Last element
* step - Step
* OUTPUT: indexvector
*
*/
matrix *makeindexvec(int start, int end, int step)
{
/* FUNCTION DOES NOT WORK AND IS NOT USED */
int k, elements;
matrix *indexvec;
elements = (end-start)/step;
indexvec=mmake(elements,1);
for(k=0;k<elements;k++)
{
cvput(indexvec,k,(double)start);
start+=step;
}
return indexvec;
}
/*
* M2VRESHAPE
* ----------
*
* Matrix to Vector Reshape. Take the elements from the matrix M ROW wise
* and put them into the vector V starting from index1
*
* INPUTS: V - Pointer to vector
* index1 - Start index in V
* M - Pointer to matrix
*
*/
void m2vreshape(matrix *V, int index1, matrix *M)
{
int i, j;
for(i=0;i<getrows(M);i++)
{
for(j=0;j<getcols(M);j++)
{
cvput(V,index1++,get_val(M,i,j));
}
}
}
/*
* M2VRESHAPE2
* -----------
*
* Matrix to Vector Reshape. Take the elements from the matrix M COLUMN wise
* and put them into the vector V starting from index1
*
* INPUTS: V - Pointer to vector
* index1 - Start index in V
* M - Pointer to matrix
*
*/
void m2vreshape2(matrix *V, int index1, matrix *M)
{
int i, j;
for(i=0;i<nof_cols(M);i++)
{
for(j=0;j<nof_rows(M);j++)
{
cvput(V,index1++,get_val(M,j,i));
}
}
}
/*
* V2MRESHAPE
* ----------
*
* Vector to Matrix Reshape. Take the elements from the vector V, starting
* at index1, and insert them into the matrix M, ROW wise.
*
* INPUTS: M - Pointer to matrix
* V - Pointer to vector
* index1 - Start index in V
*
*/
void v2mreshape(matrix *M, matrix *V, int index1)
{
int i, j;
for(i=0;i<nof_rows(M);i++)
{
for(j=0;j<nof_cols(M);j++)
{
put_val(M,i,j,cvget(V,index1++));
}
}
}
/*
* CHOLDC
* ------
* This routine constructs the Cholesky decomposition, A = L*L' of a positive
* definite symmetric matrix.
*
* INPUTS: A - Matrix to be decomposed
* L - Matrix of same dimensions used for storage
*
*/
void choldc(matrix *A, matrix *L)
{
int i, j, k, n;
double sum;
n = nof_rows(A);
for(i=0; i<n; i++)
{
for(j=0; j<n; j++)
{
for(sum=get_val(A,i,j), k=i-1; k>=0; k--) sum-=get_val(L,i,k)*get_val(L,j,k);
if(i==j)
{
if( sum<= 0.0) merror("Choldc failed. Matrix not positive definite");
put_val(L,i,i,sqrt(sum));
}
else put_val(L,j,i,sum/get_val(L,i,i));
}
}
}
/*
* CHOLSL
* ------
* This routine constructs the Cholesky decomposition, A = L*L' of a positive
* definite symmetric matrix.
*
* INPUTS: A - Matrix to be decomposed
* L - Matrix of same dimensions used for storage
*
*/
void cholsl(matrix *L, matrix *x, matrix *b)
{
int i, k, n;
double sum;
n = nof_rows(b);
for(i=0; i<n; i++)
{
for(sum=cvget(b,i), k=i-1; k>=0; k--) sum -= get_val(L,i,k)*cvget(x,k);
cvput(x,i,sum/get_val(L,i,i));
}
for(i=n-1; i>=0; i--)
{
for(sum=cvget(x,i), k=i+1; k<n; k++) sum -= get_val(L,k,i)*cvget(x,k);
cvput(x,i,sum/get_val(L,i,i));
}
}
/*
* PSI1
* ----
*
*/
void psi1(matrix *PSI, int index1, matrix *index2, int i, matrix *y1)
{
int l,k, idx, hidden, N;
hidden = nof_rows(y1);
N = nof_cols(y1);
for(l=0; l<hidden; l++)
{
idx = index1+l;
for(k=0; k<N; k++)
put_val(PSI,idx,(int)cvget(index2,k)+i,get_val(y1,l,k));
}
}
/*
* PSI2
* ----
*
*/
void psi2(matrix *PSI, int index, matrix *index2, int i, double w, matrix *PHI)
{
int l, k, inputs, N, idx;
inputs = nof_rows(PHI);
N = nof_cols(PHI);
for(l=0; l<inputs; l++)
{
idx = index+l;
for(k=0; k<N; k++)
put_val(PSI,idx,(int)cvget(index2,k)+i,w*get_val(PHI,l,k));
}
}
/*
* PSI3
* ----
*
*/
void psi3(matrix *tmp3, matrix *tmp0, int j, double w)
{
int k, N;
N = nof_cols(tmp0);
for(k=0; k<N; k++) rvput(tmp3,k,get_val(tmp0,j,k)*w);
}
/*
* PSI4
* ----
*/
void psi4(matrix *PSI, int index, matrix *index2, int i, matrix *tmp, matrix *PHI)
{
int l, k, inputs, N, idx;
inputs = nof_rows(PHI);
N = nof_cols(PHI);
for(l=0; l<inputs; l++)
{
idx = index+l;
for(k=0; k<N; k++)
put_val(PSI,idx,(int)cvget(index2,k)+i,rvget(tmp,k)*get_val(PHI,l,k));
}
}
/*
* PSI5
* ----
*/
void psi5(matrix *PSI, int index, matrix *index2, int i, matrix *tmp3, matrix *tmp2,\
matrix *PHI)
{
int l, k, inputs, N, idx;
inputs = nof_rows(PHI);
N = nof_cols(PHI);
for(l=0; l<inputs; l++)
{
idx = index+l;
for(k=0; k<N; k++)
put_val(PSI,idx,(int)cvget(index2,k)+i,rvget(tmp3,k)*rvget(tmp2,k)\
*get_val(PHI,l,k));
}
}
/*
* VCOPYI
* ------
* vcopyi(A,ari,ac,B,bri,bc)
*
* From matrix B copy the column 'bc' and the rows specified by 'bri' to
* column 'ac' and the rows specified by 'ari' in matrix A.
*
* ARGUMENTS: A, B - Pointers to matrices (matrix*)
* ari, bri - Row index vectors (matrix*)
* aci, bci - Column indices (int)
*/
void vcopyi(matrix *A, matrix *ari, int ac, matrix *B, matrix *bri,int bc)
{
register int i,j, rows;
rows = ari->row;
for(i=0;i<rows;i++)
{
A->mat[(int)cvget(ari,i)][ac] = B->mat[(int)cvget(bri,i)][bc];
}
}
/*
* VTANH
* -----
* vtanh(A, ari, ac, B, bri, bc)
*
* Computes tanh to the entries in matrix B pointed out by the
* row index vector 'bri' and the column index 'bci', and put the
* result in matrix A (at the locations specified by ari and aci).
*
* ARGUMENTS: A, B - Pointers to matrices (matrix*)
* ari, bri - Row index vectors (matrix*)
* aci, bci - Column indices (int)
*
*/
void vtanh(matrix *A, matrix *ari, int ac, matrix *B, matrix *bri, int bc)
{
register int i,j, rows;
rows = ari->row;
for(i=0;i<rows;i++)
{
A->mat[(int)cvget(ari,i)][ac] = tanh(B->mat[(int)cvget(bri,i)][bc]);
}
}
/*
* MVMUL
* -----
* mmul(ptm1, ptm2, ptm3, col3)
*
* Matrix-vector multiplication: ptm1 = ptm2*ptm3(:,col3)
*
* Arguments: *ptm1 - Pointer to column vector (Not equal to *ptm1 or *ptm2)
* *ptm2 - Pointer to left matrix
* *ptm3 - Pointer to matrix from which the vector is extracted.
* col3 - Index to column of matrix ptm multiplied with ptm2
*
*/
void mvmul( matrix *ptm1, matrix *ptm2, matrix *ptm3, int col3 )
{
register int i,j,k;
for ( i=0; i < ptm2->row; i++ ){
ptm1->mat[i][0] = 0.0;
for ( k=0; k < ptm2->col; k++ ){
ptm1->mat[i][0] += ptm2->mat[i][k] * ptm3->mat[k][col3];
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -