Author Topic: Range Function  (Read 9757 times)

0 Members and 1 Guest are viewing this topic.

LE3

  • Guest
Re: Range Function
« Reply #15 on: November 17, 2013, 12:06:42 PM »
Have you seen the work of Sergey Bochkanov ALGLIB Project --- for the LMA - give it a read/try:

http://www.alglib.net/optimization/levenbergmarquardt.php

ymg

  • Guest
Re: Range Function
« Reply #16 on: November 17, 2013, 12:19:56 PM »
Quote
Have you seen the work of Sergey Bochkanov ALGLIB Project --- for the LMA - give it a read/try:


Thanks Luis,

Yes I did see it and it is well done.

Right now concentrating to get something going, so I am translating that Python implementation.

After that with help from the gurus here maybe optimize it and then includes those nice to have like
so called  geodesic acceleration etc.

But we are in the wrong thread.

ymg

ymg

  • Guest
Re: Range Function
« Reply #17 on: November 18, 2013, 01:51:36 AM »
Here is the latest, using Cab's offering with a little modification.

Mind you we still have a divide by zero error if step is set at zero,
may be we should wrap the function with a cond.

Also get a bad return at (in_range 0 10 3), so maybe the logic behind subtracting 1 from (fix (/ (- end start) step))
is not correct.

ymg

Code - Auto/Visual Lisp: [Select]
  1. (defun in_range (start end step / value)
  2.   (setq value (list start))
  3.     (repeat (- (fix (/ (- end start) step)) 1)
  4.       (setq value (cons (setq start (+ step start)) value))
  5.     )
  6.   )
  7. )
  8.  

So we modify like so:

Code - Auto/Visual Lisp: [Select]
  1. (defun in_range (start end step / value)
  2.   (setq value (list start))
  3.   (repeat (fix (/ (- end start) step))
  4.      (setq value (cons (setq start (+ step start)) value))
  5.   )
  6.   (reverse (if (= (car value) end) (cdr value) value))
  7. )
  8.  

Returns from autolisp/ python:

