From: Xah Lee
Subject: Scheme code for getting unit vector
Date: 
Message-ID: <f32b848c-14ba-4468-90e2-1aa089dd162a@v39g2000pro.googlegroups.com>
recently there's a fun implementation for a function that returns the
unit vector of a given vector in any dimension.

Various people has given code in Mathematica, scheme, java, c,
javascript, python, ruby...

One of them is:

;; Chicken Scheme. By ···················@gmail.com
(require 'srfi-1)
(define (normalize vec)
  (map (cute / <> (sqrt (reduce + 0 (map (cute expt <> 2) vec))))
vec))

i don't have experience coding Scheme lisp. Is it possible to make
this work in scsh? (i'm running scsh 0.6.4) Also, what kinda lib is
srfi-1? I imagine it is possible to do without it and still having
elegant code?

Thanks.

The various code can be seen at bottom of:
  http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html

  Xah
∑ http://xahlee.org/

☄

From: Bakul Shah
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <494179AD.4000902@bitblocks.com>
Xah Lee wrote:
> recently there's a fun implementation for a function that returns the
> unit vector of a given vector in any dimension.
> 
> Various people has given code in Mathematica, scheme, java, c,
> javascript, python, ruby...
> 
> One of them is:
> 
> ;; Chicken Scheme. By ···················@gmail.com
> (require 'srfi-1)
> (define (normalize vec)
>   (map (cute / <> (sqrt (reduce + 0 (map (cute expt <> 2) vec))))
> vec))
> 
> i don't have experience coding Scheme lisp. Is it possible to make
> this work in scsh? (i'm running scsh 0.6.4) Also, what kinda lib is
> srfi-1? I imagine it is possible to do without it and still having
> elegant code?
> 
> Thanks.
> 
> The various code can be seen at bottom of:
>   http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html
> 
>   Xah
> ∑ http://xahlee.org/
> 
> ☄

In standard Scheme:

(define (normalize vec)
   (let ((d (sqrt (apply + (map (lambda (x) (* x x)) vec)))))
     (map (lambda (x) (/ x d)) vec)))
; test in gambit v4
 > (normalize '(1 1 1 1))
(1/2 1/2 1/2 1/2)
 > (normalize '(20 20 20 20))
(1/2 1/2 1/2 1/2)

BTW, Xah, you seem to have missed my post where I show a
Q implementation! Here it is again (this time I name the
function and its argument):

   normalize:{[vec] vec%sqrt sum vec*vec}

Test:
q)normalize 1 1 1 1
0.5 0.5 0.5 0.5
q)normalize 20 20 20 20
0.5 0.5 0.5 0.5
From: Jussi Piitulainen
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <qotvdtqv13b.fsf@ruuvi.it.helsinki.fi>
Bakul Shah writes:

> In standard Scheme:
> 
> (define (normalize vec)
>    (let ((d (sqrt (apply + (map (lambda (x) (* x x)) vec)))))
>      (map (lambda (x) (/ x d)) vec)))

I tend to write (map * vec vec) for the squares.
From: ······@darksim.com
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <b00a9a56-5e07-40ad-800f-3bba406eefd0@u18g2000pro.googlegroups.com>
On Dec 11, 2:44 pm, Jussi Piitulainen <········@ling.helsinki.fi>
wrote:
> I tend to write (map * vec vec) for the squares.

As a newbie checking out CL for possible use in future product
development, I'm interested in seeing the fastest possible version of
normalizing a 3 element float vector. I'm loving CL so far but still
don't have a good handle on optimization techniques.
From: Xah Lee
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <84fc3aa8-0f61-4f05-9828-2ebcd674447c@p2g2000prf.googlegroups.com>
Xah Lee wrote:
> recently there's a fun implementation for a function that returns the
> unit vector of a given vector in any dimension.
> The various code can be seen at bottom of:
> http://xahlee.org/UnixResource_dir/writ/Mathematica_expressiveness.html

On Dec 11, 12:35 pm, Bakul Shah wrote:
> In standard Scheme:
>
> (define (normalize vec)
>    (let ((d (sqrt (apply + (map (lambda (x) (* x x)) vec)))))
>      (map (lambda (x) (/ x d)) vec)))
> ; test in gambit v4
>  > (normalize '(1 1 1 1))
> (1/2 1/2 1/2 1/2)
>  > (normalize '(20 20 20 20))
> (1/2 1/2 1/2 1/2)

Thanks. I tested it in scsh and it works.
I've put your code up.

> BTW, Xah, you seem to have missed my post where I show a
> Q implementation! Here it is again (this time I name the
> function and its argument):
>
>    normalize:{[vec] vec%sqrt sum vec*vec}

Q? is that the term rewriting lang?
http://en.wikipedia.org/wiki/Q_(programming_language)

not sure i want to include langs where only few people heard of.

currently, the javascript example there is considered broken by me.
(it is syntax error in SpiderMonkey engine “JavaScript-C 1.7.0
2007-10-03”)

The C code by John W Kennedy there also considered broken. It needs to
be a full, runnable program that print result.

  Xah
∑ http://xahlee.org/

☄
From: William James
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <go0d2t0mjv@enews5.newsguy.com>
Xah Lee wrote:

> > In standard Scheme:
> > 
> > (define (normalize vec)
> >    (let ((d (sqrt (apply + (map (lambda (x) (* x x)) vec)))))
> >      (map (lambda (x) (/ x d)) vec)))
> > ; test in gambit v4
> >  > (normalize '(1 1 1 1))
> > (1/2 1/2 1/2 1/2)
> >  > (normalize '(20 20 20 20))
> > (1/2 1/2 1/2 1/2)

Clojure:

(defn normalize [vec]
  (let [div  (Math/sqrt (reduce + (map * vec vec)))]
    (map #(/ % div) vec)))
From: Andrew Reilly
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <6qde6jFbs44nU1@mid.individual.net>
On Thu, 11 Dec 2008 11:38:17 -0800, Xah Lee wrote:

> recently there's a fun implementation for a function that returns the
> unit vector of a given vector in any dimension.
> 
> Various people has given code in Mathematica, scheme, java, c,
> javascript, python, ruby...

Here's an easy one in matlab (actually octave):
function u = unit(a)
  u = a./norm(a)
end

Using a domain-specific language intended for numerical computation is 
probably cheating, though.

Cheers,

-- 
Andrew
From: Tamas K Papp
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <6qdeq1Fc2ckoU2@mid.individual.net>
On Thu, 11 Dec 2008 21:15:00 +0000, Andrew Reilly wrote:

> Here's an easy one in matlab (actually octave): function u = unit(a)
>   u = a./norm(a)
> end
> 
> Using a domain-specific language intended for numerical computation is
> probably cheating, though.

Given that the whole exercise of collecting variants of this extremely 
simple function in many languages is pretty pointless, I don't see how 
this is cheating.

Tamas
From: Jens Axel Soegaard
Subject: Re: Scheme code for getting unit vector
Date: 
Message-ID: <49417792$0$15894$edfadb0f@dtext01.news.tele.dk>
Xah Lee skrev:
> ;; Chicken Scheme. By ···················@gmail.com
> (require 'srfi-1)
> (define (normalize vec)
>   (map (cute / <> (sqrt (reduce + 0 (map (cute expt <> 2) vec))))
> vec))

A few solutions in PLT Scheme. The last one is the prettiest.


#lang scheme
(require srfi/26 srfi/42)

(define v '(1 0 -1))

; the solution from c.l.s
(map (cute / <> (sqrt (foldl + 0 (map sqr v)))) v)

; without cute
(map (λ (x) (/ x (sqrt (foldl + 0 (map sqr v))))) v)


; without foldl
(map (λ (x) (/ x (sqrt (sum-ec (: x v) (sqr x))))) v)


; add spice
(define (swap f) (λ (x y) (f y x)))
(map ((curry (swap /)) (sqrt (foldl + 0 (map sqr v)))) v)
(map ((curry (swap /)) (sqrt (sum-ec (: x v) (sqr x)))) v)


; better primitives
(define (sum v) (foldl + 0 v))
(define (scale c v) (map (λ (x) (* c x)) v))
(define (inv x) (/ x))

; final result
(scale (inv (sqrt (sum (map sqr v)))) v)

; well, even better
(define (norm x) (sqrt (sum (map sqr v))))
(scale (inv (norm v)) v)


-- 
Jens Axel Søgaard