[Mathematica] Newbie with Compile[]

  • Mathematica
  • Thread starter guerom00
  • Start date
  • Tags
    Mathematica
In summary, Compile[] is a function in Mathematica that allows for optimization and speed-up of code execution. It is especially useful for beginners as it simplifies the process of writing efficient code and helps with understanding basic programming concepts. To use Compile[], input arguments, output type, and code must be specified. Not all functions in Mathematica can be compiled and it is important to check the documentation for compatibility. Compile[] improves speed by converting code into low-level machine code. However, there are limitations, such as not being able to handle recursive functions or functions with variable arguments. Checking the documentation for potential limitations is important before using Compile[] in code.
  • #1
guerom00
93
0
Hello all :)

I'm just beginning trying to use compiled version of several simple functions. And right at the beginning, I stumble onto a “Numerical error” error message which puzzle me…

Here is my simple code :

Code:
11/18/11 12:51:07 In[18]:= \[Epsilon]IntrinsicSiliciumC = 
  Compile[{{\[Omega], _Real}, {L, _Real}},
   Module[{\[Omega]0, \[Epsilon]0, \[Epsilon]Inf, c = 299792458},
    \[Omega]0 = (66 10^14)/(c (10^9) ) L;
    \[Epsilon]0 = 11.87;
    \[Epsilon]Inf = 1.035;
    \[Epsilon]Inf + ((\[Epsilon]0 - \[Epsilon]Inf) \[Omega]0^2)/(\
\[Omega]^2 + \[Omega]0^2)
    ]
   , CompilationTarget -> "C"];

11/18/11 13:05:18 In[23]:= \[Epsilon]IntrinsicSiliciumC[1., 1.]

11/18/11 13:05:19 During evaluation of In[23]:= CompiledFunction::cfne: Numerical error encountered; proceeding with uncompiled evaluation. >>

11/18/11 13:05:19 Out[23]= 1.0402

Basically, how to debug such error message (which is quite vague…)

Thanks in advance :)
 
Physics news on Phys.org
  • #2
</code>The error message you are getting is a bit vague, but it is telling you that your compiled code is not able to compute the result for that particular set of inputs. The most likely cause is that the inputs you are giving it are outside of the range of values that the compiled code is able to handle. To debug this, you'll need to figure out what range of inputs your compiled code is able to handle. This can involve testing different inputs and seeing which ones work and which ones don't. You can also look at the code itself to get an idea of what range of values it can handle.Once you have figured out the input range, you can modify your code so that it will produce a valid output for the inputs you are trying to use. This could involve adding conditions to check for values outside of the allowed range, or using alternative methods to compute the result. Hope this helps!
 

1. What is Compile[] in Mathematica and why is it useful for newbies?

Compile[] is a function in Mathematica that allows you to optimize and speed up the execution time of your code. It is especially useful for newbies as it simplifies the process of writing efficient code and helps beginners to understand the basic concepts of programming in Mathematica.

2. How do I use Compile[] in my code?

To use Compile[], you need to specify the input arguments, the output type, and the code you want to compile. For example, Compile[{x,y}, x+y] will compile the function x+y with input variables x and y. You can also specify the type of output you want, such as "Integer" or "Real", to further optimize your code.

3. Can I compile any function in Mathematica?

No, not all functions in Mathematica can be compiled. Only certain functions, such as arithmetic operations, can be compiled. It is important to check the documentation for the specific function you want to compile to see if it is compatible with Compile[].

4. How does Compile[] improve the speed of my code?

Compile[] improves the speed of your code by converting it into low-level machine code, which is much faster to execute compared to the high-level code written in Mathematica. This is especially useful when dealing with large datasets or performing complex calculations.

5. Are there any limitations to using Compile[]?

Yes, there are some limitations to using Compile[]. For example, it cannot handle recursive functions, functions with variable number of arguments, or functions that rely on external variables. Additionally, the use of Compile[] may also restrict the type of output that can be generated. It is important to check the documentation for any potential limitations before using Compile[] in your code.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Nuclear Engineering
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
3
Views
2K
  • Advanced Physics Homework Help
Replies
1
Views
921
  • Nuclear Engineering
Replies
7
Views
529
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top