view doc/interpreter/special.texi @ 2342:95e511896bf5

[project @ 1996-07-24 18:05:43 by jwe]
author jwe
date Wed, 24 Jul 1996 18:08:39 +0000
parents b1a56412c385
children 31d5588dbb61
line wrap: on
line source

@c Copyright (C) 1996 John W. Eaton
@c This is part of the Octave manual.
@c For copying conditions, see the file gpl.texi.

@node Special Matrices, Matrix Manipulation, Input and Output, Top
@chapter Special Matrices

Octave provides a number of functions for creating special matrix forms.
In nearly all cases, it is best to use the built-in functions for this
purpose than to try to use other tricks to achieve the same effect.

@menu
* Special Utility Matrices::    
* Famous Matrices::             
@end menu

@node Special Utility Matrices, Famous Matrices, Special Matrices, Special Matrices
@section Special Utility Matrices

@findex eye

The function @code{eye} returns an identity matrix.  If invoked with a
single scalar argument, @code{eye} returns a square matrix with the
dimension specified.  If you supply two scalar arguments, @code{eye}
takes them to be the number of rows and columns.  If given a matrix or
vector argument, @code{eye} returns an identity matrix with the same
dimensions as the given argument.

For example,

@example
eye (3)
@end example

@noindent
creates an identity matrix with three rows and three columns,

@example
eye (5, 8)
@end example

@noindent
creates an identity matrix with five rows and eight columns, and

@example
eye ([13, 21; 34, 55])
@end example

@noindent
creates an identity matrix with two rows and two columns.

Normally, @code{eye} expects any scalar arguments you provide to be real
and non-negative.  The variables @code{ok_to_lose_imaginary_part} and
@code{treat_neg_dim_as_zero} control the behavior of @code{eye} for
complex and negative arguments.  @xref{User Preferences}.  Any
non-integer arguments are rounded to the nearest integer value.

There is an ambiguity when these functions are called with a single
argument.  You may have intended to create a matrix with the same
dimensions as another variable, but ended up with something quite
different, because the variable that you used as an argument was a
scalar instead of a matrix.

For example, if you need to create an identity matrix with the same
dimensions as another variable in your program, it is best to use code
like this

@example
eye (rows (a), columns (a))
@end example

@noindent
instead of just

@example
eye (a)
@end example

@noindent
unless you know that the variable @var{a} will @emph{always} be a matrix.

@findex ones
@findex zeros
@findex rand

The functions @code{ones}, @code{zeros}, and @code{rand} all work like
@code{eye}, except that they fill the resulting matrix with all ones,
all zeros, or a set of random values.

If you need to create a matrix whose values are all the same, you should
use an expression like

@example
val_matrix = val * ones (n, m)
@end example

The @code{rand} function also takes some additional arguments that allow
you to control its behavior.  For example, the function call

@example
rand ("normal")
@end example

@noindent
causes the sequence of numbers to be normally distributed.  You may also
use an argument of @code{"uniform"} to select a uniform distribution.  To
find out what the current distribution is, use an argument of
@code{"dist"}.

Normally, @code{rand} obtains the seed from the system clock, so that
the sequence of random numbers is not the same each time you run Octave.
If you really do need for to reproduce a sequence of numbers exactly,
you can set the seed to a specific value.  For example, the function call

@example
rand ("seed", 13)
@end example

@noindent
sets the seed to the number 13.  To see what the current seed is, use
the argument @code{"seed"}. 

If it is invoked without arguments, @code{rand} returns a
single element of a random sequence.

The @code{rand} function uses Fortran code from RANLIB, a library
of fortran routines for random number generation, compiled by Barry W.
Brown and James Lovato of the Department of Biomathematics at The
University of Texas, M.D. Anderson Cancer Center, Houston, TX 77030.

@findex diag

To create a diagonal matrix with vector @var{v} on diagonal @var{k}, use
the function diag (@var{v}, @var{k}).  The second argument is optional.
If it is positive, the vector is placed on the @var{k}-th
super-diagonal.  If it is negative, it is placed on the @var{-k}-th
sub-diagonal.  The default value of @var{k} is 0, and the vector is
placed on the main diagonal.  For example,

@example
octave:13> diag ([1, 2, 3], 1)
ans =

  0  1  0  0
  0  0  2  0
  0  0  0  3
  0  0  0  0
@end example

@findex linspace
@findex logspace

The functions @code{linspace} and @code{logspace} make it very easy to
create vectors with evenly or logarithmically spaced elements.  For
example,

@example
linspace (@var{base}, @var{limit}, @var{n})
@end example

@noindent
creates a row vector with @var{n} (@var{n} greater than 2) linearly
spaced elements between @var{base} and @var{limit}.  The @var{base} and
@var{limit} are always included in the range.  If @var{base} is greater
than @var{limit}, the elements are stored in decreasing order.  If the
number of points is not specified, a value of 100 is used.

