Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^2: Seeing if two numbers have the same sign ($x / abs $x)

by WoodyWeaver (Monk)
on Jan 11, 2008 at 20:11 UTC ( [id://661973]=note: print w/replies, xml ) Need Help??


in reply to Re: Seeing if two numbers have the same sign ($x / abs $x)
in thread Seeing if two numbers have the same sign

<obsMathRef>
lodin's observation and syphilis' cantrip come from the same underlying mathematical principles.

Triangle equality:
for vectors a, b,

||a+b|| <= ||a|| + ||b||, with equality iff a is a positive scalar multiple of b

(Note here || is "norm", not or.)

Angle between two vectors:
The angle theta between two vectors a, b satisfies

cos theta = (a dot b) / (||a|| ||b||)

(That the second theorem implies the first is left as an exercise for the reader. :-)

For one dimensional vectors ("numbers") the norm is the absolute value, and there are only two "directions" -- out along the positive axis and out along the negative axis. So syphilis is applying the triangle equality, while lodin is computing the cosine directly (note cos 0 = 1 -> "same sign" while cos 180 degrees = -1 -> "opposite sign").
</obsMathRef>

We now return to our regular interpretation of line noise as script.

--woody

  • Comment on Re^2: Seeing if two numbers have the same sign ($x / abs $x)

Replies are listed 'Best First'.
Re^3: Seeing if two numbers have the same sign ($x / abs $x)
by lodin (Hermit) on Jan 12, 2008 at 05:28 UTC

    lodin is computing the cosine directly (note cos 0 = 1 -> "same sign" while cos 180 degrees = -1 -> "opposite sign").

    Maybe this is what you mean, but I don't actually calculate the angle (or cosine) between $_[0] and $_[1]. I calculate the respective angles of $_[0] and $_[1] against the positive axis, and compare them.

    Equivalently, you could also view it as I'm taking the norm of the two 1D vectors $_[0] and $_[1], which gives me two unit vectors pointing in the direction of $_[0] and $_[1], which I then test for equality. If they're equal, they point in the same direction, and in 1D that means they have the same sign.

    lodin

      arg, eyes are getting old. You are absolutely right. I was thinking of the "trick" people had described about taking the product and checking if positive; you are indeed normalizing the vectors and checking for equality.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://661973]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-19 03:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found