Skip to main content

Extend broadcasting rules of numpy to abstract tabularshape of data from cellular shape

Project description

Introduction

When the best structure of data is any-dimensional cells arranged in any-dimensional tables - tablarray provides fast numpy-like array operations with broadcasting modified to handle both cellular-dimensions and tabular-dimensions at once.

tablarray was originally developed to manage large numbers of optical modes.

Here is a short example:

import numpy as np
import tablarray as ta
x = ta.TablArray(np.linspace(-2, 2, 4), 0)
y = ta.TablArray(np.linspace(-1.5, 1.5, 4).reshape(4,1), 0)
E = ta.zeros((4, 4, 2), cdim=1)
E.cell[0] = 1.5 / (x**2 + y**2)
E.cell[1] = -.01 + x * 0
print(E)
[[|[ 0.24  -0.01 ]|
  |[ 0.557 -0.01 ]|
  |[ 0.557 -0.01 ]|
  |[ 0.24  -0.01 ]|]

 [|[ 0.353 -0.01 ]|
  |[ 2.16  -0.01 ]|
  |[ 2.16  -0.01 ]|
  |[ 0.353 -0.01 ]|]

 [|[ 0.353 -0.01 ]|
  |[ 2.16  -0.01 ]|
  |[ 2.16  -0.01 ]|
  |[ 0.353 -0.01 ]|]

 [|[ 0.24  -0.01 ]|
  |[ 0.557 -0.01 ]|
  |[ 0.557 -0.01 ]|
  |[ 0.24  -0.01 ]|]]t(4, 4)|c(2,)

Those ‘|’ separate tabular vs cellular structure. Similarly ‘t(4, 4)|c(2,)’ is a reminder that E is set of arrays ‘c(2,)’ arranged in a 4x4 table ‘t(4, 4)’.

En = ta.abs(ta.linalg.norm(E)**2)
Efield = ta.TablaSet(x=x, y=y, E=E, En=En)
Efield['r'] = ta.sqrt(Efield['x']**2 + Efield['y']**2)
ta.set_printoptions(threshold=10, precision=3)
print(Efield.table)
        | x      | y      | E        | En    | r     |
--------+--------+--------+----------+-------+-------+
 [0, 0] | -2.000 | -1.500 | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [0, 1] | -0.667 |        | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [0, 2] | 0.667  |        | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [0, 3] | 2.000  |        | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 0] |        | -0.500 | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 1] |        |        | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 2] |        |        | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 3] |        |        | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 0] |        | 0.500  | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 1] |        |        | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 2] |        |        | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 3] |        |        | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 0] |        | 1.500  | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 1] |        |        | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 2] |        |        | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 3] |        |        | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
print(Efield.bcast)
        | x      | y      | E        | En    | r     |
--------+--------+--------+----------+-------+-------+
 [0, 0] | -2.000 | -1.500 | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [0, 1] | -0.667 | -1.500 | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [0, 2] | 0.667  | -1.500 | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [0, 3] | 2.000  | -1.500 | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 0] | -2.000 | -0.500 | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 1] | -0.667 | -0.500 | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 2] | 0.667  | -0.500 | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [1, 3] | 2.000  | -0.500 | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 0] | -2.000 | 0.500  | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 1] | -0.667 | 0.500  | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 2] | 0.667  | 0.500  | [ 2.16   | 4.666 | 0.833 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [2, 3] | 2.000  | 0.500  | [ 0.353  | 0.125 | 2.062 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 0] | -2.000 | 1.500  | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 1] | -0.667 | 1.500  | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 2] | 0.667  | 1.500  | [ 0.557  | 0.310 | 1.641 |
        |        |        |  -0.01 ] |       |       |
--------+--------+--------+----------+-------+-------+
 [3, 3] | 2.000  | 1.500  | [ 0.24   | 0.058 | 2.500 |
        |        |        |  -0.01]  |       |       |
--------+--------+--------+----------+-------+-------+
print(Efield.cell)
     | x          | y          | E           | En          | r           |
-----+------------+------------+-------------+-------------+-------------+
 [0] | [|-2.|     | [[|-1.5|]  | [[|0.24|    | [[|0.058|   | [[|2.5|     |
     |  |-0.667|  |  [|-0.5|]  |   |0.557|   |   |0.31|    |   |1.641|   |
     |  | 0.667|  |  [| 0.5|]  |   |0.557|   |   |0.31|    |   |1.641|   |
     |  | 2.   |] |  [| 1.5|]] |   |0.24 |]  |   |0.058|]  |   |2.5  |]  |
     |            |            |  [|0.353|   |  [|0.125|   |  [|2.062|   |
     |            |            |   |2.16|    |   |4.666|   |   |0.833|   |
     |            |            |   |2.16|    |   |4.666|   |   |0.833|   |
     |            |            |   |0.353|]  |   |0.125|]  |   |2.062|]  |
     |            |            |  [|0.353|   |  [|0.125|   |  [|2.062|   |
     |            |            |   |2.16|    |   |4.666|   |   |0.833|   |
     |            |            |   |2.16|    |   |4.666|   |   |0.833|   |
     |            |            |   |0.353|]  |   |0.125|]  |   |2.062|]  |
     |            |            |  [|0.24|    |  [|0.058|   |  [|2.5|     |
     |            |            |   |0.557|   |   |0.31|    |   |1.641|   |
     |            |            |   |0.557|   |   |0.31|    |   |1.641|   |
     |            |            |   |0.24 |]] |   |0.058|]] |   |2.5  |]] |
-----+------------+------------+-------------+-------------+-------------+
 [1] |            |            | [[|-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|]  |             |             |
     |            |            |  [|-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|]  |             |             |
     |            |            |  [|-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|]  |             |             |
     |            |            |  [|-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|   |             |             |
     |            |            |   |-0.01|]] |             |             |
-----+------------+------------+-------------+-------------+-------------+

Lessons from above:

  1. TablArray and TablaSet have bcast, table, and cell views.

  2. Broadcasting rules of numpy are extended to recognize tabular and cellular shapes.

  3. This frees physics libraries to write formulas while blind to tabular super-structure of the application. In other words, the goal is to abstract formulas from tabular shape.

  4. TablaSet adds to TablArray by enforcing broadcast-ability across datasets. Once a TablaSet is built, you know it is ready for formulas.

Installation

pip install tablarray

Status

Alpha - tablarray might be stable enough for prototype applications.

I.e.:

  • Key features are implemented and not expected to change.

  • A few features need further adaptation for certain cases.

  • A fair amount of testing is not done.

  • Some features are still missing.

  • Packaging needs work.

Todo

  • Provide setup.py

  • testing

  • benchmark, cythonize, re-benchmark

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

tablarray-0.2.99.tar.gz (31.6 kB view hashes)

Uploaded Source

Built Distribution

tablarray-0.2.99-py3-none-any.whl (40.0 kB view hashes)

Uploaded Python 3

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