Previous Page Next Page Contents

misc::maprec -- map a function to subexpressions of an expression

Introduction

misc::maprec(ex, selector=funci) maps the function funci to all subexpressions of the expression ex that satisfy a given criterion (defined by selector) and replaces each selected subexpression s by funci(s).

Several different functions may be mapped to subexpressions satisfying different selection criteria.

Call(s)

misc::maprec(ex, selector=funci)
misc::maprec(ex, selector=funci, PreMap)
misc::maprec(ex, selector=funci, PostMap)
misc::maprec(ex, selector=funci, PreMap, PostMap)
misc::maprec(ex <, selector=funci... > <, PreMap> <, PostMap>)

Parameters

ex - any MuPAD object
selector - any MuPAD object
funci - any MuPAD object

Options

PreMap - For each subexpressions s of ex, the selector is applied to it after visiting all of its subexpressions; s may have changed at that time due to substitutions in the subexpressions.
PostMap - For each subexpressions s of ex, the selector is applied to it before visiting its subexpressions. If s is selected by selector, it is replaced by funci(s), and misc::maprec is not recursively applied to the operands of funci(s); otherwise, misc::maprec is recursively applied to the operands of s.

Returns

misc::maprec may return any MuPAD object.

Related Functions

map, mapcoeffs, misc::breakmap

Details

Overloadable:

ex

Example 1

In the following example every integer of the given expression a+3+4 is substituted by the value 10. Since 10(n) returns 10 for every integer n, it suffices to write 10 instead of n -> 10 here.

>> misc::maprec(hold(a+3+4), {DOM_INT} = 10)
                                  a + 20

In the example above, we used hold to suppress the evaluation of the expression because otherwise a+3+4 is evaluated to a+7 and we get the result:

>> misc::maprec(a+3+4, {DOM_INT} = 10)
                                  a + 10

Example 2

Now we demonstrate the usage of the options PreMap and PostMap.

>> misc::maprec(hold(3+4), {DOM_INT} = 10)
                                    10

Here misc::maprec was used without an option, this means the default option PreMap was used. So why did we get a result of 10 instead of the (possibly expected) 20? Because pre-mapping is used by default, misc::maprec first applies itself to the integers 3 and 4. They are replaced by the value 10 each such that we get an intermediate result of 20. After that, misc::maprec tests the selection criterion for the expression as a whole. This one equals the integer 20 by now, hence it is replaced by 10. Instead, when using the PostMap option we get:

>> misc::maprec(hold(3+4), {DOM_INT} = 10, PostMap )
                                    20

Here, the expression 3+4 was tested at first -- it is not an integer. Then, misc::maprec was applied to the operands, and both were replaced by 10.

Example 3

Now we give an example where the selector is a function. We want to eleminate all the prime numbers from an expression.

>> misc::maprec(hold(_plus)( i $ i=1..20), isprime= null(), PostMap)
                                    133

Here isprime returns TRUE for every prime number between 1 and 20. Every prime number between 1 and 20 is replaced by null() (since null()(p) gives null()) which means the call above computes the sum of all composite numbers between 1 and 20..

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000