[Mathematica] Subnormal numbers handling

In summary, dealing with subnormal numbers can be challenging in scientific computing, but there are ways to avoid them and improve efficiency. Some options include using the logarithm of the matrix, using a higher precision data type, or using specialized libraries. It's important to weigh the trade-offs to find the best solution for your code.
  • #1
guerom00
93
0
Hello all :)

I have a matrix which I diagonalize giving me a list of eigenvalues lambda. Those are complex numbers.
After that, I calculate Exp[-lambda] and my code goes on.

For some parameters in my code, some lambdas have very large magnitude so that the exponentiation leads to quantities involving subnormal numbers (for Mathematica, those are numbers which are smaller than $MinMachineNumber). At this point, the code becomes _very_ slow (in addition to inherent hardware difficulties performing floating point operations with subnormal numbers, I understand Mathematica treat them in arbitrary precision !). After a _very_ long time though, Mathematica gives me the right answer (that can be said at least :))

To circumvent this situation, I apply the function If[Abs[#]<1.*^-50,0,#]& just after the exponentiation. So far, this seems to give me the right answer with no slowing down of the code.

One question though : I've chosen the value 1.*^-50 quite arbitrarily and I'm afraid that what I do might be a little bit “rough”.
Is there a rigorous and/or elegant way to avoid generation of subnormal numbers by the exponentiation?

Thanks in advance :)
 
Physics news on Phys.org
  • #2


Hello there,

Thank you for sharing your experience with us. Dealing with subnormal numbers can be a challenge in scientific computing, but there are some ways to avoid them and improve the efficiency of your code.

One approach is to use the logarithm of the matrix instead of directly exponentiating the eigenvalues. This can help avoid the issue of subnormal numbers and also improve the numerical stability of your calculations. You can then take the exponential of the resulting matrix to get the final answer.

Another option is to use a higher precision data type, such as arbitrary precision arithmetic, to perform your calculations. This can help avoid the issue of subnormal numbers and provide a more accurate result. However, it may also slow down your code significantly, so it's important to weigh the trade-offs.

Finally, you can also consider using specialized libraries or packages for matrix diagonalization and exponentiation. These may have optimized algorithms for handling subnormal numbers and can improve the efficiency of your code.

I hope these suggestions are helpful. Good luck with your research!
 

Related to [Mathematica] Subnormal numbers handling

What are subnormal numbers in Mathematica?

Subnormal numbers, also known as denormalized numbers, are floating-point numbers that are smaller than the smallest normal number. They occur when the exponent of a floating-point number is too small to represent it accurately. In Mathematica, subnormal numbers are represented as zero with a non-zero significand.

How does Mathematica handle subnormal numbers?

In Mathematica, subnormal numbers are handled by using extended precision arithmetic. This means that instead of the standard 64-bit floating-point numbers, subnormal numbers are represented using 80 or 128 bits. This allows for more precise representation of these small numbers.

Why are subnormal numbers important in Mathematica?

Subnormal numbers are important in Mathematica because they allow for more accurate calculations when dealing with very small numbers. Without the ability to handle subnormal numbers, these calculations would result in round-off errors or inaccuracies.

Can subnormal numbers be converted to normal numbers in Mathematica?

Yes, subnormal numbers can be converted to normal numbers in Mathematica by using the function SetPrecision. This function allows for specifying the precision of a number, which can be used to convert subnormal numbers to normal numbers.

Are there any potential issues with handling subnormal numbers in Mathematica?

One potential issue with handling subnormal numbers in Mathematica is that it may slow down calculations due to the use of extended precision arithmetic. Additionally, there is a risk of overflow or underflow when performing calculations with subnormal numbers, which can lead to inaccuracies if not handled properly.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
22
Views
3K
  • Calculus and Beyond Homework Help
Replies
6
Views
3K
  • Programming and Computer Science
Replies
10
Views
1K
  • Classical Physics
Replies
14
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
2
Replies
52
Views
11K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
2
Views
1K
Back
Top