From affa033f1dc12c547d16d326168d4294267e9108 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Sun, 28 Jul 2024 07:16:51 +0700 Subject: [PATCH 1/4] number_field_elements_from_algebraics: Fix CyclotomicField embedding when embedding=False --- src/sage/rings/qqbar.py | 96 +++++++++++++++++++++++++++++++++++------ 1 file changed, 84 insertions(+), 12 deletions(-) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 300d64b8279..fe37ea15fd7 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -2556,7 +2556,8 @@ def number_field_elements_from_algebraics(numbers, minimal=False, - ``same_field`` -- boolean (default: ``False``); see below - ``embedded`` -- boolean (default: ``False``); whether to make the - NumberField embedded + NumberField embedded, note that this has no effect when the + resulting field is ``QQ`` because there is only one ``QQ`` instance - ``name`` -- string (default: ``'a'``); name of the primitive element @@ -2827,6 +2828,69 @@ def number_field_elements_from_algebraics(numbers, minimal=False, To: Algebraic Real Field Defn: 1 |--> 1) + Test ``embedded`` for quadratic and cyclotomic fields:: + + sage: number_field_elements_from_algebraics([QQbar((-1)^(2/3))], embedded=False, minimal=True) + (Number Field in zeta6 with defining polynomial x^2 - x + 1, + [zeta6 - 1], + Ring morphism: + From: Number Field in zeta6 with defining polynomial x^2 - x + 1 + To: Algebraic Field + Defn: zeta6 |--> 0.500000000000000? + 0.866025403784439?*I) + sage: _[0].coerce_embedding() + sage: number_field_elements_from_algebraics([QQbar((-1)^(2/3))], embedded=True, minimal=True) + (Cyclotomic Field of order 6 and degree 2, + [zeta6 - 1], + Ring morphism: + From: Cyclotomic Field of order 6 and degree 2 + To: Algebraic Field + Defn: zeta6 |--> 0.500000000000000? + 0.866025403784439?*I) + sage: _[0].coerce_embedding() + Generic morphism: + From: Cyclotomic Field of order 6 and degree 2 + To: Complex Lazy Field + Defn: zeta6 -> 0.500000000000000? + 0.866025403784439?*I + sage: number_field_elements_from_algebraics([QQbar((-1)^(1/2))], embedded=False, minimal=True) + (Number Field in I with defining polynomial x^2 + 1, + [I], + Ring morphism: + From: Number Field in I with defining polynomial x^2 + 1 + To: Algebraic Field + Defn: I |--> 1*I) + sage: _[0].coerce_embedding() + sage: number_field_elements_from_algebraics([QQbar((-1)^(1/2))], embedded=True, minimal=True) + (Number Field in I with defining polynomial x^2 + 1 with I = 1*I, + [I], + Ring morphism: + From: Number Field in I with defining polynomial x^2 + 1 with I = 1*I + To: Algebraic Field + Defn: I |--> 1*I) + sage: _[0].coerce_embedding() + Generic morphism: + From: Number Field in I with defining polynomial x^2 + 1 with I = 1*I + To: Complex Lazy Field + Defn: I -> 1*I + sage: number_field_elements_from_algebraics([QQbar((-1)^(1/5))], embedded=False, minimal=True) + (Number Field in zeta10 with defining polynomial x^4 - x^3 + x^2 - x + 1, + [zeta10], + Ring morphism: + From: Number Field in zeta10 with defining polynomial x^4 - x^3 + x^2 - x + 1 + To: Algebraic Field + Defn: zeta10 |--> 0.8090169943749474? + 0.5877852522924731?*I) + sage: _[0].coerce_embedding() + sage: number_field_elements_from_algebraics([QQbar((-1)^(1/5))], embedded=True, minimal=True) + (Cyclotomic Field of order 10 and degree 4, + [zeta10], + Ring morphism: + From: Cyclotomic Field of order 10 and degree 4 + To: Algebraic Field + Defn: zeta10 |--> 0.8090169943749474? + 0.5877852522924731?*I) + sage: _[0].coerce_embedding() + Generic morphism: + From: Cyclotomic Field of order 10 and degree 4 + To: Complex Lazy Field + Defn: zeta10 -> 0.809016994374948? + 0.587785252292473?*I + Tests more complicated combinations:: sage: # needs sage.libs.gap sage.symbolic @@ -2905,17 +2969,25 @@ def mk_algebraic(x): exact_generator = gen.root_as_algebraic() hom = fld.hom([exact_generator]) - if fld is not QQ and embedded: - # creates the embedded field - embedded_field = NumberField(fld.defining_polynomial(), fld.variable_name(), embedding=exact_generator) - - # embeds the numbers - inter_hom = fld.hom([embedded_field.gen(0)]) - nums = [inter_hom(n) for n in nums] - - # get the field and homomorphism - hom = embedded_field.hom([exact_generator]) - fld = embedded_field + if fld is QQ: + # ignore embedded parameter + pass + else: + # cyclotomic fields are embedded by default + # number fields are not embedded by default + # if the default embedding is different from what is expected then modify the field + if embedded != (fld.coerce_embedding() is not None): + # creates the modified field + modified_field = NumberField(fld.defining_polynomial(), fld.variable_name(), + embedding=exact_generator if embedded else None) + + # embeds the numbers + inter_hom = fld.hom([modified_field.gen(0)]) + nums = [inter_hom(n) for n in nums] + + # get the field and homomorphism + hom = modified_field.hom([exact_generator]) + fld = modified_field if single_number: nums = nums[0] From b16a160f667aab5e4bcb72282ba83ebbb48f9c98 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Fri, 8 Nov 2024 07:09:10 +0700 Subject: [PATCH 2/4] Update src/sage/rings/qqbar.py Co-authored-by: Travis Scrimshaw --- src/sage/rings/qqbar.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index fe37ea15fd7..4f26e0063a2 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -2969,10 +2969,8 @@ def mk_algebraic(x): exact_generator = gen.root_as_algebraic() hom = fld.hom([exact_generator]) - if fld is QQ: - # ignore embedded parameter - pass - else: + if fld is not QQ: + # ignore the embedded parameter for QQ # cyclotomic fields are embedded by default # number fields are not embedded by default # if the default embedding is different from what is expected then modify the field From 01452e65ca0038034627ddd0cda4dcfeaf9eef07 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Fri, 8 Nov 2024 09:28:11 +0700 Subject: [PATCH 3/4] Explicitly name return value in test case --- src/sage/rings/qqbar.py | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 4f26e0063a2..3939b06651f 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -2830,62 +2830,62 @@ def number_field_elements_from_algebraics(numbers, minimal=False, Test ``embedded`` for quadratic and cyclotomic fields:: - sage: number_field_elements_from_algebraics([QQbar((-1)^(2/3))], embedded=False, minimal=True) + sage: v = number_field_elements_from_algebraics([QQbar((-1)^(2/3))], embedded=False, minimal=True); v (Number Field in zeta6 with defining polynomial x^2 - x + 1, [zeta6 - 1], Ring morphism: From: Number Field in zeta6 with defining polynomial x^2 - x + 1 To: Algebraic Field Defn: zeta6 |--> 0.500000000000000? + 0.866025403784439?*I) - sage: _[0].coerce_embedding() - sage: number_field_elements_from_algebraics([QQbar((-1)^(2/3))], embedded=True, minimal=True) + sage: v[0].coerce_embedding() + sage: v = number_field_elements_from_algebraics([QQbar((-1)^(2/3))], embedded=True, minimal=True); v (Cyclotomic Field of order 6 and degree 2, [zeta6 - 1], Ring morphism: From: Cyclotomic Field of order 6 and degree 2 To: Algebraic Field Defn: zeta6 |--> 0.500000000000000? + 0.866025403784439?*I) - sage: _[0].coerce_embedding() + sage: v[0].coerce_embedding() Generic morphism: From: Cyclotomic Field of order 6 and degree 2 To: Complex Lazy Field Defn: zeta6 -> 0.500000000000000? + 0.866025403784439?*I - sage: number_field_elements_from_algebraics([QQbar((-1)^(1/2))], embedded=False, minimal=True) + sage: v = number_field_elements_from_algebraics([QQbar((-1)^(1/2))], embedded=False, minimal=True); v (Number Field in I with defining polynomial x^2 + 1, [I], Ring morphism: From: Number Field in I with defining polynomial x^2 + 1 To: Algebraic Field Defn: I |--> 1*I) - sage: _[0].coerce_embedding() - sage: number_field_elements_from_algebraics([QQbar((-1)^(1/2))], embedded=True, minimal=True) + sage: v[0].coerce_embedding() + sage: v = number_field_elements_from_algebraics([QQbar((-1)^(1/2))], embedded=True, minimal=True); v (Number Field in I with defining polynomial x^2 + 1 with I = 1*I, [I], Ring morphism: From: Number Field in I with defining polynomial x^2 + 1 with I = 1*I To: Algebraic Field Defn: I |--> 1*I) - sage: _[0].coerce_embedding() + sage: v[0].coerce_embedding() Generic morphism: From: Number Field in I with defining polynomial x^2 + 1 with I = 1*I To: Complex Lazy Field Defn: I -> 1*I - sage: number_field_elements_from_algebraics([QQbar((-1)^(1/5))], embedded=False, minimal=True) + sage: v = number_field_elements_from_algebraics([QQbar((-1)^(1/5))], embedded=False, minimal=True); v (Number Field in zeta10 with defining polynomial x^4 - x^3 + x^2 - x + 1, [zeta10], Ring morphism: From: Number Field in zeta10 with defining polynomial x^4 - x^3 + x^2 - x + 1 To: Algebraic Field Defn: zeta10 |--> 0.8090169943749474? + 0.5877852522924731?*I) - sage: _[0].coerce_embedding() - sage: number_field_elements_from_algebraics([QQbar((-1)^(1/5))], embedded=True, minimal=True) + sage: v[0].coerce_embedding() + sage: v = number_field_elements_from_algebraics([QQbar((-1)^(1/5))], embedded=True, minimal=True); v (Cyclotomic Field of order 10 and degree 4, [zeta10], Ring morphism: From: Cyclotomic Field of order 10 and degree 4 To: Algebraic Field Defn: zeta10 |--> 0.8090169943749474? + 0.5877852522924731?*I) - sage: _[0].coerce_embedding() + sage: v[0].coerce_embedding() Generic morphism: From: Cyclotomic Field of order 10 and degree 4 To: Complex Lazy Field From 6c56c15dbdcd8dfc4daa1f13c5a4623da92cdc19 Mon Sep 17 00:00:00 2001 From: user202729 <25191436+user202729@users.noreply.github.com> Date: Fri, 8 Nov 2024 10:23:28 +0700 Subject: [PATCH 4/4] Fix failed test case --- src/sage/rings/qqbar.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/sage/rings/qqbar.py b/src/sage/rings/qqbar.py index 3939b06651f..72df892337b 100644 --- a/src/sage/rings/qqbar.py +++ b/src/sage/rings/qqbar.py @@ -2911,10 +2911,10 @@ def number_field_elements_from_algebraics(numbers, minimal=False, sage: AA((-1)^(2/3)) 1 sage: number_field_elements_from_algebraics([(-1)^(2/3)]) - (Cyclotomic Field of order 6 and degree 2, + (Number Field in zeta6 with defining polynomial x^2 - x + 1, [zeta6 - 1], Ring morphism: - From: Cyclotomic Field of order 6 and degree 2 + From: Number Field in zeta6 with defining polynomial x^2 - x + 1 To: Algebraic Field Defn: zeta6 |--> 0.500000000000000? + 0.866025403784439?*I) """