0
$\begingroup$

Consider the following program:

g = 2f;
g[x] (* (2f)[x] *)

Is there any way I can change the above expression to have it return 2 f[x]?

With Hold construct perhaps? Sorry, I'm really not familiar with these Hold* functions.

I know I could easily define g=2f[#]& and it would work fine, but in my case what I'm really trying to achieve is something like that :

Transpose @ MapThread[Standardize[{##}, Mean, 2 StandardDeviation] &, xs]

Unfortunately, (2 StandardDeviation) is taken as a single entity. Having a whole Function would make it much more messy.

$\endgroup$
3
  • $\begingroup$ There isn't really a better way, just alternatives with similar level of complexity: For example: g = (2#)&@*f uses Composition. $\endgroup$
    – Jens
    Commented Apr 12, 2015 at 4:53
  • $\begingroup$ Yeah, I think I'll just settle for Function[2 StandardDeviation[##]] $\endgroup$
    – Literal
    Commented Apr 12, 2015 at 4:54
  • $\begingroup$ @Kuba OK, I've added an answer... $\endgroup$
    – Jens
    Commented Dec 16, 2015 at 21:59

1 Answer 1

1
$\begingroup$

As mentioned in the comments, you could use Composition as follows:

g = (2 #) &@*f

(* ==> 2 #1 &@*f *)

g[x]

(* ==> 2 f[x] *)

Your own solution using Function is of course also fine, and one could make it slightly more user-friendly by defining the corresponding scalar multiplication operation something like this:

CenterDot[x_, y_] := Function[z, x y[z]]

g = 2·f

(* ==> Function[z$, 2 f[z$]] *)

g[x]

(* ==> 2 f[x] *)

This would allow you to use CenterDot to define similar constructs on the fly in a more readable form, by using the keyboard shortcut esc.esc ot enter the scalar multiplication. So as an in-line definition this would look like this:

(2·h)[x]

(* ==> 2 h[x] *)
$\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.