bool SMatrix::CutIn2_Vertical(SMatrix& Left,SMatrix& Right,int iCol) { int i,j; if (nCol()==0) { Left.ReSize(0,0); Right.ReSize(0,0); return TRUE; } if(iCol>nCol()||iCol<0) return FALSE; if(!Left.ReSize(nRow(),iCol,ERRORVAL)) return FALSE; if(!Right.ReSize(nRow(),nCol()-iCol,ERRORVAL)) return FALSE; for(i=0;i<nRow();i++) for(j=0;j<iCol;j++) Left[i][j] = GetElem(i,j); for(i=0;i<nRow();i++) for(j=0;j<Right.nCol();j++) Right[i][j] = GetElem(i,j+iCol); return TRUE; }
/** * Divide the matrix by the given matrix. The matrixes must be the same size * this is checked via assertions. We perform the division by multiplication * of the inverse so this is a costly operation. * * @param im The matrix to divide by */ SMatrix SMatrix::operator /( const SMatrix &im ) const { assert( this->getCols() == im.getCols() && "Amount of Columns Differ"); assert( this->getRows() == im.getRows() && "Amount of Rows Differ" ); return (*this) * inv ( im ); }
void CMoon::ParseBlock( const CVarBlockParser::_VariableBlock *pBlock_ ) { if( pBlock_==NULL ) { return; } pBlock_->GetBool( "Active", m_bActive ); if( pBlock_->GetString( "MeshFile", m_strMeshName ) ) { HGRPOBJ hMesh = DGraphicAcquireSMD( SFullName( DCtrlGrp()->GroupEnvironment, m_strMeshName ), DGraphicStore::StaticMesh ); if( !hMesh.IsValid() ) { hMesh = DGraphicAcquireSMD( m_strMeshName, DGraphicStore::StaticMesh ); } LoadMesh( hMesh ); } if( pBlock_->GetVector( "Translation", m_vTranslation ) ) { SMatrix mat; mat.SetTranslation( m_vTranslation ); Transform( mat ); } bool bMoonFace; if( pBlock_->GetBool( "MoonFace", bMoonFace ) ) { SetMoonFace( bMoonFace ); } SColor Color; if( pBlock_->GetColor( "Color", Color ) ) { SetColor( Color ); } pBlock_->GetRealNumber( "FillRate", m_fFillRate ); pBlock_->GetRealNumber( "RisingRate", m_fRisingRate ); pBlock_->GetRealNumber( "Pitch", m_fPitch ); }
void IndexEncoding::GKMeansPreprocess( const SMatrix<double> &matDictionary, int num_dic_each_partition, SMatrix<double> &matPrecomputed) const { int num_all_centers = matDictionary.Rows(); matPrecomputed.AllocateSpace(num_all_centers, num_all_centers); int dim = matDictionary.Cols(); for (int i = 0; i < num_all_centers; i++) { for (int j = 0; j < num_all_centers; j++) { const double* pi = matDictionary[i]; const double* pj = matDictionary[j]; matPrecomputed[i][j] = dot(pi, pj, dim); } } for (int i = 0; i < num_all_centers; i++) { matPrecomputed[i][i] = matPrecomputed[i][i] * 0.5; } }
bool SMatrix::equal(const SMatrix& otherMat) const { if(cols == otherMat.getCols() && rows == otherMat.getRows()) return true; else return false; }
void IndexEncoding::MultiDictionaryPreprocess(const SMatrix<double> &matDictionary, int num_dic_each_partition, SMatrix<double> &matPrecomputed) const { int num_all_words = matDictionary.Rows(); int dim = matDictionary.Cols(); matPrecomputed.AllocateSpace(num_all_words, num_all_words); for (int i = 0; i < num_all_words; i++) { for (int j = 0; j <= i; j++) { matPrecomputed[i][j] = dot( matDictionary[i], matDictionary[j], dim); } } for (int i = 0; i < num_all_words; i++) { for (int j = i + 1; j < num_all_words; j++) { matPrecomputed[i][j] = matPrecomputed[j][i]; } } }
bool multiplicable(const SMatrix& m1, const SMatrix& m2) { if( m1.getRows() == m2.getCols()) return true; else return false; }
bool DDynamicActor::Collision_IntersectCheck( int ) { DLogWriteSystem(" Collision_IntersectCheck : %p", this ); SVector Location = m_Locus.Location; SVector Start = Physics::g_vStart; if( m_dwColFlag & CF_COLLIDEDBY_ROOT_INTERNAL ) { Location = GMath.ZeroVector; SMatrix Matrix = (this->m_matRootMatrixForCollision * m_Locus.LocalToWorld).Inverse(); Start = Matrix.TransformVector( Physics::g_vStart ); } SVector vExtent = Physics::g_vExtent + this->m_vExtent; SVector Delta = Start - Location; if( Abs(Delta.Y) >= vExtent.Y ) return false; float DeltaSquared2D = Delta.SizeSquared2D(); float RadiusSquared = Square(vExtent.X); if( DeltaSquared2D >= RadiusSquared ) return false; float Overlap = appSqrt( RadiusSquared ) - appSqrt( DeltaSquared2D ); if( Overlap <= Physics::g_SingleResult.m_fTime ) return false; Physics::g_SingleResult.m_nActorIndex = m_dwID; Physics::g_SingleResult.m_nMeshIndex = -1; Physics::g_SingleResult.m_fTime = Overlap; Delta.Y = 0.f; Physics::g_SingleResult.m_vNormal = Delta.SafeNormal(); Physics::g_SingleResult.m_vContactPoint= m_Locus.Location + this->m_vExtent.X * Physics::g_SingleResult.m_vNormal; Physics::g_SingleResult.m_ActorType = this->m_ActorType; return true; }
bool same_order(const SMatrix& m1, const SMatrix& m2) { if(m1.getRows() == m2.getRows() && m1.getCols() == m2.getCols()) return true; else return false; }
bool DDynamicActor::Collision_IncludeCheck( int ) { DLogWriteSystem(" Collision_IncludeCheck : %p", this ); SVector Location = m_Locus.Location; SVector Start = Physics::g_vStart; if( m_dwColFlag & CF_COLLIDEDBY_ROOT_INTERNAL ) { Location = GMath.ZeroVector; SMatrix Matrix = (this->m_matRootMatrixForCollision * m_Locus.LocalToWorld).Inverse(); Start = Matrix.TransformVector( Physics::g_vStart ); } // quick rejecct SVector MaxLocation = Location + m_vExtent; SVector MinLocation = Location - m_vExtent; if( Start.X > MaxLocation.X ) return false; if( Start.X < MinLocation.X ) return false; if( Start.Y > MaxLocation.Y ) return false; if( Start.Y < MinLocation.Y ) return false; if( Start.Z > MaxLocation.Z ) return false; if( Start.Z < MinLocation.Z ) return false; // check circle float RadiusSq = ( Start.X - Location.X ) * ( Start.X - Location.X ) + ( Start.Z - Location.Z ) * ( Start.Z - Location.Z ); if( RadiusSq > m_vExtent.X * m_vExtent.X ) return false; // return SColResult *Result = new(Physics::g_aMultiResult) SColResult; Result->m_nActorIndex = m_dwID; Result->m_nMeshIndex = -1; Result->m_ActorType = this->m_ActorType; return true; }
/** * Multiply one matrix by the other - the matrixes are the same size * Multiplication is done: this * im * * @param im The matrix to multiply by */ SMatrix SMatrix::operator *( const SMatrix &im ) const { SMatrix m ( im.getRows()); m.storeProduct( *this, im ); return m; }
/** *Overloading the '*' operator. Only works when matrix a's columns are equal to matrix b's *rows, otherwise there is a size error. *Returns a new matrix equal to a * b * */ SMatrix operator*(const SMatrix& a, const SMatrix& b) throw(MatrixError) { if(a.cols() != b.rows()) { //Check whether a's columns are equal to b's rows throw MatrixError("Matrix size error"); //if not throw size error } SMatrix c(a.rows(),b.cols()); //dimensions of new matrix for(auto it = a.ridx_.cbegin();it != a.ridx_.cend();++it) { //Loop through a's rows int row = it->first; int nElements = it->second.first + it->second.second; for(SMatrix::size_type i = 0; i < b.cols(); ++i) { //Loop through b's columns int col = i; int val = 0; for(int pos = it->second.first; pos < nElements; ++pos) { //Loop through a's columns val += a.vals_[pos] * b(a.cidx_[pos],i); //adding the multiplications to } //val if(val != 0) { c.setVal(row,col,val); } } } return c; }
bool SMatrix::CutIn2_Across(SMatrix& Upper,SMatrix& Lower,int iRow) { int i,j; if (nCol()==0) { Upper.ReSize(0,0); Lower.ReSize(0,0); return TRUE; } if(iRow>nRow()||iRow<0) return FALSE; if(!Upper.ReSize(iRow,nCol(),ERRORVAL)) return FALSE; if(!Lower.ReSize(nRow()-iRow,nCol(),ERRORVAL)) return FALSE; for(i=0;i<iRow;i++) for(j=0;j<nCol();j++) Upper[i][j] = GetElem(i,j); for(i=0;i<Lower.nRow();i++) for(j=0;j<nCol();j++) Lower[i][j] = GetElem(i+iRow,j); return TRUE; }
/** * Inverse the given matrix * * @param im The matrix to invert */ SMatrix inv ( const SMatrix &im ) { SMatrix m ( im.getRows() ); m.storeInverse ( im ); return m; }
void reflTrans(const mlgeo &g, double k0, double theta, int pol, cdouble *r, double *R, cdouble *t, double *T) { SMatrix S = new SMatrix(g, k0, k0 * sin(theta)); *r = S->S21(0, g.N, pol); *t = S->S11(0, g.N, pol); *R = (*r) * conj(*r); *T = real(sqrt(g.eps(N))) / real(sqrt(g.eps(0))) * (*t) * conj(*t); }
bool SMatrix::Jointer_bottom(SMatrix& other) { if(other.nCol()!=nCol()) return FALSE; if(!AddRows(other.nRow(),ERRORVAL)) return FALSE; return Paste(other,nRow()-other.nRow(),0); }
void GDynamicActor::RenderShadow( void ) { if( m_pShadow ) { float fMinX, fMaxX, fMinZ, fMaxZ; SVector vExtent; vExtent.X = vExtent.Z = Max( (m_vMeshMax.X-m_vMeshMin.X), (m_vMeshMax.Z-m_vMeshMin.Z) ); vExtent.Y = (m_vMeshMax.Y-m_vMeshMin.Y); if( vExtent.X<m_vExtent.X ) { vExtent.X = vExtent.Z = m_vExtent.X; } if( vExtent.Y<m_vExtent.Y ) { vExtent.Y = m_vExtent.Y; } vExtent.Y *= 1.2f; // for jumping m_pShadow->RangeRectGet( fMinX, fMinZ, fMaxX, fMaxZ, m_Locus.Location, vExtent*m_Locus.Scale*0.6f ); SMatrix matUnit; matUnit.SetIdentity(); m_pShadow->MakeVtxIdx(fMinX, fMinZ, fMaxX, fMaxZ); m_pShadow->MakeStart( matUnit ); m_pShadow->MakeEnd(); /* DTerrain *pTer = DTerrain::StaticGetTerrain( m_Locus.Location.X, m_Locus.Location.Y, m_Locus.Location.Z ); if( pTer ) { pTer->GetShadowVertex( m_Locus.Location, fMinX, fMinZ, fMaxX, fMaxZ, m_pShadow->GetVertexInterface(), m_pShadow->GetIndexInterface() ); m_pShadow->MakeStart( pTer->m_matUnitToWorld ); m_pShadow->MakeEnd(); } */ } else if( m_pApproxShadow ) { float fMinX, fMaxX, fMinZ, fMaxZ; SVector vExtent; vExtent.X = vExtent.Z = Max( (m_vMeshMax.X-m_vMeshMin.X), (m_vMeshMax.Z-m_vMeshMin.Z) ); vExtent.Y = (m_vMeshMax.Y-m_vMeshMin.Y); if( vExtent.X<m_vExtent.X ) { vExtent.X = vExtent.Z = m_vExtent.X; } if( vExtent.Y<m_vExtent.Y ) { vExtent.Y = m_vExtent.Y; } m_pApproxShadow->RangeRectGet( fMinX, fMinZ, fMaxX, fMaxZ, m_Locus.Location, vExtent*m_Locus.Scale*0.6f ); SMatrix matUnit; matUnit.SetIdentity(); m_pApproxShadow->MakeVtxIdx(fMinX, fMinZ, fMaxX, fMaxZ); m_pApproxShadow->Render( matUnit ); /* DTerrain *pTer = DTerrain::StaticGetTerrain( m_Locus.Location.X, m_Locus.Location.Y, m_Locus.Location.Z ); if( pTer ) { pTer->GetShadowVertex( m_Locus.Location, fMinX, fMinZ, fMaxX, fMaxZ, m_pApproxShadow->GetVertexInterface(), m_pApproxShadow->GetIndexInterface() ); m_pApproxShadow->Render( pTer->m_matUnitToWorld ); } */ } }
void output(std::ostream& outs, const SMatrix& src) { // NOTE: r and c are row # and coloum #, NOT indices for(int r = 1; r <= src.getRows(); ++r) { for(int c = 1; c <= src.getCols(); ++c) outs << setw(SMatrix::FIELD_WIDTH) << src.valAt(r, c); outs << endl; } }
bool SMatrix::Jointer_Right(SMatrix& other) { if(other.nRow()!=nRow()) return FALSE; if(!AddCols(other.nCol(),ERRORVAL)) return FALSE; Paste(other,0,nCol()-other.nCol()); return TRUE; }
void IndexEncoding::BestNextWords(const double* p_residual, const SMatrix<double>& matDictionary, int idx_start, int idx_end, SMatrix<double> &mat_diff, Vector<short>& best_idx) const { int m_nDimension = matDictionary.Cols(); SMatrix<double> mat_each_diff(idx_end - idx_start, m_nDimension); Heap<PAIR<double> > pq; pq.Reserve(m_nNumBestCan); for (int i = idx_start; i < idx_end; i++) { const double* p_dic = matDictionary[i]; double e = 0; for (int j = 0; j < m_nDimension; j++) { double d = p_residual[j] - p_dic[j]; mat_each_diff[i - idx_start][j] = d; e += d * d; } if (pq.size() >= m_nNumBestCan) { const PAIR<double> &p = pq.Top(); if (p.distance > e) { pq.popMin(); pq.insert(PAIR<double>(i - idx_start, e)); } } else { pq.insert(PAIR<double>(i - idx_start, e)); } } mat_diff.AllocateSpace(m_nNumBestCan, m_nDimension); best_idx.AllocateSpace(m_nNumBestCan); for (int i = m_nNumBestCan - 1; i >= 0; i--) { PAIR<double> p; pq.popMin(p); best_idx[i] = p.index; memcpy(mat_diff[i], mat_each_diff[p.index], sizeof(double) * m_nDimension); } }
SMatrix operator * (double value ,const SMatrix& other) { int i,j; SMatrix result(other); for (i=0;i<other.nRow();i++) for(j=0;j<other.nCol();j++) result[i][j] *= value; return result; }
/** *Transpose method, that transposes a matrix. *Returns a new matrix equal to the transpose of a. */ SMatrix transpose(const SMatrix& a) { SMatrix c(a.rows(),a.cols()); for(auto it = a.ridx_.cbegin();it != a.ridx_.cend();++it) { //Loop through a's rows int row = it->first; int nElements = it->second.first + it->second.second; for(int pos = it->second.first; pos < nElements; ++pos) { //Loop through a's columns c.setVal(a.cidx_[pos],row,a.vals_[pos]); //add value to new matrix } //with rows and columns swapped } return c; }
SMatrix::SMatrix(const SMatrix& other) { pHead = new HeadNode; pHead->nRow = 0; pHead->nCol = 0; pHead->pFirst = NULL; AddRows(other.nRow(),0.0); AddCols(other.nCol()-1,0.0); *this += other; }
bool SMatrix::Jointer_Diagonal(SMatrix& other,ELEMTYPE Val) { int iCol; iCol = nRow()==0 ? other.nCol()-1 : other.nCol(); if(!AddRows(other.nRow(),Val)) return FALSE; if(!AddCols(iCol,Val)) return FALSE; return Paste(other,nRow()-other.nRow(),nCol()-other.nCol()); }
bool SMatrix::Paste(SMatrix& other,int top,int left) const { int i,j,iRow,iCol; iRow = other.nRow()>(nRow()-top) ? (nRow()-top) : other.nRow(); iCol = other.nCol()>(nCol()-left) ? (nCol()-left): other.nCol(); for(i=0;i<iRow;i++) for(j=0;j<iCol;j++) GetElem(i+top,j+left) = other[i][j]; return TRUE; }
ELEMTYPE SMatrix::Distance_E(SMatrix& other) const { int i,j; ELEMTYPE result = ERRORVAL; if(nRow()!=other.nRow()||nCol()!=other.nCol()) return result; for(result=0,i=0;i<nRow();i++) for(j=0;j<nCol();j++) result += pow(GetElem(i,j)-other[i][j],2); return sqrt(result); }
int test() { string str_file_name = "E:\\research\\working\\test_data\\test_gk_means.mat"; SMatrix<double> matZ; Vector<SMatrix<double> > vecmatDictionary; SMatrix<CodeType> mat_target; int num_dic; int is_initialize; int num_grouped; { MATFile* fp = matOpen(str_file_name.c_str(), "r"); SMART_ASSERT(fp).Exit(); mexConvert(matGetVariable(fp, "Z"), matZ); mexConvert(matGetVariable(fp, "all_D"), vecmatDictionary); mexConvert(matGetVariable(fp, "num_sub_dic_each_partition"), num_dic); mexConvert(matGetVariable(fp, "mat_compact_B"), mat_target); mxArray * para_encode = matGetVariable(fp, "para_encode"); mexConvert(mxGetField(para_encode, 0, "is_initialize"), is_initialize); mexConvert(mxGetField(para_encode, 0, "num_grouped"), num_grouped); matClose(fp); } IndexEncoding ie; ie.SetIsInitialize(is_initialize); ie.SetNumberGroup(num_grouped); ie.SetEncodingType(Type_gk_means); ie.SetEncodingType(Type_additive_quantization); SMatrix<CodeType> matRepresentation; matRepresentation.AllocateSpace(mat_target.Rows(), mat_target.Cols()); int num_selected_rows = 10; matRepresentation.AllocateSpace(num_selected_rows, mat_target.Cols()); ie.Solve(SMatrix<double>(matZ.Ptr(), num_selected_rows, matZ.Cols()), vecmatDictionary, num_dic, matRepresentation); PRINT << "good\n"; for (int i = 0; i < matRepresentation.Rows(); i++) { for (int j = 0; j < matRepresentation.Cols(); j++) { cout << (int)(matRepresentation[i][j]) << "\t"; } cout << "\n"; } //for (int i = 0; i < mat_target.Rows(); i++) //{ // for (int j = 0; j < mat_target.Cols(); j++) // { // SMART_ASSERT(matRepresentation[i][j] == mat_target[i][j]).Exit(); // } //} return 0; }
void IndexEncoding::SolveOptimized(const double* pz, const SMatrix<double>& matDictionary, short* prepresentation) const { int m_nDimension = matDictionary.Cols(); Vector<double> vec_residual(m_nDimension); memcpy(vec_residual.Ptr(), pz, sizeof(double) * m_nDimension); double pre_error = vec_residual.Norm2Squared(); int num_center = matDictionary.Rows(); Vector<bool> vec_representation(num_center); if (m_nNumberDictionaryEachPartition >= 0) { vec_representation.SetValueZeros(); } double best_error; short best_idx; int m_nSubDictionarySize = num_center / m_nNumberDictionaryEachPartition; int idx_start = 0; for (int i = 0; i < m_nNumberDictionaryEachPartition; i++) { idx_start = i * m_nSubDictionarySize; //BestNextWord(vec_residual.Ptr(), matDictionary, vec_representation.Ptr(), //best_error, best_idx); BestNextWord(vec_residual.Ptr(), matDictionary, idx_start, idx_start + m_nSubDictionarySize, best_error, best_idx); if (pre_error > best_error) { vec_residual -= matDictionary[best_idx]; pre_error = best_error; //int mask_start = best_idx / m_nSubDictionarySize; //memset(vec_representation.Ptr() + mask_start, 1, sizeof(bool) * m_nSubDictionarySize); prepresentation[i] = best_idx; } else { break; } } }
void CsvParser::selectCol(vector<int>ColIndex) { SMatrix newMat; int n = matrix.size(); int m = ColIndex.size(); newMat.resize(n); for(int i = 0 ; i< n ; i++) { newMat[i].resize(m); for(int j = 0 ; j<ColIndex.size() ; j++) { newMat[i][j] = matrix[i][ColIndex[j]]; } } matrix = newMat; }
Matrix gamma_exp(const SMatrix& S,const vector<double>& D,double alpha,double beta) { const int n = S.size1(); std::vector<double> DP(n); std::vector<double> DN(n); for(int i=0; i<D.size(); i++) { DP[i] = sqrt(D[i]); DN[i] = 1.0/DP[i]; } SMatrix S2 = S; for(int i=0; i<S2.size1(); i++) for(int j=0; j<=i; j++) S2(i,j) *= DP[i]*DP[j]; Matrix E = gamma_exp(S2,alpha,beta); // E = prod(DN,prod<Matrix>(E,DP)); for(int i=0; i<E.size1(); i++) for(int j=0; j<E.size2(); j++) E(i,j) *= DN[i]*DP[j]; for(int i=0; i<E.size1(); i++) for(int j=0; j<E.size2(); j++) { assert(E(i,j) >= -1.0e-13); if (E(i,j)<0) E(i,j)=0; } return E; }