1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<title>FreeMat: LEFTDIVIDE Matrix Equation Solver/Divide Operator</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">FreeMat
</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.1.1 -->
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main Page</span></a></li>
<li class="current"><a href="pages.html"><span>Related Pages</span></a></li>
</ul>
</div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('operators_leftdivide.html','');});
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">LEFTDIVIDE Matrix Equation Solver/Divide Operator </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>Section: <a class="el" href="sec_operators.html">Mathematical Operators</a> </p>
<h1><a class="anchor" id="Usage"></a>
Usage</h1>
<p>The divide operator <code></tt> is really a combination of three operators, all of which have the same general syntax: </p>
<pre class="fragment"> Y = A \ B
</pre><p> where <code>A</code> and <code>B</code> are arrays of numerical type. The result <code>Y</code> depends on which of the following three situations applies to the arguments <code>A</code> and <code>B</code>: </p>
<ol>
<li>
<code>A</code> is a scalar, <code>B</code> is an arbitrary <code>n</code>-dimensional numerical array, in which case the output is each element of <code>B</code> divided by the scalar <code>A</code>. </li>
<li>
<code>A,B</code> are matrices with the same number of rows, i.e., <code>A</code> is of size <code>M x K</code>, and <code>B</code> is of size <code>M x L</code>, in which case the output is of size <code>K x L</code>. </li>
</ol>
<p>The output follows the standard type promotion rules, although in the first two cases, if <code>A</code> and <code>B</code> are integers, the output is an integer also, while in the third case if <code>A</code> and <code>B</code> are integers, the output is of type <code>double</code>.</code></p>
<p><code>A few additional words about the third version, in which <code>A</code> and <code>B</code> are matrices. Very loosely speaking, <code>Y</code> is the matrix that satisfies <img class="formulaInl" alt="$A * Y = B$" src="form_109.png"/>. In cases where such a matrix exists. If such a matrix does not exist, then a matrix <code>Y</code> is returned that approximates <img class="formulaInl" alt="$A * Y \approx B$" src="form_110.png"/>. </code></p>
<h1><a class="anchor" id="Function"></a>
Internals</h1>
<p><code>There are three formulae for the times operator. For the first form </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ Y(m_1,\ldots,m_d) = \frac{B(m_1,\ldots,m_d)}{A}. \]" src="form_111.png"/>
</p>
<p> In the second form, the calculation of the output depends on the size of <code>A</code>. Because each column of <code>B</code> is treated independantly, we can rewrite the equation <img class="formulaInl" alt="$A Y = B$" src="form_112.png"/> as </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ A [y_1, y_2,\ldots, y_l] = [b_1, b_2, \ldots, b_l] \]" src="form_113.png"/>
</p>
<p> where <code>y_i</code> are the columns of <code>Y</code>, and <code>b_i</code> are the columns of the matrix <code>B</code>. If <code>A</code> is a square matrix, then the LAPACK routine <code>*gesvx</code> (where the <code>*</code> is replaced with <code>sdcz</code> depending on the type of the arguments) is used, which uses an LU decomposition of <code>A</code> to solve the sequence of equations sequentially. If <code>A</code> is singular, then a warning is emitted.</code></p>
<p><code>On the other hand, if <code>A</code> is rectangular, then the LAPACK routine <code>*gelsy</code> is used. Note that these routines are designed to work with matrices <code>A</code> that are full rank - either full column rank or full row rank. If <code>A</code> fails to satisfy this assumption, a warning is emitted. If <code>A</code> has full column rank (and thus necessarily has more rows than columns), then theoretically, this operator finds the columns <code>y_i</code> that satisfy: </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ y_i = \arg \min_y \| A y - b_i \|_2 \]" src="form_114.png"/>
</p>
<p> and each column is thus the Least Squares solution of <code>A y = b_i</code>. On the other hand, if <code>A</code> has full row rank (and thus necessarily has more columns than rows), then theoretically, this operator finds the columns <code>y_i</code> that satisfy </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ y_i = \arg \min_{A y = b_i} \| y \|_2 \]" src="form_115.png"/>
</p>
<p> and each column is thus the Minimum Norm vector <code>y_i</code> that satisfies <img class="formulaInl" alt="$A y_i = b_i$" src="form_116.png"/>. In the event that the matrix <code>A</code> is neither full row rank nor full column rank, a solution is returned, that is the minimum norm least squares solution. The solution is computed using an orthogonal factorization technique that is documented in the LAPACK User's Guide (see the References section for details). </code></p>
<h1><a class="anchor" id="Examples"></a>
Examples</h1>
<p><code>Here are some simple examples of the divide operator. We start with a simple example of a full rank, square matrix:</code></p>
<p><code></p>
<pre class="fragment">--> A = [1,1;0,1]
A =
1 1
0 1
</pre><p></code></p>
<p><code>Suppose we wish to solve </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ \begin{bmatrix} 1 & 1 \\ 0 & 1 \end{bmatrix} \begin{bmatrix} y_1 \\ y_2 \end{bmatrix} = \begin{bmatrix} 3 \\ 2 \end{bmatrix} \]" src="form_117.png"/>
</p>
<p> (which by inspection has the solution <code>y_1 = 1</code>, <code>y_2 = 2</code>). Thus we compute:</code></p>
<p><code></p>
<pre class="fragment">--> B = [3;2]
B =
3
2
--> Y = A\B
Y =
1
2
</pre><p></code></p>
<p><code>Suppose we wish to solve a trivial Least Squares (LS) problem. We want to find a simple scaling of the vector <code>[1;1]</code> that is closest to the point <code>[2,1]</code>. This is equivalent to solving </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ \begin{bmatrix} 1 \\ 1 \end{bmatrix} y = \begin{bmatrix} 2 \\ 1 \end{bmatrix} \]" src="form_118.png"/>
</p>
<p> in a least squares sense. For fun, we can calculate the solution using calculus by hand. The error we wish to minimize is </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ \varepsilon(y) = (y - 2)^2 + (y-1)^2. \]" src="form_119.png"/>
</p>
<p> Taking a derivative with respect to <code>y</code>, and setting to zero (which we must have for an extrema when <code>y</code> is unconstrained) </p>
<p class="formulaDsp">
<img class="formulaDsp" alt="\[ 2 (y-2) + 2 (y-1) = 0 \]" src="form_120.png"/>
</p>
<p> which we can simplify to <code>4y = 6</code> or <code>y = 3/2</code> (we must, technically, check to make sure this is a minimum, and not a maximum or an inflection point). Here is the same calculation performed using FreeMat:</code></p>
<p><code></p>
<pre class="fragment">--> A = [1;1]
A =
1
1
--> B = [2;1]
B =
2
1
--> A\B
ans =
1.5000
</pre><p></code></p>
<p><code>which is the same solution. </code></p>
</div></div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="index.html">FreeMat Documentation</a></li><li class="navelem"><a class="el" href="sec_operators.html">Mathematical Operators</a></li>
<li class="footer">Generated on Thu Jul 25 2013 17:17:45 for FreeMat by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.1.1 </li>
</ul>
</div>
</body>
</html>
|