/* Calcula a decomposição QR de uma dada matriz A. * Recebe a matriz A que será decomposta em Q*R e a Q_i e R encontradas são retornadas * armazenadas na própria A. Recebe-se o vetor 'b' (Ax = b) e retorna-se nele mesmo * o vetor c = Qt * b. Em sigma recebe-se a norma_2 de cada coluna de A e nele retorna-se * a diagonal principal de R. Em map retorna-se o vetor que representa as trocas de colunas * feitas durante a decomposição. 'n' é o número de linhas da matriz e 'm' é o número de colunas * A funcao devolve o inteiro rank que é o posto da matriz A */ int qr(double A[][MAX], double b[], double sigma[], int map[], int n, int m) { int i, j, k, maxI; double max, alpha, beta; int rank = 0; for (j = 0; j < m; j++) { double w[MAX]; /* swap columns */ max = sigma[j]; maxI = j; for (k = j + 1; k < m; k++) if (sigma[k] > max) { max = sigma[k]; maxI = k; } if (abs(max) < eps) { printf("A matriz possui posto incompleto\n"); return rank; } if (maxI != j) { swapColumns(A, n, j, maxI); swap(&sigma[j], &sigma[maxI]); } map[j] = maxI; if (A[j][j] < 0) sigma[j] = -sigma[j]; A[j][j] += sigma[j]; /* Multiplica Q_j por A_j */ /*w = uT * A*/ for (i = 0; i < m - j; i++) w[i] = 0; for (i = j; i < n; i++) for (k = j + 1; k < m; k++) w[k - (j + 1)] += A[i][j] * A[i][k]; /*A -= gama * u * w*/ for (i = j; i < n; i++) for (k = j + 1; k < m; k++) A[i][k] -= (A[i][j] * w[k - (j + 1)])/ (sigma[j] * A[j][j]); /* multiplica Q_j por b_j */ beta = 0; for (i = j; i < n; i++) beta += A[i][j] * b[i]; for (i = j; i < n; i++) b[i] -= beta * A[i][j] / (sigma[j] * A[j][j]); sigma[j] = -sigma[j]; /* Reajuste das novas normas2 de cada coluna a partir da linha j + 1 */ for (k = j + 1; k < m; k++) { sigma[k] = sigma[k] * sigma[k] - A[j][k] * A[j][k]; if (sigma[k] < 0) sigma[k] = 0; sigma[k] = sqrt(sigma[k]); } rank++; } return rank; }
void Matrix::sortColumns() //actually it is an insertSorting of pointers { int k = 0; for (int x = 1; x < width; x++) { k = x; while (firstColumnIsMore(table[k - 1], table[k])) { swapColumns(table[k], table[k - 1]); k--; if (k == 0) break; } } }
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; }
DataManipulationForm::DataManipulationForm(QWidget * parent, Qt::WindowFlags f): QDialog(parent, f) { setupUi(this); setWindowFlags(Qt::Dialog | Qt::WindowMinMaxButtonsHint | Qt::WindowCloseButtonHint); filter_hl=new SyntaxHighlighter(filter_txt); filter_hl->loadConfiguration(GlobalAttributes::SQL_HIGHLIGHT_CONF_PATH); code_compl_wgt=new CodeCompletionWidget(filter_txt); code_compl_wgt->configureCompletion(nullptr, filter_hl); refresh_tb->setToolTip(refresh_tb->toolTip() + QString(" (%1)").arg(refresh_tb->shortcut().toString())); save_tb->setToolTip(save_tb->toolTip() + QString(" (%1)").arg(save_tb->shortcut().toString())); undo_tb->setToolTip(undo_tb->toolTip() + QString(" (%1)").arg(undo_tb->shortcut().toString())); export_tb->setToolTip(export_tb->toolTip() + QString(" (%1)").arg(export_tb->shortcut().toString())); delete_tb->setToolTip(delete_tb->toolTip() + QString(" (%1)").arg(delete_tb->shortcut().toString())); add_tb->setToolTip(add_tb->toolTip() + QString(" (%1)").arg(add_tb->shortcut().toString())); //Forcing the splitter that handles the bottom widgets to resize its children to their minimum size h_splitter->setSizes({500, 250, 500}); v_splitter->setVisible(false); connect(close_btn, SIGNAL(clicked()), this, SLOT(reject())); connect(schema_cmb, SIGNAL(currentIndexChanged(int)), this, SLOT(listTables())); connect(hide_views_chk, SIGNAL(toggled(bool)), this, SLOT(listTables())); connect(schema_cmb, SIGNAL(currentIndexChanged(int)), this, SLOT(disableControlButtons())); connect(table_cmb, SIGNAL(currentIndexChanged(int)), this, SLOT(disableControlButtons())); connect(table_cmb, SIGNAL(currentIndexChanged(int)), this, SLOT(listColumns())); connect(table_cmb, SIGNAL(currentIndexChanged(int)), this, SLOT(retrieveData())); connect(refresh_tb, SIGNAL(clicked()), this, SLOT(retrieveData())); connect(add_ord_col_tb, SIGNAL(clicked()), this, SLOT(addColumnToList())); connect(ord_columns_lst, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(removeColumnFromList())); connect(ord_columns_lst, SIGNAL(itemPressed(QListWidgetItem*)), this, SLOT(changeOrderMode(QListWidgetItem*))); connect(rem_ord_col_tb, SIGNAL(clicked()), this, SLOT(removeColumnFromList())); connect(clear_ord_cols_tb, SIGNAL(clicked()), this, SLOT(clearColumnList())); connect(results_tbw, SIGNAL(itemChanged(QTableWidgetItem*)), this, SLOT(markUpdateOnRow(QTableWidgetItem *))); connect(delete_tb, SIGNAL(clicked()), this, SLOT(markDeleteOnRows())); connect(add_tb, SIGNAL(clicked()), this, SLOT(insertRow())); connect(undo_tb, SIGNAL(clicked()), this, SLOT(undoOperations())); connect(save_tb, SIGNAL(clicked()), this, SLOT(saveChanges())); connect(ord_columns_lst, SIGNAL(currentRowChanged(int)), this, SLOT(enableColumnControlButtons())); connect(move_down_tb, SIGNAL(clicked()), this, SLOT(swapColumns())); connect(move_up_tb, SIGNAL(clicked()), this, SLOT(swapColumns())); connect(filter_tb, SIGNAL(toggled(bool)), v_splitter, SLOT(setVisible(bool))); //Using the QueuedConnection here to avoid the "edit: editing failed" when editing and navigating through items using tab key connect(results_tbw, SIGNAL(currentCellChanged(int,int,int,int)), this, SLOT(insertRowOnTabPress(int,int,int,int)), Qt::QueuedConnection); connect(results_tbw, &QTableWidget::itemPressed, [=](){ SQLExecutionWidget::copySelection(results_tbw); }); connect(copy_tb, &QToolButton::clicked, [=](){ SQLExecutionWidget::copySelection(results_tbw, false); }); connect(export_tb, &QToolButton::clicked, [=](){ SQLExecutionWidget::exportResults(results_tbw); }); connect(results_tbw, &QTableWidget::itemSelectionChanged, [=](){ QList<QTableWidgetSelectionRange> sel_ranges=results_tbw->selectedRanges(); copy_tb->setEnabled(!sel_ranges.isEmpty()); delete_tb->setEnabled(results_tbw->editTriggers()!=QAbstractItemView::NoEditTriggers && !sel_ranges.isEmpty()); }); }
QList<Number> Gauss::findSolution(QList<QList<Number> > coefficients) { int variablesCount = coefficients.size(); int columnsCount = variablesCount + 1; int rowsCount = variablesCount; // Save initial order of variables. QList<int> orderOfVariables; for (int i = 0; i < variablesCount; ++i) { orderOfVariables << i; } // Direct pass for (int i = 0; i < rowsCount; ++i) { int mainElementIndex = i; Number mainElement = coefficients[i][i]; // Find main element of the current row for (int j = i + 1; j < columnsCount - 1; ++j) { if (qAbs(mainElement) < qAbs(coefficients[i][j])) { mainElementIndex = j; mainElement = coefficients[i][j]; } } if (MathUtils::isNull(mainElement)) { THROW(ENoSolution()); } // Swap columns if (mainElementIndex != i) { swapColumns(coefficients, i, mainElementIndex); orderOfVariables.swap(i, mainElementIndex); } // Divide the row by the main element coefficients[i] = MathUtils::divideVectorByNumber(coefficients[i], mainElement); // Subtract current row (multiplied before) from rows below. for (int j = i + 1; j < rowsCount; ++j) { QList<Number> multipliedRow = MathUtils::multiplyVectorByNumber(coefficients[i], coefficients[j][i]); coefficients[j] = MathUtils::subtractVectorFromVector(coefficients[j], multipliedRow); } } // Reverse pass for (int i = rowsCount - 1; i >= 0; --i) { for (int j = i - 1; j >= 0; --j) { // subtract current row (multiplied before) from rows above. QList<Number> multipliedRow = MathUtils::multiplyVectorByNumber(coefficients[i], coefficients[j][i]); coefficients[j] = MathUtils::subtractVectorFromVector(coefficients[j], multipliedRow); } } // Form result QList<Number> result = sizedVector(variablesCount); for (int i = 0; i < rowsCount; ++i) { result[orderOfVariables[i]] = coefficients[i][columnsCount - 1]; } return result; }