11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

66 • Chapter 3: <strong>Programming</strong> with Modules> if assigned( ruletab[ b ] ) then> ruletab[ b ]( expr, var )> else> ’procname( args )’> end if> end if> end proc;>> addRule( ’{list,set,tabular}’,> () -> map( differentiate, args ) );> addRule( ’‘+‘’,> () -> map( differentiate, args ) );> addRule( ’‘*‘’,> (expr,var) ->> op(1,expr)*differentiate(subsop(1=1,expr),var)> + differentiate(op(1,expr),var)*subsop(1=1,expr) );> diffPower := proc( expr, var )> local b, e;> Assert( type( expr, ’‘^‘’ ) );> b, e := op( expr );> if has( e, var ) then> expr * ( differentiate( e, var ) * ln( b )> + e * differentiate( b, var ) / b )> else # simpler formula> e * b^(e - 1) * differentiate( b, var )> end if;> end proc;> addRule( ’‘^‘’, eval( diffPower ) );>> addFunc( ’sin’, cos );> addFunc( ’cos’, x -> -sin(x) );> addFunc( ’exp’, exp );> addFunc( ’ln’, x -> 1/x );> # ... etc.>> end module:> differentiate := DiffImpl:-differentiate:To give the set of rules for nonfunctional expressions similar extensibility,you can store those rules in a table. The table is indexed by the primary (orbasic) type name for the expression type, as given by the Maple procedurewhattype.> whattype( a + 2 );‘ + ‘> whattype( a / b );‘ ∗ ‘

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

Saved successfully!

Ooh no, something went wrong!