unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Submission of Matrix Kronecker Product for calc
@ 2008-03-02 21:37 Vincent Belaïche
  2008-03-05  3:22 ` Jay Belanger
  0 siblings, 1 reply; 11+ messages in thread
From: Vincent Belaïche @ 2008-03-02 21:37 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 4514 bytes --]


Hello,

I am new to this list (I have just subscribed). I have just written a 
function for CALC that computes the Kronecker product of two matrices.

Maybe this can be useful to somebody else. I am not sure that this is 
the correct forum to submit that. Sorry if I bothered anybody.

BR,
     Vincent.

PS-1 : I think that this is perfectible to the following extent
   a) I use reverse instead of nreverse, which is more memory consuming. 
Not knowing the internals of Emacs I was unsure how the garbage 
collector free the memory if you nreverse a list not passing to nreverse 
the pointer of the first element (ie the point to the list itself). This 
is why I used reverse.

    b) I started from a template created by the "ZP" key for creating a 
permanent user function. The starting point was a simple multiplication. 
Then I modified the function to make a Kronecker product. However I 
don't know how to set the 'calc-user-defn property (what is there is 
still what there was originally after "ZP" keystroke in calc mode.
   

PS-2 : The function works on two dimensional matrices. I had no need to 
make it generic for n-dimensional (and maybe I was not proficient enough 
in Lisp to do it... ;-/ ).

PS-3 : Code below the --- line :
--------------------------------------------------
;;; Definition of Kronecker matrix product
(put 'calc-define 'calc-kron '(progn
 (defun calc-kron nil (interactive) (calc-wrapper (calc-enter-result 2 
"kron" (cons (quote calcFunc-kron) (calc-top-list-n 2)))))
 (put 'calc-kron 'calc-user-defn 't)
 (defun calcFunc-kron (x y)
   "Kronecker product of matrices x and y.
If x and y are not matrices then they are first embedded into matrices 
before computation.
After computation the result may be desembedded from matrix so that the 
Kronecker product
of two scalars is a scalar,
of one scalar and a vector or vice verse is a vector, or
of two vectors is a vector.
"
     ;; (matrix-backet-level x) returns 0 if x is not a matrix,
     ;;                                 1 if x is a vector that is not a 
matrix
     ;;                                 2 if x is a matrix
     (defun matrix-backet-level (x)
       (let ((returned-value 0) (x-sub x))
         (while
            (and (Math-vectorp x-sub) (< returned-value 2))
            (setq x-sub (elt x-sub 1))
            (setq returned-value (1+ returned-value))
         )
         returned-value
       ))
     ;; (matrix-embed x n) return x embeded in n level of brackets
     ;; (matrix-embed x 0) returns x
     ;; (matrix-embed x 1) returns (vec x)
     ;; (matrix-embed x 2) returns (vec (vec x))
     (defun matrix-embed (x n)
       (let ((returned-value x) (n-ctr 0))
         (while
            (< n-ctr n)
            (setq returned-value (list 'vec returned-value))
            (setq n-ctr (1+ n-ctr))
         )
         returned-value
       ))
     ;; evaluate level of bracketting of arguments x and y
     (let
         (
          (bl-x (matrix-backet-level x))
          (bl-y (matrix-backet-level y))
          prod-val
         )
       (let
           (
            (emb-x (matrix-embed x (- 2 bl-x)))
             (emb-y (matrix-embed y (- 2 bl-y)))
          prod-next-row
            )
         (dolist (x-row (reverse (cdr emb-x)) prod-val)
           (dolist (y-row (reverse (cdr emb-y)))
                 (setq prod-next-row nil)
                 (dolist (x-col (reverse (cdr x-row)) prod-next-row)
                   (dolist (y-col (reverse (cdr y-row)))
                     (setq prod-next-row (cons (math-mul x-col y-col) 
prod-next-row))
                     )
                   )
                 (setq prod-next-row (cons 'vec prod-next-row))
                 (setq prod-val (cons prod-next-row prod-val))
             )
           )
         (setq prod-val (cons 'vec prod-val))
         )
         (cond
          ((and (eq bl-x 0) (eq bl-y 0)) (elt (elt prod-val 1) 1))
          ((and (< bl-x 2) (< bl-y 2)) (elt prod-val 1))
          (t prod-val)
          )
       )
     )
 ;; I don't know how to set this property ... this statement does not 
implement the Kronecker product
 (put 'calcFunc-kron 'calc-user-defn '(* (var x var-x) (var y var-y)))
 (define-key calc-mode-map "zk" 'calc-kron)
))






_________________________________________________________________
Nouveau ! Créez votre profil Messenger !
http://home.services.spaces.live.com/

[-- Attachment #2: Type: text/html, Size: 4979 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread
* Re: Submission of Matrix Kronecker Product for calc
@ 2008-03-05 12:43 Vincent Belaïche
  2008-03-05 21:00 ` Jay Belanger
  0 siblings, 1 reply; 11+ messages in thread
