Skip to main content

A Python package for visualizing array-based algorithms using Manim

Project description

ArrayViz

Meta license
Testing Pylint and Tests Release

This package allows users to pass a function and visualize it as blocks and elements using Manim, a powerful mathematical animation engine. It provides an easy way to animate algorithms step-by-step, helping with educational purposes and debugging.

Note

This project is a work in progress. Package has not been released yet but is planned to be. Only limited functionality is implemented as of now.

Installation

First, install Manim according to the instructions on Manim's official website.

Install the package via PyPI:

python -m pip install --upgrade arrayviz

Usage

ArrayBlockVisualizer Class

The ArrayBlockVisualizer class is designed to animate the execution of a user-provided function. Users can control when the pointers should update in relation to the array frame updates (before, at the same time, or after).

Parameters

  • func: The user-provided function to animate.
  • func_args: Arguments to pass to the user-provided function.
  • delay (float): The delay time between frames in seconds. Default is 1.
  • **kwargs: Additional keyword arguments for the Scene superclass.

Example

merge sorted arrays

The following example demonstrates how to use the ArrayBlockVisualizer class to animate an example merge function for sorted arrays.

examples/merge_sorted_arrays.py

from arrayviz import ArrayBlockVisualizer


def merge(nums1, m, nums2, n, record_frame):
    """
    Merges two sorted arrays nums1 and nums2 into a single sorted array in place.

    Args:
        nums1 (list): The first sorted array with extra space at the end to hold nums2 elements.
        m (int): The number of valid elements in nums1.
        nums2 (list): The second sorted array.
        n (int): The number of elements in nums2.
        record_frame (callable): A callback function to record the state of arrays and pointers.
    """
    merged_index = m + n

    # Record initial array
    record_frame(arrays=[nums1[:], nums2[:]])

    while m > 0 and n > 0:
        # Record pointers
        record_frame(pointers=[{"i": m - 1}, {"j": n - 1}])
        if nums1[m - 1] > nums2[n - 1]:
            nums1[merged_index - 1] = nums1[m - 1]
            m -= 1
        else:
            nums1[merged_index - 1] = nums2[n - 1]
            n -= 1
        merged_index -= 1

        # Record new array
        record_frame(arrays=[nums1[:], nums2[:]])

    # Record final pointers
    record_frame(pointers=[{"i": n - 1}, {"j": n - 1}])

    nums1[:n] = nums2[:n]

    # Record final array
    record_frame(arrays=[nums1[:], nums2[:]])


# Initial arrays for the merge function
nums1 = [4, 5, 7, 0, 0, 0, 0, 0, 0]
nums2 = [1, 2, 2, 3, 5, 6]
m = 3  # Number of initial valid elements in nums1
n = 6  # Number of elements in nums2


class MergeScene(ArrayBlockVisualizer):
    """
    A Manim scene to visualize the merge function using the FunctionAnimation class.
    """

    def __init__(self, **kwargs):
        """
        Initializes the MergeScene with the merge function and its arguments.

        Args:
            **kwargs: Additional keyword arguments for the ArrayBlockVisualizer superclass.
        """
        super().__init__(merge, (nums1, m, nums2, n), **kwargs)


# To render this scene, use the following command in your terminal:
# manim -pql examples/merge_sorted_arrays.py MergeScene

Running the Example

To render the MergeScene, run the following command in your terminal:

manim -pql example.py MergeScene
  • -pql stands for play in low quality. You can adjust this based on the desired rendering quality.

License

This project is licensed under the MIT License - see the LICENSE file for details.

Contributing

Contributions are welcome! Please open an issue or submit a pull request for any improvements or bug fixes.

Acknowledgements

Manim Community for developing the Manim library.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

arrayviz-0.0.1.tar.gz (6.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

arrayviz-0.0.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file arrayviz-0.0.1.tar.gz.

File metadata

  • Download URL: arrayviz-0.0.1.tar.gz
  • Upload date:
  • Size: 6.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for arrayviz-0.0.1.tar.gz
Algorithm Hash digest
SHA256 f36e8424916cf099e5fd0208068e86988e87eb87c1e7d48e965880950851f12c
MD5 82553aeb2fe2beb00739e69ad1004f17
BLAKE2b-256 5c86c07e5ccc4692285e24395c8ac66e1822b5d4e04e471f07c869c557689ae7

See more details on using hashes here.

File details

Details for the file arrayviz-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: arrayviz-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.0 CPython/3.12.4

File hashes

Hashes for arrayviz-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b627d4f0ecb3840d40546b37fecced406f5ecf5627c3ee092d5487371fdb2cfb
MD5 18abe456556b3e61a20cb6f874e9b5ef
BLAKE2b-256 3f7e65933b967c7853845031f5389b2fcd51cab0f6f6a28c8bce46addcddb3ed

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page