source: git/factory/cf_algorithm.cc @ e558c41

fieker-DuValspielwiese
Last change on this file since e558c41 was 27bb97f, checked in by Hans Schönemann <hannes@…>, 15 years ago
*hannes: debug stuff: out_cf git-svn-id: file:///usr/local/Singular/svn/trunk@12273 2c84dea3-7e68-4137-9b89-c4e89433aadc
  • Property mode set to 100644
File size: 12.5 KB
RevLine 
[483c5c]1/* emacs edit mode for this file is -*- C++ -*- */
[341696]2/* $Id$ */
[483c5c]3
4//{{{ docu
5//
6// cf_algorithm.cc - simple mathematical algorithms.
7//
[a27df3]8// Hierarchy: mathematical algorithms on canonical forms
[483c5c]9//
[a27df3]10// Developers note:
11// ----------------
12// A "mathematical" algorithm is an algorithm which calculates
13// some mathematical function in contrast to a "structural"
14// algorithm which gives structural information on polynomials.
[cbb3fa]15//
[a27df3]16// Compare these functions to the functions in `cf_ops.cc', which
17// are structural algorithms.
[483c5c]18//
19//}}}
20
21#include <config.h>
22
[cbb3fa]23#include "assert.h"
24
[a27df3]25#include "cf_factory.h"
[483c5c]26#include "cf_defs.h"
27#include "canonicalform.h"
[9719434]28#include "cf_algorithm.h"
[483c5c]29#include "variable.h"
30#include "cf_iter.h"
[a27df3]31#include "ftmpl_functions.h"
[483c5c]32
[27bb97f]33void out_cf(const char *s1,const CanonicalForm &f,const char *s2);
[ad82f6]34
[cbb3fa]35//{{{ CanonicalForm psr ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
36//{{{ docu
37//
[a27df3]38// psr() - return pseudo remainder of `f' and `g' with respect
[9719434]39//   to `x'.
[cbb3fa]40//
[a27df3]41// `g' must not equal zero.
[cbb3fa]42//
[a27df3]43// For f and g in R[x], R an arbitrary ring, g != 0, there is a
44// representation
[cbb3fa]45//
[a27df3]46//   LC(g)^s*f = g*q + r
[cbb3fa]47//
48// with r = 0 or deg(r) < deg(g) and s = 0 if f = 0 or
49// s = max( 0, deg(f)-deg(g)+1 ) otherwise.
[a27df3]50// r = psr(f, g) and q = psq(f, g) are called "pseudo remainder"
51// and "pseudo quotient", resp.  They are uniquely determined if
52// LC(g) is not a zero divisor in R.
53//
54// See H.-J. Reiffen/G. Scheja/U. Vetter - "Algebra", 2nd ed.,
55// par. 15, for a reference.
56//
57// Type info:
58// ----------
59// f, g: Current
60// x: Polynomial
61//
62// Polynomials over prime power domains are admissible if
63// lc(LC(`g',`x')) is not a zero divisor.  This is a slightly
64// stronger precondition than mathematically necessary since
65// pseudo remainder and quotient are well-defined if LC(`g',`x')
66// is not a zero divisor.
67//
68// For example, psr(y^2, (13*x+1)*y) is well-defined in
69// (Z/13^2[x])[y] since (13*x+1) is not a zero divisor.  But
70// calculating it with Factory would fail since 13 is a zero
71// divisor in Z/13^2.
72//
73// Due to this inconsistency with mathematical notion, we decided
74// not to declare type `CurrentPP' for `f' and `g'.
75//
76// Developers note:
77// ----------------
78// This is not an optimal implementation.  Better would have been
79// an implementation in `InternalPoly' avoiding the
80// exponentiation of the leading coefficient of `g'.  In contrast
81// to `psq()' and `psqr()' it definitely seems worth to implement
82// the pseudo remainder on the internal level.  Should be done
83// soon.
[cbb3fa]84//
85//}}}
[483c5c]86CanonicalForm
[7fb3ba4]87#if 0
[483c5c]88psr ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
89{
[6918d65]90
[a27df3]91    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
92    ASSERT( ! g.isZero(), "math error: division by zero" );
93
94    // swap variables such that x's level is larger or equal
95    // than both f's and g's levels.
96    Variable X = tmax( tmax( f.mvar(), g.mvar() ), x );
97    CanonicalForm F = swapvar( f, x, X );
98    CanonicalForm G = swapvar( g, x, X );
[cbb3fa]99
[a27df3]100    // now, we have to calculate the pseudo remainder of F and G
101    // w.r.t. X
102    int fDegree = degree( F, X );
103    int gDegree = degree( G, X );
[5453c7]104    if ( (fDegree < 0) || (fDegree < gDegree) )
[6918d65]105        return f;
[5453c7]106    else
107    {
[6918d65]108        CanonicalForm xresult = (power( LC( G, X ), fDegree-gDegree+1 ) * F) ;
109        CanonicalForm result = xresult -(xresult/G)*G;
110        return swapvar( result, x, X );
[a27df3]111    }
[483c5c]112}
[5bb462]113#else
[ad82f6]114psr ( const CanonicalForm &rr, const CanonicalForm &vv, const Variable & x )
115{
[5bb462]116  CanonicalForm r=rr, v=vv, l, test, lu, lv, t, retvalue;
117  int dr, dv, d,n=0;
118
119
120  dr = degree( r, x );
[ad82f6]121  if (dr>0)
[dad0bc5]122  {
[ad82f6]123    dv = degree( v, x );
124    if (dv <= dr) {l=LC(v,x); v = v -l*power(x,dv);}
125    else { l = 1; }
126    d= dr-dv+1;
[e7f5ac]127    //out_cf("psr(",rr," ");
128    //out_cf("",vv," ");
129    //printf(" var=%d\n",x.level());
[ad82f6]130    while ( ( dv <= dr  ) && ( !r.isZero()) )
131    {
132      test = power(x,dr-dv)*v*LC(r,x);
133      if ( dr == 0 ) { r= CanonicalForm(0); }
134      else { r= r - LC(r,x)*power(x,dr); }
135      r= l*r -test;
136      dr= degree(r,x);
137      n+=1;
138    }
139    r= power(l, d-n)*r;
[5bb462]140  }
141  return r;
142}
143#endif
[cbb3fa]144//}}}
[483c5c]145
[cbb3fa]146//{{{ CanonicalForm psq ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
147//{{{ docu
148//
[a27df3]149// psq() - return pseudo quotient of `f' and `g' with respect
[9719434]150//   to `x'.
[cbb3fa]151//
[a27df3]152// `g' must not equal zero.
153//
[9719434]154// See `psr()' for more detailed information.
[cbb3fa]155//
[a27df3]156// Type info:
157// ----------
158// f, g: Current
159// x: Polynomial
160//
161// Developers note:
162// ----------------
163// This is not an optimal implementation.  Better would have been
164// an implementation in `InternalPoly' avoiding the
165// exponentiation of the leading coefficient of `g'.  It seemed
166// not worth to do so.
167//
[cbb3fa]168//}}}
[483c5c]169CanonicalForm
170psq ( const CanonicalForm & f, const CanonicalForm & g, const Variable & x )
171{
[a27df3]172    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
173    ASSERT( ! g.isZero(), "math error: division by zero" );
[cbb3fa]174
[a27df3]175    // swap variables such that x's level is larger or equal
176    // than both f's and g's levels.
177    Variable X = tmax( tmax( f.mvar(), g.mvar() ), x );
178    CanonicalForm F = swapvar( f, x, X );
179    CanonicalForm G = swapvar( g, x, X );
180
181    // now, we have to calculate the pseudo remainder of F and G
182    // w.r.t. X
183    int fDegree = degree( F, X );
184    int gDegree = degree( G, X );
185    if ( fDegree < 0 || fDegree < gDegree )
[6918d65]186        return 0;
[a27df3]187    else {
[6918d65]188        CanonicalForm result = (power( LC( G, X ), fDegree-gDegree+1 ) * F) / G;
189        return swapvar( result, x, X );
[a27df3]190    }
[483c5c]191}
[cbb3fa]192//}}}
[483c5c]193
[cbb3fa]194//{{{ void psqr ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r, const Variable & x )
195//{{{ docu
196//
[9719434]197// psqr() - calculate pseudo quotient and remainder of `f' and
198//   `g' with respect to `x'.
[cbb3fa]199//
[a27df3]200// Returns the pseudo quotient of `f' and `g' in `q', the pseudo
201// remainder in `r'.  `g' must not equal zero.
202//
[9719434]203// See `psr()' for more detailed information.
[cbb3fa]204//
[a27df3]205// Type info:
206// ----------
207// f, g: Current
208// q, r: Anything
209// x: Polynomial
210//
211// Developers note:
212// ----------------
213// This is not an optimal implementation.  Better would have been
214// an implementation in `InternalPoly' avoiding the
215// exponentiation of the leading coefficient of `g'.  It seemed
216// not worth to do so.
217//
[cbb3fa]218//}}}
[483c5c]219void
220psqr ( const CanonicalForm & f, const CanonicalForm & g, CanonicalForm & q, CanonicalForm & r, const Variable& x )
221{
[a27df3]222    ASSERT( x.level() > 0, "type error: polynomial variable expected" );
223    ASSERT( ! g.isZero(), "math error: division by zero" );
224
225    // swap variables such that x's level is larger or equal
226    // than both f's and g's levels.
227    Variable X = tmax( tmax( f.mvar(), g.mvar() ), x );
228    CanonicalForm F = swapvar( f, x, X );
229    CanonicalForm G = swapvar( g, x, X );
[cbb3fa]230
[a27df3]231    // now, we have to calculate the pseudo remainder of F and G
232    // w.r.t. X
233    int fDegree = degree( F, X );
234    int gDegree = degree( G, X );
235    if ( fDegree < 0 || fDegree < gDegree ) {
[6918d65]236        q = 0; r = f;
[a27df3]237    } else {
[6918d65]238        divrem( power( LC( G, X ), fDegree-gDegree+1 ) * F, G, q, r );
239        q = swapvar( q, x, X );
240        r = swapvar( r, x, X );
[cbb3fa]241    }
[483c5c]242}
[cbb3fa]243//}}}
[483c5c]244
[9719434]245//{{{ static CanonicalForm internalBCommonDen ( const CanonicalForm & f )
[cbb3fa]246//{{{ docu
247//
[9719434]248// internalBCommonDen() - recursively calculate multivariate
249//   common denominator of coefficients of `f'.
[cbb3fa]250//
[9719434]251// Used by: bCommonDen()
[cbb3fa]252//
[a27df3]253// Type info:
254// ----------
255// f: Poly( Q )
256// Switches: isOff( SW_RATIONAL )
257//
[cbb3fa]258//}}}
[483c5c]259static CanonicalForm
[9719434]260internalBCommonDen ( const CanonicalForm & f )
[483c5c]261{
[cbb3fa]262    if ( f.inBaseDomain() )
[6918d65]263        return f.den();
[483c5c]264    else {
[6918d65]265        CanonicalForm result = 1;
266        for ( CFIterator i = f; i.hasTerms(); i++ )
267            result = blcm( result, internalBCommonDen( i.coeff() ) );
268        return result;
[483c5c]269    }
270}
[cbb3fa]271//}}}
[483c5c]272
[9719434]273//{{{ CanonicalForm bCommonDen ( const CanonicalForm & f )
[cbb3fa]274//{{{ docu
275//
[9719434]276// bCommonDen() - calculate multivariate common denominator of
277//   coefficients of `f'.
[cbb3fa]278//
279// The common denominator is calculated with respect to all
[9719434]280// coefficients of `f' which are in a base domain.  In other
281// words, common_den( `f' ) * `f' is guaranteed to have integer
[a27df3]282// coefficients only.  The common denominator of zero is one.
[cbb3fa]283//
[9719434]284// Returns something non-trivial iff the current domain is Q.
[cbb3fa]285//
[a27df3]286// Type info:
287// ----------
288// f: CurrentPP
289//
[cbb3fa]290//}}}
[483c5c]291CanonicalForm
[9719434]292bCommonDen ( const CanonicalForm & f )
[483c5c]293{
294    if ( getCharacteristic() == 0 && isOn( SW_RATIONAL ) ) {
[6918d65]295        // otherwise `bgcd()' returns one
296        Off( SW_RATIONAL );
297        CanonicalForm result = internalBCommonDen( f );
298        On( SW_RATIONAL );
299        return result;
[9719434]300    } else
[6918d65]301        return CanonicalForm( 1 );
[483c5c]302}
[cbb3fa]303//}}}
[dff969]304
[ebc602]305//{{{ bool fdivides ( const CanonicalForm & f, const CanonicalForm & g )
[dff969]306//{{{ docu
307//
[ebc602]308// fdivides() - check whether `f' divides `g'.
[dff969]309//
[a27df3]310// Returns true iff `f' divides `g'.  Uses some extra heuristic
311// to avoid polynomial division.  Without the heuristic, the test
312// essentialy looks like `divremt(g, f, q, r) && r.isZero()'.
313//
314// Type info:
315// ----------
316// f, g: Current
317//
318// Elements from prime power domains (or polynomials over such
319// domains) are admissible if `f' (or lc(`f'), resp.) is not a
320// zero divisor.  This is a slightly stronger precondition than
321// mathematically necessary since divisibility is a well-defined
322// notion in arbitrary rings.  Hence, we decided not to declare
323// the weaker type `CurrentPP'.
324//
325// Developers note:
326// ----------------
[ebc602]327// One may consider the the test `fdivides( f.LC(), g.LC() )' in
[a27df3]328// the main `if'-test superfluous since `divremt()' in the
329// `if'-body repeats the test.  However, `divremt()' does not use
330// any heuristic to do so.
331//
[ebc602]332// It seems not reasonable to call `fdivides()' from `divremt()'
333// to check divisibility of leading coefficients.  `fdivides()' is
[a27df3]334// on a relatively high level compared to `divremt()'.
[dff969]335//
336//}}}
337bool
[ebc602]338fdivides ( const CanonicalForm & f, const CanonicalForm & g )
[dff969]339{
[a27df3]340    // trivial cases
341    if ( g.isZero() )
[6918d65]342        return true;
[a27df3]343    else if ( f.isZero() )
[6918d65]344        return false;
[a27df3]345
346    if ( (f.inCoeffDomain() || g.inCoeffDomain())
[6918d65]347         && ((getCharacteristic() == 0 && isOn( SW_RATIONAL ))
348             || (getCharacteristic() > 0 && CFFactory::gettype() != PrimePowerDomain)) )
349        // if we are in a field all elements not equal to zero are units
350        if ( f.inCoeffDomain() )
351            return true;
352        else
353            // g.inCoeffDomain()
354            return false;
[a27df3]355
356    // we may assume now that both levels either equal LEVELBASE
357    // or are greater zero
358    int fLevel = f.level();
359    int gLevel = g.level();
[94a967]360    if ( (gLevel > 0) && (fLevel == gLevel) )
[6918d65]361        // f and g are polynomials in the same main variable
362        if ( degree( f ) <= degree( g )
363             && fdivides( f.tailcoeff(), g.tailcoeff() )
[94a967]364             && fdivides( f.LC(), g.LC() ) )
365        {
[6918d65]366            CanonicalForm q, r;
367            return divremt( g, f, q, r ) && r.isZero();
368        }
369        else
370            return false;
[a27df3]371    else if ( gLevel < fLevel )
[6918d65]372        // g is a coefficient w.r.t. f
373        return false;
[94a967]374    else
375    {
[6918d65]376        // either f is a coefficient w.r.t. polynomial g or both
377        // f and g are from a base domain (should be Z or Z/p^n,
378        // then)
379        CanonicalForm q, r;
380        return divremt( g, f, q, r ) && r.isZero();
[9719434]381    }
382}
383//}}}
384
385//{{{ CanonicalForm maxNorm ( const CanonicalForm & f )
386//{{{ docu
387//
[a27df3]388// maxNorm() - return maximum norm of `f'.
[9719434]389//
390// That is, the base coefficient of `f' with the largest absolute
391// value.
392//
393// Valid for arbitrary polynomials over arbitrary domains, but
394// most useful for multivariate polynomials over Z.
395//
[a27df3]396// Type info:
397// ----------
398// f: CurrentPP
399//
[9719434]400//}}}
401CanonicalForm
402maxNorm ( const CanonicalForm & f )
403{
404    if ( f.inBaseDomain() )
[6918d65]405        return abs( f );
[9719434]406    else {
[6918d65]407        CanonicalForm result = 0;
408        for ( CFIterator i = f; i.hasTerms(); i++ ) {
409            CanonicalForm coeffMaxNorm = maxNorm( i.coeff() );
410            if ( coeffMaxNorm > result )
411                result = coeffMaxNorm;
412        }
413        return result;
[9719434]414    }
415}
416//}}}
417
418//{{{ CanonicalForm euclideanNorm ( const CanonicalForm & f )
419//{{{ docu
420//
[a27df3]421// euclideanNorm() - return Euclidean norm of `f'.
422//
423// Returns the largest integer smaller or equal norm(`f') =
424// sqrt(sum( `f'[i]^2 )).
[9719434]425//
[a27df3]426// Type info:
427// ----------
428// f: UVPoly( Z )
[9719434]429//
430//}}}
431CanonicalForm
432euclideanNorm ( const CanonicalForm & f )
433{
434    ASSERT( (f.inBaseDomain() || f.isUnivariate()) && f.LC().inZ(),
[6918d65]435            "type error: univariate poly over Z expected" );
[9719434]436
437    CanonicalForm result = 0;
438    for ( CFIterator i = f; i.hasTerms(); i++ ) {
[6918d65]439        CanonicalForm coeff = i.coeff();
440        result += coeff*coeff;
[dff969]441    }
[9719434]442    return sqrt( result );
[dff969]443}
[5cf721]444//}}}
Note: See TracBrowser for help on using the repository browser.