The function @code{logspace} is similar to @code{linspace} except that
the values are logarithmically spaced.

If @var{limit} is equal to
@iftex
@tex
$\pi$,
@end tex
@end iftex
@ifinfo
pi,
@end ifinfo
the points are between
@iftex
@tex
$10^{base}$ and $\pi$,
@end tex
@end iftex
@ifinfo
10^base and pi,
@end ifinfo
@emph{not}
@iftex
@tex
$10^{base}$ and $10^{\pi}$,
@end tex
@end iftex
@ifinfo
10^base and 10^pi,
@end ifinfo
in order to  be compatible with the corresponding @sc{Matlab} function.

The @code{linspace} and @code{logspace} functions always return row
vectors, regardless of the value of @code{prefer_column_vectors}.
@xref{Ranges}.

@node Famous Matrices,  , Special Utility Matrices, Special Matrices
@section Famous Matrices

The following functions return famous matrix forms.

@ftable @code
@item hadamard (@var{k})
Return the Hadamard matrix of order n = 2^k.

@item hankel (@var{c}, @var{r})
Return the Hankel matrix constructed given the first column @var{c}, and
(optionally) the last row @var{r}.  If the last element of @var{c} is
not the same as the first element of @var{r}, the last element of
@var{c} is used.  If the second argument is omitted, the last row is
taken to be the same as the first column.

A Hankel matrix formed from an m-vector @var{c}, and an n-vector
@var{r}, has the elements
@iftex
@tex
$$
H (i, j) = \cases{c_{i+j-1},&$i+j-1\le m$;\cr r_{i+j-m},&otherwise.\cr}
$$
@end tex
@end iftex
@ifinfo

@example
@group
H (i, j) = c (i+j-1),  i+j-1 <= m;
H (i, j) = r (i+j-m),  otherwise
@end group
@end example

@end ifinfo

@item hilb (@var{n})
Return the Hilbert matrix of order @var{n}.  The
@iftex
@tex
$i,\,j$
@end tex
@end iftex
@ifinfo
i, j
@end ifinfo
element of a Hilbert matrix is defined as
@iftex
@tex
$$
H (i, j) = {1 \over (i + j - 1)}
$$
@end tex
@end iftex
@ifinfo

@example
H (i, j) = 1 / (i + j - 1)
@end example
@end ifinfo

@item invhilb (@var{n})
Return the inverse of a Hilbert matrix of order @var{n}.  This is exact.
Compare with the numerical calculation of @code{inverse (hilb (n))},
which suffers from the ill-conditioning of the Hilbert matrix, and the
finite precision of your computer's floating point arithmetic.

@item toeplitz (@var{c}, @var{r})
Return the Toeplitz matrix constructed given the first column @var{c},
and (optionally) the first row @var{r}.  If the first element of @var{c}
is not the same as the first element of @var{r}, the first element of
@var{c} is used.  If the second argument is omitted, the first row is
taken to be the same as the first column.

A square Toeplitz matrix has the form
@iftex
@tex
$$
\left[\matrix{c_0    & r_1     & r_2      & \ldots & r_n\cr
              c_1    & c_0     & r_1      &        & c_{n-1}\cr
              c_2    & c_1     & c_0      &        & c_{n-2}\cr
              \vdots &         &          &        & \vdots\cr
              c_n    & c_{n-1} & c_{n-2} & \ldots & c_0}\right].
$$
@end tex
@end iftex
@ifinfo

@example
@group
c(0)  r(1)   r(2)  ...  r(n)
c(1)  c(0)   r(1)      r(n-1)
c(2)  c(1)   c(0)      r(n-2)
 .                       .
 .                       .
 .                       .

c(n) c(n-1) c(n-2) ...  c(0)
@end group
@end example
@end ifinfo

@item vander (@var{c})
Return the Vandermonde matrix whose next to last column is @var{c}.

A Vandermonde matrix has the form
@iftex
@tex
$$
\left[\matrix{c_0^n  & \ldots & c_0^2  & c_0    & 1\cr
              c_1^n  & \ldots & c_1^2  & c_1    & 1\cr
              \vdots &        & \vdots & \vdots & \vdots\cr
              c_n^n  & \ldots & c_n^2  & c_n    & 1}\right].
$$
@end tex
@end iftex
@ifinfo

@example
@group
c(0)^n ... c(0)^2  c(0)  1
c(1)^n ... c(1)^2  c(1)  1
 .           .      .    .
 .           .      .    .
 .           .      .    .
                 
c(n)^n ... c(n)^2  c(n)  1
@end group
@end example
@end ifinfo
@end ftable