Re: Piecewise and multiple values
- To: mathgroup at smc.vnet.net
- Subject: [mg90001] Re: Piecewise and multiple values
- From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
- Date: Thu, 26 Jun 2008 04:41:43 -0400 (EDT)
- Organization: The Open University, Milton Keynes, UK
- References: <g3t6vs$imi$1@smc.vnet.net>
Giacomo Ciani wrote: > Hi all, > > in brief my problem: I'm using mathematica to create the solutions for > problems that actually came in few different versions (meaning that > the problem in the same, but the initial data change). Suppose I ave > version 1 in which m=3,k=1,..., version 2 in which m=5,k=2,... and so > on... Typically, I use an approach like: > > data={m->{3,5,6},k->{1,2,7},...} > > Then I write and solve the general symbolic equations, and the replace > the values with the above rule, like: > > sol = Solve[...] > sol/.data > > obtaining a list of numerical solutions corresponding to the different > set of data contained in the "data" list, like: > > {(num sol corresponding to m=3,k=5,...),(num sol corresponding to > m=5,k=2,...)} > > This works very well for me, because it allows me to solve the problem > once and check the numerical solution for all the different versions > at a glance. > > The problem arises in the case I have to use a Piecewise function. In > this case I define: > > | f x<z > h = | > | g x>z > > but when I evaluate > > f/.data > > I obtain something like: > > | {f1,f2,...} x<{z1,z2,...} > h = | > | {g1,g2,...} x>{z1,z2,...} > > that I don't know how to plot or "disentagle", i.e. obtain expressions > like: > > | f1 x<z1 > h1 = | > | g1 x>z1 > > > | f2 x<z2 > h2 = | > | g2 x>z2 > > and so on... > > Do you have any suggestion? I would prefer to retain my data > replacement scheme, and just understand how to create "disentangled" > piecewise functions... Hi Giacomo, I believe that something along the line Thread /@ data // Transpose // ReplaceAll[f[x], #] & should do what you are looking for (though I am not a hundred percent sure to have fully grasped all of your requirements). For instance, data = {m -> {3, 5, 6}, k -> {1, 2, 7}}; f[x_] := Piecewise[{{m + k, x < m}, {m - k, x >= m}}] Thread /@ data // Transpose // ReplaceAll[f[x], #] & (* returns *) {\[Piecewise] { {4, x < 3}, {2, x >= 3} }, \[Piecewise] { {7, x < 5}, {3, x >= 5} }, \[Piecewise] { {13, x < 6}, {-1, x >= 6} }} The following should illustrate how the above expression works: Thread[data[[1]]] {m -> 3, m -> 5, m -> 6} Thread /@ data {{m -> 3, m -> 5, m -> 6}, {k -> 1, k -> 2, k -> 7}} Thread /@ data // Transpose {{m -> 3, k -> 1}, {m -> 5, k -> 2}, {m -> 6, k -> 7}} Thread /@ data // Transpose // ReplaceAll[m + k, #] & {4, 7, 13} Regards, -- Jean-Marc