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

<channel>
	<title>Matrix Math &#8211; Xojo Programming Blog</title>
	<atom:link href="https://blog.xojo.com/tag/matrix-math/feed/" rel="self" type="application/rss+xml" />
	<link>https://blog.xojo.com</link>
	<description>Blog about the Xojo programming language and IDE</description>
	<lastBuildDate>Thu, 23 Mar 2023 19:53:37 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>
	<item>
		<title>Matrix Math</title>
		<link>https://blog.xojo.com/2023/03/23/matrix-math/</link>
		
		<dc:creator><![CDATA[Paul Lefebvre]]></dc:creator>
		<pubDate>Thu, 23 Mar 2023 19:53:35 +0000</pubDate>
				<category><![CDATA[Fun]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[Development]]></category>
		<category><![CDATA[Einhugur]]></category>
		<category><![CDATA[Math]]></category>
		<category><![CDATA[Matrix Math]]></category>
		<category><![CDATA[Software Development]]></category>
		<category><![CDATA[Xojo Programming Language]]></category>
		<guid isPermaLink="false">https://blog.xojo.com/?p=11227</guid>

					<description><![CDATA[A feature in some other languages you may have seen is something called matrix math. These are operations you can perform on matrices, which are 2-dimensional arrays. Xojo does not have any matrix math functions built in, but using the Extends command you can add your own.]]></description>
										<content:encoded><![CDATA[
<p>A feature in some other languages you may have seen is something called matrix math. These are operations you can perform on matrices, which are 2-dimensional arrays. Xojo does not have any matrix math functions built in, but using the <a href="https://documentation.xojo.com/api/language/extends.html">Extends</a> command you can add your own.</p>



<p>I&#8217;ll show you how to add two of the simpler ones: add and multiply.</p>



<p>In case your <a href="https://en.wikipedia.org/wiki/Linear_algebra">linear algebra</a> is a bit rusty, let me recap a little. A matrix is a 2-dimensional array. Adding one matrix to another involves adding the values in the corresponding cells of each matrix (which both must be the same size) together and putting that sum into a new matrix at the same position.</p>



<figure class="wp-block-image size-large"><img fetchpriority="high" decoding="async" width="1024" height="576" src="https://blog.xojo.com/wp-content/uploads/2023/02/image-1024x576.png" alt="" class="wp-image-11230" srcset="https://blog.xojo.com/wp-content/uploads/2023/02/image-1024x576.png 1024w, https://blog.xojo.com/wp-content/uploads/2023/02/image-300x169.png 300w, https://blog.xojo.com/wp-content/uploads/2023/02/image-768x432.png 768w, https://blog.xojo.com/wp-content/uploads/2023/02/image-1536x864.png 1536w, https://blog.xojo.com/wp-content/uploads/2023/02/image-2048x1152.png 2048w" sizes="(max-width: 1024px) 100vw, 1024px" /><figcaption class="wp-element-caption">A different kind of Matrix</figcaption></figure>



<p>For example, consider these two matrices (taken from the <a href="https://en.wikipedia.org/wiki/Matrix_addition">Wikipedia page on Matrix Addition</a>):</p>



<pre class="wp-block-preformatted">1 3
1 0
1 2

0 0
7 5
2 1</pre>



<p>To add them</p>



<ul class="wp-block-list">
<li>1st row: 1 + 0, 3 + 0</li>



<li>2nd row: 1 + 7, 0 + 5</li>



<li>3rd row: 1 + 2, 2 + 1</li>
</ul>



<p>This results in a new matrix:</p>



<pre class="wp-block-preformatted">1 3
8 5
3 3</pre>



<p>&nbsp;As you can see, addition is pretty straightforward. So how might this look in Xojo? Here is an extension method for Integer arrays that I created to do this:</p>



<pre class="wp-block-code"><code>Public Function Add(Extends m1(, ) As Integer, m2(, ) As Integer) As Integer(,)
&nbsp; // https://en.wikipedia.org/wiki/Matrix_addition
&nbsp; // Both arrays must have the same number of rows and columns.

&nbsp; If m1.LastIndex(1) &lt;&gt; m2.LastIndex(1) Or m1.LastIndex(2) &lt;&gt; m2.LastIndex(2) Then
    Raise New UnsupportedOperationException("Array sizes must match.")
  End If

&nbsp; // The values at each cell are added together and a new
  // array is created with their sums.
  Var newMatrix(-1, -1) As Integer

&nbsp; newMatrix.ResizeTo(m1.LastIndex(1), m1.LastIndex(2))

&nbsp; For c1 As Integer = 0 To m1.LastIndex(1)
    For c2 As Integer = 0 To m1.LastIndex(2)
    &nbsp; newMatrix(c1, c2) = m1(c1, c2) + m2(c1, c2)
    Next
&nbsp; Next

&nbsp; Return newMatrix

End Function</code></pre>



<p>To use it, you populate two matrices and call matrix1.add(matrix2). Here is some sample code to solve the example above:</p>



<pre class="wp-block-code"><code>// Matrix Addition

Var matrix1(2, 1) As Integer
matrix1(0, 0) = 1
matrix1(0, 1) = 3
matrix1(1, 0) = 1
matrix1(1, 1) = 0
matrix1(2, 0) = 1
matrix1(2, 1) = 2

Var matrix2(2, 1) As Integer
matrix2(0, 0) = 0
matrix2(0, 1) = 0
matrix2(1, 0) = 7
matrix2(1, 1) = 5
matrix2(2, 0) = 2
matrix2(2, 1) = 1

Var matrix3(-1, -1) As Integer
matrix3 = matrix1.Add(matrix2)</code></pre>



<p>Multiplication gets more complex and is a bit tricky to explain. First, the sizes matter. The 2nd dimension of the 1st matrix must be the same as the 1st dimension of the 2nd matrix. That means that if you have matrix1(3,2) then matrix2(2,2) is valid, but matrix2(1,2) is not. The resulting matrix is matrix1&#8217;s 1st dimension by matrix2&#8217;s 2nd dimension.</p>



<p>To do the multiplication, for each cell in the new matrix you multiple each cell in the corresponding row of matrix1 with each cell of the corresponding column in matrix2.</p>



<p>A Xojo method to do that looks like this:</p>



<pre class="wp-block-code"><code>Public Function Multiply(Extends m1(, ) As Integer, m2(, ) As Integer) As Integer(,)

&nbsp; // https://en.wikipedia.org/wiki/Matrix_multiplication
&nbsp; // matrix1's 2nd dimension size must be the same as
&nbsp; // matrix2's 1st dimension size.

&nbsp; If m1.LastIndex(2) &lt;&gt; m2.LastIndex(1) Then
    Raise New UnsupportedOperationException("m1 2nd dimension must match m2 1st dimension.")
&nbsp; End If

&nbsp; // The result matrix is matrix1's 1st dimension by matrix2's 2nd dimension

&nbsp; Var newMatrix(-1, -1) As Integer

&nbsp; newMatrix.ResizeTo(m1.LastIndex(1), m2.LastIndex(2))

&nbsp; For c1 As Integer = 0 To newMatrix.LastIndex(1)
    For c2 As Integer = 0 To newMatrix.LastIndex(2)
&nbsp;     Var prod As Integer
&nbsp;     For p1 As Integer = 0 To m1.LastIndex(2)
        prod = prod + m1(c1, p1) * m2(p1, c2)
&nbsp;     Next
&nbsp;     newMatrix(c1, c2) = prod
    Next
&nbsp; Next

&nbsp; Return newMatrix

End Function</code></pre>



<p>The <a href="https://en.wikipedia.org/wiki/Matrix_multiplication">Wikipedia example for matrix multiplication</a> has these two matrices:</p>



<pre class="wp-block-preformatted">1 0 1
2 1 1
0 1 1
1 1 2

1 2 1
2 3 1
4 2 2</pre>



<p>Multiplying them together results in this matrix:</p>



<pre class="wp-block-preformatted"> 5 4 3
 8 9 5
 6 5 3
11 9 6</pre>



<p>This code sets up the above example and multiplies it using the method:</p>



<pre class="wp-block-code"><code>// Matrix Multiplication

Var matrix1(3, 2) As Integer
matrix1(0, 0) = 1
matrix1(0, 1) = 0
matrix1(0, 2) = 1
matrix1(1, 0) = 2
matrix1(1, 1) = 1
matrix1(1, 2) = 1
matrix1(2, 0) = 0
matrix1(2, 1) = 1
matrix1(2, 2) = 1
matrix1(3, 0) = 1
matrix1(3, 1) = 1
matrix1(3, 2) = 2

Var matrix2(2, 2) As Integer
matrix2(0, 0) = 1
matrix2(0, 1) = 2
matrix2(0, 2) = 1
matrix2(1, 0) = 2
matrix2(1, 1) = 3
matrix2(1, 2) = 1
matrix2(2, 0) = 4
matrix2(2, 1) = 2
matrix2(2, 2) = 2

Var matrix3(-1, -1) As Integer
matrix3 = matrix1.Multiply(matrix2)</code></pre>



<p>I will point out that the two methods used here are very simple algorithms that won&#8217;t be nearly fast enough when working with large matrices. But hopefully they showed you that <a href="https://documentation.xojo.com/getting_started/using_the_xojo_language/modules.html#getting-started-using-the-xojo-language-modules-extension-methods">Extension Methods</a> are a great way to extend the Xojo language for your specific needs and you found this example interesting, even if you have no use for linear algebra or matrix math.</p>



<p>Download the project: <a href="https://files.xojo.com/BlogExamples/MatrixMath.xojo_binary_project">MatrixMath</a></p>



<p>As an alternative to using Extends, you could create your own Matrix class and then use <a href="https://documentation.xojo.com/api/math/operator_overloading.html#operator-overloading">Operator overloading</a> (a subject for another blog post) so that you could write code like this:</p>



<pre class="wp-block-code"><code>Var m1 As New Matrix(1, 3, 1, 0, 1 2)
Var m2 As New Matrix(0, 0, 7, 5, 2, 1)
Var m3 As Matrix = m1 + m2</code></pre>



<p>If you actually need full matrix support in Xojo, check out the <a href="http://delaneyrm.com/">free matrix-related Delaney</a> or <a href="https://einhugur.com/Html/opensource.html">Einhugur plugins</a>.</p>



<p><em>Paul learned to program in BASIC at age 13 and has programmed in more languages than he remembers, with Xojo being an obvious favorite. When not working on Xojo, you can find him talking about retrocomputing at <a href="https://goto10.substack.com" target="_blank" rel="noreferrer noopener">Goto 10</a> and </em>on Mastodon @lefebvre@hachyderm.io.</p>
]]></content:encoded>
					
		
		
			</item>
	</channel>
</rss>
