Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.postscript > #924

Matrix Multiplication and Transpose; Dot Product and Cross Product

From Luser droog <mijoryx@yahoo.com>
Newsgroups comp.lang.postscript
Subject Matrix Multiplication and Transpose; Dot Product and Cross Product
Date 2012-09-04 09:43 -0500
Organization Netfront http://www.netfront.net/
Message-ID <k2543a$28p9$1@adenine.netfront.net> (permalink)
References <k20b63$r66$1@adenine.netfront.net> <dgmeqh2zvrg6$.1t7xv64a7obxx.dlg@40tude.net> <k22g9l$254v$1@adenine.netfront.net> <pwk49b4rilv4.808xh6sxozf.dlg@40tude.net> <k23tjg$qdl$1@adenine.netfront.net>

Show all headers | View raw


Haven't generalized the cross-product yet. But here's matrices.
I tried to make it a little forgiving by auto-promoting vectors
to 1D matrices and auto-transposing to find conformance.
Not sure how useful this will actually be. To actually use this
in a program, I think you pretty much have to *know* what 
dimensions are going in and out. 

571(1)09:37 AM:ps 0> cat mat.ps
%!
%mat.ps
%Matrix and Vector math routines

/.error where { pop /signalerror { .error } def } if

/dot { % u v
    2 copy length exch length ne {
        /dot cvx /undefinedresult signalerror
    } if
    % u v
    0 % u v sum
    0 1 3 index length 1 sub { % u v sum i
        3 index 1 index get exch % u v sum u_i i
        3 index exch get % u v sum u_i v_i
        mul add % u v sum
    } for % u v sum

    3 1 roll pop pop % sum
} bind def

% [ x1 x2 x3 ] [ y1 y2 y3 ]  cross  [ x2*y3-y2*x3 x3*y1-x1*y3 x1*y2-x2*y1 ]
/cross { % u v
    dup length 3 ne
    2 index length 3 ne or {
        /cross cvx /undefinedresult signalerror
    } if
    % u v
    exch aload pop 4 3 roll aload pop % x1 x2 x3 y1 y2 y3
    [
        5 index 2 index mul % ... [ x2*y3
        3 index 6 index mul sub % ... [ x2*y3-y2*x3
        5 index 5 index mul % ... [ x2*y3-y2*x3 x3*y1
        8 index 4 index mul sub % ... [ x2*y3-y2*x3 x3*y1-x1*y3
        8 index 5 index mul % ... [ x2*y3-y2*x3 x3*y1-x1*y3 x1*y2
        8 index 7 index mul sub % ... [ x2*y3-y2*x3 x3*y1-x1*y3 x1*y2-x2*y1
    ]
    7 1 roll 6 { pop } repeat
} bind def

/transpose { STATICDICT begin
    /A exch def
    /M A length def
    /N A 0 get length def
    [
    0 1 N 1 sub { /n exch def
        [
        0 1 M 1 sub { /m exch def
            A m get n get
        } for
        ]
    } for
    ]
end } dup 0 6 dict put def

/matmul { STATICDICT begin
    /B exch def
    B 0 get type /arraytype ne { /B [B] def } if
    /A exch def
    A 0 get type /arraytype ne { /A [A] def } if
    /Q B length def
    /R B 0 get length def
    /P A length def
    Q A 0 get length ne {
        /A A transpose def
        /P A length def
        Q A 0 get length ne {
            A B end /matmul cvx /undefinedresult signalerror
        } if
    } if

    [
    0 1 R 1 sub { /r exch def
        [
        0 1 P 1 sub { /p exch def
            0
            0 1 Q 1 sub { /q exch def
                A p get q get
                B q get r get mul
                add
            } for
        } for
        ]
    } for
    ]

end } dup 0 10 dict put def
% Here's a trick. Hashtables (ie. dictionaries) are fastest
% when less than half-full.


--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---

Back to comp.lang.postscript | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Dot Product and Cross Product Luser droog <mijoryx@yahoo.com> - 2012-09-02 14:13 -0500
  Re: Dot Product and Cross Product tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2012-09-02 23:51 -0400
    Re: Dot Product and Cross Product Luser droog <mijoryx@yahoo.com> - 2012-09-03 09:53 -0500
      Re: Dot Product and Cross Product tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2012-09-03 19:16 -0400
        Re: Dot Product and Cross Product Luser droog <mijoryx@yahoo.com> - 2012-09-03 22:46 -0500
          Matrix Multiplication and Transpose; Dot Product and Cross Product Luser droog <mijoryx@yahoo.com> - 2012-09-04 09:43 -0500
            Re: Matrix Multiplication and Transpose; Dot Product and Cross Product luser- -droog <mijoryx@yahoo.com> - 2012-09-11 12:09 -0700
          Re: Dot Product and Cross Product tlvp <mPiOsUcB.EtLlLvEp@att.net> - 2012-09-04 23:13 -0400
  Re: Dot Product and Cross Product gernot.hoffmann@hs-emden-leer.de - 2012-09-05 23:41 -0700

csiph-web