二维矩阵代数数据库设计

发布于 2024-09-08 15:00:36 字数 258 浏览 3 评论 0原文

任何人都可以就用于存储 2D 时间序列矩阵数据的数据库设计/DBMS 提供建议。允许快速后端代数计算:例如:

表 A、B、C.. Col1:日期-时间戳 col2:数据数组? (矩阵数据)

  • SQL 伪代码

INSERT INTO TABLE C 选择 将 A.Data A 乘以 B.Data 其中矩阵 A 开始日期 = 矩阵 B 开始日期 矩阵 A 结束日期 = 矩阵 B 结束日期

本质上是设置计算的坐标。

Can anyone advise on a database design/DBMS for storing 2D Time Series Matrix data. To allow for quick BACK END algebraic calculations: e.g:

Table A,B,C..
Col1: Date- Timestamp
col2: Data- Array? (Matrix Data)

  • SQL Psuedo Code

INSERT INTO TABLE C
SELECT
Multiply A.Data A by B.Data
Where Matrix A Start Date = Matrix B Start Date
And Matrix A End Date = Matrix B End Date

Essentially set the co-ordinates for the calculation.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2024-09-15 15:00:36

矩阵代数的困难在于确定用于数据建模的矩阵上的域是什么。它是一个值吗?整体是一个矩阵吗?这不是一个预先定义的问题,所以我会给你两个解决方案以及权衡是什么。

解决方案 1:矩阵单元中的值是一个域:

 CREATE TABLE matrix_info (
     x_size int,
     y_size int,
     id serial not null unique,
     timestamp not null,
 );

 CREATE TABLE matrix_cell (
     matrix_id int references matrix_info(id),
     x int,
     y int,
     value numeric not null,
     primary key (matrix_id, x, y)
);

最大的问题是这不能很好地强制矩阵大小。此外,缺失值可用于表示 0,或者可能不允许。使用矩阵作为一个整体作为域的想法具有一定的吸引力。在这种情况下:

CREATE TABLE matrix (
  id serial not null unique,
  timestamp not null,
  matrix_data numeric[]
);

请注意,包括 PostgreSQL 在内的许多数据库都会强制数组实际上是一个矩阵。然后你需要编写自己的乘法函数等。我建议在 PostgreSQL 上以对象关系方式执行此操作,因为它对于此类事情非常可编程。类似于:

CREATE TABLE matrix(int) RETURNS matrix LANGUAGE SQL AS
$ select * from matrix where id = $1 $;

CREATE FUNCTION multiply(matrix, matrix) RETURNS matrix LANGUAGE plpgsql AS 
$
DECLARE matrix1 = $1.matrix_data;
        matrix2 = $2.matrix_data;
begin
   ...
end;
$;

然后您可以将矩阵乘法称为:

SELECT * FROM multiply(matrix(1), matrix(2));

您甚至可以将其他两个矩阵的乘积插入到表中:

INSERT INTO matrix (matrix_data)
SELECT matrix_data FROM multiply(matrix(1), matrix(2));

The difficulty with matrix algebra is determining what is a domain on the matrix for data modelling purposes. Is it a value? Is it a matrix as a whole? This is not a pre-defined question, so I will give you two solutions and what the tradeoffs are.

Solution 1: Value in a matrix cell is a domain:

 CREATE TABLE matrix_info (
     x_size int,
     y_size int,
     id serial not null unique,
     timestamp not null,
 );

 CREATE TABLE matrix_cell (
     matrix_id int references matrix_info(id),
     x int,
     y int,
     value numeric not null,
     primary key (matrix_id, x, y)
);

The big concern is that this does not enforce matrix sizes very well. Additionally a missing value could be used to represent 0, or might not be allowed. The idea of using a matrix as a whole as a domain has some attractiveness. In this case:

CREATE TABLE matrix (
  id serial not null unique,
  timestamp not null,
  matrix_data numeric[]
);

Note that many db's including PostgreSQL will enforce that an array is actually a matrix. Then you'd need to write your own functions for multiplication etc. I would recommend doing this in an object-relational way and on PostgreSQL since it is quite programmable for this sort of thing. Something like:

CREATE TABLE matrix(int) RETURNS matrix LANGUAGE SQL AS
$ select * from matrix where id = $1 $;

CREATE FUNCTION multiply(matrix, matrix) RETURNS matrix LANGUAGE plpgsql AS 
$
DECLARE matrix1 = $1.matrix_data;
        matrix2 = $2.matrix_data;
begin
   ...
end;
$;

Then you can call the matrix multiplication as:

SELECT * FROM multiply(matrix(1), matrix(2));

You could even insert into the table the product of two other matrices:

INSERT INTO matrix (matrix_data)
SELECT matrix_data FROM multiply(matrix(1), matrix(2));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文