|
2 | 2 |
|
3 | 3 | package py
|
4 | 4 |
|
| 5 | +import ( |
| 6 | + "math" |
| 7 | +) |
| 8 | + |
5 | 9 | var ComplexType = NewType("complex64")
|
6 | 10 |
|
7 |
| -type Complex complex64 |
| 11 | +type Complex complex128 |
8 | 12 |
|
9 | 13 | // Type of this Complex object
|
10 | 14 | func (o Complex) Type() *Type {
|
11 | 15 | return ComplexType
|
12 | 16 | }
|
| 17 | + |
| 18 | +// Convert an Object to an Complex |
| 19 | +// |
| 20 | +// Retrurns ok as to whether the conversion worked or not |
| 21 | +func convertToComplex(other Object) (Complex, bool) { |
| 22 | + switch b := other.(type) { |
| 23 | + case Complex: |
| 24 | + return b, true |
| 25 | + case Float: |
| 26 | + return Complex(complex(b, 0)), true |
| 27 | + case Int: |
| 28 | + return Complex(complex(float64(b), 0)), true |
| 29 | + case Bool: |
| 30 | + if b { |
| 31 | + return Complex(1), true |
| 32 | + } else { |
| 33 | + return Complex(0), true |
| 34 | + } |
| 35 | + } |
| 36 | + return 0, false |
| 37 | +} |
| 38 | + |
| 39 | +func (a Complex) M__add__(other Object) Object { |
| 40 | + if b, ok := convertToComplex(other); ok { |
| 41 | + return Complex(a + b) |
| 42 | + } |
| 43 | + return NotImplemented |
| 44 | +} |
| 45 | + |
| 46 | +func (a Complex) M__radd__(other Object) Object { |
| 47 | + return a.M__add__(other) |
| 48 | +} |
| 49 | + |
| 50 | +func (a Complex) M__iadd__(other Object) Object { |
| 51 | + return a.M__add__(other) |
| 52 | +} |
| 53 | + |
| 54 | +func (a Complex) M__sub__(other Object) Object { |
| 55 | + if b, ok := convertToComplex(other); ok { |
| 56 | + return Complex(a - b) |
| 57 | + } |
| 58 | + return NotImplemented |
| 59 | +} |
| 60 | + |
| 61 | +func (a Complex) M__rsub__(other Object) Object { |
| 62 | + if b, ok := convertToComplex(other); ok { |
| 63 | + return Complex(b - a) |
| 64 | + } |
| 65 | + return NotImplemented |
| 66 | +} |
| 67 | + |
| 68 | +func (a Complex) M__isub(other Object) Object { |
| 69 | + return a.M__sub__(other) |
| 70 | +} |
| 71 | + |
| 72 | +func (a Complex) M__mul__(other Object) Object { |
| 73 | + if b, ok := convertToComplex(other); ok { |
| 74 | + return Complex(a * b) |
| 75 | + } |
| 76 | + return NotImplemented |
| 77 | +} |
| 78 | + |
| 79 | +func (a Complex) M__rmul__(other Object) Object { |
| 80 | + return a.M__mul__(other) |
| 81 | +} |
| 82 | + |
| 83 | +func (a Complex) M__imul__(other Object) Object { |
| 84 | + return a.M__mul__(other) |
| 85 | +} |
| 86 | + |
| 87 | +func (a Complex) M__truediv__(other Object) Object { |
| 88 | + if b, ok := convertToComplex(other); ok { |
| 89 | + return Complex(a / b) |
| 90 | + } |
| 91 | + return NotImplemented |
| 92 | +} |
| 93 | + |
| 94 | +func (a Complex) M__rtruediv__(other Object) Object { |
| 95 | + if b, ok := convertToComplex(other); ok { |
| 96 | + return Complex(b / a) |
| 97 | + } |
| 98 | + return NotImplemented |
| 99 | +} |
| 100 | + |
| 101 | +func (a Complex) M__itruediv(other Object) Object { |
| 102 | + return Complex(a).M__truediv__(other) |
| 103 | +} |
| 104 | + |
| 105 | +// Floor a complex number |
| 106 | +func complexFloor(a Complex) Complex { |
| 107 | + return Complex(complex(math.Floor(real(a)), math.Floor(imag(a)))) |
| 108 | +} |
| 109 | + |
| 110 | +// Floor divide two complex numbers |
| 111 | +func complexFloorDiv(a, b Complex) Complex { |
| 112 | + q := complexFloor(a / b) |
| 113 | + r := a - q*b |
| 114 | + return Complex(r) |
| 115 | +} |
| 116 | + |
| 117 | +func (a Complex) M__floordiv__(other Object) Object { |
| 118 | + if b, ok := convertToComplex(other); ok { |
| 119 | + return complexFloor(a / b) |
| 120 | + } |
| 121 | + return NotImplemented |
| 122 | +} |
| 123 | + |
| 124 | +func (a Complex) M__rfloordiv__(other Object) Object { |
| 125 | + if b, ok := convertToComplex(other); ok { |
| 126 | + return complexFloor(b / a) |
| 127 | + } |
| 128 | + return NotImplemented |
| 129 | +} |
| 130 | + |
| 131 | +func (a Complex) M__ifloordiv(other Object) Object { |
| 132 | + return a.M__floordiv__(other) |
| 133 | +} |
| 134 | + |
| 135 | +// Does Mod of two floating point numbers |
| 136 | +func complexMod(a, b Complex) Complex { |
| 137 | + q := complexFloor(a / b) |
| 138 | + r := a - Complex(q)*b |
| 139 | + return Complex(r) |
| 140 | +} |
| 141 | + |
| 142 | +func (a Complex) M__mod__(other Object) Object { |
| 143 | + if b, ok := convertToComplex(other); ok { |
| 144 | + return complexMod(a, b) |
| 145 | + } |
| 146 | + return NotImplemented |
| 147 | +} |
| 148 | + |
| 149 | +func (a Complex) M__rmod__(other Object) Object { |
| 150 | + if b, ok := convertToComplex(other); ok { |
| 151 | + return complexMod(b, a) |
| 152 | + } |
| 153 | + return NotImplemented |
| 154 | +} |
| 155 | + |
| 156 | +func (a Complex) M__imod(other Object) Object { |
| 157 | + return a.M__mod__(other) |
| 158 | +} |
0 commit comments