<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-7635392334609494121</id><updated>2012-01-22T14:44:12.209+01:00</updated><category term='ruby'/><category term='matplotlib'/><category term='transformation'/><category term='snippets'/><category term='visualization'/><category term='reflection'/><category term='matrix'/><category term='vector'/><category term='python'/><category term='golang'/><category term='pyplot'/><title type='text'>Bits from Thomas</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://bitsfromthomas.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://bitsfromthomas.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Thomas</name><uri>http://www.blogger.com/profile/13825031472497321919</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>3</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-7635392334609494121.post-4407210808299824979</id><published>2012-01-21T10:34:00.002+01:00</published><updated>2012-01-21T10:42:44.155+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='vector'/><category scheme='http://www.blogger.com/atom/ns#' term='visualization'/><category scheme='http://www.blogger.com/atom/ns#' term='pyplot'/><category scheme='http://www.blogger.com/atom/ns#' term='matplotlib'/><category scheme='http://www.blogger.com/atom/ns#' term='python'/><category scheme='http://www.blogger.com/atom/ns#' term='matrix'/><category scheme='http://www.blogger.com/atom/ns#' term='transformation'/><title type='text'>Visualizing matrix transformations i pyplot</title><content type='html'>I was playing around with some matrix transformations and trying to visualize them using python and pyplot. Run for example as: 'ipython -pylab pyplot_matrix_transformations.py' (-pylab not really needed).&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;# Visualizing some vector/matrix-transformations using pylab and pyplot&lt;br /&gt;# Thomas Lundqvist, 2012-01-21&lt;br /&gt;# Public Domain. Use freely!&lt;br /&gt;&lt;br /&gt;from pylab import *&lt;br /&gt;import matplotlib.pyplot as plt&lt;br /&gt;from mpl_toolkits.mplot3d import Axes3D&lt;br /&gt;fig = plt.figure()&lt;br /&gt;# ax = fig.add_subplot(111)&lt;br /&gt;ax = Axes3D(fig)&lt;br /&gt;&lt;br /&gt;# Draws lines between all points defined as column &lt;br /&gt;# vectors in matrix a. pyplot makes each call to plot&lt;br /&gt;# use a different color&lt;br /&gt;&lt;br /&gt;def p(a):&lt;br /&gt;   ax.plot(a[0].tolist()[0], a[1].tolist()[0], zs=a[2].tolist()[0])&lt;br /&gt;&lt;br /&gt;# Original set of points&lt;br /&gt;&lt;br /&gt;a = mat(&lt;br /&gt; [[0,0,0,1],&lt;br /&gt;  [1,0,0,1],&lt;br /&gt;  [1,1,0,1],&lt;br /&gt;  [0,1,0,1],&lt;br /&gt;  [0,0,0,1]]).T&lt;br /&gt;p(a)&lt;br /&gt;&lt;br /&gt;# Shear, new x depends on 1 * old y&lt;br /&gt;# Translate z-coord +0.2&lt;br /&gt;m = mat(&lt;br /&gt; [[1,1,0,  0],&lt;br /&gt;  [0,1,0,  0],&lt;br /&gt;  [0,0,1,0.2],&lt;br /&gt;  [0,0,0,  1]])&lt;br /&gt;b = m*a&lt;br /&gt;p(b)&lt;br /&gt;&lt;br /&gt;# Scale y 2 times and translate +1 in z&lt;br /&gt;m2 = mat(&lt;br /&gt; [[1,0,0,0],&lt;br /&gt;  [0,2,0,0],&lt;br /&gt;  [0,0,1,1],&lt;br /&gt;  [0,0,0,1]])&lt;br /&gt;c = m2*a&lt;br /&gt;p(c)&lt;br /&gt;&lt;br /&gt;# Do both transformations (m and m2)&lt;br /&gt;p(m2*b)&lt;br /&gt;&lt;br /&gt;# Rotate the 'a' and 'c' shapes 5 degrees 20 times&lt;br /&gt;# around the z-axis&lt;br /&gt;for i in range(20):&lt;br /&gt;   r = deg2rad((i+1)*5)&lt;br /&gt;   mr = mat(&lt;br /&gt; [[cos(r), -sin(r), 0, 0],&lt;br /&gt;  [sin(r),  cos(r), 0, 0],&lt;br /&gt;  [     0,       0, 1, 0],&lt;br /&gt;  [     0,       0, 0, 1]])&lt;br /&gt;   p(mr*a)&lt;br /&gt;   p(mr*c)&lt;br /&gt;&lt;br /&gt;# Display everything&lt;br /&gt;fig.show()&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;The result should look like this. pyplot lets me rotate the graph using the mouse:&lt;div class="separator" style="clear: both; text-align: center;"&gt;&lt;a href="http://2.bp.blogspot.com/-_-1ZY6LqfnA/TxqHgshfF8I/AAAAAAAAATQ/wAjBaCdgJ8k/s1600/pyplot_example.png" imageanchor="1" style="clear:left; float:left;margin-right:1em; margin-bottom:1em"&gt;&lt;img border="0" height="241" width="320" src="http://2.bp.blogspot.com/-_-1ZY6LqfnA/TxqHgshfF8I/AAAAAAAAATQ/wAjBaCdgJ8k/s320/pyplot_example.png" /&gt;&lt;/a&gt;&lt;/div&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7635392334609494121-4407210808299824979?l=bitsfromthomas.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bitsfromthomas.blogspot.com/feeds/4407210808299824979/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7635392334609494121&amp;postID=4407210808299824979' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default/4407210808299824979'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default/4407210808299824979'/><link rel='alternate' type='text/html' href='http://bitsfromthomas.blogspot.com/2012/01/visualizing-matrix-transformations-i.html' title='Visualizing matrix transformations i pyplot'/><author><name>Thomas</name><uri>http://www.blogger.com/profile/13825031472497321919</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><media:thumbnail xmlns:media='http://search.yahoo.com/mrss/' url='http://2.bp.blogspot.com/-_-1ZY6LqfnA/TxqHgshfF8I/AAAAAAAAATQ/wAjBaCdgJ8k/s72-c/pyplot_example.png' height='72' width='72'/><thr:total>0</thr:total><georss:featurename>Göteborg, Sverige</georss:featurename><georss:point>57.70887 11.97456</georss:point><georss:box>57.437440499999994 11.342846 57.9802995 12.606274</georss:box></entry><entry><id>tag:blogger.com,1999:blog-7635392334609494121.post-6600146651554802859</id><published>2010-05-20T08:43:00.003+02:00</published><updated>2010-10-12T07:36:24.151+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='reflection'/><category scheme='http://www.blogger.com/atom/ns#' term='golang'/><title type='text'>Go language reflection examples</title><content type='html'>I have played around a bit with the new &lt;a href="http://golang.org/"&gt;Go&lt;/a&gt; language (go, golang, go-lang). I couldn't find any good examples of how the reflection library worked so I made some brief examples below summarizing my findings.&lt;br /&gt;&lt;h3&gt;Functions with variable number of arguments&lt;br /&gt;&lt;/h3&gt;&lt;br /&gt;First, it is possible to do many things without the &lt;a href="http://golang.org/pkg/reflect"&gt;reflect-package&lt;/a&gt;. For example, the following code shows how to distinguish between different types in a function using a variable number of parameters. The function &lt;b&gt;split_into()&lt;/b&gt; splits a string into fields and put them all into different variables. The type of a variable determines how a field is converted (maybe a beginning of the c-language scanf).&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;// Split line at ',' and put into variables if&lt;br /&gt;// number of fields match number of arguments.&lt;br /&gt;// Handles string, int, and float.&lt;br /&gt;&lt;br /&gt;func split_into(line string, a ...interface{}) bool {&lt;br /&gt;    fa := strings.Split(line, ",", -1)&lt;br /&gt;    if len(fa) == len(a) {&lt;br /&gt;        for i, field := range a {&lt;br /&gt;            switch f := field.(type) {&lt;br /&gt;            case *string:&lt;br /&gt;                *f = fa[i]&lt;br /&gt;            case *int:&lt;br /&gt;                *f, _ = strconv.Atoi(fa[i])&lt;br /&gt;            case *float:&lt;br /&gt;                *f, _ = strconv.Atof(fa[i])&lt;br /&gt;            }&lt;br /&gt;        }&lt;br /&gt;        return true&lt;br /&gt;    }&lt;br /&gt;    return false&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;func testsplit() {&lt;br /&gt;    var a int&lt;br /&gt;    var b string&lt;br /&gt;    var c float&lt;br /&gt;    ok := split_into("12,This is some text,3.14", &amp;amp;a, &amp;amp;b, &amp;amp;c)&lt;br /&gt;    fmt.Println(ok, a, b, c)&lt;br /&gt;}&lt;br /&gt;// testsplit() prints: "true 12 This is some text 3.14"&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The example above shows how to unpack a variable number of arguments to a function and how to use a the special type switch statement. The declaration &lt;b&gt;a ...interface{}&lt;/b&gt; is used instead of the simpler &lt;b&gt;a ...&lt;/b&gt; to get all the arguments as a vector which seems to be more convenient than just "..." which gives a struct (a struct would require the reflect package I believe). The type of variable &lt;b&gt;a&lt;/b&gt; inside the function is &lt;b&gt;[]interface{}&lt;/b&gt;, that is, a vector containing unknown objects.&lt;br /&gt;&lt;br /&gt;&lt;h3&gt;Reflection example&lt;/h3&gt;&lt;br /&gt;For a reflection example, I tried to create a struct and then using reflection to modify its members. Due to a silly mistake my first attempt (below) failed:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;type My_struct struct {&lt;br /&gt;    I int&lt;br /&gt;    S string&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;func reflect_test_1() {&lt;br /&gt;    // First attempt. Didn't work:&lt;br /&gt;    a := My_struct{1, "alpha"}&lt;br /&gt;    aValue := reflect.NewValue(a).(*reflect.StructValue)&lt;br /&gt;    fmt.Println(reflect.Typeof(aValue).String()) // Convenient way to check type&lt;br /&gt;    i := aValue.Field(0).(*reflect.IntValue)&lt;br /&gt;    i.Set(2)&lt;br /&gt;    s := aValue.Field(1).(*reflect.StringValue)&lt;br /&gt;    s.Set("beta")&lt;br /&gt;    fmt.Println("I =", i.Get(), ", S =", s.Get())&lt;br /&gt;    fmt.Println("I =", a.I, ", S =", a.S)&lt;br /&gt;}&lt;br /&gt;// Prints:&lt;br /&gt;// *reflect.StructValue&lt;br /&gt;// I = 2 , S = beta&lt;br /&gt;// I = 1 , S = alpha&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;In the example above, the function &lt;b&gt;reflect.NewValue()&lt;/b&gt; is first called to create a &lt;b&gt;Value&lt;/b&gt; object. The &lt;b&gt;Value&lt;/b&gt; object is used by the reflect package as an interface to the real data. It mainly contains a pointer to the actual data stored in the struct &lt;b&gt;a&lt;/b&gt;. Since the actual data is a struct, &lt;b&gt;NewValue()&lt;/b&gt; automatically creates a &lt;b&gt;StructValue&lt;/b&gt; to refer to the data. The type assertion &lt;b&gt;.(*reflect.StructValue) &lt;/b&gt;makes sure that &lt;b&gt;aValue&lt;/b&gt; is a &lt;b&gt;StructValue&lt;/b&gt;. Using the function &lt;b&gt;Field()&lt;/b&gt; on our &lt;b&gt;StructValue&lt;/b&gt; it is possible to refer to the fields of the struct and then &lt;b&gt;Set()&lt;/b&gt; and &lt;b&gt;Get()&lt;/b&gt; field values.&lt;br /&gt;&lt;br /&gt;But, what was my problem with the example above? The final content in &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; is untouched. Clearly, when the fields are set to different values, the new values end up in a copy of the original struct, not in the original struct. The change below fixes the problem:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;func reflect_test_2() {&lt;br /&gt;    // Second attempt, success!&lt;br /&gt;    a := My_struct{1, "alpha"}&lt;br /&gt;    pValue := reflect.NewValue(&amp;amp;a).(*reflect.PtrValue)&lt;br /&gt;    fmt.Println(reflect.Typeof(pValue).String())&lt;br /&gt;    aValue = reflect.Indirect(pValue).(*reflect.StructValue)&lt;br /&gt;    i := aValue.Field(0).(*reflect.IntValue)&lt;br /&gt;    i.Set(2)&lt;br /&gt;    s := aValue.Field(1).(*reflect.StringValue)&lt;br /&gt;    s.Set("beta")&lt;br /&gt;    fmt.Println("I =", i.Get(), ", S =", s.Get())&lt;br /&gt;    fmt.Println("I =", a.I, ", S =", a.S)&lt;br /&gt;}&lt;br /&gt;// Prints:&lt;br /&gt;// *reflect.PtrValue&lt;br /&gt;// I = 2 , S = beta&lt;br /&gt;// I = 2 , S = beta&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;The mistake in the first example was to pass the array &lt;span style="font-weight: bold;"&gt;a&lt;/span&gt; by-value instead of by-reference. Without the &lt;b&gt;&amp;amp;-sign&lt;/b&gt;, a copy of the struct is created when &lt;b&gt;NewValue()&lt;/b&gt; is called. We must pass a pointer to avoid the copying. However, the reflection gets a bit more complex since we need to traverse the pointer first before we find the struct.&lt;br /&gt;&lt;br /&gt;A final note. If we make the fields in the struct private (non-exported) it will not work. The &lt;b&gt;Set()&lt;/b&gt; operation seems to work only on exported fields:&lt;br /&gt;&lt;br /&gt;&lt;pre style="background-color: #eeeeee; border: 1px dashed rgb(153, 153, 153); color: black; font-family: Andale Mono,Lucida Console,Monaco,fixed,monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;"&gt;&lt;code&gt;type My_struct struct {&lt;br /&gt;    i int&lt;br /&gt;    s string&lt;br /&gt;}&lt;br /&gt;// reflect_test_1() or reflect_test_2() will now cause:&lt;br /&gt;// panic: cannot set value obtained via unexported struct field&lt;br /&gt;&lt;/code&gt;&lt;/pre&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7635392334609494121-6600146651554802859?l=bitsfromthomas.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bitsfromthomas.blogspot.com/feeds/6600146651554802859/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7635392334609494121&amp;postID=6600146651554802859' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default/6600146651554802859'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default/6600146651554802859'/><link rel='alternate' type='text/html' href='http://bitsfromthomas.blogspot.com/2010/05/go-language-reflection-examples.html' title='Go language reflection examples'/><author><name>Thomas</name><uri>http://www.blogger.com/profile/13825031472497321919</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-7635392334609494121.post-6002282359387704114</id><published>2007-09-06T09:11:00.000+02:00</published><updated>2007-09-06T12:08:56.813+02:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='ruby'/><category scheme='http://www.blogger.com/atom/ns#' term='snippets'/><title type='text'>Ruby hexdigest to digest</title><content type='html'>&lt;code&gt;&lt;br /&gt;# Translating back from hexdigest to digest. Useful when you&lt;br /&gt;# have used Digest::SHA1.hexdigest and change your mind.&lt;br /&gt;&lt;br /&gt;def hexdigest_to_digest(h)&lt;br /&gt;  h.unpack('a2'*(h.size/2)).collect {|i| i.hex.chr }.join&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/7635392334609494121-6002282359387704114?l=bitsfromthomas.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://bitsfromthomas.blogspot.com/feeds/6002282359387704114/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=7635392334609494121&amp;postID=6002282359387704114' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default/6002282359387704114'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/7635392334609494121/posts/default/6002282359387704114'/><link rel='alternate' type='text/html' href='http://bitsfromthomas.blogspot.com/2007/09/ruby-code-snippet-translating-back-from.html' title='Ruby hexdigest to digest'/><author><name>Thomas</name><uri>http://www.blogger.com/profile/13825031472497321919</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>0</thr:total></entry></feed>
