A68 ==== Subject: Re: How to suppress plot output ? plot1=Plot[x,{x,0,1},DisplayFunction->Identity]; plot2=Plot[2x,{x,0,1},DisplayFunction->Identity]; plot3=Plot[3x,{x,0,1},DisplayFunction->Identity]; plot4=Plot[4x,{x,0,1},DisplayFunction->Identity]; Show[{plot1,plot2,plot3,plot4}, DisplayFunction->$DisplayFunction]; Alternatively, you can use DisplayTogether Needs[Graphics`]; DisplayTogether[ Plot[x,{x,0,1}], Plot[2x,{x,0,1}], Plot[3x,{x,0,1}], Plot[4x,{x,0,1}]]; ==== > Subject: How to suppress plot output ? > I am having difficulty forcing a suppression of output for mathematica > commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; > plot2=Plot[ ... ]; > plot3=Plot[ ... ]; > plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do > I suppress the first four? Paul ==== Subject: Re: How to suppress plot output ? Hi Paul, > I am having difficulty forcing a suppression of output for > mathematica commands when writing documents. I want to show > a multi-part graph: plot1=Plot[ ... ]; > plot2=Plot[ ... ]; > plot3=Plot[ ... ]; > plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. > How do I suppress the first four? Shouldn't be difficult .... Did you try plot_i = Plot[ ...., DisplayFunction->Identity]; For any for the first four? You can also try DisplayTogether[ {Plot[], Plot[]}, ]; Dave. ==== Subject: Re: how to find n in expression x^n using a pattern? > I am learning patterns in Mathemtica, and I thought this will be easy. suppose you are given a factored expression, such as x^n(x^2+2)(x^3+4) > where 'n' above can be any number, including zero (i.e. there is no > (x) as a separate factor). I need to find 'n', which can be > 0,1,2,3,..etc.. i.e I need to find the power of the factor x by itself if it exist in > the expression. not (x^n+anything), just (x^n) byitself. For example p= x (x^2+2) ---> here n=1 I tried this p /. x^n_(any_) -> n This did not work since x^1 does not match pattern x^n_. I would have > worked if I had x^2 (x^3+2) for example. I know that I need to use something like x^_:1 to make it match x, > which > is really x^1, but I do not know how to use it in a replacement rule, > becuase > when I write p /. (x^n_:1)(any_) -> n it is an error. If I write p /. (x^_:1)(any_) -> _ I do not get back anything. Next I tried the Cases command, and again had no luck. The main problem is that Mathematica makes a difference between > x, x^1, and x^2, etc... when it comes to matching with pattern x^n_ any idea how to do this? > steve Actually there are many problems with your approach. First of all, your Attempt to use optional fails because of syntax. You shoudl have had x^(n_:1) or x^Optional[Pattern[n, Blank[]], 1]. But in any case the result what have been quite different form what I think you expected: p= x (x^2+2); p/.x^(n_:1)->n 4 Of course what happened was that x was replaced by 1 and x^2 by 2. This is not the way to find the power of the factor x by itself. Perhaps what you would have preferred is something like: Cases[x (x^2+2) , x^(n_:1)->n,{1,2}] {1,2} Note however that you have to be careful about the levels here. There is a x on Level 1 of the expression and x^2 at level 2. In fact level 1 looks like this: Level[p, {1}] {x, x^2 + 2} and level 2 like: Level[p, {2}] {2, x^2} But there is also x at level 3: Level[p, {3}] {x,2} Which means that if you uses Cases with the commonly used level setting Infinity you would get a misleading answer: = Cases[x (x^2+2) , x^(n_:1)->n,Infinity] {1,1,2} You do not tell us whether you are simply learning to use patterns or trying t solve some problem in polynomial algebra. for the latter task patterns are generally not the right tool to use, instead one should use GroebnerBasis and PolynomialReduce. But there is not much point in trying to answer questions nobody asked asked ;-) Andrzej Kozlowski Chiba, JAPAN ==== Subject: Re: how to find n in expression x^n using a pattern? p=x (x^2+2); Make the power Optional p/.x^Optional[n_](any_)->n 1 This is abbreviated as ( n_. ) p/.x^n_.(any_)->n 1 Bob Hanlon > ==== > Subject: how to find n in expression x^n using a pattern? I am learning patterns in Mathemtica, and I thought this will be easy. suppose you are given a factored expression, such as x^n(x^2+2)(x^3+4) > where 'n' above can be any number, including zero (i.e. there is no > (x) as a separate factor). I need to find 'n', which can be > 0,1,2,3,..etc.. i.e I need to find the power of the factor x by itself if it exist in > the expression. not (x^n+anything), just (x^n) byitself. For example p= x (x^2+2) ---> here n=1 I tried this p /. x^n_(any_) -> n This did not work since x^1 does not match pattern x^n_. I would have > worked if I had x^2 (x^3+2) for example. I know that I need to use something like x^_:1 to make it match x, > which > is really x^1, but I do not know how to use it in a replacement rule, > becuase > when I write p /. (x^n_:1)(any_) -> n it is an error. If I write p /. (x^_:1)(any_) -> _ I do not get back anything. Next I tried the Cases command, and again had no luck. The main problem is that Mathematica makes a difference between > x, x^1, and x^2, etc... when it comes to matching with pattern x^n_ any idea how to do this? > steve ==== Subject: Re: Can't assign value to symbols Instead of ToExpression use ToExpression[#, StandardForm, Unevaluated] & For example: In[1]:= parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; In[2]:= MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated]& /@ parameters[[1]], parameters[[3]]}] ; In[3]:= a Out[3]= 4 In[4]:= MapThread[(#1 = #2) & , {ToExpression[#,StandardForm,Unevaluated]& /@ parameters[[1]], parameters[[2]]}] ; In[5]:= a Out[5]= 1 Of course instead of Standardform you can use TraditionalForm or even InputForm. Andrzej Kozlowski Chiba, Japan > now realize there is one additional problem. Once I've executed the > statement MapThread[(#1 = #2) & , {parameters[[1]], parameters > [[3]]}] , > because I've specified the first row of parameters as symbols, this > row now loses the symbol names and takes on the assigned values, such > that my attempt to execute the MapThread Statement again for a > different > set of values, e.g. parameters[[2]], fails. in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; > in: {a,b,c} > Out: {4,5,6} > in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; > out: Set:: setraw : Cannot assign to raw object 4 ... etc I want to preserve the first row of parameters as the symbol > names, so > one solution is to use strings, and modify David Park's solution to > turn > the strings into symbol names when assigned, This works the first time > it is run, and parameters[[1]] still contains the strings, but ut > still > fails the second time. but why? > in: parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; > in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], > parameters[[3]]}] ; > in: {a,b,c} > out {4,5,6} > in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], > parameters[[3]]}] ; > out: Set:: setraw : Cannot assign to raw object 4 ... etc why does the MapThread statement fail the second time? It seems > ToExpression/@parameters[[1]] converts the strings a, b, c to > symbols but then evaluates them prior to returning them, so the > attempt > to assign to them in MapThread fails. How can I modify the MapThread statement so I can call it multiple > times, each time assigning values to the symbols identified by the > strings in the first row?? Lee >> Lee, >> Set (=) has a higher precedence Than Function (&). So all you have >> to do is >> add parentheses. >> parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >> MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] >> {4, 5, 6} >> David Park >> djmp@earthlink.net >> http://home.earthlink.net/~djmp/ >> Situation: I have a table that contains parameters and sets of >> values >> that I want to assign to the parameters for the purposes of running a >> simulation. >> - parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} >> - want to assign values in a given row to the symbols listed in the >> first row. >> - tried using: MapThread[ #1 = #2 &, {parameters[[1]], parameters >> [[3]]} ] >> - fails with error Tag Slot in #1 is Protected >> - tried adding Unprotect[#1] and a variety of other attemps, but >> can't >> get it to work. >> Anyone know how might accomplish this? >> Lee > > ==== Subject: Re: How to suppress plot output ? Paul, First method: Use the DisplayFunction -> Identity option to suppress output and use DisplayFunction -> $DisplayFunction to turn it back on. plot1 = Plot[Sin[x], {x, 0, 2Pi}, DisplayFunction -> Identity]; plot2 = Plot[Cos[x], {x, 0, 2Pi}, DisplayFunction -> Identity]; Show[plot1, plot2, DisplayFunction -> $DisplayFunction]; Second method: Use the Block construct to temporarily reset $DisplayFunction. Block[{$DisplayFunction = Identity}, plot1 = Plot[Sin[x], {x, 0, 2Pi}]; plot2 = Plot[Cos[x], {x, 0, 2Pi}];] Show[plot1, plot2]; Third method: Use the DisplayTogether command. Needs[Graphics`Graphics`] DisplayTogether[ Plot[Sin[x], {x, 0, 2Pi}], Plot[Cos[x], {x, 0, 2Pi}]]; However, these constructs can get a little confusing if you want to add other elements to your plots, such as Lines, Points or Text statements to label the curves. In that case you may want to use the fourth method, which uses the DrawGraphics package at my web site. Needs[DrawGraphics`DrawingMaster`] Draw2D[ {Draw[Sin[x], {x, 0, 2Pi}], Draw[Cos[x], {x, 0, 2Pi}], Text[Sin, {Pi/2, Sin[Pi/2]}, {-2.5, 0}], Text[Cos, {1.2, Cos[1.2]}, {-1.5, 0}]}, Axes -> True, PlotRange -> All]; Also, for any of the methods, if you want to use the plot in a document you can close the input cell that contains all the graphics code. Select the Input cell bracket on the right hand side and use Alt CRC. Or use Menu/Cell/Cell Properties/Cell Open. That will close the cell so only a thin blank space, with the right hand bracket remains. You can still select and evaluate the cell so your readers can see the graphics but not the code. If you pass the notebook to someone else you will probably have to remind them to evaluate the cells because many people are not aware of this feature and overlook the thin cells. (This is not the same as Open/Close Group.) David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ I am having difficulty forcing a suppression of output for mathematica commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; plot2=Plot[ ... ]; plot3=Plot[ ... ]; plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do I suppress the first four? Paul ==== Subject: Re: a question about the UnitStep function >I want to let Mathematica compute the convolution of two sqare waves. I did as follows >>f[x_]:=(UnitStep[x+1]-UnitStep[x-1])/2; >>integrand=f[z] f[x-z]; >>Assuming[Element[x, Reals], Integrate[integrand, {z, -Infinity, Infinity}]] >>Mathematica gave me the result as follows, >>((-1 + x) UnitStep[-1 + x] - x UnitStep[x] + (2 + x) UnitStep[2 + x])/4 >>I plot the result to check >>Plot[%,{x,-10,10}, PlotRange->All]; >>It is clear wrong since the convolution of two square waves should be convergent. Can anyone give me some help with the subtlties about the UnitStep function? Any thoughts are appriciable. >> >Try this, >f[x_]:=(UnitStep[x+1]-UnitStep[x-1])/2; >integrand1=f[x] f[x-1] >d[[Omega]_]=FourierTransform[integrand1,x,[Omega]]//ExpToTrig//Simplify >g[x_]=InverseFourierTransform[Evaluate[d[[Omega]]],[Omega],x] >DisplayTogether[Plot[f[x],{x,-10,10}],Plot[f[x-1],{x,-10,10}]] >Plot[g[x],{x,-10,10}] Hope this is what your are looking for > Hello all, After seeing other posts, I realize my post was obviously wrong. I sincerely apologize for that. Although my approach using fourier transform should work for example if I change my approach as shown below, I get a triangular wave but not what everybody else is getting Clear[f,d,d1,h,z] f[x_]:=(UnitStep[x+1]-UnitStep[x-1])/2; d[[Omega]_]=FourierTransform[f[x],x,[Omega]] d1[[Omega]_]=FourierTransform[f[x-1],x,[Omega]] h[x_]=InverseFourierTransform[d[[Omega]]*d1[[Omega]]//FullSimplify,[Om ega], x] Plot[h[x],{x,-10,10}] Pratik -- Pratik Desai Graduate Student UMBC Department of Mechanical Engineering Phone: 410 455 8134 ==== Subject: Re: Integrate and Boole problems >>I'm using Mathematica 5.1 and I'm getting some inconsistent results >>when integrating the Boole function. For example: >>Integrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)*((x + 1)^2 + y^2 + z^2 - >>3^2) = 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, >>-Infinity, Infinity}]; >>N[%] >>50.2654821239234 + 3.829918318813164*^-9*I >>NIntegrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)*((x + 1)^2 + y^2 + z^2 >>-3^2) = 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, >>-Infinity, Infinity}] >>108.90854533339399 - 2.749275252793041*^-29*I >>Am I doing something wrong or is this just a limit of the Integrate >>function? >> > I assume that Boole[... = 0] is actually Boole[... <= 0]. In version 5.1.0 > Integrate just returns the outer integral (with respect to x) unevaluated. > NIntegrate apparently uses the method SymbolicPiecewiseSubdivision and it > works fine. On the other hand, Mathematica often fails on symbolic > integration of the functions of the type Sqrt[r^2 - x^2 - y^2 - z^2]. So > if there is an error, most likely it is not related to Boole or other > piecewise functions. Here it is easy to find the exact value of the integral. We're just > looking for the volume of the union of two spheres with two spherical > segments removed. The volume of a spherical segment with radius R and > height h is In[1]:= > Integrate[r^2*Sin[theta], > {r, R - h, R}, {theta, 0, ArcCos[(R - h)/r]}, {phi, 0, 2*Pi}] Out[1]= (-(1/3))*h^2*Pi*(h - 3*R) And substituting the numerical values, we obtain In[2]:= 2*(4*Pi*R^3/3 -2*%) /. {R -> 3, h -> 2} Out[2]= (104*Pi)/3 Owing to the piecewise handling routines, NIntegrate gave a good > approximation. Maxim Rytin > m.r@inbox.ru > It works well, when integrating w.r.t. x first: Integrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)* ((x + 1)^2 + y^2 + z^2 - 3^2) <= 0], {z, -Infinity, Infinity}, {y, -Infinity, Infinity}, {x, -Infinity, Infinity}] (104*Pi)/3 -- ==== Subject: Re: Integrate and Boole problems > I'm sorry, it must have pasted incorrectly, it wasn't an equal sign, it was > a less than or equal. > > Integrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)*((x + 1)^2 + y^2 + z^2 - 3^2) > <= 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, - > Infinity, Infinity}] > > > Mathematica 5.1 is able to integrate over Boolean defined regions, so in > effect if you can define a region with inequalities, integrating over it > will return it's area (if two dimensional) or volume (as is the case above). > More about it can be found here: > > http://www.wolfram.com/products/mathematica/newin51/integrationregions.html > > Chris > Hi Chris, Now I understand what you are doing :-) Unfortunately, with the correct inequality I get the same result as yours: there is a discrepancy of roughly a factor two between the numerical evaluation of the symbolic solution and the direct numerical integration. More puzzling, I have had the good (bad?) idea to use the *Timing* function and the symbolic expression returned by *Integrate* is _not_ the same that the expression return by the command use alone (although the numerical evaluation of both expression yields the same value). So I am wondering why, or in which way, the *Timing* command would interfere with the algorithms used by *Integrate* while *Timing* is suppose just to keep tract of the consumed CPU time? First, without the timing In[1]:= Integrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)* ((x + 1)^2 + y^2 + z^2 - 3^2) <= 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, -Infinity, Infinity}] Out[1]= (56*Pi)/3 - (4/135)*(315*Pi - 2*(1008*EllipticE[-(1/2)] - I*(39*Sqrt[6]*EllipticE[2/3] - 78*EllipticE[3/2] - 417*I*EllipticK[-(1/2)] - 13*Sqrt[6]*EllipticK[2/3] + 900*Sqrt[2]* EllipticPi[-1, (1/2)*I*Log[2 + Sqrt[3]], -2] - 315*Sqrt[2]*EllipticPi[2, (1/2)*I*Log[2 + Sqrt[3]], -2]))) + (2/135)*(585*Pi - 2*(1008*EllipticE[-(1/2)] - I*(39*Sqrt[6]*EllipticE[2/3] - 78*EllipticE[3/2] - 417*I*EllipticK[-(1/2)] - 13*Sqrt[6]*EllipticK[2/3] + 900*Sqrt[2]* EllipticPi[-1, (1/2)*I*Log[2 + Sqrt[3]], -2] - 315*Sqrt[2]*EllipticPi[2, (1/2)*I*Log[2 + Sqrt[3]], -2]))) + 2*Integrate[(1/2)*Pi*(-2 + x)*(4 + x) + 2*Sqrt[(-(-2 + x))*x*(4 + x)] - (-4 + x)*(2 + x)*ArcCot[2/Sqrt[-2 + 8/x - x]], {x, 0, 2}] + 2*Integrate[(1/2)*((-Pi)*(-4 + x)*(2 + x) - 4*Sqrt[(-(-2 + x))*x*(4 + x)] + 2*(-4 + x)*(2 + x)*ArcCot[ 2/Sqrt[-2 + 8/x - x]]), {x, 0, 2}] + Integrate[(1/2)*(-4*Sqrt[(-4 + x)*x*(2 + x)] - Pi*(-2 + x)*(4 + x) - 2*(-2 + x)*(4 + x)* ArcTan[Sqrt[(-4 + x)*x*(2 + x)]/(2*x)]), {x, -2, 0}] In[2]:= N[%] Out[2]= 55.04761032732088 + 5.3618391912432685*^-8*I In[3]:= Chop[%] Out[3]= 55.04761032732088 + 5.3618391912432685*^-8*I In[4]:= NIntegrate[Boole[((x - 1)^2 + y^2 + z^2 - 3^2)* ((x + 1)^2 + y^2 + z^2 - 3^2) <= 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, -Infinity, Infinity}] Out[4]= 108.90854533339399 - 2.749275252793041*^-29*I In[5]:= Chop[%] Out[5]= 108.90854533339399 Now with the *Timing* function In[1]:= Timing[sol = Integrate[ Boole[((x - 1)^2 + y^2 + z^2 - 3^2)* ((x + 1)^2 + y^2 + z^2 - 3^2) <= 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, -Infinity, Infinity}]] Out[1]= {646.094*Second, (56*Pi)/3 - (2/45)*(315*Pi - 2*(1008*EllipticE[-(1/2)] - I*(39*Sqrt[6]*EllipticE[2/3] - 78*EllipticE[3/2] - 417*I*EllipticK[-(1/2)] - 13*Sqrt[6]*EllipticK[2/3] + 900*Sqrt[2]* EllipticPi[-1, (1/2)*I*Log[2 + Sqrt[3]], -2] - 315*Sqrt[2]*EllipticPi[2, (1/2)*I*Log[2 + Sqrt[3]], -2]))) + (2/45)*(585*Pi - 2*(1008*EllipticE[-(1/2)] - I*(39*Sqrt[6]*EllipticE[2/3] - 78*EllipticE[3/2] - 417*I*EllipticK[-(1/2)] - 13*Sqrt[6]*EllipticK[2/3] + 900*Sqrt[2]* EllipticPi[-1, (1/2)*I*Log[2 + Sqrt[3]], -2] - 315*Sqrt[2]*EllipticPi[2, (1/2)*I*Log[2 + Sqrt[3]], -2]))) + (1/135)*(-630*Pi - 4*I*Sqrt[2]* (39*Sqrt[3]*EllipticE[2/3] - 39*Sqrt[3]* EllipticE[ArcCsc[Sqrt[2/3]], 2/3] + 13*Sqrt[3]*EllipticF[(1/2)*(Pi + I*(Log[2] - 2*Log[1 + Sqrt[3]])), 2/3] - 663*EllipticF[I*(-(Log[2]/2) + Log[1 + Sqrt[3]]), -2] - 13*Sqrt[3]* EllipticK[2/3] + 900*EllipticPi[-1, I*(-(Log[2]/2) + Log[1 + Sqrt[3]]), -2] - 315*EllipticPi[2, I*(-(Log[2]/2) + Log[1 + Sqrt[3]]), -2]) + 405*Sqrt[6]*Pi^(3/2)* (-((2*Hypergeometric2F1[-(5/4), -(1/4), 1/2, 1/9])/(Gamma[-(3/4)]*Gamma[9/4])) + Hypergeometric2F1[-(3/4), 1/4, 3/2, 1/9]/ (Gamma[-(1/4)]*Gamma[7/4]))) + Integrate[(1/2)*(-4*Sqrt[(-4 + x)*x*(2 + x)] - Pi*(-2 + x)*(4 + x) - 2*(-2 + x)*(4 + x)* ArcTan[Sqrt[(-4 + x)*x*(2 + x)]/(2*x)]), {x, -2, 0}]} In[2]:= Timing[N[sol]] Out[2]= {6.719000000000051*Second, 53.2858037103929 + 3.8298848235073785*^-9*I} In[3]:= Timing[numsol = NIntegrate[ Boole[((x - 1)^2 + y^2 + z^2 - 3^2)* ((x + 1)^2 + y^2 + z^2 - 3^2) <= 0], {x, -Infinity, Infinity}, {y, -Infinity, Infinity}, {z, -Infinity, Infinity}]] Out[3]= {6.467999999999961*Second, 108.90854533339399 - 2.749275252793041*^-29*I} In[4]:= Chop[numsol] Out[4]= 108.90854533339399 In[6]:= $Version Out[6]= /J.M. ==== Subject: Re: how to find n in expression x^n using a pattern? Hi Steve, I think this can help you: In[1]:= p = x^n (x^2+2) (x^3+4) ; In[2]:= Exponent[p/.Power[x_,n_Integer]+__ ->1, x] Out[2]= n ~Scout~ steve ha scritto nel messaggio >I am learning patterns in Mathemtica, and I thought this will be easy. suppose you are given a factored expression, such as x^n(x^2+2)(x^3+4) > where 'n' above can be any number, including zero (i.e. there is no > (x) as a separate factor). I need to find 'n', which can be > 0,1,2,3,..etc.. i.e I need to find the power of the factor x by itself if it exist in > the expression. not (x^n+anything), just (x^n) byitself. For example p= x (x^2+2) ---> here n=1 I tried this p /. x^n_(any_) -> n This did not work since x^1 does not match pattern x^n_. I would have > worked if I had x^2 (x^3+2) for example. I know that I need to use something like x^_:1 to make it match x, > which > is really x^1, but I do not know how to use it in a replacement rule, > becuase > when I write p /. (x^n_:1)(any_) -> n it is an error. If I write p /. (x^_:1)(any_) -> _ I do not get back anything. Next I tried the Cases command, and again had no luck. The main problem is that Mathematica makes a difference between > x, x^1, and x^2, etc... when it comes to matching with pattern x^n_ any idea how to do this? > steve > ==== Subject: Re: how to find n in expression x^n using a pattern? Steve, You will probably get a better answer, but I wouldn't use a pattern. I would use CoefficientList and the Position of the first nonzero entry. You don't really have to Factor. expr1 = x^5(x^2 + 2)(x^3 + 4); expr2 = (x^2 + 2)(x^3 + 4); minexponent[poly_, var_:x] := First@First@Position[CoefficientList[poly, var], _?(# != 0 &), {1}, 1] - 1 minexponent[expr1] 5 minexponent[expr2] 0 David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ I am learning patterns in Mathemtica, and I thought this will be easy. suppose you are given a factored expression, such as x^n(x^2+2)(x^3+4) where 'n' above can be any number, including zero (i.e. there is no (x) as a separate factor). I need to find 'n', which can be 0,1,2,3,..etc.. i.e I need to find the power of the factor x by itself if it exist in the expression. not (x^n+anything), just (x^n) byitself. For example p= x (x^2+2) ---> here n=1 I tried this p /. x^n_(any_) -> n This did not work since x^1 does not match pattern x^n_. I would have worked if I had x^2 (x^3+2) for example. I know that I need to use something like x^_:1 to make it match x, which is really x^1, but I do not know how to use it in a replacement rule, becuase when I write p /. (x^n_:1)(any_) -> n it is an error. If I write p /. (x^_:1)(any_) -> _ I do not get back anything. Next I tried the Cases command, and again had no luck. The main problem is that Mathematica makes a difference between x, x^1, and x^2, etc... when it comes to matching with pattern x^n_ any idea how to do this? steve ==== Subject: Re: Can't assign value to symbols > now realize there is one additional problem. Once I've executed the > statement MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] , > because I've specified the first row of parameters as symbols, this > row now loses the symbol names and takes on the assigned values, such > that my attempt to execute the MapThread Statement again for a different > set of values, e.g. parameters[[2]], fails. in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; > in: {a,b,c} > Out: {4,5,6} > in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; > out: Set:: setraw : Cannot assign to raw object 4 ... etc I want to preserve the first row of parameters as the symbol names, so > one solution is to use strings, and modify David Park's solution to turn > the strings into symbol names when assigned, This works the first time > it is run, and parameters[[1]] still contains the strings, but ut still > fails the second time. but why? > in: parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; > in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], > parameters[[3]]}] ; > in: {a,b,c} > out {4,5,6} > in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], > parameters[[3]]}] ; > out: Set:: setraw : Cannot assign to raw object 4 ... etc why does the MapThread statement fail the second time? It seems > ToExpression/@parameters[[1]] converts the strings a, b, c to > symbols but then evaluates them prior to returning them, so the attempt > to assign to them in MapThread fails. How can I modify the MapThread statement so I can call it multiple > times, each time assigning values to the symbols identified by the > strings in the first row?? Lee >>Lee, >>Set (=) has a higher precedence Than Function (&). So all you have to do is >>add parentheses. >>parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; >>MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] >>{4, 5, 6} >>David Park >>djmp@earthlink.net >>http://home.earthlink.net/~djmp/ >>Situation: I have a table that contains parameters and sets of values >>that I want to assign to the parameters for the purposes of running a >>simulation. >>- parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} >>- want to assign values in a given row to the symbols listed in the >>first row. >>- tried using: MapThread[ #1 = #2 &, {parameters[[1]], parameters[[3]]} ] >>- fails with error Tag Slot in #1 is Protected >>- tried adding Unprotect[#1] and a variety of other attemps, but can't >>get it to work. >>Anyone know how might accomplish this? >>Lee > Hi Lee, use Clear to clear the assigned values: In[1]:= parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}]; {a, b, c} Out[3]= {4, 5, 6} In[4]:= Clear[a, b, c]; MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}]; {a, b, c} Out[6]= {1, 2, 3} -- ==== Subject: Re: Can't assign value to symbols Lee, You should then probably go with a solution that generates a set of replacement rules. Use Rule instead of Set and save the set of rules. I'm always advising users to not set values for single character symbols because they are too valuable as symbols - but I forgot to think of that in this case. parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; rules[n_] := MapThread[(#1 -> #2) & , {parameters[[1]], parameters[[n]]}] rules[2] {a -> 1, b -> 2, c -> 3} a b c^2 % /. rules[2] a*b*c^2 18 a b c^2; % /. rules[3] 720 a b c^2; % /. rules[4] 4536 David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ now realize there is one additional problem. Once I've executed the statement MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] , because I've specified the first row of parameters as symbols, this row now loses the symbol names and takes on the assigned values, such that my attempt to execute the MapThread Statement again for a different set of values, e.g. parameters[[2]], fails. in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ; in: {a,b,c} Out: {4,5,6} in: MapThread[(#1 = #2) & , {parameters[[1]], parameters[[2]]}] ; out: Set:: setraw : Cannot assign to raw object 4 ... etc I want to preserve the first row of parameters as the symbol names, so one solution is to use strings, and modify David Park's solution to turn the strings into symbol names when assigned, This works the first time it is run, and parameters[[1]] still contains the strings, but ut still fails the second time. but why? in: parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], parameters[[3]]}] ; in: {a,b,c} out {4,5,6} in: MapThread[(#1 = #2) & , {ToExpression /@ parameters[[1]], parameters[[3]]}] ; out: Set:: setraw : Cannot assign to raw object 4 ... etc why does the MapThread statement fail the second time? It seems ToExpression/@parameters[[1]] converts the strings a, b, c to symbols but then evaluates them prior to returning them, so the attempt to assign to them in MapThread fails. How can I modify the MapThread statement so I can call it multiple times, each time assigning values to the symbols identified by the strings in the first row?? Lee > Lee, Set (=) has a higher precedence Than Function (&). So all you have to do is > add parentheses. parameters = {{a, b, c}, {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] > {4, 5, 6} David Park > djmp@earthlink.net > http://home.earthlink.net/~djmp/ > Situation: I have a table that contains parameters and sets of values > that I want to assign to the parameters for the purposes of running a > simulation. > - parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}} > - want to assign values in a given row to the symbols listed in the > first row. > - tried using: MapThread[ #1 = #2 &, {parameters[[1]], parameters[[3]]} ] > - fails with error Tag Slot in #1 is Protected > - tried adding Unprotect[#1] and a variety of other attemps, but can't > get it to work. Anyone know how might accomplish this? Lee ==== Subject: Re: How to suppress plot output ? >I am having difficulty forcing a suppression of output for >mathematica commands when writing documents. I want to show a >multi-part graph: >plot1=Plot[ ... ]; >plot2=Plot[ ... ]; >plot3=Plot[ ... ]; >plot4=Plot[ ... ]; >Show[ plot1, plot2, plot3, plot4 ]; >In the above, I get four individual plots and one final plot. How >do I suppress the first four? Set the DisplayFunction to Identity. You can do this either by including the option DisplayFunction->Identity as an arguement for each of the Plot statements. Then the Show statement needs to become Show[ plot1, plot2, plot3, plot4, DisplayFunction->$DisplayFunction ]; An alternative would be to use Block to temporarily set $DisplayFunction to Identity, i.e., Block[{$DisplayFunction=Identity}, { plot1=Plot[ ... ], plot2=Plot[ ... ], plot3=Plot[ ... ], plot4=Plot[ ... ] }]; Then you can combine the plots with Show as normal, i.e., Show[ plot1, plot2, plot3, plot4 ]; -- To reply via email subtract one hundred and four ==== Subject: Re: converting exact numbers to binary fixed-point representation > Hi group, > what I want to achieve is to represent the exact value of an irrational > number, say Sin[2*Pi*131/8191], as a binary fixed-point number having 16 > fractional (plus one sign bit) bits. First, I thought of converting to floating-point value and then > converting to fixed-point using: > Floor[N[Sin[2*Pi*(131/8191)]]*2^16] Now I'm worried about the precision of this conversion. The piece of > code above truncates all fractional bits that occur after the left shift > operation. The following two intermediate results (I changed to 4 bits > for simplicity here) 1101,000...1 and 1100,111...1 will end up in two > different code words 1101 and 1100, respectively. > Though both values might be equally close to the exact value, the second > would give the wrong solution. So how can I ensure, that *rounding* the > exact value to a floating-point number will never lead to such a case, > that eventually spoils my 16 bit representation? Is there a standard way to solve this problem? > Is this a problem at all or am I worried too much? Any explanation is welcome. Torsten > Try RealDigits[Sin[2*Pi*131/8191],2,16] -- ==== Subject: Re: converting exact numbers to binary fixed-point representation (x=Sin[2*Pi*131/8191])//N//InputForm 0.10031897494050919 d=RealDigits[x,10,17] {{1,0,0,3,1,8,9,7,4,9,4,0,5,0,9,1,9},0} 0.1003189749405092 b=RealDigits[x,2,16] {{1,1,0,0,1,1,0,1,0,1,1,1,0,1,0,0},-3} 0.10031890869140625 Bob Hanlon > ==== > Subject: converting exact numbers to binary fixed-point representation Hi group, > what I want to achieve is to represent the exact value of an irrational > number, say Sin[2*Pi*131/8191], as a binary fixed-point number having 16 > fractional (plus one sign bit) bits. First, I thought of converting to floating-point value and then > converting to fixed-point using: > Floor[N[Sin[2*Pi*(131/8191)]]*2^16] Now I'm worried about the precision of this conversion. The piece of > code above truncates all fractional bits that occur after the left shift > operation. The following two intermediate results (I changed to 4 bits > for simplicity here) 1101,000...1 and 1100,111...1 will end up in two > different code words 1101 and 1100, respectively. > Though both values might be equally close to the exact value, the second > would give the wrong solution. So how can I ensure, that *rounding* the > exact value to a floating-point number will never lead to such a case, > that eventually spoils my 16 bit representation? Is there a standard way to solve this problem? > Is this a problem at all or am I worried too much? Any explanation is welcome. Torsten ==== Subject: Re: How build a function repeating the same pattern? > (*I have built this function*) ss[ model1_, list1_, model2_, list2_, t_] := Module[{t1, y1, s1, t2, > y2, s2,a, b},{t1, y1, s1} = Transpose[list1]; > {t2, y2, s2} = Transpose[list2]; > a = model1 /. t -> t1; b = model2 /. t -> t2; > Plus @@ (a y1/s1) + Plus @@ (b y2/s2)]; (*It works as I wish. Example*) q1[t_] = a1 Exp[-b1 t]; q2[t_] = a2 Exp[-b2 t]; > ss[ q1[t], {{t11, m11, s11}, {t12, m12, s12} }, q2[t], {{t21, m21, > s21}, {t22, m22, s22}, {t23, m23, s23}}, t] Now I want to extend this function such as the pattern model, list > can be repeated any number of times.Example > q3[t_] = a3 Exp[-b3 t]; ss[ q1[t], {{t11, m11, s11}, {t12, m12, s12} }, > q2[t], {{t21, m21, s21}, {t22,m22, s22}, {t23, m23, s23}}, q3[t], > {{t31, m31, s31}}, t] Any help? Guillermo > With BlankNullSequence (___) this becomes an easy task: In[1]:= snew[t_] = 0; snew[model1_, list1_, r___, t_] := Module[{a, t1, y1, s1}, {t1, y1, s1} = Transpose[list1]; Plus @@ ((model1 /. t -> t1)*(y1/s1)) + snew[r, t]] In[3]:= q1[t_] = a1*Exp[(-b1)*t]; q2[t_] = a2*Exp[(-b2)*t]; q3[t_] = a3*Exp[(-b3)*t]; In[4]:= snew[q1[t], {{t11, m11, s11}, {t12, m12, s12}}, q2[t], {{t21, m21, s21}, {t22, m22, s22}, {t23, m23, s23}}, t] Out[4]= (a1*m11)/(E^(b1*t11)*s11) + (a1*m12)/(E^(b1*t12)*s12) + (a2*m21)/(E^(b2*t21)*s21) + (a2*m22)/(E^(b2*t22)*s22) + (a2*m23)/(E^(b2*t23)*s23) In[5]:= snew[q1[t], {{t11, m11, s11}, {t12, m12, s12}}, q2[t], {{t21, m21, s21}, {t22, m22, s22}, {t23, m23, s23}}, q3[t], {{t31, m31, s31}}, t] Out[5]= (a1*m11)/(E^(b1*t11)*s11) + (a1*m12)/(E^(b1*t12)*s12) + (a2*m21)/(E^(b2*t21)*s21) + (a2*m22)/(E^(b2*t22)*s22) + (a2*m23)/(E^(b2*t23)*s23) + (a3*m31)/(E^(b3*t31)*s31) -- ==== Subject: Re: How build a function repeating the same pattern? > (*I have built this function*) ss[ model1_, list1_, model2_, list2_, t_] := Module[{t1, y1, s1, t2, > y2, s2,a, b},{t1, y1, s1} = Transpose[list1]; > {t2, y2, s2} = Transpose[list2]; > a = model1 /. t -> t1; b = model2 /. t -> t2; > Plus @@ (a y1/s1) + Plus @@ (b y2/s2)]; (*It works as I wish. Example*) q1[t_] = a1 Exp[-b1 t]; q2[t_] = a2 Exp[-b2 t]; > ss[ q1[t], {{t11, m11, s11}, {t12, m12, s12} }, q2[t], {{t21, m21, > s21}, {t22, m22, s22}, {t23, m23, s23}}, t] Now I want to extend this function such as the pattern model, list > can be repeated any number of times.Example > q3[t_] = a3 Exp[-b3 t]; ss[ q1[t], {{t11, m11, s11}, {t12, m12, s12} }, > q2[t], {{t21, m21, s21}, {t22,m22, s22}, {t23, m23, s23}}, q3[t], > {{t31, m31, s31}}, t] Any help? Guillermo > Hi Guillermo, You could use something along the lines: In[1]:= jm40[model_, param_] := Module[{pt, py, ps}, {pt, py, ps} = Transpose[param]; Plus @@ ((model /. t -> pt)*(py/ps))] In[2]:= jm44[listparam_] := Plus @@ (jm40[#1[[1]], #1[[2]]] & ) /@ listparam In[3]:= q1[t_] = a1*Exp[(-b1)*t]; q2[t_] = a2*Exp[(-b2)*t]; q3[t_] = a3*Exp[(-b3)*t]; In[6]:= jm44[{{q1[t], {{t11, m11, s11}, {t12, m12, s12}}}, {q2[t], {{t21, m21, s21}, {t22, m22, s22}, {t23, m23, s23}}}, {q3[t], {{t31, m31, s31}}}}] Out[6]= (a1*m11)/(E^(b1*t11)*s11) + (a1*m12)/(E^(b1*t12)*s12) + (a2*m21)/(E^(b2*t21)*s21) + (a2*m22)/(E^(b2*t22)*s22) + (a2*m23)/(E^(b2*t23)*s23) + (a3*m31)/(E^(b3*t31)*s31) You have noticed that I removed the parameter t_ since it is used by but not passed to the function and the parameters must be transmitted as a list of list of model + param, that is {{model1, param1},{model2, param2}...}. Hope this helps, /J.M. ==== Subject: TimeSeries installation problem on Mac I just purchased and downloaded Mathematica Time Series Pack from here: http://www.wolfram.com/products/applications/timeseries/ I'm running OS X 10.3.9 on a Mac. I clicked on the resulting installer and asked it to put the TimeSeries files here: /cd/MAC/m51s/TimeSeries/ I see this: /cd/MAC/m51s/TimeSeries: total used in directory 232 available 7331688 drwxr-xr-x 8 oracle oracle 272 1 Jul 23:38 . drwxr-xr-x 10 oracle oracle 340 1 Jul 23:34 .. drwxr-xr-x 11 oracle oracle 374 1 Jul 23:34 Data drwxr-xr-x 4 oracle oracle 136 2 Jul 09:29 Documentation -rwxrwxrwx 1 oracle oracle 3187 1 Jul 23:34 Installer Log File drwxr-xr-x 3 oracle oracle 102 1 Jul 23:34 Kernel Next, I started a notebook. I typed TimeSeries into the Help browser. It loaded a page which says this: Time Series A Fully Integrated Environment for Time-Dependent Data Analysis I find that no further drill-down is possible. It seems to me that the Help system/browser does not know about my recent installation of TimeSeries. How do I tell the Help system/browser that I have just installed TimeSeries so then it will be able to display Help information about TimeSeries?? -Dan -- dbikle@gmail.com ==== Subject: How I attach Help Browser to TimeSeries Pack? I just purchased and downloaded Mathematica Time Series Pack from here: http://www.wolfram.com/products/applications/timeseries/ I'm running OS X 10.3.9 on a Mac. I clicked on the resulting installer and asked it to put the TimeSeries files here: /cd/MAC/m51s/TimeSeries/ I see this: /cd/MAC/m51s/TimeSeries: total used in directory 232 available 7331688 drwxr-xr-x 8 oracle oracle 272 1 Jul 23:38 . drwxr-xr-x 10 oracle oracle 340 1 Jul 23:34 .. drwxr-xr-x 11 oracle oracle 374 1 Jul 23:34 Data drwxr-xr-x 4 oracle oracle 136 2 Jul 09:29 Documentation -rwxrwxrwx 1 oracle oracle 3187 1 Jul 23:34 Installer Log File drwxr-xr-x 3 oracle oracle 102 1 Jul 23:34 Kernel Next, I started a notebook. I typed TimeSeries into the Help browser. It loaded a page which says this: Time Series A Fully Integrated Environment for Time-Dependent Data Analysis I find that no further drill-down is possible. It seems to me that the Help system/browser does not know about my recent installation of TimeSeries. How do I tell the Help system/browser that I have just installed TimeSeries so then it will be able to display Help information about TimeSeries?? msg -- SunnyGuy11@gmail.com ==== Subject: Simple List manipulation question Suppose I have a list like this: {{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}} Is there a quick way to transform it into this: {{1,1},{2,4},{3,9},{4,16},{5,25}} ?? -Dan -- dbikle@gmail.com ==== Subject: Re: How to suppress plot output ? In each of the plot commands put the option DisplayFunction->Identity i.e. plot1=Plot[...,DisplayFunction->Identity] Then in the show command put DisplayFunction->$DisplayFunction i.e. Show[ plot1, plot2, plot3, plot4,DisplayFunction->$DisplayFunction ]; -- GAVIN ERRY I am having difficulty forcing a suppression of output for mathematica > commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; > plot2=Plot[ ... ]; > plot3=Plot[ ... ]; > plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do > I suppress the first four? Paul > ==== Subject: Re: How to suppress plot output ? I am having difficulty forcing a suppression of output for mathematica > commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; > plot2=Plot[ ... ]; > plot3=Plot[ ... ]; > plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do > I suppress the first four? Paul > Block[{$DisplayFunction = #1 & }, p1 = Plot[Sin[x], {x, -Pi, Pi}]; p2 = Plot[Cos[3*x/2], {x, -Pi, Pi}] ]; Show[p1, p2]; -- ==== Subject: Re: How to suppress plot output ? I am having difficulty forcing a suppression of output for mathematica > commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; > plot2=Plot[ ... ]; > plot3=Plot[ ... ]; > plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do > I suppress the first four? Paul > Hi Paul, Setting *DisplayFunction* to *Identity* will do the trick. For example: In[1]:= p1 = Plot[Sin[x], {x, 0, 10}, DisplayFunction -> Identity]; In[2]:= p2 = Plot[Cos[x], {x, 0, 10}, DisplayFunction -> Identity]; In[3]:= Show[p1, p2, DisplayFunction -> $DisplayFunction]; /J.M. ==== Subject: Re: How to suppress plot output ? If you want to 'show' plots together you could do something like this, even combining 2D and 3D plots: Show[GraphicsArray[{Plot[x + x^2, {x, -2, 2}, PlotStyle -> Red, DisplayFunction -> Identity, AxesLabel -> TraditionalForm /@ {x, x + x^2}], Plot3D[Exp[-x^2 - y^2], {x, -2, 2}, {y, -2, 2}, AxesLabel -> TraditionalForm /@ {x, y, }, PlotLabel -> TraditionalForm[Exp[-(x^2 + y^2)]], DisplayFunction -> Identity]}], GraphicsSpacing -> -.1] Steven Shippee slshippee@comcast.net 360-493-8353 I am having difficulty forcing a suppression of output for mathematica > commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; > plot2=Plot[ ... ]; > plot3=Plot[ ... ]; > plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do > I suppress the first four? Paul > ==== Subject: Re: How to suppress plot output ? I am having difficulty forcing a suppression of output for mathematica >commands when writing documents. I want to show a multi-part graph: plot1=Plot[ ... ]; >plot2=Plot[ ... ]; >plot3=Plot[ ... ]; >plot4=Plot[ ... ]; Show[ plot1, plot2, plot3, plot4 ]; In the above, I get four individual plots and one final plot. How do >I suppress the first four? I found it: plot1=Plot[ ..., DisplayFunction->Identity ]; plot2=Plot[ ..., DisplayFunction->Identity ]; plot3=Plot[ ..., DisplayFunction->Identity ]; plot4=Plot[ ..., DisplayFunction->Identity ]; Show[ plot1, plot2, plot3, plot4, DisplayFunction->$DisplayFunction ]; How do I hide the input block. I would like the document to look more like a text book. Paul