Macaulay2 Engine
Loading...
Searching...
No Matches
SLP-defs.hpp
Go to the documentation of this file.
1// Copyright 2015 Anton Leykin and Mike Stillman
2
3// Anton Leykin's code in this file is in the public domain.
4
5#ifndef _slp_defs_hpp_
6#define _slp_defs_hpp_
7
50
51// SLP
52class SLProgram;
53
64{
65 std::unique_ptr<SLProgram> mSLProgram;
66public:
68
69 SLProgram& value() { return *mSLProgram; }
70};
71
89{
90 public:
92 typedef int GATE_SIZE;
93 typedef int GATE_POSITION; // gate position is RELATIVE (exception: ABSOLUTE
94 // for mOutputPositions)
95 std::vector<GATE_TYPE> mNodes; // nodes types
96 std::vector<GATE_SIZE> mNumInputs; // corresponding nodes sizes
97 std::vector<GATE_POSITION>
98 mInputPositions; /* which nodes does input come from?
99 !!! this vector could be longer than mNodes !!!
100 !!! since there could be several inputs per node !!!
101 (nonnegative = node position,
102 negative = var or const) */
103 std::vector<GATE_POSITION> mOutputPositions; /* which nodes are outputs
104 (nonnegative = node position,
105 negative = var or const) */
106 /* LOOKUP TABLE */
107 int inputCounter; // this is the count; the position numbering is -1, -2, ...
108 public:
109 SLProgram();
110 virtual ~SLProgram();
111 // GATE_POSITION addCopy(GATE_POSITION p);
112 // GATE_POSITION addMCopy(GATE_POSITION p, GATE_SIZE s);
113 // GATE_POSITION addSum(GATE_POSITION a, GATE_POSITION b);
114 // GATE_POSITION addProduct(GATE_POSITION a, GATE_POSITION b);
115
116 // !!! replace M2_arrayint with std::vector (M2_arrayint pertains to front end)
123 void text_out(buffer&) const;
124};
125
126class Homotopy;
127
137// needs a finalizer???
139{
140 std::unique_ptr<Homotopy> mHomotopy;
141public:
143
144 Homotopy& value() { return *mHomotopy; }
145};
146
160
172
183
196template <typename RT>
201
207template <>
212
218template <>
223/*
224template<>
225struct HomotopyAlgorithm<M2::ARingRR> {
226 typedef FixedPrecisionHomotopyAlgorithm Algorithm;
227};
228template<>
229struct HomotopyAlgorithm<M2::ARingRRR> {
230 typedef FixedPrecisionHomotopyAlgorithm Algorithm;
231};
232*/
233
234class SLEvaluator;
235
248{
250 // std::unique_ptr<SLEvaluator> mSLEvaluator;
251public:
253
255};
256
270{
271 public:
272 virtual ~SLEvaluator() {}
273 virtual SLEvaluator* specialize(const MutableMatrix* parameters) const = 0;
274 virtual bool evaluate(const MutableMatrix* inputs,
275 MutableMatrix* outputs) = 0;
276 virtual void text_out(buffer& o) const = 0;
278
279 protected:
280 int ap(int rp) { return rp + slp->inputCounter; } // absolute position
281
283 std::vector<SLProgram::GATE_POSITION> varsPos; // the rest of inputs with neg rel position
284 std::vector<SLProgram::GATE_TYPE>::iterator nIt; // slp nodes
285 std::vector<SLProgram::GATE_SIZE>::iterator numInputsIt;
286 std::vector<SLProgram::GATE_POSITION>::iterator inputPositionsIt;
287};
288
289template <typename RT>
291{
292 public:
293 SLEvaluatorConcrete(const SLEvaluatorConcrete<RT>&); // copy constructor
295 SLProgram* SLP,
296 M2_arrayint constsPos,
298 const MutableMat<DMat<RT> >* consts /*const DMat<RT>& DMat_consts */);
300 SLProgram* SLP,
301 M2_arrayint constsPos,
303 const MutableMat<SMat<RT> >* consts /*const SMat<RT>& consts*/);
305 M2_string libName,
306 int nInputs,
307 int nOutputs,
308 const MutableMat<DMat<RT> >* empty
309 );
311 M2_string libName,
312 int nInputs,
313 int nOutputs,
314 const MutableMat<SMat<RT> >* empty
315 );
317 SLEvaluator* specialize(const MutableMatrix* parameters) const;
318 SLEvaluator* specialize(const MutableMat<DMat<RT> >* parameters) const;
319 const RT& ring() const { return mRing; }
320 bool evaluate(const MutableMatrix* inputs, MutableMatrix* outputs);
321 bool evaluate(const DMat<RT>& inputs, DMat<RT>& outputs);
322 // TODO: bool evaluate(DMat<RT>& inputs, DMat<RT>& outputs);
323 void text_out(buffer& o) const;
325
326 private:
327 void computeNextNode(); // !!! should this and vIt be here???
328 using ElementType = typename RT::ElementType;
329 typename std::vector<ElementType>::iterator vIt; // values
330
331 // common data
332 const RT& mRing;
335
336 // data used by interpreted evaluation
337 std::vector<ElementType> values; /* should be a vector of values
338 starting with inputCounter many vars and consts and
339 continuing with the values of other GATEs */
340
341 // data used by compiled evaluation
342 void (*compiled_fn)(ElementType const*, ElementType*); //void (*compiled_fn)(double const*, double*);
345};
346
361{
362 public:
363 virtual ~Homotopy() {}
364 virtual bool track(const MutableMatrix* inputs,
365 MutableMatrix* outputs,
366 MutableMatrix* output_extras,
367 gmp_RR init_dt,
368 gmp_RR min_dt,
369 gmp_RR epsilon, // o.CorrectorTolerance,
370 int max_corr_steps,
371 gmp_RR infinity_threshold,
372 bool checkPrecision) = 0;
373 virtual void text_out(buffer& o) const = 0;
374};
375
376template <typename RT, typename Algorithm>
378{
379 public:
381
383 : mHx(Hx), mHxt(Hxt), mHxH(HxH)
384 {
385 }
386 /* columns of inputs are initial solutions (last coordinate is the initial
387 value of continuation parameter t,
388 outputs have the same shape as inputs (last coordinate of outputs is set to
389 the desired value of t),
390 output_extras: the first row gives the status of the solutions (or path) */
391
392 bool track(const MutableMatrix* inputs,
393 MutableMatrix* outputs,
394 MutableMatrix* output_extras,
395 gmp_RR init_dt,
396 gmp_RR min_dt,
397 gmp_RR epsilon, // o.CorrectorTolerance,
398 int max_corr_steps,
399 gmp_RR infinity_threshold,
400 bool checkPrecision);
401 void text_out(buffer& o) const;
402
403 private:
405 // struct Evaluators {SLEvaluator *mHx, *mHxt, *mHxH;};
406 // std::vector<Evaluators> mE; // a vector of evaluators increasing in
407 // precision
408 // std::vector<Ring*> mR; // a vector of available rings (corresponding to
409 // mE?)
410};
411
412template <typename RT>
414{
415 public:
417 HomotopyConcrete(EType& Hx, EType& Hxt, EType& HxH);
418 /* columns of inputs are initial solutions (last coordinate is the initial
419 value of continuation parameter t,
420 outputs have the same shape as inputs (last coordinate of outputs is set to
421 the desired value of t),
422 output_extras: the first row gives the status of the solutions (or path) */
423 bool track(const MutableMatrix* inputs,
424 MutableMatrix* outputs,
425 MutableMatrix* output_extras,
426 gmp_RR init_dt,
427 gmp_RR min_dt,
428 gmp_RR epsilon, // o.CorrectorTolerance,
429 int max_corr_steps,
430 gmp_RR infinity_threshold,
431 bool checkPrecision);
432 void text_out(buffer& o) const;
433
434 private:
436 // struct Evaluators {SLEvaluator *mHx, *mHxt, *mHxH;};
437 // std::vector<Evaluators> mE; // a vector of evaluators increasing in
438 // precision
439 // std::vector<Ring*> mR; // a vector of available rings (corresponding to
440 // mE?)
441};
442
443#endif
444
445// Local Variables:
446// compile-command: "make -C $M2BUILDDIR/Macaulay2/e "
447// indent-tabs-mode: nil
448// End:
Definition dmat.hpp:62
Tag type selecting the fixed-precision homotopy algorithm.
Definition SLP-defs.hpp:170
virtual void text_out(buffer &o) const =0
virtual bool track(const MutableMatrix *inputs, MutableMatrix *outputs, MutableMatrix *output_extras, gmp_RR init_dt, gmp_RR min_dt, gmp_RR epsilon, int max_corr_steps, gmp_RR infinity_threshold, bool checkPrecision)=0
virtual ~Homotopy()
Definition SLP-defs.hpp:363
HomotopyConcrete(EType &Hx, EType &Hxt, EType &HxH)
Definition SLP-imp.hpp:838
void text_out(buffer &o) const
Definition SLP-imp.hpp:833
HomotopyConcrete(EType &Hx, EType &Hxt, EType &HxH)
Definition SLP-defs.hpp:382
bool track(const MutableMatrix *inputs, MutableMatrix *outputs, MutableMatrix *output_extras, gmp_RR init_dt, gmp_RR min_dt, gmp_RR epsilon, int max_corr_steps, gmp_RR infinity_threshold, bool checkPrecision)
Definition SLP-imp.hpp:808
SLEvaluatorConcrete< RT > EType
Definition SLP-defs.hpp:380
Abstract base for the predictor-corrector path-tracker hierarchy.
Definition SLP-defs.hpp:361
Homotopy & value()
Definition SLP-defs.hpp:144
std::unique_ptr< Homotopy > mHomotopy
Definition SLP-defs.hpp:140
M2Homotopy(Homotopy *pa)
Definition SLP-defs.hpp:142
M2SLEvaluator(SLEvaluator *pa)
!! this is a hack to avoid memory corruption, it results in a memory leak
Definition SLP-defs.hpp:252
SLEvaluator & value()
Definition SLP-defs.hpp:254
SLEvaluator * mSLEvaluator
Definition SLP-defs.hpp:249
SLProgram & value()
Definition SLP-defs.hpp:69
std::unique_ptr< SLProgram > mSLProgram
Definition SLP-defs.hpp:65
M2SLProgram(SLProgram *pa)
Definition SLP-defs.hpp:67
Abstract base class for mutable matrices over an arbitrary engine Ring, the in-place counterpart of t...
Definition mat.hpp:79
std::vector< SLProgram::GATE_TYPE >::iterator nIt
Definition SLP-defs.hpp:284
std::vector< SLProgram::GATE_SIZE >::iterator numInputsIt
Definition SLP-defs.hpp:285
virtual Homotopy * createHomotopy(SLEvaluator *Hxt, SLEvaluator *HxH)=0
virtual bool evaluate(const MutableMatrix *inputs, MutableMatrix *outputs)=0
int ap(int rp)
Definition SLP-defs.hpp:280
std::vector< SLProgram::GATE_POSITION >::iterator inputPositionsIt
Definition SLP-defs.hpp:286
std::vector< SLProgram::GATE_POSITION > varsPos
!! can we make it a reference???
Definition SLP-defs.hpp:283
SLProgram * slp
Definition SLP-defs.hpp:282
virtual SLEvaluator * specialize(const MutableMatrix *parameters) const =0
virtual ~SLEvaluator()
Definition SLP-defs.hpp:272
virtual void text_out(buffer &o) const =0
Homotopy * createHomotopy(SLEvaluator *Hxt, SLEvaluator *HxH)
Definition SLP-imp.hpp:323
const RT & ring() const
Definition SLP-defs.hpp:319
bool evaluate(const MutableMatrix *inputs, MutableMatrix *outputs)
Definition SLP-imp.hpp:227
std::vector< ElementType > values
Definition SLP-defs.hpp:337
SLEvaluator * specialize(const MutableMatrix *parameters) const
Definition SLP-imp.hpp:151
std::vector< ElementType >::iterator vIt
Definition SLP-defs.hpp:329
void(* compiled_fn)(ElementType const *, ElementType *)
Definition SLP-defs.hpp:342
ElementType * parametersAndInputs
Definition SLP-defs.hpp:344
SLEvaluatorConcrete(const SLEvaluatorConcrete< RT > &)
Definition SLP-imp.hpp:103
void text_out(buffer &o) const
Definition SLP-imp.hpp:304
typename RT::ElementType ElementType
Definition SLP-defs.hpp:328
Abstract base for the SLP evaluator hierarchy.
Definition SLP-defs.hpp:270
Definition NAG.hpp:485
std::vector< GATE_POSITION > mInputPositions
Definition SLP-defs.hpp:98
GATE_POSITION addMProduct(const M2_arrayint)
Definition SLP.cpp:26
int GATE_POSITION
Definition SLP-defs.hpp:93
GATE_POSITION addDet(const M2_arrayint)
Definition SLP.cpp:35
int inputCounter
Definition SLP-defs.hpp:107
void text_out(buffer &) const
Definition SLP.cpp:67
SLProgram()
Definition SLP.cpp:6
GATE_POSITION addDivide(const M2_arrayint)
Definition SLP.cpp:44
void setOutputPositions(const M2_arrayint)
Definition SLP.cpp:53
GATE_POSITION addInput()
Definition SLP-defs.hpp:117
virtual ~SLProgram()
Definition SLP.cpp:12
std::vector< GATE_SIZE > mNumInputs
Definition SLP-defs.hpp:96
GATE_POSITION addMSum(const M2_arrayint)
Definition SLP.cpp:17
std::vector< GATE_TYPE > mNodes
Definition SLP-defs.hpp:95
std::vector< GATE_POSITION > mOutputPositions
Definition SLP-defs.hpp:103
int GATE_SIZE
Definition SLP-defs.hpp:92
A straight-line program: a directed acyclic graph of arithmetic gates over a fixed list of inputs and...
Definition SLP-defs.hpp:89
Definition smat.hpp:43
Tag type selecting the no-op homotopy algorithm.
Definition SLP-defs.hpp:158
Tag type selecting the variable-precision homotopy algorithm.
Definition SLP-defs.hpp:181
mpfr_srcptr gmp_RR
Definition m2-types.h:148
Definition aring-CC.cpp:3
FixedPrecisionHomotopyAlgorithm Algorithm
Definition SLP-defs.hpp:210
FixedPrecisionHomotopyAlgorithm Algorithm
Definition SLP-defs.hpp:221
TrivialHomotopyAlgorithm Algorithm
Definition SLP-defs.hpp:199
Traits class mapping a coefficient ring RT to its preferred homotopy algorithm tag.
Definition SLP-defs.hpp:198