Allow floats and ints to be converted to complex numbers.

PiperOrigin-RevId: 314357192
Change-Id: I29a4c63501a42a9456d144226d5a97d553d046b1
This commit is contained in:
Akshay Modi 2020-06-02 10:28:30 -07:00 committed by TensorFlower Gardener
parent 9ab7e6c882
commit c60b969753
2 changed files with 12 additions and 1 deletions

View File

@ -545,6 +545,13 @@ class TFETensorUtilTest(test_util.TensorFlowTestCase):
ValueError, "non-rectangular Python sequence"):
constant_op.constant(l)
@test_util.assert_no_new_pyobjects_executing_eagerly
def testFloatAndIntAreConvertibleToComplex(self):
a = [[1., 1], [1j, 2j]]
np_value = np.array(a, dtype=np.complex128)
tf_value = ops.convert_to_tensor(a, dtype=dtypes.complex128)
self.assertAllEqual(tf_value.numpy(), np_value)
if __name__ == "__main__":
test.main()

View File

@ -642,7 +642,11 @@ struct ConverterTraits<complex128> {
*out = complex128(as_complex.real, as_complex.imag);
return nullptr;
}
return ErrorMixedTypes;
double as_double;
auto error = ConvertOneFloat<double>(v, &as_double);
if (error != nullptr) return error;
*out = complex128(as_double, 0.0);
return nullptr;
}
};