Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix multiplication of number field element * ZZ-vector by handling latex names of number field generators #30360

Closed
kliem opened this issue Aug 14, 2020 · 47 comments

Comments

@kliem
Copy link
Contributor

kliem commented Aug 14, 2020

sage: 1/2*vector([1,2,3])
(1/2, 1, 3/2)
sage: AA(2).sqrt()*vector([1,2,3])                                                                                                                                                                                                                                                                                                                                         
(1.414213562373095?, 2.828427124746190?, 4.242640687119285?)
sage: K.<sqrt5> = QuadraticField(5, embedding=AA(5).sqrt())                                                                                                                                                                                                                                                                                                                
sage: sqrt5*vector([1,2])                                                                                                                                                                                                                                                                                                                                                  
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-65-1ac790b433d6> in <module>
----> 1 sqrt5*vector([Integer(1),Integer(2)])

~/Applications/sage/local/lib/python3.7/site-packages/sage/structure/element.pyx in sage.structure.element.Element.__mul__ (build/cythonized/sage/structure/element.c:12196)()
   1513             return (<Element>left)._mul_(right)
   1514         if BOTH_ARE_ELEMENT(cl):
-> 1515             return coercion_model.bin_op(left, right, mul)
   1516 
   1517         cdef long value

~/Applications/sage/local/lib/python3.7/site-packages/sage/structure/coerce.pyx in sage.structure.coerce.CoercionModel.bin_op (build/cythonized/sage/structure/coerce.c:11304)()
   1246         # We should really include the underlying error.
   1247         # This causes so much headache.
-> 1248         raise bin_op_exception(op, x, y)
   1249 
   1250     cpdef canonical_coercion(self, x, y):

TypeError: unsupported operand parent(s) for *: 'Number Field in sqrt5 with defining polynomial x^2 - 5 with sqrt5 = 2.236067977499790?' and 'Ambient free module of rank 2 over the principal ideal domain Integer Ring'

It doesn't matter whether you specify the embedding or not.

Depends on #30372

CC: @videlec @tscrim

Component: coercion

Keywords: action, scalar, free module

Author: Matthias Koeppe, Francis Clarke

Branch/Commit: a3c790f

Reviewer: Travis Scrimshaw

Issue created by migration from https://trac.sagemath.org/ticket/30360

@kliem kliem added this to the sage-9.2 milestone Aug 14, 2020
@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 14, 2020

comment:2

It's not really the pushout that's failing here:

sage: from sage.categories.pushout import pushout
sage: pushout(sqrt5.parent(), vector([1,2]).parent())
Vector space of dimension 2 over Number Field in sqrt5 with defining polynomial x^2 - 5 with sqrt5 = 2.236067977499790?

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 14, 2020

comment:3

