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])

This local context may be provided using keyword arguments (e.g., b=np.array([2, 2])), or by passing a dictionary that stores the keys and associated array values. Passing a dictionary, specifically an OrderedDict, is important when using a function or operator that references the order in which values have been provided. For example, the read function will lookup the i-th value passed:

ctx = OrderedDict((
    ('a', np.array([5, 5])),
    ('b', np.array([2, 2]))
))
snuggs.eval("(- (read 1) (read 2))", ctx)
# 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.1.tar.gz (4.3 kB view details)

Uploaded Source

Built Distributions

snuggs-1.4.1-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

snuggs-1.4.1-py2-none-any.whl (6.1 kB view details)

Uploaded Python 2

File details

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

File metadata

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

File hashes

Hashes for snuggs-1.4.1.tar.gz
Algorithm Hash digest
SHA256 e1d3c9364cec1b1b938627ebfc74f040be66711072d3ae754d844b9435f4940c
MD5 e05db13b4c45cb4fac8595adc0c99f15
BLAKE2b-256 5da72628b376d794628655d13004091801f7f867366f0b08a52a741fc5bcb5fc

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for snuggs-1.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 ef38fd4400b96f3999c928396102e65d3b0aa2f22bbc2c5fa49e151608368487
MD5 1f510b9e1381939ca6a03d21cb2c6bc8
BLAKE2b-256 23ddf8a42549be3f02af7f1edd583b4b0a7e946e330aa2cef48a9bd2f43b87fa

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for snuggs-1.4.1-py2-none-any.whl
Algorithm Hash digest
SHA256 4bea2fcefec819cc8b193d2c74e86ac448d64c3fff9df688220a9e3b2ad5af4f
MD5 90e20e92448bbf358d0b530fc87e4c39
BLAKE2b-256 5c200cdca0baf52e9fa58ee01230a06fa29a00225f7c80eb07b3d81c641dac91

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