예제 #1
0
 DataType& SparseStorage<DataType>::elem(casadi_int rr, casadi_int cc) {
   casadi_int oldsize = sparsity().nnz();
   casadi_int ind = sparsity_.add_nz(rr, cc);
   if (oldsize != sparsity().nnz())
     nonzeros().insert(nonzeros().begin()+ind, DataType(0));
   return nonzeros().at(ind);
 }
예제 #2
0
파일: blaze.hpp 프로젝트: tkoziara/parmec
    /// Copy matrix from builtin backend.
    static std::shared_ptr<matrix>
    copy_matrix(
            std::shared_ptr< typename builtin<real>::matrix > A,
            const params&
            )
    {
        typedef
            typename row_iterator<typename builtin<real>::matrix>::type
            row_iterator;

        const size_t n = rows(*A);
        const size_t m = cols(*A);

        auto B = std::make_shared<matrix>(n, m);

        B->reserve(nonzeros(*A));
        for(size_t i = 0; i < n; ++i) {
            for(row_iterator a = A->row_begin(i); a; ++a) {
                B->append(i, a.col(), a.value());
            }
            B->finalize(i);
        }

        return B;
    }
예제 #3
0
 		row_column_filtered_matrix(const Matrix &m, RowColFilterMap fm,
			size_type nrows, size_type ncols)
			: _nzi(m, fm, nonzeros(m)), 
			  _nziend(m, fm, std::make_pair(nonzeros(m).second, nonzeros(m).second)),
			  _nrows(nrows), _ncols(nrows), _nnz(0)
		{
			nonzero_iterator i = _nzi;

			_nnz = 0;
			
			while (i != _nziend)
			{
				++i;
				++_nnz;
			}
		}
