Group Abstract Group Abstract

Message Boards Message Boards

0
|
104 Views
|
5 Replies
|
7 Total Likes
View groups...
Share
Share this post:

Condensing the last line of monotonicity-check code

Posted 5 days ago
{f[x_] = (E - x)/(E x), g[x_] = (-1 + 2 x^2)/x}
{Reduce[{#[f[x], 0]}, x, Reals] & /@ {Less, Greater}, 
 Reduce[{#[g[x], 0]}, x, Reals] & /@ {Less, Greater}}

The last line of this code separately finds the ranges of x for which the two inequalities are greater than 0 and less than 0; they differ only in the inequality sign. For such code patterns that yield identical results, how can the code be shortened so that a single unified piece of code can simultaneously determine the ranges of x corresponding to “greater than 0” and “less than 0” for both inequalities?

My own attempt goes like this, but it still feels not concise enough—the Less and Greater function calls are repeated, and the two function variables are duplicated.

Reduce[{#1[#2, 0]}, x, Reals] & @@@ {{Less, f[x]}, {Greater, 
   f[x]}, {Less, g[x]}, {Greater, g[x]}}
POSTED BY: Bill Blair
5 Replies
Posted 5 days ago
f[x_] = (3 x - 2)/(7 x - 6);
g[x_] = 7 - Log[x];
fd = FunctionDomain[f[x], x];
gd = FunctionDomain[g[x], x];


functionData = {<|"fn" -> f, "dom" -> fd|>, <|"fn" -> g, "dom" -> gd|>};


reductionFunction[fnData_, comp_] := 
  Reduce[{fnData["fn"][x]~comp~0, fnData["dom"]}, x, Reals];


Outer[reductionFunction, functionData, {Less, Greater}]
POSTED BY: Bill Blair

The domains are not independent of the functions. Each function needs to be joined with its domain and not in a separate, independent list. To anticipate further addenda, here is the general framework for using Outer[], which in this problem should have only two list arguments, one containing the data for each function and one containing the comparators:

Outer[reductionFunction, functionDataObjects, {Less, Greater}]

reductionFunction[fnData, comp] should be a function with two arguments, the function data and the comparator.

Outer[] produces all combinations of elements taking one from each list from the second argument on. Compare two lists to three lists:

Outer[R, {f, g}, {c1, c2}]
(*
{ {R[f, c1], R[f, c2]},  {R[g, c1], R[g, c2]} }
*)

Outer[R, {f, g}, {df, dg}, {c1, c2}]
(*
{{ {R[f, df, c1], R[f, df, c2]},  {R[f, dg, c1], R[f, dg, c2]} },
 { {R[g, df, c1], R[g, df, c2]},  {R[g, dg, c1], R[g, dg, c2]} }}
*)
POSTED BY: Michael Rogers
Posted 5 days ago

What should be done if we expand the domain of each function individually?

{f[x_] = (3 x - 2)/(7 x - 6), g[x_] = 7 - Log[x]}
{fd = FunctionDomain[f[x], x], gd = FunctionDomain[g[x], x]}
Outer[Reduce[{#3[#1[x], 0], #2}, x, Reals] &, {f, g}, {fd, gd}, {Less,
   Greater}]

The results contain duplicates.

{{{2/3 < x < 6/7, x < 2/3 || x > 6/7}, {2/3 < x < 6/7, 
   0 < x < 2/3 || x > 6/7}}, {{x > E^7, 
   6/7 < x < E^7 || 0 < x < 6/7}, {x > E^7, 0 < x < E^7}}}

enter image description here

How to handle the problem of expanding the domain?

POSTED BY: Bill Blair

Why not write a function "funcNegPos[f_, x_] :=.." that returns the intervals where f is negative (resp. positive), and use that on f and g? Sacrifice concision for clarity. It makes programming easier in the long run.

That said, here's another short way, similar to Gianluca's but using Outer[] on a function instead of Table[] on a symbolic formula. (They are roughly equivalent, mutatis mutandis, ignoring occasional performance differences.)

Outer[Reduce[{#2[#1[x], 0]}, x, Reals] &, {f, g}, {Less, Greater}]
POSTED BY: Michael Rogers

Here is an attempt:

Table[{rel[func, 0], "\[DoubleLeftRightArrow]", 
  Reduce[rel[func[x], 0], x]},
 {func, {f, g}}, {rel, {Less, Greater}}]
Flatten[%, 1] // TableForm
POSTED BY: Gianluca Gorni
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard