需要如何在Lisp中打印矩阵的建议 [英] Need advice on how to print a matrix in lisp

查看:109
本文介绍了需要如何在Lisp中打印矩阵的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我定义了一个矩阵,所以如果我这样做

I have a matrix defined so if I do this

(format t "~a" (get-real-2d 0 0))

它会打印出第一行第一列中的元素

it prints out the element in the first row first column

如果我这样做

(format t "~a" (get-real-2d a 0 1))

它打印出第一行第二列中的元素

it prints out the element in first row second column

如果我这样做

(format t "~a" (get-real-2d a 1 0))

它打印出第二行第一列中的元素.

it prints out the element in second row first column.

矩阵a看起来像这样

a =
((0 1 2)
(3 4 5)
(6 7 8))

,我希望您可以确切地向我展示如何编写dotimes循环或其他循环 将使用get-real-2d函数在尽可能少的行中打印出矩阵,因此输出如下所示:

and I was hoping you can show me exactly how to write a dotimes loop or other loop that would in as few lines as possible would print out the matrix using the get-real-2d function so the output looks like this:

0 1 2 
3 4 5
6 7 8

我只是希望您可以向我展示一个光滑的循环,该循环非常小,可以用来打印可以在lisp库中使用的矩阵,使它们看起来真正专业,例如仅使用变量的矩阵.像这样:

I'm just hoping you can show me a slick loop that would be real small that I can use to print matrices that I can use in my lisp library something real professional looking, like one that would use only variables. Something like:

(format t "~a" (get-real-2d i j))

而不是一堆:

(format t "~a" (get-real-2d 0 0))
(format t "~a" (get-real-2d 0 1))
(format t "~a" (get-real-2d 0 2))

;;;;最新编辑;;; 为了简单起见,我打电话

;;;;LATEST EDIT;;; to make this simple I call

(defparameter a (create-mat 3 3 +32fc1+))

创建3x3矩阵-create-mat是opencv的cvCreateMat的包装

to create a 3x3 matrix - create-mat is a wrapper for opencv's cvCreateMat

该命令在repl的输出是

the output from that command at repl is

(defparameter a (create-mat 3 3 +32fc1+))
A
CL-OPENCV> a
#.(SB-SYS:INT-SAP #X7FFFD8000E00)

我/e变量a是指向3x3矩阵的指针

i/e the variable a is a pointer to the 3x3 matrix

然后我跑步

(defparameter data (cffi:foreign-alloc :float :initial-contents 
          '(0.0f0 1.0f0 2.0f0 3.0f0 4.0f0 5.0f0 6.0f0 7.0f0 8.0f0)))

为矩阵创建数据-接下来我将分配给矩阵

to create the data for the matrix - which I next will allocate to the matrix

该命令在repl的输出是

the output from that command at repl is

CL-OPENCV> (defparameter data (cffi:foreign-alloc :float :initial-contents 
          '(0.0f0 1.0f0 2.0f0 3.0f0 4.0f0 5.0f0 6.0f0 7.0f0 8.0f0)))
DATA
CL-OPENCV> data
#.(SB-SYS:INT-SAP #X7FFFD8000E40)

i/e变量a是指向要添加到矩阵的数据的数据指针

i/e the variable a is data pointer to the data ill add to the matrix

然后我打电话..

(set-data a data 12) to add the data to the matrix - set-data is a wrapper for opencv's cvSetData

所以现在当我运行时-(get-real-2d是opencv的cvGetReal2d的包装)

so now when I run - (get-real-2d is a wrapper for opencv's cvGetReal2d)

(get-real-2d a 0 0)  it gets the element of matrix a at row 0 col 0 which is 0.0d0

该命令在repl的输出是

the output from that command at repl is

CL-OPENCV> (get-real-2d a 0 0)
0.0d0

现在,当我跑步

(get-real-2d a 0 1)  it gets the element of matrix a at row 0 col 1 which is is 0.0d0

该命令在repl的输出是

the output from that command at repl is

CL-OPENCV> (get-real-2d a 0 1)
1.0d0

当我运行此循环

 (dotimes (i 3)
  (dotimes (j 3)
(format t "~a~%" (get-real-2d a i j))))

该命令在repl的输出是

the output from that command at repl is

CL-OPENCV> (dotimes (i 3)
  (dotimes (j 3)
(format t "~a~%" (get-real-2d a i j))))
0.0d0
1.0d0
2.0d0
3.0d0
4.0d0
5.0d0
6.0d0
7.0d0
8.0d0
NIL

但是当我尝试您的方法@Svante

but when I try your method @Svante

(dotimes (i 3)
  (dotimes (j 3)
(format t "~{~{~a~^ ~}~%~}" (get-real-2d a i j))))

我收到错误消息:

The value 0.0d0 is not of type LIST.
    [Condition of type TYPE-ERROR]

因为1次运行get-real-2d的输出只是1个浮点数i/e

because the output of 1 run of get-real-2d is just a 1 number float i/e

CL-OPENCV> (get-real-2d a 0 0)
0.0d0

通过该信息,您可以帮助我打印矩阵,使它看起来像这样

with that info can you help me print the matrix so it looks like this

0.0d0 1.0d0 2.0d0 
3.0d0 4.0d0 5.0d0
6.0d0 7.0d0 8.0d0

推荐答案

您的问题可以通过两种方式来理解,这就是为什么它有两种解决方案的原因:

Your question can be understood in two ways, and that is why it has two solutions:

  • 定义打印矩阵类型对象的方法(在这种情况下,它可能会使用有关矩阵内部结构的知识):

  • Define method for printing object of type matrix (in this case it may use the knowledge about the internal structure of matrix):

(defmethod print-object ((matrix matrix) stream)
    (format stream "~{~{~a~^ ~}~%~}" matrix))

如答案中所示使用format.

定义可以使用对象唯一方法的客户端功能-get-real-2d:

Define client function that can use the only method of your object - get-real-2d:

(defun print-matrix (matrix dimension-x dimension-y)
    (dotimes (x dimension-x)
        (dotimes (y dimension-y)
            (princ (get-real-2d matrix x y))
            (princ #\Space))
        (princ #\Newline)))

只需使用dotimes.

这篇关于需要如何在Lisp中打印矩阵的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