|
7 | 7 | package py
|
8 | 8 |
|
9 | 9 | import (
|
| 10 | + "fmt" |
10 | 11 | "math"
|
11 | 12 | "math/cmplx"
|
12 | 13 | )
|
@@ -60,6 +61,14 @@ func convertToComplex(other Object) (Complex, bool) {
|
60 | 61 | return 0, false
|
61 | 62 | }
|
62 | 63 |
|
| 64 | +func (a Complex) M__str__() (Object, error) { |
| 65 | + return String(fmt.Sprintf("(%g%+gj)", real(complex128(a)), imag(complex128(a)))), nil |
| 66 | +} |
| 67 | + |
| 68 | +func (a Complex) M__repr__() (Object, error) { |
| 69 | + return a.M__str__() |
| 70 | +} |
| 71 | + |
63 | 72 | func (a Complex) M__neg__() (Object, error) {
|
64 | 73 | return -a, nil
|
65 | 74 | }
|
@@ -286,6 +295,24 @@ func (a Complex) M__ge__(other Object) (Object, error) {
|
286 | 295 | return a.M__lt__(other)
|
287 | 296 | }
|
288 | 297 |
|
| 298 | +// Properties |
| 299 | +func init() { |
| 300 | + ComplexType.Dict["real"] = &Property{ |
| 301 | + Fget: func(self Object) (Object, error) { |
| 302 | + return Float(real(self.(Complex))), nil |
| 303 | + }, |
| 304 | + } |
| 305 | + ComplexType.Dict["imag"] = &Property{ |
| 306 | + Fget: func(self Object) (Object, error) { |
| 307 | + return Float(imag(self.(Complex))), nil |
| 308 | + }, |
| 309 | + } |
| 310 | + ComplexType.Dict["conjugate"] = MustNewMethod("conjugate", func(self Object) (Object, error) { |
| 311 | + cnj := cmplx.Conj(complex128(self.(Complex))) |
| 312 | + return Complex(cnj), nil |
| 313 | + }, 0, "conjugate() -> Returns the complex conjugate.") |
| 314 | +} |
| 315 | + |
289 | 316 | // Check interface is satisfied
|
290 | 317 | var _ floatArithmetic = Complex(complex(0, 0))
|
291 | 318 | var _ richComparison = Complex(0)
|
0 commit comments