quaternion fix; add v>q and q>v words

cvs
Slava Pestov 2005-10-05 04:18:55 +00:00
parent 6de799c230
commit 1fddaf44e7
4 changed files with 63 additions and 14 deletions

View File

@ -20,6 +20,17 @@
</li>
<li>User interface:
<ul>
<li>Added expandable outliners. Used by the inspector, <code>.s</code>, <code>usage.</code>, <code>uses.</code>, <code>vocabs.</code>, and various other words.</li>
<li>Added word completion to the listener pane; press <code>TAB</code>.</li>
<li>Added word navigation shortcuts to the listener pane; press <code>C+LEFT</code> and <code>C+RIGHT</code> to move a word at a time, and <code>C+BACKSPACE</code> and <code>C+DELETE</code> to delete the previous and next word, respectively.</li>
</ul>
</li>
<li>Collections:
<ul>
@ -33,6 +44,29 @@
<li>Everything else:
<ul>
<li>Quaternions added to math library.
Quaternions are represented as pairs of real numbers. Literal syntax is <code>Q{ a b c d }Q</code>, where <code>a</code>, <code>b</code>, <code>c</code> and <code>d</code> are real numbers.
Many quaternion operations can be done using Factor's existing vector algebra words; this follows from the mathematical fact that the quaternions are a 2-dimensional vector space over the complex numbers:
<ul>
<li>Addition: <code>v+</code></li>
<li>Subtraction: <code>v-</code></li>
<li>Negation: <code>vneg</code></li>
<li>Norm: <code>norm</code></li>
<li>Multiplication by a complex number on the left: <code>n*v</code></li>
</ul>
A few new words are also needed:
<ul>
<li>Convert complex number to quaternion: <code>c&gt;n</code></li>
<li>Multiply by a complex number on the right: <code>q*n</code></li>
<li>Quaternion multiplication: <code>q*</code></li>
<li>Quaternion division: <code>q/</code></li>
<li>Quaternion conjugate: <code>qconjugate</code></li>
</ul>
</li>
<li>Easier exception handling. The <code>cleanup ( try cleanup -- )</code> word encapsulates the following idiom:
<pre>
[ A ] [ B rethrow ] catch
@ -55,7 +89,7 @@ However, most uses of <code>catch</code> can be replaced by <code>cleanup</code>
<li>The distinct <code>t</code> type is gone. Now, the <code>t</code> object is just a symbol.</li>
<li>A new <code>with-server ( port ident quot -- )</code> combinator takes care of listening on a network socket, logging client connections, spawning client handler threads, and error handling. The quotation is called for each client, in a new scope with the client socket as the default stream.</li>
<li>New <code>1+</code>, <code>1-</code> words are a bit faster than <code>1 +</code> and <code>1 -</code> since there is one less generic dispatch involved.</li>
<li>On Windows, FFI errors would raise a signal 11 instead of reporting a nice message</li>
<li>On Windows, FFI errors would raise a signal 11 instead of reporting a nice message.</li>
</ul>
<li>Contributed code:

View File

@ -66,7 +66,7 @@ M: ping handle-irc ( line -- )
" " % dup definer word-name %
" " % dup word-name %
"stack-effect" word-prop [ " (" % % ")" % ] when*
] make-string ;
] "" make ;
: word-url ( word -- url )
[
@ -74,7 +74,7 @@ M: ping handle-irc ( line -- )
dup word-vocabulary url-encode %
"&word=" %
word-name url-encode %
] make-string ;
] "" make ;
: irc-loop ( -- )
irc-stream get stream-readln

View File

@ -13,14 +13,14 @@ SYMBOL: states
SYMBOL: halt
! This is a simple program that outputs 5 1's
[
{{
[[ [[ 1 0 ]] << state f 1 1 2 >> ]]
[[ [[ 2 0 ]] << state f 1 1 3 >> ]]
[[ [[ 3 0 ]] << state f 1 -1 1 >> ]]
[[ [[ 1 1 ]] << state f 1 -1 2 >> ]]
[[ [[ 2 1 ]] << state f 1 -1 3 >> ]]
[[ [[ 3 1 ]] << state f 1 -1 halt >> ]]
] states set
}} states set
! Current state
SYMBOL: state
@ -38,7 +38,7 @@ SYMBOL: position
SYMBOL: tape
! Initial tape
20 zero-vector tape set
20 0 <repeated> >vector tape set
: sym ( -- sym )
#! Symbol at head position.
@ -50,7 +50,7 @@ SYMBOL: tape
: next-state ( -- state )
#! Look up the next state/symbol/direction triplet.
state get sym cons states get assoc ;
state get sym cons states get hash ;
: turing-step ( -- )
#! Do one step of the turing machine.

View File

@ -7,11 +7,11 @@
USING: arrays kernel math sequences ;
IN: math-internals
: 2q [ first2 ] 2apply ;
: 2q [ first2 ] 2apply ; inline
: q*a 2q swapd ** >r * r> - ;
: q*a 2q swapd ** >r * r> - ; inline
: q*b 2q >r ** swap r> * + ;
: q*b 2q >r ** swap r> * + ; inline
IN: math
@ -19,14 +19,20 @@ IN: math
dup length 2 = [
first2 [ number? ] 2apply and
] [
2drop f
drop f
] if ;
: q* ( u v -- u*v ) [ q*a ] 2keep q*b 2array ;
: q* ( u v -- u*v )
#! Multiply quaternions.
[ q*a ] 2keep q*b 2array ;
: qconjugate ( u -- u' ) first2 neg >r conjugate r> 2array ;
: qconjugate ( u -- u' )
#! Quaternion conjugate.
first2 neg >r conjugate r> 2array ;
: q/ ( u v -- u/v ) [ qconjugate q* ] keep norm-sq v/n ;
: q/ ( u v -- u/v )
#! Divide quaternions.
[ qconjugate q* ] keep norm-sq v/n ;
: q*n ( q n -- q )
#! Note: you will get the wrong result if you try to
@ -39,6 +45,15 @@ IN: math
#! Turn a complex number into a quaternion.
0 2array ;
: v>q ( v -- q )
#! Turn a 3-vector into a quaternion with real part 0.
first3 rect> >r 0 swap rect> r> 2array ;
: q>v ( q -- v )
#! Get the vector part of a quaternion, discarding the real
#! part.
first2 >r imaginary r> >rect 3array ;
! Zero
: q0 Q{ 0 0 0 0 }Q ;