Loading...
Searching...
No Matches
clapconv.cc
Go to the documentation of this file.
// emacs edit mode for this file is -*- C++ -*-
/****************************************
* Computer Algebra System SINGULAR *
****************************************/
/*
* ABSTRACT: convert data between Singular and factory
*/
#include "misc/auxiliary.h"
#include "factory/factory.h"
#include "coeffs/coeffs.h"
#include "coeffs/longrat.h" // snumber is necessary
#include "polys/monomials/p_polys.h"
#include "polys/sbuckets.h"
#include "polys/clapconv.h"
#include "simpleideals.h"
#define TRANSEXT_PRIVATES
#include "polys/ext_fields/transext.h"
void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r );
static void convRecTrP ( const CanonicalForm & f, int * exp, poly & result, int offs, const ring r );
//static void convRecGFGF ( const CanonicalForm & f, int * exp, poly & result );
static number convFactoryNSingAN( const CanonicalForm &f, const ring r);
poly convFactoryPSingP ( const CanonicalForm & f, const ring r )
{
if (f.isZero()) return NULL;
int n = rVar(r)+1;
/* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
int * exp = (int*)omAlloc0(n*sizeof(int));
sBucket_pt result_bucket=sBucketCreate(r);
conv_RecPP( f, exp, result_bucket, r );
poly result; int dummy;
sBucketDestroyMerge(result_bucket,&result,&dummy);
omFreeSize((ADDRESS)exp,n*sizeof(int));
return result;
}
static void conv_RecPP ( const CanonicalForm & f, int * exp, sBucket_pt result, ring r )
{
// assume f!=0
if (LIKELY(! f.inCoeffDomain()))
{
int l = f.level();
for ( CFIterator i = f; i.hasTerms(); i++ )
{
exp[l] = i.exp();
conv_RecPP( i.coeff(), exp, result, r );
}
exp[l] = 0;
}
else
{
coeffs cf=r->cf;
number n=cf->convFactoryNSingN(f, cf);
if (UNLIKELY( n_IsZero(n, cf) ))
{
n_Delete(&n,cf);
}
else
{
poly term = p_Init(r);
//pNext( term ) = NULL; // done by p_Init
pGetCoeff(term)=n;
p_SetExpV(term,exp,r);
sBucket_Merge_m(result,term);
}
}
}
static inline void convPhalf(poly p,int l,poly &p1,poly &p2)
{
p1=p;
l=l/2;
while(l>1) { p=pNext(p); l--; }
p2=pNext(p);
pNext(p)=NULL;
}
static inline poly convPunhalf(poly p1,poly p2)
{
poly p=p1;
while(pNext(p1)!=NULL) { p1=pNext(p1);}
pNext(p1)=p2;
return p;
}
#define MIN_CONV_LEN 7
static CanonicalForm convSingPFactoryP_intern( poly p, int l, BOOLEAN & setChar,const ring r )
{
CanonicalForm result = 0;
int e, n = rVar(r);
assume(l==(int)pLength(p));
if (l>MIN_CONV_LEN)
{
poly p1,p2;
convPhalf(p,l,p1,p2);
CanonicalForm P=convSingPFactoryP_intern(p1,l/2,setChar,r);
P+=convSingPFactoryP_intern(p2,l-l/2,setChar,r);
convPunhalf(p1,p2);
return P;
}
BOOLEAN setChar_loc=setChar;
setChar=FALSE;
coeffs cf=r->cf;
while ( p!=NULL )
{
CanonicalForm term=cf->convSingNFactoryN(pGetCoeff( p ),setChar_loc, cf);
if (errorreported) break;
setChar_loc=FALSE;
for ( int i = 1; i <=n; i++ )
{
if (LIKELY( (e = p_GetExp( p, i, r)) != 0 ))
term *= CanonicalForm( Variable( i ), e );
}
result += term;
pIter( p );
}
return result;
}
CanonicalForm convSingPFactoryP( poly p, const ring r )
{
if (rVar(r)>1)
{
BOOLEAN setChar=TRUE;
return convSingPFactoryP_intern(p,pLength(p),setChar,r);
}
else
{
CanonicalForm result = 0;
BOOLEAN setChar=TRUE;
coeffs cf=r->cf;
int e;
while ( p!=NULL )
{
CanonicalForm term=cf->convSingNFactoryN(pGetCoeff( p ),setChar, cf);
if (errorreported) break;
setChar=FALSE;
if (LIKELY( (e = p_GetExp( p, 1, r)) != 0 ))
term *= CanonicalForm( Variable( 1 ), e );
result += term;
pIter( p );
}
return result;
}
}
int convFactoryISingI( const CanonicalForm & f)
{
if (!f.isImm()) WerrorS("int overflow in det");
return f.intval();
}
CanonicalForm convSingAPFactoryAP ( poly p , const Variable & a, const ring r)
{
CanonicalForm result = 0;
int e, n = r-> N;
int off=rPar(r);
if (!rField_is_Zp_a(r))
On(SW_RATIONAL);
while ( p!=NULL)
{
CanonicalForm term=convSingAFactoryA(((poly)p_GetCoeff(p, r->cf->extRing)),a, r);
for ( int i = 1; i <= n; i++ )
{
if ( (e = p_GetExp( p, i, r )) != 0 )
term *= CanonicalForm( Variable( i + off), e );
}
result += term;
pIter( p );
}
return result;
}
static void
convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r) ;
poly convFactoryAPSingAP_R ( const CanonicalForm & f, int par_start, int var_start, const ring r )
{
if (f.isZero()) return NULL;
int n = rVar(r)+rPar(r)+1;
int * exp = (int *)omAlloc0(n*sizeof(int));
poly result = NULL;
convRecAP_R( f, exp, result,par_start, var_start, r );
omFreeSize((ADDRESS)exp,n*sizeof(int));
return result;
}
poly convFactoryAPSingAP ( const CanonicalForm & f, const ring r )
{
return convFactoryAPSingAP_R(f,0,rPar(r),r);
}
static void convRecAP_R ( const CanonicalForm & f, int * exp, poly & result, int par_start, int var_start, const ring r )
{
// assume f!=0
if ( ! f.inCoeffDomain() )
{
int l = f.level();
for ( CFIterator i = f; i.hasTerms(); i++ )
{
exp[l] = i.exp();
convRecAP_R( i.coeff(), exp, result, par_start, var_start, r);
}
exp[l] = 0;
}
else
{
poly z=(poly)convFactoryASingA( f,r );
if (z!=NULL)
{
poly term = p_Init(r);
//pNext( term ) = NULL; // done by p_Init
int i;
for ( i = rVar(r); i>0 ; i-- )
p_SetExp( term, i , exp[i+var_start],r);
//if (rRing_has_Comp(currRing->extRing)) p_SetComp(term, 0, currRing->extRing); // done by pInit
if (par_start==0)
{
for ( i = 1; i <= var_start; i++ )
//z->e[i-1]+=exp[i];
p_AddExp(z,i,exp[i],r->cf->extRing);
}
else
{
for ( i = par_start+1; i <= var_start+rPar(r); i++ )
//z->e[i-1]+=exp[i];
p_AddExp(z,i,exp[i-par_start],r->cf->extRing);
}
p_GetCoeff(term, r->cf->extRing)=(number) z;
p_Setm( term,r );
result = p_Add_q( result, term, r );
}
}
}
CanonicalForm convSingAFactoryA ( poly p , const Variable & a, const ring r )
{
CanonicalForm result = 0;
int e;
while ( p!=NULL )
{
CanonicalForm term;
if ( rField_is_Zp_a(r) )
{
term = n_Int( p_GetCoeff( p, r->cf->extRing ), r->cf->extRing->cf );
}
else
{
if ( SR_HDL(p_GetCoeff( p, r->cf->extRing )) & SR_INT )
term = SR_TO_INT(p_GetCoeff( p, r->cf->extRing )) ;
else
{
if ( p_GetCoeff( p, r->cf->extRing )->s == 3 )
{
mpz_t dummy;
mpz_init_set( dummy, (p_GetCoeff( p,r->cf->extRing )->z) );
term = make_cf( dummy );
}
else
{
// assume s==0 or s==1
mpz_t num, den;
On(SW_RATIONAL);
mpz_init_set( num, (p_GetCoeff( p, r->cf->extRing )->z) );
mpz_init_set( den, (p_GetCoeff( p, r->cf->extRing )->n) );
term = make_cf( num, den, ( p_GetCoeff( p, r->cf->extRing )->s != 1 ));
}
}
}
if ( (e = p_GetExp( p, 1, r->cf->extRing )) != 0 )
term *= power( a , e );
result += term;
p = pNext( p );
}
return result;
}
static number convFactoryNSingAN( const CanonicalForm &f, const ring r)
{
assume (r != NULL);
assume (r->cf != NULL);
assume (r->cf->extRing != NULL);
// r->cf->extRing->cf has to be Q or Z/p (the supported types of factory)
return n_convFactoryNSingN( f, r->cf->extRing->cf );
}
poly convFactoryASingA ( const CanonicalForm & f, const ring r )
{
poly a=NULL;
for( CFIterator i=f; i.hasTerms(); i++)
{
number n= convFactoryNSingAN( i.coeff(), r );
if (n_IsZero(n,r->cf->extRing->cf))
{
n_Delete(&n,r->cf->extRing->cf);
}
else
{
poly t= p_Init (r->cf->extRing);
pGetCoeff(t)=n;
p_SetExp(t,1,i.exp(),r->cf->extRing);
//p_Setm(t,r->cf->extRing);// not needed for rings with 1 variable
a=p_Add_q(a,t,r->cf->extRing);
}
}
if (a!=NULL)
{
if( r->cf->extRing != NULL )
if (r->cf->extRing->qideal->m[0]!=NULL)
{
poly l=r->cf->extRing->qideal->m[0];
if (p_GetExp(a,1,r->cf->extRing) >= p_GetExp(l,1,r->cf->extRing))
a = p_PolyDiv (a, l, FALSE, r->cf->extRing); // ???
}
}
return a;
}
CanonicalForm convSingTrPFactoryP ( poly p, const ring r )
{
CanonicalForm result = 0;
int e, n = rVar(r);
int offs = rPar(r);
while ( p!=NULL )
{
//n_Normalize(p_GetCoeff(p, r), r->cf);
// test if denominator is constant
if (!errorreported && !p_IsConstant(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing))
WerrorS("conversion error: denominator!= 1");
CanonicalForm term=convSingPFactoryP(NUM ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
// if denominator is not NULL it should be a constant at this point
if (DEN ((fraction)p_GetCoeff(p,r)) != NULL)
{
CanonicalForm den= convSingPFactoryP(DEN ((fraction)p_GetCoeff(p, r)),r->cf->extRing);
if (rChar (r) == 0)
On (SW_RATIONAL);
term /= den;
}
for ( int i = n; i > 0; i-- )
{
if ( (e = p_GetExp( p, i,r )) != 0 )
term = term * power( Variable( i + offs ), e );
}
result += term;
p = pNext( p );
}
return result;
}
BOOLEAN convSingTrP(poly p, const ring r )
{
while ( p!=NULL )
{
n_Normalize(p_GetCoeff(p, r), r->cf);
// test if denominator is constant
if (!p_IsConstant(DEN ((fraction)p_GetCoeff (p,r)),r->cf->extRing))
return FALSE;
pIter(p);
}
return TRUE;
}
poly convFactoryPSingTrP ( const CanonicalForm & f, const ring r )
{
if (f.isZero()) return NULL;
int n = rVar(r)+1;
int * exp = (int*)omAlloc0(n*sizeof(int));
poly result = NULL;
convRecTrP( f, exp, result , rPar(r), r );
omFreeSize((ADDRESS)exp,n*sizeof(int));
return result;
}
static void
convRecTrP ( const CanonicalForm & f, int * exp, poly & result , int offs, const ring r)
{
// assume f!= 0
if ( f.level() > offs )
{
int l = f.level();
for ( CFIterator i = f; i.hasTerms(); i++ )
{
exp[l-offs] = i.exp();
convRecTrP( i.coeff(), exp, result, offs, r );
}
exp[l-offs] = 0;
}
else
{
poly term = p_Init(r);
//pNext( term ) = NULL; // done by p_Init
for ( int i = rVar(r); i>0; i-- )
p_SetExp( term, i ,exp[i], r);
//if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
pGetCoeff(term)=ntInit(convFactoryPSingP( f, r->cf->extRing ), r->cf);
p_Setm( term,r );
result = p_Add_q( result, term,r );
}
}
#if 0
CanonicalForm
convSingGFFactoryGF( poly p )
{
CanonicalForm result=CanonicalForm(0);
int e, n = pVariables;
while ( p != NULL )
{
CanonicalForm term;
term = make_cf_from_gf( (int)(long)pGetCoeff( p ) );
//int * A=(int *)&term;
//Print("term=%x, == 0 ?: %d\n",*A, term.isZero());
for ( int i = 1; i <= n; i++ )
{
if ( (e = pGetExp( p, i )) != 0 )
term *= power( Variable( i ), e );
}
result += term;
p = pNext( p );
}
return result;
}
poly
convFactoryGFSingGF ( const CanonicalForm & f )
{
// cerr << " f = " << f << endl;
int n = pVariables+1;
/* ASSERT( level( f ) <= pVariables, "illegal number of variables" ); */
int * exp = (int*)omAlloc0(n*sizeof(int));
poly result = NULL;
convRecGFGF( f, exp, result );
omFreeSize((ADDRESS)exp,n*sizeof(int));
return result;
}
static void
convRecGFGF ( const CanonicalForm & f, int * exp, poly & result )
{
if (f.isZero())
return;
if ( ! f.inCoeffDomain() )
{
int l = f.level();
for ( CFIterator i = f; i.hasTerms(); i++ )
{
exp[l] = i.exp();
convRecGFGF( i.coeff(), exp, result );
}
exp[l] = 0;
}
else
{
poly term = pInit();
pNext( term ) = NULL;
for ( int i = 1; i <= pVariables; i++ )
pSetExp( term, i, exp[i]);
//if (rRing_has_Comp(currRing)) p_SetComp(term, 0, currRing); // done by pInit
pGetCoeff( term ) = (number) gf_value (f);
pSetm( term );
result = pAdd( result, term );
}
}
#endif
auxiliary.h
All the auxiliary stuff.
UNLIKELY
#define UNLIKELY(X)
Definition auxiliary.h:405
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
LIKELY
#define LIKELY(X)
Definition auxiliary.h:404
ADDRESS
void * ADDRESS
Definition auxiliary.h:120
On
void On(int sw)
switches
Definition canonicalform.cc:1957
power
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
Definition canonicalform.cc:1896
num
CanonicalForm num(const CanonicalForm &f)
Definition canonicalform.h:337
den
CanonicalForm den(const CanonicalForm &f)
Definition canonicalform.h:340
N
const CanonicalForm CFMap CFMap & N
Definition cfEzgcd.cc:56
l
int l
Definition cfEzgcd.cc:100
i
int i
Definition cfEzgcd.cc:132
p
int p
Definition cfModGcd.cc:4086
cf
CanonicalForm cf
Definition cfModGcd.cc:4091
SW_RATIONAL
static const int SW_RATIONAL
set to 1 for computations over Q
Definition cf_defs.h:31
f
FILE * f
Definition checklibs.c:9
convSingPFactoryP
CanonicalForm convSingPFactoryP(poly p, const ring r)
Definition clapconv.cc:138
convSingTrP
BOOLEAN convSingTrP(poly p, const ring r)
Definition clapconv.cc:375
convFactoryPSingTrP
poly convFactoryPSingTrP(const CanonicalForm &f, const ring r)
Definition clapconv.cc:389
convFactoryAPSingAP
poly convFactoryAPSingAP(const CanonicalForm &f, const ring r)
Definition clapconv.cc:207
convSingPFactoryP_intern
static CanonicalForm convSingPFactoryP_intern(poly p, int l, BOOLEAN &setChar, const ring r)
Definition clapconv.cc:104
convSingAFactoryA
CanonicalForm convSingAFactoryA(poly p, const Variable &a, const ring r)
Definition clapconv.cc:255
convPhalf
static void convPhalf(poly p, int l, poly &p1, poly &p2)
Definition clapconv.cc:86
convSingAPFactoryAP
CanonicalForm convSingAPFactoryAP(poly p, const Variable &a, const ring r)
Definition clapconv.cc:171
convFactoryASingA
poly convFactoryASingA(const CanonicalForm &f, const ring r)
Definition clapconv.cc:307
convFactoryPSingP
poly convFactoryPSingP(const CanonicalForm &f, const ring r)
Definition clapconv.cc:40
convFactoryNSingAN
static number convFactoryNSingAN(const CanonicalForm &f, const ring r)
Definition clapconv.cc:298
convFactoryISingI
int convFactoryISingI(const CanonicalForm &f)
Definition clapconv.cc:165
convRecTrP
static void convRecTrP(const CanonicalForm &f, int *exp, poly &result, int offs, const ring r)
Definition clapconv.cc:401
conv_RecPP
static void conv_RecPP(const CanonicalForm &f, int *exp, sBucket_pt result, ring r)
Definition clapconv.cc:54
out_cf
void out_cf(const char *s1, const CanonicalForm &f, const char *s2)
Definition cf_factor.cc:103
MIN_CONV_LEN
#define MIN_CONV_LEN
Definition clapconv.cc:103
convSingTrPFactoryP
CanonicalForm convSingTrPFactoryP(poly p, const ring r)
Definition clapconv.cc:339
convRecAP_R
static void convRecAP_R(const CanonicalForm &f, int *exp, poly &result, int par_start, int var_start, const ring r)
Definition clapconv.cc:212
convPunhalf
static poly convPunhalf(poly p1, poly p2)
Definition clapconv.cc:95
convFactoryAPSingAP_R
poly convFactoryAPSingAP_R(const CanonicalForm &f, int par_start, int var_start, const ring r)
Definition clapconv.cc:196
clapconv.h
CFIterator
class to iterate through CanonicalForm's
Definition cf_iter.h:44
CanonicalForm
factory's main class
Definition canonicalform.h:86
Variable
factory's class for variables
Definition factory.h:127
term
Definition int_poly.h:33
coeffs.h
Coefficient rings, fields and other domains suitable for Singular polynomials.
n_Int
static FORCE_INLINE long n_Int(number &n, const coeffs r)
conversion of n to an int; 0 if not possible in Z/pZ: the representing int lying in (-p/2 ....
Definition coeffs.h:550
n_convFactoryNSingN
static FORCE_INLINE number n_convFactoryNSingN(const CanonicalForm n, const coeffs r)
Definition coeffs.h:981
n_IsZero
static FORCE_INLINE BOOLEAN n_IsZero(number n, const coeffs r)
TRUE iff 'n' represents the zero element.
Definition coeffs.h:470
n_Delete
static FORCE_INLINE void n_Delete(number *p, const coeffs r)
delete 'p'
Definition coeffs.h:461
n_Normalize
static FORCE_INLINE void n_Normalize(number &n, const coeffs r)
inplace-normalization of n; produces some canonical representation of n;
Definition coeffs.h:581
result
return result
Definition facAbsBiFact.cc:76
factory.h
factory.h' is the user interface to Factory.
make_cf
CanonicalForm FACTORY_PUBLIC make_cf(const mpz_ptr n)
Definition singext.cc:66
make_cf_from_gf
CanonicalForm make_cf_from_gf(const int z)
Definition singext.cc:76
gf_value
int gf_value(const CanonicalForm &f)
Definition singext.cc:60
errorreported
VAR short errorreported
Definition feFopen.cc:23
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
longrat.h
SR_INT
#define SR_INT
Definition longrat.h:67
SR_TO_INT
#define SR_TO_INT(SR)
Definition longrat.h:69
assume
#define assume(x)
Definition mod2.h:389
pIter
#define pIter(p)
Definition monomials.h:37
pNext
#define pNext(p)
Definition monomials.h:36
p_GetCoeff
#define p_GetCoeff(p, r)
Definition monomials.h:50
pGetCoeff
static number & pGetCoeff(poly p)
return an alias to the leading coefficient of p assumes that p != NULL NOTE: not copy
Definition monomials.h:44
exp
gmp_float exp(const gmp_float &a)
Definition mpr_complex.cc:349
coeffs
The main handler for Singular numbers which are suitable for Singular polynomials.
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
NULL
#define NULL
Definition omList.c:12
p_PolyDiv
poly p_PolyDiv(poly &p, const poly divisor, const BOOLEAN needResult, const ring r)
assumes that p and divisor are univariate polynomials in r, mentioning the same variable; assumes div...
Definition p_polys.cc:1874
p_polys.h
pLength
static int pLength(poly a)
Definition p_polys.h:190
p_Add_q
static poly p_Add_q(poly p, poly q, const ring r)
Definition p_polys.h:938
p_AddExp
static long p_AddExp(poly p, int v, long ee, ring r)
Definition p_polys.h:608
p_SetExpV
static void p_SetExpV(poly p, int *ev, const ring r)
Definition p_polys.h:1565
p_SetExp
static unsigned long p_SetExp(poly p, const unsigned long e, const unsigned long iBitmask, const int VarOffset)
set a single variable exponent @Note: VarOffset encodes the position in p->exp
Definition p_polys.h:490
p_Setm
static void p_Setm(poly p, const ring r)
Definition p_polys.h:235
p_GetExp
static long p_GetExp(const poly p, const unsigned long iBitmask, const int VarOffset)
get a single variable exponent @Note: the integer VarOffset encodes:
Definition p_polys.h:471
p_IsConstant
static BOOLEAN p_IsConstant(const poly p, const ring r)
Definition p_polys.h:1985
p_Init
static poly p_Init(const ring r, omBin bin)
Definition p_polys.h:1341
pAdd
#define pAdd(p, q)
Definition polys.h:204
pSetm
#define pSetm(p)
Definition polys.h:272
pGetExp
#define pGetExp(p, i)
Exponent.
Definition polys.h:42
pInit
#define pInit()
allocates a new monomial and initializes everything to 0
Definition polys.h:62
pSetExp
#define pSetExp(p, i, v)
Definition polys.h:43
NUM
#define NUM
Definition readcf.cc:180
rChar
int rChar(ring r)
Definition ring.cc:719
rField_is_Zp_a
static BOOLEAN rField_is_Zp_a(const ring r)
Definition ring.h:535
rPar
static int rPar(const ring r)
(r->cf->P)
Definition ring.h:605
rVar
static short rVar(const ring r)
define rVar(r) (r->N)
Definition ring.h:598
sBucketCreate
sBucket_pt sBucketCreate(const ring r)
Definition sbuckets.cc:96
sBucket_Merge_m
void sBucket_Merge_m(sBucket_pt bucket, poly p)
Definition sbuckets.cc:127
sbuckets.h
sBucket_pt
sBucket * sBucket_pt
Definition sbuckets.h:16
sBucketDestroyMerge
void sBucketDestroyMerge(sBucket_pt bucket, poly *p, int *length)
Definition sbuckets.h:61
simpleideals.h
SR_HDL
#define SR_HDL(A)
Definition tgb.cc:35
ntInit
number ntInit(long i, const coeffs cf)
Definition transext.cc:704
transext.h