MathGroup Archive 2000

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

Search the Archive

Re: Re: Evaluate and HoldAll

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24232] Re: [mg24222] Re: Evaluate and HoldAll
  • From: Andrzej Kozlowski <andrzej at tuins.ac.jp>
  • Date: Mon, 3 Jul 2000 20:39:15 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

The reason why what you are trying to do is not working is that when you
define a function in the form

f[x_]:=Body[...x..]

and then evaluate f[a], first a is evaluated (if the function does not hold
its arguments) and then its value literally substituted into Body in place
of x. So if there is no x in Body at that moment nothing will be
substituted. Moreover, the function SetDelayed has the HoldRest attribute,
which means that the first but not the second argument are evaluated before
a rule is entered into the rule database.

In your case on the left hand side you have t[q_, lseq] and lseq has already
been assigned the value Sequence[a1_,a2_,a3_]. So when you use the
definition SetDelayed[t[q_, lseq],Body[...rseq]] lseq is evaluated to
Sequence[a1_,a2_,a3_] but  rseq is not. Now when you next evaluate t[0.1, 1,
2, 3] the values 1,2,3 would be literaly substituted into Body for a1,a2,a3
but there is no trace of a1,a2,a3 in Body: instead there is the completely
useless rseq...

This is the problem. How about a solution? Well, unfortunately your posting
belongs to the common category, which consist of messages of the type: I
"want to do this and it does not work". Now, sometimes it happens that the
intention of the questioner is obvious, or that what he asks for can easily
be achieved by making a slight change to his code. But often the the only
thing one can say is "you can't do this. You may be able to achieve the same
purpose by a different route, except that you give us no idea of what this
purpose is". 

In your case the question is* why must you use unevaluated rseq on the right
hand side? The only reason seems to be that you want to be able to vary the
number of parameters a1, a2, a3 .... But then why you can simply use:

Clear[t]

t[q_, lseq__] :=
NIntegrate[Evaluate[(4\[Pi]/q)*r*Sin[q r]*gr[r, lseq]], {r, 0, 10^5},
MinRecursion -> 5, MaxRecursion -> 30, PrecisionGoal -> 10, WorkingPrecision
-> 64]

When you evaluate 

t[0.1, 1, 2, 3]

you will get the same numerical answer as in your example where you used
{a1,a2,a3} explicitely on the right. You will be able to vary the number of
arguments (provided you  define gr so that it also can take any number of
arguments).

Maybe this is still unsatisfactory for some mysterious reason but I would
need more explanation before I could try to suggest anything.


-- 
Andrzej Kozlowski
Toyama International University, JAPAN

For Mathematica related links and resources try:
<http://www.sstreams.com/Mathematica/>


on 7/1/00 4:21 PM, Andre Heinemann at andre at aflo4.ifw-dresden.de wrote:

