TheSwamp

Code Red => AutoLISP (Vanilla / Visual) => Topic started by: Shay Gaghe on July 16, 2015, 10:24:46 PM

Title: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 16, 2015, 10:24:46 PM
hi

simple code to get an array of points. i tried using macpar,lambda and others....but  coudnt make it work.

is this the only way to achieve the task?

Code: [Select]
(defun getPolarCoors(stp int / lst )
  (setq x 0)
 
   (repeat 4
      (setq lst (cons (polar stp 0.0  (setq x (+ x int)) )lst)) 
     
   )
)
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 16, 2015, 11:16:17 PM
Is this the function signature ??

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))
  11.        
  12.  |;
  13.  
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 17, 2015, 03:45:48 AM
Is this the function signature ??

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))
  11.        
  12.  |;
  13.  

exactly Kerry!
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 17, 2015, 04:04:04 AM
Good.

I tried about 4 variations and surprisingly polar seemed the fastest.
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (DOIT (LIST 1 2 42) 5).................765 / 1.4011 <slowest>: 0.04669189ms Per iteration
  5.     (INCREMENTXVALUE4 (LIST 1 2 42) 5).....764 / 1.3993
  6.     (DOIT3 (LIST 1 2 42) 5)................764 / 1.3993
  7.     (INCREMENTXVALUE (LIST 1 2 42) 5)......749 / 1.3718
  8.     (GETPOLARCOORS (LIST 1 2 42) 5)........546 / 1.0000 <fastest>: 0.03332520ms Per iteration

I thought this would cream it, but still takes 1.4 times as long as the polar version and ended up being the slowest :)