From: Vincent Belaïche @ 2008-03-05 12:43 UTC (permalink / raw)
  To: jay.p.belanger; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2376 bytes --]


Dear Jay,

1) No problem if you like to tidy up. Please do as you like.

2) For the legal paper, no problem either. Please send me the form and 
make me know whether I can fill it electronically, or if I need a postal 
real paper send.

3) Concerning whether the result should or should not be a matrix, I am 
not sure that this is 100% useful. My intent was not to add extra 
useless bracketing. However the counterpart of this is that if you use 
the calc-Funckron in Lisp you would may need in the sequel extra testing 
in order to know into what level of bracketing it has resulted. Maybe 
some extra optional argument telling whether to minimize bracketing or 
not would therefore be useful.

However, not being a Lisp expert I did not know how to make this on the 
Lisp function on the one hand, and have it compatible with Stack 
manipulation on the other hand (in case of Stack manipulation some "C-u 
+ digit" before calling function could serve telling whether bracketing 
is to be minimized or not).

In fact, for my own purpose (I wanted to computed some Walsh codes by 
"kron"-ing some 2x2 Hadamard matrices together) and having 
systematically matrices and not vectors was sufficient.

4) note that the function name "kron" has already some publicity as it 
is borrowed from Matlab and Scilab (a license-free equivalent made by 
INRIA and ENPC France). So I suggest to keep the same.

BR,
       Vincent.



Jay Belanger a écrit :
> Vincent Belaïche  writes:
> ...
>   
>> Maybe this can be useful to somebody else.
>>     
>
> Yes.  Would there be a problem if I tidied it up a bit?
> Altogether, it would end up 20 lines or so; to be included in Calc,
> we would need legal papers from you.
>
>   
>> (defun calcFunc-kron (x y)
>> "Kronecker product of matrices x and y.
>>     
>
> If x and y are not explicitly matrices, I take it they will be
> implicitly considered matrices.  
>
>   
>> After computation the result may be desembedded from matrix so that
>> the Kronecker product of two scalars is a scalar,
>>     
> ...
>
> Why should the result not be a matrix?  Is it useful?
>
> Jay
>
>
>   



_________________________________________________________________
Microsoft vous offre un logiciel pour classer, retoucher et partager vos photos !
http://www.windowslive.fr/galerie/

[-- Attachment #2: Type: text/html, Size: 2738 bytes --]

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2008-03-08  6:29 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-03-02 21:37 Submission of Matrix Kronecker Product for calc Vincent Belaïche
2008-03-05  3:22 ` Jay Belanger
  -- strict thread matches above, loose matches on Subject: below --
2008-03-05 12:43 Vincent Belaïche
2008-03-05 21:00 ` Jay Belanger
2008-03-05 22:09   ` David Kastrup
2008-03-06  1:25     ` Jay Belanger
2008-03-06  8:36       ` David Kastrup
2008-03-07  3:36       ` Richard Stallman
2008-03-07  3:38     ` Richard Stallman
2008-03-07 18:56       ` Glenn Morris
2008-03-08  6:29         ` Richard Stallman

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).