fixing matrices
parent
8e7652e644
commit
135a114787
|
@ -7,6 +7,8 @@ for controlling it:
|
|||
+Yn Size of 2 youngest generations, megabytes
|
||||
+An Size of tenured and semi-spaces, megabytes
|
||||
|
||||
OpenGL binding in contrib/gl/ (Alex Chapman).
|
||||
|
||||
The compiler now does constant folding for certain words with literal
|
||||
operands. The compiler's peephole optimizer has been improved.
|
||||
|
||||
|
|
|
@ -3423,7 +3423,9 @@ The \verb|matrices| vocabulary provides a set of words for simple algebraic oper
|
|||
|
||||
\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}
|
||||
|
@ -3466,6 +3468,35 @@ Computes the inner product of two vectors. They must be of equal length.
|
|||
|
||||
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.
|
||||
|
@ -3513,7 +3544,6 @@ The following are the usual algebraic operations on matrices.
|
|||
\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 ]
|
||||
|
@ -3544,6 +3574,17 @@ Multiplies two matrices element-wise. They must have the same dimensions. This i
|
|||
}
|
||||
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.
|
||||
|
|
|
@ -18,17 +18,19 @@ vectors ;
|
|||
! : 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.
|
||||
|
@ -72,7 +74,7 @@ M: matrix clone ( matrix -- matrix )
|
|||
|
||||
: 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.
|
||||
|
|
|
@ -3,8 +3,5 @@
|
|||
IN: matrices
|
||||
USING: kernel math ;
|
||||
|
||||
: norm ( v -- a )
|
||||
dup v. sqrt ;
|
||||
|
||||
: normalize ( v -- v )
|
||||
[ norm recip ] keep n*v ;
|
||||
: norm ( vec -- n ) dup v. sqrt ;
|
||||
: normalize ( vec -- vec ) [ norm recip ] keep n*v ;
|
||||
|
|
|
@ -98,3 +98,11 @@ USING: kernel lists math matrices namespaces test ;
|
|||
|
||||
m.v
|
||||
] unit-test
|
||||
|
||||
[ { 0 0 1 } ] [ { 1 0 0 } { 0 1 0 } cross ] unit-test
|
||||
[ { 1 0 0 } ] [ { 0 1 0 } { 0 0 1 } cross ] unit-test
|
||||
[ { 0 1 0 } ] [ { 0 0 1 } { 1 0 0 } cross ] unit-test
|
||||
|
||||
[ M[ [ 1 3 5 ] [ 2 4 6 ] ]M ]
|
||||
[ M[ [ 1 2 ] [ 3 4 ] [ 5 6 ] ]M transpose ]
|
||||
unit-test
|
||||
|
|
Loading…
Reference in New Issue