11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

128 • Chapter 3: <strong>Programming</strong> with ModulesfalseFields produced by the quotient field constructor satisfy this interface.The Quotient Field Functorfields.Here is the generic constructor for quotient> QuotientField := proc( R::GcdRing )> description "quotient field functor";> module()> description "a quotient field";> export ‘+‘, ‘*‘, ‘-‘, ‘/‘,> zero, one,> iszero, isone,> make,> numer, denom,> normal, embed;> make := proc( n, d )> local u, nd;> if R:-iszero( d ) then> error "division by zero"> end if;> u, nd := R:-unormal( d );> ’FRACTION’( u*n, nd )> end proc;> embed := d -> make( d, R:-one );> numer := f -> op( 1, f );> denom := f -> op( 2, f );> zero := embed( R:-zero );> one := embed( R:-one );> iszero := f -> evalb( normal( f ) = zero );> isone := f -> evalb( normal( f ) = one );> normal := proc( f )> local g, a, b;> g := R:-gcd( numer( f ), denom( f ) );> if R:-isone( g ) then> f> else> a := R:-quo( numer( f ), g );> b := R:-quo( denom( f ), g );> make( a, b )> end if> end proc;> ‘-‘ := f -> normal( R:-‘-‘( numer( f ) ), denom( f ) );> ‘/‘ := f -> normal( make( denom( f ), numer( f ) ) );> ‘+‘ := proc( a, b )> use ‘+‘ = R:-‘+‘, ‘*‘ = R:-‘*‘ in> normal( make( numer( a ) * denom( b )> + denom( a ) * numer( b ),> denom( a ) * denom( b ) ) )> end use> end proc;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!