Re: Mathematica 7 compares to other languages

2008-12-10 Thread w_a_x_man
On Dec 5, 9:51 am, Xah Lee <[EMAIL PROTECTED]> wrote:
>
> For those of you who don't know linear algebra but knows coding, this
> means, we want a function whose input is a list of 3 elements say
> {x,y,z}, and output is also a list of 3 elements, say {a,b,c}, with
> the condition that
>
> a = x/Sqrt[x^2+y^2+z^2]
> b = y/Sqrt[x^2+y^2+z^2]
> c = z/Sqrt[x^2+y^2+z^2]

>
> In lisp, python, perl, etc, you'll have 10 or so lines. In C or Java,
> you'll have 50 or hundreds lines.

Ruby:

def norm a
  s = Math.sqrt(a.map{|x|x*x}.inject{|x,y|x+y})
  a.map{|x| x/s}
end
--
http://mail.python.org/mailman/listinfo/python-list


Re: Mathematica 7 compares to other languages

2008-12-25 Thread w_a_x_man
On Dec 25, 5:24 am, Xah Lee  wrote:

> The JavaScript example:
>
> // Javascript. By William James
> function normalize( vec ) {
> var div=Math.sqrt(vec.map(function(x) x*x).reduce(function(a,b) a+b))
>   return vec.map(function(x) x/div)
>
> }
>
> is also not qualified. (it is syntax error in SpiderMonkey engine
> “JavaScript-C 1.7.0 2007-10-03”)

Since you are using the latest version of Mathematica, you should
also use the latest version of SpiderMonkey.

The function works outside of a web browser with jslibs, and it
works in Firefox 3.0.1.






// Tested with Firefox 3.0.1.
// SpiderMonkey JavaScript 1.6 added map().
// 1.8 added reduce() and function shorthand:
// function(x) { return x * x }
//   can now be:
// function(x) x * x

// Javascript. By William James
function normalize( vec ) {
var div=Math.sqrt(vec.map(function(x) x*x).reduce(function(a,b) a+b))
  return vec.map(function(x) x/div)
}

window.alert( normalize( [2,3,4] ).toSource() )





--
http://mail.python.org/mailman/listinfo/python-list


Re: Xah's Edu Corner: The importance of syntax & notations.

2009-08-16 Thread w_a_x_man
On Aug 16, 11:05 am, Petey Keller  wrote:
>  Compiler go through *great* pains

Compiler work real hard.
Compiler have heap big trouble.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: sed/awk/perl: How to replace all spaces each with an underscore that occur before a specific string ?

2009-08-22 Thread w_a_x_man
On Aug 22, 1:11 pm, bolega  wrote:
> sed/awk/perl:
>
> How to replace all spaces each with an underscore that occur before a
> specific string ?
>
> I really prefer a sed one liner.
>
> Example
> Input :  This is my book. It is too  thick to read. The author gets
> little royalty but the publisher makes a lot.
> Output: This_is_my_book._It_is_too__thick_to read. The author gets
> little royalty but the publisher makes a lot.
>
> We replaced all the spaces with underscores before the first occurence
> of the string "to ".
>
> Thanks
> Gnuist

awk 'BEGIN{FS=OFS="to "}{gsub(/ /,"_",$1);print}' myfile
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: toy list processing problem: collect similar terms

2010-09-29 Thread w_a_x_man
On Sep 26, 9:24 am, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> Xah Lee  writes:
> > here's a interesting toy list processing problem.
>
> > I have a list of lists, where each sublist is labelled by
> > a number. I need to collect together the contents of all sublists
> > sharing
> > the same label. So if I have the list
>
> > ((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n) (2 o p) (4 q
> > r) (5 s t))
>
> > where the first element of each sublist is the label, I need to
> > produce:
>
> > output:
> > ((a b) (c d i j) (e f k l o p) (g h) (m n q r) (s t))
>
> > a Mathematica solution is here:
> >http://xahlee.org/UnixResource_dir/writ/notations_mma.html
>
> > R5RS Scheme lisp solution:
> >http://xahlee.org/UnixResource_dir/writ/Sourav_Mukherjee_sourav.work_...
> > by Sourav Mukherjee
>
> > also, a Common Lisp solution can be found here:
> >http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/5d1de...
>
> It's too complex. Just write:
>
> (let ((list '((0 a b) (1 c d) (2 e f) (3 g h) (1 i j) (2 k l) (4 m n)
>               (2 o p) (4 q r) (5 s t
>
>   (mapcar (lambda (class) (reduce (function append) class :key (function 
> rest)))
>            (com.informatimago.common-lisp.list:equivalence-classes list :key 
> (function first)))
>
>    )
>
> --> ((S T) (Q R M N) (G H) (O P K L E F) (I J C D) (A B))
>
> --
> __Pascal Bourguignon__                    http://www.informatimago.com/

Ruby:

[[0, 'a', 'b'], [1, 'c', 'd'], [2, 'e', 'f'], [3, 'g', 'h'], [1,
'i', 'j'], [2, 'k', 'l'], [4, 'm', 'n'], [2, 'o', 'p'], [4, 'q', 'r'],
[5, 's', 't']].
group_by{|x| x.first}.values.map{|x| x.map{|y| y[1..-1]}.flatten}

==>[["s", "t"], ["a", "b"], ["c", "d", "i", "j"],
 ["e", "f", "k", "l", "o", "p"],
 ["g", "h"], ["m", "n", "q", "r"]]
-- 
http://mail.python.org/mailman/listinfo/python-list