mat addPadding(mat x, int ksize) { int offset = ksize/2; x.insert_rows(0, offset); x.insert_rows(x.n_rows, offset); x.insert_cols(0, offset); x.insert_cols(x.n_cols, offset); return(x); }
void SolveX(mat& C,mat& D,mat& G,mat& B,mat& A,mat& X,vec& Z){//Solve vector of unknowns A.zeros(0,0); C = trans(B); A.insert_rows(0,G); A.insert_cols(A.n_cols,B); C.insert_cols(C.n_cols,D); A.insert_rows(A.n_rows,C); X = solve( A, Z ); }
//' C++ Wrapper to Check Stability of Two-sided Matching //' //' This function checks if a given matching is stable for a particular set of //' preferences. This function provides an R wrapper for the C++ backend. Users //' should not call this function directly and instead use //' \code{\link{galeShapley.checkStability}}. //' //' @param proposerUtils is a matrix with cardinal utilities of the proposing //' side of the market. If there are \code{n} proposers and \code{m} reviewers, //' then this matrix will be of dimension \code{m} by \code{n}. The //' \code{i,j}th element refers to the payoff that individual \code{j} receives //' from being matched to individual \code{i}. //' @param reviewerUtils is a matrix with cardinal utilities of the courted side //' of the market. If there are \code{n} proposers and \code{m} reviewers, then //' this matrix will be of dimension \code{n} by \code{m}. The \code{i,j}th //' element refers to the payoff that individual \code{j} receives from being //' matched to individual \code{i}. //' @param proposals is a matrix that contains the number of the reviewer that a //' given proposer is matched to: the first row contains the number of the //' reviewer that is matched with the first proposer (using C++ indexing), the //' second row contains the id of the reviewer that is matched with the second //' proposer, etc. The column dimension accommodates proposers with multiple //' slots. //' @param engagements is a matrix that contains the number of the proposer that //' a given reviewer is matched to (using C++ indexing). The column dimension //' accommodates reviewers with multiple slots. //' @return true if the matching is stable, false otherwise // [[Rcpp::export]] bool cpp_wrapper_galeshapley_check_stability(mat proposerUtils, mat reviewerUtils, umat proposals, umat engagements) { // number of workers const int M = proposerUtils.n_cols; // number of firms const int N = proposerUtils.n_rows; // number of slots per firm const int slotsReviewers = engagements.n_cols; // number of slots per worker const int slotsProposers = proposals.n_cols; // more jobs than workers (add utility from being unmatched to firms' preferences) if(N*slotsReviewers>M*slotsProposers) { reviewerUtils.insert_rows(M, 1); reviewerUtils.row(M).fill(-1e10); } // more workers than jobs (add utility from being unmatched to workers' preferences) if(M*slotsProposers>N*slotsReviewers) { proposerUtils.insert_rows(N, 1); proposerUtils.row(N).fill(-1e10); } // loop over workers for(int wX=0; wX<M; wX++) { // loop over firms for(int fX=0; fX<N; fX++) { // loop over multiple "slots" at the same worker for(int swX=0;swX<slotsProposers;swX++) { // loop over multiple slots at the same firm for(int sfX=0;sfX<slotsReviewers;sfX++) { // check if wX and fX would rather be matched with each other than with their actual matches if(reviewerUtils(wX, fX) > reviewerUtils(engagements(fX, sfX), fX) && proposerUtils(fX, wX) > proposerUtils(proposals(wX, swX), wX)) { ::Rf_warning("matching is not stable; worker %d would rather be matched to firm %d and vice versa.\n", wX, fX); return false; } } } } } return true; }
void addBias(mat &inputs) { mat inputBias = mat(1,1); inputBias(0,0) = 1.0; inputs.insert_rows(0, inputBias); }
void initialize_Matrices () { //assign matrices mat A_d_triple = {{1, dT, 0}, {0,1,0}, {dT, dT*dT/2, 1}}; mat tmp; blkdiag( A_d_triple, A_d_triple, tmp ); blkdiag( A_d_triple, tmp, A_d_all); mat B_one_state; B_one_state << dT*dT/2 <<endr << dT <<endr << dT*dT*dT/6 <<endr; blkdiag( B_one_state, B_one_state, tmp ); blkdiag( B_one_state, tmp, B_d); mat R(3,3); R.eye(); R = R*0.001; float a[] = {1,1,0.001,1,1,0.001,0.001,0.01,0.001}; mat Q_end, Q; bldMatrixDiag(a, Q_end, sizeof(a)/sizeof(*a)); float b[] = {1, 0.05, 0.01, 1, 0.05, 0.01, 0.01, 0.01, 0.01}; bldMatrixDiag(b, Q, sizeof(b)/sizeof(*b)); H.reset(); C.reset(); mat zero_mat, eye_mat; for (int i = 0; i < HORIZON; i ++) { // cout<<"i: "<<i<<endl; if(i != HORIZON - 1) { blkdiag(H, R, H); blkdiag(H, Q, H); } else { blkdiag(H, R, H); blkdiag(H, Q_end, H); } if ( i == 0) { // cout<<__LINE__<<endl; tmp.reset(); pushMatrix_row(tmp, -B_d); eye_mat.eye(9,9); pushMatrix_row(tmp, eye_mat); eye_mat.zeros(9, 12*(HORIZON - 1)); pushMatrix_row(tmp, eye_mat); C.insert_rows(C.n_rows, tmp); } else if (i == HORIZON - 1) { // cout<<__LINE__<<endl; tmp.reset(); zero_mat.zeros(9,3); pushMatrix_row(tmp, zero_mat); zero_mat.zeros(9, 12*((i + 1) - 2)); pushMatrix_row(tmp, zero_mat); pushMatrix_row(tmp, -A_d_all); pushMatrix_row(tmp, -B_d); // tmp.print("tmp at i = 1"); eye_mat.eye(9,9); pushMatrix_row(tmp, eye_mat); // tmp.print("tmp at i = 1"); C.insert_rows(C.n_rows, tmp); // pushMatrix_col(C, tmp); // C.print("C"); } else if (i == 1) { // cout<<__LINE__<<endl; tmp.reset(); zero_mat.zeros(9, 3); pushMatrix_row(tmp, zero_mat); pushMatrix_row(tmp, -A_d_all); pushMatrix_row(tmp, -B_d); eye_mat.eye(9,9); pushMatrix_row(tmp, eye_mat); zero_mat.zeros(9, 12 * (HORIZON - 2)); pushMatrix_row(tmp, zero_mat); // tmp.print("tmp at i = 1"); C.insert_rows(C.n_rows, tmp); } else { // cout<<__LINE__<<endl; tmp.reset(); zero_mat.zeros(9, 3); pushMatrix_row(tmp, zero_mat); zero_mat.zeros(9, ((i + 1) - 2)*12); pushMatrix_row(tmp, zero_mat); pushMatrix_row(tmp, -A_d_all); pushMatrix_row(tmp, -B_d); eye_mat.eye(9, 9); pushMatrix_row(tmp, eye_mat); zero_mat.zeros(9, 12*(HORIZON - (i + 1))); pushMatrix_row(tmp, zero_mat); C.insert_rows(C.n_rows, tmp); } } // cout << "C: " << C.n_rows << ", " <<C.n_cols << endl; }