]> gitweb.factorcode.org Git - factor.git/commitdiff
fixing matrices
authorSlava Pestov <slava@factorcode.org>
Mon, 23 May 2005 02:08:46 +0000 (02:08 +0000)
committerSlava Pestov <slava@factorcode.org>
Mon, 23 May 2005 02:08:46 +0000 (02:08 +0000)
CHANGES.txt
doc/handbook.tex
library/math/matrices.factor
library/math/more-matrices.factor
library/test/math/matrices.factor

index ad930ae220be1a813fa847eaf571898bbc640aef..461f17d18b05515497fd1bc53113125822434bee 100644 (file)
@@ -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.
 
index cd7c86d7857e327299222b0a42df2121e32a22fc..a3449a055f6e25c9e104241ee7a8fe3ee60b90ad 100644 (file)
@@ -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.
index b50a7f338c3f8a01a86d50c6d0bd60dc94b36b8c..bebef9e469ebc195f5c7a2b90ac6e559627ec48b 100644 (file)
@@ -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.
index cc951ddaaaf166e66a630e4959f770e377946da0..b3343ad93e0821f92b50ec69971f66b198839b94 100644 (file)
@@ -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 ;
index 48619945e591ccf77b828232221678f003f78300..26d98af44481b23f05d1100981a5f203563c6458 100644 (file)
@@ -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