예제 #4
0
        pointwise_aggregates(const Matrix &A, const params &prm) : count(0)
        {
            if (prm.block_size == 1) {
                plain_aggregates aggr(A, prm);

                count = aggr.count;
                strong_connection.swap(aggr.strong_connection);
                id.swap(aggr.id);
            } else {
                strong_connection.resize( nonzeros(A) );
                id.resize( rows(A) );

                Matrix Ap = pointwise_matrix(A, prm.block_size);

                plain_aggregates pw_aggr(Ap, prm);
                count = pw_aggr.count * prm.block_size;

#pragma omp parallel
                {
                    std::vector<ptrdiff_t> marker(Ap.nrows, -1);

#ifdef _OPENMP
                    int nt  = omp_get_num_threads();
                    int tid = omp_get_thread_num();

                    size_t chunk_size  = (Ap.nrows + nt - 1) / nt;
                    size_t chunk_start = tid * chunk_size;
                    size_t chunk_end   = std::min(Ap.nrows, chunk_start + chunk_size);
#else
                    size_t chunk_start = 0;
                    size_t chunk_end   = Ap.nrows;
#endif

                    for(size_t ip = chunk_start, ia = ip * prm.block_size; ip < chunk_end; ++ip) {
                        ptrdiff_t row_beg = Ap.ptr[ip];
                        ptrdiff_t row_end = row_beg;

                        for(unsigned k = 0; k < prm.block_size; ++k, ++ia) {
                            id[ia] = prm.block_size * pw_aggr.id[ip] + k;

                            for(ptrdiff_t ja = A.ptr[ia], ea = A.ptr[ia+1]; ja < ea; ++ja) {
                                ptrdiff_t cp = A.col[ja] / prm.block_size;

                                if (marker[cp] < row_beg) {
                                    marker[cp] = row_end;
                                    strong_connection[ja] = pw_aggr.strong_connection[row_end];
                                    ++row_end;
                                } else {
                                    strong_connection[ja] = pw_aggr.strong_connection[ marker[cp] ];
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #5
0
		row_column_filtered_matrix(const Matrix &m, RowColFilterMap fm)
			: _nzi(m, fm, nonzeros(m)), 
			  _nziend(m, fm, std::make_pair(nonzeros(m).second, nonzeros(m).second)),
			  _nrows(0), _ncols(0), _nnz(0)
		{
			nonzero_iterator i = _nzi;

			_nnz = 0;
			
			while (i != _nziend)
			{
				_nrows = std::max(row(*i,m),_nrows);
				_ncols = std::max(column(*i,m),_ncols);

				++i;
				++_nnz;
			}

			// we underestimate by one value
			++_nrows;
			++_ncols;
		}
예제 #6
0
파일: eigen.hpp 프로젝트: HongLi15/amgcl
    /// Copy matrix from builtin backend.
    static boost::shared_ptr<matrix>
    copy_matrix(boost::shared_ptr< typename builtin<real>::matrix > A, const params&)
    {
        const typename builtin<real>::matrix &a = *A;

        BOOST_AUTO(Aptr, a.ptr_data());
        BOOST_AUTO(Acol, a.col_data());
        BOOST_AUTO(Aval, a.val_data());

        return boost::shared_ptr<matrix>(
                new matrix(
                    rows(*A), cols(*A), nonzeros(*A),
                    const_cast<index_type*>(Aptr),
                    const_cast<index_type*>(Acol),
                    const_cast<value_type*>(Aval)
                    ),
                hold_host(A)
                );
    }
예제 #7
0
        matrix_row_col_graph_nz_iterator(Matrix& m,bool)
			: _m(&m), _repeat(true), _nr(nrows(m))
        { 
			i = nonzeros(m).second;
		}
예제 #8
0
        matrix_row_col_graph_nz_iterator(Matrix& m)
			: _m(&m), _repeat(true), _nr(nrows(m))
        { 
			i = nonzeros(m).first;
		}
예제 #9
0
 void SparseStorage<DataType>::clear() {
   sparsity_ = Sparsity(0, 0);
   nonzeros().clear();
 }
예제 #10
0
 void SparseStorage<DataType>::reserve(casadi_int nnz, casadi_int ncol) {
   nonzeros().reserve(nnz);
 }
예제 #11
0
        pointwise_aggregates(const Matrix &A, const params &prm, unsigned min_aggregate)
            : count(0)
        {
            typedef typename backend::value_type<Matrix>::type value_type;
            typedef typename math::scalar_of<value_type>::type scalar_type;
            if (prm.block_size == 1) {
                plain_aggregates aggr(A, prm);

                remove_small_aggregates(A.nrows, 1, min_aggregate, aggr);

                count = aggr.count;
                strong_connection.swap(aggr.strong_connection);
                id.swap(aggr.id);
            } else {
                strong_connection.resize( nonzeros(A) );
                id.resize( rows(A) );

                auto ap = backend::pointwise_matrix(A, prm.block_size);
                backend::crs<scalar_type> &Ap = *ap;

                plain_aggregates pw_aggr(Ap, prm);

                remove_small_aggregates(
                        Ap.nrows, prm.block_size, min_aggregate, pw_aggr);

                count = pw_aggr.count * prm.block_size;

#pragma omp parallel
                {
                    std::vector<ptrdiff_t> j(prm.block_size);
                    std::vector<ptrdiff_t> e(prm.block_size);

#pragma omp for
                    for(ptrdiff_t ip = 0; ip < static_cast<ptrdiff_t>(Ap.nrows); ++ip) {
                        ptrdiff_t ia = ip * prm.block_size;

                        for(unsigned k = 0; k < prm.block_size; ++k, ++ia) {
                            id[ia] = prm.block_size * pw_aggr.id[ip] + k;

                            j[k] = A.ptr[ia];
                            e[k] = A.ptr[ia+1];
                        }

                        for(ptrdiff_t jp = Ap.ptr[ip], ep = Ap.ptr[ip+1]; jp < ep; ++jp) {
                            ptrdiff_t cp = Ap.col[jp];
                            bool      sp = (cp == ip) || pw_aggr.strong_connection[jp];

                            ptrdiff_t col_end = (cp + 1) * prm.block_size;

                            for(unsigned k = 0; k < prm.block_size; ++k) {
                                ptrdiff_t beg = j[k];
                                ptrdiff_t end = e[k];

                                while(beg < end && A.col[beg] < col_end) {
                                    strong_connection[beg] = sp && A.col[beg] != (ia + k);
                                    ++beg;
                                }

                                j[k] = beg;
                            }
                        }
                    }
                }
            }
        }
예제 #12
0
		row_column_filtered_matrix(const Matrix &m, RowColFilterMap fm,
			size_type nrows, size_type ncols, size_type nnz)
			: _nzi(m, fm, nonzeros(m)), 
			  _nziend(m, fm, std::make_pair(nonzeros(m).second, nonzeros(m).second)),
			  _nrows(nrows), _ncols(nrows), _nnz(nnz)
		{}
예제 #13
0
SegImage* MeanFiller::fillDynamic(int startX, int startY, int startZ, int startRadius)
{
	int cols, rows, slices;
	int minx, miny, minz, maxx, maxy, maxz;
	cube sample, xes;
	vec values;
	stack<triple> historyEntity;
	image->getSize(cols, rows, slices);
	cube result = zeros(rows, cols, slices);
	cube sphere = Utils::sphere(startRadius);
	result(startY, startX, startZ, arma::size(sphere)) = sphere;
	res_image = Utils::convert(result);
	Utils::bounds(result, minx, miny, minz, maxx, maxy, maxz);
	double thres;
	history.clear();
	result.reset();

	if (minx < 3) minx = 3;
	if (maxx > cols - 2) maxx = cols - 2;
	if (miny < 3) miny = 3;
	if (maxy > rows - 2) maxy = rows - 2;
	if (minz < 3) minz = 3;
	if (maxz > slices - 2) maxz = slices - 2;
	bool flag = false;

	while (!flag)
	{
		flag = true;

		xes = Utils::convert_d(image->getRegion(minx, miny, maxx, maxy, minz, maxz));
		sample = wBright * Utils::convert_d(res_image->getRegion(minx, miny, maxx, maxy, minz, maxz)) + 
			wCurv * xes;
		sample = Utils::conv3(Utils::conv3(sample, kernel), kernel);
		sample %= -xes + 1;
		values = sort(nonzeros(vectorise(sample)));
		thres = values(quantile * values.size());

		for (int i = minx; i < maxx; i++)
		{
			for (int j = miny; j < maxy; j++)
			{
				for (int k = minz; k < maxz; k++)
				{
					if (image->getVoxel(i, j, k) > 0 && res_image->getVoxel(i, j, k) == 0)
					{
						if (sample(j-miny, i - minx, k - minz) > thres)
						{
							res_image->setVoxel(i, j, k, 255);
							if (i <= minx && i >= 4)
								minx = i - 1;
							if (i >= maxx && i <= cols - 4 && maxx < i + 1)
								maxx = i + 1;
							if (j <= miny && j >= 4)
								miny = j - 1;
							if (j >= maxy && j <= rows - 4 && maxy < j + 1)
								maxy = j + 1;
							if (k <= minz && k >= 4)
								minz = k - 1;
							if (k >= maxz && k <= slices - 4 && maxz < k + 1)
								maxz = k + 1;
							historyEntity.push(triple(i, j, k));
							flag = false;
						}
					}
				}
			}
		}
		if (!flag) {
			if (history.size() >= h_size)
			{
				stack<triple> first = history.back();
				while (!first.empty())
				{
					first.pop();
				}
				history.pop_back();
			}
			history.push_front(historyEntity);
		}
	}
	return res_image;
}