Back
long (clj)
(source)function
(long x)
Coerce to long
Examples
originrose/cortex
(ns cortex.nn.impl
"Implementation helpers to aid implementing neural network cortex protocols
or specific neural network layers"
(:require [cortex.nn.layers :as layers]
[clojure.core.matrix.macros :refer [c-for]]))
(defmacro convolution-outer-kernel
[conv-desc & body]
`(let [~'conv-desc ~conv-desc
~'output-width (long (:output-width ~'conv-desc))
~'output-height (long (:output-height ~'conv-desc))
~'num-in-channels (long (:input-channels ~'conv-desc))
~'num-out-channels (long (:output-channels ~'conv-desc))
~'input-width (long (:input-width ~'conv-desc))
~'input-height (long (:input-height ~'conv-desc))
~'input-planar-stride (* ~'input-width ~'input-height)
~'output-planar-stride (* ~'output-width ~'output-height)
~'kernel-width (long (:kernel-width ~'conv-desc))
~'kernel-height (long (:kernel-height ~'conv-desc))
~'output-channel-stride (* ~'kernel-width ~'kernel-height)
~'output-column-stride (* ~'output-channel-stride ~'num-in-channels)
~'stride-y (long (:stride-y ~'conv-desc))
~'stride-x (long (:stride-x ~'conv-desc))
~'pad-x (long (:pad-x ~'conv-desc))
~'pad-y (long (:pad-y ~'conv-desc))
~'min-x (- 0 ~'pad-x)
~'min-y (- 0 ~'pad-y)
~'max-x (+ ~'input-width ~'pad-x)
~'max-y (+ ~'input-height ~'pad-y)
~'kernel-num-elems (* ~'kernel-width ~'kernel-height)]
(c-for
[~'chan 0 (< ~'chan ~'num-in-channels) (inc ~'chan)]
(let [~'chan-input-offset (* ~'chan ~'input-planar-stride)
~'chan-output-offset (* ~'chan ~'output-planar-stride)]
(c-for
[~'out-y 0 (< ~'out-y ~'output-height) (inc ~'out-y)]
(let [~'input-rel-y (- (* ~'out-y ~'stride-y) ~'pad-y)]
(c-for
[~'out-x 0 (< ~'out-x ~'output-width) (inc ~'out-x)]
(let [~'input-rel-x (- (* ~'out-x ~'stride-x) ~'pad-x)]
~@body))))))))
mikera/core.matrix
Indexes are intended to be used to specify elements, ranges or sub-arrays of core.matrix arrays.
As such they can be considered as a 1D vector of integer values."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.macros :refer [error]])
(:import [clojure.lang IPersistentVector]))
(extend-protocol mp/PIndexImplementation
(Class/forName "[J")
(index? [m]
true)
(index-to-longs [m]
m)
(index-to-ints [m]
(int-array m))
(index-from-longs [m xs]
xs)
(index-from-ints [m xs]
(long-array xs))
(index-coerce [m a]
(mp/index-to-longs a)))
(extend-protocol mp/PIndexImplementation
(Class/forName "[I")
(index? [m]
true)
(index-to-longs [m]
(long-array m))
(index-to-ints [m]
m)
(index-from-longs [m xs]
(int-array xs))
(index-from-ints [m xs]
xs)
(index-coerce [m a]
(mp/index-to-ints a)))
(extend-protocol mp/PIndexImplementation
IPersistentVector
(index? [m]
(every? integer? m))
(index-to-longs [m]
(long-array m))
(index-to-ints [m]
(int-array m))
(index-from-longs [m xs]
(vec xs))
(index-from-ints [m xs]
(vec xs))
(index-coerce [m a]
(cond
(mp/index? a)
(mp/persistent-vector-coerce a)
(== 1 (long (mp/dimensionality a)))
(vec (mp/index-to-longs a))
:else
(error "Can't make a 1D index from array of shape " (mp/get-shape a)))))
mikera/core.matrix
WARNING: because they lack efficient indexed access, sequences will perform badly for most
array operations. In general they should be converted to other implementations before use."
(:require [clojure.core.matrix.protocols :as mp]
[clojure.core.matrix.implementations :as imp]
#?(:clj [clojure.core.matrix.macros :refer [scalar-coerce error]]))
#?(:clj (:import [clojure.lang ISeq])
:cljs (:require-macros [clojure.core.matrix.macros :refer [scalar-coerce error]])))
(extend-protocol mp/PSliceSeq2
ISeq
(get-slice-seq [m dimension]
(let [ldimension (long dimension)]
(cond
(== ldimension 0) (mp/get-major-slice-seq m)
(< ldimension 0) (error "Can't get slices of a negative dimension: " dimension)
:else (mapv #(mp/get-slice m dimension %) (range (mp/dimension-count m dimension)))))))
(extend-protocol mp/PFunctionalOperations
ISeq
(element-seq [m]
(if (== 0 (long (mp/dimensionality (first m))))
m ;; handle 1D case, just return this sequence unchanged
(mapcat mp/element-seq m)))
(element-map
([m f]
(mapv #(mp/element-map % f) m))
([m f a]
(let [[m a] (mp/broadcast-compatible m a)]
(mapv #(mp/element-map % f %2) m (mp/get-major-slice-seq a))))
([m f a more]
(let [[m a & more] (apply mp/broadcast-compatible m a more)] ; FIXME
(mapv #(mp/element-map % f %2 %3) m (mp/get-major-slice-seq a) (map mp/get-major-slice-seq more)))))
(element-map!
([m f]
(error "Sequence arrays are not mutable!"))
([m f a]
(error "Sequence arrays are not mutable!"))
([m f a more]
(error "Sequence arrays are not mutable!")))
(element-reduce
([m f]
(reduce f (mapcat mp/element-seq m)))
([m f init]
(reduce f init (mapcat mp/element-seq m)))))
hraberg/deuce
(ns deuce.emacs.floatfns
(:use [deuce.emacs-lisp :only (defun defvar)])
(:require [clojure.core :as c]
[deuce.emacs.alloc :as alloc])
(:refer-clojure :exclude [float]))
(defun ftruncate (arg)
"Truncate a floating point number to an integral float value.
Rounds the value toward zero."
(double (long arg)))
(defun abs (arg)
"Return the absolute value of ARG."
(if (float? arg)
(Math/abs (double arg))
(Math/abs (long arg))))
(defun floor (arg &optional divisor)
"Return the largest integer no greater than ARG.
This rounds the value towards -inf.
With optional DIVISOR, return the largest integer no greater than ARG/DIVISOR."
(long (Math/floor (/ arg (or divisor 1)))))
(defun ffloor (arg)
"Return the largest integer no greater than ARG, as a float.
(Round towards -inf.)"
(double (long arg)))
(defun truncate (arg &optional divisor)
"Truncate a floating point number to an int.
Rounds ARG toward zero.
With optional DIVISOR, truncate ARG/DIVISOR."
(long (/ arg (or divisor 1))))
(defun ceiling (arg &optional divisor)
"Return the smallest integer no less than ARG.
This rounds the value towards +inf.
With optional DIVISOR, return the smallest integer no less than ARG/DIVISOR."
(long (Math/ceil (/ arg (or divisor 1)))))
(defun fround (arg)
"Return the nearest integer to ARG, as a float."
(double (long arg)))
hraberg/deuce
(ns deuce.emacs.alloc
(:use [deuce.emacs-lisp :only (defun defvar) :as el]
[taoensso.timbre :as timbre
:only (trace debug info warn error fatal spy)])
(:require [clojure.core :as c]
[clojure.walk :as w]
[deuce.emacs-lisp.cons :as cons])
(:refer-clojure :exclude [vector cons list])
(:import [java.util Arrays]
[java.lang.management ManagementFactory MemoryNotificationInfo MemoryType MemoryPoolMXBean]
[javax.management NotificationListener NotificationEmitter Notification]))
;; From http://www.javaspecialists.eu/archive/Issue092.html
(let [^MemoryPoolMXBean tenured-gen-pool (->> (ManagementFactory/getMemoryPoolMXBeans)
(filter (fn [^MemoryPoolMXBean mb]
(and (= (.getType mb) MemoryType/HEAP) (.isUsageThresholdSupported mb))))
first)
warning-level 0.8]
(.setUsageThreshold tenured-gen-pool
(long (* warning-level (.getMax (.getUsage tenured-gen-pool)))))
(.addNotificationListener ^NotificationEmitter (ManagementFactory/getMemoryMXBean)
(proxy [NotificationListener] []
(handleNotification [^Notification n hb]
(when (= (.getType n) MemoryNotificationInfo/MEMORY_THRESHOLD_EXCEEDED)
(el/setq memory-full true)))) nil nil))
(defun garbage-collect ()
"Reclaim storage for Lisp objects no longer needed.
Garbage collection happens automatically if you cons more than
`gc-cons-threshold' bytes of Lisp data since previous garbage collection.
`garbage-collect' normally returns a list with info on amount of space in use:
((USED-CONSES . FREE-CONSES) (USED-SYMS . FREE-SYMS)
(USED-MISCS . FREE-MISCS) USED-STRING-CHARS USED-VECTOR-SLOTS
(USED-FLOATS . FREE-FLOATS) (USED-INTERVALS . FREE-INTERVALS)
(USED-STRINGS . FREE-STRINGS))
However, if there was overflow in pure space, `garbage-collect'
returns nil, because real GC can't be done.
See Info node `(elisp)Garbage Collection'."
(interactive)
(System/gc)
'(()))