L4K ︰ Python Turtle《十下》

『通』與『用』熟重的耶?『通』而會『用』,『用』而能『通』而已乎!!??

玉連環

玉連環

九連環

九連環

Baguenaudier.svg

Baguenaudier

戰國策‧卷十三

齊 閔王之遇殺,其子法章變姓名 ,為莒太史家庸夫。太史敫女,奇法章之狀貌,以為非常人,憐而常竊衣食之,與私焉。莒中及齊亡臣相聚,求閔王子,欲立之 。法章 乃自言於莒。共立法章為襄王。襄王立,以太史氏女為王后,生子建。太史敫曰:『女無謀而嫁者,非吾種也,汙吾世矣 。』終身不睹。君王后賢,不以不睹之故, 失人子之禮也。

襄王卒,子建立為齊王。君王后事秦謹,與諸侯信,以故建立四十有餘年不受兵。

秦始皇嘗使使者遺君王后玉連環 ,曰:『齊多知,而解此環不? 』君王后以示群臣,群臣不知解 。君王后椎椎破之,謝秦使曰 :『謹以解矣。』

及君王后病且卒,誡建曰:『群臣之可用者某』。建曰:『請書之。』君王后曰:『善。』取筆牘受言。君王后曰:『老婦已亡 矣!

君王后死,後后勝相齊,多受秦間金玉,使賓客入秦,皆為變辭 ,勸王朝秦,不脩攻戰之備。

卓文君』為四川臨卭巨商卓王孫之女,姿嬌貌美,精通音律,善彈琴,富文采。司馬相如到卓王孫家裡赴宴,聞知『文君新寡』,便奏了一曲《鳳求凰》,於是夜『連夜私奔』逃到了成都。

傳說她倆也曾過得幾年鴛鴦生活,據聞當司馬相如在長安,被封為中郎將時,自覺身份已是不凡,興起休妻的念頭,便寫了一封『數字信』︰一二三四五六七八九十百千 萬;且囑送信人必得帶回『回信』。冰雪聰明『卓文君』頓然識破乃『無億』也,是『無憶』又『無意』的啊!於是回了封《怨郎詩》給司馬相如:

一別之後,二地相思,只說三四月,又誰知五六年。七弦琴無心彈,八行書無可傳,九連環從中折斷, 十里長亭望眼欲穿。百思量,千繫念,萬般無奈把郎怨。萬語千言說不完,百無聊賴十依欄,重九登高看孤雁,八月中秋月圓人不圓。七月半燒香秉燭問蒼天,六月 伏天人人搖扇我心寒。五月石榴如火偏遇陣陣冷雨澆花端,四月枇杷未黃我欲對鏡心意亂,三月桃花隨水轉,飄零零,二月風箏線兒斷。噫,郎呀郎,巴不得下一世 你為女來我為男。

這一千古奇聞,至今考證不完。

220px-Zhuo_Wenjun
西京雜記
眉色如望遠山,
臉際常若芙蓉,
肌膚柔滑如脂。

白頭吟》卓文君

皚如山上雪,  皎若雲間月。
聞君有兩意,  故來相決絕。
今日斗酒會,  明旦溝水頭。
躞蹀御溝上,  溝水東西流。
淒淒復淒淒,  嫁娶不須啼。
願得一心人,  白頭不相離。
竹竿何嫋嫋,  魚尾何簁簁。
男兒重意氣,  何用錢刀為。

於是乎『九連環』出自何時何處誰人之手,眾說紛紜『莫宰羊』矣。『惠施』一句【連環可解也。】是『可解』是『不可解』的呢?『攻玉石』而得『玉連環』本來自『一石』,秦國之意明矣,非在問『』,以『技力』『』之也,而『君王后』引『』『』之,是知其『不可解』,寧可『玉碎』以為之『』,乃『不畏刀兵』之意也明矣。一九三一年時,『哥德爾』發表了兩條『哥德爾不完備定理』︰

一、任何『相容』的形式系統,只要蘊涵著『皮亞諾算術公理』,就一定可以在之中構造出體系裡既『不能證明』也『不能否證』的『命題』,也就是說那個體系是『不完備』的。

