//Partition the array into two halves and return the //index about which the array is partitioned int partition(int rows, int cols, int colToSortOn, double * array, int left, int right) { //Makes the leftmost element a good pivot, //specifically the median of medians findMedianOfMedians(rows, cols, colToSortOn, array, left, right); int pivotIndex = left, index = left, i; double * colToSort = array+colToSortOn*rows, pivotValue = colToSort[pivotIndex]; swapRows(rows, cols, array, pivotIndex, right); for(i = left; i < right; i++) { if(colToSort[i] < pivotValue) { swapRows(rows, cols, array, i, index); index += 1; } } swapRows(rows, cols, array, right, index); return index; }
void echelonV2(Matlab& in, Matlab& coVector) { assert(in.rows() == in.columns()); Matlab mat = in; #ifdef DEBUG int nstage = 0; #endif for(int k = 0; k < mat.columns(); k++){ in = mat; /* Division only occurs at k ([k,k]) (column) changes. These changes occur at the diagonals @ ([k,k]) which make life easy This way, we prevent Zero Division */ if(mat[k][k] == 0 /*Checking for out of bound also*/ && (k + 1) < mat.columns() ){ swapRows(mat, k, k + 1); //swap the defective row with immediate row swapRows(coVector, k, k + 1); } for(int i = 0; i < mat.columns(); i++) mat[k][i] = mat[k][i] / in[k][k]; for(int i = 0; i < 1; i++) coVector[k][i] = coVector[k][i] / in[k][k]; if(k == mat.columns()) return; in = mat; for(int row = k + 1; row < mat.rows(); row++){ for (int col = 0; col < mat.columns(); ++col) mat[row][col] = mat[row][col] - in[row][k] * mat[k][col];// * (mat[k][col] / mat[k][k]); for(int col = 0; col < 1; col++) coVector[row][col] = coVector[row][col] - in[row][k] * coVector[k][col];// * (coVector[k][col] / mat[k][k]); #ifdef DEBUG cerr << "Stage: " << ++nstage << endl << "=========" << endl; cerr << "K: " << k << endl; cerr << endl << "Unknowns" << endl; mat.print(); cerr << "Constants" << endl; coVector.print(); #endif } #ifdef DEBUG cerr << "==============" << endl; #endif } }
void gaussian(float *a, float *b, int n) { float *c; c = (float*) calloc(n, sizeof(float)); if (c == NULL) { printf("c memory allocation error"); } for (int i = 0; i < n; i++) { float c1 = 0; for (int j = 0; j < n; j++) { float c0 = fabsf(a[ind(i, j, n)]); if (c0 > c1) { c1 = c0; } c[i] = c1; } } for (int i = 0; i < n; i++) { printf("%f ", c[i]); printf("\n"); } int k = 0; for (int j = 0; j < n - 1; j++) { float pi1 = 0; for (int i = j; i < n; i++) { float pi0 = fabsf(a[ind(i, j, n)]); pi0 /= c[i]; printf("pi0 = %f\n", pi0); printf("pi1 = %f\n", pi1); if (pi0 > pi1) { pi1 = pi0; k = i; } } printf("pi1 = %f\n", pi1); printf("k = %d\n", k); swapRows(a, j, k, n); swapRows(b, j, k, n); double temp = c[j]; c[j] = c[k]; c[k] = temp; for (int i = j + 1; i < n; i++) { double pj = a[ind(i, j, n)] / a[ind(j, j, n)]; a[ind(i, j, n)] = pj; for (int l = j + 1; l < n; l++) { a[ind(i, l, n)] -= pj * a[ind(j, l, n)]; } } } }
void reverse(Row *rows, const int size) { int i, j; for (i = 0, j = size - 1; i < j; i++, j--) swapRows(rows, i, j); }
void ScheduleDialog::moveRowDown() { int i = timingTable->currentRow(); if (i < timingTable->numRows() - 1) { swapRows(i, i + 1); } }
void RulesModel::moveRowDown(int row) { if(row+1>=stateList.count())return; if(!isConcurrentMode&&stateList.at(row)==1&&stateList.at(row+1)==2) { setRuleStateByRow(row,0); stateList[row]=2; swapRows(row,row+1); setRuleStateByRow(row,0); setRuleStateByRow(row,1); } else swapRows(row,row+1); emit dataChanged(index(row,0),index(row+1,columnsCount-1)); }
void PLUDecomposite(PMatrix *P) { for (int k = 1; k <= n()-1; ++k) { int m = k; for (int i = k+1; i <= n(); ++i) { if (fabs(M(i, k)) > fabs(M(m, k))) { m = i; } } if (fabs(M(m, k) + 0.0) < 1e-15) { _reg = false; return; } if (m != k) { swapRows(m, k); P -> swapRows(m, k); } for (int i = k+1; i <= n(); ++i) { setM(i, k, M(i, k) / M(k, k)); for (int j = k+1; j <= n(); ++j) { setM(i, j, M(i, j) - M(i, k) * M (k, j)); } } } _reg = fabs(M(n(), n())) >= 1e-15; }
void ScheduleDialog::moveRowUp() { int i = timingTable->currentRow(); if (i > 0) { swapRows(i, i - 1); } }
//Find the index of the Median of the elements //of array that occur at every "shift" positions. int findMedianIndex(int rows, int cols, int colToSortOn, double * array, int left, int right, int shift) { int i, groups = (right - left)/shift + 1, k = left + groups/2*shift; double * colToSort = array+colToSortOn*rows; for(i = left; i <= k; i += shift) { int minRow = i; double minValue = colToSort[minRow]; for(int j = i; j <= right; j +=shift) { if(colToSort[j] < minValue) { minRow = j; minValue = colToSort[minRow]; } } swapRows(rows, cols, array, i, minRow); } return k; }
void TToolbarEditor::onUpButtonClicked() { int row = active_actions_table->currentRow(); if (row > 0) { swapRows(row - 1, row); setCurrentRow(row - 1); } }
void TToolbarEditor::onDownButtonClicked() { int row = active_actions_table->currentRow(); if (row >= 0 && row < active_actions_table->rowCount() - 1) { swapRows(row, row + 1); setCurrentRow(row + 1); } }
void RulesModel::moveRowUp(int row) { if(row-1<0)return; if(!isConcurrentMode&&stateList.at(row-1)==1&&stateList.at(row)==2) { setRuleStateByRow(row-1,0); stateList[row-1]=2; swapRows(row,row-1); setRuleStateByRow(row-1,0); setRuleStateByRow(row-1,1); } else swapRows(row,row-1); emit dataChanged(index(row-1,0),index(row,columnsCount-1)); }
Matrix4x3 invertMatrix(const Matrix4x3 &m) { double w[3][8]; for (int r = 0; r != 3; ++r) { for (int c = 0; c != 4; ++c) { w[r][c] = m(r, c); w[r][c+4] = (r == c) ? 1.0 : 0.0; } } if (w[0][0] == 0.0) { if (w[1][0] != 0.0) swapRows(w[0], w[1]); else swapRows(w[0], w[2]); } divideRow(w[0], w[0][0]); subtractRow(w[1], w[1][0], w[0]); subtractRow(w[2], w[2][0], w[0]); if (w[1][1] == 0.0) swapRows(w[1], w[2]); divideRow(w[1], w[1][1]); subtractRow(w[0], w[0][1], w[1]); subtractRow(w[2], w[2][1], w[1]); divideRow(w[2], w[2][2]); subtractRow(w[0], w[0][2], w[2]); subtractRow(w[1], w[1][2], w[2]); for (int r = 0; r != 3; ++r) { w[r][7] = -w[r][3]; w[r][3] = 0.0; } Matrix4x3 result; for (int r = 0; r != 3; ++r) for (int c = 0; c != 4; ++c) result(r, c) = float(w[r][c+4]); return result; }
//EFFECTS: Outputs a Sudoku Puzzle with "count" values filled in void generateSudokuPuzzle(int count) { char sudokustring[] = "123567894456189237789234156214356789365798412897412365532641978648973521971825643"; int usedArray[9][9]; int countArray[9] = {0,0,0,0,0,0,0,0,0} ; int i, j, cursor1, cursor2, counter; int **sudokuarray; memset(usedArray, 0, 9 * sizeof(usedArray[0])); sudokuarray = decodeSudokuString(sudokustring); if (VERBOSE) printSudokuPuzzle(sudokuarray); for (i = 0; i < (rand() % count); i ++) { if (VERBOSE) printf("Swapping rows.\n"); swapRows(&sudokuarray); if (VERBOSE) printSudokuPuzzle(sudokuarray); if (VERBOSE) printf("Rotating array.\n"); sudokuarray = rotateArray(sudokuarray); if (VERBOSE) printSudokuPuzzle(sudokuarray); } for(i=0; i< (81-count); i++) { do { cursor1 = rand() % 9; cursor2 = rand() % 9; } while (usedArray[cursor1][cursor2]); usedArray[cursor1][cursor2] = 1; if (countArray[sudokuarray[cursor1][cursor2]-1] >= 8) { if (VERBOSE) printf("%d has already been taken away 8 times\n", sudokuarray[cursor1][cursor2]); i--; } else { countArray[sudokuarray[cursor1][cursor2]-1] = countArray[sudokuarray[cursor1][cursor2]-1] + 1; if (VERBOSE) printf("%d has already been taken away %d times\n", sudokuarray[cursor1][cursor2], countArray[sudokuarray[cursor1][cursor2]-1]); sudokuarray[cursor1][cursor2]=0; } } printSudokuPuzzle(sudokuarray); for (i = 0, counter = 0; i < 9; i++) for (j = 0; j < 9; j++, counter++) { sudokustring[counter] = (char) (((int) '0') + sudokuarray[i][j]); } printf("Testing if sudoku puzzle is correct\n"); printf("Result: %s\n", (testSudokuString(sudokustring) == 1) ? "Correct" : "Incorrect" ); for(i = 0; i < 9; i++) free(sudokuarray[i]); free(sudokuarray); }
void scramble(Row *rows, const int size) { int i, j, z; srand((unsigned int)time(0)); for (z = 0; z < size; z++) { i = randomAB(0, size - 1); j = randomAB(0, size - 1); swapRows(rows, i, j); } }
void GaussianMatrix::makeGaussian2() { int max_index; for( int i=0; i<getRowsNb(); ++i ) { max_index = findMaxRow( i ); if( max_index != i ) { swapRows( max_index, i ); } multiply( i ); subtractRow(i); } makeDiagonalOnes(); }
bool SetsModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int /*column*/, const QModelIndex &parent) { if (action != Qt::MoveAction) return false; if (row == -1) { if (!parent.isValid()) return false; row = parent.row(); } int oldRow = qobject_cast<const SetsMimeData *>(data)->getOldRow(); if (oldRow < row) row--; swapRows(oldRow, row); return true; }
void QgsAttributeActionDialog::moveDown() { // Swap the selected row with the one below int row1 = -1, row2 = -1; QList<QTableWidgetItem *> selection = attributeActionTable->selectedItems(); if ( !selection.isEmpty() ) { row1 = selection.first()->row(); } if ( row1 < attributeActionTable->rowCount() - 1 ) row2 = row1 + 1; if ( row1 != -1 && row2 != -1 ) { swapRows( row1, row2 ); // Move the selection to follow attributeActionTable->selectRow( row2 ); } }
//Computes the median of each group of 5 elements and stores //it as the first element of the group. Recursively does this //till there is only one group and hence only one Median double findMedianOfMedians(int rows, int cols, int colToSortOn, double * array, int left, int right) { double * colToSort = array+colToSortOn*rows; if(left == right) return colToSort[left]; int i, shift = 1; while(shift <= (right - left)) { for(i = left; i <= right; i+=shift*5) { int endIndex = (i + shift*5 - 1 < right) ? i + shift*5 - 1 : right; int medianIndex = findMedianIndex(rows, cols, colToSortOn, array, i, endIndex, shift); swapRows(rows, cols, array, i, medianIndex); } shift *= 5; } return colToSort[left]; }
void QgsAttributeActionDialog::moveUp() { // Swap the selected row with the one above int row1 = -1, row2 = -1; QList<QTableWidgetItem *> selection = mAttributeActionTable->selectedItems(); if ( !selection.isEmpty() ) { row1 = selection.first()->row(); } if ( row1 > 0 ) row2 = row1 - 1; if ( row1 != -1 && row2 != -1 ) { swapRows( row1, row2 ); // Move the selection to follow mAttributeActionTable->selectRow( row2 ); } }
/** * Changes this matrix into its \c ref. * * \internal * * \return True on success. */ bool RMatrix::ref(int startRow) { int pr, pc; // row which has elements most left becomes first row if ((pr = getPivotRow(startRow)) == -1) { return false; } swapRows(startRow, pr); // where is the pivot element in the 1st row? if ((pc = getPivotCol(startRow)) == -1) { return false; } multiplyRow(startRow, 1.0 / m[startRow][pc]); for (int rc = startRow + 1; rc < rows; ++rc) { addRow(rc, -m[rc][pc], startRow); } if (startRow < rows) { ref(startRow + 1); } return true; }
/* gaussReduce: Apply Gaussian elimination to a matrix. */ static unsigned int gaussReduce(matrix_t m, unsigned int cols, unsigned int rows) { unsigned int rowUpto = 0; unsigned int irow, checkRow; int icol; for (icol = cols-1; icol >= 0; icol--) { irow = rowUpto; while ( (irow < rows) && (getEntry(m,irow,icol) == 0) ) irow++; if (irow < rows) { swapRows(m,rowUpto,irow); for (checkRow = rowUpto+1; checkRow < rows; checkRow++) { if (getEntry(m,checkRow,icol) != 0) xorRows(m, cols, rowUpto, checkRow); } rowUpto++; } } return rowUpto; }
int main(){ int i = 0; int j = 0; int k = 0; swapRows(test()); for(i=0; i<3;i++){ for(j=i+1; j<3; j++){ cte = A[j][i]/A[i][i]; for(k=i; k<3; k++){ A[j][k] = A[j][k] - cte * A[i][k]; } B[j] = B[j] - cte * B[i]; } } printMatrix(); printf("\nx_3 value: %0.2f", B[2]/A[2][2]); return 0; }
// LU decomposition IN PLACE // Uses simple Dolittle Algorithm // Returns the permuation of the rows int *Matrix::LU() { int *perm; assertDefined("LU decomposition"); perm = new int(maxr); for (int r=0; r<maxr; r++) perm[r] = r; for (int r=0; r<maxr; r++) { for (int rr=r+1; rr<maxr; rr++) { double l; if (m[r][r]==0) { for (int j=r+1; j<maxr; j++) { if (m[j][r]!=0) { int tmp; swapRows(r, j); tmp = perm[r]; perm[r] = perm[j]; perm[j] = tmp; break; } } } l = m[rr][r]/m[r][r]; for (int c=r; c<maxc; c++) { m[rr][c] -= l * m[r][c]; } m[rr][r] = l; } } for (int r=0; r<maxr; r++) printf("%d\n", perm[r]); return perm; }
void GSparseMatrix::shuffle(GRand* pRand) { for(unsigned int n = m_rows; n > 0; n--) swapRows((unsigned int)pRand->next(n), n - 1); }
int main() { /************************************************************/ /* */ /* ここに課題のプログラムを書く. */ /* */ /* 関数 read_matrix() を呼ぶことで,標準入力から連立一次 */ /* 方程式の次数および各係数を読み込む.その結果, */ /* 次数が大域変数 n に, */ /* 係数行列 A と右辺ベクトル b を合わせた拡大行列 [A b] が */ /* 帯域変数 a[][] に,それぞれ格納される. */ /* なお,a[][]の内部は */ /* A → a[1][1] ... a[n][n] , */ /* b → a[1][n+1] ... a[n][n+1] */ /* のようになっている. */ /* */ /* 関数 print_matrix() は a[][] の内容を標準出力に */ /* 出力する.アルゴリズムのデバッグに活用すべし. */ /* */ /************************************************************/ /* 以下はサンプルプログラムで,行列を読み込んでそのまま出力する.*/ /* 標準入力を読み込んで,係数行列を a[][] に,次数を n に格納する */ if (read_matrix() != 0) { /* 返り値が 0 以外であればエラー */ fprintf(stderr, "input error!\n"); return -1; } /* 現在の係数行列 a[][] の内容を出力する */ print_matrix(); /* ここにガウス消去法などの課題のプログラムを書く */ // Forward elimination int j, k; // Flag that shows whether there is an unique solution or not. 0 if not, 1 // otherwise. Default is 0 and it will be changed when not ALL j >= k is 0. int uniqueSolution = 0; for (k = 1; k <= n; k++) { for (j = k + 1; j <= n; j++) { // (a) if (j >= k && a[j][k] != 0) uniqueSolution = 1; // Swap rows if a[k][k] is zero // or if the difference is less than a particular value double diff = a[k][k] - a[j][k] > 0 ? a[k][k] - a[j][k] : -(a[k][k] - a[j][k]); /* Test data that shows this edit is valid 3 0.000000000000000001 1 2 4 0.000000000000000001 2 2 7 1 2 1 3 */ if ((a[k][k] >= 0 && a[k][k] < EPSILON) || (diff > 0 && diff < EPSILON)) swapRows(k, k); // (b) // i double mjk = a[j][k] / a[k][k]; //printf("k: %d\n", k); //printf("mjk = %f / %f = %f\n", a[j][k], a[k][k], mjk); // ii int p; for (p = k; p <= n + 1; p++) a[j][p] -= mjk * a[k][p]; } } if (uniqueSolution == 0 || a[n][n] == 0) { printf("前進消去後\n"); print_matrix(); printf("一意解が存在しません。\n"); return 0; } // Checking if the forward elimination went good printf("前進消去後\n"); print_matrix(); // Now, on to the backward substitution double x[n + 1]; x[n] = a[n][n + 1] / a[n][n]; int i; // Since we've already done n 2 lines before, we start with n - 1 for (i = n - 1; i >= 1; i--) { double sum = 0; for (j = i + 1; j <= n; j++) sum += a[i][j] * x[j]; x[i] = (a[i][n + 1] - sum) / a[i][i]; } // Finally, print the result for (j = 1; j < n + 1; j++) printf("%f ", x[j]); /* プログラムの終了 */ return 0; }
void Config::down() { swapRows(tblActions->currentRow(), tblActions->currentRow() + 1); }
void Config::up() { swapRows(tblActions->currentRow(), tblActions->currentRow() - 1); }
bool QTable::qt_invoke( int _id, QUObject* _o ) { switch ( _id - staticMetaObject()->slotOffset() ) { case 0: setNumRows((int)static_QUType_int.get(_o+1)); break; case 1: setNumCols((int)static_QUType_int.get(_o+1)); break; case 2: setShowGrid((bool)static_QUType_bool.get(_o+1)); break; case 3: hideRow((int)static_QUType_int.get(_o+1)); break; case 4: hideColumn((int)static_QUType_int.get(_o+1)); break; case 5: showRow((int)static_QUType_int.get(_o+1)); break; case 6: showColumn((int)static_QUType_int.get(_o+1)); break; case 7: static_QUType_bool.set(_o,isRowHidden((int)static_QUType_int.get(_o+1))); break; case 8: static_QUType_bool.set(_o,isColumnHidden((int)static_QUType_int.get(_o+1))); break; case 9: setColumnWidth((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 10: setRowHeight((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 11: adjustColumn((int)static_QUType_int.get(_o+1)); break; case 12: adjustRow((int)static_QUType_int.get(_o+1)); break; case 13: setColumnStretchable((int)static_QUType_int.get(_o+1),(bool)static_QUType_bool.get(_o+2)); break; case 14: setRowStretchable((int)static_QUType_int.get(_o+1),(bool)static_QUType_bool.get(_o+2)); break; case 15: static_QUType_bool.set(_o,isColumnStretchable((int)static_QUType_int.get(_o+1))); break; case 16: static_QUType_bool.set(_o,isRowStretchable((int)static_QUType_int.get(_o+1))); break; case 17: setSorting((bool)static_QUType_bool.get(_o+1)); break; case 18: swapRows((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 19: swapRows((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2),(bool)static_QUType_bool.get(_o+3)); break; case 20: swapColumns((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 21: swapColumns((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2),(bool)static_QUType_bool.get(_o+3)); break; case 22: swapCells((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2),(int)static_QUType_int.get(_o+3),(int)static_QUType_int.get(_o+4)); break; case 23: setLeftMargin((int)static_QUType_int.get(_o+1)); break; case 24: setTopMargin((int)static_QUType_int.get(_o+1)); break; case 25: setCurrentCell((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 26: clearSelection(); break; case 27: clearSelection((bool)static_QUType_bool.get(_o+1)); break; case 28: setColumnMovingEnabled((bool)static_QUType_bool.get(_o+1)); break; case 29: setRowMovingEnabled((bool)static_QUType_bool.get(_o+1)); break; case 30: setReadOnly((bool)static_QUType_bool.get(_o+1)); break; case 31: setRowReadOnly((int)static_QUType_int.get(_o+1),(bool)static_QUType_bool.get(_o+2)); break; case 32: setColumnReadOnly((int)static_QUType_int.get(_o+1),(bool)static_QUType_bool.get(_o+2)); break; case 33: setDragEnabled((bool)static_QUType_bool.get(_o+1)); break; case 34: static_QUType_bool.set(_o,dragEnabled()); break; case 35: insertRows((int)static_QUType_int.get(_o+1)); break; case 36: insertRows((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 37: insertColumns((int)static_QUType_int.get(_o+1)); break; case 38: insertColumns((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 39: removeRow((int)static_QUType_int.get(_o+1)); break; case 40: removeRows((const QMemArray<int>&)*((const QMemArray<int>*)static_QUType_ptr.get(_o+1))); break; case 41: removeColumn((int)static_QUType_int.get(_o+1)); break; case 42: removeColumns((const QMemArray<int>&)*((const QMemArray<int>*)static_QUType_ptr.get(_o+1))); break; case 43: editCell((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2)); break; case 44: editCell((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2),(bool)static_QUType_bool.get(_o+3)); break; case 45: setRowLabels((const QStringList&)*((const QStringList*)static_QUType_ptr.get(_o+1))); break; case 46: setColumnLabels((const QStringList&)*((const QStringList*)static_QUType_ptr.get(_o+1))); break; case 47: columnWidthChanged((int)static_QUType_int.get(_o+1)); break; case 48: rowHeightChanged((int)static_QUType_int.get(_o+1)); break; case 49: columnIndexChanged((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2),(int)static_QUType_int.get(_o+3)); break; case 50: rowIndexChanged((int)static_QUType_int.get(_o+1),(int)static_QUType_int.get(_o+2),(int)static_QUType_int.get(_o+3)); break; case 51: columnClicked((int)static_QUType_int.get(_o+1)); break; case 52: doAutoScroll(); break; case 53: doValueChanged(); break; case 54: updateGeometriesSlot(); break; default: return QScrollView::qt_invoke( _id, _o ); } return TRUE; }
bool PointModel::swapRows(int oldRow, int newRow) { QModelIndex oldIndex=index(oldRow,0,QModelIndex()); QModelIndex newIndex=index(newRow,0,QModelIndex()); return swapRows(oldIndex, newIndex); }