本文整理汇总了C++中viennacl::matrix类的典型用法代码示例。如果您正苦于以下问题:C++ matrix类的具体用法?C++ matrix怎么用?C++ matrix使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了matrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: luT
double luT(
viennacl::matrix<T> &vclX,
viennacl::vector_base<T> &vclD) {
double logdet=-99.9;
viennacl::linalg::lu_factorize(vclX);
// pointer to the actual diagonal
viennacl::vector_base<T> diagOfVar(
vclX.handle(), vclX.size1(), 0,
vclX.internal_size2() + 1);
// compute log determinant
vclD = viennacl::linalg::element_log(diagOfVar);
logdet = viennacl::linalg::sum(vclD);
// OPERATION_UNARY_LOG_TYPE
//http://viennacl.sourceforge.net/doc/scheduler_8cpp-example.html#a11
// put the diagonals in D, and 1's on the diagonal of L
vclD = diagOfVar;
//diagOfVar = T(1); // problem here
return(logdet);
}
示例2: init_random
void init_random(viennacl::matrix<T, F> & M)
{
std::vector<T> cM(M.internal_size());
for (std::size_t i = 0; i < M.size1(); ++i)
for (std::size_t j = 0; j < M.size2(); ++j)
cM[F::mem_index(i, j, M.internal_size1(), M.internal_size2())] = T(rand())/T(RAND_MAX);
viennacl::fast_copy(&cM[0],&cM[0] + cM.size(),M);
}
示例3: matrix_print
void matrix_print(viennacl::matrix<ScalarType, MatrixLayout>& A)
{
for (unsigned int i = 0; i < A.size1(); i++) {
for (unsigned int j = 0; j < A.size2(); j++)
std::cout << std::fixed << A(i, j) << "\t";
std::cout << "\n";
}
}
示例4: fill_matrix
void fill_matrix(viennacl::matrix<NumericT> & mat)
{
for (std::size_t i = 0; i < mat.size1(); ++i)
{
for (std::size_t j = 0; j < mat.size2(); ++j)
mat(i, j) = static_cast<NumericT>(-0.5) * random<NumericT>();
mat(i, i) = NumericT(1.0) + NumericT(2.0) * random<NumericT>(); //some extra weight on diagonal for stability
}
}
示例5: matrix_print
void matrix_print(viennacl::matrix<ScalarType>& A_orig)
{
ublas::matrix<ScalarType> A(A_orig.size1(), A_orig.size2());
viennacl::copy(A_orig, A);
std::cout << "matrix_print!\n";
for (unsigned int i = 0; i < A.size1(); i++) {
for (unsigned int j = 0; j < A.size2(); j++)
std::cout << A(i, j) << "\t";
std::cout << "\n";
}
}
示例6: sharedCol
viennacl::vector_range<viennacl::vector_base<T> > sharedCol(){
// viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->internal_size(), 0, 1);
// std::cout << "returning column" << std::endl;
viennacl::vector_base<T> tmp(ptr_matrix->handle(), ptr_matrix->size1(), begin, ptr_matrix->internal_size2());
// std::cout << "got column" << std::endl;
viennacl::vector_range<viennacl::vector_base<T> > v_sub(tmp, r);
// std::cout << "got range" << std::endl;
return v_sub;
}
示例7: test_eigen_val_vec
bool test_eigen_val_vec(viennacl::matrix<ScalarType, MatrixLayout> & Q,
std::vector<ScalarType> & eigenvalues,
std::vector<ScalarType> & d,
std::vector<ScalarType> & e)
{
unsigned int Q_size = Q.size2();
ScalarType value = 0;
for(unsigned int j = 0; j < Q_size; j++)
{
// calculate first row
value = (d[0]- eigenvalues[j]) * Q(0, j) + e[1] * Q(1, j);
if(value > EPS)
return false;
// calcuate inner rows
for(unsigned int i = 1; i < Q_size - 1; i++)
{
value = e[i] * Q(i - 1, j) + (d[i]- eigenvalues[j]) * Q(i, j) + e[i + 1] * Q(i + 1, j);
if(value > EPS)
return false;
}
// calculate last row
value = e[Q_size - 1] * Q(Q_size - 2, j) + (d[Q_size - 1] - eigenvalues[j]) * Q(Q_size - 1, j);
if(value > EPS)
return false;
}
return true;
}
示例8: check_tridiag
bool check_tridiag(viennacl::matrix<ScalarType, MatrixLayout>& A_orig)
{
ublas::matrix<ScalarType> A(A_orig.size1(), A_orig.size2());
viennacl::copy(A_orig, A);
for (unsigned int i = 0; i < A.size1(); i++) {
for (unsigned int j = 0; j < A.size2(); j++) {
if ((std::abs(A(i, j)) > EPS) && ((i - 1) != j) && (i != j) && ((i + 1) != j))
{
// std::cout << "Failed at " << i << " " << j << " " << A(i, j) << "\n";
return false;
}
}
}
return true;
}
示例9: check_hessenberg
bool check_hessenberg(viennacl::matrix<ScalarType, MatrixLayout>& A_orig)
{
ublas::matrix<ScalarType> A(A_orig.size1(), A_orig.size2());
viennacl::copy(A_orig, A);
for (std::size_t i = 0; i < A.size1(); i++) {
for (std::size_t j = 0; j < A.size2(); j++) {
if ((std::abs(A(i, j)) > EPS) && (i > (j + 1)))
{
// std::cout << "Failed at " << i << " " << j << " " << A(i, j) << "\n";
return false;
}
}
}
return true;
}
示例10: matrix_compare
float matrix_compare(viennacl::matrix<ScalarType>& res,
viennacl::matrix<ScalarType>& ref)
{
std::vector<ScalarType> res_std(res.internal_size());
std::vector<ScalarType> ref_std(ref.internal_size());
viennacl::fast_copy(res, &res_std[0]);
viennacl::fast_copy(ref, &ref_std[0]);
float diff = 0.0;
float mx = 0.0;
for(std::size_t i = 0; i < res_std.size(); i++) {
diff = std::max(diff, std::abs(res_std[i] - ref_std[i]));
mx = std::max(mx, res_std[i]);
}
return diff / mx;
}
示例11: diff
ScalarType diff(ublas::matrix<ScalarType> & mat1, viennacl::matrix<ScalarType, F, ALIGNMENT> & mat2)
{
ublas::matrix<ScalarType> mat2_cpu(mat2.size1(), mat2.size2());
copy(mat2, mat2_cpu);
ScalarType ret = 0;
ScalarType act = 0;
for (unsigned int i = 0; i < mat2_cpu.size1(); ++i)
{
for (unsigned int j = 0; j < mat2_cpu.size2(); ++j)
{
act = fabs(mat2_cpu(i,j) - mat1(i,j)) / std::max( fabs(mat2_cpu(i, j)), fabs(mat1(i,j)) );
if (act > ret)
ret = act;
}
}
//std::cout << ret << std::endl;
return ret;
}
示例12: read_matrix_body
void read_matrix_body(std::fstream& f, viennacl::matrix<ScalarType, MatrixLayout>& A)
{
if(!f.is_open())
{
throw std::invalid_argument("File is not opened");
}
boost::numeric::ublas::matrix<ScalarType> h_A(A.size1(), A.size2());
for(std::size_t i = 0; i < h_A.size1(); i++) {
for(std::size_t j = 0; j < h_A.size2(); j++) {
ScalarType val = 0.0;
f >> val;
h_A(i, j) = val;
}
}
viennacl::copy(h_A, A);
}
示例13: runNormalization
FeatureNormalize<Scalar>::FeatureNormalize(const viennacl::matrix<Scalar> &X):
blas_matrix_(X.size1(), X.size2()),
normalized_matrix_(X.size1(), X.size2()),
mu_(X.size2()), sigma_(X.size2()) {
viennacl::copy(X, blas_matrix_);
runNormalization();
}
示例14: check_bidiag
bool check_bidiag(viennacl::matrix<ScalarType>& A)
{
const ScalarType EPS = 0.0001f;
std::vector<ScalarType> aA(A.size1() * A.size2());
viennacl::fast_copy(A, &aA[0]);
for(std::size_t i = 0; i < A.size1(); i++)
{
for(std::size_t j = 0; j < A.size2(); j++)
{
ScalarType val = aA[i * A.size2() + j];
if((fabs(val) > EPS) && (i != j) && ((i + 1) != j))
{
std::cout << "Failed at " << i << " " << j << " " << val << std::endl;
return false;
}
}
}
return true;
}
示例15: svd
void svd(viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & A,
viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & QL,
viennacl::matrix<SCALARTYPE, row_major, ALIGNMENT> & QR)
{
viennacl::ocl::context & ctx = const_cast<viennacl::ocl::context &>(viennacl::traits::opencl_handle(A).context());
viennacl::linalg::opencl::kernels::svd<SCALARTYPE>::init(ctx);
vcl_size_t row_num = A.size1();
vcl_size_t col_num = A.size2();
vcl_size_t to = std::min(row_num, col_num);
//viennacl::vector<SCALARTYPE, ALIGNMENT> d(to);
//viennacl::vector<SCALARTYPE, ALIGNMENT> s(to + 1);
// first stage
detail::bidiag(A, QL, QR);
// second stage
//std::vector<SCALARTYPE> dh(to, 0);
//std::vector<SCALARTYPE> sh(to + 1, 0);
boost::numeric::ublas::vector<SCALARTYPE> dh = boost::numeric::ublas::scalar_vector<SCALARTYPE>(to, 0);
boost::numeric::ublas::vector<SCALARTYPE> sh = boost::numeric::ublas::scalar_vector<SCALARTYPE>(to + 1, 0);
viennacl::linalg::opencl::bidiag_pack_svd(A, dh, sh);
detail::svd_qr_shift( QL, QR, dh, sh);
// Write resulting diagonal matrix with singular values to A:
boost::numeric::ublas::matrix<SCALARTYPE> h_Sigma(row_num, col_num);
h_Sigma.clear();
for (vcl_size_t i = 0; i < to; i++)
h_Sigma(i, i) = dh[i];
copy(h_Sigma, A);
}