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
Click For Summary
SUMMARY

The discussion focuses on optimizing function evaluations in Mathematica, specifically the function f[a,b,c] defined as f[a,b,c] = a*g[b,c]. Users encountered issues with Mathematica not evaluating g[1,2] due to the use of the If function without proper evaluation directives. The solution involves defining functions with underscores and using Evaluate[] to force evaluations, which can significantly reduce computation time. Additionally, the discussion highlights the importance of avoiding symbolic manipulations when numerical computations would suffice.

PREREQUISITES
  • Understanding of Mathematica syntax, particularly function definitions with underscores.
  • Familiarity with the Evaluate[] function in Mathematica.
  • Knowledge of conditional statements in Mathematica, specifically the If function.
  • Basic concepts of symbolic versus numerical computations in Mathematica.
NEXT STEPS
  • Learn how to use the Evaluate[] function effectively in Mathematica.
  • Explore best practices for defining functions in Mathematica, including the use of underscores.
  • Research techniques for optimizing Mathematica code performance, focusing on numerical methods.
  • Review the guide "10 tips for writing fast Mathematica code" for additional optimization strategies.
USEFUL FOR

Mathematica users, data scientists, and researchers looking to optimize their code for better performance and efficiency in symbolic and numerical computations.

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 5 ·
Replies
5
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K