From: Nicholas on
I ran across an unexpected behavior of FromContinuedFraction that I
hope someone can help me with. I originally thought is was a
representation problem, but now I am not sure.

When I was playing with the continued fraction solutions of a
quadratic

x^2 -a c x -c = 0,

The continued fraction solution

x = a c + 1/( a + 1/ ( a c + ...

converges if the discriminant (a^2 c^2 + 4 c) is nonnegative. I tried
to do this in Mathematica, using the function "quadcon" defined by

quadcon[a_, c_, n_] :=
FromContinuedFraction[
Take[Flatten(a)Table[{a c, a}, {1/8 (6 + 4 n)}], n]]

which makes a list like {ac, a, ac, a, ac, a} that is then entered
into FromContinuedFraction, but I got some weird results. For
example, when I tried it with a=1, c =-5, (which should converge), I
get that it converges to the right value for the first 90 values of n,
but then starts to wander around after that in fits of convergence:

N[Table[quadcon[1, -5, i], {i, 1, 200}],2]

{-5.0, -4.0, -3.8, -3.7, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -0.077, 1.3, 5.5, 30., -62., -30., -25., -23., -1.2*10^2,
2.0*10^2, 99., -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6,
\
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, 2.1, 2.1, -0.16, 0.57, 5.9, 1.9, 17., 26., \
23., 17., -29., -29., -34., -34., -23., -23., -22., -24., 1.9*10^2,
2.0*10^2}

However, when I call quadcon with real numbers instead, I get the
desired result for as long as I was patient enough to check. For
example,

N(a)Table[quadcon[1., -5., i], {i, 1, 20}]

{-5., -4., -3.75, -3.66667, -3.63636, -3.625, -3.62069, -3.61905, \
-3.61842, -3.61818, -3.61809, -3.61806, -3.61804, -3.61804, -3.61804,
\
-3.61803, -3.61803, -3.61803, -3.61803, -3.61803} ...

This seems to happen when c is negative and the discriminant is
positive. If the discriminant is zero (i.e. c=-1 or -4),
Mathematica's continued fraction converges, and when a>0 and c>0, it
also converges to the correct result.

There are some theorems about convergence of quadratic continued
fractions, but I have not tested these, and the problem persists if I
use rational numbers as well, but I have not looked into it deeply.

The problem seems to be averted if I plug in undetermined values into
quadcon, then replace with numbers after, but I have not tested this
extensively, and it is not reassuring.

eg

N(a)quadcon[-9., -5., 20]

44.8886

N(a)quadcon[-9, -5, 20]

1.26434

N(a)Simplify[quadcon[a, c, 20]] /. {a -> -9, c -> -5}

44.8886


What am I missing?

From: Bob Hanlon on
On my system, I do not get the behavior that you showed

$Version

7.0 for Mac OS X x86 (64-bit) (February 19, 2009)

quadcon[a_, c_, n_] :==
FromContinuedFraction[Take[Flatten@
Table[{a c, a}, {1/8 (6 + 4 n)}], n]]

data == Table[qu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6,-3.6}

However, asking for low precision calculations is not conducive to good results. Recommend that you use NumberForm to control what is displayed

NumberForm[data // N, {4, 1}]


Bob Hanlon

---- Nicholas <physnick(a)gmail.com> wrote:

==========================
I ran across an unexpected behavior of FromContinuedFraction that I
hope someone can help me with. I originally thought is was a
representation problem, but now I am not sure.

When I was playing with the continued fraction solutions of a
quadratic

x^2 -a c x -c == 0,

The continued fraction solution

x == a c + 1/( a + 1/ ( a c + ...

converges if the discriminant (a^2 c^2 + 4 c) is nonnegative. I tried
to do this in Mathematica, using the function "quadcon" defined by

quadcon[a_, c_, n_] :==
FromContinuedFraction[
Take[Flatten(a)Table[{a c, a}, {1/8 (6 + 4 n)}], n]]

which makes a list like {ac, a, ac, a, ac, a} that is then entered
into FromContinuedFraction, but I got some weird results. For
example, when I tried it with a==1, c ==-5, (which should converge), I
get that it converges to the right value for the first 90 values of n,
but then starts to wander around after that in fits of convergence:

N[Table[quadcon[1, -5, i], {i, 1, 200}],2]

{-5.0, -4.0, -3.8, -3.7, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -0.077, 1.3, 5.5, 30., -62., -30., -25., -23., -1.2*10^2,
2.0*10^2, 99., -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6,
\
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, -3.6, \
-3.6, -3.6, -3.6, -3.6, 2.1, 2.1, -0.16, 0.57, 5.9, 1.9, 17., 26., \
23., 17., -29., -29., -34., -34., -23., -23., -22., -24., 1.9*10^2,
2.0*10^2}

However, when I call quadcon with real numbers instead, I get the
desired result for as long as I was patient enough to check. For
example,

N(a)Table[quadcon[1., -5., i], {i, 1, 20}]

{-5., -4., -3.75, -3.66667, -3.63636, -3.625, -3.62069, -3.61905, \
-3.61842, -3.61818, -3.61809, -3.61806, -3.61804, -3.61804, -3.61804,
\
-3.61803, -3.61803, -3.61803, -3.61803, -3.61803} ...

This seems to happen when c is negative and the discriminant is
positive. If the discriminant is zero (i.e. c==-1 or -4),
Mathematica's continued fraction converges, and when a>0 and c>0, it
also converges to the correct result.

There are some theorems about convergence of quadratic continued
fractions, but I have not tested these, and the problem persists if I
use rational numbers as well, but I have not looked into it deeply.

The problem seems to be averted if I plug in undetermined values into
quadcon, then replace with numbers after, but I have not tested this
extensively, and it is not reassuring.

eg

N(a)quadcon[-9., -5., 20]

44.8886

N(a)quadcon[-9, -5, 20]

1.26434

N(a)Simplify[quadcon[a, c, 20]] /. {a -> -9, c -> -5}

44.8886


What am I missing?


--

Bob Hanlon

From: Nicholas on
On Jul 18, 1:03 am, Bob Hanlon <hanl...(a)cox.net> wrote:
> On my system, I do not get the behavior that you showed
>
> $Version
>
> 7.0 for Mac OS X x86 (64-bit) (February 19, 2009)
>
> quadcon[a_, c_, n_] :==
> FromContinuedFraction[Take[Flatten@
> Table[{a c, a}, {1/8 (6 + 4 n)}], n]]
>
> data == Table[quadcon[1, -5, i], {i, 1, 200}];
>
> N[data, 2]
>
> {-5.0,-4.0,-3.8,-3.7,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3.6,-3=
..6,-3.6,-3.6,-3.6,-3.6,-3.6}
>
> However, asking for low precision calculations is not conducive to good r=
esults. Recommend that you use NumberForm to control what is displayed
>
> NumberForm[data // N, {4, 1}]
>
> Bob Hanlon

Bob,

Thank you very much for testing this, and I am surprised that you did
not get the behavior I was talking about. I ran into this trouble on
both of my systems:

7.0 for Microsoft Windows (32-bit) (November 10, 2008)

and

7.0 on a 64-bit Linux distribution at work.

However, I tried it with

5.0 for Microsoft Windows (June 11, 2003)

that happened to be on my system, and I did not seem to encounter the
problem, could it be an earlier version of 7 that is causing the
problem? Would this be fixed with an update? I think I will contact
support.

Additionally, I was interested in the exact results, but to display
them for troubleshooting purposes on the MathGroup, I chose to display
the problem using N since the exact rational numbers would have been
overkill, but I will use NumberForm in the future.

Anyway, thank you for your help.

Nick.

From: Nicholas on
Evidently, according to support, this seems to be a version 7.0.0
problem that was apparently fixed in 7.0.1.