当前位置: 首页>>代码示例>>C++>>正文


C++ SparseMatrix::LockedDataBuffer方法代码示例

本文整理汇总了C++中SparseMatrix::LockedDataBuffer方法的典型用法代码示例。如果您正苦于以下问题:C++ SparseMatrix::LockedDataBuffer方法的具体用法?C++ SparseMatrix::LockedDataBuffer怎么用?C++ SparseMatrix::LockedDataBuffer使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在SparseMatrix的用法示例。


在下文中一共展示了SparseMatrix::LockedDataBuffer方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: OneNorm

T OneNorm(const SparseMatrix<T>& A)
{
    // compute the max absolute column sum
    const unsigned int* cols_a = A.LockedColBuffer();
    const T*            data_a = A.LockedDataBuffer();
    const unsigned int width_a = A.Width();

    T max_col_sum = T(0), col_sum;
    for (unsigned int c=0; c != width_a; ++c)
    {
        unsigned int start = cols_a[c];
        unsigned int   end = cols_a[c+1];
        col_sum = T(0);
        for (unsigned int offset=start; offset != end; ++offset)
        {
            T val = fabs(data_a[offset]);
            col_sum += val;
        }

        if (col_sum > max_col_sum)
            max_col_sum = col_sum;
    }

    return max_col_sum;
}
开发者ID:beckgom,项目名称:smallk,代码行数:25,代码来源:sparse_matrix_norms.hpp

示例2: FrobeniusNorm

T FrobeniusNorm(const SparseMatrix<T>& A)
{
    // compute the sum of the absolute value squared of each element
    const T*           data_a = A.LockedDataBuffer();
    const unsigned int size_a = A.Size();

    T sum = T(0);
    for (unsigned int i=0; i != size_a; ++i)
    {
        T val = fabs(data_a[i]);
        sum += val*val;
    }

    return sqrt(sum);
}
开发者ID:beckgom,项目名称:smallk,代码行数:15,代码来源:sparse_matrix_norms.hpp

示例3: MaxNorm

T MaxNorm(const SparseMatrix<T>& A)
{
    // find max( |A_ij| )
    const T*           data_a = A.LockedDataBuffer();
    const unsigned int size_a = A.Size();

    T max_norm = T(0);
    for (unsigned int i=0; i != size_a; ++i)
    {
        T val = fabs(data_a[i]);
        if (val > max_norm)
            max_norm = val;
    }

    return max_norm;
}
开发者ID:beckgom,项目名称:smallk,代码行数:16,代码来源:sparse_matrix_norms.hpp

示例4: WriteMatrixMarketFile

bool WriteMatrixMarketFile(const std::string& file_path,
                           const SparseMatrix<T>& A,
                           const unsigned int precision)
{
    // Write a MatrixMarket file with no comments.  Note that the
    // MatrixMarket format uses 1-based indexing for rows and columns.

    std::ofstream outfile(file_path);
    if (!outfile)
        return false;

    unsigned int height = A.Height();
    unsigned int width  = A.Width();
    unsigned int nnz    = A.Size();
    
    // write the 'banner'
    outfile << MM_BANNER << " matrix coordinate real general" << std::endl;

    // write matrix dimensions and number of nonzeros
    outfile << height << " " << width << " " << nnz << std::endl;

    outfile << std::fixed;
    outfile.precision(precision);
    
    const unsigned int* cols_a = A.LockedColBuffer();
    const unsigned int* rows_a = A.LockedRowBuffer();
    const T*            data_a = A.LockedDataBuffer();
    unsigned int width_a = A.Width();

    for (unsigned int c=0; c != width_a; ++c)
    {
        unsigned int start = cols_a[c];
        unsigned int end   = cols_a[c+1];
        for (unsigned int offset=start; offset != end; ++offset)
        {
            unsigned int r = rows_a[offset];
            T val = data_a[offset];
            outfile << r+1 << " " << c+1 << " " << val << std::endl;
        }
    }

    outfile.close();
    return true;
}
开发者ID:beckgom,项目名称:smallk,代码行数:44,代码来源:sparse_matrix_io.hpp

示例5: Print