二、任何『相容』的形式系統,只要蘊涵著『皮亞諾算術公理』,它就無法用此來證明它自身的『相容性』。

由此看來若是『連環』出自『天工』,自是『無縫而成』,自然就『無可解』,那麼『惠施』的時代果真已有『九連環』的嗎?假使那時真有『九連環』的話,『惠施』此言又在說什麼呢??或許以『名家思辨』揣想『既然可以成連環,必然可之解連環』,也許這就是『惠施』所說的【連環可解也。】之理的吧!這個『順逆操作觀』就是理解『九連環數學』的關鍵。據聞,西方人多以為『九連環』是『諸葛孔明』所作,因他經年帶兵打仗,為排遣妻子寂寞而發明。之後,那個講『梵天寺』之『浮屠』── 塔 ──的傳說之法國數學家『Édouard Lucas』最早用『二進制』觀點,給出了 n 連環的最少步驟之『一般解』︰

a(n) = \begin{cases} \frac{2^{n+1}-2}{3}, & \text{when }n\text{ is even,}\\ \frac{2^{n+1}-1}{3}, & \text{when }n\text{ is odd.}\end{cases}

─── 摘自《【Sonic π】電聲學之電路學《四》之《 V!!》

 

苟知其理︰

Recurrence relation

In mathematics, a recurrence relation is an equation that recursively defines a sequence or multidimensional array of values, once one or more initial terms are given: each further term of the sequence or array is defined as a function of the preceding terms.

The term difference equation sometimes (and for the purposes of this article) refers to a specific type of recurrence relation. However, “difference equation” is frequently used to refer to any recurrence relation.

……

Solving

Solving homogeneous linear recurrence relations with constant coefficients

Roots of the characteristic polynomial

An order-d homogeneous linear recurrence with constant coefficients is an equation of the form

a_{n}=c_{1}a_{n-1}+c_{2}a_{n-2}+\cdots +c_{d}a_{n-d},

where the d coefficients ci (for all i) are constants.

A constant-recursive sequence is a sequence satisfying a recurrence of this form. There are d degrees of freedom for solutions to this recurrence, i.e., the initial values a 0 , … , a d − 1 {\displaystyle a_{0},\dots ,a_{d-1}} a_{0},\dots ,a_{d-1} can be taken to be any values but then the recurrence determines the sequence uniquely.

The same coefficients yield the characteristic polynomial (also “auxiliary polynomial”)

p(t)=t^{d}-c_{1}t^{d-1}-c_{2}t^{d-2}-\cdots -c_{d}

whose d roots play a crucial role in finding and understanding the sequences satisfying the recurrence. If the roots r1, r2, … are all distinct, then each solution to the recurrence takes the form

a_{n}=k_{1}r_{1}^{n}+k_{2}r_{2}^{n}+\cdots +k_{d}r_{d}^{n},

where the coefficients ki are determined in order to fit the initial conditions of the recurrence. When the same roots occur multiple times, the terms in this formula corresponding to the second and later occurrences of the same root are multiplied by increasing powers of n. For instance, if the characteristic polynomial can be factored as (xr)3, with the same root r occurring three times, then the solution would take the form

a_{n}=k_{1}r^{n}+k_{2}nr^{n}+k_{3}n^{2}r^{n}.[2]

As well as the Fibonacci numbers, other constant-recursive sequences include the Lucas numbers and Lucas sequences, the Jacobsthal numbers, the Pell numbers and more generally the solutions to Pell’s equation.

For order 1, the recurrence

  a_{n}=ra_{n-1}

has the solution an = rn with a0 = 1 and the most general solution is an = krn with a0 = k. The characteristic polynomial equated to zero (the characteristic equation) is simply t − r = 0.

Solutions to such recurrence relations of higher order are found by systematic means, often using the fact that an = rn is a solution for the recurrence exactly when t = r is a root of the characteristic polynomial. This can be approached directly or using generating functions (formal power series) or matrices.

……

Solving non-homogeneous linear recurrence relations with constant coefficients

If the recurrence is non-homogeneous, a particular solution can be found by the method of undetermined coefficients and the solution is the sum of the solution of the homogeneous and the particular solutions. Another method to solve an non-homogeneous recurrence is the method of symbolic differentiation. For example, consider the following recurrence:

