Matrices and Databases

I went through the topic and found out that this link is very useful and simple at the same time. Storing matrices in a relational database But can you please tell me if the path mentioned

ABCD EFGH IJKL [ABCDEFGHIJKL] 

- The best and easiest or even reliable way to store matrix elements in a database. In addition, I need to multiply two matrices and make the operation dynamic. So will storing data create any problems for the task?

+6
database
source share
3 answers

- The best and easiest or even reliable way to store matrix elements in a database. In addition, I need to multiply two matrices and make the operation dynamic. So will storing data create any problems for the task?

To begin with, both approaches are valid, but the second is not enough, as you wrote. You should have some other information, such as the length of the rows or the indices (row, column) of each element to store the matrix as a 1D array. This is usually done for sparse matrices, where there are many zeros surrounding the values ​​grouped on either side of the diagonal.

Storing a matrix in a database and managing it in memory are two separate things.

Tasks such as multiplication require (rows, columns) indexes. Storing the matrix as a 2D array means you get them, so no other information is required. A 1D array also needs this information, so you have to provide it.

The advantage goes into a 1D array for sparse matrices. In this case, you do not need to keep the null values ​​outside the bandwidth, but your operations, such as addition and multiplication, become more complicated for the code.

0
source share

In postgresql, you can have multidimensional arrays, define your own types and define your own functions for these types. For example, you can simply do:

 CREATE TABLE tictactoe ( squares integer[3][3] ); 

See the PostgreSQL Handbook for information on how to create your own types.

+1
source share

I think this pretty much depends on how you want to use matrices in your application.

Is the database only for saving for the same application, speed is important, and sizes cannot be known in advance? Create your own serialization scheme and save the binary code.

Is a database shared between applications whose size is not known in advance? Use a comma separated list.

Are you concerned about data integrity, type safety, and would like to request individual cells? Then use the schema (row, col, cell value).

Do you know that your matrices are fixed in size and relatively small, for example, 4X4 transformation matrices, and will have a 1 to 1 ratio with any element that you have in the database? Then you can have 16 rows in a table lined up.

Think about your use cases and experiment!

0
source share

All Articles