MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Use of Mathematica with Rule-based Equation Derivations

  • To: mathgroup at
  • Subject: [mg61935] Re: [mg61914] Use of Mathematica with Rule-based Equation Derivations
  • From: Andrzej Kozlowski <akoz at>
  • Date: Sat, 5 Nov 2005 01:52:18 -0500 (EST)
  • References: <>
  • Sender: owner-wri-mathgroup at

On 4 Nov 2005, at 19:11, mmorriss at wrote:

> Hi All - I'm a many-year user of mathematica, but have always had one
> particular problem with it that I may have just simply missed reading
> about.
> Mathematica Version: 5.2
> Problem: I would like to develop a set of re-rewite rules to apply  
> to the
> Expected Value operator.  For example:
> E[a x] = a E[x]  a -> constant, x -> variable
> E[b + f[x]] = b + E[f[x]] , etc.
> The issue is how does one using Mathematica distinguish a 'constant
> variable (i.e. a and b)' from a variable 'variable' (i.e. 'x')? The  
> head
> of a, b and x is 'Symbol' and neither a, b nor x contain a number so I
> can't use a_?NumberQ to identify it as a constant.
> This actually goes to the wider question of how does one use  
> Mathematica
> for symbolic derviations where numbers are not actually substituted  
> in the
> derviation?
> E.g. E[a + E[b x]] /. Rule2
>    out= a + b E[x] etc.
> Thanks all - Mark Morrissey
> University of Oklahoma

First of all, it is not a good idea to use a protected symbol (E) in  
this way. But concerning your question: there is no reason why you  
shouldn't use NumberQ or NumericQ in the way you indicated.

First define the rule exactly in the way you said you could not:

Expectation[b_?NumericQ* x_]:=b*Expectation[x]

Now set:


Now you get

Expectation[a x]

a Expectation[x]

and of course as a bonus you automatically get:

Expectation[3 x]

3 Expectation[x]

This gives me a chance to offer a puzzle (for which I offer no prize)  
for those who like this sort of things. Note that



but where is this information stored by Mathematica? It can't be a  
DownValue for NumericQ, since NumericQ has the Attribute Protected  
and I did not unprotect it. And indeed:



Or, we could try to give a an UpValue:

Expectation[b_?NumericQ* x_]:=b*Expectation[x]




Expectation[a x]

a Expectation(x)

Everything works fine yet:



So now again the puzzle: where is this information stored?

Note also that NumberQ behaves quite differently. In fact you can't  
set NumberQ[a]=True without first unprotecting NumberQ. On the other  
hand if you use an UpValue to make a into a "number"


then as expected:


{HoldPattern[NumberQ[a]] :> True}

Well, any guesses?

Andrzej Kozlowski

P.S. All the above with Mathematica 5.1 but I expect it;s the same in  

  • Prev by Date: Re: Re: Question regarding Mathematica's treatment of whitespace
  • Next by Date: Re: Use of Mathematica with Rule-based Equation Derivations
  • Previous by thread: Re: Use of Mathematica with Rule-based Equation Derivations
  • Next by thread: Re: Re: Use of Mathematica with Rule-based Equation Derivations