Skip to main content

Snuggs are s-expressions for Numpy

Project description

https://travis-ci.org/mapbox/snuggs.svg?branch=master https://coveralls.io/repos/mapbox/snuggs/badge.svg

Snuggs are s-expressions for Numpy

>>> snuggs.eval("(+ (asarray 1 1) (asarray 2 2))")
array([3, 3])

Syntax

Snuggs wraps Numpy in expressions with the following syntax:

expression = "(" (operator | function) *arg ")"
arg = expression | name | number | string

Examples

Addition of two numbers

import snuggs
snuggs.eval('(+ 1 2)')
# 3

Multiplication of a number and an array

Arrays can be created using asarray.

snuggs.eval("(* 3.5 (asarray 1 1))")
# array([ 3.5,  3.5])

Evaluation context

Expressions can also refer by name to arrays in a local context.

snuggs.eval("(+ (asarray 1 1) b)", b=np.array([2, 2]))
# array([3, 3])

Functions and operators

Arithmetic (* + / -) and logical (< <= == != >= > & |) operators are available. Members of the numpy module such as asarray(), mean(), and where() are also available.

snuggs.eval("(mean (asarray 1 2 4))")
# 2.3333333333333335
snuggs.eval("(where (& tt tf) 1 0)",
    tt=numpy.array([True, True]),
    tf=numpy.array([True, False]))
# array([1, 0])

Higher-order functions

New in snuggs 1.1 are higher-order functions map and partial.

snuggs.eval("((partial * 2) 2)")
# 4

snuggs.eval('(asarray (map (partial * 2) (asarray 1 2 3)))')
# array([2, 4, 6])

Performance notes

Snuggs makes simple calculator programs possible. None of the optimizations of, e.g., numexpr (multithreading, elimination of temporary data, etc) are currently available.

If you’re looking to combine Numpy with a more complete Lisp, see Hy:

=> (import numpy)
=> (* 2 (.asarray numpy [1 2 3]))
array([2, 4, 6])

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

snuggs-1.4.0.tar.gz (4.0 kB view details)

Uploaded Source

Built Distributions

snuggs-1.4.0-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

snuggs-1.4.0-py2-none-any.whl (5.6 kB view details)

Uploaded Python 2

File details

Details for the file snuggs-1.4.0.tar.gz.

File metadata

  • Download URL: snuggs-1.4.0.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for snuggs-1.4.0.tar.gz
Algorithm Hash digest
SHA256 2e299bec9a3d3c1f7cf7610b9df44b553d5673f2a01dc3a9a5da8a7f77bc9f8d
MD5 2b40406f63e9564904a96bd6faba23ea
BLAKE2b-256 29c177cd671a7e150249348aabfb10a60e7947b610daab1ffbe4e0157e299842

See more details on using hashes here.

File details

Details for the file snuggs-1.4.0-py3-none-any.whl.

File metadata

File hashes

Hashes for snuggs-1.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 ef4a2bce6a7d9726282cec8616a1c9025cad98fdd832a8245f6636d3e5d6c5af
MD5 0137274fc9986ec70968b3bb67e0928b
BLAKE2b-256 190aed625280cb4eb33185a6a6bfabcf9632bd7117606cda1912701a3bdcfae6

See more details on using hashes here.

File details

Details for the file snuggs-1.4.0-py2-none-any.whl.

File metadata

File hashes

Hashes for snuggs-1.4.0-py2-none-any.whl
Algorithm Hash digest
SHA256 863564ab9da58559b2c13d146ca20c7738d2a1a3439dc9e0e8452a4662fdbba5
MD5 90bf174b49f50225931e486389454a88
BLAKE2b-256 f9017c226ebcecd6925fbfb8cb8db1770ee0e175162e0d01f6c872e2984d3dd3

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page