mm-789 === Subject: writing ASCII-files in UNIX-format from Windows is there a possibility to write an ASCII-file in UNIX-format from Mathematica under Windows (or vice versa)? === Subject: Re: writing ASCII-files in UNIX-format from Windows I had the exact same problem. I was creating input files in Mathematica and then using them with a C program in cygwin on my windows machine. In the end I gave up and used fromdos.exe, which is designed for such a task: http://www.thefreecountry.com/tofrodos/ I just added a line to the shell script that would execute fromdos.exe on the input file before the C program read the input. I too would love to hear of a Mathematica solution! James Gilmore > is there a possibility to write an ASCII-file in UNIX-format from > Mathematica under Windows (or vice versa)? > Hannes === Subject: Re: Filtering same elements from two lists The Split-based solutions are not quite correct. Also, using Dispatch it is possible to be even quicker than the nice Reap/Sow based solution. Rolf Mertig GluonVision GmbH, Berlin, Gernany ---------------------------------------------------------------------------- In[1]:= !!f.m test1 = {{PLUS1, 55, 43}, {PLUS2, 555, 83}}; test2 = {{PLUS1, 77, 99}, {PLUS2, 52, 103}}; Print[simple check without Dispatch: , test1 /. Function[{x,y,z}, {x,a_Integer,b_Integer}:>{x,a,b,y,z}]@@@test2]; (* create two lists of 10^n elements *) <ToString[x]],y,z}]@@@Table[ {r1n[[i]], Random[Integer,{1,10^(n+1)}], Random[Integer,{1,10^(n+1)}]},{i,10^n}]; list2 = Function[{x,y,z}, {Symbol[PLUS<>ToString[x]],y,z}]@@@Table[ {s1n[[j]], Random[Integer,{1,10^(n+1)}], Random[Integer,{1,10^(n+1)}]},{j,10^n}]; (* using Dispatch is quite quick: *) Print[t2 timing , t2 = Timing[( r2 = list1/. Dispatch[Function[{x,y,z}, x:> Sequence[x,y,z]]@@@list2] /. {p_Symbol,x_Integer,y_,a_,b_}:>{p,a,b,x,y} );]]; (* if the order is irrelevant and there are the same amount of PLUS', then: *) Print[t3 timing , t3 = Timing[( r3 = list2/. Dispatch[Function[{x,y,z}, x:> Sequence[x,y,z]]@@@list1]);]]; (* erroneous Split-based solution: Jens-Peer Kuska *) Print[t4 timing , t4 = Timing[( r4 = Prepend[Join @@ (Rest /@ # ), #[[1, 1]]] & /@ First[#2] &] );]]; (* erroneous Split-based solution: Peter Pein *) Print[t5 timing , t5 = Timing[ ( r5 = (Flatten[{#1[[1,1]], Rest /@ #1}] & ) /@ #2[[1]] & ], Length[#1] > 1 & ] ) ;]]; (* correct Reap/Sow-based solution: dkr *) Print[t6 timing , t6 = Timing[( r6 = Reap[Sow[Rest@#,First@#]&/@ Join[list1,list2],_,{#1,Sequence@@Flatten[#2]}&][[2]] );]]; (* show that something is not right with the Split-based solutions : print things for n = 1: *) Print[list1 sorted = , list1 // Sort ]; Print[list2 sorted = , list2 // Sort ]; Print[r2 sorted = , r2 // Sort ]; Print[r4 sorted = , r4 // Sort ]; Print[r6 sorted = , r6 // Sort ]; ]; (* end For - loop *)] In[1]:= < {PLUS2, 555, 83, 52, 103}} n = 1 t2 timing {0. Second, Null} t3 timing {0. Second, Null} t4 timing {0. Second, Null} t5 timing {0. Second, Null} t6 timing {0. Second, Null} list1 sorted = {{PLUS1, 52, 100}, {PLUS10, 3, 53}, {PLUS2, 71, 17}, > {PLUS3, 50, 63}, {PLUS4, 15, 18}, {PLUS5, 16, 17}, {PLUS6, 60, 63}, > {PLUS7, 36, 67}, {PLUS8, 58, 35}, {PLUS9, 83, 90}} list2 sorted = {{PLUS1, 22, 80}, {PLUS10, 49, 32}, {PLUS2, 67, 80}, > {PLUS3, 64, 61}, {PLUS4, 69, 46}, {PLUS5, 75, 24}, {PLUS6, 80, 37}, > {PLUS7, 52, 76}, {PLUS8, 33, 19}, {PLUS9, 92, 37}} r2 sorted = {{PLUS1, 52, 100, 22, 80}, {PLUS10, 3, 53, 49, 32}, > {PLUS2, 71, 17, 67, 80}, {PLUS3, 50, 63, 64, 61}, > {PLUS4, 15, 18, 69, 46}, {PLUS5, 16, 17, 75, 24}, > {PLUS6, 60, 63, 80, 37}, {PLUS7, 36, 67, 52, 76}, > {PLUS8, 58, 35, 33, 19}, {PLUS9, 83, 90, 92, 37}} r4 sorted = {{PLUS1, 22, 80, 52, 100}, {PLUS10, 3, 53, 49, 32}, > {PLUS2, 67, 80, 71, 17}, {PLUS3, 50, 63, 64, 61}, > {PLUS4, 15, 18, 69, 46}, {PLUS5, 16, 17, 75, 24}, > {PLUS6, 60, 63, 80, 37}, {PLUS7, 36, 67, 52, 76}, > {PLUS8, 33, 19, 58, 35}, {PLUS9, 83, 90, 92, 37}} r6 sorted = {{PLUS1, 52, 100, 22, 80}, {PLUS10, 3, 53, 49, 32}, > {PLUS2, 71, 17, 67, 80}, {PLUS3, 50, 63, 64, 61}, > {PLUS4, 15, 18, 69, 46}, {PLUS5, 16, 17, 75, 24}, > {PLUS6, 60, 63, 80, 37}, {PLUS7, 36, 67, 52, 76}, > {PLUS8, 58, 35, 33, 19}, {PLUS9, 83, 90, 92, 37}} check r2 with r3 True check r2 with r4 False check r2 with r5 False check r2 with r6 True n = 4 t2 timing {0.140009 Second, Null} t3 timing {0.128008 Second, Null} t4 timing {0.236015 Second, Null} t5 timing {0.264017 Second, Null} t6 timing {0.244014 Second, Null} check r2 with r3 True check r2 with r4 False check r2 with r5 False check r2 with r6 True === Subject: Re: Why Does Repeated Dot Product Take So Long? I think I found the problem. Vector x contains a mixture of rational and irrational numbers. I went back and recreated x, which is based on another computation, by applying the N[] function. This reduced the time to compute the dot products from about 112 seconds to .06 seconds for an even larger vectors of length 8,000! the different ways of arriving at an answer. Gregory > Unless your matrices are symbolic, it should go pretty fast. > $Version > 5.2 for Mac OS X (June 20, 2005) > n=2000; > x=Table[Random[],{n},{3}]; > y=Table[Random[],{n},{3}]; > Timing[sol1=Table[x[[i]].y[[i]], {i, n}];] > {0.007042 Second,Null} > Timing[sol2=#[[1]].#[[2]]&/@Thread[{x,y}];] > {0.028243 Second,Null} > Timing[sol3=Dot@@@Thread[{x,y}];] > {0.04984 Second,Null} > Timing[sol4=MapThread[Dot,{x,y}];] > {0.047926 Second,Null} > sol1==sol2==sol3==sol4 > True > >> Hello Everyone, >> x and y are both 2000 x 3 matrices. I wanted to created a 2000 x 1 >> vector with each element equal to the dot product of the >> corresponding rows of x and y. So I tried this: >> Table[x[[i]].y[[i]], {i, 1, 2000}] >> It took more than two and a half minutes on my iBook G4. Is that >> normal? I've done seemingly more demanding computations in Do loops >> and Tables that are completed in a split second. Am I doing >> something wrong with this one? >> Gregory === Subject: delayed rule evaluation order Hold[{2, 3}] /. {{x_, y_} :> x^y} the result is Hold[Power[2,3]] I would like the result to be Hold[8] The original context is post-processing of a large Trace output (via SetPrecision to get rid of digits and improve readability). Any ideas? -- http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order I can see now what went wrong. The problem is with the use of ExactNumberQ. Here is an approach that works: f[1] = 1; f[n_Integer] /; n > 1 = n f[n - 1]; InputForm[z = Trace[f[2]]]; v = z /. {blah_Integer :> SetPrecision[blah, 3]}; v/.p_SetPrecision:> With[{eval = p}, eval /; True] {f(2.00),{2.00>1.00,True},2.00 f(-1.00+2.00),{{-1.00+2.00,1.00},f( 1.00),1.00},2.00 1.00,2.00} Of course if you have Rationals in your expression and other non- exact numbers you will have to deal with these too in a similar way. Andrzej Kozlowski > O maybe that is not so ideal after all, since f got evaluated. You > can of course combine it with the Block technique I used earlier. > Andrzej >> Here is another way, which you may like better. It uses the blah >> approach and the Trott-Strzebonski partial evaluation technique. >> f[1] = 1; >> f[n_Integer] /; n > 1 = n f[n - 1]; >> InputForm[z = Trace[f[2]]]; >> v = z /. {blah_?ExactNumberQ :> SetPrecision[blah, 3]}; >> v/.p_SetPrecision:> With[{eval = p}, eval /; True] >> {2.00,{2.00>1.00,True},2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} >> This must be finally what you wanted, isn't it? >> Andrzej Kozlowski > It's much easier to answer questions if the persons who pose them > explain clearly what they mean. > The most obvious way to modify my code seems to me to be: > f[1] = 1; > f[n_Integer] /; n > 1 = n f[n - 1]; > InputForm[z = Trace[f[2]]]; > Block[{f=g},z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f > {f(2.00),{True,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, > 2.00,2.00} > Now f is not evaluated. The only thing you might still complain > about is that {2 > 1, True} evaluated to {True,True}. If you > really care about this you, can prevent it in various ways, for > example: > Block[{f=g}, > z/.{HoldForm[a_]/; > FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f > {f(2.00),{2>1,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, > 2.00,2.00} > or > Block[{f=g,Greater=greater}, > z/.{HoldForm[a_]/; > FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.{g- >f,greater->Greater} > {f(2.00),{2.00>1.00,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, > 2.00,2.00} > There are many other possibilities. > If there is still anything you do not like than it probably means > you still have not explained completely what you want. > Andrzej Kozlowski >> What I want it to do is be able to replace numbers that aren't >> direct >> arguments of HoldForm (maybe they are nested a few levels deep, >> etc). >> In the example you sent me, f is evaluated - which is >> undesirable for me. >>> *This message was transferred with a trial version of >>> It seems to me that a variant of my first suggestion works fine: >>>> In[1]:= >>> f[1]=1; >>> f[n_Integer]/;n>1=n f[n-1]; >>> InputForm[z=Trace[f[2]]]; >>> InputForm[z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}] >>>> Out[4]//InputForm= >>> {HoldForm[2.`2.9999999999999973], >>> {HoldForm[True], HoldForm[True]}, >>> HoldForm[2.`2.9999999999999973], >>> {{HoldForm[1.`2.9999999999999973], >>> HoldForm[1.`2.9999999999999973]}, >>> HoldForm[1.`2.9999999999999973], >>> HoldForm[1.`2.9999999999999973]}, >>> HoldForm[2.`2.9999999999999973], >>> HoldForm[2.`2.9999999999999973]} >>>> Or is this not what you wanted? >>>> Andrzej Kozlowski >>>>>>> received >>> any others so far), I am now using this type of replacement: >>>> Hold[5.55555555]/.{blah_?InexactNumberQ:>> junk[SetPrecision[blah,3]]}/.junk->Evaluate >>>> Hold[5.56] >>>> However, this still does not totally work on Trace's output: >>>> In[1]:= >>> f[1]=1; >>> f[n_Integer]/;n>1=n f[n-1]; >>> InputForm[z=Trace[f[2]]] >>>> Out[3]//InputForm= >>> {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm >>> [2*f[-1 >>> + 2]], >>> {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, >>> HoldForm[2*1], HoldForm[2]} >>>> In[4]:= >>> InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah, >>> 3]]}/.junk- >>> >Evaluate] >>>> Out[4]//InputForm= >>> {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate >>> [SetPrecision[2, >>> 3]] > Evaluate[SetPrecision[1, 3]]], >>> HoldForm[True]}, HoldForm[2.`2.9999999999999996], >>> {{HoldForm[1.`2.9999999999999996], HoldForm >>> [1.`2.9999999999999996]}, >>> HoldForm[1.`2.9999999999999996], >>> HoldForm[1.`2.9999999999999996]}, HoldForm >>> [2.`2.9999999999999996], >>> HoldForm[2.`2.9999999999999996]} >>>> Notice the leftover Evaluate and SetPrecision commands. Does >>> anyone >>> have ideas on how to get this to work? >>>>> Hold[{2, 3}] /. >>>> {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} >>>>>> Hold[8] >>>>>>>> >>>>>> Hold[{2, 3}] /. {{x_, y_} :> x^y} >>>>>> the result is Hold[Power[2,3]] >>>>>> I would like the result to be Hold[8] >>>>>> The original context is post-processing of a large Trace >>> output >>>> (via >>>> SetPrecision to get rid of digits and improve readability). >>>>>> Any ideas? >>>>>> -- >>>> http://chris.chiasson.name/ >>>>>>>> -- >>>>>> >>>> hanlonr@cox.net >>>>>>>>>>> -- >>> http://chris.chiasson.name/ >>>> -- >> http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order Look at the refguide entry for ReplaceAll. It gives an example of evaluatePatternMatches[heldexpr_Hold, patt_] := heldexpr /.p_ /; MatchQ[Unevaluated[p], patt] :> With[{eval = p}, eval /; True] which illustrates the way to make rule evaluate inside held expressions. In[3]:= Hold[{2,3}]/.{{x_,y_} :> With[{m=x^y},m/;True]} Out[3]= Hold[8] Oleksandr Pavlyk Wolfram Research > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? > -- > http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order When rule evaluation is done outside the HoldForms that Trace produces, the PatternTest (the infixed question in blah_?ExactNumberQ) starts causing the unwanted evaluations. A way to get around that is to require that the candidate expression have an appropriate Head. In this example, the appropriate Head is Integer, as Dr. Woll pointed out. In my actual problem, the appropriate Head is Real. Peter Pein Andrzej Kozlowski Carl Woll David Park > This is a fairly interesting replacement technique; it reminds me of > the one David Park used for capturing side-effects of Condition > processing. > http://forums.wolfram.com/mathgroup/archive/2006/May/msg00621.html > Unfortunately, it is causing unwanted evaluations. Why does it do this? > In[1]:= > f[1]=1; > f[n_Integer]=n f[n-1]; > f[2`3] > z=Trace[f[2]] > z/.{blah_?ExactNumberQ[RuleDelayed]With[{p=SetPrecision[blah,3]},p/;True]} > Out[3]= > f[2.00] > Out[4]= > {f[2],2 f[-1+2],{{-1+2,1},f[1],1},2 1,2} > Out[5]= > {2.00,2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} > Chris, > First let's look at the input form of z: > In[7]:= z // InputForm > Out[7]//InputForm= > {HoldForm[f[2]], HoldForm[2*f[-1 + 2]], {{HoldForm[-1 + 2], > HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, > HoldForm[2*1], HoldForm[2]} > So, your question is why HoldForm[f[2]] is being replaced with 2.00. > In[7]:= HoldForm[f[2]] /. > blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, p /; True] > Out[7]= 2.00 > Let's use Reap/Sow to find out what blah is: > In[8]:= HoldForm[f[2]] /. > blah_?ExactNumberQ : With[{p = SetPrecision[blah, 3]}, Sow[Hold[blah]]; p /; True] // Reap > Out[8]= {2.00, {{Hold[f[2]]}}} > So, M-- looks at f[2], which when evaluated is 2, an exact number: > In[9]:= ExactNumberQ[f[2]] > Out[9]= True > Hence, f[2] is replaced with 2.00. > I don't know what you are trying to accomplish here, but two > possibilities are: > In[10]:= Block[{f}, > z /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, p /; True]] > Out[10]= {f[2.00], 2.00 f[1.00], {{1.00, 1.00}, f[1.00], 1.00}, 2.00, 2.00} > and > In[11]:= z /. blah_Integer :> With[{p = SetPrecision[blah, 3]}, p /; True] > Out[11]= {f[2.00], 2.00 f[-1.00 + 2.00], {{-1.00 + 2.00, 1.00}, f[1.00], > 1.00}, 2.00 1.00, 2.00} > Carl Woll > Wolfram Research > The output is humorous (in a demented way, I guess) if With is > replaced with Module. >> Hold[{2, 3}] /. {{x_, y_} :> x^y} >> the result is Hold[Power[2,3]] >> I would like the result to be Hold[8] >> The original context is post-processing of a large Trace output (via >> SetPrecision to get rid of digits and improve readability). >> Any ideas? >> This is not an easy one to figure out. One possibility is given in the >> help for ReplaceAll in the last example of the Further Examples section. >> In this example the following construct is used: >> In[27]:= Hold[{2, 3}] /. {x_, y_} :> With[{p = x^y}, p /; True] >> Out[27]= Hold[8] >> Carl Woll >> Wolfram Research >> -- http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order > This is a fairly interesting replacement technique; it reminds me of > the one David Park used for capturing side-effects of Condition > processing. > http://forums.wolfram.com/mathgroup/archive/2006/May/msg00621.html > Unfortunately, it is causing unwanted evaluations. Why does it do this? > In[1]:= > f[1]=1; > f[n_Integer]=n f[n-1]; > f[2`3] > z=Trace[f[2]] > z/.{blah_?ExactNumberQ[RuleDelayed]With[{p=SetPrecision[blah,3]},p/;True]} > Out[3]= > f[2.00] > Out[4]= > {f[2],2 f[-1+2],{{-1+2,1},f[1],1},2 1,2} > Out[5]= > {2.00,2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} Chris, First let's look at the input form of z: In[7]:= z // InputForm Out[7]//InputForm= {HoldForm[f[2]], HoldForm[2*f[-1 + 2]], {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, HoldForm[2*1], HoldForm[2]} So, your question is why HoldForm[f[2]] is being replaced with 2.00. In[7]:= HoldForm[f[2]] /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, p /; True] Out[7]= 2.00 Let's use Reap/Sow to find out what blah is: In[8]:= HoldForm[f[2]] /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, Sow[Hold[blah]]; p /; True] // Reap Out[8]= {2.00, {{Hold[f[2]]}}} So, M-- looks at f[2], which when evaluated is 2, an exact number: In[9]:= ExactNumberQ[f[2]] Out[9]= True Hence, f[2] is replaced with 2.00. I don't know what you are trying to accomplish here, but two possibilities are: In[10]:= Block[{f}, z /. blah_?ExactNumberQ :> With[{p = SetPrecision[blah, 3]}, p /; True]] Out[10]= {f[2.00], 2.00 f[1.00], {{1.00, 1.00}, f[1.00], 1.00}, 2.00, 2.00} and In[11]:= z /. blah_Integer :> With[{p = SetPrecision[blah, 3]}, p /; True] Out[11]= {f[2.00], 2.00 f[-1.00 + 2.00], {{-1.00 + 2.00, 1.00}, f[1.00], 1.00}, 2.00 1.00, 2.00} Carl Woll Wolfram Research > The output is humorous (in a demented way, I guess) if With is > replaced with Module. >> Hold[{2, 3}] /. {{x_, y_} :> x^y} >> the result is Hold[Power[2,3]] >> I would like the result to be Hold[8] >> The original context is post-processing of a large Trace output (via >> SetPrecision to get rid of digits and improve readability). >> Any ideas? >> This is not an easy one to figure out. One possibility is given in the >> help for ReplaceAll in the last example of the Further Examples section. >> In this example the following construct is used: >> In[27]:= Hold[{2, 3}] /. {x_, y_} :> With[{p = x^y}, p /; True] >> Out[27]= Hold[8] >> Carl Woll >> Wolfram Research === Subject: Re: delayed rule evaluation order It's much easier to answer questions if the persons who pose them explain clearly what they mean. The most obvious way to modify my code seems to me to be: f[1] = 1; f[n_Integer] /; n > 1 = n f[n - 1]; InputForm[z = Trace[f[2]]]; Block[{f=g},z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f {f(2.00),{True,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00},2.00,2.00} Now f is not evaluated. The only thing you might still complain about is that {2 > 1, True} evaluated to {True,True}. If you really care about this you, can prevent it in various ways, for example: Block[{f=g}, z/.{HoldForm[a_]/; FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f {f(2.00),{2>1,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00},2.00,2.00} or Block[{f=g,Greater=greater}, z/.{HoldForm[a_]/; FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.{g->f,greater- >Greater} {f(2.00),{2.00>1.00,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, 2.00,2.00} There are many other possibilities. If there is still anything you do not like than it probably means you still have not explained completely what you want. Andrzej Kozlowski > What I want it to do is be able to replace numbers that aren't direct > arguments of HoldForm (maybe they are nested a few levels deep, etc). > In the example you sent me, f is evaluated - which is undesirable > for me. >> (tm) Pro* >> It seems to me that a variant of my first suggestion works fine: >> In[1]:= >> f[1]=1; >> f[n_Integer]/;n>1=n f[n-1]; >> InputForm[z=Trace[f[2]]]; >> InputForm[z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}] >> Out[4]//InputForm= >> {HoldForm[2.`2.9999999999999973], >> {HoldForm[True], HoldForm[True]}, >> HoldForm[2.`2.9999999999999973], >> {{HoldForm[1.`2.9999999999999973], >> HoldForm[1.`2.9999999999999973]}, >> HoldForm[1.`2.9999999999999973], >> HoldForm[1.`2.9999999999999973]}, >> HoldForm[2.`2.9999999999999973], >> HoldForm[2.`2.9999999999999973]} >> Or is this not what you wanted? >> Andrzej Kozlowski >> received >> any others so far), I am now using this type of replacement: >> Hold[5.55555555]/.{blah_?InexactNumberQ:> junk[SetPrecision[blah,3]]}/.junk->Evaluate >> Hold[5.56] >> However, this still does not totally work on Trace's output: >> In[1]:= >> f[1]=1; >> f[n_Integer]/;n>1=n f[n-1]; >> InputForm[z=Trace[f[2]]] >> Out[3]//InputForm= >> {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm[2*f[-1 >> + 2]], >> {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, >> HoldForm[2*1], HoldForm[2]} >> In[4]:= >> InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah, >> 3]]}/.junk- >> >Evaluate] >> Out[4]//InputForm= >> {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate[SetPrecision >> [2, >> 3]] > Evaluate[SetPrecision[1, 3]]], >> HoldForm[True]}, HoldForm[2.`2.9999999999999996], >> {{HoldForm[1.`2.9999999999999996], HoldForm >> [1.`2.9999999999999996]}, >> HoldForm[1.`2.9999999999999996], >> HoldForm[1.`2.9999999999999996]}, HoldForm[2.`2.9999999999999996], >> HoldForm[2.`2.9999999999999996]} >> Notice the leftover Evaluate and SetPrecision commands. Does anyone >> have ideas on how to get this to work? >>> Hold[{2, 3}] /. >>> {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} >>>> Hold[8] >>>>> >>>> Hold[{2, 3}] /. {{x_, y_} :> x^y} >>>> the result is Hold[Power[2,3]] >>>> I would like the result to be Hold[8] >>>> The original context is post-processing of a large Trace output >>> (via >>> SetPrecision to get rid of digits and improve readability). >>>> Any ideas? >>>> -- >>> http://chris.chiasson.name/ >>>>> -- >>>> >>> hanlonr@cox.net >>>>> -- >> http://chris.chiasson.name/ > -- > http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order This is a fairly interesting replacement technique; it reminds me of the one David Park used for capturing side-effects of Condition processing. http://forums.wolfram.com/mathgroup/archive/2006/May/msg00621.html Unfortunately, it is causing unwanted evaluations. Why does it do this? In[1]:= f[1]=1; f[n_Integer]=n f[n-1]; f[2`3] z=Trace[f[2]] z/.{blah_?ExactNumberQ[RuleDelayed]With[{p=SetPrecision[blah,3]},p/;True]} Out[3]= f[2.00] Out[4]= {f[2],2 f[-1+2],{{-1+2,1},f[1],1},2 1,2} Out[5]= {2.00,2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} The output is humorous (in a demented way, I guess) if With is replaced with Module. > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? > This is not an easy one to figure out. One possibility is given in the > help for ReplaceAll in the last example of the Further Examples section. > In this example the following construct is used: > In[27]:= Hold[{2, 3}] /. {x_, y_} :> With[{p = x^y}, p /; True] > Out[27]= Hold[8] > Carl Woll > Wolfram Research -- http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order O maybe that is not so ideal after all, since f got evaluated. You can of course combine it with the Block technique I used earlier. Andrzej > Here is another way, which you may like better. It uses the blah > approach and the Trott-Strzebonski partial evaluation technique. > f[1] = 1; > f[n_Integer] /; n > 1 = n f[n - 1]; > InputForm[z = Trace[f[2]]]; > v = z /. {blah_?ExactNumberQ :> SetPrecision[blah, 3]}; > v/.p_SetPrecision:> With[{eval = p}, eval /; True] > {2.00,{2.00>1.00,True},2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} > This must be finally what you wanted, isn't it? > Andrzej Kozlowski >> It's much easier to answer questions if the persons who pose them >> explain clearly what they mean. >> The most obvious way to modify my code seems to me to be: >> f[1] = 1; >> f[n_Integer] /; n > 1 = n f[n - 1]; >> InputForm[z = Trace[f[2]]]; >> Block[{f=g},z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f >> {f(2.00),{True,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, >> 2.00,2.00} >> Now f is not evaluated. The only thing you might still complain >> about is that {2 > 1, True} evaluated to {True,True}. If you >> really care about this you, can prevent it in various ways, for >> example: >> Block[{f=g}, >> z/.{HoldForm[a_]/; >> FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f >> {f(2.00),{2>1,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, >> 2.00,2.00} >> or >> Block[{f=g,Greater=greater}, >> z/.{HoldForm[a_]/; >> FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.{g- >>f,greater->Greater} >> {f(2.00),{2.00>1.00,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, >> 2.00,2.00} >> There are many other possibilities. >> If there is still anything you do not like than it probably means >> you still have not explained completely what you want. >> Andrzej Kozlowski > What I want it to do is be able to replace numbers that aren't > direct > arguments of HoldForm (maybe they are nested a few levels deep, > etc). > In the example you sent me, f is evaluated - which is undesirable > for me. >> (tm) Pro* >> It seems to me that a variant of my first suggestion works fine: >> In[1]:= >> f[1]=1; >> f[n_Integer]/;n>1=n f[n-1]; >> InputForm[z=Trace[f[2]]]; >> InputForm[z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}] >> Out[4]//InputForm= >> {HoldForm[2.`2.9999999999999973], >> {HoldForm[True], HoldForm[True]}, >> HoldForm[2.`2.9999999999999973], >> {{HoldForm[1.`2.9999999999999973], >> HoldForm[1.`2.9999999999999973]}, >> HoldForm[1.`2.9999999999999973], >> HoldForm[1.`2.9999999999999973]}, >> HoldForm[2.`2.9999999999999973], >> HoldForm[2.`2.9999999999999973]} >> Or is this not what you wanted? >> Andrzej Kozlowski >> received >> any others so far), I am now using this type of replacement: >> Hold[5.55555555]/.{blah_?InexactNumberQ:> junk[SetPrecision[blah,3]]}/.junk->Evaluate >> Hold[5.56] >> However, this still does not totally work on Trace's output: >> In[1]:= >> f[1]=1; >> f[n_Integer]/;n>1=n f[n-1]; >> InputForm[z=Trace[f[2]]] >> Out[3]//InputForm= >> {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm >> [2*f[-1 >> + 2]], >> {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, >> HoldForm[2*1], HoldForm[2]} >> In[4]:= >> InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah, >> 3]]}/.junk- >> >Evaluate] >> Out[4]//InputForm= >> {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate >> [SetPrecision[2, >> 3]] > Evaluate[SetPrecision[1, 3]]], >> HoldForm[True]}, HoldForm[2.`2.9999999999999996], >> {{HoldForm[1.`2.9999999999999996], HoldForm >> [1.`2.9999999999999996]}, >> HoldForm[1.`2.9999999999999996], >> HoldForm[1.`2.9999999999999996]}, HoldForm >> [2.`2.9999999999999996], >> HoldForm[2.`2.9999999999999996]} >> Notice the leftover Evaluate and SetPrecision commands. Does >> anyone >> have ideas on how to get this to work? >>> Hold[{2, 3}] /. >>> {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} >>>> Hold[8] >>>>> >>>> Hold[{2, 3}] /. {{x_, y_} :> x^y} >>>> the result is Hold[Power[2,3]] >>>> I would like the result to be Hold[8] >>>> The original context is post-processing of a large Trace >> output >>> (via >>> SetPrecision to get rid of digits and improve readability). >>>> Any ideas? >>>> -- >>> http://chris.chiasson.name/ >>>>> -- >>>> >>> hanlonr@cox.net >>>>> -- >> http://chris.chiasson.name/ > -- > http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order Here is another way, which you may like better. It uses the blah approach and the Trott-Strzebonski partial evaluation technique. f[1] = 1; f[n_Integer] /; n > 1 = n f[n - 1]; InputForm[z = Trace[f[2]]]; v = z /. {blah_?ExactNumberQ :> SetPrecision[blah, 3]}; v/.p_SetPrecision:> With[{eval = p}, eval /; True] {2.00,{2.00>1.00,True},2.00,{{1.00,1.00},1.00,1.00},2.00,2.00} This must be finally what you wanted, isn't it? Andrzej Kozlowski > It's much easier to answer questions if the persons who pose them > explain clearly what they mean. > The most obvious way to modify my code seems to me to be: > f[1] = 1; > f[n_Integer] /; n > 1 = n f[n - 1]; > InputForm[z = Trace[f[2]]]; > Block[{f=g},z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f > {f(2.00),{True,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, > 2.00,2.00} > Now f is not evaluated. The only thing you might still complain > about is that {2 > 1, True} evaluated to {True,True}. If you really > care about this you, can prevent it in various ways, for example: > Block[{f=g}, > z/.{HoldForm[a_]/; > FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.g->f > {f(2.00),{2>1,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00},2.00,2.00} > or > Block[{f=g,Greater=greater}, > z/.{HoldForm[a_]/; > FreeQ[a,True]:>HoldForm@@{SetPrecision[a,3]}}]/.{g->f,greater- >Greater} > {f(2.00),{2.00>1.00,True},2.00 f(1.00),{{1.00,1.00},f(1.00),1.00}, > 2.00,2.00} > There are many other possibilities. > If there is still anything you do not like than it probably means > you still have not explained completely what you want. > Andrzej Kozlowski >> What I want it to do is be able to replace numbers that aren't direct >> arguments of HoldForm (maybe they are nested a few levels deep, etc). >> In the example you sent me, f is evaluated - which is undesirable >> for me. > (tm) Pro* > It seems to me that a variant of my first suggestion works fine: > In[1]:= > f[1]=1; > f[n_Integer]/;n>1=n f[n-1]; > InputForm[z=Trace[f[2]]]; > InputForm[z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}] > Out[4]//InputForm= > {HoldForm[2.`2.9999999999999973], > {HoldForm[True], HoldForm[True]}, > HoldForm[2.`2.9999999999999973], > {{HoldForm[1.`2.9999999999999973], > HoldForm[1.`2.9999999999999973]}, > HoldForm[1.`2.9999999999999973], > HoldForm[1.`2.9999999999999973]}, > HoldForm[2.`2.9999999999999973], > HoldForm[2.`2.9999999999999973]} > Or is this not what you wanted? > Andrzej Kozlowski > received > any others so far), I am now using this type of replacement: Hold[5.55555555]/.{blah_?InexactNumberQ: junk[SetPrecision[blah,3]]}/.junk->Evaluate Hold[5.56] However, this still does not totally work on Trace's output: In[1]:= > f[1]=1; > f[n_Integer]/;n>1=n f[n-1]; > InputForm[z=Trace[f[2]]] Out[3]//InputForm= > {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm[2*f > [-1 > + 2]], > {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, > HoldForm[2*1], HoldForm[2]} In[4]:= > InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah, > 3]]}/.junk- > >Evaluate] Out[4]//InputForm= > {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate > [SetPrecision[2, > 3]] > Evaluate[SetPrecision[1, 3]]], > HoldForm[True]}, HoldForm[2.`2.9999999999999996], > {{HoldForm[1.`2.9999999999999996], HoldForm > [1.`2.9999999999999996]}, > HoldForm[1.`2.9999999999999996], > HoldForm[1.`2.9999999999999996]}, HoldForm > [2.`2.9999999999999996], > HoldForm[2.`2.9999999999999996]} Notice the leftover Evaluate and SetPrecision commands. Does > anyone > have ideas on how to get this to work? > Hold[{2, 3}] /. >> {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} >> Hold[8] >> >> Hold[{2, 3}] /. {{x_, y_} :> x^y} >> the result is Hold[Power[2,3]] >> I would like the result to be Hold[8] >> The original context is post-processing of a large Trace output >> (via >> SetPrecision to get rid of digits and improve readability). >> Any ideas? >> -- >> http://chris.chiasson.name/ >> -- >> >> hanlonr@cox.net > -- > http://chris.chiasson.name/ >> -- >> http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order It seems to me that a variant of my first suggestion works fine: In[1]:= f[1]=1; f[n_Integer]/;n>1=n f[n-1]; InputForm[z=Trace[f[2]]]; InputForm[z/.{HoldForm[a_]:>HoldForm@@{SetPrecision[a,3]}}] Out[4]//InputForm= {HoldForm[2.`2.9999999999999973], {HoldForm[True], HoldForm[True]}, HoldForm[2.`2.9999999999999973], {{HoldForm[1.`2.9999999999999973], HoldForm[1.`2.9999999999999973]}, HoldForm[1.`2.9999999999999973], HoldForm[1.`2.9999999999999973]}, HoldForm[2.`2.9999999999999973], HoldForm[2.`2.9999999999999973]} Or is this not what you wanted? Andrzej Kozlowski > any others so far), I am now using this type of replacement: > Hold[5.55555555]/.{blah_?InexactNumberQ: junk[SetPrecision[blah,3]]}/.junk->Evaluate > Hold[5.56] > However, this still does not totally work on Trace's output: > In[1]:= > f[1]=1; > f[n_Integer]/;n>1=n f[n-1]; > InputForm[z=Trace[f[2]]] > Out[3]//InputForm= > {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm[2*f[-1 > + 2]], > {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, > HoldForm[2*1], HoldForm[2]} > In[4]:= > InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah,3]]}/.junk- >Evaluate] > Out[4]//InputForm= > {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate[SetPrecision[2, > 3]] > Evaluate[SetPrecision[1, 3]]], > HoldForm[True]}, HoldForm[2.`2.9999999999999996], > {{HoldForm[1.`2.9999999999999996], HoldForm[1.`2.9999999999999996]}, > HoldForm[1.`2.9999999999999996], > HoldForm[1.`2.9999999999999996]}, HoldForm[2.`2.9999999999999996], > HoldForm[2.`2.9999999999999996]} > Notice the leftover Evaluate and SetPrecision commands. Does anyone > have ideas on how to get this to work? >> Hold[{2, 3}] /. >> {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} >> Hold[8] >> >> Hold[{2, 3}] /. {{x_, y_} :> x^y} >> the result is Hold[Power[2,3]] >> I would like the result to be Hold[8] >> The original context is post-processing of a large Trace output >> (via >> SetPrecision to get rid of digits and improve readability). >> Any ideas? >> -- >> http://chris.chiasson.name/ >> -- >> >> hanlonr@cox.net > -- > http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order any others so far), I am now using this type of replacement: Hold[5.55555555]/.{blah_?InexactNumberQ:> junk[SetPrecision[blah,3]]}/.junk->Evaluate Hold[5.56] However, this still does not totally work on Trace's output: In[1]:= f[1]=1; f[n_Integer]/;n>1=n f[n-1]; InputForm[z=Trace[f[2]]] Out[3]//InputForm= {HoldForm[f[2]], {HoldForm[2 > 1], HoldForm[True]}, HoldForm[2*f[-1 + 2]], {{HoldForm[-1 + 2], HoldForm[1]}, HoldForm[f[1]], HoldForm[1]}, HoldForm[2*1], HoldForm[2]} In[4]:= InputForm[z/.{blah_?ExactNumberQ:>junk[SetPrecision[blah,3]]}/.junk->Evaluat e] Out[4]//InputForm= {HoldForm[2.`2.9999999999999996], {HoldForm[Evaluate[SetPrecision[2, 3]] > Evaluate[SetPrecision[1, 3]]], HoldForm[True]}, HoldForm[2.`2.9999999999999996], {{HoldForm[1.`2.9999999999999996], HoldForm[1.`2.9999999999999996]}, HoldForm[1.`2.9999999999999996], HoldForm[1.`2.9999999999999996]}, HoldForm[2.`2.9999999999999996], HoldForm[2.`2.9999999999999996]} Notice the leftover Evaluate and SetPrecision commands. Does anyone have ideas on how to get this to work? > Hold[{2, 3}] /. > {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} > Hold[8] > > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? > -- > http://chris.chiasson.name/ > -- > > hanlonr@cox.net -- http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order Hold[{2, 3}] /. {h_[{x_, y_}] :> (h @@ { x^y})} ?? Jens Chris Chiasson schrieb im | Hold[{2, 3}] /. {{x_, y_} :> x^y} | | the result is Hold[Power[2,3]] | | I would like the result to be Hold[8] | | The original context is post-processing of a large Trace output (via | SetPrecision to get rid of digits and improve readability). | | Any ideas? | | -- | http://chris.chiasson.name/ | === Subject: Re: delayed rule evaluation order > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? > -- > http://chris.chiasson.name/ There are a number of ways to do this for this particular case (including some very obvious ones which I will ignore) but some of them may not work in the situation you seem to have in mind. It would be helpful to see a little more of your Trace output, but anyway, it seems to me that the following should work: Hold[{2, 3}] /. {Hold[{x_, y_}] :> Hold@@{x^y}} Hold[8] Andrzej Kozlowski === Subject: Re: delayed rule evaluation order At the risk of making this thread even more long in the tooth, I would like to contradict my statement about PatternTest being the culprit. PatternTest and appropriate Pattern objects are not mutually exclusive; it was my overly-general Pattern object, Blank[] (aka _ ), that caused the problems. If I have any more to add, I'll just append it to the MathGroup thread rather than using direct email. === Subject: Re: delayed rule evaluation order > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? This is not an easy one to figure out. One possibility is given in the help for ReplaceAll in the last example of the Further Examples section. In this example the following construct is used: In[27]:= Hold[{2, 3}] /. {x_, y_} :> With[{p = x^y}, p /; True] Out[27]= Hold[8] Carl Woll Wolfram Research === Subject: Re: delayed rule evaluation order > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? Hold[{2, 3}] /. {{x_, y_} :> x^y, Hold -> Composition[Hold, Evaluate]} --> Hold[8] or, if you only want to process held pairs: Hold[{2, 3}] /. Hold[{x_, y_}] :> Composition[Hold, Evaluate][x^y] hth, Peter === Subject: RE: LogLogErrorListPlot? Antonio, Yes, I'm certain that you can. But I wouldn't use any of the various Mathematica List type plots. Instead put your data and error bar limits in log form. Then use a Show[Graphics[{Point/@data, Line[data],errorbar primitives}]] plot statement. Make a routine to draw the kind of error bar that you want for each point. It would, say, draw a vertical Line between the two error limits and then draw two short cross lines at the top and bottom. Map this routine onto your errorbar data. Then use LogScale from Graphics`Graphics to produce and label the ticks. It is a certain amount of custom work but in the end it will be faster, easier and more controlled than trying to force the various List plots into what you want. In fact, I think all the List plot routines should be thrown into the trash can. David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ I would like to know if it is possible to make a logarithmic listplot with error bars. Something like LogLogErrorListPlot or LogLogMultipleListPlot? Antonio Cardoso -- ___________________________________________________ Play 100s of games for FREE! http://games.mail.com/ === Subject: Extracting a Function's Domain and Image Re Mathematica 5.2.2. Why does the last line of code return {x} and not {a,b,_}, while if the f[_]=0 is struck, the answer is {a,b}? Bruce Remove@f; f[_]=0; f[a]=1; f[b]=2; Clear@x; h=DownValues@f Cases[h,f[x_]->x,Infinity] Out[52]= {HoldPattern[f[a]]:>1,HoldPattern[f[ b]]:>2,HoldPattern[f[_]]:>0} Out[53]= {x} === Subject: Re: Extracting a Function's Domain and Image It is because, your definition implies f[x_] evaluates to zero. This Cases sees Cases[h,0->x,Infinity]. Thus zero is picked up from h and replaced with x. You should use In[33]:= Cases[h, HoldPattern[f][x_] -> x, {0, Infinity}] Out[33]= {a, b, _} Oleksandr Pavlyk Wolfram Research > Re Mathematica 5.2.2. > Why does the last line of code return {x} and not {a,b,_}, while if the f[_]=0 is struck, the answer is {a,b}? > Bruce > Remove@f; > f[_]=0; > f[a]=1; > f[b]=2; > Clear@x; > h=DownValues@f > Cases[h,f[x_]->x,Infinity] > Out[52]= > {HoldPattern[f[a]]:>1,HoldPattern[f[ > b]]:>2,HoldPattern[f[_]]:>0} > Out[53]= > {x} === Subject: Re: Extracting a Function's Domain and Image because Cases[] handels Rules in a special way: Cases[expr, pattern -> rhs, levspec] gives the values of rhs which match the pattern. and you mean Cases[h, HoldPattern[f[a_]] -> a, Infinity] Jens Bruce Colletti schrieb im | Re Mathematica 5.2.2. | | Why does the last line of code return {x} and not {a,b,_}, while if the f[_]=0 is struck, the answer is {a,b}? | | I want to extract the domain and image of a user-defined function, and tech support recommended using DownValues (pretty handy). If you have an easier approach, please share it. | | Bruce | | Remove@f; | f[_]=0; | f[a]=1; | f[b]=2; | Clear@x; | h=DownValues@f | Cases[h,f[x_]->x,Infinity] | | Out[52]= | {HoldPattern[f[a]]:>1,HoldPattern[f[ | b]]:>2,HoldPattern[f[_]]:>0} | | Out[53]= | {x} | === Subject: Re: Extracting a Function's Domain and Image > Re Mathematica 5.2.2. > Why does the last line of code return {x} and not {a,b,_}, while if the f[_]=0 is struck, the answer is {a,b}? > Bruce > Remove@f; > f[_]=0; > f[a]=1; > f[b]=2; > Clear@x; > h=DownValues@f > Cases[h,f[x_]->x,Infinity] > Out[52]= > {HoldPattern[f[a]]:>1,HoldPattern[f[ > b]]:>2,HoldPattern[f[_]]:>0} > Out[53]= > {x} Cases[h,HoldPattern[f[x_]]->x,Infinity] --> {a,b,_} ... or did I misunderstand you? Peter === Subject: RE: Efficient compounding of growth figures I found the answer to my own question in a posting on this list from 2001. FoldList[#1(1+#2)&,1, samplelist] MS _____ === Subject: Efficient compounding of growth figures I have a list of periodic percentage changes in a time series. For example, samplelist= {0.018, 0.017, 0.009, 0.022, 0.009, -0.005, 0.027, 0.02, 0.013}; I need to create a new list that shows the effect of compounding these changes over time. In other words, element n of the answer list equals (1+Part[samplelist,1]) * (1+Part[samplelist,2]) * ... * (1+Part[samplelist,n]) The answer, in the case of the example above, is {1.018, 1.03531, 1.04462, 1.06761, 1.07721, 1.07183, 1.10077, 1.12278, 1.13738}. I have created the following function, which gives the correct answer, inserting the starting normalized value 1 at the start of the list. compoundlist[changes_]:=Table[Product[Prepend[1 + changes, 1]j , {j, 1, i}], {i, 1, Length[Prepend[changes, 1]]}] This works perfectly but it is very inefficient. It repeats calculations over and over again that need to be done only once. I would appreciate any counsel on how to do this more efficiently. Michael Stern === Subject: Re: Efficient compounding of growth figures FoldList[Times, 1, 1+samplelist] > I found the answer to my own question in a posting on this list from 2001. > FoldList[#1(1+#2)&,1, samplelist] > MS > _____ === > Subject: Efficient compounding of growth figures > I have a list of periodic percentage changes in a time series. For example, > samplelist= {0.018, 0.017, 0.009, 0.022, 0.009, -0.005, 0.027, 0.02, 0.013}; > I need to create a new list that shows the effect of compounding these > changes over time. In other words, element n of the answer list equals > (1+Part[samplelist,1]) * (1+Part[samplelist,2]) * ... * > (1+Part[samplelist,n]) > The answer, in the case of the example above, is {1.018, 1.03531, 1.04462, > 1.06761, 1.07721, 1.07183, 1.10077, 1.12278, 1.13738}. > I have created the following function, which gives the correct answer, > inserting the starting normalized value 1 at the start of the list. > compoundlist[changes_]:=Table[Product[Prepend[1 + changes, 1]j > , > {j, 1, i}], {i, 1, Length[Prepend[changes, 1]]}] > This works perfectly but it is very inefficient. It repeats calculations > over and over again that need to be done only once. I would appreciate any > counsel on how to do this more efficiently. > Michael Stern === Subject: Norm I want to calculate a distance matrix, similar to (as poorly explained at) http://en.wikipedia.org/wiki/Distance_matrix I found out about the Function Norm in mathematica 5. Here is a little example. I want to calculate the distance between vectors {0,1} and {5,1}. The distance should be 5 Now, Norm[{{0., 1.}, {5., 1.}}, 2] results 5.10293 Norm[{{0., 1.} - {5., 1.}}, 2] results 5.0 According to the documentation I have (Mathematica Help Browser, search for Norm under Built-in Functions) the version with the comma is documented. I like the solution with the dash better. Which one is it? In other words, is there some Wolfram description or can you explain the difference? Claus === Subject: Re: Norm > I want to calculate a distance matrix, similar to (as poorly explained > at) http://en.wikipedia.org/wiki/Distance_matrix > I found out about the Function Norm in mathematica 5. > Here is a little example. I want to calculate the distance between > vectors {0,1} and {5,1}. The distance should be 5 And it is indeed. Let v1 = {0., 1.} and v2 = {5., 1.}. What you want is the length of the vector v1 - v2, which is equal to {-5., 0.}, length that can be computed by taking the square root of the dot product of v1 - v2 by itself (assuming that all the entries are reals): Sqrt[(v1 - v2) . (v1 - v2)] returns 5, as expected. This is equivalent to Norm[v1 - v2] or Norm[v1 - v2, 2]. > Now, > Norm[{{0., 1.}, {5., 1.}}, 2] > results 5.10293 Beware! Here you are computing a *matrix* 2-norm, which has nothing to do with Eucledian distance. The matrix 2-norm is defined as the max of the singular values of the given matrix Max[SingularValueList[{{0., 1.}, {5., 1.}}]] yields 5.10293407795794 which is the same values as the one returned by Norm[{{0., 1.}, {5., 1.}}, 2] Here you are dealing with the two-by-two matrix / ! 0. 1. ! ! ! ! 5. 1. ! / In Mathematica, a list of lists of equal lengths is usually interpreted as a matrix and a list of atomic expressions is interpreted as a vector. > Norm[{{0., 1.} - {5., 1.}}, 2] > results 5.0 Even though you got the correct result, what you really want to write above is a vector, so get rid of the outermost curly braces: Norm[{0., 1.} - {5., 1.}, 2] Now, let's go back to your original problem. To compute the distance matrix of a list of points, say points = {{0., 1.}, {5., 1.}, {3., 4.}, {-1., -5.}}; you could use the generalized outer product as follows Outer[Norm[#1 - #2] & , points, points, 1] returns {{0., 5., 4.242640687119285, 6.082762530298219}, {5., 0., 3.605551275463989, 8.48528137423857}, {4.242640687119285, 3.605551275463989, 0., 9.848857801796104}, {6.082762530298219, 8.48528137423857, 9.848857801796104, 0.}} We can easily check that the resulting matrix is a well formed distance matrix (at least according to the description in wikipedia). MatrixForm[%] Regarding distance matrices, the following web sites might be of interest: http://planetmath.org/encyclopedia/EuclideanDistanceMatrix.html http://www.itl.nist.gov/div898/software/dataplot/refman2/auxillar/matrdist.h tm You could also have a look at Stephen Boyd's and Lieven Vandenberghe's associated web sites at http://www.stanford.edu/~boyd/cvxbook/ where the text is available as well as lectures slides, code programs, etc. HTH, Jean-Marc === Subject: Re: Norm > I want to calculate a distance matrix, similar to (as poorly explained > at) http://en.wikipedia.org/wiki/Distance_matrix > I found out about the Function Norm in mathematica 5. > Here is a little example. I want to calculate the distance between > vectors {0,1} and {5,1}. The distance should be 5 > Now, > Norm[{{0., 1.}, {5., 1.}}, 2] > results 5.10293 > Norm[{{0., 1.} - {5., 1.}}, 2] > results 5.0 > According to the documentation I have (Mathematica Help Browser, > search > for Norm under Built-in Functions) the version with the comma is > documented. I like the solution with the dash better. > Which one is it? In other words, is there some Wolfram description or > can you explain the difference? > Claus Norm[{{0., 1.}, {5., 1.}}, 2] does not compute the distance between the points {0,1} and {5,1} but the 2-norm of the matrix {{0., 1.}, {5., 1.}}. This is defined as the square root of the sum of the squares of the matrix entries, in other words it is Sqrt[0^2+1^2+5^2+1^2]//N 5.19615 This is of course completely different form the distance, which is: Norm[{{0, 1}-{5, 1}}, 2] 5 where - is not the dash but the subtraction sign! Andrzej Kozlowski === Subject: Re: Norm Mathematically, the norm of a vector gives that vector's length. And the distance between two vectors is the norm of the difference between the two vectors. (What you call the dash is in fact a subtraction sign.) So, assuming you want the ordinary (that is, Euclidean) distance, the desired result is given by Norm[{0, 1, 5, 1}] and the result (in InputForm) is 3 Sqrt[3]. The final argument, 2, is superfluous in the case of the ordinary (Euclidean) norm, which is the 2-norm. It would help when doing such things if you were familiar, first, with the underlying mathematical ideas and second, with the documentation that Mathematica itself provides. For the latter, just evaluate ?Norm and then to get further information click the hyperlink in the output produced (or in the first instance look up Norm directly in the HelpBrowser). > I want to calculate a distance matrix, similar to (as poorly explained > at) http://en.wikipedia.org/wiki/Distance_matrix > I found out about the Function Norm in mathematica 5. > Here is a little example. I want to calculate the distance between > vectors {0,1} and {5,1}. The distance should be 5 > Now, > Norm[{{0., 1.}, {5., 1.}}, 2] > results 5.10293 > Norm[{{0., 1.} - {5., 1.}}, 2] > results 5.0 > According to the documentation I have (Mathematica Help Browser, search > for Norm under Built-in Functions) the version with the comma is > documented. I like the solution with the dash better. > Which one is it? In other words, is there some Wolfram description or > can you explain the difference? > Claus -- Murray Eisenberg murray@math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305 === Subject: Re: Norm > I want to calculate a distance matrix, similar to (as poorly explained > at) http://en.wikipedia.org/wiki/Distance_matrix > I found out about the Function Norm in mathematica 5. > Here is a little example. I want to calculate the distance between > vectors {0,1} and {5,1}. The distance should be 5 > Now, > Norm[{{0., 1.}, {5., 1.}}, 2] > results 5.10293 Here you are computing the 2-norm of a matrix. > Norm[{{0., 1.} - {5., 1.}}, 2] > results 5.0 Here you are computing the 2-norm of a vector. The norms of a matrix and a vector are not the same thing. Since you are interested in the distance between two points, you want to compute the magnitude of the difference of the two points, so you want to evaluate the norm of point1 minus point2. Carl Woll Wolfram Research > According to the documentation I have (Mathematica Help Browser, search > for Norm under Built-in Functions) the version with the comma is > documented. I like the solution with the dash better. > Which one is it? In other words, is there some Wolfram description or > can you explain the difference? > Claus === Subject: Re: Norm and why do you think, that Norm[{{0., 1.}, {5., 1.}}, 2] which computes the maximum singular value of {{0., 1.}, {5., 1.}} has something to do with Norm[{{0., 1.} - {5., 1.}}, 2] which compute the Euclidian distance ?? And all this stand in the Help-Browser and in the reference of The Mathematica book ... Jens schrieb im Newsbeitrag | I want to calculate a distance matrix, similar to (as poorly explained | at) http://en.wikipedia.org/wiki/Distance_matrix | | I found out about the Function Norm in mathematica 5. | | Here is a little example. I want to calculate the distance between | vectors {0,1} and {5,1}. The distance should be 5 | | Now, | | Norm[{{0., 1.}, {5., 1.}}, 2] | results 5.10293 | | Norm[{{0., 1.} - {5., 1.}}, 2] | results 5.0 | | According to the documentation I have (Mathematica Help Browser, search | for Norm under Built-in Functions) the version with the comma is | documented. I like the solution with the dash better. | Which one is it? In other words, is there some Wolfram description or | can you explain the difference? | | Claus | === Subject: line thickness 2D plot legend For a 2D Plot in Mathematica: How can I modify the line thickness in the legend of a 2D Plot. The lines in the plot can be made thickner through PlotStyle->{Thickness[0.08]}, but this does not influence the legend. How can I change the line thickness there? === Subject: building a list provided the position of element is known I have two elements, say A and B and their positions in a three dimensional list. Eg {1,3} means that position of A is 1 , position ob is 3 and a list is given {A,_,B}. {3,2} corresponds to {_,B,A}, {3,3} corresponds to {_,_,{A,B}}. How can I code it in Mathematica using any number of elements and a list of any length>0? Thx, Arek === Subject: MapThread and If Of course MapThread[If[#1 == #2, 0, tr] &, {{1, 2, 3}, {2, 2, 4}}] I get {tr, 0, tr} and for MapThread[If[#1 == #2, 0, tr] &, {{1, 2, 3}, {2, 2, {3, 4}}}] I get {tr, 0, If[3 == {3, 4}, 0, tr]} How I can build in above MapThread expression information that if 'a' is equal to any element from the list {a,b,c,d} the result is 0. In above example we have If[3=={3,4},0,tr] what I want to be 0, because 3 is equal to an element belonging to {3,4}. Thx, Arek === Subject: Error message from mathematica Hi all, I am new to Mathematica. Now I am using it to solve a system of nonlinear equations. I got an error message from it: FindRoot::eqns: -- Message text not found -- ({True, <<14>>, varTConnGood == -1.28 + 2.56 (<<1>>)}). I defined the following equation, in which varDelta, varS, and varTConnGood are all variables in the set of equations. (* define an equation for T_conn_good *) eqTConnGood = varTConnGood == ( 1 / varDelta - ( 1 / varDelta + varS ) * ( 1 - varDelta ) ^ varS ) * 2.56 - 1.28 -Ghyan === Subject: Reasonable integration speed? (24 hrs and counting) I downloaded the trial version of Mathematica to see if it could solve a complex integral. After getting comfortable with solving simple integrals, I input my target problem: In[1]:= f[x] = Sqrt[a^2+(q-x)^2] In[2]:= Integrate[Cos[(2*n + 1)*Pi*x/d]* (Exp[-I*k*f[x]]/(4*Pi*f[x]^5)*((1 + I*k*f[x])*(2*f[x]^2 - 3*a^2) + (k*a*f[x])^2) * I*d/(2*Pi* w) + p), {x, -d/2, d/2}] Observations: 1. It's been sitting there for over 24 hours now. 2. Task Manager shows Mathematica's CPU usage at a constant 50%. 3. The Kernel pull-down menu has Interrupt Evaluation grayed out. I can abort it. 4. Mathematica won't solve any subsequent problem in another window (seems like the first one is occupying a queue). Earlier I tested some simple function-reference integrals (e.g. defining f[x] first and then integrating f[x]dx) and they worked. What sort of speed can I expect from this? Is 24 hours too long to solve a problem like this on a 2.6 GHz Windows XP platform with 1.25 using the trial version make any difference? Did I do something wrong? -Alex === Subject: DiscreteDelta and RSolve Bug? difference equation, specifically the unit impulse response of a simple system. For this example I have reduced the system to a first-order as it illustrates the main problem. The following evaluation seems to hang RSolve[{y[n] - 1/2 y[n - 1] == DiscreteDelta[n], y[-1] == 0}, y[n], n] Is there an explanation? Note that the following alternatives work as expected: RSolve[{y[n] - 1/2 y[n - 1] == 0, y[0] == 1}, y[n], n] RSolve[{y[n] - 1/2 y[n - 1] == KroneckerDelta[n], y[-1] == 0}, y[n], n] === Subject: Discrete Event handling in Mathematica Hi! Could somebody tell me if Mathematica can handle discrete events in Ordinary Differential Equations. For example in the following code Clear[[Theta], [Theta]l, tl] [Theta]l = Sin[0.5] tl = 0; Reap[NDSolve[{[Theta]'[t] == Sin[[Theta][t]], [Theta][0] == 0.5}, [Theta], {t, 0, 100}, Method -> {EventLocator, Event -> [Theta]l (t - tl) - 0.01, EventAction :> Sow[{tl = t, [Theta]l = Sin[[Theta][t]]}]}]] I want Mathematica to print out tl and [Theta]l everytime the Event condition is met. Raj === Subject: Re: delayed rule evaluation order >Hold[{2, 3}] /. {{x_, y_} :> x^y} >the result is Hold[Power[2,3]] >I would like the result to be Hold[8] You need to force evaluation of Power before doing Hold, i.e., something like In[15]:= {2,3}/.{{x_,y_}:>x^y}//Evaluate//Hold Out[15]= Hold[8] -- === Subject: Re: LogLogErrorListPlot? >I would like to know if it is possible to make a logarithmic >listplot with error bars. Something like LogLogErrorListPlot or >LogLogMultipleListPlot? Yes, here is an example using LogListPlot < MapThread[ Line@{{#1, Log[10, #2(1 - #3)]}, {#1, Log[10, #2(1 + #3)]}}&, Transpose@data]]; will create the desired plot. If I wanted a LogLogListPlot I would obviously use LogLogListPlot and also change the Epilog directive to change be MapThread[Line@Log[10, {{#1, #2(1 - #3)}, {#1, #2(1 + #3)}}] &,Transpose@data] Multiple plots would be done with multiple statements and combined to one plot using Show. -- === Subject: Re: delayed rule evaluation order Hold[{2, 3}] /. {Hold[{x_, y_}] :> Hold[Evaluate[x^y]]} Hold[8] > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? > -- > http://chris.chiasson.name/ -- hanlonr@cox.net === Subject: Wolfram Workbench Hi This group has been very quiet on the issue of Wolfram Workbench. I've just started dipping my toes into it. One of the matters which bugs me is its built-in integration with CVS ... and then I found http://www-128.ibm.com/developerworks/opensource/library/os-ecl-subversion/? ca=dgr-lnxw01EclipseSubversion which gave good instructions on how to integrate with Subversion. I just followed along, pointed and clicked, and within 10 minutes have WW talking to my repository. Westwood Parallel Programmer === Subject: Re: Extracting a Function's Domain and Image Try this: In[]:= #[[1, 1]] & /@ ReleaseHold[DownValues[f] /. f -> Unique[]] Out[]:= {a,b,_} christoph On Wed, 19 Jul 2006 05:21:10 -0400 (EDT) > Re Mathematica 5.2.2. > Why does the last line of code return {x} and not {a,b,_}, while if the > f[_]=0 is struck, the answer is {a,b}? > I want to extract the domain and image of a user-defined function, and tech > support recommended using DownValues (pretty handy). If you have an easier > Bruce > Remove@f; > f[_]=0; > f[a]=1; > f[b]=2; > Clear@x; > h=DownValues@f > Cases[h,f[x_]->x,Infinity] > Out[52]= > {HoldPattern[f[a]]:>1,HoldPattern[f[ > b]]:>2,HoldPattern[f[_]]:>0} > Out[53]= > {x} -- Mag. Christoph Lhotka -- University of Vienna / Institute for Astronomy fon. +43 (1) 4277 51841 mail. lhotka@astro.univie.ac.at === Subject: Re: InputForm changes the order of output? I am starting to wonder if maybe it is just my system. The errors seem too ... something ... to be part of regular Mathematica. In[1]:= InputForm[$Version] InputForm[q] Out[2]//InputForm= q > Someone, please explain this. > In[1]:= > InputForm[$Version] > Out[2]= > 5.2 for Microsoft Windows (June 20, 2005) > -- > http://chris.chiasson.name/ -- http://chris.chiasson.name/ === Subject: InputForm changes the order of output? Someone, please explain this. In[1]:= InputForm[$Version] z Out[2]= z 5.2 for Microsoft Windows (June 20, 2005) -- http://chris.chiasson.name/ === Subject: Re: delayed rule evaluation order In[]:= Hold[{2, 3}] /. Hold[{x_, y_}] :>f[x^y] /. f -> Hold Out[]:=Hold[8] wkr, christoph On Wed, 19 Jul 2006 05:21:08 -0400 (EDT) > Hold[{2, 3}] /. {{x_, y_} :> x^y} > the result is Hold[Power[2,3]] > I would like the result to be Hold[8] > The original context is post-processing of a large Trace output (via > SetPrecision to get rid of digits and improve readability). > Any ideas? > -- > http://chris.chiasson.name/ -- Mag. Christoph Lhotka -- University of Vienna / Institute for Astronomy fon. +43 (1) 4277 51841 mail. lhotka@astro.univie.ac.at === Subject: Re: Extracting a Function's Domain and Image > Re Mathematica 5.2.2. > Why does the last line of code return {x} and not {a,b,_}, while if the f[_]=0 is struck, the answer is {a,b}? > Bruce > Remove@f; > f[_]=0; > f[a]=1; > f[b]=2; > Clear@x; > h=DownValues@f > Cases[h,f[x_]->x,Infinity] > Out[52]= > {HoldPattern[f[a]]:>1,HoldPattern[f[ > b]]:>2,HoldPattern[f[_]]:>0} > Out[53]= > {x} The problem with Cases[h,f[x_]->x,Infinity] is that f[x] is being evaluated before the rule is used. So, you are actually evaluating Cases[h, 0->x, Infinity] You need to prevent the f from evaluating. The reason things worked as expected when f[_]=0 was not included is because f[x_] didn't turn into 0 in that case, so preventing evaluation of f was not necessary. There are many possible ways of preventing f from evaluating. Here are two: Cases[h, HoldPattern[f][x_]->x, Infinity] Block[{f}, Cases[h, f[x_]->x, Infinity] Carl Woll Wolfram Research === Subject: Re: Extracting a Function's Domain and Image > Re Mathematica 5.2.2. > Why does the last line of code return {x} and not {a,b,_}, while if > the f[_]=0 is struck, the answer is {a,b}? > I want to extract the domain and image of a user-defined function, > and tech support recommended using DownValues (pretty handy). If > Bruce > Remove@f; > f[_]=0; > f[a]=1; > f[b]=2; > Clear@x; > h=DownValues@f > Cases[h,f[x_]->x,Infinity] > Out[52]= > {HoldPattern[f[a]]:>1,HoldPattern[f[ > b]]:>2,HoldPattern[f[_]]:>0} > Out[53]= > {x} Because what happens when you evaluate Cases[h,f[x_]->x,Infinity] is completely different from what you obviously intended. First of all, because of your definition of f, f[x_] is evaluated to 0 and then Cases[h,0->x,Infinity] finds one 0 in the DownValues of f and performs the requested replacement by x. You can avoid this simply by using Cases[h,HoldPattern[f[x_]]->x,Infinity] {a,b,_} Andrzej Kozlowski Karakida,Tokyo,Japan === Subject: Re: Extracting a Function's Domain and Image Remove@f; f[_]=0; f[a]=1; f[b]=2; Clear@x; h=DownValues@f ; Cases[h,HoldPattern[f[x_]]->x,Infinity] {a,b,_} > Re Mathematica 5.2.2. > Why does the last line of code return {x} and not {a,b,_}, while if the f[_]=0 is struck, the answer is {a,b}? > Bruce > Remove@f; > f[_]=0; > f[a]=1; > f[b]=2; > Clear@x; > h=DownValues@f > Cases[h,f[x_]->x,Infinity] > Out[52]= > {HoldPattern[f[a]]:>1,HoldPattern[f[ > b]]:>2,HoldPattern[f[_]]:>0} > Out[53]= > {x} === Subject: Re: Norm >explained at) http://en.wikipedia.org/wiki/Distance_matrix >I found out about the Function Norm in mathematica 5. >Here is a little example. I want to calculate the distance between >vectors {0,1} and {5,1}. The distance should be 5 >Now, >Norm[{{0., 1.}, {5., 1.}}, 2] results 5.10293 >Norm[{{0., 1.} - {5., 1.}}, 2] results 5.0 >According to the documentation I have (Mathematica Help Browser, >search for Norm under Built-in Functions) the version with the >comma is documented. I like the solution with the dash better. Which >one is it? In other words, is there some Wolfram description or can >you explain the difference? Yes, In[10]:= {{0.,1.},{5.,1.}}!={{0.,1.}-{5.,1.}} Out[10]= True Norm[{{0,.1,}-{5.,1.}},2] is exactly the same as Norm[{{5.,0}},2] which is 5. The dash tells Mathematica to do a subtraction then compute the norm. Norm[{{0.,1.},{5.,1.}},2] is the norm of a 2x2 matrix and is not equal to 5. In particular for a matrix, m, Norm[m] is a singular value of m. Also, the default for the Norm function is the 2-norm. That is In[4]:= Norm[{{0,1},{5,1}}]==Norm[{{0,1},{5,1}},2] Out[4]= True and In[9]:= Norm[{{0,1},{5,1}}//N]==First@SingularValueList[{{0,1},{5,1}}//N] Out[9]= True Finally, all of this is documented and can be found using the Help Browser. -- === Subject: Re: WebServices InstallService bug in 5.2 on Max OS X 10.4.7 Matt, Here is a workaround. Needs[WebServices`] Block[{$SystemID=Darwin},InstallService[ http://soap.amazon.com/schemas3/AmazonWebServices.wsdl]] This problem with Mathematica 5.2.0 on 64-bit systems was fixed in version 5.2.2. Bruce Miller Technical Support Wolfram Research, Inc. support@wolfram.com http://support.wolfram.com/ > I believe I may have found a bug in the InstallService function in > the WebServices package for Mathematica 5.2 on Mac OS X 10.4.7 > (PowerPC G5). To demonstrate I used the example for the Amazon web > services in Mathematica Help (see Add-ons Links, Web Services > Package, Examples, Amazon). > The URL for the WSDL is here: > http://confluence.atypon.com/rpc/soap-axis/confluenceservice-v1?wsdl > The error I get is: > XML`Parser`XMLGet::prserr: MalformedURLException: The URL used an > unsupported protocol > I've cut-and-pasted my exact steps below. > In[1]:= > Needs[WebServices`] > In[2]:= > InstallService[http://soap.amazon.com/schemas3/ > AmazonWebServices.wsdl] > =46rom In[2]:= > !(* > RowBox[{(XML`Parser`XMLGet:: > prserr), ((:)( )), URL used an unsupported > protocol !(*ButtonBox[More=85, > ButtonStyle->RefGuideLinkText, ButtonFrame->None, > ButtonData:>XML`Parser`XMLGet::prserr])>}]) > Out[2]= > $Failed > Do other Mac users see this behavior too? Can anyone help me find a > workaround? === Subject: WebServices InstallService bug in 5.2 on Max OS X 10.4.7 I believe I may have found a bug in the InstallService function in the WebServices package for Mathematica 5.2 on Mac OS X 10.4.7 (PowerPC G5). To demonstrate I used the example for the Amazon web services in Mathematica Help (see Add-ons Links, Web Services Package, Examples, Amazon). The URL for the WSDL is here: http://confluence.atypon.com/rpc/soap-axis/confluenceservice-v1?wsdl The error I get is: XML`Parser`XMLGet::prserr: MalformedURLException: The URL used an unsupported protocol I've cut-and-pasted my exact steps below. In[1]:= Needs[WebServices`] In[2]:= InstallService[http://soap.amazon.com/schemas3/AmazonWebServices.wsdl] =46rom In[2]:= !(* RowBox[{(XML`Parser`XMLGet:: prserr), ((:)( )), RefGuideLinkText, ButtonFrame->None, ButtonData:>XML`Parser`XMLGet::prserr])>}]) Out[2]= $Failed Do other Mac users see this behavior too? Can anyone help me find a workaround? === Subject: Re: How do declare real functions? Experimental`CompileEvaluate You might want to look at Compile[], depending on what you're trying to accomplish. (There is also an Experimental`CompileEvaluate[] FYI.) --Urijah Kaplan > I have a related question: is it possible in Mathematica to declare my > function to be strictly real? === Subject: Re: How do declare real functions? (was: NDSolve Problems) Perhaps http://support.wolfram.com/mathematica/kernel/Symbols/System/NDSolve.html will be of some use? One can wrap the function in Re[] to force the function to be real, e.g. In[4]:= f[x_] = Sqrt[x]; freal[(x_)?NumericQ] := Re[f[x]] freal[-1] Out[6]= 0 but Im not sure of the context that you want a strictly real function. James Gilmore > I have a related question: is it possible in Mathematica to declare my > function to be strictly real? === Subject: Re: How do declare real functions? > I have a related question: is it possible in Mathematica to declare my > function to be strictly real? Compare In[1]:= Simplify[Log[Exp[f[x]]]] Out[1]= Log[E^f[x]] with In[2]:= Simplify[Log[E^realf[x]], Element[_realf, Reals]] Out[2]= realf[x] Of course, Element[_realf, Reals] can be used in Assuming[],$Assumptions=... etc. Peter === Subject: RE: delayed rule evaluation order Chris, You could use the ExpressionManipulation package (free) at my web site. It has a routine to evaluate held expressions at given positions or patterns. Needs[Algebra`ExpressionManipulation`] Hold[{2, 3}] /. {{x_, y_} :> x^y} % // EvaluateAtPattern[_Power] Hold[2^3] Hold[8] David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ Hold[{2, 3}] /. {{x_, y_} :> x^y} the result is Hold[Power[2,3]] I would like the result to be Hold[8] The original context is post-processing of a large Trace output (via SetPrecision to get rid of digits and improve readability). Any ideas? -- http://chris.chiasson.name/ === Subject: Reconciling BinCounts and RangeCounts Hello Everyone, I get a discrepancy between the results of BinCounts and RangeCounts and can confirm only that RangeCounts is, in fact, counting the number of instances where a number is at least as big as the lower cut-off and less than the upper cut-off. Not so for BinCounts, which leads me to believe that it is buggy or, more likely, I am. I have a vector, x, with 7320 observations of real numbers in the range .06 to .14 with up to seven decimal places. Here's what I get if I use bins or cut-offs of .01. First with BinCounts BinCounts[x, {.06, .14, .01}] {103, 333, 802, 1266, 997, 662, 611, 2265, 281} Now with RangeCounts RangeCounts[x, Range[.07, .14, .01]] {103, 333, 797, 1270, 997, 663, 611, 2265, 281} Notice that elements 3, 4, and 6 of the results differ. So I tried to check what was going on by using Select and was able to confirm all of the RangeCounts elements. For example, the third element of the RangeCounts results, 797, can be confirmed by using Length[Select[x, .08 =B2 # < .09 &]] >> returns 797 However, the third element of the BinCounts results, 802, can be obtained only if I include the upper bound, .09, in the count as Length[Select[x, .08 =B2 # =B2 .09 &]] >>> returns 802, which of course makes no sense because we need a strong inequality for one of them. But it gets worse. When I go on to check elements 4 and 6 of BinCounts, there is no combination of weak or strong inequalities that will give me the results 1266 and 662. Can anyone shed any light on this? In the meantime, I think it safest to use RangeCounts. Gregory= === Subject: Re: hadamard finite part Below is an implementation with examples based on the algorithm in Computational Integration by Krommer & Ueberhuber, 1998, pp 18-19. I have made the definitions to work with symbolic singular roots, and to use one of the integrators Integrate or NIntegrate. Anton Antonov, Wolfram Research, Inc. Clear[HFP]; HFP[f_, {x_, a_, b_}] := Module[{y}, y = f /. !(((x + y1_. ))^(-1)) -> -y1; Which[ TrueQ[y == a], Log[b - y], TrueQ[y == b], -Log[y - a], True(*a {-y1, -[Alpha]1}; [Alpha] = [Alpha] - 1; Which[ TrueQ[y == a], -1/([Alpha] ((b - y))^[Alpha]), TrueQ[y == b], 1/([Alpha] ((y - a))^[Alpha]), True(*a {f1, -y1, -[Alpha]1}; HFP[f, !(((x - y))^(-[Alpha])), {x, a, b}, int] ]; HFP[f_, d_, {x_, a_, b_}, int_: Integrate] := Module[{y, [Alpha], s, k, t}, {y, [Alpha]} = d /. ((x + y1_.))^[Alpha]1_. -> {-y1, -[Alpha]1}; [Alpha] = [Alpha] - 1; (*Print[{y,[Alpha]}=,{y,[Alpha]}];*) If[[Alpha] <= 0, Return[$Failed]]; k = Ceiling[[Alpha]]; s = Total@ Table[((!([PartialD]_{x, i}f))/(i!) /.[InvisibleSpace]x -> y) HFP[1/(((x - y))^([Alpha] + 1 - i)), {x, a, b}], {i, 0, k}]; t = Total@ Table[((!([PartialD]_{x, i}f))/( i!) /.[InvisibleSpace]x -> y) ((x - y))^i, {i, 0, k}]; int[(f - t)/(((x - y))^([Alpha] + 1)), {x, a, b}] + s ] /; MatchQ[ Integrate); Clear[x, [Lambda]] In[124]:= HFP[1/(x - [Lambda]), {x, [Lambda], 1}] (Out[124]= Log[1 - [Lambda]] In[125]:= HFP[1/(x - [Pi]/4), {x, [Pi]/4, 1}] Out[125]= Log[1 - [Pi]/4] In[126]:= HFP[1/(x - [Lambda]), {x, 0, [Lambda]}] Out[126]= -Log[[Lambda]] In[127]:= HFP[1/(((x - [Lambda]))^([Alpha] + 1)), {x, 0, 1}] Out[127]= -(!(((1 - [Lambda]))^(-[Alpha])) - !([Lambda]^(-[Alpha])))/ [Alpha] In[128]:= HFP[1/(((x - [Pi]/4))^2), {x, 0, 1}] Out[128]= -1/(1 - [Pi]/4) + 4/[Pi] In[129]:= HFP[1/(((x))^2), {x, 0, 1}] (Out[129]= -1 In[130]:= HFP[(1 + x + x^2)/(x^2), {x, 0, 2}] Out[130]= 3/2 + Log[2] Davis & Rabinowitz example, Methods of Numerical Integration, 2nd ed., 1984, pp 190 In[132]:= res = HFP[(!(y^(-2)))/(((((y - 2))^2 + 1))^(1/2)), {y, 0, 1}, Integrate] % // N Out[132]= -1/(Sqrt[5]) + (3 - Sqrt[10] - 2 ArcSinh[3] + Log[100])/(5 Sqrt[5]) Out[133]= -0.375123 In[134]:= res = HFP[(!(y^(-2)))/(((((y - 2))^2 + 1))^(1/2)), {y, 0, 1}, NIntegrate] Out[134]= -0.375123 In[135]:= exact = (2 Sqrt[5])/25 (Log[10 Sqrt[10] - 30] - 1) - (Sqrt[2])/5; In[136]:= res - exact // N Out[136]= -2.53964Ì÷!(10^(-15)) > I work in the field of applied mathematics and I am interested in the > symbolical/numerical integration of integrals in the Hadamard sense > (that is, the finite part of divergent integrals). > My integrals are much more complicated but here I use some trivial > examples to show the point. > [For clarity, I have converted the output expressions to > InputForm] > 5.2 for Microsoft Windows (June 20, 2005) > Consider first the integral, > Integrate[1/x,{x,0,1}] > 1 > Integrate::idiv: Integral of - does not converge on {-1, 2}. > MoreëÓ.89[Hyphen]Âå[Capital IDoubleDot] > x > Integrate[x^(-1), {x, -1, 2}] > which does not exist in the Reimann sense, since it has a > non-integrable singularity at x=0. > However in the Cauchy sense the integral exists, > Integrate[1/x,{x,-1,2},PrincipalValue->True] > Log[2] > N[%] > 0.6931471805599453 > which is equivalent to > Integrate[1/x,{x,-1,-e1},Assumptions00 Log[e1] + Log[2/e2] > PowerExpand[%] > Log[2] + Log[e1] - Log[e2] > and then taking e1=e2 > %/.e1->e2 > Log[2] > Limit[%,e2->0,Direction->-1] > Log[2] > Of course the Mathematica is also able to evaluate numerically the > principal value of > this integral, > Needs[NumericalMath`] > CauchyPrincipalValue[1/x,{x,-1,{0},2}] > NIntegrate::ploss: Numerical integration stopping due to loss of > precision. Achieved neither the requested PrecisionGoal nor > AccuracyGoal; suspect one of the following: highly oscillatory > integrand or the true value of the integral is 0. If your > integrand is oscillatory on a (semi-)infinite interval try using > the option Method->Oscillatory in NIntegrate. MoreëÓ.89[Hyphen]Âå[Capital IDoubleDot] > 0.6931471805602387 > So everything is good up to now. > Next, suppose the integral > Integrate[1/x^2,{x,-1,2}] > -2 > Integrate::idiv: Integral of x does not converge on {-1, 2}. > MoreëÓ.89[Hyphen]Âå[Capital IDoubleDot] > Integrate[x^(-2), {x, -1, 2}] > There is a again non-integrable singularity at x=0. Due to the kind of > the singularity (double pole) the integral does not exist even in the > Cauchy sense. > Integrate[1/x2,{x,-1,-e1},Assumptions->00 -3/2 + e1^(-1) + e2^(-1) > %/.e1->e2 > -3/2 + 2/e2 > Limit[%,e2->0,Direction->-1] > Infinity > Nevertheless, the integral exists in the Hadamard sense and is equal to > finite part of the previous result > %%%-( e1^(-1) + e2^(-1)) > -3/2 > Mathematica is able to provide previous result: > Integrate[1/x^2,{x,-1,2},GenerateConditions->False] > -3/2 > My first question now: > Is it a way to get the finite part of a divergent integral through > performing > numerical integration (e.g. using NIntegrate) in Mathematica? > I have seen some papers presenting some propper algorithms dealing with > numerical integration of Hadamard finite part integrals but I cannot > find any related work in connection with Mathematica. > There are also some other questions. > Consider the integral, > Integrate[1/x,{x,0,2}] > 1 > Integrate::idiv: Integral of - does not converge on {0, 2}. MoreëÓ.89[Hyphen]Âå[Capital IDoubleDot] > x > Integrate[x^(-1), {x, 0, 2}] > The integral does not exist neither in the Reimann sense nor in the > Cauchy > sense. However it does exist in the Hadamard sense and is equal to the > finite part of > Integrate[1/x,{x,e,2},Assumptions->0 Log[2] - Log[e] > %-Log[e] > Log[2] > Mathematica 3.0 and 4.0 suceeds in providing this result: > Integrate[1/x,{x,0,2},GenerateConditions->False] (*version 3.0 and > 4.0*) > Log[2] > However Mathematica 5.1 and 5.2 gives the result > Integrate[1/x,{x,0,2},GenerateConditions->False] (*version 5.1 and > 5.2*) > Infinity > Why exists this difference? > I can trust that for divergent integrals > Integrate[integrand,{x,a,b},GenerateConditions->False] > provides the desirable result in the Hadamard sense? > Is it a way to get Integrate to give always the finite part of a > divergent integral? > Are there any other alternative methods (such as the implementation of > aymptotic techniques in mathematica) to get the finite part of a > divergent integral? > Is there any book that connect Mathematica with distribution theory? > I really appreciate any assistance. > P.S. The finite part of a divergent integral is of great importance in > the area of applied mathematics. For example, the vertical displacement > at the surface of an elastic linear isotropic half space acted upon by > an applied line load (the so called Flamant problem) is given by the > Fourier cosine transform of the function 1/ë.b2, which does not exist in > the Reimann sense (the integral has a non integrable singularity at > ë.b2=0). Indeed, > Series[Cos[j]/j,{j,0,3}]//Normal > !(TraditionalForm`x^3/24 - x/2 + 1/x) > However in the Hadamard sense it does exist and is equal to > Cancel[PowerExpand[Integrate[(1/ëõë[CapitalU Acute])Cos[ > ëõëÚ x],{ëõëÚ,0,ë[Capi talOAcute]åÃåõ},GenerateConditions[Rul e]False]]] > -EulerGamma - Log[x] > or > FullSimplify[Sqrt[Pi/2]FourierCosTransform[1/j,j,x],x>0] > -EulerGamma - Log[x] > This is a common practice to elasticity problems and the loss of > infinity correcponds to rigid-body displacement (cf. e.g. Horacio A. > Sosa and Leon Y. Bahar: Transition from airy stress function to state > space formulation of elasticity Journal of the Franklin Institute, > Volume 329, Issue 5, September 1992, Pages 817-828) === Subject: Re: hadamard finite part not quite as general as you may need it, but Hadamard integration is used in Quantum Field Theory a lot since a long time and implemented e.g. in FeynCalc ( for Feynman integrals), see: http://www.feyncalc.org/FeynCalcBook/Integrate2 http://www.feyncalc.org/FeynCalcBook/Integrate3 http://www.feyncalc.org/FeynCalcBook/PlusDistribution All this is rather basically programmed as a smart table-lookup. Just look at the source code. I tried once to implement more of distribution theory, but it is tricky and I found Mathematica extremely well-suited for table-lookup-algorithm-implementation. I.e., you work out what you want, calculate the basic building blocks once (by hand etc.) and let Mathematica just do the algebra but not the complicated mathematics. Rolf Mertig GluonVision GmbH, Berlin, Mathematica training & consulting http://www.gluonvision.com