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?
I managed to get something, thanks to the snippet provided in MySQL: Union of a Left Join with a Right Join: