1
$\begingroup$

I have a function in three variables:

f[var1_,var2_,var3_]:=expression[var1,var2,var3];

I want to reduce this to g[var3] and have it output f[10,20,var3].

Hopefully you get the idea. I want to create a new function g by evaluating f at two of its three inputs, how do I do this?

Any help is appreciated.

$\endgroup$

closed as off-topic by Anton Antonov, m_goldberg, Roman, Bob Hanlon, Carl Woll May 31 at 22:29

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – Anton Antonov, m_goldberg, Roman, Bob Hanlon, Carl Woll
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 6
    $\begingroup$ Use f[10,20,var3], that is, drop the blank (_) on the RHS. $\endgroup$ – Carl Woll May 31 at 19:07
2
$\begingroup$

B.! @Carl Woll's answer is a great one, it would look like this:

f[var1_,var2_,var3_]:=expression[var1,var2,var3];
g[altvar3_]:=f[10,20,altvar3];

Notice that you don't have to stick to the same naming convention for the expressions which you create another function for. Though, I may suggest a method that looks a bit fancier, and uses pure functions, which I prefer to use in code to save space, though with taking a hit on readability if the user is unfamiliar. Find it below:

g=Evaluate[f[10,20,#]]&;

Off hand, I don't know if it runs any faster or does much more than make you use pure functions, but hey! It's a different answer :D

Also notice that you don't necessarily need to use a := SetDelayed method of defining the function, and maybe this will help you come up with some neat tricks where you define the initial function one way, use this redefinition/reduction method to define g and then ClearAll and Remove the initial function definition, as the = Set would evaluate the initial function at that very moment, but actually you have to wrap it in an Evaluate if you are to do what I just suggested, but my note about the use of := remains true to form, as that would evaluate f upon execution of your redefined/alternative function g.

g:=f[10,20,#];
g=f[10,20,#];
g:=Evaluate[f[10,20,#]]&;

Will all accomplish what you desire, and all of these would carry the same required input of g[altv3] to achieve evaluation!

Hope this helps!

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.