void moveTwoStrings(int size, double mat[size][size]) { int i,j; double tmp; int indexMax = 0; int indexMin = 0; double sum = 0; double sumMax = -sumElements(size, mat); double sumMin = sumElements(size, mat); for(i = 0; i < size; i++) { for(j = 0; j < size; j++) { sum += mat[i][j]; } if(sum > sumMax) { sumMax = sum; indexMax = i; } if(sum < sumMin) { sumMin = sum; indexMin = i; } } for(j = 0; j < size; j++) { tmp = mat[indexMin][j]; mat[indexMin][j] = mat[indexMax][j]; mat[indexMax][j] = tmp; } }
WHcoord fileManager::meanCoordFromMask() const { if (m_maskMatrix.empty()) { std::cerr<< "ERROR @ fileManager::storeFullTract(): Mask hast not been loaded, returning 0 coordinate"<<std::endl; return WHcoord(); } size_t sumX( 0 ), sumY( 0 ), sumZ( 0 ), sumElements( 0 ); for( int i=0 ; i<m_maskMatrix.size() ; ++i ) { for( int j=0 ; j< m_maskMatrix[i].size() ; ++j ) { for( int k=0 ; k<m_maskMatrix[i][j].size() ; ++k ) { if (m_maskMatrix[i][j][k]) { sumX += i; sumY += j; sumZ += k; ++sumElements; } } } } size_t meanX( sumX/sumElements ); size_t meanY( sumY/sumElements ); size_t meanZ( sumZ/sumElements ); WHcoord meanCoord(meanX,meanY,meanZ); return meanCoord; } // end "meanCoordFromMask()" -----------------------------------------------------------------
void LinearRegressionAnalyzer::DoAnalyze() { BaseAnalyzer::DoAnalyze(); QList<double> yList = matrix[0]; QList<QList<double> > xMatrix; for (int i=1;i<matrix.size();i++) { xMatrix.append(matrix[i]); } QList<QList<double> > xNMatrix; for (int i=0;i<xMatrix.size();i++) { QList<double> xNiMatrix; double avg = average(xMatrix[i]); QList<double> razn; for (int j=0;j<xMatrix[i].size();j++) { razn.append(fabs(xMatrix[i][j] - avg)); } double R = maxElement(razn); for (int j=0;j<xMatrix[i].size();j++) xNiMatrix.append((xMatrix[i][j] - avg)/R); xNMatrix.append(xNiMatrix); } QList<double> bMatrix; double temp = 0.0; for (int i=0;i<yList.size();i++) temp += yList[i]; bMatrix.append(temp); for (int i=0;i<xNMatrix.size();i++) { temp = 0.0; for (int j=0;j<xNMatrix[i].size();j++) { temp += yList[j] * xNMatrix[i][j]; } bMatrix.append(temp); } QList<QList<double> > aMatrix; QList<double> firstRow; firstRow.append(yList.size()); for (int i=0;i<xNMatrix.size();i++) firstRow.append(sumElements(xNMatrix[i])); aMatrix.append(firstRow); for (int row=0;row<xNMatrix.size();row++) { QList<double> tempRow; for (int col=0;col<bMatrix.size();col++) { if (col<=row) { tempRow.append(0.0); } else { if (col>0) { tempRow.append(sumListsElements(xNMatrix[col-1], xNMatrix[row])); } } } aMatrix.append(tempRow); } for (int row = 0;row<aMatrix.size();row++) { for (int col = 0; col < aMatrix[row].size();col++) { if (row <= col) continue; aMatrix[row][col] = aMatrix[col][row]; } } results = Gauss(aMatrix, bMatrix); QList<double> y2List; for (int i=0;i<xNMatrix[0].size();i++) { temp = 0.0; for (int j= 0;j<results.size();j++) { if (j<results.size()-1) temp += results[j] * xNMatrix[j][i]; else temp += results[j]; } y2List.append(temp); } double y2Avg = average(y2List); double yAvg = average(yList); double RUp = 0.0, RDown = 0.0; for (int i=0;i<yList.size();i++) { RUp += (y2List[i]-y2Avg)*(y2List[i]-y2Avg); RDown += (yList[i] - yAvg)*(yList[i] - yAvg); } R2 = RUp/RDown; }
unsigned int simulator::drawEventType() { /// Determine which event type occurs /// (after an event time has been drawn) vector<double> x; // sufficiently small such that probability to fall // b/w x and x+tiny is pratically 0 but large enough // that a 'double comparison' recognizes it // (used when no indiv in a given status) double tiny = 1E-10; // Anchor at 0 x.push_back(0.0); // New infection event double tmp = at_least_one_S_and_I()?_beta*_count_I*_count_S/_popSize:tiny; x.push_back(tmp); // Migrations through the E[k] for(int i=0; i<_nE; i++) { double tmp = (census_status(i+1)>0)?_sigma[i]*census_status(i+1):tiny; x.push_back(tmp); } // Migrations through the I[k] for(int i=0; i<_nI; i++) { double tmp = (census_status(_nE+1+i)>0)?_gamma[i]*census_status(_nE+1+i):tiny; x.push_back(tmp); } // draw a uniform on sum of all rates and // see where it lands -> that's the drawn event type double u = uniform01()*sumElements(x); // cout<<"drawEventType: "<<u<<" :: " <<sumElements(x)<<endl; // vector of cumulative sum of x elements vector<double> cumx; for(int i=0; i<x.size(); i++) { // initialize sum at 0 cumx.push_back(0.0); // Calculate cumulative value for(int j=0; j<=i; j++) cumx[i]+=x[j]; } unsigned int idx = 0; bool found = false; for(int i=0; i<cumx.size(); i++) { if(cumx[i]<u) { idx = i; found = true; } } stopif(!found, "can't find index"); // Make sure there is an individual // in the drawn event (they may be very close, see 'tiny') if (census_status(idx)==0) { unsigned int idx2 = idx; while (census_status(idx2)==0) idx2=idx2-1; idx=idx2; } return idx; }
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// /// calcMean //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// double calcMean( const RealVector& x ) { return sumElements( x ) / x.size(); }
float getAverage(int nelem_in_array, int *array) { sumElements(nelem_in_array, array); return (float)sum/nelem_in_array; }