Code - Auto/Visual Lisp: [Select]
  1. (defun doit (StartPoint Interval / x)
  2.    (setq x 0)
  3.    (mapcar
  4.       (function
  5.          (lambda (pt) (mapcar '+ (list (setq x (+ x Interval)) 0. 0.) pt))
  6.       )
  7.       (list StartPoint StartPoint StartPoint StartPoint)
  8.    )
  9. )
  10.  

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))        
  11.  |;
  12.  
  13. (defun IncrementXvalue (StartPoint Interval / lst pt)
  14.    (setq pt StartPoint )
  15.    (repeat 4
  16.       (setq lst
  17.               (cons (setq pt (mapcar '+ pt (list Interval 0 0)))
  18.                     lst
  19.               )
  20.       )
  21.    )
  22. )
  23.  
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 17, 2015, 05:05:46 AM
Cant wait to test and learn it.
What about recrusive function? Maybe its for me to stop being afraid  of them :)
Title: Re: iteration code : short and efficiency challenge
Post by: ElpanovEvgeniy on July 17, 2015, 05:46:03 AM
recursive:

Code - Auto/Visual Lisp: [Select]
  1. (defun f (s i n)
  2.   ;; s - start point             ~ '(0 0 0)
  3.   ;; i - interval                ~ '(5 2 0)
  4.   ;; n - number of points       ~ 5
  5.   ;;(setq s '(0 0 0) i  '(5 2 0) n 5)
  6.   ;; (f s i n)
  7.   (cond ((atom (car s)) (f (list s) i n))
  8.         ((> n 0) (f (cons (mapcar '+ (car s) i) s) i (1- n)))
  9.         (s)
  10.   )
  11. )
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 17, 2015, 06:30:17 AM
Good.

I tried about 4 variations and surprisingly polar seemed the fastest.
Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (DOIT (LIST 1 2 42) 5).................765 / 1.4011 <slowest>: 0.04669189ms Per iteration
  5.     (INCREMENTXVALUE4 (LIST 1 2 42) 5).....764 / 1.3993
  6.     (DOIT3 (LIST 1 2 42) 5)................764 / 1.3993
  7.     (INCREMENTXVALUE (LIST 1 2 42) 5)......749 / 1.3718
  8.     (GETPOLARCOORS (LIST 1 2 42) 5)........546 / 1.0000 <fastest>: 0.03332520ms Per iteration

I thought this would cream it, but still takes 1.4 times as long as the polar version and ended up being the slowest :)

Code - Auto/Visual Lisp: [Select]
  1. (defun doit (StartPoint Interval / x)
  2.    (setq x 0)
  3.    (mapcar
  4.       (function
  5.          (lambda (pt) (mapcar '+ (list (setq x (+ x Interval)) 0. 0.) pt))
  6.       )
  7.       (list StartPoint StartPoint StartPoint StartPoint)
  8.    )
  9. )
  10.  

Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))        
  11.  |;
  12.  
  13. (defun IncrementXvalue (StartPoint Interval / lst pt)
  14.    (setq pt StartPoint )
  15.    (repeat 4
  16.       (setq lst
  17.               (cons (setq pt (mapcar '+ pt (list Interval 0 0)))
  18.                     lst
  19.               )
  20.       )
  21.    )
  22. )
  23.  

Thanx Kerry
ive tested your codes. both generate artificial X so you are limited. and as you said they are slowest.

mapcar is forced to be in this solution...its obviously not its natural place.

as you said polar is the ideal solution, fastest and can retrieve points by angle.

do you agree?

BDW how do you get this time report?

Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 17, 2015, 06:40:48 AM
recursive:

Code - Auto/Visual Lisp: [Select]
  1. (defun f (s i n)
  2.   ;; s - start point             ~ '(0 0 0)
  3.   ;; i - interval                ~ '(5 2 0)
  4.   ;; n - number of points       ~ 5
  5.   ;;(setq s '(0 0 0) i  '(5 2 0) n 5)
  6.   ;; (f s i n)
  7.   (cond ((atom (car s)) (f (list s) i n))
  8.         ((> n 0) (f (cons (mapcar '+ (car s) i) s) i (1- n)))
  9.         (s)
  10.   )
  11. )

wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 17, 2015, 06:44:47 AM
< .. >

wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?

Shay, you'll have to do some study. Google may help with the principles.

added piccy
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 17, 2015, 06:57:36 AM
Hi Evgeniy,

Nice bit of code.

I think   (f (list 1 2 42) '(5 2 0) 4)

should return  ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )

Regards,


Title: Re: iteration code : short and efficiency challenge
Post by: ElpanovEvgeniy on July 17, 2015, 07:07:45 AM
Hi Evgeniy,

Nice bit of code.

I think   (f (list 1 2 42) '(5 2 0) 4)

should return  ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )

Regards,

Thank you  :-)
Code - Auto/Visual Lisp: [Select]
  1. (defun f (s i n)
  2.   ;; s - start point             ~ '(0 0 0)
  3.   ;; i - interval                ~ '(5 2 0)
  4.   ;; n - number of points       ~ 5
  5.   ;;(setq s '(0 0 0) i  '(5 2 0) n 5)
  6.   ;; (f s i n)
  7.   (cond ((atom (car s)) (f (list (mapcar '+ s i)) i n))
  8.         ((> n 1) (f (cons (mapcar '+ (car s) i) s) i (1- n)))
  9.         (s)
  10.   )
  11. )
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 17, 2015, 07:13:48 AM
Cool !

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking-V2 :B=500 [M.P.2005 <revised kdub 2005,2014>] .................
  2. Elapsed milliseconds for 16384 iteration(s)/ relative Timing :
  3.  
  4.     (INCREMENTXVALUE (LIST 1 2 42) 5).......749 / 1.4544 <slowest>: 0.04571533ms Per iteration
  5.     (DOIT3 (LIST 1 2 42) 5).................749 / 1.4544
  6.     (INCREMENTXVALUE4 (LIST 1 2 42) 5)......733 / 1.4233
  7.     (F (LIST 1 2 42) (QUOTE (5 2 0)) 4).....733 / 1.4233
  8.     (DOIT (LIST 1 2 42) 5)..................733 / 1.4233
  9.     (GETPOLARCOORS (LIST 1 2 42) 5).........515 / 1.0000 <fastest>: 0.03143311ms Per iteration
  10.  
Title: Re: iteration code : short and efficiency challenge
Post by: ElpanovEvgeniy on July 17, 2015, 07:20:12 AM
 :-D
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 17, 2015, 09:14:44 AM
If you are only looking for raw performance, I would think the following would be most efficient:

Code - Auto/Visual Lisp: [Select]
  1. (defun LM:f ( p i / x )
  2.     (setq x (car p)
  3.           p (cdr p)
  4.     )
  5.     (list
  6.         (cons (+ x i) p)
  7.         (cons (+ x i i) p)
  8.         (cons (+ x i i i) p)
  9.         (cons (+ x i i i i) p)
  10.     )
  11. )

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):
  2.  
  3.     (LM:F (QUOTE (1 2 42)) 5)..................1107 / 1.87 <fastest>
  4.     (GETPOLARCOORS (QUOTE (1 2 42)) 5).........1373 / 1.51
  5.     (INCREMENTXVALUE (QUOTE (1 2 42)) 5).......1981 / 1.05
  6.     (DOIT (QUOTE (1 2 42)) 5)..................2044 / 1.02
  7.     (F (QUOTE (1 2 42)) (QUOTE (5 0 0)) 4).....2075 / 1 <slowest>
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 17, 2015, 09:41:16 PM
Hi Lee. THANX. how do you get the time report?
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 18, 2015, 06:12:47 AM
Hi Lee. THANX. how do you get the time report?

I use MP's excellent Benchmark (http://www.theswamp.org/lilly_pond/mp/lisp/benchmark.txt?nossi=1) utility.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 19, 2015, 03:30:38 AM
Hi Lee. THANX. how do you get the time report?

I use MP's excellent Benchmark (http://www.theswamp.org/lilly_pond/mp/lisp/benchmark.txt?nossi=1) utility.

THanks Lee

i need to retrive coordinatyes pretty much the same way the masure command is doing , but i need it as a function . thats the code i wrote:

Code: [Select]
;_measure coordinates and retrive a list
 ;_stp - start point
 ;_etp - end point
 ;_ int - interval
(defun measurex (stp etp int / x lst)
  (setq x 0)
   (append
(repeat (fix (/ (distance stp etp) int))
  (setq lst
(cons
   (polar stp (angle stp etp) (setq x (+ x int)))
   lst
   ) ;_ end of cons
) ;_ end of setq
  ) ;_ end of lst
(list stp)
    )
  ) ;_ end of defun


;_$ (setq res (measurex '(0 0 0) '(12 0 0) 2))

1. how can i make sure that the precision of the coordinate  '(0 0 0) is respecting the precision factor in rule?
2. advise to make this function more stable and efficient

THx

Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 19, 2015, 04:15:56 AM
1. how can i make sure that the precision of the coordinate  '(0 0 0) is respecting the precision factor in rule?

Please explain exactly what you mean ..
what is "respecting the precision factor in rule" ?

2. advise [how to]  make this function more stable and efficient

Where is it not stable ??
Where is it not efficient ??


one tip :
Don't use the end of statement comment.
It is really painful to look at and just clutters up what could be nice clean code.


added:
It is a pity that you didn't describe this functionality in the first post.
Using polar seems to be the best solution.
You could experiment with the sample from ElpanovEvgeniy but I don't think you will find a more efficient solution than the one you have.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 19, 2015, 04:56:59 AM
original post :
Quote
hi

simple code to get an array of points. i tried using macpar,lambda and others....but  coudnt make it work.

is this the only way to achieve the task?

Code: [Select]
(defun getPolarCoors(stp int / lst )
  (setq x 0)
 
   (repeat 4
      (setq lst (cons (polar stp 0.0  (setq x (+ x int)) )lst)) 
     
   )
)

i dont understand why my first post and the currnt one is not related in your eyes.both retrivie points , both needs iteration of some sort.

Quote
Please explain exactly what you mean ..
what is "respecting the precision factor in rule" ?

if you run the code with

Code: [Select]
$ (setq res (measurex '(0 0 0) '(12 0 0) 2))
ull get

((12.0 0.0 0.0) (10.0 0.0 0.0) (8.0 0.0 0.0) (6.0 0.0 0.0) (4.0 0.0 0.0) (2.0 0.0 0.0) (0 0 0))

i want to make sure no one will call the function with integers but with reals ( i think i answer myself here  :cry:)


Quote
Where is it not stable ??
Where is it not efficient ??

thats why i post it. i want you guys to try to kill it. im sure its easy for you

Thanks
Shay
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 19, 2015, 05:08:21 AM
Quote
i dont understand why my first post and the current one is not related in your eyes.both retrivie points , both needs iteration of some sort.

Because the first one ONLY incremented the X ordinate ... and I went to the trouble of asking explicitly if that was what you wantd and you said yes it was what you wanted.
Quote
Is this the function signature ??
Code - Auto/Visual Lisp: [Select]
  1. ;|
  2.  IncrementXvalue <StartPoint> <Interval>
  3.  
  4.  StartPoint -> [list of 3 numbers ] Point coordinate list.
  5.  Interval   -> [number] Step distance.
  6.  
  7.  Return     -> [list of [point list][point list][point list]]
  8.         The x component of each point shall be sequentially increased by the Interval value
  9.         eg: (IncrementXvalue (list 1 2 42) 5)
  10.             ==> ((21.0 2.0 42.0) (16.0 2.0 42.0) (11.0 2.0 42.0) (6.0 2.0 42.0))
  11.        
  12.  |;
  13.  
Quote
exactly Kerry!


We are not mind readers ! and can only work with what we are given.

Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 19, 2015, 05:18:32 AM
< ... >
Quote
Where is it not stable ??
Where is it not efficient ??

thats why i post it. i want you guys to try to kill it. im sure its easy for you

Thanks
Shay

I think that YOU should be the one to look for ways to "kill" the routine. That is an excellent way to learn how to understand code.

If you can't find at least 3 or 4 I'd be very disappointed. :)

Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 19, 2015, 05:45:58 AM
Quote
We are not mind readers ! and can only work with what we are given.

with the skills that you have reached over the years...i do except you to read my mind  ;-)

anyway...i really intended this post to be generic  and demonstrate some approaches to handle code.
so im sorry if i mislead you and other readers.

Quote
I think that YOU should be the one to look for ways to "kill" the routine. That is an excellent way to learn how to understand code.

If you can't find at least 3 or 4 I'd be very disappointed. :)

You know Kerry ,
i been always scared by error trapping functions..but you leave me no choice  :evil: :-D
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 19, 2015, 05:58:51 AM
You don't need complicated error trapping.

Do you know the words assert or assertion ?

(if
   (and
      (is int a number)
      (is start a point)
      (is end a point)
    )
    ;; then
    (proceed)
)
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 19, 2015, 05:59:05 AM
Code: [Select]
(polar stp (angle stp etp) (setq x (+ x int)))

Hint: (angle stp etp) will not change, therefore it need not be recalculated for every iteration of the repeat loop  :wink:
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 19, 2015, 06:46:04 AM
This should offer some minor performance improvements:

Code - Auto/Visual Lisp: [Select]
  1. (defun measure1 ( s e i / a r )
  2.     (setq r (list s)
  3.           a (angle s e)
  4.     )
  5.     (repeat (fix (/ (distance s e) i))
  6.         (setq r (cons (polar (car r) a i) r))
  7.     )
  8. )

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):
  2.  
  3.     (MEASURE1 (QUOTE (0 0 0)) (QUOTE (12...).....2559 / 1.34 <fastest>
  4.     (MEASUREX (QUOTE (0 0 0)) (QUOTE (12...).....3432 / 1.00 <slowest>
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 19, 2015, 06:57:05 AM
Consider also that the current solutions which use a polar/angle combination will be restricted to 2D applications, as 3D points will be projected to the UCS plane when the angle is calculated.

For 3D applications, I would suggest something like:

Code - Auto/Visual Lisp: [Select]
  1. (defun measure2 ( s e i / d r v )
  2.     (setq r (list s)
  3.           d (distance s e)
  4.           v (mapcar '(lambda ( a b ) (* i (/ (- b a) d))) s e)
  5.     )
  6.     (repeat (fix (/ d i))
  7.         (setq r (cons (mapcar '+ (car r) v) r))
  8.     )
  9. )

Observe:

Code - Auto/Visual Lisp: [Select]
  1. _$ (measurex '(1 2 3) '(5 3 2) 1.3)
  2. ((4.78356 2.94589 3.0) (3.52237 2.63059 3.0) (2.26119 2.3153 3.0) (1 2 3))
  3. _$ (measure1 '(1 2 3) '(5 3 2) 1.3)
  4. ((4.78356 2.94589 3.0) (3.52237 2.63059 3.0) (2.26119 2.3153 3.0) (1 2 3))
  5. _$ (measure2 '(1 2 3) '(5 3 2) 1.3)
  6. ((4.67696 2.91924 2.08076) (3.4513 2.61283 2.38717) (2.22565 2.30641 2.69359) (1 2 3))
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 19, 2015, 08:14:25 AM
Thanks Kerry and Lee.
this is what i been after by writing this post.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 20, 2015, 12:16:57 AM
You don't need complicated error trapping.

Do you know the words assert or assertion ?

(if
   (and
      (is int a number)
      (is start a point)
      (is end a point)
    )
    ;; then
    (proceed)
)

no i dont , but i read about it, i assume its this

Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )

i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 20, 2015, 12:21:10 AM
Code: [Select]
(polar stp (angle stp etp) (setq x (+ x int)))

Hint: (angle stp etp) will not change, therefore it need not be recalculated for every iteration of the repeat loop  :wink:

you right!  thanx Lee
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 20, 2015, 12:26:20 AM
This should offer some minor performance improvements:

Code - Auto/Visual Lisp: [Select]
  1. (defun measure1 ( s e i / a r )
  2.     (setq r (list s)
  3.           a (angle s e)
  4.     )
  5.     (repeat (fix (/ (distance s e) i))
  6.         (setq r (cons (polar (car r) a i) r))
  7.     )
  8. )

i cant test it right now but looking at the code cant really  understand why you put s in a list?

Code - Auto/Visual Lisp: [Select]
  1. Benchmarking ...................Elapsed milliseconds / relative speed for 65536 iteration(s):
  2.  
  3.     (MEASURE1 (QUOTE (0 0 0)) (QUOTE (12...).....2559 / 1.34 <fastest>
  4.     (MEASUREX (QUOTE (0 0 0)) (QUOTE (12...).....3432 / 1.00 <slowest>
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 20, 2015, 12:55:29 AM
Quote
i cant test it right now but looking at the code cant really  understand why you put s in a list?

What does the function return ... which variable ??



what is the result of this
Code - Auto/Visual Lisp: [Select]
  1. (setq startpoint '(1 1 1) )

Then:
Code - Auto/Visual Lisp: [Select]
  1. (setq result (list startpoint))

Then :
Code - Auto/Visual Lisp: [Select]
  1. (setq result (cons '(2 2 2) result))
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 20, 2015, 01:16:03 AM
< ..>

no i dont , but i read about it, i assume its this

Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )


i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.

Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-every 'numberp pt))
  3.  
  4. )

edit kdub: revised from (vl-member-if 'numberp pt)to(vl-every 'numberp pt)
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 20, 2015, 04:30:07 AM
Quote
i cant test it right now but looking at the code cant really  understand why you put s in a list?

What does the function return ... which variable ??



what is the result of this
Code - Auto/Visual Lisp: [Select]
  1. (setq startpoint '(1 1 1) )

Then:
Code - Auto/Visual Lisp: [Select]
  1. (setq result (list startpoint))

Then :
Code - Auto/Visual Lisp: [Select]
  1. (setq result (cons '(2 2 2) result))

I get it now
Thank you Kerry
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 20, 2015, 04:44:43 AM
< ..>

no i dont , but i read about it, i assume its this

Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )


i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.

Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-member-if 'numberp pt))
  3. )

i didnt know about numbrtp function.  :embarrassed: Thanx

anyhow i would be happy to understand why my writen function return a list and not an atom as it were instructed to do.
Title: Re: iteration code : short and efficiency challenge
Post by: roy_043 on July 20, 2015, 04:47:05 AM
Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-member-if 'numberp pt))
  3. )
Not doubt you meant to use vl-every instead of vl-member-if:
Code: [Select]
(point-p '(1 "a" 3)) => T
Title: Re: iteration code : short and efficiency challenge
Post by: roy_043 on July 20, 2015, 05:00:27 AM
anyhow i would be happy to understand why my writen function return a list and not an atom as it were instructed to do.
But your function does not return a list! It is flawed though:
Code: [Select]
(isCoordReal '(1.0 "a" 3.0)) => T
Title: Re: iteration code : short and efficiency challenge
Post by: Kerry on July 20, 2015, 05:45:00 AM
Perhaps have a play with something like this as an alternative :
Code - Auto/Visual Lisp: [Select]
  1. (defun point-p (pt )
  2.   (and (listp pt) (= 3 (vl-list-length pt)) (vl-member-if 'numberp pt))
  3. )
Not doubt you meant to use vl-every instead of vl-member-if:
Code: [Select]
(point-p '(1 "a" 3)) => T


Thanks Roy ,

I'm rushing too much ..   :embarrassed:

Code revised.
Title: Re: iteration code : short and efficiency challenge
Post by: bruno_vdh on July 20, 2015, 06:04:05 AM
Hi,

wow....its too advanced for me. can you please give a simple code which explain better how to design recursive functions?

Perhaps this version it will be easier for you ...
Code: [Select]
(defun f (s i n)
  (if (> n 0)
    (cons s (f (mapcar '+ s i) i (1- n)))
  )
)
Or
Code: [Select]
(defun f (s i n)
  (if (> n 1)
    (cons s (f (mapcar '+ s i) i (1- n)))
    (list s)
  )
)
Code: [Select]
_$ (f '(1 2 42) '(5 0 0) 5)
((1 2 42) (6 2 42) (11 2 42) (16 2 42) (21 2 42))

One more not keep the starting point in the outcome.
Code: [Select]
(defun f (s i n)
  (if (> n 0)
    (cons (setq s (mapcar '+ s i)) (f s i (1- n)))
  )
)
Code: [Select]
_$ (f '(1 2 42) '(5 0 0) 4)
((6 2 42) (11 2 42) (16 2 42) (21 2 42))


no i dont , but i read about it, i assume its this
Code: [Select]
(defun isCoordReal (crd)
  (car
    (mapcar
      (function
(lambda (x y z)
  (= (type x) 'REAL)
  (= (type y) 'REAL)
  (= (type z) 'REAL)
  )
)
      (list (car crd))
      (list (cadr crd))
      (list (caddr crd))
      )
    )
  )

i cant understand why but the returned value is a list (T)  rather than an element. the car exp is totally ignored.

My variant:
Code: [Select]
(defun 3Dpoint-p (pt)
  (and (numberp (car pt)) (numberp (cadr pt)) (numberp (caddr pt)) (null (cdddr pt)))
)

Best Regards
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 20, 2015, 11:34:52 PM
This should offer some minor performance improvements:

Code - Auto/Visual Lisp: [Select]
  1. (defun measure1 ( s e i / a r )
  2.     (setq r (list s)
  3.           a (angle s e)
  4.     )
  5.     (repeat (fix (/ (distance s e) i))
  6.         (setq r (cons (polar (car r) a i) r))
  7.     )
  8. )

Quote
A genius is a person who displays exceptionally superior intellectual ability, creativity, or originality, typically to a degree that is associated with the achievement of new advances in a domain of knowledge. A scholar in many subjects or a scholar in a single subject may be referred to as a genius.[1] There is no scientifically precise definition of genius, and the question of whether the notion itself has any real meaning has long been a subject of debate, although psychologists are converging on a definition that emphasizes creativity and eminent achievement.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 22, 2015, 04:33:44 AM
hi

i need mapcar to iterate each 2 items rather than each item. i sign it with this code

Code: [Select]
(defun mapcar-x2(lst)
  (mapcar
    (function
      (lambda (a b)
(command "._line" a b)
(princ (strcat "Segment length: " (distance a b) ))
      )
     )
    lst ;_ how to shuffle the list in a way a and b will have not the same number each iteration?
   lst
    )
  )

hope i am unmderstood
Thanks
Shay
Title: Re: iteration code : short and efficiency challenge
Post by: VovKa on July 22, 2015, 06:09:04 AM
use (cdr lst) as a second argument
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 22, 2015, 06:31:14 AM
im not sure i get what you say :embarrassed:
Title: Re: iteration code : short and efficiency challenge
Post by: VovKa on July 22, 2015, 07:10:06 AM
Code: [Select]
(mapcar (function (lambda (a b)
    .....
  )
)
lst
(cdr lst)
)
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 22, 2015, 08:13:02 AM
Code: [Select]
(defun mapcar-x2(lst)
  (mapcar
    (function
      (lambda (a b)

(princ (strcat "Segment length: " (distance a b) ))
      )
     )
    lst
    (cdr lst)
    )
  )


Code: [Select]
_$ (mapcar-x2 '( (0 0 0)(0 0 0)(0 0 0)(0 0 0)) )
Code: [Select]
; error: bad argument type: stringp 0.0
Title: Re: iteration code : short and efficiency challenge
Post by: ElpanovEvgeniy on July 22, 2015, 08:13:48 AM
Hi Evgeniy,

Nice bit of code.

I think   (f (list 1 2 42) '(5 2 0) 4)

should return  ( (21 10 42) (16 8 42) (11 6 42) (6 4 42) )

Regards,

Thanks!
I understood, but not immediately...   :-)
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 22, 2015, 10:27:19 AM
Code: [Select]
(defun mapcar-x2(lst)
  (mapcar
    (function
      (lambda (a b)

(princ (strcat "Segment length: " (distance a b) ))
      )
     )
    lst
    (cdr lst)
    )
  )


Code: [Select]
_$ (mapcar-x2 '( (0 0 0)(0 0 0)(0 0 0)(0 0 0)) )
Code: [Select]
; error: bad argument type: stringp 0.0

(distance) returns a double (real), not a string.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 22, 2015, 01:41:09 PM
Thanks Lee

this solution is not working for me

Code: [Select]
_$ (mapcar-x2 '( (0 0 0)(1 0 0)(2 0 0)(3 0 0)) )
Code: [Select]
1th iteration

a = 0
b = 1

2th iteration

a = 1
b = 2

3th iteration

a = 2
b = 3

while i want

Code: [Select]
1th iteration

a = 0
b = 1

2th iteration

a = 2
b = 3


Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 22, 2015, 01:59:39 PM
i want
Code: [Select]
1th iteration

a = 0
b = 1

2th iteration

a = 2
b = 3

Use something like:

Code - Auto/Visual Lisp: [Select]
  1. (defun foo ( l )
  2.     (while (cadr l)
  3.         (princ (strcat "\nSegment length: " (rtos (distance (car l) (cadr l)))))
  4.         (setq l (cddr l))
  5.     )
  6.     (princ)
  7. )
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 22, 2015, 02:10:12 PM
Why mapcar wasnt your defualt choise for thos task?
Title: Re: iteration code : short and efficiency challenge
Post by: Lee Mac on July 22, 2015, 02:30:07 PM
Why mapcar wasnt your defualt choise for thos task?

Because mapcar iterates over every item; you are asking to iterate over every other item.
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 23, 2015, 12:31:46 AM
Why mapcar wasnt your defualt choise for thos task?

Because mapcar iterates over every item; you are asking to iterate over every other item.

Thanks Lee (thats what i thought but i was sure that i wrong)

put it all together, i see in the Watch that the list tems are removed before the interpeter even get there and there for i cant get the return that i want

Code: [Select]
;_divids  length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval

(defun measurex ( s e i / a r )
    (setq r (list s)
          a (angle s e)
    )
    (repeat (fix (/ (distance s e) i))
        (setq r (cons (polar (car r) a i) r))
    )
)

;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts

(defun dividex ( s e i / a r sl)
    (setq r (list s)
          a (angle s e)
  sl (/ (distance s e) i) ;
    )
    (repeat (fix i)
        (setq r (cons (polar (car r) a sl) r))
    )
 
  )

;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the sprcified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ang twc rm rwl rwc  PTLIST)
  (if (and (>= typ mn) (<= typ mx))                                                      ;; if typ is greater/smaller or equal to min/max
    (if (/= (distance (setq stp (cadr (setq PTLIST (measurex stp etp typ)))) etp) typ );; if ther is reminder
      (append (vl-remove (car PTLIST) PTLIST) (dividex stp etp 2))
       PTLIST
  )
        )
   
  )
   

the list i expect to get

Code: [Select]
(getTypicalLength '(0.0 0.0 0.0)'(20.0 0.0 0.0) 6.0 5.0 8.0)

((20.0 0.0 0.0)(16.0 0.0 0.0) (12.0 0.0 0.0) (6.0 0.0 0.0) (0.0 0.0 0.0))

Thanks
Shay
Title: Re: iteration code : short and efficiency challenge
Post by: roy_043 on July 23, 2015, 04:45:20 AM
When you use append in your 3rd function the new start point is still in the list of points.
The rest you should be able to figure out yourself.

Note:
Please don't use this:
Code: [Select]
(vl-remove (car PTLIST) PTLIST)Instead of this:
Code: [Select]
(cdr PTLIST)
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 23, 2015, 08:25:55 AM
When you use append in your 3rd function the new start point is still in the list of points.
The rest you should be able to figure out yourself.

Note:
Please don't use this:
Code: [Select]
(vl-remove (car PTLIST) PTLIST)Instead of this:
Code: [Select]
(cdr PTLIST)

Code: [Select]
;_divids  length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval

(defun measurex ( s e i / a r )
    (setq r (list s)
          a (angle s e)
    )
    (repeat (fix (/ (distance s e) i))
        (setq r (cons (polar (car r) a i) r))
    )
)

;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts

(defun dividex ( s e i / a r sl)
    (setq r (list s)
          a (angle s e)
  sl (/ (distance s e) i) ;
    )
    (repeat (fix i)
        (setq r (cons (polar (car r) a sl) r))
    )
 
  )

;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the sprcified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ang twc rm rwl rwc  PTLIST)
  (if (and (>= typ mn) (<= typ mx))                                                      ;; if typ is greater/smaller or equal to min/max
    (if (/= (distance (setq stp (cadr (setq PTLIST (measurex stp etp typ)))) etp) typ );; if the distance between past last point to the end is different than typ
     ;; it means that there is a remaining
     

      (reverse (append  (dividex stp etp 2)(cddr PTLIST)))
      (reverse PTLIST)
  )
        )
   
  )


   

Code: [Select]
_$ (getTypicalLength '(0.0 0.0 0.0)'(20.0 0.0 0.0) 6.0 5.0 8.0)
Code: [Select]
((0.0 0.0 0.0) (6.0 0.0 0.0) (12.0 0.0 0.0) (16.0 0.0 0.0) (20.0 0.0 0.0))
Thanks roy_43
Title: Re: iteration code : short and efficiency challenge
Post by: Shay Gaghe on July 25, 2015, 12:05:10 AM
The code is measuring specified distance by specified segment length typ  and return resulting 3d point list,
If a reminder left over typ+reminder, its being tested for its length:
If the left over segment length is smaller or equal to maximum , remove the last point and add the end point to the list, return resulting list.
If the left over segment length is greater than the maximum limit, divide its length into I , and try to pass the test again.
If the left over segment is smaller than minimum than prompt the user and exit.

How can I code it more efficient , shorter , stable?

Thanks
Shay

Code: [Select]
;_divids  length between 2 points by interval
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - interval

(defun measurex ( s e i / a r )
    (setq r (list s)
          a (angle s e)
    )
    (repeat (fix (/ (distance s e) i))
        (setq r (cons (polar (car r) a i) r))
    )
)

;_divids length between 2 points by segment count
;_retval - a coordinate list
;_s - start point
;_e_endpoint
;_i - segment counts

(defun dividex ( s e i / a r sl)
    (setq r (list s)
          a (angle s e)
  sl (/ (distance s e) i) ;
    )
    (repeat (fix i)
        (setq r (cons (polar (car r) a sl) r))
    )
 
  )

;_measure segments at a specidied length
;_if there is a reminder, substract the last coordinate from the list
;_add the reminder to the specified length
;_divide it equaly into 2
(defun getTypicalLength (stp etp typ mn mx / dist ml i w f )
  (setq i 1)
  (if (and (> typ mn) (< typ mx)(>= (distance stp etp) typ))                                                     
    (if (/= (setq dist (distance (setq stp (cadr (setq ml (measurex stp etp typ)))) etp)) typ ) ;_if there is a reminder
(while (null f)             ;_flag.is there a need to loop? t=no nil=yes
  (if (>= dist mn)     ;_is greater than minimum limit
    (if (<= dist mx)     ;_is smaller than maxiuum limit
      (if (null w)     ;_flag. segment were divided?. t=no nil-yes
  (setq f t ml (cdr ml) ml (cons etp ml))     ;_flag. no loop is needed. return list+end point
  (progn
    (setq f t)     ;_flag. no loop is needed.
    (append (reverse(cdr ml))(cdr(reverse(dividex stp etp i))))     ;_return list+divid seg+reminder equally
    )
      )

     
(setq i (1+ i) dist (/ (distance stp etp)i) w t)    ;_divid seg when it doesnt meet maximum length, start over

   
  )
    (princ "Cannot constract a layout with those limits")    ;_stop when seg length doesnt meet minimum length
         )
               )
       ml    ;_return list when no reminder left
             )
    )
  )