Convert a 4 x 4 Cholesky inverse to real arithmetic

Split each complex signal into real and imaginary signals, choosing either three or four real multipliers for the complex products in MADS operations. The Cholesky generator expects a symmetric positive definite matrix, supplied in packed lower-triangular row order. This example uses real matrix entries represented as complex signals; all imaginary input channels are zero.

import numpy as np

from b_asic.core_operations import MADS, Multiplication, ReciprocalSquareRoot
from b_asic.sfg_generators import cholesky_matrix_inverse

Generate the complex arithmetic graph. Its diagonal reciprocal square roots have real_valued=True: conversion terminates their imaginary inputs at Sinks and produces zero constants for their imaginary outputs.

complex_sfg = cholesky_matrix_inverse(N=4, mode="mult", pe="mads")
assert all(op.real_valued for op in complex_sfg.find_by_type(ReciprocalSquareRoot))

Options are forwarded as keyword arguments to each operation’s private conversion hook. Both graphs have 20 real inputs and 20 real outputs: real0, imag0, real1, imag1, … for the ten lower-triangular entries.

real_four = complex_sfg.complex_to_real(multiplication_method="four")
real_three = complex_sfg.complex_to_real(multiplication_method="three")
print("Four-product multipliers:", len(real_four.find_by_type(Multiplication)))
print("Three-product multipliers:", len(real_three.find_by_type(Multiplication)))
Four-product multipliers: 208
Three-product multipliers: 156

Check both decompositions and the original graph against NumPy.

Keep the original complex interface with split_io=False. Boundary operations extract and join real/imaginary signals while internal arithmetic stays real. With memory interfaces, addresses and memory names are preserved as well.

memory_sfg = cholesky_matrix_inverse(N=4, mode="mult", pe="mads", mem=True)
real_memory_sfg = memory_sfg.complex_to_real(
    multiplication_method="three", split_io=False
)
np.testing.assert_allclose(real_memory_sfg.evaluate(*packed), expected, atol=1e-12)

Convert individual operations with different settings. Unselected operations keep their graph IDs and complex interfaces. Neighboring converted regions share real signals after redundant split/join pairs are removed. A type or iterator of types/IDs can also select several operations at once.

mads_ids = [op.graph_id for op in memory_sfg.find_by_type(MADS)]
mixed_sfg = memory_sfg.complex_to_real(
    operations=mads_ids[0], multiplication_method="three"
)
mixed_sfg = mixed_sfg.complex_to_real(
    operations=mads_ids[1], multiplication_method="four"
)
np.testing.assert_allclose(mixed_sfg.evaluate(*packed), expected, atol=1e-12)

Display the operation counts. Constant propagation and sink propagation can be applied separately when the imaginary input channels are known to be zero.

Total running time of the script: (0 minutes 0.171 seconds)

Gallery generated by Sphinx-Gallery