/** * Print out Chain after each iteration */ void MCMCObjective::DoExecute() { if (!mcmc_) LOG_CODE_ERROR() << "if (!mcmc_)"; if (first_write_ && !model_->global_configuration().resume()) { cache_ << "starting_covariance_matrix {m}\n"; auto covariance = mcmc_->covariance_matrix(); for (unsigned i = 0; i < covariance.size1(); ++i) { for (unsigned j = 0; j < covariance.size2() - 1; ++j) cache_ << covariance(i,j) << " "; cache_ << covariance(i, covariance.size2() - 1) << "\n"; } cache_ << "samples {d} \n"; cache_ << "sample objective_score prior likelihood penalties additional_priors jacobians step_size acceptance_rate acceptance_rate_since_adapt\n"; } auto chain = mcmc_->chain(); unsigned element = chain.size() - 1; cache_ << chain[element].iteration_ << " " << chain[element].score_ << " " << chain[element].prior_ << " " << chain[element].likelihood_ << " " << chain[element].penalty_ << " " << chain[element].additional_priors_ << " " << chain[element].jacobians_ << " " << chain[element].step_size_ << " " << chain[element].acceptance_rate_ << " " << chain[element].acceptance_rate_since_adapt_ << "\n"; ready_for_writing_ = true; }
void CovarianceMatrix::DoExecute() { /* * This reports the Covariance, Correlation and Hessian matrix */ LOG_TRACE(); auto minimiser_ = model_->managers().minimiser()->active_minimiser(); covariance_matrix_ = minimiser_->covariance_matrix(); cache_ << "*" << label_ << " " << "(" << type_ << ")" << "\n"; cache_ << "Covariance_Matrix " << REPORT_R_MATRIX << "\n"; for (unsigned i = 0; i < covariance_matrix_.size1(); ++i) { for (unsigned j = 0; j < covariance_matrix_.size2(); ++j) cache_ << covariance_matrix_(i, j) << " "; cache_ << "\n"; } if (model_->run_mode() == RunMode::kMCMC) { auto mcmc_ = model_->managers().mcmc()->active_mcmc(); if (mcmc_->recalculate_covariance()) { cache_ << REPORT_END << "\n\n"; LOG_FINE() << "During the mcmc run you recalculated the the covariance matrix, so we will print the modified matrix at the end of the chain"; cache_ << "Modified_covariance_matrix " << REPORT_R_MATRIX << "\n"; auto covariance = mcmc_->covariance_matrix(); for (unsigned i = 0; i < covariance.size1(); ++i) { for (unsigned j = 0; j < covariance.size2() - 1; ++j) cache_ << covariance(i, j) << " "; cache_ << covariance(i, covariance.size2() - 1) << "\n"; } } } ready_for_writing_ = true; }
/* void InitProc() { static tbb::task_scheduler_init init; progress_mutex = new mutex(); } void UnInitProc() { delete progress_mutex; } class tbb_ResampleTo256 { VData *data,*res; float sc; public: tbb_ResampleTo256(VData* _data,VData* _res) : data(_data),res(_res){ sc = data->GetSize().z/256.0f;}; public: void operator()(const blocked_range <size_t>& r) const { ivec3 size = data->GetSize(); for (size_t i=r.begin(); i!=r.end(); i++) { for(int j=0;j<256;j++) for(int k=0;k<256;k++) { float sum=0,ww=0; int k1=int(k*sc)-1,k2=int((k+1)*sc)+1; if(k1<0)k1=0; if(k2>=size.z)k2=size.z-1; for(int kk=k1;kk<=k2;kk++) for(int ii=0;ii<2;ii++) for(int jj=0;jj<2;jj++) { float www = 1.0/(abs(k*sc-kk)+1); sum += data->GetValue(i*2+ii,j*2+jj,kk)*www; ww += www; } //res[i+256*(j+256*k)] = short(sum/ww); res->SetValue(short(sum/ww),i,j,k); } } } }; VData ResampleTo256(VData& data) { InitProc(); VData data_small(ivec3(256)); ivec3 size=data.GetSize(); if(!(size.x==512 && size.y==512))throw "123123"; { for(int i=0;i<4;i++) { parallel_for(blocked_range<size_t>(i*64, (i+1)*64, 16), tbb_ResampleTo256(&data,&data_small) ); Progress::inst->SetPercent((i+1)*0.25); } } UnInitProc(); return data_small; }*/ VData GetMipData(VData& data,int dv) { ivec3 size = data.GetSize(); ivec3 size2(size.x/dv,size.y/dv,size.z/dv); VData data_small(size2,data.GetValueFormat()); data_small.spacing = data.spacing*dv; for(int i=0;i<size2.x;i++) { for(int j=0;j<size2.y;j++) for(int k=0;k<size2.z;k++) { int sum = 0; for(int ii=0;ii<dv;ii++) for(int jj=0;jj<dv;jj++) for(int kk=0;kk<dv;kk++) sum += data.GetValue(i*dv+ii,j*dv+jj,k*dv+kk); data_small.SetValue(sum/float(dv*dv*dv), i,j,k); } Progress::inst->SetPercent(i/float(size2.x)); } return data_small; }
/// Constructor /// @param M :: A matrix to copy. GSLMatrix::GSLMatrix(const Kernel::Matrix<double> &M) { m_matrix = gsl_matrix_alloc(M.numRows(), M.numCols()); for (size_t i = 0; i < size1(); ++i) for (size_t j = 0; j < size2(); ++j) { set(i, j, M[i][j]); } }
/// Copy a column into a GSLVector /// @param i :: A column index. GSLVector GSLMatrix::copyColumn(size_t i) const { if (i >= size2()) { throw std::out_of_range("GSLMatrix column index is out of range."); } auto columnView = gsl_matrix_const_column(gsl(), i); return GSLVector(&columnView.vector); }
bool fullMatrix<double>::invert(fullMatrix<double> &result) const { int M = size1(), N = size2(), lda = size1(), info; int *ipiv = new int[std::min(M, N)]; if (result.size2() != M || result.size1() != N) { if (result._own_data || !result._data) result.resize(M,N,false); else Msg::Fatal("FullMatrix: Bad dimension, I cannot write in proxy"); } result.setAll(*this); F77NAME(dgetrf)(&M, &N, result._data, &lda, ipiv, &info); if(info == 0){ int lwork = M * 4; double *work = new double[lwork]; F77NAME(dgetri)(&M, result._data, &lda, ipiv, work, &lwork, &info); delete [] work; } delete [] ipiv; if(info == 0) return true; else if(info > 0) Msg::Error("U(%d,%d)=0 in matrix inversion", info, info); else Msg::Error("Wrong %d-th argument in matrix inversion", -info); return false; }
void run() { // Code from // http://llvm.org/svn/llvm-project/libcxx/trunk/test/utilities/intseq/intseq.general/integer_seq.pass.cpp // Make a couple of sequences using int3 = std::make_integer_sequence<int, 3>; // generates int: 0,1,2 using size7 = std::make_integer_sequence<size_t, 7>; // generates size_t: 0,1,2,3,4,5,6 using size4 = std::make_index_sequence<4>; // generates size_t: 0,1,2,3 using size2 = std::index_sequence_for<int, size_t>; // generates size_t: 0,1 using intmix = std::integer_sequence<int, 9, 8, 7, 2>; // generates int: 9,8,7,2 using sizemix = std::index_sequence<1, 1, 2, 3, 5>; // generates size_t: 1,1,2,3,5 // Make sure they're what we expect static_assert ( std::is_same <int3::value_type, int>::value, "int3 type wrong" ); static_assert ( int3::static_size == 3, "int3 size wrong" ); static_assert ( std::is_same <size7::value_type, size_t>::value, "size7 type wrong" ); static_assert ( size7::static_size == 7, "size7 size wrong" ); static_assert ( std::is_same <size4::value_type, size_t>::value, "size4 type wrong" ); static_assert ( size4::static_size == 4, "size4 size wrong" ); static_assert ( std::is_same <size2::value_type, size_t>::value, "size2 type wrong" ); static_assert ( size2::static_size == 2, "size2 size wrong" ); static_assert ( std::is_same <intmix::value_type, int>::value, "intmix type wrong" ); static_assert ( intmix::static_size == 4, "intmix size wrong" ); static_assert ( std::is_same <sizemix::value_type, size_t>::value, "sizemix type wrong" ); static_assert ( sizemix::static_size == 5, "sizemix size wrong" ); auto tup = std::make_tuple ( 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ); // Use them auto t3 = extract ( tup, int3() ); static_assert ( std::tuple_size<decltype(t3)>::value == int3::static_size, "t3 size wrong"); expect ( t3 == std::make_tuple ( 10, 11, 12 )); auto t7 = extract ( tup, size7 ()); static_assert ( std::tuple_size<decltype(t7)>::value == size7::static_size, "t7 size wrong"); expect ( t7 == std::make_tuple ( 10, 11, 12, 13, 14, 15, 16 )); auto t4 = extract ( tup, size4 ()); static_assert ( std::tuple_size<decltype(t4)>::value == size4::static_size, "t4 size wrong"); expect ( t4 == std::make_tuple ( 10, 11, 12, 13 )); auto t2 = extract ( tup, size2 ()); static_assert ( std::tuple_size<decltype(t2)>::value == size2::static_size, "t2 size wrong"); expect ( t2 == std::make_tuple ( 10, 11 )); auto tintmix = extract ( tup, intmix ()); static_assert ( std::tuple_size<decltype(tintmix)>::value == intmix::static_size, "tintmix size wrong"); expect ( tintmix == std::make_tuple ( 19, 18, 17, 12 )); auto tsizemix = extract ( tup, sizemix ()); static_assert ( std::tuple_size<decltype(tsizemix)>::value == sizemix::static_size, "tsizemix size wrong"); expect ( tsizemix == std::make_tuple ( 11, 11, 12, 13, 15 )); pass(); }
int main() { #if _LIBCPP_STD_VER > 11 // Make a couple of sequences using int3 = std::make_integer_sequence<int, 3>; // generates int: 0,1,2 using size7 = std::make_integer_sequence<size_t, 7>; // generates size_t: 0,1,2,3,4,5,6 using size4 = std::make_index_sequence<4>; // generates size_t: 0,1,2,3 using size2 = std::index_sequence_for<int, size_t>; // generates size_t: 0,1 using intmix = std::integer_sequence<int, 9, 8, 7, 2>; // generates int: 9,8,7,2 using sizemix = std::index_sequence<1, 1, 2, 3, 5>; // generates size_t: 1,1,2,3,5 // Make sure they're what we expect static_assert ( std::is_same<int3::value_type, int>::value, "int3 type wrong" ); static_assert ( int3::size () == 3, "int3 size wrong" ); static_assert ( std::is_same<size7::value_type, size_t>::value, "size7 type wrong" ); static_assert ( size7::size () == 7, "size7 size wrong" ); static_assert ( std::is_same<size4::value_type, size_t>::value, "size4 type wrong" ); static_assert ( size4::size () == 4, "size4 size wrong" ); static_assert ( std::is_same<size2::value_type, size_t>::value, "size2 type wrong" ); static_assert ( size2::size () == 2, "size2 size wrong" ); static_assert ( std::is_same<intmix::value_type, int>::value, "intmix type wrong" ); static_assert ( intmix::size () == 4, "intmix size wrong" ); static_assert ( std::is_same<sizemix::value_type, size_t>::value, "sizemix type wrong" ); static_assert ( sizemix::size () == 5, "sizemix size wrong" ); auto tup = std::make_tuple ( 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 ); // Use them auto t3 = extract ( tup, int3() ); static_assert ( std::tuple_size<decltype(t3)>::value == int3::size (), "t3 size wrong"); assert ( t3 == std::make_tuple ( 10, 11, 12 )); auto t7 = extract ( tup, size7 ()); static_assert ( std::tuple_size<decltype(t7)>::value == size7::size (), "t7 size wrong"); assert ( t7 == std::make_tuple ( 10, 11, 12, 13, 14, 15, 16 )); auto t4 = extract ( tup, size4 ()); static_assert ( std::tuple_size<decltype(t4)>::value == size4::size (), "t4 size wrong"); assert ( t4 == std::make_tuple ( 10, 11, 12, 13 )); auto t2 = extract ( tup, size2 ()); static_assert ( std::tuple_size<decltype(t2)>::value == size2::size (), "t2 size wrong"); assert ( t2 == std::make_tuple ( 10, 11 )); auto tintmix = extract ( tup, intmix ()); static_assert ( std::tuple_size<decltype(tintmix)>::value == intmix::size (), "tintmix size wrong"); assert ( tintmix == std::make_tuple ( 19, 18, 17, 12 )); auto tsizemix = extract ( tup, sizemix ()); static_assert ( std::tuple_size<decltype(tsizemix)>::value == sizemix::size (), "tsizemix size wrong"); assert ( tsizemix == std::make_tuple ( 11, 11, 12, 13, 15 )); #endif // _LIBCPP_STD_VER > 11 }
inline Eigen::Block<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic> > block(int start_row, int start_col, int rows, int cols) const { assert(is_allocated()); assert(start_row+rows<=size1()); assert(start_col+cols<=size2()); return values_.block(start_row, start_col, rows, cols); }
void CRSSparsity::spy(std::ostream &stream) const { for (int i=0;i<size1();++i) { for (int j=0;j<size2();++j) { stream << (getNZ(i,j)==-1? "." : "*"); } stream << std::endl; } }
double& el (int i, int j) { int dead1, dead2; if (j < size2 () && i-j < 1) return data ().el (i, j); barrier (); return zero_; }
bool fullMatrix<double>::luFactor(fullVector<int> &ipiv) { int M = size1(), N = size2(), lda = size1(), info; ipiv.resize(std::min(M, N)); F77NAME(dgetrf)(&M, &N, _data, &lda, &ipiv(0), &info); if(info == 0) return true; return false; }
void fullMatrix<double>::gemm(const fullMatrix<double> &a, const fullMatrix<double> &b, double alpha, double beta, bool transposeA, bool transposeB) { int M = size1(), N = size2(), K = transposeA ? a.size1() : a.size2(); int LDA = a.size1(), LDB = b.size1(), LDC = size1(); F77NAME(dgemm)(transposeA ? "T" : "N", transposeB ? "T" :"N", &M, &N, &K, &alpha, a._data, &LDA, b._data, &LDB, &beta, _data, &LDC); }
vcl_size_t size(vector_expression<LHS, const int, op_matrix_diag> const & proxy) { int k = proxy.rhs(); int A_size1 = static_cast<int>(size1(proxy.lhs())); int A_size2 = static_cast<int>(size2(proxy.lhs())); int row_depth = std::min(A_size1, A_size1 + k); int col_depth = std::min(A_size2, A_size2 - k); return std::min(row_depth, col_depth); }
/// Calculate the eigensystem of a symmetric matrix /// @param eigenValues :: Output variable that receives the eigenvalues of this /// matrix. /// @param eigenVectors :: Output variable that receives the eigenvectors of /// this matrix. void GSLMatrix::eigenSystem(GSLVector &eigenValues, GSLMatrix &eigenVectors) { size_t n = size1(); if (n != size2()) { throw std::runtime_error("Matrix eigenSystem: the matrix must be square."); } eigenValues.resize(n); eigenVectors.resize(n, n); auto workspace = gsl_eigen_symmv_alloc(n); gsl_eigen_symmv(gsl(), eigenValues.gsl(), eigenVectors.gsl(), workspace); gsl_eigen_symmv_free(workspace); }
int CRSSparsity::getNZ(int i, int j){ casadi_assert_message(i<size1() && j<size2(),"Indices out of bounds"); if (i<0) i += size1(); if (j<0) j += size2(); // Quick return if matrix is dense if(numel()==size()) return j+i*size2(); // Quick return if we are adding an element to the end if(rowind(i)==size() || (rowind(i+1)==size() && col().back()<j)){ vector<int>& colv = colRef(); vector<int>& rowindv = rowindRef(); colv.push_back(j); for(int ii=i; ii<size1(); ++ii){ rowindv[ii+1]++; } return colv.size()-1; } // go to the place where the element should be int ind; for(ind=rowind(i); ind<rowind(i+1); ++ind){ // better: loop from the back to the front if(col(ind) == j){ return ind; // element exists } else if(col(ind) > j) break; // break at the place where the element should be added } // Make sure that there no other objects are affected makeUnique(); // insert the element colRef().insert(colRef().begin()+ind,j); for(int row=i+1; row<size1()+1; ++row) rowindRef()[row]++; // Return the location of the new element return ind; }
/// Invert this matrix void GSLMatrix::invert() { if (size1() != size2()) { throw std::runtime_error("Matrix inverse: the matrix must be square."); } size_t n = size1(); int s; GSLMatrix LU(*this); gsl_permutation *p = gsl_permutation_alloc(n); gsl_linalg_LU_decomp(LU.gsl(), p, &s); gsl_linalg_LU_invert(LU.gsl(), p, this->gsl()); gsl_permutation_free(p); }
/** * @brief Read-write access to a single element of the matrix * * @param row_index Row index of accessed element * @param col_index Column index of accessed element * @return Proxy for matrix entry */ entry_proxy<SCALARTYPE> operator()(std::size_t row_index, std::size_t col_index) { assert(row_index < size1() && col_index < size2() && bool("Invalid access")); int index = static_cast<int>(col_index) - static_cast<int>(row_index); if (index < 0) index = -index; else if (index > 0) index = 2 * size1() - index; return elements_[index]; }
double spectral_norm_diag(const M &mat) { typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> matrix_t; const double cutoff = 1E-15; const int rows = size1(mat); const int cols = size2(mat); if (rows == 0 || cols == 0) { return 0.0; } matrix_t mat_tmp(rows, cols); double max_abs = -1; for (int j = 0; j < cols; ++j) { for (int i = 0; i < rows; ++i) { mat_tmp(i, j) = mat(i, j); max_abs = std::max(max_abs, std::abs(mat_tmp(i, j))); } } if (std::abs(max_abs) < 1E-300) { return 0.0; } else { const double coeff = 1.0 / max_abs; for (int j = 0; j < cols; ++j) { for (int i = 0; i < rows; ++i) { mat_tmp(i, j) *= coeff; if (std::abs(mat_tmp(i, j)) < cutoff) { mat_tmp(i, j) = 0.0; } } } if (mat_tmp.rows() > mat_tmp.cols()) { mat_tmp = mat_tmp.adjoint() * mat_tmp; } else { mat_tmp = mat_tmp * mat_tmp.adjoint(); } const double max_abs2 = mat_tmp.cwiseAbs().maxCoeff(); for (int j = 0; j < mat_tmp.cols(); ++j) { for (int i = 0; i < mat_tmp.rows(); ++i) { if (std::abs(mat_tmp(i, j)) < cutoff) { mat_tmp(i, j) = 0.0; } } } Eigen::SelfAdjointEigenSolver<matrix_t> esolv(mat_tmp, false); const double norm = std::sqrt(esolv.eigenvalues().cwiseAbs().maxCoeff()) / coeff; if (std::isnan(norm)) { std::cout << "Warning: spectral_norm_diag is NaN. max_abs = " << max_abs << " max_abs2 = " << max_abs2 << std::endl; return 0.0; } //assert(!isnan(norm)); return norm; } }
/// Calculate the determinant double GSLMatrix::det() { if (size1() != size2()) { throw std::runtime_error("Matrix inverse: the matrix must be square."); } size_t n = size1(); int s; GSLMatrix LU(*this); gsl_permutation *p = gsl_permutation_alloc(n); gsl_linalg_LU_decomp(LU.gsl(), p, &s); double res = gsl_linalg_LU_det(LU.gsl(), s); gsl_permutation_free(p); return res; }
/// Construct from an initialisation list /// @param ilist :: Initialisation list as a list of rows: /// {{M00, M01, M02, ...}, /// {M10, M11, M12, ...}, /// ... /// {Mn0, Mn1, Mn2, ...}} GSLMatrix::GSLMatrix(std::initializer_list<std::initializer_list<double>> ilist) : GSLMatrix(ilist.size(), ilist.begin()->size()) { for (auto row = ilist.begin(); row != ilist.end(); ++row) { if (row->size() != size2()) { throw std::runtime_error( "All rows in initializer list must have the same size."); } auto i = static_cast<size_t>(std::distance(ilist.begin(), row)); for (auto cell = row->begin(); cell != row->end(); ++cell) { auto j = static_cast<size_t>(std::distance(row->begin(), cell)); set(i, j, *cell); } } }
bool Brush::operator==(const Brush& brush) const { return size1() == brush.size1() && size2() == brush.size2() && qAbs(hardness1() - brush.hardness1()) <= 1.0/256.0 && qAbs(hardness2() - brush.hardness2()) <= 1.0/256.0 && qAbs(opacity1() - brush.opacity1()) <= 1.0/256.0 && qAbs(opacity2() - brush.opacity2()) <= 1.0/256.0 && color1() == brush.color1() && color2() == brush.color2() && spacing() == brush.spacing() && subpixel() == brush.subpixel() && incremental() == brush.incremental() && blendingMode() == brush.blendingMode(); }
void Reshape::evaluateMX(const MXPtrV& input, MXPtrV& output, const MXPtrVV& fwdSeed, MXPtrVV& fwdSens, const MXPtrVV& adjSeed, MXPtrVV& adjSens, bool output_given){ // Quick return if inplace if(input[0]==output[0]) return; if(!output_given){ *output[0] = reshape(*input[0],size1(),size2()); } // Forward sensitivities int nfwd = fwdSens.size(); for(int d = 0; d<nfwd; ++d){ *fwdSens[d][0] = reshape(*fwdSeed[d][0],size1(),size2()); } // Adjoint sensitivities int nadj = adjSeed.size(); for(int d=0; d<nadj; ++d){ MX& aseed = *adjSeed[d][0]; MX& asens = *adjSens[d][0]; asens += reshape(aseed,dep().size1(),dep().size2()); aseed = MX(); } }
void CWndNeuz::OnSize(UINT nType, int cx, int cy) { if(IsWndRoot()) return; if( m_bTile ) //m_strTexture.IsEmpty() == FALSE ) { CRect rectWnd = GetWndRect(); CSize size2( rectWnd.Width(), rectWnd.Height() ); CSize sizeDiv = size2; sizeDiv.cx %= 16; sizeDiv.cy %= 16; size2.cx /= 16; size2.cx *= 16; size2.cy /= 16; size2.cy *= 16; if( sizeDiv.cx ) size2.cx += 16; if( sizeDiv.cy ) size2.cy += 16; rectWnd.bottom = rectWnd.top + size2.cy; rectWnd.right = rectWnd.left + size2.cx; SetWndRect( rectWnd, FALSE ); } AdjustWndBase(); m_wndTitleBar.Replace(); CWndBase::OnSize( nType, cx, cy ); // if( rectOld.Width() != m_rectClient.Width() || rectOld.Height() != m_rectClient.Height() ) // 차일드 윈도우들의 사이즈를 조절 /* for(int i = 0; i < m_wndArray.GetSize(); i++) { CWndBase* pWnd = (CWndBase*)m_wndArray.GetAt(i); if(pWnd->IsWndStyle(WBS_DOCKED))// && pWnd->IsWndStyle(WBS_CHILD)) { CRect rect = pWnd->GetWindowRect(TRUE); rect.SetRect(0,0,cx,cy); pWnd->SetWndRect(rect); } } */ /* if(IsWndStyle(WBS_DOCKED))// && IsWndStyle(WBS_CHILD)) { m_wndTitleBar.m_wndMinimize.SetVisible(FALSE); m_wndTitleBar.m_wndMaximize.SetVisible(FALSE); } */ }
int main() { int s1, s2; printf("size1: %d\n", s1=size1()); printf("size2: %d\n", s2=size2()); printf("(correct output is 8, then 12)\n"); if (s1==8 && s2==12) { return 0; } else { return 2; } }
double spectral_norm_SVD(const M &mat) { typedef Eigen::Matrix<SCALAR, Eigen::Dynamic, Eigen::Dynamic> matrix_t; const double cutoff = 1E-15; const int rows = size1(mat); const int cols = size2(mat); if (rows == 0 || cols == 0) { return 0.0; } matrix_t mat_tmp(rows, cols); double max_abs = -1; for (int j = 0; j < cols; ++j) { for (int i = 0; i < rows; ++i) { mat_tmp(i, j) = mat(i, j); max_abs = std::max(max_abs, std::abs(mat_tmp(i, j))); } } if (max_abs == 0.0) { return 0.0; } const double coeff = 1.0 / max_abs; for (int j = 0; j < cols; ++j) { for (int i = 0; i < rows; ++i) { mat_tmp(i, j) *= coeff; if (std::abs(mat_tmp(i, j)) < cutoff) { mat_tmp(i, j) = 0.0; } } } //const double tmp = mat_tmp.squaredNorm(); Eigen::JacobiSVD<matrix_t> svd(mat_tmp); #ifndef NDEBUG const int size_SVD = svd.singularValues().size(); for (int i = 0; i < size_SVD - 1; ++i) { assert(std::abs(svd.singularValues()[i]) >= std::abs(svd.singularValues()[i + 1])); } if (isnan(std::abs(svd.singularValues()[0]) / coeff)) { std::cout << "Norm is Nan" << std::endl; std::cout << "max_abs is " << max_abs << std::endl; std::cout << "coeff is " << coeff << std::endl; std::cout << "mat is " << std::endl << mat << std::endl; std::cout << "mat_tmp is " << std::endl << mat_tmp << std::endl; exit(-1); } #endif const double norm = std::abs(svd.singularValues()[0]) / coeff; assert(!isnan(norm)); return norm; }
bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S) { fullMatrix<double> VT(V.size2(), V.size1()); int M = size1(), N = size2(), LDA = size1(), LDVT = VT.size1(), info; int lwork = std::max(3 * std::min(M, N) + std::max(M, N), 5 * std::min(M, N)); fullVector<double> WORK(lwork); F77NAME(dgesvd)("O", "A", &M, &N, _data, &LDA, S._data, _data, &LDA, VT._data, &LDVT, WORK._data, &lwork, &info); V = VT.transpose(); if(info == 0) return true; if(info > 0) Msg::Error("SVD did not converge"); else Msg::Error("Wrong %d-th argument in SVD decomposition", -info); return false; }
/// Matrix by vector multiplication /// @param v :: A vector to multiply by. Must have the same size as size2(). /// @returns A vector - the result of the multiplication. Size of the returned /// vector equals size1(). /// @throws std::invalid_argument if the input vector has a wrong size. /// @throws std::runtime_error if the underlying GSL routine fails. GSLVector GSLMatrix::operator*(const GSLVector &v) const { if (v.size() != size2()) { throw std::invalid_argument( "Matrix by vector multiplication: wrong size of vector."); } GSLVector res(size1()); auto status = gsl_blas_dgemv(CblasNoTrans, 1.0, gsl(), v.gsl(), 0.0, res.gsl()); if (status != GSL_SUCCESS) { std::string message = "Failed to multiply matrix by a vector.\n" "Error message returned by the GSL:\n" + std::string(gsl_strerror(status)); throw std::runtime_error(message); } return res; }
void FBOTexture::config(fzTextureFormat format, fzSize size, const fzPoint& anchorPoint, fzFloat quality) { FZ_ASSERT(getTexture() == NULL, "FBOTexture is already configured."); fzFloat factor = Director::Instance().getResourcesFactor() * quality; Texture2D *texture = new Texture2D(format, size * factor); texture->setFactor(factor); texture->setAliasTexParameters(); m_grabber.grab(texture); // generate transform fzSize size2(size.width * anchorPoint.x, size.height * anchorPoint.y); // Inverted projection fzMath_mat4OrthoProjection(-size2.width, size.width-size2.width, size.height-size2.height, -size2.height, -1500, 1500, m_transform); }
/// Solve system of linear equations M*x == rhs, M is this matrix /// This matrix is destroyed. /// @param rhs :: The right-hand-side vector /// @param x :: The solution vector void GSLMatrix::solve(const GSLVector &rhs, GSLVector &x) { if (size1() != size2()) { throw std::runtime_error( "System of linear equations: the matrix must be square."); } size_t n = size1(); if (rhs.size() != n) { throw std::runtime_error( "System of linear equations: right-hand side vector has wrong size."); } x.resize(n); int s; gsl_permutation *p = gsl_permutation_alloc(n); gsl_linalg_LU_decomp(gsl(), p, &s); // matrix is modified at this moment gsl_linalg_LU_solve(gsl(), p, rhs.gsl(), x.gsl()); gsl_permutation_free(p); }