32#ifndef BERTINI_SLICE_HPP
33#define BERTINI_SLICE_HPP
62 mutable std::tuple<Mat<dbl>, Mat<mpfr_complex> > coefficients_working_;
63 Mat< mpfr_complex > coefficients_highest_precision_;
65 mutable std::tuple<Vec<dbl>, Vec<mpfr_complex> > constants_working_;
66 Vec< mpfr_complex > constants_highest_precision_;
68 VariableGroup sliced_vars_;
69 unsigned num_dims_sliced_;
70 mutable unsigned precision_;
78 static LinearSlice RandomReal(VariableGroup
const& v,
unsigned dim,
bool homogeneous =
false,
bool orthogonal =
true)
80 typedef void (*funtype) (mpfr_complex&, unsigned);
82 return Make(v, dim, homogeneous, orthogonal, gen);
90 typedef void (*funtype) (mpfr_complex&, unsigned);
91 funtype gen = bertini::multiprecision::RandomComplexAssign;
92 return Make(v, dim, homogeneous, orthogonal, gen);
99 template<
typename NumT>
100 void Eval(Vec<NumT> & result, Vec<NumT>
const& x)
const
102 result = std::get<Mat<NumT> >(coefficients_working_) * x;
104 if (!is_homogeneous_)
105 result += std::get<Vec<NumT> >(constants_working_);
111 template<
typename NumT>
112 Vec<NumT>
Eval(Vec<NumT>
const& x)
const
114 if (!is_homogeneous_)
115 return std::get<Mat<NumT> >(coefficients_working_) * x + std::get<Vec<NumT> >(constants_working_);
117 return std::get<Mat<NumT> >(coefficients_working_) * x;
124 template<
typename NumT>
125 void Jacobian(Mat<NumT> & result, Mat<NumT>
const& x)
const
127 result = std::get<Mat<NumT> >(coefficients_working_);
133 template<
typename NumT>
136 return std::get<Mat<NumT> >(coefficients_working_);
161 if (new_precision > DoublePrecision())
163 Mat<mpfr_complex>& coefficients_mpfr = std::get<Mat<mpfr_complex> >(coefficients_working_);
164 Vec<mpfr_complex>& constants_mpfr = std::get<Vec<mpfr_complex> >(constants_working_);
165 for (
unsigned ii = 0; ii <
Dimension(); ++ii)
169 coefficients_mpfr(ii,jj).precision(new_precision);
170 if (new_precision>precision_)
171 coefficients_mpfr(ii,jj) = coefficients_highest_precision_(ii,jj);
174 if (!is_homogeneous_)
176 constants_mpfr(ii).precision(new_precision);
177 if (new_precision>precision_)
178 constants_mpfr(ii) = constants_highest_precision_(ii);
182 precision_ = new_precision;
190 return num_dims_sliced_;
198 return sliced_vars_.size();
210 precision_(DefaultPrecision()),
212 coefficients_highest_precision_(0, 0),
213 is_homogeneous_(false),
214 constants_highest_precision_(static_cast<unsigned>(0))
219 std::get<Vec<dbl> > (constants_working_).resize(
Dimension());
220 std::get<Vec<mpfr_complex> >(constants_working_).resize(
Dimension());
226 LinearSlice(VariableGroup
const& v,
unsigned dim,
bool homogeneous) : sliced_vars_(v), precision_(DefaultPrecision()), num_dims_sliced_(dim), coefficients_highest_precision_(dim, v.size()), is_homogeneous_(homogeneous), constants_highest_precision_(dim)
233 std::get<Vec<dbl> > (constants_working_).resize(
Dimension());
234 std::get<Vec<mpfr_complex> >(constants_working_).resize(
Dimension());
243 LinearSlice Make(VariableGroup
const& v,
unsigned dim,
bool homogeneous,
bool orthogonal, std::function<
void(mpfr_complex&,
unsigned)> gen)
259 s.coefficients_highest_precision_.resize(maxdim,mindim);
261 for (
unsigned ii(0); ii<maxdim; ++ii)
262 for (
unsigned jj(0); jj<mindim; ++jj)
263 gen(s.coefficients_highest_precision_(ii,jj), MaxPrecisionAllowed());
265 auto prev_precision = DefaultPrecision();
266 DefaultPrecision(MaxPrecisionAllowed());
268 auto QR_factorization = Eigen::HouseholderQR<Mat<mpfr_complex> >(s.coefficients_highest_precision_);
269 s.coefficients_highest_precision_ = QR_factorization.householderQ()*Mat<mpfr_complex>::Identity(maxdim, mindim);
272 s.coefficients_highest_precision_.transposeInPlace();
274 DefaultPrecision(prev_precision);
278 for (
unsigned ii(0); ii<s.
Dimension(); ++ii)
280 gen(s.coefficients_highest_precision_(ii,jj), MaxPrecisionAllowed());
283 for (
unsigned ii(0); ii<s.
Dimension(); ++ii)
286 std::get<Mat<dbl> >(s.coefficients_working_)(ii,jj) = dbl(s.coefficients_highest_precision_(ii,jj));
287 std::get<Mat<mpfr_complex> >(s.coefficients_working_)(ii,jj) = s.coefficients_highest_precision_(ii,jj);
292 s.constants_highest_precision_.resize(s.
Dimension());
293 std::get<Vec<dbl> >(s.constants_working_).resize(s.
Dimension());
294 std::get<Vec<mpfr_complex> >(s.constants_working_).resize(s.
Dimension());
296 for (
unsigned ii(0); ii<s.
Dimension(); ++ii)
298 gen(s.constants_highest_precision_(ii), MaxPrecisionAllowed());
299 std::get<Vec<dbl> >(s.constants_working_)(ii) = dbl(s.constants_highest_precision_(ii));
300 std::get<Vec<mpfr_complex> >(s.constants_working_)(ii) = s.constants_highest_precision_(ii);
304 assert(s.coefficients_highest_precision_.rows()==s.
Dimension());
305 assert(s.coefficients_highest_precision_.cols()==s.
NumVariables());
308 assert(s.constants_highest_precision_.size()==s.
Dimension());
310 assert(s.constants_highest_precision_.size()==0);
318 friend class boost::serialization::access;
320 template <
typename Archive>
321 void serialize(Archive& ar,
const unsigned version) {
324 ar & coefficients_highest_precision_;
326 ar & std::get<0>(coefficients_working_);
327 ar & std::get<1>(coefficients_working_);
337 std::ostream& operator<<(std::ostream& out,
LinearSlice const& s);
Slice an affine or projective space with a LinearSlice today!
Definition slice.hpp:59
static LinearSlice RandomComplex(VariableGroup const &v, unsigned dim, bool homogeneous=false, bool orthogonal=true)
Generate a random complex slice.
Definition slice.hpp:88
friend std::ostream & operator<<(std::ostream &, LinearSlice const &)
Provides output streaming for LinearSlice.
Definition slice.cpp:5
unsigned NumVariables() const
Get the number of variables sliced.
Definition slice.hpp:196
static LinearSlice RandomReal(VariableGroup const &v, unsigned dim, bool homogeneous=false, bool orthogonal=true)
Definition slice.hpp:78
void Eval(Vec< NumT > &result, Vec< NumT > const &x) const
Evaluate the function values of the LinearSlice, in-place.
Definition slice.hpp:100
unsigned Dimension() const
Get the dimension of the slice.
Definition slice.hpp:188
void Jacobian(Mat< NumT > &result, Mat< NumT > const &x) const
Evaluate the Jacobian of the LinearSlice, in-place.
Definition slice.hpp:125
LinearSlice()
the default constructor for linear slices.
Definition slice.hpp:208
void Precision(unsigned new_precision) const
Set the precision of the slice.
Definition slice.hpp:159
static LinearSlice Make(VariableGroup const &v, unsigned dim, bool homogeneous, bool orthogonal, std::function< void(mpfr_complex &, unsigned)> gen)
factory function for generating slices
Definition slice.hpp:243
LinearSlice(VariableGroup const &v, unsigned dim, bool homogeneous)
the constructor for linear slices.
Definition slice.hpp:226
unsigned Precision() const
Get the current precision of the slice.
Definition slice.hpp:147
Vec< NumT > Eval(Vec< NumT > const &x) const
Evaluate the function values of the LinearSlice.
Definition slice.hpp:112
Mat< NumT > Jacobian(Mat< NumT > const &x) const
Evaluate the Jacobian of the LinearSlice.
Definition slice.hpp:134
Base class for other Slices.
Definition slice.hpp:49
Bertini extensions of the Eigen linear algebra library.
Collects the various header files which define the Bertini2 function tree node types.
T max(T const &a, T const &b, T const &c)
Three-argument form of max.
Definition mpfr_extensions.hpp:98
Provides an Eigen-like NumTraits struct for querying traits of a number type.
void RandomRealAssign(complex &a, unsigned num_digits)
Definition random.hpp:168