大衍之数五十,其用四十有九。分而为二以象两,挂一以象三,揲之以四以象四时,归奇于扐以象闰。五岁再闰,故再扐而后挂。天一,地二;天三,地四;天五,地六;天七,地八;天九,地十。天数五,地数五。五位相得而各有合,天数二十有五,地数三十,凡天地之数五十有五,此所以成变化而行鬼神也。乾之策二百一十有六,坤之策百四十有四,凡三百六十,当期之日。二篇之策,万有一千五百二十,当万物之数也。是故四营而成《易》,十有八变而成卦,八卦而小成。引而伸之,触类而长之,天下之能事毕矣。显道神德行,是故可与酬酢,可与祐神矣。子曰:“知变化之道者,其知神之所为乎。”
大衍之数五十,存一不用,构造天地人三者,历经三变,第一次的余数是5或9,第二次的是4或8,第三次的是4或8,剩下的数量除以4就是结果。即为一爻,算六爻要一个小时。古人构造随机数的方法太费时间啦。用Python写个程序来搞吧!
In [1]:
import random
In [23]:
def sepSkyEarth(data):
sky = random.randint(1, data-2)
earth = data - sky
earth -= 1
return sky , earth
def getRemainder(num):
rm = num % 4
if rm == 0:
rm = 4
return rm
def getChange(data):
sky, earth = sepSkyEarth(data)
skyRemainder = getRemainder(sky)
earthRemainder = getRemainder(earth)
change = skyRemainder + earthRemainder + 1
data = data - change
return sky, earth, change, data
def getYao(data):
sky, earth, firstChange, data = getChange(data)
sky, earth, secondChange, data = getChange(data)
sky, earth, thirdChange, data = getChange(data)
yao = data/4
return yao, firstChange, secondChange, thirdChange
def sixYao():
yao1 = getYao(data = 50 - 1)[0]
yao2 = getYao(data = 50 - 1)[0]
yao3 = getYao(data = 50 - 1)[0]
yao4 = getYao(data = 50 - 1)[0]
yao5 = getYao(data = 50 - 1)[0]
yao6 = getYao(data = 50 - 1)[0]
return[yao1, yao2, yao3, yao4, yao5, yao6]
def fixYao(num):
if num == 6 or num == 9:
print "there is a changing predict! Also run changePredict()"
return num % 2
def changeYao(num):
if num == 6:
num = 1
elif num == 9:
num = 2
num = num % 2
return(num)
def fixPredict(pred):
fixprd = [fixYao(i) for i in pred]
fixprd = list2str(fixprd)
return fixprd
def list2str(l):
si = ''
for i in l:
si = si + str(i)
return si
def changePredict(pred):
changeprd = [changeYao(i) for i in pred]
changeprd = list2str(changeprd)
return changeprd
def getPredict():
pred = sixYao()
fixPred = fixPredict(pred)
if 6 in pred or 9 in pred:
changePred = changePredict(pred)
else:
changePred = None
return fixPred, changePred
def interpretPredict(now, future):
dt = {'111111':'乾','011111':'夬','000000':'坤','010001':'屯','100010':'蒙','010111':'需','111010':'讼','000010':'师',
'010000':'比','110111':'小畜','111011':'履','000111':'泰','111000':'否','111101':'同人','101111':'大有','000100':'谦',
'001000':'豫','011001':'随','100110':'蛊','000011':'临','110000':'观','101001':'噬嗑','100101':'贲','100000':'剥',
'000001':'复','111001':'无妄','100111':'大畜','100001':'颐','011110':'大过','010010':'坎','101101':'离','011100':'咸',
'001110':'恒','111100':'遁','001111':'大壮','101000':'晋','000101':'明夷','110101':'家人','101011':'睽','010100':'蹇',
'001010':'解','100011':'损','110001':'益','111110':'姤','011000':'萃','000110':'升','011010':'困','010110':'井',
'011101':'革','101110':'鼎','001001':'震','100100':'艮','110100':'渐','001011':'归妹','001101':'丰','101100':'旅',
'110110':'巽','011011':'兑','110010':'涣','010011':'节','110011':'中孚','001100':'小过','010101':'既济','101010':'未济'}
if future:
name = dt[now] + ' & ' + dt[future]
else:
name = dt[now]
print name
def plotTransitionRemainder(N, w):
import matplotlib.cm as cm
import matplotlib.pyplot as plt
from collections import defaultdict
changes = {}
for i in range(N):
sky, earth, firstChange, data = getChange(data = 50 -1)
sky, earth, secondChange, data = getChange(data)
sky, earth, thirdChange, data = getChange(data)
changes[i]=[firstChange, secondChange, thirdChange, data/4]
ichanges = changes.values()
firstTransition = defaultdict(int)
for i in ichanges:
firstTransition[i[0], i[1]]+=1
secondTransition = defaultdict(int)
for i in ichanges:
secondTransition[i[1], i[2]]+=1
thirdTransition = defaultdict(int)
for i in ichanges:
thirdTransition[i[2], i[3]]+=1
cmap = cm.get_cmap('Accent_r', len(ichanges))
for k, v in firstTransition.iteritems():
plt.plot([1, 2], k, linewidth = v*w/N)
for k, v in secondTransition.iteritems():
plt.plot([2, 3], k, linewidth = v*w/N)
for k, v in thirdTransition.iteritems():
plt.plot([3, 4], k, linewidth = v*w/N)
plt.xlabel(u'Time')
plt.ylabel(u'Changes')
In [104]:
data = 50 - 1
In [105]:
sky, earth, firstChange, data = getChange(data)
print sky, '\n', earth, '\n',firstChange, '\n', data
In [106]:
sky, earth, secondChange, data = getChange(data)
print sky, '\n', earth, '\n',secondChange, '\n', data
In [107]:
sky, earth, thirdChange, data = getChange(data)
print sky, '\n', earth, '\n',thirdChange, '\n', data
In [234]:
getPredict()
Out[234]:
In [233]:
getPredict()
Out[233]:
In [251]:
getPredict()
Out[251]:
In [232]:
fixPred, changePred = getPredict()
interpretPredict(fixPred, changePred )
In [57]:
#http://baike.fututa.com/zhouyi64gua/
import urllib2
from bs4 import BeautifulSoup
import os
# set work directory
os.chdir('/Users/chengjun/github/iching/')
dt = {'111111':'乾','011111':'夬','000000':'坤','010001':'屯','100010':'蒙','010111':'需','111010':'讼','000010':'师',
'010000':'比','110111':'小畜','111011':'履','000111':'泰','111000':'否','111101':'同人','10111':'大有','000100':'谦',
'001000':'豫','011001':'随','100110':'蛊','000011':'临','110000':'观','101001':'噬嗑','100101':'贲','100000':'剥',
'000001':'复','111001':'无妄','100111':'大畜','100001':'颐','011110':'大过','010010':'坎','101101':'离','011100':'咸',
'001110':'恒','111100':'遁','001111':'大壮','101000':'晋','000101':'明夷','110101':'家人','101011':'睽','010100':'蹇',
'001010':'解','100011':'损','110001':'益','111110':'姤','011000':'萃','000110':'升','011010':'困','010110':'井',
'011101':'革','101110':'鼎','001001':'震','100100':'艮','110100':'渐','001011':'归妹','001101':'丰','101100':'旅',
'110110':'巽','011011':'兑','110010':'涣','010011':'节','110011':'中孚','001100':'小过','010101':'既济','101010':'未济'}
dr = {}
for i, j in dt.iteritems():
dr[unicode(j, 'utf8')]= i
In [60]:
url = "http://baike.fututa.com/zhouyi64gua/"
content = urllib2.urlopen(url).read() #获取网页的html文本
soup = BeautifulSoup(content)
articles = soup.find_all('div', {'class', 'gualist'})[0].find_all('a')
links = [i['href'] for i in articles]
In [61]:
links[:2]
Out[61]:
In [64]:
dtext = {}
from time import sleep
num = 0
for j in links:
sleep(0.1)
num += 1
ghtml = urllib2.urlopen(j).read() #获取网页的html文本
print j, num
gua = BeautifulSoup(ghtml, from_encoding = 'gb18030')
guaName = gua.title.text.split('_')[1].split(u'卦')[0]
guaId = dr[guaName]
guawen = gua.find_all('div', {'class', 'gua_wen'})
guaText = []
for i in guawen:
guaText.append(i.get_text() + '\n\n')
guaText = ''.join(guaText)
dtext[guaId] = guaText
In [67]:
dtextu = {}
for i, j in dtext.iteritems():
dtextu[i]= j.encode('utf-8')
In [75]:
dtext.values()[0]
Out[75]:
In [93]:
import json
with open("/Users/chengjun/github/iching/package_data.dat",'w') as outfile:
json.dump(dtextu, outfile, ensure_ascii=False) #, encoding = 'utf-8')
In [90]:
dat = json.load(open('package_data.dat'), encoding='utf-8')
In [92]:
print dat.values()[1]
In [35]:
now, future = getPredict()
In [38]:
def ichingText(k):
import json
dat = json.load(open('iching/package_data.dat'))
print dat[k]
In [40]:
ichingText(future)
In [29]:
%matplotlib inline
plotTransitionRemainder(10000, w = 50)
In [31]:
%matplotlib inline
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(15, 10),facecolor='white')
plt.subplot(2, 2, 1)
plotTransitionRemainder(1000, w = 50)
plt.subplot(2, 2, 2)
plotTransitionRemainder(1000, w = 50)
plt.subplot(2, 2, 3)
plotTransitionRemainder(1000, w = 50)
plt.subplot(2, 2, 4)
plotTransitionRemainder(1000, w = 50)
In [48]:
dt = {'111111':u'乾','011111':u'夬','000000':u'坤','010001':u'屯','100010':u'蒙','010111':u'需','111010':u'讼','000010':'师',
'010000':u'比','110111':u'小畜','111011':u'履','000111':u'泰','111000':u'否','111101':u'同人','101111':u'大有','000100':u'谦',
'001000':u'豫','011001':u'随','100110':u'蛊','000011':u'临','110000':u'观','101001':u'噬嗑','100101':u'贲','100000':'u剥',
'000001':u'复','111001':u'无妄','100111':u'大畜','100001':u'颐','011110':u'大过','010010':u'坎','101101':u'离','011100':u'咸',
'001110':u'恒','111100':u'遁','001111':u'大壮','101000':u'晋','000101':u'明夷','110101':u'家人','101011':u'睽','010100':u'蹇',
'001010':u'解','100011':u'损','110001':u'益','111110':u'姤','011000':u'萃','000110':u'升','011010':u'困','010110':u'井',
'011101':u'革','101110':u'鼎','001001':u'震','100100':u'艮','110100':u'渐','001011':u'归妹','001101':u'丰','101100':u'旅',
'110110':u'巽','011011':u'兑','110010':u'涣','010011':u'节','110011':u'中孚','001100':u'小过','010101':u'既济','101010':u'未济'
}
In [50]:
for i in dt.values():
print i
In [39]:
dtu = {}
for i, j in dt.iteritems():
dtu[i] = unicode(j, 'utf-8')
In [44]:
def ichingDate(d):
import random
random.seed(d)
try:
print 'Your birthday & your prediction time:', str(d)
except:
print('Your birthday & your prediction time:', str(d))