File tree Expand file tree Collapse file tree 2 files changed +25
-1
lines changed
Expand file tree Collapse file tree 2 files changed +25
-1
lines changed Original file line number Diff line number Diff line change 390390# =end
391391# @author https://github.com/SuperFola
392392(let lerp (fun (_x _v0 _v1) (+ (* (- 1 _x) _v0) (* _x _v1))))
393+
394+ # @brief Compute the dot product of two vectors
395+ # @details The vectors must have the same length
396+ # @param _v1 vector 1
397+ # @param _v2 vector 2
398+ # =begin
399+ # (print (dotProduct [1 2 3] [4 5 6])) # 32
400+ # =end
401+ # @author https://github.com/SuperFola
402+ (let dotProduct (fun (_v1 _v2) {
403+ (assert (= (len _v1) (len _v2)) "vectors should have the same length")
404+
405+ (mut _result 0)
406+ (mut _i 0)
407+ (while (< _i (len _v1)) {
408+ (set _result (+ _result (* (@ _v1 _i) (@ _v2 _i))))
409+ (set _i (+ 1 _i)) })
410+
411+ _result }))
412+
Original file line number Diff line number Diff line change 117117 (test:expect (< (math:abs (- (math:complex-module c1) 1)) 0.0001))
118118 (test:expect (< (math:abs (- (math:complex-module c2) 49.244289008980523)) 0.0001))
119119 (test:expect (< (math:abs (- (math:complex-module c3) 111.400179533068976)) 0.0001))
120- (test:expect (< (math:abs (- (math:complex-module c4) 12.649110640673517)) 0.0001)) })})
120+ (test:expect (< (math:abs (- (math:complex-module c4) 12.649110640673517)) 0.0001)) })
121+
122+ (test:case "vectors" {
123+ (test:eq 32 (math:dotProduct [1 2 3] [4 5 6]))
124+ (test:eq 3 (math:dotProduct [1 3 -5] [4 -2 -1])) })})
You can’t perform that action at this time.
0 commit comments