Re: What does & mean?
- To: mathgroup at smc.vnet.net
- Subject: [mg107087] Re: [mg107050] What does & mean?
- From: Murray Eisenberg <murray at math.umass.edu>
- Date: Tue, 2 Feb 2010 03:29:41 -0500 (EST)
- Organization: Mathematics & Statistics, Univ. of Mass./Amherst
- References: <201002011114.GAA22737@smc.vnet.net>
- Reply-to: murray at math.umass.edu
The short answer: you don't actually need that #1 == #2& there. You could use simply: DSolve[MapThread[Equal, {X'[t], A.X[t]}], {x[t],y[t]}, t] [I prefer to solve for things like x[t] rather than x, but you can change that 2nd argument of DSolve to {x,y} if you wish.] The long answer: First, let's understand why something like MapThread is needed at all. If you evaluate each of X'[t] and A.X[t] you'll get a 2-element list in each case, namely: X'[t] {x'[t],y'[t]} A.X[t] {4 x[t] - 6 y[t], x[t] - y[t]} What you want to do is to solve together the pair of differential equations: x'[t] == 4 x[t] - 6 y[t] y'[t] == x[t] - y[t] In other words, what you want to construct is the expression: DSolve[{x'[t]==4x[t]-6y[t],y'[t]==x[t]-y[t]}, {x[t],y[t]}, t] In a perfect world, Mathematica would save you the work of separating out the two equations if you just used instead: DSolve[{x'[t], y'[t]} == {4x[t]-6y[t], x[t]-y[t]}, {x[t],y[t]}, t] That is, you would expect Mathematica to "thread" the Equal function (==) over the two lists {x'[t], y'[t]} and {4x[t]-6y[t], x[t]-y[t]} so as to automatically create what you really wanted. More generally -- and this should help to understand what's involved here -- given two lists {a,b} and {c,d}, you'd like Mathematica to automatically convert (a,b} == {c,d} into: {a == c, b == d} In earlier versions, Mathematica never (or hardly ever) made such conversions. With more recent Mathematica versions, Mathematica does do it in some situations, e.g., with Solve. For example: Solve[{x+y,2x+3y}=={4,5},{x,y}] does automatically solve the pair of simultaneous algebraic equations. But this automatic threading has not yet permeated to all situations. And one of the latter is DSolve. (We hope it will soon!) So you have to tell Mathematica explicitly to do such threading for DSolve. How? Let's go back to the more general situation again: (a,b} == {c,d} The function that does this is MapThread with first argument Equal (that is, ==) and second argument the list consisting of the two lists {a,b} and {c,d}: MapThread[Equal, { {a,b}, {c,d} }] {a == c, b == d} Perhaps you'd really like to use the standard abbreviation == for Equal, like this: MapThread[==, { {a,b}, {c,d} }] Unfortunately, that's illegal syntax in Mathematica. So you need to do the next best thing if you insist on using == instead of Equal: create a function F of two variables that's denoted in a way that incorporates the == abbreviation and that does not violate syntax. That is, F should do: F[one, two] one == two You can create such an F "on the fly", without creating any new name for the function, if you use a "pure function" -- what in some languages is called a lambda-function: Function[{var1, var2}, var1 == var2] This works: for example: Function[{var1, var2}, var1 == var2][one,two] one == two But that Function expression is much too wordy. So Mathematica has a shorthand notation for such a pure function. In this notation, instead of using explicit names for the variables you use #1 and #2; and to indicate that you're forming a pure function without using "Function", you terminate the expression for the pure function with the & characters. Thus: #1 == #2& means exactly the same thing as Function[{var1, var2}, var1 == var2], so that: #1==#2&[one,two] one == two Thus the alternative to the general MapThread[Equal, { {a,b}, {c,d} }] is MapThread[#1 == #2&, { {a,b}, {c,d} }] and the result is the same: {a == c, b == d} Finally, back to the differential equation, we see that MapThread[#1 == #2&, {X'[t], A.X[t]}] gives the same pair of differential equations as MapThread[Equal, {X'[t], A.X[t]}] and thus DSolve[MapThread[#1 == #2&, {X'[t], A.X[t]}], {x[t],y[t]}, t] solves the same system as does the slightly shorter: DSolve[MapThread[Equal, {X'[t], A.X[t]}], {x[t],y[t]}, t] As I suggested earlier, perhaps in a new version of Mathematica soon it will no longer be necessary to do that. On 2/1/2010 6:14 AM, Michael Knudsen wrote: > Hi, > > I have recently bought Mathematica, and I have a really tough time > getting started. I'm reading the various documents found under > "Complete Documentation" at the Mathematica homepage, but it doesn't > feel like the right place to start. > > For example, I'm now trying to solve some simple differential > equations, and the documentation provides the following example: > > A = {{4, -6}, {1,-1}}; > > X[t_] = {x[t], y[t]}; > system = MapThread[#1 == #2&, {X'[t], A.X[t]}]; > > sol = DSolve[system, {x,y}, t] > > However, there is no explanation of how& works here (and it isn't in > the MapThread documentation either). Where should one start reading in > order to understand basic constructs like this? This particular > example is really nasty, since& is generally ignored by search > engines. > > Thanks, > Michael Knudsen > -- Murray Eisenberg murray at math.umass.edu Mathematics & Statistics Dept. Lederle Graduate Research Tower phone 413 549-1020 (H) University of Massachusetts 413 545-2859 (W) 710 North Pleasant Street fax 413 545-1801 Amherst, MA 01003-9305
- References:
- What does & mean?
- From: Michael Knudsen <micknudsen@gmail.com>
- What does & mean?