[fa8420] | 1 | /* emacs edit mode for this file is -*- C++ -*- */ |
---|
[341696] | 2 | /* $Id$ */ |
---|
[fa8420] | 3 | |
---|
| 4 | ftestSetNameOfGame( divrem, `" |
---|
| 5 | Usage: divrem [<options>] [<envSpec>] <f> <g> [<divisionCheck>] |
---|
[9110891] | 6 | calculates quotient q and remainder r of division from |
---|
[fa8420] | 7 | canonical form <f> by canonical form <g> such that f=g*q+r. |
---|
[9110891] | 8 | <g> should not equal zero. The result is not defined if such q |
---|
| 9 | and r do not exist. |
---|
| 10 | If the optional integer <divisionCheck> is specified and non- |
---|
| 11 | zero additional checks are done whether q and r exist. The |
---|
[fa8420] | 12 | result of the check is printed as a boolean. |
---|
[9110891] | 13 | The results are printed in the order q - r [ - checkResult ]. |
---|
[fa8420] | 14 | "'`' ) |
---|
| 15 | |
---|
| 16 | //{{{ docu |
---|
| 17 | // |
---|
| 18 | // ftestAlgorithm.m4 - ftestAlgorithm test program. |
---|
| 19 | // |
---|
| 20 | // To create ftestAlgorithm.cc, run m4 using the ftest_util.m4 library in |
---|
| 21 | // the following way: |
---|
| 22 | // |
---|
| 23 | // m4 ftest_util.m4 ftestAlgorithm.m4 > ftestAlgorithm.cc |
---|
| 24 | // |
---|
| 25 | //}}} |
---|
| 26 | |
---|
| 27 | ftestPreprocInit(); |
---|
| 28 | |
---|
| 29 | ftestGlobalInit(); |
---|
| 30 | |
---|
| 31 | // |
---|
| 32 | // - functions. |
---|
| 33 | // |
---|
| 34 | |
---|
| 35 | //{{{ static ftestStatusT divremCheck ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & q, const CanonicalForm & r, bool checkResult ) |
---|
| 36 | //{{{ docu |
---|
| 37 | // |
---|
| 38 | // divremCheck() - check result of divrem(). |
---|
| 39 | // |
---|
| 40 | //}}} |
---|
| 41 | static ftestStatusT |
---|
| 42 | divremCheck ( const CanonicalForm & f, const CanonicalForm & g, const CanonicalForm & q, const CanonicalForm & r, bool checkResult ) |
---|
| 43 | { |
---|
| 44 | if ( checkResult ) |
---|
| 45 | if ( q != f / g ) { |
---|
| 46 | ftestError( CheckError, "q != f/g\n" ); |
---|
| 47 | return Failed; |
---|
| 48 | } else if ( r != f % g ) { |
---|
[67a182] | 49 | ftestError( CheckError, "r != f%%g\n" ); |
---|
[fa8420] | 50 | return Failed; |
---|
| 51 | } else if ( f.inBaseDomain() && g.inBaseDomain() && getCharacteristic() == 0 |
---|
| 52 | && (r < 0 || r > abs( g )) ) { |
---|
| 53 | // check euclidean division in Z |
---|
[67a182] | 54 | ftestError( CheckError, "!(0 <= f%%g < abs(g))\n" ); |
---|
[fa8420] | 55 | return Failed; |
---|
| 56 | } else if ( f != g*q+r ) { |
---|
| 57 | ftestError( CheckError, "f != g*q+r\n" ); |
---|
| 58 | return Failed; |
---|
| 59 | } else if ( f.inPolyDomain() || g.inPolyDomain() ) { |
---|
| 60 | // check euclidean division in R[x] |
---|
| 61 | Variable x = (mvar( f ) >= mvar( g )) ? mvar( f ) : mvar( g ); |
---|
| 62 | if ( r.isZero() || degree( r, x ) < degree( g, x ) ) |
---|
| 63 | return Passed; |
---|
| 64 | else { |
---|
| 65 | ftestError( CheckError, "degree(r) >= degree(g)\n" ); |
---|
| 66 | return Failed; |
---|
| 67 | } |
---|
| 68 | } else |
---|
| 69 | return Passed; |
---|
| 70 | else |
---|
| 71 | return UndefinedResult; |
---|
| 72 | } |
---|
| 73 | //}}} |
---|
| 74 | |
---|
| 75 | // |
---|
| 76 | // - main program. |
---|
| 77 | // |
---|
| 78 | int |
---|
| 79 | main ( int argc, char ** argv ) |
---|
| 80 | { |
---|
| 81 | // initialization |
---|
| 82 | ftestMainInit(); |
---|
| 83 | |
---|
| 84 | // declare input and output variables |
---|
| 85 | ftestOutVar( CanonicalForm, q ); |
---|
| 86 | ftestOutVar( CanonicalForm, r ); |
---|
| 87 | ftestOutVar( bool, checkResult ); |
---|
| 88 | ftestInVar( CanonicalForm, f ); |
---|
| 89 | ftestInVar( CanonicalForm, g ); |
---|
| 90 | ftestInVar( int, divisionCheck ); |
---|
| 91 | |
---|
| 92 | // process argument list and set environment |
---|
| 93 | ftestGetOpts(); |
---|
| 94 | ftestGetEnv(); |
---|
| 95 | ftestGetInVar( f ); |
---|
| 96 | ftestGetInVar( g ); |
---|
| 97 | ftestGetInVar( divisionCheck, 0 ); |
---|
| 98 | |
---|
| 99 | // do the test! |
---|
| 100 | if ( divisionCheck ) { |
---|
| 101 | ftestRun( checkResult = divremt( f, g, q, r ); ); |
---|
| 102 | } else { |
---|
| 103 | checkResult = true; |
---|
| 104 | ftestRun( divrem( f, g, q, r ); ); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | // do the check |
---|
[9110891] | 108 | ftestCheck( divremCheck( f, g, q, r, checkResult ); ); |
---|
[fa8420] | 109 | |
---|
| 110 | // print results |
---|
| 111 | if ( divisionCheck ) { |
---|
| 112 | ftestOutput( "q", q, "r", r, "divremt(f, g, q, r)", checkResult ); |
---|
| 113 | } else { |
---|
| 114 | ftestOutput( "q", q, "r", r ); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | // clean up |
---|
| 118 | ftestMainExit(); |
---|
| 119 | } |
---|