I have an array of expressions, each depends on a
.
I want to find the minimal positive value, as it depends on a
, without having to substitute for a.
For example, if the array is [a^2, 1-2a, 1]
, then the function, call it MinPositive
would return:
(MinPositive[a^2, 1-2a, 1]) /. a-> 0
0
(MinPositive[a^2, 1-2a, 1]) /. a-> 0.7
0.7^2
and so on.
Any ideas?
I would appreciate help to write the MinPositive
function so that it can be used, for example, instead of the regular Min
function.
Thanks.
-
Oh, I think your answer was reasonable! Can you please restore it? I want to try it out, I think that if MinPositive can be used just as Min is used, it would work, because I already tried integrating with the Min function and it works fine.kloop– kloop04/19/2013 14:51:48Commented Apr 19, 2013 at 14:51
3 Answers 3
Did you have something like this in mind? Return the expression that retsults in the min value..
minp[lst_, a_, v_] := (
pos = Select[lst, ((# /. a -> v) > 0) &];
Last@Sort[pos , ( (#1 /. a -> v ) > (#2 /. a -> v )) &])
minp[{a^2, 1 - 2 a, 1}, a, .2] -> a^2
minp[{a^2, 1 - 2 a, 1}, a, .48] -> 1-2 a
minp[{a^2, 1 - 2 a, 1}, a, 2] -> 1
The expression
[a^2, 1-2a, 1]
is not a well-formed Mathematica expression, perhaps you mean
{a^2, 1-2a, 1}
which is a valid expression for a list of 3 elements. Mathematica doesn't really do arrays as such, though lists can generally be used to model arrays.
On the other hand the expression
MinPositive[a^2, 1-2a, 1]
is a valid call to a function called MinPositive
with 3 arguments.
All that to one side, I think you might be looking for a function call such as
MinPositive[{a^2, 1-2a, 1}/.a->0]
in which the value of 0
will be substituted for a
inside the call to MinPositive
but will not be applied outside that call.
It's not clear from your question whether you want help to write the MinPositive
function; if you do, edit your question and make it clear. Further, your question title asks for the maximum positive value, while the body of your question refers to minima. You might want to sort that out too.
EDIT
I don't have Mathematica on this machine so I haven't checked this, but it should be close enough for you to finish off:
minPositive[lst_List] := Min[Select[lst,#>0&]]
which you would then call like this
minPositive[{a^2, 1-2a, 1}]
(NB: I avoid creating functions with a name with an initial capital letter.)
Or, considering your comment, perhaps you want something like
minPositive[lst_List, rl_Rule] := Min[Select[lst/.rl,#>0&]]
which you would call like this:
minPositive[{a^2, 1-2a, 1},a->2]
EDIT 2
The trouble, for you, with an expression such as
(MinPositive[a^2, 1-2a, 1]) /. a-> 0
is that the normal evaluation loop in Mathematica will cause the evaluation of the MinPositive
function before the replacement rule is applied. How then can Mathematica figure out the minimum positive value in the list when a
is set to a particular value ?
-
Thanks. I actually don't want to apply the substitution inside the call, but instead I want it to act like the Min function, where it is substituted on demand, when the expressions are actually evaluated. And yes, I do need help with the MinPositive function.user2299502– user229950204/19/2013 14:25:18Commented Apr 19, 2013 at 14:25
-
But the use of
/.
does, as you seem to want, substitute on demand when the expressions are actually evaluated. I'm now extremely uncertain about what you want in this respect.High Performance Mark– High Performance Mark04/19/2013 14:27:23Commented Apr 19, 2013 at 14:27 -
thanks! I want to be able to Integrate over MinPositive, that's why I want it to be substituted on "demand" by the integration mechanism.kloop– kloop04/19/2013 14:42:11Commented Apr 19, 2013 at 14:42
To prevent evaluation of the arguments before calling the body of the function is achieved by setting the attributes of the function to HoldAll (prevents evaluations of all arguments), HoldFirst (prevents the evaluation of the first argument only) or HoldRest (prevents the evaluation of all but the first argument). In addition, since "a" by itself is not an argument, you need to use Block to isolate it from (potential) definitions for "a"
so
SetAttributes[minPositive, HoldAll]
minPositive[lst_List] := Block[{a},Min[Select[lst /. a -> 0, # > 0 &]]]
and even if you explicitly set a to some other value, say
a=3
than
minPositive[{a^2, 1 - 2 a, 100}]
returns 9 as expected
HTH
yehuda