How do i fill in all of the elements of a matrix in Lisp?

Discussion of Common Lisp

How do i fill in all of the elements of a matrix in Lisp?

Postby joeish80829 » Mon Sep 30, 2013 2:31 am

'm trying to figure out how to do this....basically i have a 5x5 matrix and in pseudo code for explanation purpose id like to do this

in a forever loop(must be do* loop)

Code: Select all
i = 0
j = 0
print a number at element 0x0
print a number at element 1x0
print a number at element 2x0
print a number at element 3x0
print a number at element 4x0
j = 1
print a number at element 0x1
print a number at element 1x1
print a number at element 2x1
print a number at element 3x1
print a number at element 4x1
j = 2
print a number at element 0x2
print a number at element 1x2
print a number at element 2x2
print a number at element 3x2
print a number at element 4x2
j = 3
print a number at element 0x3
print a number at element 1x3
print a number at element 2x3
print a number at element 3x3
print a number at element 4x3
j = 4
print a number at element 0x4
print a number at element 1x4
print a number at element 2x4
print a number at element 3x4
print a number at element 4x4
Code: Select all


this would be big code so i'm hoping some one can help me write this in common lisp

this is the code i'm trying to convert to use a 5x5 matrix it uses a vector now

Code: Select all
(defun ptr-1d-example ()

(let* ((vec (create-mat 1 4 +8uc1+))
(scalar (make-cv-scalar 0))
(window-name "Watch the vector change! - PTR-1D Example"))
(named-window window-name +window-normal+)
(move-window window-name 690 285)
(do* ((1d-elem-ptr 0)
(element 0))
((plusp (wait-key *millis-per-frame*)) nil)
(set-1d vec element scalar)
(setf 1d-elem-ptr (ptr-1d vec element))
(if (< (cffi:mem-ref 1d-elem-ptr :uchar) 255)
(progn
(incf (cffi:mem-ref 1d-elem-ptr :uchar) 1)
(setf scalar (make-cv-scalar (cffi:mem-ref 1d-elem-ptr :uchar)))
(set-1d vec element scalar)
(show-image window-name vec)
(incf element)
(if (equal element 4) (setf element 0))
(format t "Now on ELEMENT ~a~%~%" element)
(format t "ID-ELEM-PTR = ~a~%~%"
(cffi:mem-ref 1d-elem-ptr :uchar)))
(progn
(setf (cffi:mem-ref 1d-elem-ptr :uchar) 0)
(setf scalar (make-cv-scalar (cffi:mem-ref 1d-elem-ptr :uchar)))
(set-1d vec element scalar))))
(release-mat vec)
(destroy-window window-name)))
Code: Select all


the

Code: Select all
(ptr-1d vec element)
Code: Select all


creats a pointer to a "element" of the "vec"

this dereferences the pointer to an int

Code: Select all
(
cffi:mem-ref 1d-elem-ptr :uchar)
Code: Select all


this creates the element of the matrix i'm trying to allocate

Code: Select all
(make-cv-scalar 0)
Code: Select all


this sets the 1 dimensional element

Code: Select all
(set-1d vec element scalar)
Code: Select all


when i convert the code to use a matrix it will be

Code: Select all
(set-2d mat i j scalar)
Code: Select all


this is just a loop that keeps the do loop running til a key is pressed

Code: Select all
((plusp (wait-key *millis-per-frame*)) nil)
Code: Select all


I tried

