Skip to content

Replace np.tensordot(..., axes=0) with np.outer for faster and clearer outer product #552

@SaFE-APIOpt

Description

@SaFE-APIOpt

lambda a, b: np.tensordot(a, b, axes=0),

Current code:
lambda a, b: np.tensordot(a, b, axes=0)
Proposed replacement:
lambda a, b: np.outer(a, b)
np.tensordot(a, b, axes=0) is a general-purpose tensor operation that, in this case, computes exactly the same result as np.outer(a, b) for 1-D inputs. However, tensordot carries additional overhead from its general implementation, while outer is optimized specifically for outer products. Benchmarks show that np.outer is consistently faster and also more expressive, since it directly conveys the mathematical intent of an outer product.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions