forked from alanjds/grumpy
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathcomplex.go
536 lines (486 loc) · 16.4 KB
/
complex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
// Copyright 2016 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package grumpy
import (
"errors"
"fmt"
"math"
"math/cmplx"
"reflect"
"regexp"
"strconv"
"strings"
)
// ComplexType is the object representing the Python 'complex' type.
var ComplexType = newBasisType("complex", reflect.TypeOf(Complex{}), toComplexUnsafe, ObjectType)
// Complex represents Python 'complex' objects.
type Complex struct {
Object
value complex128
}
// NewComplex returns a new Complex holding the given complex value.
func NewComplex(value complex128) *Complex {
return &Complex{Object{typ: ComplexType}, value}
}
func toComplexUnsafe(o *Object) *Complex {
return (*Complex)(o.toPointer())
}
// ToObject upcasts c to an Object.
func (c *Complex) ToObject() *Object {
return &c.Object
}
// Value returns the underlying complex value held by c.
func (c *Complex) Value() complex128 {
return c.value
}
func complexAbs(f *Frame, o *Object) (*Object, *BaseException) {
c := toComplexUnsafe(o).Value()
return NewFloat(cmplx.Abs(c)).ToObject(), nil
}
func complexAdd(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__add__", v, w, func(lhs, rhs complex128) complex128 {
return lhs + rhs
})
}
func complexCompareNotSupported(f *Frame, v, w *Object) (*Object, *BaseException) {
if w.isInstance(IntType) || w.isInstance(LongType) || w.isInstance(FloatType) || w.isInstance(ComplexType) {
return nil, f.RaiseType(TypeErrorType, "no ordering relation is defined for complex numbers")
}
return NotImplemented, nil
}
func complexComplex(f *Frame, o *Object) (*Object, *BaseException) {
return o, nil
}
func complexDiv(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivModOp(f, "__div__", v, w, func(v, w complex128) (complex128, bool) {
if w == 0 {
return 0, false
}
return v / w, true
})
}
func complexDivMod(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivAndModOp(f, "__divmod__", v, w, func(v, w complex128) (complex128, complex128, bool) {
if w == 0 {
return 0, 0, false
}
return complexFloorDivOp(v, w), complexModOp(v, w), true
})
}
func complexEq(f *Frame, v, w *Object) (*Object, *BaseException) {
e, ok := complexCompare(toComplexUnsafe(v), w)
if !ok {
return NotImplemented, nil
}
return GetBool(e).ToObject(), nil
}
func complexFloorDiv(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivModOp(f, "__floordiv__", v, w, func(v, w complex128) (complex128, bool) {
if w == 0 {
return 0, false
}
return complexFloorDivOp(v, w), true
})
}
func complexHash(f *Frame, o *Object) (*Object, *BaseException) {
v := toComplexUnsafe(o).Value()
hashCombined := hashFloat(real(v)) + 1000003*hashFloat(imag(v))
if hashCombined == -1 {
hashCombined = -2
}
return NewInt(hashCombined).ToObject(), nil
}
func complexMod(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivModOp(f, "__mod__", v, w, func(v, w complex128) (complex128, bool) {
if w == 0 {
return 0, false
}
return complexModOp(v, w), true
})
}
func complexMul(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__mul__", v, w, func(lhs, rhs complex128) complex128 {
return lhs * rhs
})
}
func complexNE(f *Frame, v, w *Object) (*Object, *BaseException) {
e, ok := complexCompare(toComplexUnsafe(v), w)
if !ok {
return NotImplemented, nil
}
return GetBool(!e).ToObject(), nil
}
func complexNeg(f *Frame, o *Object) (*Object, *BaseException) {
c := toComplexUnsafe(o).Value()
return NewComplex(-c).ToObject(), nil
}
func complexNew(f *Frame, t *Type, args Args, _ KWArgs) (*Object, *BaseException) {
argc := len(args)
if argc == 0 {
return newObject(t), nil
}
if argc > 2 {
return nil, f.RaiseType(TypeErrorType, "'__new__' of 'complex' requires at most 2 arguments")
}
if t != ComplexType {
// Allocate a plain complex then copy it's value into an object
// of the complex subtype.
x, raised := complexNew(f, ComplexType, args, nil)
if raised != nil {
return nil, raised
}
result := toComplexUnsafe(newObject(t))
result.value = toComplexUnsafe(x).Value()
return result.ToObject(), nil
}
if complexSlot := args[0].typ.slots.Complex; complexSlot != nil && argc == 1 {
c, raised := complexConvert(complexSlot, f, args[0])
if raised != nil {
return nil, raised
}
return c.ToObject(), nil
}
if args[0].isInstance(StrType) {
if argc > 1 {
return nil, f.RaiseType(TypeErrorType, "complex() can't take second arg if first is a string")
}
s := toStrUnsafe(args[0]).Value()
result, err := parseComplex(s)
if err != nil {
return nil, f.RaiseType(ValueErrorType, "complex() arg is a malformed string")
}
return NewComplex(result).ToObject(), nil
}
if argc > 1 && args[1].isInstance(StrType) {
return nil, f.RaiseType(TypeErrorType, "complex() second arg can't be a string")
}
cr, raised := complex128Convert(f, args[0])
if raised != nil {
return nil, raised
}
var ci complex128
if argc > 1 {
ci, raised = complex128Convert(f, args[1])
if raised != nil {
return nil, raised
}
}
// Logically it should be enough to return this:
// NewComplex(cr + ci*1i).ToObject()
// But Go complex arithmatic is not satisfying all conditions, for instance:
// cr := complex(math.Inf(1), 0)
// ci := complex(math.Inf(-1), 0)
// fmt.Println(cr + ci*1i)
// Output is (NaN-Infi), instead of (+Inf-Infi).
return NewComplex(complex(real(cr)-imag(ci), imag(cr)+real(ci))).ToObject(), nil
}
func complexNonZero(f *Frame, o *Object) (*Object, *BaseException) {
return GetBool(toComplexUnsafe(o).Value() != 0).ToObject(), nil
}
func complexPos(f *Frame, o *Object) (*Object, *BaseException) {
return o, nil
}
func complexPow(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__pow__", v, w, func(lhs, rhs complex128) complex128 {
return cmplx.Pow(lhs, rhs)
})
}
func complexRAdd(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__radd__", v, w, func(lhs, rhs complex128) complex128 {
return lhs + rhs
})
}
func complexRDiv(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivModOp(f, "__rdiv__", v, w, func(v, w complex128) (complex128, bool) {
if v == 0 {
return 0, false
}
return w / v, true
})
}
func complexRDivMod(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivAndModOp(f, "__rdivmod__", v, w, func(v, w complex128) (complex128, complex128, bool) {
if v == 0 {
return 0, 0, false
}
return complexFloorDivOp(w, v), complexModOp(w, v), true
})
}
func complexRepr(f *Frame, o *Object) (*Object, *BaseException) {
c := toComplexUnsafe(o).Value()
rs, is := "", ""
pre, post := "", ""
sign := ""
if real(c) == 0.0 {
is = strconv.FormatFloat(imag(c), 'g', -1, 64)
} else {
pre = "("
rs = strconv.FormatFloat(real(c), 'g', -1, 64)
is = strconv.FormatFloat(imag(c), 'g', -1, 64)
if imag(c) >= 0.0 || math.IsNaN(imag(c)) {
sign = "+"
}
post = ")"
}
rs = unsignPositiveInf(strings.ToLower(rs))
is = unsignPositiveInf(strings.ToLower(is))
return NewStr(fmt.Sprintf("%s%s%s%sj%s", pre, rs, sign, is, post)).ToObject(), nil
}
func complexRFloorDiv(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivModOp(f, "__rfloordiv__", v, w, func(v, w complex128) (complex128, bool) {
if v == 0 {
return 0, false
}
return complexFloorDivOp(w, v), true
})
}
func complexRMod(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexDivModOp(f, "__rmod__", v, w, func(v, w complex128) (complex128, bool) {
if v == 0 {
return 0, false
}
return complexModOp(w, v), true
})
}
func complexRMul(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__rmul__", v, w, func(lhs, rhs complex128) complex128 {
return rhs * lhs
})
}
func complexRPow(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__rpow__", v, w, func(lhs, rhs complex128) complex128 {
return cmplx.Pow(rhs, lhs)
})
}
func complexRSub(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__rsub__", v, w, func(lhs, rhs complex128) complex128 {
return rhs - lhs
})
}
func complexSub(f *Frame, v, w *Object) (*Object, *BaseException) {
return complexArithmeticOp(f, "__sub__", v, w, func(lhs, rhs complex128) complex128 {
return lhs - rhs
})
}
func initComplexType(dict map[string]*Object) {
ComplexType.slots.Abs = &unaryOpSlot{complexAbs}
ComplexType.slots.Add = &binaryOpSlot{complexAdd}
ComplexType.slots.Complex = &unaryOpSlot{complexComplex}
ComplexType.slots.Div = &binaryOpSlot{complexDiv}
ComplexType.slots.DivMod = &binaryOpSlot{complexDivMod}
ComplexType.slots.Eq = &binaryOpSlot{complexEq}
ComplexType.slots.FloorDiv = &binaryOpSlot{complexFloorDiv}
ComplexType.slots.GE = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.GT = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.Hash = &unaryOpSlot{complexHash}
ComplexType.slots.LE = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.LT = &binaryOpSlot{complexCompareNotSupported}
ComplexType.slots.Mod = &binaryOpSlot{complexMod}
ComplexType.slots.Mul = &binaryOpSlot{complexMul}
ComplexType.slots.NE = &binaryOpSlot{complexNE}
ComplexType.slots.Neg = &unaryOpSlot{complexNeg}
ComplexType.slots.New = &newSlot{complexNew}
ComplexType.slots.NonZero = &unaryOpSlot{complexNonZero}
ComplexType.slots.Pos = &unaryOpSlot{complexPos}
ComplexType.slots.Pow = &binaryOpSlot{complexPow}
ComplexType.slots.RAdd = &binaryOpSlot{complexRAdd}
ComplexType.slots.RDiv = &binaryOpSlot{complexRDiv}
ComplexType.slots.RDivMod = &binaryOpSlot{complexRDivMod}
ComplexType.slots.RFloorDiv = &binaryOpSlot{complexRFloorDiv}
ComplexType.slots.Repr = &unaryOpSlot{complexRepr}
ComplexType.slots.RMod = &binaryOpSlot{complexRMod}
ComplexType.slots.RMul = &binaryOpSlot{complexRMul}
ComplexType.slots.RPow = &binaryOpSlot{complexRPow}
ComplexType.slots.RSub = &binaryOpSlot{complexRSub}
ComplexType.slots.Sub = &binaryOpSlot{complexSub}
}
func complex128Convert(f *Frame, o *Object) (complex128, *BaseException) {
if complexSlot := o.typ.slots.Complex; complexSlot != nil {
c, raised := complexConvert(complexSlot, f, o)
if raised != nil {
return complex(0, 0), raised
}
return c.Value(), nil
} else if floatSlot := o.typ.slots.Float; floatSlot != nil {
result, raised := floatConvert(floatSlot, f, o)
if raised != nil {
return complex(0, 0), raised
}
return complex(result.Value(), 0), nil
} else {
return complex(0, 0), f.RaiseType(TypeErrorType, "complex() argument must be a string or a number")
}
}
func complexArithmeticOp(f *Frame, method string, v, w *Object, fun func(v, w complex128) complex128) (*Object, *BaseException) {
if w.isInstance(ComplexType) {
return NewComplex(fun(toComplexUnsafe(v).Value(), toComplexUnsafe(w).Value())).ToObject(), nil
}
floatW, ok := floatCoerce(w)
if !ok {
if math.IsInf(floatW, 0) {
return nil, f.RaiseType(OverflowErrorType, "long int too large to convert to float")
}
return NotImplemented, nil
}
return NewComplex(fun(toComplexUnsafe(v).Value(), complex(floatW, 0))).ToObject(), nil
}
// complexCoerce will coerce any numeric type to a complex. If all is
// well, it will return the complex128 value, and true (OK). If an overflow
// occurs, it will return either (+Inf, false) or (-Inf, false) depending
// on whether the source value was too large or too small. Note that if the
// source number is an infinite float, the result will be infinite without
// overflow, (+-Inf, true).
// If the input is not a number, it will return (0, false).
func complexCoerce(o *Object) (complex128, bool) {
if o.isInstance(ComplexType) {
return toComplexUnsafe(o).Value(), true
}
floatO, ok := floatCoerce(o)
if !ok {
if math.IsInf(floatO, 0) {
return complex(floatO, 0.0), false
}
return 0, false
}
return complex(floatO, 0.0), true
}
func complexCompare(v *Complex, w *Object) (bool, bool) {
lhsr := real(v.Value())
rhs, ok := complexCoerce(w)
if !ok {
return false, false
}
return lhsr == real(rhs) && imag(v.Value()) == imag(rhs), true
}
func complexConvert(complexSlot *unaryOpSlot, f *Frame, o *Object) (*Complex, *BaseException) {
result, raised := complexSlot.Fn(f, o)
if raised != nil {
return nil, raised
}
if !result.isInstance(ComplexType) {
exc := fmt.Sprintf("__complex__ returned non-complex (type %s)", result.typ.Name())
return nil, f.RaiseType(TypeErrorType, exc)
}
return toComplexUnsafe(result), nil
}
func complexDivModOp(f *Frame, method string, v, w *Object, fun func(v, w complex128) (complex128, bool)) (*Object, *BaseException) {
complexW, ok := complexCoerce(w)
if !ok {
if cmplx.IsInf(complexW) {
return nil, f.RaiseType(OverflowErrorType, "long int too large to convert to complex")
}
return NotImplemented, nil
}
x, ok := fun(toComplexUnsafe(v).Value(), complexW)
if !ok {
return nil, f.RaiseType(ZeroDivisionErrorType, "complex division or modulo by zero")
}
return NewComplex(x).ToObject(), nil
}
func complexDivAndModOp(f *Frame, method string, v, w *Object, fun func(v, w complex128) (complex128, complex128, bool)) (*Object, *BaseException) {
complexW, ok := complexCoerce(w)
if !ok {
if cmplx.IsInf(complexW) {
return nil, f.RaiseType(OverflowErrorType, "long int too large to convert to complex")
}
return NotImplemented, nil
}
q, m, ok := fun(toComplexUnsafe(v).Value(), complexW)
if !ok {
return nil, f.RaiseType(ZeroDivisionErrorType, "complex division or modulo by zero")
}
return NewTuple2(NewComplex(q).ToObject(), NewComplex(m).ToObject()).ToObject(), nil
}
func complexFloorDivOp(v, w complex128) complex128 {
return complex(math.Floor(real(v/w)), 0)
}
func complexModOp(v, w complex128) complex128 {
return v - complexFloorDivOp(v, w)*w
}
const (
blank = iota
real1
imag1
real2
sign2
imag3
real4
sign5
onlyJ
)
// ParseComplex converts the string s to a complex number.
// If string is well-formed (one of these forms: <float>, <float>j,
// <float><signed-float>j, <float><sign>j, <sign>j or j, where <float> is
// any numeric string that's acceptable by strconv.ParseFloat(s, 64)),
// ParseComplex returns the respective complex128 number.
func parseComplex(s string) (complex128, error) {
c := strings.Count(s, "(")
if (c > 1) || (c == 1 && strings.Count(s, ")") != 1) {
return complex(0, 0), errors.New("Malformed complex string, more than one matching parantheses")
}
ts := strings.TrimSpace(s)
ts = strings.Trim(ts, "()")
ts = strings.TrimSpace(ts)
re := `(?i)(?:(?:(?:(?:\d*\.\d+)|(?:\d+\.?))(?:[Ee][+-]?\d+)?)|(?:infinity)|(?:nan)|(?:inf))`
fre := `[-+]?` + re
sre := `[-+]` + re
fsfj := `(?:(?P<real1>` + fre + `)(?P<imag1>` + sre + `)j)`
fsj := `(?:(?P<real2>` + fre + `)(?P<sign2>[-+])j)`
fj := `(?P<imag3>` + fre + `)j`
f := `(?P<real4>` + fre + `)`
sj := `(?P<sign5>[-+])j`
j := `(?P<onlyJ>j)`
r := regexp.MustCompile(`^(?:` + fsfj + `|` + fsj + `|` + fj + `|` + f + `|` + sj + `|` + j + `)$`)
subs := r.FindStringSubmatch(ts)
if subs == nil {
return complex(0, 0), errors.New("Malformed complex string, no mathing pattern found")
}
if subs[real1] != "" && subs[imag1] != "" {
r, _ := strconv.ParseFloat(unsignNaN(subs[real1]), 64)
i, err := strconv.ParseFloat(unsignNaN(subs[imag1]), 64)
return complex(r, i), err
}
if subs[real2] != "" && subs[sign2] != "" {
r, err := strconv.ParseFloat(unsignNaN(subs[real2]), 64)
if subs[sign2] == "-" {
return complex(r, -1), err
}
return complex(r, 1), err
}
if subs[imag3] != "" {
i, err := strconv.ParseFloat(unsignNaN(subs[imag3]), 64)
return complex(0, i), err
}
if subs[real4] != "" {
r, err := strconv.ParseFloat(unsignNaN(subs[real4]), 64)
return complex(r, 0), err
}
if subs[sign5] != "" {
if subs[sign5] == "-" {
return complex(0, -1), nil
}
return complex(0, 1), nil
}
if subs[onlyJ] != "" {
return complex(0, 1), nil
}
return complex(0, 0), errors.New("Malformed complex string")
}
func unsignNaN(s string) string {
ls := strings.ToLower(s)
if ls == "-nan" || ls == "+nan" {
return "nan"
}
return s
}