From 05c9696c94ffb4ee839c30f7cbcd481f6a3eaf47 Mon Sep 17 00:00:00 2001 From: He Sichao <1310722434@qq.com> Date: Mon, 17 Jun 2024 22:13:06 +0800 Subject: [PATCH] Update --- docs/apis/brainunit.math.rst | 796 ++--- docs/index.rst | 2 +- docs/tutorials.rst | 7 +- docs/tutorials/mathematical_functions.ipynb | 95 + docs/tutorials/old_physical_units.ipynb | 3396 +++++++++++++++++++ docs/tutorials/physical_units.ipynb | 3299 +----------------- 6 files changed, 3914 insertions(+), 3681 deletions(-) create mode 100644 docs/tutorials/mathematical_functions.ipynb create mode 100644 docs/tutorials/old_physical_units.ipynb diff --git a/docs/apis/brainunit.math.rst b/docs/apis/brainunit.math.rst index 60ec0a1..2a05c7d 100644 --- a/docs/apis/brainunit.math.rst +++ b/docs/apis/brainunit.math.rst @@ -1,398 +1,398 @@ -``brainunit.math`` module -========================= - -.. currentmodule:: brainunit.math -.. automodule:: brainunit.math - -Array Creation --------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - full - full_like - eye - identity - diag - tri - tril - triu - empty - empty_like - ones - ones_like - zeros - zeros_like - array - asarray - arange - linspace - logspace - fill_diagonal - array_split - meshgrid - vander - - -Array Manipulation ------------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - reshape - moveaxis - transpose - swapaxes - row_stack - concatenate - stack - vstack - hstack - dstack - column_stack - split - dsplit - hsplit - vsplit - tile - repeat - unique - append - flip - fliplr - flipud - roll - atleast_1d - atleast_2d - atleast_3d - expand_dims - squeeze - sort - argsort - argmax - argmin - argwhere - nonzero - flatnonzero - searchsorted - extract - count_nonzero - max - min - amax - amin - block - compress - diagflat - diagonal - choose - ravel - - -Functions Accepting Unitless ----------------------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - exp - exp2 - expm1 - log - log10 - log1p - log2 - arccos - arccosh - arcsin - arcsinh - arctan - arctanh - cos - cosh - sin - sinc - sinh - tan - tanh - deg2rad - rad2deg - degrees - radians - angle - percentile - nanpercentile - quantile - nanquantile - hypot - arctan2 - logaddexp - logaddexp2 - - -Functions with Bitwise Operations ---------------------------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - bitwise_not - invert - bitwise_and - bitwise_or - bitwise_xor - left_shift - right_shift - - -Functions Changing Unit ------------------------ - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - reciprocal - prod - product - nancumprod - nanprod - cumprod - cumproduct - var - nanvar - cbrt - square - sqrt - multiply - divide - power - cross - ldexp - true_divide - floor_divide - float_power - divmod - remainder - convolve - - -Indexing Functions ------------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - where - tril_indices - tril_indices_from - triu_indices - triu_indices_from - take - select - - -Functions Keeping Unit ----------------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - real - imag - conj - conjugate - negative - positive - abs - round - around - round_ - rint - floor - ceil - trunc - fix - sum - nancumsum - nansum - cumsum - ediff1d - absolute - fabs - median - nanmin - nanmax - ptp - average - mean - std - nanmedian - nanmean - nanstd - diff - modf - fmod - mod - copysign - heaviside - maximum - minimum - fmax - fmin - lcm - gcd - interp - clip - - -Logical Functions ------------------ - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - all - any - logical_not - equal - not_equal - greater - greater_equal - less - less_equal - array_equal - isclose - allclose - logical_and - logical_or - logical_xor - alltrue - sometrue - - -Functions Matching Unit ------------------------ - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - add - subtract - nextafter - - -Functions Removing Unit ------------------------ - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - signbit - sign - histogram - bincount - corrcoef - correlate - cov - digitize - - -Window Functions ----------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - bartlett - blackman - hamming - hanning - kaiser - - -Get Attribute Functions ------------------------ - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - ndim - isreal - isscalar - isfinite - isinf - isnan - shape - size - - -Linear Algebra Functions ------------------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - dot - vdot - inner - outer - kron - matmul - trace - - -More Functions --------------- - -.. autosummary:: - :toctree: generated/ - :nosignatures: - :template: classtemplate.rst - - finfo - iinfo - broadcast_arrays - broadcast_shapes - einsum - gradient - intersect1d - nan_to_num - nanargmax - nanargmin - rot90 - tensordot - frexp - dtype - e - pi - inf - - +``brainunit.math`` module +========================= + +.. currentmodule:: brainunit.math +.. automodule:: brainunit.math + +Array Creation +-------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + full + full_like + eye + identity + diag + tri + tril + triu + empty + empty_like + ones + ones_like + zeros + zeros_like + array + asarray + arange + linspace + logspace + fill_diagonal + array_split + meshgrid + vander + + +Array Manipulation +------------------ + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + reshape + moveaxis + transpose + swapaxes + row_stack + concatenate + stack + vstack + hstack + dstack + column_stack + split + dsplit + hsplit + vsplit + tile + repeat + unique + append + flip + fliplr + flipud + roll + atleast_1d + atleast_2d + atleast_3d + expand_dims + squeeze + sort + argsort + argmax + argmin + argwhere + nonzero + flatnonzero + searchsorted + extract + count_nonzero + max + min + amax + amin + block + compress + diagflat + diagonal + choose + ravel + + +Functions Accepting Unitless +---------------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + exp + exp2 + expm1 + log + log10 + log1p + log2 + arccos + arccosh + arcsin + arcsinh + arctan + arctanh + cos + cosh + sin + sinc + sinh + tan + tanh + deg2rad + rad2deg + degrees + radians + angle + percentile + nanpercentile + quantile + nanquantile + hypot + arctan2 + logaddexp + logaddexp2 + round + around + round_ + rint + floor + ceil + trunc + fix + corrcoef + correlate + cov + + +Functions with Bitwise Operations +--------------------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + bitwise_not + invert + bitwise_and + bitwise_or + bitwise_xor + left_shift + right_shift + + +Functions Changing Unit +----------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + reciprocal + prod + product + nancumprod + nanprod + cumprod + cumproduct + var + nanvar + cbrt + square + sqrt + multiply + divide + power + cross + ldexp + true_divide + floor_divide + float_power + divmod + remainder + convolve + + +Indexing Functions +------------------ + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + where + tril_indices + tril_indices_from + triu_indices + triu_indices_from + take + select + + +Functions Keeping Unit +---------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + real + imag + conj + conjugate + negative + positive + abs + sum + nancumsum + nansum + cumsum + ediff1d + absolute + fabs + median + nanmin + nanmax + ptp + average + mean + std + nanmedian + nanmean + nanstd + diff + modf + fmod + mod + copysign + heaviside + maximum + minimum + fmax + fmin + lcm + gcd + interp + clip + histogram + + +Logical Functions +----------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + all + any + logical_not + equal + not_equal + greater + greater_equal + less + less_equal + array_equal + isclose + allclose + logical_and + logical_or + logical_xor + alltrue + sometrue + + +Functions Matching Unit +----------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + add + subtract + nextafter + + +Functions Removing Unit +----------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + signbit + sign + bincount + digitize + + +Window Functions +---------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + bartlett + blackman + hamming + hanning + kaiser + + +Get Attribute Functions +----------------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + ndim + isreal + isscalar + isfinite + isinf + isnan + shape + size + + +Linear Algebra Functions +------------------------ + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + dot + vdot + inner + outer + kron + matmul + trace + + +More Functions +-------------- + +.. autosummary:: + :toctree: generated/ + :nosignatures: + :template: classtemplate.rst + + finfo + iinfo + broadcast_arrays + broadcast_shapes + einsum + gradient + intersect1d + nan_to_num + nanargmax + nanargmin + rot90 + tensordot + frexp + dtype + e + pi + inf + + diff --git a/docs/index.rst b/docs/index.rst index 67cb379..8b09d23 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -59,6 +59,6 @@ See also the BDP ecosystem :hidden: :maxdepth: 2 - tutorials/physical_units.rst + tutorials.rst api.rst diff --git a/docs/tutorials.rst b/docs/tutorials.rst index ed16e77..25ca43c 100644 --- a/docs/tutorials.rst +++ b/docs/tutorials.rst @@ -1,7 +1,8 @@ -Tutorials Documentation -======================= +Tutorials +========= .. toctree:: :maxdepth: 2 - tutorials/physical_units \ No newline at end of file + tutorials/physical_units + tutorials/mathematical_functions \ No newline at end of file diff --git a/docs/tutorials/mathematical_functions.ipynb b/docs/tutorials/mathematical_functions.ipynb new file mode 100644 index 0000000..8edb093 --- /dev/null +++ b/docs/tutorials/mathematical_functions.ipynb @@ -0,0 +1,95 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# NumPy Functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basic" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Basic Operations" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Universal Functions" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Indexing, Slicing and Iterating" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Shape Manipulation" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Changing the shape of a Quantity" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Stacking together different Quantities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Splitting one Quantity into several smaller ones" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Comparing Quantities" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Customize Functions that Accept Quantities" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "language_info": { + "name": "python" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +} diff --git a/docs/tutorials/old_physical_units.ipynb b/docs/tutorials/old_physical_units.ipynb new file mode 100644 index 0000000..f6ebb36 --- /dev/null +++ b/docs/tutorials/old_physical_units.ipynb @@ -0,0 +1,3396 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Physical Units" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Braincore includes a system for physical units. The base units are defined by their standard SI unit names:\n", + "`amp`/`ampere`, `kilogram`/`kilogramme`, `second`, `metre`/`meter`, `kilogram`, `mole`/`mol`, `kelvin`, and `candela`. In addition to these base units, braincore defines a set of derived units: `coulomb`, `farad`, `gram`/`gramme`, `hertz`, `joule`, `liter`/\n", + "`litre`, `molar`, `pascal`, `ohm`, `siemens`, `volt`, `watt`,\n", + "together with prefixed versions (e.g. `msiemens = 0.001*siemens`) using the\n", + "prefixes `p, n, u, m, k, M, G, T` (two exceptions to this rule: `kilogram`\n", + "is not defined with any additional prefixes, and `metre` and `meter` are\n", + "additionaly defined with the \"centi\" prefix, i.e. `cmetre`/`cmeter`).\n", + "For convenience, a couple of additional useful standard abbreviations such as\n", + "`cm` (instead of `cmetre`/`cmeter`), `nS` (instead of `nsiemens`),\n", + "`ms` (instead of `msecond`), `Hz` (instead of `hertz`), `mM`\n", + "(instead of `mmolar`) are included. To avoid clashes with common variable\n", + "names, no one-letter abbreviations are provided (e.g. you can use `mV` or\n", + "`nS`, but *not* `V` or `S`)." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Importing units\n", + "Braincore generates standard names for units, combining the unit name (e.g. “siemens”) with a prefixes (e.g. “m”), and also generates squared and cubed versions by appending a number. For example, the units “msiemens”, “siemens2”, “usiemens3” are all predefined. You can import these units from the package `brianunit` – accordingly, an `from brainunit import *` will result in everything being imported.\n", + "\n", + "We recommend importing only the units you need, to have a cleaner namespace. For example, `import brainunit as bu` and then using `bu.msiemens` instead of `msiemens`." + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:18:06.720762Z", + "start_time": "2024-06-08T08:18:06.566328Z" + } + }, + "outputs": [], + "source": [ + "import brainunit as bu" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Using units\n", + "You can generate a physical quantity by multiplying a scalar or ndarray with its physical unit:" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "20. * msecond" + ] + }, + "execution_count": 2, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "tau = 20 * bu.ms\n", + "tau" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([10., 20., 30.], dtype=float32) * hertz" + ] + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rates = [10, 20, 30] * bu.Hz\n", + "rates" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[10., 20., 30.],\n", + " [20., 30., 40.]], dtype=float32) * hertz" + ] + }, + "execution_count": 4, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rates = [[10, 20, 30], [20, 30, 40]] * bu.Hz\n", + "rates" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Braincore will check the consistency of operations on units and raise an error for dimensionality mismatches:" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cannot calculate ... += 1, units do not match (units are s and 1).\n" + ] + } + ], + "source": [ + "try:\n", + " tau += 1 # ms? second?\n", + "except Exception as e:\n", + " print(e)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Cannot calculate 3.0 + 3.0, units do not match (units are kg and A).\n" + ] + } + ], + "source": [ + "try:\n", + " 3 * bu.kgram + 3 * bu.amp\n", + "except Exception as e:\n", + " print(e)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Basics\n", + "Numpy functions have been overwritten to correctly work with units.\n", + "\n", + "The important attributes of a `Quantity` object are:\n", + "- `value`: the numerical value of the quantity\n", + "- `unit`: the unit of the quantity\n", + "- `ndim`: the number of dimensions of quantity's value\n", + "- `shape`: the shape of the quantity's value\n", + "- `size`: the size of the quantity's value\n", + "- `dtype`: the dtype of the quantity's value" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### An example" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[10., 20., 30.],\n", + " [20., 30., 40.]], dtype=float32) * hertz" + ] + }, + "execution_count": 7, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rates" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Array([[10., 20., 30.],\n", + " [20., 30., 40.]], dtype=float32)" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rates.value" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "second ** -1" + ] + }, + "execution_count": 9, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rates.unit" + ] + }, + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "(2, (2, 3), 6, dtype('float32'))" + ] + }, + "execution_count": 10, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rates.ndim, rates.shape, rates.size, rates.dtype" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Quantity Creation\n", + "Creating a Quantity object can be accomplished in several ways, categorized based on the type of input used. Here, we present the methods grouped by their input types and characteristics for better clarity." + ] + }, + { + "cell_type": "code", + "execution_count": 11, + "metadata": {}, + "outputs": [], + "source": [ + "import jax.numpy as jnp\n", + "import brainstate as bst\n", + "bst.environ.set(precision=64) # we recommend using 64-bit precision for better numerical stability" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "\n", + "#### Scalar and Array Multiplication\n", + "- Multiplying a Scalar with a Unit\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5. * msecond" + ] + }, + "execution_count": 12, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "5 * bu.ms" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Multiplying a Jax nunmpy value type with a Unit:" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5. * msecond" + ] + }, + "execution_count": 13, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "jnp.float64(5) * bu.ms" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Multiplying a Jax numpy array with a Unit:" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 2., 3.]) * msecond" + ] + }, + "execution_count": 14, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "jnp.array([1, 2, 3]) * bu.ms" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Multiplying a List with a Unit:" + ] + }, + { + "cell_type": "code", + "execution_count": 15, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 2., 3.]) * msecond" + ] + }, + "execution_count": 15, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[1, 2, 3] * bu.ms" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Direct Quantity Creation\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Creating a Quantity Directly with a Value" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Quantity(5.)" + ] + }, + "execution_count": 16, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity(5)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Creating a Quantity Directly with a Value and Unit" + ] + }, + { + "cell_type": "code", + "execution_count": 18, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "5. * msecond" + ] + }, + "execution_count": 18, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity(5, unit=bu.ms)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Creating a Quantity with a Jax numpy Array of Values and a Unit" + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 2., 3.]) * msecond" + ] + }, + "execution_count": 19, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity(jnp.array([1, 2, 3]), unit=bu.ms)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Creating a Quantity with a List of Values and a Unit" + ] + }, + { + "cell_type": "code", + "execution_count": 20, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 2., 3.]) * msecond" + ] + }, + "execution_count": 20, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity([1, 2, 3], unit=bu.ms)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Creating a Quantity with a List of Quantities" + ] + }, + { + "cell_type": "code", + "execution_count": 21, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([0.5, 1. ]) * second" + ] + }, + "execution_count": 21, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity([500 * bu.ms, 1 * bu.second])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "- Using the with_units Method" + ] + }, + { + "cell_type": "code", + "execution_count": 22, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([0.5, 1. ]) * second" + ] + }, + "execution_count": 22, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity.with_units(jnp.array([0.5, 1]), second=1)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Unitless Quantity\n", + "Quantities can be unitless, which means they have no units. If there is no unit provided, the quantity is assumed to be unitless. The following are examples of creating unitless quantities:" + ] + }, + { + "cell_type": "code", + "execution_count": 23, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Quantity(ArrayImpl([1., 2., 3.]))" + ] + }, + "execution_count": 23, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity([1, 2, 3])" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Quantity(ArrayImpl([1., 2., 3.]))" + ] + }, + "execution_count": 24, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity(jnp.array([1, 2, 3]))" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "Quantity(ArrayImpl([], dtype=float64))" + ] + }, + "execution_count": 25, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "bu.Quantity([])" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Illegal Quantity Creation\n", + "The following are examples of illegal quantity creation:" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "All elements must have the same unit\n" + ] + } + ], + "source": [ + "try:\n", + " bu.Quantity([500 * bu.ms, 1])\n", + "except Exception as e:\n", + " print(e)" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "Value 'some' with dtype , >=) are supported:" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.350568Z", + "start_time": "2024-06-08T08:17:18.335369Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(ArrayImpl([10., 12., 14., 16., 18.]) * mvolt,\n", + " ArrayImpl([ 8., 12., 16., 20., 24.]) * mvolt)" + ] + }, + "execution_count": 95, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 = jnp.arange(10, 20, 2) * bu.mV\n", + "q2 = jnp.arange(8, 27, 4) * bu.mV\n", + "q1, q2" + ] + }, + { + "cell_type": "code", + "execution_count": 96, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.463131Z", + "start_time": "2024-06-08T08:17:18.448183Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(Array([False, True, False, False, False], dtype=bool),\n", + " Array([ True, False, True, True, True], dtype=bool))" + ] + }, + "execution_count": 96, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 == q2, q1 != q2" + ] + }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.536630Z", + "start_time": "2024-06-08T08:17:18.523328Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(Array([False, False, True, True, True], dtype=bool),\n", + " Array([False, True, True, True, True], dtype=bool))" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 < q2, q1 <= q2" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.658605Z", + "start_time": "2024-06-08T08:17:18.644318Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(Array([ True, False, False, False, False], dtype=bool),\n", + " Array([ True, True, False, False, False], dtype=bool))" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 > q2, q1 >= q2" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Binary Operations\n", + "The binary operations add (+), subtract (-), multiply (*), divide (/), floor divide (//), remainder (%), divmod (divmod), power (**), matmul (@), shift (<<, >>), round(round) are supported:" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.750090Z", + "start_time": "2024-06-08T08:17:18.733415Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(ArrayImpl([1., 2., 3.]) * mvolt, ArrayImpl([2., 3., 4.]) * mvolt)" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 = jnp.array([1, 2, 3]) * bu.mV\n", + "q2 = jnp.array([2, 3, 4]) * bu.mV\n", + "q1, q2" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.855866Z", + "start_time": "2024-06-08T08:17:18.840386Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(ArrayImpl([3., 5., 7.]) * mvolt, ArrayImpl([-1., -1., -1.]) * mvolt)" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 + q2, q1 - q2" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:18.941214Z", + "start_time": "2024-06-08T08:17:18.927264Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([ 2., 6., 12.]) * mvolt2" + ] + }, + "execution_count": 101, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 * q2" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.058063Z", + "start_time": "2024-06-08T08:17:19.045255Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(Array([0.5 , 0.66666667, 0.75 ], dtype=float64),\n", + " Array([0., 0., 0.], dtype=float64),\n", + " ArrayImpl([1., 2., 3.]) * mvolt)" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 / q2, q1 // q2, q1 % q2" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.137905Z", + "start_time": "2024-06-08T08:17:19.125338Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(Array([0., 0., 0.], dtype=float64), ArrayImpl([1., 2., 3.]) * mvolt)" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "divmod(q1, q2)" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.289789Z", + "start_time": "2024-06-08T08:17:19.275896Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 4., 9.]) * mvolt2" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 ** 2" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.347106Z", + "start_time": "2024-06-08T08:17:19.334827Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "20. * mvolt2" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 @ q2" + ] + }, + { + "cell_type": "code", + "execution_count": 106, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.472101Z", + "start_time": "2024-06-08T08:17:19.456793Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(ArrayImpl([ 0., 4., 8., 12., 16.]) * volt,\n", + " ArrayImpl([0., 0., 0., 0., 1.]) * volt)" + ] + }, + "execution_count": 106, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q1 = bu.Quantity(jnp.arange(5, dtype=jnp.int32), unit=bu.mV.unit)\n", + "q1 << 2, q1 >> 2" + ] + }, + { + "cell_type": "code", + "execution_count": 107, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.531264Z", + "start_time": "2024-06-08T08:17:19.514038Z" + } + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "round(80.23456, 2) : 80.23 mV\n", + "round(100.000056, 3) : 100. mV\n", + "round(-100.000056, 3) : -100. mV\n" + ] + } + ], + "source": [ + "q1 = 80.23456 * bu.mV\n", + "q2 = 100.000056 * bu.mV\n", + "q3 = -100.000056 * bu.mV\n", + "print(\"round(80.23456, 2) : \", q1.round(5))\n", + "print(\"round(100.000056, 3) : \", q2.round(6))\n", + "print(\"round(-100.000056, 3) : \", q3.round(6))" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Shape Manipulation\n", + "The shape of an array can be changed with various commands. Note that the following three commands all return a modified array, but do not change the original array:" + ] + }, + { + "cell_type": "code", + "execution_count": 108, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.688835Z", + "start_time": "2024-06-08T08:17:19.670457Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[1., 2.],\n", + " [3., 4.]]) * mvolt" + ] + }, + "execution_count": 108, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q = [[1, 2], [3, 4]] * bu.mV\n", + "q" + ] + }, + { + "cell_type": "code", + "execution_count": 109, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.754912Z", + "start_time": "2024-06-08T08:17:19.743837Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 2., 3., 4.]) * mvolt" + ] + }, + "execution_count": 109, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.flatten()" + ] + }, + { + "cell_type": "code", + "execution_count": 110, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.867999Z", + "start_time": "2024-06-08T08:17:19.854522Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[1., 3.],\n", + " [2., 4.]]) * mvolt" + ] + }, + "execution_count": 110, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.swapaxes(0, 1)" + ] + }, + { + "cell_type": "code", + "execution_count": 111, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:19.923531Z", + "start_time": "2024-06-08T08:17:19.907391Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([1., 3.]) * mvolt" + ] + }, + "execution_count": 111, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.take(jnp.array([0, 2]))" + ] + }, + { + "cell_type": "code", + "execution_count": 112, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.036298Z", + "start_time": "2024-06-08T08:17:20.023150Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[1., 3.],\n", + " [2., 4.]]) * mvolt" + ] + }, + "execution_count": 112, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.transpose()" + ] + }, + { + "cell_type": "code", + "execution_count": 113, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.122048Z", + "start_time": "2024-06-08T08:17:20.109833Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[1., 2., 1., 2.],\n", + " [3., 4., 3., 4.]]) * mvolt" + ] + }, + "execution_count": 113, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.tile(2)" + ] + }, + { + "cell_type": "code", + "execution_count": 114, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.228759Z", + "start_time": "2024-06-08T08:17:20.213853Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[[1., 2.],\n", + " [3., 4.]]]) * mvolt" + ] + }, + "execution_count": 114, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.unsqueeze(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 115, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.294326Z", + "start_time": "2024-06-08T08:17:20.279806Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[[1., 2.],\n", + " [3., 4.]]]) * mvolt" + ] + }, + "execution_count": 115, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q.expand_dims(0)" + ] + }, + { + "cell_type": "code", + "execution_count": 116, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.410465Z", + "start_time": "2024-06-08T08:17:20.397348Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[[1., 2.],\n", + " [3., 4.]]]) * mvolt" + ] + }, + "execution_count": 116, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "expand_as_shape = (1, 2, 2)\n", + "q.expand_as(jnp.zeros(expand_as_shape).shape)" + ] + }, + { + "cell_type": "code", + "execution_count": 117, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.473612Z", + "start_time": "2024-06-08T08:17:20.454902Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[ 1., 30.],\n", + " [10., 4.]]) * mvolt" + ] + }, + "execution_count": 117, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q_put = [[1, 2], [3, 4]] * bu.mV\n", + "q_put.put([[1, 0], [0, 1]], [10, 30] * bu.mV)\n", + "q_put" + ] + }, + { + "cell_type": "code", + "execution_count": 118, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.586577Z", + "start_time": "2024-06-08T08:17:20.573505Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "ArrayImpl([[1., 2.],\n", + " [3., 4.]]) * mvolt" + ] + }, + "execution_count": 118, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q_squeeze = [[1, 2], [3, 4]] * bu.mV\n", + "q_squeeze.squeeze()" + ] + }, + { + "cell_type": "code", + "execution_count": 119, + "metadata": { + "ExecuteTime": { + "end_time": "2024-06-08T08:17:20.667592Z", + "start_time": "2024-06-08T08:17:20.652077Z" + } + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[ArrayImpl([[1., 2.]]) * mvolt, ArrayImpl([[3., 4.]]) * mvolt]" + ] + }, + "execution_count": 119, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "q_spilt = [[1, 2], [3, 4]] * bu.mV\n", + "q_spilt.split(2)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### Numpy Methods\n", + "All methods that make sense on quantities should work, i.e. they check for the correct units of their arguments and return quantities with units were appropriate.\n", + "\n", + "These methods defined at `braincore.math`, so you can use them by importing `import braincore.math as bm` and then using `bm.method_name`.\n", + "#### Functions that remove unit\n", + "- all\n", + "- any\n", + "- nonzero\n", + "- argmax\n", + "- argmin\n", + "- argsort\n", + "- ones_like\n", + "- zeros_like\n", + "\n", + "#### Functions that keep unit\n", + "- round\n", + "- std\n", + "- sum\n", + "- trace\n", + "- cumsum\n", + "- diagonal\n", + "- max\n", + "- mean\n", + "- min\n", + "- ptp\n", + "- ravel\n", + "- absolute\n", + "- rint\n", + "- negative\n", + "- positive\n", + "- conj\n", + "- conjugate\n", + "- floor\n", + "- ceil\n", + "- trunc\n", + "\n", + "#### Functions that change unit\n", + "- var\n", + "- multiply\n", + "- divide\n", + "- true_divide\n", + "- floor_divide\n", + "- dot\n", + "- matmul\n", + "- sqrt\n", + "- square\n", + "- reciprocal\n", + "\n", + "#### Functions that need to match unit\n", + "- add\n", + "- subtract\n", + "- maximum\n", + "- minimum\n", + "- remainder\n", + "- mod\n", + "- fmod\n", + "\n", + "#### Functions that only work with unitless quantities\n", + "- sin\n", + "- sinh\n", + "- arcsinh\n", + "- cos\n", + "- cosh\n", + "- arccos\n", + "- arccosh\n", + "- tan\n", + "- tanh\n", + "- arctan\n", + "- arctanh\n", + "- log\n", + "- log10\n", + "- exp\n", + "- expm1\n", + "- log1p\n", + "\n", + "#### Functions that compare quantities\n", + "- less\n", + "- less_equal\n", + "- greater\n", + "- greater_equal\n", + "- equal\n", + "- not_equal\n", + "\n", + "#### Functions that work on all quantities and return boolean arrays(Logical operations)\n", + "- logical_and\n", + "- logical_or\n", + "- logical_xor\n", + "- logical_not\n", + "- isreal\n", + "- iscomplex\n", + "- isfinite\n", + "- isinf\n", + "- isnan" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.5" + } + }, + "nbformat": 4, + "nbformat_minor": 4 +} diff --git a/docs/tutorials/physical_units.ipynb b/docs/tutorials/physical_units.ipynb index f6ebb36..88f3c83 100644 --- a/docs/tutorials/physical_units.ipynb +++ b/docs/tutorials/physical_units.ipynb @@ -4,3393 +4,134 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "# Physical Units" + "# Quantity" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Braincore includes a system for physical units. The base units are defined by their standard SI unit names:\n", - "`amp`/`ampere`, `kilogram`/`kilogramme`, `second`, `metre`/`meter`, `kilogram`, `mole`/`mol`, `kelvin`, and `candela`. In addition to these base units, braincore defines a set of derived units: `coulomb`, `farad`, `gram`/`gramme`, `hertz`, `joule`, `liter`/\n", - "`litre`, `molar`, `pascal`, `ohm`, `siemens`, `volt`, `watt`,\n", - "together with prefixed versions (e.g. `msiemens = 0.001*siemens`) using the\n", - "prefixes `p, n, u, m, k, M, G, T` (two exceptions to this rule: `kilogram`\n", - "is not defined with any additional prefixes, and `metre` and `meter` are\n", - "additionaly defined with the \"centi\" prefix, i.e. `cmetre`/`cmeter`).\n", - "For convenience, a couple of additional useful standard abbreviations such as\n", - "`cm` (instead of `cmetre`/`cmeter`), `nS` (instead of `nsiemens`),\n", - "`ms` (instead of `msecond`), `Hz` (instead of `hertz`), `mM`\n", - "(instead of `mmolar`) are included. To avoid clashes with common variable\n", - "names, no one-letter abbreviations are provided (e.g. you can use `mV` or\n", - "`nS`, but *not* `V` or `S`)." + "## Creating Quantity Instances" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Importing units\n", - "Braincore generates standard names for units, combining the unit name (e.g. “siemens”) with a prefixes (e.g. “m”), and also generates squared and cubed versions by appending a number. For example, the units “msiemens”, “siemens2”, “usiemens3” are all predefined. You can import these units from the package `brianunit` – accordingly, an `from brainunit import *` will result in everything being imported.\n", - "\n", - "We recommend importing only the units you need, to have a cleaner namespace. For example, `import brainunit as bu` and then using `bu.msiemens` instead of `msiemens`." - ] - }, - { - "cell_type": "code", - "execution_count": 1, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:18:06.720762Z", - "start_time": "2024-06-08T08:18:06.566328Z" - } - }, - "outputs": [], - "source": [ - "import brainunit as bu" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Using units\n", - "You can generate a physical quantity by multiplying a scalar or ndarray with its physical unit:" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "20. * msecond" - ] - }, - "execution_count": 2, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "tau = 20 * bu.ms\n", - "tau" - ] - }, - { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([10., 20., 30.], dtype=float32) * hertz" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "rates = [10, 20, 30] * bu.Hz\n", - "rates" - ] - }, - { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[10., 20., 30.],\n", - " [20., 30., 40.]], dtype=float32) * hertz" - ] - }, - "execution_count": 4, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "rates = [[10, 20, 30], [20, 30, 40]] * bu.Hz\n", - "rates" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "Braincore will check the consistency of operations on units and raise an error for dimensionality mismatches:" - ] - }, - { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Cannot calculate ... += 1, units do not match (units are s and 1).\n" - ] - } - ], - "source": [ - "try:\n", - " tau += 1 # ms? second?\n", - "except Exception as e:\n", - " print(e)" - ] - }, - { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Cannot calculate 3.0 + 3.0, units do not match (units are kg and A).\n" - ] - } - ], - "source": [ - "try:\n", - " 3 * bu.kgram + 3 * bu.amp\n", - "except Exception as e:\n", - " print(e)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Basics\n", - "Numpy functions have been overwritten to correctly work with units.\n", - "\n", - "The important attributes of a `Quantity` object are:\n", - "- `value`: the numerical value of the quantity\n", - "- `unit`: the unit of the quantity\n", - "- `ndim`: the number of dimensions of quantity's value\n", - "- `shape`: the shape of the quantity's value\n", - "- `size`: the size of the quantity's value\n", - "- `dtype`: the dtype of the quantity's value" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### An example" - ] - }, - { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[10., 20., 30.],\n", - " [20., 30., 40.]], dtype=float32) * hertz" - ] - }, - "execution_count": 7, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "rates" - ] - }, - { - "cell_type": "code", - "execution_count": 8, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Array([[10., 20., 30.],\n", - " [20., 30., 40.]], dtype=float32)" - ] - }, - "execution_count": 8, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "rates.value" - ] - }, - { - "cell_type": "code", - "execution_count": 9, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "second ** -1" - ] - }, - "execution_count": 9, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "rates.unit" - ] - }, - { - "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(2, (2, 3), 6, dtype('float32'))" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "rates.ndim, rates.shape, rates.size, rates.dtype" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### Quantity Creation\n", - "Creating a Quantity object can be accomplished in several ways, categorized based on the type of input used. Here, we present the methods grouped by their input types and characteristics for better clarity." - ] - }, - { - "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [], - "source": [ - "import jax.numpy as jnp\n", - "import brainstate as bst\n", - "bst.environ.set(precision=64) # we recommend using 64-bit precision for better numerical stability" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "\n", - "#### Scalar and Array Multiplication\n", - "- Multiplying a Scalar with a Unit\n" - ] - }, - { - "cell_type": "code", - "execution_count": 12, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5. * msecond" - ] - }, - "execution_count": 12, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "5 * bu.ms" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Multiplying a Jax nunmpy value type with a Unit:" - ] - }, - { - "cell_type": "code", - "execution_count": 13, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5. * msecond" - ] - }, - "execution_count": 13, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "jnp.float64(5) * bu.ms" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Multiplying a Jax numpy array with a Unit:" - ] - }, - { - "cell_type": "code", - "execution_count": 14, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 2., 3.]) * msecond" - ] - }, - "execution_count": 14, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "jnp.array([1, 2, 3]) * bu.ms" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Multiplying a List with a Unit:" - ] - }, - { - "cell_type": "code", - "execution_count": 15, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 2., 3.]) * msecond" - ] - }, - "execution_count": 15, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "[1, 2, 3] * bu.ms" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Direct Quantity Creation\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Creating a Quantity Directly with a Value" - ] - }, - { - "cell_type": "code", - "execution_count": 16, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Quantity(5.)" - ] - }, - "execution_count": 16, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity(5)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Creating a Quantity Directly with a Value and Unit" - ] - }, - { - "cell_type": "code", - "execution_count": 18, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "5. * msecond" - ] - }, - "execution_count": 18, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity(5, unit=bu.ms)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Creating a Quantity with a Jax numpy Array of Values and a Unit" - ] - }, - { - "cell_type": "code", - "execution_count": 19, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 2., 3.]) * msecond" - ] - }, - "execution_count": 19, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity(jnp.array([1, 2, 3]), unit=bu.ms)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Creating a Quantity with a List of Values and a Unit" - ] - }, - { - "cell_type": "code", - "execution_count": 20, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 2., 3.]) * msecond" - ] - }, - "execution_count": 20, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity([1, 2, 3], unit=bu.ms)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Creating a Quantity with a List of Quantities" - ] - }, - { - "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([0.5, 1. ]) * second" - ] - }, - "execution_count": 21, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity([500 * bu.ms, 1 * bu.second])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "- Using the with_units Method" - ] - }, - { - "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([0.5, 1. ]) * second" - ] - }, - "execution_count": 22, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity.with_units(jnp.array([0.5, 1]), second=1)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Unitless Quantity\n", - "Quantities can be unitless, which means they have no units. If there is no unit provided, the quantity is assumed to be unitless. The following are examples of creating unitless quantities:" - ] - }, - { - "cell_type": "code", - "execution_count": 23, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Quantity(ArrayImpl([1., 2., 3.]))" - ] - }, - "execution_count": 23, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity([1, 2, 3])" - ] - }, - { - "cell_type": "code", - "execution_count": 24, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Quantity(ArrayImpl([1., 2., 3.]))" - ] - }, - "execution_count": 24, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity(jnp.array([1, 2, 3]))" - ] - }, - { - "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "Quantity(ArrayImpl([], dtype=float64))" - ] - }, - "execution_count": 25, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "bu.Quantity([])" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "#### Illegal Quantity Creation\n", - "The following are examples of illegal quantity creation:" - ] - }, - { - "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "All elements must have the same unit\n" - ] - } - ], - "source": [ - "try:\n", - " bu.Quantity([500 * bu.ms, 1])\n", - "except Exception as e:\n", - " print(e)" - ] - }, - { - "cell_type": "code", - "execution_count": 27, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Value 'some' with dtype , >=) are supported:" - ] - }, - { - "cell_type": "code", - "execution_count": 95, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.350568Z", - "start_time": "2024-06-08T08:17:18.335369Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(ArrayImpl([10., 12., 14., 16., 18.]) * mvolt,\n", - " ArrayImpl([ 8., 12., 16., 20., 24.]) * mvolt)" - ] - }, - "execution_count": 95, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 = jnp.arange(10, 20, 2) * bu.mV\n", - "q2 = jnp.arange(8, 27, 4) * bu.mV\n", - "q1, q2" - ] - }, - { - "cell_type": "code", - "execution_count": 96, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.463131Z", - "start_time": "2024-06-08T08:17:18.448183Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(Array([False, True, False, False, False], dtype=bool),\n", - " Array([ True, False, True, True, True], dtype=bool))" - ] - }, - "execution_count": 96, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 == q2, q1 != q2" - ] - }, - { - "cell_type": "code", - "execution_count": 97, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.536630Z", - "start_time": "2024-06-08T08:17:18.523328Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(Array([False, False, True, True, True], dtype=bool),\n", - " Array([False, True, True, True, True], dtype=bool))" - ] - }, - "execution_count": 97, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 < q2, q1 <= q2" - ] - }, - { - "cell_type": "code", - "execution_count": 98, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.658605Z", - "start_time": "2024-06-08T08:17:18.644318Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(Array([ True, False, False, False, False], dtype=bool),\n", - " Array([ True, True, False, False, False], dtype=bool))" - ] - }, - "execution_count": 98, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 > q2, q1 >= q2" + "# Combining, Defining, and Displaying Units" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Binary Operations\n", - "The binary operations add (+), subtract (-), multiply (*), divide (/), floor divide (//), remainder (%), divmod (divmod), power (**), matmul (@), shift (<<, >>), round(round) are supported:" - ] - }, - { - "cell_type": "code", - "execution_count": 99, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.750090Z", - "start_time": "2024-06-08T08:17:18.733415Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(ArrayImpl([1., 2., 3.]) * mvolt, ArrayImpl([2., 3., 4.]) * mvolt)" - ] - }, - "execution_count": 99, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 = jnp.array([1, 2, 3]) * bu.mV\n", - "q2 = jnp.array([2, 3, 4]) * bu.mV\n", - "q1, q2" - ] - }, - { - "cell_type": "code", - "execution_count": 100, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.855866Z", - "start_time": "2024-06-08T08:17:18.840386Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(ArrayImpl([3., 5., 7.]) * mvolt, ArrayImpl([-1., -1., -1.]) * mvolt)" - ] - }, - "execution_count": 100, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 + q2, q1 - q2" - ] - }, - { - "cell_type": "code", - "execution_count": 101, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:18.941214Z", - "start_time": "2024-06-08T08:17:18.927264Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([ 2., 6., 12.]) * mvolt2" - ] - }, - "execution_count": 101, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 * q2" - ] - }, - { - "cell_type": "code", - "execution_count": 102, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.058063Z", - "start_time": "2024-06-08T08:17:19.045255Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(Array([0.5 , 0.66666667, 0.75 ], dtype=float64),\n", - " Array([0., 0., 0.], dtype=float64),\n", - " ArrayImpl([1., 2., 3.]) * mvolt)" - ] - }, - "execution_count": 102, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 / q2, q1 // q2, q1 % q2" - ] - }, - { - "cell_type": "code", - "execution_count": 103, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.137905Z", - "start_time": "2024-06-08T08:17:19.125338Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(Array([0., 0., 0.], dtype=float64), ArrayImpl([1., 2., 3.]) * mvolt)" - ] - }, - "execution_count": 103, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "divmod(q1, q2)" - ] - }, - { - "cell_type": "code", - "execution_count": 104, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.289789Z", - "start_time": "2024-06-08T08:17:19.275896Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 4., 9.]) * mvolt2" - ] - }, - "execution_count": 104, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 ** 2" - ] - }, - { - "cell_type": "code", - "execution_count": 105, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.347106Z", - "start_time": "2024-06-08T08:17:19.334827Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "20. * mvolt2" - ] - }, - "execution_count": 105, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 @ q2" - ] - }, - { - "cell_type": "code", - "execution_count": 106, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.472101Z", - "start_time": "2024-06-08T08:17:19.456793Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "(ArrayImpl([ 0., 4., 8., 12., 16.]) * volt,\n", - " ArrayImpl([0., 0., 0., 0., 1.]) * volt)" - ] - }, - "execution_count": 106, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q1 = bu.Quantity(jnp.arange(5, dtype=jnp.int32), unit=bu.mV.unit)\n", - "q1 << 2, q1 >> 2" - ] - }, - { - "cell_type": "code", - "execution_count": 107, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.531264Z", - "start_time": "2024-06-08T08:17:19.514038Z" - } - }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "round(80.23456, 2) : 80.23 mV\n", - "round(100.000056, 3) : 100. mV\n", - "round(-100.000056, 3) : -100. mV\n" - ] - } - ], - "source": [ - "q1 = 80.23456 * bu.mV\n", - "q2 = 100.000056 * bu.mV\n", - "q3 = -100.000056 * bu.mV\n", - "print(\"round(80.23456, 2) : \", q1.round(5))\n", - "print(\"round(100.000056, 3) : \", q2.round(6))\n", - "print(\"round(-100.000056, 3) : \", q3.round(6))" + "## Basic example" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Shape Manipulation\n", - "The shape of an array can be changed with various commands. Note that the following three commands all return a modified array, but do not change the original array:" - ] - }, - { - "cell_type": "code", - "execution_count": 108, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.688835Z", - "start_time": "2024-06-08T08:17:19.670457Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[1., 2.],\n", - " [3., 4.]]) * mvolt" - ] - }, - "execution_count": 108, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q = [[1, 2], [3, 4]] * bu.mV\n", - "q" - ] - }, - { - "cell_type": "code", - "execution_count": 109, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.754912Z", - "start_time": "2024-06-08T08:17:19.743837Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 2., 3., 4.]) * mvolt" - ] - }, - "execution_count": 109, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.flatten()" - ] - }, - { - "cell_type": "code", - "execution_count": 110, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.867999Z", - "start_time": "2024-06-08T08:17:19.854522Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[1., 3.],\n", - " [2., 4.]]) * mvolt" - ] - }, - "execution_count": 110, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.swapaxes(0, 1)" - ] - }, - { - "cell_type": "code", - "execution_count": 111, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:19.923531Z", - "start_time": "2024-06-08T08:17:19.907391Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([1., 3.]) * mvolt" - ] - }, - "execution_count": 111, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.take(jnp.array([0, 2]))" - ] - }, - { - "cell_type": "code", - "execution_count": 112, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.036298Z", - "start_time": "2024-06-08T08:17:20.023150Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[1., 3.],\n", - " [2., 4.]]) * mvolt" - ] - }, - "execution_count": 112, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.transpose()" - ] - }, - { - "cell_type": "code", - "execution_count": 113, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.122048Z", - "start_time": "2024-06-08T08:17:20.109833Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[1., 2., 1., 2.],\n", - " [3., 4., 3., 4.]]) * mvolt" - ] - }, - "execution_count": 113, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.tile(2)" - ] - }, - { - "cell_type": "code", - "execution_count": 114, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.228759Z", - "start_time": "2024-06-08T08:17:20.213853Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[[1., 2.],\n", - " [3., 4.]]]) * mvolt" - ] - }, - "execution_count": 114, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.unsqueeze(0)" - ] - }, - { - "cell_type": "code", - "execution_count": 115, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.294326Z", - "start_time": "2024-06-08T08:17:20.279806Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[[1., 2.],\n", - " [3., 4.]]]) * mvolt" - ] - }, - "execution_count": 115, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q.expand_dims(0)" - ] - }, - { - "cell_type": "code", - "execution_count": 116, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.410465Z", - "start_time": "2024-06-08T08:17:20.397348Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[[1., 2.],\n", - " [3., 4.]]]) * mvolt" - ] - }, - "execution_count": 116, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "expand_as_shape = (1, 2, 2)\n", - "q.expand_as(jnp.zeros(expand_as_shape).shape)" - ] - }, - { - "cell_type": "code", - "execution_count": 117, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.473612Z", - "start_time": "2024-06-08T08:17:20.454902Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[ 1., 30.],\n", - " [10., 4.]]) * mvolt" - ] - }, - "execution_count": 117, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q_put = [[1, 2], [3, 4]] * bu.mV\n", - "q_put.put([[1, 0], [0, 1]], [10, 30] * bu.mV)\n", - "q_put" - ] - }, - { - "cell_type": "code", - "execution_count": 118, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.586577Z", - "start_time": "2024-06-08T08:17:20.573505Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "ArrayImpl([[1., 2.],\n", - " [3., 4.]]) * mvolt" - ] - }, - "execution_count": 118, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q_squeeze = [[1, 2], [3, 4]] * bu.mV\n", - "q_squeeze.squeeze()" - ] - }, - { - "cell_type": "code", - "execution_count": 119, - "metadata": { - "ExecuteTime": { - "end_time": "2024-06-08T08:17:20.667592Z", - "start_time": "2024-06-08T08:17:20.652077Z" - } - }, - "outputs": [ - { - "data": { - "text/plain": [ - "[ArrayImpl([[1., 2.]]) * mvolt, ArrayImpl([[3., 4.]]) * mvolt]" - ] - }, - "execution_count": 119, - "metadata": {}, - "output_type": "execute_result" - } - ], - "source": [ - "q_spilt = [[1, 2], [3, 4]] * bu.mV\n", - "q_spilt.split(2)" + "## Defining units" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "### Numpy Methods\n", - "All methods that make sense on quantities should work, i.e. they check for the correct units of their arguments and return quantities with units were appropriate.\n", - "\n", - "These methods defined at `braincore.math`, so you can use them by importing `import braincore.math as bm` and then using `bm.method_name`.\n", - "#### Functions that remove unit\n", - "- all\n", - "- any\n", - "- nonzero\n", - "- argmax\n", - "- argmin\n", - "- argsort\n", - "- ones_like\n", - "- zeros_like\n", - "\n", - "#### Functions that keep unit\n", - "- round\n", - "- std\n", - "- sum\n", - "- trace\n", - "- cumsum\n", - "- diagonal\n", - "- max\n", - "- mean\n", - "- min\n", - "- ptp\n", - "- ravel\n", - "- absolute\n", - "- rint\n", - "- negative\n", - "- positive\n", - "- conj\n", - "- conjugate\n", - "- floor\n", - "- ceil\n", - "- trunc\n", - "\n", - "#### Functions that change unit\n", - "- var\n", - "- multiply\n", - "- divide\n", - "- true_divide\n", - "- floor_divide\n", - "- dot\n", - "- matmul\n", - "- sqrt\n", - "- square\n", - "- reciprocal\n", - "\n", - "#### Functions that need to match unit\n", - "- add\n", - "- subtract\n", - "- maximum\n", - "- minimum\n", - "- remainder\n", - "- mod\n", - "- fmod\n", - "\n", - "#### Functions that only work with unitless quantities\n", - "- sin\n", - "- sinh\n", - "- arcsinh\n", - "- cos\n", - "- cosh\n", - "- arccos\n", - "- arccosh\n", - "- tan\n", - "- tanh\n", - "- arctan\n", - "- arctanh\n", - "- log\n", - "- log10\n", - "- exp\n", - "- expm1\n", - "- log1p\n", - "\n", - "#### Functions that compare quantities\n", - "- less\n", - "- less_equal\n", - "- greater\n", - "- greater_equal\n", - "- equal\n", - "- not_equal\n", - "\n", - "#### Functions that work on all quantities and return boolean arrays(Logical operations)\n", - "- logical_and\n", - "- logical_or\n", - "- logical_xor\n", - "- logical_not\n", - "- isreal\n", - "- iscomplex\n", - "- isfinite\n", - "- isinf\n", - "- isnan" + "## Displaying in JIT / grad / ... transformations" ] } ], "metadata": { - "kernelspec": { - "display_name": "Python 3 (ipykernel)", - "language": "python", - "name": "python3" - }, "language_info": { - "codemirror_mode": { - "name": "ipython", - "version": 3 - }, - "file_extension": ".py", - "mimetype": "text/x-python", - "name": "python", - "nbconvert_exporter": "python", - "pygments_lexer": "ipython3", - "version": "3.11.5" + "name": "python" } }, "nbformat": 4, - "nbformat_minor": 4 + "nbformat_minor": 2 }