pytorch/c10/core/Scalar.cpp
anjali411 cedeee2cd4 Add scalar.conj() and update backward formulas for add and sub (#46596)
Summary:
Pull Request resolved: https://github.com/pytorch/pytorch/pull/46596

1. Added `conj` method for scalar similar to numpy.
2. Updates backward formulas for add and sub to work correctly for R -> C cases and for the case when alpha is complex.
3. Enabled complex backward for nonzero (no formula update needed).

Test Plan: Imported from OSS

Reviewed By: glaringlee

Differential Revision: D24529227

Pulled By: anjali411

fbshipit-source-id: da871309a6decf5a4ab5c561d5ab35fc66b5273d
2020-11-02 16:17:00 -08:00

25 lines
465 B
C++

#include <c10/core/Scalar.h>
namespace c10 {
Scalar Scalar::operator-() const {
TORCH_CHECK(!isBoolean(), "torch boolean negative, the `-` operator, is not suppported.");
if (isFloatingPoint()) {
return Scalar(-v.d);
} else if (isComplex()) {
return Scalar(-v.z);
} else {
return Scalar(-v.i);
}
}
Scalar Scalar::conj() const {
if (isComplex()) {
return Scalar(std::conj(v.z));
} else {
return *this;
}
}
} // namespace c10