Created
July 19, 2012 20:00
-
-
Save m0smith/3146395 to your computer and use it in GitHub Desktop.
A simple function to add vectors. Zero pads vectors to the length of the longest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn vec+ | |
([] nil) | |
([v1] v1) | |
([v1 v2] | |
(let [c1 (count v1) | |
c2 (count v2) | |
seq (if (< c1 c2) | |
(map + (concat v1 (repeat 0)) v2) | |
(map + (concat v2 (repeat 0)) v1))] | |
(vec seq))) | |
([ v1 v2 & vn] | |
(reduce vec+ (vec+ v1 v2) vn))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment