How do i print out all the elements in a N-D matrix?
Posted: Tue Oct 01, 2013 1:20 am
How do i print out all the elements in a N-Dimensional matrix sequentially in Lisp/CFFI?
;all the code here except for princ and dotimes is either lisp wrappers for opencv or cffi
for example I can print a 3d matrix with this btw way i'm trying to make the print-nd-matrix (hypothetical) function just like the below function except for upping the dimensions to infinite:
(defun print-3d-matrix (matrix dim-i dim-j dim-k)
(dotimes (i dim-i)
(dotimes (j dim-j)
(dotimes (k dim-k)
(princ (get-real-3d matrix i j k))
(princ #\Space))
(princ #\Newline))
(princ #\Newline)))
but the variation for a n-dimensional matrix....a matrix with infinite dimensions
would include
a new "get-element function" in the above code I use get-real-3d which takes the two to set up
;;; this makes a pointer for the number of matrix elements i/e it makes it a 5x5x5 matrix - a 3-d matrix
(sizes (cffi:foreign-alloc :int :initial-contents
(list 5 5 5)))
;;this creates the matrix using sizes to set the matrix number of matrix elements
;;3 is the part that actually sets the matrix dimensions which here is 3
(mat (create-mat-nd 3 sizes +8uc1+))
for a n-dimensional matrix i would need to create the matrix the same way as above but for testing lets make a 5x5x5x5x5 matrix so:
(sizes (cffi:foreign-alloc :int :initial-contents
(list 5 5 5 5 5)))
(mat (create-mat-nd 5 sizes +8uc1+))
so to change the print-3d-matrix function above for the first line of it i would do
(defun print-nd-matrix (matrix indices)
indices is a variable for the get-real-nd function I'll need to use instead of the above get-real-3d it is a wrapper for the opencv function cvGetRealND here http://docs.opencv.org/modules/core/doc ... nt*%20idx):
enter code here
it equal to this
(indices (cffi:foreign-alloc :int :initial-contents
'(0 0 0 0 0)))
and the get-real-nd function looks like this:
(get-real-nd mat indices)
so if i use
(get-real-nd mat indices)
it retrieves the element at 0x0x0x0x0 of the matrix "mat"
so if some one were to run my hypothetical print-nd-matrix function the first parameter would be mat and the second would be indices
so to change the print-3d-matrix function above for the next 3 lines of it i would do
(dotimes (i dim-i)
(dotimes (j dim-j)
(dotimes (k dim-k)
for dim-i,j,k so far i know i can use this:
(loop for i from 0 below 5
collect (cffi:mem-aref indices :int i))
whech dereferences the indice pointer and outputs this:
(0 0 0 0 0)
a list of 5 elements ...I can do
(length (loop for i from 0 below 5
collect (cffi:mem-aref indices :int i)))
which gives me length of list which id
5
but how wouuld i write a funtion that would "write itself" by creating the right amount of dotimes loops and nesting them properly along with adding the
(princ #\Space))
(princ #\Newline)
(princ #\Newline)
all in the right places for each one or is that not the right way to go....I would like it similiar to the print-3d-matrix for looks but it doesnt really matter because it will be buried in my library source code
If anyone can help me inerate over the elements of a n-dimensional matrix so the (princ) output is basic and looks like this (the below is 3d -matrix contents translated would be n(ulimited)-dimensions).....I would really appreciate it..
*3D Matrix Contents*
1.0d0 2.0d0 3.0d0 4.0d0 5.0d0
6.0d0 7.0d0 8.0d0 9.0d0 10.0d0
11.0d0 12.0d0 13.0d0 14.0d0 15.0d0
16.0d0 17.0d0 18.0d0 19.0d0 20.0d0
21.0d0 22.0d0 23.0d0 24.0d0 25.0d0
26.0d0 27.0d0 28.0d0 29.0d0 30.0d0
31.0d0 32.0d0 33.0d0 34.0d0 35.0d0
36.0d0 37.0d0 38.0d0 39.0d0 40.0d0
41.0d0 42.0d0 43.0d0 44.0d0 45.0d0
46.0d0 47.0d0 48.0d0 49.0d0 50.0d0
51.0d0 52.0d0 53.0d0 54.0d0 55.0d0
56.0d0 57.0d0 58.0d0 59.0d0 60.0d0
61.0d0 62.0d0 63.0d0 64.0d0 65.0d0
66.0d0 67.0d0 68.0d0 69.0d0 70.0d0
71.0d0 72.0d0 73.0d0 74.0d0 75.0d0
76.0d0 77.0d0 78.0d0 79.0d0 80.0d0
81.0d0 82.0d0 83.0d0 84.0d0 85.0d0
86.0d0 87.0d0 88.0d0 89.0d0 90.0d0
91.0d0 92.0d0 93.0d0 94.0d0 95.0d0
96.0d0 97.0d0 98.0d0 99.0d0 100.0d0
101.0d0 102.0d0 103.0d0 104.0d0 105.0d0
106.0d0 107.0d0 108.0d0 109.0d0 110.0d0
111.0d0 112.0d0 113.0d0 114.0d0 115.0d0
116.0d0 117.0d0 118.0d0 119.0d0 120.0d0
121.0d0 122.0d0 123.0d0 124.0d0 125.0d0
*3D Matrix Contents*
;all the code here except for princ and dotimes is either lisp wrappers for opencv or cffi
for example I can print a 3d matrix with this btw way i'm trying to make the print-nd-matrix (hypothetical) function just like the below function except for upping the dimensions to infinite:
(defun print-3d-matrix (matrix dim-i dim-j dim-k)
(dotimes (i dim-i)
(dotimes (j dim-j)
(dotimes (k dim-k)
(princ (get-real-3d matrix i j k))
(princ #\Space))
(princ #\Newline))
(princ #\Newline)))
but the variation for a n-dimensional matrix....a matrix with infinite dimensions
would include
a new "get-element function" in the above code I use get-real-3d which takes the two to set up
;;; this makes a pointer for the number of matrix elements i/e it makes it a 5x5x5 matrix - a 3-d matrix
(sizes (cffi:foreign-alloc :int :initial-contents
(list 5 5 5)))
;;this creates the matrix using sizes to set the matrix number of matrix elements
;;3 is the part that actually sets the matrix dimensions which here is 3
(mat (create-mat-nd 3 sizes +8uc1+))
for a n-dimensional matrix i would need to create the matrix the same way as above but for testing lets make a 5x5x5x5x5 matrix so:
(sizes (cffi:foreign-alloc :int :initial-contents
(list 5 5 5 5 5)))
(mat (create-mat-nd 5 sizes +8uc1+))
so to change the print-3d-matrix function above for the first line of it i would do
(defun print-nd-matrix (matrix indices)
indices is a variable for the get-real-nd function I'll need to use instead of the above get-real-3d it is a wrapper for the opencv function cvGetRealND here http://docs.opencv.org/modules/core/doc ... nt*%20idx):
enter code here
it equal to this
(indices (cffi:foreign-alloc :int :initial-contents
'(0 0 0 0 0)))
and the get-real-nd function looks like this:
(get-real-nd mat indices)
so if i use
(get-real-nd mat indices)
it retrieves the element at 0x0x0x0x0 of the matrix "mat"
so if some one were to run my hypothetical print-nd-matrix function the first parameter would be mat and the second would be indices
so to change the print-3d-matrix function above for the next 3 lines of it i would do
(dotimes (i dim-i)
(dotimes (j dim-j)
(dotimes (k dim-k)
for dim-i,j,k so far i know i can use this:
(loop for i from 0 below 5
collect (cffi:mem-aref indices :int i))
whech dereferences the indice pointer and outputs this:
(0 0 0 0 0)
a list of 5 elements ...I can do
(length (loop for i from 0 below 5
collect (cffi:mem-aref indices :int i)))
which gives me length of list which id
5
but how wouuld i write a funtion that would "write itself" by creating the right amount of dotimes loops and nesting them properly along with adding the
(princ #\Space))
(princ #\Newline)
(princ #\Newline)
all in the right places for each one or is that not the right way to go....I would like it similiar to the print-3d-matrix for looks but it doesnt really matter because it will be buried in my library source code
If anyone can help me inerate over the elements of a n-dimensional matrix so the (princ) output is basic and looks like this (the below is 3d -matrix contents translated would be n(ulimited)-dimensions).....I would really appreciate it..
*3D Matrix Contents*
1.0d0 2.0d0 3.0d0 4.0d0 5.0d0
6.0d0 7.0d0 8.0d0 9.0d0 10.0d0
11.0d0 12.0d0 13.0d0 14.0d0 15.0d0
16.0d0 17.0d0 18.0d0 19.0d0 20.0d0
21.0d0 22.0d0 23.0d0 24.0d0 25.0d0
26.0d0 27.0d0 28.0d0 29.0d0 30.0d0
31.0d0 32.0d0 33.0d0 34.0d0 35.0d0
36.0d0 37.0d0 38.0d0 39.0d0 40.0d0
41.0d0 42.0d0 43.0d0 44.0d0 45.0d0
46.0d0 47.0d0 48.0d0 49.0d0 50.0d0
51.0d0 52.0d0 53.0d0 54.0d0 55.0d0
56.0d0 57.0d0 58.0d0 59.0d0 60.0d0
61.0d0 62.0d0 63.0d0 64.0d0 65.0d0
66.0d0 67.0d0 68.0d0 69.0d0 70.0d0
71.0d0 72.0d0 73.0d0 74.0d0 75.0d0
76.0d0 77.0d0 78.0d0 79.0d0 80.0d0
81.0d0 82.0d0 83.0d0 84.0d0 85.0d0
86.0d0 87.0d0 88.0d0 89.0d0 90.0d0
91.0d0 92.0d0 93.0d0 94.0d0 95.0d0
96.0d0 97.0d0 98.0d0 99.0d0 100.0d0
101.0d0 102.0d0 103.0d0 104.0d0 105.0d0
106.0d0 107.0d0 108.0d0 109.0d0 110.0d0
111.0d0 112.0d0 113.0d0 114.0d0 115.0d0
116.0d0 117.0d0 118.0d0 119.0d0 120.0d0
121.0d0 122.0d0 123.0d0 124.0d0 125.0d0
*3D Matrix Contents*