void Helmholtz( SparseMatrix<F>& H, Int nx, Int ny, F shift ) { DEBUG_CSE typedef Base<F> Real; const Int n = nx*ny; Zeros( H, n, n ); const Real hxInv = nx+1; const Real hyInv = ny+1; const Real hxInvSquared = hxInv*hxInv; const Real hyInvSquared = hyInv*hyInv; const F mainTerm = 2*(hxInvSquared+hyInvSquared) - shift; H.Reserve( 5*n ); for( Int i=0; i<n; ++i ) { const Int x = i % nx; const Int y = i/nx; H.QueueUpdate( i, i, mainTerm ); if( x != 0 ) H.QueueUpdate( i, i-1, -hxInvSquared ); if( x != nx-1 ) H.QueueUpdate( i, i+1, -hxInvSquared ); if( y != 0 ) H.QueueUpdate( i, i-nx, -hyInvSquared ); if( y != ny-1 ) H.QueueUpdate( i, i+nx, -hyInvSquared ); } H.ProcessQueues(); }
void ShiftDiagonal ( SparseMatrix<T>& A, S alphaPre, Int offset, bool existingDiag ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); const T alpha = T(alphaPre); if( existingDiag ) { T* valBuf = A.ValueBuffer(); for( Int i=Max(0,-offset); i<Min(m,n-offset); ++i ) { const Int e = A.Offset( i, i+offset ); valBuf[e] += alpha; } } else { const Int diagLength = Min(m,n-offset) - Max(0,-offset); A.Reserve( diagLength ); for( Int i=Max(0,-offset); i<Min(m,n-offset); ++i ) A.QueueUpdate( i, i+offset, alpha ); A.ProcessQueues(); } }
void JordanCholesky( SparseMatrix<T>& A, Int n ) { DEBUG_ONLY(CSE cse("JordanCholesky")) Zeros( A, n, n ); A.Reserve( 3*n ); for( Int e=0; e<n; ++e ) { if( e == 0 ) A.QueueUpdate( e, e, T(1) ); else A.QueueUpdate( e, e, T(5) ); if( e > 0 ) A.QueueUpdate( e, e-1, T(2) ); if( e < n-1 ) A.QueueUpdate( e, e+1, T(2) ); } A.ProcessQueues(); }
void Helmholtz( SparseMatrix<F>& H, Int n, F shift ) { DEBUG_CSE typedef Base<F> Real; Zeros( H, n, n ); const Real hInv = n+1; const Real hInvSquared = hInv*hInv; const F mainTerm = 2*hInvSquared - shift; H.Reserve( 3*n ); for( Int i=0; i<n; ++i ) { H.QueueUpdate( i, i, mainTerm ); if( i != 0 ) H.QueueUpdate( i, i-1, -hInvSquared ); if( i != n-1 ) H.QueueUpdate( i, i+1, -hInvSquared ); } H.ProcessQueues(); }
void Fill( SparseMatrix<T>& A, T alpha ) { EL_DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); A.Resize( m, n ); Zero( A ); if( alpha != T(0) ) { A.Reserve( m*n ); for( Int i=0; i<m; ++i ) for( Int j=0; j<n; ++j ) A.QueueUpdate( i, j, alpha ); A.ProcessQueues(); } }
void RLS ( const SparseMatrix<Real>& A, const Matrix<Real>& b, Real rho, Matrix<Real>& x, const socp::affine::Ctrl<Real>& ctrl ) { DEBUG_CSE const Int m = A.Height(); const Int n = A.Width(); Matrix<Int> orders, firstInds; Zeros( orders, m+n+3, 1 ); Zeros( firstInds, m+n+3, 1 ); for( Int i=0; i<m+1; ++i ) { orders.Set( i, 0, m+1 ); firstInds.Set( i, 0, 0 ); } for( Int i=0; i<n+2; ++i ) { orders.Set( i+m+1, 0, n+2 ); firstInds.Set( i+m+1, 0, m+1 ); } // G := | -1 0 0 | // | 0 0 A | // | 0 -1 0 | // | 0 0 -I | // | 0 0 0 | SparseMatrix<Real> G; { const Int numEntriesA = A.NumEntries(); Zeros( G, m+n+3, n+2 ); G.Reserve( numEntriesA+n+2 ); G.QueueUpdate( 0, 0, -1 ); for( Int e=0; e<numEntriesA; ++e ) G.QueueUpdate( A.Row(e)+1, A.Col(e)+2, A.Value(e) ); G.QueueUpdate( m+1, 1, -1 ); for( Int j=0; j<n; ++j ) G.QueueUpdate( j+m+2, j+2, -1 ); G.ProcessQueues(); } // h := | 0 | // | b | // | 0 | // | 0 | // | 1 | Matrix<Real> h; Zeros( h, m+n+3, 1 ); auto hb = h( IR(1,m+1), ALL ); hb = b; h.Set( END, 0, 1 ); // c := [1; rho; 0] Matrix<Real> c; Zeros( c, n+2, 1 ); c.Set( 0, 0, 1 ); c.Set( 1, 0, rho ); SparseMatrix<Real> AHat; Zeros( AHat, 0, n+2 ); Matrix<Real> bHat; Zeros( bHat, 0, 1 ); Matrix<Real> xHat, y, z, s; SOCP( AHat, G, bHat, c, h, orders, firstInds, xHat, y, z, s, ctrl ); x = xHat( IR(2,END), ALL ); }