0
$\begingroup$

I have something like this:

Solve[f[x] == g[x] / h[x], x]

where g[x] are symbolic mononomials in x (eg a[i] b[1] x for i=1,...,n) and, similarly, f[x] is a monomial in x eg b[1](1-x).

I want to solve the equation for x, but Mathematica complains that This system cannot be solved with the methods available to Solve..

I understand that the reason Solve issues the Solve::nsmet message is due to the fact that there is h[x] in the denominator.

h[x] is an expression that is dependent upon x, but its exact dependence is not essential for the purpose at hand (think of it as a constant that just has to bear a sign reminding the user that it is-somewhere down along the line of computations-dependent upon x).

I understand that I can Solve the problem by simply removing the dependence of h on x eg

{sol} = Solve[{f[x] == g[x] / h, x]

will return

{{x->h/(h+Sum[a[i],{i,1,n}])}}

and I can, in turn, be explicit about h's dependence upon x with doing something like eg

sol = sol/.h->h[x]

that returns

{x->h[x]/(h[x]+Sum[a[i],{i,1,n}])}

I am asking if there is a less round-about way to achieve the same result.

$\endgroup$
3
  • $\begingroup$ But you're not solving for x if you're writing it in terms of h[x]. It's fine if that's what you want to do, but it's no surprise that Solve doesn't do it. $\endgroup$
    – jjc385
    Commented Sep 23, 2017 at 17:52
  • 1
    $\begingroup$ It seems like the way you do it is more or less the right way, but this can be automated somewhat with a wrapper function. Are you happy with having to specify in some way that h is to have its x-dependence ignored (with something like ignoreDependence[ h, x, Solve[ ... ] ], or do you want the function to be able to 'seek out' unevaluated functions such as h? (The latter case is harder to write and test; you'd probably need to specify more about what kind of functions h can appear; and I'm not entirely sure the solution would be well-defined.) $\endgroup$
    – jjc385
    Commented Sep 23, 2017 at 17:59
  • $\begingroup$ @jjc385: h[x] is not actually a function of x like eg f[x]=b[1](1-x) is; it can be considered an expression with head h and body x; for the purposes of Solve, h should be treated as a quantity free of x; I understand that what I'm saying might sound weird but in the context I'm working on it makes sense; a wrapper function makes sense as it would essentially automate the process I described in the question; I was hoping there would be a more elegant way of doing it (perhaps Hold-ing something or whatever) $\endgroup$
    – user42582
    Commented Sep 23, 2017 at 18:32

1 Answer 1

1
$\begingroup$
ClearAll[ignoringDependence]
SetAttributes[ignoringDependence, HoldAll]

ignoringDependence[patt_, Solve[eqs_, solveArgs__]] :=
 Reap[Solve[eqs /. p : patt :> Sow[Unique@"ignored", p] // Evaluate, 
    solveArgs], _, Rule[First@#2, #1] &] // Apply[ReplaceAll]

Then you can do

With[{f = b[1] (1 - #) &, g = #*b[1]*Sum[a[i], {i, 1, n}] &}, 

 ignoringDependence[h[x], Solve[f[x] == g[x]/h[x], x]]

 ]
{{ x -> h[x]/( h[x] + Sum[a[i], {i, 1, n}] ) }}

Note that you could also have done, e.g., ignoringDependence[_h, ... ], though that would have replaced e.g. h[x0] (but then put it back in the end, so it probably wouldn't make a difference anyway).

This would work at least in a limited way if you want to solve for multiple variables.

$\endgroup$
1
  • $\begingroup$ really interesting use of Reap[] and Sow[] $\endgroup$
    – user42582
    Commented Sep 24, 2017 at 11:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.