# Extracting part of an expression

My goal is to solve $(\alpha - \dfrac{1}{\alpha}) - (\beta- \dfrac{1}{\beta}) = \dfrac 1 n$ where $\alpha$ and $\beta$ are rational and $n$ is a positive integer. Towards finding a particular solution, we will let $\beta = \dfrac{u}{v \alpha}$ where $u > v$ are positive integers.

myExpression[α_, β_]  := (α  - 1/α) - (β - 1/β)

myBase[u_, v_] := If[EvenQ[u] || EvenQ[v], (u v)/2, u v]

myQuad[u_, v_] := α /.
Solve[myExpression[α, u/(v α)] == 1/(myBase[u, v] k), α][[2]]

test = myQuad[3,2]


$\frac{1 + \sqrt{1 + 150 k^2}}{10 k}$

For $u = 3$ and $v = 2$ I get the expression shown above. What I need is to extract the 150 from the expression.

• This looks like a math question, rather than a question about the software Mathematica. – bbgodfrey May 5 '15 at 16:02
• If you know for a fact, that this will be the form of your expression, you can alway take a pattern-matching approach or taking the appropriate part. I'd start with putting the expression in TreeForm – LLlAMnYP May 5 '15 at 18:17
• This looks like the root of a quadaratic equation of the form $\alpha k x^2 - x - \beta k = 0$, in which case the $n$ in your equation will always be $n = 4 \alpha \beta$. You might take a look at the equation you're solving and see if you can extract $\alpha$ and $\beta$ more easily at that step. – Michael Seifert May 5 '15 at 18:23
• Try this, for example. Not sure, how robust it is, but should work for simple cases. (1 + Sqrt[1 - 150 k^2])/(m k) /. {(1 + Sqrt[1 + n_ k^2])/(m_ k) -> n} – LLlAMnYP May 5 '15 at 18:24
• you can readily get the result analytically, a b (a+b)^2 times 4 if both odd. – george2079 May 6 '15 at 4:00

## 2 Answers

If you really want to parse the output you can do this (including a check for validity )

 Module[{m, n},
{n, m} = {
First@Cases[#  , Sqrt[1 + n_ k^2] :> n, {2}] ,
1/#[[1]]} ;
If[Simplify[(1 + Sqrt[1 + n k^2 ])/(m k) == #] , {n, m} ]] &@ myQuad[4, 3]


This only works because your function seems to always return exactly the same form (At least I couldn't find a case to break it )

• I'm not going to be able to test this until this Sunday. But it looks promising. For a second, I thought it was written in APL. – Steven Gregory May 6 '15 at 20:05

Consider the function $f(k) = (1 + \sqrt{1 + n k^2})/m$ (note that this is your expression times $k$). We have $$f(0) = \frac{2}{m}$$ $$f''(0) = \frac{n}{m}$$ So the following Mathematica code does the trick:

(2 D[f, {k,2}] / f) /. k -> 0


In your case, you would need to define f = (your quantity)*k. Of course, this isn't really a solution by "parsing", but it does the trick.

• You could also use f(k) = Numerator[$\alpha$] – Steven Gregory May 6 '15 at 11:02