<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>BlackBox : Tag linear_algebra</title>
    <link>http://www.warneronstine.com/blog/articles/tag/linear_algebra</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Where technology and art disappear</description>
    <item>
      <title>Math DSL Part Deux - Elementary Row Operations (EROs)</title>
      <description>&lt;p&gt;After sending out my query to the groovy user list I got back some ideas (no offense to Yann, just something I need to tackle on my own, so I didn&amp;#8217;t look at your solution). What I ended up doing was two-fold:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Create a new MetaClass using ExpandoMetaClass &lt;/li&gt;
&lt;li&gt;Define a new class that understood what row and matrix it was going to operate on (so it can have its own operator overloading)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Yes it was a little bit of extra work, but worth it. I also implemented my &lt;code&gt;or&lt;/code&gt; operator overloading so that I can specify augmented matrices in three formats:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A|I&lt;/code&gt; for the identity matrix (only works on square matrices)&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A|0&lt;/code&gt; for the zero column, used for solving the set of linear equations&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A|b&lt;/code&gt; for a vector column, also used for solving the set of linear equations&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As always it helps to learn from others&amp;#8217; mistakes (and oddities) so I&amp;#8217;ll share mine along the road:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;When dealing with EMC (ExpandoMetaClass) watch out for the use of the &lt;code&gt;this&lt;/code&gt; keyword&lt;/li&gt;
&lt;li&gt;Closure delegates don&amp;#8217;t always act the way you would hope when extending &lt;code&gt;GroovyObjectSupport&lt;/code&gt; (at least I couldn&amp;#8217;t get it to work right which is one reason why I switched to EMC)&lt;/li&gt;
&lt;li&gt;Overriding the &lt;code&gt;or&lt;/code&gt; operator works fine until you get to an object that has overridden the &lt;code&gt;equals&lt;/code&gt; operator. In this case Groovy kept trying to find out if my &lt;code&gt;Matrix&lt;/code&gt; was equal to my &lt;code&gt;MatrixColumn&lt;/code&gt;. Once I figured this out I just returned false if the &lt;code&gt;MatrixColumn&lt;/code&gt; was being compared against a &lt;code&gt;Matrix&lt;/code&gt; then it worked as expected.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The next part I want to add is the ability to track the EROs through Elementary Matrices. These are matrices that start out as 1s along the diagonal and they get the same EROs applied to them (this will be useful further down the line for other operations).&lt;/p&gt;</description>
      <pubDate>Mon, 26 Nov 2007 11:59:24 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:57fad73d-f7fd-481b-8474-f518f4e92dd9</guid>
      <author>Warner Onstine</author>
      <link>http://www.warneronstine.com/blog/articles/2007/11/26/math-dsl-part-deux-elementary-row-operations-eros</link>
      <category>programming</category>
      <category>linear_algebra</category>
      <category>math</category>
      <category>matrix</category>
      <category>groovy</category>
      <category>dsl</category>
      <trackback:ping>http://www.warneronstine.com/blog/articles/trackback/306</trackback:ping>
    </item>
    <item>
      <title>Math DSL beginning</title>
      <description>&lt;p&gt;I decided to start working on this taking one of the examples I worked out for the book for math Sets (partially incomplete due to Groovy&amp;#8217;s inability to directly override &lt;code&gt;lessThan&lt;/code&gt;, &lt;code&gt;lessThanEquals&lt;/code&gt;, &lt;code&gt;greaterThan&lt;/code&gt;, &lt;code&gt;greaterThanEquals&lt;/code&gt;) and to put into code some of the techniques I was learning in my Linear Algebra class. &lt;/p&gt;

&lt;p&gt;So far I&amp;#8217;ve implemented the following into my matrix math DSL:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Matrix/Row equality - allows you to test if two matrices or rows are equal (i.e. - contain the same ordered set of numbers)&lt;/li&gt;
&lt;li&gt;Matrix addition/subtraction - You can add two matrices together if they are the same dimensions (2x2, 2x3, whatever, as long as the match in row and columns)&lt;/li&gt;
&lt;li&gt;Matrix/Row scalar multiplication - You can multiply a scalar value against a row or matrix&lt;/li&gt;
&lt;li&gt;Matrix multiplication - You can multiply two matrices together if the first matrix A&amp;#8217;s (say 2x3) columns match B&amp;#8217;s  rows of the matrix you are multiplying against (say 3x2) which will produce a 2x2 matrix. If you did BxA then you would end up with a new matrix of 3x3&lt;/li&gt;
&lt;li&gt;Matrix powers - If the matrix is square (say 3x3) you can multiply it by itself to get the power of the matrix&lt;/li&gt;
&lt;li&gt;Left-shift operation - be able to dynamically create a Row or Matrix by adding new elements to the end&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I still have a lot of work which is laid out on my Trac site for &lt;a href="http://warneronstine.com/groovymath/roadmap"&gt;GroovyMath&lt;/a&gt; which leads me to my next conundrum. &lt;/p&gt;

&lt;p&gt;When dealing with matrices you need to do a lot of Reduced Echelon Form (REF) or Row Reduced Echelon Form (RREF) this consists of taking one of two Elementary Row Operations (EROs) and applying it to a row to get a matrix in the form of a diagonal (all 1s down the upper-left to lower-right diagonal with 0s everywhere else) or getting it into an upper-diagonal form (0s below the diagonal and integers everywhere else). The two EROs that you can consist of a) row interchange, where you can exchange one row with another; and b) row addition, where you can add one row to another multiplying that row by a scalar. What I would like to be able to do is something like this:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def A = new Matrix(//rows go in here)
A.r1 + A.r2*2 //which will add 2 times the second row and add it to the first and replace it
A.r1 % A.r2 //which will exchange rows 1 and 2 with each other
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Another thought I had was to do this in some kind of &amp;#8220;rowOperations&amp;#8221; closure:&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;def A = new Matrix(//rows go in here)
A.rowOperations {
    r1 + r2*2
    r1 % r2
}
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Which definitely reads better than the first (and of course that&amp;#8217;s all part of a DSL how well does it translate the native tongue into code). But the issue that I have with either of these approaches (maybe, still ironing out the second in my head) is that the properties r1 and r2 don&amp;#8217;t exist. These property accessors will need to be intercepted. Ok, not a big deal. But if I choose to implement this in MatrixRow (i.e. - actually return the row itself) it doesn&amp;#8217;t allow me to modify the originating matrix (A in this case) only change the row. For the addition problem I think I can get around it as it is executing in a closure, but the row interchange is an issue. The individual rows don&amp;#8217;t know they&amp;#8217;re a part of a matrix and I&amp;#8217;m not sure that knowing it allows them to do anything.&lt;/p&gt;

&lt;p&gt;Anyways, just wrestling with this concept right now as it is key to moving on to the more advanced functionality in my matrix math. Any suggestions are more than welcome. One new feature that I really like will be overriding the &lt;code&gt;or&lt;/code&gt; operator to provide Augmented Matrices (another key part). Normally you define an augmented matrix in one of three ways:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;A|0&lt;/code&gt; - add a column of all zeros&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A|b&lt;/code&gt; - add a vector as the augmented matrix&lt;/li&gt;
&lt;li&gt;&lt;code&gt;A|I&lt;/code&gt; - add an Identity matrix&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The first two are used to solve the set of linear equations and a bunch of other things. The second is used to find the inverse of the matrix as you transform through EROs the matrix &lt;code&gt;A&lt;/code&gt; so that it equals &lt;code&gt;I&lt;/code&gt; (the identity matrix) and then the side that &amp;#8220;I&amp;#8221; was on now equals the inverse of &lt;code&gt;A&lt;/code&gt;. I would say stop me if I&amp;#8217;m boring you but it&amp;#8217;s probably too late for that. I just wanted to point out how cool it was I can override the &lt;code&gt;or&lt;/code&gt; operator to do something completely different that makes perfect sense in my given domain.&lt;/p&gt;</description>
      <pubDate>Fri, 23 Nov 2007 18:11:12 -0700</pubDate>
      <guid isPermaLink="false">urn:uuid:a393a739-cbd5-423b-8877-07cd92ff46fc</guid>
      <author>Warner Onstine</author>
      <link>http://www.warneronstine.com/blog/articles/2007/11/23/math-dsl-beginning</link>
      <category>programming</category>
      <category>linear_algebra</category>
      <category>math</category>
      <category>matrix</category>
      <category>groovy</category>
      <category>dsl</category>
      <trackback:ping>http://www.warneronstine.com/blog/articles/trackback/305</trackback:ping>
    </item>
  </channel>
</rss>
