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

• Mathematica
• Gwinterz
In summary, you were using the If function and it was causing an increase in the computation time. You fixed it by putting in Evaluates[..] around each of the conditions.
Gwinterz
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

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,

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,

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.

## 1. What is the meaning of the expression "f[a,b,c] = a*g[b,c]" in Mathematica?

The expression "f[a,b,c] = a*g[b,c]" in Mathematica is a definition of a function named "f" with three variables "a", "b", and "c". It states that the value of "f" is equal to the product of "a" and another function "g" with two variables "b" and "c".

## 2. What does the "force evaluate" at the end of the expression mean?

The "force evaluate" at the end of the expression instructs Mathematica to immediately evaluate the expression and assign the resulting value to "f". This ensures that the function "f" is defined with a specific value instead of just an expression.

## 3. Can I omit the "force evaluate" at the end of the expression?

Yes, you can omit the "force evaluate" at the end of the expression. However, in this case, the function "f" will be defined as an unevaluated expression, and its value will only be calculated when it is explicitly called with specific values for the variables.

## 4. Can I use other mathematical operations or functions in the expression?

Yes, you can use other mathematical operations or functions in the expression. Mathematica has a wide range of built-in mathematical operations and functions that can be used in defining functions. You can also define your own custom functions and use them in the expression.

## 5. How does the "force evaluate" affect the performance of the function?

The "force evaluate" does not have any significant impact on the performance of the function. It only ensures that the function is defined with a specific value instead of an unevaluated expression. However, using too many "force evaluate" statements in a program can make it less efficient, so it is recommended to only use it when necessary.

Replies
5
Views
122
Replies
4
Views
2K
Replies
1
Views
1K
Replies
6
Views
1K
Replies
5
Views
3K
Replies
1
Views
1K
Replies
5
Views
3K
Replies
22
Views
3K
Replies
5
Views
2K
Replies
5
Views
2K