Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add better interaction with and export to sympy, and uses sympy to implement more Hessian functions #191

Merged
merged 18 commits into from Oct 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/windows-tests.yml
Expand Up @@ -11,7 +11,7 @@ on:
jobs:
build:
name: "py${{ matrix.python-version }} on ${{ matrix.os }}"
if: "!contains(github.event.head_commit.message, 'skip ci')"
if: "!contains(toJSON(github.event.commits.*.message), '[skip ci]') && !contains(toJSON(github.event.commits.*.message), '[ci skip]')"
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
Expand Down
8 changes: 8 additions & 0 deletions CHANGES.rst
Expand Up @@ -4,13 +4,21 @@
New Features
------------

- Added a new ``.to_sympy()`` classmethod for the ``Potential`` classes to
return a sympy expression and variables.

Bug fixes
---------

- Fixed a bug with ``Potential`` classes ``.replace_units()`` so that classes
with dimensionless unit systems cannot be replaced with physical unit systems,
and vice versa.

- Implemented Hessian functions for most potentials.

- Fixed ``.to_latex()`` to properly return a latex representation of the
potential. This uses the new ``.to_sympy()`` method under the hood.

API changes
-----------

Expand Down
34 changes: 34 additions & 0 deletions docs/potential/index.rst
Expand Up @@ -237,6 +237,40 @@ and method::
>>> load("potential.yml")
<NFWPotential: m=6.00e+11, r_s=20.00, a=1.00, b=1.00, c=1.00 (kpc,Myr,solMass,rad)>

Exporting potentials as ``sympy`` expressions
=============================================

Most of the potential classes can be exported to a ``sympy`` expression that can
be used to manipulate or evaluate the form of the potential. To access this
functionality, the potential classes have a ``.to_sympy()`` classmethod (note:
this requires that ``sympy`` is installed):

>>> expr, vars_, pars = gp.LogarithmicPotential.to_sympy()
>>> str(expr)
'0.5*v_c**2*log(r_h**2 + z**2/q3**2 + y**2/q2**2 + x**2/q1**2)'

This method also returns a dictionary containing the coordinate variables used
in the expression as ``sympy`` symbols, here defined as ``vars_``:

>>> vars_
{'x': x, 'y': y, 'z': z}

A second dictionary containing the potential parameters as ``sympy`` symbols is
also returned, here defined as ``pars``:

>>> pars
{'v_c': v_c, 'r_h': r_h, 'q1': q1, 'q2': q2, 'q3': q3, 'phi': phi, 'G': G}

The expressions and variables returned can be used to perform operations on the
potential expression. For example, to create a ``sympy`` expression for the
gradient of the potential:

>>> import sympy as sy
>>> grad = sy.derive_by_array(expr, list(vars_.values()))
>>> grad[0] # dPhi/dx
1.0*v_c**2*x/(q1**2*(r_h**2 + z**2/q3**2 + y**2/q2**2 + x**2/q1**2))


Using gala.potential
====================
More details are provided in the linked pages below:
Expand Down