# Extracting equations from Piecewise expressions

Say I have a PDF:

PDF[LogNormalDistribution[1.75, 0.65], x]


Calculating it, Mathematica gives me an expression that looks like this:

I want to extract the equation itself so I can use it in other calculations. On a one off basis, I can do that with Part[]:

PDF[LogNormalDistribution[1.75, 0.65], x][[1, 1, 1]]


which gives me the following:

That's fine, but I need something more universal. For instance, for a different PDF that looks like this:

I could get what I need with:

%[[1, 2, 1]]


You see my dilemma. How do I extract the business part of such expressions in a more universal way so I can use them in other calculations and functions that may need to deal with a variety of such expressions?

-

Here is a way to separate your equation into different subparts. It uses Reap and Sow to tag parts of the expression as either "equation" or "conditions" or "constants".

f = Which[FreeQ[#, x], Sow[#, "constants"],
MemberQ[{Equal, Unequal, Greater, GreaterEqual, Less, LessEqual, And, Or}, Head[#]],
Sow[#, "conditions"],True, Sow[#, "equation"]] &;

Last@Reap[Scan[f, List @@ PDF[LogNormalDistribution[1.75, 0.65], x] // Flatten],
{"equation", "conditions", "constants"}] ~Flatten~ 1

(* Out[1]= {{(0.613757 E^(-1.18343 (-1.75 + Log[x])^2))/x}, {x > 0}, {0}} *)


So now you have your equations in the first sublist, conditions in the second and constants in the third. If you need only the first, you need to Reap only that. This should work with your second example too.

Note that you'll have to be careful to ensure that x above does not have any value — using formal symbols is better.

-
Many thanks. Your suggestion works nicely in my application and you grasped the intent of my question relative to what I needed to do exactly. This might not work in every situation, but in mine where I need to assess pdfs it show work perfectly. – Jagra Apr 5 '12 at 19:41

There is a very simple answer here:

pdf = PDF[LogNormalDistribution[1.75, 0.65], x];
pdf[[1, 1, 1]]

(* (0.613757 E^(-1.18343 (-1.75 + Log[x])^2))/x *)


To see that you need {1,1,1}, and not another term use the TreeForm expression:

TreeForm[pdf]

-

"How do I extract the business part of such expressions in a more universal way?"

Well, do you always know what the "business part" of your expression is?

For instance,

pw = Series[Piecewise[{{Cos[x], x <= 0}}, E^x], {x, a, 2}]


gives:

I believe you're thinking of the most complex piece in the Piecewise expression. If that's the case you could use a function like LeafCount to provide a measure of complexity.

SortBy[pw[[1, All, 1]], LeafCount][[-1]]


pw = Piecewise[{{x^2 + x, x > 0}, {0, x == 0}, {1, x < 0}}]


SortBy[pw[[1, All, 1]], LeafCount][[-1]]


-
While not as relevant as the solution R.M. supplied for my specific application, I very much appreciate your input on this. I learned something which should prove very useful in the future. Thanx. – Jagra Apr 5 '12 at 19:44

Since you get the result in terms of Piecewise, you can use things like Refine or Simplify, particularly when you want to get a result given some additional condition on your variables. In particular,

Refine[PDF[LogNormalDistribution[1.75, 0.65], x], x > 0]

(* ==>  (0.613757 E^(-1.18343 (-1.75+Log[x])^2))/x  *)

-
@Leonid_Shifrin I'm confused. To get what I want with Refine, it seems I would need to know the condition(s) before hand. Identifying the conditions programmatically seems much like the same problem with which I started. – Jagra Apr 5 '12 at 17:54
@Jagra, if you don't know the condition beforehand, explain in words how would you choose what expression you want to extract for the general case (in which you might have more than a single "big formula" – Rojolalalalalalalalalalalalala Apr 5 '12 at 18:17
@Jagra You have to somehow identify the piece in your function you want to extract. You did this by indicating position of it inside Piecewise, but were apparently unsatisfied because that was fragile. I agree. But to have it not fragile, the only option I see is to use conditions for this, at least if you want to do this programmatically. – Leonid Shifrin Apr 5 '12 at 18:17