Math https://yyhh.org/notebook/math en Count Number of Maps: First Exercises of Conceptual Mathematics in Clojure https://yyhh.org/blog/2012/05/count-number-maps-first-exercises-conceptual-mathematics-clojure <span>Count Number of Maps: First Exercises of Conceptual Mathematics in Clojure </span> <span><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span>Wed, 05/16/2012 - 21:33</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/notebook/math" id="taxonomy-term-25" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/math"> <div class="field field--name-name field--type-string field--label-hidden field__item">Math</div> </a> <div class="content"> </div> </div> </div> <div class="field__item"><div about="/notebook/clojure" id="taxonomy-term-35" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/clojure"> <div class="field field--name-name field--type-string field--label-hidden field__item">Clojure</div> </a> <div class="content"> </div> </div> </div> <div class="field__item"><div about="/notebook/categorytheory" id="taxonomy-term-38" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/categorytheory"> <div class="field field--name-name field--type-string field--label-hidden field__item">CategoryTheory</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p>As <a href="http://yyhh.org/blog/2012/04/start-learning-category-theory">previously mentioned</a>, I am learning category theory, beginning with <a href="http://en.wikipedia.org/wiki/William_Lawvere">Lawvere</a>'s <a href="http://www.amazon.com/Conceptual-Mathematics-First-Introduction-Categories/dp/052171916X">Conceptual Mathematics</a> book. This is a very elementry book that assumes almost nothing as a background. However, it is still a math book, which requires doing some exercises. Since the book provides no answer to exercises, I decide to make my own and post them here as I did them. Hopefully someone will find them useful. </p> <p>Since a large part of category theory is constructive, I will try to implement the concepts computationally in order to understand them better. Cateogry theory has been implemented as types in some strong typed languages such as <a href="http://www.cs.man.ac.uk/~david/categories/">ML</a> and Haskell. I think it would be fun to see how it would look in a dynamic typed language such as Clojure. Even if I could not go very far, at minimum, I will have a mechanical means to check my solutions to the exercises. </p> <p>The first article of the book deals with the category of sets, and the main topic is about maps between sets. The second article talks about isomorphisms and related concepts. Some exercises (on page 20 and 47) are of the "how many maps are there" variety. Here are some Clojure code I used to calculate the results.</p> <div class="codeblock"> <font face="monospace"><br /><span><font color="#912f11">(</font></span><span><font color="#800090">ns</font></span> CM.core<br />   <font color="#cd3700">(</font><span><font color="#1f3f81"><b>:use</b></font></span> clojure.math.combinatorics<font color="#cd3700">)</font><span><font color="#912f11">)</font></span> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> all-maps<br />   <span><font color="#077807">"Return a lazy sequence of all the possible maps from a domain to </font></span><br /><span><font color="#077807">  a codomain"</font></span><br />   <span><font color="#912f11">[</font></span>domain codomain<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font><span><font color="#1f3f81"><b>map</b></font></span> <span><font color="#912f11">#(</font></span><span><font color="#007080">conj</font></span> <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> domain <span><font color="#1f3f81"><b>:codomain</b></font></span> codomain<span><font color="#912f11">}</font></span> <span><font color="#912f11">[</font></span><span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#007080">zipmap</font></span> %<span><font color="#077807">1</font></span> %<span><font color="#077807">2</font></span><span><font color="#912f11">)])</font></span><br />        <font color="#ee9a00">(</font><span><font color="#007080">repeat</font></span> domain<font color="#ee9a00">)</font><br />        <font color="#ee9a00">(</font><span><font color="#007080">apply</font></span> <span><font color="#007080">cartesian-product</font></span> <font color="#cdcd00">(</font><span><font color="#007080">repeat</font></span> <font color="#698b22">(</font><span><font color="#007080">count</font></span> domain<font color="#698b22">)</font> codomain<font color="#cdcd00">)</font><font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> same-mapping-rule?<br />   <span><font color="#077807">"Return true if two mapping rules give the same results for a domain"</font></span><br />   <span><font color="#912f11">[</font></span>domain r1 r2<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font><span><font color="#007080">every?</font></span> <span><font color="#007080">identity</font></span> <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>map</b></font></span> <span><font color="#007080">=</font></span> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>map</b></font></span> r1 domain<font color="#cdcd00">)</font> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>map</b></font></span> r2 domain<font color="#cdcd00">)</font><font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> same-map?<br />   <span><font color="#077807">"Return true if two maps are the same"</font></span><br />   <span><font color="#912f11">[</font></span>f g<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font><span><font color="#800090">and</font></span> <font color="#ee9a00">(</font><span><font color="#007080">=</font></span> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> f<font color="#cdcd00">)</font> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> g<font color="#cdcd00">)</font><font color="#ee9a00">)</font><br />        <font color="#ee9a00">(</font><span><font color="#007080">=</font></span> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:codomain</b></font></span> f<font color="#cdcd00">)</font> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:codomain</b></font></span> g<font color="#cdcd00">)</font><font color="#ee9a00">)</font><br />        <font color="#ee9a00">(</font>same-mapping-rule? <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> f<font color="#cdcd00">)</font> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:rule</b></font></span> f<font color="#cdcd00">)</font> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:rule</b></font></span> g<font color="#cdcd00">)</font><font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> compose-map<br />   <span><font color="#077807">"Return a composed map, also ensure domains and codomains match"</font></span><br />   <span><font color="#912f11">[</font></span>f g<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font><span><font color="#912f11">if</font></span> <font color="#ee9a00">(</font><span><font color="#007080">=</font></span> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:codomain</b></font></span> g<font color="#cdcd00">)</font> <font color="#cdcd00">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> f<font color="#cdcd00">)</font><font color="#ee9a00">)</font><br />     <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> g<span><font color="#912f11">)</font></span>, <span><font color="#1f3f81"><b>:codomain</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#1f3f81"><b>:codomain</b></font></span> f<span><font color="#912f11">)</font></span>, <br />      <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#007080">comp</font></span> <font color="#cd3700">(</font><span><font color="#1f3f81"><b>:rule</b></font></span> f<font color="#cd3700">)</font> <font color="#cd3700">(</font><span><font color="#1f3f81"><b>:rule</b></font></span> g<font color="#cd3700">)</font><span><font color="#912f11">)}</font></span><br />     <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>throw</b></font></span> <font color="#cdcd00">(</font>Exception. <span><font color="#077807">"Cannot compose, domain does not match codomain"</font></span><font color="#cdcd00">)</font><font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> composed? <br />   <span><font color="#077807">"Return true if map f and map g compose to map c"</font></span><br />   <span><font color="#912f11">[</font></span>f g c<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>same-map? c <font color="#ee9a00">(</font>compose-map f g<font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span><br />   <br /><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> idempotent? <br />   <span><font color="#077807">"Return true if the given map return the same results as when it is </font></span><br /><span><font color="#077807">  applied twice"</font></span><br />   <span><font color="#912f11">[</font></span>f<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>composed? f f f<font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> solutions<br />   <span><font color="#077807">"Return a lazy sequence of maps that match the given predicates and go</font></span><br /><span><font color="#077807">  from the given domain to given codomain"</font></span><br />   <span><font color="#912f11">[</font></span>pred domain codomain<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font><span><font color="#1f3f81"><b>filter</b></font></span> pred <font color="#ee9a00">(</font>all-maps domain codomain<font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> idempotent-maps<br />   <span><font color="#077807">"Return a lazy sequence of idempotent maps between a domain and itself </font></span><br /><span><font color="#077807">  as the codomain"</font></span><br />   <span><font color="#912f11">[</font></span>domain<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>solutions idempotent? domain domain<font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> choice<br />   <span><font color="#077807">"Return a lazy sequence of maps that are applied before the given map to</font></span><br /><span><font color="#077807">  return the same results as the given composed map, i.e. solution of </font></span><br /><span><font color="#077807">  choice problem"</font></span><br />   <span><font color="#912f11">[</font></span>f c<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>solutions <span><font color="#912f11">#(</font></span>composed? f <span><font color="#912f11">%</font></span> c<span><font color="#912f11">)</font></span> <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> c<font color="#ee9a00">)</font> <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> f<font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> determination<br />   <span><font color="#077807">"Return a lazy sequence of maps that are applied after the given map to</font></span><br /><span><font color="#077807">  return the same results as the given composed map, i.e. solution of </font></span><br /><span><font color="#077807">  determination problem"</font></span><br />   <span><font color="#912f11">[</font></span>g c<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>solutions <span><font color="#912f11">#(</font></span>composed? <span><font color="#912f11">%</font></span> g c<span><font color="#912f11">)</font></span> <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>:codomain</b></font></span> g<font color="#ee9a00">)</font> <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>:codomain</b></font></span> c<font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> section<br />   <span><font color="#077807">"Return a lazy sequence of maps that are sections of the given map"</font></span><br />   <span><font color="#912f11">[</font></span>f<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>choice f <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#1f3f81"><b>:codomain</b></font></span> f<span><font color="#912f11">)</font></span> <span><font color="#1f3f81"><b>:codomain</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#1f3f81"><b>:codomain</b></font></span> f<span><font color="#912f11">)</font></span> <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#007080">identity</font></span><span><font color="#912f11">}</font></span><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> retraction<br />   <span><font color="#077807">"Return a lazy sequence of maps that are retractions of the given map"</font></span><br />   <span><font color="#912f11">[</font></span>f<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>determination f <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> f<span><font color="#912f11">)</font></span> <span><font color="#1f3f81"><b>:codomain</b></font></span> <span><font color="#912f11">(</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> f<span><font color="#912f11">)</font></span> <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#007080">identity</font></span><span><font color="#912f11">}</font></span><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> composed-solution-pairs <br />   <span><font color="#077807">"Return a lazy sequence of pairs of maps that compose to a given map, with</font></span><br /><span><font color="#077807">  the given shared domain in between"</font></span><br />   <span><font color="#912f11">[</font></span>domain c<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font><span><font color="#1f3f81"><b>filter</b></font></span> <br />     <span><font color="#912f11">#(</font></span>composed? <span><font color="#912f11">(</font></span><span><font color="#007080">first</font></span> %<span><font color="#912f11">)</font></span> <span><font color="#912f11">(</font></span><span><font color="#007080">last</font></span> %<span><font color="#912f11">)</font></span> c<span><font color="#912f11">)</font></span><br />     <font color="#ee9a00">(</font><span><font color="#1f3f81"><b>for</b></font></span> <span><font color="#912f11">[</font></span>g <span><font color="#912f11">(</font></span>all-maps <font color="#cd3700">(</font><span><font color="#1f3f81"><b>:domain</b></font></span> c<font color="#cd3700">)</font> domain<span><font color="#912f11">)</font></span><br />           f <span><font color="#912f11">(</font></span>all-maps domain <font color="#cd3700">(</font><span><font color="#1f3f81"><b>:codomain</b></font></span> c<font color="#cd3700">)</font><span><font color="#912f11">)]</font></span><br />       <span><font color="#912f11">[</font></span>f g<span><font color="#912f11">]</font></span><font color="#ee9a00">)</font><font color="#cd3700">)</font><span><font color="#912f11">)</font></span><br />       <br /><span><font color="#912f11">(</font></span><span><font color="#800090">defn</font></span> retraction-section-pairs<br />   <span><font color="#077807">"Return a lazy sequence of pairs of maps r and s, where r goes from domain</font></span><br /><span><font color="#077807">  X to domain A,  s goes from A to X, and r of s is the same as the identity </font></span><br /><span><font color="#077807">  map on A."</font></span><br />   <span><font color="#912f11">[</font></span>A X<span><font color="#912f11">]</font></span><br />   <font color="#cd3700">(</font>composed-solution-pairs X <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> A <span><font color="#1f3f81"><b>:codomain</b></font></span> A <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#007080">identity</font></span><span><font color="#912f11">}</font></span><font color="#cd3700">)</font><span><font color="#912f11">)</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> A <span><font color="#912f11">#{</font></span><span><font color="#077807">"John"</font></span> <span><font color="#077807">"Mary"</font></span> <span><font color="#077807">"Sam"</font></span><span><font color="#912f11">})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> B <span><font color="#912f11">#{</font></span><span><font color="#077807">"eggs"</font></span> <span><font color="#077807">"coffee"</font></span><span><font color="#912f11">})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> U <span><font color="#912f11">#{</font></span><span><font color="#1f3f81"><b>:b</b></font></span> <span><font color="#1f3f81"><b>:p</b></font></span> <span><font color="#1f3f81"><b>:q</b></font></span> <span><font color="#1f3f81"><b>:r</b></font></span> <span><font color="#1f3f81"><b>:s</b></font></span><span><font color="#912f11">})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> D <span><font color="#912f11">#{</font></span><span><font color="#077807">0</font></span> <span><font color="#077807">1</font></span><span><font color="#912f11">})</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> one-a <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> A <span><font color="#1f3f81"><b>:codomain</b></font></span> A <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#007080">identity</font></span><span><font color="#912f11">})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> one-b <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> B <span><font color="#1f3f81"><b>:codomain</b></font></span> B <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#007080">identity</font></span><span><font color="#912f11">})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> one-d <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> D <span><font color="#1f3f81"><b>:codomain</b></font></span> D <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#007080">identity</font></span><span><font color="#912f11">})</font></span></p> <p><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> g <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> U <span><font color="#1f3f81"><b>:codomain</b></font></span> D <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:b</b></font></span> <span><font color="#077807">0</font></span> <span><font color="#1f3f81"><b>:p</b></font></span> <span><font color="#077807">0</font></span> <span><font color="#1f3f81"><b>:q</b></font></span> <span><font color="#077807">0</font></span> <span><font color="#1f3f81"><b>:r</b></font></span> <span><font color="#077807">1</font></span> <span><font color="#1f3f81"><b>:s</b></font></span> <span><font color="#077807">1</font></span><span><font color="#912f11">}})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> f <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> D <span><font color="#1f3f81"><b>:codomain</b></font></span> U <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#912f11">{</font></span><span><font color="#077807">0</font></span> <span><font color="#1f3f81"><b>:b</b></font></span> <span><font color="#077807">1</font></span> <span><font color="#1f3f81"><b>:r</b></font></span><span><font color="#912f11">}})</font></span><br /><span><font color="#912f11">(</font></span><span><font color="#912f11">def</font></span> u <span><font color="#912f11">{</font></span><span><font color="#1f3f81"><b>:domain</b></font></span> D <span><font color="#1f3f81"><b>:codomain</b></font></span> U <span><font color="#1f3f81"><b>:rule</b></font></span> <span><font color="#912f11">{</font></span><span><font color="#077807">0</font></span> <span><font color="#1f3f81"><b>:b</b></font></span> <span><font color="#077807">1</font></span> <span><font color="#1f3f81"><b>:b</b></font></span><span><font color="#912f11">}})</font></span><br /></p></font> </div> <p>In this code, maps in category theory are simply implemented as Clojure's map data structure. So a map in category theory will have three keys in the implementation, a :domain, a :codomain, and a :rule. The first two are sets, and the last one is a function, which defines the actual mapping rule from domain to codomain. </p> <p><b><i>Article 1, Exercise 2. </i></b> How many different maps $f$ are there with domain $A$ and codomain $B$? </p> <p>Domain $A$ has 3 elements "John", "Mary" and "Sam", codomain $B$ has 2 elements "eggs" and "coffee". To find the answer, evaluate this in REPL:</p> <div class="codeblock"> CM.core=&gt; (count (all-maps A B))<br /> 8 </div> <p>For our domain of 3 elements and codomain of 2 elements, the number of maps is 8, or $2^3$. If we try some other domains (Exercise 3, 4, 5), we will soon discover that the answer is always $$n^m$$ where $n$ and $m$ is the size of codomain and domain, respectively. The reason is simple: each and every element of the domain can pick any one of the elment of codomain as the target. So the first element in the domain has $n$ choices of target, the second can pick $n$ choices as well, and so on, doing this $m$ times, and multipling them all up gives the answer.</p> <p><b><i>Article 1, Exercise 6. </i></b> How many different maps $A \stackrel{f}{\longrightarrow} A$ satisfy $f \circ f = f$?</p> <p>Basically, this is asking how many maps are there that composes with itself to get the same results as itself. Such maps are called idempotent maps. The code to find the answer:</p> <div class="codeblock"> CM.core=&gt; (count (idempotent-maps A))<br /> 10 </div> <p>Let's try Exercise 7, the number of idempotent maps for $B$:</p> <div class="codeblock"> CM.core=&gt; (count (idempotent-maps B))<br /> 3 </div> <p>How about a domain with 4, 5 or 6 elments?</p> <div class="codeblock"> CM.core=&gt; (count (idempotent-maps #{1 2 3 4}))<br /> 41<br /> CM.core=&gt; (count (idempotent-maps #{1 2 3 4 5}))<br /> 196<br /> CM.core=&gt; (count (idempotent-maps #{1 2 3 4 5 6}))<br /> 1057 </div> <p>Hmm, 3, 10, 41, 196, 1057, ... what is the regularity here? It is not obvious. Let's examine the details of the first two maps and look at their rules:</p> <div class="codeblock"> CM.core=&gt; (map :rule (idempotent-maps B))<br /> ({"eggs" "coffee", "coffee" "coffee"} {"eggs" "eggs", "coffee" "coffee"} {"eggs" "eggs", "coffee" "eggs"})<br /> CM.core=&gt; (map :rule (idempotent-maps A))<br /> ({"Sam" "John", "Mary" "John", "John" "John"} {"Sam" "Sam", "Mary" "John", "John" "John"} {"Sam" "John", "Mary" "Mary", "John" "John"} {"Sam" "Mary", "Mary" "Mary", "John" "John"} {"Sam" "Sam", "Mary" "Mary", "John" "John"} {"Sam" "Sam", "Mary" "Sam", "John" "John"} {"Sam" "Mary", "Mary" "Mary", "John" "Mary"} {"Sam" "Sam", "Mary" "Mary", "John" "Mary"} {"Sam" "Sam", "Mary" "Mary", "John" "Sam"} {"Sam" "Sam", "Mary" "Sam", "John" "Sam"}) </div> <p>The regularity seems to be this: either an element must map to itself, or it must map to an elment that maps to itself. For example, for domain $B$, if "eggs" maps to itself, "coffee" must either map to "coffee" or to "eggs". Cross mappings are not allowed. So "eggs" maps to "coffee" and "coffee" maps to "eggs" is illegal.</p> <p>Given this regularity, let's work out a formula for the number of idempotent maps. For a 2 element domain, there are two cases: both elements map to themselves, or both map to one element, so the total is ${2 \choose 2} + {2 \choose 1} = 3$; For a 3 element domain, there are three cases: all map to themselves, two map to themselves and the third maps to one of them, or all three map to one, total is ${3 \choose 3} + {3 \choose 2}{2^1} + {3 \choose 1} = 1+ 3\times2 + 3 = 10$; For 4 element domain, four cases: all map to themselves, three map to themselves and the fouth to one of three, two map to themselves and remaining two map to those two, or all map to one: ${4 \choose 4} + {4 \choose 3}{3^1} + {4 \choose 2}{2^2} + {4 \choose 1} = 1 + 4\times3 + 6\times4 + 4 = 41$; and so on... A general formula for the number of idempotent maps emerges, it is $$\sum\limits_{k=0}^n {n \choose k}k^{n-k}$$ Where $n$ is the size of the domain. Notice that we used the results of the previous exercise in the derivation: the number of maps is $|codomain|^{|domain|}$.</p> <p><b><i>Article 1, Exercise 8. </i></b> Can you find a pair of maps $A \stackrel{f}{\longrightarrow} B \stackrel{g}{\longrightarrow} A$ for which $g \circ f = 1_A$? </p> <p>OK, this is asking if we can find a pair of maps that compose to an identity map of domain $A$, with the map $B$ in between. Let's see:</p> <div class="codeblock"> CM.core=&gt; (count (composed-solution-pairs B one-a))<br /> 0 </div> <p>No such map pair exists, so it is not possible to go through $B$ back to $A$. What about going through $A$ itself? </p> <div class="codeblock"> CM.core=&gt; (count (composed-solution-pairs A one-a))<br /> 6 </div> <p>There are 6 such map pairs. It is the same as going through another 3 element domain: </p> <div class="codeblock"> CM.core=&gt; (count (composed-solution-pairs #{1 2 3} one-a))<br /> 6 </div> <p>So when the domains have the same number of elements, it is possible for the map compositions to go through them back and forth. Let's look at the details of these map pairs:</p> <div class="codeblock"> CM.core=&gt; (composed-solution-pairs #{1 2 3} one-a)<br /> ([{:rule {3 "Sam", 2 "Mary", 1 "John"}, :domain #{1 2 3}, :codomain #{"John" "Mary" "Sam"}} {:rule {"Sam" 3, "Mary" 2, "John" 1}, :domain #{"John" "Mary" "Sam"}, :codomain #{1 2 3}}] [{:rule {3 "Mary", 2 "Sam", 1 "John"}, :domain #{1 2 3}, :codomain #{"John" "Mary" "Sam"}} {:rule {"Sam" 2, "Mary" 3, "John" 1}, :domain #{"John" "Mary" "Sam"}, :codomain #{1 2 3}}] [{:rule {3 "Sam", 2 "John", 1 "Mary"}, :domain #{1 2 3}, :codomain #{"John" "Mary" "Sam"}} {:rule {"Sam" 3, "Mary" 1, "John" 2}, :domain #{"John" "Mary" "Sam"}, :codomain #{1 2 3}}] [{:rule {3 "Mary", 2 "John", 1 "Sam"}, :domain #{1 2 3}, :codomain #{"John" "Mary" "Sam"}} {:rule {"Sam" 1, "Mary" 3, "John" 2}, :domain #{"John" "Mary" "Sam"}, :codomain #{1 2 3}}] [{:rule {3 "John", 2 "Sam", 1 "Mary"}, :domain #{1 2 3}, :codomain #{"John" "Mary" "Sam"}} {:rule {"Sam" 2, "Mary" 1, "John" 3}, :domain #{"John" "Mary" "Sam"}, :codomain #{1 2 3}}] [{:rule {3 "John", 2 "Mary", 1 "Sam"}, :domain #{1 2 3}, :codomain #{"John" "Mary" "Sam"}} {:rule {"Sam" 1, "Mary" 2, "John" 3}, :domain #{"John" "Mary" "Sam"}, :codomain #{1 2 3}}]) </div> <p>Obviously, for each pair, the two map rules are simply the reverse of the another, i.e. flipping the arrows around. If a map has an inverse, it is unique. These maps are called isomorphic, bijective, or one-to-one and onto. </p> <p>In fact, when the map in the middle has larger size than $A$, there may also be map pairs that compose to the identity of $A$. </p> <div class="codeblock"> CM.core=&gt; (count (composed-solution-pairs #{1 2 3 4} one-a))<br /> 72 </div> <p>In such pairs, each map is called <i>retraction</i> and <i>section</i> to each other. Let's calculate the number of retractions and sections.</p> <p><b><i>Article 2, Exercise 5 (1) </i></b> Given map $g$ (see code for its definition), how many maps $f$ are there with $g \circ f = 1_{\{0, 1\}}$?</p> <p>This is asking the number of sections of the map $g$, which has a 5 element domain $U$, and a 2 element codomain $D$. Three of the elments of $U$, b, p, q, map to 0 in $D$; two elements of $U$, r and s, map to 1 in $D$. The answer can be found by:</p> <div class="codeblock"> CM.core=&gt; (count (section g))<br /> 6 </div> <p>Basically, each section $f$ must choose two elements in $U$ to map 0 and 1 to, such that $g$ can map the results back to form an identity map on {0, 1}. For element 0, $f$ can choose one of b, p or q to map to; for element 1, $f$ can choose one of r and s. Therefore, the number of possible $f$ is $2 \times 3 = 6$. </p> <p>Obviously, not all maps have sections. </p> <div class="codeblock"> CM.core=&gt; (count (section f))<br /> 0 </div> <p>To have sections, the map must have a domain size larger than or equal to the codomain size. In addition, each elment of the codomain must be mapped to. Such map property is called surjective or onto. The general formula for the number of sections for map $g$ is therefore $$\prod_{i=1}^{n}m_i$$ where $n$ is the size of the codomain of $g$, and $m_i$ is the number of elements in the domain of $g$ that map to the $i$th element of the codomain.</p> <p><b><i>Article 2, Exercise 5 (2) </i></b> Choose a particular such $f$ (see code for its definition), how many maps $g$ satisfy $g \circ f = 1_{\{0, 1\}}$?</p> <p>Given a chosen $f$, this question is asking its number of retractions. The answer is: </p> <div class="codeblock"> CM.core=&gt; (count (retraction f))<br /> 8 </div> <p>For given $f$, 0 and 1 each maps to its own element in $U$, its retraction only need to flip the arrows to point back to 0 and 1, the remaining three element in $U$ can freely choose any of 0 and 1 to map to, so the number of retractions is the same as the total number of maps from a 3 element domain to a 2 element codomain, $2^3$. The general formula is $$n^{m-n}$$ where $n$ is the size of domain of $f$, and $m$ is its codomain size. To have retraction, a map must have a domain smaller or equal to the size of its codomain. In addition, it must be a one-to-one mapping, also called injective mapping. </p> <p><i><b>Number of section-retraction pairs</b></i> On page 117 of the book, the above formula for the number of retractions and sections of a given map are given, but it also says that the formula for the number of pairs of section/retraction in term of $m$ and $n$ is rather complicated. As it turns out, it is simple to derive a formula that is not complicated at all. First, each element of the smaller domain (size $n$) must map injectively to the lager domain (size $m$), the number of possibilities is just the number of permutation of choosing $n$ out of $m$. Then the remaining $m-n$ in the larger domain can freely choose any of the $n$ to map back to. Finally, we time up the two terms to arrive at $$\frac{m!}{(m-n)!}n^{m-n}$$ where $n$ is the size of domain $A$, m is the size of the domain $X$, $n \le m$, and $A \stackrel{s}{\longrightarrow} X \stackrel{r}{\longrightarrow} A$ satisfy $r \circ s = 1_A$.</p> <p>The formula seem to be correct as verified by the code:</p> <div class="codeblock"> CM.core=&gt; (count (retraction-section-pairs #{1 2} #{:a}))<br /> 0<br /> CM.core=&gt;(count (retraction-section-pairs #{1} #{:a}))<br /> 1<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2} #{:a :b}))<br /> 2<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2} #{:a :b}))<br /> 2<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2} #{:a :b :c}))<br /> 12<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2} #{:a :b :c :d :e}))<br /> 160<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3} #{:a :b :c}))<br /> 6<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3} #{:a :b :c :d}))<br /> 72<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3 4} #{:a :b :c :d}))<br /> 24<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3 4} #{:a :b :c :d :e}))<br /> 480<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3 4 5} #{:a :b :c :d :e}))<br /> 120<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3 4} #{:a :b :c :d :e :f}))<br /> 5760<br /> CM.core=&gt; (count (retraction-section-pairs #{1 2 3 4 5} #{:a :b :c :d :e :f}))<br /> 3600 </div> <p>Don't try the last two function calls, as they will run a long long time.</p> </div></div> <section> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=153&amp;2=comment_node_blog&amp;3=comment_node_blog" token="_L_AyMuM_-y6wIUtU1GJ-JKg4M37FCUSCBk_GDrq2PE"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Wed, 16 May 2012 20:33:16 +0000 Huahai 153 at https://yyhh.org Start learning category theory https://yyhh.org/blog/2012/04/start-learning-category-theory <span>Start learning category theory</span> <span><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span>Mon, 04/30/2012 - 08:48</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/notebook/math" id="taxonomy-term-25" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/math"> <div class="field field--name-name field--type-string field--label-hidden field__item">Math</div> </a> <div class="content"> </div> </div> </div> <div class="field__item"><div about="/notebook/categorytheory" id="taxonomy-term-38" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/categorytheory"> <div class="field field--name-name field--type-string field--label-hidden field__item">CategoryTheory</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p>Perhaps due to my rather small brain (literally), I dislike remembering tedious details. When in elementary school, I hated reciting classic Chinese poems, but liked composing my own :-).&nbsp; In high school, I hated chemistry but loved physics, because one could do everything based on a few principles in physics, whereas chemistry was all about memorization. Last year, I was chatting with a colleage of mine who had a Ph.D. degree in chemistry from Harvard. He said he's good at it because he could find patterns in all the tedious details and summerized them in his own head, so he didn't have to remember them all. So I said why not write those patterns down so others can benefit, and he didn't seem to like that idea. Anyway, let's go back to the main topic.</p> <p>I did not do very well in math classes in China, perhaps because Chinese math education focused too much on problem solving techniques, which were basically a matter of remembering all the test coping tricks. But I do like math, because I find it elegant and profound. A mathematical understanding of the world seems always the most economical one. So I want to learn more.</p> <p>To consistently apply my preferred model of starting from the most fundamental ideas, I decide to learn <a href="http://en.wikipedia.org/wiki/Category_theory">category theory</a>. It might sound strange as even many professional mathematicians consider this branch of mathematics "abstract nonsense", but I think it might fit my aptitude. Who knows. In any case, I am following <a href="http://math.ucr.edu/home/baez/topos.html">John Baez's book recommendation</a> as a guide, and will be updating my progress here from time to time, when I have something interesting to write about.</p> <p>UPDATE: Baez, among others, said they benefited from <a href="http://www.amazon.com/Topoi-Categorial-Analysis-Logic-Mathematics/dp/0486450260/">Goldbatt's book</a>, which I myself find very readable as well. However, this <a href="http://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=12&amp;ved=0CCUQFjABOAo&amp;url=http%3A%2F%2Fwww.cwru.edu%2Fartsci%2Fphil%2FUsesandAbuses%2520HistoryToposTheory.pdf">MacLarty's article</a> convinced me that Goldbatt's book might give the wrong idea about category theory. So I will stick with <a href="http://www.amazon.com/Conceptual-Mathematics-First-Introduction-Categories/dp/052171916X">Lawvere's</a> <a href="http://www.amazon.com/Sets-Mathematics-F-William-Lawvere/dp/0521010608">books</a>.</p> </div></div> <section> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=151&amp;2=comment_node_blog&amp;3=comment_node_blog" token="an6eGycI_zuqTFU8Sfi_MQ0S2F54z2eK-W3b7E0eGtg"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Mon, 30 Apr 2012 07:48:15 +0000 Huahai 151 at https://yyhh.org Why category thoery is fundamental yet hard to understand https://yyhh.org/blog/2012/01/why-category-thoery-fundamental-yet-hard-understand <span>Why category thoery is fundamental yet hard to understand</span> <span><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span>Sat, 01/07/2012 - 07:02</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/notebook/math" id="taxonomy-term-25" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/math"> <div class="field field--name-name field--type-string field--label-hidden field__item">Math</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-field-opinion field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/opinion/science" id="taxonomy-term-23" class="taxonomy-term vocabulary-opinion"> <a href="/opinion/science"> <div class="field field--name-name field--type-string field--label-hidden field__item">Science</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p>Category theory is mathematical theory about mapping. Some call it the foundation of mathematics.</p> <p>According to <a href="http://yyhh.org/blog/2008/04/%E6%98%BE%E5%BE%AE%E9%95%9C%E4%B8%8B%E7%9A%84%E6%95%B0%E5%AD%A6">my understanding of math as reproducible mental object</a>, this is not supprising. As all the brain does is doing mapping. Or in Plovian's words: conduction. Signals going from one set of neuros to another.&nbsp; Understanding mapping is fundamental obviously.</p> <p>Now category is billed the hardest to understand branch of mathematics, also known as "abstract nonsense".&nbsp;</p> <p>In programming world, functional programming is considered hard. Object Oriented Programming, the world of "nouns", despite of its limitations and actual complexity, is considered easier to understand.</p> <p>The reason is the same.</p> <p>Brain does not feel pain when touched or even lesioned. Similarly, the actual processing itself of the brain is hard to be perceived by brain itself. The results of the processing, often the nouns, are easier to be feeled. Hence property (nouns) are easier, mapping (verbs) is harder to conceputalize.</p> <p>However, mapping is still more fundamental, in term of governing the laws of mental operation.</p> <p>Training and education is the key. Cannot just cater to what feel easier.</p> </div></div> <section> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=148&amp;2=comment_node_blog&amp;3=comment_node_blog" token="r_JSBsZZLzoWyJw-74Wf5bSoJWeXBgESVeu0U4gb550"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Sat, 07 Jan 2012 07:02:40 +0000 Huahai 148 at https://yyhh.org Display LaTeX Math on Drupal with MathJax https://yyhh.org/blog/2011/07/display-latex-math-drupal-mathjax <span>Display LaTeX Math on Drupal with MathJax</span> <span><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span>Mon, 07/18/2011 - 21:19</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/notebook/software" id="taxonomy-term-24" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/software"> <div class="field field--name-name field--type-string field--label-hidden field__item">Software</div> </a> <div class="content"> </div> </div> </div> <div class="field__item"><div about="/notebook/math" id="taxonomy-term-25" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/math"> <div class="field field--name-name field--type-string field--label-hidden field__item">Math</div> </a> <div class="content"> </div> </div> </div> <div class="field__item"><div about="/notebook/latex" id="taxonomy-term-18" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/latex"> <div class="field field--name-name field--type-string field--label-hidden field__item">LaTeX</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p><a href="http://www.mathjax.org/">MathJax</a> seems to be the emerging standard for displaying math on the Web at this moment. It is supported by American Mathematical Society and American Physical Society, and has already been adopted by major math related discussion venues such as Physics Forums and Stack Exchange. MathJax displays math using CSS and Web fonts instead of images, so the quality is very high and is resize-friendly. Below are some examples:</p> <p>Inline math: the geometric product $\boldsymbol{uv}$ of vectors $\boldsymbol{u}$ and $\boldsymbol{v}$ is $\boldsymbol{u}\cdot\boldsymbol{v} + \boldsymbol{u}\wedge\boldsymbol{v}$, where $\boldsymbol{u}\cdot\boldsymbol{v}$ is the inner product and $\boldsymbol{u}\wedge\boldsymbol{v}$ is the outer product.</p> <p>Display math: the rotation of vector $\boldsymbol{u}$ by angle $\theta$ in plane $\boldsymbol{i}$ is $$R_{\boldsymbol{i}\theta}(\boldsymbol{u}) = e^{-\boldsymbol{i}\theta/2}\boldsymbol{u}e^{\boldsymbol{i}\theta/2}$$</p> <p>Some random expressions copied directly from the <a href="ftp://ftp.ams.org/ams/doc/amsmath/short-math-guide.pdf">short math guide for latex</a>: $$\begin{pmatrix}<br /> \alpha&amp; \beta^{*}\\<br /> \gamma^{*}&amp; \delta<br /> \end{pmatrix}$$ $$\frac{{\displaystyle\sum_{n > 0} z^n}}<br /> {{\displaystyle\prod_{1\leq k\leq n} (1-q^k)}}$$ $$2^k-\binom{k}{1}2^{k-1}+\binom{k}{2}2^{k-2}$$</p> <p> Finally, an obligatory integral: $$\int \!\!\! \int_D f(x,y)\,dx\,dy$$</p> <p> The math input could be either LaTeX or MathML, embedded in regular HTML text. MathJax is a Javascript library, so it works at the browser's side. After the HTML is rendered, the MathJax code scans the output, find pieces of text marked by user defined math delimiters (more on that later), and replaces them by typesetted math. The math may take a couple of seconds to show up depending on the browser and the network speed. The MathJax library can be fetched from content delivery network (CDN), so it is fairly simple to add MathJax to any Web site. Here is how I did it for this Drupal 6 site.</p> <p>There is a <a href="http://drupal.org/project/mathjax">Drupal module for MathJax</a> that loads MathJax from CDN for every Drupal page. The module also allows Drupal pages to be selectively MathJax enabled according to some URL patterns. By default, all pages are MathJax enabled except for these paths: <em>admin*, node/add/*, node/*/edit</em>, which is reasonable. However, path based selective enabling is still a bit crude, because most pages would not contain math, so MathJax simply slows down page rendering for most pages without any benefit. It would be great if the selection can also be tag based, so only the posts tagged as "math" and maybe the front page need to incur such slow down. Another useful feature would be to allow MathJax configuration within the module. The default configuration of MathJax is not very reasonable for Drupal, <span style="text-decoration: line-through">so users have to add the configurations manually to the page template at this time</span>. <b>Updated Aug.25, 2011:</b> Julou, the author of the module, has responded to my feature request and added a text area for MathJax configuration in the development version of the module. I have tested it and it works, so the steps below are no longer necessary.</p> <p>One of the most annoying default settings of MathJax is the definition of math delimiters.&nbsp; For inline math, the default delimiters are ( ) and \$\$, and for math on its own line, the default delimiters are [ ] and \$\$ \$\$.&nbsp; Because ( ) and [ ] are so commonly used in regular non-math text, this default setting is unreasonable for Drupal, as MathJax will remove the parentheses and change the text font to be math like for a Drupal page. \$\$ is okay, but to show the regular dollar sign properly, it needs to be escaped by reverse backslash, which is not the default setting of MathJax. Also, the default message display of MathJax is a bit too much for my taste, and I minimized them a bit. Here is the MathJax configuration I added to <em>page.tpl.php</em> for my theme:</p> <div class="codeblock" style="background-color:#ffffff"><span style="background-color: #ffffff"><font color="#007080">&lt;</font></span><span style="background-color: #ffffff"><font color="#1f3f81"><b>script</b></font></span><span style="background-color: #ffffff"><font color="#007080">&nbsp;</font></span><span style="background-color: #ffffff"><font color="#912f11"><b>type</b></font></span><span style="background-color: #ffffff"><font color="#007080">=</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;text/x-mathjax-config&quot;</font></span><span style="background-color: #ffffff"><font color="#007080">&gt;</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;MathJax.Hub.Config</font></span>(<span style="background-color: #ffffff"><font color="#007080">{</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;showProcessingMessages: </font></span><span style="background-color: #ffffff"><font color="#077807">false</font></span><span style="background-color: #ffffff"><font color="#912f11">, </font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;messageStyle: </font></span><span style="background-color: #ffffff"><font color="#077807">&quot;simple&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;jax: </font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;input/TeX&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">, </font></span><span style="background-color: #ffffff"><font color="#077807">&quot;output/HTML-CSS&quot;</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">, </font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;extensions: </font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;tex2jax.js&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;MathMenu.js&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;MathZoom.js&quot;</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">, </font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;TeX: </font></span><span style="background-color: #ffffff"><font color="#007080">{</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;extensions: </font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;AMSmath.js&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;AMSsymbols.js&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;noErrors.js&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;noUndefined.js&quot;</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">}</font></span><span style="background-color: #ffffff"><font color="#912f11">, </font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;HTML-CSS&quot;</font></span><span style="background-color: #ffffff"><font color="#912f11">: </font></span><span style="background-color: #ffffff"><font color="#007080">{</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;availableFonts: </font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#077807">&quot;TeX&quot;</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">}</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;tex2jax: </font></span><span style="background-color: #ffffff"><font color="#007080">{</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;inlineMath: </font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#077807">'$'</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">'$'</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;displayMath: </font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">[</font></span><span style="background-color: #ffffff"><font color="#077807">'$$'</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><span style="background-color: #ffffff"><font color="#077807">'$$'</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">]</font></span><span style="background-color: #ffffff"><font color="#912f11">,</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;processEscapes: </font></span><span style="background-color: #ffffff"><font color="#077807">true</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;&nbsp;&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">}</font></span><br /> <span style="background-color: #ffffff"><font color="#912f11">&nbsp;&nbsp;</font></span><span style="background-color: #ffffff"><font color="#007080">}</font></span>)<span style="background-color: #ffffff"><font color="#912f11">;</font></span><br /> <span style="background-color: #ffffff"><font color="#007080">&lt;/</font></span><span style="background-color: #ffffff"><font color="#1f3f81"><b>script</b></font></span><span style="background-color: #ffffff"><font color="#007080">&gt;</font></span> </div> <p><strong>Important</strong>: this configuration must be inserted <em>before</em> the line that loads MathJax itself. Otherwise, it has no effect. In the zen template I use, I inserted it before this line:</p> <p><code><br /> <?php print $scripts ?></code></p> <p>If your site has the <a href="http://drupal.org/project/print">print module</a> installed, you need to copy the file <em>print.tpl.php </em>to your theme templates directory, and add the same as above so the print friendly version also show math properly.&nbsp;</p> <p>For more MathJax configuration options, please refer to the <a href="http://www.mathjax.org/docs/1.1/configuration.html">MathJax documentation</a></p> </div></div> <ul class="links inline"><li class="comment-add"><a href="/blog/2011/07/display-latex-math-drupal-mathjax#comment-form" title="Share your thoughts and opinions." hreflang="und">Add new comment</a></li></ul><section> <a id="comment-1"></a> <article data-comment-user-id="0" class="js-comment comment"> <mark class="hidden" data-comment-timestamp="1311225240"></mark> <footer class="attribution"> <article typeof="schema:Person" about="/user/0"> <div class="field field--name-user-picture field--type-image field--label-hidden field__item"> <img src="/sites/default/files/styles/thumbnail/public/default_images/default-user-image.png?itok=hWWOuuKw" width="100" height="100" alt="Profile picture for user subhead" title="Anonymous user" typeof="foaf:Image" /> </div> </article> <div class="comment-submitted"> <p class="commenter-name"> <span lang="" typeof="schema:Person" property="schema:name" datatype="">Anonymous</span> </p> <p class="comment-time"> Thu, 07/21/2011 - 06:14 </p> </div> </footer> <div class="comment-text"> <div class="comment-arrow"></div> <h3><a href="/comment/1#comment-1" class="permalink" rel="bookmark" hreflang="en">Does this work for comments?</a></h3> <div class="content"> <div class="field field--name-comment-body field--type-text-long field--label-hidden field__item"><div class="tex2jax_process"><p>Does this work for comments? The square root of 4 equals 2:$\sqrt{4} = 2$</p> </div></div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=1&amp;1=default&amp;2=en&amp;3=" token="f2l6E3exSxFDxa_4V_oxZaC4lhbUjGxcEMIVDS7iiXI"></drupal-render-placeholder> </div> </div> </article> <a id="comment-18"></a> <article data-comment-user-id="0" class="js-comment comment"> <mark class="hidden" data-comment-timestamp="1312894896"></mark> <footer class="attribution"> <article typeof="schema:Person" about="/user/0"> <div class="field field--name-user-picture field--type-image field--label-hidden field__item"> <img src="/sites/default/files/styles/thumbnail/public/default_images/default-user-image.png?itok=hWWOuuKw" width="100" height="100" alt="Profile picture for user subhead" title="Anonymous user" typeof="foaf:Image" /> </div> </article> <div class="comment-submitted"> <p class="commenter-name"> <span lang="" typeof="schema:Person" property="schema:name" datatype="">Anonymous</span> </p> <p class="comment-time"> Tue, 08/09/2011 - 14:01 </p> </div> </footer> <div class="comment-text"> <div class="comment-arrow"></div> <h3><a href="/comment/18#comment-18" class="permalink" rel="bookmark" hreflang="en">sample tex file for testing </a></h3> <div class="content"> <div class="field field--name-comment-body field--type-text-long field--label-hidden field__item"><div class="tex2jax_process"><p>At each time$\tau$, the test uses as the conditional inference frame<br /> the set$\mathcal{F}(H_\tau)$of all tables with the same three two-way<br /> marginal tables as the obtained table$H_\tau\$.</p> </div></div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=18&amp;1=default&amp;2=en&amp;3=" token="jVhirhDd91eC6XV9WsQl4PLehFgKsOgX9j8i4FgvScs"></drupal-render-placeholder> </div> </div> </article> <a id="comment-26"></a> <article data-comment-user-id="0" class="js-comment comment"> <mark class="hidden" data-comment-timestamp="1314267707"></mark> <footer class="attribution"> <article typeof="schema:Person" about="/user/0"> <div class="field field--name-user-picture field--type-image field--label-hidden field__item"> <img src="/sites/default/files/styles/thumbnail/public/default_images/default-user-image.png?itok=hWWOuuKw" width="100" height="100" alt="Profile picture for user subhead" title="Anonymous user" typeof="foaf:Image" /> </div> </article> <div class="comment-submitted"> <p class="commenter-name"> <span lang="" typeof="schema:Person" property="schema:name" datatype="">julou</span> </p> <p class="comment-time"> Thu, 08/25/2011 - 11:21 </p> </div> </footer> <div class="comment-text"> <div class="comment-arrow"></div> <h3><a href="/comment/26#comment-26" class="permalink" rel="bookmark" hreflang="en">Custom configuration</a></h3> <div class="content"> <div class="field field--name-comment-body field--type-text-long field--label-hidden field__item"><div class="tex2jax_process"><p>I just implemented custom configuration in the current dev version… so you can set it in the admin section.<br /> Enjoy!</p> </div></div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=26&amp;1=default&amp;2=en&amp;3=" token="EmVUnj3z2YTfN-OcyU4E9A4MBsv14xGvLjouZVdNTnc"></drupal-render-placeholder> </div> </div> </article> <div class="indented"><a id="comment-27"></a> <article data-comment-user-id="1" class="js-comment comment"> <mark class="hidden" data-comment-timestamp="1314300840"></mark> <footer class="attribution"> <article typeof="schema:Person" about="/user/huahai"> <div class="field field--name-user-picture field--type-image field--label-hidden field__item"> <img src="/sites/default/files/styles/thumbnail/public/pictures/2017-11/huahai.jpg?itok=ZwjJWYAc" width="88" height="100" alt="Profile picture for user Huahai" typeof="foaf:Image" /> </div> </article> <div class="comment-submitted"> <p class="commenter-name"> <a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a> </p> <p class="comment-time"> Thu, 08/25/2011 - 20:34 </p> </div> <p class="visually-hidden">In reply to <a href="/comment/26#comment-26" class="permalink" rel="bookmark" hreflang="en">Custom configuration</a> by <span lang="" typeof="schema:Person" property="schema:name" datatype="">julou</span></p> </footer> <div class="comment-text"> <div class="comment-arrow"></div> <h3><a href="/comment/27#comment-27" class="permalink" rel="bookmark" hreflang="en">It works</a></h3> <div class="content"> <div class="field field--name-comment-body field--type-text-long field--label-hidden field__item"><div class="tex2jax_process"><p>Hi Julou, I have removed my template changes and am using the current version of the module. As we can see, it works. Thank you very much <img src="/sites/all/libraries/tinymce/jscripts/tiny_mce/plugins/emotions/img/smiley-smile.gif" alt="Smile" title="Smile" border="0"></p> </div></div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=27&amp;1=default&amp;2=en&amp;3=" token="otqyAq8w5Pd0NDlWnsqJTdnPUTbkZ3076qwkZ7AXGc8"></drupal-render-placeholder> </div> </div> </article> </div> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=135&amp;2=comment_node_blog&amp;3=comment_node_blog" token="HoqMxjelTon9qsN6_9KIEFcn_IjO-NtziRBWWApkl3s"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Mon, 18 Jul 2011 20:19:58 +0000 Huahai 135 at https://yyhh.org 显微镜下的数学 https://yyhh.org/blog/2008/04/%E6%98%BE%E5%BE%AE%E9%95%9C%E4%B8%8B%E7%9A%84%E6%95%B0%E5%AD%A6 <span>显微镜下的数学</span> <span><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span>Sat, 04/12/2008 - 06:29</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/notebook/psychology" id="taxonomy-term-29" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/psychology"> <div class="field field--name-name field--type-string field--label-hidden field__item">Psychology</div> </a> <div class="content"> </div> </div> </div> <div class="field__item"><div about="/notebook/math" id="taxonomy-term-25" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/math"> <div class="field field--name-name field--type-string field--label-hidden field__item">Math</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p>显微镜下的数学 Mathematics under microscope (<a href="http://www.maths.manchester.ac.uk/~avb/micromath">电子书下载</a>)是本数学家写的<br /> 从认知角度看数学的书，挺有启发性的。</p> <p>数学和心理学的联系，应该要比和物理学的联系还要紧密，还要更自然：数学是心理产<br /> 物（Mathematics is the study of mental objects with reproducible properties.<br /> p.IX），而心理学是研究心理产物的产生过程的。如果对心理产物本身都不了解，如<br /> 何能正确认识其产生过程？</p> <p>从方法上看，心理学也需要极大地增加数学的使用。有很多心理学实验结果，其实可以<br /> 找到数学上的解释。比如最近的一个例子，关于认知失调（cognitive dissonant)的，<br /> 实验结果用概率论也能解释，而不一定就是认识失调。具体可见这篇<a href="http://tierneylab.blogs.nytimes.com/2008/04/07/monty-hall-meets-cognitive-dissonance/">纽约时报的文章</a></p> <p>另外一个有趣的观点，就是很多高深的数学计算，其实是很低级的神经系统作的。好的<br /> 数学家能把这些低级功能带入意识来。一个有关的例子就是所谓的白痴数学家。</p> <p>我还没看完这本书，觉得挺有意思的，推荐一下。</p> </div></div> <section> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=89&amp;2=comment_node_blog&amp;3=comment_node_blog" token="8GSi2ZcwyqiSVsMWh7dZezk-Y1mGJBsbxqlxSmpdKhE"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Sat, 12 Apr 2008 05:29:08 +0000 Huahai 89 at https://yyhh.org Yaha! A Math Game for Everyone. https://yyhh.org/yaha-math-game-everyone <span property="schema:name">Yaha! A Math Game for Everyone.</span> <span rel="schema:author"><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span property="schema:dateCreated" content="2007-10-31T05:43:56+00:00">Wed, 10/31/2007 - 05:43</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><a href="/notebook/math" hreflang="en">Math</a></div> </div> <div property="schema:text" class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p>Yaha! is a traditional math game popular among Chinese speaking populations. It has been historically given a varieties of names, but the essence of the game is the same: the players are given 4 playing cards, their task is to use the 4 numbers represented by the cards, use them once and only once, to get the number 24. The players are allowed to apply all four arithmetic operations: addition, subtraction, multiplication and division, as well as parenthesis as they see fit. The winner is whoever comes up with a correct solution first.</p> <p>This program is a simulation of the game. It is at present form designed to be played by only one player. Remember, your goal is to form an arithmetic expression by dragging cards and operators into appropriate slots and let the result of your expression equals to 24. Got it? Click the picture below to load the game. It will start in a new window.</p> <p><a href="/game/yaha.html" target="yaha"><img alt="" data-entity-type="" data-entity-uuid="" src="/game/yaha-scr.gif" width="590" /> </a></p> <h2>Question and Answers:</h2> <dl> <dt>Why 24, but not any other numbers?</dt> <dd>It turns out that the probability of getting an answer of twenty-four is larger than any other numbers [1]. So, the game is fun to play.</dd> <dd>&nbsp;</dd> <dt>Why do I get an error message while trying to load the game?</dt> <dd>Please make sure your browser supports Java.</dd> <dd>&nbsp;</dd> <dt>What numbers do "A", "K", "Q" and "J" represent?</dt> <dd>"A" = 1, "K"=13, "Q"=12, "J" =11</dd> <dd>&nbsp;</dd> <dt>What shall I do if I cannot figure out the solution?</dt> <dd>Probably there is actually no solution. If it is the case, the correct action is to click "No solution" button. But maybe it has. Anyway, you'd better make you decision as soon as possible.</dd> <dd>&nbsp;</dd> <dt>What is that little disc for?</dt> <dd>It is a clock reminding you the remaining time for this deal of game. If time out, you will not be allowed to continue. You set the time limit by selecting "Beginner-Expert" choice.</dd> <dd>&nbsp;</dd> <dt>How long am I allowed to play for a new deal of cards?</dt> <dd>Beginners have 2 minutes, Intermediate players have 1.5 minutes, and Experts have only 1 minute to finish their job.</dd> <dd>&nbsp;</dd> <dt>What does that score count for?</dt> <dd>Its value is determined by your answer, your response time and the level your selected, such that</p> <ul> <li>You score increases when you get the right answer, otherwise your score decreases.</li> <li>The more quickly you response, the higher score you will get.</li> <li>Experts get higher score when they have the right answer, but lose more points when they make a mistake.</li> </ul> </dd> <dt>&nbsp;</dt> <dt>Can I play the game offline?</dt> <dd>Yes. This game runs as a standalone application as well. You may <a href="http://sourceforge.net/project/showfiles.php?group_id=218193">download it</a>, unpack it, go to where the unpacked jar file is, and issue the command <em>javaw -jar yaha.jar</em> in a command console to start the game. The next version will include an installer, if you want to help, drop me a line.</dd> <dt>&nbsp;</dt> <dt>Can I get the source code?</dt> <dd>Of course. Yaha! is an open source software. It is released under <a href="http://www.fsf.org/licensing/licenses/agpl-3.0.html">GNU AGPL</a> license. So it is free, as in freedom. Grab the code <a href="http://sourceforge.net/project/showfiles.php?group_id=218193">here</a>.</dd> <dd>&nbsp;</dd> <dt>Any future plan?</dt> <dd>Absolutely, if you want to contribute to the project, go to <a href="http://yaha.sourceforge.net/">yaha.sourceforge.net</a>, where you can see the <a href="http://yaha.sourceforge.net/roadmap.html">roadmap</a> currently planned for Yaha! Drop me an email if you want to join.</dd> </dl> <p>[1] Yixun Shi, 1999, A mathematical study of the game "twenty-four points", <em>The Mathematics Teacher</em>. Vol. 92, Iss. 9, p. 828</p> </div></div> <ul class="links inline"><li class="comment-add"><a href="/yaha-math-game-everyone#comment-form" title="Share your thoughts and opinions." hreflang="und">Add new comment</a></li></ul><section> <a id="comment-53"></a> <article data-comment-user-id="0" class="js-comment comment"> <mark class="hidden" data-comment-timestamp="1337695499"></mark> <footer class="attribution"> <article typeof="schema:Person" about="/user/0"> <div class="field field--name-user-picture field--type-image field--label-hidden field__item"> <img src="/sites/default/files/styles/thumbnail/public/default_images/default-user-image.png?itok=hWWOuuKw" width="100" height="100" alt="Profile picture for user subhead" title="Anonymous user" typeof="foaf:Image" /> </div> </article> <div class="comment-submitted"> <p class="commenter-name"> <a rel="nofollow" href="http://www.coolmathgamess.net" lang="" typeof="schema:Person" property="schema:name" datatype="">coolmathgames</a> </p> <p class="comment-time"> Tue, 05/22/2012 - 15:04 </p> </div> </footer> <div class="comment-text"> <div class="comment-arrow"></div> <h3><a href="/comment/53#comment-53" class="permalink" rel="bookmark" hreflang="en">Great Equation</a></h3> <div class="content"> <div class="field field--name-comment-body field--type-text-long field--label-hidden field__item"><div class="tex2jax_process"><p>I love math games. I love the way you do this I'm going to share it with my students.</p> </div></div> <drupal-render-placeholder callback="comment.lazy_builders:renderLinks" arguments="0=53&amp;1=default&amp;2=en&amp;3=" token="YoL2HeMCtMUjkAs8DNSF_I4HkdrPeJDdN9h04oE5ejQ"></drupal-render-placeholder> </div> </div> </article> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=32&amp;2=comment_node_page&amp;3=comment_node_page" token="9lBeoEsFdLTl7c3wS_2xXb3qCusFya7G5_bpr0y6WUE"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Wed, 31 Oct 2007 05:43:56 +0000 Huahai 32 at https://yyhh.org 一个关于逻辑和概率的简单科普 https://yyhh.org/blog/2007/10/%E4%B8%80%E4%B8%AA%E5%85%B3%E4%BA%8E%E9%80%BB%E8%BE%91%E5%92%8C%E6%A6%82%E7%8E%87%E7%9A%84%E7%AE%80%E5%8D%95%E7%A7%91%E6%99%AE <span>一个关于逻辑和概率的简单科普</span> <span><a title="View user profile." href="/user/huahai" lang="" about="/user/huahai" typeof="schema:Person" property="schema:name" datatype="">Huahai</a></span> <span>Tue, 10/30/2007 - 07:24</span> <div class="field field--name-field-notebook field--type-entity-reference field--label-hidden field__items"> <div class="field__item"><div about="/notebook/math" id="taxonomy-term-25" class="taxonomy-term vocabulary-notebook"> <a href="/notebook/math"> <div class="field field--name-name field--type-string field--label-hidden field__item">Math</div> </a> <div class="content"> </div> </div> </div> </div> <div class="field field--name-body field--type-text-with-summary field--label-hidden field__item"><div class="tex2jax_process"><p>人们一般说的逻辑指命题逻辑，它是理性思维的基本规则。比如以罗素为代表的数理哲学家就把逻辑看作数学的基础。命题逻辑的规则并不多，很多人心里都是知道一些大概的的，不过有人没有正式学过，有些地方不太清楚而已。这篇文章就简单扼要地说一说。更重要的是，本文要介绍现在一个新的进展，就是把概率论看作逻辑的扩展，把传统的亚里士多德的逻辑看成是概率论的特例。</p> <p>让我们用大写字母表示命题，命题可以是任何论断或描述，有真假之分。比如，A:="今天要下雨"，:=表示"定义为"。这命题可能是真，也可以为假。逻辑学不管命题究竟现实是真是假，只根据这些命题的已知的真假，来决定逻辑推理的结论是真是假。</p> <p>简单的命题可以用布尔逻辑操作符来组合成复杂的命题。比如，A:="今天要下"， B:="我出门要带伞"， AB:="今天要下雨，并且，我出门要带伞"。A＋B:="今天要下雨，或者，我出门要带伞"。～A 表示否定，"今天不会下雨"。</p> <p>注意，只有当A，B都为假时，A＋B才为假，其余三种情况，A＋B都为真，这和人们日常的"或者"含义有点不同。</p> <p>当A和B的真假情况总是一致的时候，我们用A＝B表示。</p> <p>下面就是布尔逻辑的一些规则：</p> <div class="codeblock"><code><br /> AA ＝ A<br /> A+A ＝ A<br /> AB ＝ BA<br /> A+B = B+A<br /> A(BC) = (AB)C = ABC<br /> A+(B+C) = (A+B)+C = A+B+C<br /> 如果C＝AB， 那么～C＝～A＋～B<br /> 如果D＝A＋B， 那么～D＝～A～B<br /></code></div> <p>现在我们讲逻辑最有用的操作：推论。如果A为真，那么B也必须为真，我们就用A=&gt;B来表示，也就是A推出B。注意，A=&gt;B和A＝AB是一回事，也意味着A～B是假，也就是～A＋B是真。这其实就是所谓的两种强三段论：</p> <div class="codeblock"><code><br /> (１)<br /> A=&gt;B<br /> A 为真<br /> －－－－－<br /> B为真 <p>(２)<br /> A=&gt;B<br /> B 为假<br /> －－－－－<br /> A为假<br /></p></code></div> <p>注意，当A为假的时候，A=&gt;B不能确定地告诉我们B的真假，B可能为真，也可能为假。当B为真的时候，A=&gt;B不能确定地告诉我们A的真假，A可能为真，也可能为假。事实上，有的逻辑学家认为，A=&gt;B只有当A真B假的时候才为假，其他三种情况都为真！也就是说，A为假时，A=&gt;B总是真的，无论B是什么命题。这也是很多错误推论的由来：错的前提可以推出一切结果！</p> <p>当然，这样的逻辑规则好像太有局限性了。首先，命题只有正误之分，要么对，要么错。这和人的常识不一致，因为人对很多东西的判断不是那么绝对的，有不确定性。第二，强的三段论也对很多我们想作的推论无能为力（具体见上面一段）。现在一个发展，就是把概率论看作逻辑的延伸，解决这两个布尔逻辑的局限性问题。</p> <p>概率论这个数学分支有两种传统。一个是把概率看成客观的随机事件发生的频率。这是一般学校教的传统。持这种观点的人叫频率论者。另一个，也就是我这儿谈的，把概率看成是对命题正确可能性的判断。这样，布尔逻辑就是概率论的特例，命题确定为真就是概率为１，反之为０，在０和１之间的实数反映了命题为真的不确定性。比如，p(A)=0.8, 其值接近１，表示我认为"今天要下雨"的命题为真的可能性是比较大的; p(B|A)=1是一个条件概率，其值为１，表示在我认为今天要下雨的条件下，我出门肯定要带伞的。事实上，这后一种概率论是概率最初的本意，持这种观点的人叫贝叶斯论者。当然，又有主观客观贝叶斯论者之分。无论那种概率论的解读，概率论的规则是一样的。而且，很多不同角度的数学证明表明，概率论的规则是唯一和人的常识一致的，自洽的一套不确定性推理规则。</p> <p>概率论的规则只有两条。</p> <div class="codeblock"><code><br /> *乘法律： <p>p(AB|C) = p(A|C)p(B|AC) = p(B|C)p(A|BC)<br /> p(A|C) + p(~A|C) = 1</p> <p>*加法律：</p> <p>p(A+B|C) = p(A|C) + p(B|C) - p(AB|C)<br /></p></code></div> <p>这样写主要是强调所有概率都是条件概率，这儿C是一个背景命题，代表所有这些命题的前提条件。</p> <p>上面已经提到，布尔逻辑是概率论的特例，自然，这些概率论规则已经包含了强三段论。下面我们就看看是不是这样。</p> <p>让我们定义C:="A=&gt;B"，也就是说让C代表"如果A真，那么B真"，这是强三段论的主前提。我们知道，A=&gt;B就是A＝AB，我们的定义就等于说p(A|C)=p(AB|C)。</p> <p>先看强三段论(1)， 根据概率论的乘法律，我们有</p> <p>p(B|AC) = p(AB|C) / p(A|C) = 1</p> <p>这就与强三段论(1)的结论一致了。我们可以同样证明强三段论(2)与概率论规则的一致性。</p> <p>概率论真正的好处，当然是让我们进行逻辑所不能作的推论。这就是所谓的弱三段论。</p> <div class="codeblock"><code><br /> (3)<br /> A=&gt;B<br /> B为真<br /> －－－－<br /> A为真的可能性增大 <p>(4)<br /> A=&gt;B<br /> A为假<br /> －－－－<br /> B为假的可能性增大<br /></p></code></div> <p>这儿我们证明弱三段论(3)和概率论的规则一致。要证明(3), 其实就是要证明p(A|BC) &gt;= p(A|C)。根据概率论的乘法律，我们有</p> <p>p(A|BC) = p(A|C)p(B|AC) / p(B|C)</p> <p>这儿顺便提一下（这段可以跳过），这个公式就是有名的贝叶斯定理。p(A|BC)叫后验概率（posterior），是我们想知道的结论。p(A|C)叫先验概率(prior)，是在不知道B真假的情况下A为真的可能性。 p(B|AC)叫似然概率（likelihood)，是在A为真的条件下B发生的可能性。p(B|C)这个分母把后验概率的值规范化为一个正确的概率值(0 &lt;= p &lt;= 1)。</p> <p>我们在证明(1)的时候已经证明了当前提是A=&gt;B时，p(B|AC)=1，所以，上面的公式现在变成了</p> <p>p(A|BC) = p(A|C) / p(B|C)</p> <p>因为p(B|C)是一个概率值，p(B|C)&lt;=1</p> <p>p(A|BC) &gt;= p(A|C)</p> <p>这就证明了弱三段论(3)是与概率论一致的。弱三段论(4)的一致性证明留作家庭作业。</p> <p>事实上，还有个更弱的三段论：</p> <div class="codeblock"><code><br /> (5)<br /> 如果A为真，那么B为真的可能性增大<br /> B为真<br /> －－－－<br /> A为真的可能性增大<br /></code></div> <p>这个三段论的证明相当于要证明p(A|BC)&gt;=p(A|C)，这儿的C是任意背景命题。先看这个三段论的主前提，可写成p(B|AC)&gt;=p(B|C)，这样，从前面提到的贝叶斯定理</p> <p>p(A|BC) = p(A|C)p(B|AC) / p(B|C)</p> <p>就自然得到p(A|BC)&gt;=p(A|C)，证毕。</p> <p>相应的，我们也有：</p> <div class="codeblock"><code><br /> (6)<br /> 如果A为真，那么B为真的可能性增大<br /> A为假<br /> －－－－<br /> B为假的可能性增大<br /></code></div> <p>这些弱三段论也是人们日常使用的推理规则，现在概率论给他们赋予了理论上的合理性。当然，要记住的是用弱三段论所作的推论不像用强三段论作出的推论那样有逻辑上的确定性。</p> <p>参考文献：</p> <p>E.T. Jaynes, Probability Theory: the Logic of Science, Cambridge Press, 2003</p> </div></div> <section> <h2>Add new comment</h2> <drupal-render-placeholder callback="comment.lazy_builders:renderForm" arguments="0=node&amp;1=23&amp;2=comment_node_blog&amp;3=comment_node_blog" token="1EJNa8MFyUSLuMlYetv61xDFUXzPk7fDK4xZgMWM6h8"></drupal-render-placeholder> </section> <strong class="node_view"></strong> Tue, 30 Oct 2007 07:24:49 +0000 Huahai 23 at https://yyhh.org