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

  • Context: Mathematica 
  • Thread starter Thread starter Gwinterz
  • Start date Start date
  • Tags Tags
    Force Mathematica
Click For Summary

Discussion Overview

The discussion revolves around the evaluation of a function in Mathematica, specifically how to force the evaluation of a nested function to improve computation time. Participants explore issues related to symbolic versus numerical evaluations and the implications of using conditional statements within function definitions.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes a function f[a,b,c] defined in terms of another function g[b,c], expressing a need for Mathematica to evaluate g[1,2] directly instead of returning it unevaluated.
  • Another participant suggests defining functions with underscores and using the correct syntax to ensure Mathematica treats them as functions.
  • A participant identifies that the use of the If function was causing Mathematica to delay evaluation until a condition was met, which they resolved by using Evaluates[..].
  • One participant notes the complexity of optimizing Mathematica code and mentions a guide on writing efficient code, suggesting that there are multiple approaches to achieve similar results.
  • Another participant points out that low performance may stem from using symbolic manipulations when numerical evaluations would be more efficient, expressing concern over the omission of syntax details that could lead to misunderstandings.

Areas of Agreement / Disagreement

Participants express differing views on the best practices for defining functions and optimizing performance in Mathematica. There is no consensus on a single approach to resolve the performance issues discussed.

Contextual Notes

Participants mention specific syntax requirements and the impact of conditional statements on evaluation, indicating that the discussion is sensitive to how functions are defined and utilized in Mathematica.

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