Re: Use of Mathematica with Rule-based Equation Derivations

*To*: mathgroup at smc.vnet.net*Subject*: [mg61935] Re: [mg61914] Use of Mathematica with Rule-based Equation Derivations*From*: Andrzej Kozlowski <akoz at mimuw.edu.pl>*Date*: Sat, 5 Nov 2005 01:52:18 -0500 (EST)*References*: <200511041011.FAA14913@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On 4 Nov 2005, at 19:11, mmorriss at gcn.ou.edu 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: NumericQ[a]=True; 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 NumericQ[a] True 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: DownValues[NumericQ] {} Or, we could try to give a an UpValue: Expectation[b_?NumericQ* x_]:=b*Expectation[x] a/:NumericQ[a]=True; NumericQ[a] True Expectation[a x] a Expectation(x) Everything works fine yet: UpValues[a] {} 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" a/:NumberQ[a]=True; then as expected: UpValues[a] {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 5.2

**Follow-Ups**:**Re: Re: Use of Mathematica with Rule-based Equation Derivations***From:*János <janos.lobb@yale.edu>

**References**:**Use of Mathematica with Rule-based Equation Derivations***From:*mmorriss@gcn.ou.edu