Author Topic: Which Way is better to use in equation?  (Read 3440 times)

0 Members and 1 Guest are viewing this topic.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Which Way is better to use in equation?
« on: June 11, 2012, 11:12:18 AM »
Eq1: I make VAR for each step
Eq2: is less steps
Both give the same result
So, Which one is better to use?

Eq1
Code: [Select]
(setq Dst2 (/ Dist 2)
Dst1 (* 0.1 Dist)
S    (* 0.8 Dist)
SS   (* S S)
SS2  (/ SS 2)
SS4  (/ SS 4)
S42  (- SS4 SS2)
X1   (* idx step)
X    (- (* idx step) Dst1)
XX   (* X X)
Lc   (- *L* *c*)
Lc1  (* Lc 0.1)
LcD  (* Lc1 Dist)
a1   (/ LcD Dst2)
ys1  (- *L* a1)
yc   (- *c* ys1)
A    (/ yc S42)
AS   (* A S)
B    (- AS)
XXA  (* A XX)
XB   (* B X)
XAB  (+ XXA XB)
y    (+ XAB ys1)
yt   (rtos (round y 5) 2 0)
  )

Eq2
Code: [Select]
(setq X    (- (* idx step) (* 0.1 Dist))
A    (/ (- *c* (- *L* (/ (* (* (- *L* *c*) 0.1) Dist) (/ Dist 2))))
(- (/ (* (* 0.8 Dist) (* 0.8 Dist)) 4) (/ (* (* 0.8 Dist) (* 0.8 Dist)) 2)))
B    (- (* A (* 0.8 Dist)))
XB   (* B X)
yt (rtos (round (+ (+ (* A (* X X)) (* B X)) (- *L* (/ (* (* (- *L* *c*) 0.1) Dist) (/ Dist 2)))) 5) 2 0)
)

Lee Mac

  • Seagull
  • Posts: 12914
  • London, England
Re: Which Way is better to use in equation?
« Reply #1 on: June 11, 2012, 11:32:07 AM »
Untested, but I think the whole thing could be simplified to:

Code - Auto/Visual Lisp: [Select]
  1. (setq X  (- (* idx step) (* 0.1 Dist))
  2.       C  (- *L* (* (- *L* *c*) 0.2))
  3.       A  (/ (- *c* C) (* -0.16 Dist Dist))
  4.       yt (rtos (round (+ (* A X X) (* -0.8 A X Dist) C) 5) 2 0)
  5. )

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Which Way is better to use in equation?
« Reply #2 on: June 11, 2012, 11:48:36 AM »
There's no right answer. It depends on what you want to do.
My rule of thumb is: If you only use the value once somewhere else, then rather just calculate it where it's needed. If you use it 2 or more times somewhere else, then it's more efficient to save it into a variable and reference it in the other places. In such case it's between speed efficiency and memory use. If you only use the variable once, then there's no speed efficiency gain, but you're using extra RAM for no reason.

E.g. in your 1st codes: S42 is only used once to calculate A. But to get to S42 you used SS twice. So I'd omit S42, S44 & S22. Take this as a sample though, there are many others with similar efficiency gains / minimizing unneeded RAM use.

The only possible reason I'd consider to make a variable where it's only going to be used once - is if I'd like to use it to make the code more readable. Though that's very seldom the case.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Which Way is better to use in equation?
« Reply #3 on: June 11, 2012, 11:53:09 AM »
Untested, but I think the whole thing could be simplified to:

Code - Auto/Visual Lisp: [Select]
  1. (setq X  (- (* idx step) (* 0.1 Dist))
  2.       C  (- *L* (* (- *L* *c*) 0.2))
  3.       A  (/ (- *c* C) (* -0.16 Dist Dist))
  4.       yt (rtos (round (+ (* A X X) (* -0.8 A X Dist) C) 5) 2 0)
  5. )
I was not asking about simplifying the equation, but I found your way is very good and I'll replace mine with yours when test and get the same results.
As usual awesome.

My question is which way is better to make a lot of variables or making a long equation with less VAR

Lets say
Eq1 (LEE)
Code: [Select]
(setq X  (- (* idx step) (* 0.1 Dist))
      C  (- *L* (* (- *L* *c*) 0.2))
      A  (/ (- *c* C) (* -0.16 Dist Dist))
      yt (rtos (round (+ (* A X X) (* -0.8 A X Dist) C) 5) 2 0)
)
Eq2
Code: [Select]
(setq yt (rtos (round (+ (* (/ (- *c* (- *L* (* (- *L* *c*) 0.2))) (* -0.16 Dist Dist))
    (- (* idx step) (* 0.1 Dist)) (- (* idx step) (* 0.1 Dist)))
(* -0.8 A (- (* idx step) (* 0.1 Dist)) Dist) (- *L* (* (- *L* *c*) 0.2)))
      5) 2 0)
      )

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Which Way is better to use in equation?
« Reply #4 on: June 11, 2012, 11:56:32 AM »
There's no right answer.
...
Thanks irneb I got it

