NumericMatrix calc_ALK(NumericMatrix x) { NumericVector sums = rowSums(x); NumericMatrix alk(x.nrow(), x.ncol()); for (int r = 0; r < x.nrow(); ++r) { if(sums[r] == 0) sums[r] = 1; alk(r, _) = x(r, _) / sums[r]; } return alk; }
void ImageSegmenter::SplitIntoLines(const Mat& img) { Mat processImg, dilated_img; if (m_do_dilation) { Mat element = GetLineElement(); dilated_img = img.clone(); dilate(img, dilated_img, element); //imshow("before",img); //imshow("after", dilated_img); //waitKey(); processImg = dilated_img; } else processImg = img; // Form row histogram of sum-across-columns vector<unsigned int> rowSums(processImg.rows,0); unsigned int rowSum =0; for(unsigned int row = 0; row < processImg.rows; ++row) { rowSum =0; for(unsigned int col = 0; col < processImg.cols; ++col) rowSum += processImg.at<unsigned char>(row,col); rowSums.at(row) = rowSum/255; //cout << "Row = " << row << " RowSum = " << rowSums.at(row) << endl; } // If an entry in the row histogram exceeds a threshold, // it is considered a white line. Otherwise, the entry // implies it is part of a text line in the document. We scan the row // histogram from top to bottom looking for transitions from white line // to text and vice-versa. unsigned int white_row_threshold = static_cast<unsigned int>(static_cast<double>(processImg.cols) * m_white_row_fill_factor); unsigned int lineTop=0,lineBottom=0; for(unsigned int i = 1 ; i < rowSums.size() ;i++) { //cout << "Rowsums: prev[" << rowSums.at(i-1) << "] curr[" << rowSums.at(i) << "]" << endl; if ( rowSums.at(i-1) >= white_row_threshold && rowSums.at(i) <= white_row_threshold ) lineTop = i; else if ( rowSums.at(i-1) <= white_row_threshold && rowSums.at(i) >= white_row_threshold ) { lineBottom = i; pair<unsigned int,unsigned int> lineB; lineB.first = lineTop; lineB.second = lineBottom; m_lineBoundaries.push_back(lineB); //cout << "Line: " << lineTop << " " << lineBottom << endl; } } }
//---------------------------------------------------------------------------- //! Compute maximal row sum (infinity-norm) of a matrix in coordinate format. double infNorm() { // sort entries to make sure if ( not sorted_ ) { this -> finishAssembly( ); } unsigned nr = this -> deduceNumRows_( ); std::vector<double> rowSums( nr, 0.0 ); typename MatrixCooVec_::const_iterator iter = matrixCooVec_.begin(); typename MatrixCooVec_::const_iterator last = matrixCooVec_.end(); for ( ; iter != last; ++iter ) { const IndexType iRow = (*iter).first.first; rowSums[ iRow ] += std::fabs( (*iter).second ); } return *std::max_element( rowSums.begin(), rowSums.end() ); }
void Trilinos_Util_GenerateVbrProblem(int nx, int ny, int npoints, int * xoff, int * yoff, int nsizes, int * sizes, int nrhs, const Epetra_Comm &comm, Epetra_BlockMap *& map, Epetra_VbrMatrix *& A, Epetra_MultiVector *& x, Epetra_MultiVector *& b, Epetra_MultiVector *&xexact) { int i, j; // Number of global equations is nx*ny. These will be distributed in a linear fashion int numGlobalEquations = nx*ny; Epetra_Map ptMap(numGlobalEquations, 0, comm); // Create map with equal distribution of equations. int numMyElements = ptMap.NumMyElements(); Epetra_IntVector elementSizes(ptMap); // This vector will have the list of element sizes for (i=0; i<numMyElements; i++) elementSizes[i] = sizes[ptMap.GID64(i)%nsizes]; // cycle through sizes array map = new Epetra_BlockMap(-1, numMyElements, ptMap.MyGlobalElements(), elementSizes.Values(), ptMap.IndexBase(), ptMap.Comm()); A = new Epetra_VbrMatrix(Copy, *map, 0); // Construct matrix int * indices = new int[npoints]; // double * values = new double[npoints]; // double dnpoints = (double) npoints; // This section of code creates a vector of random values that will be used to create // light-weight dense matrices to pass into the VbrMatrix construction process. int maxElementSize = 0; for (i=0; i< nsizes; i++) maxElementSize = EPETRA_MAX(maxElementSize, sizes[i]); Epetra_LocalMap lmap(maxElementSize*maxElementSize, ptMap.IndexBase(), ptMap.Comm()); Epetra_Vector randvec(lmap); randvec.Random(); randvec.Scale(-1.0); // Make value negative for (i=0; i<numMyElements; i++) { int rowID = map->GID(i); int numIndices = 0; int rowDim = sizes[rowID%nsizes]; for (j=0; j<npoints; j++) { int colID = rowID + xoff[j] + nx*yoff[j]; // Compute column ID based on stencil offsets if (colID>-1 && colID<numGlobalEquations) indices[numIndices++] = colID; } A->BeginInsertGlobalValues(rowID, numIndices, indices); for (j=0; j < numIndices; j++) { int colDim = sizes[indices[j]%nsizes]; A->SubmitBlockEntry(&(randvec[0]), rowDim, rowDim, colDim); } A->EndSubmitEntries(); } delete [] indices; A->FillComplete(); // Compute the InvRowSums of the matrix rows Epetra_Vector invRowSums(A->RowMap()); Epetra_Vector rowSums(A->RowMap()); A->InvRowSums(invRowSums); rowSums.Reciprocal(invRowSums); // Jam the row sum values into the diagonal of the Vbr matrix (to make it diag dominant) int numBlockDiagonalEntries; int * rowColDims; int * diagoffsets = map->FirstPointInElementList(); A->BeginExtractBlockDiagonalView(numBlockDiagonalEntries, rowColDims); for (i=0; i< numBlockDiagonalEntries; i++) { double * diagVals; int diagLDA; A->ExtractBlockDiagonalEntryView(diagVals, diagLDA); int rowDim = map->ElementSize(i); for (j=0; j<rowDim; j++) diagVals[j+j*diagLDA] = rowSums[diagoffsets[i]+j]; } if (nrhs<=1) { x = new Epetra_Vector(*map); b = new Epetra_Vector(*map); xexact = new Epetra_Vector(*map); } else { x = new Epetra_MultiVector(*map, nrhs); b = new Epetra_MultiVector(*map, nrhs); xexact = new Epetra_MultiVector(*map, nrhs); } xexact->Random(); // Fill xexact with random values A->Multiply(false, *xexact, *b); return; }
void GenerateVbrProblem(int numNodesX, int numNodesY, int numProcsX, int numProcsY, int numPoints, int * xoff, int * yoff, int nsizes, int * sizes, int nrhs, const Epetra_Comm &comm, bool verbose, bool summary, Epetra_BlockMap *& map, Epetra_VbrMatrix *& A, Epetra_MultiVector *& b, Epetra_MultiVector *& bt, Epetra_MultiVector *&xexact, bool StaticProfile, bool MakeLocalOnly) { int i; // Determine my global IDs long long * myGlobalElements; GenerateMyGlobalElements(numNodesX, numNodesY, numProcsX, numProcsY, comm.MyPID(), myGlobalElements); int numMyElements = numNodesX*numNodesY; Epetra_Map ptMap((long long)-1, numMyElements, myGlobalElements, 0, comm); // Create map with 2D block partitioning. delete [] myGlobalElements; Epetra_IntVector elementSizes(ptMap); // This vector will have the list of element sizes for (i=0; i<numMyElements; i++) elementSizes[i] = sizes[ptMap.GID64(i)%nsizes]; // cycle through sizes array map = new Epetra_BlockMap((long long)-1, numMyElements, ptMap.MyGlobalElements64(), elementSizes.Values(), ptMap.IndexBase64(), ptMap.Comm()); int profile = 0; if (StaticProfile) profile = numPoints; // FIXME: Won't compile until Epetra_VbrMatrix is modified. #if 0 int j; long long numGlobalEquations = ptMap.NumGlobalElements64(); if (MakeLocalOnly) A = new Epetra_VbrMatrix(Copy, *map, *map, profile); // Construct matrix rowmap=colmap else A = new Epetra_VbrMatrix(Copy, *map, profile); // Construct matrix long long * indices = new long long[numPoints]; // This section of code creates a vector of random values that will be used to create // light-weight dense matrices to pass into the VbrMatrix construction process. int maxElementSize = 0; for (i=0; i< nsizes; i++) maxElementSize = EPETRA_MAX(maxElementSize, sizes[i]); Epetra_LocalMap lmap((long long)maxElementSize*maxElementSize, ptMap.IndexBase(), ptMap.Comm()); Epetra_Vector randvec(lmap); randvec.Random(); randvec.Scale(-1.0); // Make value negative int nx = numNodesX*numProcsX; for (i=0; i<numMyElements; i++) { long long rowID = map->GID64(i); int numIndices = 0; int rowDim = sizes[rowID%nsizes]; for (j=0; j<numPoints; j++) { long long colID = rowID + xoff[j] + nx*yoff[j]; // Compute column ID based on stencil offsets if (colID>-1 && colID<numGlobalEquations) indices[numIndices++] = colID; } A->BeginInsertGlobalValues(rowID, numIndices, indices); for (j=0; j < numIndices; j++) { int colDim = sizes[indices[j]%nsizes]; A->SubmitBlockEntry(&(randvec[0]), rowDim, rowDim, colDim); } A->EndSubmitEntries(); } delete [] indices; A->FillComplete(); // Compute the InvRowSums of the matrix rows Epetra_Vector invRowSums(A->RowMap()); Epetra_Vector rowSums(A->RowMap()); A->InvRowSums(invRowSums); rowSums.Reciprocal(invRowSums); // Jam the row sum values into the diagonal of the Vbr matrix (to make it diag dominant) int numBlockDiagonalEntries; int * rowColDims; int * diagoffsets = map->FirstPointInElementList(); A->BeginExtractBlockDiagonalView(numBlockDiagonalEntries, rowColDims); for (i=0; i< numBlockDiagonalEntries; i++) { double * diagVals; int diagLDA; A->ExtractBlockDiagonalEntryView(diagVals, diagLDA); int rowDim = map->ElementSize(i); for (j=0; j<rowDim; j++) diagVals[j+j*diagLDA] = rowSums[diagoffsets[i]+j]; } if (nrhs<=1) { b = new Epetra_Vector(*map); bt = new Epetra_Vector(*map); xexact = new Epetra_Vector(*map); } else { b = new Epetra_MultiVector(*map, nrhs); bt = new Epetra_MultiVector(*map, nrhs); xexact = new Epetra_MultiVector(*map, nrhs); } xexact->Random(); // Fill xexact with random values A->Multiply(false, *xexact, *b); A->Multiply(true, *xexact, *bt); #endif // EPETRA_NO_32BIT_GLOBAL_INDICES return; }
// [[Rcpp::export]] RcppExport SEXP getMLE(SEXP mtx, SEXP steps, SEXP epsilon){ NumericMatrix mt(mtx); const NumericVector st(steps); const NumericVector eps(epsilon); const size_t stps=st[0]; const double epsil=eps[0]; const NumericVector * gprimePrior=E(mt); NumericVector * gprime= new NumericVector(*gprimePrior); delete gprimePrior; NumericMatrix track(stps,2); fillInMatrix(track,0); double gDist(sumDist(mt,*gprime)); double deltaG=0; R_len_t counter=0; while ((counter < stps) && (deltaG > epsil)){ track(counter,0)=gDist; track(counter,1)=deltaG; NumericMatrix * devisable = new NumericMatrix(mt.nrow(),mt.ncol()); for(int i=0;i<mt.nrow();i++){ NumericVector row=mt(i,_); const NumericVector * ddr = diffDistRatio(row,*gprime); (*devisable)(i,_)=*ddr; delete ddr; } const NumericVector * rs=rowSums(*devisable); delete devisable; NumericMatrix * devider = new NumericMatrix(mt.nrow(),mt.ncol()); for(int i=0;i<mt.nrow();i++){ NumericVector row=mt(i,_); const NumericVector * ddr = diffDistInverse(row,*gprime); (*devisable)(i,_)=*ddr; delete ddr; } const double suDiv=sumMatrix(*devider); delete devider; for(int i=0;i<gprime->size();i++){ (*gprime)[i]=(*rs)[i]/suDiv; } delete rs; gDist=double(sumDist(mt,*gprime)); deltaG=fabs(track(counter,0)-gDist); counter++; } List ret; ret.push_back(*gprime); delete gprime; ret.push_back(track); return ret; }