Loading...
Searching...
No Matches
gfops.h
Go to the documentation of this file.
/* emacs edit mode for this file is -*- C++ -*- */
/**
* @file gfops.h
*
* Operations in GF, where GF is a finite field of size less than 2^16
* represented by a root of Conway polynomial. Uses look up tables for addition.
*
* @sa gf_tabutil.h
**/
#ifndef INCL_GFOPS_H
#define INCL_GFOPS_H
// #include "config.h"
#ifndef NOSTREAMIO
#ifdef HAVE_IOSTREAM
#include <iostream>
#define OSTREAM std::ostream
#elif defined(HAVE_IOSTREAM_H)
#include <iostream.h>
#define OSTREAM ostream
#endif
#endif /* NOSTREAMIO */
#include "cf_assert.h"
#include "cf_defs.h"
#include "canonicalform.h"
EXTERN_VAR int gf_q;
EXTERN_VAR int gf_p;
EXTERN_VAR int gf_n;
EXTERN_VAR int gf_q1;
EXTERN_VAR int gf_m1;
EXTERN_VAR char gf_name;
EXTERN_VAR unsigned short * gf_table;
EXTERN_INST_VAR CanonicalForm gf_mipo;
//{{{ predicates
inline bool gf_iszero ( int a )
{
return gf_q == a;
}
inline bool gf_iszero ( long a )
{
return gf_q == a;
}
inline bool gf_isone ( int a )
{
return 0 == a;
}
inline bool gf_isone ( long a )
{
return 0 == a;
}
//}}}
//{{{ conversion functions
inline int gf_int2gf ( int i )
{
while ( i < 0 )
i += gf_p;
while ( i >= gf_p )
i -= gf_p;
if ( i == 0 )
return gf_q;
int c = 0;
while ( i > 1 ) {
c = gf_table[c];
i--;
}
return c;
}
inline long gf_int2gf ( long i )
{
while ( i < 0 )
i += gf_p;
while ( i >= gf_p )
i -= gf_p;
if ( i == 0 )
return gf_q;
long c = 0;
while ( i > 1 ) {
c = gf_table[c];
i--;
}
return c;
}
//}}}
//{{{ zero and one
inline int gf_zero()
{
return gf_q;
}
inline int gf_one()
{
return 0;
}
//}}}
//{{{ inline int gf_sign ( int a )
// docu: see imm_sign()
inline
int gf_sign ( int a )
{
if ( gf_iszero( a ) )
return 0;
else
return 1;
}
//}}}
//{{{ arithmetic operators
inline int gf_neg ( int a )
{
// -z^a=z^a*(-1)=z^a*gf_m1;
if ( a == gf_q ) return a;
int i = a + gf_m1;
if ( i >= gf_q1 )
i -= gf_q1;
return i;
}
inline int gf_add ( int a, int b )
{
// z^a+z^b=z^b*(z^(a-b)+1), if a>=b;
// =z^a*(z^(b-a)+1), if a<b;
if ( a == gf_q ) return b;
if ( b == gf_q ) return a;
int zb, zab, r;
if ( a >= b ) {
zb = b;
zab = a - b;
}
else {
zb = a;
zab = b - a;
}
if ( gf_table[zab] == gf_q )
r = gf_q; /*if z^(a-b)+1 =0*/
else {
r= zb + gf_table[zab];
if ( r >= gf_q1 )
r -= gf_q1;
}
return r;
}
inline int gf_sub ( int a, int b )
{
return gf_add( a, gf_neg( b ) );
}
inline int gf_mul ( int a, int b )
{
if ( a == gf_q || b == gf_q )
return gf_q;
else {
int i = a + b;
if ( i >= gf_q1 ) i -= gf_q1;
return i;
}
}
inline long gf_mul ( long a, int b )
{
if ( a == gf_q || b == gf_q )
return gf_q;
else {
long i = a + b;
if ( i >= gf_q1 ) i -= gf_q1;
return i;
}
}
inline int gf_div ( int a, int b )
{
ASSERT( b != gf_q, "divide by zero" );
if ( a == gf_q )
return gf_q;
else {
int s = a - b;
if (s < 0)
s += gf_q1;
return s;
}
}
inline int gf_inv ( int a )
{
ASSERT( a != gf_q, "divide by zero" );
return gf_q1 - a;
}
//}}}
//{{{ input/output
#ifndef NOSTREAMIO
inline void gf_print ( OSTREAM & os, int a )
{
if ( a == gf_q )
os << "0";
else if ( a == 0 )
os << "1";
else if ( a == 1 )
os << gf_name;
else
os << gf_name << "^" << a;
}
#endif /* NOSTREAMIO */
//}}}
//{{{ exponentiation
inline int gf_power ( int a, int n )
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return a;
else
return gf_mul( a, gf_power( a, n-1 ) );
}
inline long gf_power ( long a, int n )
{
if ( n == 0 )
return 0;
else if ( n == 1 )
return a;
else
return gf_mul( a, gf_power( a, n-1 ) );
}
//}}}
void gf_setcharacteristic ( int p, int n, char name );
// Singular needs this
/*BEGINPUBLIC*/
long gf_gf2ff ( long a );
int gf_gf2ff ( int a );
bool gf_isff ( long a );
bool gf_isff ( int a );
/*ENDPUBLIC*/
#endif /* ! INCL_GFOPS_H */
canonicalform.h
Header for factory's main class CanonicalForm.
OSTREAM
#define OSTREAM
Definition canonicalform.h:16
i
int i
Definition cfEzgcd.cc:132
p
int p
Definition cfModGcd.cc:4086
b
CanonicalForm b
Definition cfModGcd.cc:4111
cf_assert.h
assertions for Factory
ASSERT
#define ASSERT(expression, message)
Definition cf_assert.h:99
cf_defs.h
factory switches.
CanonicalForm
factory's main class
Definition canonicalform.h:86
s
const CanonicalForm int s
Definition facAbsFact.cc:51
name
char name(const Variable &v)
Definition factory.h:189
gf_m1
VAR int gf_m1
Definition gfops.cc:51
gf_q
VAR int gf_q
Definition gfops.cc:47
gf_n
VAR int gf_n
Definition gfops.cc:49
gf_table
VAR unsigned short * gf_table
Definition gfops.cc:54
gf_name
VAR char gf_name
Definition gfops.cc:52
gf_p
VAR int gf_p
Definition gfops.cc:48
gf_mipo
INST_VAR CanonicalForm gf_mipo
Definition gfops.cc:56
gf_q1
VAR int gf_q1
Definition gfops.cc:50
gf_sub
int gf_sub(int a, int b)
Definition gfops.h:158
gf_int2gf
int gf_int2gf(int i)
Definition gfops.h:65
gf_setcharacteristic
void gf_setcharacteristic(int p, int n, char name)
Definition gfops.cc:202
gf_print
void gf_print(OSTREAM &os, int a)
Definition gfops.h:207
gf_neg
int gf_neg(int a)
Definition gfops.h:123
gf_isone
bool gf_isone(int a)
Definition gfops.h:53
gf_inv
int gf_inv(int a)
Definition gfops.h:198
gf_power
int gf_power(int a, int n)
Definition gfops.h:222
gf_zero
int gf_zero()
Definition gfops.h:99
gf_iszero
bool gf_iszero(int a)
Definition gfops.h:43
gf_one
int gf_one()
Definition gfops.h:104
gf_mul
int gf_mul(int a, int b)
Definition gfops.h:163
gf_div
int gf_div(int a, int b)
Definition gfops.h:185
gf_sign
int gf_sign(int a)
Definition gfops.h:113
gf_gf2ff
long gf_gf2ff(long a)
Definition gfops.cc:209
gf_isff
bool gf_isff(long a)
Definition gfops.cc:253
gf_add
int gf_add(int a, int b)
Definition gfops.h:133
EXTERN_INST_VAR
#define EXTERN_INST_VAR
Definition globaldefs.h:9
EXTERN_VAR
#define EXTERN_VAR
Definition globaldefs.h:6