High School Learning 1

1766 days ago by seor3821

# Let's start by generating some random numbers U = RealDistribution('uniform',[0,1]) # U here is going to generate a real random number uniformly on the interval 0 to 1 # To generate a random number we use the following command U.get_random_element() # Each time you run the command you should get a new random element 
       
0.03868397884070873
0.03868397884070873
# Let's practice by generating ten random numbers for i in range(10): U.get_random_element() 
       
0.0887760582845658
0.7490602587349713
0.7737742704339325
0.8358944009523839
0.902733813971281
0.2736991601996124
0.6581876324489713
0.429364099400118
0.998775901272893
0.5197844081558287
0.0887760582845658
0.7490602587349713
0.7737742704339325
0.8358944009523839
0.902733813971281
0.2736991601996124
0.6581876324489713
0.429364099400118
0.998775901272893
0.5197844081558287
# Now lets do the same thing, but store all ten random numbers in an array randomarray = [] for i in range(10): randomarray.append(U.get_random_element()) 
       
# We can check the contents of our array with the following print randomarray 
       
[0.38795924186706543, 0.6550191214773804, 0.10893169301562011,
0.07445122115314007, 0.55893844505772, 0.9948705309070647,
0.6346993839833885, 0.33718440774828196, 0.22083842498250306,
0.8111776884179562]
[0.38795924186706543, 0.6550191214773804, 0.10893169301562011, 0.07445122115314007, 0.55893844505772, 0.9948705309070647, 0.6346993839833885, 0.33718440774828196, 0.22083842498250306, 0.8111776884179562]
# Now let's work with some complex numbers C = ComplexField(300) # The above command makes C the complex field with 300 bits of precision # Typically such precision is not needed, but it will be useful when we start dealing with polynomials 
       
# Let's create some complex numbers # The first is the complex number 3 + 4i print C(3) + C(4) * C(I) # As you can see, we must cast each number into it's complex number counter part using 'C' # Here C(I) represents the imaginary unit sqrt(-1) 
       
3.0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000 +
4.0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000*I
3.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 + 4.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*I
# Now let's generate a random complex number C(U.get_random_element()) + C(U.get_random_element()) * C(I) 
       
0.4721335277426987886428833007812500000000000000000000000000000000000000\
00000000000000000000 +
0.2944684487301856279373168945312500000000000000000000000000000000000000\
00000000000000000000*I
0.472133527742698788642883300781250000000000000000000000000000000000000000000000000000000000 + 0.294468448730185627937316894531250000000000000000000000000000000000000000000000000000000000*I
# As practice, generate 10 random complex numbers and store them into an array # Let's also try to generate some complex numbers on a circle T = RealDistribution('uniform',[0,2*pi]) t = T.get_random_element(); C(cos(t)) + C(sin(t))*C(I) 
       
-0.884004545846005740372675063554197549819946289062500000000000000000000\
000000000000000000000 -
0.4674783020885538431521410984714748337864875793457031250000000000000000\
00000000000000000000*I
-0.884004545846005740372675063554197549819946289062500000000000000000000000000000000000000000 - 0.467478302088553843152141098471474833786487579345703125000000000000000000000000000000000000*I
# Notice this random complex number will always have magnitude one (or close to it due to rounding errors) abs(C(cos(t)) + C(sin(t))*C(I)) 
       
1.0000000000000000346796557463081997837142978131708890347939480351562477\
6674905407382295811
1.00000000000000003467965574630819978371429781317088903479394803515624776674905407382295811
# Now let's generate some polynomials # The next line makes R a polynomial ring in the variable z over C R.<z> = C['z'] 
       
# Let's create a simple polynomial (z-1)*(z-i) poly = (z - C(1))*(z - C(I)) print poly 
       
z^2 +
(-1.00000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000 - I)*z + I
z^2 + (-1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 - I)*z + I
# We already know the roots of this polynomial, but let's find them using Sage print poly.roots() 
       
