{ "cells": [ { "cell_type": "markdown", "id": "backends-0", "metadata": { "tags": [ "readme" ] }, "source": [ "# Compare NumPy and Rust\n", "\n", "Choose backend namespaces explicitly without changing global state. This notebook requires the compiled Rust extension and checks both fields and gradients at exterior points.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "backends-1", "metadata": { "tags": [ "readme" ] }, "outputs": [], "source": [ "import numpy as np\n", "\n", "from microcubed import get_backend\n", "\n", "points = np.array([[0, 0, -150], [80, 30, -120], [-90, 50, 160]]).T\n", "results = {}\n", "for name in (\"numpy\", \"rust\"):\n", " backend = get_backend(name)\n", " magnet = backend.Magnet([100, 80, 40], [0, 0, 0], [2e5, 1e5, 8e5])\n", " results[name] = (magnet.Bfield(points), magnet.dBfield(points))\n", "\n", "for reference, compiled in zip(results[\"numpy\"], results[\"rust\"]):\n", " np.testing.assert_allclose(compiled, reference, rtol=1e-9, atol=1e-13)\n", "print(\"Fields and gradients agree.\")" ] }, { "cell_type": "markdown", "id": "backends-2", "metadata": {}, "source": [ "## Verify the result\n", "\n", "These assertions also run in GitHub Actions.\n" ] }, { "cell_type": "code", "execution_count": null, "id": "backends-3", "metadata": {}, "outputs": [], "source": [ "assert all(np.isfinite(value).all() for pair in results.values() for value in pair)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 5 }