Author Topic: iteration code : short and efficiency challenge  (Read 13716 times)

0 Members and 1 Guest are viewing this topic.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #15 on: July 18, 2015, 06:12:47 AM »
Hi Lee. THANX. how do you get the time report?

I use MP's excellent Benchmark utility.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #16 on: July 19, 2015, 03:30:38 AM »
Hi Lee. THANX. how do you get the time report?

I use MP's excellent Benchmark 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


Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #17 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.
« Last Edit: July 19, 2015, 04:20:58 AM by Kerry »
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #18 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
« Last Edit: July 19, 2015, 05:07:02 AM by Shay Gaghe »

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #19 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.

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #20 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. :)

kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #21 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

Kerry

  • Mesozoic relic
  • Seagull
  • Posts: 11654
  • class keyThumper<T>:ILazy<T>
Re: iteration code : short and efficiency challenge
« Reply #22 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)
)
kdub, kdub_nz in other timelines.
Perfection is not optional.
Everything will work just as you expect it to, unless your expectations are incorrect.
Discipline: None at all.

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #23 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:

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #24 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>

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: iteration code : short and efficiency challenge
« Reply #25 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))

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #26 on: July 19, 2015, 08:14:25 AM »
Thanks Kerry and Lee.
this is what i been after by writing this post.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #27 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.

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #28 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

Shay Gaghe

  • Newt
  • Posts: 89
Re: iteration code : short and efficiency challenge
« Reply #29 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>