# Norm in UniversalCyclotomicField

Hi,

I'm trying to work with vectors in the UniversalCyclotomicField, But I can't find a way to make the norm work. I'm writing the following code :

sage: a = vector([E(8)])
sage: a.norm()

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-5b5ddb3f4c99> in <module>()
----> 1 a.norm()

/home/[name]/SageMath/local/lib/python2.7/site-packages/sage/modules/free_module_element.pyx in sage.modules.free_module_element.FreeModuleElement.norm (build/cythonized/sage/modules/free_module_element.c:12840)()
1671             sqrt(5)
1672         """
-> 1673         abs_self = [abs(x) for x in self]
1674         if p == Infinity:
1675             return max(abs_self)

TypeError: bad operand type for abs(): 'UniversalCyclotomicField_with_category.element_class'


And having no luck with it. Does any of you know a workaround allowing me to stay in exact calculations ?

edit retag close merge delete

Maybe using

sage: E(8)*E(8).conjugate()
1


to make your own norm method for vectors ?

( 2018-12-11 02:48:49 -0500 )edit

Sort by » oldest newest most voted

Hi again, this bug was marked as fixed in version 8.6, but I still encounter it in this same version :

Forcing sage-location, probably because a new package was installed.
Cleaning up, do not interrupt this.
Done cleaning.
┌────────────────────────────────────────────────────────────────────┐
│ SageMath version 8.6, Release Date: 2019-01-15                     │
│ Using Python 2.7.15. Type "help()" for help.                       │
└────────────────────────────────────────────────────────────────────┘
sage: a = vector([E(8)])
sage: a.norm()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-5b5ddb3f4c99> in <module>()
----> 1 a.norm()

/home/henri/SageMath/local/lib/python2.7/site-packages/sage/modules/free_module_element.pyx in sage.modules.free_module_element.FreeModuleElement.norm (build/cythonized/sage/modules/free_module_element.c:12190)()
1672             sqrt(5)
1673         """
-> 1674         abs_self = [abs(x) for x in self]
1675         if p == Infinity:
1676             return max(abs_self)

TypeError: bad operand type for abs(): 'UniversalCyclotomicField_with_category.element_class'


Am I doing something wrong ?

more

Sorry, there was a mistake in the fix. The method should have been called __abs__ for it to fix the problem, but there was an oversight and the missing method was introduced under the name abs.

I opened a follow-up ticket at

to give it the proper name.

( 2019-02-01 16:52:49 -0500 )edit

Thank you for your report, this is indeed a bug. Now tracked at

As a workaround,

• one could convert a to a vector over the field of algebraic numbers; then the norm is given as an exact algebraic number:

sage: a = vector([E(8), E(7)])
sage: b = a.change_ring(QQbar)
sage: bn = b.norm()
sage: bn
1.414213562373095?
sage: bn.parent()
Algebraic Real Field

• one could convert a to a vector over a cyclotomic field (not the universal one); sadly the norm is then given as an inexact (floating-point) number:

sage: a = vector([E(8), E(7)])
sage: c = a.apply_map(lambda x: x.to_cyclotomic_field())
sage: cn = c.norm()
sage: cn
1.41421356237310
sage: cn.parent()
Real Field with 53 bits of precision

• the square of the norm of the vector a can be obtained directly as a * a.conjugate(), but one then gets it as an element in the universal cyclotomic field, and a similar bug prevents from taking the square root of that directly:

sage: a = vector([E(8), E(7)])
sage: ann = a * a.conjugate()
sage: ann
2
sage: ann.parent()
Universal Cyclotomic Field
sage: an = sqrt(ann)
Traceback (most recent call last)
...
NotImplementedError: is_square() not implemented for elements of Universal Cyclotomic Field


and one again has to go through a cyclotomic field other than the universal one, or the field of real algebraic numbers:

sage: nna = AA(a*a.conjugate())
sage: nna
2
sage: nna.parent()
Algebraic Real Field
sage: na = sqrt(nna)
sage: na
1.414213562373095?
sage: na.parent()
Algebraic Real Field

more

The fix is merged in Sage 8.6.beta0.

( 2018-12-28 03:27:30 -0500 )edit