Loading...
Searching...
No Matches
fglmvec.cc
Go to the documentation of this file.
// emacs edit mode for this file is -*- C++ -*-
/****************************************
* Computer Algebra System SINGULAR *
****************************************/
/*
* ABSTRACT - The FGLM-Algorithm
* Implementation of number-vectors for the fglm algorithm.
* (See fglm.cc). Based on a letter-envelope implementation, mainly
* written to be used by the fglm algorithm. Hence they are
* specialized for this purpose.
*/
#include "kernel/mod2.h"
#include "kernel/structs.h"
#include "coeffs/numbers.h"
#include "fglm.h"
#include "fglmvec.h"
#define PROT(msg)
#define STICKYPROT(msg) if (BTEST1(OPT_PROT)) Print(msg)
#define PROT2(msg,arg)
#define STICKYPROT2(msg,arg) if (BTEST1(OPT_PROT)) Print(msg,arg)
#define fglmASSERT(ignore1,ignore2)
class fglmVectorRep
{
private:
int ref_count;
int N;
number *elems;
public:
fglmVectorRep ():ref_count (1), N (0), elems (0)
{
}
fglmVectorRep (int n, number * e):ref_count (1), N (n), elems (e)
{
}
fglmVectorRep (int n):ref_count (1), N (n)
{
fglmASSERT (N >= 0, "illegal Vector representation");
if(N == 0)
elems = 0;
else
{
elems = (number *) omAlloc (N * sizeof (number));
for(int i = N - 1; i >= 0; i--)
elems[i] = nInit (0);
}
}
~fglmVectorRep ()
{
if(N > 0)
{
for(int i = N - 1; i >= 0; i--)
nDelete (elems + i);
omFreeSize ((ADDRESS) elems, N * sizeof (number));
}
}
fglmVectorRep *clone () const
{
if(N > 0)
{
number *elems_clone;
elems_clone = (number *) omAlloc (N * sizeof (number));
for(int i = N - 1; i >= 0; i--)
elems_clone[i] = nCopy (elems[i]);
return new fglmVectorRep (N, elems_clone);
}
else
return new fglmVectorRep (N, 0);
}
BOOLEAN deleteObject ()
{
return --ref_count == 0;
}
fglmVectorRep *copyObject ()
{
ref_count++;
return this;
}
int refcount () const
{
return ref_count;
}
BOOLEAN isUnique () const
{
return ref_count == 1;
}
int size () const
{
return N;
}
int isZero () const
{
int k;
for(k = N; k > 0; k--)
{
if(!nIsZero (getconstelem (k)))
return 0;
}
return 1;
}
int numNonZeroElems () const
{
int num = 0;
int k;
for(k = N; k > 0; k--)
{
if(!nIsZero (getconstelem (k)))
num++;
}
return num;
}
void setelem (int i, number n)
{
fglmASSERT (0 < i && i <= N, "setelem: wrong index");
nDelete (elems + i - 1);
elems[i - 1] = n;
}
number ejectelem (int i, number n)
{
fglmASSERT (isUnique (), "should only be called if unique!");
number temp = elems[i - 1];
elems[i - 1] = n;
return temp;
}
number & getelem (int i)
{
fglmASSERT (0 < i && i <= N, "getelem: wrong index");
return elems[i - 1];
}
number getconstelem (int i) const
{
fglmASSERT (0 < i && i <= N, "getconstelem: wrong index");
return elems[i - 1];
}
friend class fglmVector;
};
///--------------------------------------------------------------------------------
/// Implementation of class fglmVector
///--------------------------------------------------------------------------------
fglmVector::fglmVector (fglmVectorRep * r):rep (r)
{
}
fglmVector::fglmVector ():rep (new fglmVectorRep ())
{
}
fglmVector::fglmVector (int size):rep (new fglmVectorRep (size))
{
}
fglmVector::fglmVector (int size, int basis):rep (new fglmVectorRep (size))
{
rep->setelem (basis, nInit (1));
}
fglmVector::fglmVector (const fglmVector & v)
{
rep = v.rep->copyObject ();
}
fglmVector::~fglmVector ()
{
if(rep->deleteObject ())
delete rep;
}
#ifndef HAVE_EXPLICIT_CONSTR
void fglmVector::mac_constr (const fglmVector & v)
{
rep = v.rep->copyObject ();
}
void fglmVector::mac_constr_i (int size)
{
rep = new fglmVectorRep (size);
}
void fglmVector::clearelems ()
{
if(rep->deleteObject ())
delete rep;
}
#endif
void fglmVector::makeUnique ()
{
if(rep->refcount () != 1)
{
rep->deleteObject ();
rep = rep->clone ();
}
}
int fglmVector::size () const
{
return rep->size ();
}
int fglmVector::numNonZeroElems () const
{
return rep->numNonZeroElems ();
}
void
fglmVector::nihilate (const number fac1, const number fac2,
const fglmVector v)
{
int i;
int vsize = v.size ();
number term1, term2;
fglmASSERT (vsize <= rep->size (), "v has to be smaller or equal");
if(rep->isUnique ())
{
for(i = vsize; i > 0; i--)
{
term1 = nMult (fac1, rep->getconstelem (i));
term2 = nMult (fac2, v.rep->getconstelem (i));
rep->setelem (i, nSub (term1, term2));
nDelete (&term1);
nDelete (&term2);
}
for(i = rep->size (); i > vsize; i--)
{
rep->setelem (i, nMult (fac1, rep->getconstelem (i)));
}
}
else
{
number *newelems;
newelems = (number *) omAlloc (rep->size () * sizeof (number));
for(i = vsize; i > 0; i--)
{
term1 = nMult (fac1, rep->getconstelem (i));
term2 = nMult (fac2, v.rep->getconstelem (i));
newelems[i - 1] = nSub (term1, term2);
nDelete (&term1);
nDelete (&term2);
}
for(i = rep->size (); i > vsize; i--)
{
newelems[i - 1] = nMult (fac1, rep->getconstelem (i));
}
rep->deleteObject ();
rep = new fglmVectorRep (rep->size (), newelems);
}
}
fglmVector & fglmVector::operator = (const fglmVector & v)
{
if(this != &v)
{
if(rep->deleteObject ())
delete rep;
rep = v.rep->copyObject ();
}
return *this;
}
int fglmVector::operator == (const fglmVector & v)
{
if(rep->size () == v.rep->size ())
{
if(rep == v.rep)
return 1;
else
{
int i;
for(i = rep->size (); i > 0; i--)
if(!nEqual (rep->getconstelem (i), v.rep->getconstelem (i)))
return 0;
return 1;
}
}
return 0;
}
int fglmVector::operator != (const fglmVector & v)
{
return !(*this == v);
}
int fglmVector::isZero ()
{
return rep->isZero ();
}
int fglmVector::elemIsZero (int i)
{
return nIsZero (rep->getconstelem (i));
}
fglmVector & fglmVector::operator += (const fglmVector & v)
{
fglmASSERT (size () == v.size (), "incompatible vectors");
// ACHTUNG : Das Verhalten hier mit gcd genau ueberpruefen!
int i;
if(rep->isUnique ())
{
for(i = rep->size (); i > 0; i--)
n_InpAdd (rep->getelem(i), v.rep->getconstelem (i), currRing->cf);
}
else
{
int n = rep->size ();
number *newelems;
newelems = (number *) omAlloc (n * sizeof (number));
for(i = n; i > 0; i--)
newelems[i - 1] = nAdd (rep->getconstelem (i), v.rep->getconstelem (i));
rep->deleteObject ();
rep = new fglmVectorRep (n, newelems);
}
return *this;
}
fglmVector & fglmVector::operator -= (const fglmVector & v)
{
fglmASSERT (size () == v.size (), "incompatible vectors");
int i;
if(rep->isUnique ())
{
for(i = rep->size (); i > 0; i--)
rep->setelem (i, nSub (rep->getconstelem (i), v.rep->getconstelem (i)));
}
else
{
int n = rep->size ();
number *newelems;
newelems = (number *) omAlloc (n * sizeof (number));
for(i = n; i > 0; i--)
newelems[i - 1] = nSub (rep->getconstelem (i), v.rep->getconstelem (i));
rep->deleteObject ();
rep = new fglmVectorRep (n, newelems);
}
return *this;
}
fglmVector & fglmVector::operator *= (const number & n)
{
int s = rep->size ();
int i;
if(!rep->isUnique ())
{
number *temp;
temp = (number *) omAlloc (s * sizeof (number));
for(i = s; i > 0; i--)
temp[i - 1] = nMult (rep->getconstelem (i), n);
rep->deleteObject ();
rep = new fglmVectorRep (s, temp);
}
else
{
for(i = s; i > 0; i--)
nInpMult (rep->getelem (i), n);
}
return *this;
}
fglmVector & fglmVector::operator /= (const number & n)
{
int s = rep->size ();
int i;
if(!rep->isUnique ())
{
number *temp;
temp = (number *) omAlloc (s * sizeof (number));
for(i = s; i > 0; i--)
{
temp[i - 1] = nDiv (rep->getconstelem (i), n);
nNormalize (temp[i - 1]);
}
rep->deleteObject ();
rep = new fglmVectorRep (s, temp);
}
else
{
for(i = s; i > 0; i--)
{
rep->setelem (i, nDiv (rep->getconstelem (i), n));
nNormalize (rep->getelem (i));
}
}
return *this;
}
fglmVector operator - (const fglmVector & v)
{
fglmVector temp (v.size ());
int i;
number n;
for(i = v.size (); i > 0; i--)
{
n = nCopy (v.getconstelem (i));
n = nInpNeg (n);
temp.setelem (i, n);
}
return temp;
}
fglmVector operator + (const fglmVector & lhs, const fglmVector & rhs)
{
fglmVector temp = lhs;
temp += rhs;
return temp;
}
fglmVector operator - (const fglmVector & lhs, const fglmVector & rhs)
{
fglmVector temp = lhs;
temp -= rhs;
return temp;
}
fglmVector operator * (const fglmVector & v, const number n)
{
fglmVector temp = v;
temp *= n;
return temp;
}
fglmVector operator * (const number n, const fglmVector & v)
{
fglmVector temp = v;
temp *= n;
return temp;
}
number & fglmVector::getelem (int i)
{
makeUnique ();
return rep->getelem (i);
}
number fglmVector::getconstelem (int i) const
{
return rep->getconstelem (i);
}
void fglmVector::setelem (int i, number & n)
{
makeUnique ();
rep->setelem (i, n);
n = n_Init (0, currRing->cf);
}
number fglmVector::gcd () const
{
int i = rep->size ();
BOOLEAN found = FALSE;
BOOLEAN gcdIsOne = FALSE;
number theGcd;
number current;
while(i > 0 && !found)
{
current = rep->getconstelem (i);
if(!nIsZero (current))
{
theGcd = nCopy (current);
found = TRUE;
if(!nGreaterZero (theGcd))
{
theGcd = nInpNeg (theGcd);
}
if(nIsOne (theGcd))
gcdIsOne = TRUE;
}
i--;
}
if(found)
{
if (getCoeffType(currRing->cf)!=n_Zp)
{
while(i > 0 && !gcdIsOne)
{
current = rep->getconstelem (i);
if(!nIsZero (current))
{
number temp = n_SubringGcd (theGcd, current, currRing->cf);
nDelete (&theGcd);
theGcd = temp;
if(nIsOne (theGcd))
gcdIsOne = TRUE;
}
i--;
}
}
}
else
theGcd = nInit (0);
return theGcd;
}
number fglmVector::clearDenom ()
{
number theLcm = nInit (1);
if (getCoeffType(currRing->cf)==n_Zp)
{
return theLcm;
}
BOOLEAN isZero = TRUE;
int i;
for(i = size (); i > 0; i--)
{
if(!nIsZero (rep->getconstelem (i)))
{
isZero = FALSE;
number temp = n_NormalizeHelper (theLcm, rep->getconstelem (i), currRing->cf);
nDelete (&theLcm);
theLcm = temp;
}
}
if(isZero)
{
nDelete (&theLcm);
theLcm = nInit (0);
}
else
{
if(!nIsOne (theLcm))
{
*this *= theLcm;
for(i = size (); i > 0; i--)
{
nNormalize (rep->getelem (i));
}
}
}
return theLcm;
}
// ----------------------------------------------------------------------------
// Local Variables: ***
// compile-command: "make Singular" ***
// page-delimiter: "^\\‍(␌\\|//!\\‍)" ***
// fold-internal-margins: nil ***
// End: ***
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
ADDRESS
void * ADDRESS
Definition auxiliary.h:120
size
int size(const CanonicalForm &f, const Variable &v)
int size ( const CanonicalForm & f, const Variable & v )
Definition cf_ops.cc:600
num
CanonicalForm num(const CanonicalForm &f)
Definition canonicalform.h:337
i
int i
Definition cfEzgcd.cc:132
k
int k
Definition cfEzgcd.cc:99
fglmVectorRep
Definition fglmvec.cc:31
fglmVectorRep::fglmVector
friend class fglmVector
Definition fglmvec.cc:144
fglmVectorRep::getconstelem
number getconstelem(int i) const
Definition fglmvec.cc:139
fglmVectorRep::ejectelem
number ejectelem(int i, number n)
Definition fglmvec.cc:127
fglmVectorRep::size
int size() const
Definition fglmvec.cc:96
fglmVectorRep::isUnique
BOOLEAN isUnique() const
Definition fglmvec.cc:91
fglmVectorRep::refcount
int refcount() const
Definition fglmvec.cc:87
fglmVectorRep::fglmVectorRep
fglmVectorRep(int n)
Definition fglmvec.cc:43
fglmVectorRep::ref_count
int ref_count
Definition fglmvec.cc:33
fglmVectorRep::setelem
void setelem(int i, number n)
Definition fglmvec.cc:121
fglmVectorRep::deleteObject
BOOLEAN deleteObject()
Definition fglmvec.cc:78
fglmVectorRep::clone
fglmVectorRep * clone() const
Definition fglmvec.cc:65
fglmVectorRep::~fglmVectorRep
~fglmVectorRep()
Definition fglmvec.cc:55
fglmVectorRep::fglmVectorRep
fglmVectorRep()
Definition fglmvec.cc:37
fglmVectorRep::copyObject
fglmVectorRep * copyObject()
Definition fglmvec.cc:82
fglmVectorRep::getelem
number & getelem(int i)
Definition fglmvec.cc:134
fglmVectorRep::numNonZeroElems
int numNonZeroElems() const
Definition fglmvec.cc:110
fglmVectorRep::N
int N
Definition fglmvec.cc:34
fglmVectorRep::elems
number * elems
Definition fglmvec.cc:35
fglmVectorRep::isZero
int isZero() const
Definition fglmvec.cc:100
fglmVectorRep::fglmVectorRep
fglmVectorRep(int n, number *e)
Definition fglmvec.cc:40
fglmVector
Definition fglmvec.h:19
fglmVector::operator-=
fglmVector & operator-=(const fglmVector &)
Definition fglmvec.cc:328
fglmVector::rep
fglmVectorRep * rep
Definition fglmvec.h:21
fglmVector::clearDenom
number clearDenom()
Definition fglmvec.cc:505
fglmVector::size
int size() const
Definition fglmvec.cc:207
fglmVector::numNonZeroElems
int numNonZeroElems() const
Definition fglmvec.cc:212
fglmVector::elemIsZero
int elemIsZero(int i)
Definition fglmvec.cc:300
fglmVector::getconstelem
number getconstelem(int i) const
Definition fglmvec.cc:446
fglmVector::setelem
void setelem(int i, number &n)
Definition fglmvec.cc:451
fglmVector::operator!=
int operator!=(const fglmVector &)
Definition fglmvec.cc:290
fglmVector::gcd
number gcd() const
Definition fglmvec.cc:458
fglmVector::operator*=
fglmVector & operator*=(const number &)
Definition fglmvec.cc:350
fglmVector::operator+=
fglmVector & operator+=(const fglmVector &)
Definition fglmvec.cc:305
fglmVector::makeUnique
void makeUnique()
Definition fglmvec.cc:198
fglmVector::operator/=
fglmVector & operator/=(const number &)
Definition fglmvec.cc:371
fglmVector::~fglmVector
~fglmVector()
Definition fglmvec.cc:174
fglmVector::nihilate
void nihilate(const number fac1, const number fac2, const fglmVector v)
Definition fglmvec.cc:218
fglmVector::isZero
int isZero()
Definition fglmvec.cc:295
fglmVector::fglmVector
fglmVector(fglmVectorRep *rep)
Definition fglmvec.cc:152
fglmVector::operator==
int operator==(const fglmVector &)
Definition fglmvec.cc:272
fglmVector::getelem
number & getelem(int i)
Definition fglmvec.cc:440
fglmVector::fglmVector
fglmVector()
Definition fglmvec.cc:156
fglmVector::operator=
fglmVector & operator=(const fglmVector &v)
Definition fglmvec.cc:261
n_NormalizeHelper
static FORCE_INLINE number n_NormalizeHelper(number a, number b, const coeffs r)
assume that r is a quotient field (otherwise, return 1) for arguments (a1/a2,b1/b2) return (lcm(a1,...
Definition coeffs.h:698
n_Zp
@ n_Zp
\F{p < 2^31}
Definition coeffs.h:29
getCoeffType
static FORCE_INLINE n_coeffType getCoeffType(const coeffs r)
Returns the type of coeffs domain.
Definition coeffs.h:431
n_Init
static FORCE_INLINE number n_Init(long i, const coeffs r)
a number representing i in the given coeff field/ring r
Definition coeffs.h:541
n_SubringGcd
static FORCE_INLINE number n_SubringGcd(number a, number b, const coeffs r)
Definition coeffs.h:669
n_InpAdd
static FORCE_INLINE void n_InpAdd(number &a, number b, const coeffs r)
addition of 'a' and 'b'; replacement of 'a' by the sum a+b
Definition coeffs.h:649
s
const CanonicalForm int s
Definition facAbsFact.cc:51
v
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39
found
bool found
Definition facFactorize.cc:55
operator-
fglmVector operator-(const fglmVector &v)
Definition fglmvec.cc:398
fglmASSERT
#define fglmASSERT(ignore1, ignore2)
Definition fglmvec.cc:28
operator+
fglmVector operator+(const fglmVector &lhs, const fglmVector &rhs)
Definition fglmvec.cc:412
operator*
fglmVector operator*(const fglmVector &v, const number n)
Definition fglmvec.cc:426
fglmvec.h
fglm.h
fglmASSERT
#define fglmASSERT(ignore1, ignore2)
Definition fglm.h:23
mod2.h
numbers.h
nDiv
#define nDiv(a, b)
Definition numbers.h:32
nDelete
#define nDelete(n)
Definition numbers.h:16
nInpNeg
#define nInpNeg(n)
Definition numbers.h:21
nIsZero
#define nIsZero(n)
Definition numbers.h:19
nEqual
#define nEqual(n1, n2)
Definition numbers.h:20
nInpMult
#define nInpMult(a, b)
Definition numbers.h:37
nSub
#define nSub(n1, n2)
Definition numbers.h:22
nCopy
#define nCopy(n)
Definition numbers.h:15
nAdd
#define nAdd(n1, n2)
Definition numbers.h:18
nGreaterZero
#define nGreaterZero(n)
Definition numbers.h:27
nIsOne
#define nIsOne(n)
Definition numbers.h:25
nNormalize
#define nNormalize(n)
Definition numbers.h:30
nInit
#define nInit(i)
Definition numbers.h:24
nMult
#define nMult(n1, n2)
Definition numbers.h:17
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
currRing
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition polys.cc:13
structs.h