12.07.2015 Views

Matrices en Lisp - Dpto. Ciencias de la Computación e Inteligencia ...

Matrices en Lisp - Dpto. Ciencias de la Computación e Inteligencia ...

Matrices en Lisp - Dpto. Ciencias de la Computación e Inteligencia ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Lógica y Programación Curso 2002–2003Tema : <strong>Matrices</strong> <strong>en</strong> <strong>Lisp</strong>José A. Alonso JiménezFrancisco J. Martín MateosJosé Luis Ruiz Reina<strong>Dpto</strong>. <strong>de</strong> Ci<strong>en</strong>cias <strong>de</strong> <strong>la</strong> Computación e Intelig<strong>en</strong>cia ArtificialUniversidad <strong>de</strong> Sevil<strong>la</strong>LP 2002–2003 CcIa <strong>Matrices</strong> <strong>en</strong> <strong>Lisp</strong> .1


<strong>Matrices</strong>1Creación:(MAKE-ARRAY DIMENSIONES:INITIAL-ELEMENT ELEMENTO)(MAKE-ARRAY DIMENSIONES:INITIAL-CONTENTS EXPRESION)> (make-array ’(2 2))#2A((NIL NIL) (NIL NIL))> (make-array ’(2 1))#2A((NIL) (NIL))> (make-array ’(1 2))#2A((NIL NIL))> (make-array ’(2 2 1))#3A(((NIL) (NIL)) ((NIL) (NIL)))> (make-array ’(2 1 2))#3A(((NIL NIL)) ((NIL NIL)))> (make-array ’(2 2) :initial-elem<strong>en</strong>t 2)#2A((2 2) (2 2))> (make-array ’(3 2):initial-cont<strong>en</strong>ts ’((a b c)(1 2 3)(x y z)))#2A((A B C) (1 2 3) (X Y Z))> (setf *matriz*(make-array ’(2 3):initial-cont<strong>en</strong>ts ’((a b c)(1 2 3))))#2A((A B C) (1 2 3))LP 2002–2003 CcIa <strong>Matrices</strong> <strong>en</strong> <strong>Lisp</strong> .2


<strong>Matrices</strong>1Acceso:(AREF MATRIZ INDICE-1 ... INDICE-N)> *matriz*#2A((A B C) (1 2 3))> (aref *matriz* 0 0)A> (aref *matriz* 1 1)21Modificación:(SETF (AREF MATRIZ INDICE-1 ... INDICE-N) EXPRESION)> *matriz*#2A((A B C) (1 2 3))> (setf (aref *matriz* 1 2) ’h)H> *matriz*#2A((A B C) (1 2 H))1Otras funciones:> (array-dim<strong>en</strong>sions *matriz*)(2 3)> (array-dim<strong>en</strong>sion *matriz* 0)2> (array-dim<strong>en</strong>sion *matriz* 1)3> (array-total-size *matriz*)6> (arrayp *matriz*)TLP 2002–2003 CcIa <strong>Matrices</strong> <strong>en</strong> <strong>Lisp</strong> .3


<strong>Matrices</strong>1Ejemplo:;;; (SUMA-COLUMNAS MATRIZ);;; > (setf mat;;; (make-array ’(3 3);;; :initial-cont<strong>en</strong>ts;;; ’((1 2 3);;; (4 5 6);;; (7 8 9))));;; #2A((1 2 3) (4 5 6) (7 8 9));;; > (suma-columnas mat);;; #(12 15 18)(<strong>de</strong>fun suma-columnas (a)(let* ((dim (array-dim<strong>en</strong>sions a))(f (first dim))(c (second dim))(res (make-array (list c))))(loop for i from 0 to (- c 1)do (setf (aref res i)(loop for j from 0 to (- f 1)summing (aref a j i))))res))LP 2002–2003 CcIa <strong>Matrices</strong> <strong>en</strong> <strong>Lisp</strong> .4

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!