I need correction in function given by Hak_vz

I need correction in function given by Hak_vz

avinash00002002
Collaborator Collaborator
830 Views
14 Replies
Message 1 of 15

I need correction in function given by Hak_vz

avinash00002002
Collaborator
Collaborator

My list is 

List = (25.0 164.0 137.0 0.0 132.5 158.5 181.5 0.0 192.0 0.0 204.5 0.0 215.5 183.5 75.5 82.5 25.0)

After running of this function is

Result = (4.0 21.4615 17.9282 4.0 17.3393 20.7417 23.7516 4.0 25.1257 4.0 26.7614 4.0 28.2009 24.0133 9.88014 10.7962 4.0)

 

I need correction is that where 0.0 there required also in 0.0 in result and summation is always as 230.

I have made some correction but result is not coming as I required.

Code:

(defun plus_minus (lst fitting) ; /  plusp set_nth i sum_list k co cu su so e m p)
  (defun plusp (num) (cond ((numberp num) (>= num 0.0))))
  (defun set_nth (lst n value)
    (cond
    ((and (plusp n) (<= n (length lst)))
      (cond
       ((zerop n)
       (cons value (cdr lst))
      )
      (t (cons (car lst) (set_nth  (cdr lst) (1- n) value)))
      )
    )
    )
  )
 
  (setq
    i -1
    sum_list (apply '+ lst)
    k (/ (float fitting) sum_list)
    cu 0
    co 0
    su 0
    so 0
  )
  (while (< (setq i (1+ i)) (length lst))
    (setq e (* k (nth i lst)))
    (if (< e 4.0)
      (setq su (+ su e) cu (1+ cu))
      ;(setq so (+ so e) co (1+ co))
      (setq so 0)
    )
    (setq lst (set_nth lst i e))
  )
  (setq m (- fitting (* cu 4.0)))
  (setq p (/ m (* (/ 1.0 k) so)))
  (setq i -1)
  (while (< (setq i (1+ i)) (length lst))
    (setq e (nth i lst))
    (if (<= e 4.0)
      (setq lst (set_nth lst i 4.0))
      (setq lst (set_nth lst i (/ (* e p) k)))
    )
  )
  lst
)

 

0 Likes
831 Views
14 Replies
Replies (14)
Message 2 of 15

john.uhden
Mentor
Mentor

Your topic implies that @hak_vz's function was erroneous.  That' hard to believe as his work is almost always perfect.

I get the feeling that instead of trying to learn to fix your own mistakes you are counting on us to fix your mistakes for you.  Don't expect to get much help if that's the way you are going to treat this forum and its contributors.

I am getting seriously sick, sick, sick of "I need" "I need" "I need."

John F. Uhden

Message 3 of 15

avinash00002002
Collaborator
Collaborator

I never used this function is erroneous. What I said is correction require for me. 

0 Likes
Message 4 of 15

hak_vz
Advisor
Advisor

@avinash00002002 

Code provided is correct and in accordance to your original request where it was told that after calculation  any value less than 4 to be replaced with 4 (minimal scaled distance). And you didn't mention that distance can be 0. I understand that zero distance comes from situation when you have two or more hole at the same  vertical. But I had to figure out by myself from your other post that followed this request.

Since you've started learning autolisp, at least you told that in other post, try to figure out what has to be changed to take into account zero distances.  It's only one simple IF statement at the bottom of the function provided.

 

To be clear, your mathematical solution posted in original request was brilliant and code is made according to your lets call it pseudo code. 

Try to make changes to the code and post it here, and if you need some other changes write them down. It's really hard to restart working on code where I nearly forgot what it was all about.

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
Message 5 of 15

avinash00002002
Collaborator
Collaborator

I have tried to put a IF function at the bottom but result is coming 0 0 0 0 0 0 0.

so, I tried other way

This is my list

(25.0 0.0 0.0 82.5 0.0 0.0 75.5 0.0 0.0 183.0 0.0 0.0 215.0 0.0 27.5 0.0 177.0 0.0 0.0 192.0 181.5 158.5 132.5 301.0 25.0)

I found which Nth has 0.0 by