I think the methods _lmul_ and _rmul_ of NumberFieldElement_quadratic are in the way. I think they may just need to be removed (haven't tried)

@kliem kliem changed the title Pushout of number field and free module over ZZ fails Action of number field on free module over ZZ not discovered Aug 15, 2020
@kliem
Copy link
Contributor Author

kliem commented Aug 15, 2020

Changed keywords from pushout, free module to action, scalar, free module

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

Commit: 8a9c2aa

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:7

Replying to @mkoeppe:

I think the methods _lmul_ and _rmul_ of NumberFieldElement_quadratic are in the way. I think they may just need to be removed (haven't tried)

By itself this does not fix it... (see branch)


New commits:

8a9c2aaWIP: NumberFieldElement_quadratic._lmul_, _rmul_: Remove

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:8

You are right, the action is missing.

sage: cm.explain(AA, vector([1,2]).parent(), operator.mul)                                                                                                          
Action discovered.
    Left scalar multiplication by Algebraic Real Field on Ambient free module of rank 2 over the principal ideal domain Integer Ring
Result lives in Vector space of dimension 2 over Algebraic Real Field
Vector space of dimension 2 over Algebraic Real Field
sage: cm.explain(K, vector([1,2]).parent(), operator.mul)                                                                                                           

Unknown result parent.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:9

It looks like this is because of a unique representation / equality bug for number fields

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:10
sage: K.<sqrt5> = QuadraticField(5, embedding=AA(5).sqrt())
sage: V = vector([1, 2]).parent()
sage: from sage.structure.coerce_actions import LeftModuleAction                                                                                                    
sage: LeftModuleAction(AA, V)
Left scalar multiplication by Algebraic Real Field on Ambient free module of rank 2 over the principal ideal domain Integer Ring
sage: LeftModuleAction(K, V)                                                                                                                               
CoercionException: Actor must be coercible into base.

Looking at LeftModuleAction.__init__, this boils down to:

sage: from sage.categories.pushout import pushout                                                         
sage: K                                                                                                   
Number Field in sqrt5 with defining polynomial x^2 - 5 with sqrt5 = 2.236067977499790?
sage: pushout(K, ZZ) is K                                                                                 
True
sage: K2 = pushout(K, V).base(); K2                                                                       
Number Field in sqrt5 with defining polynomial x^2 - 5 with sqrt5 = 2.236067977499790?
sage: K2 is K                                                                                             
False
sage: K2 == K                                                                                             
False

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:11

This bug seems specific to quadratic fields:

sage: K = QuadraticField(5)                                                                               
sage: pushout(K, ZZ^2).base() is K                                                                        
False
sage: K.<a> = NumberField(x^3 + 2/3*x + 1)                                                                
sage: pushout(K, ZZ^2).base() is K                                                                        
True
sage: K.<a> = NumberField(x^3 + 2/3*x + 1, embedding=AA(1))                                               
sage: pushout(K, ZZ^2).base() is K                                                                        
True

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:12
sage: QuadraticField(5).latex_variable_name()                                                                                       
'\\sqrt{5}'
sage: pushout(QuadraticField(5), ZZ^2).base().latex_variable_name()                                                                 
'a'

That's why they're not equal...

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:13
sage: K = QuadraticField(5, latex_name=None)                                                                                        
sage: pushout(K, ZZ^2).base() is K                                                                                                  
True
sage: K.<sqrt5> = QuadraticField(5, embedding=AA(5).sqrt(), latex_name=None)                                                                                                                                                                                                                                                                                                                
sage: sqrt5*vector([1,2])  
(sqrt5, 2*sqrt5)

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:14

Also

sage: K.<a> = NumberField(x^3 + 2/3*x + 1, latex_name='mangrove') 
sage: pushout(K, ZZ^2).base() is K                                                                                            
False

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:15

Looks like NumberField_generic.construction needs to handle latex_variable_names.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

comment:16

One more broken thing:

sage: K3.<a> = NumberField(x^3+x^2+1, latex_name=['alpha'])                                                                         
sage: K3.latex_variable_names()                                                                                                     
['a']
sage: K3.latex_variable_name()                                                                                                      
'alpha'

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

Dependencies: #30372

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 15, 2020

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

fc48d6dsage.categories.pushout.AlgebraicExtensionFunctor: Handle latex_names
d85068fCommutativeRing.extension, NumberField_generic.extension, FiniteField.extension: Accept latex_name argument
7a88492NumberField_generic.construction: Pass latex_names to AlgebraicExtensionFunctor

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 15, 2020

Changed commit from 8a9c2aa to 7a88492

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 15, 2020

Author: Matthias Koeppe

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 15, 2020

Changed commit from 7a88492 to 20ce248

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 15, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

1842e47sage.rings.number_field.number_field.NumberField_generic: Use _latex_names instead of __latex_variable_name, deprecate .latex_variable_name()
f9ceef7Merge branch 't/30372/replace_numberfield_generic___latex_variable_name_by___latex_name' into t/30360/action_of_number_field_on_free_module_over_zz_not_discovered
56f6c4dsage.rings.number_field.number_field.NumberFieldTower: Pass latex_name to extension
42eb272sage.rings.number_field.number_field.NumberField_generic.construction: Collect latex_names from the tower of extensions
6697bcaFixup
a6e828eMerge branch 't/30372/replace_numberfield_generic___latex_variable_name_by___latex_name' into t/30360/action_of_number_field_on_free_module_over_zz_not_discovered
20ce248src/sage/rings/number_field/number_field_element_quadratic.pyx: Add test for #30360

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 16, 2020

Branch pushed to git repo; I updated commit sha1. New commits:

da551dcsrc/sage/rings/number_field/number_field.py: Fix error found by pyflakes
6d29391Merge branch 't/30372/replace_numberfield_generic___latex_variable_name_by___latex_name' into t/30360/action_of_number_field_on_free_module_over_zz_not_discovered

@sagetrac-git
Copy link
Mannequin

sagetrac-git mannequin commented Aug 16, 2020

Changed commit from 20ce248 to 6d29391

@tscrim
Copy link
Collaborator

tscrim commented Aug 18, 2020

comment:32

I am not sure about this:

        if latex_name is None and latex_names is not None:
            latex_name = latex_names

I would expect latex_names to be a tuple, so I would think the correct thing to do would be latex_name = latex_names[0].

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 18, 2020

comment:33

This is passed on to the extension method, which handles both singular and plural.

@tscrim
Copy link
Collaborator

tscrim commented Aug 18, 2020

Reviewer: Travis Scrimshaw

@tscrim
Copy link
Collaborator

tscrim commented Aug 18, 2020

comment:34

Okay, good. Then let's wait for the patchbot, but if that is green, then positive review.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 18, 2020

comment:35

Thanks!

@vbraun
Copy link
Member

vbraun commented Aug 24, 2020

comment:36

PDF docs don't build

@kliem
Copy link
Contributor Author

kliem commented Aug 25, 2020

comment:37

@vbraun: Can you be more precice? Is this on a test branch on top of other tickets?

The patchbot exceeds disk quota due to ccache, so I don't think this has anything to do with this ticket.

For me, documentation builds just fine.

@vbraun
Copy link
Member

vbraun commented Aug 26, 2020

comment:39

PDF docs don't build (non-pdf is ok)

@vbraun
Copy link
Member

vbraun commented Aug 26, 2020

comment:40
[docpdf] 
[docpdf] ! Package inputenc Error: Unicode character ^^G (U+0007)
[docpdf] (inputenc)                not set up for use with LaTeX.
[docpdf] 
[docpdf] See the inputenc package documentation for explanation.
[docpdf] Type  H <return>  for immediate help.
[docpdf]  ...                                              
[docpdf]                                                   
[docpdf] l.1012 ...G{l+s+s1}{\PYGZsq{}}\PYG{p}{]}\PYG{p}{)}
[docpdf]                                                   
[docpdf] ? 
[docpdf] ! Emergency stop.

@tscrim
Copy link
Collaborator

tscrim commented Aug 27, 2020

comment:41

I think the problem is in NumberFieldTower, the string is not a r""". Let me see if that fixes it.

@tscrim
Copy link
Collaborator

tscrim commented Aug 27, 2020

Changed commit from c598fbb to a3c790f

@tscrim
Copy link
Collaborator

tscrim commented Aug 27, 2020

@tscrim
Copy link
Collaborator

tscrim commented Aug 27, 2020

comment:42

Replying to @tscrim:

I think the problem is in NumberFieldTower, the string is not a r""". Let me see if that fixes it.

I think that fixes it. Please double-check. What a cryptic error message through...


New commits:

45e0c4fMerge branch 'u/mkoeppe/action_of_number_field_on_free_module_over_zz_not_discovered' of git://trac.sagemath.org/sage into public/coercion/latex_names-30360
a3c790fFix pdf docbuild in NumberFieldTower.

@mkoeppe
Copy link
Contributor

mkoeppe commented Aug 27, 2020

comment:43

Thanks, this did the job.

@vbraun
Copy link
Member

vbraun commented Sep 1, 2020

Changed branch from public/coercion/latex_names-30360 to a3c790f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants