IntegratorExport* createDiagonallyIRK4Export( UserInteraction* _userInteraction, const std::string &_commonHeaderName) { const double alpha = 1.137158042603258; DMatrix AA(3,3); DVector bb(3); DVector cc(3); AA(0,0) = (1.0+alpha)/2.0; AA(0,1) = 0.0; AA(0,2) = 0.0; AA(1,0) = -alpha/2.0; AA(1,1) = (1.0+alpha)/2.0; AA(1,2) = 0.0; AA(2,0) = 1.0+alpha; AA(2,1) = -(1.0+2.0*alpha); AA(2,2) = (1.0+alpha)/2.0; bb(0) = 1.0/(6.0*alpha*alpha); bb(1) = 1.0-1.0/(3.0*alpha*alpha); bb(2) = 1.0/(6.0*alpha*alpha); cc(0) = (1.0+alpha)/2.0; cc(1) = 1.0/2.0; cc(2) = (1.0-alpha)/2.0; DiagonallyImplicitRKExport* integrator = createDiagonallyImplicitRKExport(_userInteraction, _commonHeaderName); integrator->initializeButcherTableau(AA, bb, cc); return integrator; }
void CCBuffer::skipData(unsigned int u_len) { BEGIN_IF(LXE(CA(_u_read_pos, u_len), _u_content_size)) AA(_u_read_pos, u_len); BEGIN_ELSE AA(_u_read_pos, (CS(_u_content_size, _u_read_pos))); END_IF }
void CCBuffer::readData(char* p_out_data, unsigned int u_len) { BEGIN_IF(isReadable(u_len)) memcpy(p_out_data, CA(_p_buffer, _u_read_pos), u_len); AA(_u_read_pos, u_len); BEGIN_ELSE_IF(LQ(LDE(CS(_u_content_size, _u_read_pos),0), LNE(u_len, 0))) DO_ASSIGN(u_len, CS(_u_content_size, _u_read_pos)); memcpy(p_out_data, CA(_p_buffer, _u_read_pos), u_len); AA(_u_read_pos, u_len); END_IF }
void CCBuffer::moveRight(unsigned int u_len) { BEGIN_IF(LE(_u_content_size, 0)) DO_RETURN; END_IF _reallocBufferSizeInChanged(u_len); BEGIN_FOR(DO_ASSIGN(int i, CS(_u_content_size, 1)), LDE(i, 0), SSI(i)) DO_ASSIGN(QV(CA(CA(_p_buffer, i), TO_INT(u_len))), QV(CA(_p_buffer, i))); DO_ASSIGN(QV(CA(_p_buffer, i)), 0); END_FOR AA(_u_write_pos, u_len); AA(_u_read_pos, u_len); AA(_u_mark_pos, u_len); AA(_u_content_size, u_len); }
KOKKOS_INLINE_FUNCTION int Gemm<Trans::ConjTranspose,Trans::NoTranspose, AlgoGemm::SparseSparseSuperNodes,Variant::One> ::invoke(PolicyType &policy, MemberType &member, const ScalarType alpha, CrsExecViewTypeA &A, CrsExecViewTypeB &B, const ScalarType beta, CrsExecViewTypeC &C) { if (member.team_rank() == 0) { DenseMatrixView<typename CrsExecViewTypeA::flat_mat_base_type> AA(A.Flat()); DenseMatrixView<typename CrsExecViewTypeA::flat_mat_base_type> BB(B.Flat()); DenseMatrixView<typename CrsExecViewTypeA::flat_mat_base_type> CC(C.Flat()); Gemm<Trans::ConjTranspose,Trans::NoTranspose, AlgoGemm::ExternalBlas,Variant::One> ::invoke(policy, member, alpha, AA, BB, beta, CC); } return 0; }
int main(int argc, char* argv[]) { char tipo1, tipo2; int numeroV, numeroA, vertice1, vertice2, peso = 1, i = 0, j = 0; tipo1 = 'D'; tipo2 = 'M'; scanf("%d %d", &numeroV, &numeroA); createGraph(G, tipo1, tipo2, numeroV, numeroA); //IG(G); for(i = 0; i < numeroA; i++) { scanf("%d %d", &vertice1, &vertice2); AA(G, vertice1, vertice2, peso); } int origem, destino; /*for(i = 0; i < numeroA; i++) { scanf("%d %d", &origem, &destino); }*/ while(scanf("%d %d", &origem, &destino) != EOF) { path(G, origem, destino); printf("%d %d\n", origem, destino); } return 0; }
returnValue ExplicitRungeKutta2Export::initializeButcherTableau() { AA = Matrix(2,2); bb = Vector(2); cc = Vector(2); AA(0,0) = 0.0; AA(0,1) = 0.0; AA(1,0) = 1.0/2.0; AA(1,1) = 0.0; bb(0) = 0.0; bb(1) = 1.0; cc(0) = 0.0; cc(1) = 1.0/2.0; return SUCCESSFUL_RETURN; }
KOKKOS_INLINE_FUNCTION int Herk<Uplo::Upper,Trans::ConjTranspose, AlgoHerk::SparseSparseSuperNodesByBlocks,Variant::One> ::invoke(PolicyType &policy, MemberType &member, const ScalarType alpha, CrsExecViewTypeA &A, const ScalarType beta, CrsExecViewTypeC &C) { if (member.team_rank() == 0) { DenseMatrixView<typename CrsExecViewTypeA::hier_mat_base_type> AA(A.Hier()); DenseMatrixView<typename CrsExecViewTypeA::hier_mat_base_type> CC(C.Hier()); Herk<Uplo::Upper,Trans::ConjTranspose, AlgoHerk::DenseByBlocks,Variant::One> ::invoke(policy, member, alpha, AA, beta, CC); } return 0; }
/* SUBGOAL connect-to ?grasper ?object1 ?object2 */ void PA_ConnectTo(Context *cx, Subgoal *sg, Ts *ts, Obj *a, Obj *o) { Dur d; Dbg(DBGPLAN, DBGOK, "PA_Connect", E); switch (sg->state) { case STBEGIN: if (!DbIsPartOf(ts, NULL, I(o,1), a)) { Dbg(DBGPLAN, DBGDETAIL, "failure: grasper not part of actor", E); goto failure; } SG(cx, sg, 1, STFAILURE, L(N("grasp"), I(o,1), I(o,2), E)); return; case 1: SG(cx, sg, 2, STFAILURE, L(N("near-graspable"), I(o,1), I(o,3), E)); return; case 2: d = DurationOf(I(o, 0)); AA(ts, d, o); TsIncrement(ts, d); TOSTATE(cx, sg, 3); return; case 3: /* todo: Perhaps merge release into this type of connect? */ SG(cx, sg, 4, STFAILURE, L(N("release"), I(o,1), I(o,2), E)); return; case 4: AS(ts, 0, L(N("connected-to"), I(o,2), I(o,3), E)); TOSTATE(cx, sg, STSUCCESS); return; default: Dbg(DBGPLAN, DBGBAD, "PA_Connect: undefined state %d", sg->state); } failure: TOSTATE(cx, sg, STFAILURE); }
/* Initializes variables to specified size */ void gen_test_problem(real** A, real ** B, real ** C, int const m, int const n) { real * a; real * b; real * c; a = (real*) malloc(n*m*sizeof(real)); b = (real*) malloc(m*sizeof(real)); c = (real*) malloc(n*sizeof(real)); for(int idx = 0; idx < n; ++idx) { for(int idy = 0; idy < m; ++idy) { AA(idy,idx)=1; } } for(int idx = 0; idx < m; ++idx) { b[idx]=1; } for(int idx = 0; idx < n; ++idx) { c[idx]=1; } *A = a; *B = b; *C = c; }
/* SUBGOAL pour-onto ?grasper ?holding ?onto */ void PA_PourOnto(Context *cx, Subgoal *sg, Ts *ts, Obj *a, Obj *o) { Dur d; Dbg(DBGPLAN, DBGOK, "PA_PourOnto", E); switch (sg->state) { case STBEGIN: if (!DbIsPartOf(ts, NULL, I(o,1), a)) { Dbg(DBGPLAN, DBGDETAIL, "failure: grasper not part of actor", E); goto failure; } SG(cx, sg, 2, STFAILURE, L(N("holding"), I(o,1), I(o,2), E)); return; case 2: SG(cx, sg, 3, STFAILURE, L(N("near-graspable"), I(o,1), I(o,3), E)); return; case 3: d = DurationOf(I(o, 0)); AA(ts, d, o); TsIncrement(ts, d); TOSTATE(cx, sg, STSUCCESS); return; default: Dbg(DBGPLAN, DBGBAD, "PA_PourOnto: undefined state %d", sg->state); } failure: TOSTATE(cx, sg, STFAILURE); }
/* SUBGOAL action-close ?grasper ?object */ void PA_ActionClose(Context *cx, Subgoal *sg, Ts *ts, Obj *a, Obj *o) { Dur d; Dbg(DBGPLAN, DBGOK, "PA_ActionClose", E); switch (sg->state) { case STBEGIN: if (!DbIsPartOf(ts, NULL, I(o,1), a)) { Dbg(DBGPLAN, DBGDETAIL, "failure: grasper not part of actor", E); goto failure; } /* todo: Cannot already be holding too much. */ SG(cx, sg, 1, STFAILURE, L(N("near-graspable"), I(o,1), I(o,2), E)); return; case 1: d = DurationOf(I(o, 0)); /* Depends on object. */ AA(ts, d, o); TsIncrement(ts, d); if (!TE(ts, L(N("open"), I(o,2), E))) { Dbg(DBGPLAN, DBGBAD, "object not already open", E); } AS(ts, 0, L(N("closed"), I(o,2), E)); TOSTATE(cx, sg, STSUCCESS); return; default: Dbg(DBGPLAN, DBGBAD, "PA_ActionClose: undefined state %d", sg->state); } failure: TOSTATE(cx, sg, STFAILURE); }
returnValue DiagonallyIRK3Export::initializeButcherTableau() { AA = Matrix(numStages,numStages); bb = Vector(numStages); cc = Vector(numStages); AA(0,0) = 1.0/2.0+1.0/(2.0*sqrt(3.0)); AA(0,1) = 0.0; AA(1,0) = -1.0/sqrt(3.0); AA(1,1) = 1.0/2.0+1.0/(2.0*sqrt(3.0)); bb(0) = 1.0/2.0; bb(1) = 1.0/2.0; cc(0) = 1.0/2.0+1.0/(2.0*sqrt(3.0)); cc(1) = 1.0/2.0-1.0/(2.0*sqrt(3.0)); return SUCCESSFUL_RETURN; }
void CCBuffer::writeData(const char* p_data, unsigned int u_len) { DO_ASSERT(LQ(p_data, LD(u_len,0)), "LQ(p_data, LD(u_len,0))"); _reallocBufferSizeInChanged(u_len); memcpy(CA(_p_buffer, _u_write_pos), p_data, u_len); AA(_u_write_pos, u_len); MAXA(_u_content_size, _u_write_pos); }
returnValue RadauIIA3Export::initializeButcherTableau() { AA = Matrix(2,2); bb = Vector(2); cc = Vector(2); AA(0,0) = 5/(double)12; AA(0,1) = -1/(double)12; AA(1,0) = 3/(double)4; AA(1,1) = 1/(double)4; bb(0) = 3/(double)4; bb(1) = 1/(double)4; cc(0) = 1/(double)3; cc(1) = 1; return SUCCESSFUL_RETURN; }
IntegratorExport* createGaussLegendre2Export( UserInteraction* _userInteraction, const std::string &_commonHeaderName) { DMatrix AA(1,1); DVector bb(1); DVector cc(1); AA(0,0) = 1.0/2.0; bb(0) = 1.0; cc(0) = 1.0/2.0; ImplicitRungeKuttaExport* integrator = createImplicitRungeKuttaExport(_userInteraction, _commonHeaderName); integrator->initializeButcherTableau(AA, bb, cc); return integrator; }
static PetscErrorCode RHSJacobian_function( TS ts, double t_, Vec u, Mat A, Mat B, void* G_u ) { Vector U( u, Vector::owner::other ); Matrix AA( A, false ); Matrix BB( B, false ); TimeStepper T( ts, false ); ( *(Jac*)G_u )( U, AA, BB, T, t_ ); return 0; }
int simEmbGetRotationAxis(const float* quaternionStart,const float* quaternionGoal,float* axis,float* angle) { if (!hasLaunched()) return(-1); // V-REP quaternion, internally: w x y z // V-REP quaternion, at interfaces: x y z w (like ROS) C4Vector qStart; qStart(0)=quaternionStart[3]; qStart(1)=quaternionStart[0]; qStart(2)=quaternionStart[1]; qStart(3)=quaternionStart[2]; C4Vector qGoal; qGoal(0)=quaternionGoal[3]; qGoal(1)=quaternionGoal[0]; qGoal(2)=quaternionGoal[1]; qGoal(3)=quaternionGoal[2]; // Following few lines taken from the quaternion interpolation part: C4Vector AA(qStart); C4Vector BB(qGoal); if (AA(0)*BB(0)+AA(1)*BB(1)+AA(2)*BB(2)+AA(3)*BB(3)<0.0f) AA=AA*-1.0f; C4Vector r((AA.getInverse()*BB).getAngleAndAxis()); C3Vector v(r(1),r(2),r(3)); v=AA*v; axis[0]=v(0); axis[1]=v(1); axis[2]=v(2); float l=sqrt(v(0)*v(0)+v(1)*v(1)+v(2)*v(2)); if (l!=0.0f) { axis[0]/=l; axis[1]/=l; axis[2]/=l; } angle[0]=r(0); return(1); }
/** * AA1: * "&&" B AA2 * AA2.st = AA1.st || B.val * AA1.val = AA2.val * AA: * empty * AA.val = AA.st */ int AA(int st){ if (lex(0) == '&'){ lex(1); if (lex(0) == '&'){ lex(1); return AA(st && B()); } } else { return st; } }
IntegratorExport* createGaussLegendre4Export( UserInteraction* _userInteraction, const String &_commonHeaderName) { Matrix AA(2,2); Vector bb(2); Vector cc(2); AA(0,0) = 1.0/4.0; AA(0,1) = (1.0/4.0+sqrt(3.0)/6.0); AA(1,0) = (1.0/4.0-sqrt(3.0)/6.0); AA(1,1) = 1.0/4.0; bb(0) = 1.0/2.0; bb(1) = 1.0/2.0; cc(0) = 1.0/2.0+sqrt(3.0)/6.0; cc(1) = 1.0/2.0-sqrt(3.0)/6.0; ImplicitRungeKuttaExport* integrator = createImplicitRungeKuttaExport(_userInteraction, _commonHeaderName); integrator->initializeButcherTableau(AA, bb, cc); return integrator; }
IntegratorExport* createDiagonallyIRK3Export( UserInteraction* _userInteraction, const String &_commonHeaderName) { Matrix AA(2,2); Vector bb(2); Vector cc(2); AA(0,0) = 1.0/2.0+1.0/(2.0*sqrt(3.0)); AA(0,1) = 0.0; AA(1,0) = -1.0/sqrt(3.0); AA(1,1) = 1.0/2.0+1.0/(2.0*sqrt(3.0)); bb(0) = 1.0/2.0; bb(1) = 1.0/2.0; cc(0) = 1.0/2.0+1.0/(2.0*sqrt(3.0)); cc(1) = 1.0/2.0-1.0/(2.0*sqrt(3.0)); DiagonallyImplicitRKExport* integrator = createDiagonallyImplicitRKExport(_userInteraction, _commonHeaderName); integrator->initializeButcherTableau(AA, bb, cc); return integrator; }
/* Convert back to max c^{T}x s.t. Ax \leq{} b */ void from_standard_form(real * const a, real const *const b, real const * const c, int const m, int const n) { /* Scale up by b */ for (int jj = 0; jj < n; ++jj) { for(int ii = 0; ii < m; ++ii) { AA(ii,jj) = AA(ii,jj) * b[ii]; } } /* Scale up by c */ for (int jj = 0; jj < n; ++jj) { for(int ii = 0; ii < m; ++ii) { AA(ii,jj) = AA(ii,jj) * c[jj]; } } }
int main(int, char**) { typedef mtl::dense2D<double> Matrix; typedef mtl::dense_vector<double> Vector; Matrix A(4, 4), L(4, 4), U(4, 4), AA(4, 4); Vector v(4); double c=1.0; for (unsigned i= 0; i < 4; i++) for(unsigned j= 0; j < 4; j++) { U[i][j]= i <= j ? c * (i+j+2) : (0); L[i][j]= i > j ? c * (i+j+1) : (i == j ? (1) : (0)); } std::cout << "L is:\n" << L << "U is:\n" << U; A= L * U; std::cout << "A is:\n" << A; AA= adjoint(A); for (unsigned i= 0; i < 4; i++) v[i]= double(i); Vector b( A*v ), b2( adjoint(A)*v ); Matrix LU(A); lu(LU); std::cout << "LU decomposition of A is:\n" << LU; Matrix B( lu_f(A) ); std::cout << "LU decomposition of A (as function result) is:\n" << B; Vector v1( lu_solve_straight(A, b) ); std::cout << "v1 is " << v1 << "\n"; Vector v2( lu_solve(A, b) ); std::cout << "v2 is " << v2 << "\n"; mtl::dense_vector<unsigned> P; lu(A, P); std::cout << "LU with pivoting is \n" << with_format(A, 5, 2) << "Permutation is " << P << "\n"; Vector v3( lu_apply(A, P, b) ); std::cout << "v3 is " << v3 << "\n"; Vector v4(lu_adjoint_apply(A, P, b2)); std::cout << "v4 is " << v4 << "\n"; Vector v5(lu_adjoint_solve(AA, b)); std::cout << "v5 is " << v5 << "\n"; return 0; }
/** \ingroup matop Matrix exponential. The matrix exponential is calculated using the Pade approximation adapted from Moler, Cleve; Van Loan, Charles F. (2003), "Nineteen Dubious Ways to Compute the Exponential of a Matrix, Twenty-Five Years Later" The main use of the matrix exponential is to solve linear ordinary differential equation (ODE) systems: \f[ \frac{d}{dt}y(t) = Ay(t)\ , \ \mbox{with } y(0) = y_0 \f] \item then the solution becomes \f[ y(t) = e^{At}y_0 \f] \param A square df1b2matrix \returns The matrix exponential of A */ df1b2matrix expm(const df1b2matrix & A) { RETURN_ARRAYS_INCREMENT(); int rmin = A.rowmin(); int rmax = A.rowmax(); if(rmax != A.colmax()) {cout<<"Error: Not square matrix in expm."<<endl; ad_exit(1);} if(rmin != A.colmin()) {cout<<"Error: Not square matrix in expm."<<endl; ad_exit(1);} df1b2matrix I(rmin,rmax,rmin,rmax); df1b2matrix AA(rmin,rmax,rmin,rmax); df1b2matrix X(rmin,rmax,rmin,rmax); df1b2matrix E(rmin,rmax,rmin,rmax); df1b2matrix D(rmin,rmax,rmin,rmax); df1b2matrix cX(rmin,rmax,rmin,rmax); I.initialize(); for(int i = rmin; i<=rmax; ++i){I(i,i) = 1.0;} df1b2variable log2NormInf; log2NormInf = log(max(rowsum(fabs(value(A))))); log2NormInf/=log(2.0); int e = (int)value(log2NormInf) + 1; int s = e+1; s = (s<0) ? 0 : s; AA = 1.0/pow(2.0,s)*A; X = AA; df1b2variable c = 0.5; E = I+c*AA; D = I-c*AA; int q = 6, p = 1; for(int k = 2; k<=q; ++k){ c*=((double)q-k+1.0)/((double)k*(2*q-k+1)); X = AA*X; cX = c*X; E+=cX; if(p==1){D+=cX;}else{D-=cX;} p = (p==1) ? 0 : 1; } // E = inv(D)*E; E = solve(D,E); for(int k = 1; k<=s; ++k){ E = E*E; } RETURN_ARRAYS_DECREMENT(); return E; }
int Quad4FiberOverlay::getEltBb(double Xi, double Eta) { Matrix B(SL_NUM_NDF,SL_NUM_NODE); B.Zero(); Bb.Zero(); this->UpdateBase(Xi,Eta); this->Dual(); for(int i=0; i<4; i++) { for(int j =0; j<2;j++){ B(j,i) = dNidxAlphai(i,0)*dualg1(j) + dNidxAlphai(i,1)*dualg2(j); } //B(0,i) = dNidxAlphai(i,0)*dualg1(0) + dNidxAlphai(i,1)*dualg1(1); //B(1,i) = dNidxAlphai(i,0)*dualg2(0) + dNidxAlphai(i,1)*dualg2(1); //opserr << "B = " << B(0,i) << B(1,i) << endln; } for(int i =0; i<4;i++) { Bb[(i+1)*2-2] = AA(0)*B(0,i)+AA(2)*B(1,i); Bb[(i+1)*2-1] = AA(1)*B(1,i)+AA(2)*B(0,i); } return 0; }
inline Stat Herk<Uplo::Upper,Trans::ConjTranspose, AlgoHerk::SparseSparseSuperNodesByBlocks,Variant::One> ::stat(const ScalarType alpha, CrsExecViewTypeA &A, const ScalarType beta, CrsExecViewTypeC &C) { DenseMatrixView<typename CrsExecViewTypeA::hier_mat_base_type> AA(A.Hier()); DenseMatrixView<typename CrsExecViewTypeA::hier_mat_base_type> CC(C.Hier()); return Herk<Uplo::Upper,Trans::ConjTranspose, AlgoHerk::DenseByBlocks,Variant::One> ::stat(alpha, AA, beta, CC); }
IntegratorExport* createExplicitRungeKutta3Export( UserInteraction* _userInteraction, const std::string &_commonHeaderName) { DMatrix AA(3,3); DVector bb(3); DVector cc(3); AA(0,0) = 0.0; AA(0,1) = 0.0; AA(0,2) = 0.0; AA(1,0) = 1.0/3.0; AA(1,1) = 0.0; AA(1,2) = 0.0; AA(2,0) = 0.0; AA(2,1) = 2.0/3.0; AA(2,2) = 0.0; bb(0) = 1.0/4.0; bb(1) = 0.0; bb(2) = 3.0/4.0; cc(0) = 0.0; cc(1) = 1.0/3.0; cc(2) = 2.0/3.0; ExplicitRungeKuttaExport* integrator = createExplicitRungeKuttaExport(_userInteraction, _commonHeaderName); integrator->initializeButcherTableau(AA, bb, cc); return integrator; }
//----------------------------------------------------------------------------- bool matrix::lsq_solve(vector<double>& x, vector<double>& b) { if ((int) x.size() != m_nc) return false; if ((int) b.size() != m_nr) return false; vector<double> y(m_nc); mult_transpose(b, y); matrix AA(m_nc, m_nc); mult_transpose_self(AA); AA.solve(x, y); return true; }
string getHint(string secret, string guess) { int A = 0 , B = 0 , n = (int)secret.size (); vector <int> AA (10 , 0) , BB (10 , 0); for (int i = 0 ; i < n ; ++ i) { if (secret[i] == guess[i]) A ++; else { AA[secret[i] - '0'] ++; BB[guess[i] - '0'] ++; } } for (int i = 0 ; i < 10 ; i ++) { B += min (AA[i] , BB[i]); } char str[100]; sprintf (str , "%dA%dB" , A , B); return string (str); }
inline Stat Gemm<Trans::ConjTranspose,Trans::NoTranspose, AlgoGemm::SparseSparseSuperNodes,Variant::One> ::stat(const ScalarType alpha, CrsExecViewTypeA &A, CrsExecViewTypeB &B, const ScalarType beta, CrsExecViewTypeC &C) { DenseMatrixView<typename CrsExecViewTypeA::flat_mat_base_type> AA(A.Flat()); DenseMatrixView<typename CrsExecViewTypeA::flat_mat_base_type> BB(B.Flat()); DenseMatrixView<typename CrsExecViewTypeA::flat_mat_base_type> CC(C.Flat()); return Gemm<Trans::ConjTranspose,Trans::NoTranspose, AlgoGemm::ExternalBlas,Variant::One> ::stat(alpha, AA, BB, beta, CC); }