Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 0b078bfa2c
Fetching contributors…

Cannot retrieve contributors at this time

55 lines (45 sloc) 1.39 KB
;; The Computer Language Benchmarks Game
;; contributed by Jesse Rosenstock
(ns spectralnorm
(set! *warn-on-reflection* true)
(defmacro eval-a [i j]
`(let [n# (+ ~i ~j)
n+1# (+ n# 1)]
(/ 1.0
(+ (bit-shift-right (* n# n+1#) 1)
(defn multiply-a-v [^doubles v
^doubles av]
(dotimes [i (alength av)]
(aset av i (areduce v j sum 0.0 (+ sum (* (eval-a i j) (aget v j)))))))
(defn multiply-at-v [^doubles v
^doubles atv]
(dotimes [i (alength atv)]
(aset atv i (areduce v j sum 0.0 (+ sum (* (eval-a j i) (aget v j)))))))
(defn multiply-at-a-v [^doubles v
^doubles tmp
^doubles at-av]
(multiply-a-v v tmp)
(multiply-at-v tmp at-av))
(defn dot-product [^doubles u
^doubles v]
(areduce u i sum 0.0 (+ sum (* (aget u i) (aget v i)))))
(defn run-game [n]
(let [u (double-array n 1.0)
tmp (double-array n 0.0)
v (double-array n 0.0)]
(dotimes [_ 10]
(multiply-at-a-v u tmp v)
(multiply-at-a-v v tmp u))
(let [vbv (dot-product u v)
vv (dot-product v v)]
(Math/sqrt (/ vbv vv)))))
(defn -main [& args]
(let [n (if (empty? args)
(Integer/valueOf (first args)))]
(println (format "%.9f" (run-game n)))))
Jump to Line
Something went wrong with that request. Please try again.