Skip to content

Commit 5645c2a

Browse files
committed
feat(math): adding dotProduct
1 parent cc9873e commit 5645c2a

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

Math.ark

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,3 +390,23 @@
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+

tests/math-tests.ark

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,8 @@
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])) })})

0 commit comments

Comments
 (0)