/
qqbar.jl
1541 lines (1273 loc) · 45.1 KB
/
qqbar.jl
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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
###############################################################################
#
# qqbar.jl : Calcium algebraic numbers in minimal polynomial representation
#
###############################################################################
###############################################################################
#
# Data type and parent methods
#
###############################################################################
parent(a::QQBarFieldElem) = CalciumQQBar
parent_type(::Type{QQBarFieldElem}) = QQBarField
elem_type(::Type{QQBarField}) = QQBarFieldElem
base_ring(a::QQBarField) = CalciumQQBar
base_ring(a::QQBarFieldElem) = CalciumQQBar
is_domain_type(::Type{QQBarFieldElem}) = true
check_parent(a::QQBarFieldElem, b::QQBarFieldElem, throw::Bool = true) = true
characteristic(::QQBarField) = 0
###############################################################################
#
# Hashing
#
###############################################################################
# todo: want a C function for this
function Base.hash(a::QQBarFieldElem, h::UInt)
R, x = polynomial_ring(ZZ, "x")
return xor(hash(minpoly(R, a)), h)
end
###############################################################################
#
# Constructors
#
###############################################################################
function QQBarFieldElem(a::Int)
z = QQBarFieldElem()
ccall((:qqbar_set_si, libcalcium), Nothing, (Ref{QQBarFieldElem}, Int, ), z, a)
return z
end
function QQBarFieldElem(a::Complex{Int})
r = QQBarFieldElem(real(a))
s = QQBarFieldElem(imag(a))
z = QQBarFieldElem()
ccall((:qqbar_set_re_im, libcalcium),
Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, r, s)
return z
end
function QQBarFieldElem(a::ZZRingElem)
z = QQBarFieldElem()
ccall((:qqbar_set_fmpz, libcalcium),
Nothing, (Ref{QQBarFieldElem}, Ref{ZZRingElem}, ), z, a)
return z
end
function QQBarFieldElem(a::QQFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_set_fmpq, libcalcium),
Nothing, (Ref{QQBarFieldElem}, Ref{QQFieldElem}, ), z, a)
return z
end
QQBarFieldElem(a::Rational) = QQBarFieldElem(QQFieldElem(a))
function deepcopy_internal(a::QQBarFieldElem, dict::IdDict)
z = QQBarFieldElem()
ccall((:qqbar_set, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
###############################################################################
#
# Canonicalisation
#
###############################################################################
canonical_unit(a::QQBarFieldElem) = a
###############################################################################
#
# AbstractString I/O
#
###############################################################################
# todo
# function expressify(a::QQBarFieldElem; context = nothing)
# end
#=
function qqbar_vec(n::Int)
return ccall((:_qqbar_vec_init, libcalcium), Ptr{qqbar_struct}, (Int,), n)
end
function array(R::QQBarField, v::Ptr{qqbar_struct}, n::Int)
r = Vector{QQBarFieldElem}(undef, n)
for i=1:n
r[i] = R()
ccall((:qqbar_set, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ptr{qqbar_struct}),
r[i], v + (i-1)*sizeof(qqbar_struct))
end
return r
end
function qqbar_vec_clear(v::Ptr{qqbar_struct}, n::Int)
ccall((:_qqbar_vec_clear, libcalcium),
Nothing, (Ptr{qqbar_struct}, Int), v, n)
end
function roots(R::QQBarField, f::ZZPolyRingElem)
deg = degree(f)
if deg <= 0
return Array{QQBarFieldElem}(undef, 0)
end
roots = qqbar_vec(deg)
ccall((:qqbar_roots_fmpz_poly, libcalcium),
Nothing, (Ptr{qqbar_struct}, Ref{ZZPolyRingElem}, Int), roots, f, 0)
res = array(R, roots, deg)
qqbar_vec_clear(roots, deg)
return res
end
=#
function native_string(x::QQBarFieldElem)
cstr = ccall((:qqbar_get_str_nd, libcalcium),
Ptr{UInt8}, (Ref{QQBarFieldElem}, Int), x, Int(6))
number = unsafe_string(cstr)
ccall((:flint_free, libflint), Nothing, (Ptr{UInt8},), cstr)
number = number[1:first(findfirst(" (", number))-1]
number = replace(number, "I" => "im")
R, Rx = polynomial_ring(ZZ, "x")
polynomial = string(minpoly(R, x))
polynomial = replace(polynomial, "*" => "")
res = string("Root ", number, " of ", polynomial)
return res
end
function show(io::IO, F::QQBarField)
if get(io, :supercompact, false)
io = pretty(io)
print(io, LowercaseOff(), "QQBar")
else
print(io, "Field of algebraic numbers")
end
end
function show(io::IO, x::QQBarFieldElem)
print(io, native_string(x))
end
###############################################################################
#
# Basic manipulation
#
###############################################################################
is_unit(x::QQBarFieldElem) = !is_zero(x)
zero(a::QQBarField) = a(0)
one(a::QQBarField) = a(1)
zero(::Type{QQBarFieldElem}) = CalciumQQBar(0)
one(::Type{QQBarFieldElem}) = CalciumQQBar(1)
@doc raw"""
degree(x::QQBarFieldElem)
Return the degree of the minimal polynomial of `x`.
"""
function degree(x::QQBarFieldElem)
return ccall((:qqbar_degree, libcalcium), Int, (Ref{QQBarFieldElem}, ), x)
end
@doc raw"""
iszero(x::QQBarFieldElem)
Return whether `x` is the number 0.
"""
function iszero(x::QQBarFieldElem)
return Bool(ccall((:qqbar_is_zero, libcalcium), Cint, (Ref{QQBarFieldElem},), x))
end
@doc raw"""
isone(x::QQBarFieldElem)
Return whether `x` is the number 1.
"""
function isone(x::QQBarFieldElem)
return Bool(ccall((:qqbar_is_one, libcalcium), Cint, (Ref{QQBarFieldElem},), x))
end
@doc raw"""
isinteger(x::QQBarFieldElem)
Return whether `x` is an integer.
"""
function isinteger(x::QQBarFieldElem)
return Bool(ccall((:qqbar_is_integer, libcalcium), Cint, (Ref{QQBarFieldElem},), x))
end
@doc raw"""
is_rational(x::QQBarFieldElem)
Return whether `x` is a rational number.
"""
function is_rational(x::QQBarFieldElem)
return Bool(ccall((:qqbar_is_rational, libcalcium), Cint, (Ref{QQBarFieldElem},), x))
end
@doc raw"""
isreal(x::QQBarFieldElem)
Return whether `x` is a real number.
"""
function isreal(x::QQBarFieldElem)
return Bool(ccall((:qqbar_is_real, libcalcium), Cint, (Ref{QQBarFieldElem},), x))
end
@doc raw"""
is_algebraic_integer(x::QQBarFieldElem)
Return whether `x` is an algebraic integer.
"""
function is_algebraic_integer(x::QQBarFieldElem)
return Bool(ccall((:qqbar_is_algebraic_integer, libcalcium),
Cint, (Ref{QQBarFieldElem},), x))
end
@doc raw"""
minpoly(R::ZZPolyRing, x::QQBarFieldElem)
Return the minimal polynomial of `x` as an element of the polynomial ring `R`.
"""
function minpoly(R::ZZPolyRing, x::QQBarFieldElem)
z = R()
ccall((:fmpz_poly_set, libflint),
Nothing, (Ref{ZZPolyRingElem}, Ref{QQBarFieldElem}, ), z, x)
return z
end
@doc raw"""
minpoly(R::ZZPolyRing, x::QQBarFieldElem)
Return the minimal polynomial of `x` as an element of the polynomial ring `R`.
"""
function minpoly(R::QQPolyRing, x::QQBarFieldElem)
z = R()
ccall((:fmpq_poly_set_fmpz_poly, libflint),
Nothing, (Ref{QQPolyRingElem}, Ref{QQBarFieldElem}, ), z, x)
return z
end
@doc raw"""
denominator(x::QQBarFieldElem)
Return the denominator of `x`, defined as the leading coefficient of the
minimal polynomial of `x`. The result is returned as an `ZZRingElem`.
"""
function denominator(x::QQBarFieldElem)
d = degree(x)
q = ZZRingElem()
ccall((:fmpz_poly_get_coeff_fmpz, libflint),
Nothing, (Ref{ZZRingElem}, Ref{QQBarFieldElem}, Int), q, x, d)
return q
end
@doc raw"""
numerator(x::QQBarFieldElem)
Return the numerator of `x`, defined as `x` multiplied by its denominator.
The result is an algebraic integer.
"""
function numerator(x::QQBarFieldElem)
return x * denominator(x)
end
@doc raw"""
height(x::QQBarFieldElem)
Return the height of the algebraic number `x`. The result is an `ZZRingElem` integer.
"""
function height(x::QQBarFieldElem)
z = ZZRingElem()
ccall((:qqbar_height, libcalcium), Nothing, (Ref{ZZRingElem}, Ref{QQBarFieldElem}, ), z, x)
return z
end
@doc raw"""
height_bits(x::QQBarFieldElem)
Return the height of the algebraic number `x` measured in bits.
The result is a Julia integer.
"""
function height_bits(x::QQBarFieldElem)
return ccall((:qqbar_height_bits, libcalcium), Int, (Ref{QQBarFieldElem}, ), x)
end
###############################################################################
#
# Random generation
#
###############################################################################
@doc raw"""
rand(R::QQBarField; degree::Int, bits::Int, randtype::Symbol=:null)
Return a random algebraic number with degree up to `degree`
and coefficients up to `bits` in size. By default, both real and
complex numbers are generated. Set the optional `randtype` to `:real` or
`:nonreal` to generate a specific type of number. Note that
nonreal numbers require `degree` at least 2.
"""
function rand(R::QQBarField; degree::Int, bits::Int,
randtype::Symbol=:null)
state = _flint_rand_states[Threads.threadid()]
x = R()
degree <= 0 && error("degree must be positive")
bits <= 0 && error("bits must be positive")
if randtype == :null
ccall((:qqbar_randtest, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ptr{Cvoid}, Int, Int), x, state.ptr, degree, bits)
elseif randtype == :real
ccall((:qqbar_randtest_real, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ptr{Cvoid}, Int, Int), x, state.ptr, degree, bits)
elseif randtype == :nonreal
degree < 2 && error("nonreal requires degree >= 2")
ccall((:qqbar_randtest_nonreal, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ptr{Cvoid}, Int, Int), x, state.ptr, degree, bits)
else
error("randtype not defined")
end
return x
end
###############################################################################
#
# Unary operators
#
###############################################################################
function -(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_neg, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
###############################################################################
#
# Binary operators
#
###############################################################################
function +(a::QQBarFieldElem, b::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_add, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function +(a::QQBarFieldElem, b::QQFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_add_fmpq, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQFieldElem}), z, a, b)
return z
end
function +(a::QQBarFieldElem, b::ZZRingElem)
z = QQBarFieldElem()
ccall((:qqbar_add_fmpz, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{ZZRingElem}), z, a, b)
return z
end
function +(a::QQBarFieldElem, b::Int)
z = QQBarFieldElem()
ccall((:qqbar_add_si, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Int), z, a, b)
return z
end
+(a::QQFieldElem, b::QQBarFieldElem) = b + a
+(a::ZZRingElem, b::QQBarFieldElem) = b + a
+(a::Int, b::QQBarFieldElem) = b + a
function -(a::QQBarFieldElem, b::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_sub, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function -(a::QQBarFieldElem, b::QQFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_sub_fmpq, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQFieldElem}), z, a, b)
return z
end
function -(a::QQBarFieldElem, b::ZZRingElem)
z = QQBarFieldElem()
ccall((:qqbar_sub_fmpz, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{ZZRingElem}), z, a, b)
return z
end
function -(a::QQBarFieldElem, b::Int)
z = QQBarFieldElem()
ccall((:qqbar_sub_si, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Int), z, a, b)
return z
end
function -(a::QQFieldElem, b::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_fmpq_sub, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQFieldElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function -(a::ZZRingElem, b::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_fmpz_sub, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{ZZRingElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function -(a::Int, b::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_si_sub, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Int, Ref{QQBarFieldElem}), z, a, b)
return z
end
function *(a::QQBarFieldElem, b::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_mul, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function *(a::QQBarFieldElem, b::QQFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_mul_fmpq, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQFieldElem}), z, a, b)
return z
end
function *(a::QQBarFieldElem, b::ZZRingElem)
z = QQBarFieldElem()
ccall((:qqbar_mul_fmpz, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{ZZRingElem}), z, a, b)
return z
end
function *(a::QQBarFieldElem, b::Int)
z = QQBarFieldElem()
ccall((:qqbar_mul_si, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Int), z, a, b)
return z
end
*(a::QQFieldElem, b::QQBarFieldElem) = b * a
*(a::ZZRingElem, b::QQBarFieldElem) = b * a
*(a::Int, b::QQBarFieldElem) = b * a
function ^(a::QQBarFieldElem, b::QQBarFieldElem)
z = QQBarFieldElem()
ok = Bool(ccall((:qqbar_pow, libcalcium), Cint,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a, b))
!ok && throw(DomainError((a, b)))
return z
end
# todo: want qqbar_pow_fmpz, qqbar_pow_fmpq, qqbar_pow_si
^(a::QQBarFieldElem, b::ZZRingElem) = a ^ QQBarFieldElem(b)
^(a::QQBarFieldElem, b::QQFieldElem) = a ^ QQBarFieldElem(b)
^(a::QQBarFieldElem, b::Int) = a ^ QQBarFieldElem(b)
^(a::ZZRingElem, b::QQBarFieldElem) = QQBarFieldElem(a) ^ b
^(a::QQFieldElem, b::QQBarFieldElem) = QQBarFieldElem(a) ^ b
^(a::Int, b::QQBarFieldElem) = QQBarFieldElem(a) ^ b
###############################################################################
#
# Exact division
#
###############################################################################
function inv(a::QQBarFieldElem)
iszero(a) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_inv, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
function divexact(a::QQBarFieldElem, b::QQBarFieldElem; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_div, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function divexact(a::QQBarFieldElem, b::QQFieldElem; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_div_fmpq, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{QQFieldElem}), z, a, b)
return z
end
function divexact(a::QQBarFieldElem, b::ZZRingElem; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_div_fmpz, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Ref{ZZRingElem}), z, a, b)
return z
end
function divexact(a::QQBarFieldElem, b::Int; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_div_si, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Int), z, a, b)
return z
end
function divexact(a::QQFieldElem, b::QQBarFieldElem; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_fmpq_div, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQFieldElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function divexact(a::ZZRingElem, b::QQBarFieldElem; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_fmpz_div, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{ZZRingElem}, Ref{QQBarFieldElem}), z, a, b)
return z
end
function divexact(a::Int, b::QQBarFieldElem; check::Bool=true)
iszero(b) && throw(DivideError())
z = QQBarFieldElem()
ccall((:qqbar_si_div, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Int, Ref{QQBarFieldElem}), z, a, b)
return z
end
//(a::QQBarFieldElem, b::QQBarFieldElem) = divexact(a, b)
//(a::QQBarFieldElem, b::QQFieldElem) = divexact(a, b)
//(a::QQBarFieldElem, b::ZZRingElem) = divexact(a, b)
//(a::QQBarFieldElem, b::Int) = divexact(a, b)
//(a::QQFieldElem, b::QQBarFieldElem) = divexact(a, b)
//(a::ZZRingElem, b::QQBarFieldElem) = divexact(a, b)
//(a::Int, b::QQBarFieldElem) = divexact(a, b)
function <<(a::QQBarFieldElem, b::Int)
z = QQBarFieldElem()
ccall((:qqbar_mul_2exp_si, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Int), z, a, b)
return z
end
function >>(a::QQBarFieldElem, b::Int)
z = QQBarFieldElem()
ccall((:qqbar_mul_2exp_si, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, Int), z, a, -b)
return z
end
###############################################################################
#
# Polynomial evaluation
#
###############################################################################
function evaluate(x::QQPolyRingElem, y::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_evaluate_fmpq_poly, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{QQPolyRingElem}, Ref{QQBarFieldElem}), z, x, y)
return z
end
function evaluate(x::ZZPolyRingElem, y::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_evaluate_fmpz_poly, libcalcium), Nothing,
(Ref{QQBarFieldElem}, Ref{ZZPolyRingElem}, Ref{QQBarFieldElem}), z, x, y)
return z
end
###############################################################################
#
# Comparison
#
###############################################################################
function ==(a::QQBarFieldElem, b::QQBarFieldElem)
return Bool(ccall((:qqbar_equal, libcalcium), Cint,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b))
end
function cmp(a::QQBarFieldElem, b::QQBarFieldElem)
!isreal(a) && throw(DomainError(a, "comparing nonreal numbers"))
!isreal(b) && throw(DomainError(b, "comparing nonreal numbers"))
return ccall((:qqbar_cmp_re, libcalcium), Cint,
(Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
end
isless(a::QQBarFieldElem, b::QQBarFieldElem) = cmp(a, b) < 0
isless(a::QQBarFieldElem, b::ZZRingElem) = isless(a, QQBarFieldElem(b))
isless(a::QQBarFieldElem, b::QQFieldElem) = isless(a, QQBarFieldElem(b))
isless(a::QQBarFieldElem, b::Int) = isless(a, QQBarFieldElem(b))
isless(a::QQFieldElem, b::QQBarFieldElem) = isless(QQBarFieldElem(a), b)
isless(a::ZZRingElem, b::QQBarFieldElem) = isless(QQBarFieldElem(a), b)
isless(a::Int, b::QQBarFieldElem) = isless(QQBarFieldElem(a), b)
is_positive(a::QQBarFieldElem) = a > 0
is_negative(a::QQBarFieldElem) = a < 0
# todo: export the cmp functions?
cmp_real(a::QQBarFieldElem, b::QQBarFieldElem) = ccall((:qqbar_cmp_re, libcalcium),
Cint, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
cmp_imag(a::QQBarFieldElem, b::QQBarFieldElem) = ccall((:qqbar_cmp_im, libcalcium),
Cint, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
cmpabs(a::QQBarFieldElem, b::QQBarFieldElem) = ccall((:qqbar_cmpabs, libcalcium),
Cint, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
cmpabs_real(a::QQBarFieldElem, b::QQBarFieldElem) = ccall((:qqbar_cmpabs_re, libcalcium),
Cint, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
cmpabs_imag(a::QQBarFieldElem, b::QQBarFieldElem) = ccall((:qqbar_cmpabs_im, libcalcium),
Cint, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
cmp_root_order(a::QQBarFieldElem, b::QQBarFieldElem) = ccall((:qqbar_cmp_root_order, libcalcium),
Cint, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), a, b)
@doc raw"""
is_equal_real(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the real parts of `a` and `b`.
"""
is_equal_real(a::QQBarFieldElem, b::QQBarFieldElem) = cmp_real(a, b) == 0
@doc raw"""
is_equal_imag(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the imaginary parts of `a` and `b`.
"""
is_equal_imag(a::QQBarFieldElem, b::QQBarFieldElem) = cmp_imag(a, b) == 0
@doc raw"""
is_equal_abs(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the absolute values of `a` and `b`.
"""
is_equal_abs(a::QQBarFieldElem, b::QQBarFieldElem) = cmpabs(a, b) == 0
@doc raw"""
is_equal_abs_real(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the absolute values of the real parts of `a` and `b`.
"""
is_equal_abs_real(a::QQBarFieldElem, b::QQBarFieldElem) = cmpabs_real(a, b) == 0
@doc raw"""
is_equal_abs_imag(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the absolute values of the imaginary parts of `a` and `b`.
"""
is_equal_abs_imag(a::QQBarFieldElem, b::QQBarFieldElem) = cmpabs_imag(a, b) == 0
@doc raw"""
is_less_real(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the real parts of `a` and `b`.
"""
is_less_real(a::QQBarFieldElem, b::QQBarFieldElem) = cmp_real(a, b) < 0
@doc raw"""
is_less_imag(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the imaginary parts of `a` and `b`.
"""
is_less_imag(a::QQBarFieldElem, b::QQBarFieldElem) = cmp_imag(a, b) < 0
@doc raw"""
is_less_abs(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the absolute values of `a` and `b`.
"""
is_less_abs(a::QQBarFieldElem, b::QQBarFieldElem) = cmpabs(a, b) < 0
@doc raw"""
is_less_abs_real(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the absolute values of the real parts of `a` and `b`.
"""
is_less_abs_real(a::QQBarFieldElem, b::QQBarFieldElem) = cmpabs_real(a, b) < 0
@doc raw"""
is_less_abs_imag(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the absolute values of the imaginary parts of `a` and `b`.
"""
is_less_abs_imag(a::QQBarFieldElem, b::QQBarFieldElem) = cmpabs_imag(a, b) < 0
@doc raw"""
is_less_root_order(a::QQBarFieldElem, b::QQBarFieldElem)
Compares the `a` and `b` in root sort order.
"""
is_less_root_order(a::QQBarFieldElem, b::QQBarFieldElem) = cmp_root_order(a, b) < 0
# todo: wrap qqbar_equal_fmpq_poly_val
###############################################################################
#
# Complex parts
#
###############################################################################
@doc raw"""
real(a::QQBarFieldElem)
Return the real part of `a`.
"""
function real(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_re, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
imag(a::QQBarFieldElem)
Return the imaginary part of `a`.
"""
function imag(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_im, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
abs(a::QQBarFieldElem)
Return the absolute value of `a`.
"""
function abs(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_abs, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
conj(a::QQBarFieldElem)
Return the complex conjugate of `a`.
"""
function conj(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_conj, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
abs2(a::QQBarFieldElem)
Return the squared absolute value of `a`.
"""
function abs2(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_abs2, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
sign(a::QQBarFieldElem)
Return the complex sign of `a`, defined as zero if `a` is zero
and as $a / |a|$ otherwise.
"""
function sign(a::QQBarFieldElem)
z = QQBarFieldElem()
ccall((:qqbar_sgn, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
csgn(a::QQBarFieldElem)
Return the extension of the real sign function taking the value 1
strictly in the right half plane, -1 strictly in the left half plane,
and the sign of the imaginary part when on the imaginary axis.
Equivalently, $\operatorname{csgn}(x) = x / \sqrt{x^2}$ except that the value is 0
at zero. The value is returned as a Julia integer.
"""
function csgn(a::QQBarFieldElem)
return QQBarFieldElem(Int(ccall((:qqbar_csgn, libcalcium), Cint, (Ref{QQBarFieldElem}, ), a)))
end
@doc raw"""
sign_real(a::QQBarFieldElem)
Return the sign of the real part of `a` as a Julia integer.
"""
function sign_real(a::QQBarFieldElem)
return QQBarFieldElem(Int(ccall((:qqbar_sgn_re, libcalcium),
Cint, (Ref{QQBarFieldElem}, ), a)))
end
@doc raw"""
sign_imag(a::QQBarFieldElem)
Return the sign of the imaginary part of `a` as a Julia integer.
"""
function sign_imag(a::QQBarFieldElem)
return QQBarFieldElem(Int(ccall((:qqbar_sgn_im, libcalcium),
Cint, (Ref{QQBarFieldElem}, ), a)))
end
function floor(a::QQBarFieldElem)
return QQBarFieldElem(floor(ZZRingElem, a))
end
function floor(::Type{ZZRingElem}, a::QQBarFieldElem)
z = ZZRingElem()
ccall((:qqbar_floor, libcalcium), Nothing, (Ref{ZZRingElem}, Ref{QQBarFieldElem}, ), z, a)
return z
end
function ceil(a::QQBarFieldElem)
return QQBarFieldElem(ceil(ZZRingElem, a))
end
function ceil(::Type{ZZRingElem}, a::QQBarFieldElem)
z = ZZRingElem()
ccall((:qqbar_ceil, libcalcium), Nothing, (Ref{ZZRingElem}, Ref{QQBarFieldElem}, ), z, a)
return z
end
###############################################################################
#
# Round
#
###############################################################################
# rounding
function round(::Type{ZZRingElem}, a::QQBarFieldElem, ::RoundingMode{:Nearest})
if is_zero(a)
return zero(ZZ)
end
ca = floor(ZZRingElem, a)
if a < ca + QQ(1//2)
return ca
elseif a > ca + QQ(1//2)
return ca + 1
else
return is_even(ca) ? ca : ca + 1
end
end
function round(a::QQBarFieldElem, ::RoundingMode{:Nearest})
return parent(a)(round(ZZRingElem, a, RoundNearest))
end
round(x::QQBarFieldElem, ::RoundingMode{:Up}) = ceil(x)
round(::Type{ZZRingElem}, x::QQBarFieldElem, ::RoundingMode{:Up})= ceil(ZZRingElem, x)
round(x::QQBarFieldElem, ::RoundingMode{:Down}) = floor(x)
round(::Type{ZZRingElem}, x::QQBarFieldElem, ::RoundingMode{:Down}) = floor(ZZRingElem, x)
round(x::QQBarFieldElem, ::RoundingMode{:NearestTiesAway}) = sign(x) * floor(abs(x) + 1//2)
function round(::Type{ZZRingElem}, x::QQBarFieldElem, ::RoundingMode{:NearestTiesAway})
tmp = floor(ZZRingElem, abs(x) + 1//2)
return is_positive(x) ? tmp : -tmp
end
# default
round(a::QQBarFieldElem) = round(a, RoundNearestTiesAway)
round(::Type{ZZRingElem}, a::QQBarFieldElem) = round(ZZRingElem, a, RoundNearestTiesAway)
###############################################################################
#
# Roots
#
###############################################################################
@doc raw"""
sqrt(a::QQBarFieldElem; check::Bool=true)
Return the principal square root of `a`.
"""
function sqrt(a::QQBarFieldElem; check::Bool=true)
z = QQBarFieldElem()
ccall((:qqbar_sqrt, libcalcium),
Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}), z, a)
return z
end
@doc raw"""
root(a::QQBarFieldElem, n::Int)
Return the principal `n`-th root of `a`. Requires positive `n`.
"""
function root(a::QQBarFieldElem, n::Int)
n <= 0 && throw(DomainError(n))
z = QQBarFieldElem()
ccall((:qqbar_root_ui, libcalcium),
Nothing, (Ref{QQBarFieldElem}, Ref{QQBarFieldElem}, UInt), z, a, n)
return z
end
function qqbar_vec(n::Int)
return ccall((:_qqbar_vec_init, libcalcium), Ptr{qqbar_struct}, (Int,), n)
end
function array(R::QQBarField, v::Ptr{qqbar_struct}, n::Int)
r = Vector{QQBarFieldElem}(undef, n)
for i=1:n
r[i] = R()
ccall((:qqbar_set, libcalcium), Nothing, (Ref{QQBarFieldElem}, Ptr{qqbar_struct}),
r[i], v + (i-1)*sizeof(qqbar_struct))
end
return r
end
function qqbar_vec_clear(v::Ptr{qqbar_struct}, n::Int)
ccall((:_qqbar_vec_clear, libcalcium),
Nothing, (Ptr{qqbar_struct}, Int), v, n)
end
@doc raw"""
roots(R::QQBarField, f::ZZPolyRingElem)
Return all the roots of the polynomial `f` in the field of algebraic
numbers `R`. The output array is sorted in the default sort order for
algebraic numbers. Roots of multiplicity higher than one are repeated
according to their multiplicity.
"""
function roots(R::QQBarField, f::ZZPolyRingElem)
deg = degree(f)
if deg <= 0
return Array{QQBarFieldElem}(undef, 0)
end
roots = qqbar_vec(deg)
ccall((:qqbar_roots_fmpz_poly, libcalcium),
Nothing, (Ptr{qqbar_struct}, Ref{ZZPolyRingElem}, Int), roots, f, 0)
res = array(R, roots, deg)
qqbar_vec_clear(roots, deg)
return res
end
@doc raw"""
roots(R::QQBarField, f::QQPolyRingElem)
Return all the roots of the polynomial `f` in the field of algebraic
numbers `R`. The output array is sorted in the default sort order for
algebraic numbers. Roots of multiplicity higher than one are repeated
according to their multiplicity.
"""
function roots(R::QQBarField, f::QQPolyRingElem)
deg = degree(f)
if deg <= 0
return Array{QQBarFieldElem}(undef, 0)
end
roots = qqbar_vec(deg)
ccall((:qqbar_roots_fmpq_poly, libcalcium),
Nothing, (Ptr{qqbar_struct}, Ref{QQPolyRingElem}, Int), roots, f, 0)
res = array(R, roots, deg)
qqbar_vec_clear(roots, deg)
return res
end
@doc raw"""
conjugates(a::QQBarFieldElem)