Community Tip - Did you get called away in the middle of writing a post? Don't worry you can find your unfinished post later in the Drafts section of your profile page. X
The values of a symmetric function f(x, y) are calculated and entered into a table using the matrix(m, n, f) function. However, taking into account the function's symmetry effectively doubles the computational effort. How can I eliminate this double computational effort while taking the function's symmetry into account?
Solved! Go to Solution.
You should have a good reason to forego the simplicity of using the “matrix” function (e.g., the function is quite complex and calculating a single value takes a long time).
Here is the option using nested loops. The last method (M3) is slightly more efficient in Mathcad because the entire memory for the matrix is allocated right from the start (since the lower right element is calculated and assigned first).
BTW, using the simple straightforward way is not exactly "double computational effort" because of the diagonal elements 😉
To create all n2 matrix elements, the function has to be called n*(n+1)/2 times which is more than n2/2 times.
EDIT: Here is a playful method that assembles the matrix from the diagonal elements and the upper triangular matrix.
and here the same as a one-liner
You should have a good reason to forego the simplicity of using the “matrix” function (e.g., the function is quite complex and calculating a single value takes a long time).
Here is the option using nested loops. The last method (M3) is slightly more efficient in Mathcad because the entire memory for the matrix is allocated right from the start (since the lower right element is calculated and assigned first).
BTW, using the simple straightforward way is not exactly "double computational effort" because of the diagonal elements 😉
To create all n2 matrix elements, the function has to be called n*(n+1)/2 times which is more than n2/2 times.
EDIT: Here is a playful method that assembles the matrix from the diagonal elements and the upper triangular matrix.
and here the same as a one-liner
The reason is compelling: each value is calculated numerically over many iterations.
I turned all the five methods shown into functions of matrix size n and generating function f.
Then I used a timer function to find out how long it takes to create 1000 matrices of size 50x50.
I set the times needed in comparison to the time for the standard "matrix" method.
I was surprised to see that m3 was so significant superior over m2 and even was faster than m1 which uses "matrix".
Next I used a generating function f with a time delay of 10 ms to simulate a more time consuming generator.
Still used a 50x50 matrix but I let it calculate only 3 times for obvious reasons 😉
Here we see that it does not much matter which method to use. Each takes just slightly more than half the time of method m1.
I attach the Mathcad 15 sheet for your convenience in case you want make your own experiments.