zoltan

  • Guest
Re: Which Way is better to use in equation?
« Reply #5 on: June 12, 2012, 09:20:59 AM »
Code: [Select]
(setq   yt   (rtos (round (+ (+ (* (/ (- *c* (- *L* (/ (* (* (- *L* *c*) 0.1) Dist) (/ Dist 2)))) (- (/ (* (* 0.8 Dist) (* 0.8 Dist)) 4) (/ (* (* 0.8 Dist) (* 0.8 Dist)) 2))) (* (- (* idx step) (* 0.1 Dist)) (- (* idx step) (* 0.1 Dist)))) (* (- (* (/ (- *c* (- *L* (/ (* (* (- *L* *c*) 0.1) Dist) (/ Dist 2)))) (- (/ (* (* 0.8 Dist) (* 0.8 Dist)) 4) (/ (* (* 0.8 Dist) (* 0.8 Dist)) 2))) (* 0.8 Dist))) (- (* idx step) (* 0.1 Dist)))) (- *L* (/ (* (* (- *L* *c*) 0.1) Dist) (/ Dist 2)))) 5) 2 0)

This is clearly the right answer!  :lmao:

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Which Way is better to use in equation?
« Reply #6 on: June 12, 2012, 10:03:57 AM »
Code: [Select]
(setq   yt   ...)

This is clearly the right answer!  :lmao:
WHat  :evil: you are

LibertyOne

  • Guest
Re: Which Way is better to use in equation?
« Reply #7 on: June 12, 2012, 11:14:46 AM »
This seems to be a quite lengthly equation. Why not create a function for the equation?

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Which Way is better to use in equation?
« Reply #8 on: June 12, 2012, 11:27:38 AM »
The only real issue with defining variables is that lisp by default makes all variables global in nature unless you specify them to be local. This can create a problem across multiple functions. For example:

Code: [Select]
(defun foo ()
 (setq a 10)
)

(defun bar ()
  (princ a)
)

For simple functions it is readily apparent, however as functions grow in size and scope, it may pick up an inadvertent value .. for example:

Code: [Select]
(defun foo ()
 (setq a 10)
)

(defun bar ()
  (princ a)
)

(defun test()
 (setq a 42)
 (foo)
 (bar)
)

As you can see the result isn't what one might expect after looking at the code for test.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie

HasanCAD

  • Swamp Rat
  • Posts: 1422
Re: Which Way is better to use in equation?
« Reply #9 on: June 12, 2012, 11:37:31 AM »
This seems to be a quite lengthly equation. Why not create a function for the equation?
I did. This is the code

irneb

  • Water Moccasin
  • Posts: 1794
  • ACad R9-2016, Revit Arch 6-2016
Re: Which Way is better to use in equation?
« Reply #10 on: June 12, 2012, 11:56:41 AM »
That might be another reason to keep the number of variables down: So you don't have to include so many localized variables.

BTW, does anyone know if there's a lot of efficiency loss to assign a value to a variable through setq as opposed to simply using the value in a calculation? I'd imagine there's a very small amount of extra time to set a symbol and then use it as reference instead of simply using the value in place of the reference. Perhaps not much of a reason in that code though, since it only does the calculation after user interaction - but could make for slower running code if in a loop.
Common sense - the curse in disguise. Because if you have it, you have to live with those that don't.

Keith™

  • Villiage Idiot
  • Seagull
  • Posts: 16899
  • Superior Stupidity at its best
Re: Which Way is better to use in equation?
« Reply #11 on: June 12, 2012, 02:14:39 PM »
May years ago I read some development notes on lisp and the original concept didn't include variables because it was an interpreted language. Variables were added in the original development because it was determined that it was extremely useful to have them, for example in passing values between functions, also the fact that functions themselves are actually variables makes it hard to argue against using them.

I generally only use variables where required by the constraints of code. Where possible, I compartmentalize functions for efficiency and usability and limit the variables to those whose scope is readily useful in other portions of the function. I don't use globals at all (well except for the functions themselves), due to the problem I indicated previously.
Proud provider of opinion and arrogance since November 22, 2003 at 09:35:31 am
CadJockey Militia Field Marshal

Find me on https://parler.com @kblackie