I know it is easy to compute a sparse dot product in SQL, but what is the best way to do a sum (for very long vectors)?
A join is not enough because if a coordinate is filled in one vector but not in the other, it will be ignored.
Thus, I computed the sum with a PHP loop... and that was a pretty stupid idea.
I'm currently thinking of filling the missing 0's in order to prepare an inner join, but is there a shortcut (like an outer join converting NULL to 0)?
Edit. Here is the structure of my table of vectors:
CREATE TABLE `eigaki_vectors` (
`name` varchar(2) COLLATE utf8_unicode_ci NOT NULL,
`i1` int(10) NOT NULL,
`i2` int(10) NOT NULL,
`value` double NOT NULL,
UNIQUE KEY `key` (`name`,`i1`,`i2`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
In this particular case, a vector has composed indices: v_{i_1, i_2}
, but this has nothing to do with the problem.
I expected to do something like (thanks xQbert):
SELECT v1.i1, v1.i2, isNull(v1.value, 0) + isNull(v2.value, 0)
FROM eigaki_vectors v1 FULL OUTER JOIN eigaki_vectors v2
ON v1.i1 = v2.i1 AND v1.i2 = v2.i2
AND v1.name = 'a' AND v2.name = 'b'
to add vectors a
and b
. But FULL OUTER JOIN
doesn't exist on MySQL, and I think I'm clumsy with the name
column. Any ideas?
coalesce(Field,OtherField, AnotherField,0)
coalesce is basically a if then forever... it picks the first non-null value from your list of variables
isNull does the same thing but only for 2 values
isNull(Field,0)
I managed to get something, thanks to the snippet provided in MySQL: Union of a Left Join with a Right Join :
SELECT IFNULL(v1.value, 0) + IFNULL(v2.value, 0) FROM
(
SELECT i1, i2 FROM eigaki_vectors WHERE name = 'a'
UNION
SELECT i1, i2 FROM eigaki_vectors WHERE name = 'b'
) indices
LEFT OUTER JOIN eigaki_vectors v1 ON indices.i1 = v1.i1 AND indices.i2 = v1.i2 AND v1.name = 'a'
LEFT OUTER JOIN eigaki_vectors v2 ON indices.i1 = v2.i1 AND indices.i2 = v2.i2 AND v2.name = 'b'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.