Skip to main content

Snuggs are s-expressions for Numpy

Project description

Snuggs are s-expressions for Numpy

>>> snuggs.eval("(+ (asarray 1 1) (asarray 2 2))")
array([3, 3])
https://travis-ci.org/mapbox/snuggs.svg?branch=master https://coveralls.io/repos/mapbox/snuggs/badge.svg

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

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.0.tar.gz (3.4 kB view details)

Uploaded Source

File details

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

File metadata

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

File hashes

Hashes for snuggs-1.0.tar.gz
Algorithm Hash digest
SHA256 bb45028e4d4f02fa9bcdcdd1543b4240f726c64a5dae12e6cf205ac5c107d244
MD5 d68e3586e0fc64f63d34ba732eb6bdbd
BLAKE2b-256 7df77e3b9e7b754dce1e1cd1d7ad74b555c3929fa5015feedec90783cedf9e61

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