Loading...
Searching...
No Matches
pcv.cc
Go to the documentation of this file.
/*****************************************
* Computer Algebra System SINGULAR *
*****************************************/
/*
* ABSTRACT: conversion between polys and coef vectors
*/
#include "kernel/mod2.h"
#ifdef HAVE_PCV
#include "Singular/tok.h"
#include "Singular/ipid.h"
#include "coeffs/numbers.h"
#include "kernel/polys.h"
#include "kernel/ideals.h"
#include "Singular/lists.h"
#include "polys/matpol.h"
#include "Singular/ipshell.h"
#include "Singular/pcv.h"
STATIC_VAR int pcvMaxDegree;
STATIC_VAR int pcvTableSize;
STATIC_VAR int pcvIndexSize;
STATIC_VAR unsigned* pcvTable=NULL;
STATIC_VAR unsigned** pcvIndex=NULL;
lists pcvLAddL(lists l1,lists l2)
{
lists l0=(lists)omAllocBin(slists_bin);
int i=l1->nr;
if(l1->nr<l2->nr) i=l2->nr;
l0->Init(i+1);
for(;i>=0;i--)
{
if(i<=l1->nr
&&(l1->m[i].rtyp==POLY_CMD||l1->m[i].rtyp==BUCKET_CMD||l1->m[i].rtyp==VECTOR_CMD))
{
if (l1->m[i].rtyp==BUCKET_CMD)
{
poly p=sBucketPeek((sBucket_pt)l1->m[i].data);
l0->m[i].rtyp=POLY_CMD;
l0->m[i].data=pCopy(p);
}
else
{
l0->m[i].rtyp=l1->m[i].rtyp;
l0->m[i].data=pCopy((poly)l1->m[i].data);
}
if(i<=l2->nr&&l2->m[i].rtyp==l0->m[i].rtyp)
l0->m[i].data=pAdd((poly)l0->m[i].data,pCopy((poly)l2->m[i].data));
}
else
if(i<=l2->nr
&&(l2->m[i].rtyp==POLY_CMD||l2->m[i].rtyp==BUCKET_CMD||l2->m[i].rtyp==VECTOR_CMD))
{
if (l2->m[i].rtyp==BUCKET_CMD)
{
poly p=sBucketPeek((sBucket_pt)l2->m[i].data);
l0->m[i].rtyp=POLY_CMD;
l0->m[i].data=pCopy(p);
}
else
{
l0->m[i].rtyp=l2->m[i].rtyp;
l0->m[i].data=pCopy((poly)l2->m[i].data);
}
}
}
return(l0);
}
lists pcvPMulL(poly p,lists l1)
{
lists l0=(lists)omAllocBin(slists_bin);
l0->Init(l1->nr+1);
for(int i=l1->nr;i>=0;i--)
{
if(l1->m[i].rtyp==POLY_CMD)
{
l0->m[i].rtyp=POLY_CMD;
l0->m[i].data=ppMult_qq(p,(poly)l1->m[i].data);
}
else if(l1->m[i].rtyp==BUCKET_CMD)
{
l0->m[i].rtyp=POLY_CMD;
l0->m[i].data=ppMult_qq(p,sBucketPeek((sBucket_pt)l1->m[i].data));
}
}
return(l0);
}
BOOLEAN pcvLAddL(leftv res,leftv h)
{
const short t[]={2,LIST_CMD,LIST_CMD};
if (iiCheckTypes(h,t,1))
{
lists l1=(lists)h->Data();
h=h->next;
lists l2=(lists)h->Data();
res->rtyp=LIST_CMD;
res->data=(void*)pcvLAddL(l1,l2);
return FALSE;
}
return TRUE;
}
BOOLEAN pcvPMulL(leftv res,leftv h)
{
const short t1[]={2,POLY_CMD,LIST_CMD};
const short t2[]={2,BUCKET_CMD,LIST_CMD};
if (iiCheckTypes(h,t2,0)
||iiCheckTypes(h,t1,1))
{
poly p=(poly)h->Data();
h=h->next;
lists l=(lists)h->Data();
res->rtyp=LIST_CMD;
res->data=(void*)pcvPMulL(p,l);
return FALSE;
}
return TRUE;
}
int pcvDeg(poly p)
{
int d=0;
for(int i=currRing->N;i>=1;i--) d+=pGetExp(p,i);
return d;
}
int pcvMinDeg(poly p)
{
if(!p) return -1;
int md=pcvDeg(p);
pIter(p);
while(p)
{
int d=pcvDeg(p);
if(d<md) md=d;
pIter(p);
}
return md;
}
int pcvMinDeg(matrix m)
{
int i,j,d;
int md=-1;
for(i=1;i<=MATROWS(m);i++)
{
for(j=1;j<=MATCOLS(m);j++)
{
d=pcvMinDeg(MATELEM(m,i,j));
if((d>=0&&md>d)||md==-1) md=d;
}
}
return(md);
}
BOOLEAN pcvMinDeg(leftv res,leftv h)
{
if(h)
{
if(h->Typ()==BUCKET_CMD)
{
res->rtyp=INT_CMD;
res->data=(void*)(long)pcvMinDeg(sBucketPeek((sBucket_pt)h->Data()));
return FALSE;
}
else
if(h->Typ()==POLY_CMD)
{
res->rtyp=INT_CMD;
res->data=(void*)(long)pcvMinDeg((poly)h->Data());
return FALSE;
}
else
if(h->Typ()==MATRIX_CMD)
{
res->rtyp=INT_CMD;
res->data=(void*)(long)pcvMinDeg((matrix)h->Data());
return FALSE;
}
}
WerrorS("<poly> expected");
return TRUE;
}
void pcvInit(int d)
{
if(d<0) d=1;
pcvMaxDegree=d+1;
pcvTableSize=currRing->N*pcvMaxDegree*sizeof(unsigned);
pcvTable=(unsigned*)omAlloc0(pcvTableSize);
pcvIndexSize=currRing->N*sizeof(unsigned*);
pcvIndex=(unsigned**)omAlloc(pcvIndexSize);
for(int i=0;i<currRing->N;i++)
pcvIndex[i]=pcvTable+i*pcvMaxDegree;
for(int i=0;i<pcvMaxDegree;i++)
pcvIndex[0][i]=i;
unsigned k,l;
for(int i=1;i<currRing->N;i++)
{
k=0;
for(int j=0;j<pcvMaxDegree;j++)
{
l=pcvIndex[i-1][j];
if(l>unsigned(~0)-k)
{
j=pcvMaxDegree;
i=currRing->N;
WerrorS("unsigned overflow");
}
else pcvIndex[i][j]=k+=l;
}
}
}
void pcvClean()
{
if(pcvTable)
{
omFreeSize(pcvTable,pcvTableSize);
pcvTable=NULL;
}
if(pcvIndex)
{
omFreeSize(pcvIndex,pcvIndexSize);
pcvIndex=NULL;
}
}
int pcvM2N(poly m)
{
unsigned n=0,dn,d=0;
for(int i=0;i<currRing->N;i++)
{
d+=pGetExp(m,i+1);
dn=pcvIndex[i][d];
if(dn>MAX_INT_VAL-n)
{
i=currRing->N;
WerrorS("component overflow");
}
else n+=dn;
}
return n+1;
}
poly pcvN2M(int n)
{
n--;
poly m=pOne();
int i,j=0,k;
for(i=currRing->N-1;i>=0;i--)
{
k=j;
for(j=0; (j<pcvMaxDegree) && (pcvIndex[i][j]<=(unsigned)n); j++);
j--;
n-=pcvIndex[i][j];
if(i<currRing->N-1) pSetExp(m,i+2,k-j);
}
if(n==0)
{
pSetExp(m,1,j);
pSetm(m);
return m;
}
else
{
pLmDelete(&m);
return NULL;
}
}
poly pcvP2CV(poly p,int d0,int d1)
{
poly cv=NULL;
while(p)
{
int d=pcvDeg(p);
if(d0<=d&&d<d1)
{
poly c=pNSet(nCopy(pGetCoeff(p)));
pSetComp(c,pcvM2N(p));
cv=pAdd(cv,c);
}
pIter(p);
}
return cv;
}
poly pcvCV2P(poly cv,int d0,int d1)
{
poly p=NULL;
while(cv)
{
poly m=pcvN2M(pGetComp(cv));
if(m)
{
int d=pcvDeg(m);
if(d0<=d&&d<d1)
{
pSetCoeff(m,nCopy(pGetCoeff(cv)));
p=pAdd(p,m);
}
}
pIter(cv);
}
return p;
}
lists pcvP2CV(lists pl,int d0,int d1)
{
lists cvl=(lists)omAllocBin(slists_bin);
cvl->Init(pl->nr+1);
pcvInit(d1);
for(int i=pl->nr;i>=0;i--)
{
if(pl->m[i].rtyp==POLY_CMD)
{
cvl->m[i].rtyp=VECTOR_CMD;
cvl->m[i].data=pcvP2CV((poly)pl->m[i].data,d0,d1);
}
else if(pl->m[i].rtyp==BUCKET_CMD)
{
cvl->m[i].rtyp=VECTOR_CMD;
cvl->m[i].data=pcvP2CV(sBucketPeek((sBucket_pt)pl->m[i].data),d0,d1);
}
}
pcvClean();
return cvl;
}
lists pcvCV2P(lists cvl,int d0,int d1)
{
lists pl=(lists)omAllocBin(slists_bin);
pl->Init(cvl->nr+1);
pcvInit(d1);
for(int i=cvl->nr;i>=0;i--)
{
if(cvl->m[i].rtyp==VECTOR_CMD)
{
pl->m[i].rtyp=POLY_CMD;
pl->m[i].data=pcvCV2P((poly)cvl->m[i].data,d0,d1);
}
}
pcvClean();
return pl;
}
BOOLEAN pcvP2CV(leftv res,leftv h)
{
if(currRing)
{
const short t[]={3,LIST_CMD,INT_CMD,INT_CMD};
if (iiCheckTypes(h,t,1))
{
lists p=(lists)h->Data();
h=h->next;
int d0=(int)(long)h->Data();
h=h->next;
int d1=(int)(long)h->Data();
res->rtyp=LIST_CMD;
res->data=(void*)pcvP2CV(p,d0,d1);
return FALSE;
}
return TRUE;
}
WerrorS("no ring active");
return TRUE;
}
BOOLEAN pcvCV2P(leftv res,leftv h)
{
if(currRing)
{
const short t[]={3,LIST_CMD,INT_CMD,INT_CMD};
if (iiCheckTypes(h,t,1))
{
lists pl=(lists)h->Data();
h=h->next;
int d0=(int)(long)h->Data();
h=h->next;
int d1=(int)(long)h->Data();
res->rtyp=LIST_CMD;
res->data=(void*)pcvCV2P(pl,d0,d1);
return FALSE;
}
return TRUE;
}
WerrorS("no ring active");
return TRUE;
}
int pcvDim(int d0,int d1)
{
if(d0<0) d0=0;
if(d1<0) d1=0;
pcvInit(d1);
int d=pcvIndex[currRing->N-1][d1]-pcvIndex[currRing->N-1][d0];
pcvClean();
return d;
}
BOOLEAN pcvDim(leftv res,leftv h)
{
if(currRing)
{
const short t[]={2,INT_CMD,INT_CMD};
if (iiCheckTypes(h,t,1))
{
int d0=(int)(long)h->Data();
h=h->next;
int d1=(int)(long)h->Data();
res->rtyp=INT_CMD;
res->data=(void*)(long)pcvDim(d0,d1);
return FALSE;
}
return TRUE;
}
WerrorS("no ring active");
return TRUE;
}
int pcvBasis(lists b,int i,poly m,int d,int n)
{
if(n<currRing->N)
{
for(int k=0,l=d;k<=l;k++,d--)
{
pSetExp(m,n,k);
i=pcvBasis(b,i,m,d,n+1);
}
}
else
{
pSetExp(m,n,d);
pSetm(m);
b->m[i].rtyp=POLY_CMD;
b->m[i++].data=pCopy(m);
}
return i;
}
lists pcvBasis(int d0,int d1)
{
if(d0<0) d0=0;
if(d1<0) d1=0;
lists b=(lists)omAllocBin(slists_bin);
b->Init(pcvDim(d0,d1));
poly m=pOne();
for(int d=d0,i=0;d<d1;d++)
i=pcvBasis(b,i,m,d,1);
pLmDelete(&m);
return b;
}
BOOLEAN pcvBasis(leftv res,leftv h)
{
if(currRing)
{
const short t[]={2,INT_CMD,INT_CMD};
if (iiCheckTypes(h,t,1))
{
int d0=(int)(long)h->Data();
h=h->next;
int d1=(int)(long)h->Data();
res->rtyp=LIST_CMD;
res->data=(void*)pcvBasis(d0,d1);
return FALSE;
}
return TRUE;
}
WerrorS("no ring active");
return TRUE;
}
#endif /* HAVE_PCV */
BOOLEAN
int BOOLEAN
Definition auxiliary.h:88
TRUE
#define TRUE
Definition auxiliary.h:101
FALSE
#define FALSE
Definition auxiliary.h:97
N
const CanonicalForm CFMap CFMap & N
Definition cfEzgcd.cc:56
l
int l
Definition cfEzgcd.cc:100
m
int m
Definition cfEzgcd.cc:128
i
int i
Definition cfEzgcd.cc:132
k
int k
Definition cfEzgcd.cc:99
p
int p
Definition cfModGcd.cc:4086
b
CanonicalForm b
Definition cfModGcd.cc:4111
sleftv::rtyp
int rtyp
Definition subexpr.h:91
sleftv::data
void * data
Definition subexpr.h:88
slists::m
sleftv * m
Definition lists.h:46
slists::Init
INLINE_THIS void Init(int l=0)
slists::nr
int nr
Definition lists.h:44
res
CanonicalForm res
Definition facAbsFact.cc:60
j
int j
Definition facHensel.cc:110
WerrorS
void WerrorS(const char *s)
Definition feFopen.cc:24
STATIC_VAR
#define STATIC_VAR
Definition globaldefs.h:7
MATRIX_CMD
@ MATRIX_CMD
Definition grammar.cc:287
BUCKET_CMD
@ BUCKET_CMD
Definition grammar.cc:284
VECTOR_CMD
@ VECTOR_CMD
Definition grammar.cc:293
POLY_CMD
@ POLY_CMD
Definition grammar.cc:290
ideals.h
ipid.h
iiCheckTypes
BOOLEAN iiCheckTypes(leftv args, const short *type_list, int report)
check a list of arguemys against a given field of types return TRUE if the types match return FALSE (...
Definition ipshell.cc:6576
ipshell.h
h
STATIC_VAR Poly * h
Definition janet.cc:971
slists_bin
VAR omBin slists_bin
Definition lists.cc:23
lists.h
matpol.h
MATELEM
#define MATELEM(mat, i, j)
1-based access to matrix
Definition matpol.h:29
matrix
ip_smatrix * matrix
Definition matpol.h:43
MATROWS
#define MATROWS(i)
Definition matpol.h:26
MATCOLS
#define MATCOLS(i)
Definition matpol.h:27
mod2.h
pIter
#define pIter(p)
Definition monomials.h:37
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
lists
slists * lists
Definition mpr_numeric.h:146
MAX_INT_VAL
const int MAX_INT_VAL
Definition mylimits.h:12
numbers.h
nCopy
#define nCopy(n)
Definition numbers.h:15
omFreeSize
#define omFreeSize(addr, size)
Definition omAllocDecl.h:260
omAlloc
#define omAlloc(size)
Definition omAllocDecl.h:210
omAllocBin
#define omAllocBin(bin)
Definition omAllocDecl.h:205
omAlloc0
#define omAlloc0(size)
Definition omAllocDecl.h:211
NULL
#define NULL
Definition omList.c:12
pcvDeg
int pcvDeg(poly p)
Definition pcv.cc:128
pcvP2CV
poly pcvP2CV(poly p, int d0, int d1)
Definition pcv.cc:280
pcvBasis
int pcvBasis(lists b, int i, poly m, int d, int n)
Definition pcv.cc:430
pcvMinDeg
int pcvMinDeg(poly p)
Definition pcv.cc:135
pcvMaxDegree
STATIC_VAR int pcvMaxDegree
Definition pcv.cc:25
pcvInit
void pcvInit(int d)
Definition pcv.cc:193
pcvDim
int pcvDim(int d0, int d1)
Definition pcv.cc:400
pcvPMulL
lists pcvPMulL(poly p, lists l1)
Definition pcv.cc:76
pcvTable
STATIC_VAR unsigned * pcvTable
Definition pcv.cc:28
pcvN2M
poly pcvN2M(int n)
Definition pcv.cc:254
pcvIndex
STATIC_VAR unsigned ** pcvIndex
Definition pcv.cc:29
pcvM2N
int pcvM2N(poly m)
Definition pcv.cc:237
pcvClean
void pcvClean()
Definition pcv.cc:223
pcvCV2P
poly pcvCV2P(poly cv, int d0, int d1)
Definition pcv.cc:297
pcvLAddL
lists pcvLAddL(lists l1, lists l2)
Definition pcv.cc:31
pcvTableSize
STATIC_VAR int pcvTableSize
Definition pcv.cc:26
pcvIndexSize
STATIC_VAR int pcvIndexSize
Definition pcv.cc:27
pcv.h
currRing
VAR ring currRing
Widely used global variable which specifies the current polynomial ring for Singular interpreter and ...
Definition polys.cc:13
polys.h
Compatibility layer for legacy polynomial operations (over currRing).
pAdd
#define pAdd(p, q)
Definition polys.h:204
pSetm
#define pSetm(p)
Definition polys.h:272
pGetComp
#define pGetComp(p)
Component.
Definition polys.h:38
pSetCoeff
#define pSetCoeff(p, n)
deletes old coeff before setting the new one
Definition polys.h:32
pNSet
#define pNSet(n)
Definition polys.h:314
ppMult_qq
#define ppMult_qq(p, q)
Definition polys.h:209
pSetComp
#define pSetComp(p, v)
Definition polys.h:39
pLmDelete
#define pLmDelete(p)
assume p != NULL, deletes Lm(p)->coef and Lm(p)
Definition polys.h:77
pGetExp
#define pGetExp(p, i)
Exponent.
Definition polys.h:42
pSetExp
#define pSetExp(p, i, v)
Definition polys.h:43
pCopy
#define pCopy(p)
return a copy of the poly
Definition polys.h:186
pOne
#define pOne()
Definition polys.h:316
sBucketPeek
poly sBucketPeek(sBucket_pt b)
Definition sbuckets.cc:455
sBucket_pt
sBucket * sBucket_pt
Definition sbuckets.h:16
leftv
sleftv * leftv
Definition structs.h:53
tok.h
LIST_CMD
@ LIST_CMD
Definition tok.h:118
INT_CMD
@ INT_CMD
Definition tok.h:96