cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a faster way to create an Array using a program loop?

SOLVED
Highlighted
Guest

Is there a faster way to create an Array using a program loop?

I'm not sure what I'm doing wrong but Mathcad is taking far longer than I would expect to create simple arrays.

 

Goal: create an array with 86,400 rows, all values are 0

 

For example, I can make an array like that immediately with B[86400 := 0.

 

However if i try to do the same thing using a while or for loop it takes nearly a minute. 

 

C:] t<--86400

   ] j<--0

   ] while j<t+1

   ]  ] C[j <--0

   ]  ] j<--j+1

   ] C

 

Now the program I'm actually trying to write is a little more complicated but I need to write arrays/matrices using programs.  My current file takes 20 min to run which is far to much considering that the calculations are not intense.  I figure I must be making mathcad work harder than necessary.  What am I doing wrong?

Tags (3)
1 ACCEPTED SOLUTION

Accepted Solutions

Re: Is there a faster way to create an Array using a program loop?

I guess your Powermatrix is a 86401 x 2 matrix. Thats not really big, indeed.

And you want to create a 49 x 3 matrix by averaging some values.

I retyped without doing any optimizations - just used a syntax to access matrix elements which I am more used to.

I even omitted the preallocation of the three vectors. Usually when dealing with big matrices thats a good idea but given that your three vectors are just 49 element vectors I guess the benefit will not be noticable.

As you can see in the screenshot it took about 30 ms to calculate your compressed matrix.

The calculation was done on a very old and lame notebook.

How long does it take youu to do the calculation? Or did I miss something?

Pic.png

 

 

15 REPLIES 15

Re: Is there a faster way to create an Array using a program loop?

Array60-400.png

Re: Is there a faster way to create an Array using a program loop?

Mathcad was  (I'm not sure if Prime kept it) optimized for vector/matrix operations.  The "vectorize function" that creates an arrow above the operation is far faster (for example) than multiplying two vectors element by element.  When you write a program to populate an array you force Mathcad to throw out that optimization and do element by element.

 

Without a more specific example I can't offer better guidance.  Investigate stack, augment, and vectorize to see if there is any way to reduce the amount of programming.

Re: Is there a faster way to create an Array using a program loop?

This is why I'm confused.  That matrix takes no time to generate.  However if I try to do it within a program block it takes 10000000x longer.  I need to create arrays within a program block.

Re: Is there a faster way to create an Array using a program loop?

Thanks for the reply.  I'll be more specific with what I'm trying to accomplish.  I've essentially modelled power (kW) generated for every second of a day (86,400 seconds).  I have to calculate it at this scale. 

 

I want to compress the data into 30 min intervals (1800 seconds).  1st column will be the time in min (30,60,90..) and 2nd column will be the average power generated within 30 min interval.  I've done this successfully, it just takes an excessive amount of time. 

 

I read somewhere that it was faster to create a matrix with all 0's then with a loop replace the values rather than just create the matrix with loop.  It didn't affect calculation time that way.  Attached is the block that takes 15-20min to run. 

Re: Is there a faster way to create an Array using a program loop?

Try:

 

Prog(N) := | A[N,N <--0

                  return A

 

Works up to 16000 x 16000, and it's fast.

Re: Is there a faster way to create an Array using a program loop?

Rather than create the large vector (86400 seconds) can you

  • calculate the first 30 seconds, take the average, write the first row of your "compressed" array,
  • calculate the next 30 seconds,
  •  
  • rinse and repeat

Filling a matrix with zeros then overwriting it sounds like a memory storage trick.

Re: Is there a faster way to create an Array using a program loop?

I guess your Powermatrix is a 86401 x 2 matrix. Thats not really big, indeed.

And you want to create a 49 x 3 matrix by averaging some values.

I retyped without doing any optimizations - just used a syntax to access matrix elements which I am more used to.

I even omitted the preallocation of the three vectors. Usually when dealing with big matrices thats a good idea but given that your three vectors are just 49 element vectors I guess the benefit will not be noticable.

As you can see in the screenshot it took about 30 ms to calculate your compressed matrix.

The calculation was done on a very old and lame notebook.

How long does it take youu to do the calculation? Or did I miss something?

Pic.png

 

 

Re: Is there a faster way to create an Array using a program loop?

I'm not entirely sure why yet, but your  HRes runs perfectly on my computer with my PowerMatrix.  So thank you very much!  I just started using Mathcad this week and was impressed with the software but that excessive calc time was starting to drive me insane. 

 

I'm very curious though as to what specific subtle edit made such an exponential difference.  

Re: Is there a faster way to create an Array using a program loop?

My original program took 20 mins to run