Fill the matrix automatically

Fill the matrix automatically - Messages

#1 Posted: 2/7/2023 9:24:12 AM
Maciej

Maciej

1 likes in 10 posts.

Group: User

Once again I have to ask the Community of Smath Users for help. I hope this is my last struggle with the program.

I would like to create a big matrix M (let's say 1000 x 1000) and fill it with values from other vectors, let's say A and B, as well as some constansts - X and Y.
The pourpose of the matrix is to use it in solving a system of equations, so it should be filled in a very specific way.

The matrix M should look like this (it has zeros, and varied elements from vectors A and B, elements separated with spaces):

| X A1 A1+B1+A2 0 0 ... 0 |
| 0 A2 A2+B2+A3 A3 0 ... 0 |
| 0 0 A3 A3+B3+A4 A4 0 ... 0 |
...
| 0 0 0 ... A999 A999+B999+A1000 A1000 Y|

I tried nested for loops (two loops one for rows and columns) but this way does not allow to put varied values and zeros.

Is there any way I could fill the matrix M automatically?
#2 Posted: 2/7/2023 10:48:30 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

Is there any way I could fill the matrix M automatically?


Please, attach the vectors c/w the maths of each vector.
As well, once tabulated, described more exploration you desire.
No need to be so immense to reduce timing.
See you soon ... Jean.
#3 Posted: 2/7/2023 11:06:22 AM
⚜ Kenny Lemens, P.E. ᵂᴵ

⚜ Kenny Lemens, P.E. ᵂᴵ

74 likes in 164 posts.

Group: User

Greetings,

Wrote

Is there any way I could fill the matrix M automatically?



If you just want a 'filled in matrix', you can do that easily with the following (copy & paste):
M:identity(1000)
this will give you an identity matrix, 1000x1000.

You also have the function:
M:matrix(1000,1000)
which will provide you a 1000x1000 matrix with 'zeros'.


Hope this helps!
-Kenny Lemens, P.E. ᵂᴵ
"No matter where you go, there you are." -Buckaroo BanzaiHotkeys: https://en.smath.com/forum/resource.ashx?a=45771&b=2
#4 Posted: 2/7/2023 1:41:45 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

I hope this is my last struggle with the program.


Certainly NOT !!! if you continue using Smath.Page7 XyleneMatrix.sm (6 KiB) downloaded 56 time(s).
Cheers ... Jean.



#5 Posted: 2/7/2023 2:01:34 PM
overlord

overlord

554 likes in 1333 posts.

Group: Moderator

This algorithm should do it.
Just replace X,Y values and A,B matrices with yours.
Don't know what your 999 row is, so I have improvised.

Regards

fillmatrix.sm (12 KiB) downloaded 51 time(s).

2023-02-07_17-02.png
1 users liked this post
sergio 2/7/2023 2:10:00 PM
#6 Posted: 2/7/2023 2:34:25 PM
Maciej

Maciej

1 likes in 10 posts.

Group: User

Wrote

This algorithm should do it.
Just replace X,Y values and A,B matrices with yours.
Don't know what your 999 row is, so I have improvised.

Regards

fillmatrix.sm (12 KiB) downloaded 51 time(s).



I have tried your algorithm on my data and it seems to work. I checked it on a 38 x 38 matrix and it does the job perfectly.
Unfortunatelly I am not able to use it for my purposes because it takes forever to execute for bigger matrices... My final matrix is not 1000 x 1000 - it's 2688 x 2688 and for this matrix the algorithm did not complete for 15 minutes (I have terminated calculations after this time). It did not complete for 1000 x 1000 as well (but in this case I have terminated calculations after 5 minutes). Is there anything I can do to speed it up?

Please find my file with necessary data attached below.

Matrix.sm (105 KiB) downloaded 49 time(s).
#7 Posted: 2/7/2023 3:32:44 PM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1675 posts.

Group: User

Hi Xylene. First of all, try not to use i if you are going to use complexes, as you would be redefining it, and i is not a protected or special variable, like in Mathcad or other CAS.

Next, notice that in this version of the lasso the opposite of the entire vector is first found, and then the element is selected.

Clipboard02.png

while in this other one the element is first selected and then the opposite is found

Clipboard01.png

Yes, they look the same, but they are not. These improvements bring the code to a few seconds for a few values, but it's still slow for the larger array.

Finally you could try using diag to fill a matrix with the diagonal values, and augment(0,diag(X)) or stack(0,diag(X)) to create subdiagonal or similar matrices and add to the diagonal matrix. In theory that should be the most efficient way to build the Z matrix.

Best regards.
Alvaro
1 users liked this post
sergio 2/7/2023 3:46:00 PM
#8 Posted: 2/7/2023 5:00:19 PM
overlord

overlord

554 likes in 1333 posts.

Group: Moderator

Wrote

Is there anything I can do to speed it up?

Please find my file with necessary data attached below.


Using diag() and submatrices sure speed things up.
Not that fast enough (5 mins), but it finished at least.
Previous solution was still at i=821 after 12 mins when I paused it.
Maybe someone make it even faster, that's all I can do for now.

Here is your file with new algorithm.

Regards

Matrix_diag.sm (114 KiB) downloaded 62 time(s).
#9 Posted: 2/7/2023 6:38:01 PM
Maciej

Maciej

1 likes in 10 posts.

Group: User

Wrote


Previous solution was still at i=821 after 12 mins when I paused it.
Maybe someone make it even faster, that's all I can do for now.



Maybe this is a silly (and off-topic) question but how can I check on what stage is the program in calculations? Where does that information i = 821 after 12 mins come from?
I know that there is a green progress bar in the bottom right corner, but this bar shows only the number of current operation and not the number of iteration in current operation.

PS pausing the program causes error and results in shuting it down.
#10 Posted: 2/7/2023 7:30:22 PM
overlord

overlord

554 likes in 1333 posts.

Group: Moderator

Wrote

but how can I check on what stage is the program in calculations?
PS pausing the program causes error and results in shuting it down.


I simply paused it. In debug panel I looked for i value.
It was 821 while time was around 12 minutes at bottom bar.

I use linux, SMath doesn't crash on me when I pause the process.
Monitor your ram usage while running algorithm
That's what come into my mind with info I have.

Regards

PS: I rerun my bad code, it was just past half of your data.
And it took 20 mins to get there, don't use file with for() algorithm.
Use the one with diag(), it completes in 4 minutes.

2023-02-08_01-27.png
1 users liked this post
Mark R Harris 2/8/2023 4:22:00 AM
#11 Posted: 2/8/2023 1:41:26 AM
Alvaro Diaz Falconi

Alvaro Diaz Falconi

992 likes in 1675 posts.

Group: User

My grain of sand. The described algorithm does not return a square matrix, but one with two more columns than rows. This would be the most efficient way to calculate it.

Clipboard01.png

Best regards.
Alvaro.
1 users liked this post
Mark R Harris 2/8/2023 4:22:00 AM
  • New Posts New Posts
  • No New Posts No New Posts