// TODO implement error handling void row_echolon_form(index_type start) { if (start == std::min(rows, cols)) return; index_type not_found = n_pos; // sort the rows so that the row with the first leading // pivot element is the top row std::sort(data.begin(), data.end(), [not_found](const std::array<double, m> &row_one, const std::array<double, m> &row_two){ return find_pivot(row_one, not_found) < find_pivot(row_two, not_found); }); // get pivot for first row index_type pivot = find_pivot(start); for (index_type i = start + 1; i < rows; i++) { index_type curr_pivot = find_pivot(i); // if there's no pivot element in this row then there will be no // pivot element in this column in some row later on, because the rows // have been sorted by the position of the row's pivot element. if (curr_pivot != pivot) break; // pivot * x = -curr_pivot; | / pivot // x = - curr_pivot / pivot; scale_and_add(start, i, -(data[i][curr_pivot] / data[start][pivot])); } row_echolon_form(start + 1); }
int find_pivot(int *arr,int low,int high){ if(high<low) return -1; if(high==low) return low; int mid = low + (high-low)/2; if(mid<high && arr[mid]>arr[mid+1]) return mid; if(mid>low && arr[mid]<arr[mid-1]) return (mid-1); if(arr[low]>=arr[mid]) return find_pivot(arr,low,mid-1); else return find_pivot(arr,mid+1,high); }
void merge_task(long n, T left[n], T right[n], T result[n*2], long start, long length) { long leftStart, rightStart; find_pivot(left, right, n, start, &leftStart, &rightStart); result += start; while (length != 0L) { if (leftStart == n) { *result = right[rightStart]; rightStart++; result++; } else if (rightStart == n) { *result = left[leftStart]; leftStart++; result++; } else if (left[leftStart] <= right[rightStart]) { *result = left[leftStart]; leftStart++; result++; } else { *result = right[rightStart]; rightStart++; result++; } length--; } }
void seq_merge(long n, T left[n], T right[n], T result[n*2], long start, long length) { long leftStart, rightStart; find_pivot(left, right, n, start, &leftStart, &rightStart); //printf("Merge %x[%i:%i] %x[%i:%i] -> %x[%i:%i]\n\n", left, leftStart, n, right, rightStart, n, result, start, length); result += start; while (length != 0) { if (leftStart == n) { *result = right[rightStart]; rightStart++; result++; } else if (rightStart == n) { *result = left[leftStart]; leftStart++; result++; } else if (left[leftStart] <= right[rightStart]) { *result = left[leftStart]; leftStart++; result++; } else { *result = right[rightStart]; rightStart++; result++; } length--; } }
template<typename MatrixType> bool find_pivot(typename MatrixType::Scalar tol, MatrixType &diffs, Index col=0) { bool match = diffs.diagonal().sum() <= tol; if(match || col==diffs.cols()) { return match; } else { Index n = diffs.cols(); std::vector<std::pair<Index,Index> > transpositions; for(Index i=col; i<n; ++i) { Index best_index(0); if(diffs.col(col).segment(col,n-i).minCoeff(&best_index) > tol) break; best_index += col; diffs.row(col).swap(diffs.row(best_index)); if(find_pivot(tol,diffs,col+1)) return true; diffs.row(col).swap(diffs.row(best_index)); // move current pivot to the end diffs.row(n-(i-col)-1).swap(diffs.row(best_index)); transpositions.push_back(std::pair<Index,Index>(n-(i-col)-1,best_index)); } // restore for(Index k=transpositions.size()-1; k>=0; --k) diffs.row(transpositions[k].first).swap(diffs.row(transpositions[k].second)); } return false; }
void quick_sort(int num[], int start, int end) { int pivot_index = find_pivot(num, start, end); //找轴点位置 swap(&num[pivot_index], &num[end]); //把轴点放最后 int k = partition(num, start, end); //把小于轴点的值放在k前面,大于轴点的值放在k后面 swap(&num[k], &num[end]); //把轴点放回k的位置 if (k - start > 1) quick_sort(num, start, k-1); //用快速排序继续解决k前面的部分; if (end - k > 1) quick_sort(num, k+1, end);//用快速排序继续解决k后面的部分 }
void ConvexHull::graham() { if(is_degenerate()) // nothing to do return; find_pivot(); angle_sort(); graham_scan(); }
void GaussJordan::uptriangle () { for (int k = 0; k < size; k++) { find_pivot (k); process_column (k); } }
int find_num(int *arr,int siz,int num){ int pivot = find_pivot(arr,0,siz-1); if(pivot==-1) return binary_srch(arr,0,siz-1,num); if(arr[pivot]==num) return pivot; if(arr[0]<=num) return binary_srch(arr,0,pivot-1,num); else return binary_srch(arr,pivot+1,siz-1,num); }
void QuickSort::sort(vector<int> &vectorToSort, SortParams params, int begining, int end) { if (begining < end) { int pivot = find_pivot(vectorToSort, begining, end); int i = partition(vectorToSort, begining, end, pivot, params); sort(vectorToSort, params, begining, i-1); sort(vectorToSort, params, i, end); } MemoryTracker::save_memory(); }
static void quick_sort(int a[], int start, int end) { int pivot; if (start < end) { pivot = find_pivot(a, start, end); quick_sort(a, start, pivot - 1); quick_sort(a, pivot + 1, end); } }
void quicker_sort(EDGE *left, EDGE *right) { EDGE *p; double pivot; if (find_pivot(left, right, &pivot) == yes) { p = partition(left, right, pivot); quicker_sort(left, p - 1); quicker_sort(p, right); } }
static void qksort_population(entity **array_of_ptrs, int first, int last) { double pivot; int k; #if GA_QSORT_DEBUG>1 printf("DEBUG: first %d last %d\n", first, last); #endif #if GA_QSORT_DEBUG>2 for (k = first ; k < last ; k++) printf("%u: %f\n", k, array_of_ptrs[k]->fitness); #endif /* Don't bother sorting small ranges if ( last - first < 7) return; */ if (find_pivot(array_of_ptrs, first, last, &pivot) == FALSE) return; k = partition(array_of_ptrs, first, last, pivot); #if GA_QSORT_DEBUG>1 printf("DEBUG: partition %d pivot %f\n", k, pivot); #endif /* * Don't really need these comparisions, unless we selected a poor pivot? * But actually, I've realised that since the population often has degenerate * fitness scores, this is a good way to cut down on the sort's workload. */ if (k==first) { while (array_of_ptrs[k]->fitness == array_of_ptrs[first]->fitness && first<last) { first++; k++; } } else if (k==last) { while (array_of_ptrs[k]->fitness == array_of_ptrs[last]->fitness && last>first) { last--; } } if (k > first+7) qksort_population(array_of_ptrs, first, k-1); if (k < last-6) qksort_population(array_of_ptrs, k, last); return; }
int linear_search(vector<int>& nums, int s, int e, int k) { if(s >= e) { return nums[s]; } int large = find_pivot(nums, s, e); if(large == k) { return nums[large]; } else if(large > k) { return linear_search(nums, s, large - 1, k); } else { return linear_search(nums, large + 1, e, k); } }
static void inner_sort(T* array, int length, C comparator) { if (length < 2) { return; } int pivot = find_pivot(array, length, comparator); if (length < 4) { // arrays up to length 3 will be sorted after finding the pivot return; } int split = partition<T, C, idempotent>(array, pivot, length, comparator); int first_part_length = split + 1; inner_sort<T, C, idempotent>(array, first_part_length, comparator); inner_sort<T, C, idempotent>(&array[first_part_length], length - first_part_length, comparator); }
//The main quicksort function. void quicksort(char **left, char **right, int n_piv) { char **p, *pivot; /*If a pivot is found, then the array needs sorting. * If no pivot is found this indicates all values * in the array are equal and quicksort doesn't need * to run. */ if (find_pivot(left, right, &pivot, n_piv) == yes) { p = partition(left, right, pivot); quicksort(left, p - 1, n_piv); quicksort(p, right, n_piv); } }
void verify_is_approx_upto_permutation(const VectorType& vec1, const VectorType& vec2) { typedef typename VectorType::Scalar Scalar; typedef typename NumTraits<Scalar>::Real RealScalar; VERIFY(vec1.cols() == 1); VERIFY(vec2.cols() == 1); VERIFY(vec1.rows() == vec2.rows()); Index n = vec1.rows(); RealScalar tol = test_precision<RealScalar>()*test_precision<RealScalar>()*numext::maxi(vec1.squaredNorm(),vec2.squaredNorm()); Matrix<RealScalar,Dynamic,Dynamic> diffs = (vec1.rowwise().replicate(n) - vec2.rowwise().replicate(n).transpose()).cwiseAbs2(); VERIFY( find_pivot(tol, diffs) ); }
void eliminate(double* m, int N, double* b) { for(int r=0; r<N-1; r++) { int p = find_pivot(m, N, r); if(p != r) { swap_rows(m, N, r, p); swap(b+r, b+p); } double piv = m[r*N+r]; // if(fabs(piv)<1e-20) {printf("matrix nearly singular ...."); abort();} for(int rp = r+1; rp<N; rp++) { double fact = -m[rp*N+r]/piv; for(int c=r; c<N; c++) m[rp*N+c]+=fact*m[r*N+c]; b[rp]+=fact*b[r]; } } }
void hilb_comp::do_ideal(MonomialIdeal *I) { // This either will multiply to current->h1, or it will recurse down // Notice that one, and minus_one are global in this class. // LOCAL_deg1, LOCAL_vp are scratch variables defined in this class nideal++; ring_elem F = R->from_int(1); ring_elem G; int len = I->length(); if (len <= 2) { // len==1: set F to be 1 - t^(deg m), where m = this one element // len==2: set F to be 1 - t^(deg m1) - t^(deg m2) + t^(deg lcm(m1,m2)) M->degree_of_varpower(I->first_elem(), LOCAL_deg1); G = R->make_flat_term(minus_one, LOCAL_deg1); R->add_to(F, G); if (len == 2) { M->degree_of_varpower(I->second_elem(), LOCAL_deg1); G = R->make_flat_term(minus_one, LOCAL_deg1); R->add_to(F, G); LOCAL_vp.shrink(0); varpower::lcm(I->first_elem(), I->second_elem(), LOCAL_vp); M->degree_of_varpower(LOCAL_vp.raw(), LOCAL_deg1); G = R->make_flat_term(one, LOCAL_deg1); R->add_to(F,G); } delete I; } else { int npure; intarray pivot; intarray pure_a; int *pure = pure_a.alloc(I->topvar()+1); if (!find_pivot(*I, npure, pure, pivot)) { // set F to be product(1-t^(deg x_i^e_i)) // - t^(deg pivot) product((1-t^(deg x_i^(e_i - m_i))) // where e_i >= 1 is the smallest number s.t. x_i^(e_i) is in I, // and m_i = exponent of x_i in pivot element (always < e_i). M->degree_of_varpower(pivot.raw(), LOCAL_deg1); G = R->make_flat_term(one, LOCAL_deg1); for (index_varpower i = pivot.raw(); i.valid(); ++i) if (pure[i.var()] != -1) { ring_elem H = R->from_int(1); D->power(M->degree_of_var(i.var()), pure[i.var()], LOCAL_deg1); ring_elem tmp = R->make_flat_term(minus_one, LOCAL_deg1); R->add_to(H, tmp); R->mult_to(F, H); R->remove(H); H = R->from_int(1); D->power(M->degree_of_var(i.var()), pure[i.var()] - i.exponent(), LOCAL_deg1); tmp = R->make_flat_term(minus_one, LOCAL_deg1); R->add_to(H, tmp); R->mult_to(G, H); R->remove(H); } R->subtract_to(F, G); delete I; } else { // This is the one case in which we recurse down R->remove(F); recurse(I, pivot.raw()); return; } } R->mult_to(current->h1, F); R->remove(F); current->i--; }
int lux_decomp(LUX *lux, int (*col)(void *info, int j, int ind[], mpq_t val[]), void *info) { int n = lux->n; LUXELM **V_row = lux->V_row; LUXELM **V_col = lux->V_col; int *P_row = lux->P_row; int *P_col = lux->P_col; int *Q_row = lux->Q_row; int *Q_col = lux->Q_col; LUXELM *piv, *vij; LUXWKA *wka; int i, j, k, p, q, t, *flag; mpq_t *work; /* allocate working area */ wka = xmalloc(sizeof(LUXWKA)); wka->R_len = xcalloc(1+n, sizeof(int)); wka->R_head = xcalloc(1+n, sizeof(int)); wka->R_prev = xcalloc(1+n, sizeof(int)); wka->R_next = xcalloc(1+n, sizeof(int)); wka->C_len = xcalloc(1+n, sizeof(int)); wka->C_head = xcalloc(1+n, sizeof(int)); wka->C_prev = xcalloc(1+n, sizeof(int)); wka->C_next = xcalloc(1+n, sizeof(int)); /* initialize LU-factorization data structures */ initialize(lux, col, info, wka); /* allocate working arrays */ flag = xcalloc(1+n, sizeof(int)); work = xcalloc(1+n, sizeof(mpq_t)); for (k = 1; k <= n; k++) { flag[k] = 0; mpq_init(work[k]); } /* main elimination loop */ for (k = 1; k <= n; k++) { /* choose a pivot element v[p,q] */ piv = find_pivot(lux, wka); if (piv == NULL) { /* no pivot can be chosen, because the active submatrix is empty */ break; } /* determine row and column indices of the pivot element */ p = piv->i, q = piv->j; /* let v[p,q] correspond to u[i',j']; permute k-th and i'-th rows and k-th and j'-th columns of the matrix U = P*V*Q to move the element u[i',j'] to the position u[k,k] */ i = P_col[p], j = Q_row[q]; xassert(k <= i && i <= n && k <= j && j <= n); /* permute k-th and i-th rows of the matrix U */ t = P_row[k]; P_row[i] = t, P_col[t] = i; P_row[k] = p, P_col[p] = k; /* permute k-th and j-th columns of the matrix U */ t = Q_col[k]; Q_col[j] = t, Q_row[t] = j; Q_col[k] = q, Q_row[q] = k; /* eliminate subdiagonal elements of k-th column of the matrix U = P*V*Q using the pivot element u[k,k] = v[p,q] */ eliminate(lux, wka, piv, flag, work); } /* determine the rank of A (and V) */ lux->rank = k - 1; /* free working arrays */ xfree(flag); for (k = 1; k <= n; k++) mpq_clear(work[k]); xfree(work); /* build column lists of the matrix V using its row lists */ for (j = 1; j <= n; j++) xassert(V_col[j] == NULL); for (i = 1; i <= n; i++) { for (vij = V_row[i]; vij != NULL; vij = vij->r_next) { j = vij->j; vij->c_prev = NULL; vij->c_next = V_col[j]; if (vij->c_next != NULL) vij->c_next->c_prev = vij; V_col[j] = vij; } } /* free working area */ xfree(wka->R_len); xfree(wka->R_head); xfree(wka->R_prev); xfree(wka->R_next); xfree(wka->C_len); xfree(wka->C_head); xfree(wka->C_prev); xfree(wka->C_next); xfree(wka); /* return to the calling program */ return (lux->rank < n); }
int main() { //m = num_of_constraints //n = num_of_variables int i, j, k, l=0, m, n, count_surp=0, count_art=0, c=0, dummy=0, res=0; float **A, **S, *obj_fun, *type, sum=0; // char *c1, *c2, c; printf("enter the number of constraints: "); scanf("%d", &m); printf("enter the number of variables: "); scanf("%d", &n); printf("\n"); //forming the constraint matrix A = (float **)malloc(m * sizeof(float *)); for(i=0; i<m; i++) { printf("constraint #%d: ",i+1); A[i] = (float *)malloc((n+1) * sizeof(float)); for(j=0; j<(n+1) ; j++) scanf("%f",&A[i][j]); } printf("\n"); //forming the objective function array obj_fun = (float *)malloc(n * sizeof(float)); printf("enter the coefficients of the objective function: "); for(i=0; i<n; i++) scanf("%f", &obj_fun[i]); printf("\n"); //forming the type array type = (float *)malloc(m * sizeof(float)); printf("1 -> less than equal to\n2 -> greater than equal to\n3 -> equal to\n"); for(i=0; i<m; i++) { printf("type for constraint #%d: ",i+1); scanf("%f", &type[i]); if(type[i]==2) count_surp++; } printf("\n"); //forming the first simplex tableau S = (float **)calloc((m+1), sizeof(float *)); for(i=0; i<m+1; i++) { k=0; l=0; S[i] = (float *)calloc((n+count_surp+1), sizeof(float)); for(j=0; j<(n+count_surp+1) ; j++) { sum=0; if(i<m && j<n) S[i][j] = A[i][j]; if(i<m && k==0 && j>=n && j<count_surp+n && type[i]==2) { k=1; S[i][j+c] = -1; c++; } if(i<m && j==n+count_surp) S[i][j] = A[i][n]; if(i==m) { for(k=0; k<m; k++) sum += S[k][l]; if(j<n) S[i][j] = -(sum*M) - obj_fun[j]; if(j>=n && j<=count_surp+n) S[i][j] = -(sum*M); } l++; } } //forming title matrices /* c1 = (char **)malloc((m+1) * sizeof(char*)); for(i=0; i<m+1; i++) { c1[i] = (char *)malloc(sizeof(char)); for(j=0; j<2; j++) { c = (char)(i+1); c1[i][ } } */ printf("Coefficient matrix:\n"); display2(A, m, n+1); printf("type array:\n"); display1(type, m); printf("objective function\n"); display1(obj_fun, n); printf("Simplex matirx\n"); display2(S, m+1, n+count_surp+1); while(res==0) { res = find_pivot(S, m+1, n+count_surp+1); display2(S, m+1,n+count_surp+1); } return 0; }