-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathPyComplex.java
More file actions
969 lines (826 loc) · 30.3 KB
/
PyComplex.java
File metadata and controls
969 lines (826 loc) · 30.3 KB
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
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
// Copyright (c) Corporation for National Research Initiatives
// Copyright (c) Jython Developers
package org.python.core;
import org.python.core.stringlib.FloatFormatter;
import org.python.core.stringlib.InternalFormat;
import org.python.core.stringlib.InternalFormat.Formatter;
import org.python.core.stringlib.InternalFormat.Spec;
import org.python.expose.ExposedGet;
import org.python.expose.ExposedMethod;
import org.python.expose.ExposedNew;
import org.python.expose.ExposedType;
import org.python.expose.MethodType;
/**
* A builtin python complex number
*/
@Untraversable
@ExposedType(name = "complex", doc = BuiltinDocs.complex_doc)
public class PyComplex extends PyObject {
public static final PyType TYPE = PyType.fromClass(PyComplex.class);
/** Format specification used by repr(). */
static final Spec SPEC_REPR = InternalFormat.fromText(" >r"); // but also minFracDigits=0
/** Format specification used by str() and none-format. (As CPython, but is that right?) */
static final Spec SPEC_STR = InternalFormat.fromText(" >.12g");
static PyComplex J = new PyComplex(0, 1.);
public static final PyComplex Inf = new PyComplex(Double.POSITIVE_INFINITY,
Double.POSITIVE_INFINITY);
public static final PyComplex NaN = new PyComplex(Double.NaN, Double.NaN);
@ExposedGet(doc = BuiltinDocs.complex_real_doc)
public double real;
@ExposedGet(doc = BuiltinDocs.complex_imag_doc)
public double imag;
public PyComplex(PyType subtype, double r, double i) {
super(subtype);
real = r;
imag = i;
}
public PyComplex(double r, double i) {
this(TYPE, r, i);
}
public PyComplex(double r) {
this(r, 0.0);
}
@ExposedNew
public static PyObject complex_new(PyNewWrapper new_, boolean init, PyType subtype,
PyObject[] args, String[] keywords) {
ArgParser ap = new ArgParser("complex", args, keywords, "real", "imag");
PyObject real = ap.getPyObject(0, Py.Zero);
PyObject imag = ap.getPyObject(1, null);
// Special-case for single argument that is already complex
if (real.getType() == TYPE && new_.for_type == subtype && imag == null) {
return real;
} else if (real instanceof PyString) {
if (imag != null) {
throw Py.TypeError("complex() can't take second arg if first is a string");
}
return real.__complex__();
} else if (imag != null && imag instanceof PyString) {
throw Py.TypeError("complex() second arg can't be a string");
}
try {
real = real.__complex__();
} catch (PyException pye) {
if (!pye.match(Py.AttributeError)) {
// __complex__ not supported
throw pye;
}
// otherwise try other means
}
PyComplex complexReal;
PyComplex complexImag;
PyFloat toFloat = null;
if (real instanceof PyComplex) {
complexReal = (PyComplex)real;
} else {
try {
toFloat = real.__float__();
} catch (PyException pye) {
if (pye.match(Py.AttributeError)) {
// __float__ not supported
throw Py.TypeError("complex() argument must be a string or a number");
}
throw pye;
}
complexReal = new PyComplex(toFloat.getValue());
}
if (imag == null) {
complexImag = new PyComplex(0.0);
} else if (imag instanceof PyComplex) {
complexImag = (PyComplex)imag;
} else {
toFloat = null;
try {
toFloat = imag.__float__();
} catch (PyException pye) {
if (pye.match(Py.AttributeError)) {
// __float__ not supported
throw Py.TypeError("complex() argument must be a string or a number");
}
throw pye;
}
complexImag = new PyComplex(toFloat.getValue());
}
complexReal.real -= complexImag.imag;
if (complexReal.imag == 0.0) {
// necessary if complexImag is -0.0, given that adding 0.0 + -0.0 is 0.0
complexReal.imag = complexImag.real;
} else {
complexReal.imag += complexImag.real;
}
if (new_.for_type != subtype) {
complexReal = new PyComplexDerived(subtype, complexReal.real, complexReal.imag);
}
return complexReal;
}
public final PyFloat getReal() {
return Py.newFloat(real);
}
public final PyFloat getImag() {
return Py.newFloat(imag);
}
public static String toString(double value) {
if (value == Math.floor(value) && value <= Long.MAX_VALUE && value >= Long.MIN_VALUE) {
return Long.toString((long)value);
} else {
return Double.toString(value);
}
}
@Override
public String toString() {
return __str__().toString();
}
@Override
public PyString __str__() {
return complex___str__();
}
@ExposedMethod(doc = BuiltinDocs.complex___str___doc)
final PyString complex___str__() {
return Py.newString(formatComplex(SPEC_STR));
}
@Override
public PyString __repr__() {
return complex___repr__();
}
@ExposedMethod(doc = BuiltinDocs.complex___repr___doc)
final PyString complex___repr__() {
return Py.newString(formatComplex(SPEC_REPR));
}
/**
* Format this complex according to the specification passed in. Supports <code>__str__</code>
* and <code>__repr__</code>, and none-format.
* <p>
* In general, the output is surrounded in parentheses, like <code>"(12.34+24.68j)"</code>.
* However, if the real part is zero (but not negative zero), only the imaginary part is
* printed, and without parentheses like <code>"24.68j"</code>. The number format specification
* passed in is used for the real and imaginary parts individually, with padding to width
* afterwards (if the specification requires it).
*
* @param spec parsed format specification string
* @return formatted value
*/
private String formatComplex(Spec spec) {
int size = 2 * FloatFormatter.size(spec) + 3; // 2 floats + "(j)"
FloatFormatter f = new FloatFormatter(new StringBuilder(size), spec);
f.setBytes(true);
// Even in r-format, complex strips *all* the trailing zeros.
f.setMinFracDigits(0);
if (Double.doubleToLongBits(real) == 0L) {
// Real part is truly zero: show no real part.
f.format(imag).append('j');
} else {
f.append('(').format(real).format(imag, "+").append("j)");
}
return f.pad().getResult();
}
@Override
public int hashCode() {
return complex___hash__();
}
@ExposedMethod(doc = BuiltinDocs.complex___hash___doc)
final int complex___hash__() {
if (imag == 0) {
return new PyFloat(real).hashCode();
} else {
long v = Double.doubleToLongBits(real) ^ Double.doubleToLongBits(imag);
return (int)v ^ (int)(v >> 32);
}
}
@Override
public boolean __nonzero__() {
return complex___nonzero__();
}
@ExposedMethod(doc = BuiltinDocs.complex___nonzero___doc)
final boolean complex___nonzero__() {
return real != 0 || imag != 0;
}
@Override
public int __cmp__(PyObject other) {
if (!canCoerce(other)) {
return -2;
}
PyComplex c = coerce(other);
double oreal = c.real;
double oimag = c.imag;
if (real == oreal && imag == oimag) {
return 0;
}
if (real != oreal) {
return real < oreal ? -1 : 1;
} else {
return imag < oimag ? -1 : 1;
}
}
@Override
public PyObject __eq__(PyObject other) {
return complex___eq__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___eq___doc)
final PyObject complex___eq__(PyObject other) {
switch (eq_helper(other)) {
case 0:
return Py.False;
case 1:
return Py.True;
default:
return null;
}
}
/**
* Helper for {@link #complex___eq__(PyObject)} and {@link #complex___ne__(PyObject)}.
*
* @param other to compare for equality with this
* @return 0 = false, 1 = true, 2 = don't know (ask the <code>other</code> object)
*/
private int eq_helper(PyObject other) {
// We only deal with primitive types here. All others delegate upwards (return 2).
boolean equal;
if (other instanceof PyComplex) {
PyComplex c = ((PyComplex)other);
equal = (this.real == c.real && this.imag == c.imag);
} else if (other instanceof PyFloat) {
PyFloat f = ((PyFloat)other);
equal = (this.imag == 0.0 && this.real == f.getValue());
} else if (other instanceof PyInteger || other instanceof PyLong) {
if (this.imag == 0.0) {
// The imaginary part is zero: other object primitive might equal the real part.
double r = this.real;
if (Double.isInfinite(r) || Double.isNaN(r)) {
// No integer primitive type can be infinite, and NaN never equals anything.
equal = false;
} else {
// Delegate the logic to PyFloat
PyFloat f = new PyFloat(r);
equal = (f.float___cmp__(other) == 0);
}
} else {
// No other primitive can have an imaginary part.
equal = false;
}
} else {
// other is not one of the types we know how to deal with.
return 2;
}
// Only "known" cases end here: translate to return code
return equal ? 1 : 0;
}
@Override
public PyObject __ne__(PyObject other) {
return complex___ne__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___ne___doc)
final PyObject complex___ne__(PyObject other) {
switch (eq_helper(other)) {
case 0:
return Py.True;
case 1:
return Py.False;
default:
return null;
}
}
private PyObject unsupported_comparison(PyObject other) {
if (!canCoerce(other)) {
return null;
}
throw Py.TypeError("cannot compare complex numbers using <, <=, >, >=");
}
@Override
public PyObject __ge__(PyObject other) {
return complex___ge__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___ge___doc)
final PyObject complex___ge__(PyObject other) {
return unsupported_comparison(other);
}
@Override
public PyObject __gt__(PyObject other) {
return complex___gt__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___gt___doc)
final PyObject complex___gt__(PyObject other) {
return unsupported_comparison(other);
}
@Override
public PyObject __le__(PyObject other) {
return complex___le__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___le___doc)
final PyObject complex___le__(PyObject other) {
return unsupported_comparison(other);
}
@Override
public PyObject __lt__(PyObject other) {
return complex___lt__(other);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___lt___doc)
final PyObject complex___lt__(PyObject other) {
return unsupported_comparison(other);
}
@Override
public Object __coerce_ex__(PyObject other) {
return complex___coerce_ex__(other);
}
@ExposedMethod(doc = BuiltinDocs.complex___coerce___doc)
final PyObject complex___coerce__(PyObject other) {
return adaptToCoerceTuple(complex___coerce_ex__(other));
}
/**
* Coercion logic for complex. Implemented as a final method to avoid invocation of virtual
* methods from the exposed coerce.
*/
final PyObject complex___coerce_ex__(PyObject other) {
if (other instanceof PyComplex) {
return other;
} else if (other instanceof PyFloat) {
return new PyComplex(((PyFloat)other).getValue(), 0);
} else if (other instanceof PyInteger) {
return new PyComplex(((PyInteger)other).getValue(), 0);
} else if (other instanceof PyLong) {
return new PyComplex(((PyLong)other).doubleValue(), 0);
}
return Py.None;
}
private final boolean canCoerce(PyObject other) {
return other instanceof PyComplex || other instanceof PyFloat || other instanceof PyInteger
|| other instanceof PyLong;
}
private final PyComplex coerce(PyObject other) {
if (other instanceof PyComplex) {
return (PyComplex)other;
} else if (other instanceof PyFloat) {
return new PyComplex(((PyFloat)other).getValue(), 0);
} else if (other instanceof PyInteger) {
return new PyComplex(((PyInteger)other).getValue(), 0);
} else if (other instanceof PyLong) {
return new PyComplex(((PyLong)other).doubleValue(), 0);
}
throw Py.TypeError("xxx");
}
@Override
public PyObject __add__(PyObject right) {
return complex___add__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___add___doc)
final PyObject complex___add__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
PyComplex c = coerce(right);
return new PyComplex(real + c.real, imag + c.imag);
}
@Override
public PyObject __radd__(PyObject left) {
return complex___radd__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___radd___doc)
final PyObject complex___radd__(PyObject left) {
return __add__(left);
}
private final static PyObject _sub(PyComplex o1, PyComplex o2) {
return new PyComplex(o1.real - o2.real, o1.imag - o2.imag);
}
@Override
public PyObject __sub__(PyObject right) {
return complex___sub__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___sub___doc)
final PyObject complex___sub__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
return _sub(this, coerce(right));
}
@Override
public PyObject __rsub__(PyObject left) {
return complex___rsub__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rsub___doc)
final PyObject complex___rsub__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _sub(coerce(left), this);
}
private final static PyObject _mul(PyComplex o1, PyComplex o2) {
return new PyComplex(o1.real * o2.real - o1.imag * o2.imag, o1.real * o2.imag + o1.imag
* o2.real);
}
@Override
public PyObject __mul__(PyObject right) {
return complex___mul__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___mul___doc)
final PyObject complex___mul__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
return _mul(this, coerce(right));
}
@Override
public PyObject __rmul__(PyObject left) {
return complex___rmul__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rmul___doc)
final PyObject complex___rmul__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _mul(coerce(left), this);
}
private final static PyObject _div(PyComplex a, PyComplex b) {
double abs_breal = b.real < 0 ? -b.real : b.real;
double abs_bimag = b.imag < 0 ? -b.imag : b.imag;
if (abs_breal >= abs_bimag) {
// Divide tops and bottom by b.real
if (abs_breal == 0.0) {
throw Py.ZeroDivisionError("complex division");
}
double ratio = b.imag / b.real;
double denom = b.real + b.imag * ratio;
return new PyComplex((a.real + a.imag * ratio) / denom, //
(a.imag - a.real * ratio) / denom);
} else {
/* divide tops and bottom by b.imag */
double ratio = b.real / b.imag;
double denom = b.real * ratio + b.imag;
return new PyComplex((a.real * ratio + a.imag) / denom, //
(a.imag * ratio - a.real) / denom);
}
}
@Override
public PyObject __div__(PyObject right) {
return complex___div__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___div___doc)
final PyObject complex___div__(PyObject right) {
if (!canCoerce(right)) {
return null;
} else if (Options.division_warning >= 2) {
Py.warning(Py.DeprecationWarning, "classic complex division");
}
return _div(this, coerce(right));
}
@Override
public PyObject __rdiv__(PyObject left) {
return complex___rdiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rdiv___doc)
final PyObject complex___rdiv__(PyObject left) {
if (!canCoerce(left)) {
return null;
} else if (Options.division_warning >= 2) {
Py.warning(Py.DeprecationWarning, "classic complex division");
}
return _div(coerce(left), this);
}
@Override
public PyObject __floordiv__(PyObject right) {
return complex___floordiv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___floordiv___doc)
final PyObject complex___floordiv__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
return _divmod(this, coerce(right)).__finditem__(0);
}
@Override
public PyObject __rfloordiv__(PyObject left) {
return complex___rfloordiv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rfloordiv___doc)
final PyObject complex___rfloordiv__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _divmod(coerce(left), this).__finditem__(0);
}
// Special case __tojava__ for bug 1605, since we broke it with our support for faux floats.
@Override
public Object __tojava__(Class<?> c) {
if (c.isInstance(this)) {
return this;
}
return Py.NoConversion;
}
@Override
public PyObject __truediv__(PyObject right) {
return complex___truediv__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___truediv___doc)
final PyObject complex___truediv__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
return _div(this, coerce(right));
}
@Override
public PyObject __rtruediv__(PyObject left) {
return complex___rtruediv__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rtruediv___doc)
final PyObject complex___rtruediv__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _div(coerce(left), this);
}
@Override
public PyObject __mod__(PyObject right) {
return complex___mod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___mod___doc)
final PyObject complex___mod__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
return _mod(this, coerce(right));
}
@Override
public PyObject __rmod__(PyObject left) {
return complex___rmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rmod___doc)
final PyObject complex___rmod__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _mod(coerce(left), this);
}
private static PyObject _mod(PyComplex value, PyComplex right) {
Py.warning(Py.DeprecationWarning, "complex divmod(), // and % are deprecated");
PyComplex z = (PyComplex)_div(value, right);
z.real = Math.floor(z.real);
z.imag = 0.0;
return value.__sub__(z.__mul__(right));
}
@Override
public PyObject __divmod__(PyObject right) {
return complex___divmod__(right);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___divmod___doc)
final PyObject complex___divmod__(PyObject right) {
if (!canCoerce(right)) {
return null;
}
return _divmod(this, coerce(right));
}
@Override
public PyObject __rdivmod__(PyObject left) {
return complex___rdivmod__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rdivmod___doc)
final PyObject complex___rdivmod__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _divmod(coerce(left), this);
}
private static PyObject _divmod(PyComplex value, PyComplex right) {
Py.warning(Py.DeprecationWarning, "complex divmod(), // and % are deprecated");
PyComplex z = (PyComplex)_div(value, right);
z.real = Math.floor(z.real);
z.imag = 0.0;
return new PyTuple(z, value.__sub__(z.__mul__(right)));
}
private static PyObject ipow(PyComplex value, int iexp) {
int pow = iexp;
if (pow < 0) {
pow = -pow;
}
double xr = value.real;
double xi = value.imag;
double zr = 1;
double zi = 0;
double tmp;
while (pow > 0) {
if ((pow & 0x1) != 0) {
tmp = zr * xr - zi * xi;
zi = zi * xr + zr * xi;
zr = tmp;
}
pow >>= 1;
if (pow == 0) {
break;
}
tmp = xr * xr - xi * xi;
xi = xr * xi * 2;
xr = tmp;
}
PyComplex ret = new PyComplex(zr, zi);
if (iexp < 0) {
return new PyComplex(1, 0).__div__(ret);
}
return ret;
}
@Override
public PyObject __pow__(PyObject right, PyObject modulo) {
return complex___pow__(right, modulo);
}
@ExposedMethod(type = MethodType.BINARY, defaults = "null",
doc = BuiltinDocs.complex___pow___doc)
final PyObject complex___pow__(PyObject right, PyObject modulo) {
if (modulo != null) {
throw Py.ValueError("complex modulo");
} else if (!canCoerce(right)) {
return null;
}
return _pow(this, coerce(right));
}
@Override
public PyObject __rpow__(PyObject left) {
return complex___rpow__(left);
}
@ExposedMethod(type = MethodType.BINARY, doc = BuiltinDocs.complex___rpow___doc)
final PyObject complex___rpow__(PyObject left) {
if (!canCoerce(left)) {
return null;
}
return _pow(coerce(left), this);
}
public static PyObject _pow(PyComplex value, PyComplex right) {
double xr = value.real;
double xi = value.imag;
double yr = right.real;
double yi = right.imag;
if (yr == 0 && yi == 0) {
return new PyComplex(1, 0);
}
if (xr == 0 && xi == 0) {
if (yi != 0 || yr < 0) {
throw Py.ZeroDivisionError("0.0 to a negative or complex power");
}
}
// Check for integral powers
int iexp = (int)yr;
if (yi == 0 && yr == iexp && iexp >= -128 && iexp <= 128) {
return ipow(value, iexp);
}
double abs = Math.hypot(xr, xi);
double len = Math.pow(abs, yr);
double at = Math.atan2(xi, xr);
double phase = at * yr;
if (yi != 0) {
len /= Math.exp(at * yi);
phase += yi * Math.log(abs);
}
return new PyComplex(len * Math.cos(phase), len * Math.sin(phase));
}
@Override
public PyObject __neg__() {
return complex___neg__();
}
@ExposedMethod(doc = BuiltinDocs.complex___neg___doc)
final PyObject complex___neg__() {
return new PyComplex(-real, -imag);
}
@Override
public PyObject __pos__() {
return complex___pos__();
}
@ExposedMethod(doc = BuiltinDocs.complex___pos___doc)
final PyObject complex___pos__() {
return getType() == TYPE ? this : new PyComplex(real, imag);
}
@Override
public PyObject __invert__() {
throw Py.TypeError("bad operand type for unary ~");
}
@Override
public PyObject __abs__() {
return complex___abs__();
}
@ExposedMethod(doc = BuiltinDocs.complex___abs___doc)
final PyObject complex___abs__() {
double mag = Math.hypot(real, imag);
if (Double.isInfinite(mag) && !(Double.isInfinite(real) || Double.isInfinite(imag))) {
// In these circumstances Math.hypot quietly returns inf, but Python should raise.
throw Py.OverflowError("absolute value too large");
}
return new PyFloat(mag);
}
@Override
public PyObject __int__() {
return complex___int__();
}
@ExposedMethod(doc = BuiltinDocs.complex___int___doc)
final PyInteger complex___int__() {
throw Py.TypeError("can't convert complex to int; use e.g. int(abs(z))");
}
@Override
public PyObject __long__() {
return complex___long__();
}
@ExposedMethod(doc = BuiltinDocs.complex___long___doc)
final PyObject complex___long__() {
throw Py.TypeError("can't convert complex to long; use e.g. long(abs(z))");
}
@Override
public PyFloat __float__() {
return complex___float__();
}
@ExposedMethod(doc = BuiltinDocs.complex___float___doc)
final PyFloat complex___float__() {
throw Py.TypeError("can't convert complex to float; use e.g. abs(z)");
}
@Override
public PyComplex __complex__() {
return new PyComplex(real, imag);
}
@Override
public PyComplex conjugate() {
return complex_conjugate();
}
@ExposedMethod(doc = BuiltinDocs.complex_conjugate_doc)
final PyComplex complex_conjugate() {
return new PyComplex(real, -imag);
}
@ExposedMethod(doc = BuiltinDocs.complex___getnewargs___doc)
final PyTuple complex___getnewargs__() {
return new PyTuple(new PyFloat(real), new PyFloat(imag));
}
@Override
public PyTuple __getnewargs__() {
return complex___getnewargs__();
}
@Override
public PyObject __format__(PyObject formatSpec) {
return complex___format__(formatSpec);
}
@ExposedMethod(doc = BuiltinDocs.complex___format___doc)
final PyObject complex___format__(PyObject formatSpec) {
// Parse the specification
Spec spec = InternalFormat.fromText(formatSpec, "__format__");
// fromText will have thrown if formatSpecStr is not a PyString (including PyUnicode)
PyString formatSpecStr = (PyString)formatSpec;
String result;
// Validate the specification and detect the special case for none-format
switch (checkSpecification(spec)) {
case 0: // None-format
// In none-format, we take the default type and precision from __str__.
spec = spec.withDefaults(SPEC_STR);
// And then we use the __str__ mechanism to get parentheses or real 0 elision.
result = formatComplex(spec);
break;
case 1: // Floating-point formats
// In any other format, defaults are those commonly used for numeric formats.
spec = spec.withDefaults(Spec.NUMERIC);
int size = 2 * FloatFormatter.size(spec) + 1; // 2 floats + "j"
FloatFormatter f = new FloatFormatter(new StringBuilder(size), spec);
f.setBytes(!(formatSpecStr instanceof PyUnicode));
// Convert both parts as per specification
f.format(real).format(imag, "+").append('j');
result = f.pad().getResult();
break;
default: // The type code was not recognised
throw Formatter.unknownFormat(spec.type, "complex");
}
// Wrap the result in the same type as the format string
return formatSpecStr.createInstance(result);
}
/**
* Validate a parsed specification, for <code>PyComplex</code>, returning 0 if it is a valid
* none-format specification, 1 if it is a valid float specification, and some other value if it
* not a valid type. If it has any other faults (e.g. alternate form was specified) the method
* raises a descriptive exception.
*
* @param spec a parsed PEP-3101 format specification.
* @return 0, 1, or other value for none-format, a float format, or incorrect type.
* @throws PyException {@code ValueError} if the specification is faulty.
*/
@SuppressWarnings("fallthrough")
private static int checkSpecification(Spec spec) {
// Slight differences between format types
switch (spec.type) {
case 'n':
if (spec.grouping) {
throw Formatter.notAllowed("Grouping", "complex", spec.type);
}
// Fall through
case Spec.NONE:
case 'e':
case 'f':
case 'g':
case 'E':
case 'F':
case 'G':
// Check for disallowed parts of the specification
if (spec.alternate) {
throw FloatFormatter.alternateFormNotAllowed("complex");
} else if (spec.fill == '0') {
throw FloatFormatter.zeroPaddingNotAllowed("complex");
} else if (spec.align == '=') {
throw FloatFormatter.alignmentNotAllowed('=', "complex");
} else {
return (spec.type == Spec.NONE) ? 0 : 1;
}
default:
// spec.type is invalid for complex
return 2;
}
}
@Override
public boolean isNumberType() {
return true;
}
}