> 
> Andre Heinemann wrote:
> 
>> Hi mathgroup
>> 
>> Attributes[NIntegrate] gives: {HoldAll, Protected}
>> 
>> In THE BOOK (or online Help) one can find:
>> 
>> *****************************************************************************
>> 
>> *You can use Evaluate to evaluate the arguments of a HoldAll function in
>> a controlled way.  *
>> *****************************************************************************
>> 
>> So I built:
>> 
>> rseq = Sequence[a1, a2, a3]
>> lseq = Sequence[a1_?NumericQ, a2_?NumericQ, a3_?NumericQ]
>> 
>> My Function f[r, rseq]
>> 
>> gives the output: (e.g.) a1 Exp[a2 - a3 r]
>> (I know that I can use Integrate here, instead of Nintegrate but it is
>> for testing)
>> 
>> Clear[test]
>> test[q_, lseq] := NIntegrate[Evaluate[f[r, rseq]], {r, 0, 10^5}]
>> 
>> test[0.1, 1, 0.2, 0.5 ]  presented:
>> 
>> NIntegrate::"inum" :  ... Integrand ... is not numerical at r = .... and
>> so on
>> 
>> NIntegrate[f[r, rseq],{r, 0, 10^5}]
>> 
>> Ok, I can see that the integrand is not numerical  and so I tried:
>> 
>> Clear[test]
>> test[q_, lseq] := NIntegrate[a1 Exp[a2 - a3 r] , {r, 0, 10^5}] and
>> 
>> test[0.1, 1, 0.2, 0.5 ] works very well and finds: 2.442805516320 as the
>> answer !
>> 
>> What is the point here ? I think my understanding of Evaluate[] is false
>> but
>> how can I solve the problem ?
>> 
>> I would be glad to have an explanation or a hint.
> 
> Hi
> 
> Sorry for this update, but I think I found a better formulation of my problem,
> and I found out it has nothing to do with Evaluate and HoldAll.
> Here is what I mean:
> 
> lseq = Sequence[a1_, a2_, a3_]
> rseq = Sequence[a1, a2, a3]
> 
> Clear[gr]
> gr[x_, lseq] := a1*Exp[a2 - a3 x]
> 
> In[1415]:=
> gr[r, rseq]
> 
> Out[1415]=
> \!\(a1\ \[ExponentialE]\^\(a2 - a3\ r\)\)
> 
> Clear[t]
> t[q_, lseq] :=
> NIntegrate[(4\[Pi]/q)*r*Sin[q r]*gr[r, Sequence[a1, a2, a3]], {r, 0, 10^5},
> MinRecursion -> 5,
> MaxRecursion -> 30,
> PrecisionGoal -> 10,
> WorkingPrecision -> 64]
> 
> In[1406]:=
> t[0.1, 1, 2, 3]
> 
> Out[1406]=
> 6.862786625992
> 
> Works very well ! But I need for further developments:
> 
> Clear[t]
> t[q_, lseq] :=
> NIntegrate[(4\[Pi]/q)*r*Sin[q r]*gr[r, rseq], {r, 0, 10^5},
> MinRecursion -> 5,
> MaxRecursion -> 30,
> PrecisionGoal -> 10,
> WorkingPrecision -> 64]
> 
> ... some errors...
> 
> \!\(NIntegrate[\(\((4\ \[Pi])\)\ r\ Sin[0.1`\ r]\ gr[r, rseq]\)\/0.1`, {r, 0,
> 10\^5}, MinRecursion -> 5, MaxRecursion -> 30, PrecisionGoal -> 10,
> WorkingPrecision -> 64]\)
> 
> Where you can see the problem. The right side in the definition := has not
> been
> evaluated,
> so mathematica doesn't know somthing about a1, a2, a3.
> 
> 
> Clear[t]
> t[q_, lseq] := Module[{},
> core = r*Sin[q r]*gr[r, rseq]
> ]
> 
> t[0.1, 1, 2, 3]
> 
> \!\(125.66370614359172`\ a1\ \[ExponentialE]\^\(a2 - a3\ r\)\ r\ Sin[
> 0.1`\ r]\)
> 
> -------------------------
> AND the rigth answer should be:
> -------------------------
> 
> Clear[t]
> t[q_, lseq] := Module[{},
> core = (4\[Pi]/q)*r*Sin[q r]*gr[r, a1,a2,a3]
> ]
> 
> t[0.1, 1., 2., 3.]
> 
> \!\(1.\[ExponentialE]\^\(\(\(2.)\(\[InvisibleSpace]\)\) - 3. r\)\ r\ \
> Sin[0.1 r]\)
> 
> 
> 
> I have not the faintest idea how to find a solution for this.
> 
> Andre
> 




  • Prev by Date: Re: Re: Evaluate and HoldAll
  • Next by Date: Re: PrimeQ queries
  • Previous by thread: Re: Re: Evaluate and HoldAll
  • Next by thread: Re: Re: Evaluate and HoldAll