<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>阅微堂 &#187; 算法</title>
	<atom:link href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95/feed" rel="self" type="application/rss+xml" />
	<link>http://zhiqiang.org/blog</link>
	<description>数学、金融、计算机</description>
	<lastBuildDate>Sat, 19 May 2012 13:47:10 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>最大回撤和最大短期回撤的线性算法</title>
		<link>http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html#comments</comments>
		<pubDate>Sun, 08 Jan 2012 02:40:49 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[drawdown]]></category>
		<category><![CDATA[我贡献的源代码]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=2952</guid>
		<description><![CDATA[博客 » 计算机科学 » drawdown，我贡献的源代码，算法 » 系列：我贡献的源代码 查看该系列所有文章 最大回撤是一个重要的风险指标。对于对冲基金和数量化策略交易，这个指标比波动率还重要。 最大回撤 定义：对于序列，定义最大回撤为 根据上述等式，很容易得到一个最大回撤的O(n)的算法。 最大短期回撤 最大回撤发生的时间跨度可以非常长。现在我们定义局部最大回撤...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/science/computer-science">计算机科学</a> » <a href="http://zhiqiang.org/blog/tag/drawdown" rel="tag">drawdown</a>，<a href="http://zhiqiang.org/blog/tag/zhiqiangcodes" rel="tag">我贡献的源代码</a>，<a href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a> » </p><div class="series"><span>系列：<b>我贡献的源代码</b></span><br/>
<a href="http://zhiqiang.org/blog/tag/zhiqiangcodes">查看该系列所有文章</a>
<div id='series'></div>
</div>  <p>最大回撤是一个重要的风险指标。对于对冲基金和数量化策略交易，这个指标比波动率还重要。</p>
<h3>最大回撤</h3>
<p>定义：对于序列<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_edb33204eb8ebe0c551cba602ad511cb.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="x_1,x_2,\cdots,x_n" /></span><script type='math/tex'>x_1,x_2,\cdots,x_n</script>，定义最大回撤<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_8277e0910d750195b448797616e091ad.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="d" /></span><script type='math/tex'>d</script>为</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_82aba616b50d56608fce8977f057bc52.gif' style='vertical-align: middle; border: none;' class='tex' alt="d = \min_{i\leq j} (x_j - x_i) = \min_j (x_j - \max_{i\leq j} x_i)" /></span><script type='math/tex;  mode=display'>d = \min_{i\leq j} (x_j - x_i) = \min_j (x_j - \max_{i\leq j} x_i)</script></p></p>
<p>根据上述等式，很容易得到一个最大回撤的O(n)的算法。</p>
<h3>最大短期回撤</h3>
<p>最大回撤发生的时间跨度可以非常长。现在我们定义局部最大回撤，区别在于限制回撤发生的时间跨度，对给定的跨度<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_8ce4b16b22b58894aa86c421e8759df3.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="k" /></span><script type='math/tex'>k</script>，定义最大短期回撤<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_7516b96678349ed002f1931a294f577c.gif' style='vertical-align: middle; border: none; ' class='tex' alt="d_k" /></span><script type='math/tex'>d_k</script>为：</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_166c8b4d480896da7f6d4470c501101a.gif' style='vertical-align: middle; border: none;' class='tex' alt="d_k = \min_{0\leq j-i\leq k} (x_j - x_i)= \min_j (x_j - \max_{j-k\leq i\leq j} x_i)" /></span><script type='math/tex;  mode=display'>d_k = \min_{0\leq j-i\leq k} (x_j - x_i)= \min_j (x_j - \max_{j-k\leq i\leq j} x_i)</script></p></p>
<p>这个问题在网上贴出来一个小时，Yangzhe1990和uni（其中uni是我7年前参加ACM的队友，这么多年过去他还宝刀未老）就给出了算法。具体方法这两位在留言处写得比较清楚了，这里不再阐述。</p>
<blockquote><p>最大短期回撤在风险度量上是有意义的。对于一个指数（或者产品），如果我们会长时间持有，那么直接考虑最大回撤。但若策略中限定了持有时间，最大短期回撤的指标会更合适一些。</p></blockquote>
<h3>VBA程序</h3>
<p>对上面的算法做了实现，见下面的Excel模板，内含函数演示。VBA代码有详细注释。</p>
Note: There is a file embedded within this post, please visit this post to download the file.
<p>&nbsp;</p>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html">最大回撤和最大短期回撤的线性算法</a></li><li ><a href="http://zhiqiang.org/blog/it/date-series-in-matlab-excel-sql.html">Matlab、Excel、SQL中的日期的数字序列形式</a></li><li ><a href="http://zhiqiang.org/blog/it/use-adodb-in-matlab.html">Matlab中使用ADODB访问数据库</a></li><li ><a href="http://zhiqiang.org/blog/it/pivottable-in-matlab.html">Matlab里的PivotTable</a></li><li ><a href="http://zhiqiang.org/blog/finance/riskmanage/var-excel-vba-code.html">计算VaR的VBA代码和Excel模板</a></li><li ><a href="http://zhiqiang.org/blog/it/options-profile-generator.html">期权到期收益结构生成器</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">有序矩阵的中位数算法</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2012. | <a href="http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html#comments">15 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/max-drawdown-algorithm.html/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>图片搜索的原理</title>
		<link>http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html#comments</comments>
		<pubDate>Thu, 21 Jul 2011 12:52:30 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[图片搜索]]></category>
		<category><![CDATA[搜索]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=2393</guid>
		<description><![CDATA[博客 » 计算机科学 » 图片搜索，搜索，算法 » Google新推出了图片搜索，可直接上传图片（或者用图片链接）搜索网络上的相似图片，例子。估计还没多少人意识到，这玩意儿是人肉搜索的大杀器，以后大家还是少上传私人照片到公开网络。 阮一峰介绍了一个简单的图片搜索原理，可分为下面几步： 缩小尺寸。将图片缩小到8x8的尺寸，总共64个像素。这一步的作用是去除图片...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/science/computer-science">计算机科学</a> » <a href="http://zhiqiang.org/blog/tag/%e5%9b%be%e7%89%87%e6%90%9c%e7%b4%a2" rel="tag">图片搜索</a>，<a href="http://zhiqiang.org/blog/tag/%e6%90%9c%e7%b4%a2" rel="tag">搜索</a>，<a href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a> » </p><p>Google新推出了<a title="图片搜索" href="http://images.google.com.hk/" target="_blank">图片搜索</a>，可直接上传图片（或者用图片链接）搜索网络上的相似图片，<a title="Google图片搜索示例" href="http://images.google.com.hk/search?tbs=sbi:AMhZZiub0aevgXm9IVunrcdmjFiDKUbz2BAvYURynMjiNR0pAf3abjiQcuUE9XFJHlFMr1cxJ5ub4lgyxYwyVK2qgvUSCD6kK2YDTadslg6TwRQcG_1PnvSdfIIK4W_1KrJ3i16AhBLVmSHYZKomjEBNWdWt9hr000DHeIdJLf6t4ODa1uJ6MwCmXCfGqJLWYEqEXyuAa_1Zg9rUBXrDYh7MtpzVY-2Y4_1BegAVHQ9Nj871b-JHGkYWeSQ2Pyp0SYxVkMczwhyO_1OqtTqE9EJL3Xvn4e7rJQdAClNSu2uLCQ6yuGWQsb5BncHpOLYiNmoI5StZveg-UwA5I2wg-dfqTWn1xOwYvKURHz5xxzj4CQkpkfBsEz9RiEpja4KK1r_1VxSGLYedGXjq4A2_1_1ASNVi_1_1FpEHgU4f66EFPd3j6zngu4_1Mv5POJMX7YjvHmb82HJ00_1ZOPK6F3Hl1strPjdhT7ir7MNJ9tBM0KPyxMOFS-f8y1hnX6xMw8N065sTmnsxXfY2Vrstf0gMx8CdNbI0eLGK13Ju-2tXPciFBh3M4pG7CYyyZzHDnHf_1V6VrXNAdVTU-D_1pLUJcbkP2AX9OzPVl1BaKZy1RAp3edU7iZb7IXOWMVkgA2_1b7F7mBewG1TClGSMRPHSxAe9WHMtDPZcBBdnIS6RXfU3HcC7iGcQjeAV6I8wgnuB1nNK_1WI4JtAcd591KH3ey168cO9p2yrmb_1sOKU_1LAKM6pYb7Tp6jdbqFhgn5WfBUAbbpXVBrJMbTh3UCyXtrOF2vyvqeDsLWVVikWydTtfjao5PrOsYK7RihZO_1Rks_1R-FowS0eAqVIuQGJVgFJUMk6Q6qw5Qmh02LfXLMP5jkzdV6foi87VQxtMCO8NWgzVIC4BURKr4Vi4_1NVY3uIk9-vtfBnwxl-bWKBIVuvK9BRaA&amp;btnG=%E6%90%9C%E7%B4%A2&amp;hl=zh-CN&amp;newwindow=1&amp;sa=N&amp;gbv=2&amp;biw=1024&amp;bih=631" target="_blank">例子</a>。估计还没多少人意识到，这玩意儿是人肉搜索的大杀器，以后大家还是少上传私人照片到公开网络。</p>
<p><a href="http://www.ruanyifeng.com/blog/2011/07/principle_of_similar_image_search.html" target="_blank">阮一峰介绍</a>了一个简单的图片搜索原理，可分为下面几步：</p>
<ol>
<li>缩小尺寸。将图片缩小到8x8的尺寸，总共64个像素。这一步的作用是去除图片的细节，只保留结构、明暗等基本信息，摒弃不同尺寸、比例带来的图片差异。</li>
<li>简化色彩。将缩小后的图片，转为64级灰度。也就是说，所有像素点总共只有64种颜色。</li>
<li>计算平均值。计算所有64个像素的灰度平均值。</li>
<li>比较像素的灰度。将每个像素的灰度，与平均值进行比较。大于或等于平均值，记为1；小于平均值，记为0。</li>
<li>计算哈希值。将上一步的比较结果，组合在一起，就构成了一个64位的整数，这就是这张图片的指纹。组合的次序并不重要，只要保证所有图片都采用同样次序就行了。</li>
</ol>
<p>这种方法对于寻找一模一样的图片是有效的，但并不能够去搜索“相似”的照片，也不能局部搜索，比如从一个人的单人照找到这个人与别人的合影。这些Google Images都能做到。</p>
<p>Google的算法，其<a href="http://insidesearch.blogspot.com/2011/07/teaching-computers-to-see-image.html" target="_blank">官方描述</a>为：</p>
<blockquote><p>When you upload an image to Search by Image, the algorithms analyze the content of the image and break it down into smaller pieces called “<a href="http://en.wikipedia.org/wiki/Feature_detection_(computer_vision)">features</a>”. These features try to capture specific, distinct characteristics of the image - like textures, colors, and shapes. Features and their geometric configuration represent the computer’s understanding of what the image looks like.</p></blockquote>
<p>大意是说在Google眼里，图片并不是一个由像素点构成点阵，而是一些“features"，包括形状、质地和颜色块。图片之间的比较，是直接去比较图片里的线条等。<a href="http://insidesearch.blogspot.com/2011/07/teaching-computers-to-see-image.html" target="_blank">官方文档里有一个视频</a>（在Youtube上）动态地描述了计算机是如何看图片的。</p>
<p>早在2008年，Google公布了一篇图片搜索的论文（<a href="http://www.www2008.org/papers/pdf/p307-jingA.pdf" target="_blank">PDF版</a>），和文本搜索的思路是一样的：</p>
<ul>
<li>对于每张图片，抽取其特征。这和文本搜索对于网页进行分词类似。</li>
<li>对于两张图片，其相关性定义为其特征的相似度。这和文本搜索里的文本相关性也是差不多的。</li>
<li>图片一样有image rank。文本搜索中的page rank依靠文本之间的超链接。图片之间并不存在这样的超链接，image rank主要依靠图片之间的相似性（两张图片相似，便认为它们之间存在超链接）。具有更多相似图片的图片，其image rank更高一些。</li>
</ul>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html">图片搜索的原理</a></li><li ><a href="http://zhiqiang.org/blog/it/google-web-search-api-build-station.html">Google Web API打造站内搜索</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a></li><li ><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">有序矩阵的中位数算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2011. | <a href="http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html#comments">10 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>一个简单图的三染色算法问题</title>
		<link>http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html#comments</comments>
		<pubDate>Thu, 25 Sep 2008 06:57:19 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[染色]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/?p=851</guid>
		<description><![CDATA[博客 » 计算机科学 » 染色，算法 » 注: 这个问题来自China Theory Week 2008的Open Problems Session。 我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程中并没有说明如何找到它，这种证明方法被称为“非构造性证明”。有些流派的数学家对这种证明方法非常不满，具体这里就不详谈了。 这里要说的是，一个玩意儿，即时你知道它总是存在...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/science/computer-science">计算机科学</a> » <a href="http://zhiqiang.org/blog/tag/%e6%9f%93%e8%89%b2" rel="tag">染色</a>，<a href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a> » </p><p><font color="#808080">注: 这个问题来自<a href="http://conference.itcs.tsinghua.edu.cn/CTW2008/" target="_blank">China Theory Week 2008</a>的Open Problems Session。</font></p>
<p>我们知道在数学里证明一个东西的存在性的时候，有时候只证明了“存在性”，而且在证明过程中并没有说明如何找到它，这种证明方法被称为“非构造性证明”。有些流派的数学家对这种证明方法非常不满，具体这里就不详谈了。</p>
<p>这里要说的是，一个玩意儿，即时你知道它总是存在的，它也是可以算出来的，但是不是就一定能够很快的比如在多项式时间之内算出来呢？</p>
<p><font color="#808080"><a href="http://zhiqiang.org/blog/science/computer-science/game-theory-computing-nash-equilibrium.html">Game Theory</a></font><font color="#808080">已经证明了在两人非合作博弈中，</font><font color="#808080">纳什均衡</font><font color="#808080">总是存在的，但是</font><font color="#808080"><a href="http://zhiqiang.org/blog/science/computer-science/game-theory-computing-nash-equilibrium.html">如何计算它确被证明是PPAD-hard</a></font><font color="#808080">的，一个被猜测不属于P的复杂类。</font></p>
<p><font color="#808080">纳什均衡的计算问题不太好理解，下面介绍一个问题，描述比较简单非常容易理解。它有类似的效果，存在性可在数学上被证明，但如何计算它却不知道，复杂性也未知。</font></p>
<p>输入：一个<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_fa7674a88b1ff1139a00caf969933a8c.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="3n" /></span><script type='math/tex'>3n</script>点的图，顶点构成一个正<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_fa7674a88b1ff1139a00caf969933a8c.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="3n" /></span><script type='math/tex'>3n</script>边形，以及它们之间的<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_2e0a8f7435e9e48c3f86c8d72266a034.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="6n" /></span><script type='math/tex'>6n</script>条边。其中<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_fa7674a88b1ff1139a00caf969933a8c.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="3n" /></span><script type='math/tex'>3n</script>条为多边形的边，另外<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_fa7674a88b1ff1139a00caf969933a8c.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="3n" /></span><script type='math/tex'>3n</script>条边构成<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_7b8b965ad4bca0e41ab51de7b31363a1.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="n" /></span><script type='math/tex'>n</script>个顶点互不重合的三角形。下图为一个<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_f4b339682e05755eb7408448ef87e1ca.gif' style='vertical-align: middle; border: none; padding-bottom:1px;' class='tex' alt="n=3" /></span><script type='math/tex'>n=3</script>的例子。</p>
<p><img style="display: block; float: none; margin-left: auto; margin-right: auto" src="http://lh3.ggpht.com/mathzqy/SNmzOj9_LWI/AAAAAAAAFCw/BIh6tAzGgaw/3color.JPG?imgmax=640"/></p>
<p>输出：这个图的一种三染色方案（给定点染色，使任何有边相连的顶点都不同色）。</p>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a></li><li ><a href="http://zhiqiang.org/blog/science/countable-coloring-of-real.html">实数上的可数颜色染色问题</a></li><li ><a href="http://zhiqiang.org/blog/science/color-points-on-plane.html">点染色问题</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a></li><li ><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/programs-depend-on-date-struct-not-algorithm.html">编程的核心是数据结构，而不是算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html">图片搜索的原理</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html#comments">5 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>求平方根倒数的算法</title>
		<link>http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html#comments</comments>
		<pubDate>Fri, 19 Sep 2008 04:10:28 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[算法]]></category>
		<category><![CDATA[算法分析]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/inverse-square-root-algorithm-analysis.html</guid>
		<description><![CDATA[博客 » 计算机科学 » 算法，算法分析 » 下面这个求的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。 float InvSqrt (float x){ float xhalf = 0.5f*x; int i = *(int*)&#38;x; i = 0x5f3759df - (i&#62;&#62;1); y = *(float*)&#38;i; y = y*(1.5f - xhalf*x*x); return x; } 我们这里分析一下它的原理（指程序的正确性，而不是解释为何快）。 分析程序之前，我们必须解释一下float数据在计算机里的表示方...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/science/computer-science">计算机科学</a> » <a href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a>，<a href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95%e5%88%86%e6%9e%90" rel="tag">算法分析</a> » </p><p>下面这个求<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_c316ab9d453dd89c01a6fdb29cfb28de.gif' style='vertical-align: middle; border: none; ' class='tex' alt="1/\sqrt{x}" /></span><script type='math/tex'>1/\sqrt{x}</script>的函数号称比直接调用sqrt库函数快4倍，来自游戏Quake III的源代码。</p>
<blockquote>
<pre><span style="color: #0000ff;">float</span> InvSqrt (<span style="color: #0000ff;">float</span> x){
    <span style="color: #0000ff;">float</span> xhalf = 0.5f*x;
    <span style="color: #0000ff;">int</span> i = *(<span style="color: #0000ff;">int</span>*)&amp;x;
    i = 0x5f3759df - (i&gt;&gt;1);
    y = *(<span style="color: #0000ff;">float</span>*)&amp;i;
    y = y*(1.5f - xhalf*x*x);
    <span style="color: #0000ff;">return</span> x;
}</pre>
</blockquote>
<p>我们这里分析一下它的原理（指程序的正确性，而不是解释为何快）。</p>
<p>分析程序之前，我们必须解释一下float数据在计算机里的表示方式。一般而言，一个float数据<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script>共32个bit，和int数据一样。其中前23位为有效数字<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_832cab6245118c67b73c5ef0be7cf7e8.gif' style='vertical-align: middle; border: none; ' class='tex' alt="M_x" /></span><script type='math/tex'>M_x</script>，后面接着一个8位数据<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_893be2279f4c4bc665184cf9f87da90c.gif' style='vertical-align: middle; border: none; ' class='tex' alt="E_x" /></span><script type='math/tex'>E_x</script>表示指数，最后一位表示符号，由于这里被开方的数总是大于0，所以我们暂不考虑最后一个符号位。此时</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_149061d576381d545ca1689fb2bde7fb.gif' style='vertical-align: middle; border: none;' class='tex' alt="x=1.M_x 2^{E_x-127}" /></span><script type='math/tex;  mode=display'>x=1.M_x 2^{E_x-127}</script></p></p>
<p>如果我们把计算机内的浮点数<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script>看做一个整数<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_e0c12d615090d3574f32ebeab63f5601.gif' style='vertical-align: middle; border: none; ' class='tex' alt="I_x" /></span><script type='math/tex'>I_x</script>，那么<p style='text-align:center;'><span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_c50bf4aa3fcd7224ad0a620841645302.gif' style='vertical-align: middle; border: none;' class='tex' alt="I_x = 2^{23}E_x+M_x" /></span><script type='math/tex;  mode=display'>I_x = 2^{23}E_x+M_x</script></p></p>
<p>现在开始逐步分析函数。这个函数的主体有四个语句，分别的功能是：</p>
<blockquote><p><span style="color: #0000ff;">int</span> i = *(<span style="color: #0000ff;">int</span>*)&amp;x; 这条语句把<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_9dd4e461268c8034f5c8564e155c67a6.gif' style='vertical-align: middle; border: none; padding-bottom:2px;' class='tex' alt="x" /></span><script type='math/tex'>x</script>转成<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_1efc275e97e02ac108c7836caad83cc0.gif' style='vertical-align: middle; border: none; ' class='tex' alt="i=I_x" /></span><script type='math/tex'>i=I_x</script>。</p>
<p>i = 0x5f3759df - (i&gt;&gt;1); 这条语句从<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_e0c12d615090d3574f32ebeab63f5601.gif' style='vertical-align: middle; border: none; ' class='tex' alt="I_x" /></span><script type='math/tex'>I_x</script>计算<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_ed49444b7e57c2fc4dfc8f056fae6bc4.gif' style='vertical-align: middle; border: none; ' class='tex' alt="I_{1/\sqrt{x}}" /></span><script type='math/tex'>I_{1/\sqrt{x}}</script>。</p>
<p>y = *(<span style="color: #0000ff;">float</span>*)&amp;i; 这条语句将<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_ed49444b7e57c2fc4dfc8f056fae6bc4.gif' style='vertical-align: middle; border: none; ' class='tex' alt="I_{1/\sqrt{x}}" /></span><script type='math/tex'>I_{1/\sqrt{x}}</script>转换为<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_c316ab9d453dd89c01a6fdb29cfb28de.gif' style='vertical-align: middle; border: none; ' class='tex' alt="1/\sqrt{x}" /></span><script type='math/tex'>1/\sqrt{x}</script>。</p>
<p>y = y*(1.5f - xhalf*y*y); 这时候的y是近似解；此步就是经典的牛顿迭代法。迭代次数越多越准确。</p></blockquote>
<p>关键是第二步 i = 0x5f3759df - (i&gt;&gt;1); 这条语句从<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_e0c12d615090d3574f32ebeab63f5601.gif' style='vertical-align: middle; border: none; ' class='tex' alt="I_x" /></span><script type='math/tex'>I_x</script>计算<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_ed49444b7e57c2fc4dfc8f056fae6bc4.gif' style='vertical-align: middle; border: none; ' class='tex' alt="I_{1/\sqrt{x}}" /></span><script type='math/tex'>I_{1/\sqrt{x}}</script>，原理:</p>
<p>令<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_aa1b97bb7383597663a51e7ad5b0da35.gif' style='vertical-align: middle; border: none; ' class='tex' alt="y=1/\sqrt{x}" /></span><script type='math/tex'>y=1/\sqrt{x}</script>，用<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_1759e9f3f5a3125e49343a92a2b7cf7c.gif' style='vertical-align: middle; border: none; ' class='tex' alt="x=(1+m_x)2^{e_x}" /></span><script type='math/tex'>x=(1+m_x)2^{e_x}</script>和<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_8a525bf37b169a440892a25eb7403799.gif' style='vertical-align: middle; border: none; ' class='tex' alt="y=(1+m_y)2^{e_y}" /></span><script type='math/tex'>y=(1+m_y)2^{e_y}</script>带入之后两边取对数，再利用近似表示<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_ea3d4f8b071da0aba0556f4b7de23443.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\log_2(1+z)\sim z+\delta" /></span><script type='math/tex'>\log_2(1+z)\sim z+\delta</script>，算一算就得到</p>
<p><p style='text-align:center;'><span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_50d25462e9d0d8d4d6bace750b3f1b67.gif' style='vertical-align: middle; border: none;' class='tex' alt="I_y = \frac{2}{3}(127-\delta)2^{23}-I_x/2" /></span><script type='math/tex;  mode=display'>I_y = \frac{2}{3}(127-\delta)2^{23}-I_x/2</script></p></p>
<p>若取<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_322863072b22b9062d0ad72cb98692f7.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\delta=0.0450465679168701171875" /></span><script type='math/tex'>\delta=0.0450465679168701171875</script>，<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_5c30a3072abd9f51847218d4ddede824.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\frac{2}{3}(127-\delta)2^{23}" /></span><script type='math/tex'>\frac{2}{3}(127-\delta)2^{23}</script>就是程序里所用的常量0x5f3759df。至于为何选择这个<span class='MathJax_Preview'><img src='http://zhiqiang.org/blog/wp-content/plugins/latex/cache/tex_77a3b715842b45e440a5bee15357ad29.gif' style='vertical-align: middle; border: none; ' class='tex' alt="\delta" /></span><script type='math/tex'>\delta</script>，则应该是曲线拟合实验的结果。</p>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">求平方根倒数的算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-recursive-algorithm.html">递归算法的复杂度</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a></li><li ><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/graph-isomorphism-is-polynomial.html">图同构问题属于P？</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html">图片搜索的原理</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html#comments">25 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html/feed</wfw:commentRss>
		<slash:comments>25</slash:comments>
		</item>
		<item>
		<title>算法百科全书 - Encyclopedia of Algorithms</title>
		<link>http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html</link>
		<comments>http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html#comments</comments>
		<pubDate>Wed, 16 Jul 2008 04:50:20 +0000</pubDate>
		<dc:creator>zhiqiang</dc:creator>
				<category><![CDATA[计算机科学]]></category>
		<category><![CDATA[Encyclopedia of Algorithms]]></category>
		<category><![CDATA[算法]]></category>

		<guid isPermaLink="false">http://zhiqiang.org/blog/posts/download-encyclopedia-of-algorithm.html</guid>
		<description><![CDATA[博客 » 计算机科学 » Encyclopedia of Algorithms，算法 » Xie Xie推荐了一本今年出版的一本新书，Encyclopedia of Algorithms，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。 翻了一下，很多算法都不是直接给出的，只是给出一些原理描述和参考文献。看来这本也真只能当当索引用了。 没事做的话可以先欣赏一下第698页的O(n)的平面图判定算法。 按照推荐人xiexie...]]></description>
			<content:encoded><![CDATA[<p id="breadcrumb" class="breadcrumb"><a href="http://zhiqiang.org/blog/">博客</a> » <a href="http://zhiqiang.org/blog/category/science/computer-science">计算机科学</a> » <a href="http://zhiqiang.org/blog/tag/encyclopedia-of-algorithms" rel="tag">Encyclopedia of Algorithms</a>，<a href="http://zhiqiang.org/blog/tag/%e7%ae%97%e6%b3%95" rel="tag">算法</a> » </p><p>Xie Xie推荐了一本今年出版的一本新书，<em>Encyclopedia of Algorithms</em>，全书1200页，涵盖各类有名问题的算法，概率算法，近似算法，量子算法等。</p>
<p>翻了一下，很多算法都不是直接给出的，只是给出一些原理描述和参考文献。看来这本也真只能当当索引用了。</p>
<p>没事做的话可以先欣赏一下第698页的O(n)的平面图判定算法。</p>
<p>按照推荐人xiexie的说法，看在原书高达309欧元的价格份上，也得下载一本回来爽一爽。</p>
<p>听说个人blog上不要放版权材料，下载地址大家自己找吧。<a href="http://ifile.it/?error=1001" target="_blank"></a></p>
<div><h4>相关文章</h4><ul><li class='currentpost'><a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">算法百科全书 - Encyclopedia of Algorithms</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/preliminary-computer-theory-algorithms-and-calculation-model.html">理论计算机初步：算法和计算模型</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/an-algorithm-face-interviews-question-test.html">一个算法面试题 &#038; 面试题库</a></li><li ><a href="http://zhiqiang.org/blog/science/mathmatics-in-rubik-cube-and-algorithm.html">魔方里的数学</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/complexity-of-prime-sieve.html">素数筛法的复杂度</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/another-perfect-shuffle-algorithm.html">Perfect Shuffle的算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/median-algorithm-of-ordered-matrix.html">有序矩阵的中位数算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/inverse-square-root-algorithm-analysis.html">求平方根倒数的算法</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/3-color-a-simple-graph.html">一个简单图的三染色算法问题</a></li><li ><a href="http://zhiqiang.org/blog/science/computer-science/how-google-search-similar-images.html">图片搜索的原理</a></li></ul></div>    <p></p>
    <hr noshade style="margin:0;height:1px" />
    <p>&copy; zhiqiang for <a href="http://zhiqiang.org/blog">阅微堂</a>, 2008. | <a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html">&#38142;&#25509;</a> | <a href="http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html#comments">11 &#26465;&#35780;&#35770;</a></p>]]></content:encoded>
			<wfw:commentRss>http://zhiqiang.org/blog/science/computer-science/download-encyclopedia-of-algorithm.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
	</channel>
</rss>

