brainx-skill 1.0.3 → 1.0.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/README.md +29 -64
- package/adapters/gemini.js +7 -0
- package/adapters/opencode.js +7 -0
- package/adapters/windsurf.js +7 -0
- package/{lib → installation/lib}/installer.js +8 -5
- package/manifest.json +7 -1
- package/package.json +25 -10
- package/skills/braincell/SKILL.md +182 -0
- package/skills/braincell/references/area-scaled-hh-pattern.md +54 -0
- package/skills/braincell/references/braincell-custom-ion-channel-authoring.md +210 -0
- package/skills/braincell/references/channel-library.md +170 -0
- package/skills/braincell/references/ion-library.md +154 -0
- package/skills/braincell/references/mixions-for-adaptation.md +81 -0
- package/skills/braincell/references/multicompartment/braincell-manual-morphology-construction.md +180 -0
- package/skills/braincell/references/multicompartment/cv-policy-reference.md +133 -0
- package/skills/braincell/references/multicompartment/filter-function-library.md +162 -0
- package/skills/braincell/references/multicompartment/morphology-io-loading-validation.md +135 -0
- package/skills/braincell/references/multicompartment/multicompartment-cell-workflow.md +391 -0
- package/skills/braincell/references/multicompartment/probe-reference.md +128 -0
- package/skills/braincell/references/multicompartment/topology-building-and-visualization.md +121 -0
- package/skills/braincell/references/scripts/calcium_channel_gating.py +66 -0
- package/skills/braincell/references/scripts/cell_multicompartment_reference.py +221 -0
- package/skills/braincell/references/scripts/channel_ablation.py +61 -0
- package/skills/braincell/references/scripts/fi_curve.py +68 -0
- package/skills/braincell/references/scripts/hh_neuron_basics.py +59 -0
- package/skills/braincell/references/scripts/spike_frequency_adaptation.py +75 -0
- package/skills/braincell/references/scripts/t_current_rebound.py +72 -0
- package/skills/braincell/references/scripts/thalamic_neurons.py +228 -0
- package/skills/braincell/references/solver-library-with-effects.md +188 -0
- package/skills/brainevent/SKILL.md +268 -0
- package/skills/brainevent/references/connectivity-variants.md +312 -0
- package/skills/brainevent/references/custom-operators-cpu.md +304 -0
- package/skills/brainevent/references/custom-operators-gpu.md +468 -0
- package/skills/brainevent/references/scripts/102_EI_net_1996.py +107 -0
- package/skills/brainevent/references/scripts/204_joglekar_2018_propagation.py +168 -0
- package/skills/brainevent/references/scripts/coba_ei_teaching.py +356 -0
- package/skills/brainevent/references/sparse-formats.md +182 -0
- package/skills/brainevent/references/synaptic-plasticity.md +285 -0
- package/skills/brainpy-state/SKILL.md +232 -0
- package/skills/brainpy-state/references/array-creation.md +158 -0
- package/skills/brainpy-state/references/brain-dynamics-delay-protocol.md +96 -0
- package/skills/brainpy-state/references/braintools/brainstate-control-flow-patterns.md +334 -0
- package/skills/brainpy-state/references/braintools/data-preprocessing.md +79 -0
- package/skills/brainpy-state/references/braintools/metric.md +152 -0
- package/skills/brainpy-state/references/braintools/optimizer.md +179 -0
- package/skills/brainpy-state/references/braintools/parameter-initializer.md +183 -0
- package/skills/brainpy-state/references/braintools/surrogate.md +147 -0
- package/skills/brainpy-state/references/component-selection.md +273 -0
- package/skills/brainpy-state/references/nest-compatible/devices.md +157 -0
- package/skills/brainpy-state/references/nest-compatible/divergence-and-parity.md +114 -0
- package/skills/brainpy-state/references/nest-compatible/integration-categories.md +41 -0
- package/skills/brainpy-state/references/nest-compatible/model-library.md +200 -0
- package/skills/brainpy-state/references/nest-compatible/nest-workflow.md +156 -0
- package/skills/brainpy-state/references/nest-compatible/network-building.md +264 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brette_et_al_2007.py +320 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_alpha.py +108 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/brunel_delta.py +93 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/clopath_synapse_spike_pairing.py +171 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/evaluate_tsodyks2_synapse.py +147 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/spatial_gaussex.py +112 -0
- package/skills/brainpy-state/references/nest-compatible/scripts/synapsecollection.py +198 -0
- package/skills/brainpy-state/references/nest-compatible/synapse-and-connectivity.md +153 -0
- package/skills/brainpy-state/references/projection-patterns.md +125 -0
- package/skills/brainpy-state/references/scripts/103_COBA_2005.py +86 -0
- package/skills/brainpy-state/references/scripts/106_COBA_HH_2007.py +181 -0
- package/skills/brainpy-state/references/scripts/107_gamma_oscillation_1996.py +156 -0
- package/skills/brainpy-state/references/scripts/109_fast_global_oscillation.py +111 -0
- package/skills/brainpy-state/references/scripts/201_surrogate_grad_lif_fashion_mnist.py +221 -0
- package/skills/brainpy-state/references/scripts/training-snn.py +119 -0
- package/skills/brainstate/SKILL.md +399 -0
- package/skills/brainstate/agents/openai.yaml +4 -0
- package/skills/brainstate/references/brainstate/brainstate-control-flow-patterns.md +334 -0
- package/skills/brainstate/references/brainstate/brainstate-transformed-diagnostics.md +180 -0
- package/skills/brainstate/references/brainstate/parameter-constraints-regularization.md +213 -0
- package/skills/brainstate/references/brainstate/parameter-transforms-regularizers-catalog.md +171 -0
- package/skills/brainstate/references/brainstate/randomness-and-reproducibility.md +131 -0
- package/skills/brainstate/references/brainstate/transformation-grad-expansion.md +462 -0
- package/skills/brainstate/references/brainstate/transformation-jit-expansion.md +167 -0
- package/skills/brainstate/references/brainstate/transformation-vmap-expansion.md +314 -0
- package/skills/brainstate/references/collective_model_operations.md +195 -0
- package/skills/brainstate/references/extension_mechanisms.md +293 -0
- package/skills/brainstate/references/libraries/prebuilt-activation-library.md +115 -0
- package/skills/brainstate/references/libraries/prebuilt-layer-library.md +143 -0
- package/skills/brainstate/references/model-interop-and-migration.md +305 -0
- package/skills/brainstate/references/simulation-environment.md +117 -0
- package/skills/brainstate/references/size-inference-variations.md +279 -0
- package/skills/brainstate/references/state-graph-operations.md +216 -0
- package/skills/brainstate/references/state_collections_and_utilities.md +235 -0
- package/skills/brainstate/scripts/integrator_rnn.py +359 -0
- package/skills/brainstate/scripts/lif_neuron_model.py +52 -0
- package/skills/brainstate/scripts/modern_cnn.py +73 -0
- package/skills/brainstate/scripts/resnet.py +130 -0
- package/skills/brainunit/SKILL.md +287 -0
- package/skills/brainunit/references/array-creation.md +158 -0
- package/skills/brainunit/references/array-mechanics/functional-structural-api.md +90 -0
- package/skills/brainunit/references/array-mechanics.md +165 -0
- package/skills/brainunit/references/math-function-library.md +433 -0
- package/skills/brainunit/references/physical-constant-library.md +58 -0
- package/skills/brainunit/references/prefix-library.md +116 -0
- package/skills/brainunit/references/quantity-inspection-and-conversion.md +104 -0
- package/skills/brainunit/references/typing-and-runtime-validation.md +203 -0
- package/skills/brainunit/references/unit-structure-and-definition.md +140 -0
- package/skills/brainx-general-guard/SKILL.md +153 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/advanced-randomness.md +303 -0
- package/skills/brainx-general-guard/references/brainstate-randomness-reproducibility/randomness-and-reproducibility.md +62 -0
- package/skills/brainx-install/SKILL.md +2 -2
- package/skills/brainx-install/references/compatibility-and-release-matching.md +3 -3
- /package/{bin → installation/bin}/brainx.js +0 -0
- /package/{lib → installation/lib}/adapter-transaction.js +0 -0
- /package/{lib → installation/lib}/bundle.js +0 -0
- /package/{lib → installation/lib}/cli.js +0 -0
- /package/{lib → installation/lib}/constants.js +0 -0
- /package/{lib → installation/lib}/hash.js +0 -0
- /package/{lib → installation/lib}/paths.js +0 -0
- /package/{lib → installation/lib}/prompts.js +0 -0
- /package/{lib → installation/lib}/receipt.js +0 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
# Functional structural API
|
|
2
|
+
|
|
3
|
+
Open this second-level reference from `../array-mechanics.md` when selecting a `brainunit.math` function for joining, stacking, splitting, broadcasting, reshaping, repeating, or selecting values. The module API categorizes these as functions that keep units.
|
|
4
|
+
|
|
5
|
+
```python
|
|
6
|
+
import brainunit as u
|
|
7
|
+
import jax.numpy as jnp
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
Examples use concise `Quantity(..., "unit")` results; the exact displayed representation can vary by backend and BrainUnit version.
|
|
11
|
+
|
|
12
|
+
## Contents
|
|
13
|
+
|
|
14
|
+
- [Joining and stacking](#joining-and-stacking)
|
|
15
|
+
- [Splitting](#splitting)
|
|
16
|
+
- [Minimum rank and broadcasting](#minimum-rank-and-broadcasting)
|
|
17
|
+
- [Shape and axis operations](#shape-and-axis-operations)
|
|
18
|
+
- [Repetition](#repetition)
|
|
19
|
+
- [Selection](#selection)
|
|
20
|
+
|
|
21
|
+
## Joining and stacking
|
|
22
|
+
|
|
23
|
+
| Exact signature | One-line description | Example & result |
|
|
24
|
+
|---|---|---|
|
|
25
|
+
| `concatenate(arrays, axis=0, dtype=None, **kwargs)` | Join a sequence of quantities or arrays along an existing axis. | `a = [1, 2] * u.second; b = [3, 4] * u.second; u.math.concatenate([a, b])`<br>`# Quantity([1 2 3 4], "s")` |
|
|
26
|
+
| `stack(arrays, axis=0, dtype=None, **kwargs)` | Join a sequence of quantities or arrays along a new axis. | `a = [1, 2, 3] * u.second; b = [4, 5, 6] * u.second; u.math.stack([a, b])`<br>`# Quantity([[1 2 3] [4 5 6]], "s")` |
|
|
27
|
+
| `block(arrays, **kwargs)` | Assemble a quantity or an array from nested lists of blocks. | `a = [[1, 2], [3, 4]] * u.second; u.math.block(a)`<br>`# Quantity([[1 2] [3 4]], "s")` |
|
|
28
|
+
| `append(arr, values, axis=None, **kwargs)` | Append values to the end of a quantity or an array. | `a = [1, 2, 3] * u.second; u.math.append(a, 4 * u.second)`<br>`# Quantity([1 2 3 4], "s")` |
|
|
29
|
+
| `row_stack(tup, dtype=None, **kwargs)` | Stack quantities or arrays in sequence vertically (row wise). | `a = [1, 2, 3] * u.meter; b = [4, 5, 6] * u.meter; u.math.row_stack([a, b])`<br>`# Quantity([[1 2 3] [4 5 6]], "m")` |
|
|
30
|
+
| `vstack(tup, dtype=None, **kwargs)` | Stack quantities or arrays in sequence vertically (row wise). | `a = [1, 2, 3] * u.meter; b = [4, 5, 6] * u.meter; u.math.vstack([a, b])`<br>`# Quantity([[1 2 3] [4 5 6]], "m")` |
|
|
31
|
+
| `hstack(arrays, dtype=None, **kwargs)` | Stack quantities or arrays in sequence horizontally (column wise). | `a = [1, 2, 3] * u.meter; b = [4, 5, 6] * u.meter; u.math.hstack([a, b])`<br>`# Quantity([1 2 3 4 5 6], "m")` |
|
|
32
|
+
| `dstack(arrays, dtype=None, **kwargs)` | Stack quantities or arrays in sequence depth wise (along the third axis). | `a = [[1], [2], [3]] * u.meter; b = [[4], [5], [6]] * u.meter; u.math.dstack([a, b])`<br>`# Quantity([[[1 4]] [[2 5]] [[3 6]]], "m")` |
|
|
33
|
+
| `column_stack(tup, **kwargs)` | Stack 1-D arrays as columns into a 2-D array. | `a = [1, 2, 3] * u.second; b = [4, 5, 6] * u.second; u.math.column_stack([a, b])`<br>`# Quantity([[1 4] [2 5] [3 6]], "s")` |
|
|
34
|
+
|
|
35
|
+
`row_stack` is documented as an alias of `vstack`; its callable signature therefore mirrors `vstack`.
|
|
36
|
+
|
|
37
|
+
## Splitting
|
|
38
|
+
|
|
39
|
+
| Exact signature | One-line description | Example & result |
|
|
40
|
+
|---|---|---|
|
|
41
|
+
| `split(a, indices_or_sections, axis=0, **kwargs)` | Split a quantity or array into a list of multiple sub-arrays. | `a = jnp.arange(9.0) * u.second; u.math.split(a, 3)`<br>`# [Quantity([0. 1. 2.], "s"), Quantity([3. 4. 5.], "s"), Quantity([6. 7. 8.], "s")]` |
|
|
42
|
+
| `array_split(ary, indices_or_sections, axis=0, **kwargs)` | Split an array into multiple sub-arrays. | `a = jnp.arange(9.0) * u.second; parts = u.math.array_split(a, 3); [x.shape for x in parts]`<br>`# [(3,), (3,), (3,)]` |
|
|
43
|
+
| `dsplit(a, indices_or_sections, **kwargs)` | Split a quantity or an array into multiple sub-arrays along the third axis (depth). | `a = jnp.arange(16.0).reshape(2, 2, 4) * u.meter; parts = u.math.dsplit(a, 2); [x.shape for x in parts]`<br>`# [(2, 2, 2), (2, 2, 2)]` |
|
|
44
|
+
| `hsplit(a, indices_or_sections, **kwargs)` | Split a quantity or an array into multiple sub-arrays horizontally (column-wise). | `a = jnp.arange(16.0).reshape(4, 4) * u.meter; parts = u.math.hsplit(a, 2); [x.shape for x in parts]`<br>`# [(4, 2), (4, 2)]` |
|
|
45
|
+
| `vsplit(a, indices_or_sections, **kwargs)` | Split a quantity or an array into multiple sub-arrays vertically (row-wise). | `a = jnp.arange(16.0).reshape(4, 4) * u.meter; parts = u.math.vsplit(a, 2); [x.shape for x in parts]`<br>`# [(2, 4), (2, 4)]` |
|
|
46
|
+
|
|
47
|
+
## Minimum rank and broadcasting
|
|
48
|
+
|
|
49
|
+
| Exact signature | One-line description | Example & result |
|
|
50
|
+
|---|---|---|
|
|
51
|
+
| `atleast_1d(*arys, **kwargs)` | View inputs as quantities or arrays with at least one dimension. | `u.math.atleast_1d(0 * u.second)`<br>`# Quantity([0], "s")` |
|
|
52
|
+
| `atleast_2d(*arys, **kwargs)` | View inputs as quantities or arrays with at least two dimensions. | `a = [1, 2, 3] * u.second; u.math.atleast_2d(a)`<br>`# Quantity([[1 2 3]], "s")` |
|
|
53
|
+
| `atleast_3d(*arys, **kwargs)` | View inputs as quantities or arrays with at least three dimensions. | `a = [[1, 2], [3, 4]] * u.meter; u.math.atleast_3d(a).shape`<br>`# (2, 2, 1)` |
|
|
54
|
+
| `broadcast_arrays(*args, **kwargs)` | Broadcast any number of arrays against each other. | `a = [1, 2, 3] * u.second; b = [[4], [5]] * u.second; result = u.math.broadcast_arrays(a, b); [x.shape for x in result]`<br>`# [(2, 3), (2, 3)]` |
|
|
55
|
+
| `broadcast_to(array, shape, **kwargs)` | Broadcast an array to a new shape. | `a = [1, 2, 3] * u.meter; u.math.broadcast_to(a, (2, 3))`<br>`# Quantity([[1 2 3] [1 2 3]], "m")` |
|
|
56
|
+
|
|
57
|
+
## Shape and axis operations
|
|
58
|
+
|
|
59
|
+
| Exact signature | One-line description | Example & result |
|
|
60
|
+
|---|---|---|
|
|
61
|
+
| `reshape(a, shape, order='C', **kwargs)` | Gives a new shape to a quantity or an array without changing its data. | `a = [1, 2, 3, 4] * u.second; u.math.reshape(a, (2, 2))`<br>`# Quantity([[1 2] [3 4]], "s")` |
|
|
62
|
+
| `moveaxis(a, source, destination, **kwargs)` | Moves axes of a quantity or an array to new positions. | `a = jnp.zeros((3, 4, 5)) * u.meter; u.math.moveaxis(a, 0, -1).shape`<br>`# (4, 5, 3)` |
|
|
63
|
+
| `transpose(a, axes=None, **kwargs)` | Permute the dimensions of a quantity or an array. | `a = jnp.ones((2, 3)) * u.second; u.math.transpose(a).shape`<br>`# (3, 2)` |
|
|
64
|
+
| `swapaxes(a, axis1, axis2, **kwargs)` | Interchange two axes of a quantity or an array. | `a = jnp.zeros((3, 4, 5)) * u.meter; u.math.swapaxes(a, 0, 2).shape`<br>`# (5, 4, 3)` |
|
|
65
|
+
| `expand_dims(a, axis, **kwargs)` | Expand the shape of a quantity or an array. | `a = [1, 2, 3] * u.meter; u.math.expand_dims(a, axis=0).shape`<br>`# (1, 3)` |
|
|
66
|
+
| `squeeze(a, axis=None, **kwargs)` | Remove single-dimensional entries from the shape of a quantity or an array. | `a = [[[1], [2], [3]]] * u.second; u.math.squeeze(a).shape`<br>`# (3,)` |
|
|
67
|
+
| `flatten(x, start_axis=None, end_axis=None, **kwargs)` | Flattens input by reshaping it into a one-dimensional tensor. | `a = [[1, 2], [3, 4]] * u.second; u.math.flatten(a)`<br>`# Quantity([1 2 3 4], "s")` |
|
|
68
|
+
| `unflatten(x, axis, sizes, **kwargs)` | Expands a dimension of the input tensor over multiple dimensions. | `a = [1, 2, 3, 4, 5, 6] * u.meter; u.math.unflatten(a, 0, (2, 3))`<br>`# Quantity([[1 2 3] [4 5 6]], "m")` |
|
|
69
|
+
|
|
70
|
+
## Repetition
|
|
71
|
+
|
|
72
|
+
| Exact signature | One-line description | Example & result |
|
|
73
|
+
|---|---|---|
|
|
74
|
+
| `tile(A, reps, **kwargs)` | Construct a quantity or an array by repeating `A` the number of times given by `reps`. | `a = [1, 2, 3] * u.meter; u.math.tile(a, 2)`<br>`# Quantity([1 2 3 1 2 3], "m")` |
|
|
75
|
+
| `repeat(a, repeats, axis=None, total_repeat_length=None, **kwargs)` | Repeat elements of a quantity or an array. | `a = [1, 2, 3] * u.second; u.math.repeat(a, 2)`<br>`# Quantity([1 1 2 2 3 3], "s")` |
|
|
76
|
+
|
|
77
|
+
## Selection
|
|
78
|
+
|
|
79
|
+
| Exact signature | One-line description | Example & result |
|
|
80
|
+
|---|---|---|
|
|
81
|
+
| `take(a, indices, axis=None, mode=None, unique_indices=False, indices_are_sorted=False, fill_value=None, **kwargs)` | Take elements from an array along an axis. | `a = [4, 3, 5, 7, 6, 8] * u.second; u.math.take(a, jnp.array([0, 1, 4]))`<br>`# Quantity([4 3 6], "s")` |
|
|
82
|
+
| `gather(input, dim, index, **kwargs)` | Gather values along an axis specified by `dim`, according to `index`. | `a = jnp.array([[1, 2], [3, 4]]) * u.mV; index = jnp.array([[0, 0], [1, 0]]); u.math.gather(a, 1, index)`<br>`# Quantity([[1 1] [4 3]], "mV")` |
|
|
83
|
+
| `choose(a, choices, mode='raise', **kwargs)` | Construct a quantity or an array from an index array and a set of arrays to choose from. | `choices = [jnp.array([1, 2, 3]), jnp.array([4, 5, 6])]; u.math.choose(jnp.array([0, 1, 0]), choices)`<br>`# Array([1, 5, 3], dtype=int32)` |
|
|
84
|
+
| `compress(condition, a, axis=None, *, size=None, fill_value=None, **kwargs)` | Return selected slices of a quantity or an array along given axis. | `a = [1, 2, 3, 4] * u.meter; u.math.compress(jnp.array([0, 1, 1, 0]), a)`<br>`# Quantity([2 3], "m")` |
|
|
85
|
+
| `extract(condition, arr, *, size=None, fill_value=None, **kwargs)` | Return the elements of an array that satisfy some condition. | `a = jnp.array([1, 2, 3]) * u.meter; u.math.extract(a.mantissa > 1, a)`<br>`# Quantity([2 3], "m")` |
|
|
86
|
+
|
|
87
|
+
## Sources mirrored
|
|
88
|
+
|
|
89
|
+
- https://brainunit.readthedocs.io/apis/brainunit.math.html
|
|
90
|
+
- Generated function pages linked from the module index: `https://brainunit.readthedocs.io/apis/generated/brainunit.math.<function>.html`
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
# Array mechanics
|
|
2
|
+
|
|
3
|
+
Use this reference for array metadata, indexing, functional updates, shape and axis transformations, joins and splits, repetition, named-axis transformations, and backend conversion of an existing `Quantity`.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- [Metadata and identity](#metadata-and-identity)
|
|
8
|
+
- [Indexing and functional updates](#indexing-and-functional-updates)
|
|
9
|
+
- [Quantity methods](#quantity-methods)
|
|
10
|
+
- [Functional structural API](#functional-structural-api)
|
|
11
|
+
- [Named-axis transformations](#named-axis-transformations)
|
|
12
|
+
- [Backend conversion](#backend-conversion)
|
|
13
|
+
- [Source-backed gotchas](#source-backed-gotchas)
|
|
14
|
+
|
|
15
|
+
## Metadata and identity
|
|
16
|
+
|
|
17
|
+
| Property | Official meaning |
|
|
18
|
+
|---|---|
|
|
19
|
+
| `q.shape` | Shape of the mantissa array |
|
|
20
|
+
| `q.dtype` | Data type of the mantissa |
|
|
21
|
+
| `q.backend` | One of `'numpy'`, `'jax'`, `'cupy'`, `'torch'`, `'dask'`, or `'ndonnx'` |
|
|
22
|
+
| `q.itemsize` | Bytes in one mantissa element |
|
|
23
|
+
| `q.nbytes` | Total bytes consumed by the mantissa array |
|
|
24
|
+
| `q.strides` | Byte steps in each dimension, mirroring `numpy.ndarray.strides` |
|
|
25
|
+
| `q.flat` | 1-D iterator over mantissa elements with units preserved |
|
|
26
|
+
| `q.mT` | Transpose of the final two dimensions; requires at least 2-D |
|
|
27
|
+
|
|
28
|
+
Use `u.math.ndim(x)`, `u.math.shape(x)`, and `u.math.size(x)` when a functional query is preferable to a property lookup.
|
|
29
|
+
|
|
30
|
+
## Indexing and functional updates
|
|
31
|
+
|
|
32
|
+
Ordinary indexing preserves the unit. `q.at` provides a functionally pure update interface:
|
|
33
|
+
|
|
34
|
+
```python
|
|
35
|
+
import brainunit as u
|
|
36
|
+
import jax.numpy as jnp
|
|
37
|
+
|
|
38
|
+
x = jnp.arange(5.0) * u.mV
|
|
39
|
+
y = x.at[2].add(10 * u.mV)
|
|
40
|
+
# Expected: [0, 1, 12, 3, 4] mV; x remains unchanged.
|
|
41
|
+
selected = x.at[2].get()
|
|
42
|
+
# Expected: 2 mV.
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
The documented update mappings are:
|
|
46
|
+
|
|
47
|
+
| Functional form | In-place equivalent |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `x = x.at[idx].set(y)` | `x[idx] = y` |
|
|
50
|
+
| `x = x.at[idx].add(y)` | `x[idx] += y` |
|
|
51
|
+
| `x = x.at[idx].multiply(y)` | `x[idx] *= y` |
|
|
52
|
+
| `x = x.at[idx].divide(y)` | `x[idx] /= y` |
|
|
53
|
+
| `x = x.at[idx].power(y)` | `x[idx] **= y` |
|
|
54
|
+
| `x = x.at[idx].min(y)` | `x[idx] = minimum(x[idx], y)` |
|
|
55
|
+
| `x = x.at[idx].max(y)` | `x[idx] = maximum(x[idx], y)` |
|
|
56
|
+
| `x = x.at[idx].apply(ufunc)` | `ufunc.at(x, idx)` |
|
|
57
|
+
| `x.at[idx].get()` | `x[idx]` |
|
|
58
|
+
|
|
59
|
+
None of these expressions modifies the original object; each returns a modified copy. Inside `jit()`, assignments such as `x = x.at[idx].set(y)` are documented as being applied in place.
|
|
60
|
+
|
|
61
|
+
## Quantity methods
|
|
62
|
+
|
|
63
|
+
| Exact signature | One-line description | Example & result |
|
|
64
|
+
|---|---|---|
|
|
65
|
+
| `reshape(shape, order='C')` | Return a quantity with the same data but a new shape. | `q = u.Quantity(jnp.array([1.0, 2.0, 3.0]), unit=u.mV); q.reshape((3, 1)).shape`<br>`# (3, 1)` |
|
|
66
|
+
| `flatten()` | Return a 1-D copy of this quantity. | `q = u.Quantity(jnp.array([[1.0, 2.0], [3.0, 4.0]]), unit=u.mV); q.flatten()`<br>`# Quantity([1. 2. 3. 4.], "mV")` |
|
|
67
|
+
| `squeeze(axis=None)` | Remove length-one axes from the array. | `q = u.Quantity(jnp.array([[[1.0]]]), unit=u.mV); q.squeeze().shape`<br>`# ()` |
|
|
68
|
+
| `expand_dims(axis)` | Insert new axes at the given positions. | `q = u.Quantity(jnp.array([1.0, 2.0]), unit=u.mV); q.expand_dims(0).shape`<br>`# (1, 2)` |
|
|
69
|
+
| `unsqueeze(axis)` | Insert a length-one axis (PyTorch-style alias for `expand_dims()`). | `q = u.Quantity(jnp.array([1.0, 2.0]), unit=u.mV); q.unsqueeze(0).shape`<br>`# (1, 2)` |
|
|
70
|
+
| `transpose(*axes)` | Return the array with axes transposed. | `q = u.Quantity(jnp.array([[1.0, 2.0], [3.0, 4.0]]), unit=u.mV); q.transpose().shape`<br>`# (2, 2)` |
|
|
71
|
+
| `swapaxes(axis1, axis2)` | Interchange two axes of the array. | `q = u.Quantity(jnp.array([[1.0, 2.0], [3.0, 4.0]]), unit=u.mV); q.swapaxes(0, 1).shape`<br>`# (2, 2)` |
|
|
72
|
+
| `repeat(repeats, axis=None)` | Repeat elements of the array. | `q = u.Quantity(jnp.array([1.0, 2.0]), unit=u.mV); q.repeat(2)`<br>`# Quantity([1. 1. 2. 2.], "mV")` |
|
|
73
|
+
| `tile(reps)` | Construct an array by repeating this quantity. | `q = u.Quantity(jnp.array([1.0, 2.0]), unit=u.mV); q.tile(2)`<br>`# Quantity([1. 2. 1. 2.], "mV")` |
|
|
74
|
+
| `split(indices_or_sections, axis=0)` | Split the array into multiple sub-arrays. | `q = u.Quantity(jnp.array([1.0, 2.0, 3.0]), unit=u.mV); parts = q.split(3); len(parts)`<br>`# 3` |
|
|
75
|
+
| `take(indices, axis=None, mode=None, unique_indices=False, indices_are_sorted=False, fill_value=None)` | Select elements from the array at the given indices. | `q = u.Quantity(jnp.array([10.0, 20.0, 30.0]), unit=u.mV); q.take(jnp.array([0, 2]))`<br>`# Quantity([10. 30.], "mV")` |
|
|
76
|
+
| `sort(axis=-1, stable=True, order=None)` | Sort the array in-place along the given axis. | `q = u.Quantity(jnp.array([3.0, 1.0, 2.0]), unit=u.mV); q.sort()`<br>`# Quantity([1. 2. 3.], "mV")` |
|
|
77
|
+
| `diagonal(offset=0, axis1=0, axis2=1)` | Return specified diagonals, preserving units. | `q = u.Quantity(jnp.array([[1.0, 2.0], [3.0, 4.0]]), unit=u.mV); q.diagonal()`<br>`# Quantity([1. 4.], "mV")` |
|
|
78
|
+
| `trace(offset=0, axis1=0, axis2=1)` | Sum along diagonals of the array, preserving units. | `q = u.Quantity(jnp.eye(3), unit=u.mV); q.trace()`<br>`# Quantity(3., "mV")` |
|
|
79
|
+
| `astype(dtype)` | Return a copy of this quantity with the mantissa cast to `dtype`. | `q = u.Quantity(jnp.array([1.0, 2.0]), unit=u.mV); q.astype(jnp.float64).dtype`<br>`# float64` |
|
|
80
|
+
|
|
81
|
+
## Functional structural API
|
|
82
|
+
|
|
83
|
+
Use these `brainunit.math` functions when composing transformations without binding the operation to a method:
|
|
84
|
+
|
|
85
|
+
| Family | Exact APIs |
|
|
86
|
+
|---|---|
|
|
87
|
+
| Join | `concatenate(arrays, axis=0, dtype=None, **kwargs)`, `stack(arrays, axis=0, dtype=None, **kwargs)`, `block(arrays, **kwargs)`, `append(arr, values, axis=None, **kwargs)` |
|
|
88
|
+
| Stack variants | `row_stack`, `vstack`, `hstack`, `dstack`, `column_stack` |
|
|
89
|
+
| Split | `split`, `array_split`, `dsplit`, `hsplit`, `vsplit` |
|
|
90
|
+
| Minimum rank | `atleast_1d`, `atleast_2d`, `atleast_3d` |
|
|
91
|
+
| Broadcast | `broadcast_arrays(*args, **kwargs)`, `broadcast_to(array, shape, **kwargs)` |
|
|
92
|
+
| Shape | `reshape`, `moveaxis`, `transpose`, `swapaxes`, `expand_dims`, `squeeze`, `flatten`, `unflatten` |
|
|
93
|
+
| Repetition | `tile(A, reps, **kwargs)`, `repeat(a, repeats, axis=None, total_repeat_length=None, **kwargs)` |
|
|
94
|
+
| Selection | `take`, `gather`, `choose`, `compress`, `extract` |
|
|
95
|
+
|
|
96
|
+
The module API categorizes these as functions that keep units. Open the [complete functional structural API reference](array-mechanics/functional-structural-api.md) for every exact signature, official one-line description, and example with its result.
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
a = [1, 2] * u.second
|
|
100
|
+
b = [3, 4] * u.second
|
|
101
|
+
joined = u.math.concatenate([a, b])
|
|
102
|
+
# Expected: [1, 2, 3, 4] s.
|
|
103
|
+
rows = u.math.stack([a, b])
|
|
104
|
+
# Expected: [[1, 2], [3, 4]] s with shape (2, 2).
|
|
105
|
+
parts = u.math.array_split(joined, 2)
|
|
106
|
+
# Expected: two Quantity arrays, [1, 2] s and [3, 4] s.
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## Named-axis transformations
|
|
110
|
+
|
|
111
|
+
`einrearrange(x, pattern, **axes_lengths)` covers transpose, reshape, squeeze, unsqueeze, stack, and concatenate. Composition uses C-order enumeration.
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
channels_last = u.math.einrearrange(x, 'b c h w -> b h w c')
|
|
115
|
+
# Expected: for shape-compatible x, axes change from (b, c, h, w) to (b, h, w, c); unit preserved.
|
|
116
|
+
flat = u.math.einrearrange(x, 'b h w c -> (b h w c)')
|
|
117
|
+
# Expected: for shape-compatible x, a one-dimensional Quantity with all elements; unit preserved.
|
|
118
|
+
split_batch = u.math.einrearrange(
|
|
119
|
+
x,
|
|
120
|
+
'(b1 b2) h w c -> b1 b2 h w c',
|
|
121
|
+
b1=2,
|
|
122
|
+
)
|
|
123
|
+
# Expected: for shape-compatible x, the batch axis splits into axes 2 and b/2; unit preserved.
|
|
124
|
+
```
|
|
125
|
+
|
|
126
|
+
`einrepeat(x, pattern, **axes_lengths)` covers repeat, tile, and broadcast-like patterns:
|
|
127
|
+
|
|
128
|
+
```python
|
|
129
|
+
expanded = u.math.einrepeat(x, 'h w c -> h new_axis w c', new_axis=5)
|
|
130
|
+
# Expected: for shape-compatible x, a new length-5 axis is inserted after h; unit preserved.
|
|
131
|
+
tiled = u.math.einrepeat(x, 'h w c -> (2 h) (2 w) c')
|
|
132
|
+
# Expected: for shape-compatible x, h and w each double by repetition; unit preserved.
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Use `einshape(x, pattern)` to map named axes to lengths. Use `einreduce()` and `einsum()` through `math-function-library.md` when axes are reduced or contracted.
|
|
136
|
+
|
|
137
|
+
## Backend conversion
|
|
138
|
+
|
|
139
|
+
All `Quantity` backend methods retain the wrapper and attached unit:
|
|
140
|
+
|
|
141
|
+
| Exact signature | Target mantissa |
|
|
142
|
+
|---|---|
|
|
143
|
+
| `to_jax()` | JAX `Array` |
|
|
144
|
+
| `to_numpy()` | `numpy.ndarray` |
|
|
145
|
+
| `to_cupy(*, device=None)` | `cupy.ndarray` |
|
|
146
|
+
| `to_torch(*, device=None, dtype=None)` | `torch.Tensor` |
|
|
147
|
+
| `to_dask(*, chunks='auto')` | `dask.array.Array` |
|
|
148
|
+
| `to_ndonnx()` | Symbolic `ndonnx.Array` |
|
|
149
|
+
|
|
150
|
+
`u.math.as_numpy(x)` is different: for a `Quantity`, it returns the underlying mantissa in the current unit scale as a NumPy array.
|
|
151
|
+
|
|
152
|
+
## Source-backed gotchas
|
|
153
|
+
|
|
154
|
+
- `q.at` supports NumPy, JAX, CuPy, Torch, and Dask mantissas. The ndonnx backend raises `BackendError`; the API recommends `.to_numpy()` first.
|
|
155
|
+
- Repeated-index update semantics differ by backend. JAX, NumPy, and CuPy accumulate documented operations; some Torch and Dask operations use last-write-wins behavior.
|
|
156
|
+
- Dask functional updates support slice, scalar integer, 1-D integer, and boolean-mask indices; multidimensional fancy integer indexing raises `NotImplementedError`.
|
|
157
|
+
- `mT` transposes only the last two dimensions and requires at least two dimensions.
|
|
158
|
+
- `u.math.as_numpy(q)` drops the `Quantity` wrapper; `q.to_numpy()` keeps it.
|
|
159
|
+
- In Einstein patterns, axis order inside parentheses changes element order. A literal `1` or `()` creates a length-one axis, and either can be removed in the inverse pattern.
|
|
160
|
+
|
|
161
|
+
## Sources mirrored
|
|
162
|
+
|
|
163
|
+
- https://brainx.chaobrain.com/brainunit/apis/generated/brainunit.Quantity.html
|
|
164
|
+
- https://brainunit.readthedocs.io/apis/brainunit.math.html
|
|
165
|
+
- https://brainunit.readthedocs.io/unit_operations/einstein_operations.html
|