.2588 === Subject: Numeric Report by M. Sofroniou Under the Getting Started/Demos Section on the Help Browser of version FrontEndExecute[{HelpBrowserLookup[GettingStarted, Numerics Report]}] In version 5.2 is this report been somewhere or it has been dropped? Dimitris === Subject: Re: two questions I wanted. For anyone who is interested in the procedure he can contact with me. Here is a very helpful link Dimitris > As Chris Chiasson pointed me out the problem with my first question > is that the hyperlink isn't loaded because cell and box data is not > saved in > the init1.m file. So, AutoSave cannot handle TextData? Anyaway, following another way I got what I wanted. I created a initialization file initHelp.m which contains the following > commands NotebookOpen[C:Documents and SettingsAll UsersApplication > DataMathematicaApplicationsSupHelpBrowserSupplementary Help > Browser.nb] NotebookOpen[C:Documents and SettingsAll UsersApplication > DataMathematicaApplicationsSupHelpBrowserSupplementary Help > Browser for Mathematica Graphics.nb] Executing in a new notebook the commands << SupHelpBrowser`initHelp` > FrontEndExecute[{FrontEndToken[TileWindowsTall]}] everything looks fine. Of course the question is how I will add the Ted Ersek's Help Browser > to the Help Browser but I still hope someone will help me! of the Help Browser but the problem I want to understand how this > happened! Hello to all. > > I downloaded the Ted Ersek's Supplementary Help Browser > > (see http://library.wolfram.com/infocenter/MathSource/4557/) > > a few months ago. > > I spent much time with it but now I want to do two things. > > First I have a notebook called init1. This book contain the follwoing Initialization Cell (I have also selected the AutoSave Package option). > > Cell[TextData[{ Click , ButtonBox[here, ButtonData:>{ FrontEnd`FileName[ {$RootDirectory, C:, Documents and Settings, All Users, Application Data, Mathematica, Applications}, Supplementary Help Browser.nb, CharacterEncoding -> WindowsGreek], a cell for hyperlink}, ButtonStyle->Hyperlink], for Supplementary Help Browser }], Text, InitializationCell->True] > > The Hyperlink works fine when I execute the command from the init1.nb opening the Supplementary Help Browser (I have changed the filename of Ted Ersek's notebook) as I intend. > > Suppose now I work on another notebook and I ececute the following command > > << C:Documents and SettingsAll UsersApplication DataMathematicaApplicationsinit1.m > > Nothing happened. What I have on my mind was the opening of the Supplementary Help Browser from the working Notebook just with the loading of the Initialization Cell, i.e. without the need of creating another Hyperlink. > > How is it possible to do that? > > And here comes my second question. > > How the Supplementary Help Browser (and as well the Supplementary Help Browser for Mathematica Graphics) can be a part of the Help Browser? Dimitris === Subject: how to compact many maps? For table of the following structure tab=Table[{Random[],Random[]},{10},{5}] I want to apply function f to the second argument of list. I did: (MapAt[f, #, 2] & /@ #) & /@ tab How can I do it in a simpler, more compact way? Arek === Subject: Re: how to compact many maps? For table of the following structure tab=Table[{Random[],Random[]},{10},{5}] I want to apply function f to the second argument of list. I did: (MapAt[f, #, 2] & /@ #) & /@ tab How can I do it in a simpler, more compact way? > Arek Here is a newbie approach: In[54]:= MapAt[f, lst, Flatten[ Table[{i, j, 2}, {i, 10}, {j, 5}], 1]] Might not be more simpler or compact, but surely more readable :) J.87nos ---------------------------------------------- Trying to argue with a politician is like lifting up the head of a corpse. (S. Lem: His Master Voice) === Subject: Re: how to compact many maps? Arek, Try a replacement rule: tab /. {x_Real, y_Real} -> {x, f[y]} Brian For table of the following structure tab=Table[{Random[],Random[]},{10},{5}] I want to apply function f to the second argument of list. I did: (MapAt[f, #, 2] & /@ #) & /@ tab How can I do it in a simpler, more compact way? > > > Arek === Subject: Re: how to compact many maps? somewhat simpler: Map[f, tab[[All, All, 2]], {-1}] Daniel > > For table of the following structure > > tab=Table[{Random[],Random[]},{10},{5}] > > I want to apply function f to the second argument of list. > > I did: > > (MapAt[f, #, 2] & /@ #) & /@ tab > > How can I do it in a simpler, more compact way? > > > Arek > === Subject: Re: how to compact many maps? data = Table[{Random[], Random[]}, {10}, {5}]; d0 = ((MapAt[f, #1, 2] & ) /@ #1 & ) /@ data; d1 = Table[{data[[i,j,1]], f[data[[i,j,2]]]}, {i, 1, Dimensions[data][[1]]}, {j, 1, Dimensions[data][[2]]}]; d2 = data /. {x_Real, y_Real} -> {x, f[y]}; Dimensions /@ {data, d0, d1, d2} {{10, 5, 2}, {10, 5, 2}, {10, 5, 2}, {10, 5, 2}} d0 == d1 == d2 True Dimitris === Subject: Re: how to compact many maps? For table of the following structure tab=Table[{Random[],Random[]},{10},{5}] I want to apply function f to the second argument of list. I did: (MapAt[f, #, 2] & /@ #) & /@ tab How can I do it in a simpler, more compact way? > > > Arek tab[[All,All,2]] = f /@ tab[[All,All,2]] ?? === Subject: How to stop NDSolve on a given curve? how can I tell NDSolve to stop when a given condition is true? I want to stop when the trajectory reaches a given manifold (a given line for a 2-dim problem), say x==1. -- Pavel Pokorny Math Dept, Prague Institute of Chemical Technology http://www.vscht.cz/mat/Pavel.Pokorny === Subject: Re: How to stop NDSolve on a given curve? > > how can I tell NDSolve to stop when a given condition is true? > I want to stop when the trajectory reaches a given manifold > (a given line for a 2-dim problem), say x==1. > > Hi Pavel, Have a look at Built-in Functions --> Advanced Documentation --> Differential Equations --> NDSolve --> ODE Integration Methods --> Controller methods --> Event Locator http://documents.wolfram.com/mathematica/Built-inFunctions/AdvancedDocumenta tion/DifferentialEquations/NDSolve/ODEIntegrationMethods/ControllerMethods/Ev entLocation.html Jean-Marc === Subject: Importing data Question I want to plot four graphs. The element set of each graph is of a different length. The values are in a single file which I import into Mathematica. However, when I go to plot them, Mathematica tags blank entries on to the smaller sets. For example, if I have four sets of 10, 20, 30, 40 elements, Mathematica will add blank entries of 30, 20 and 10 to the first three sets. Anyone know how to fix this very simple request? === Subject: MathModelica System Designer Professional now available MathModelica System Designer Professional is now available for download from the Wolfram store. Built on the high-level Modelica standard, this new application package provides a unique graphical environment for developing dynamic models with custom-built and ready-made components from different engineering domains. Each component can be used in different roles, decreasing the need for variants of the same model. With Mathematica, users can control their simulation and model parameters to do further analysis, including visualizing, optimizing, performing system identification, and using measurement data to test and validate their models. Key features include: * Easy drag-and-drop modeling with ready-made and user-defined components also accessible programmatically * Hierarchical design structure for managing complex models * Ability to handle models from multiple engineering domains, including hybrid and discrete event systems * Built-in Simulation Center for interactive parameter variation and visualization of simulation results * Full Mathematica integration to extend, document, and automate many modeling tasks MathModelica System Designer Professional is available for Windows and requires Mathematica 4.2 or higher. An automatic 20% discount is available through December 11, 2006. More information is available at: http://www.wolfram.com/applications/mathmodelica === Subject: CovarianceMatrix Function Hello everyone, I've loaded << Statistics`DataManipulation` << Statistics`MultiDescriptiveStatistics` but cannot get the CovarianceMatrix function to work. It simply returns its name with all of the data as an argument. However, the CorrelationMatrix function does work on the same data. Am I doing something wrong or is CovarianceMatrix simply disabled? Gregory === Subject: Re: CovarianceMatrix Function What version do you use? On my 5.2 for Windows everything works fine. Try to refresh the kernel and after reload the packages. Dimitris > Hello everyone, I've loaded << Statistics`DataManipulation` > << Statistics`MultiDescriptiveStatistics` but cannot get the CovarianceMatrix function to work. It simply > returns its name with all of the data as an argument. However, the > CorrelationMatrix function does work on the same data. Am I doing something wrong or is CovarianceMatrix simply disabled? > > > Gregory === Subject: Re: CovarianceMatrix Function > Hello everyone, > > I've loaded > > << Statistics`DataManipulation` > << Statistics`MultiDescriptiveStatistics` > > but cannot get the CovarianceMatrix function to work. It simply > returns its name with all of the data as an argument. However, the > CorrelationMatrix function does work on the same data. > > Am I doing something wrong or is CovarianceMatrix simply disabled? > > > Gregory > Why do you think it is disabled? The example from the online help works fine. In[1]:= << Statistics`DataManipulation` In[2]:= << Statistics`MultiDescriptiveStatistics` In[3]:= data = N[{{1232, 4175}, {1115, 6652}, {2205, 7612}, {1897, 10914}, {1932, 10850}, {1612, 7627}, {1598, 6954}, {1804, 8365}, {1752, 9469}, {2067, 6410}, {2365, 10327}, {1646, 7320}, {1579, 8196}, {1880, 9709}, {1773, 10370}, {1712, 7749}, {1932, 6818}, {1820, 9307}, {1900, 6457}, {2426, 10102}, {1558, 7414}, {1470, 7556}, {1858, 7833}, {1587, 8309}, {2208, 9559}, {1487, 6255}, {2206, 10723}, {2332, 5430}, {2540, 12090}, {2322, 10072}}]; In[4]:= CovarianceMatrix[data] Out[4]= 6 {{124055., 361620.}, {361620., 3.48633 10 }} In[5]:= $Version Out[5]= 5.2 for Microsoft Windows (June 20, 2005) Jean-Marc === Subject: Re: CovarianceMatrix Function working before. I think that sometimes loading packages can be a little finicky. Gregory >> Hello everyone, >> I've loaded >> << Statistics`DataManipulation` >> << Statistics`MultiDescriptiveStatistics` >> but cannot get the CovarianceMatrix function to work. It simply >> returns its name with all of the data as an argument. However, >> the CorrelationMatrix function does work on the same data. >> Am I doing something wrong or is CovarianceMatrix simply disabled? >> Gregory > Why do you think it is disabled? The example from the online help > works fine. In[1]:= > << Statistics`DataManipulation` In[2]:= > << Statistics`MultiDescriptiveStatistics` In[3]:= > data = N[{{1232, 4175}, {1115, 6652}, {2205, 7612}, > {1897, 10914}, {1932, 10850}, {1612, 7627}, > {1598, 6954}, {1804, 8365}, {1752, 9469}, > {2067, 6410}, {2365, 10327}, {1646, 7320}, > {1579, 8196}, {1880, 9709}, {1773, 10370}, > {1712, 7749}, {1932, 6818}, {1820, 9307}, > {1900, 6457}, {2426, 10102}, {1558, 7414}, > {1470, 7556}, {1858, 7833}, {1587, 8309}, > {2208, 9559}, {1487, 6255}, {2206, 10723}, > {2332, 5430}, {2540, 12090}, {2322, 10072}}]; In[4]:= > CovarianceMatrix[data] Out[4]= > 6 > {{124055., 361620.}, {361620., 3.48633 10 }} In[5]:= > $Version Out[5]= > 5.2 for Microsoft Windows (June 20, 2005) Jean-Marc === Subject: Solving a PDE with fixed endpoints I am trying to solve PDE's with fixed endpoints. That is, my function is u[x,t], and I fix u[0,t] = A and u[L,t]=B, where A and B are constants. However, when I solve these PDE's out to, say, time t = 8,000, I find that the endpoints don't always stay fixed. Instead, they sometimes drift away from A and B. An example is shown in the following JPG images: 3D plot of times 7000 through 8000 and plot of u at the final time. Note that the endpoints should be fixed at 0.1 on both ends, but the left end (u(0,t)) drifts downward to -120. http://i13.tinypic.com/2roqszm.jpg Here is the initial condition, showing that the endpoints should be fixed at 0.1 on both ends: http://i14.tinypic.com/4humgww.jpg Should I be circumspect of the solution that NDSolve gives me if the endpoint condition isn't satisfied correctly for all time? Is there any way I could fix this? I think this phenomenon of the end points drifting away from A, B occurs whenever there is a constant term in the equation. That is, du/dt = const. + (rest of equation). Thus, this constant term increases the value of u at every spatial point, which makes it hard (impossible?) to satisfy the endpoint conditions. Charlie === Subject: Re: what's the command to load a specific page in the help browser? > Once in a while, I will see someone post a command that causes the > help browser to load a specific page (really a notebook). I have tried > searching for the command, but the words I remember are rather common > on MathGroup, and don't turn up useful Google search results. > > Hi Chris, I believe your are looking for *HelpBrowserLookup*. See it in action in Dimitris's post FrontEndExecute[ HelpBrowserLookup[AddOns, Graphics`Graphics3D`, 1.6]] Jean-Marc === Subject: Re: Quizzes and Tests A possible useful link http://library.wolfram.com/infocenter/MathSource/5690/ === Subject: Re: ListPlot and ListPlot3D You can also define your own function as follows pointListPlot3D[lis_List, opts___] := Show[Graphics3D[Point /@ lis], opts, Axes -> True] Example of application pointListPlot3D[Table[{n Cos[n], n Sin[n], n}, {n, 0, 100, 0.1}]] Compare with Needs[Graphics`Graphics3D`] ScatterPlot3D[Table[{n Cos[n], n Sin[n], n}, {n, 0, 100, 0.1}]] Dimitris === Subject: Re: Memory problem when multiplying large sparse matrices... > I encounter a frustrating memory problem when using Mathematica 5.2 to > multiply one large sparse matrix by another diagonal one: > > In[1]: = $HistoryLength=0; > > In[2]: = bigmat = Import[ C:Documents and Settings/bigmat.mx, > Dump] > > Out[2] = SparseArray[<48356957>, {38743, 76936}] > > In[3]: = diagmat = Import[C:Documents and Settings/diagmat.mx, > Dump] > > Out[3] = SparseArray[<38743>, {38743, 38743}] > > In[4]: = {ByteCount[bigmat], ByteCount[diagmat], MemoryInUse[ ], > MaxMemoryUsed[ ]} > > Out[4] = {387011004, 1085152, 391366120, 391528736} > > In[5]: = prodmat = diagmat . bigmat > > > No more memory available. > Mathematica kernel has shut down. > Try quitting other applications and then retry. > > The out-of-memory message appears almost immediately upon executing > have the 3GB switch set, I assume that Mathematica can access only 2 > GB. But 2 GB seems like more than enough memory for this problem. > After all, bigmat occupies 387 MB, and the resulting prodmat should > require only another 387 MB. So what is it about performing the matrix > dot product in In[5] that instantly balloons memory beyond the 2 GB > limit? > > Puzzled, > Ron > If your sparse matrix occupies 387MB in its sparse form it must have enormous dimensions and/or not be very sparse! Remember that a sparse array can be bigger than the equivalent 'ordinary' array if it is not in fact very sparse. I think it is inevitable that intermediate steps in this multiplication will require more memory - for example, at the very least, the result will be accumulated while the original is still in memory. Furthermore, if you are doing a calculation of this size, the chances are that you have already used considerable memory to store other objects. I suggest you either represent diagmat as a vector and perform the multiplication explicitly or get yourself a 64-bit machine with a lot of physical memory! David Bailey http://www.dbaileyconsultancy.co.uk === Subject: Re: Defining two functions at once? and it does not help to define a single function that return a vector ?? FAndG[x_,y_]:=Module[{myEqns,myValues,mySolns}, myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; myValues = {x1 -> x, y1 -> y}; mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; {f1} /. mySolns] f[x_,y_]:=FandG[x,y][[1]] g[x_,y_]:=FandG[x,y][[2]] Jens > I want to define two functions at once, where both of their values > depend on the same two variables, and both of their values come out of a > single two-dimensional FindRoot which I'd rather not evaluate twice. > > The following approach seems to work fine for a *single* function > > f[x_, y_] := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > f1 /. mySolns] > > But if I try to define two functions at once by replacing the first and > last lines with > > {f[x_, y_], g[x_,y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1, g1} /. mySolns] > > I get a message about shapes not being the same. In fact, if I just > make the first and last lines even a single element list, e.g. > > {f[x_, y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1} /. mySolns] > > this doesn't work either. > > If a module supposedly returns the result of its (compound) expression, > and the final term in that expression is a list, shouldn't the final > example work? More important (to me anyway): Is there a simple way to > define two functions that use a shared FindRoot evaluation in a way that > (a) evaluates the FindRoot only once, and (b) involves only a single > compound expression of some sort? > === Subject: Re: Defining two functions at once? > I want to define two functions at once, where both of their values > depend on the same two variables, and both of their values come out > of a > single two-dimensional FindRoot which I'd rather not evaluate twice. The following approach seems to work fine for a *single* function f[x_, y_] := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > f1 /. mySolns] But if I try to define two functions at once by replacing the first > and > last lines with {f[x_, y_], g[x_,y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1, g1} /. mySolns] I get a message about shapes not being the same. In fact, if I just > make the first and last lines even a single element list, e.g. {f[x_, y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1} /. mySolns] this doesn't work either. If a module supposedly returns the result of its (compound) > expression, > and the final term in that expression is a list, shouldn't the final > example work? More important (to me anyway): Is there a simple way to > define two functions that use a shared FindRoot evaluation in a way > that > (a) evaluates the FindRoot only once, and (b) involves only a single > compound expression of some sort? > I know this is not really relevant here, but I feel I should mention the fact that there is absolutely no point in using Module without any local variables; if you do not need local variables you will do better to enclose the right hand side in parentheses (or use explicit Composition[....,....,...]). Now to answer your question. You can only use lists with SetDelayed with lists if both sides have the head List *before evaluation* and the lists have the same shape. However, before evaluation your right hand side has Head Module, before Evaluation so your approach can't work. This problem would disappear if you used Set (=) instead of SetDelayed but another would take its place: the right hand side would be evaluated immediately, but then FindRoot would produce an error since it would no be able to solve the equations without numerical values for x and y. The simplest way do deal with your problem requires a lot of typing (or at east copying and pasting): {f[x_, y_], g[x_, y_]} := {Module[{myEqns, myValues, mySolns, x1, y1, f1, g1}, myEqns = {f1 - g1 == x1 + 3*y1, f1 + g1 == 2*x1 - 7*y1}; myValues = {x1 -> x, y1 -> y}; mySolns = FindRoot[myEqns /. myValues, {f1,0},{g1,0}];f1 /. mySolns],Module[{myEqns, myValues, mySolns}, myEqns = {f1 - g1 == x1 + 3*y1, f1 + g1 == 2*x1 - 7*y1};myValues = {x1 -> x, y1 -> y};mySolns = FindRoot[myEqns /. myValues, {f1, 0}, {g1, 0}]; g1 /. mySolns]} This long-winded method will indeed define two functions in one go: In[3]:= Through[{f,g}[1,2]] Out[3]= {-2.5,-9.5} Actually, I can see another way, which uses only one Module, but as I do not think it is any better than the above one, I will only describe it in words and leave implementation to anyone who sees more value in it than I do. Basically the idea is first, to replace SetDelayed by Set, so that the right hand side is evaluated immediately and then to re-write the right hand side so that it will evaluate to a list with two elements. In order to do this you would need to prevent FindRoot and Replace from trying to evaluate prematurely. This could be done, for example, by using a list of two conditional statements of the form {If[Element[Complexes, x|y], FindRoot....., If[Element[Complexes,x|y],FindRoot.....}. In other words the right hand side should immediately evaluate to a list of two conditional expressions, which will return the values of f[x,y] and g[x,y] when x and y are numerical. Andrzej Kozlowski === Subject: Re: Defining two functions at once? I hope the following will help you a little. Clear[f, g] g[a_] := f[a] = FindRoot[Cos[a*x] - x, {x, 0}] g[2] {x -> 0.5149332646611294} f[2] {x -> 0.5149332646611294} Dimitris === Subject: Re: Defining two functions at once? >> I want to define two functions at once, where both of their values >> depend on the same two variables, and both of their values come >> out of a >> single two-dimensional FindRoot which I'd rather not evaluate twice. The following approach seems to work fine for a *single* function f[x_, y_] := Module[{}, >> myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; >> myValues = {x1 -> x, y1 -> y}; >> mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; >> f1 /. mySolns] But if I try to define two functions at once by replacing the >> first and >> last lines with {f[x_, y_], g[x_,y_]} := Module[{}, >> myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; >> myValues = {x1 -> x, y1 -> y}; >> mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; >> {f1, g1} /. mySolns] I get a message about shapes not being the same. In fact, if I >> just >> make the first and last lines even a single element list, e.g. {f[x_, y_]} := Module[{}, >> myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; >> myValues = {x1 -> x, y1 -> y}; >> mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; >> {f1} /. mySolns] this doesn't work either. If a module supposedly returns the result of its (compound) >> expression, >> and the final term in that expression is a list, shouldn't the final >> example work? More important (to me anyway): Is there a simple >> way to >> define two functions that use a shared FindRoot evaluation in a >> way that >> (a) evaluates the FindRoot only once, and (b) involves only a single >> compound expression of some sort? > I know this is not really relevant here, but I feel I should > mention the fact that there is absolutely no point in using Module > without any local variables; if you do not need local variables you > will do better to enclose the right hand side in parentheses (or > use explicit Composition[....,....,...]). > Now to answer your question. You can only use lists with SetDelayed > with lists if both sides have the head List *before evaluation* > and the lists have the same shape. However, before evaluation your > right hand side has Head Module, before Evaluation so your approach > can't work. This problem would disappear if you used Set (=) > instead of SetDelayed but another would take its place: the right > hand side would be evaluated immediately, but then FindRoot would > produce an error since it would no be able to solve the equations > without numerical values for x and y. The simplest way do deal with your problem requires a lot of > typing (or at east copying and pasting): {f[x_, y_], g[x_, y_]} := {Module[{myEqns, myValues, mySolns, x1, > y1, f1, g1}, > myEqns = {f1 - g1 == x1 + 3*y1, f1 + g1 == 2*x1 - 7*y1}; > myValues = {x1 -> x, y1 -> y}; mySolns = FindRoot[myEqns /. > myValues, {f1,0},{g1,0}];f1 /. mySolns],Module[{myEqns, myValues, > mySolns}, > myEqns = {f1 - g1 == x1 + 3*y1, f1 + g1 == 2*x1 - 7*y1};myValues = > {x1 -> x, y1 -> y};mySolns = FindRoot[myEqns /. myValues, {f1, 0}, > {g1, 0}]; g1 /. mySolns]} This long-winded method will indeed define two functions in one go: In[3]:= > Through[{f,g}[1,2]] Out[3]= > {-2.5,-9.5} Actually, I can see another way, which uses only one Module, but as > I do not think it is any better than the above one, I will only > describe it in words and leave implementation to anyone who sees > more value in it than I do. Basically the idea is first, to > replace SetDelayed by Set, so that the right hand side is evaluated > immediately and then to re-write the right hand side so that it > will evaluate to a list with two elements. In order to do this you > would need to prevent FindRoot and Replace from trying to evaluate > prematurely. This could be done, for example, by using a list of > two conditional statements of the form {If[Element[Complexes, x|y], > FindRoot....., If[Element[Complexes,x|y],FindRoot.....}. In other > words the right hand side should immediately evaluate to a list of > two conditional expressions, which will return the values of f[x,y] > and g[x,y] when x and y are numerical. Andrzej Kozlowski After reading your message more carefully for the second time I realized that you were concerned (naturally!) to avoid double evaluation of FindRoot etc. Actually, this is quite easy to do, while avoiding the use of global variables by means of the following somewaht unconventional use of Module (or Block) Module[{myEqns, myValues, mySolns, x1, y1, f1, g1}, {f[x_, y_], g[x_, y_]} := { myEqns = {f1 - g1 == x1 + 3*y1, f1 + g1 == 2*x1 - 7*y1}; myValues = {x1 -> x, y1 -> y}; mySolns = FindRoot[myEqns /. myValues, {f1, 0}, {g1, 0}]; f1 /. mySolns, g1 /. mySolns}] Now as before we get: In[2]:= Through[{f,g}[1,2]] Out[2]= {-2.5,-9.5} Note that FindRoot was evaluated only once, as you had wished. Andrzej Kozlowski Tokyo, Japan === Subject: Re: Defining two functions at once? I know this is not really relevant here, but I feel I should mention the fact that there is absolutely no point in using Module without any local variables; if you do not need local variables you will do better to enclose the right hand side in parentheses (or use explicit Composition[....,....,...]). Mathematica! -- would not have been really dead sure that that was the case. [Actually, I'll probably, stubbornly, just keep enclosing the right-hand side stuff in a Module[ ] wrapper anyway, nonetheless, just because it gives me a kind of psychic feeling that this does somehow encapsulate this content into a single named unit. Maybe it's because I spent too many years writing and using FORTRAN subroutines.] === Subject: Re: Defining two functions at once? The shape difference is due to the HoldAll attribute of SetDelayed. The right hand side is not evaluated until a pattern that matches the left hand side is encountered. Also, the first definition you gave will execute a full FindRoot (starting from f1->0 and g ->0) every time you call it; I don't know if you wanted that or not. Maybe you did, because I think I remember a time you were looking for the numeric solution of some equation over a small interval - and when I suggested homotopic extension, you said you weren't worried about the answer jumping to a different solution branch (and this probably means you weren't worried about performance, either). The second requirement, b, you gave for the solution seems rather odd, given that a call to a function like FindRoot probably involves executing many CompoundExpressions. However, I have learned my lesson and will attempt to give you exactly for what you asked. The definitions below only contain one CompundExpression and only evaluate FindMinimum as often as your original definition for the single f. In[1]:= defineFG[x_,y_]:= Block[{f1,g1}, defineFG[x, y]={f1,g1}/.FindRoot[{f1-g1[Equal]x+3y,f1+g1[Equal]2x-7y},{f1, 0},{g1,0}]];f[x_,y_]:=defineFG[x,y][[1]]; g[x_,y_]:=defineFG[x,y][[2]]; In[2]:= f[0.1,0.1]//Trace Out[2]= {f[0.1,0.1], defineFG[0.1,0.1][LeftDoubleBracket]1[RightDoubleBracket],{defineFG[0.1, 0.1],Block[{f1,g1}, defineFG[0.1, 0.1]={f1, g1}/.[InvisibleSpace]FindRoot[{f1- g1[Equal]0.1[InvisibleSpace]+3 0.1, f1+g1[Equal]2 0.1-7 0.1},{f1,0},{g1, 0}]],{{{FindRoot[{f1-g1[Equal]0.1[InvisibleSpace]+3 0.1, f1+g1[Equal]2 0.1-7 0.1},{f1,0},{g1, 0}],{{{{3 0.1,0.3},0.1[InvisibleSpace]+0.3,0.4}, f1-g1[Equal]0.4},{{{2 0.1,0.2},{{7 0.1,0.7},-0.7,-0.7}, 0.2[InvisibleSpace]-0.7,-0.5}, f1+g1[Equal]-0.5},{f1-g1[Equal]0.4, f1+g1[Equal]-0.5}},{{f1,g1}=.,{f1=.,g1=.},{f1=.,Null},{g1=., Null},{Null,Null}},{f1=.,Null},{g1=.,Null},{f1[Rule]-0.05, g1[Rule]-0.45}},{f1,g1}/.[InvisibleSpace]{f1[Rule]-0.05, g1[Rule]-0.45},{-0.05,-0.45}}, defineFG[0.1, 0.1]={-0.05,-0.45},{-0.05,-0.45}},{-0.05,-0.45}},{-0.05,-0.45} [LeftDoubleBracket]1[RightDoubleBracket],-0.05} In[3]:= g[0.1,0.1]//Trace Out[3]= {g[0.1,0.1], defineFG[0.1,0.1][LeftDoubleBracket]2[RightDoubleBracket],{defineFG[0.1, 0.1],{-0.05,-0.45}},{-0.05,-0.45}[LeftDoubleBracket]2 [RightDoubleBracket],-0.45} > I want to define two functions at once, where both of their values > depend on the same two variables, and both of their values come out of a > single two-dimensional FindRoot which I'd rather not evaluate twice. The following approach seems to work fine for a *single* function f[x_, y_] := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > f1 /. mySolns] But if I try to define two functions at once by replacing the first and > last lines with {f[x_, y_], g[x_,y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1, g1} /. mySolns] I get a message about shapes not being the same. In fact, if I just > make the first and last lines even a single element list, e.g. {f[x_, y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1} /. mySolns] this doesn't work either. If a module supposedly returns the result of its (compound) expression, > and the final term in that expression is a list, shouldn't the final > example work? More important (to me anyway): Is there a simple way to > define two functions that use a shared FindRoot evaluation in a way that > (a) evaluates the FindRoot only once, and (b) involves only a single > compound expression of some sort? -- http://chris.chiasson.name/ === Subject: Re: Defining two functions at once? Not sure if this will help you : f[x_, y_] := {f3[x, y], g3[x, y]} = Module[{}, myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; myValues = {x1 -> x, y1 -> y}; mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; {f1, g1} /. mySolns] In[2]:=f[3,5] Out[2]={-5.5,-23.5} In[6]:={f3[3,5],g3[3,5]} Out[6]={-5.5,-23.5} Of course with this approach the arguments of the functions f2 and g3 are not pattern matched Brian > I want to define two functions at once, where both of their values > depend on the same two variables, and both of their values come out of a > single two-dimensional FindRoot which I'd rather not evaluate twice. The following approach seems to work fine for a *single* function f[x_, y_] := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > f1 /. mySolns] But if I try to define two functions at once by replacing the first and > last lines with {f[x_, y_], g[x_,y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1, g1} /. mySolns] I get a message about shapes not being the same. In fact, if I just > make the first and last lines even a single element list, e.g. {f[x_, y_]} := Module[{}, > myEqns = {f1 - g1 == x1 + 3y1, f1 + g1 == 2x1 - 7y1}; > myValues = {x1 -> x, y1 -> y}; > mySolns = FindRoot[ myEqns /. myValues, {f1, 0} , {g1, 0}]; > {f1} /. mySolns] this doesn't work either. If a module supposedly returns the result of its (compound) expression, > and the final term in that expression is a list, shouldn't the final > example work? More important (to me anyway): Is there a simple way to > define two functions that use a shared FindRoot evaluation in a way that > (a) evaluates the FindRoot only once, and (b) involves only a single > compound expression of some sort? === Subject: Re: Declaring a vector for NDSolve S = T[t]; ModelEqs = {T'[t] == S, T[0] == {0, 0}} sol = NDSolve[ModelEqs, {T}, {t, 0, 1}] tmp = T /. sol[[1]] tmp[t] /. t -> 0.5 and I'm not able to understand your problem. > > I am trying to use Mathematica to solve a set of differential > equations in an epidemiological model (of sexually-transmitted disease.) > > I have been using NDSolve on a system that consists of the following > two equations: > > P'[t]=A.P[t], > P[0]={0.9, 0.1, ...} > > Where P is a vector and A a matrix. Initial efforts with a simple A > went fine (Mathematica gives an error, Part :: partw:: Part 2 of P > [t] does not exist, but it still produces a correct interpolating > function (verified by solving the simple system with each equation > written out separately.)) > > I am now complicating the matrix, A, by including in some of its > cells functions of P[t]. In particular, some of the terms in A will > include P[t][[1]] through P[t][[9]]. While P[t][[2]] and higher > terms are handled correctly (I get the error message, but it does > not interrupt the solving process), P[t][[1]] is not. P[t][[1]] is > immediately evaluated as t. > > I have the following simplified formulation which shows the issue: > > S = {T[t][[1]], T[t][[2]]} > ModelEqs = {T'[t] == S, T[0] == {0, 0}} > sol = NDSolve[ModelEqs, {T}, {t, 0, 1}] > T[1] /. sol > > S Evaluates immediately to {t,T[t][[2]]}, which then leads to the > final line evaluating to {{0.5,0}}, rather than {{0,0}} as it should. > > My naive thought is that I need to be able to declare T[t] as a > vector, so that T[t][[1]] is not immediately evaluated. I do not > know if that is a reasonable approach, or if that would work (or how > to do it.) > > I am attempting to write out the program flexibly enough to handle > varying numbers of diseases (and thus varying dimensions for the > matrices), and would much prefer not to write out the equations if I > can do this generally. If worst came to worst, I suspect I could use > > subscripts instead of indices and generate each equation, but the > matrix solution would be much more elegant (excepting this one issue.) > > > Eric > > -------------------------------- > Eric Poolman, MD, MBA > Post-doctoral Fellow > Epidemiology of Microbial Diseases > Yale School of Medicine > 60 College Street, Room 147 > New Haven, CT 06520-8034 > eric.poolman@yale.edu > 203-589-8925 cell > > === Subject: Mathlink and Builder C++ I am trying to use MathLink for the first time. I am trying to link Mathematica with C code compiled with Borland C++ Builder 6.0 under Windows XP. I have a problem to create a .exe file to be called from Mathematica. I tried to follow the manual: I created a .tm file (the same as the one described in the manual) that was preprocessed using mprep. It created a .c file. In the options of my Builder project, I added the following directories: C:Program FilesWolfram ResearchMathematica5.2AddOnsMathLinkDeveloperKitWindowsCompile rAdditionsmldev32lib C:Program FilesWolfram ResearchMathematica5.2AddOnsMathLinkDeveloperKitWindowsCompile rAdditionsmldev32include. I had to add a main{} in this file in order for Builder C++ to compile it. It worked fine: the c code is compiled, the header Mathematica.h seems to be found correctly, but the linker indicates unresolved external MathLink functions (MLConnect, MLFlush, MLPutInteger, ...). Does anyone have an idea where these MLfunctions are defined? Did I used the correct directories? More generally, do you have an idea on how I could compile the file automatically produced by mprep? Any indication would be highly appreciated! JN === Subject: Bug in RowReduce of a matrix with symbolic entries Hi all, the Mathematica help browser claims that one can apply RowReduce to a matrix with numbers and letters, however that's not true, try to row-reduce the following augmented matrix: Clear[x, y, z, s, t]; RowReduce[{{ 1, 1, 1, x}, {-2, -1, -1, y}, {-3, -3, -2, z}, {0, 2, 2, s}, {-2, -4, -5, t}}] // MatrixForm the output does not contain any expression involving the letters! Am I doing something wrong? For the sake of completeness, one can obtain the right answer (the homogeneous system that makes the above augmented matrix consistent) by typing: Clear[a, v, x, y, z, s, t]; a = {{1, 1, 1}, {-2, -1, -1}, {-3, -3, -2}, {0, 2, 2}, {-2, -4, -5}}; answer = NullSpace[Transpose[a]]; v = {x, y, z, s, t}; Thread[answer.v == 0] Ruben. === Subject: Proving sign of cross partial I have the following functions: w = (1 - y/x)^(1/(1 - alpha)) Term1 = w /. {y -> L2, x -> n} Term2 = w/.{y -> (R2-L2)*(R2 +(R1 (n + L1 - R1) - L1 L2)/((n-R1)*(n + L1 - L2 - R1)), x->n} U = Term1*D*(1/(n-L2))*(R1 - ((L1 L2)/(n+L1-R1)) + Term2*(1-(1/(n-L2))*(R1 - ((L1 L2)/(n+L1-R1))) All parameters except L2 are constants with 0< alpha<1, 0=0. However, analytically, This seems harder to show. Any thoughts on how to prove this analytically. Note: I tried the binomial expansion of the two w(x,y) terms and ignored the quadratic and higher order terms because n is large. The cross-partial is positive, however the solution for minimize U by choosing L2 returns a complex L2. So, that simplification is not useful. === Subject: GraphPlot I have a question about extracting data from the internals of GraphPlot. For example, the following code plots multiple subplots of disconnected clusters. << DiscreteMath`GraphPlot` n = 129; d = Table[i -> Mod[i^2, n], {i, 0, n - 1}]; GraphPlot[d]; However, I would like to know the subsets that are being plotted. I know you can plot the vertex labels on the chart, but my dataset is ~50,000 connected vertices and GraphPlot outputs ~100 clusters, so vertex labeling is unrealistic. It would be very interesting to know which vertices GraphPlot has associated. There maybe a way to do this using some functions from Combninatorica but I have not been able to find them. Can anyone help? Matt === Subject: RE: Re: Quizzes and Tests Hmm. I don't see why Sudoko puzzles would be applicable here. But it did cause me to finally find the link I wanted. This allows you to generate and grade random tests selected from a bank of questions. http://library.wolfram.com/infocenter/MathSource/4477/ David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ A possible useful link http://library.wolfram.com/infocenter/MathSource/5690/ === Subject: RE: how to compact many maps? Arek, Two possible suggestions: tab = Table[{Random[], Random[]}, {10}, {5}]; Map[{First[#], f[Last[#]]} &, tab, {2}] tab /. {a_, b_} -> {a, f[b]} David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ For table of the following structure tab=Table[{Random[],Random[]},{10},{5}] I want to apply function f to the second argument of list. I did: (MapAt[f, #, 2] & /@ #) & /@ tab How can I do it in a simpler, more compact way? Arek === Subject: RE: How to stop NDSolve on a given curve? Pavel, Use the StoppingTest option? David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ how can I tell NDSolve to stop when a given condition is true? I want to stop when the trajectory reaches a given manifold (a given line for a 2-dim problem), say x==1. -- Pavel Pokorny Math Dept, Prague Institute of Chemical Technology http://www.vscht.cz/mat/Pavel.Pokorny === Subject: Why all the if's the answer Suppose I start with : f = -Log[y*(1 + Cos[alpha])] + Log[(b*c - b*y + c*y*Cos[alpha] + Sqrt[b^2*(c - y)^2 + c^2*y^2 + 2*b*c*(c - y)*y*Cos[alpha]])/c]; which I think is relatively innocent. I then integrate over y: Assuming[y > 0 && c > 0 && b > 0 && alpha > 0 && alpha < Pi/2, Integrate[y/c*f, {y, 0, c}]] After a few hours get a humongous answer with many If's, etc. What else can I assume or otherwise do to get an analytical answer? Aaron Fude === Subject: Re: DownValues and Cases Of course I really meant: h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b :> 3}; in which case you get: Cases[h, (x_ :> _) :> Level[x, -1]] {{x, _, x_, 1}, {x, _, x_, 2}, {}} Of course one can construct even more artificial examples where even my method will not produce the wanted result, namely, when the list h already contains something of the form Hold[f[a]]:>b, which we do not wish to pick up. One could modify it for such cases but then one could again construct examples where the new method would fail. I doubt that a completely universal and fail proof method could be found to deal with all such cases. Andrzej > That will, of course, solve the problem you asked about, concerning > DownValues. But it would not solve the more general problem with > HoldPattern (admittedly one that is not very likely to occur > naturally) . Suppose we have the definitions f[x_] := 1; g[x_] := 2; and consider the list h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b -> 3}; Suppose we are only interesting in extracting information about f, > in the way you originally wanted. This will do it: Cases[h /. HoldPattern -> Hold, (Hold[f[x_]] :> y_) - {x, y}, Infinity] > {{x_, 1}} but this will do something rather different: Cases[h, (x_ :> _) :> Level[x, -1]] > {{x, _, x_, 1}, {x, _, x_, 2}} that will allow you to pick up exactly the pattern you want when > similar ones are present. Andrzej Kozlowski > Andrezj Your reply, and that of another, has gripped and led me the >> statement below (crafted with Wolfram Tech Support's help): Cases[h, (x_ :> _) :> Level[x, -1]] This is likewise appealing...but does it suffer a flaw not found >> in your solutions? >> Bruce >> === >> Subject: Re: DownValues and Cases > > Re Mathematica 5.2 under WinXP. The results of the first two Cases statements leads me to >> anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. The output of the fourth Cases statement confirms this expectation. Unfortunately, the output of the third statement is the empty list. Why so? I've reviewed a related mid-July MathGroup thread, but >> don't see the answer (if it's there at all). >> Bruce -------------------------- f@1=3.5; >> f@2=5; >> f[_]:=0; >> h=DownValues@f Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, >> HoldPattern[f[_]] :> 0} Cases[h,HoldPattern[f[x_]] -> x,Infinity] >> Out[5]={1,2,_} Cases[h,(_ :> y_) -> y,Infinity] >> Out[6]={3.5,5,0} Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] >> Out[7]={} Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] >> Out[8]={{a,4},{b,5},{c,6}} > > This is rather tricky. The problem is how to force the > PatternMatcher to interpret HoldPattenr literally rather than as > the pattern to be held. Note that: Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y}, > Infinity] {{x, 5}} works, but: > Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y}, > Infinity] {} doesn't, because now x_ is also interpreted literally rather than > as a pattern. So you have got to somehow to avoid using HoldPattern > directly in your pattern (this is now rally getting confusing), for > example like this: > {p[[1,1]], y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} There are probably more elegant ways but none comes to my mind just > now. Andrzej Kozlowski >> A more compact way to do the above is: Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity] >> But, it seems to me that the most accurate way to match expressions >> with Head HoldPattern, as you wanted to do, is to first replace >> HoldPattern by something like Hold or Unevaluated: >> Cases[h /. HoldPattern -> Hold, >> (Hold[f[x_]] :> y_) -> {x, y}, Infinity] >> {{1, 3.5}, {2, 5}, {_, 0}} >> Andrzej Kozlowski > === Subject: Re: DownValues and Cases That will, of course, solve the problem you asked about, concerning DownValues. But it would not solve the more general problem with HoldPattern (admittedly one that is not very likely to occur naturally) . Suppose we have the definitions f[x_] := 1; g[x_] := 2; and consider the list h = {HoldPattern[f[x_]] :> 1, HoldPattern[g[x_]] :> 2, b -> 3}; Suppose we are only interesting in extracting information about f, in the way you originally wanted. This will do it: Cases[h /. HoldPattern -> Hold, (Hold[f[x_]] :> y_) -> {x, y}, Infinity] {{x_, 1}} but this will do something rather different: Cases[h, (x_ :> _) :> Level[x, -1]] {{x, _, x_, 1}, {x, _, x_, 2}} that will allow you to pick up exactly the pattern you want when similar ones are present. Andrzej Kozlowski > Andrezj Your reply, and that of another, has gripped and led me the > statement below (crafted with Wolfram Tech Support's help): Cases[h, (x_ :> _) :> Level[x, -1]] This is likewise appealing...but does it suffer a flaw not found in > your solutions? > Bruce > === > Subject: Re: DownValues and Cases (tm) Pro* > Re Mathematica 5.2 under WinXP. The results of the first two Cases statements leads me to > anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. The output of the fourth Cases statement confirms this expectation. Unfortunately, the output of the third statement is the empty list. Why so? I've reviewed a related mid-July MathGroup thread, but > don't see the answer (if it's there at all). > Bruce -------------------------- f@1=3.5; > f@2=5; > f[_]:=0; > h=DownValues@f Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, > HoldPattern[f[_]] :> 0} Cases[h,HoldPattern[f[x_]] -> x,Infinity] > Out[5]={1,2,_} Cases[h,(_ :> y_) -> y,Infinity] > Out[6]={3.5,5,0} Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] > Out[7]={} Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] > Out[8]={{a,4},{b,5},{c,6}} > This is rather tricky. The problem is how to force the >> PatternMatcher to interpret HoldPattenr literally rather than as >> the pattern to be held. Note that: Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y}, >> Infinity] {{x, 5}} works, but: >> Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y}, >> Infinity] {} doesn't, because now x_ is also interpreted literally rather than >> as a pattern. So you have got to somehow to avoid using HoldPattern >> directly in your pattern (this is now rally getting confusing), for >> example like this: >> {p[[1,1]], y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} There are probably more elegant ways but none comes to my mind just >> now. Andrzej Kozlowski > A more compact way to do the above is: Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity] > But, it seems to me that the most accurate way to match expressions > with Head HoldPattern, as you wanted to do, is to first replace > HoldPattern by something like Hold or Unevaluated: > Cases[h /. HoldPattern -> Hold, > (Hold[f[x_]] :> y_) -> {x, y}, Infinity] > {{1, 3.5}, {2, 5}, {_, 0}} > Andrzej Kozlowski > === Subject: Re: Re: DownValues and Cases Andrezj Your reply, and that of another, has gripped and led me the statement below (crafted with Wolfram Tech Support's help): Cases[h, (x_ :> _) :> Level[x, -1]] This is likewise appealing...but does it suffer a flaw not found in your solutions? Bruce === Subject: Re: DownValues and Cases >> Re Mathematica 5.2 under WinXP. The results of the first two Cases statements leads me to >> anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. The output of the fourth Cases statement confirms this expectation. Unfortunately, the output of the third statement is the empty list. Why so? I've reviewed a related mid-July MathGroup thread, but >> don't see the answer (if it's there at all). >> Bruce -------------------------- f@1=3.5; >> f@2=5; >> f[_]:=0; >> h=DownValues@f Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, >> HoldPattern[f[_]] :> 0} Cases[h,HoldPattern[f[x_]] -> x,Infinity] >> Out[5]={1,2,_} Cases[h,(_ :> y_) -> y,Infinity] >> Out[6]={3.5,5,0} Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] >> Out[7]={} Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] >> Out[8]={{a,4},{b,5},{c,6}} > > This is rather tricky. The problem is how to force the > PatternMatcher to interpret HoldPattenr literally rather than as > the pattern to be held. Note that: Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y}, > Infinity] {{x, 5}} works, but: > Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y}, > Infinity] {} doesn't, because now x_ is also interpreted literally rather than > as a pattern. So you have got to somehow to avoid using HoldPattern > directly in your pattern (this is now rally getting confusing), for > example like this: > {p[[1,1]], y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} There are probably more elegant ways but none comes to my mind just > now. Andrzej Kozlowski A more compact way to do the above is: Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity] But, it seems to me that the most accurate way to match expressions with Head HoldPattern, as you wanted to do, is to first replace HoldPattern by something like Hold or Unevaluated: Cases[h /. HoldPattern -> Hold, (Hold[f[x_]] :> y_) -> {x, y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} Andrzej Kozlowski === Subject: Re: kernel error: invalid machine instruction Kt, 2006 10 26 02:38 -0400, earthnut@web.de ra[CapitalYAcute].91: > I have just installed the trial version of Mathematica 5.2 under Linux. > The front-end works very fine, but I am not able to start the kernel. > When I type 'math' on the console, I get the following message: > > Mathematica 5.2 for Linux > Copyright 1988-2005 Wolfram Research, Inc. > Ung.9fltiger Maschinenbefehl > > Where 'Ung.9fltiger Maschinenbefehl' means 'invalid machine > instruction'. I get this error, when I start Mathematica 5.2 on very old pentium I processor. 5.0 works fine on that old hardware. Do your hardware meets technical requirements listed on Wolfram site? > > I am using an almost up to date Debian unstable on a 2.6.15 kernel with > glibc 2.12.3-2. > > Has anyone an idea? Pleas help. > > Bastian. -- Arturas Acus === Subject: kernel error: invalid machine instruction I have just installed the trial version of Mathematica 5.2 under Linux. The front-end works very fine, but I am not able to start the kernel. When I type 'math' on the console, I get the following message: Mathematica 5.2 for Linux Copyright 1988-2005 Wolfram Research, Inc. Ung.9fltiger Maschinenbefehl Where 'Ung.9fltiger Maschinenbefehl' means 'invalid machine instruction'. I am using an almost up to date Debian unstable on a 2.6.15 kernel with glibc 2.12.3-2. Has anyone an idea? Pleas help. Bastian. === Subject: Re: DownValues and Cases > Re Mathematica 5.2 under WinXP. The results of the first two Cases statements leads me to > anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. The output of the fourth Cases statement confirms this expectation. Unfortunately, the output of the third statement is the empty list. Why so? I've reviewed a related mid-July MathGroup thread, but > don't see the answer (if it's there at all). > Bruce -------------------------- f@1=3.5; > f@2=5; > f[_]:=0; > h=DownValues@f Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, > HoldPattern[f[_]] :> 0} Cases[h,HoldPattern[f[x_]] -> x,Infinity] > Out[5]={1,2,_} Cases[h,(_ :> y_) -> y,Infinity] > Out[6]={3.5,5,0} Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] > Out[7]={} Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] > Out[8]={{a,4},{b,5},{c,6}} > This is rather tricky. The problem is how to force the PatternMatcher to interpret HoldPattenr literally rather than as the pattern to be held. Note that: Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y}, Infinity] {{x, 5}} works, but: Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y}, Infinity] {} doesn't, because now x_ is also interpreted literally rather than as a pattern. So you have got to somehow to avoid using HoldPattern directly in your pattern (this is now rally getting confusing), for example like this: {p[[1,1]], y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} There are probably more elegant ways but none comes to my mind just now. Andrzej Kozlowski === Subject: Re: DownValues and Cases >> Re Mathematica 5.2 under WinXP. The results of the first two Cases statements leads me to >> anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. The output of the fourth Cases statement confirms this expectation. Unfortunately, the output of the third statement is the empty list. Why so? I've reviewed a related mid-July MathGroup thread, but >> don't see the answer (if it's there at all). >> Bruce -------------------------- f@1=3.5; >> f@2=5; >> f[_]:=0; >> h=DownValues@f Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, >> HoldPattern[f[_]] :> 0} Cases[h,HoldPattern[f[x_]] -> x,Infinity] >> Out[5]={1,2,_} Cases[h,(_ :> y_) -> y,Infinity] >> Out[6]={3.5,5,0} Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] >> Out[7]={} Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] >> Out[8]={{a,4},{b,5},{c,6}} > > This is rather tricky. The problem is how to force the > PatternMatcher to interpret HoldPattenr literally rather than as > the pattern to be held. Note that: Cases[h, (Verbatim[HoldPattern[f[2]]] :> y_) -> {x, y}, > Infinity] {{x, 5}} works, but: > Cases[h, (Verbatim[HoldPattern[f[x_]]] :> y_) -> {x, y}, > Infinity] {} doesn't, because now x_ is also interpreted literally rather than > as a pattern. So you have got to somehow to avoid using HoldPattern > directly in your pattern (this is now rally getting confusing), for > example like this: > {p[[1,1]], y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} There are probably more elegant ways but none comes to my mind just > now. Andrzej Kozlowski A more compact way to do the above is: Cases[h, (p_HoldPattern :> y_) :> {p[[1, 1]], y}, Infinity] But, it seems to me that the most accurate way to match expressions with Head HoldPattern, as you wanted to do, is to first replace HoldPattern by something like Hold or Unevaluated: Cases[h /. HoldPattern -> Hold, (Hold[f[x_]] :> y_) -> {x, y}, Infinity] {{1, 3.5}, {2, 5}, {_, 0}} Andrzej Kozlowski === Subject: DownValues and Cases Re Mathematica 5.2 under WinXP. The results of the first two Cases statements leads me to anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. The output of the fourth Cases statement confirms this expectation. Unfortunately, the output of the third statement is the empty list. Why so? I've reviewed a related mid-July MathGroup thread, but don't see the answer (if it's there at all). Bruce -------------------------- f@1=3.5; f@2=5; f[_]:=0; h=DownValues@f Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, HoldPattern[f[_]] :> 0} Cases[h,HoldPattern[f[x_]] -> x,Infinity] Out[5]={1,2,_} Cases[h,(_ :> y_) -> y,Infinity] Out[6]={3.5,5,0} Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] Out[7]={} Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] Out[8]={{a,4},{b,5},{c,6}} === Subject: Re: DownValues and Cases Hi Bruce, I do not know the exact answer, I can only guess. It looks like nested HoldPattern are stripped, they have the same effect as a single HoldPattern. This prevents the use of HoldPattern as a pattern. What we can nevertheless do to get {x,y }is a 2 step replacement: Cases[h, (x_ :> y_) -> {x, y}] /. _[_[x_]] -> x Daniel > Re Mathematica 5.2 under WinXP. > > The results of the first two Cases statements leads me to anticipate {{1,3.5}, {2,5}, {_,0}} as the output of the third. > > The output of the fourth Cases statement confirms this expectation. > > Unfortunately, the output of the third statement is the empty list. > > Why so? I've reviewed a related mid-July MathGroup thread, but don't see the answer (if it's there at all). > > > Bruce > > -------------------------- > > f@1=3.5; > f@2=5; > f[_]:=0; > h=DownValues@f > > Out[4]={HoldPattern[f[1]] :> 3.5, HoldPattern[f[2]] :> 5, HoldPattern[f[_]] :> 0} > > Cases[h,HoldPattern[f[x_]] -> x,Infinity] > Out[5]={1,2,_} > > Cases[h,(_ :> y_) -> y,Infinity] > Out[6]={3.5,5,0} > > Cases[h,(HoldPattern[f[x_]] :> y_) -> {x,y},Infinity] > Out[7]={} > > Cases[{a :> 4,b :> 5, c :> 6},(x_ :> y_) -> {x,y}] > Out[8]={{a,4},{b,5},{c,6}} > === Subject: package problem Hello to all. I faced problem with the loading of the ExpressionManipulation package from the David Park's link. Here are some of the messages I got. Needs[Algebra`ExpressionManipulation`] MessageName::messg: Algebra`ExpressionManipulation::usage cannot be set to 3D. It must be set to a string. Syntax::newl: The newline character after =20 is understood as a multiplication operator. (line 260 of Algebra`ExpressionManipulation`) Syntax::sntx: Syntax error in or before = . (line 256 of Algebra`ExpressionManipulation`) Does someone know what I did wrong? Dimitris