Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

push for idx & dates ... not much to see right now...

  • Loading branch information...
commit 3c63d4bdeddedf034c05ada4c000e0a34e49ccc3 1 parent d3f9939
@locklin authored
View
6 project.clj
@@ -2,9 +2,9 @@
:description "clushjr is for numerics, yo"
:dependencies [[org.clojure/clojure "1.3.0"]
[incanter/parallelcolt "0.9.4"]
- ;; [org.clojars.slocklin/jblas-static-sandybridge "1.2.1"]]
- [kephale/jblas "1.2.0"]]
+ [clj-time "0.3.3"]
+ [kephale/jblas "1.2.0"]]
+ ;; [org.clojars.slocklin/jblas-static-sandybridge "1.2.1"]]
:dev-dependencies [[lein-clojars "0.7.0"] [swank-clojure "1.3.4-SNAPSHOT"]]
- :java-source-path ["src"]
:jvm-opts ["-Xmx1g"])
View
537 src/dates.clj
@@ -0,0 +1,537 @@
+;; looted from the internets and an old incanter checkin; no copyright listed
+;; my mods
+;; Copyright (c) 2011 Scott Locklin/Lugos LLC
+;; distributed under the zlib/libpng License
+
+(ns #^{:doc "
+ chrono.clj --- Because calling it date-utils would be boring.
+
+ Use the date function to create dates. You can look up components
+ much like you would in a map:
+
+ (def my-date (date 2009 2 27 12 34 56))
+
+ (my-date :year) ;; 2009
+ (my-date :month) ;; 2
+ (my-date :day) ;; 27
+ (my-date :hour) ;; 12
+ (my-date :minute) ;; 34
+ (my-date :second) ;; 56
+
+ You may omit the time if you like:
+
+ (def my-other-date (date 2009 2 27))
+ (my-other-date :hour) ;; 0
+
+ To get a date relative to another date, use earlier and later:
+
+ (earlier my-date 100 :minute) ;; 2009 2 27 10:54:56
+ (later my-other-date 10 :day) ;; 2009 3 9
+
+ For comparing dates, use earlier? and later?:
+
+ (earlier? my-date my-other-date) ;; false
+ (later? (later my-date 10 :day) my-date) ;; true
+
+ You can see the time between two dates by calling time-between:
+
+ (time-between my-other-date (date 2009 2 25) :days) ;; 2
+
+ The date-seq function returns a lazy seq of dates incrementing by
+ the units in its first arg starting from its second arg. The third
+ arg if given dictates the end of the sequence.
+
+ (date-seq :hours my-other-date my-date) ;; (a seq of twelve hours)
+ (take 4 (date-seq :years my-date))
+ ;; (date 2009 2 27 12 34 56) (date 2010 2 27 12 34 56)
+ ;; (date 2011 2 27 12 34 56) (date 2012 2 27 12 34 56) [...]
+
+ For converting between strings and dates, use format-date and
+ parse-date
+
+ (format-date my-date :short-date-time) ;; 2/27/09 12:34 PM
+ (format-date my-other-date :long-date) ;; February 27, 2009
+ (parse-date \"12/25/09\" :short-date) ;; (date 2009 12 25)
+ (parse-date \"January 1, 2008 1:45:23 PM EST\" :long-date-time)
+ ;; (date 2008 1 1 13 45 23)
+
+ Supported date formats are:
+ iso8601
+ short-date
+ medium-date
+ long-date
+ full-date
+ short-date-time
+
+ Both format-date and parse-date also support a string for the
+ format argument, which will use the string as the format for a
+ SimpleDateFormat (see the javadocs of that class for how to write
+ these formats).
+
+ See test_contrib/chrono.clj for more details.
+
+ TODO:
+
+ * Timezones
+ * More support for weeks
+ * Various others scattered through code
+
+ "
+ :author "Matt Moriarity, Phil Hagelberg, and Bradford Cross"}
+dates
+;; (:use [incanter.internal :only [maybe?]])
+;; no: get this namespace and add the maybe? and helper functions
+;; (:use [clojure.contrib.monads
+;; :only [m-lift m-bind m-result defmonad with-monad]])
+ (:import (java.util Calendar TimeZone)
+ (java.text DateFormat SimpleDateFormat)
+ (org.joda.time DateTime DateTime$Property DateTimeZone
+ Minutes Hours Period Interval)))
+
+;; notre that monads is now here: https://github.com/clojure/algo.monads
+;; git://github.com/clojure/algo.monads.git
+;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;;;; stuff from old incanter core ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; https://raw.github.com/liebke/incanter/1.2.2/modules/incanter-core/src/incanter/internal.clj
+;;
+;; (defn- nil-coll? [mv]
+;; (or (nil? mv)
+;; (and (coll? mv)
+;; (any? nil? mv))))
+
+;; ;;TODO: refactor based on email exchange with Konrad.
+;; (defmonad maybe-seq-m
+;; [m-zero nil
+;; m-result (fn m-result-maybe [v] v)
+;; m-bind (fn m-bind-maybe [mv f]
+;; (if (nil-coll? mv)
+;; nil
+;; (f mv)))
+;; m-plus (fn m-plus-maybe [& mvs]
+;; (first (drop-while nil-coll? mvs)))])
+
+;; ;;TODO: this doesn't have to be macros if the m-lift is not a macro might be able to do eval trickery
+;; (defn- maybe? [pred & args]
+;; (or
+;; (with-monad maybe-seq-m
+;; ((m-lift 1 (partial apply pred)) args))
+;; false))
+
+
+
+(def #^{:doc "Conversion of unit keywords to Calendar units"}
+ units-to-calendar-units
+ {:year Calendar/YEAR,
+ :month Calendar/MONTH,
+ :day Calendar/DATE,
+ :hour Calendar/HOUR_OF_DAY,
+ :minute Calendar/MINUTE,
+ :second Calendar/SECOND,
+ :dayofweek Calendar/DAY_OF_WEEK})
+
+(def #^{:doc "Number of seconds in each unit"}
+ units-in-seconds
+ {:year 31557600,
+ :month 2592000,
+ :week 604800,
+ :day 86400,
+ :hour 3600,
+ :minute 60,
+ :second 1,
+ :millisecond 0.001})
+
+(defn- make-calendar
+ "Given some date values, create a Java Calendar object."
+ ([] (doto (Calendar/getInstance)
+ (.clear)
+ (.setLenient true)))
+ ([calendar]
+ (.clone calendar))
+ ([year month day]
+ (doto (make-calendar)
+ (.set year (dec month) day)))
+ ([year month day hours minutes]
+ (doto (make-calendar)
+ (.set year (dec month) day hours minutes)))
+ ([year month day hours minutes seconds]
+ (doto (make-calendar)
+ (.set year (dec month) day hours minutes seconds))))
+
+(defn- get-unit [calendar unit]
+ (.get calendar (units-to-calendar-units unit)))
+
+(defmulti format-date
+ "Take in a date and a format (either a keyword or a string) and
+ return a string with the formatted date."
+ (fn [date & form] (first form)))
+
+(defmulti parse-date
+ "Take in a string with a formatted date and a format (either a
+ keyword or a string) and return a parsed date."
+ (fn [source & form] (first form)))
+
+(defn- date
+ "Returns a new date object. Takes year, month, and day as args as
+ well as optionally hours, minutes, and seconds."
+ [& args]
+ (let [calendar (apply make-calendar args)]
+ (proxy [clojure.lang.IFn clojure.lang.Associative] []
+ (toString [] (format-date this :iso8601))
+ (equals [other-date]
+ (and (instance? (.getClass this) other-date)
+ (.equals calendar (other-date :calendar))))
+ ;; look up :year, :month, :date, etc.
+ (invoke [unit]
+ (cond (= :calendar unit) calendar ;; mostly for internal use
+ (= :month unit) (inc (get-unit calendar :month))
+ true (get-unit calendar unit)))
+ ;; These (along with implementing Associative) allow us to use
+ ;; (:month my-date), etc. Good idea? Not sure since we don't
+ ;; implement all of Associative, just enough for keywords.
+ (valAt [unit] (.invoke this unit))
+ (equiv [o] (.equals this o)))))
+
+;;brad's hacks
+
+(defn- date>
+ ""
+ ([d]
+ (let [cal (Calendar/getInstance)]
+ (.setTime cal d)
+ (date cal))))
+
+;;joda time
+
+(defn- time-zone
+ ""
+ ([offset] (DateTimeZone/forOffsetHours offset)))
+
+(defn- joda-date
+ ""
+ ([str-d] (DateTime. str-d))
+ ([y m d h min sec mill zone]
+ (DateTime. y m d h min sec mill zone)))
+
+(defn- joda-proxy
+ "joda-date object wraped in a proxy of goodness."
+ [& args]
+ (let [d (apply joda-date args)]
+ (proxy [clojure.lang.IFn
+ clojure.lang.Associative] []
+ (toString [] (str d))
+ (equals [other-date]
+ (and (instance? (.getClass this) other-date)
+ (.equals d (other-date :datetime))))
+ (invoke [unit]
+ (let [res
+ (cond (= :years unit) (.year d)
+ (= :months unit) (.monthOfYear d)
+ (= :days unit) (.dayOfMonth d)
+ (= :day-of-week unit) (.dayOfWeek d)
+ (= :hours unit) (.hourOfDay d)
+ (= :minutes unit) (.minuteOfHour d)
+ (= :seconds unit) (.secondOfMinute d)
+ (= :millis unit) (.millisOfSecond d)
+ :otherwise d)]
+ (if (instance? DateTime$Property res)
+ (.get res)
+ res)))
+ ;; These (along with implementing Associative) allow us to use
+ ;; (:month my-date), etc. Good idea? Not sure since we don't
+ ;; implement all of Associative, just enough for keywords.
+ (valAt [unit] (.invoke this unit))
+ (equiv [o] (.equals this o)))))
+
+(defn- joda-str
+ ""
+ ([d] (str `(DateTime. ~(str d)))))
+
+(defmethod print-dup org.joda.time.DateTime [d w] (.write w (joda-str d)))
+
+(defn- joda-guard
+ ""
+ ([d] (not (instance? org.joda.time.DateTime d))))
+
+ ;;TODO: make this stuff monadic
+(defn- minutes-between
+ ""
+ ([start end]
+ (if (or (joda-guard start) (joda-guard end))
+ nil
+ (.getMinutes (Minutes/minutesBetween start end)))))
+
+(defn- hours-between
+ ""
+ ([start end]
+ (if (or (joda-guard start) (joda-guard end))
+ nil
+ (.getHours (Hours/hoursBetween start end)))))
+
+(defn- hours-from
+ ""
+ ([d #^Integer h] (.plusHours d h)))
+
+(defn- minutes-from
+ ""
+ ([d #^Integer m] (.plusMinutes d m)))
+
+(defn- hours-around
+ ""
+ ([r #^Integer d] (map #(.plusHours d %) r)))
+
+(defn- before?
+ ""
+ ([start end] (.isBefore start end)))
+
+;; (defn- valid-range?
+;; ""
+;; ([[start end]] (maybe? before? start end)))
+;; need to fix maybe?
+
+(defn- is-within?
+ ""
+ ([d [s e]] (.contains (Interval. s e) d)))
+
+;; (defn- are-overlapping?
+;; ""
+;; ([[s e] [s1 e1]]
+;; (if (and (valid-range? [s e]) (valid-range? [s1 e1]))
+;; (letfn [(has-overlap? [start end start1 end1]
+;; (not (nil? (.overlap (Interval. start end) (Interval. start1 end1)))))]
+;; (maybe? has-overlap? s e s1 e1))
+;; false)))
+;; fix maybe
+
+;;todo: find otu why this yields different resutls thatn reading the
+;;other joda-str function output back in.
+;; joda-date
+;; ~(.getYear d)
+;; ~(.getMonthOfYear d)
+;; ~(.getDayOfWeek d)
+;; ~(.getHourOfDay d)
+;; ~(.getMinuteOfHour d)
+;; ~(.getSecondOfMinute d)
+;; ~(.getMillisOfSecond d)
+;; (DateTimeZone/forID ~(str (.getZone d))))))
+
+(defn now
+ "Returns a new date object with the current date and time."
+ []
+ (date (Calendar/getInstance)))
+
+(defn today
+ "Returns a new date object with only the current date. The time
+ fields will be set to 0."
+ []
+ (let [d (now)]
+ (date (d :year) (d :month) (d :day))))
+
+;;; Relative functions
+
+(defn- later
+ "Returns a date that is later than the-date by amount units.
+ Amount is one if not specified."
+ ([the-date amount units]
+ (date (doto (.clone (the-date :calendar))
+ (.set (units-to-calendar-units units)
+ (+ (.get (the-date :calendar)
+ (units-to-calendar-units units))
+ amount)))))
+ ([the-date units]
+ (later the-date 1 units)))
+
+(defn- date-time
+ ""
+ ([d t] (later d t :minute)))
+
+(defn- earlier
+ "Returns a date that is earlier than the-date by amount units.
+ Amount is one if not specified."
+ ([the-date amount units]
+ (later the-date (- amount) units))
+ ([the-date units]
+ (later the-date -1 units)))
+
+(defn- later?
+ "Is date-a later than date-b?"
+ ([date-a date-b]
+ (.after (date-a :calendar) (date-b :calendar))))
+
+(defn- earlier?
+ "Is date-a earlier than date-b?"
+ ([date-a date-b] (.before (date-a :calendar) (date-b :calendar))))
+
+(defn- time-between
+ "How many units between date-a and date-b? Units defaults to seconds."
+ ([date-a date-b]
+ (/ (java.lang.Math/abs
+ (- (.getTimeInMillis (date-a :calendar))
+ (.getTimeInMillis (date-b :calendar)))) 1000))
+ ([date-a date-b units]
+ ;; TODO: should we move plural support up to
+ ;; units-in-seconds and units-to-calendar-units?
+ (let [units (if (re-find #"s$" (name units)) ;; Allow plurals
+ ;; This relies on the patched subs defn below
+ ;;(keyword (subs (name units) 0 -1))
+ (keyword (apply str (drop-last (name units)))) ;; DEL 12/12/2009
+ units)]
+ (/ (time-between date-a date-b)
+ (units-in-seconds units)))))
+
+(defn- args-for "Allow round-tripping through date function"
+ [date]
+ [(date :year) (date :month) (date :day)
+ (date :hour) (date :minute) (date :second)])
+
+(defn- beginning-of
+ "Return a date at the beginning of the month, year, day, etc. from the-date."
+ [the-date unit]
+ ;; TODO: clean up!
+ (let [position ({:year 1 :month 2 :day 3 :hour 4 :minute 5 :second 6} unit)]
+ (apply date (concat (take position (args-for the-date))
+ (drop position [1970 1 1 0 0 0])))))
+
+(defn- end-of
+ "Return a date at the end of the month, year, day, etc. from the-date."
+ [the-date unit]
+ ;; TODO: this is kinda ugly too?
+ (earlier (later (beginning-of the-date unit) unit) :second))
+
+(defn date-seq
+ "Returns a lazy seq of dates starting with from up until to in
+ increments of units. If to is omitted, returns an infinite seq."
+ ([units from to]
+ (lazy-seq
+ (when (or (nil? to) (earlier? from to))
+ (cons from (date-seq units (later from units) to)))))
+ ([units from] (date-seq units from nil)))
+
+;;; Formatting and Parsing
+
+(defmacro def-date-format
+ ""
+ ([fname [arg] & body]
+ `(defmethod format-date ~(keyword (name fname)) [~arg ~'_]
+ ~@body)))
+
+(defmacro def-date-parser
+ ""
+ ([fname [arg] & body]
+ `(defmethod parse-date ~(keyword (name fname)) [~arg ~'_]
+ ~@body)))
+
+;;; Use the normal Java date formats
+
+(def #^{:private true}
+ format-to-java-const
+ {:short DateFormat/SHORT,
+ :medium DateFormat/MEDIUM,
+ :long DateFormat/LONG,
+ :full DateFormat/FULL})
+
+(doseq [[key con] format-to-java-const]
+ (defmethod format-date (keyword (str (name key) "-date")) [date _]
+ (.format (DateFormat/getDateInstance con)
+ (.getTime (date :calendar))))
+ (defmethod parse-date (keyword (str (name key) "-date")) [source _]
+ (date (doto (make-calendar)
+ (.setTime (.parse (DateFormat/getDateInstance con)
+ source)))))
+ (defmethod format-date (keyword (str (name key) "-date-time")) [date _]
+ (.format (DateFormat/getDateTimeInstance con con)
+ (.getTime (date :calendar))))
+ (defmethod parse-date (keyword (str (name key) "-date-time")) [source _]
+ (date (doto (make-calendar)
+ (.setTime (.parse (DateFormat/getDateTimeInstance con con)
+ source))))))
+
+;;; Formats dates with a custom string format
+(defmethod format-date :default [date form]
+ (.format (SimpleDateFormat. form)
+ (.getTime (date :calendar))))
+
+;;; Parse a date from a string format
+(defmethod parse-date :default [source form]
+ (date
+ (doto (make-calendar)
+ (.setTime (.parse (SimpleDateFormat. form) source)))))
+
+(defmethod format-date nil [date]
+ (format-date date :iso8601))
+
+(defmacro def-simple-date-format [fname form]
+ `(do
+ (def-date-format ~fname [date#]
+ (format-date date# ~form))
+ (def-date-parser ~fname [source#]
+ (parse-date source# ~form))))
+
+;; Technically this should also have a single character time zone
+;; indicator, but I'm not sure how to do that yet.
+(def-simple-date-format iso8601 "yyyy-MM-dd HH:mm:ss")
+
+
+;; TODO: parse-date should be able to guess at the format
+
+;; Redefine subs to allow for negative indices.
+;; TODO: This should be submitted as a patch to Clojure.
+;; (in-ns 'clojure.core)
+;; (defn- subs
+;; "Returns the substring of s beginning at start inclusive, and ending
+;; at end (defaults to length of string), exclusive."
+;; ([#^String s start] (subs s start (count s)))
+;; ([#^String s start end]
+;; (let [count-back #(if (< % 0) (+ (count s) %) %)]
+;; (.substring s (count-back start) (count-back end)))))
+
+
+
+
+(defn add-sec [d n]
+ "clone of date-add-day"
+ (later d n :second))
+
+(defn add-minute [date n]
+ "clone of date-add-day"
+ (later date n :minute))
+
+(defn add-hour [date n]
+ "clone of date-add-day"
+ (later date n :hour))
+
+(defn add-day [date n]
+ " clone of date-add-day"
+ (later date n :day))
+
+(defn add-month [date n]
+ "clone of date-add-day"
+ (later date n :month))
+
+(defn add-year [date n]
+ " clone of date-add-day"
+ (later date n :year))
+
+(defn string-to-date
+ " can generate optional format; default is yyyy-MM-dd HH:mm:ss"
+ ([string] (parse-date string "yyyy-MM-dd HH:mm:ss"))
+ ([string fmt] (parse-date string fmt)))
+
+(defn to-string [d]
+ (str d))
+
+ (defn split-date [d]
+ {(d :year) :year (d :month) :month (d :day) :day
+ (d :hour) :hour (d :minute) :minute (d :second) :second})
+;; (d :millis) :milliseconds})
+
+(defn code [d flag]
+ (cond
+ (= flag 1) ()
+ (= flag 16) ()
+ (= flag 8) ()
+ (= flag 4) ()
+ (= flag 2) ()))
+
+(defn date-range [start ndays]
+ "start day, & ndays forward"
+ (date-seq :day start (add-day start ndays)))
View
136 src/idx.clj
@@ -13,6 +13,7 @@
;; pow
;; sup
;; subset
+(defn subset [^org.jblas.DoubleMatrix x ^:Integers ind])
;; into-idx
;; makes a list or double array into a DoubleMatrix
;; removerow
@@ -22,47 +23,54 @@
;; iexcluder (maybe)
;; rearrange (maybe not)
+
(defn zeros [a b]
(DoubleMatrix/zeros a b))
(defn eye [n]
-;; requrns square eye matrix
+" requrns square eye matrix"
(DoubleMatrix/eye n))
(defn rand-normal [n m]
- ;; returns an n by m matrix of normally distributed randoms
+ " returns an n by m matrix of normally distributed randoms
+ optional arguments for mean and std?"
(DoubleMatrix/randn n m))
(defn rand-flat [n m]
- ;; returns an n by m matrix of randoms between 0 and 1
+ "returns an n by m matrix of randoms between 0 and 1"
(DoubleMatrix/rand n m))
-(defn shape [^org.jblas.DoubleMatrix x]
- ;; returns shape of matrix
- [(.rows x) (.columns x)])
+(defn shape
+ " returns shape of matrix, optional argument selects rows or cols
+ repro of lush idx-shape"
+ ([^org.jblas.DoubleMatrix x]
+ [(.rows x) (.columns x)])
+ ([^org.jblas.DoubleMatrix x rowcol]
+ (cond (= rowcol 0) (.rows x)
+ (= rowcol 1) (.columns x))))
(defn reshape [^org.jblas.DoubleMatrix x r c]
- ;; reshapes matrix
+ " reshapes matrix"
(.reshape x r c))
(defn issquare [^org.jblas.DoubleMatrix x]
- ;; squareness check
+ " squareness check"
(.isSquare x))
(defn element-mul [^org.jblas.DoubleMatrix x ^org.jblas.DoubleMatrix y]
- ;; multiplies elements of x by y, returns a new matrix
+ " multiplies elements of x by y, returns a new matrix"
(.mul x y))
(defn mul [^org.jblas.DoubleMatrix x ^org.jblas.DoubleMatrix y]
(.mmul x y))
(defn rescale [^org.jblas.DoubleMatrix x ^org.jblas.DoubleMatrix y]
- ;; divides elements of x by y, returns a new matrix
+ "divides elements of x by y, returns a new matrix"
(.div x y))
(defn take-real [^org.jblas.ComplexDoubleMatrix x]
- ;; reals of a complex matrix
+ " reals of a complex matrix"
(let [c (class x)]
(cond (= c org.jblas.ComplexDoubleMatrix)
(.getReal x)
@@ -71,7 +79,7 @@
(defn take-imag [^org.jblas.ComplexDoubleMatrix x]
- ;; returns complex part of a complex matrix as a real matrix
+ " returns complex part of a complex matrix as a real matrix"
(let [c (class x)]
(cond (= c org.jblas.ComplexDoubleMatrix)
(.imag x)
@@ -81,70 +89,59 @@
(defn add [^org.jblas.DoubleMatrix x ^org.jblas.DoubleMatrix y]
- ;; adds x to y, returns a new matrix
+ "adds x to y, returns a new matrix"
(.add x y))
(defn subtract [^org.jblas.DoubleMatrix x ^org.jblas.DoubleMatrix y]
- ;; subtracts y from x, returns a new matrix
+ "subtracts y from x, returns a new matrix"
(.add x (.neg y)))
(defn elt-max [^org.jblas.DoubleMatrix x]
- ;; largest element of x
+ "largest element of x"
(.max x))
(defn elt-min [^org.jblas.DoubleMatrix x]
- ;; smallest element of x
+ "smallest element of x"
(.min x))
(defn ind-max [^org.jblas.DoubleMatrix x]
- ;; largest element index of x
+ " largest element index of x"
(.argmax x))
(defn ind-min [^org.jblas.DoubleMatrix x]
- ;; smallest element index of x
+ " smallest element index of x"
(.argmin x))
(defn col-ind-max [^org.jblas.DoubleMatrix x]
- ;; largest element index of x by columns
+ " largest element index of x by columns"
(.columnArgmaxs x))
(defn col-ind-min [^org.jblas.DoubleMatrix x]
- ;; smallest element index of x by columns
+ "smallest element index of x by columns"
(.columnArgmins x))
(defn col-elt-max [^org.jblas.DoubleMatrix x]
- ;; largest element values of x by columns
+ "largest element values of x by columns"
(.columnMaxs x))
(defn col-elt-min [^org.jblas.DoubleMatrix x]
- ;; smallest element values of x by columns
+ "smallest element values of x by columns"
(.columnMins x))
-
-
-
-
-
(defn sum [^org.jblas.DoubleMatrix x]
(.sum x))
(defn fill [^org.jblas.DoubleMatrix x ^Double v]
- ;; fills matrix x with value v
+ " fills matrix x with value v"
(.fill x v))
(defn ones [n m]
- ;; fills a n by m matrix with 1's
+ "fills a n by m matrix with 1's"
(let [out (zeros n m)]
(fill out 1)))
-(defn select [x a b]
-;;
- )
-;; DoubleMatrix/get and getColumn for select
-
-
(defn mat-transpose [^org.jblas.DoubleMatrix x]
(.transpose x))
@@ -152,14 +149,14 @@
(defn dotc [^org.jblas.DoubleMatrix x ^Double y]
-;; should be .mul, but doesn't work for some reason
+ " should be .mul, but doesn't work for some reason"
(.mul x y))
(defn copy-matrix [^org.jblas.DoubleMatrix x]
(.dup x))
(defn solve [^org.jblas.DoubleMatrix a ^org.jblas.DoubleMatrix b]
- ;; finds X for A*X = B
+ "finds X for A*X = B"
(Solve/solve a b))
;; also solveSymmetric for symmetric A
;; solvePositive for positive definite A
@@ -174,54 +171,79 @@
(MatrixFunctions/exp a))
(defn fracpos [^org.jblas.DoubleMatrix x]
- ;; what fraction are positive
+ " what fraction are positive"
(let [len (.length x)]
(/ (sum (.gt x (double 0))) len)))
(defn fracneg [^org.jblas.DoubleMatrix x]
- ;; what fraction are negative
+ "what fraction are negative"
(let [len (.length x)]
(/ (sum (.lt x (double 0))) len)))
(defn cumsum [^org.jblas.DoubleMatrix x]
- ;; cumulative sum by rows
+ " cumulative sum by rows"
(.cumulativeSum x))
(defn cholesky [a]
- ;; cholesky decomp of a
+ "cholesky decomp of a"
(Decompose/cholesky a))
;; missing in OS-X blas
(defn ludecomp [a]
- ;; lu decomposition of a
+ " lu decomposition of a"
(Decompose/lu a))
;; missing in OS-X blas
;; returns lu class; change this to a list of three matrices, l, p (permutation) and u
(defn concat-horiz [x y]
- ;; concats matrices horizontally
+ "concats matrices horizontally"
(DoubleMatrix/concatHorizontally x y))
(defn concat-vert [x y]
- ;; concats matrices vertically
- ;; fix later
+ " concats matrices vertically"
(DoubleMatrix/concatVertically x y))
-(defn eigenvalues2 [^org.jblas.DoubleMatrix x & [compout]]
-;; broken optional argument
- (if (issquare x)
- (let [out (Eigen/eigenvalues x)]
- (if (compout)
- (out)
- (take-real out)))))
+(defn eigenvalues
+ " returns complex matrix of eigenvalues and eigenvectors of x
+ add an optional arg to take reals"
+ ([^org.jblas.DoubleMatrix x]
+ (if (issquare x)
+ (Eigen/eigenvalues x)))
+ ([^org.jblas.DoubleMatrix x nocomp]
+ (if (issquare x)
+ (take-real (Eigen/eigenvalues x)))))
+
+
+(defn select [^org.jblas.DoubleMatrix x a b]
+ " clone of idx-select in Lush; 0 gets columns 1 gets rows
+ (select <mx> <rowcol> <num>)"
+ (cond (= a 1) (.getColumn x b)
+ (= a 0) (.getRow x b)))
+
+
+(defn narrow [^org.jblas.DoubleMatrix x n s o]
+"Make a clone of x and reduce its size in the n -th dimension to s elements, offset by o"
+(let [nrow (shape x 0)
+ ncol (shape x 1)]
+ (cond
+ (= n 1) (.getRange x 0 nrow o s)
+ (= n 0) (.getRange x o s 0 ncol))))
+
+
+(defn save-mx [^org.jblas.DoubleMatrix x ^String s]
+ "saves a matrix to <filename>"
+ (.save x s))
+
+(defn load-mx [^org.jblas.DoubleMatrix x ^String s]
+ "loads a matrix from <filename>"
+ (.load x s))
+(defn load-csv [^String s]
+ (DoubleMatrix/loadCSVFile s))
-(defn eigenvalues [^org.jblas.DoubleMatrix x]
-;; returns complex matrix of eigenvalues and eigenvectors
- (if (issquare x)
- (Eigen/eigenvalues x)
-))
+(defn load-ascii [^String s]
+ (DoubleMatrix/loadAsciiFile s))
View
3  src/includes.clj
@@ -0,0 +1,3 @@
+(defn one- [^Integer x]
+;; subtract 1
+ (- x 1))
View
8 src/timeseries.clj
@@ -0,0 +1,8 @@
+;; Copyright (c) 2011 Scott Locklin/Lugos LLC
+;; distributed under the zlib/libpng License
+
+
+(ns timeseries
+ (use 'dates)
+ (require 'idx))
+
Please sign in to comment.
Something went wrong with that request. Please try again.