inline void Hanowa( DistMatrix<T,U,V>& A, int n, T mu ) { #ifndef RELEASE CallStackEntry entry("Hanowa"); #endif if( n % 2 != 0 ) throw std::logic_error("n must be an even integer"); A.ResizeTo( n, n ); const int m = n/2; std::vector<T> d(m); DistMatrix<T,U,V> ABlock( A.Grid() ); for( int j=0; j<m; ++j ) d[j] = mu; View( ABlock, A, 0, 0, m, m ); Diagonal( ABlock, d ); View( ABlock, A, m, m, m, m ); Diagonal( ABlock, d ); for( int j=0; j<m; ++j ) d[j] = -(j+1); View( ABlock, A, 0, m, m, m ); Diagonal( ABlock, d ); for( int j=0; j<m; ++j ) d[j] = j+1; View( ABlock, A, m, 0, m, m ); Diagonal( ABlock, d ); }
void GameManager::init(){ background.setDiagonal(Diagonal(Point2f(-1, 1), Point2f(1, -1))); leftTube.setDiagonal(Diagonal(Point2f(-1, 0), Point2f(-0.7, -1))); rightTube.setDiagonal(Diagonal(Point2f(0.7, 0), Point2f(1, -1))); leftNoteList.setSpeed(lvl->getSpeed()); leftNoteList.setMinDelayBetweenNotes(lvl->getMinDelayBetweenNotes()); rightNoteList.setType(Note::toRight); rightNoteList.setSpeed(lvl->getSpeed()); rightNoteList.setMinDelayBetweenNotes(lvl->getMinDelayBetweenNotes()); }
void Score::draw() { if(refreshCoord) { int lenPixDigit = digitSize.w * maxCountDigits; float HDigit = (float)digitSize.h * Screen::getPixelSize().h; float WDigit = (float)digitSize.w * Screen::getPixelSize().w; float xPos = (float)lenPixDigit * Screen::getPixelSize().w / (-2); float yStartPos = yPos; float _yPos; int k = 0; for(int i = 0; i < maxCountDigits; i++){ _yPos=yStartPos; digitDiagonal[k].x = xPos; digitDiagonal[k].y = _yPos; xPos += WDigit; _yPos -= HDigit; digitDiagonal[k + 1].x = xPos; digitDiagonal[k + 1].y = _yPos; k += 2; } refreshCoord = false; } int number = value; int digit = 0; int k = (maxCountDigits) * 2 - 1; Texture tex; tex.setUseAlpha(true); for(int i = maxCountDigits - 1; i >= 0; i--){ digit = number % 10; number /= 10; tex.setDiagonal(Diagonal(digitDiagonal[k-1], digitDiagonal[k])); tex.setID(texDigits[digit]); tex.draw(); k -= 2; } }
void HermitianUniformSpectrum ( Matrix<F>& A, Int n, Base<F> lower, Base<F> upper ) { DEBUG_ONLY(CallStackEntry cse("HermitianUniformSpectrum")) A.Resize( n, n ); typedef Base<F> Real; const bool isComplex = IsComplex<F>::val; // Form d and D std::vector<F> d( n ); for( Int j=0; j<n; ++j ) d[j] = SampleUniform<Real>( lower, upper ); Diagonal( A, d ); // Apply a Haar matrix from both sides Matrix<F> Q, t; Matrix<Real> s; ImplicitHaar( Q, t, s, n ); qr::ApplyQ( LEFT, NORMAL, Q, t, s, A ); qr::ApplyQ( RIGHT, ADJOINT, Q, t, s, A ); if( isComplex ) { const Int height = A.Height(); for( Int j=0; j<height; ++j ) A.SetImagPart( j, j, Real(0) ); } }
void AugmentedKKT ( const ElementalMatrix<Real>& A, const ElementalMatrix<Real>& x, const ElementalMatrix<Real>& z, ElementalMatrix<Real>& JPre, bool onlyLower ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); DistMatrixWriteProxy<Real,Real,MC,MR> JProx( JPre ); auto& J = JProx.Get(); Zeros( J, m+n, m+n ); const IR xInd(0,n), yInd(n,n+m); auto Jxx = J(xInd,xInd); auto Jxy = J(xInd,yInd); auto Jyx = J(yInd,xInd); auto Jyy = J(yInd,yInd); DistMatrix<Real,MC,STAR> d( z ); DiagonalSolve( LEFT, NORMAL, x, d ); Diagonal( Jxx, d ); Jyx = A; if( !onlyLower ) Transpose( A, Jxy ); }
void NormalUniformSpectrum ( ElementalMatrix<Complex<Real>>& APre, Int n, Complex<Real> center, Real radius ) { EL_DEBUG_CSE typedef Complex<Real> C; DistMatrixWriteProxy<C,C,MC,MR> AProx( APre ); auto& A = AProx.Get(); const Grid& grid = A.Grid(); A.Resize( n, n ); // Form d and D vector<C> d( n ); if( grid.Rank() == 0 ) for( Int j=0; j<n; ++j ) d[j] = SampleBall<C>( center, radius ); mpi::Broadcast( d.data(), n, 0, grid.Comm() ); Diagonal( A, d ); // Apply a Haar matrix from both sides DistMatrix<C> Q(grid); DistMatrix<C,MD,STAR> t(grid); DistMatrix<Real,MD,STAR> s(grid); ImplicitHaar( Q, t, s, n ); // Copy the result into the correct distribution qr::ApplyQ( LEFT, NORMAL, Q, t, s, A ); qr::ApplyQ( RIGHT, ADJOINT, Q, t, s, A ); }
void Triangulate( void ) { tVertex v0, v1, v2, v3, v4; /* five consecutive vertices */ int n = nvertices; /* number of vertices; shrinks to 3. */ bool earfound; /* for debugging and error detection only. */ EarInit(); printf("\nnewpath\n"); /* Each step of outer loop removes one ear. */ while ( n > 3 ) { /* Inner loop searches for an ear. */ v2 = vertices; earfound = FALSE; do { if (v2->ear) { earfound = TRUE; /* Ear found. Fill variables. */ v3 = v2->next; v4 = v3->next; v1 = v2->prev; v0 = v1->prev; /* (v1,v3) is a diagonal */ PrintDiagonal( v1, v3 ); /* Update earity of diagonal endpoints */ v1->ear = Diagonal( v0, v3 ); v3->ear = Diagonal( v1, v4 ); /* Cut off the ear v2 */ v1->next = v3; v3->prev = v1; vertices = v3; /* In case the head was v2. */ n--; break; /* out of inner loop; resume outer loop */ } /* end if ear found */ v2 = v2->next; } while ( v2 != vertices ); if ( !earfound ) { printf("%%Error in Triangulate: No ear found.\n"); PrintPoly(); printf("showpage\n%%%%EOF\n"); exit(EXIT_FAILURE); } } /* end outer while loop */ printf("closepath stroke\n\n"); }
inline void MakeNormalUniformSpectrum ( Matrix<Complex<R> >& A, Complex<R> center, R radius ) { #ifndef RELEASE PushCallStack("MakeNormalUniformSpectrum"); #endif typedef Complex<R> C; if( A.Height() != A.Width() ) throw std::logic_error("Cannot make a non-square matrix normal"); // Sample the diagonal matrix D from the ball B_radius(center) // and then rotate it with a random Householder similarity transformation: // // (I-2uu^H) D (I-2uu^H)^H = D - 2(u (conj(D) u)^H + (D u) u^H) + // (4 u^H D u) u u^H // // Form d and D const int n = A.Height(); std::vector<C> d( n ); for( int j=0; j<n; ++j ) d[j] = center + radius*SampleUnitBall<C>(); Diagonal( d, A ); // Form u Matrix<C> u( n, 1 ); MakeUniform( u ); const R origNorm = Nrm2( u ); Scale( 1/origNorm, u ); // Form v := D u Matrix<C> v( n, 1 ); for( int i=0; i<n; ++i ) v.Set( i, 0, d[i]*u.Get(i,0) ); // Form w := conj(D) u Matrix<C> w( n, 1 ); for( int i=0; i<n; ++i ) w.Set( i, 0, Conj(d[i])*u.Get(i,0) ); // Update A := A - 2(u w^H + v u^H) Ger( C(-2), u, w, A ); Ger( C(-2), v, u, A ); // Form \gamma := 4 u^H (D u) = 4 (u,Du) const C gamma = 4*Dot(u,v); // Update A := A + gamma u u^H Ger( gamma, u, u, A ); #ifndef RELEASE PopCallStack(); #endif }
inline void Lauchli( BlockDistMatrix<T,U,V>& A, Int n, T mu ) { DEBUG_ONLY(CallStackEntry cse("Lauchli")) A.Resize( n+1, n ); auto ABlock = View( A, 0, 0, 1, n ); MakeOnes( ABlock ); std::vector<T> d(n,mu); ABlock = View( A, 1, 0, n, n ); Diagonal( ABlock, d ); }
bool AxisAlignedBox3::CollisionTriangle(sglTriangle * triangle) { mmVector3 center = Center(); mmVector3 diagonal = Diagonal(); //const float eps = 1.00f; float boxcenter[3] = {center.x, center.y, center.z}; float boxhalfsize[3] = {diagonal.x / 2.0f, diagonal.y / 2.0f, diagonal.z / 2.0f}; float triverts[3][3] = { {triangle->point1.x, triangle->point1.y, triangle->point1.z}, {triangle->point2.x, triangle->point2.y, triangle->point2.z}, {triangle->point3.x, triangle->point3.y, triangle->point3.z}}; return triBoxOverlap(boxcenter, boxhalfsize, triverts); }
void HermitianUniformSpectrum ( DistMatrix<F,U,V>& A, Int n, Base<F> lower, Base<F> upper ) { DEBUG_ONLY(CallStackEntry cse("HermitianUniformSpectrum")) A.Resize( n, n ); const Grid& grid = A.Grid(); typedef Base<F> Real; const bool isComplex = IsComplex<F>::val; const bool standardDist = ( U == MC && V == MR ); // Form d and D std::vector<F> d( n ); if( grid.Rank() == 0 ) for( Int j=0; j<n; ++j ) d[j] = SampleUniform<Real>( lower, upper ); mpi::Broadcast( d.data(), n, 0, grid.Comm() ); DistMatrix<F> ABackup( grid ); ABackup.AlignWith( A ); Diagonal( ABackup, d ); // Apply a Haar matrix from both sides DistMatrix<F> Q(grid); DistMatrix<F,MD,STAR> t(grid); DistMatrix<Real,MD,STAR> s(grid); ImplicitHaar( Q, t, s, n ); // Copy the result into the correct distribution qr::ApplyQ( LEFT, NORMAL, Q, t, s, ABackup ); qr::ApplyQ( RIGHT, ADJOINT, Q, t, s, ABackup ); A = ABackup; // Force the diagonal to be real-valued if( isComplex ) { const Int localHeight = A.LocalHeight(); const Int localWidth = A.LocalWidth(); for( Int jLoc=0; jLoc<localWidth; ++jLoc ) { const Int j = A.GlobalCol(jLoc); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = A.GlobalRow(iLoc); if( i == j ) A.SetLocalImagPart( iLoc, jLoc, Real(0) ); } } } }
/*--------------------------------------------------------------------- This function initializes the data structures, and calls Triangulate2 to clip off the ears one by one. ---------------------------------------------------------------------*/ void EarInit( void ) { tVertex v0, v1, v2; /* three consecutive vertices */ /* Initialize v1->ear for all vertices. */ v1 = vertices; printf("newpath\n"); do { v2 = v1->next; v0 = v1->prev; v1->ear = Diagonal( v0, v2 ); v1 = v1->next; } while ( v1 != vertices ); printf("closepath stroke\n\n"); }
void KKT ( const ElementalMatrix<Real>& A, const ElementalMatrix<Real>& x, const ElementalMatrix<Real>& z, ElementalMatrix<Real>& JPre, bool onlyLower ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); DistMatrixWriteProxy<Real,Real,MC,MR> JProx( JPre ); auto& J = JProx.Get(); Zeros( J, 2*n+m, 2*n+m ); const IR xInd(0,n), yInd(n,n+m), zInd(n+m,2*n+m); auto Jxx = J(xInd,xInd); auto Jxy = J(xInd,yInd); auto Jxz = J(xInd,zInd); auto Jyx = J(yInd,xInd); auto Jyy = J(yInd,yInd); auto Jyz = J(yInd,zInd); auto Jzx = J(zInd,xInd); auto Jzy = J(zInd,yInd); auto Jzz = J(zInd,zInd); // Jyx := A // ======== Jyx = A; // Jzx := -I // ========= Identity( Jzx, n, n ); Jzx *= -1; // Jzz := - z <> x // =============== DistMatrix<Real,MC,STAR> t(x); DiagonalSolve( LEFT, NORMAL, z, t ); t *= -1; Diagonal( Jzz, t ); if( !onlyLower ) { // Jxy := A^T // ========== Transpose( A, Jxy ); // Jxz := -I // ========= Identity( Jxz, n, n ); Jxz *= -1; } }
void KKT ( const Matrix<Real>& A, const Matrix<Real>& G, const Matrix<Real>& s, const Matrix<Real>& z, Matrix<Real>& J, bool onlyLower ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); const Int k = G.Height(); Zeros( J, n+m+k, n+m+k ); const IR xInd(0,n), yInd(n,n+m), zInd(n+m,n+m+k); auto Jxx = J(xInd,xInd); auto Jxy = J(xInd,yInd); auto Jxz = J(xInd,zInd); auto Jyx = J(yInd,xInd); auto Jyy = J(yInd,yInd); auto Jyz = J(yInd,zInd); auto Jzx = J(zInd,xInd); auto Jzy = J(zInd,yInd); auto Jzz = J(zInd,zInd); // Jyx := A // ======== Jyx = A; // Jzx := G // ======== Jzx = G; // Jzz := - z <> s // =============== Matrix<Real> t; t = s; DiagonalSolve( LEFT, NORMAL, z, t ); t *= -1; Diagonal( Jzz, t ); if( !onlyLower ) { // Jxy := A^T // ========== Transpose( A, Jxy ); // Jxz := G^T // ========== Transpose( G, Jxz ); } }
void KKT ( const Matrix<Real>& A, const Matrix<Real>& x, const Matrix<Real>& z, Matrix<Real>& J, bool onlyLower ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); Zeros( J, 2*n+m, 2*n+m ); const IR xInd(0,n), yInd(n,n+m), zInd(n+m,2*n+m); auto Jxx = J(xInd,xInd); auto Jxy = J(xInd,yInd); auto Jxz = J(xInd,zInd); auto Jyx = J(yInd,xInd); auto Jyy = J(yInd,yInd); auto Jyz = J(yInd,zInd); auto Jzx = J(zInd,xInd); auto Jzy = J(zInd,yInd); auto Jzz = J(zInd,zInd); // Jyx := A // ======== Jyx = A; // Jzx := -I // ========= Identity( Jzx, n, n ); Jzx *= -1; // Jzz := - z <> x // =============== Matrix<Real> t; t = x; DiagonalSolve( LEFT, NORMAL, z, t ); t *= -1; Diagonal( Jzz, t ); if( !onlyLower ) { // Jxy := A^T // ========== Transpose( A, Jxy ); // Jxz := -I // ========= Identity( Jxz, n, n ); Jxz *= -1; } }
void DruinskyToledo( ElementalMatrix<F>& A, Int k ) { EL_DEBUG_CSE const Int n = 2*k; Zeros( A, n, n ); if( k == 0 ) return; if( k == 1 ) { Ones( A, n, n ); return; } typedef Base<F> Real; const Real phi = Real(1) + 4*limits::Epsilon<Real>(); const Real alphaPhi = LDLPivotConstant<Real>(BUNCH_KAUFMAN_A)*phi; vector<Real> d( k-2 ); Real sigma(1); for( Int i=0; i<k-2; ++i ) { d[i] = -alphaPhi/sigma; sigma -= 1/d[i]; } unique_ptr<ElementalMatrix<F>> ASub( A.Construct(A.Grid(),A.Root()) ); View( *ASub, A, IR(k-2,k), IR(0,k) ); Ones( *ASub, 2, k ); View( *ASub, A, IR(0,k), IR(k-2,k) ); Ones( *ASub, k, 2 ); View( *ASub, A, IR(0,k-2), IR(0,k-2) ); Diagonal( *ASub, d ); View( *ASub, A, IR(k,n), IR(0,k) ); Identity( *ASub, k, k ); View( *ASub, A, IR(k,n), IR(k,n) ); Identity( *ASub, k, k ); View( *ASub, A, IR(0,k), IR(k,n) ); Identity( *ASub, k, k ); }
void NormalUniformSpectrum ( Matrix<Complex<Real>>& A, Int n, Complex<Real> center, Real radius ) { EL_DEBUG_CSE typedef Complex<Real> C; A.Resize( n, n ); // Form d and D vector<C> d( n ); for( Int j=0; j<n; ++j ) d[j] = SampleBall<C>( center, radius ); Diagonal( A, d ); // Apply a Haar matrix from both sides Matrix<C> Q, t; Matrix<Real> s; ImplicitHaar( Q, t, s, n ); qr::ApplyQ( LEFT, NORMAL, Q, t, s, A ); qr::ApplyQ( RIGHT, ADJOINT, Q, t, s, A ); }
void DruinskyToledo( Matrix<F>& A, Int k ) { EL_DEBUG_CSE const Int n = 2*k; Zeros( A, n, n ); if( k == 0 ) return; if( k == 1 ) { Ones( A, n, n ); return; } typedef Base<F> Real; const Real phi = Real(1) + 4*limits::Epsilon<Real>(); const Real alphaPhi = LDLPivotConstant<Real>(BUNCH_KAUFMAN_A)*phi; vector<Real> d( k-2 ); Real sigma(1); for( Int i=0; i<k-2; ++i ) { d[i] = -alphaPhi/sigma; sigma -= 1/d[i]; } auto GBL = A(IR(k-2,k),IR(0,k)); Ones( GBL, GBL.Height(), GBL.Width() ); auto GTR = A(IR(0,k),IR(k-2,k)); Ones( GTR, GTR.Height(), GTR.Width() ); auto GTL = A(IR(0,k-2),IR(0,k-2)); Diagonal( GTL, d ); auto ABL = A(IR(k,n),IR(0,k)); Identity( ABL, k, k ); auto ABR = A(IR(k,n),IR(k,n)); Identity( ABR, k, k ); auto ATR = A(IR(0,k),IR(k,n)); Identity( ATR, k, k ); }
void Matrix::AbstractAlphadiag(AbstractTarget *currentTarget, Complex *dielec, real b1, real b2, real *b3, const Complex &cxrr) { const real onex_ = (real)1.; int curTargetNat = currentTarget->Nat(); short *curTargetIcompData = currentTarget->Icomp().GetData(); int nncomp = currentTarget->Ncomp(); Vect3<Complex> *cax = new Vect3<Complex>[nncomp]; Complex *cxalph = Diagonal(); Complex *cxalof = OffDiagonal(); // int ia, l; for(ia=0; ia<nncomp; ++ia) { Complex temp = dielec[ia]; for(l=0; l<3; ++l) { real b33 = (b3) ? b3[l] : (real)0.; Complex cxterm = (temp - onex_) / (temp + (real)2.) * (real)(0.75 / Pi); // First compute Clausius-Mossotti polarizability: cxterm = cxterm / (cxterm * (temp * (b2 + b33) + b1) + onex_); // Now apply corretion term cax[ia].data[l] = cxterm / (cxterm * cxrr + onex_); } } // for(ia=0; ia<curTargetNat; ++ia) { for(l=0; l<3; ++l) { int index = 3*ia + l; short ic = curTargetIcompData[index]; if (ic > nncomp) ic = nncomp; if (ic > 0) cxalph[index] = cax[ic-1].data[l]; else cxalph[index].unityRe(); // To avoid divisions by zero, etc., set CXALPH=1 for vacuum sites. cxalof[index].clear(); // Set off-diagonal terms to zero } } delete [] cax; }
void AugmentedKKT ( const Matrix<Real>& A, const Matrix<Real>& x, const Matrix<Real>& z, Matrix<Real>& J, bool onlyLower ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); Zeros( J, m+n, m+n ); const IR xInd(0,n), yInd(n,n+m); auto Jxx = J(xInd,xInd); auto Jxy = J(xInd,yInd); auto Jyx = J(yInd,xInd); auto Jyy = J(yInd,yInd); Matrix<Real> d( z ); DiagonalSolve( LEFT, NORMAL, x, d ); Diagonal( Jxx, d ); Jyx = A; if( !onlyLower ) Transpose( A, Jxy ); }
void Matrix::LoadableAlphadiag(LoadableTarget *loadableTarget) { if (loadableTarget->GetDFdata() == NULL) return; Complex s1, s2, s3, s4, s5, s6; real r[3][3]; int curTargetNat = loadableTarget->Nat(); short *curTargetIcompData = loadableTarget->Icomp().GetData(); Complex *cxalph = Diagonal(); Complex *cxalof = OffDiagonal(); AbstractDFData *dfData = loadableTarget->GetDFdata(); for(int ia=0; ia<curTargetNat; ++ia) { int index = 3*ia; if (curTargetIcompData[index] > 0) { if (loadableTarget->IsDipolePhysicallyAnisotropic(ia)) // if ((curTargetIcompData[index] != curTargetIcompData[index + 1]) || // (curTargetIcompData[index] != curTargetIcompData[index + 2])) { if (dfData->AreAnglesZero(ia)) continue; real costh = Cos(dfData->GetThetadf(ia)); real cosph = Cos(dfData->GetPhidf(ia)); real cosbe = Cos(dfData->GetBetadf(ia)); if (costh * cosbe * cosph < (real)1.) { // // if nonzero rotation, recalculate CXALPH and CXALOF real sinth = Sin(dfData->GetThetadf(ia)); real sinph = Sin(dfData->GetPhidf(ia)); real sinbe = Sin(dfData->GetBetadf(ia)); // // Define R = rotation matrix // ! Define RI = inverse of R r[0][0] = costh; r[0][1] = sinth*cosph; r[0][2] = sinth*sinph; r[1][0] = -sinth*cosbe; r[1][1] = costh*cosbe*cosph - sinbe*sinph; r[1][2] = costh*cosbe*sinph + sinbe*cosph; r[2][0] = sinth*sinbe; r[2][1] = -costh*sinbe*cosph - cosbe*sinph; r[2][2] = -costh*sinbe*sinph + cosbe*cosph; // // Calculate diagonal elements: s1.clear(); s2.clear(); s3.clear(); s4.clear(); s5.clear(); s6.clear(); for(int kl=0; kl<3; ++kl) { s1 += (cxalph[index + kl] * r[0][kl] * r[0][kl]); s2 += (cxalph[index + kl] * r[1][kl] * r[1][kl]); s3 += (cxalph[index + kl] * r[2][kl] * r[2][kl]); s4 += (cxalph[index + kl] * r[2][kl] * r[1][kl]); s5 += (cxalph[index + kl] * r[0][kl] * r[2][kl]); s6 += (cxalph[index + kl] * r[1][kl] * r[0][kl]); } cxalph[index ] = s1; cxalph[index + 1] = s2; cxalph[index + 2] = s3; cxalof[index ] = s4; cxalof[index + 1] = s5; cxalof[index + 2] = s6; } } } } }
inline void MakeNormalUniformSpectrum ( DistMatrix<Complex<R>,U,V>& A, Complex<R> center=0, R radius=1 ) { #ifndef RELEASE CallStackEntry entry("MakeNormalUniformSpectrum"); #endif typedef Complex<R> C; if( A.Height() != A.Width() ) LogicError("Cannot make a non-square matrix normal"); const Grid& grid = A.Grid(); const bool standardDist = ( U == MC && V == MR ); // Sample the diagonal matrix D from the ball B_radius(center) // and then rotate it with a random Householder similarity transformation: // // (I-2uu^H) D (I-2uu^H)^H = D - 2(u (Conj(D) u)^H + (D u) u^H) + // (4 u^H D u) u u^H // // Form d and D const Int n = A.Height(); std::vector<C> d( n ); if( grid.Rank() == 0 ) for( Int j=0; j<n; ++j ) d[j] = SampleBall<C>( center, radius ); mpi::Broadcast( &d[0], n, 0, grid.Comm() ); DistMatrix<C> ABackup( grid ); if( standardDist ) Diagonal( A, d ); else { ABackup.AlignWith( A ); Diagonal( ABackup, d ); } // Form u DistMatrix<C> u( grid ); if( standardDist ) u.AlignWith( A ); else u.AlignWith( ABackup ); Uniform( u, n, 1 ); const R origNorm = Nrm2( u ); Scale( 1/origNorm, u ); // Form v := D u DistMatrix<C> v( grid ); if( standardDist ) v.AlignWith( A ); else v.AlignWith( ABackup ); v.ResizeTo( n, 1 ); if( v.LocalWidth() == 1 ) { const Int colShift = v.ColShift(); const Int colStride = v.ColStride(); const Int localHeight = v.LocalHeight(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = colShift + iLoc*colStride; v.SetLocal( iLoc, 0, d[i]*u.GetLocal(iLoc,0) ); } } // Form w := Conj(D) u DistMatrix<C> w( grid ); if( standardDist ) w.AlignWith( A ); else w.AlignWith( ABackup ); w.ResizeTo( n, 1 ); if( w.LocalWidth() == 1 ) { const Int colShift = w.ColShift(); const Int colStride = w.ColStride(); const Int localHeight = w.LocalHeight(); for( Int iLoc=0; iLoc<localHeight; ++iLoc ) { const Int i = colShift + iLoc*colStride; w.SetLocal( iLoc, 0, Conj(d[i])*u.GetLocal(iLoc,0) ); } } // Update A := A - 2(u w^H + v u^H) if( standardDist ) { Ger( C(-2), u, w, A ); Ger( C(-2), v, u, A ); } else { Ger( C(-2), u, w, ABackup ); Ger( C(-2), v, u, ABackup ); } // Form \gamma := 4 u^H (D u) = 4 (u,Du) const C gamma = 4*Dot(u,v); // Update A := A + gamma u u^H if( standardDist ) Ger( gamma, u, u, A ); else Ger( gamma, u, u, ABackup ); // Copy the result into the correct distribution if( !standardDist ) A = ABackup; }
int main(int agc,char**argv){ Diagonal(argv); }
bool CrsMatrixInfo( const Epetra_CrsMatrix & A, ostream & os ) { int MyPID = A.Comm().MyPID(); // take care that matrix is already trasformed bool IndicesAreGlobal = A.IndicesAreGlobal(); if( IndicesAreGlobal == true ) { if( MyPID == 0 ) { os << "WARNING : matrix must be transformed to local\n"; os << " before calling CrsMatrixInfo\n"; os << " Now returning...\n"; } return false; } int NumGlobalRows = A.NumGlobalRows(); int NumGlobalNonzeros = A.NumGlobalNonzeros(); int NumGlobalCols = A.NumGlobalCols(); double NormInf = A.NormInf(); double NormOne = A.NormOne(); int NumGlobalDiagonals = A.NumGlobalDiagonals(); int GlobalMaxNumEntries = A.GlobalMaxNumEntries(); int IndexBase = A.IndexBase(); bool StorageOptimized = A.StorageOptimized(); bool LowerTriangular = A.LowerTriangular(); bool UpperTriangular = A.UpperTriangular(); bool NoDiagonal = A.NoDiagonal(); // these variables identifies quantities I have to compute, // since not provided by Epetra_CrsMatrix double MyFrobeniusNorm( 0.0 ), FrobeniusNorm( 0.0 ); double MyMinElement( DBL_MAX ), MinElement( DBL_MAX ); double MyMaxElement( DBL_MIN ), MaxElement( DBL_MIN ); double MyMinAbsElement( DBL_MAX ), MinAbsElement( DBL_MAX ); double MyMaxAbsElement( 0.0 ), MaxAbsElement( 0.0 ); int NumMyRows = A.NumMyRows(); int * NzPerRow = new int[NumMyRows]; int Row; // iterator on rows int Col; // iterator on cols int MaxNumEntries = A.MaxNumEntries(); double * Values = new double[MaxNumEntries]; int * Indices = new int[MaxNumEntries]; double Element, AbsElement; // generic nonzero element and its abs value int NumEntries; std::vector<double> Diagonal(NumMyRows); // SumOffDiagonal is the sum of absolute values for off-diagonals double * SumOffDiagonal = new double [NumMyRows]; for( Row=0 ; Row<NumMyRows ; ++Row ) { SumOffDiagonal[Row] = 0.0; } int * IsDiagonallyDominant = new int [NumMyRows]; // cycle over all matrix elements for( Row=0 ; Row<NumMyRows ; ++Row ) { // int GlobalRow = A.GRID(Row); NzPerRow[Row] = A.NumMyEntries(Row); A.ExtractMyRowCopy(Row,NzPerRow[Row],NumEntries,Values,Indices); for( Col=0 ; Col<NumEntries ; ++Col ) { Element = Values[Col]; AbsElement = abs(Element); if( Element<MyMinElement ) MyMinElement = Element; if( Element>MyMaxElement ) MyMaxElement = Element; if( AbsElement<MyMinAbsElement ) MyMinAbsElement = AbsElement; if( AbsElement>MyMaxAbsElement ) MyMaxAbsElement = AbsElement; if( Indices[Col] == Row ) Diagonal[Row] = Element; else SumOffDiagonal[Row] += abs(Element); MyFrobeniusNorm += pow(Element,2); } } // analise storage per row int MyMinNzPerRow( NumMyRows ), MinNzPerRow( NumMyRows ); int MyMaxNzPerRow( 0 ), MaxNzPerRow( 0 ); for( Row=0 ; Row<NumMyRows ; ++Row ) { if( NzPerRow[Row]<MyMinNzPerRow ) MyMinNzPerRow=NzPerRow[Row]; if( NzPerRow[Row]>MyMaxNzPerRow ) MyMaxNzPerRow=NzPerRow[Row]; } // a test to see if matrix is diagonally-dominant int MyDiagonalDominance( 0 ), DiagonalDominance( 0 ); int MyWeakDiagonalDominance( 0 ), WeakDiagonalDominance( 0 ); for( Row=0 ; Row<NumMyRows ; ++Row ) { if( abs(Diagonal[Row])>SumOffDiagonal[Row] ) ++MyDiagonalDominance; else if( abs(Diagonal[Row])==SumOffDiagonal[Row] ) ++MyWeakDiagonalDominance; } // reduction operations A.Comm().SumAll(&MyFrobeniusNorm, &FrobeniusNorm, 1); A.Comm().MinAll(&MyMinElement, &MinElement, 1); A.Comm().MaxAll(&MyMaxElement, &MaxElement, 1); A.Comm().MinAll(&MyMinAbsElement, &MinAbsElement, 1); A.Comm().MaxAll(&MyMaxAbsElement, &MaxAbsElement, 1); A.Comm().MinAll(&MyMinNzPerRow, &MinNzPerRow, 1); A.Comm().MaxAll(&MyMaxNzPerRow, &MaxNzPerRow, 1); A.Comm().SumAll(&MyDiagonalDominance, &DiagonalDominance, 1); A.Comm().SumAll(&MyWeakDiagonalDominance, &WeakDiagonalDominance, 1); // free memory delete[] Values; delete[] Indices; delete[] SumOffDiagonal; delete[] IsDiagonallyDominant; delete[] NzPerRow; // simply no output for MyPID>0, only proc 0 write on os if( MyPID != 0 ) return true; os << "*** general Information about the matrix\n"; os << "Number of Global Rows = " << NumGlobalRows << endl; os << "Number of Global Cols = " << NumGlobalCols << endl; os << "is the matrix square = " << ((NumGlobalRows==NumGlobalCols)?"yes":"no") << endl; os << "||A||_\\infty = " << NormInf << endl; os << "||A||_1 = " << NormOne << endl; os << "||A||_F = " << sqrt(FrobeniusNorm) << endl; os << "Number of nonzero diagonal entries = " << NumGlobalDiagonals << "( " << 1.0* NumGlobalDiagonals/NumGlobalRows*100 << " %)\n"; os << "Nonzero per row : min = " << MinNzPerRow << " average = " << 1.0*NumGlobalNonzeros/NumGlobalRows << " max = " << MaxNzPerRow << endl; os << "Maximum number of nonzero elements/row = " << GlobalMaxNumEntries << endl; os << "min( a_{i,j} ) = " << MinElement << endl; os << "max( a_{i,j} ) = " << MaxElement << endl; os << "min( abs(a_{i,j}) ) = " << MinAbsElement << endl; os << "max( abs(a_{i,j}) ) = " << MaxAbsElement << endl; os << "Number of diagonal dominant rows = " << DiagonalDominance << " (" << 100.0*DiagonalDominance/NumGlobalRows << " % of total)\n"; os << "Number of weakly diagonal dominant rows = " << WeakDiagonalDominance << " (" << 100.0*WeakDiagonalDominance/NumGlobalRows << " % of total)\n"; os << "*** Information about the Trilinos storage\n"; os << "Base Index = " << IndexBase << endl; os << "is storage optimized = " << ((StorageOptimized==true)?"yes":"no") << endl; os << "are indices global = " << ((IndicesAreGlobal==true)?"yes":"no") << endl; os << "is matrix lower triangular = " << ((LowerTriangular==true)?"yes":"no") << endl; os << "is matrix upper triangular = " << ((UpperTriangular==true)?"yes":"no") << endl; os << "are there diagonal entries = " << ((NoDiagonal==false)?"yes":"no") << endl; return true; }
Matrix3x3 Matrix3x3::Scale(const float& x, const float& y, const float& z) { return Diagonal(x, y, z); }
void MatrixEpetra::zeroRows( std::vector<int> const& rows, Vector<value_type> const& values, Vector<value_type>& rhs, Context const& on_context ) { FEELPP_ASSERT ( this->isInitialized() ).error( "MatrixEpetra<> not properly initialized" ); const Epetra_Map& rowMap( M_mat->RowMatrixRowMap() ); const Epetra_Map& colMap( M_mat->RowMatrixColMap() ); std::vector<int>::const_iterator rit = rows.begin(); std::vector<int>::const_iterator ren = rows.end(); int start = rowMap.MinMyGID(); int stop = rowMap.MaxMyGID()+1; DVLOG(2) << "[MatrixEpetra::zeroRows] Number of rows to zero out (except diagonal) : " << rows.size() << "\n"; DVLOG(2) << "[MatrixEpetra::zeroRows] start : " << start << " stop : " << stop << "\n"; DVLOG(2) << "[MatrixEpetra::zeroRows] keep diag ? " << on_context.test( ON_ELIMINATION_KEEP_DIAGONAL ) << "\n"; DVLOG(2) << "[MatrixEpetra::zeroRows] on symmetric ? " << on_context.test( ON_ELIMINATION_SYMMETRIC ) << "\n"; Epetra_Vector Diagonal( rowMap ); M_mat->ExtractDiagonalCopy( Diagonal ); for ( size_type i = 0; rit != ren; ++rit, ++i ) { int myRow = rowMap.LID( *rit ); if ( myRow >= 0 ) { int NumEntries; double* Values; int* Indices; DVLOG(2) << "extract row gid: " << *rit << "( " << i << ") lid: " << myRow << "\n"; //When doing ExtractMyRowView, Indices contain the local indices int ierr = M_mat->ExtractMyRowView( myRow, NumEntries, Values, Indices ); DVLOG(2) << "ExtractGlobalRowView1 ierr = " << ierr << "\n"; FEELPP_ASSERT( ierr == 0 )( ierr )( myRow )( NumEntries ).warn ( "error in ExtractGlobalRowView" ); if ( on_context.test( ON_ELIMINATION_SYMMETRIC ) ) { // we suppose here that the pattern is // symmetric (not necessarily the matrix) // do a loop on all indices, j, in Indices and // zero out corresponding columns // (Indices[j],myRow) for ( int j = 0; j < NumEntries; ++j ) { if ( Indices[j] == myRow ) continue; int NumRowEntries; double *RowValues; int *RowIndices; DVLOG(2) << "[zeroRows] working with row lid: " << Indices[j] << " (" << *rit << ", " << i << ")" << " gid: " << rowMap.LID( Indices[j] ) << " is_local: " << rowMap.MyLID( Indices[j] ) << "\n"; if ( rowMap.MyLID( Indices[j] ) ) { int gid = Indices[j]; DVLOG(2) << "[zeroRows] local, gid =" << gid << " lid = " << Indices[j] << "\n"; ierr = M_mat->ExtractMyRowView( Indices[j], NumRowEntries, RowValues, RowIndices ); DVLOG(2) << "ExtractMyRowView ierr = " << ierr << "\n"; FEELPP_ASSERT( ierr == 0 )( ierr )( Indices[j] )( NumRowEntries ).warn ( "error in ExtractMyRowView/symm" ); bool found = false; for ( int k = 0; k < NumRowEntries; ++k ) if ( RowIndices[k] == myRow ) { found=true; rhs.add( gid, -values(gid)*RowValues[k] ); RowValues[k] = 0.0; break; } FEELPP_ASSERT( found == true )( myRow )( Indices[j] ).error ( "matrix graph is not symmetric" ); } else { FEELPP_ASSERT( 0 ).error ( "zeroRows() not available in parallel computation" ); } } } std::fill( Values, Values+NumEntries, 0.0 ); NumEntries = 1; if ( on_context.test( ON_ELIMINATION_KEEP_DIAGONAL ) ) { ierr = M_mat->ReplaceMyValues( myRow, NumEntries, &Diagonal[myRow], &myRow ); FEELPP_ASSERT( ierr == 0 )( ierr )( myRow )( NumEntries ).warn ( "error in ReplaceMyValues()/diag" ); } else { /* put diagonal to 1 */ Diagonal[myRow] = 1.0; ierr = M_mat->ReplaceMyValues( myRow, NumEntries, &Diagonal[myRow], &myRow ); FEELPP_ASSERT( ierr == 0 )( ierr )( myRow )( NumEntries ).warn ( "error in ReplaceMyValues()/1" ); } M_bc_index.push_back( *rit ); // warning: a row index may belong to another // processor, so make sure that we access only the // rows that belong to this processor rhs.set( *rit, values(*rit)*Diagonal[myRow] ); } } if ( on_context.test( ON_ELIMINATION_SYMMETRIC ) ) { } }