Opened 5 years ago

Closed 5 years ago

#22571 closed defect (fixed)

weird memory error while approximating sin

Reported by: kcrisman Owned by:
Priority: minor Milestone: sage-7.6
Component: basic arithmetic Keywords:
Cc: rws Merged in:
Authors: Jeroen Demeyer Reviewers: Ralf Stephan
Report Upstream: N/A Work issues:
Branch: e2fac19 (Commits, GitHub, GitLab) Commit: e2fac195237f10bb39da40c489a1cba3ecaeff00
Dependencies: Stopgaps:

Status badges

Description (last modified by jdemeyer)

Reported at this ask.sagemath question:

sage: bool(sin(pi*RR("0.7000000000000002")) > 0)
---------------------------------------------------------------------------
MemoryError                               Traceback (most recent call last)
<ipython-input-19-4f7b466aff9f> in <module>()
----> 1 bool(sin(Integer(2)*pi*(RealNumber('1.75')-RealNumber('1.4'))) > Integer(0))

/usr/local/src/sage-config/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.__nonzero__ (build/cythonized/sage/symbolic/expression.cpp:17971)()
   2608                 if pynac_result == relational_notimplemented and self.operator()==operator.ne:
   2609                     return not (self.lhs()-self.rhs()).is_trivial_zero()
-> 2610                 res = self.test_relation()
   2611                 if res is True:
   2612                     return True

/usr/local/src/sage-config/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.test_relation (build/cythonized/sage/symbolic/expression.cpp:18615)()
   2717         if domain is None:
   2718             is_interval = True
-> 2719             if self.lhs().is_algebraic() and self.rhs().is_algebraic():
   2720                 if op == equal or op == not_equal:
   2721                     domain = QQbar

/usr/local/src/sage-config/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression.is_algebraic (build/cythonized/sage/symbolic/expression.cpp:14911)()
   1953         """
   1954         try:
-> 1955             ex = sage.rings.all.QQbar(self)
   1956         except (TypeError, ValueError, NotImplementedError):
   1957             return False

/usr/local/src/sage-config/src/sage/structure/parent.pyx in sage.structure.parent.Parent.__call__ (build/cythonized/sage/structure/parent.c:9844)()
    953         if mor is not None:
    954             if no_extra_args:
--> 955                 return mor._call_(x)
    956             else:
    957                 return mor._call_with_args(x, args, kwds)

/usr/local/src/sage-config/src/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4890)()
    108                 print(type(C), C)
    109                 print(type(C._element_constructor), C._element_constructor)
--> 110             raise
    111 
    112     cpdef Element _call_with_args(self, x, args=(), kwds={}):

/usr/local/src/sage-config/src/sage/structure/coerce_maps.pyx in sage.structure.coerce_maps.DefaultConvertMap_unique._call_ (build/cythonized/sage/structure/coerce_maps.c:4757)()
    103         cdef Parent C = self._codomain
    104         try:
--> 105             return C._element_constructor(x)
    106         except Exception:
    107             if print_warnings:

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/rings/qqbar.pyc in _element_constructor_(self, x)
   1126             return AlgebraicNumber(x._descr)
   1127         elif hasattr(x, '_algebraic_'):
-> 1128             return x._algebraic_(QQbar)
   1129         return AlgebraicNumber(x)
   1130 

/usr/local/src/sage-config/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._algebraic_ (build/cythonized/sage/symbolic/expression.cpp:11720)()
   1475         """
   1476         from sage.symbolic.expression_conversions import algebraic
-> 1477         return algebraic(self, field)
   1478 
   1479     def __hash__(self):

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in algebraic(ex, field)
    956         0
    957     """
--> 958     return AlgebraicConverter(field)(ex)
    959 
    960 ##############

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in __call__(self, ex)
    224             return self.tuple(ex)
    225         else:
--> 226             return self.composition(ex, operator)
    227 
    228     def get_fake_div(self, ex):

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in composition(self, ex, operator)
    889             res = mag * QQbar.zeta(rat_arg.denom())**rat_arg.numer()
    890         elif func_name in ['sin', 'cos', 'tan']:
--> 891             exp_ia = exp(SR(-1).sqrt()*operand)._algebraic_(QQbar)
    892             if func_name == 'sin':
    893                 res = (exp_ia - ~exp_ia)/(2*QQbar.zeta(4))

/usr/local/src/sage-config/src/sage/symbolic/expression.pyx in sage.symbolic.expression.Expression._algebraic_ (build/cythonized/sage/symbolic/expression.cpp:11720)()
   1475         """
   1476         from sage.symbolic.expression_conversions import algebraic
-> 1477         return algebraic(self, field)
   1478 
   1479     def __hash__(self):

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in algebraic(ex, field)
    956         0
    957     """
--> 958     return AlgebraicConverter(field)(ex)
    959 
    960 ##############

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in __call__(self, ex)
    224             return self.tuple(ex)
    225         else:
--> 226             return self.composition(ex, operator)
    227 
    228     def get_fake_div(self, ex):

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/symbolic/expression_conversions.pyc in composition(self, ex, operator)
    887             else:
    888                 mag = 1
--> 889             res = mag * QQbar.zeta(rat_arg.denom())**rat_arg.numer()
    890         elif func_name in ['sin', 'cos', 'tan']:
    891             exp_ia = exp(SR(-1).sqrt()*operand)._algebraic_(QQbar)

/usr/local/src/sage-config/src/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedMethodCaller.__call__ (build/cythonized/sage/misc/cachefunc.c:10792)()
   2036                 return cache[k]
   2037         except KeyError:
-> 2038             w = self._instance_call(*args, **kwds)
   2039             cache[k] = w
   2040             return w

/usr/local/src/sage-config/src/sage/misc/cachefunc.pyx in sage.misc.cachefunc.CachedMethodCaller._instance_call (build/cythonized/sage/misc/cachefunc.c:10238)()
   1912             True
   1913         """
