Loading...
Searching...
No Matches
variable.cc
Go to the documentation of this file.
/* emacs edit mode for this file is -*- C++ -*- */
#include "config.h"
#include <string.h>
#include "cf_assert.h"
#include "cf_defs.h"
#include "variable.h"
#include "canonicalform.h"
#include "cf_factory.h"
#include "int_poly.h"
#include "cf_iter.h"
class ext_entry
{
private:
InternalPoly * _mipo;
bool _reduce;
public:
ext_entry () : _mipo(0), _reduce(false) {}
ext_entry ( InternalPoly * mipoly, bool reduce ) : _mipo(mipoly), _reduce(reduce) {};
ext_entry ( const ext_entry & e ) : _mipo(e._mipo), _reduce(e._reduce) {}
~ext_entry () {};
ext_entry & operator= ( const ext_entry & e )
{
if ( this != &e ) {
_mipo = e._mipo;
_reduce = e._reduce;
}
return *this;
}
InternalPoly * mipo () { return _mipo; }
void setmipo( InternalPoly * p ) { _mipo = p; }
bool & reduce () { return _reduce; }
};
STATIC_VAR ext_entry * algextensions = 0;
STATIC_VAR char * var_names = 0;
STATIC_VAR char * var_names_ext = 0;
STATIC_VAR char default_name = 'v';
STATIC_VAR char default_name_ext = 'a';
Variable::Variable( int l, bool flag ) : _level(l)
{
ASSERT( flag, "illegal level" );
}
Variable::Variable( int l ) : _level(l)
{
//ASSERT( l > 0 && l != LEVELQUOT, "illegal level" );
}
Variable::Variable( char name )
{
bool isext = false;
int n, i;
if ( var_names_ext != 0 ) {
n = strlen( var_names_ext );
i = 1;
while ( i < n && var_names_ext[i] != name ) i++;
if ( i < n ) {
_level = -i;
isext = true;
}
}
if ( ! isext ) {
if ( var_names == 0 ) {
var_names = new char [3];
var_names[0] = '@';
var_names[1] = name;
var_names[2] = '\0';
_level = 1;
}
else {
n = strlen( var_names );
i = 1;
while ( i < n && var_names[i] != name ) i++;
if ( i < n )
_level = i;
else {
ASSERT( name != '@', "illegal variable name" );
char * newvarnames = new char [n+2];
for ( i = 0; i < n; i++ )
newvarnames[i] = var_names[i];
newvarnames[n] = name;
newvarnames[n+1] = 0;
delete [] var_names;
var_names = newvarnames;
_level = n;
}
}
}
}
Variable::Variable( int l, char name ) : _level(l)
{
ASSERT( l > 0 && l != LEVELQUOT, "illegal level" );
int n;
if ( (n = (var_names == 0 ? 0 : strlen( var_names ))) <= l ) {
char * newvarnames = new char [l+2];
int i;
for ( i = 0; i < n; i++ )
newvarnames[i] = var_names[i];
for ( i = n; i < l; i++ )
newvarnames[i] = '@';
newvarnames[l] = name;
newvarnames[l+1] = 0;
delete [] var_names;
var_names = newvarnames;
}
else {
ASSERT( var_names[l] == '@', "illegal name" );
var_names[l] = name;
}
}
char
Variable::name() const
{
if ( _level > 0 && _level < (int)strlen( var_names ) )
return( var_names[_level] );
else if ( _level < 0 && -_level < (int)strlen( var_names_ext ) )
return( var_names_ext[-_level] );
else
return '@';
}
#ifndef NOSTREAMIO
OSTREAM & operator << ( OSTREAM & os, const Variable & v )
{
if ( v._level == LEVELBASE )
os << "1";
else {
char * vn = ( v._level > 0 ) ? var_names : var_names_ext;
char dn = ( v._level > 0 ) ? default_name : default_name_ext;
int l = v._level;
if ( l < 0 ) l = -l;
if ( (vn == 0) || ((int)strlen( vn ) <= l) )
os << dn << "_" << l;
else if ( vn[l] == '@' )
os << dn << "_" << l;
else
os << vn[l];
}
return os;
}
#endif /* NOSTREAMIO */
static CanonicalForm conv2mipo ( const CanonicalForm & mipo, const Variable & alpha )
{
CanonicalForm result;
for ( CFIterator i = mipo; i.hasTerms(); i++ )
result += i.coeff() * power( alpha, i.exp() );
return result;
}
Variable rootOf( const CanonicalForm & mipo, char name )
{
ASSERT (mipo.isUnivariate(), "not a legal extension");
int l;
if ( var_names_ext == 0 ) {
var_names_ext = new char [3];
var_names_ext[0] = '@';
var_names_ext[1] = name;
var_names_ext[2] = '\0';
l = 1;
Variable result( -l, true );
algextensions = new ext_entry [2];
algextensions[1] = ext_entry( 0, false );
algextensions[1] = ext_entry( (InternalPoly*)(conv2mipo( mipo, result ).getval()), true );
return result;
}
else {
int i, n = strlen( var_names_ext );
char * newvarnames = new char [n+2];
for ( i = 0; i < n; i++ )
newvarnames[i] = var_names_ext[i];
newvarnames[n] = name;
newvarnames[n+1] = 0;
delete [] var_names_ext;
var_names_ext = newvarnames;
l = n;
Variable result( -l, true );
ext_entry * newalgext = new ext_entry [n+1];
for ( i = 0; i < n; i++ )
newalgext[i] = algextensions[i];
newalgext[n] = ext_entry( 0, false );
delete [] algextensions;
algextensions = newalgext;
algextensions[n] = ext_entry( (InternalPoly*)(conv2mipo( mipo, result ).getval()), true );
return result;
}
}
InternalPoly * getInternalMipo ( const Variable & alpha )
{
ASSERT( alpha.level() < 0 && alpha.level() != LEVELBASE, "illegal extension" );
return algextensions[-alpha.level()].mipo();
}
CanonicalForm getMipo( const Variable & alpha, const Variable & x )
{
ASSERT( alpha.level() < 0 && alpha.level() != LEVELBASE, "illegal extension" );
return CanonicalForm( algextensions[-alpha.level()].mipo()->copyObject() )(x,alpha);
}
CanonicalForm getMipo( const Variable & alpha )
{
ASSERT( alpha.level() < 0 && alpha.level() != LEVELBASE, "illegal extension" );
return CanonicalForm( algextensions[-alpha.level()].mipo()->copyObject() );
}
void setMipo ( const Variable & alpha, const CanonicalForm & mipo)
{
ASSERT( alpha.level() < 0 && alpha.level() != LEVELBASE, "illegal extension" );
algextensions[-alpha.level()]= ext_entry( 0, false );
algextensions[-alpha.level()]= ext_entry((InternalPoly*)(conv2mipo( mipo, alpha ).getval()), true );
}
bool hasMipo( const Variable & alpha )
{
ASSERT( alpha.level() < 0, "illegal extension" );
return (alpha.level() != LEVELBASE && (algextensions!=NULL) && getReduce(alpha) );
}
bool getReduce( const Variable & alpha )
{
ASSERT( alpha.level() < 0 && alpha.level() != LEVELBASE, "illegal extension" );
return algextensions[-alpha.level()].reduce();
}
void setReduce( const Variable & alpha, bool reduce )
{
ASSERT( alpha.level() < 0 && alpha.level() != LEVELBASE, "illegal extension" );
algextensions[-alpha.level()].reduce() = reduce;
}
char getDefaultVarName()
{
return default_name;
}
char getDefaultExtName()
{
return default_name_ext;
}
int ExtensionLevel()
{
if( var_names_ext == 0)
return 0;
return strlen( var_names_ext )-1;
}
void prune (Variable& alpha)
{
if (alpha.level()==LEVELBASE) return;
int last_var=-alpha.level();
if ((last_var <= 0)||(var_names_ext==NULL)) return;
int i, n = strlen( var_names_ext );
ASSERT (n+1 >= last_var, "wrong variable");
if (last_var == 1)
{
delete [] var_names_ext;
delete [] algextensions;
var_names_ext= 0;
algextensions= 0;
alpha= Variable();
return;
}
char * newvarnames = new char [last_var+1];
for ( i = 0; i < last_var; i++ )
newvarnames[i] = var_names_ext[i];
newvarnames[last_var] = 0;
delete [] var_names_ext;
var_names_ext = newvarnames;
ext_entry * newalgext = new ext_entry [last_var];
for ( i = 0; i < last_var; i++ )
newalgext[i] = algextensions[i];
delete [] algextensions;
algextensions = newalgext;
alpha= Variable();
}
void prune1 (const Variable& alpha)
{
int i, n = strlen( var_names_ext );
ASSERT (n+1 >= -alpha.level(), "wrong variable");
char * newvarnames = new char [-alpha.level() + 2];
for ( i = 0; i <= -alpha.level(); i++ )
newvarnames[i] = var_names_ext[i];
newvarnames[-alpha.level()+1] = 0;
delete [] var_names_ext;
var_names_ext = newvarnames;
ext_entry * newalgext = new ext_entry [-alpha.level()+1];
for ( i = 0; i <= -alpha.level(); i++ )
newalgext[i] = algextensions[i];
delete [] algextensions;
algextensions = newalgext;
}
void Reduce( bool on)
{
int i;
for (i=ExtensionLevel(); i>0;i--)
{
Variable l(-i);
setReduce(l,on);
}
}
power
CanonicalForm power(const CanonicalForm &f, int n)
exponentiation
Definition canonicalform.cc:1896
canonicalform.h
Header for factory's main class CanonicalForm.
reduce
CanonicalForm reduce(const CanonicalForm &f, const CanonicalForm &M)
polynomials in M.mvar() are considered coefficients M univariate monic polynomial the coefficients of...
Definition cf_ops.cc:660
l
int l
Definition cfEzgcd.cc:100
i
int i
Definition cfEzgcd.cc:132
x
Variable x
Definition cfModGcd.cc:4090
p
int p
Definition cfModGcd.cc:4086
false
return false
Definition cfModGcd.cc:85
cf_assert.h
assertions for Factory
ASSERT
#define ASSERT(expression, message)
Definition cf_assert.h:99
cf_defs.h
factory switches.
LEVELBASE
#define LEVELBASE
Definition cf_defs.h:25
LEVELQUOT
#define LEVELQUOT
Definition cf_defs.h:27
cf_factory.h
Interface to generate InternalCF's over various domains from intrinsic types or mpz_t's.
cf_iter.h
Iterators for CanonicalForm's.
CFIterator
class to iterate through CanonicalForm's
Definition cf_iter.h:44
CanonicalForm
factory's main class
Definition canonicalform.h:86
InternalPoly
factory's class for polynomials
Definition int_poly.h:71
Variable
factory's class for variables
Definition factory.h:127
Variable::Variable
Variable(int l, bool flag)
Definition variable.cc:47
Variable::Variable
Variable()
Definition factory.h:132
Variable::name
char name() const
Definition variable.cc:122
Variable::_level
int _level
Definition factory.h:129
ext_entry
Definition variable.cc:19
ext_entry::~ext_entry
~ext_entry()
Definition variable.cc:27
ext_entry::setmipo
void setmipo(InternalPoly *p)
Definition variable.cc:37
ext_entry::ext_entry
ext_entry(InternalPoly *mipoly, bool reduce)
Definition variable.cc:25
ext_entry::ext_entry
ext_entry()
Definition variable.cc:24
ext_entry::mipo
InternalPoly * mipo()
Definition variable.cc:36
ext_entry::operator=
ext_entry & operator=(const ext_entry &e)
Definition variable.cc:28
ext_entry::reduce
bool & reduce()
Definition variable.cc:38
ext_entry::_reduce
bool _reduce
Definition variable.cc:22
ext_entry::ext_entry
ext_entry(const ext_entry &e)
Definition variable.cc:26
ext_entry::_mipo
InternalPoly * _mipo
Definition variable.cc:21
alpha
Variable alpha
Definition facAbsBiFact.cc:52
result
return result
Definition facAbsBiFact.cc:76
mipo
CanonicalForm mipo
Definition facAlgExt.cc:57
v
const Variable & v
< [in] a sqrfree bivariate poly
Definition facBivar.h:39
name
char name(const Variable &v)
Definition factory.h:189
STATIC_VAR
#define STATIC_VAR
Definition globaldefs.h:7
int_poly.h
Factory's internal polynomials.
NULL
#define NULL
Definition omList.c:12
setReduce
void setReduce(const Variable &alpha, bool reduce)
Definition variable.cc:238
conv2mipo
static CanonicalForm conv2mipo(const CanonicalForm &mipo, const Variable &alpha)
Definition variable.cc:154
getReduce
bool getReduce(const Variable &alpha)
Definition variable.cc:232
rootOf
Variable rootOf(const CanonicalForm &mipo, char name)
returns a symbolic root of polynomial with name name Use it to define algebraic variables
Definition variable.cc:162
getDefaultVarName
char getDefaultVarName()
Definition variable.cc:244
prune1
void prune1(const Variable &alpha)
Definition variable.cc:291
getInternalMipo
InternalPoly * getInternalMipo(const Variable &alpha)
Definition variable.cc:201
algextensions
STATIC_VAR ext_entry * algextensions
Definition variable.cc:41
prune
void prune(Variable &alpha)
Definition variable.cc:261
ExtensionLevel
int ExtensionLevel()
Definition variable.cc:254
getMipo
CanonicalForm getMipo(const Variable &alpha, const Variable &x)
Definition variable.cc:207
var_names_ext
STATIC_VAR char * var_names_ext
Definition variable.cc:43
operator<<
OSTREAM & operator<<(OSTREAM &os, const Variable &v)
Definition variable.cc:133
default_name_ext
STATIC_VAR char default_name_ext
Definition variable.cc:45
Reduce
void Reduce(bool on)
Definition variable.cc:309
default_name
STATIC_VAR char default_name
Definition variable.cc:44
setMipo
void setMipo(const Variable &alpha, const CanonicalForm &mipo)
Definition variable.cc:219
var_names
STATIC_VAR char * var_names
Definition variable.cc:42
getDefaultExtName
char getDefaultExtName()
Definition variable.cc:249
hasMipo
bool hasMipo(const Variable &alpha)
Definition variable.cc:226
variable.h
operations on variables
OSTREAM
#define OSTREAM
Definition variable.h:16