cached#

Handy aliases for working with cached SymPy expressions.

doit(expr: SympyObject) SympyObject[source]#

Perform doit() and cache the result to disk.

The cached result is fetched from disk if the hash of the original expression is the same as the hash embedded in the filename (see get_readable_hash()).

Parameters:

expr – A sympy.Expr on which to call doit().

Added in version 0.14.4.

Helper functions for cached.doit() and related functions.

These methods are private, but can be imported from this module:

cache_to_disk(func: Callable[P, T]) Callable[P, T][source]#
cache_to_disk(*, dump_function: Callable[[Any, SupportsWrite[bytes]], None] = pickle.dump, load_function: Callable[[BufferedReader], Any] = pickle.load, dependencies: list[str] | None = None, function_name: str | None = None) Callable[[Callable[P, T]], Callable[P, T]]

Decorator for caching the result of a function to disk.

This function works similarly to functools.cache, but it stores the result of the function to disk as a pickle file.

Tip

  • Caching can be disabled by setting the environment variable NO_CACHE. This can be useful to test if caches are correctly invalidated.

  • Set COMPWA_CACHE_DIR to change the cache directory. Alternatively, have a look at the implementation of get_system_cache_directory() to see how the cache directory is determined from system environment variables.

get_system_cache_directory() str[source]#

Return the system cache directory for the current platform.

>>> import sys
>>> if sys.platform.startswith("darwin"):
...     assert get_system_cache_directory().endswith("/Library/Caches")
>>> if sys.platform.startswith("linux"):
...     assert get_system_cache_directory().endswith("/.cache")
>>> if sys.platform.startswith("win"):
...     assert get_system_cache_directory().endswith(R"\AppData\Local")
get_readable_hash(obj: Hashable) str[source]#

Get a human-readable hash of any hashable Python object.

Parameters:

obj – Any hashable object, mutable or immutable, to be hashed.

to_bytes(obj) bytes[source]#

Convert any Python object to bytes using pickle.dumps().

make_hashable(*args) Hashable[source]#

Make a hashable object from any Python object.

>>> make_hashable("a", 1, {"b": 2}, {3, 4})
('a', 1, frozendict.frozendict({'b': 2}), frozenset({3, 4}))
>>> make_hashable({"a": {"sub-key": {1, 2, 3}, "b": [4, 5]}})
frozendict.frozendict({'a': frozendict.frozendict({'sub-key': frozenset({1, 2, 3}), 'b': (4, 5)})})
>>> make_hashable("already-hashable")
'already-hashable'
simplify(expr: Expr, *args, **kwargs) Expr[source]#

Perform simplify() and cache the result to disk.

Added in version 0.15.7.

trigsimp(expr: Expr, *args, **kwargs) Expr[source]#

Perform trigsimp() and cache the result to disk.

Added in version 0.15.7.

subs(expr: Expr, substitutions: Mapping[Basic, Basic]) Expr[source]#

Call subs() and cache the result to disk.

xreplace(expr: Expr, substitutions: Mapping[Basic, Basic]) Expr[source]#

Call xreplace() and cache the result to disk.

unfold(obj: Model) Expr[source]#
unfold(obj: Expr, substitutions: Mapping[Basic, Basic]) Expr

Efficiently perform both substitutions and doit().

class Model(*args, **kwargs)[source]#

Bases: Protocol

property intensity: Expr[source]#
property amplitudes: Mapping[Basic, Basic][source]#