If my interpretation of your question is right (and that is quite uncertain), then the following definition of g
should work for you. If my interpretation is wrong, perhaps this will help you to revise your question, so that a correct interpretation might more obvious. Note that I had to generate my own array for points
since you did not provide one.
With[{points = Table[{x, 1./(1. + .5 x)}, {x, 0, 2, .1}]},
g[x_Real, h_Symbol] :=
(If[0. <= 2. #[[2]] - x <= 1., h[#[[1]]] = .5 x + .5] & /@ points;)]
With this, evaluating g
for various values of x
gives
Clear[a]; g[0., a]; DownValues[a]
{HoldPattern[a[2.]] :> 0.5}
Clear[b]; g[.5, b]; DownValues[b]
{HoldPattern[b[0.7]] :> 0.75, HoldPattern[b[0.8]] :> 0.75,
HoldPattern[b[0.9]] :> 0.75, HoldPattern[b[1.]] :> 0.75,
HoldPattern[b[1.1]] :> 0.75, HoldPattern[b[1.2]] :> 0.75,
...
HoldPattern[b[1.9]] :> 0.75, HoldPattern[b[2.]] :> 0.75}
Clear[c]; g[1., c]; DownValues[c]
{HoldPattern[c[0.]] :> 1., HoldPattern[c[0.1]] :> 1.,
HoldPattern[c[0.2]] :> 1., HoldPattern[c[0.3]] :> 1.,
HoldPattern[c[0.4]] :> 1., HoldPattern[c[0.5]] :> 1.,
...
HoldPattern[c[1.9]] :> 1., HoldPattern[c[2.]] :> 1.}
Clear[d]; g[1.5, d]; DownValues[d]
{HoldPattern[d[0.]] :> 1.25, HoldPattern[d[0.1]] :> 1.25,
HoldPattern[d[0.2]] :> 1.25, HoldPattern[d[0.3]] :> 1.25,
...
HoldPattern[d[0.5]] :> 1.25, HoldPattern[d[0.6]] :> 1.25}
Clear[e]; g[2., e]; DownValues[e]
{HoldPattern[e[0.]] :> 1.5}
The discrete functions a, b, c, d, e
all have the value of 5{x + 1)
for each value of points[[i, 1]]
where points[[i, 2]]
satisfies your inequality for the given x
.
Update
I write this to address an issue raised in a comment below. It appears that my use of concise operator notation is confusing. I will rewrite g
, spelling out all the non-arithmetic operators appearing in its body. This will make it easy for anyone working out how g
operates to look up its constituent functions in the docs to get more info.
With[{points = Table[{x, 1./(1. + .5 x)}, {x, 0, 2, .1}]},
g[x_Real, h_Symbol] :=
Map[
Function[{pt},
If[LessEqual[0., 2. Part[pt, 2] - x, 1.], h[Part[pt, 1]] = 0.5 + 0.5 x]],
points];]
Map
takes two arguments (ignoring level specs for now), the 1st a being a function of one variable (here a pure function) and the 2nd being a list (here points
). It calls the function once for each element in the list, binding the formal argument pt
to the list element. Hence, for each call pt
is bound to one of the pairs {v, w}
from the list. Therefore, Part[pt, 1]
is v
and Part[pt, 2]
is w
.
g
should have no output? I.e. it's sole purpose is to set the values forh
? Secondly, it seems like in the general case,h
for a particularv
could be set to different values, since you scan over possiblex
's. Either the list is such that this doesn't happen, or you want to define anh
for everyx
. I'm not sure which it is. (Or some third option based on my misunderstanding of the problem.) $\endgroup$g
beh
, if that makes sense, but I couldn't find a way to make that work). Secondly, I think you may be correct. However, if I make the $\leq$ be $<$ then no $h(v)$ (h for no v) can be set to two different values (although differentv
's could give the sameh(v)
which I am okay with). It is also very possible that I incorrectly articulated my idea. Thirdly, thank you for your comment. $\endgroup$