- Notifications
You must be signed in to change notification settings - Fork1
Python library that implements named tensors with Astropy Quantity support.
sun-data/named-arrays
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
named-arrays is an implementation of anamed tensor, which assigns names to each axis of an n-dimensional array such as a numpy array.
When using a Numpy n-dimensional array, the programmer must manually keep track of the physical meaning of each axis in the array.Furthermore, it is often necessary to insert singleton dimensions at the end of the array to allow it to broadcastable against other arrays.Named tensors solve this problem by giving each axis a name, which allows for automatic axis alignment without the need for inserting extra dimensions.named-arrays provides a very unapologetic implementation of a named tensor, since axes canonly be accessed using their names,unlikexarray which allows for both name and index.
ScalarArray, a named tensor implementation with Astropy Quantity support. Analogue ofxarray.VariableUncertainScalarArray, a named tensor implementation with automatic uncertainty propagation.Cartesian2dVectorArray, representation of a 2D vector.Cartesian3dVectorArray, representation of a 3D vector.FunctionArray, representation of a discrete function. Analogue of anxarray.DataArray
named-arrays is available on PyPi and can be installed using pip
pip install named-arrays
The fundamental type ofnamed-arrays is theScalarArray, which is a composition of a numpy ndarray-like object and a tuple of axis names which must have the same length as the number of dimensions in the array.
importnumpyasnpimportnamed_arraysasnaa=na.ScalarArray(np.array([1,2,3]),axes=('x',))
If we create another array with a different axis name, it will be broadcasted automatically against the first array if we add them together
b=na.ScalarArray(np.array([4,5]),axes=('y',))c=a+bc
ScalarArray( ndarray=[[5, 6], [6, 7], [7, 8]], axes=('x', 'y'),)All the usual numpy reduction operations use the axis name instead of the axis index
c.mean('x')
ScalarArray( ndarray=[6., 7.], axes=('y',),)To index the array we can use a dictionary with the axis names as the keys
c[dict(x=0)]
ScalarArray( ndarray=[5, 6], axes=('y',),)We recommend that you rarely directly create instances ofScalarArray directly. Instead, you can use the implicit array classes:ScalarLinearSpace,ScalarLogarithmicSpace, andScalarGeometricSpace to create arrays in a similar fashion tonumpy.linspace(),numpy.logspace(), andnumpy.geomspace() with the advantage of being able to access the inputs to these functions at a later point.
d=na.ScalarLinearSpace(0,1,axis='z',num=4)d
ScalarLinearSpace(start=0, stop=1, axis='z', num=4, endpoint=True)Thses implicit array classes work just likeScalarArray and can be used with any of the usual array operations.
a+d
ScalarArray( ndarray=[[1. , 1.33333333, 1.66666667, 2. ], [2. , 2.33333333, 2.66666667, 3. ], [3. , 3.33333333, 3.66666667, 4. ]], axes=('x', 'z'),)About
Python library that implements named tensors with Astropy Quantity support.
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Uh oh!
There was an error while loading.Please reload this page.
Contributors2
Uh oh!
There was an error while loading.Please reload this page.