[(1.00000000000000000000000000000000000000000000000000000000000000000000\
000000000000000000000, 1),
(1.000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000*I, 1)]
[(1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 1), (1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*I, 1)]
# This output is a bit confusing. Basically, each roots will be listed along with a number after it # The first value (1.000) is the root and the second number (1) after it represents how many times this # value occurs as a root. So if a root is repeated twice this number would have been (2). Unfortunately, this # representation for the roots is not very useful for us. Let's fix this by just creating an array of the roots # where each root appears in the array the same number of times as its multiplicity. rootarray = [] for root in poly.roots(): j = root[1] for i in range(j): rootarray.append(root[0]) print rootarray 
       
[1.000000000000000000000000000000000000000000000000000000000000000000000\
00000000000000000000,
1.0000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000*I]
[1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000, 1.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000*I]
# For practice, let's plot these roots Gout = Graphics() Gout += points(rootarray) show(Gout) 
       
# The points are there but a bit hard to see. To fix this we can give Gout some extra options that make # things more visible Gout = Graphics() Gout += points(rootarray,color='white', markeredgecolor = 'red', pointsize=20,zorder = 1, marker = "o") show(Gout,aspect_ratio=1) 
       
# Experiment with the different options to see what they do and what kind of pictures you can create. # Now let's plot the roots of a random polynomial. We will first create the random polynommial # and then we will plot its roots. For fun, let's create a degree 100 polynomial. rndpoly1 = C(1) for i in range(100): rndpoly1 = rndpoly1 * (z - C(U.get_random_element()) - C(U.get_random_element())*C(I)) rootarray = [] for root in rndpoly1.roots(): j = root[1] for i in range(j): rootarray.append(root[0]) Gout = Graphics() Gout += points(rootarray,color='white', markeredgecolor = 'red', pointsize=20,zorder = 1, marker = "o") show(Gout,aspect_ratio=1) 
       
# Now let's do the same, but this time let's choose all the roots on a circle. rndpoly2 = C(1) for i in range(100): t = T.get_random_element() rndpoly2 = rndpoly2 * (z - C(cos(t)) - C(sin(t))*C(I)) rootarray = [] for root in rndpoly2.roots(): j = root[1] for i in range(j): rootarray.append(root[0]) Gout = Graphics() Gout += points(rootarray,color='white', markeredgecolor = 'red', pointsize=20,zorder = 1, marker = "o") show(Gout,aspect_ratio=1) 
       
# Excellent. Practice by generating several other polynomials using different choices for the roots. # Try some different boxes, circles, disks, blobs, etc... # For something new, let's plot the critical points of rndpoly2. We will plot both the roots and critical points # on the same plot. derpoly = rndpoly2.derivative(z) cparray = [] for root in derpoly.roots(): j = root[1] for i in range(j): cparray.append(root[0]) Gout = Graphics() Gout += points(rootarray,color='white', markeredgecolor = 'red', pointsize=20,zorder = 1, marker = "o") Gout += points(cparray,color='white', markeredgecolor = 'blue', pointsize=20,zorder = 1, marker = "o") show(Gout,aspect_ratio=1) 
       
# what we are really interested in is the roots of rndpoly1 + rndpoly2. So let's find those roots. # In the final plot, we will plot the roots of rndpoly1, the roots of rndpoly2, and the roots of the sum all # using different colors. sumpoly = rndpoly1 + rndpoly2 rootarray1 = [] rootarray2 = [] sumrootarray = [] for root in rndpoly1.roots(): j = root[1] for i in range(j): rootarray1.append(root[0]) for root in rndpoly2.roots(): j = root[1] for i in range(j): rootarray2.append(root[0]) for root in sumpoly.roots(): j = root[1] for i in range(j): sumrootarray.append(root[0]) Gout = Graphics() Gout += points(rootarray1,color='white', markeredgecolor = 'red', pointsize=20,zorder = 1, marker = "o") Gout += points(rootarray2,color='white', markeredgecolor = 'blue', pointsize=20,zorder = 1, marker = "o") Gout += points(sumrootarray,color='white', markeredgecolor = 'green', pointsize=20,zorder = 1, marker = "o") show(Gout,aspect_ratio=1) 
       
# Pretty neat! The goal of our project, is try to figure out why the green dots appear where they do. In addition, # we want to see how changing the blue and red dots changes the green dots. # To get started, practice generating some other random polynomials (with roots onside disks, rectangles, etc.) and # plot the roots of the sum