Code - Auto/Visual Lisp: [Select]
  1. $ (in_range 0 10 1)
  2.    (0 1 2 3 4 5 6 7 8 9)
  3. >>> range(10)
  4. [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
  5.  
  6. $ (in_range 1 11 1)
  7. (1 2 3 4 5 6 7 8 9 10)
  8. >>> range(1, 11)
  9. [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  10.  
  11. $ (in_range 0 30 5)
  12. (0 5 10 15 20 25)
  13. >>> range(0, 30, 5)
  14. [0, 5, 10, 15, 20, 25]
  15.  
  16. $ (in_range 0 10 3)
  17. (0 3 6 9)        
  18. >>> range(0, 10, 3)
  19. [0, 3, 6, 9]
  20.  
  21. $ (in_range 0 -10 -1)
  22. (0 -1 -2 -3 -4 -5 -6 -7 -8 -9)
  23. >>> range(0, -10, -1)
  24. [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
  25.  
  26. $ (in_range 0 0 1)
  27. nil
  28. >>> range(0)
  29. []
  30. >>> range(1, 0)
  31. []
  32.  
  33.  
« Last Edit: November 18, 2013, 03:06:35 AM by ymg »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Range Function
« Reply #18 on: November 18, 2013, 02:58:58 AM »
recursion way

Code - Auto/Visual Lisp: [Select]
  1. (defun range (s e i)
  2.   (if (< s e)
  3.     (cons s (range (+ i s) e i))
  4.   )
  5. )
:-)

ymg

  • Guest
Re: Range Function
« Reply #19 on: November 18, 2013, 03:12:21 AM »
Quote
recursion way

Best one!!! by a tall margin.

ymg

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Range Function
« Reply #20 on: November 18, 2013, 03:16:31 AM »
Quote
recursion way

Best one!!! by a tall margin.

ymg

Thank you!

roy_043

  • Water Moccasin
  • Posts: 1895
  • BricsCAD 18
Re: Range Function
« Reply #21 on: November 18, 2013, 04:24:31 AM »
@ ElpanovEvgeniy:
Your function is nice and short but:
Code: [Select]
(EE_range 0 -10 -1) => nil

ymg

  • Guest
Re: Range Function
« Reply #22 on: November 18, 2013, 04:44:13 AM »
Quote
@ ElpanovEvgeniy:
Your function is nice and short but:
Code: [Select]
(EE_range 0 -10 -1) => nil

Ooops! ,  So then we have:

Code - Auto/Visual Lisp: [Select]
  1. ;;  By Cab      (iterative)                                                   ;
  2. ;;                                                                            ;
  3. (defun in_range (start end step / value)
  4.   (setq value (list start))
  5.   (repeat (fix (/ (- end start) step))
  6.      (setq value (cons (setq start (+ step start)) value))
  7.   )
  8.   (reverse (if (= (car value) end) (cdr value) value))
  9. )
  10.  

But we  still haven't addressed if there is any merit to such construct as
  (foreach  i  (in_range 0 10 1) ....

ymg

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Range Function
« Reply #23 on: November 18, 2013, 05:34:24 AM »
@ ElpanovEvgeniy:
Your function is nice and short but:
Code: [Select]
(EE_range 0 -10 -1) => nil

Thank you, I corrected!

Code - Auto/Visual Lisp: [Select]
  1. (defun range (s e i)
  2.   (if (or (and (> i 0) (< s e)) (and (< i 0) (> s e)))
  3.     (cons s (range (+ i s) e i))
  4.   )
  5. )
:-)

ymg

  • Guest
Re: Range Function
« Reply #24 on: November 18, 2013, 06:22:04 AM »
No matter how I try, I don't have the recursive bone in me.

Haven't figured how the first one work, now need to wrap my head around the corrected one.

Still the Best One!!

But, Evgeniy what do you think of construct like the one proposed???

Quote
But we  still haven't addressed if there is any merit to such construct as
  (foreach  i  (in_range 0 10 1) ....

ymg

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Range Function
« Reply #25 on: November 18, 2013, 06:36:27 AM »
(foreach i (in_range 0 10 1) [I do not understand what needs to be here] ....

give any example...

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Range Function
« Reply #26 on: November 18, 2013, 06:47:12 AM »
An iterative solution perhaps:
Code - Auto/Visual Lisp: [Select]
  1. (defun range ( s e i / l )
  2.     (repeat (fix (abs (/ (- e s) i)))
  3.         (setq e (- e i) l (cons e l))
  4.     )
  5. )

EDIT: spoke too soon...
Code - Auto/Visual Lisp: [Select]
  1. _$ (range 0 10 3)
  2. (1 4 7)

Lee Mac

  • Seagull
  • Posts: 12905
  • London, England
Re: Range Function
« Reply #27 on: November 18, 2013, 06:55:37 AM »
A quick fix, but not great  :-(

Code - Auto/Visual Lisp: [Select]
  1. (defun range ( s e i / l )
  2.     (if (< 0 (abs (rem e i)))
  3.         (setq e (+ e (- i (rem e i))))
  4.     )
  5.     (repeat (fix (abs (/ (- e s) i)))
  6.         (setq e (- e i) l (cons e l))
  7.     )
  8. )

ymg

  • Guest
Re: Range Function
« Reply #28 on: November 18, 2013, 07:01:45 AM »
Quote
(foreach i (in_range 0 10 1) [I do not understand what needs to be here] ....

Here is a stupid one:

Code: [Select]
(foreach i (in_range 0 10 1)
    (setq tot (+ tot i))
)

In other word we do not need to increment the index to the loop.

The python boys use it at every sauce.

ymg
« Last Edit: November 18, 2013, 07:11:10 AM by ymg »

ElpanovEvgeniy

  • Water Moccasin
  • Posts: 1569
  • Moscow (Russia)
Re: Range Function
« Reply #29 on: November 18, 2013, 09:11:00 AM »
Here is a stupid one:

Code: [Select]
(foreach i (in_range 0 10 1)
    (setq tot (+ tot i))
)

In other word we do not need to increment the index to the loop.

The python boys use it at every sauce.

ymg

Code - Auto/Visual Lisp: [Select]
  1. (defun range (s e i f)
  2.   (if (or (and (> i 0) (< s e)) (and (< i 0) (> s e)))
  3.     (progn (f s) (range (+ i s) e i f))
  4.   )
  5. )

test:  corrected  (test 0 10 1 0) >> (test 0 10 1 55)
Code - Auto/Visual Lisp: [Select]
  1. (defun test (s e i tot)
  2.    (range s e i (lambda (a) (setq tot (+ tot a))))
  3.    tot
  4. )
  5.  
  6. (test 0 10 1 0) ; >> 45
  7. (test 0 10 1 55) ; >> 100
« Last Edit: November 18, 2013, 10:47:54 AM by ElpanovEvgeniy »