TermStreamer

Symbolica documentation for getting started, symbolic expressions, numerical evaluation, pattern matching, and APIs in Python and Rust.

TermStreamer

TermStreamer()

A term streamer that can handle large expressions, by streaming terms to and from disk.

Methods

Name Description
__add__ Add two term streamers together, returning the result.
__iadd__ Add another term streamer to this one.
__new__ Create a new term streamer with a given path for its files, the maximum size of the memory buffer and the number of cores.
clear Clear the term streamer.
fits_in_memory Check if the term stream fits in memory.
get_byte_size Get the byte size of the term stream.
get_num_terms Get the number of terms in the stream.
load Load terms and their state from a binary file into the term streamer
map Apply a transformer to all terms in the stream.
map_single_thread Apply a transformer to all terms in the stream using a single thread.
normalize Sort and fuse all terms in the stream.
push Push an expression to the term stream.
save Export terms and their state to a binary file
to_expression Convert the term stream into an expression

__add__

TermStreamer.__add__(other: TermStreamer) -> TermStreamer

Add two term streamers together, returning the result.

Parameters

  • other (TermStreamer) The other operand to combine or compare with.

__iadd__

TermStreamer.__iadd__(other: TermStreamer) -> None

Add another term streamer to this one.

Parameters

  • other (TermStreamer) The other operand to combine or compare with.

__new__

TermStreamer.__new__(
    path: str | None = None,
    max_mem_bytes: int | None = None,
    n_cores: int | None = None,
) -> TermStreamer

Create a new term streamer with a given path for its files, the maximum size of the memory buffer and the number of cores.

Parameters

  • path (str | None) The directory used for the streamer’s temporary files.
  • max_mem_bytes (int | None) The maximum in-memory buffer size in bytes.
  • n_cores (int | None) The number of CPU cores used for streaming operations.

clear

TermStreamer.clear() -> None

Clear the term streamer.

fits_in_memory

TermStreamer.fits_in_memory() -> bool

Check if the term stream fits in memory.

get_byte_size

TermStreamer.get_byte_size() -> int

Get the byte size of the term stream.

get_num_terms

TermStreamer.get_num_terms() -> int

Get the number of terms in the stream.

load

TermStreamer.load(filename: str, conflict_fn: Callable[[str], str] | None = None) -> int

Load terms and their state from a binary file into the term streamer. The number of terms loaded is returned.

The state will be merged with the current one. If a symbol has conflicting attributes, the conflict can be resolved using the renaming function conflict_fn.

A term stream can be exported using TermStreamer.save.

Parameters

  • filename (str) The file path to load from or save to.
  • conflict_fn (Callable[[str], str] | None) A callback that resolves symbol conflicts during loading.

map

TermStreamer.map(f: Transformer, stats_to_file: str | None = None) -> TermStreamer

Apply a transformer to all terms in the stream.

Parameters

  • f (Transformer) The transformer to apply.
  • stats_to_file (str, optional) If set, the output of the stats transformer will be written to a file in JSON format.

map_single_thread

TermStreamer.map_single_thread(f: Transformer, stats_to_file: str | None = None) -> TermStreamer

Apply a transformer to all terms in the stream using a single thread.

Parameters

  • f (Transformer) The transformer to apply.
  • stats_to_file (str, optional) If set, the output of the stats transformer will be written to a file in JSON format.

normalize

TermStreamer.normalize() -> None

Sort and fuse all terms in the stream.

push

TermStreamer.push(expr: Expression) -> None

Push an expression to the term stream.

Parameters

  • expr (Expression) The expression to operate on.

save

TermStreamer.save(filename: str, compression_level: int = 9) -> None

Export terms and their state to a binary file. The resulting file can be read back using TermStreamer.load or by using Expression.load. In the latter case, the whole term stream will be read into memory as a single expression.

Parameters

  • filename (str) The file path to load from or save to.
  • compression_level (int) The compression level for serialized output.

to_expression

TermStreamer.to_expression() -> Expression

Convert the term stream into an expression. This may exceed the available memory.