Michael Chen wrote:
> --this is my first version for the purpose of storing sparse numerical
> matrix in sql
> --please let me know how to fix the bug at the end of the file, and how to
> tune the performance
> --or any better reference, thanks!
>
> .explain                                -- return result in more readable
> format
> .echo on                                -- echo the sql stmnt
>
> --I plan to store my sparse matrix (3 x 4) in table like this:
> -- [ 1 2 0 0 ]
> -- [ 0 3 9 0 ]
> -- [ 0 1 4 0 ]
>   
A sparse matrix can be stored in a table like

CREATE TABLE Matrix (
   Row INTEGER NOT NULL,
   Col INTEGER NOT NULL,
   Value REAL,
   PRIMARY KEY (Row, Col)
);

With this representation, arithmetic on matrices can be done as:

-- MatrixA + MatrixB
SELECT Row, Col, SUM(Value) AS Value FROM (SELECT * FROM MatrixA UNION 
ALL SELECT * FROM MatrixB) GROUP BY Row, Col;

-- MatrixA - MatrixB
SELECT Row, Col, SUM(Value) AS Value FROM (SELECT * FROM MatrixA UNION 
ALL SELECT Row, Col, -Value FROM MatrixB) GROUP BY Row, Col;

-- MatrixA * MatrixB
SELECT MatrixA.Row AS Row, MatrixB.Col AS Col, SUM(MatrixA.Value * 
MatrixB.Value) AS Value FROM MatrixA INNER JOIN MatrixB ON MatrixA.Col = 
MatrixB.Row GROUP BY Row, Col;

_______________________________________________
sqlite-users mailing list
sqlite-users@sqlite.org
http://sqlite.org:8080/cgi-bin/mailman/listinfo/sqlite-users

Reply via email to