Mathematica Mathematica: f[a,b,c] = a*g[b,c], force evaluate

  • Thread starter Thread starter Gwinterz
  • Start date Start date
  • Tags Tags
    Force Mathematica
AI Thread Summary
The discussion centers on optimizing function evaluations in Mathematica, particularly when dealing with nested functions. The user initially encounters an issue where Mathematica does not evaluate the inner function g[b, c] when computing f[a, b, c], leading to inefficient computations. The solution involves defining functions with underscores and using the Evaluate function to ensure conditions within If statements are processed correctly. A key point raised is the importance of avoiding omissions in code, as they can lead to misunderstandings and performance issues. Additionally, the conversation highlights the challenge of optimizing Mathematica code, with a suggestion to refer to resources that provide tips for writing faster code, emphasizing the potential inefficiency of symbolic manipulations when numerical computations would suffice.
Gwinterz
Messages
27
Reaction score
0
Hey,

Say I have a function f[a,b,c] which depends on another function g[b,c] such that:

f[a,b,c] = a*g[b,c]

If I input, f[a,1,2], mathematica spits out a*g[1,2] rather then actually evaluating what g[1,2] is. Is there a way I can force mathematica to evaluate g[1,2] so instead, e/g if g = b/c, mathematica will give:

f[a,1,2] = a/2

This is just a simple example, in what I am doing this lack of evaluating causes a significant increase to the computation time.

Thanks
 
Physics news on Phys.org
You want to tell Mathematica that your functions are to be treated as functions,
g[b_, c_] := b/c
f[a_, b_, c_] := a*g[b, c]
Note the underscores after the function variables.

Also see the tutorial.
 
Hey,

Thanks for your reply.

Sorry I omitted the formal code for the sake of explaining. I found the source of my problem, I didn't include it here because I didn't think it was the problem.

The problem was that I was using the If function, e.g.

f[a,b,c] = If[a>x,a*g[b,c],100*a*g[b,c]]

(omitting the _'s and :)

Mathematica would choose not to evaluate the two conditions until it decided if a>x or not. To fix it I put in Evaluates[..] around each of the conditions.

On the down side, this wasn't the reason for the increase computation time, still looking for that, but I won't bring up that problem here.

Thanks again,
 
Glad you solved it.

I find optimizing Mathematica code tricky, as there generally is several ways of doing the same thing, and it's often not obvious why one should be quicker than the other. Anyway, there's this guide 10 tips for writing fast Mathematica code. You might find it helpful too.
 
The reason behind the low performance is probably utilising symbolic manipulations where numerical would suffice.

Gwinterz said:
I was using the If function, e.g.

f[a,b,c] = If[a>x,a*g[b,c],100*a*g[b,c]]

(omitting the _'s and :)

Why would you omit anything? With underscores and := this would be an entirely different definition.

The way it's written here it looks alarming for several reasons. Don't omit anything because it makes it unnecessary hard to understand what's happening.
 

Similar threads

Replies
4
Views
2K
Replies
5
Views
3K
Replies
1
Views
2K
Replies
5
Views
3K
Replies
22
Views
3K
Replies
5
Views
3K
Replies
5
Views
2K
Replies
3
Views
4K
Back
Top