source: git/dyn_modules/syzextra/syzextra.h @ e98c64

jengelh-datetimespielwiese
Last change on this file since e98c64 was e98c64, checked in by Oleksandr Motsak <motsak@…>, 10 years ago
adding IGNORETAILS option for computing pure Schreyer Frames (test with 'testAGRFrame')
  • Property mode set to 100644
File size: 9.6 KB
Line 
1// -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2/*****************************************************************************\
3 * Computer Algebra System SINGULAR   
4\*****************************************************************************/
5/** @file syzextra.h
6 *
7 * Computation of Syzygies
8 *
9 * ABSTRACT: Computation of Syzygies due to Schreyer
10 *
11 * @author Oleksandr Motsak
12 *
13 **/
14/*****************************************************************************/
15
16#ifndef SYZEXTRA_H
17#define SYZEXTRA_H
18
19#include <vector>
20#include <map>
21
22// include basic definitions
23#include "singularxx_defs.h"
24
25struct  spolyrec;
26typedef struct spolyrec    polyrec;
27typedef polyrec *          poly;
28
29struct ip_sring;
30typedef struct ip_sring *         ring;
31
32struct sip_sideal;
33typedef struct sip_sideal *       ideal;
34
35class idrec;
36typedef idrec *   idhdl;
37
38BEGIN_NAMESPACE_SINGULARXX    BEGIN_NAMESPACE(SYZEXTRA)
39
40poly leadmonom(const poly p, const ring r);
41
42/// return the tail of a given polynomial or vector
43/// returns NULL if input is NULL, otherwise
44/// the result is a new polynomial/vector in the ring r
45poly p_Tail(const poly p, const ring r);
46
47
48/// return the tail of a given ideal or module
49/// returns NULL if input is NULL, otherwise
50/// the result is a new ideal/module in the ring r
51/// NOTE: the resulting rank is autocorrected
52ideal id_Tail(const ideal id, const ring r);
53
54/// inplace sorting of the module (ideal) id wrt <_(c,ds)
55void Sort_c_ds(const ideal id, const ring r);
56
57
58
59
60
61/// Computation attribute storage
62class SchreyerSyzygyComputationFlags
63{
64  public: 
65    SchreyerSyzygyComputationFlags(idhdl rootRingHdl);
66
67    SchreyerSyzygyComputationFlags(const SchreyerSyzygyComputationFlags& attr):
68        __DEBUG__(attr.__DEBUG__),
69  //      __SYZCHECK__(attr.__SYZCHECK__),
70        __LEAD2SYZ__(attr.__LEAD2SYZ__),  __TAILREDSYZ__(attr.__TAILREDSYZ__),
71        __HYBRIDNF__(attr.__HYBRIDNF__), __IGNORETAILS__(attr.__IGNORETAILS__),
72        m_rBaseRing(attr.m_rBaseRing)
73    {}   
74
75  /// output all the intermediate states
76  const bool __DEBUG__; // DebugOutput;
77
78  //  const bool __SYZCHECK__; // CheckSyzygyProperty: never tested here...
79
80  /// ?
81  const bool __LEAD2SYZ__; // TwoLeadingSyzygyTerms;
82
83  /// Reduce syzygy tails wrt the leading syzygy terms
84  const bool __TAILREDSYZ__; // TailReducedSyzygies;
85
86  /// Use the usual NF's S-poly reduction while dropping lower order terms
87  const bool __HYBRIDNF__; // UseHybridNF
88
89
90  /// ignore tails and compute the pure Schreyer frame
91  const bool __IGNORETAILS__; // @IGNORETAILS
92 
93  /// global base ring
94  const ring m_rBaseRing;
95
96};
97
98class SchreyerSyzygyComputation;
99
100class CLCM: public SchreyerSyzygyComputationFlags, public std::vector<bool>
101{
102  public:
103    CLCM(const ideal& L, const SchreyerSyzygyComputationFlags& flags);
104
105    bool Check(const poly m) const;
106   
107  private:
108    bool m_compute;
109
110    const unsigned int m_N; ///< number of ring variables
111};
112
113
114class CReducerFinder: public SchreyerSyzygyComputationFlags
115{
116  private:
117    class CLeadingTerm
118    {
119      public: 
120        CLeadingTerm(unsigned int id,  const poly p, const ring);
121
122      private:
123        CLeadingTerm();
124
125      public:
126
127        const unsigned long m_sev; ///< not short exp. vector
128        // NOTE/TODO: either of the following should be enough:
129        const unsigned int  m_label; ///< index in the main L[] + 1
130        const poly          m_lt; ///< the leading term itself L[label-1]
131    };
132
133    typedef long TComponentKey;
134    typedef std::vector<const CLeadingTerm*> TReducers;
135    typedef std::map< TComponentKey, TReducers> CReducersHash;
136   
137  public:
138    /// goes over all leading terms
139    CReducerFinder(const ideal L, const SchreyerSyzygyComputationFlags& flags);
140
141    void Initialize(const ideal L);
142
143    ~CReducerFinder();
144
145    // TODO: save shortcut (syz: |-.->) LM(LM(m) * "t") -> syz?   
146    poly FindReducer(const poly product, const poly syzterm, const CReducerFinder& checker) const;
147
148    bool IsDivisible(const poly q) const;
149
150    bool IsNonempty() const { return !m_hash.empty(); }
151
152  private:
153    ideal m_L; ///< only for debug
154
155    CReducersHash m_hash; // can also be replaced with a vector indexed by components
156};
157
158
159
160/** @class SchreyerSyzygyComputation syzextra.h
161 *
162 * Computing syzygies after Schreyer
163 *
164 * Storing/accumulating data during the computation requires some global
165 * object, like this class. Ideally the above global functions should not
166 * be used in favour of this class.
167 *
168 * @sa Schreyer Syzygy Computation Paper & Talk & Python prototype
169 */
170class SchreyerSyzygyComputation: public SchreyerSyzygyComputationFlags
171{
172  friend class CLCM;
173  friend class CReducerFinder;
174 
175  public:
176
177    /// Construct a global object for given input data (separated into leads & tails)
178    SchreyerSyzygyComputation(const ideal idLeads, const ideal idTails, const SchreyerSyzygyComputationFlags setting):
179        SchreyerSyzygyComputationFlags(setting),
180        m_idLeads(idLeads), m_idTails(idTails), 
181        m_syzLeads(NULL), m_syzTails(NULL), m_LS(NULL),
182        m_lcm(m_idLeads, setting), m_div(m_idLeads, setting), m_checker(NULL, setting)
183    {
184    }
185
186    /// Construct a global object for given input data (separated into leads & tails)
187    SchreyerSyzygyComputation(const ideal idLeads, const ideal idTails, const ideal syzLeads, const SchreyerSyzygyComputationFlags setting):
188        SchreyerSyzygyComputationFlags(setting),
189        m_idLeads(idLeads), m_idTails(idTails), 
190        m_syzLeads(syzLeads), m_syzTails(NULL), m_LS(syzLeads), 
191        m_lcm(m_idLeads, setting), m_div(m_idLeads, setting), m_checker(NULL, setting)
192    {
193      if( __TAILREDSYZ__ && syzLeads != NULL )
194        m_checker.Initialize(syzLeads);
195    }
196
197   
198    /// Destructor should not destruct the resulting m_syzLeads, m_syzTails.
199    ~SchreyerSyzygyComputation(){ CleanUp(); }
200
201    /// Read off the results while detaching them from this object
202    /// NOTE: no copy!
203    inline void ReadOffResult(ideal& syzL, ideal& syzT)
204    {
205      syzL = m_syzLeads; syzT = m_syzTails; 
206
207      m_syzLeads = m_syzTails = NULL; // m_LS ?
208    }
209   
210    /// The main driver function: computes
211    void ComputeSyzygy();
212
213    /// Computes Syz(leads) or only LEAD of it.
214    /// The result is stored into m_syzLeads
215    void ComputeLeadingSyzygyTerms(bool bComputeSecondTerms = true);
216
217    poly SchreyerSyzygyNF(poly syz_lead, poly syz_2) const;
218
219    // TODO: store m * @tail -.-^-.-^-.--> ?
220    poly TraverseTail(poly multiplier, poly tail) const;
221
222    // TODO: save shortcut (syz: |-.->) LM(m) * "t" -> ?
223    poly ReduceTerm(poly multiplier, poly term4reduction, poly syztermCheck) const;
224
225  public:
226    /// just for testing via the wrapper below
227    inline poly _FindReducer(const poly product, const poly syzterm) const
228        { return m_div.FindReducer(product, syzterm, m_checker); } 
229   
230  protected:
231
232    /// just leading terms
233    ideal Compute1LeadingSyzygyTerms();
234
235    /// leading + second terms
236    ideal Compute2LeadingSyzygyTerms();
237
238    /// Clean up all the accumulated data
239    void CleanUp() {}
240
241  private:
242    /// input leading terms
243    const ideal m_idLeads;
244
245    /// input tails
246    const ideal m_idTails;
247
248    /// output (syzygy) leading terms (+2nd terms?)
249    ideal m_syzLeads;
250
251    /// output (syzygy) tails
252    ideal m_syzTails;
253
254    /// Bitmask for variables occuring in leading terms
255    const CLCM m_lcm;
256
257    /// Divisor finder
258    const CReducerFinder m_div;
259
260    /// for checking tail-terms and makeing them irreducible (wrt m_LS!)
261    CReducerFinder m_checker;
262
263    /*mutable?*/ ideal m_LS; ///< leading syzygy terms used for reducing syzygy tails
264};
265
266
267// The following wrappers are just for testing separate functions on highest level (within schreyer.lib)
268
269static inline void ComputeSyzygy(const ideal L, const ideal T, ideal& LL, ideal& TT, const SchreyerSyzygyComputationFlags A)
270{
271  SchreyerSyzygyComputation syz(L, T, A);
272  syz.ComputeSyzygy();
273  syz.ReadOffResult(LL, TT);
274}
275
276static inline ideal ComputeLeadingSyzygyTerms(const ideal& L, const SchreyerSyzygyComputationFlags A)
277{
278  SchreyerSyzygyComputation syz(L, NULL, A);
279  syz.ComputeLeadingSyzygyTerms(false);
280  ideal LL, TT;
281  syz.ReadOffResult(LL, TT);
282  return LL; // assume TT is NULL!
283}
284
285static inline ideal Compute2LeadingSyzygyTerms(const ideal& L, const SchreyerSyzygyComputationFlags A)
286{
287  SchreyerSyzygyComputation syz(L, NULL, A);
288  syz.ComputeLeadingSyzygyTerms(true);
289  ideal LL, TT;
290  syz.ReadOffResult(LL, TT);
291  return LL; // assume TT is NULL!
292}
293
294static inline poly FindReducer(poly product, poly syzterm,
295                               ideal L, ideal LS, const SchreyerSyzygyComputationFlags A)
296{
297  SchreyerSyzygyComputation syz(L, NULL, LS, A);
298  return syz._FindReducer(product, syzterm);
299}
300
301static inline poly TraverseTail(poly multiplier, poly tail, 
302                                ideal L, ideal T, ideal LS, const SchreyerSyzygyComputationFlags A)
303{
304  SchreyerSyzygyComputation syz(L, T, LS, A);
305  return syz.TraverseTail(multiplier, tail);
306}
307
308static inline poly ReduceTerm(poly multiplier, poly term4reduction, poly syztermCheck,
309                              ideal L, ideal T, ideal LS, const SchreyerSyzygyComputationFlags A)
310{
311  SchreyerSyzygyComputation syz(L, T, LS, A);
312  return syz.ReduceTerm(multiplier, term4reduction, syztermCheck);
313}
314
315
316static inline poly SchreyerSyzygyNF(poly syz_lead, poly syz_2,
317                                    ideal L, ideal T, ideal LS, const SchreyerSyzygyComputationFlags A)
318{
319  SchreyerSyzygyComputation syz(L, T, LS, A);
320  return syz.SchreyerSyzygyNF(syz_lead, syz_2);
321}
322
323END_NAMESPACE
324   
325END_NAMESPACE_SINGULARXX
326
327#endif
328/* #ifndef SYZEXTRA_H */
329
330// Vi-modeline: vim: filetype=c:syntax:shiftwidth=2:tabstop=8:textwidth=0:expandtab
331
Note: See TracBrowser for help on using the repository browser.