(ns grad-descent.vec)
; This file uses some of the vector functions fromhttp://mark.reid.name/blog/minilight-clojure-vectors.html

(require '[clojure.math.numeric-tower :as math])

; Binary operators
(defn dot 
    "Returns the value of dot product of the vectors v1 and v2"
    [v1 v2] (reduce + (map * v1 v2)))
    
(defn add 
    "Returns a vector that is the sum of the vectors v1 and v2"
    [v1 v2] (map + v1 v2))
    
(defn sub 
    "Returns a vector that when added to v1 gives v2"
    [v1 v2] (map - v1 v2))

; Scalar operators
(defn scale 
    "Returns the vector that is m times the vector v"
    [m v] (map #(* m %) v))

; Unary operators
(defn norm 
    "Returns the (Euclidean) length of the vector v"
    [v] (math/sqrt (dot v v)))

(defn normalise 
    "Returns a vector of unit length in the same direction as v"
    [v] (scale (/ 1 (norm v)) v))
