Skip to content

Commit ffd7e9f

Browse files
authored
Add new methods for abs2 (#2098)
* Add new methods for abs2; Nemo issue 2093 * Simplified implementation of abs2(ComplexFieldElem), thanks to Tommy H
1 parent fcfa3d0 commit ffd7e9f

File tree

14 files changed

+73
-2
lines changed

14 files changed

+73
-2
lines changed

src/arb/Complex.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,6 +531,12 @@ function abs(x::ComplexFieldElem, prec::Int = precision(Balls))
531531
return z
532532
end
533533

534+
function abs2(x::ComplexFieldElem, prec::Int = precision(Balls))
535+
set_precision!(Balls, prec) do
536+
return real(x)^2 + imag(x)^2
537+
end
538+
end
539+
534540
################################################################################
535541
#
536542
# Inversion

src/arb/Real.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,10 @@ function abs(x::RealFieldElem)
842842
return z
843843
end
844844

845+
function abs2(x::RealFieldElem)
846+
return x^2
847+
end
848+
845849
################################################################################
846850
#
847851
# Inverse

src/arb/acb.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,10 @@ function abs(x::AcbFieldElem)
543543
return z
544544
end
545545

546+
function abs2(x::AcbFieldElem)
547+
return real(x)^2 + imag(x)^2
548+
end
549+
546550
################################################################################
547551
#
548552
# Inversion

src/arb/arb.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -836,6 +836,10 @@ function abs(x::ArbFieldElem)
836836
return z
837837
end
838838

839+
function abs2(x::ArbFieldElem)
840+
return x^2
841+
end
842+
839843
################################################################################
840844
#
841845
# Inverse

src/calcium/ca.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,17 @@ function abs(a::CalciumFieldElem)
818818
return r
819819
end
820820

821+
@doc raw"""
822+
abs2(a::CalciumFieldElem)
823+
824+
Return the square of the absolute value of `a`.
825+
"""
826+
function abs2(a::CalciumFieldElem)
827+
a2 = real(a)^2 + imag(a)^2 # ***OR*** a2 = a * conj(a)
828+
check_special(a2)
829+
return a2
830+
end
831+
821832
@doc raw"""
822833
conj(a::CalciumFieldElem; form::Symbol=:default)
823834

src/flint/fmpq.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ function abs(a::QQFieldElem)
100100
return z
101101
end
102102

103+
function abs2(a::QQFieldElem)
104+
return a^2
105+
end
106+
103107
zero(_::QQField) = QQFieldElem(0)
104108

105109
one(_::QQField) = QQFieldElem(1)

src/flint/fmpz.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ function abs(x::ZZRingElem)
357357
return z
358358
end
359359

360+
function abs2(x::ZZRingElem)
361+
return x^2
362+
end
363+
360364
floor(x::ZZRingElem) = x
361365
ceil(x::ZZRingElem) = x
362366
trunc(x::ZZRingElem) = x

test/arb/Complex-test.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@ end
138138
@test -CC(3) == CC(-3)
139139
@test abs(-CC(3)) == 3
140140
@test abs(CC(3)) == 3
141+
@test abs2(-CC(3)) == 9
142+
@test abs2(CC(3)) == 9
143+
@test abs2(CC(0)) == 0
141144
@test inv(CC(2)) == CC(QQ(1,2))
142145

143146
@test angle(CC(3)) == 0

test/arb/Real-test.jl

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,31 @@ end
8787
@test_throws ErrorException Int(RR(c))
8888

8989
@test abs(Float64(RR("2.3")) - 2.3) < 1e-10
90+
@test abs(abs2(Float64(RR("2.3"))) - 5.29) < 1e-10
9091
@test setprecision(BigFloat, 1000) do
9192
set_precision!(Balls, 1000) do
9293
abs(BigFloat(RR("2.3")) - BigFloat("2.3")) < 1e-299
9394
end
9495
end
96+
@test setprecision(BigFloat, 1000) do
97+
set_precision!(Balls, 1000) do
98+
abs(abs2(BigFloat(RR("2.3"))) - BigFloat("5.29")) < 1e-299
99+
end
100+
end
95101

96102
@test setprecision(BigFloat, 1000) do
97103
abs(BigFloat(RR("2.3", 1000)) - BigFloat("2.3")) < 1e-299
98104
end
105+
@test setprecision(BigFloat, 1000) do
106+
abs(abs2(BigFloat(RR("2.3", 1000))) - BigFloat("5.29")) < 1e-299
107+
end
99108

100109
@test setprecision(BigFloat, 1000) do
101110
abs(BigFloat(RR("2.3"; precision=1000)) - BigFloat("2.3")) < 1e-299
102111
end
112+
@test setprecision(BigFloat, 1000) do
113+
abs(abs2(BigFloat(RR("2.3"; precision=1000))) - BigFloat("5.29")) < 1e-299
114+
end
103115

104116
for T in [Float64, BigFloat]
105117
x = RR(-1)/3
@@ -246,6 +258,9 @@ end
246258
@test -RR(3) == RR(-3)
247259
@test abs(-RR(3)) == 3
248260
@test abs(RR(3)) == 3
261+
@test abs2(-RR(3)) == 9
262+
@test abs2(RR(3)) == 9
263+
@test abs2(RR(0)) == 0
249264
@test inv(RR(2)) == RR(0.5)
250265
end
251266

@@ -561,7 +576,7 @@ end
561576
CC = ComplexField()
562577

563578
tau = (1 + sqrt(CC(-23)))/2
564-
a = abs(modular_weber_f2(tau))^2
579+
a = abs2(modular_weber_f2(tau))
565580
C = lindep([RR(1), a, a^2, a^3, a^4, a^5], 20)
566581

567582
@test C == ZZRingElem[-1, 1, 1, 0, 1, 0]

test/arb/acb-test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,8 @@ end
138138
@test -CC(3) == CC(-3)
139139
@test abs(-CC(3)) == 3
140140
@test abs(CC(3)) == 3
141+
@test abs2(CC(3)) == 9
142+
@test abs2(CC(0)) == 0
141143
@test inv(CC(2)) == CC(QQ(1,2))
142144
end
143145

0 commit comments

Comments
 (0)