{ "cells": [ { "cell_type": "markdown", "id": "6537d267", "metadata": {}, "source": [ "" ] }, { "cell_type": "raw", "id": "a7a5972c", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "########\n", "Examples\n", "########\n", "\n", "Here we show a selection of examples of using cweqgen.\n", "\n", "Generating an equation\n", "======================\n", "\n", "To generate an equation you just need the correct name for the equation, as given in the\n", ":ref:`Equations` section, and the :func:`~cweqgen.equations.equations` function. To generate\n", "the equation for the gravitational-wave amplitude :math:`h_0` you would do:" ] }, { "cell_type": "code", "execution_count": 1, "id": "938f7cbe", "metadata": {}, "outputs": [], "source": [ "from cweqgen import equations\n", "eq = equations(\"h0\")" ] }, { "cell_type": "raw", "id": "47d276f7", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If you print the returned :class:`~cweqgen.equations.EquationBase` object (called ``eq`` in this\n", "case) it will return a LaTeX string, via the :meth:`~cweqgen.equations.EquationBase.equation`\n", "method, giving the equation (note that the equation is not enclosed in \"$\" symbols):" ] }, { "cell_type": "code", "execution_count": 2, "id": "d6b05def", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\")\n", "h_0 = \\frac{16 G}{c^{4}} \\pi^{2}\\frac{\\varepsilon I_{zz}}{d} f_{\\rm rot}^{2}\n" ] } ], "source": [ "print(eq)" ] }, { "cell_type": "raw", "id": "7cb6d505", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If working in a Jupyter notebook, you can show the typeset LaTeX equation by just running a cell\n", "containing ``eq``:" ] }, { "cell_type": "code", "execution_count": 3, "id": "65896ce2", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\")\n", "h_0 = \\frac{16 G}{c^{4}} \\pi^{2}\\frac{\\varepsilon I_{zz}}{d} f_{\\rm rot}^{2}$" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\")\n", "h_0 = \\frac{16 G}{c^{4}} \\pi^{2}\\frac{\\varepsilon I_{zz}}{d} f_{\\rm rot}^{2}" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq" ] }, { "cell_type": "raw", "id": "6edb51e4", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Equation as a figure\n", "--------------------\n", "\n", "You can return an equation as an object containing a :class:`matplotlib.figure.Figure`, which can then be saved in whatever format you require by using the :meth:`~cweqgen.equations.EquationBase.equation` method with the ``displaytype`` keyword set to ``\"matplotlib\"``. If running this in a Jupyter notebook, a png version of the equation will be shown." ] }, { "cell_type": "code", "execution_count": 4, "id": "71062a47", "metadata": {}, "outputs": [ { "data": { "image/png": "iVBORw0KGgoAAAANSUhEUgAAARoAAABKCAYAAABkSpv5AAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAB7CAAAewgFu0HU+AAAK1ElEQVR4nO2d8XXiuhLGv3nnFeDN64DtgGUruNBB2K3gJh2EsxXcw3bApoK9pAOyFdwlHYQObkIH8/7QCCvGBhuQLcj3O4dDYstYlqXPo9FIFlUFIYTE5D9dZ4AQcvlQaAgh0aHQEEKiQ6EhhESHQkMIiQ6FhhASHQoNISQ6FBpCSHQoNISQ6FBoCCHRodAQQqJDoSGERIdCQwiJDoWGEBIdCg0hJDoUGkJIdCg0hJDoUGgIIdGh0BBCokOheeeIyLWIzEVkKSIL+8xEJLP9UxHpd5zNk2HXuxCRV/ss7fp7XeftkhEuTp4u1tjnAOaq+mNP2hsAIwAvtulZVb/vSH8NYArgCcBEVVfBvh6AGYAVgBtVlWOuI0VE5BlAD8AHVV13nJ2L579dZ4BsIyIzAFf27xDAYkfaDE6MnlR1bNt6AJYi8qSqjyXHTAH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QEp3/A6+AFllNZ25uAAAAAElFTkSuQmCC\n", "text/plain": [ "
" ] }, "metadata": { "needs_background": "light" }, "output_type": "display_data" } ], "source": [ "fig = eq.equation(displaytype=\"matplotlib\")\n", "fig.savefig(\"myequation.pdf\") # save a pdf version of the equation" ] }, { "cell_type": "raw", "id": "b998902b", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Equation with fiducial values\n", "-----------------------------\n", "\n", "Each equation is defined with a set of \"`fiducial `_\" values. A LaTeX string containing a version of the equation evaluated at the fiducial values can be created using the :meth:`~cweqgen.equations.EquationBase.fiducial_equation` method:" ] }, { "cell_type": "code", "execution_count": 5, "id": "fb80f296", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\")\n", "h_0 = 4.23 \\times 10^{-26} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-6} \\; \\mathrm{}}\\right) \\left(\\frac{1 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} \n" ] } ], "source": [ "print(eq.fiducial_equation())" ] }, { "cell_type": "raw", "id": "6495122e", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If running this in a Jupyter notebook, it can display the typeset LaTeX equation:" ] }, { "cell_type": "code", "execution_count": 6, "id": "402959ef", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0 = 4.23 \\times 10^{-26} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-6} \\; \\mathrm{}}\\right) \\left(\\frac{1 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} $" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\")\n", "h_0 = 4.23 \\times 10^{-26} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-6} \\; \\mathrm{}}\\right) \\left(\\frac{1 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} " ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq.fiducial_equation()" ] }, { "cell_type": "raw", "id": "01231901", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "The :meth:`~cweqgen.equations.EquationBase.fiducial_equation` method can also take the ``displaytype=\"matplotlib\"`` keyword argument to return a Matplotlib :class:`~matplotlib.figure.Figure` containing the equation." ] }, { "cell_type": "raw", "id": "f4c73a78", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Setting fiducial values\n", "^^^^^^^^^^^^^^^^^^^^^^^\n", "\n", "You can generate the equation with different fiducial values. You can either do this when creating the :class:`~cweqgen.equations.EquationBase` object by passing :func:`~cweqgen.equations.equations` your own values, e.g.: " ] }, { "cell_type": "code", "execution_count": 7, "id": "9582fbb4", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0 = 1.69 \\times 10^{-27} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-7} \\; \\mathrm{}}\\right) \\left(\\frac{2.5 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} $" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\", ellipticity=1e-07, distance=2.5)\n", "h_0 = 1.69 \\times 10^{-27} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-7} \\; \\mathrm{}}\\right) \\left(\\frac{2.5 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} " ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq = equations(\"h0\", ellipticity=1e-7, distance=2.5)\n", "eq.fiducial_equation()" ] }, { "cell_type": "raw", "id": "bcdc8780", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "or do it through :meth:`~cweqgen.equations.EquationBase.fiducial_equation`:" ] }, { "cell_type": "code", "execution_count": 8, "id": "5c4afda7", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0 = 1.35 \\times 10^{-26} \\; \\mathrm{}\\left(\\frac{I_{zz}}{2 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-7} \\; \\mathrm{}}\\right) \\left(\\frac{2.5 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{200 \\; \\mathrm{Hz}}\\right)^{2} $" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\", ellipticity=1e-07, distance=2.5)\n", "h_0 = 1.35 \\times 10^{-26} \\; \\mathrm{}\\left(\\frac{I_{zz}}{2 \\times 10^{38} \\; \\mathrm{m^{2}\\,kg}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-7} \\; \\mathrm{}}\\right) \\left(\\frac{2.5 \\; \\mathrm{kpc}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{200 \\; \\mathrm{Hz}}\\right)^{2} " ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq.fiducial_equation(momentofinertia=2e38, rotationfrequency=200)" ] }, { "cell_type": "raw", "id": "cc743e0b", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If you pass fiducial values as dimensionless values the default units from the equation definitons will be assumed. However, you can pass values with astropy :class:`~astropy.units.Unit` types and these will get correctly intepreted. For example, if you wanted to have the fiducial distance of 1000 light years and the principal moment of inertia in `cgs units `_, you could use: " ] }, { "cell_type": "code", "execution_count": 9, "id": "d6fb9bdb", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0 = 1.38 \\times 10^{-25} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{45} \\; \\mathrm{cm^{2}\\,g}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-6} \\; \\mathrm{}}\\right) \\left(\\frac{1 \\times 10^{3} \\; \\mathrm{lyr}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} $" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\", distance=1000.0 lyr, momentofinertia=1e+45 cm2 g)\n", "h_0 = 1.38 \\times 10^{-25} \\; \\mathrm{}\\left(\\frac{I_{zz}}{1 \\times 10^{45} \\; \\mathrm{cm^{2}\\,g}}\\right) \\left(\\frac{\\varepsilon}{1 \\times 10^{-6} \\; \\mathrm{}}\\right) \\left(\\frac{1 \\times 10^{3} \\; \\mathrm{lyr}}{d}\\right) \\left(\\frac{f_{\\rm rot}}{100 \\; \\mathrm{Hz}}\\right)^{2} " ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from astropy.units import Unit\n", "eq = equations(\"h0\", distance=1000 * Unit(\"lyr\"), momentofinertia=1e45 * Unit(\"g cm^2\"))\n", "eq.fiducial_equation()" ] }, { "cell_type": "raw", "id": "9e311f99", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "The keywords for providing the fiducial values can be from a range of aliases given in :obj:`cweqgen.definitions.ALLOWED_VARIABLES`.\n", "\n", "Evaluating the equation\n", "-----------------------\n", "\n", "The :class:`~cweqgen.equations.EquationBase` class does not only provide ways to output LaTeX strings, but can also be used to evaluate the equation at given values. To can be done using the :meth:`~cweqgen.equations.EquationBase.evaluate` method. If no values are provided to :meth:`~cweqgen.equations.EquationBase.evaluate` it will return the equation as evaluated at the default fiducial values (or those provided when initialising the equation), e.g.:" ] }, { "cell_type": "code", "execution_count": 10, "id": "81d1fccd", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$4.2285561 \\times 10^{-26} \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq = equations(\"h0\")\n", "eq.evaluate()" ] }, { "cell_type": "raw", "id": "8c86c195", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "The :class:`~cweqgen.equations.EquationBase` actually has a `__call__ method `_ defined allowing you to use :class:`~cweqgen.equations.EquationBase` objects as functions by running the :meth:`~cweqgen.equations.EquationBase.evaluate` method, e.g.," ] }, { "cell_type": "code", "execution_count": 11, "id": "24783d1f", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$4.2285561 \\times 10^{-26} \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq()" ] }, { "cell_type": "raw", "id": "d06a5890", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "You can pass values for any of the variables in the equation to calculate it at those values (any variable not provided will still assume the fiducial values). Values can have astropy :class:`~astropy.units.Unit` types, but if not the default units will be assumed. You can also pass arrays of values, e.g.:" ] }, { "cell_type": "code", "execution_count": 12, "id": "6cc95c41", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$[4.2285561 \\times 10^{-26},~2.114278 \\times 10^{-26},~1.4095187 \\times 10^{-26}] \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq.evaluate(distance=[1.0, 2.0, 3.0] * Unit(\"kpc\"))" ] }, { "cell_type": "raw", "id": "043d79d6", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If you pass equal length arrays then the output will be the same length as the inputs (i.e., the equation is evaluate by each index in the arrays):" ] }, { "cell_type": "code", "execution_count": 13, "id": "688f3fb1", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$[1.057139 \\times 10^{-26},~2.114278 \\times 10^{-26},~3.1714171 \\times 10^{-26}] \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq.evaluate(distance=[1.0, 2.0, 3.0] * Unit(\"kpc\"), rotationfrequency=[50, 100, 150] * Unit(\"Hz\"))" ] }, { "cell_type": "raw", "id": "3101d218", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "However, if you require values on a `mesh grid `_ of values, you can add the ``mesh=True`` keyword argument:" ] }, { "cell_type": "code", "execution_count": 14, "id": "a9228b5b", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$[[1.057139 \\times 10^{-26},~5.2856951 \\times 10^{-27},~3.5237967 \\times 10^{-27}],~\n", " [4.2285561 \\times 10^{-26},~2.114278 \\times 10^{-26},~1.4095187 \\times 10^{-26}],~\n", " [9.5142512 \\times 10^{-26},~4.7571256 \\times 10^{-26},~3.1714171 \\times 10^{-26}]] \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq.evaluate(distance=[1.0, 2.0, 3.0] * Unit(\"kpc\"), rotationfrequency=[50, 100, 150] * Unit(\"Hz\"), mesh=True)" ] }, { "cell_type": "raw", "id": "6eb3cdfc", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "If arrays of different lengths are passed to :meth:`~cweqgen.equations.EquationBase.evaluate` then it will automatically perform the evaluation on a mesh grid." ] }, { "cell_type": "code", "execution_count": 15, "id": "910d2e82", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$[[1.057139 \\times 10^{-26},~1.057139 \\times 10^{-27}],~\n", " [4.2285561 \\times 10^{-26},~4.2285561 \\times 10^{-27}],~\n", " [9.5142512 \\times 10^{-26},~9.5142512 \\times 10^{-27}]] \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq.evaluate(ellipticity=[1e-6, 1e-7], rotationfrequency=[50, 100, 150] * Unit(\"Hz\"), mesh=True)" ] }, { "cell_type": "raw", "id": "308e1d26", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Rearranging an equation\n", "-----------------------\n", "\n", "You can rearrange an equation to switch the value on the left hand side with one of the other variables. This uses the :meth:`~cweqgen.equations.EquationBase.rearrange` method, which returns a new :class:`~cweqgen.equations.EquationBase` (the original :class:`~cweqgen.equations.EquationBase` will not be changed):" ] }, { "cell_type": "code", "execution_count": 16, "id": "6a6bf7d8", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\dot{f}_{\\rm rot} = \\frac{1}{n^{1/2}} \\left(\\ddot{f}_{\\rm rot} f_{\\rm rot}\\right)^{1/2}$" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"brakingindex\")\n", "\\dot{f}_{\\rm rot} = \\frac{1}{n^{1/2}} \\left(\\ddot{f}_{\\rm rot} f_{\\rm rot}\\right)^{1/2}" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# equation for the braking index\n", "eq = equations(\"brakingindex\")\n", "\n", "# rearrange to put frequency derivative on the lhs\n", "req = eq.rearrange(\"rotationfdot\")\n", "req.equation()" ] }, { "cell_type": "raw", "id": "6f40d11e", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "The fiducial values for the old right hand side variable will be set from the value evaluated at the fiducial values from the original equation. However, a new fiducial value can be set by passing it to :meth:`~cweqgen.equations.EquationBase.rearrange`, e.g.:" ] }, { "cell_type": "code", "execution_count": 17, "id": "692941f5", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\dot{f}_{\\rm rot} = 1.12 \\times 10^{-11} \\; \\mathrm{\\frac{Hz}{s}}\\left(\\frac{4 \\; \\mathrm{}}{n}\\right)^{1/2} \\left(\\frac{\\ddot{f}_{\\rm rot}}{1 \\times 10^{-23} \\; \\mathrm{\\frac{Hz}{s^{2}}}}\\right)^{1/2} \\left(\\frac{f_{\\rm rot}}{50 \\; \\mathrm{Hz}}\\right)^{1/2} $" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"brakingindex\")\n", "\\dot{f}_{\\rm rot} = 1.12 \\times 10^{-11} \\; \\mathrm{\\frac{Hz}{s}}\\left(\\frac{4 \\; \\mathrm{}}{n}\\right)^{1/2} \\left(\\frac{\\ddot{f}_{\\rm rot}}{1 \\times 10^{-23} \\; \\mathrm{\\frac{Hz}{s^{2}}}}\\right)^{1/2} \\left(\\frac{f_{\\rm rot}}{50 \\; \\mathrm{Hz}}\\right)^{1/2} " ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# set fiducial value for the braking index of 4!\n", "req = eq.rearrange(\"rotationfdot\", 4)\n", "req.fiducial_equation()" ] }, { "cell_type": "raw", "id": "0d6ec5b9", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Substituting an equation\n", "------------------------\n", "\n", "It is possible to substitute one equation into another using the :meth:`~cweqgen.equations.EquationBase.substitute` method. If we take the above rearranged equation giving rotation frequency derivative in terms of braking index, rotation frequency and frequency second derivative, it can be substituted into the equation for the gravitational-wave amplitude spin-down limit:" ] }, { "cell_type": "code", "execution_count": 18, "id": "cbfed15d", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0^{\\rm sd} = \\frac{\\sqrt{10} \\sqrt{G}}{2 c^{3/2}}\\frac{I_{zz}^{1/2} \\left|{\\ddot{f}_{\\rm rot}}\\right|^{1/4}}{d f_{\\rm rot}^{1/4} \\left|{n}\\right|^{1/4}}$" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0spindown\")\n", "h_0^{\\rm sd} = \\frac{\\sqrt{10} \\sqrt{G}}{2 c^{3/2}}\\frac{I_{zz}^{1/2} \\left|{\\ddot{f}_{\\rm rot}}\\right|^{1/4}}{d f_{\\rm rot}^{1/4} \\left|{n}\\right|^{1/4}}" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from cweqgen import equations\n", "\n", "# equation for the braking index\n", "eq = equations(\"brakingindex\")\n", "\n", "# rearrange to put frequency derivative on the lhs\n", "req = eq.rearrange(\"rotationfdot\")\n", "\n", "eqsd = equations(\"h0spindown\")\n", "subeq = eqsd.substitute(req) # substitute in req\n", "subeq.equation()" ] }, { "cell_type": "raw", "id": "66bef9bf", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Another useful example is getting the gravitational-wave amplitude in terms of the mass quadrupole :math:`Q_{22}`. This can be achieved with:" ] }, { "cell_type": "code", "execution_count": 19, "id": "d44be20e", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0 = \\frac{32 \\sqrt{30} \\pi^{5/2} G}{15 c^{4}}\\frac{Q_{22}}{d} f_{\\rm rot}^{2}$" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0\")\n", "h_0 = \\frac{32 \\sqrt{30} \\pi^{5/2} G}{15 c^{4}}\\frac{Q_{22}}{d} f_{\\rm rot}^{2}" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# equation for gravitational-wave amplitude\n", "eqh0 = equations(\"h0\")\n", "\n", "# equation for mass quadrupole (in terms of ellipticity and moment of inertia)\n", "eqq22 = equations(\"massquadrupole\")\n", "\n", "# rearrange and substitute\n", "eqh0q22 = eqh0.substitute(eqq22.rearrange(\"ellipticity\"))\n", "eqh0q22.equation()" ] }, { "cell_type": "raw", "id": "be7bbcf6", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Equivalent variables\n", "--------------------\n", "\n", "Some equations allow you to pass in variables that are equivalent (bar some conversion) to the required variables, e.g., using rotation period when rotation frequency is required:" ] }, { "cell_type": "code", "execution_count": 20, "id": "f39981f1", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$\\varepsilon^{\\rm sd} = \\frac{\\sqrt{10} c^{5/2}}{32 \\pi^{2} \\sqrt{G}}\\frac{\\left|{\\dot{f}_{\\rm rot}}\\right|^{1/2}}{I_{zz}^{1/2} f_{\\rm rot}^{5/2}}$" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"ellipticityspindown\")\n", "\\varepsilon^{\\rm sd} = \\frac{\\sqrt{10} c^{5/2}}{32 \\pi^{2} \\sqrt{G}}\\frac{\\left|{\\dot{f}_{\\rm rot}}\\right|^{1/2}}{I_{zz}^{1/2} f_{\\rm rot}^{5/2}}" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq = equations(\"ellipticityspindown\")\n", "eq.equation()" ] }, { "cell_type": "code", "execution_count": 21, "id": "74db8855", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$0.00033715079 \\; \\mathrm{}$" ], "text/plain": [ "" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# evaluate using rotation period rather than rotation frequency\n", "eq.evaluate(rotationperiod=0.05 * Unit(\"s\"))" ] }, { "cell_type": "raw", "id": "b57fe824", "metadata": { "raw_mimetype": "text/restructuredtext" }, "source": [ "Converting between frequencies\n", "------------------------------\n", "\n", "If an equation contains a frequency parameter (or equivalently a rotation period) and/or its first derivative you can convert it to another frequency parameter using the :meth:`~cweqgen.equations.EquationBase.to` method. For example, an equation containing rotation frequency can be converted to one with rotation period:" ] }, { "cell_type": "code", "execution_count": 22, "id": "b9435639", "metadata": {}, "outputs": [ { "data": { "text/latex": [ "$h_0^{\\rm sd} = \\frac{\\sqrt{10} \\sqrt{G}}{2 c^{3/2}}\\frac{I_{zz}^{1/2} \\left|{\\dot{P}}\\right|^{1/2}}{d P^{1/2}}$" ], "text/plain": [ "% equation generated with cweqgen v0.4.2.dev2+gb08a31c.d20220426:\n", "% equations(equation=\"h0spindown\")\n", "h_0^{\\rm sd} = \\frac{\\sqrt{10} \\sqrt{G}}{2 c^{3/2}}\\frac{I_{zz}^{1/2} \\left|{\\dot{P}}\\right|^{1/2}}{d P^{1/2}}" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "eq = equations(\"h0spindown\")\n", "\n", "neweq = eq.to(\"rotationperiod\")\n", "neweq.eqn" ] } ], "metadata": { "celltoolbar": "Raw Cell Format", "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.8.12" } }, "nbformat": 4, "nbformat_minor": 5 }