void Print(const SparseMatrix<T>& M)
{
    // Print a SparseMatrix to the screen.

    const unsigned int* col_buf = M.LockedColBuffer();
    const unsigned int* row_buf = M.LockedRowBuffer();
    const T*                buf = M.LockedDataBuffer();

    if (0 == M.Size())
    {
        std::cout << "Matrix is empty." << std::endl;
        return;
    }

    for (unsigned int c=0; c != M.Width(); ++c)
    {
        unsigned int start = col_buf[c];
        unsigned int end   = col_buf[c+1];
        for (unsigned int offset=start; offset != end; ++offset)
        {
            assert(offset >= 0);
            assert(offset < M.Size());
            unsigned int row_index = row_buf[offset];
            T                 data = buf[offset];
            std::cout << "(" << row_index << ", " << c << "): " << data << std::endl;
        }
    }

    std::cout << "Col indices: "; std::cout.flush();
    for (unsigned int i=0; i != M.Width(); ++i)
        std::cout << col_buf[i] << ", ";
    std::cout << col_buf[M.Width()] << std::endl;

    std::cout << "Row indices: "; std::cout.flush();
    for (unsigned int i=0; i != M.Size(); ++i)
        std::cout << row_buf[i] << ", ";
    std::cout << std::endl;

    std::cout << "Data:        "; std::cout.flush();
    for (unsigned int i=0; i != M.Size(); ++i)
        std::cout << buf[i] << ", ";
    std::cout << std::endl;
}
开发者ID:beckgom,项目名称:smallk,代码行数:43,代码来源:sparse_matrix_io.hpp

示例6: Nmf


//.........这里部分代码省略.........

    cout << "Initializing matrix H..." << endl;
    if (csv_file_h.empty())
        ok = RandomMatrix(&buf_h[0], ldim_h, k, n, rng);
    else
        ok = LoadDelimitedFile(buf_h, height_h, width_h, csv_file_h);

    if (!ok)
    {
        std::ostringstream msg;
        msg << "smallk error (Nmf): load failed for file ";
        msg << "\"" << csv_file_h << "\"";
        throw std::runtime_error(msg.str());
    }
    
    if ( (height_h != k) || (width_h != n))
    {
        cerr << "\tdimensions of matrix H are " << height_h
             << " x " << width_h << endl;
        cerr << "\texpected " << k << " x " << n << endl;
        throw std::logic_error("smallk error (Nmf): non-conformant matrix H.");
    }    

    // The ratio of projected gradient norms doesn't seem to work very well
    // with MU.  We frequently observe a 'leveling off' behavior and the 
    // convergence is even slower than usual.  So for MU use the relative
    // change in the Frobenius norm of W as the stopping criterion, which
    // always seems to behave well, even though it is on shaky theoretical
    // ground.

    if (NmfAlgorithm::MU == nmf_opts.algorithm)
        nmf_opts.prog_est_algorithm = NmfProgressAlgorithm::DELTA_FNORM;
    else
        nmf_opts.prog_est_algorithm = NmfProgressAlgorithm::PG_RATIO;

    nmf_opts.tol         = nmf_tolerance;
    nmf_opts.height      = m;
    nmf_opts.width       = n;
    nmf_opts.k           = k;
    nmf_opts.min_iter    = min_iter;
    nmf_opts.max_iter    = max_iter;
    nmf_opts.tolcount    = 1;
    nmf_opts.max_threads = max_threads;
    nmf_opts.verbose     = true;
    nmf_opts.normalize   = true;

    // display all params to user
    PrintNmfOpts(nmf_opts);

    NmfStats stats;
    Result result;
    if (is_sparse)
    {
        result = NmfSparse(nmf_opts, 
                           A.Height(), A.Width(), A.Size(),
                           A.LockedColBuffer(),
                           A.LockedRowBuffer(),
                           A.LockedDataBuffer(),
                           &buf_w[0], ldim_w,
                           &buf_h[0], ldim_h,
                           stats);
    }
    else
    {
        result = Nmf(nmf_opts,
                     &buf_a[0], ldim_a,
                     &buf_w[0], ldim_w,
                     &buf_h[0], ldim_h,
                     stats);
    }

    cout << "Elapsed wall clock time: ";
    cout << ElapsedTime(stats.elapsed_us) << endl;
    cout << endl;

    if (Result::OK != result)
        throw std::runtime_error("smallk error (Nmf): NMF solver failure.");

    // write the computed W and H factors to disk

    std::string outfile_w, outfile_h;
    if (outdir.empty())
    {
        outfile_w = DEFAULT_FILENAME_W;
        outfile_h = DEFAULT_FILENAME_H;
    }
    else
    {
        outfile_w = outdir + DEFAULT_FILENAME_W;
        outfile_h = outdir + DEFAULT_FILENAME_H;
    }

    cout << "Writing output files..." << endl;
    
    if (!WriteDelimitedFile(&buf_w[0], ldim_w, m, k, outfile_w, outprecision))
        throw std::runtime_error("smallk error (Nmf): could not write W result.");
    
    if (!WriteDelimitedFile(&buf_h[0], ldim_h, k, n, outfile_h, outprecision))
        throw std::runtime_error("smallk error (Nmf): could not write H result.");
}
开发者ID:jtitusj,项目名称:smallk,代码行数:101,代码来源:smallk.cpp


注:本文中的SparseMatrix::LockedDataBuffer方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。