/** \brief Counts the number of non-zero elements in a matrix along the specified row or column. The resulting value is returned in the return parameter: uint32 *count. */ merror mCountNonZero( const Mat *mat, int index, int dimension, uint32* count) { uint32 local_count; int i; if(mat == NULL || count == NULL) return mError(MAT_ERROR_NULL_PTR); local_count=0; switch(dimension){ case MAT_COL: if(index >= mat->cols) return mError(MAT_ERROR_COL_OUT_OF_BOUNDS); for(i = 0, local_count=0; i < mat->rows ; i++) if(mGet(mat,i,index) != 0) local_count++; break; case MAT_ROW: if(index >= mat->rows) return mError(MAT_ERROR_ROW_OUT_OF_BOUNDS); for(i = 0, local_count=0 ; i < mat->cols ; i++) if( mGet(mat,index,i) != 0 ) local_count++; break; } *count = local_count; return MAT_OK; }
/** The transposes matrix src into dst Matricies must be the same type and appropriatly sized. dst(i,j)=src(j,i) */ merror mTranspose( const Mat* src, Mat* dst ) { int i,j; const Mat *A = src; Mat *D = dst; // Error checking if(src == NULL || dst == NULL) return mError(MAT_ERROR_NULL_PTR); // Are the matrices the same type? if(A->type != D->type) { mZero(D); return mError(MAT_ERROR_MISMATCHED_TYPES); } // Are the matrices the same size? if(A->cols != D->cols || A->rows != D->rows) { mZero(D); return mError(MAT_ERROR_MISMATCHED_SIZES); } // Transpose the matrix for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,j,i, mGet(A,i,j)); return MAT_OK; }
void CFileBuffer::resort() { unsigned char * tmp = (unsigned char*) malloc(m_data_size); mGet(tmp, m_data_size, m_data_start); mPut(tmp, m_data_size, 0); m_data_start = 0; free(tmp); }
/** Scales all values of the matrix by the specified value */ merror mScale(Mat* mat, float scale) { int i,j; if(mat == NULL) return mError(MAT_ERROR_NULL_PTR); for(i = 0 ; i < mat->rows ; i++) for(j = 0 ; j < mat->cols ; j++) mSet(mat,i,j, scale * mGet(mat,i,j)); return MAT_OK; }
/** Returns trace of matrix. Summs the diagonal elements of the matrix. trace(mat) = sum over mat(i,i) */ float mTrace( const Mat *mat ) { float sum; int i; // Error Checking if(mat == NULL) return 0;//MAT_ERROR_NULL_PTR; for(i = 0, sum = 0 ; i < mat->rows && i < mat->cols ; i++) sum += mGet(mat,i,i); return sum; }
BOOPSI_DISPATCHER(IPTR, classdispatcher, cl, obj, msg) { mybug(0,("Class Dispatcher!\n")); switch (msg->MethodID) { case OM_NEW: return mNew(cl,obj,(APTR)msg); case OM_DISPOSE: return mDispose(cl,obj,(APTR)msg); case MUIM_Setup: return mSetup(cl,obj,(APTR)msg); case MUIM_Cleanup: return mCleanup(cl,obj,(APTR)msg); case OM_SET: return mSet(cl,obj,(APTR)msg); case OM_GET: return mGet(cl,obj,(APTR)msg); case MUIM_AskMinMax: return mAskMinMax(cl,obj,(APTR)msg); case MUIM_Hide: return mHide(cl,obj,(APTR)msg); case MUIM_Show: return mShow(cl,obj,(APTR)msg); case MUIM_Draw: return mDraw(cl,obj,(APTR)msg); // case MUIM_Action_HandlePsdEvents: // return mPsdEventHandler(cl,obj,(APTR)msg); case MUIM_Action_HandleTmrEvents: return mTmrEventHandler(cl,obj,(APTR)msg); default: return DoSuperMethodA(cl,obj,msg); } return 0; }
int CFileBuffer::remove(int size, BYTE *buf) { cs.Lock(); if (m_data_size < size) { cs.Unlock(); return -1; // not enough data, please retry } //memcpy(buf, m_the_buffer, size); //memmove(m_the_buffer, m_the_buffer+size, m_data_size-size); mGet(buf, size, m_data_start); m_data_size -= size; m_data_start += size; m_data_start %= m_buffer_size; cs.Unlock(); return 0; }
/** Finds global minimum and maximum value and location in a matrix This function returns the location and values of the max and min values in the matrix along the specified column or row. The values and locations are returned using the pointer parameters (min_val, max_val, min_loc, max_loc). All of these parameters need not be included. If a particular metric is not needed, just pass a NULL pointer in as that return paramter: \pre mMinMaxLoc( mat, 0, MAT_COL, &min, NULL, NULL, NULL); \endpre. The function basically finds the min / max values and locations, and then if the return paramters are not NULL, it assigns the determined values to those pointers. */ merror mMinMaxLoc( const Mat *mat, int index, int dimension, float *min_val, float *max_val, int *min_loc, int *max_loc) { float min,max,val; int i, minloc, maxloc; // Error checking if(mat == NULL) return mError(MAT_ERROR_NULL_PTR); // Initialization min = 0; max = 0; val = 0; i = 0; minloc = 0; maxloc = 0; // Search along the specified row or column switch(dimension) { case MAT_COL: if(index >= mat->cols) return mError(MAT_ERROR_COL_OUT_OF_BOUNDS); minloc = maxloc = 0; max = min = mGet(mat,0,index); for(i = 0; i < mat->rows ; i++) { val = mGet(mat,i,index); if(val > max) { max = val; maxloc = i; } if(val < min) { min = val; minloc = i; } } break; case MAT_ROW: if(index >= mat->rows) return mError(MAT_ERROR_ROW_OUT_OF_BOUNDS); minloc = maxloc = 0; max = min = mGet(mat,index,0); for(i = 0; i < mat->cols ; i++) { val = mGet(mat,index,i); if(val > max) { max = val; maxloc = i; } if(val < min) { min = val; minloc = i; } } break; } // If return parameters are not null, then assign them if(min_val) *min_val = min; if(max_val) *max_val = max; if(min_loc) *min_loc = minloc; if(max_loc) *max_loc = maxloc; return MAT_OK; }
/** Print out a matrix to the GUI or command line */ void mPrint(Mat* m, char* msg, int inv) { #ifndef SIMULATOR { int i,j; if(m == NULL || msg == NULL) return;// MAT_ERROR_NULL_PTR; Println(); if(msg != NULL) { Print(msg); if(m->cols > 1) Println(); } for(i=0 ; i < m->rows ; i++) { for(j=0 ; j < m->cols ; j++) { if(m->type == MAT_32F || m->type == MAT_64F) { PrintFloat(mGet(m,i,j)); Print("\t"); } else { PrintFloat(mGetu(m,i,j)); Print("\t"); } } if(m->cols > 1) Println(); } if(m->cols == 1) Println(); } #endif #ifdef SIMULATOR { int i,j,iend,jend; printf("\r\n"); iend = (inv == 0) ? m->rows : m->cols; jend = (inv == 0) ? m->cols : m->rows; if(msg != NULL) { printf(msg); if(jend > 1) printf("\r\n"); } for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { if(inv == 0) printf("%16.5f\t",mGet(m,i,j)); else printf("%16.5f\t",mGet(m,j,i)); } if(jend > 1) printf("\r\n"); } if(jend == 1) printf("\r\n"); } #endif }
/** Performs specified opperation on matrix data (element by element) This function implements several of the various element opperations that can be performed on two or just one matrix. For example, if you wanted to add two matricies together: \pre mElem(A, B, D, 0, 0, ELEM_ADD); \endpre All possible operations are listed in the mElementOpperation enumeration. */ merror mElem( const Mat* src1, const Mat* src2, Mat* dst, const float scaler, const uint32 bit_vector, int op) { int i,j; const Mat *A = src1; const Mat *B = src2; Mat *D = dst; if(B != NULL) /*B will be NULL if we are just doing a scaler opperation*/ { // Are ALL the matrices the same type? if(A->type != B->type || A->type != D->type ) { mZero(D); return mError(MAT_ERROR_MISMATCHED_TYPES); } // Are the matrices the same size? if( A->cols != B->cols || A->cols != D->cols || A->rows != B->rows || A->rows != D->rows ) { mZero(D); return mError(MAT_ERROR_MISMATCHED_SIZES); } } else { // Are ALL the matrices the same type? if(A->type != D->type ) { mZero(D); return mError(MAT_ERROR_MISMATCHED_TYPES); } // Are the matrices the same size? if( A->cols != D->cols || A->rows != D->rows ) { mZero(D); return mError(MAT_ERROR_MISMATCHED_SIZES); } } switch(op){ case ELEM_ADD: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) + mGet(B,i,j)); break; case ELEM_ADDS: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) + scaler); break; case ELEM_SUB: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) - mGet(B,i,j)); break; case ELEM_SUBS: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) - scaler); break; case ELEM_MUL: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) * mGet(B,i,j)); break; case ELEM_MULS: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) * scaler); break; case ELEM_DIV: for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) / mGet(B,i,j)); break; case ELEM_DIVS: if(scaler == 0) { mZero(D); return mError(MAT_ERROR_DIVIDE_BY_0); } for(i = 0 ; i < A->rows ; i++) for(j = 0 ; j < A->cols ; j++) mSet(D,i,j, mGet(A,i,j) / scaler); break; } return MAT_OK; }
/** Performs generalized matrix multiplication The flags indicate if any of the three matricies should be transposed. - Flags: - MUL_A_T = Transpose source matrix A - MUL_B_T = Transpose source matrix B - MUL_D_T = Transpose destination matrix D \pre dst = alpha*src1T*src2 \endpre */ merror mGEMM(Mat* src1, Mat* src2, float alpha, Mat* dst , uint32 flags ) { int i, j, k, iend, jend, kend; float sum; Mat *A = src1; Mat *B = src2; Mat *D = dst; // Are ALL the matrices the same type? if(A->type != B->type || A->type != D->type ) { mZero(D); return mError(MAT_ERROR_MISMATCHED_TYPES); } // Are A and B multiliable? { int A_dimension = (flags & MUL_A_T) ? A->rows : A->cols; int B_dimension = (flags & MUL_B_T) ? B->cols : B->rows; if(A_dimension != B_dimension) { mZero(D); return mError(MAT_MUL_ERROR_MISMATCHED_SIZE_AB); } } //Does the destination have the corerct number of rows? { int A_dimension = (flags & MUL_A_T) ? A->cols : A->rows; int D_dimension = (flags & MUL_D_T) ? D->cols : D->rows; if(A_dimension != D_dimension) { mZero(D); return mError(MAT_MUL_ERROR_MISMATCHED_ROWS_D); } } //Does the destination have the corerct number of cols? { int B_dimension = (flags & MUL_B_T) ? B->rows : B->cols; int D_dimension = (flags & MUL_D_T) ? D->rows : D->cols; if(B_dimension != D_dimension) { mZero(D); return mError(MAT_MUL_ERROR_MISMATCHED_COLS_D); } } iend = (flags & MUL_B_T) ? B->rows : B->cols; jend = (flags & MUL_A_T) ? A->cols : A->rows; kend = (flags & MUL_A_T) ? A->rows : A->cols; switch(flags){ case 0: /* No transposes. Straight multiply * indexes: * i = bcol, dcol * j = arow, drow * k = acol, brow */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,j,k) * mGet(B,k,i); mSet(D,j,i,alpha*sum); } } break; case MUL_A_T: /* Transpose: A * indexes: * i = bcol, dcol * j = acol, drow * k = arow, brow */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,k,j) * mGet(B,k,i); mSet(D,j,i,alpha*sum); } } break; case MUL_B_T: /* Transpose: B * indexes: * i = brow, dcol * j = arow, drow * k = acol, bcol */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,j,k) * mGet(B,i,k); mSet(D,j,i,alpha*sum); } } break; case MUL_B_T | MUL_A_T: /* Transpose: A,B * indexes: * i = brow, dcol * j = acol, drow * k = arow, bcol */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,k,j) * mGet(B,i,k); mSet(D,j,i,alpha*sum); } } break; case MUL_D_T: /* Transpose: D * indexes: * i = bcol, drow * j = arow, dcol * k = acol, brow */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,j,k) * mGet(B,k,i); mSet(D,i,j,alpha*sum); } } break; case MUL_D_T | MUL_A_T: /* Transpose: D,A * indexes: * i = bcol, drow * j = acol, dcol * k = arow, brow */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,k,j) * mGet(B,k,i); mSet(D,i,j,alpha*sum); } } break; case MUL_D_T | MUL_B_T: /* Transpose: D,B * indexes: * i = brow, drow * j = arow, dcol * k = acol, bcol */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,j,k) * mGet(B,i,k); mSet(D,i,j,alpha*sum); } } break; case MUL_D_T | MUL_B_T | MUL_A_T: /* Transpose: D,B,A * indexes: * i = brow, drow * j = acol, dcol * k = arow, bcol */ for(i = 0 ; i < iend ; i++) { for(j = 0 ; j < jend ; j++) { for(k = 0, sum = 0 ; k < kend ; k++) sum += mGet(A,k,j) * mGet(B,i,k); mSet(D,i,j,alpha*sum); } } break; } return MAT_OK; }