Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I want to define a function of two variables, func[f_, g_], such that when it sees a general constant multiplying one of the variables, say a, it will take it outside the evaluated expression. So for example, if f and g are variables and a is a constant multiplying g, it will be evaluated as

func[f_, a_ g_]:= a func[f_, g_]

I need to inform Mathematica that a is a constant, not a variable, and that it should be pulled out of the expression.

share|improve this question
    
m_goldberg, I know your edit was intended to be helpful, and the vast majority of your edits are, but I think it has been discussed that fixing syntax such as func(a, b) = . . . is a bad idea because the that is often a problem that needs to be addressed in the answers rather than "swept under the rug" as it were. –  Mr.Wizard Jul 31 at 1:28

2 Answers 2

I think you need a clear differentiation of "constant" and "variable" and I don't see that in your question. For the sake of the example let us consider anything numeric to be a constant and anything else to be a variable, so my PatternTest function will be NumericQ.

func[f_, a_?NumericQ g_] := a func[f, g]

Now:

func[foo, 6 bar]

func[x, Pi y^2]
6 func[foo, bar]

π func[x, y^2]
share|improve this answer
    
ok, but with this solution I want to insert a customized constant, say "a", and have the function pull it out. i.e. func[foo, a bar] will yield a func[foo, bar] –  Gogo Jul 31 at 21:41
    
@Gogo Do you have a list of expressions that you wish to consider constant? Only literal a? Something else? –  Mr.Wizard Jul 31 at 22:27
    
Yes, I have a specific parameter. I want a factor "1/M" to be such a constant –  Gogo Aug 1 at 13:58

Assuming g is the only variable in the second position and any thing before is a constant:

ClearAttributes[Times, Orderless];
func[f_, a__ g_] := a func[f, g]

func[f, 3 g]
func[f, 3 Pi g]
func[f, 3 Pi a^3 g]
(*3 func[f, g]*)
(*3 \[Pi] func[f, g]*)
(*3 \[Pi] a^3 func[f, g]*)
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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