Code: Select all
(i 0)
(j 0)
(if (< i 6)
(incf i) (setf i 4...but then i gets incremented again

(if (i < 6) (progn
(if (< j 6)....then my mind just goes blank I could use help wrapping my head around the process
Code: Select all


i tried for an 2 housr to figure this out i just can't figure out how to substitute this

Code: Select all
(vec (create-mat 1 4 +8uc1+))
Code: Select all


for

(mat (create-mat 5 5 +8uc1+))

and fill it up in a forever loop like i outlined above without writing big code if some one could help me with the logic on this one...I would really appreciate it

to help you understand

here is the code made for a matrix...i need to keep every thing the same but just change the

Code: Select all
(incf i)
(if (equal i 5) (setf i 0))

part

(defun ptr-2d-example ()
"This function changes the vector's elements visually using PTR-ID.
It's an amazing hint as how to have fun(and success) with math."
(let* ((mat (create-mat 5 5 +8uc1+))
(scalar (make-cv-scalar 0))
(window-name "Watch the vector change! - PTR-1D Example"))
(named-window window-name +window-normal+)
(move-window window-name 690 285)
(do* ((2d-elem-ptr 0)
(i 0)
(j 0))
((plusp (wait-key *millis-per-frame*)) nil)
(set-2d mat i j scalar)
(setf 2d-elem-ptr (ptr-2d mat i j))
(if (< (cffi:mem-ref 2d-elem-ptr :uchar) 255)
(progn
(incf (cffi:mem-ref 2d-elem-ptr :uchar) 1)
(setf scalar (make-cv-scalar (cffi:mem-ref 2d-elem-ptr :uchar)))
(set-2d mat i j scalar)
(show-image window-name mat)
(incf i)
(if (equal i 5) (setf i 0))
(format t "Now on ELEMENT ~ax~a~%~%" i j)
(format t "2D-ELEM-PTR = ~a~%~%"
(cffi:mem-ref 2d-elem-ptr :uchar)))
(progn
(setf (cffi:mem-ref 2d-elem-ptr :uchar) 0)
(setf scalar (make-cv-scalar (cffi:mem-ref 2d-elem-ptr :uchar)))
(set-2d mat i j scalar))))
(release-mat mat)
(destroy-window window-name)))
Code: Select all


the code as is just sets and resets all the elements of the first column in the matrix

i tried

Code: Select all
(if (< (and i j) 5)
(progn (incf i)
; (incf j)
)
(progn
(setf i 0)

(if (< j 6) (incf j) (if (not (equal j -1)) (decf j)))))
Code: Select all


that just gets the first column....filling every element 0c0 to 5x0 with 1 to 5 respectively

I'm just outta ideas on this and i could use help getting this done...it will help me break into using better logic in my progams and get me ready for A.I. research

I would appreciate any help on this
joeish80829
 
Posts: 153
Joined: Tue Sep 03, 2013 5:32 am

Re: How do i fill in all of the elements of a matrix in Lisp

Postby pjstirling » Tue Oct 01, 2013 8:12 am

joeish80829 wrote:'m trying to figure out how to do this....basically i have a 5x5 matrix and in pseudo code for explanation purpose id like to do this

in a forever loop(must be do* loop)

Code: Select all
 i = 0
  j = 0
  print a number at element 0x0
  print a number at element 1x0
  print a number at element 2x0
  print a number at element 3x0
  print a number at element 4x0
  j = 1
  print a number at element 0x1
  print a number at element 1x1
  print a number at element 2x1
  print a number at element 3x1
  print a number at element 4x1
  j = 2
  print a number at element 0x2
  print a number at element 1x2
  print a number at element 2x2
  print a number at element 3x2
  print a number at element 4x2
  j = 3
  print a number at element 0x3
  print a number at element 1x3
  print a number at element 2x3
  print a number at element 3x3
  print a number at element 4x3
  j = 4
  print a number at element 0x4
  print a number at element 1x4
  print a number at element 2x4
  print a number at element 3x4
  print a number at element 4x4


this would be big code so i'm hoping some one can help me write this in common lisp

this is the code i'm trying to convert to use a 5x5 matrix it uses a vector now

Code: Select all
 (defun ptr-1d-example ()

  (let* ((vec (create-mat 1 4 +8uc1+))
         (scalar (make-cv-scalar 0))
         (window-name "Watch the vector change! - PTR-1D Example"))     
              (named-window window-name +window-normal+)
              (move-window window-name 690 285)
          (do* ((1d-elem-ptr 0)
                (element 0))
               ((plusp (wait-key *millis-per-frame*)) nil)
                (set-1d vec element scalar)
                (setf 1d-elem-ptr (ptr-1d vec element))
            (if (< (cffi:mem-ref 1d-elem-ptr :uchar) 255)
            (progn
                (incf (cffi:mem-ref 1d-elem-ptr :uchar) 1)
                (setf scalar (make-cv-scalar (cffi:mem-ref 1d-elem-ptr :uchar)))
                (set-1d vec element scalar)
                (show-image window-name vec)
                (incf element)
            (if (equal element 4) (setf element 0))
                (format t "Now on ELEMENT ~a~%~%" element)
                (format t "ID-ELEM-PTR = ~a~%~%"
                (cffi:mem-ref 1d-elem-ptr :uchar)))
            (progn
                (setf (cffi:mem-ref 1d-elem-ptr :uchar) 0)
                (setf scalar (make-cv-scalar (cffi:mem-ref 1d-elem-ptr :uchar)))
                (set-1d vec element scalar))))
              (release-mat vec)
              (destroy-window window-name)))


the

Code: Select all
 (ptr-1d vec element)


creats a pointer to a "element" of the "vec"

this dereferences the pointer to an int

Code: Select all
(cffi:mem-ref 1d-elem-ptr :uchar)


this creates the element of the matrix i'm trying to allocate

Code: Select all
(make-cv-scalar 0)


this sets the 1 dimensional element

Code: Select all
(set-1d vec element scalar)


when i convert the code to use a matrix it will be

Code: Select all
(set-2d mat i j  scalar)


this is just a loop that keeps the do loop running til a key is pressed

Code: Select all
((plusp (wait-key *millis-per-frame*)) nil)


I tried

Code: Select all
(i 0)
(j 0)
(if (< i 6)
    (incf i) (setf i 4...but then i gets incremented again

(if (i < 6) (progn
               (if (< j 6)....then my mind just goes blank I could use help wrapping my head around the process


i tried for an 2 housr to figure this out i just can't figure out how to substitute this

Code: Select all
(vec (create-mat 1 4 +8uc1+))


for

(mat (create-mat 5 5 +8uc1+))

and fill it up in a forever loop like i outlined above without writing big code if some one could help me with the logic on this one...I would really appreciate it

to help you understand

here is the code made for a matrix...i need to keep every thing the same but just change the

Code: Select all
 (incf i)
                    (if (equal i 5) (setf i 0))

part

(defun ptr-2d-example ()
      "This function changes the vector's elements visually using PTR-ID.
       It's an amazing hint as how to have fun(and success) with math."
      (let* ((mat (create-mat 5 5 +8uc1+))
             (scalar (make-cv-scalar 0))
             (window-name "Watch the vector change! - PTR-1D Example"))     
                  (named-window window-name +window-normal+)
                  (move-window window-name 690 285)
              (do* ((2d-elem-ptr 0)
                    (i 0)
                    (j 0))
                   ((plusp (wait-key *millis-per-frame*)) nil)
                    (set-2d mat i j scalar)
                    (setf 2d-elem-ptr (ptr-2d mat i j))
                (if (< (cffi:mem-ref 2d-elem-ptr :uchar) 255)
                (progn
                    (incf (cffi:mem-ref 2d-elem-ptr :uchar) 1)
                    (setf scalar (make-cv-scalar (cffi:mem-ref 2d-elem-ptr :uchar)))
                    (set-2d mat i j scalar)
                    (show-image window-name mat)
                    (incf i)
                (if (equal i 5) (setf i 0))
                    (format t "Now on ELEMENT ~ax~a~%~%" i j)
                    (format t "2D-ELEM-PTR = ~a~%~%"
                    (cffi:mem-ref 2d-elem-ptr :uchar)))
                (progn
                    (setf (cffi:mem-ref 2d-elem-ptr :uchar) 0)
                    (setf scalar (make-cv-scalar (cffi:mem-ref 2d-elem-ptr :uchar)))
                    (set-2d mat i j scalar))))
                  (release-mat mat)
                  (destroy-window window-name)))


the code as is just sets and resets all the elements of the first column in the matrix

i tried

Code: Select all
 (if (< (and i j) 5)
                     (progn (incf i)
                           ; (incf j)
)
                                   (progn
                                        (setf i 0)

                                        (if (< j 6) (incf j) (if (not (equal j -1)) (decf j)))))


that just gets the first column....filling every element 0c0 to 5x0 with 1 to 5 respectively

I'm just outta ideas on this and i could use help getting this done...it will help me break into using better logic in my progams and get me ready for A.I. research

I would appreciate any help on this


I fixed the formatting. Please use the preview button before you post so that you can see if it worked!
pjstirling
 
Posts: 84
Joined: Sun Nov 28, 2010 4:21 pm

Re: How do i fill in all of the elements of a matrix in Lisp

Postby nuntius » Tue Oct 01, 2013 6:05 pm

Is this something like what you want?

Code: Select all
(let ((x (make-array '(3 4) :initial-element 2)))
  (dotimes (m (array-dimension x 0))
    (dotimes (n (array-dimension x 1))
      (format t "~A " (aref x m n)))
    (format t "~%")))
User avatar
nuntius
 
Posts: 502
Joined: Sat Aug 09, 2008 10:44 am
Location: Burlington, MA


Return to Common Lisp

Who is online

Users browsing this forum: No registered users and 2 guests