(setq dlst1 (atom_nth distlist 0.0)) ;0.0 nth position in the list
then
(setq dlst (vl-remove-if (function (lambda ( x ) (equal x 0.0))) distlist)) ;Removal of 0.0 from list
;Scaling
(setq lst1  (mapcar '(lambda (x) (* sc x))  dlst))
(setq SDist (plus_minus lst1 SLg)) ;plot distance
 
Now adding 0.0 to the new list
(setq iCnt 0)
  (setq newlst sdist)
  (repeat (length dlst1)
    (setq newlst (LM:insertnth 0.0 (nth iCnt dlst1) newlst))
  (setq icnt (+ 1 iCnt))
)
 
This way I have changed.
 
0 Likes
Message 6 of 15

hak_vz
Advisor
Advisor

@avinash00002002  Try this code. 

(defun plus_minus (lst fitting /  plusp set_nth i sum_list k co cu su so e m p)
	(defun plusp (num) (cond ((numberp num) (>= num 0.0))))
	(defun set_nth (lst n value)
	  (cond 
		((and (plusp n) (<= n (length lst)))
		  (cond
		   ((zerop n)
			 (cons value (cdr lst))
			)
			(t (cons (car lst) (set_nth  (cdr lst) (1- n) value)))
		  )
		)
	  )
	)
	(setq 
		i -1 
		sum_list (apply '+ lst)
		k (/ (float fitting) sum_list)
		cu 0
		co 0
		su 0
		so 0
	)
	(while (< (setq i (1+ i)) (length lst))
		(setq e (* k (nth i lst)))
		(if (and (> e 0.0)(< e 4.0)) 
			(setq su (+ su e) cu (1+ cu))
			(setq so (+ so e) co (1+ co))
		)
		(setq lst (set_nth lst i e))
	)
	(setq m (- fitting (* cu 4.0)))
	(setq p (/ m (* (/ 1.0 k) so)))
	(setq i -1)
	(while (< (setq i (1+ i)) (length lst))
		(setq e (nth i lst))
		(cond
			((and(> e 0.0)(< e 4.0))(setq lst (set_nth lst i 4.0))) 
			((>= e 4.0)(setq lst (set_nth lst i (/ (* e p) k))))
		)
	)	
	lst
)
(setq lst '(25.0 164.0 137.0 0.0 132.5 158.5 181.5 0.0 192.0 0.0 204.5 0.0 215.5 183.5 75.5 82.5 25.0))
Command: (setq b (plus_minus lst 230))
(4.0 21.0816 17.6109 0.0 17.0324 20.3746 23.3312 0.0 24.6809 0.0 26.2878 0.0 27.7018 23.5883 9.70527 10.6051 4.0)
Command: (apply '+ b)
230.0

Or for different length (let say 877)

Command: (setq b (plus_minus lst 877))
(12.3382 80.9387 67.6134 0.0 65.3925 78.2243 89.5754 0.0 94.7575 0.0 100.927 0.0 106.355 90.5625 37.2614 40.7161 12.3382)
Command: (apply '+ b)
877.0

In some of your other requests you also asked for final values rounded to 0.5.  This code is not so complicated, and you have also been provided with code that rounds values and keeps correct total sum. As an exercise,  try to join this two code to one.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 7 of 15

avinash00002002
Collaborator
Collaborator

Thanks for reply,

 

I am trying since early morning to modify the list where double block inserted in same point, I have to ignore/delete from list.

Sample:

list -1 for Y-dimensions

(50.0 50.0 50.0 50.0 50.0 50.0 50.0) = bmlst

List-2 for Cumulative dimensions

(50.0 50.0 156.5 238.0 238.0 288.0 388.0) = cumor

list-3 for block to block dimension

(50.0 0.0 106.5 81.5 0.0 50.0 100.0) = distlist

list-4 block name list

("H2" "H2" "H2" "H2" "H2" "H2") = blklst

 

From above list image that we have a 6 nos. of blocks but actually 3 block are there and other 3 blocks are at same point. I want to delete over drawn block from above lists.

I tried since early morning but all list answer came wrong (50.0 50.0 50.0 50.0 50.0 50.0 50.0) and (0.0 0.0 0.0 0.0 0.0 0.0 0.0) and (0.0 106.5 81.5 50.0 100.0) and ("H2" "H2" "H2" "H2" "H2" "H2").

 

Code: 

  (setq cnthl 0)
  (setq nohnew 0)
  (repeat noh
     (if (and (= (nth cnthl bmlst) (nth (+ 1 cnthl) bmlst)) (= (nth cnthl cumor) (nth (+ 1 cnthl) cumor)))
     (setq nohnew (+ 1 nohnew))
    )      
           (setq cnthl (+ 1 cnthl))
  )
  (setq nohnew noh)
  (setq cnthl1 0)
  (setq cumorb1 (list (nth 0 cumor)))
  (setq blklstb1 (list (nth 0 blklst)))
  (setq bmlstb1 (list (nth 0 bmlst)))
  ;(setq distlistb1 (list (nth 0 distlist)))
  (repeat noh
     (if (and (= (nth cnthl1 bmlst) (nth (+ 1 cnthl1) bmlst)) (= (nth cnthl1 cumor) (nth (+ 1 cnthl1) cumor)))
       (progn
         (setq distlist (LM:RemoveNth cnthl1 distlist))
         (alert (strcat "DistList =  " (rtos (nth cnthl1 distlist) 2 1)))
        (setq cumorb1 (append cumorb1 (list (nth cnthl1 cumor))))
        (setq blklstb1 (append blklstb1 (list (nth cnthl1 blklst))))
        (setq bmlstb1 (append bmlstb1 (list (nth cnthl1 bmlst))))
        (setq distlistb1 (append distlistb1 (list (nth cnthl1 distlist))))
     )
       )
     
    ;(if (and (/= (nth cnthl1 bmlst) (nth (+ 1 cnthl1) bmlst)) (= (nth cnthl1 cumor) (nth (+ 1 cnthl1) cumor)))
    ;     (setq distlistb1 (append distlistb1 (list (nth cnthl1 distlist))))
    😉
    (setq cnthl1 (+ 1 cnthl1))
  )

 

0 Likes
Message 8 of 15

hak_vz
Advisor
Advisor

Working with separate list of x and y coordinates plus block name rises algorithm complexity and resulting code is not easy to follow. Elements in your lists are not sorted so checking if nth element is equal to nth+1 won't create correct result. Your code is hard to read and I don't have time to spent.

If you would have a list defined as follows I guess you would have more options 

'(((x1 y1) "namex")((x2 y2) "namex")((x3 y3) "namex")...)

 

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 9 of 15

avinash00002002
Collaborator
Collaborator

Hi!

Thanks for reply,

 

(plus_minus lst1 250.0)

Some times this function gives less than 4.0 mm. any suggestion. please

 

my list is 

 

(40.0 70.0 70.0 165.0 159.0 32.5 43.5 12.5 43.5 4.5 79.0 0.0 35.0 0.0 91.0 4.5 35.0 35.0 0.0 34.0 29.0 6.0 30.0 33.0 153.5 900.0 711.0 70.0 70.0 49.0 800.0 841.5 75.0 13.0 62.0 1.0 42.0 28.0 62.0 21.5 23.5 27.0 10.0 25.0 20.0 548.0 99.5 0.0 695.5 46.5 53.5 84.0 0.0 0.0 66.0 10.0 45.5 394.5 140.0 55.0 55.0 55.0 55.0 40.0)

 

amd ans =

 

(4.0 4.0 4.0 1.8573 1.78976 4.0 4.0 4.0 4.0 4.0 4.0 0.0 4.0 0.0 4.0 4.0 4.0 4.0 0.0 4.0 4.0 4.0 4.0 4.0 1.72785 10.1307 8.00327 4.0 4.0 4.0 9.00508 9.47222 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 6.16848 4.0 0.0 7.82879 4.0 4.0 4.0 0.0 0.0 4.0 4.0 4.0 4.44063 1.57589 4.0 4.0 4.0 4.0 4.0)

 

 

0 Likes
Message 10 of 15

hak_vz
Advisor
Advisor

I'm not sure that my original code is prone to that kind of error, but will have to look later today. Sorry, but with all this various posts and requests, it is really hard to follow.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 11 of 15

avinash00002002
Collaborator
Collaborator

Any suggestion please.

0 Likes
Message 12 of 15

avinash00002002
Collaborator
Collaborator

I am trying but no sucess.

0 Likes
Message 13 of 15

hak_vz
Advisor
Advisor

Code is wrong and it needs to be written again.

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes
Message 14 of 15

avinash00002002
Collaborator
Collaborator

Can u give me solution?

0 Likes
Message 15 of 15

hak_vz
Advisor
Advisor

 Code is working properly but when you use long list like this with combinations of lots of small values and few large ones, all small values when scaled receive value less than 4.0. and are changed to 4.0. 

For example you can have for your list:

Command: (setq c(plus_minus lst 250))
(4.0 4.0 4.0 1.8573 1.78976 4.0 4.0 4.0 4.0 4.0 4.0 0.0 4.0 0.0 4.0 4.0 4.0 4.0 0.0 4.0 4.0 4.0 4.0 4.0 1.72785 10.1307 8.00327 4.0 4.0 4.0 9.00508 9.47222 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 4.0 6.16848 4.0 0.0 7.82879 4.0 4.0 4.0 0.0 0.0 4.0 4.0 4.0 4.44063 1.57589 4.0 4.0 4.0 4.0 4.0)
Command: (setq d(plus_minus lst 1000))
(5.17586 9.05775 9.05775 21.3504 20.574 4.20538 5.62874 4.0 5.62874 4.0 10.2223 0.0 4.52887 0.0 11.7751 4.0 4.52887 4.52887 0.0 4.39948 4.0 4.0 3.88189 4.27008 19.8623 116.457 92.0008 9.057

Code is created according to your algorithm and that is all how I can help. You are doing  this work from day to day and for me this is still a mystery.

 

Miljenko Hatlak

EESignature

Did you find this post helpful? Feel free to Like this post.
Did your question get successfully answered? Then click on the ACCEPT SOLUTION button.
0 Likes