|
|
@ -22,22 +22,43 @@ class CDiv(Expander):
|
|
|
|
"""CDiv expander"""
|
|
|
|
"""CDiv expander"""
|
|
|
|
|
|
|
|
|
|
|
|
def _expand(self, graph_builder):
|
|
|
|
def _expand(self, graph_builder):
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
CDiv Implementation
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
|
|
|
graph_builder: 图构建器对象,用于构建计算图。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
|
|
|
返回复数除法结果。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
实现复数除法(CDiv)操作。
|
|
|
|
|
|
|
|
获取两个输入的复数,分别计算它们的实部和虚部。
|
|
|
|
|
|
|
|
然后计算分母和分子的实部和虚部,并进行除法运算,
|
|
|
|
|
|
|
|
最后将得到的商的实部和虚部合并为复数结果返回。
|
|
|
|
|
|
|
|
"""
|
|
|
|
"""CDiv Implementation"""
|
|
|
|
"""CDiv Implementation"""
|
|
|
|
|
|
|
|
# 获取输入的两个复数
|
|
|
|
input_x, input_y = self.inputs
|
|
|
|
input_x, input_y = self.inputs
|
|
|
|
x_real = graph_builder.emit('CReal', [input_x])
|
|
|
|
# 获取输入复数的实部
|
|
|
|
y_real = graph_builder.emit('CReal', [input_y])
|
|
|
|
x_real = graph_builder.emit('CReal', [input_x]) # 发射 CReal 操作获取 input_x 的实部
|
|
|
|
x_imag = graph_builder.emit('CImag', [input_x])
|
|
|
|
y_real = graph_builder.emit('CReal', [input_y]) # 发射 CReal 操作获取 input_y 的实部
|
|
|
|
y_imag = graph_builder.emit('CImag', [input_y])
|
|
|
|
# 获取输入复数的虚部
|
|
|
|
squre_y_real = graph_builder.emit('Mul', [y_real, y_real])
|
|
|
|
x_imag = graph_builder.emit('CImag', [input_x]) # 发射 CImag 操作获取 input_x 的虚部
|
|
|
|
squre_y_imag = graph_builder.emit('Mul', [y_imag, y_imag])
|
|
|
|
y_imag = graph_builder.emit('CImag', [input_y]) # 发射 CImag 操作获取 input_y 的虚部
|
|
|
|
final_denominator = graph_builder.emit('Add', [squre_y_real, squre_y_imag])
|
|
|
|
# 计算分母
|
|
|
|
x_real_mul_y_real = graph_builder.emit('Mul', [x_real, y_real])
|
|
|
|
squre_y_real = graph_builder.emit('Mul', [y_real, y_real]) # 计算 y_real 的平方
|
|
|
|
x_imag_mul_y_imag = graph_builder.emit('Mul', [x_imag, y_imag])
|
|
|
|
squre_y_imag = graph_builder.emit('Mul', [y_imag, y_imag]) # 计算 y_imag 的平方
|
|
|
|
x_real_mul_y_imag = graph_builder.emit('Mul', [x_real, y_imag])
|
|
|
|
final_denominator = graph_builder.emit('Add', [squre_y_real, squre_y_imag]) # 计算分母
|
|
|
|
x_imag_mul_y_real = graph_builder.emit('Mul', [x_imag, y_real])
|
|
|
|
# 计算分子
|
|
|
|
final_numerator_real = graph_builder.emit('Add', [x_real_mul_y_real, x_imag_mul_y_imag])
|
|
|
|
x_real_mul_y_real = graph_builder.emit('Mul', [x_real, y_real]) # 计算 x_real 和 y_real 的乘积
|
|
|
|
final_numerator_imag = graph_builder.emit('Sub', [x_imag_mul_y_real, x_real_mul_y_imag])
|
|
|
|
x_imag_mul_y_imag = graph_builder.emit('Mul', [x_imag, y_imag]) # 计算 x_imag 和 y_imag 的乘积
|
|
|
|
result_real = graph_builder.emit('RealDiv', [final_numerator_real, final_denominator])
|
|
|
|
x_real_mul_y_imag = graph_builder.emit('Mul', [x_real, y_imag]) # 计算 x_real 和 y_imag 的乘积
|
|
|
|
result_imag = graph_builder.emit('RealDiv', [final_numerator_imag, final_denominator])
|
|
|
|
x_imag_mul_y_real = graph_builder.emit('Mul', [x_imag, y_real]) # 计算 x_imag 和 y_real 的乘积
|
|
|
|
result = graph_builder.emit('Complex', [result_real, result_imag])
|
|
|
|
final_numerator_real = graph_builder.emit('Add', [x_real_mul_y_real, x_imag_mul_y_imag]) # 计算分子的实部
|
|
|
|
|
|
|
|
final_numerator_imag = graph_builder.emit('Sub', [x_imag_mul_y_real, x_real_mul_y_imag]) # 计算分子的虚部
|
|
|
|
|
|
|
|
# 计算商
|
|
|
|
|
|
|
|
result_real = graph_builder.emit('RealDiv', [final_numerator_real, final_denominator]) # 计算商的实部
|
|
|
|
|
|
|
|
result_imag = graph_builder.emit('RealDiv', [final_numerator_imag, final_denominator]) # 计算商的虚部
|
|
|
|
|
|
|
|
# 将商合并为复数结果
|
|
|
|
|
|
|
|
result = graph_builder.emit('Complex', [result_real, result_imag]) # 将实部和虚部合并为复数结果
|
|
|
|
return result
|
|
|
|
return result
|
|
|
|