tech.v3.datatype.gradient

Calculate the numeric gradient of successive elements. Contains simplified versions of numpy.gradient and numpy.diff.

diff1d

(diff1d data & [options])

Returns a lazy reader of each successive element minus the previous element of length input-len - 1 unless the :prepend option is used.

Options:

  • :prepend - prepend a value to the returned sequence making it the same length as the passed in sequence.
  • :append - append a value to the end of the returned sequence making it the same length as the passsed in sequence.

Examples:

user> (dt-grad/diff1d [1 2 4 7 0])
[1 2 3 -7]
user> (dt-grad/diff1d (dt-grad/diff1d [1 2 4 7 0]))
[1 1 -10]
user> (dt-grad/diff1d [1 2 4] {:prepend 2})
[2 1 2]
user> (dt-grad/diff1d [1 2 4])
[1 2]

downsample

(downsample data n-elems)(downsample data n-elems window-fn)

Downsample to n-elems using nearest algorithm. If data is less than n-elems it is returned unchanged.

gradient1d

(gradient1d data dx)(gradient1d data)

Implement gradient as (f(x+1)-f(x-1))/2*dx. At the boundaries use (f(x)-f(x-1))/dx. Returns a lazy representation, use clone to realize.

Calculates the gradient inline returning a double reader. For datetime types please convert to epoch-milliseconds first and the result will be in milliseconds.

dx is a non-zero floating-point scalar and defaults to 1, representing the signed offset between successive sample points.

Returns a double reader of the same length as data.

Example:

user> (require '[tech.v3.datatype.gradient :as dt-grad])
nil
user> (dt-grad/gradient1d [1 2 4 7 11 16])
[1.0 1.5 2.5 3.5 4.5 5.0]
user> (dt-grad/gradient1d [1 2 4 7 11 16] 2.)
[0.5 0.75 1.25 1.75 2.25 2.5]