mm-569 === Subject: Re: Function of function analytic definition I doubt if there is a simple relationship between the n-th term of f1=f[g[x]] and f2=g[f[x]] because in the Taylor Series of f1 and f2 the terms f and g are evaluated for different arguments. Consider f1, then all derivatives of f are avaluated at the point g[x0]. However, for f2, all derivatives of f are evaluated at the point x0. A relationship between these terms must therefore involve all terms of the Taylor Series and is certainly not simple. Daniel > Is there perhaps a mixed functional relation between 'n' th term of > Taylor's Series for f(x0 + h) = f '(n-1)(x0)/(n-1)! and the same > for g(x0 + h) = g '(n-1)(x0)/(n-1)! to obtain/ evaluate 'n' th > term of f( g(x) ) and g( f(x) )? Is it not possible by using Jacobian > criteria using multi-parameter function definitions available on > Mathematica to find this out? [Subject to of course assumption of > existence, uniqueness, smoothness and other well ordered analytic > functional behaviors of f and g]. Hoping for any related comments. > Somehow this comes to me often, earlier queries did not help clear this === Subject: Evaluating integrals I am new to Mathematica and am tryong to figure out how to work efficently with integrals and run into the following issue. Let's say I want to integrate a simple function like Exp[-r b c], where c is a normal random variable. Doing this by hand, I can simply complete the square and end up with a simple result: Exp[(r^2 b^2 sc^2 )/ 2]. However, if I run the following code in Mathematica: g[c_] := PDF[NormalDistribution[[Micro]c, sc], c] pi = Exp[-r b c] FullSimplify[Integrate[pi g[c], c, Assumptions -> {c > 0, r > 0, b > 0}]] I get: !(1/2 [ExponentialE]^(1/2 b r (((-2) [Micro]c + b r sc^2))) Erf[(c - [Micro]c + b r sc^2)/(@2 sc)]) And Mathematica does not seem to be able to simplify this answer any further. Is there a way to make it return the simple answer? Can it do 'tricks' like completing the square? Masha === Subject: Faster Random Walk Simulation ?!? I am running a simulation with Mathematica 5 that includes random walk described below. The argument boundaryA is upper and lower boundary for a random walk. Parameter value is just a constant arbitrarily set to 0.5. The output of the table function is the 10000 random walks, and their path to either +5 or -5 boundary value. While everything works as expected, it takes considerable duration of time. For example it takes 2.6 seconds to generate the table output, on a relatively fast computer. I would very appreciate if any significantly faster solution could be suggested. value = .5 RandomWalk[boundaryA_] := Block[{l = {{0, 0}}, x = 0, i = 0 }, While[boundaryA > x > -boundaryA, x += If[Random[] > value, 1, -1]; l = Append[l, {++i, x}]]; l] Timing[Table[RandomWalk[5], {i, 1, 10000}];] Out[420]= {2.672 Second, Null} Mario Fific Mario Fific Cognitive Psychology, Cognitive Science Indiana University 1101 E. 10th St. Bloomington, IN 47405-7007 === Subject: Is it possible for me to implement associative arrays this way? I've tried searching the net to see if I could find an answer before I bothered folks with this. been very convenient and clear if I could have used short lists as subscripts, something similar to AWK's associative arrays, not what Mathematica usually does with lists as subscripts. With experimenting I think I see how Mathematica uses {1, 2, 3, 4, 5, 6}[[2]]; {1, 2, 3, 4, 5, 6}[[{2,3,4}]]; x={1, 2, 3, 4, 5, 6}; x[[2]]; x[[2]]=4; x[[{2, 3, 4}]] = 5 x[[{2, 3, 4}]] = {5, 4, 3} and even a bit oddly x[[{2, 3, 4}]] = {5, 4, 3, 2, 1} But the the syntax {1, 2, 3, 4, 5, 6}[[{{2,3,4}}]] is not currently used by Mathematica. In other words when a Part specification is not an integer or list of integers this isn't used. So, is there some way I could unprotect some bit of Mathematica, insert a tiny bit of code such that if a Part specification was a list of a list that it would create a table to implement the associative array, and then reprotect Mathematica, for use both on the left and right side of '='? This would be really convenient and not seem to subvert the principles too much that Mathematica tries to use. === Hello everyone, I've discovered another use or need for the Select function, which I suspect requires mapping of some sort. In my previous posts, members of this MathGroup kindly showed me how to apply Select to many columns of a matrix at once. For example, (Select[#1, #1 > K & ] & ) /@ Transpose[theMatrix] will pull out all values greater than K, where K is a number such as 100. But suppose now that K is a list of numbers, such as K={34, 876, 199}, and I simply want to extract or identify all of the rows in the first column of theMatrix equal to any one of those numbers. How would I do that? I started with Select[theMatrix, #[[1]]==any element of list K] and I imagine something similar could be applied to the Position function. Any hint would be much appreciated. Gregory === Subject: svg import Can Mathematica 5.2 import and display svg (scalable vector graphics) files into its notebook XML format? If not, is there a plan in the future to import svg files? I know currently data can be exported as svg. I wanted to import some figures with loss-less reproduction and then generate a pdf. === Subject: Re: Limit bug? More than one week past. Is anybody know the reason? But if i try to find the limit of x-Log[Cosh[x]] where x->Infinity, Mathematica will find the answer. Is that funny? === Subject: Re: Periodic Rebirth of Hyperbolic Functions by ODE in Mathematica > BTW, why does not integ(r' r '' ) = r'^2 /2 work for tanh? Yes, ultimately this method is just a reduction of order, but used in an indirect way. Note that if we simply tried to feed the invariant to NDSolve it wouldn't work: In[1]:= inv = Integrate[(r''[th] - r[th]*(1 - 2*r[th]^2))*r'[th], th]; r[1] /. NDSolve[{inv == 0, r[0] == 1}, r, {th, 0, 100}] Out[2]= {1., 1.} because NDSolve would only find the solution to the system {r'[th] == 0, r[0] == 1}. To make it work for your second example, we need to increase WorkingPrecision: In[3]:= ode = r''[th] - 2*(r[th]^3 - r[th]); inv = Integrate[ode*r'[th], th]; sol = First@ NDSolve[{ode == 0, r[0] == 0, r'[0] == 1}, r, {th, 0, 100}, Method -> {Projection, Invariants -> inv}, MaxStepFraction -> .001, WorkingPrecision -> 100, PrecisionGoal -> 10]; Plot[r[th] - Tanh[th] /. sol, {th, 0, 100}, PlotRange -> All] I don't know how to get an accurate solution from NDSolve without using this additional information about the invariant. Here's how to obtain the exact solution: In[7]:= r[th] /. DSolve[{inv == 1/2, r[0] == 0, r'[0] == 1}, r, th] // FullSimplify Out[7]= {Tanh[th]} Another standard method is to introduce the new variable p[r] = r': In[8]:= ode2 = ode //. Derivative[k_][r][th] :> D[p[r[th]], {th, k - 1}] /. r[th] :> r Out[8]= -2*(-r + r^3) + p[r]*Derivative[1][p][r] In[9]:= sol = First@ DSolve[{ode2 == 0, p[0] == 1}, p, r] out[9]= {p -> Function[{r}, Sqrt[1 - 2*r^2 + r^4]]} In[10]:= r[th] /. DSolve[ {r'[th]^2 == (p[r]^2 /. sol /. r -> r[th]), r[0] == 0, r'[0] == 1}, r, th] // FullSimplify Out[10]= {Tanh[th]} Maxim Rytin m.r@inbox.ru === Subject: Re: nearest neighbor if you would like to find several nearest neighbors then you might want to try a kd-Tree method. You will find the documenation here: http://www.imtek.uni-freiburg.de/simulation/mathematica/IMSweb/imsTOC/Data%2 0Structures/TreesDocu.html to download follow: http://www.imtek.uni-freiburg.de/simulation/mathematica/IMSweb/ there is also a mailing list: http://elmo.imtek.uni-freiburg.de/mailman/listinfo/ims hth, oliver > Chris, > It is an interesing question how to find the nearest neighbor without > computing ALL distances. But I would not be surprised if such a solution > turns out to be slower than computing all distances, e.g. in the > following way. Finding the nearest neighbor from a set of 10^6 points > takes less than 0.4 second on my slow computer here at the university. > p = {2, 1, 3}; > mat = Array[Random[Real, {0, 4}] & , {10^6, 3}]; > Extract[mat, Ordering[ > Total[(Transpose[mat] - p)^2], 1]] // Timing > {0.361 Second, {1.97792, 0.982634, 2.98964}} > Fred Simons > Eindhoven University of Technology > -----Original Message----- === > Subject: nearest neighbor > > > > I am in interested in finding the nearest neighbor for points > in a space whose dimension is greater than two. How can I do > this in Mathematica without computing the distances between > all of the points? In particular, I am interested in > developing a Mathematica notebook for the false nearest > neighbor algorithm used in nonlinear time series analysis. > > > > > > > > Chris Kulp > > > > Dr. Christopher W. Kulp, Ph.D. > > Assistant Professor of Physics > > Eastern Kentucky University > > Moore 351 > > 521 Lancaster Ave. > > Richmond, KY 40475 > > 859.622.1528 > > chris.kulp@eku.edu > > http://people.eku.edu/kulpc > > > > Oliver Ruebenkoenig, Phone: ++49 +761 203 7388 === Subject: Re: Re: Apparent accuracy error in least squares fit Sorry! It was a matter of copy and paste from Mathematica! Here is the f definition: f[T_,Ms_]=Ms-Mo*Coth[(a+b*Ms)/T]+Mo*T/(a+b*Ms) El Martes, 2 de Mayo de 2006 08:43, escribi.97: > what is the definition of f? -- Daniel Ortega === Subject: Re: In progress saving of data collected using Reap/Sow Very good idea. I was thinking about something similar, but I was also wondering if there was a more direct way of collecting intermediate data from Reap function. Anyway, I thik I will implemet your suggestion. Only one more question: I can't understand exactly why you introduced the strm tag to collect data... === Subject: Re: In progress saving of data collected using Reap/Sow Expanding on my previous post, here are three ways to unravel z into a simple list of all the past results. The first way is fastest, but doesn't work if each result itself is a list. In[1]:= n = 8 y = {}; Do[AppendTo[y,result[i]],{i,n}]; y z = {}; Do[z = {z,result[i]},{i,n}]; z Out[1]= 8 Out[2]= {result[1],result[2],result[3],result[4], result[5],result[6],result[7],result[8]} Out[3]= {{{{{{{{{},result[1]},result[2]},result[3]},result[4]}, result[5]},result[6]},result[7]},result[8]} Out[4]= True Out[5]= True Out[6]= True In[7]:= n = 8 y = {}; Do[AppendTo[y,{i,-i}],{i,n}]; y z = {}; Do[z = {z,{i,-i}},{i,n}]; z Out[7]= 8 Out[8]= {{1,-1},{2,-2},{3,-3},{4,-4}, {5,-5},{6,-6},{7,-7},{8,-8}} Out[9]= {{{{{{{{{},{1,-1}},{2,-2}},{3,-3}},{4,-4}}, {5,-5}},{6,-6}},{7,-7}},{8,-8}} Out[10]= False Out[11]= True Out[12]= True > Hello to everyone, > I'm quite new to Mathematica, so please be patient if I ask something > trivial. > thousand of times, storing the result of each iteration. At first, I > used the Append function to store data in a list at every iteration, > but I noticed that this operation become slower and slower as the list > increases in size, so that appending a single data at the end of a > quite big list takes a lot of time. > So I migrated to the reap/sow functions that are written exactly for > this purpose, ad gives much better performances. My code now is > something like: > results = Reap[Do[a lot of iterations with almost a call to Sow in > each]]]; > The problem now is that, since the collected data are returned by the > Reap function only whern it terminates, I have no way to access that > data if the calculation is in progress, ie if the Do loop is not > completed. This way, I can't check the status of the calculation nor > provide backup save of data during the calculation itself, that can > last for days... > As you can imagine, using the Append function both this (and others) > tasks where easily accomplishable, since in every moment I had a list > whith all the results obtained so far... > Any idea on how I can solve the problem? > Giacomo > In[1]:= n = 1*^4 > Timing[y = {}; Do[AppendTo[y,i],{i,n}];] > Timing[z = {}; Do[z = {z,i},{i,n}];] > i = Random[Integer,{1,n}] > y[[i]] > Last@Nest[First,z,n-i] > Out[1]= 10000 > Out[2]= {1.86 Second,Null} > Out[3]= {0.02 Second,Null} > Out[4]= 9353 > Out[5]= 9353 > Out[6]= 9353 === Subject: Re: In progress saving of data collected using Reap/Sow Are you suggesting me just to store every new result as a new level in the list? Very interesting approach... there is a limit on the number of levels of a list? And what about the amount of memory needed to store the data with such an approach? Thaks Giacomo === Subject: Re: In progress saving of data collected using Reap/Sow Ok, I supposed I didn't explained so good... I needed an efficient way of collecting in a variable data generated in a loop; to do this, using Reap/Saw is a good solution, except that there is no direct way of saving data till all the iteration are finished. Anyway I already found convenient solution fot that. In addition, your suggestion solves onether problem of mines, that is report the calculation progress quite often without filling the notebook's output whit a lot of lines like: 0.1 % done 0.2 % done 0.3 % done ... Giacomo === Subject: Re: Re: Conditions with Statistical Functions curve! Gregory > Gregory, > And you can always use the Function statement for pure functions > instead of > slots and ampersand. Sometimes that makes things clearer, > especially when > you have nested pure functions. > David Park === Subject: Re: Re: Conditions with Statistical Functions Gregory, And you can always use the Function statement for pure functions instead of slots and ampersand. Sometimes that makes things clearer, especially when you have nested pure functions. David Park djmp@earthlink.net http://home.earthlink.net/~djmp/ Hi Jean-Marc, take the time to write such a lucid and detailed explanation. I'm embarrassed about the transposition question. That had occurred to me when I started thinking about the problem initially, but I quickly forgot about it when I got bogged down in trying to construct the appropriate function (unsuccessfully, of course). I think your explanation has helped me better understand the use of slots (#) and the ampersand (&). If I understand correctly, the ampersand is part of the definition of a pure function and is therefore required to terminate it. That means that its placement in nested functions is important. So, at the risk of messing up, the first ampersand turns the greater-than condition into a pure function, and the second turns the Select part, along with the enclosing greater-than condition, into another pure function. Each has its own slot (#1) or user-supplied variable, which in your example is the matrix 1st. I will now apply it to my data. I suspect I will be able to cut down processing time over using a Do loop. If I'm feeling courageous, I will try to use your approach and Bob Hanlon's to create similar functions using Count. Greg >> This is elegant. As I mentioned before, I'm facing an uphill >> battle with >> Mathematica's syntax, and in particular, the meaning and placement >> of things >> like #, #1, or #[[1]], &, @, @@, @@@, /@, /. . It's all a bit >> overwhelming! >> I think I understand your second version better, so I'll work with >> it first. >> If I'm not mistaken, /@ tells the Mean function to map over its >> argument >> and #1 directs that mapping to the first argument, which for Mean >> applied to >> a matrix is a vector. What I never would have gotten on my own is >> the use >> of the second ampersand in parenthesis. Is that meant to connect >> Select >> with Transpose? I'm also not quite clear on why we need Transpose >> because >> Mean operates on columns anyway. >> Greg >> In[3]:= >> Mean /@ (Transpose[lst /. x_ /; x <= 100 -> 0] /. >> 0 -> Sequence[]) >> Out[3]= >> 1311 1450 527 >> {125, ----, ----, ---} >> 8 9 3 >> In[4]:= >> Mean /@ (Select[#1, #1 > 100 & ] & ) /@ Transpose[lst] >> Out[4]= >> 1311 1450 527 >> {125, ----, ----, ---} >> 8 9 3 >> Jean-Marc > Hi Gregory, > Let us try to follow gradually what is going on. Say that our data set > is a 12 by 4 matrix of integer entries: > lst={{109,168,173,109},{4,143,200,90},{181,162,85,196},{30, > 108,86,34},{94,127,144,34},{199,109,195,188},{176, > 34,46,110},{95,27,160,109},{43, > 71,130,66},{56,148,109,163},{110,43,50,53},{32,34,16,95}} > First, we will focus on the second expression, which use the > *Select* function. > Mean/@(Select[#1,#1>100&]&)/@Transpose[lst] > Out[3]= > 965 1111 875 > {155, ---, ----, ---} > 7 7 6 > If we look at the full form ö Mathematica internal representation ö of > the above expression, we see that (Select[#1, #1 > 100 & ] & ) is an > expression made of nested pure functions. Pure functions are > equivalent to anonymous functions in, say, LISP. The #n's (or > Slot[n]'s) are placeholders for variables and a pure function > definition ends by an ampersand character '&'. In our function, it is > really important to realize that the first #1 has nothing to do with > the second #1 because they are located in different function > definitions (this is clearer in the full form of the expression). > In[6]:= > FullForm[HoldForm[Mean /@ (Select[#1, #1 > 100 & ] & ) /@ Transpose > [lst]]] > Out[6]//FullForm= > HoldForm[Map[Mean, > Map[Function[Select[Slot[ > 1],Function[Greater[Slot[1],100]]]],Transpose[lst]]]] > Note, we could have written the pure function with explicit ö local > övariable name such as in the line below. *Select* works on each row > and applies the test to each element. > In[10]:= > Map[Mean,Map[Function[rowvec,Select[rowvec,Function[elem, > Greater[elem,100]]]],Transpose[lst]]] > Out[10]= > 965 1111 875 > {155, ---, ----, ---} > 7 7 6 > Now, let us see why we need to transpose the matrix first. Below, we > can see the original matrix. By visual inspection, we notice that the > first column has five values that are greater than 100, and the > second, third and fourth columns have seven,seven,and six values > greater than 100, respectively. > In[2]:= > TableForm[lst] > Out[2]//TableForm= > 109 168 173 109 > 4 143 200 90 > 181 162 85 196 > 30 108 86 34 > 94 127 144 34 > 199 109 195 188 > 176 34 46 110 > 95 27 160 109 > 43 71 130 66 > 56 148 109 163 > 110 43 50 53 > 32 34 16 95 > Applying the select clause to the original matrix without > transposition yields the following result: any values less than or > equal to 100 have been discarded; however, the structure of the > original matrix has been changed too. Not only do we have a collection > of row vectors of unequal lengths, but also many elements have shifted > to the left! > In[13]:= > (Select[#1,#1>100&]&)/@lst//TableForm > Out[13]//TableForm= > 109 168 173 109 > 143 200 > 181 162 196 > 108 > 127 144 > 199 109 195 188 > 176 110 > 160 109 > 130 > 148 109 163 > 110 > On the other hand, transposing first allows getting the correct values > in each row. > In[12]:= > (Select[#1,#1>100&]&)/@Transpose[lst]//TableForm > Out[12]//TableForm= > 109 181 199 176 110 > 168 143 162 108 127 109 148 > 173 200 144 195 160 130 109 > 109 196 188 110 109 163 > Finally, mapping *Mean* to the resulting list of lists allows to > compute the mean of each row vectors that correspond to our original > columns without the unwanted values (so using *Map* allows us to > change the behavior of Mean which is to compute column by column). > In[15]:= > Mean/@(Select[#1,#1>100&]&)/@Transpose[lst]//Trace > Well, I hope that I have not been to long and to obscure in my > explanation and that indeed I have successfully shred some light on > Mathematica programming. > Jean-Marc === Subject: How to find expected value? I am new to Mathematica and I am trying to calculate the expected value of a function using the code pasted below. However, mathematica does not find it and just spits back the last line of code. Could you please help me figure out how to evaluate the expected value? Masha !(Clear[][IndentingNewLine] Remove[][IndentingNewLine] << Statistics`ContinuousDistributions`[IndentingNewLine] << Statistics`DescriptiveStatistics`[IndentingNewLine] h[q_] := PDF[NormalDistribution[ë.b9q, ì°q], q][IndentingNewLine] g[c_] := PDF[NormalDistribution[ë.b9c, ì°c], c][IndentingNewLine] (pio = p q - c q;)[IndentingNewLine] (pif = b ë.b9q (.89ÃÇ_ë.b9_c%.89[Capit alATilde]õ((c - ë.b9c)) g[ c] [DifferentialD]c) + a ë.b9q ((c - ë.b9c)) - Po b ë.b9q;)[IndentingNewLine] pit = pio + pif[IndentingNewLine] util = Exp[(-r)*W] Exp[(-r)*ëÓ pit] Exp[r*ch][IndentingNewLine] ExpectedValue[util, g[c], c]) === Subject: Beginner--Help on using FindRoot to solve the system of equations This is a code to solve vapor-liquid equilibrium by van der Waals Equation of State. (* Name of EOS *) EOSName = Van der Waals; Ttilde = .; Dtilde = .; Z = 3/(3-Dtilde)-9*Dtilde/8/Ttilde; (* Related variables *) Psi1 = (-9*Dtilde)/(8*Ttilde) - Log[3-Dtilde]; lnB = (1-Z)-Psi1; mu = -lnB+Log[Dtilde]; P = Dr Ttilde Z; DtildeG=10^-14; DtildeL=2.91; Dtilde1=.; Dtilde2=.; mu1=mu/.Dtilde->Dtilde1; mu2=mu/.Dtilde->Dtilde2; P1=P/.Dtilde->Dtilde1; P2=P/.Dtilde->Dtilde2; Ttilde = 0.1; Result= FindRoot[ {P1==P2,mu1==mu2}, {Dtilde1,DtildeG}, {Dtilde2,DtildeL}, MaxIterations->1000, WorkingPrecision->16]; Link to the forum page for this post: http://www.mathematica-users.org/webMathematica/wiki/wiki.jsp?pageName=Speci al:Forum_ViewTopic&pid=10101#p10101 === Subject: Re: Do Mathematica applications EVER get updated? why not ask the individual developer ?? At least for my package I can tell you that I'm working on the next version and it should be finished at the end of the year. The Digital Image Processing package is also active developed and I'm sure the Mariusz Jankowski is working on the next version. Jens > Is it worth buying more Mathematica applications? > I am asking, because it seems these applications are hardly ever being > updated. > Look at the structural mechanics application > http://documents.wolfram.com/applications/structural/index.html > It is still version 1, and the copyright shows it was published in 1999. > This is 7 years ago !! This is like a million years ago in computer software > time. > The control systems application > http://documents.wolfram.com/applications/control/FrontMatter/0.2.html > single update. > The Signals and Systems application > http://www.wolfram.com/products/applications/signals/ > Still at version 1.2.1 from 1995 (this is 11 years ago). > And I can go on and on. > Another big problem is that it is almost impossible to figure what version > number these application are at and when was the last version released. It > is as if this is intentionally kept hidden. Look at the mechanical system > package > http://www.wolfram.com/products/applications/mechsystems/ > It says at the top NEW VERSION. but no version number or when was this > version released. When you scroll to the bottom of the page, it says > mechanical system 2. Then it says 1994-2005. Whatever this is supposed to > mean is anyone's guess. > Can any one from the company explain all of this? > I was about to buy the structural mechanics package, but last minute when I > checked the date on it, and that it was still version 1 since 1999, I closed > the window and did not buy it. It is clear that there are many dead > applications that are not being maintained and are still being sold to > customers. > Nasser === Subject: Re: Do Mathematica applications EVER get updated? Some of the applications do get updated but only to remain compatible with the latest version of Mathematica. I purchased a number of applications (database access, excel link, dynamic visualizer, finance essentials etc) in the past and have not seen a single update in terms of new features ever since. Sometimes application's functionality gets moved into Mathematica proper (e.g. database access). Alex