-> 1914         return self.f(self._instance, *args, **kwds)
   1915 
   1916     cdef fix_args_kwds(self, tuple args, dict kwds):

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/rings/qqbar.pyc in zeta(self, n)
   1314             return self.gen()
   1315         else:
-> 1316             nf = CyclotomicField(n)
   1317             p = nf.polynomial()
   1318             root = ANRoot(p, ComplexIntervalField(64).zeta(n))

/usr/local/src/sage-config/src/sage/structure/factory.pyx in sage.structure.factory.UniqueFactory.__call__ (build/cythonized/sage/structure/factory.c:1938)()
    362         key, kwds = self.create_key_and_extra_args(*args, **kwds)
    363         version = self.get_version(sage_version)
--> 364         return self.get_object(version, key, kwds)
    365 
    366     cpdef get_object(self, version, key, extra_args):

/usr/local/src/sage-config/src/sage/structure/factory.pyx in sage.structure.factory.UniqueFactory.get_object (build/cythonized/sage/structure/factory.c:2320)()
    405         except KeyError:
    406             pass
--> 407         obj = self.create_object(version, key, **extra_args)
    408         self._cache[version, cache_key] = obj
    409         try:

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/rings/number_field/number_field.pyc in create_object(self, version, key, **extra_args)
   1123             return UniversalCyclotomicField()
   1124         else:
-> 1125             return NumberField_cyclotomic(n, names, embedding=embedding)
   1126 
   1127 CyclotomicField = CyclotomicFieldFactory("sage.rings.number_field.number_field.CyclotomicField")

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/rings/number_field/number_field.pyc in __init__(self, n, names, embedding, assume_disc_small, maximize_at_primes)
   8923             <type 'sage.rings.number_field.number_field_element.NumberFieldElement_absolute'>
   8924         """
-> 8925         f = QQ['x'].cyclotomic_polynomial(n)
   8926         if names[0].startswith('zeta'):
   8927             latex_name = "\\zeta_{%s}"%n

/usr/local/src/sage-config/local/lib/python2.7/site-packages/sage/rings/polynomial/polynomial_ring.pyc in cyclotomic_polynomial(self, n)
   1020             return self.gen() - 1
   1021         else:
-> 1022             return self(cyclotomic.cyclotomic_coeffs(n), check=True)
   1023 
   1024     def gen(self, n=0):

/usr/local/src/sage-config/src/sage/rings/polynomial/cyclotomic.pyx in sage.rings.polynomial.cyclotomic.cyclotomic_coeffs (build/cythonized/sage/rings/polynomial/cyclotomic.c:4932)()
    153     cdef long* coeffs = <long*>sig_malloc(sizeof(long) * (max_deg+1))
    154     if coeffs == NULL:
--> 155         raise MemoryError("Not enough memory to calculate cyclotomic polynomial of %s" % n)
    156     memset(coeffs, 0, sizeof(long) * (max_deg+1))
    157     coeffs[0] = 1

MemoryError: Not enough memory to calculate cyclotomic polynomial of 428914250225777

Change History (9)

comment:1 follow-up: Changed 5 years ago by jdemeyer

  • Description modified (diff)

Never omit the traceback on Trac tickets, they usually contain very useful information!

comment:2 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:3 Changed 5 years ago by jdemeyer

  • Description modified (diff)

comment:4 Changed 5 years ago by jdemeyer

  • Authors set to Jeroen Demeyer
  • Cc rws added

comment:5 Changed 5 years ago by jdemeyer

  • Branch set to u/jdemeyer/weird_memory_error_while_approximating_sin

comment:6 Changed 5 years ago by jdemeyer

  • Commit set to e2fac195237f10bb39da40c489a1cba3ecaeff00
  • Status changed from new to needs_review

New commits:

e2fac19Coerce (not convert) argument of exp() to rational

comment:7 in reply to: ↑ 1 Changed 5 years ago by kcrisman

Never omit the traceback on Trac tickets, they usually contain very useful information!

Haha! Ordinarily I wouldn't, but in this case I didn't have time to do more than simply open the ticket with verbatim from the question so that there was a ticket at all. Sorry! Glad you could clear it up.

comment:8 Changed 5 years ago by rws

  • Reviewers set to Ralf Stephan
  • Status changed from needs_review to positive_review

Looking good. Patchbot says fine.

comment:9 Changed 5 years ago by vbraun

  • Branch changed from u/jdemeyer/weird_memory_error_while_approximating_sin to e2fac195237f10bb39da40c489a1cba3ecaeff00
  • Resolution set to fixed
  • Status changed from positive_review to closed
Note: See TracTickets for help on using tickets.