{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Computation\n", "\n", "## General concepts and mechanisms\n", "\n", "### Overview\n", "\n", "Binary operations between data arrays or datasets behave as follows:\n", "\n", "| Property | Action |\n", "| --- | --- |\n", "| coord | compare, abort on mismatch |\n", "| data | apply operation |\n", "| mask | combine with `or` |\n", "\n", "### Dimension matching and transposing\n", "\n", "Operations \"align\" variables based on their dimension labels.\n", "That is, an operation between two variables that have a transposed memory layout behave correctly:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "import scipp as sc\n", "\n", "rng = np.random.default_rng(12345)\n", "a = sc.array(\n", " dims=['x', 'y'], values=rng.random((2, 4)), variances=rng.random((2, 4)), unit='m'\n", ")\n", "b = sc.array(\n", " dims=['y', 'x'], values=rng.random((4, 2)), variances=rng.random((4, 2)), unit='s'\n", ")\n", "a / b" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Propagation of uncertainties\n", "\n", "If variables have variances, operations correctly propagate uncertainties (the variances), in contrast to a naive implementation using NumPy:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result = a / b\n", "result.values" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.values / np.transpose(b.values)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "result.variances" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a.variances / np.transpose(b.variances)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The implementation assumes uncorrelated data and is otherwise based on, e.g., [Wikipedia: Propagation of uncertainty](https://en.wikipedia.org/wiki/Propagation_of_uncertainty#Example_formulae).\n", "See also [Propagation of uncertainties](../reference/error-propagation.rst) for the concrete equations used for error propagation." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "