////////////////////////////////////////////////////////////////////// // difference of two matrices ////////////////////////////////////////////////////////////////////// MATRIX operator-(const MATRIX& A, const MATRIX& B) { MATRIX result(A.rows(), A.cols()); for (int y = 0; y < A.cols(); y++) for (int x = 0; x < A.rows(); x++) result(x,y) = A(x,y) - B(x,y); return result; }
MATRIX *MATRIX::GetColumn(int j) { MATRIX *column = new MATRIX(length,1); for (int currRow=0;currRow<length;currRow++) column->Set(currRow,0,Get(currRow,j)); return( column ); }
MATRIX *MATRIX::GetRow(int i) { MATRIX *row = new MATRIX(1,width); for (int currColumn=0;currColumn<width;currColumn++) row->Set(0,currColumn,Get(i,currColumn)); return( row ); }
////////////////////////////////////////////////////////////////////// // Vector-matrix multiply ////////////////////////////////////////////////////////////////////// VECTOR operator*(VECTOR& x, MATRIX& A) { assert(A.rows() == x.size()); VECTOR y(A.cols()); for (int i = 0; i < A.cols(); i++) for (int j = 0; j < A.rows(); j++) y[i] += A(j, i) * x(j); return y; }
////////////////////////////////////////////////////////////////////// // Matrix-vector multiply ////////////////////////////////////////////////////////////////////// VECTOR operator*(const MATRIX& A, const VECTOR& x) { VECTOR y(A.rows()); for (int i = 0; i < A.rows(); i++) for (int j = 0; j < A.cols(); j++) y(i) += x(j) * A(i, j); return y; }
////////////////////////////////////////////////////////////////////// // Scale matrix ////////////////////////////////////////////////////////////////////// MATRIX operator*(float alpha, const MATRIX& A) { MATRIX y(A.rows(), A.cols()); for (int i = 0; i < A.rows(); i++) for (int j = 0; j < A.cols(); j++) y(i,j) = A(i, j) * alpha; return y; }
static MATRIX RotateZ(float a){ float cosinus=cosf(a); float sinus=sinf(a); MATRIX ret; ret.Identity(); ret.m[0][0]=cosinus; ret.m[1][1]=cosinus; ret.m[0][1]=-sinus; ret.m[1][0]=sinus; return ret; }
MATRIX *MATRIX::CountValues(int i1, int i2, int j1, int j2, int maxVal) { MATRIX *valueCount = new MATRIX(1,maxVal,0); for (int i=i1;i<i2;i++) for (int j=j1;j<j2;j++) valueCount->Add(0,int(Get(i,j)),1); return( valueCount ); }
/************* * DESCRIPTION: Draw a brush * INPUT: disp display class * stack matrix stack * OUTPUT: none *************/ void BRUSH_OBJECT::Draw(DISPLAY *disp,MATRIX_STACK *stack) { VECTOR size, trans; MATRIX m; switch(disp->display) { case DISPLAY_BBOX: case DISPLAY_WIRE: switch(brush->wrap) { case BRUSH_WRAP_FLAT: // draw a bounding box around pos disp->DrawBox(stack,&bboxmin,&bboxmax); break; case BRUSH_WRAP_X: SetVector(&size, (bboxmax.y - bboxmin.y) * .5f, bboxmax.x - bboxmin.x, (bboxmax.z - bboxmin.z) * .5f); m.SetRotZMatrix(90.f); stack->Push(&m); SetVector(&trans, 0.f, - size.y * .5f, 0.f); m.SetTransMatrix(&trans); stack->Push(&m); disp->DrawCylinder(stack, &size, flags); stack->Pop(&m); stack->Pop(&m); break; case BRUSH_WRAP_Y: SetVector(&size, (bboxmax.x - bboxmin.x) * .5f, bboxmax.y - bboxmin.y, (bboxmax.z - bboxmin.z) * .5f); SetVector(&trans, 0.f, - size.y * .5f, 0.f); m.SetTransMatrix(&trans); stack->Push(&m); disp->DrawCylinder(stack, &size, flags); stack->Pop(&m); break; case BRUSH_WRAP_XY: disp->DrawSphere(stack, (bboxmax.x-bboxmin.x)*.5f); break; } break; } }
int main(){ freopen("in.txt","r",stdin); int p,q; int n; MATRIX initial; initial.resize(2); for (int i=0;i<initial.size();++i){ initial[i].resize(2); } initial[1][0]=1; int kase; scanf("%d",&kase); for (int kk=1;kase--;++kk){ scanf("%d %d %d",&p,&q,&n); printf("Case %d: ",kk); if ( n==0 ){ printf("2\n"); }else if ( n==1 ){ printf("%d\n",p); }else if ( n==2 ){ printf("%llu\n",LLL(p)*p-2*q); } else{ initial[0][0]=p; initial[0][1]= -q; MATRIX result=powerMatrix( initial ,n-2 ); LLL base1=p; LLL base2=LLL(p)*p-2*LLL(q); LLL rr=result[0][0]*(base2/2) +result[0][1]*(base1/2) ; rr+=result[0][0]*(base2/2) +result[0][1]*(base1/2) ; if ( base2 & 1 ){ rr+=result[0][0]; } if ( base1 & 1 ){ rr+=result[0][1]; } //printMatrix(result); printf("%llu\n",LLL(rr) ); } } return 0; }
MATRIX MATRIX::operator*(MATRIX r){ MATRIX ret; ret.Zero(); for(int i=0;i<4;i++){ for(int j=0;j<4;j++){ ret.m[i][j]+=m[0][j]*r.m[i][0]; ret.m[i][j]+=m[1][j]*r.m[i][1]; ret.m[i][j]+=m[2][j]*r.m[i][2]; ret.m[i][j]+=m[3][j]*r.m[i][3]; } } return ret; }
template<class T_SIMPLICIAL_OBJECT> typename T_SIMPLICIAL_OBJECT::SCALAR Filled_Volume_Helper(const T_SIMPLICIAL_OBJECT& object, typename ENABLE_IF<(T_SIMPLICIAL_OBJECT::MESH::dimension==(T_SIMPLICIAL_OBJECT::VECTOR_T::m-1)),UNUSABLE>::TYPE unused=UNUSABLE()) { typedef typename T_SIMPLICIAL_OBJECT::SCALAR T;typedef typename T_SIMPLICIAL_OBJECT::VECTOR_T TV;//enum WORKAROUND{d=T_SIMPLICIAL_OBJECT::MESH::dimension}; static const int d=T_SIMPLICIAL_OBJECT::MESH::dimension; if(d!=TV::m-1) PHYSBAM_FATAL_ERROR("only codimension 1 objects can be filled"); const TV base=object.particles.X(object.mesh.elements(1)[1]); T scaled_volume=0; // (d+1)!*volume for(int t=1;t<=object.mesh.elements.m;t++){const VECTOR<int,d+1>& nodes=object.mesh.elements(t); MATRIX<T,TV::m,d+1> DX;for(int i=1;i<=nodes.m;i++) DX.Column(i)=object.particles.X(nodes[i])-base; scaled_volume+=DX.Parallelepiped_Measure();} return (T)1/FACTORIAL<d+1>::value*scaled_volume; }
void test2() { MATRIX a = MATRIX::Random(); for (int count = 0; count < 5; ++count) { clock_t start = clock(); for (int j = 0; j < 5000; ++j) { a.Transposed().Translated(1, 2, 3).Transposed().Translated(3, 2, 1).Transposed().Translated(8, 9, 10) .Transposed().Translated(10, 9, 8).Transposed().Translated(4, 5, 6).Transposed().Translated(7, 9, 8); } clock_t finish = clock(); std::cout << finish - start << " milliseconds" << std::endl; } std::cout << std::endl; }
void MATRIX::Target(VECTOR v,VECTOR c,float a){ VECTOR u(0,1,0); VECTOR z=v-c;z.Normalize(); VECTOR y=u-z*Dot(u,z);y.Normalize(); VECTOR x=Cross(y,z); MATRIX swp; swp.Identity(); swp.m[0][0]=x.x,swp.m[0][1]=y.x,swp.m[0][2]=z.x; swp.m[1][0]=x.y,swp.m[1][1]=y.y,swp.m[1][2]=z.y; swp.m[2][0]=x.z,swp.m[2][1]=y.z,swp.m[2][2]=z.z; swp.m[3][0]=-Dot(c,x); swp.m[3][1]=-Dot(c,y); swp.m[3][2]=-Dot(c,z); *this=RotateZ(-a)*swp; }
MATRIX multiply(MATRIX &a, MATRIX &b) { MATRIX res; res.resize( a.size() ); for (int i=0;i<res.size();++i){ res[i].resize( b[0].size() ); } for (int i = 0; i < a.size(); i++) for (int j = 0; j < b[i].size(); j++) for (int k = 0; k < a[i].size() ; k++) //col of A and row of B must be same which is equal to a[i].size() res[i][j] = ( res[i][j] + ( ( (a[i][k] % mod ) * (b[k][j] % mod))) % mod ) %mod; return res; }
void processMatrix(MATRIX & mat, int match, FuncType &processor) { set<int> row; set<int> col; for (int i = 0; i < mat.size(); ++i) { for (int j = 0; j < mat[i].size(); ++j) { if (mat[i][j] == match) { row.insert(i); col.insert(j); } } } // Process matrix for (int i : row) { processor(mat, i, match, ROW); } for (int i : col) { processor(mat, i, match, COL); } }
bool is_symmetric(const MATRIX &n) { unsigned int x, y; for (y = 0; y < n.GetHeight(); ++y) { for (x = 0; x < n.GetWidth(); ++x) { if (*n.Get(y, x) != *n.Get(x, y)) { return false; } } } return true; }
void cholesky_invert (MATRIX &M) { using namespace ublas; typedef typename MATRIX::size_type size_type; typedef typename MATRIX::value_type value_type; size_type size = M.size1(); // determine the inverse of the lower traingular matrix for (size_type i = 0; i < size; ++ i) { M(i,i) = 1 / M(i,i); for (size_type j = i+1; j < size; ++ j) { value_type elem(0); for (size_type k = i; k < j; ++ k) { elem -= M(j,k)*M(k,i); } M(j,i) = elem / M(j,j); } } // multiply the upper and lower inverses together M = prod(trans(triangular_adaptor<MATRIX,lower>(M)), triangular_adaptor<MATRIX,lower>(M)); }
void MatrixApplier::concat(const MATRIX& m) { float matrix[16]; m.get4DMatrix(matrix); lsglMultMatrixf(matrix); rt->setMatrixUniform(LSGL_MODELVIEW); }
/* * function members */ explicit TSP(const MATRIX &_costs) : MATRIX(_costs), current_tour(_costs.size()), best_tour(_costs.size()), epsilon(0.000001), n(_costs.size()), updatecalls(0), swap_count(0), slide_count(0), reverse_count(0), improve_count(0) { pgassert(n == MATRIX::size()); bestCost = MATRIX::tourCost(best_tour); current_cost = MATRIX::tourCost(current_tour); pgassert(bestCost == current_cost); }
size_t cholesky_decompose(MATRIX& A) { using namespace ublas; const MATRIX& A_c(A); const size_t n = A.size1(); for (size_t k=0 ; k < n; k++) { double qL_kk = A_c(k,k) - inner_prod( project( row(A_c, k), range(0, k) ), project( row(A_c, k), range(0, k) ) ); if (qL_kk <= 0) { return 1 + k; } else { double L_kk = sqrt( qL_kk ); matrix_column<MATRIX> cLk(A, k); project( cLk, range(k+1, n) ) = ( project( column(A_c, k), range(k+1, n) ) - prod( project(A_c, range(k+1, n), range(0, k)), project(row(A_c, k), range(0, k) ) ) ) / L_kk; A(k,k) = L_kk; } } return 0; }
/************* * DESCRIPTION: Push a matrix * INPUT: m matrix * OUTPUT: TRUE if ok else FALSE *************/ BOOL INVMATRIX_STACK::Push(MATRIX *m) { STACK_ITEM *si; MATRIX mat; si = new STACK_ITEM; if(!si) return FALSE; si->m = *m; si->Insert((DLIST**)&root); mat.MultMat(&matrix,m); matrix = mat; return TRUE; }
// Makes and returns a deep copy of *this, including all the BLOB_CHOICEs // on the lists, but not any LanguageModelState that may be attached to the // BLOB_CHOICEs. MATRIX* MATRIX::DeepCopy() const { int dim = dimension(); int band_width = bandwidth(); MATRIX* result = new MATRIX(dim, band_width); for (int col = 0; col < dim; ++col) { for (int row = col; row < dim && row < col + band_width; ++row) { BLOB_CHOICE_LIST* choices = get(col, row); if (choices != NULL) { BLOB_CHOICE_LIST* copy_choices = new BLOB_CHOICE_LIST; copy_choices->deep_copy(choices, &BLOB_CHOICE::deep_copy); result->put(col, row, copy_choices); } } } return result; }
/************* * DESCRIPTION: Calculate with three angles three vectors such that all are * mutually perpendicular. The vectors are normalized * INPUT: align alignment * orient result vectors * OUTPUT: none *************/ void CalcOrient(VECTOR *align, VECTOR *orient_x, VECTOR *orient_y, VECTOR *orient_z) { MATRIX matrix; matrix.SetRotMatrix(align); // x-orientation SetVector(orient_x, 1.f, 0.f, 0.f); matrix.MultVectMat(orient_x); // y-orientation SetVector(orient_y, 0.f, 1.f, 0.f); matrix.MultVectMat(orient_y); // z-orientation SetVector(orient_z, 0.f, 0.f, 1.f); matrix.MultVectMat(orient_z); }
/************* * DESCRIPTION: Push a matrix at the end of the stack * INPUT: m matrix * OUTPUT: TRUE if ok else FALSE *************/ BOOL MATRIX_STACK::PushEnd(MATRIX *m) { STACK_ITEM *si; MATRIX tmp; si = new STACK_ITEM; if(!si) return FALSE; si->m = *m; si->Append((DLIST**)&root); si->matrix = *m; tmp.MultMat(&matrix, m); matrix = tmp; return TRUE; }
void printMatrix(MATRIX mat){ for (int i=0;i<mat.size();++i){ for (int j=0;j<mat[i].size();++j){ printf("%3lld",mat[i][j]); } printf("\n"); } printf("\n"); }
void Wordrec::ProcessSegSearchPainPoint( float pain_point_priority, const MATRIX_COORD &pain_point, const char* pain_point_type, GenericVector<SegSearchPending>* pending, WERD_RES *word_res, LMPainPoints *pain_points, BlamerBundle *blamer_bundle) { if (segsearch_debug_level > 0) { tprintf("Classifying pain point %s priority=%.4f, col=%d, row=%d\n", pain_point_type, pain_point_priority, pain_point.col, pain_point.row); } ASSERT_HOST(pain_points != NULL); MATRIX *ratings = word_res->ratings; // Classify blob [pain_point.col pain_point.row] if (!pain_point.Valid(*ratings)) { ratings->IncreaseBandSize(pain_point.row + 1 - pain_point.col); } ASSERT_HOST(pain_point.Valid(*ratings)); BLOB_CHOICE_LIST *classified = classify_piece(word_res->seam_array, pain_point.col, pain_point.row, pain_point_type, word_res->chopped_word, blamer_bundle); BLOB_CHOICE_LIST *lst = ratings->get(pain_point.col, pain_point.row); if (lst == NULL) { ratings->put(pain_point.col, pain_point.row, classified); } else { // We can not delete old BLOB_CHOICEs, since they might contain // ViterbiStateEntries that are parents of other "active" entries. // Thus if the matrix cell already contains classifications we add // the new ones to the beginning of the list. BLOB_CHOICE_IT it(lst); it.add_list_before(classified); delete classified; // safe to delete, since empty after add_list_before() classified = NULL; } if (segsearch_debug_level > 0) { print_ratings_list("Updated ratings matrix with a new entry:", ratings->get(pain_point.col, pain_point.row), getDict().getUnicharset()); ratings->print(getDict().getUnicharset()); } // Insert initial "pain points" to join the newly classified blob // with its left and right neighbors. if (classified != NULL && !classified->empty()) { if (pain_point.col > 0) { pain_points->GeneratePainPoint( pain_point.col - 1, pain_point.row, LM_PPTYPE_SHAPE, 0.0, true, segsearch_max_char_wh_ratio, word_res); } if (pain_point.row + 1 < ratings->dimension()) { pain_points->GeneratePainPoint( pain_point.col, pain_point.row + 1, LM_PPTYPE_SHAPE, 0.0, true, segsearch_max_char_wh_ratio, word_res); } } (*pending)[pain_point.col].SetBlobClassified(pain_point.row); }
/************* * DESCRIPTION: Pop a matrix * INPUT: - * OUTPUT: - *************/ void INVMATRIX_STACK::Pop() { STACK_ITEM *si; MATRIX mat; if(!root) return; root->Remove((DLIST**)&root); matrix.IdentityMatrix(); si = root; while(si) { mat.MultMat(&si->m,&matrix); matrix = mat; si = (STACK_ITEM*)si->GetNext(); } }
int main(){ //freopen("in.txt","r",stdin); int n,m; MATRIX initial; initial.resize(DIM); for (int i=0;i<initial.size();++i){ initial[i].resize(DIM); } for (int i=0;i<DIM;++i){ for (int j=0;j<DIM;++j){ initial[i][j]=1; } } initial[1][1]=0; int kase; LLL x,y; scanf("%d",&kase); for (int kk=1;kase--;++kk){ scanf("%d %d %d %d",&x,&y,&n,&m); mod=LLL ( pow(10,m)+1e-6 ); printf("Case %d: ",kk); if( n==0 ){ printf("%d\n",x%mod); continue; }else if ( n==1 ){ printf("%d\n",y%mod); continue; } MATRIX result=powerMatrix( initial , n-1 ); printf("%d\n",( result[0][0]*y+result[0][1]*x ) %mod); } return 0; }
void testMotionExtract (void) { MATRIX AffineMatrix (3,2); MATRIX_CONTENT_TYPE** AffineData = AffineMatrix.getDataPtr (); double theta = 3.14 / 6; cout << "theta: " << theta << endl; // row 1 AffineData[0][0] = cos (theta); AffineData[1][0] = sin (theta); AffineData[2][0] = 1; // row 2 AffineData[0][1] = - sin (theta); AffineData[1][1] = cos (theta); AffineData[2][1] = 2; showMotion (AffineMatrix); }