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])
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file snuggs-1.4.3.tar.gz
.
File metadata
- Download URL: snuggs-1.4.3.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d819e52a6b104936b293b625e5ab53cb8b95194643525a7d181334d743a77625 |
|
MD5 | 756115fe852567e7a6570cd177e61d5d |
|
BLAKE2b-256 | a0f54795c9f4bc8ab62a6a43f8260c35304d12df2339e4920af6d519d806317c |
File details
Details for the file snuggs-1.4.3-py3-none-any.whl
.
File metadata
- Download URL: snuggs-1.4.3-py3-none-any.whl
- Upload date:
- Size: 5.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.8.0 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.6.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c06f68cfa5f700339ba000875049f87b394679aa1dd06fc64af71e446caf4a6 |
|
MD5 | 8c815d0a2d189fa4b87df16a00eea2fe |
|
BLAKE2b-256 | a58aabb0803f05feb4401ab9e4c776ce11f0048b6cb0f2d84ace97cd93d7da04 |