ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 14 Apr 2020 11:05:37 +0200Continued fractions with various algorithmshttps://ask.sagemath.org/question/50749/continued-fractions-with-various-algorithms/Hi,
Can I find a package allowing to compute continued fractions and convergents with an algorithm different than the euclidian division ? nearest integer for example or others...
ThanksoldaniTue, 14 Apr 2020 11:05:37 +0200https://ask.sagemath.org/question/50749/plotting complicated functionhttps://ask.sagemath.org/question/46495/plotting-complicated-function/*I would like to approximate the sum* $$h(a,x) = \frac{-2}{n} \sum_{n=0}^{n-1} log|T_a^n(x)| $$ where $n$ is large like $n= 1000 - 5000$ and for a fixed $a$ $$T_a(x) = \Big|\frac{1}{x}\Big| - \Big\lfloor{\Big|\frac{1}{x}\Big| - 1 +a}\Big\rfloor$$
where $x \in (0,1).$
By fixing $x$ to be a value $x_0 \in (0,1)$, e.g. $x_0 = 1/\pi$, $$h(a, x_0) = h(a, 1/\pi)$$ a function of one variable, and I want to plot a 2D graph of point $(a, h(a, 1/\pi))$, by fixing $n = 2000$, for $a \in [0,1].$
I figure how to calculate the value at one given $a$ using SageMath,
for example, when $a = 1$,
T(x) = 1/x - floor(1/x)
s = 0
for k in xrange(0, 1000):
a = 0
a = nest(T, k, 0.79)
b = abs(a)
c = log(b)
s = s + c
Then $\frac{-2}{1000}s$ give the approximation for the sum when $x = 0.79$, $n = 1000$, $a = 1$.
But for plotting, I think I need to define the function $h(a, x)$ which is a summation over composition of functions. I tried to use `sum` and `symbolic_sum` but failed.
Any help how to achieve this please?PusheenMoewSun, 12 May 2019 19:43:57 +0200https://ask.sagemath.org/question/46495/Recovering numbers from continued fractionhttps://ask.sagemath.org/question/45093/recovering-numbers-from-continued-fraction/Okay, not really, since we won't type in infinitely many numbers. But it would be nice to have a way to convert `[1,1,1,1,1,1,1,1,1]` to a rational approximation of the golden ratio. I couldn't find anything built-in, but maybe I didn't look hard enough? kcrismanFri, 18 Jan 2019 06:02:07 +0100https://ask.sagemath.org/question/45093/ContinuedFractions fail on large integers?https://ask.sagemath.org/question/38309/continuedfractions-fail-on-large-integers/ I've been doing some work with continued fractions, and when I get to a large enough number, I start hitting an error. For example, with the ContinuedFraction from [(18806263158919164762262694978536817267490601162205305175017345804331141023863425152608922362862834892943764814900901973487239748665085369033027389281788183,), [1, 1, 1, 1, 37612526317838329524525389957073634534981202324410610350034691608662282047726850305217844725725669785887529629801803946974479497330170738066054778563576366]]), I get the error below.
Specifically, I get it after I've created a ContinuedFraction with the arguments above, and then call .value() on it. I've tried to make sure all of those are sage Integer types, but it doesn't seem to help, and it looks like internally the CF code is overflowing somewhere. Is there any way around this, or is this a limitation I'll have to live with?
Thanks for any help!
Traceback (most recent call last):
File "/home/tc/Downloads/sagetemp/SageMath/local/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
self.run()
File "/home/tc/Downloads/sagetemp/SageMath/local/lib/python2.7/multiprocessing/process.py", line 114, in run
self._target(*self._args, **self._kwargs)
File "/home/tc/code/cfp/pi/piX/ones.py", line 16, in leeloo
v = ocf.value()
File "/home/tc/Downloads/sagetemp/SageMath/local/lib/python2.7/site-packages/sage/rings/continued_fraction.py", line 1423, in value
Q = QuadraticField(DD, 'sqrt%d' % DD)
File "/home/tc/Downloads/sagetemp/SageMath/local/lib/python2.7/site-packages/sage/rings/number_field/number_field.py", line 922, in QuadraticField
return NumberField(f, name, check=False, embedding=embedding, latex_name=latex_name, **args)
File "/home/tc/Downloads/sagetemp/SageMath/local/lib/python2.7/site-packages/sage/rings/number_field/number_field.py", line 524, in NumberField
return NumberField_version2(polynomial=polynomial, name=name, check=check, embedding=embedding, latex_name=latex_name, assume_disc_small=assume_disc_small, maximize_at_primes=maximize_at_primes, structure=structure)
File "sage/structure/factory.pyx", line 362, in sage.structure.factory.UniqueFactory.__call__ (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/structure/factory.c:1856)
File "/home/tc/Downloads/sagetemp/SageMath/local/lib/python2.7/site-packages/sage/rings/number_field/number_field.py", line 612, in create_key_and_extra_args
x = number_field_morphisms.root_from_approx(polynomial, embedding)
File "sage/rings/number_field/number_field_morphisms.pyx", line 490, in sage.rings.number_field.number_field_morphisms.root_from_approx (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/number_field/number_field_morphisms.c:7640)
File "sage/rings/real_lazy.pyx", line 1584, in sage.rings.real_lazy.LazyAlgebraic.__init__ (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/real_lazy.c:17914)
File "sage/rings/polynomial/polynomial_element.pyx", line 7247, in sage.rings.polynomial.polynomial_element.Polynomial.roots (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/polynomial/polynomial_element.c:68115)
File "sage/rings/polynomial/polynomial_element.pyx", line 7143, in sage.rings.polynomial.polynomial_element.Polynomial.roots (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/polynomial/polynomial_element.c:64632)
File "sage/rings/polynomial/polynomial_element.pyx", line 5786, in sage.rings.polynomial.polynomial_element.Polynomial._pari_ (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/polynomial/polynomial_element.c:55229)
File "sage/rings/polynomial/polynomial_element.pyx", line 5839, in sage.rings.polynomial.polynomial_element.Polynomial._pari_with_name (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/polynomial/polynomial_element.c:55757)
File "sage/rings/real_mpfr.pyx", line 3103, in sage.rings.real_mpfr.RealNumber._pari_ (/home/tc/Downloads/sagetemp/SageMath/src/build/cythonized/sage/rings/real_mpfr.c:22771)
ValueError: Cannot convert NaN or infinity to Pari floatcappalloTue, 18 Jul 2017 16:31:37 +0200https://ask.sagemath.org/question/38309/Coercion on continued fractionshttps://ask.sagemath.org/question/33131/coercion-on-continued-fractions/Currently, when user types
sage: x = continued_fraction(pi)
sage: y = 3*x
an TypeError is raised saying
TypeError: unsupported operand parent(s) for '*': 'Integer Ring' and '<class 'sage.rings.continued_fraction.ContinuedFraction_real'>'
I understand that Sage tries to find common parent of Integer element and `ContinuedFraction_real` and fails. It seems that continued fractions in Sage are not implemented as elements of any particular field, and are derived directly from `SageObject`.
I am implementing an algorithm for arithmetical operations on continued fractions. Is it possible to make Sage call a particular method when the user tries to perform an arithmetical binary operation, where one of the operands is a continued fraction?
I have found those [1](http://doc.sagemath.org/html/en/reference/coercion/index.html#methods-to-implement), [2](http://doc.sagemath.org/html/en/reference/coercion/sage/structure/coerce.html), [3](http://doc.sagemath.org/html/en/tutorial/tour_coercion.html) links, but it seems that the topic of coercion is rather extensive and I don't know what to look for, as I don't know what the correct solution to the problem is.
Thank you for any response!mirgeeSun, 17 Apr 2016 14:49:52 +0200https://ask.sagemath.org/question/33131/Implementing new CF class advicehttps://ask.sagemath.org/question/32672/implementing-new-cf-class-advice/My goal is to implement Gosper's algorithm for algebraic operations on continued fractions into Sage. To begin with, I would like to implement a functionality to perform a homographic transformation
$$x \rightarrow \frac{ax + b}{cx + d}$$
where $x$ is a CF.
The easiest way to do this is probably to create a new class in `sage.rings.continued_fraction` that would on inicialization accept the integer constants $a, b, c, d$ and a descendant of `ContinuedFraction_base` class. The most important method of this class would be `next()` or `_iter_()`, which would read the input $x$ until it can output new term using Gosper's algorithm.
How do I create an instance of this class with proper arguments, when for example $(3*x + 1)/2$, where $x$ is a CF, is called from Sage? (Instead of `_mul_()` being called, than `_add_()` and so on...)
Simplifying using inbuilt Sage methods and then evaluating the expression as a string using regular expressions somehow (like [this](http://sunjay.ca/2014/04/27/evaluating-simple-math-expressions-using-python-and-regular-expressions/)) might be a solution, but I am not sure if the best one.
Thank you very much for any advice on this!
EDIT: I have found this promising [link](http://doc.sagemath.org/html/en/thematic_tutorials/coercion_and_categories.html), but haven't found anything useful in there so far...mirgeeFri, 26 Feb 2016 07:29:45 +0100https://ask.sagemath.org/question/32672/Continued fraction of pi by handhttps://ask.sagemath.org/question/10702/continued-fraction-of-pi-by-hand/Hello, I'm a newbie in Sage.
I tried to compute (sagenb.org) the continued fraction of pi by hand with the following commands
<pre><code>n=15
x=range(n+1);a=range(n)
x[0]=pi
for i in range(n):
a[i] = int(x[i])
x[i+1]=1/(x[i]-a[i])
print(a[i])
</code></pre>
the answer (14 terms) is correct, but then I get
<pre><code>Traceback (click to the left of this block for traceback)
...
ValueError: Calling floor() on infinity or NaN</code></pre>
Am I doing something wrong?
CheersMarco CaliariWed, 06 Nov 2013 04:41:54 +0100https://ask.sagemath.org/question/10702/Continued fraction expansion of quadratic irrationalshttps://ask.sagemath.org/question/8115/continued-fraction-expansion-of-quadratic-irrationals/Let me emphasize that **I am very new to sage** and to computing in general, but I did research as much as I could before asking this question.
It is well know that that quadratic irrationals has **eventually** periodic continued fraction expansion. I really tried to find a function that gives me the period of a given quadratic irrational. The best I could find was in this forum:
http://www.mail-archive.com/sage-support@googlegroups.com/msg04201.html
It explains that you can get using GAP the PrePeriod+Period. It has two disadvantages: the first, you can't get the pure period, i.e., without the preperiod, and the second is that it is the input is not the number itself but the polynomial it solve. If this polynomial has many positive roots it is a problem...
I tried to look also in PARI and didn't find anything.
I did see that it is written that sage itself does not have this function.
Let me also remark that such algorithm exist.
My question: Is there a way (via packages that are contained in sage) to find the period of a given quadratic irrational?
Thanks a lot!
Menny MennyMon, 16 May 2011 02:49:17 +0200https://ask.sagemath.org/question/8115/