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.

2.5 Extending Maple • 33The following ‘&^‘ procedure uses I, J, and K as the three specialsymbols. However, I is implemented as the complex imaginary unit inMaple. Therefore, you should assign another letter to represent the imaginaryunit by using the interface function. For more information, seethe ?interface help page.> interface(imaginaryunit=j);You can multiply many types of expressions by using ‘&^‘, making itconvenient to define a new type, Hamiltonian, by assigning a structuredtype to the name ‘type/Hamiltonian‘.> ‘type/Hamiltonian‘ := { ‘+‘, ‘*‘, name, realcons,> specfunc(anything, ‘&^‘) };‘type/Hamiltonian‘ :={name, realcons, ‘ ∗ ‘, ‘ + ‘, specfunc(anything, ‘&^‘)}The ‘&^‘ procedure multiplies the two Hamiltonians, x and y. If eitherx or y is a real number or variable, then their product is the usual productdenoted by * in Maple. If x or y is a sum, ‘&^‘ maps the product onto thesum; that is, ‘&^‘ applies the distributive laws: x(u + v) = xu + xv and(u + v)x = ux + vx. If x or y is a product, ‘&^‘ extracts any real factors.You must take special care to avoid infinite recursion when x or y is aproduct that does not contain real factors. If none of the multiplicationrules apply, ‘&^‘ returns the product unevaluated.> ‘&^‘ := proc( x::Hamiltonian, y::Hamiltonian )> local Real, unReal, isReal;> isReal := z -> evalb( is(z, real) = true );>> if isReal(x) or isReal(y) then> x * y;>> elif type(x, ‘+‘) then> # x is a sum, u+v, so x&^y = u&^y + v&^y.> map(‘&^‘, x, y);>> elif type(y, ‘+‘) then> # y is a sum, u+v, so x&^y = x&^u + x&^v.> map2(‘&^‘, x, y);>> elif type(x, ‘*‘) then> # Pick out the real factors of x.> Real, unReal := selectremove(isReal, x);> # Now x&^y = Real * (unReal&^y)> if Real=1 then> if type(y, ‘*‘) then

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

Saved successfully!

Ooh no, something went wrong!