Skip to content

Commit 20a06af

Browse files
committed
add det() and inverse() functions to Eidos
1 parent 80e9b40 commit 20a06af

28 files changed

Lines changed: 2020 additions & 7 deletions

EidosScribe/EidosHelpFunctions.rtf

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4466,7 +4466,20 @@ To combine vectors or matrices by row instead, see
44664466
\f3\fs20 .\
44674467
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
44684468

4469-
\f1\fs18 \cf2 \kerning1\expnd0\expndtw0 (*)diag([*\'a0x\'a0=\'a01], [Ni$\'a0nrow\'a0=\'a0NULL], [Ni$\'a0ncol\'a0=\'a0NULL])\
4469+
\f1\fs18 \cf2 \kerning1\expnd0\expndtw0 (numeric$)det(numeric\'a0x)\
4470+
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
4471+
4472+
\f0\b\fs20 \cf2 Returns the determinant
4473+
\f3\b0 of
4474+
\f1\fs18 x
4475+
\f3\fs20 , which must be a square matrix (otherwise an error is raised). The determinant is a scalar-valued function of the entries of the matrix, and characterizes some properties of the matrix. In particular, the determinant is nonzero if and only if the matrix is invertible. If the determinant is zero, the matrix does not have an inverse and is referred to as \'93singular\'94. In Eidos the determinant is calculated from the
4476+
\f7\i LU
4477+
\f3\i0 decomposition of the matrix. The return type will match the type of
4478+
\f1\fs18 x
4479+
\f3\fs20 .\
4480+
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
4481+
4482+
\f1\fs18 \cf2 (*)diag([*\'a0x\'a0=\'a01], [Ni$\'a0nrow\'a0=\'a0NULL], [Ni$\'a0ncol\'a0=\'a0NULL])\
44704483
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
44714484

44724485
\f0\b\fs20 \cf2 Returns the diagonal
@@ -4566,7 +4579,48 @@ Returns the result of dropping redundant dimensions
45664579
\f3\fs20 is not a matrix or array, it is returned unmodified.\
45674580
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
45684581

4569-
\f1\fs18 \cf2 \kerning1\expnd0\expndtw0 (logical)lowerTri(*\'a0x, [logical$\'a0diag\'a0=\'a0F])\
4582+
\f1\fs18 \cf2 \kerning1\expnd0\expndtw0 (float)inverse(numeric\'a0x)\
4583+
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
4584+
4585+
\f0\b\fs20 \cf2 Returns the (multiplicative) inverse
4586+
\f3\b0 of
4587+
\f1\fs18 x
4588+
\f3\fs20 , which must be a square non-singular matrix (otherwise an error is raised). If matrix
4589+
\f0\b B
4590+
\f3\b0 is the inverse of
4591+
\f7\i n
4592+
\f3\i0 -by-
4593+
\f7\i n
4594+
\f3\i0 matrix
4595+
\f0\b A
4596+
\f3\b0 , then
4597+
\f0\b AB
4598+
\f3\b0 =
4599+
\f0\b BA
4600+
\f3\b0 =
4601+
\f0\b I
4602+
\f7\i\b0\fs13\fsmilli6667 \sub n
4603+
\f3\i0\fs20 \nosupersub , where
4604+
\f0\b I
4605+
\f7\i\b0\fs13\fsmilli6667 \sub n
4606+
\f3\i0\fs20 \nosupersub denotes the
4607+
\f7\i n
4608+
\f3\i0 -by-
4609+
\f7\i n
4610+
\f3\i0 identity matrix and the multiplication used is ordinary matrix multiplication as performed by
4611+
\f1\fs18 matrixMult()
4612+
\f3\fs20 . If
4613+
\f1\fs18 x
4614+
\f3\fs20 might be singular (and thus non-invertible), and you wish to avoid the possibility of an error, you can call
4615+
\f1\fs18 det()
4616+
\f3\fs20 first to find the determinant of the matrix; if the determinant is zero, the matrix is singular and does not have an inverse, and so
4617+
\f1\fs18 inverse()
4618+
\f3\fs20 should not be called. In Eidos the inverse is calculated from the
4619+
\f7\i LU
4620+
\f3\i0 decomposition of the matrix.\
4621+
\pard\pardeftab720\li720\fi-446\ri720\sb180\sa60\partightenfactor0
4622+
4623+
\f1\fs18 \cf2 (logical)lowerTri(*\'a0x, [logical$\'a0diag\'a0=\'a0F])\
45704624
\pard\pardeftab720\li547\ri720\sb60\sa60\partightenfactor0
45714625
45724626
\f0\b\fs20 \cf2 Returns the lower triangle

QtSLiM/help/EidosHelpFunctions.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,13 +360,17 @@
360360
<p class="p2">(*)cbind(...)</p>
361361
<p class="p5"><span class="s5"><b>Combines vectors or matrices by column</b> to produce a single matrix.<span class="Apple-converted-space">  </span>The parameters must be vectors (which are interpreted by </span><span class="s6">cbind()</span><span class="s5"> as if they were one-column matrices) or matrices.<span class="Apple-converted-space">  </span>They must be of the same type, of the same class if they are of type </span><span class="s6">object</span><span class="s5">, and have the same number of rows.<span class="Apple-converted-space">  </span>If these conditions are met, the result is a single matrix with the parameters joined together, left to right.<span class="Apple-converted-space">  </span>Parameters may instead be </span><span class="s6">NULL</span><span class="s5">, in which case they are ignored; or if all parameters are </span><span class="s6">NULL</span><span class="s5">, the result is </span><span class="s6">NULL</span><span class="s5">.<span class="Apple-converted-space">  </span>A sequence of vectors, matrices, and </span><span class="s6">NULL</span><span class="s5">s may thus be concatenated with the </span><span class="s6">NULL</span><span class="s5"> values removed, analogous to </span><span class="s6">c()</span><span class="s5">.<span class="Apple-converted-space">  </span>Calling </span><span class="s6">cbind(x)</span><span class="s5"> is an easy way to create a one-column matrix from a vector.</span></p>
362362
<p class="p5"><span class="s5">To combine vectors or matrices by row instead, see </span><span class="s6">rbind()</span><span class="s5">.</span></p>
363+
<p class="p4">(numeric$)det(numeric x)</p>
364+
<p class="p5"><b>Returns the determinant</b> of <span class="s2">x</span>, which must be a square matrix (otherwise an error is raised).<span class="Apple-converted-space">  </span>The determinant is a scalar-valued function of the entries of the matrix, and characterizes some properties of the matrix.<span class="Apple-converted-space">  </span>In particular, the determinant is nonzero if and only if the matrix is invertible.<span class="Apple-converted-space">  </span>If the determinant is zero, the matrix does not have an inverse and is referred to as “singular”.<span class="Apple-converted-space">  </span>In Eidos the determinant is calculated from the <i>LU</i> decomposition of the matrix.<span class="Apple-converted-space">  </span>The return type will match the type of <span class="s2">x</span>.</p>
363365
<p class="p4">(*)diag([* x = 1], [Ni$ nrow = NULL], [Ni$ ncol = NULL])</p>
364366
<p class="p5"><b>Returns the diagonal</b> of <span class="s2">x</span>.<span class="Apple-converted-space">  </span>This function has four distinct usage patterns (matching R).<span class="Apple-converted-space">  </span>First, if <span class="s2">x</span> is a matrix of any type, it returns the diagonal elements of <span class="s2">x</span> as a vector; in this case, <span class="s2">nrow</span> and <span class="s2">ncol</span> must be <span class="s2">NULL</span>.<span class="Apple-converted-space">  </span>Second, if <span class="s2">x</span> is <span class="s2">1</span> (the default) and <span class="s2">nrow</span> is non-<span class="s2">NULL</span>, it returns an identity matrix with the requested number of rows (and, if <span class="s2">ncol</span> is also non-<span class="s2">NULL</span>, the requested number of columns, otherwise the matrix will be square).<span class="Apple-converted-space">  </span>Third, if <span class="s2">x</span> is a singleton <span class="s2">integer</span> value and <span class="s2">nrow</span> and <span class="s2">ncol</span> are <span class="s2">NULL</span>, it returns a square identity matrix of size <span class="s2">x</span>.<span class="Apple-converted-space">  </span>Fourth, if x is a <span class="s2">logical</span>, <span class="s2">integer</span>, or <span class="s2">float</span> vector of length at least <span class="s2">2</span>, it returns a matrix that uses the values of <span class="s2">x</span> as its diagonal (without recycling or truncation, unlike R) and has <span class="s2">F</span>, <span class="s2">0</span>, or <span class="s2">0.0</span> off-diagonal entries as appropriate.</p>
365367
<p class="p5">Note that using <span class="s2">diag(x)</span>, without <span class="s2">nrow</span> or <span class="s2">ncol</span>, can have unexpected effects if <span class="s2">x</span> is a vector that could be of length one.<span class="Apple-converted-space">  </span>Use <span class="s2">diag(x, nrow=length(x))</span> for consistent behavior.</p>
366368
<p class="p2">(integer)dim(* x)</p>
367369
<p class="p5"><span class="s5"><b>Returns the dimensions</b> of matrix or array </span><span class="s6">x</span><span class="s5">.<span class="Apple-converted-space">  </span>The first dimension value is the number of rows, the second is the number of columns, and further values indicate the sizes of higher-order dimensions, identically to how dimensions are supplied to </span><span class="s6">array()</span><span class="s5">.<span class="Apple-converted-space">  </span></span><span class="s6">NULL</span><span class="s5"> is returned if </span><span class="s6">x</span><span class="s5"> is not a matrix or array.</span></p>
368370
<p class="p2">(*)drop(* x)</p>
369371
<p class="p5"><span class="s5"><b>Returns the result of dropping redundant dimensions</b> from matrix or array </span><span class="s6">x</span><span class="s5">.<span class="Apple-converted-space">  </span>Redundant dimensions are those with a size of exactly 1.<span class="Apple-converted-space">  </span>Non-redundant dimensions are retained.<span class="Apple-converted-space">  </span>If only one non-redundant dimension is present, the result is a vector; if more than one non-redundant dimension is present, the result will be a matrix or array.<span class="Apple-converted-space">  </span>If </span><span class="s6">x</span><span class="s5"> is not a matrix or array, it is returned unmodified.</span></p>
372+
<p class="p4">(float)inverse(numeric x)</p>
373+
<p class="p5"><b>Returns the (multiplicative) inverse</b> of <span class="s2">x</span>, which must be a square non-singular matrix (otherwise an error is raised).<span class="Apple-converted-space">  </span>If matrix <b>B</b> is the inverse of <i>n</i>-by-<i>n</i> matrix <b>A</b>, then <b>AB</b> = <b>BA</b> = <b>I</b><span class="s13"><i><sub>n</sub></i></span>, where <b>I</b><span class="s13"><i><sub>n</sub></i></span> denotes the <i>n</i>-by-<i>n</i> identity matrix and the multiplication used is ordinary matrix multiplication as performed by <span class="s2">matrixMult()</span>.<span class="Apple-converted-space">  </span>If <span class="s2">x</span> might be singular (and thus non-invertible), and you wish to avoid the possibility of an error, you can call <span class="s2">det()</span> first to find the determinant of the matrix; if the determinant is zero, the matrix is singular and does not have an inverse, and so <span class="s2">inverse()</span> should not be called.<span class="Apple-converted-space">  </span>In Eidos the inverse is calculated from the <i>LU</i> decomposition of the matrix.</p>
370374
<p class="p4">(logical)lowerTri(* x, [logical$ diag = F])</p>
371375
<p class="p5"><b>Returns the lower triangle</b> of <span class="s2">x</span>, which must be a matrix.<span class="Apple-converted-space">  </span>The return value will be a <span class="s2">logical</span> matrix of the same dimensions as <span class="s2">x</span>, with elements <span class="s2">T</span> in the lower triangle, <span class="s2">F</span> elsewhere.<span class="Apple-converted-space">  </span>If <span class="s2">diag</span> is <span class="s2">F</span> (the default), the diagonal is not included in the lower triangle; if <span class="s2">diag</span> is <span class="s2">T</span>, the diagonal is included in the lower triangle (i.e., its elements will be <span class="s2">T</span>).</p>
372376
<p class="p2">(*)matrix(* data, [Ni$ nrow = NULL], [Ni$ ncol = NULL], [logical$ byrow = F])</p>

0 commit comments

Comments
 (0)