0
$\begingroup$

Is there any way to multiply certain terms within a vector with a constant? e.g. assume that I start with a vector

{f[4] - g[-1] , h[2] , 2 * g[2] + h[-1]}

where f[ i ], g[ i ], h[ i ] are functions that I'd like to keep in their general form. I would like to multiply every function that is evaluated at a negative number with a constant c. The above example would then look like

{f[4] - c * g[-1] , h[2] , 2 * g[2] + c * h[-1]}

$\endgroup$
3
  • 1
    $\begingroup$ Clear["*"]; {f[4] - g[-1], h[2], 2*g[-2] + h[1]} /. g[x_] -> c*g[x]` $\endgroup$
    – cvgmt
    Sep 28, 2020 at 0:56
  • $\begingroup$ Maybe I should've made clear in my example that any function at a negative position is supposed to get the pre-factor of c, not only the g[]'s. I edited my example. $\endgroup$
    – xabdax
    Sep 28, 2020 at 1:01
  • $\begingroup$ {f[4] - g[-1], h[2], 2*g[2] + h[-1]} /. y_[x_ /; x < 0] -> c*y[x] $\endgroup$
    – cvgmt
    Sep 28, 2020 at 1:08

2 Answers 2

3
$\begingroup$

Does it work?

{f[4] - g[-1], h[2], 2*g[2] + h[-1]} /. y_[x_ /; x < 0] -> c*y[x]
$\endgroup$
1
  • $\begingroup$ {f[4] - g[-1], h[2], 2*g[2] + h[-1]} /. y_[x_?(# < 0 &)] :> c*y[x] $\endgroup$
    – cvgmt
    Sep 29, 2020 at 0:19
0
$\begingroup$
{f[4] - g[-1], h[2], 2 g[2] + h[-2], g[2] + h[-z]} /. 
     y_[x_?Internal`SyntacticNegativeQ] :> c y[x]
 {f[4] - c g[-1], h[2], 2 g[2] + c h[-2], g[2] + c h[-z]}
$\endgroup$

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.