A63 ==== Subject: Re: 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 > Have you run the rebuild help index option from the help menu? David Bailey http://www.dbaileyconsultancy.co.uk ==== Subject: Re: How build a function repeating the same pattern? In[1]:= sss[w:{_,_List,_}..]:={w}/.{m_/;(Head[m]=!=List),n_List,t_}:>{m,Transpose[n] , t}/.{m_/;(Head[m]=!=List),n_List,t_}:> Plus@@((m/.t->n[[1]])*n[[2]]/n[[3]])/.{a__}:>Plus[a]; In[2]:= q1[t_] = a1*Exp[(-b1)*t]; q2[t_] = a2*Exp[(-b2)*t]; q3[t_] = a3*Exp[(-b3)*t]; In[3]:= sss[{q1[t],{{t11,m11,s11},{t12,m12,s12}}, t},{q2[t],{{t21,m21,s21},{t22,m22,s22},{t23,m23,s23}},t}] Out[3]= (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[4]:= sss[{q1[t],{{t11,m11,s11},{t12,m12,s12}}, t},{q2[t],{{t21,m21,s21},{t22,m22,s22},{t23,m23,s23}}, t},{q3[t],{{t31,m31,s31}},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) + (a3*m31)/(E^(b3*t31)*s31) ==== Subject: Re: TimeSeries installation problem on Mac Assuming this installation location is in one of the directories specified by the SystemHelpPath FrontEnd option setting (it doesn't appear to be one of the default locations), choose Rebuild Help Browser from the Help menu. -Rob ==== Subject: Re: 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 > Dan, After each installation of a new package, you must manually reconstruct the help index. Start Mathematica then click on the menu *Help* and select (usually at the bottom of the menu) *Rebuild Help Index*. A dialogue box showing the progress of the process will be displayed; then, you are all set, ready to access the documentation. /J.M. ==== Subject: Re: Simple List manipulation question x={{1,2,3,4,5},{1,4,9,16,25}}; Thread[x] {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} Transpose[x] {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} Bob Hanlon > ==== > 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: Re: How to suppress plot output ? To hide the input block use the menu command Cell/Cell Properties/Cell Open (checked to unchecked) Bob Hanlon > ==== > 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 > ==== Subject: Re: Re: Can't assign value to symbols and thus spake Peter Pein [2005.07.03 @ 04:08]: > 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. [ ... ] > use Clear to clear the assigned values: As others have already suggested, this is a situation where Mathematica's *Rule Replacements* shine. Instead of setting and clearing variables, it would be more efficient to apply diffierent rules. First off, translate your parameters list into a list of rules. Here's a goofy way to do this ;) : In[1]:= parameters = {{a,b,c},{1,2,3},{4,5,6},{7,8,9}}; In[2]:= rules = Thread/@ Thread[Rule[First@parameters,Rest@parameters],List,{2}] Out[2]= {{a -> 1, b -> 2, c -> 3}, {a -> 4, b -> 5, c -> 6}, {a -> 7, b -> 8, c -> 9}} Now, for each set of rules,do something with them: In[3]:= f[a_, b_, c_] := x^a - (10 c b) Exp[Sin[c x]] In[4]:= Plot[f[a,b,c]/.#,{x,0,10}]&/@rules This is much cleaner than assigning and killing variables in a loop. If you'd rather work with assigned variables, consider passing your values into another function, where you can work with localized variables as you wish. In other words, something like: In[5]:= doSomething[{a_,b_,c_}]:= Plot[x^a - (10 c b) Exp[Sin[c x]],{x,0,10}] In[6]:= doSomething/@Rest[parameters] There are at least Aleph0 different ways to do things, but functional and rule based approaches will almost always be the most efficient in Mathematica. Happy hacking. -- /*------------------------------* | stephen layland | | Documentation Programmer | | http://members.wri.com/layland | *------------------------------*/ ==== Subject: Re: Can't assign value to symbols > and thus spake Peter Pein [2005.07.03 @ 04:08]: 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. [ ... ] >use Clear to clear the assigned values: > As others have already suggested, this is a situation where > Mathematica's *Rule Replacements* shine. Instead of setting and > clearing variables, it would be more efficient to apply diffierent > rules. ... > There are at least Aleph0 different ways to do things, but functional > and rule based approaches will almost always be the most efficient > in Mathematica. Happy hacking. > -- > /*------------------------------* > | stephen layland | > | Documentation Programmer | > | http://members.wri.com/layland | > *------------------------------*/ > ). Generally I prefer functionally/rule-based style. _Lee_ has chosen David's approach ( MapThread[(#1 = #2) & , {parameters[[1]], parameters[[3]]}] ). I just answered his question regarding assignments to the variables. Sincerly, Peter -- http://people.freenet.de/Peter_Berlin/ ==== Subject: Re: Simple List manipulation question and thus spake DAN BIKLE [2005.07.03 @ 03:03]: 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 Yes. :) (See Transpose[]) -- /*------------------------------* | stephen layland | | Documentation Programmer | | http://members.wri.com/layland | *------------------------------*/ ==== Subject: Re: Simple List manipulation question In[1]:= Transpose[{{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}}] This:0. Seconds Total:0.42 Seconds Out[1]= {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} 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: 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 > Hi Try this list1 = {{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}} l = Length[Flatten[list1[[1]]]] Table[{list1[[1]][[s]], list1[[2]][[s]]}, {s, 1, 5}] >>{{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} Pratik -- Pratik Desai Graduate Student UMBC Department of Mechanical Engineering Phone: 410 455 8134 ==== Subject: Re: Simple List manipulation question Dan, list = {{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}}; Transpose[list] {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ 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: Re: Weird behavior of SparseArray and thus spake Adel Elsabbagh [2005.07.02 @ 04:13]: > But here is what I get using the same input: In[218]:= > cp1=SparseArray[{i_,j_}[Rule]0,{4,2}]; > cp1[[Range[4],1]]=cp1[[Range[4],1]]+{1,1,1,1}; > Normal[cp1] > $Version Out[220]= > {{0,0},{0,0},{0,0},{0,0}} Out[221]= Looks like a bug with 5.0.0, fixed in 5.0.1; This use of SparseArray is rather unintuitive, however. What you shold probably be doing is something like this: cp1 = SparseArray[{_,1}->1,{4,2}] Assigning a rule for every position in a sparse array defeats the purpose of a sparse array. > !!!!!!!!!!!!!!!!!!!!!!!!!!!!! That's a lot of exclaiming... -- /*------------------------------* | stephen layland | | Documentation Programmer | | http://members.wri.com/layland | *------------------------------*/ ==== Subject: Re: 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}} Use Transpose -- To reply via email subtract one hundred and four ==== Subject: Re: 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}} ?? Transpose -- Dr Jon D Harrop, Flying Frog Consultancy http://www.ffconsultancy.com ==== Subject: Re: Simple List manipulation question I think Transpose[] is what you're looking for. Stefan Linnik ~~~~~~~~~ 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: Simple List manipulation question Hi Dan I'm not a Math expert but your request is not difficult to solve with a bit of mathematics. Try to use Transpose built'in command: In[1]:= jk ={{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}}; In[2]:= Transpose[ jk ] Out[2]= {{1,1},{2,4},{3,9},{4,16},{5,25}} ~Scout~ DAN BIKLE ha scritto nel messaggio 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 ==== Subject: Re: 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 > In[1]:= Transpose[{{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}}] Out[1]= {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} You should have a look at http://documents.wolfram.com/mathematica/book/section-2.4 (The Mathematica Book is also available in the help browser). /J.M. ==== Subject: Re: 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 > To get the transpose of a matrix, use Transpose ;-) In[1]:= Transpose[{{1, 2, 3, 4, 5}, {1, 4, 9, 16, 25}}] Out[1]= {{1, 1}, {2, 4}, {3, 9}, {4, 16}, {5, 25}} -- ==== Subject: Re: 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 > You were right - it was simple! {{1,2,3,4,5},{1,4,9,16,25}}//Transpose David Bailey http://www.dbaileyconsultancy.co.uk ==== Subject: Re: How I attach Help Browser to TimeSeries Pack? Hi MSG, > 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?? It sounds like you've forgotten to rebuild the help browser. If you've put TimeSeries in a standard location, then you can simply rebuild the help browser (Help->Rebuild ...). If you haven't, then you'll have to put it somewhere standard (either $AddOnsDirectory or $UserAddOnsDirectory) and rebuild. Dave. ==== 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. >> > Piecewise works well: In[1]:= > fp[x_] := Piecewise[{{1, -1 <= x <= 1}}, 0]; > cv = Integrate[fp[z]* fp[x - z], {z, -Infinity, Infinity}] > Out[2]= > Piecewise[{{2 - x, 0 < x < 2}, > {2 + x, -2 < x <= 0}}, 0] Sorry, my eyes have been wide shut (again). These: In[1]:= f1[x_] := Piecewise[{{1/2, -1 <= x <= 1}}, 0]; Timing[Integrate[f1[z]*f1[z - x], {z, -Infinity, Infinity}]] Out[3]= {0.328 Second, Piecewise[{{(2 - x)/4, 0 < x < 2}, {(2 + x)/4, -2 < x <= 0]}}]} In[4]:= f2[x_] := Boole[-1 <= x <= 1]/2; Timing[Integrate[f2[z]*f2[z - x], {z, -Infinity, Infinity}]] Out[5]= {0.047 Second, Piecewise[{{(2 - x)/4, 0 < x < 2}, {(2 + x)/4, -2 < x <= 0]}}]} are of course the correct approaches. -- http://people.freenet.de/Peter_Berlin/ ==== Subject: Re: a question about the UnitStep function >of two sqare waves. I did as follows >f[x_]:=(UnitStep[x+1]-UnitStep[x-1])/2; >integrand=f[z] f[x-z]; >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,[O mega], >x] >Plot[h[x],{x,-10,10}] If you look carefully at the original poster's integrand, you will see he is convolving f with itself. So, I think your h needs to be h[x_]=InverseFourierTransform[(d[[Omega]])^2//FullSimplify,[Omega], x] -- To reply via email subtract one hundred and four ==== Subject: display of function The first equation below shows a function for u(y) which is the sum of the products of pi times yi. In the following steps, I assign values of p and y. How can I get Mathematica to display the numerical value of u for the given p and y values? When I insert the u[y] command, I just get a repeat of the equation I have entered. u[y_] := Sum[Subscript[p, i]*Subscript[y, i], {i, 1, n}] p = {.3,.4,.3} y={10000, 20000, 30000} ==== Subject: Re: display of function The first equation below shows a function for u(y) which is > the sum of the products of pi times yi. In the following steps, I > assign values of p and y. How can I get Mathematica to display the > numerical value of u for the given p and y values? When I insert the > u[y] command, I just get a repeat of the equation I have entered. u[y_] := Sum[Subscript[p, i]*Subscript[y, i], {i, 1, n}] p = {.3,.4,.3} y={10000, 20000, 30000} just guessing: * you want Part[], not Subscript * you mean Length[p] (or Length[y]), not n * you expect the dot-product {.3,.4,.3).{10000, 20000, 30000} Am I right? -- http://people.freenet.de/Peter_Berlin/ ==== Subject: Re: display of function You are confusing two distinct kinds of Mathematica notation here. Vectors and subscripted variables are not the same thing (at least by default) even though vector components are often referred to using subscript notation in conventional mathematics. You could use either notation consistently, so lets use Mathematica vector notation: u[y_]:=Dot[y,p]; p={1,2,3}; y={10000,20000,30000}; u[y] Alternatively, you may prefer the longer (less efficient) definition for u because perhaps you really have a more complex summation than that presented here: u[y_] := Sum[p[[i]]*y[[i]], {i, 1, Length[y]}] Working with subscripted variables is slightly trickier, but it is more visually appealing. I would start with vector notation and think about subscripted variables a bit later. David Bailey http://www.dbaileyconsultancy.co.uk ==== Subject: Re: display of function The first equation below shows a function for u(y) which is > the sum of the products of pi times yi. In the following steps, I > assign values of p and y. How can I get Mathematica to display the > numerical value of u for the given p and y values? When I insert the > u[y] command, I just get a repeat of the equation I have entered. u[y_] := Sum[Subscript[p, i]*Subscript[y, i], {i, 1, n}] p = {.3,.4,.3} y={10000, 20000, 30000} Hi Darrell, Assuming that I have correctly understood what you tried to do, you could use either one of the following approaches: In[1]:= p = {0.3, 0.4, 0.3} y = {10000, 20000, 30000} Out[1]= {0.3, 0.4, 0.3} Out[2]= {10000, 20000, 30000} We take the product of the corresponding elements in each list In[3]:= p*y Out[3]= {3000., 8000., 9000.} Finally, we take the sum of these products In[4]:= Plus @@ % Out[4]= 20000. This series of operations is indeed the dot product of p and y In[5]:= p . y Out[5]= 20000. If you need to use some other functions in place of *Plus* and *Times*, have a look at the *Inner* product. /J.M. ==== Subject: Re: display of function u[y] /. n -> 3 /. Subscript -> Part ?? Jens Bosch, Darrell schrieb im > The first equation below shows a > function for u(y) which is > the sum of the products of pi times yi. In the > following steps, I > assign values of p and y. How can I get > Mathematica to display the > numerical value of u for the given p and y > values? When I insert the > u[y] command, I just get a repeat of the > equation I have entered. > great help. Darrell u[y_] := Sum[Subscript[p, i]*Subscript[y, i], {i, 1, n}] p = {.3,.4,.3} y={10000, 20000, 30000} ==== Subject: Function to handle array with variable _number_ of dimensions? I'd like to write a function that takes two integer arguments, P and Q say, and then returns a value calculated from the P x P x ... x P (Q factors) array, whose entries depend on both P and Q. My first impulse is to try and iterate over the array, but I don't see how to generate a non-fixed number of iteration variables. Does anyone have a suggestion of a good way to do this, short of writing a a distinct variant function for each possible value of Q? -- ------------------ http://public.xdi.org/=joe.christy ------------------ == If I can save you any time, give it to me, I'll keep it with mine. == ==== Subject: Re: Function to handle array with variable _number_ of dimensions? > I'd like to write a function that takes two integer arguments, P and Q > say, and then returns a value calculated from the P x P x ... x P (Q > factors) array, whose entries depend on both P and Q. My first impulse > is to try and iterate over the array, but I don't see how to generate a > non-fixed number of iteration variables. Does anyone have a suggestion of a good way to do this, short of writing > a a distinct variant function for each possible value of Q? I'm not exactly sure what you are trying to do, but I would have a look at Outer and Tuples (in 5.1). Also have a look at the Advanced Documentation for these functions in the Help Browser. Paul -- Paul Abbott Phone: +61 8 6488 2734 School of Physics, M013 Fax: +61 8 6488 1014 The University of Western Australia (CRICOS Provider No 00126G) AUSTRALIA http://physics.uwa.edu.au/~paul http://InternationalMathematicaSymposium.org/IMS2005/ ==== Subject: How to simulate random samples from crooked coin toss? Math People, I'm new to Mathematica so please bear with me. I have a simple problem I want to solve and I'm looking for some ideas to get started on it. I want to simulate 100 tosses of a crooked coin where probability of Heads is 0.6 and Tails is 0.4. After I figure the coin out, I want to move on to crooked dice and crooked card decks. So, really what I'm looking for has more to do with developing a method to simulate the collection of random samples. I looked around in Help and found a promising sentence: Random[distribution] gives a random number with the specified statistical distribution. Then I found a page here which has some clues: http://mathworld.wolfram.com/DiscreteDistribution.html Unfortunately, I'm weak with math notation so the above page makes little sense to me. So, let me put it to you as simply as I can: I want to define a function that could be called like this: numberOfHeads = coinTosser[infoAboutCoin, numberOfTosses, head] numberOfTails = coinTosser[infoAboutCoin, numberOfTosses, tail] Once I figure out how to write the above function, I could template off of it to build something like this: numberOfOnes = diceRoller[infoAboutThisDie, numberOfRolls, aOne] Do any of you have thoughts on how to approach the writing of the above functions? -Dan -- dbikle@gmail.com ==== Subject: Re: How to simulate random samples from crooked coin toss? say a 1 mean a head and a 0 mean a tail than HeadOrTail[]:=If[Random[]<0.6,1,0] will generate one realisation of your random variable and with sample = Table[HeadOrTail[], {1000}]; you get a sample of the random variable, here is the number of heads in the sample Count[sample, 1] and here the number of tails Count[sample, 0] Jens DAN BIKLE schrieb im > Math People, I'm new to Mathematica so please bear with me. I have a simple problem I want to solve and I'm > looking > for some ideas to get started on it. I want to simulate 100 tosses of a crooked coin > where > probability of Heads is 0.6 and Tails is 0.4. After I figure the coin out, I want to move on > to crooked > dice and crooked card decks. So, really what I'm looking for has more to do > with developing > a method to simulate the collection of random > samples. I looked around in Help and found a promising > sentence: Random[distribution] gives a random number with > the specified > statistical distribution. Then I found a page here which has some clues: > http://mathworld.wolfram.com/DiscreteDistribution.html Unfortunately, I'm weak with math notation so > the above > page makes little sense to me. So, let me put it to you as simply as I can: I want to define a function that could be called > like this: numberOfHeads = coinTosser[infoAboutCoin, > numberOfTosses, head] > numberOfTails = coinTosser[infoAboutCoin, > numberOfTosses, tail] Once I figure out how to write the above > function, > I could template off of it to build something > like this: numberOfOnes = diceRoller[infoAboutThisDie, > numberOfRolls, aOne] Do any of you have thoughts on how to approach > the > writing of the above functions? -Dan > -- > dbikle@gmail.com > ==== 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 > Why not do something like this , it sounds quite simplistic but > it works :) > Clear[p] > p[x_] = x ^5(x^3 + 2x^2 + 3x - 13) > sol2 = Solve[p[x] == 0, x] > y = x /. sol2 > n = Count[y, 0] > -- > Pratik Desai > Graduate Student > UMBC > Department of Mechanical Engineering > Phone: 410 455 8134 >> In fact after seeing this posting I understood what the original >> so I thought this was meant just to be an exercise in pattern >> matching). However, using algebraic method, like the one above, >> could be unnecessarily complicated if the second factor of the >> equation is a high degree polynomial or something even more >> complicated. It is in fact easier to use patterns. Compare: >> p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13); >> Timing[Cases[p[x], x^(n_.) -> n, {1}]] >> {0.0005019999999973379*Second, {5}} >> specification is important). >> Timing[Count[x /. Solve[p[x] == 0, x], 0]] >> {0.08699899999999872*Second, 5} >> Andrzej Kozlowski >> Chiba, Japan > Hi Andrzej I have had occasion to use patterns before and by far I have found > it the most difficult feature of mathematica to work with and the > help on it leaves a lot to be desired. I agree the poster seems to > be trying to learn patterns, I was just trying to point out, as you > have on your earlier reply, that there are easier ways to do the > same job (i.e finding the highest common exponent in x). Again not > to put a finer point on it, it seems to me that the use of > patterning as you have shown seems only to work when the expression > is in its factored form (which I admit was the posters original > query) whereas using Solve and count works irrespective of the > form of the expression. Or am I wrong? You are of course right. Mathematica's pattern matching is (with some rudimentary exceptions) only syntactic, which means that expressions are matched literally and not according to their mathematical meaning (that would be semantic pattern matching). A factored expression and an unfactored oen are quite different as far as pattern matching is concerned. Algebraic methods can do much more than pattern matching but they tend to be very slow and suitable mostly for small problems. So both approaches have their strong and weak points and one or the other should be preferred depending on what one is trying to do. Obviously there is no panacea for all types of situations, which is why posters should try to state exactly what they are really trying to do. Andrzej Kozlowski p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5); > Timing[Cases[p[x], x^(n_.) -> n, {1}]] >>{0. Second, {5}} Timing[Count[x /. Solve[p[x] == 0, x], 0]] >>{0.01 Second, 10} Now if you use simplify > p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5) // Simplify; > Timing[Cases[p[x], x^(n_.) -> n, {1}]] > {0. Second, {10}} Pratik -- > Pratik Desai > Graduate Student > UMBC > Department of Mechanical Engineering > Phone: 410 455 8134 > ==== 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 >> Why not do something like this , it sounds quite simplistic but it >> works :) >> Clear[p] >> p[x_] = x ^5(x^3 + 2x^2 + 3x - 13) >> sol2 = Solve[p[x] == 0, x] >> y = x /. sol2 >> n = Count[y, 0] >> -- >> Pratik Desai >> Graduate Student >> UMBC >> Department of Mechanical Engineering >> Phone: 410 455 8134 > > In fact after seeing this posting I understood what the original > I thought this was meant just to be an exercise in pattern matching). > However, using algebraic method, like the one above, could be > unnecessarily complicated if the second factor of the equation is a > high degree polynomial or something even more complicated. It is in > fact easier to use patterns. Compare: p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13); > Timing[Cases[p[x], x^(n_.) -> n, {1}]] > {0.0005019999999973379*Second, {5}} specification is important). > Timing[Count[x /. Solve[p[x] == 0, x], 0]] > {0.08699899999999872*Second, 5} Andrzej Kozlowski Chiba, Japan Hi Andrzej I have had occasion to use patterns before and by far I have found it the most difficult feature of mathematica to work with and the help on it leaves a lot to be desired. I agree the poster seems to be trying to learn patterns, I was just trying to point out, as you have on your earlier reply, that there are easier ways to do the same job (i.e finding the highest common exponent in x). Again not to put a finer point on it, it seems to me that the use of patterning as you have shown seems only to work when the expression is in its factored form (which I admit was the posters original query) whereas using Solve and count works irrespective of the form of the expression. Or am I wrong? p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5); Timing[Cases[p[x], x^(n_.) -> n, {1}]] >>{0. Second, {5}} Timing[Count[x /. Solve[p[x] == 0, x], 0]] >>{0.01 Second, 10} Now if you use simplify p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13x^5) // Simplify; Timing[Cases[p[x], x^(n_.) -> n, {1}]] {0. Second, {10}} Pratik -- Pratik Desai Graduate Student UMBC Department of Mechanical Engineering Phone: 410 455 8134 ==== Subject: Re: how to find n in expression x^n using a pattern? Timing[Cases[p[x], x^(n_.) -> n, {1}]] > {0.0005019999999973379*Second, {5}} specification is important). > On the other hand, the default level specification for Cases is, of course, {1}... Andrzej Kozlowski ==== 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 > Why not do something like this , it sounds quite simplistic but it > works :) > Clear[p] > p[x_] = x ^5(x^3 + 2x^2 + 3x - 13) > sol2 = Solve[p[x] == 0, x] > y = x /. sol2 > n = Count[y, 0] > -- > Pratik Desai > Graduate Student > UMBC > Department of Mechanical Engineering > Phone: 410 455 8134 > In fact after seeing this posting I understood what the original I thought this was meant just to be an exercise in pattern matching). However, using algebraic method, like the one above, could be unnecessarily complicated if the second factor of the equation is a high degree polynomial or something even more complicated. It is in fact easier to use patterns. Compare: p[x_] = x^5*(x^32 + 2x^21 + 3x^17 - 13); Timing[Cases[p[x], x^(n_.) -> n, {1}]] {0.0005019999999973379*Second, {5}} specification is important). Timing[Count[x /. Solve[p[x] == 0, x], 0]] {0.08699899999999872*Second, 5} Andrzej Kozlowski Chiba, Japan ==== Subject: Re: how to find n in expression x^n using a pattern? 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 think a more mathematical approach might be as follows p = x(x^2 + 2); sol = Solve[p == 0]; r = ComplexExpand[x /. sol]; Count[r, 0] Out[31]= 1 Nasser ==== Subject: Re: how to find n in expression x^n using a pattern? and thus spake steve [2005.07.02 @ 04:08]: > 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 Here' ssomething that might work for you: (Exponent[Cases[p, x | x^n_], x] /. {} -> {0})[[1]] It's kind of ugly, but like someone suggested before, first we use cases to find any occurance of x or x^n_ by itself in the multiplication expression. We then use the function Exponent to turn the x^n_ or x into n or 1. Lastly, we explicitly set the exponent to 0 if we didn't find anything, and get rid of the list enclosure. I'm sure there are other methods, however. Happy hacking! -- /*------------------------------* | stephen layland | | Documentation Programmer | | http://members.wri.com/layland | *------------------------------*/ ==== 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 > Why not do something like this , it sounds quite simplistic but it works :) Clear[p] p[x_] = x ^5(x^3 + 2x^2 + 3x - 13) sol2 = Solve[p[x] == 0, x] y = x /. sol2 n = Count[y, 0] -- Pratik Desai Graduate Student UMBC Department of Mechanical Engineering Phone: 410 455 8134 ==== Subject: Re: Can't assign value to symbols To tell the truth, I don't understand what you are trying to do. Why do you want to reevaluate the line X=... ? If you do not want at the end of your Do loop the variables a,b,c to have assigned values the simplest way is to use Block: Block[{a, b, c}, Do[ Print@MapThread[Set, {ToExpression[X, StandardForm, Unevaluated], T[[k]]}], {k, 2, Length@T}]]; All the assignments will now take place locally inside Block and the variables {a,b,c} are automatically cleared on exit-ting Block. Andrzej Kozlowski > Andrzej Using your reply, I offer this code for Lee's consideration. Although it works when first run, if re-run without quitting the > kernal, the X= line causes an error because a,b,c are instantiated. At the start of the program, how can I clear these variables > WITHOUT explicitly declaring them? I've failed to find the right > argument for Clear (e.g., Clear@Subscript...can't find the right > counterpart for Subscript). > Bruce > T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; X=Map[SymbolName,T[[1]]]; Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; > Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}], > {k,2,Length@T}]; > ==== > Subject: 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 >> but >> 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: Can't assign value to symbols Andrzej Using your reply, I offer this code for Lee's consideration. Although it works when first run, if re-run without quitting the kernal, the X= line causes an error because a,b,c are instantiated. At the start of the program, how can I clear these variables WITHOUT explicitly declaring them? I've failed to find the right argument for Clear (e.g., Clear@Subscript...can't find the right counterpart for Subscript). Bruce T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; X=Map[SymbolName,T[[1]]]; Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T[[k]]}], {k,2,Length@T}]; ==== Subject: 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: Any ideas about solving an underdetermined system? Mathematica has a method of given the mininum-length solution. Let (x1, x2, ...) be a solution, I want that there are as many X-es equal to 0. In other words, I want to solving a underdetermined system in which many unknowns might be 0. And if I known which ones are 0, then the system may be simplified to a overdetermined one. Any ideas? ==== Subject: Re: Partial diff equations > I have a not difficult to integrate but huge system of partial > differential equations that I would never attempt to solve by hand. > So I > tried to feed it to mathematica and got the message bellow. I got > annoyed and > tested DSolve with a trivial problem only to realize that, apparently, > mathematica is not very good when it comes to partial diff equations. Indeed, how come mathematica can't solve this simple system: DSolve[{D[f[x,y],x]==2 x y^2, D[f[x,y],y]==2 x^2 y}, f[x,y], {x, y}] the solution is trivial (f[x,y]=x^2 y^2), but if I enter the above > command I get: DSolve::overdet: > The system has fewer dependent variables than equations, so is > overdetermined. any info would be appreciated, > David Boily > Centre for Intelligent Machines > McGill University > Montreal, Quebec Computer algebra programs are becoming pretty good at solving ODE's and PDE's numerically (I would rate Mathematica's capabilities in this field since version 5 as excellent) but they are hopeless at finding symbolic solutions. I do not know anybody who is using a computer program for any serious symbolic PDE work. However, first of all, in my case Mathematica 5.1 does not produce the message you report; it simply returns the input unevaluated almost immediately, which means that it does not not know any general methods to apply to a system of this kind, and it does not fall in into any special cases it knows how to deal with. It does not however mean that it is entirely useless in this sort of situation; one can sometimes reduce such a system to something more palatable to it, but of course human input is needed. In your case we first define the system we want to work on: sys = {D[f[x, y], x] == 2*x*y^2, D[f[x, y], y] == 2*x^2*y}; When you have a system of equations it often helps to replace it first by a single more general equation. One can often use Eliminate to make Mathemtica find one: v=Eliminate[sys,x] (message removed) 2*x^2*y == Derivative[0, 1][f][x, y] && 2*x*y^2 == Derivative[1, 0][f][x, y] && x*Derivative[1, 0][f][x, y] == y*Derivative[0, 1][f][x, y] && Derivative[1, 0][f][x, y]^2 == 2*y^3*Derivative[0, 1][f][x, y] The first two equations are just the ones we had already but the third one looks promising, so we try to DSolve it: DSolve[v[[3]], f[x, y], {x, y}] {{f[x, y] -> C[1][x*y]}} This is certainly progress. Any solutions of the original equations must be of this form. So we simply define f as f[x_, y_] := c[x *y] and now our sys becomes: sys1=Simplify[sys, x != 0 && y != 0] {2*x*y == Derivative[1][c][x*y], 2*x*y == Derivative[1][c][x*y]} O.K., so we see that all we need to do is to DSolve: DSolve[Union[sys1] /. x*y -> t, c[t], t] {{c[t] -> t^2 + C[1]}} hence we get the solution f[x,y]:=x^2*y^2+constant Obviously this requires a lot of human input and making some good or lucky choices (if I had chosen v[[4]] instead of v[[3]] I would not have got anywhere) but occasionally this kind of approach may be useful even in quite complex cases (I have several examples of this kind). Andrzej Kozlowski Chiba,JAPAN ==== Subject: Partial diff equations I have a not difficult to integrate but huge system of partial differential equations that I would never attempt to solve by hand. So I tried to feed it to mathematica and got the message bellow. I got annoyed and tested DSolve with a trivial problem only to realize that, apparently, mathematica is not very good when it comes to partial diff equations. Indeed, how come mathematica can't solve this simple system: DSolve[{D[f[x,y],x]==2 x y^2, D[f[x,y],y]==2 x^2 y}, f[x,y], {x, y}] the solution is trivial (f[x,y]=x^2 y^2), but if I enter the above command I get: DSolve::overdet: The system has fewer dependent variables than equations, so is overdetermined. any info would be appreciated, David Boily Centre for Intelligent Machines McGill University Montreal, Quebec ==== Subject: Re: Can't assign value to symbols I see. In this case, does not Scan[Clear,X] after the Do loop do what you want? Andrzej > Andrzej The aim is to explictly avoid (throughout the code) referring to > the variables. For instance, suppose matrix T (in my code) springs from an Excel > spreadsheet imported into Mathematica, one whose top row declares > the variable names, and whose later rows give the variables' values > for each iteration in a simulation. Suppose further that the > number of columns may change. Bruce > ==== > Subject: Can't > assign value to symbols To tell the truth, I don't understand what you are trying to do. Why > do you want to reevaluate the line X=... ? If you do not want at the end of your Do loop the variables a,b,c to > have assigned values the simplest way is to use Block: Block[{a, b, c}, Do[ > Print@MapThread[Set, {ToExpression[X, > StandardForm, Unevaluated], T[[k]]}], > {k, 2, Length@T}]]; All the assignments will now take place locally inside Block and the > variables {a,b,c} are automatically cleared on exit-ting Block. Andrzej Kozlowski >> Andrzej >> Using your reply, I offer this code for Lee's consideration. >> Although it works when first run, if re-run without quitting the >> kernal, the X= line causes an error because a,b,c are instantiated. >> At the start of the program, how can I clear these variables >> WITHOUT explicitly declaring them? I've failed to find the right >> argument for Clear (e.g., Clear@Subscript...can't find the right >> counterpart for Subscript). >> Bruce >> T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; >> X=Map[SymbolName,T[[1]]]; >> Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; >> Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T >> [[k]]}], >> {k,2,Length@T}]; >> ==== >> Subject: 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 > but > 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: Can't assign value to symbols Andrzej The aim is to explictly avoid (throughout the code) referring to the variables. For instance, suppose matrix T (in my code) springs from an Excel spreadsheet imported into Mathematica, one whose top row declares the variable names, and whose later rows give the variables' values for each iteration in a simulation. Suppose further that the number of columns may change. Bruce Bruce ==== Subject: Can't assign value to symbols To tell the truth, I don't understand what you are trying to do. Why do you want to reevaluate the line X=... ? If you do not want at the end of your Do loop the variables a,b,c to have assigned values the simplest way is to use Block: Block[{a, b, c}, Do[ Print@MapThread[Set, {ToExpression[X, StandardForm, Unevaluated], T[[k]]}], {k, 2, Length@T}]]; All the assignments will now take place locally inside Block and the variables {a,b,c} are automatically cleared on exit-ting Block. Andrzej Kozlowski > Andrzej Using your reply, I offer this code for Lee's consideration. Although it works when first run, if re-run without quitting the > kernal, the X= line causes an error because a,b,c are instantiated. At the start of the program, how can I clear these variables > WITHOUT explicitly declaring them? I've failed to find the right > argument for Clear (e.g., Clear@Subscript...can't find the right > counterpart for Subscript). > Bruce > T={{a,b,c},{3,4,2},{12,2,1},{6,5,-3}}; X=Map[SymbolName,T[[1]]]; Do[Apply[Clear,ToExpression[X,StandardForm,Unevaluated]]; > Print@MapThread[Set,{ToExpression[X,StandardForm,Unevaluated],T > [[k]]}], > {k,2,Length@T}]; > ==== > Subject: 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 >> but >> 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: display of function p = {p1,p2,p3}; y={y1, y2, y3}; Use the dot product p.y p1*y1 + p2*y2 + p3*y3 p = {.3,.4,.3}; y={10000, 20000, 30000}; p.y 20000. For your function to work, you would need to use Part rather than Subscript and define n (i.e., provide a numerical value for the index upper limit). u[y_]:=Sum[p[[i]]*y[[i]],{i,1,Length[y]}]; u[y] 20000. Bob Hanlon > ==== > Subject: display of function The first equation below shows a function for u(y) which is > the sum of the products of pi times yi. In the following steps, I > assign values of p and y. How can I get Mathematica to display the > numerical value of u for the given p and y values? When I insert the > u[y] command, I just get a repeat of the equation I have entered. u[y_] := Sum[Subscript[p, i]*Subscript[y, i], {i, 1, n}] p = {.3,.4,.3} y={10000, 20000, 30000} >