Fun with Triangular Numbers + Triangular Corporate Logos

I’ve always been fond of triangular numbers [1] and found the pattern they create beautiful.

                         .
                       .   .
                     .   .   .
                   .   .   .   .
                 .   .   .   .   .
               .   .   .   .   .   .
             .   .   .   .   .   .   .
           .   .   .   .   .   .   .   .
         .   .   .   .   .   .   .   .   .
       .   .   .   .   .   .   .   .   .   .
     .   .   .   .   .   .   .   .   .   .   .

Embedded within are many a corporate logo:

Mitsubishi, Chase, Star of David, &c.

Mitsubishi Triangle Logo

Chase Logo

I wanted to find out the relationships between the dots and the possible lines drawn between them. For example.
If you have T_n, how many dots exist as a recurrence relation? How many lines can one draw between the dots for T_n?

           o
          / \  (T_2 -> n_2 = 3)
         o - o

number of dots:
d_i = d_{i-1} + i
number of lines:
n_i = n_{i-1} + 3 * (i – 1)

I wrote some of it up in CLISP to check it out:

;; calculates numer of edges in triangular graph
(defun nlines (i)
(let ((prev (- i 1)))
(if (eq i 0)
0
(+ (* 3 prev) (nlines prev)))))

;; returns number of nodes in graph
(defun ndots (i)
(if (eq i 0)
0
(+ (ndots (- i 1)) i)))

Now what if we looked at the triangular numbers with respect to a triangular array like Pascal’s Triangle? [2]
That is, how many operations need to take place in order to calculate the entire triangle?
; calcs number of operations for triangular arrays based on n
; nops = 6, i = 3
; .
; / \
; . . (nops 3) == 6
; / \ / \
; . . .
;
(defun nops (i)
(if (eq i 0)
0
(let ((prev (- i 1)))
(+ (nops prev) (* 2 prev)))))

I brute-forced the limit by just putting large numbers in and found out that the ratio between the number of dots and lines approaches 1/3 as i approaches infinity and the number of dots to the number of operations (in a triangular array) approaches 1/2 as i approaches infinity.

\lim_{i\to\infty} \frac{ndots_I}{nlines_i} = \frac{1}{3}
\lim_{i\to\infty} \frac{ndots_I}{nops_i} = \frac{1}{2}

Sources:
http://en.wikipedia.org/wiki/Triangular_number
http://en.wikipedia.org/wiki/Pascal’s_triangle

An elegant pythonic solution to – TypeError: is not JSON serializable

So if you ever have to deal with python and JSON at the same time, I’m sure you’ve run into this problem at least once.  With Baybo, the typical example of this is failure for json.dumps to successfully coerce Decimal objects into strings (go figure).  Anyways, the solution to the error above is simply:

dictionary = {"key": "val", "foo": Decimal("8.88")}

dict(map(lambda x: isinstance(x[1], Decimal) and (str(x[0]), str(x[1])) or (str(x[0]), x[1]), dictionary.items()))

Let’s break that down a bit

dictionary.items() => [("key", "val"), ("foo", Decimal("8.88"))]

lambda x: lambda x: isinstance(x[1], Decimal) and (str(x[0]), str(x[1])) or (str(x[0]), x[1]) => ("foo", "8.88")

map turns it all into [("key", "val"), ("foo", "8.88")] if it is in fact a Decimal object, otherwise leaves it alone

which we wrap up with a dict() => {"key": "val", "foo": "8.88"} and dump to json

'{"key": "val", "foo": "8.88"}'

voila! problem solved!

edit (2011-01-04): after looking over this again, I would like to comment that one *might* want to keep integers of type int, list of type list, etc. simply mapping everything to a string is a crufty hack at best. I’ve revised the code to adjust for this. isinstance(x[1], Decimal) and (str(x[0]), str(x[1])) or ... the ever useful and-or logic short-circuting which you may be familiar with if you’ve programmed in C or JavaScript.

edit (2015-12-07): Switched from type(foo) == “TypeString” to isinstance(foo, TypeString).

Working with utf8 (like Chinese) with sqlAlchemy + Elixir

Today I’ve been playing around with sqlAlchemy and Elixir.

I was having problems with how some strings (Chinese Characters) were being encoded and stored. The issue was with the

metadata.bind statement (create_engine() without Elixir)

before it was:

metadata.bind = 'mysql://username:password@localhost/database'

You need to select utf8 encoding manually.

metadata.bind = 'mysql://username:password@localhost/database?charset=utf8'

or, plain SQLAlchemy:

db = create_engine('mysql://...', encoding='utf8')

back button doesn’t revert, reload, retrigger scripts | jQuery + JavaScript

The answer to this tricky question, after much google searching came down to be the way in which the WebKit Cache works and how the unload event is not fired when the page is called from cache.

the following solution worked for me;
adding the onunload=”” to the body tag.

another solution is to create event listeners like this:
http://webkit.org/blog/516/webkit-page-cache-ii-the-unload-event/

Original Solution
http://stackoverflow.com/questions/158319/cross-browser-onload-event-and-the-back-button/201406#201406

FOAF – Friend of a Friend – Machine readable people

So, I just created my first FOAF file using http://www.ldodds.com/foaf/foaf-a-matic.

<rdf:RDF
xmlns:rdf=”http://www.w3.org/1999/02/22-rdf-syntax-ns#”
xmlns:rdfs=”http://www.w3.org/2000/01/rdf-schema#”
xmlns:foaf=”http://xmlns.com/foaf/0.1/”
xmlns:admin=”http://webns.net/mvcb/”>
<foaf:PersonalProfileDocument rdf:about=””>
<foaf:maker rdf:resource=”#me”/>
<foaf:primaryTopic rdf:resource=”#me”/>
<admin:generatorAgent rdf:resource=”http://www.ldodds.com/foaf/foaf-a-matic”/>
<admin:errorReportsTo rdf:resource=”mailto:leigh@ldodds.com”/>
</foaf:PersonalProfileDocument>
<foaf:Person rdf:ID=”me”>
<foaf:name>Stephen Balaban</foaf:name>
<foaf:givenname>Stephen</foaf:givenname>
<foaf:family_name>Balaban</foaf:family_name>
<foaf:nick>Steve</foaf:nick>
<foaf:mbox_sha1sum>a245f1ad98c749cca35b14a078581654e7a48faa</foaf:mbox_sha1sum>
<foaf:homepage rdf:resource=”http://www.stephenbalaban.com/”/>
<foaf:workplaceHomepage rdf:resource=”http://www.yuanfenflow.com/”/>
<foaf:workInfoHomepage rdf:resource=”http://www.yuanfenflow.com/en/people/stephen-balaban/”/>
<foaf:schoolHomepage rdf:resource=”http://www.umich.edu/”/></foaf:Person>
</rdf:RDF>