void ADFun<Base>::ForSparseJacCase(
	const std::set<size_t>&    set_type      ,
	bool                       transpose     ,
	size_t                     q             ,
	const VectorSet&           r             ,
	VectorSet&                 s             )
{	size_t m = Range();

	// check VectorSet is Simple Vector class with sets for elements
	CheckSimpleVector<std::set<size_t>, VectorSet>(
		one_element_std_set<size_t>(), two_element_std_set<size_t>()
	);

	// dimension size of result vector
	if( transpose )
		s.resize(q);
	else	s.resize( m );

	// store results in r and for_jac_sparse_pack_
	CppAD::ForSparseJacSet(
		transpose        , 
		q                , 
		r                ,
		s                ,
		num_var_tape_    ,
		dep_taddr_       ,
		ind_taddr_       ,
		play_            ,
		for_jac_sparse_set_
	);
}
Beispiel #2
0
void ADFun<Base>::RevSparseHesCase(
	const std::set<size_t>&   set_type         ,
	bool                      transpose        ,  
	size_t                    q                ,  
	const VectorSet&          s                ,
	VectorSet&                h                )
{	size_t n = Domain();
	if( transpose )
		h.resize(n);
	else	h.resize(q);

	CPPAD_ASSERT_KNOWN( 
		for_jac_sparse_set_.n_set() > 0,
		"RevSparseHes: previous stored call to ForSparseJac did not "
		"use std::set<size_t> for the elements of r."
	);
	CPPAD_ASSERT_UNKNOWN( for_jac_sparse_pack_.n_set() == 0 );
	CPPAD_ASSERT_UNKNOWN( for_jac_sparse_set_.n_set() == num_var_tape_  );
	
	// use sparse_pack for the calculation
	CppAD::RevSparseHesSet( 
		transpose                ,
		q                        ,
		s                        ,
		h                        ,
		num_var_tape_            ,
		dep_taddr_               ,
		ind_taddr_               ,
		play_                    ,
		for_jac_sparse_set_ 
	);
}
Beispiel #3
0
void ADFun<Base>::RevSparseJacCase(
	const std::set<size_t>&      set_type          ,
	bool                         transpose         ,
	bool                         dependency        ,
	size_t                       q                 ,
	const VectorSet&             r                 ,
	VectorSet&                   s                 )
{	// dimension of the result vector
	if( transpose )
		s.resize( Domain() );
	else	s.resize( q );

	// store results in r
	RevSparseJacSet(
		transpose      ,
		dependency     ,
		q              ,
		r              ,
		s              ,
		num_var_tape_  ,
		dep_taddr_     ,
		ind_taddr_     ,
		play_
	);
}
void ADFun<Base>::ForSparseJacCase(
	bool                set_type      ,
	bool                transpose     ,
	size_t              q             ,
	const VectorSet&    r             ,
	VectorSet&          s             )
{	size_t m = Range();

	// check VectorSet is Simple Vector class with bool elements
	CheckSimpleVector<bool, VectorSet>();

	// dimension size of result vector
	s.resize( m * q );

	// store results in s and for_jac_sparse_pack_
	ForSparseJacBool(
		transpose        , 
		q                , 
		r                ,
		s                ,
		num_var_tape_    ,
		dep_taddr_       ,
		ind_taddr_       ,
		play_            ,
		for_jac_sparse_pack_ 
	);
}
Beispiel #5
0
void ADFun<Base>::RevSparseJacCase(
	bool                set_type          ,
	bool                transpose         ,
	bool                dependency        ,
	size_t              q                 ,
	const VectorSet&    r                 ,
	VectorSet&          s                 )
{	size_t n = Domain();

	// dimension of the result vector
	s.resize( q * n );

	// store results in s
	RevSparseJacBool(
		transpose      ,
		dependency     ,
		q              ,
		r              ,
		s              ,
		num_var_tape_  ,
		dep_taddr_     ,
		ind_taddr_     ,
		play_
	);
}
void ADFun<Base>::RevSparseHesCase(
	bool              set_type         ,
	bool              transpose        ,  
	size_t            q                ,  
	const VectorSet&  s                ,
	VectorSet&        h                )
{	size_t n = Domain(); 	
	h.resize(q * n );

	CPPAD_ASSERT_KNOWN( 
		for_jac_sparse_pack_.n_set() > 0,
		"RevSparseHes: previous stored call to ForSparseJac did not "
		"use bool for the elements of r."
	);
	CPPAD_ASSERT_UNKNOWN( for_jac_sparse_set_.n_set() == 0 );
	CPPAD_ASSERT_UNKNOWN( for_jac_sparse_pack_.n_set() == total_num_var_ );
	
	// use sparse_pack for the calculation
	CppAD::RevSparseHesBool( 
		transpose                ,
		q                        ,
		s                        ,
		h                        ,
		total_num_var_           ,
		dep_taddr_               ,
		ind_taddr_               ,
		play_                    ,
		for_jac_sparse_pack_ 
	);
}
Beispiel #7
0
void ADFun<Base>::RevSparseJacCase(
	const std::set<size_t>&      set_type          ,
	size_t                       p                 ,
	const VectorSet&             s                 ,
	VectorSet&                   r                 )
{	// dimension of the result vector
	r.resize( p );

	// store results in r
	RevSparseJacSet(
		p              ,
		s              ,
		r              ,
		total_num_var_ ,
		dep_taddr_     ,
		ind_taddr_     ,
		play_
	);
}
Beispiel #8
0
void ADFun<Base>::RevSparseJacCase(
	bool                set_type          ,
	size_t              p                 ,
	const VectorSet&    s                 ,
	VectorSet&          r                 )
{	size_t n = Domain();

	// dimension of the result vector
	r.resize( p * n );

	// store results in r
	RevSparseJacBool(
		p              ,
		s              ,
		r              ,
		total_num_var_ ,
		dep_taddr_     ,
		ind_taddr_     ,
		play_
	);
}
Beispiel #9
0
void ADFun<Base>::ForSparseHesCase(
	const std::set<size_t>&   set_type         ,
	const VectorSet&          r                ,
	const VectorSet&          s                ,
	VectorSet&                h                )
{	size_t n = Domain();
# ifndef NDEBUG
	size_t m = Range();
# endif
	std::set<size_t>::const_iterator itr_1;
	//
	// check VectorSet is Simple Vector class with sets for elements
	CheckSimpleVector<std::set<size_t>, VectorSet>(
		local::one_element_std_set<size_t>(), local::two_element_std_set<size_t>()
	);
	CPPAD_ASSERT_KNOWN(
		r.size() == 1,
		"ForSparseHes: size of s is not equal to one."
	);
	CPPAD_ASSERT_KNOWN(
		s.size() == 1,
		"ForSparseHes: size of s is not equal to one."
	);
	//
	// sparsity pattern corresponding to r
	local::sparse_list for_jac_pattern;
	for_jac_pattern.resize(num_var_tape_, n + 1);
	itr_1 = r[0].begin();
	while( itr_1 != r[0].end() )
	{	size_t i = *itr_1++;
		CPPAD_ASSERT_UNKNOWN( ind_taddr_[i] < n + 1 );
		// ind_taddr_[i] is operator taddr for i-th independent variable
		CPPAD_ASSERT_UNKNOWN( play_.GetOp( ind_taddr_[i] ) == local::InvOp );
		//
		for_jac_pattern.add_element( ind_taddr_[i], ind_taddr_[i] );
	}
	// compute forward Jacobiain sparsity pattern
	bool dependency = false;
	local::ForJacSweep(
		dependency,
		n,
		num_var_tape_,
		&play_,
		for_jac_pattern
	);
	// sparsity pattern correspnding to s
	local::sparse_list rev_jac_pattern;
	rev_jac_pattern.resize(num_var_tape_, 1);
	itr_1 = s[0].begin();
	while( itr_1 != s[0].end() )
	{	size_t i = *itr_1++;
		CPPAD_ASSERT_KNOWN(
			i < m,
			"ForSparseHes: an element of the set s[0] has value "
			"greater than or equal m"
		);
		CPPAD_ASSERT_UNKNOWN( dep_taddr_[i] < num_var_tape_ );
		rev_jac_pattern.add_element( dep_taddr_[i], 0);
	}
	//
	// compute reverse sparsity pattern for dependency analysis
	// (note that we are only want non-zero derivatives not true dependency)
	local::RevJacSweep(
		dependency,
		n,
		num_var_tape_,
		&play_,
		rev_jac_pattern
	);
	//
	// vector of sets that will hold reverse Hessain values
	local::sparse_list for_hes_pattern;
	for_hes_pattern.resize(n+1, n+1);
	//
	// compute the Hessian sparsity patterns
	local::ForHesSweep(
		n,
		num_var_tape_,
		&play_,
		for_jac_pattern,
		rev_jac_pattern,
		for_hes_pattern
	);
	// return values corresponding to independent variables
	// j is index corresponding to reverse mode partial
	h.resize(n);
	CPPAD_ASSERT_UNKNOWN( for_hes_pattern.end() == n+1 );
	for(size_t i = 0; i < n; i++)
	{	CPPAD_ASSERT_UNKNOWN( ind_taddr_[i] == i + 1 );
		CPPAD_ASSERT_UNKNOWN( play_.GetOp( ind_taddr_[i] ) == local::InvOp );

		// extract the result from for_hes_pattern
		local::sparse_list::const_iterator itr_2(for_hes_pattern, ind_taddr_[i] );
		size_t j = *itr_2;
		while( j < for_hes_pattern.end() )
		{	CPPAD_ASSERT_UNKNOWN( 0 < j )
				h[i].insert(j-1);
			j = *(++itr_2);
		}
	}
}
Beispiel #10
0
void ADFun<Base>::ForSparseHesCase(
	bool              set_type         ,
	const VectorSet&  r                ,
	const VectorSet&  s                ,
	VectorSet&        h                )
{	size_t n = Domain();
	size_t m = Range();
	//
	// check Vector is Simple VectorSet class with bool elements
	CheckSimpleVector<bool, VectorSet>();
	//
	CPPAD_ASSERT_KNOWN(
		size_t(r.size()) == n,
		"ForSparseHes: size of r is not equal to\n"
		"domain dimension for ADFun object."
	);
	CPPAD_ASSERT_KNOWN(
		size_t(s.size()) == m,
		"ForSparseHes: size of s is not equal to\n"
		"range dimension for ADFun object."
	);
	//
	// sparsity pattern corresponding to r
	local::sparse_pack for_jac_pattern;
	for_jac_pattern.resize(num_var_tape_, n + 1);
	for(size_t i = 0; i < n; i++)
	{	CPPAD_ASSERT_UNKNOWN( ind_taddr_[i] < n + 1 );
		// ind_taddr_[i] is operator taddr for i-th independent variable
		CPPAD_ASSERT_UNKNOWN( play_.GetOp( ind_taddr_[i] ) == local::InvOp );
		//
		if( r[i] )
			for_jac_pattern.add_element( ind_taddr_[i], ind_taddr_[i] );
	}
	// compute forward Jacobiain sparsity pattern
	bool dependency = false;
	local::ForJacSweep(
		dependency,
		n,
		num_var_tape_,
		&play_,
		for_jac_pattern
	);
	// sparsity pattern correspnding to s
	local::sparse_pack rev_jac_pattern;
	rev_jac_pattern.resize(num_var_tape_, 1);
	for(size_t i = 0; i < m; i++)
	{	CPPAD_ASSERT_UNKNOWN( dep_taddr_[i] < num_var_tape_ );
		if( s[i] )
			rev_jac_pattern.add_element( dep_taddr_[i], 0);
	}
	// compute reverse sparsity pattern for dependency analysis
	// (note that we are only want non-zero derivatives not true dependency)
	local::RevJacSweep(
		dependency,
		n,
		num_var_tape_,
		&play_,
		rev_jac_pattern
	);
	// vector of sets that will hold the forward Hessain values
	local::sparse_pack for_hes_pattern;
	for_hes_pattern.resize(n+1, n+1);
	//
	// compute the Hessian sparsity patterns
	local::ForHesSweep(
		n,
		num_var_tape_,
		&play_,
		for_jac_pattern,
		rev_jac_pattern,
		for_hes_pattern
	);
	// initialize return values corresponding to independent variables
	h.resize(n * n);
	for(size_t i = 0; i < n; i++)
	{	for(size_t j = 0; j < n; j++)
			h[ i * n + j ] = false;
	}
	// copy to result pattern
	CPPAD_ASSERT_UNKNOWN( for_hes_pattern.end() == n+1 );
	for(size_t i = 0; i < n; i++)
	{	// ind_taddr_[i] is operator taddr for i-th independent variable
		CPPAD_ASSERT_UNKNOWN( ind_taddr_[i] == i + 1 );
		CPPAD_ASSERT_UNKNOWN( play_.GetOp( ind_taddr_[i] ) == local::InvOp );

		// extract the result from for_hes_pattern
		local::sparse_pack::const_iterator itr(for_hes_pattern, ind_taddr_[i] );
		size_t j = *itr;
		while( j < for_hes_pattern.end() )
		{	CPPAD_ASSERT_UNKNOWN( 0 < j )
			h[ i * n + (j-1) ] = true;
			j = *(++itr);
		}
	}
}