2
$\begingroup$

Consider a two-variable function as follows $$f(a,b)=\frac {T(a,b)+C(a,b)}{V(a,b)} \tag{1}$$ I define a $step$ and then define two new functions using (1) and step as $$f_+(a+step,b)=\frac {T(a+step,b)+C(a+step,b)}{V(a+step,b)} \tag{2}$$ and $$f_-(a-step,b)=\frac {T(a-step,b)+C(a-step,b)}{V(a-step,b)} \tag{3}$$ In fact $f_+$ and $f_-$ are two functions which have been changed by $+step$ and $-step$ for each parameter namely generally a $f_+$ defined as $$f_+(... a_i ...)=f(... a_i+step ...) \tag{4}$$ equations 1-3 were an example. How can I write a general form for $f_+$ and $f_-$ which adds to or subtracts step from a special variable each time and do this for all variables, do I need a loop? For the sake of simplicity suppose function (1).

Example:

I need to step is added to $i$th variable where i is the loop number (in fact I'm running this command inside a outer loop), so if currently loop number is 1, step must be added to $a$ in eq(1) and in the next time when $i$ is 2, step must be added to $b$ in eq(1) and so on. Equation (1) is a special case, but an arbitrary function can have n variable so I need each time one of them (regarding to loop number) changes.

$\endgroup$
7
  • $\begingroup$ The required syntax is almost the same as the traditional math notation: Clear[f, a, b]; f[a_, b_] = (T[a, b] + cc[a, b])/V[a, b]; fplus[a_, b_] = f[a + step, b] Why do you think you need a loop? $\endgroup$
    – xzczd
    Nov 29, 2019 at 6:34
  • $\begingroup$ because I need to use these $f_+$ and $f_-$ functions into a loop and in an algorithm where these functions are constructed regarding to number of loop, for example if the loop number is 3, I need to construct a $f_+$ function in which step added to its third variable $\endgroup$
    – Wisdom
    Nov 29, 2019 at 6:38
  • 1
    $\begingroup$ The order isn't important, right? Then you don't need loop: f @@@ Transpose[IdentityMatrix[3] step + {a, b, c}] $\endgroup$
    – xzczd
    Nov 29, 2019 at 6:45
  • $\begingroup$ No, order is important, in fact I don't determine that step must be added to which variable, but the loop number does. So if i be the loop number, step must added to ith variable and the $f_+$ is constructed for that special i $\endgroup$
    – Wisdom
    Nov 29, 2019 at 6:50
  • 1
    $\begingroup$ It would be nice if you present a minimal example. $\endgroup$
    – yarchik
    Nov 29, 2019 at 6:50

1 Answer 1

3
$\begingroup$
ClearAll[fpm];
fpm[g_, a__][step_, pos_: 1] :=  Module[{arg = MapAt[# + step &, {a}, {pos}]}, g @@ arg]

Examples:

ClearAll[f,t, v, a, b, c];
f[a_, b_] := (t[a, b] + c[a, b])/v[a, b]

fpm[f, a, b][step]

(c[a + step, b] + t[a + step, b])/v[a + step, b]

fpm[f, x, y][-step]

(c[-step + x, y] + t[-step + x, y])/v[-step + x, y]

fpm[f, u, t][-step, 2]

(c[u, -step + t] + t[u, -step + t])/v[u, -step + t]

h = Total[{##}^2] &;
fpm[h, x1, x2, x3, x4, x5, x6][-step, 3]

x1^2 + x2^2 + (-step + x3)^2 + x4^2 + x5^2 + x6^2

$\endgroup$
3
  • $\begingroup$ Thanks, however as I said in the above comment, I need a I need to use these $f_+$ and $f_−$ functions into a loop and in an algorithm where these functions are constructed regarding to number of loop. I think that I can set t in your code equal to my loop number to obtain desire result, right? $\endgroup$
    – Wisdom
    Nov 29, 2019 at 6:43
  • $\begingroup$ @Wisdom, not sure how you intend to use loop. Maybe you can use the argument pos as the "loop number" to iterate over the positions as in Table[fpm[f, a, b][step, i], {i, 2}] and Table[fpm[f, a, b][-step, i], {i, 2}]. $\endgroup$
    – kglr
    Nov 29, 2019 at 6:57
  • 1
    $\begingroup$ I added an example. In fact I wanted to construct $f_+$ and $f_-$ functions inside my loop, but seems your idea is good so that I construct a table of all possible functions and call one of them (regarding the loop number) inside my loop each time, right? $\endgroup$
    – Wisdom
    Nov 29, 2019 at 7:04

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.