Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

There are two functions here, the first

    randomPoly[n_, x_, {a_, b_}] := 
      x^Range[0, n].Table[RandomReal[{a, b}], {n + 1}];
Simply generates the expressions {x^0,x^1,x^2...x^(n-1),x^n} using 'x^Range[0,n]' and multiplies that list elementwise with a list of random numbers (in the range a,b) of the same length (Table evaluates an expression over a parameter range, in this case, the parameter doesn't matter, he just wants RandomReal evaluated n+1 times). This will generate a random polynomial of order n with coefficients in the range {a,b} if you pass in a symbolic variable for x_.

    Show[Graphics[
      Point[{Re[x], Im[x]}] /. 
      NSolve[randomPoly[300, x, {27, 42}], x], Axes -> True]]
Now he invokes the ranNo, it really doesn't matter whidomPoly to generate a polynomial of the symbol 'x' of order 300, with coefficients between [27,42]. He passes them into NSolve which returns a list of replacement expressions for x (NSolve assumes it's being asked to find the roots of the expression in the symbol variable 'x'). Mathematica has this idea of a replacement operator (/.) that will take one expression and replace all occurrences of a symbol with a new expression. If you provide a list of replacement expressions, you get a list of expressions as output. So the output of NSolve is something like

     {x->1+i, x->2+i}  # Except it's going to have 300 entries and be closer to 1
And he replaces the expression like

    Point[{Re[x], Im[x] }] /. {x->1+i, x->2+i}
which would yield

    {Pint[{Re[1+i],Im[1+i]}],Point[{Re[2+i], Im[2+i]}]}
Which results in

    {Point{1,1},Point[{2,1}]}
He then wraps them up into a single Graphics object and displays it.


Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: