Skip to content

BrianChevalier/numpy-clj

Repository files navigation

numpy-clj

Build Status Binder

A Clojure core.matrix implementation using NumPy via libpython-clj. numpy-clj allows you to use an idiomatic Clojure library, while still interoperating with Numpy, SciPy and the entire Python ecosystem.

Try out numpy-clj in JupyterLab via Binder along with other core.matrix implementations. Click here or click the 'launch binder' badge above.

Usage

numpy-clj does not directly provide an API, but is instead used by core.matrix. numpy-clj extends core.matrix to translate to equivalent numpy functions. Include core.matrix in your project and use the core.matrix API.

If you're new to core.matrix check out matrix-compare to see how to translate common operations from MATLAB and NumPy into core.matrix.

Examples

To ensure the numpy-clj implementation is registered, make sure to require it in your namespace and set it as the current implementation.

(ns your.namespace
  (:require [numpy-clj.core]
            [core.matrix :as m]))

(m/set-current-implementation :numpy-clj)

(m/array [[0 1 2]] [3 4 5])
=> [[0 1 2] ;; this will be a numpy object
    [3 4 5]]

You can also directly interoperate using libpython-clj and use numpy functions directly. Read the official documentation to learn more on writing Python in Clojure.

(ns your.namespace
  (:require [numpy-clj.core]
            [core.matrix :as m]
            [libpython-clj.require :refer [require-python]]))

(require-python '[numpy :as np])

(np/linspace 0 10 100)
=> [ 0.          0.1010101 ...

Using numpy-clj in your project

You can include any commit of this project as a git dependency. Copy and paste the sha hash from any commit and add it to your deps.edn, like below.

{:deps
 {brianchevalier/numpy-clj
  {:git/url "https://github.com/brianchevalier/numpy-clj"
   :sha ""}}}

Development

Note: numpy-clj currently uses my fork of core.matrix (via GitHub) which adds a deps.edn file. When you run make test it will automatically checkout my fork of core.matrix with this change (branch: deps.edn). Make sure to use the :core-matrix alias to use my branch.

Useful resources:

Testing

To run the core.matrix compliance tests run the following make rule.

make test

Continuous Integration (CI)

To test changes against the exact CI environment:

  • Install Docker
  • Install act (eg. with homebrew: brew install act)
  • Run the following make rule. Warning: This will require an 18GB(!) download but will fully replicate the GitHub Actions environment
make ci/local