\subsubsection{Vectors}
-Any Factor sequence can be used to represent a mathematical vector, not just instances of the \verb|vector| class. The usual mathematical operations are supported.
+Any Factor sequence can be used to represent a mathematical vector, not just instances of the \verb|vector| class. Anywhere a vector is mentioned in this section, keep in mind it is a mathematical term, not a Factor data type.
+
+The usual mathematical operations on vectors are supported.
\wordtable{
\vocabulary{matrices}
Mathematically speaking, this is a map $<,>: {\mathbb{C}}^n \times {\mathbb{C}}^n \rightarrow \mathbb{C}$. It is the complex inner product; that is, $<a,b> =\overline{<b,a>}$, where $\overline{z}$ is the complex conjugate.
+\wordtable{
+\vocabulary{matrices}
+\ordinaryword{norm}{norm~( vec -- n )}
+}
+Computes the norm (``length'') of a vector. The norm of a vector $v$ is defined as $\sqrt{<v,v>}$.
+
+\wordtable{
+\vocabulary{matrices}
+\ordinaryword{normalize}{normalize~( vec -- vec )}
+}
+Outputs a vector with the same direction, but length 1. Defined as follows:
+\begin{verbatim}
+: normalize ( vec -- vec ) [ norm recip ] keep n*v ;
+\end{verbatim}
+
+\wordtable{
+\vocabulary{matrices}
+\ordinaryword{cross}{cross~( v1 v2 -- vec )}
+}
+Computes the cross product $v_1\times v_2$. The following example illustrates the mathematical fact that a cross product of two vectors is always orthogonal to either vector.
+\begin{alltt}
+\textbf{ok} \tto 1 6/7 -8 \ttc \tto 8/5 3 -2 \ttc cross .
+\textbf{\tto 156/7 -54/5 -118/35 \ttc}
+\textbf{ok} \tto 156/7 -54/5 57/35 \ttc \tto 1 6/7 -8 \ttc v. .
+\textbf{0}
+\textbf{ok} \tto 156/7 -54/5 57/35 \ttc \tto 8/5 3 -2 \ttc v. .
+\textbf{0}
+\end{alltt}
+
\subsubsection{\label{matrices}Matrices}
Matrix literal syntax is documented in \ref{syntax:matrices}. In addition to the literal syntax, new matrices may be created from scratch in one of several ways.
\ordinaryword{n*m}{n*m ( n matrix -- matrix )}
}
Multiplies each element of a matrix by a scalar.
-
\begin{alltt}
\textbf{ok} 5 2 <identity-matrix> n*m prettyprint
\textbf{M[ [ 5 0 ]
}
Composes two matrices as linear operators. This is the usual mathematical matrix multiplication, and the first matrix must have the same number of columns as the second matrix has rows.
+\wordtable{
+\vocabulary{matrices}
+\ordinaryword{transpose}{transpose~( matrix -- matrix )}
+}
+Outputs a matrix where each row is a column of the original matrix, and each column is a row of the original matrix.
+\begin{alltt}
+\textbf{ok}
+\textbf{M[ [ 5 0 ]
+ [ 0 5 ] ]M}
+\end{alltt}
+
\subsubsection{Column and row matrices}
There is a natural isomorphism between the vector space $\mathbb{C}^m$, the $m\times 1$ matrices, and the $1 \times m$ matrices. Additionally, a $m\times n$ matrix acts as a linear operator from the vector space $\mathbb{C}^n$ to $\mathbb{C}^m$ in the same way as multiplying the $m\times n$ matrix by a $n \times 1$ matrix. In Factor, these ideas are embodied by a set of words for converting vectors to matrices, and vice-versa.
! : v. ( v v -- x ) 0 swap [ * + ] 2each ;
: v. ( v v -- x ) v** 0 swap [ + ] each ;
-: (cross) ( v1 v2 i1 i2 -- n )
- rot nth >r swap nth r> * ;
+: cross-trace ( v1 v2 i1 i2 -- v1 v2 n )
+ pick nth >r pick nth r> * ;
+
+: cross-minor ( v1 v2 i1 i2 -- n )
+ [ cross-trace -rot ] 2keep swap cross-trace 2nip - ;
: cross ( { x1 y1 z1 } { x2 y2 z2 } -- { z1 z2 z3 } )
#! Cross product of two 3-dimensional vectors.
- [
- 2dup 2 1 (cross) >r 2dup 1 2 (cross) r> - ,
- 2dup 0 2 (cross) >r 2dup 2 0 (cross) r> - ,
- 2dup 1 0 (cross) >r 2dup 0 2 (cross) r> - ,
- 2drop
- ] make-vector ;
+ 3 <vector>
+ [ >r 2dup 1 2 cross-minor 0 r> set-nth ] keep
+ [ >r 2dup 2 0 cross-minor 1 r> set-nth ] keep
+ [ >r 2dup 0 1 cross-minor 2 r> set-nth ] keep
+ 2nip ;
! Matrices
! The major dimension is the number of elements per row.
: transpose ( matrix -- matrix )
dup matrix-cols over matrix-rows [
- pick matrix-get
+ swap pick matrix-get
] make-matrix nip ;
! Sequence of elements in a row of a matrix.