a_{n+1}=a_{n}+1

This is an non-homogeneous recurrence. If we substitute nn+1, we obtain the recurrence

a_{n+2}=a_{n+1}+1

Subtracting the original recurrence from this equation yields

{\displaystyle a_{n+2}-a_{n+1}=a_{n+1}-a_{n}}

or equivalently

a_{n+2}=2a_{n+1}-a_{n}

This is a homogeneous recurrence, which can be solved by the methods explained above. In general, if a linear recurrence has the form

a_{n+k}=\lambda _{k-1}a_{n+k-1}+\lambda _{k-2}a_{n+k-2}+\cdots +\lambda _{1}a_{n+1}+\lambda _{0}a_{n}+p(n)

where  \lambda _{0},\lambda _{1},\dots ,\lambda _{k-1} are constant coefficients and p(n) is the inhomogeneity, then if p(n) is a polynomial with degree r, then this non-homogeneous recurrence can be reduced to a homogeneous recurrence by applying the method of symbolic differencing r times.

If

  P(x)=\sum _{n=0}^{\infty }p_{n}x^{n}

is the generating function of the inhomogeneity, the generating function

A(x)=\sum _{n=0}^{\infty }a(n)x^{n}

of the non-homogeneous recurrence

a_{n}=\sum _{i=1}^{s}c_{i}a_{n-i}+p_{n},\quad n\geq n_{r},

with constant coefficients ci is derived from

\left(1-\sum _{i=1}^{s}c_{i}x^{i}\right)A(x)=P(x)+\sum _{n=0}^{n_{r}-1}[a_{n}-p_{n}]x^{n}-\sum _{i=1}^{s}c_{i}x^{i}\sum _{n=0}^{n_{r}-i-1}a_{n}x^{n}.

If P(x) is a rational generating function, A(x) is also one. The case discussed above, where pn = K is a constant, emerges as one example of this formula, with P(x) = K/(1−x). Another example, the recurrence } a_{n}=10a_{n-1}+n with linear inhomogeneity, arises in the definition of the schizophrenic numbers. The solution of homogeneous recurrences is incorporated as p = P = 0.

 

且善其工︰

pi@raspberrypi:~ $ python3
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from sympy import Function, rsolve
>>> from sympy.abc import n
>>> y = Function('y')
>>> k = y(n+3)- 7*y(n+2) + 12*y(n+1) - 10*y(n)
>>> rsolve(k, y(n))
5**n*C0 + C1*(1 - I)**n + C2*(1 + I)**n
>>> k = y(n+2)-2*y(n)-y(n+1)-1
>>> rsolve(k, y(n), {y(1):1, y(2):2})
-(-1)**n/6 + 2*2**n/3 - 1/2
>>> 

【※ 參考資料】

Recurrence Equations

sympy.solvers.recurr.rsolve(f, y, init=None)
Solve univariate recurrence with rational coefficients.

Given k-th order linear recurrence Ly=f, or equivalently:

ak(n)y(n+k)+ak1(n)y(n+k1)++a0(n)y(n)=f(n)

where ai(n), for i=0,,k, are polynomials or rational functions in n, and f is a hypergeometric function or a sum of a fixed number of pairwise dissimilar hypergeometric terms in n, finds all solutions or returns None, if none were found.

Initial conditions can be given as a dictionary in two forms:

  1. {   n_0  : v_0,   n_1  : v_1, ...,   n_m  : v_m }
  2. { y(n_0) : v_0, y(n_1) : v_1, ..., y(n_m) : v_m }

or as a list L of values:

L = [ v_0, v_1, ..., v_m ]

where L[i] = v_i, for i=0,,m, maps to y(ni).

4.3 Using SymPy

SymPy is a pure Python package included in Sage, but not integrated with Sage.

http://docs.sympy.org/dev/modules/solvers/solvers.html#recurrence-equtions (sic)

Import pieces of the SymPy library.

from sympy import Function, rsolve
from sympy.abc import n
y = Function('y')

Define the recurrence as an expression in a() that equals zero.

k = y(n+3)- 7*y(n+2) + 12*y(n+1) - 10*y(n)

And solve:

rsolve(k, y(n))

 

,弗得悠遊天地哉??!!