Efficient sampling of function. [Mathematica]

In summary, the individual is seeking advice on how to improve the speed of their code for sampling numerical values of integrated functions in Mathematica. They have already optimized their integration process but are experiencing a bottleneck with their current sampling method. They are open to suggestions and mention being a beginner in coding."
  • #1
Inve
8
0
Hey there PF.
I wanted to ask here if someone had any input as to why a tiny part of my code is "slow".

I have sloved a somewhat big eq.system numerically with Mathematica. Now, I do not really need the
actual integrated functions and so I sample them.

Below: I sample (picks out only 4 datapoints within each time unit) numerical values of the actual integrated functions through the use of Block and with
digits 12. I put these values into a table for interpolation later. "Solution" contains these functions
that I wish to sample, part "f" is present because I want to store for some different parameter values and "k"
denotes the index of solution function. (If you'd like, nevermind the "Joining"-part)

Print["Sampling..."];
gSolutionPart =
Table[SetAccuracy[
Block[{t = v}, Subscript[y, k][t] /. Solution[[f, 1, k]]],
12], {f, 1, diffrentb}, {k, 1, 201}, {v, i,
i + (parttime - 1/4), 1/4}];
Print["Joining..."];
If[i == 0, gSolution = gSolutionPart,
gSolution =
Table[Join[gSolution[[f, k]], gSolutionPart[[f, k]]], {f, 1,
diffrentb}, {k, 1, 201}]];

I was keeping track of the time things in my code take, and then I noticed that this sampling takes a vast
majority of time. Example: I integrate for a solution that spans a measly 10 time units. Integration took not
even 1 minute. Sampling took 9 minutes. Yay. And when I sought a solution for something more accurate over a
larger intervall, integration took ~50 minutes. I waited hours on the sampling but..

Can anyone give some tips on how to improve this without having some huge RAM spike?
I am quite the noob when it comes to coding, but I wish to learn.
 
Physics news on Phys.org
  • #2


Hi there, thank you for reaching out to the community for help with your code. It sounds like you have already done a great job optimizing your integration process, but now you are experiencing a bottleneck with your sampling method. Here are a few suggestions that may help improve the speed of your code:

1. Consider using a different interpolation method. The "Block" method you are currently using may be slowing down your code. Look into other interpolation methods that may be faster, such as "Interpolation" or "ListInterpolation."

2. Instead of using a table, try using a matrix to store your sampled values. This can be done using the "TableForm" function. This may help speed up your code as it is more efficient to access data from a matrix than a table.

3. You mentioned that you do not need the actual integrated functions, only the sampled values. In that case, you could try skipping the "Block" step and directly sample the values from the integrated functions using the "Interpolation" function. This may save time as it eliminates the need for the "Block" step.

4. If possible, try using parallel processing to speed up your code. This can be done using the "ParallelTable" function in Mathematica.

I hope these suggestions help you improve the speed of your code. Keep in mind that there may be other factors affecting the speed, such as the complexity of your equations and the hardware you are using. Keep experimenting and learning, and don't hesitate to ask for help from the community when needed. Best of luck!
 

1. What is efficient sampling of function in Mathematica?

Efficient sampling of function in Mathematica is a method used to generate a set of points (samples) that accurately represent a function in a minimum amount of time. This can be useful for analyzing and visualizing functions, as well as for optimization and machine learning purposes.

2. How does Mathematica perform efficient sampling of function?

Mathematica uses various algorithms and techniques, such as adaptive sampling and importance sampling, to efficiently sample a function. These methods take into account the properties and characteristics of the function to generate a representative set of points.

3. What are the benefits of using efficient sampling of function in Mathematica?

Efficient sampling of function in Mathematica can save time and computational resources, as it generates a smaller number of samples compared to traditional methods while still accurately representing the function. This can also lead to faster and more accurate analysis and optimization results.

4. Are there any limitations to efficient sampling of function in Mathematica?

Efficient sampling of function in Mathematica may not be suitable for all types of functions, as it relies on certain assumptions and techniques. It is important to understand the properties and behavior of the function being sampled to determine if efficient sampling is appropriate.

5. Can efficient sampling of function in Mathematica be used for high-dimensional functions?

Yes, efficient sampling of function in Mathematica can be used for high-dimensional functions. However, as the dimensionality increases, the efficiency of the sampling may decrease, and other techniques such as Monte Carlo sampling may be more suitable.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
258
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
218
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • Electrical Engineering
Replies
4
Views
826
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top