連分数

単純連分数（1）

リスト {(Subscript[a, 1]),(Subscript[a, 2]),(Subscript[a, 3]),...} を与えて連分数値 (1/((Subscript[a, 1])+(1/((Subscript[a, 2])+⋯))))を返す関数 cFraction[ ]

cFraction[{}] := 0;
cFraction[{a_}] := 1/a;
cFraction[{a_,b__}] := cFraction[{a + cFraction[{b}]}];

例

cFraction[{x,y}]

cFraction[{x,y,z}]

Table[n, {n, 1, 10}]

cFraction[Table[n, {n, 1, 20}]]

Table[cFraction[Table[1,{n}]], {n, 1, 15}]

ListPlot[Table[cFraction[Table[1,{n}]], {n, 5, 20}], Joined->True]

cFraction[{1,1,1,1,1,1,1,1,1,1,1}]

単純連分数（2）

{(Subscript[a, 1]),(Subscript[a, 2]),(Subscript[a, 3])(Subscript[a, 4]),...} を与えて連分数知 (Subscript[a, 1])+(1/((Subscript[a, 2])+(1/((Subscript[a, 3])+(1/((Subscript[a, 4])..))))))を返す

Fold[func,x,{a,b,c}]

FoldList[f,x, {a,b,c,d..}] -> [x, f[x,a], f[f[x,a]],b], ...}

Fold[#1 x+#2&,0,{a,b,c,d}]

continuedFraction[lst_List] :=
	    Fold[(#2 + 1 / #1)&,
		           First[Reverse[lst]], Rest[Reverse[lst]] ]

continuedFraction[{a,b}]

continuedFraction[{a,b,c}]

continuedFraction[{a,b,c,d}]

continuedFraction[{1,1,1,1}]

連分数表示

任意の実数は連分数に展開可能

同等の ContinuedFraction が Mathematica組み込まれている。
 次の関数 cFractionExpansion[] は問題があり得る。
 演習 : その理由を考えよ

cFractionExpansion[x_Real, n_Integer?Positive] :=Module[{gaussPart, fractionPart=x, result = {} },
Do[gaussPart= Floor[fractionPart];
AppendTo[result, gaussPart];
fractionPart = 1 / (fractionPart -  gaussPart),
{n}];
result
]

cFractionExpansion[Sqrt[2],30]

cFractionExpansion[121212/9999]

cFractionExpansion[E,30]

cFractionExpansion[Pi,30]

cFractionExpansion[N[Pi],30]

cFractionExpansion[N[Pi,100],30]

cFractionExpansion[N[GoldenRatio],30]

実数を連分数表示形で表示する

ToString[expr] gives a string corresponding to the printed form of expr in OutputForm.

ToString[x+y+x+y]

expresssCFraction[x_Real, n_Integer?Positive] :=continuedFraction[
Map[ToString, cFractionExpansion[x, n]]
]

expresssCFraction[N[Pi],6]

expresssCFraction[N[GoldenRatio],6]

組み込み連分数関数

Mathematica組み込み関数 ContinuedFraction は賢い

ContinuedFraction[Sqrt[2],30]

ContinuedFraction[121212/9999]

ContinuedFraction[E,30]

ContinuedFraction[Pi,30]

FromContinuedFraction

FromContinuedFraction[{(Subscript[a, 1]),(Subscript[a, 2]),(Subscript[a, 3]),…}]は値(Subscript[a, 1])+(1/((Subscript[a, 2])+(1/((Subscript[a, 3])+(1/((Subscript[a, 4])..))))))を返す

FromContinuedFraction[{a,b,c,d,e,f}]

演習：
 上で定義した自前関数 continuedFraction との差異を考えよ。

FromContinuedFraction[{a,b,c,d,e,f}]

continuedFraction[{a,b,c,d,e,f}] (* our defined function *)

ContinuedFraction[121212/9999]

ContinuedFraction[Sqrt[2], 10]

cFractionExpansion[Sqrt[2],10](* our defined function *)

expresssCFraction[Sqrt[2],10] (* our defined function *)

expresssCFraction[N[Sqrt[2]],10]