Пример #1
0
void set_internal_sparsity(
    bool                          zero_empty       ,
    bool                          input_empty      ,
    bool                          transpose        ,
    const pod_vector<size_t>&     internal_index   ,
    InternalSparsity&             internal_pattern ,
    const sparse_rc<SizeVector>&  pattern_in       )
{
    size_t nr = internal_index.size();
# ifndef NDEBUG
    size_t nc = internal_pattern.end();
    if( transpose )
    {   CPPAD_ASSERT_UNKNOWN( pattern_in.nr() == nc );
        CPPAD_ASSERT_UNKNOWN( pattern_in.nc() == nr );
    }
    else
    {   CPPAD_ASSERT_UNKNOWN( pattern_in.nr() == nr );
        CPPAD_ASSERT_UNKNOWN( pattern_in.nc() == nc );
    }
    if( input_empty ) for(size_t i = 0; i < nr; i++)
    {   size_t i_var = internal_index[i];
        CPPAD_ASSERT_UNKNOWN( internal_pattern.number_elements(i_var) == 0 );
    }
# endif
    const SizeVector& row( pattern_in.row() );
    const SizeVector& col( pattern_in.col() );
    size_t nnz = row.size();
    for(size_t k = 0; k < nnz; k++)
    {   size_t r = row[k];
        size_t c = col[k];
        if( transpose )
            std::swap(r, c);
        //
        size_t i_var = internal_index[r];
        CPPAD_ASSERT_UNKNOWN( i_var < internal_pattern.n_set() );
        CPPAD_ASSERT_UNKNOWN( c < nc );
        bool ignore  = zero_empty && i_var == 0;
        if( ! ignore )
            internal_pattern.post_element( internal_index[r], c );
    }
    // process posts
    for(size_t i = 0; i < nr; ++i)
        internal_pattern.process_post( internal_index[i] );
}
Пример #2
0
void ADFun<Base>::for_jac_sparsity(
	const sparse_rc<SizeVector>& pattern_in       ,
	bool                         transpose        ,
	bool                         dependency       ,
	bool                         internal_bool    ,
	sparse_rc<SizeVector>&       pattern_out      )
{	// number or rows, columns, and non-zeros in pattern_in
	size_t nr_in  = pattern_in.nr();
	size_t nc_in  = pattern_in.nc();
	//
	size_t n   = nr_in;
	size_t ell = nc_in;
	if( transpose )
		std::swap(n, ell);
	//
	CPPAD_ASSERT_KNOWN(
		n == Domain() ,
		"for_jac_sparsity: number rows in R "
		"is not equal number of independent variables."
	);
	bool zero_empty  = true;
	bool input_empty = true;
	if( internal_bool )
	{	// allocate memory for bool sparsity calculation
		// (sparsity pattern is emtpy after a resize)
		for_jac_sparse_pack_.resize(num_var_tape_, ell);
		for_jac_sparse_set_.resize(0, 0);
		//
		// set sparsity patttern for independent variables
		local::set_internal_sparsity(
			zero_empty            ,
			input_empty           ,
			transpose             ,
			ind_taddr_            ,
			for_jac_sparse_pack_  ,
			pattern_in
		);

		// compute sparsity for other variables
		local::ForJacSweep(
			dependency,
			n,
			num_var_tape_,
			&play_,
			for_jac_sparse_pack_
		);
		// set the output pattern
		local::get_internal_sparsity(
			transpose, dep_taddr_, for_jac_sparse_pack_, pattern_out
		);
	}
	else
	{
		// allocate memory for set sparsity calculation
		// (sparsity pattern is emtpy after a resize)
		for_jac_sparse_set_.resize(num_var_tape_, ell);
		for_jac_sparse_pack_.resize(0, 0);
		//
		// set sparsity patttern for independent variables
		local::set_internal_sparsity(
			zero_empty            ,
			input_empty           ,
			transpose             ,
			ind_taddr_            ,
			for_jac_sparse_set_   ,
			pattern_in
		);

		// compute sparsity for other variables
		local::ForJacSweep(
			dependency,
			n,
			num_var_tape_,
			&play_,
			for_jac_sparse_set_
		);
		// get the ouput pattern
		local::get_internal_sparsity(
			transpose, dep_taddr_, for_jac_sparse_set_, pattern_out
		);
	}
	return;
}