21 std::shared_ptr<muq::Utilities::MultiIndexSet> multisIn,
25 Eigen::MatrixXd::Zero(1,multisIn->Size()))
30 std::shared_ptr<muq::Utilities::MultiIndexSet> multisIn,
31 Eigen::MatrixXd
const& coeffsIn,
33 ModPiece(GetInputSizes(multisIn, coeffsIn, coeffInput), GetOutputSizes(multisIn, coeffsIn)),
34 basisComps(basisCompsIn),
43 Eigen::MatrixXd
const& coeffsIn,
46 Eigen::VectorXi output;
49 output(0) = multisIn->GetMultiLength();
50 output(1) = coeffsIn.cols()*coeffsIn.rows();
53 output(0) = multisIn->GetMultiLength();
59 Eigen::MatrixXd
const& coeffsIn)
61 return coeffsIn.rows() * Eigen::VectorXi::Ones(1);
68 Eigen::VectorXi maxOrders =
multis->GetMaxOrders();
71 std::vector<std::vector<double>> uniEvals(
basisComps.size());
72 assert(uniEvals.size() == maxOrders.size());
74 for(
int i=0; i<uniEvals.size(); ++i){
75 uniEvals.at(i).resize(maxOrders(i)+1);
76 for(
int j=0; j<=maxOrders(i); ++j){
77 uniEvals.at(i).at(j) =
basisComps.at(i)->BasisEvaluate(j, x(i));
82 Eigen::VectorXd allTerms = Eigen::VectorXd::Ones(
multis->Size());
83 for(
int i=0; i<
multis->Size(); ++i){
85 for(
auto it =
multis->at(i)->GetNzBegin(); it !=
multis->at(i)->GetNzEnd(); ++it)
86 allTerms(i) *= uniEvals.at(it->first).at(it->second);
95 Eigen::VectorXi maxOrders =
multis->GetMaxOrders();
98 std::vector<std::vector<double>> uniEvals(
basisComps.size());
99 std::vector<std::vector<double>> uniDerivs(
basisComps.size());
100 assert(uniEvals.size() == maxOrders.size());
102 for(
int i=0; i<uniEvals.size(); ++i){
103 uniEvals.at(i).resize(maxOrders(i)+1);
104 uniDerivs.at(i).resize(maxOrders(i)+1);
106 for(
int j=0; j<=maxOrders(i); ++j){
107 uniEvals.at(i).at(j) =
basisComps.at(i)->BasisEvaluate(j, x(i));
108 uniDerivs.at(i).at(j) =
basisComps.at(i)->DerivativeEvaluate(j, 1, x(i));
113 Eigen::MatrixXd allDerivs = Eigen::MatrixXd::Ones(
multis->Size(),x.size());
114 for(
int i=0; i<
multis->Size(); ++i){
117 for(
int j=0; j<x.size(); ++j){
118 if(
multis->at(i)->GetValue(j)==0){
121 for(
auto it =
multis->at(i)->GetNzBegin(); it !=
multis->at(i)->GetNzEnd(); ++it){
124 allDerivs(i,j) *= uniDerivs.at(it->first).at(it->second);
126 allDerivs(i,j) *= uniEvals.at(it->first).at(it->second);
140 Eigen::VectorXi maxOrders =
multis->GetMaxOrders();
143 std::vector<std::vector<double>> uniEvals(
basisComps.size());
144 std::vector<std::vector<double>> uniD1(
basisComps.size());
145 std::vector<std::vector<double>> uniD2(
basisComps.size());
147 assert(uniEvals.size() == maxOrders.size());
149 for(
int i=0; i<uniEvals.size(); ++i){
150 uniEvals.at(i).resize(maxOrders(i)+1);
151 uniD1.at(i).resize(maxOrders(i)+1);
152 uniD2.at(i).resize(maxOrders(i)+1);
154 for(
int j=0; j<=maxOrders(i); ++j){
155 uniEvals.at(i).at(j) =
basisComps.at(i)->BasisEvaluate(j, x(i));
156 uniD1.at(i).at(j) =
basisComps.at(i)->DerivativeEvaluate(j, 1, x(i));
157 uniD2.at(i).at(j) =
basisComps.at(i)->DerivativeEvaluate(j, 2, x(i));
161 std::vector<Eigen::MatrixXd> hessians(
coeffs.rows(), Eigen::MatrixXd::Zero(x.size(),x.size()));
164 for(
int i=0; i<
multis->Size(); ++i){
167 for(
int j=0; j<x.size(); ++j){
168 for(
int k=0;
k<=j; ++
k){
169 if((
multis->at(i)->GetValue(j)!=0) && ((
multis->at(i)->GetValue(
k)!=0))){
171 double tempVal = 1.0;
173 for(
auto it =
multis->at(i)->GetNzBegin(); it !=
multis->at(i)->GetNzEnd(); ++it){
175 if((j==
k) && (it->first == j)){
176 tempVal *= uniD2.at(it->first).at(it->second);
177 }
else if((it->first == j)||(it->first ==
k)){
178 tempVal *= uniD1.at(it->first).at(it->second);
180 tempVal *= uniEvals.at(it->first).at(it->second);
185 for(
int kk=0; kk<
coeffs.rows(); ++kk)
186 hessians.at(kk)(j,
k) +=
coeffs(kk,i)*tempVal;
193 for(
int kk=0; kk<
coeffs.rows(); ++kk)
194 hessians.at(kk).triangularView<Eigen::Upper>() = hessians.at(kk).triangularView<Eigen::Lower>().transpose();
202 assert(newCoeffs.size() ==
coeffs.rows()*
coeffs.cols());
204 Eigen::Map<const Eigen::MatrixXd> coeffMap(newCoeffs.data(),
coeffs.rows(),
coeffs.cols());
212 Eigen::VectorXd
const& x = inputs.at(0);
222 unsigned int const wrtIn,
227 Eigen::VectorXd
const& x = inputs.at(0);
242 Eigen::VectorXd
const& x,
243 Eigen::MatrixXd
const& newCoeffs)
252 Eigen::VectorXd
const& x)
254 if((derivDim1==0) && (derivDim2==1)){
256 }
else if((derivDim1==1) && (derivDim2==0)){
258 }
else if(derivDim1==0){
261 return Eigen::MatrixXd::Zero(
coeffs.cols(),
coeffs.cols());
271 assert(
coeffs.rows()==allCoeffs.rows());
272 assert(
coeffs.cols()==allCoeffs.cols());
278 Eigen::MatrixXd vand(evalPts.cols(),
NumTerms());
280 for(
int i=0; i<evalPts.cols(); ++i)
281 vand.row(i) =
GetAllTerms(evalPts.col(i)).transpose();
289 Eigen::MatrixXd output(evalPts.cols(),
NumTerms());
291 for(
int i=0; i<evalPts.cols(); ++i)
292 output.row(i) =
GetAllDerivs(evalPts.col(i)).col(wrtDim).transpose();
306 multis->ToHDF5(group,
"/multiindices");
311 std::vector<std::string> type_strs;
314 bool matchFound =
false;
317 for(
auto const& pair : nameMap){
318 std::shared_ptr<IndexedScalarBasis> testBasis = pair.second();
322 type_strs.push_back(pair.first);
330 group.
attrs[
"expansion_type"] =
"Generic";
346 std::vector<std::shared_ptr<IndexedScalarBasis>> scalarBases;
347 for(
auto&
name : scalarBasisNames)
350 auto mset = MultiIndexSet::FromHDF5(group[
"/multiindices"]);
351 Eigen::MatrixXd
coeffs = group[
"/coefficients"];
353 std::string expansionType = group.
attrs[
"expansion_type"];
354 if(expansionType==
"PCE"){
355 return std::make_shared<PolynomialChaosExpansion>(scalarBases, mset,
coeffs);
357 return std::make_shared<BasisExpansion>(scalarBases, mset,
coeffs, coeffInput);
Class for defining expansions of basis functions defined by a MultiIndexSet and collection of IndexSc...
std::vector< Eigen::MatrixXd > GetHessians(Eigen::VectorXd const &x) const
void ProcessCoeffs(Eigen::VectorXd const &newCoeffs)
Eigen::VectorXd GetAllTerms(Eigen::VectorXd const &x) const
virtual void JacobianImpl(unsigned int const wrtIn, unsigned int const wrtOut, muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs) override
Eigen::MatrixXd BuildDerivMatrix(Eigen::MatrixXd const &evalPts, int wrtDim) const
std::shared_ptr< muq::Utilities::MultiIndexSet > multis
static std::shared_ptr< BasisExpansion > FromHDF5(std::string filename, std::string groupName="/")
Loads an expansion from an HDF5 file.
Eigen::MatrixXd BuildVandermonde(Eigen::MatrixXd const &evalPts) const
virtual unsigned NumTerms() const
static Eigen::VectorXi GetOutputSizes(std::shared_ptr< muq::Utilities::MultiIndexSet > multisIn, Eigen::MatrixXd const &coeffsIn)
BasisExpansion(std::vector< std::shared_ptr< IndexedScalarBasis >> const &basisCompsIn, bool coeffInput=false)
Eigen::MatrixXd GetCoeffs() const
Eigen::MatrixXd GetAllDerivs(Eigen::VectorXd const &x) const
Eigen::MatrixXd SecondDerivative(unsigned outputDim, unsigned wrtDim1, unsigned wrtDim2, Eigen::VectorXd const &evalPt, Eigen::MatrixXd const &coeffs)
static Eigen::VectorXi GetInputSizes(std::shared_ptr< muq::Utilities::MultiIndexSet > multisIn, Eigen::MatrixXd const &coeffsIn, bool coeffInput)
std::vector< std::shared_ptr< IndexedScalarBasis > > basisComps
virtual void EvaluateImpl(muq::Modeling::ref_vector< Eigen::VectorXd > const &inputs) override
void SetCoeffs(Eigen::MatrixXd const &allCoeffs)
virtual void ToHDF5(std::string filename, std::string groupName="/") const
Saves the expansion to group in an HDF5 file.
static std::shared_ptr< IndexedScalarBasis > Construct(std::string const &polyName)
static std::shared_ptr< ScalarBasisMapType > GetScalarBasisMap()
const Eigen::VectorXi inputSizes
std::vector< Eigen::VectorXd > outputs
std::string name
A unique name for this WorkPiece. Defaults to <ClassName>_<id>
H5Object & CreateDataset(std::string const &setName, unsigned int rows, unsigned int cols=0)
A factory class with static methods for generating MultiIndexSets.
std::vector< std::reference_wrapper< const T > > ref_vector
A vector of references to something ...
std::string Combine(std::vector< std::string > strs, char delim=',')
Combine a vector of strings with a specified delimiter. The opposite of Split.
std::vector< std::string > Split(std::string str, char delim=',')
Split a string into parts based on a particular character delimiter. Also Strips whitespace from part...
std::string GetTypeName(PointerType const &ptr)
H5Object OpenFile(std::string const &filename)
Open an HDF5 file and return the root object.