Using 'If' to run the loop again? - python

I am producing 2 arrays of random numbers that range from -3 to 3 (this would provide to be my x and y coordinates per say). However I want to produce a new set of numbers if the respective x and y is greater than my radius (which is 3) by using an if loop? What do I put in after my if loop to achieve this?
from physics import *
import random
N=3
x= zeros(N,float)
y= zeros(N,float)
for i in range (0, N):
x[i] = uniform(6)-3
y[i] = uniform(6)-3
if (x[n]*2+y[n]**2)**0.5 > 3:
Thanks

Most random library modules provide a routine to return a floating point number from a uniform distribution over the interval [0,1). To choose randomly from a uniform distribution over the interval [a,b), where b > a, you can just multiply by b-a, and then add a.
I'm not certain which modules you're importing in your example, so I'll give examples using the standard random module, as well as the numpy.random
random
import random
def rand_range(a, b):
return a + random.uniform() * (b - a)
x = [rand_range(-3, 3) for i in range(3)]
y = [rand_range(-3, 3) for i in range(3)]
numpy.random
Numpy allows one to vectorize operations for speed and clarity.
import numpy as np
def rand_range(a, b, size):
return a + np.random.random(size) * (b - a)
x = rand_range(-3, 3, (3))
y = rand_range(-3, 3, (3))
or, create an 2D array of 2 rows of 3 elements, and use unpacking
x, y = rand_range(-3, 3, (2, 3))
bounds checking
In order to ensure the points generated meet the criteria of being with 3 units of x, y = (0, 0), you can do something like the following:
x = []
y = []
for i in range(3):
xi, yi = 3, 3 #initally set candidates to fail check
while xi ** 2 + yi ** 2 > 9:
xi = rand_range(-3, 3)
yi = rand_range(-3, 3)
x.append(xi)
y.append(yi)
Given the underlying random method returns a value on the interval [0,1), these arrays should all pass the following assertion:
for xi, yi in zip(x,y):
assert xi**2 + yi**2 <= 9

Can't you just create a new range and then reset your index?
from physics import *
import random
N=3
x= zeros(N,float)
y= zeros(N,float)
for i in range (0, N):
x[i] = uniform(6)-3
y[i] = uniform(6)-3
if (x[n]*2+y[n]**2)**0.5 <= 3:
x = random.sample(range(-3, 3), n)
y = random.sample(range(-3, 3), n)
i = 0

#mtadd
I am answering my question because I found a way to solve it. However, I'm curious whether the way you solved it is more efficient than my process mtadd.
In my physics import is where the random and numpy module are stored, hence why it isn't used in my code.
from physics import *
N=3 #Number of point charges
x= zeros(N,float) #grid
y= zeros(N,float)
i=0
for i in range (0, N): #Loop to ensure two values are <= 3
while i < N:
x[i] = uniform(6)-3
y[i] = uniform(6)-3
if x[i] ** 2 + y[i] ** 2 <= 9:
i+=1
else:
i=0
print x,y
Thanks for the help guys

Related

starting Summation value at i=2

I am trying to plot the error of this algorithm against h and I have run into a problem, for this error calculation, it cant use the first value, as it divides 0/0. How do I go about ignoring the first value where x =0? I basically need to start the summation on i=2 on line 46 (the absolute error one). Any help is much appreciated
import numpy
import matplotlib.pyplot as pyplot
from scipy.optimize import fsolve
from matplotlib import rcParams
rcParams['font.family'] = 'serif'
rcParams['font.size'] = 16
rcParams['figure.figsize'] = (12,6)
printing = False
def rk3(A, bvector, y0, interval, N):
h = (interval[1] - interval[0]) / N
x = numpy.linspace(interval[0], interval[1], N+1)
y = numpy.zeros((len(y0), N+1))
y[:, 0] = y0
b = bvector
for i in range(N):
y_1 = y[:, i] + h *(numpy.dot(A, y[:, i]) + b(x[i]))
y_2= (3/4)*y[:, i] + 0.25*y_1+0.25* h* (numpy.dot(A,y_1)+b(x[i]+h))
y[:, i+1] = (1/3)*y[:, i] + (2/3)*y_2 + (2/3)*h*(numpy.dot(A,y_2)+b(x[i]+h))
return x, y
def exact( interval, N):
w = numpy.linspace(interval[0], interval[1], N+1)
z = numpy.array([numpy.exp(-1000*w),(1000/999)*(numpy.exp(-w)-numpy.exp(-1000*w))])
return w, z
A=numpy.array([[-1000,0],[1000,-1]])
def bvector(x):
return numpy.zeros(2)
y0=numpy.array([1,0])
interval=numpy.array([0,0.1])
N=numpy.arange(40,401,40)
h=numpy.zeros(len(N))
abs_err = numpy.zeros(len(N))
for i in range(len(N)):
interval=numpy.array([0,0.1])
h[i]=(interval[1] - interval[0]) / N[i]
x, y = rk3(A, bvector, y0, interval, N[i])
w,z=exact(interval,N[i])
abs_err[i] = h[i]*numpy.sum(numpy.abs((y[1,:]-z[1,:])/z[1,:]))
p = numpy.polyfit(numpy.log(h), numpy.log(abs_err),1)
fig = pyplot.figure(figsize = (12, 8), dpi = 50)
pyplot.loglog(h, abs_err, 'kx')
pyplot.loglog(h, numpy.exp(p[1]) * h**(p[0]), 'b-')
pyplot.xlabel('$h$', size = 16)
pyplot.ylabel('$|$Error$|$', size = 16)
pyplot.show()
Simply add an if for the value which is zero. so for example if the dividing variable is x.
if x>0:
#code here for the calculation
The above code will use all positive non-zero value. to only skip zero use this
if x!=0:
You can also us the three arguments of a for loop:
for a in range(start_value, end_value, increment):
so this means
for a in range(2,10,2):
print a
will give you the below result
2
4
6
8

Generate random locations within a triangular domain

I want to generate x and y having a uniform distribution and limited by [xmin,xmax] and [ymin,ymax]
The points (x,y) should be inside a triangle.
How can I solve such a problem?
Here's some code that generates points uniformly on an arbitrary triangle in the plane.
import random
def point_on_triangle(pt1, pt2, pt3):
"""
Random point on the triangle with vertices pt1, pt2 and pt3.
"""
x, y = sorted([random.random(), random.random()])
s, t, u = x, y - x, 1 - y
return (s * pt1[0] + t * pt2[0] + u * pt3[0],
s * pt1[1] + t * pt2[1] + u * pt3[1])
The idea is to compute a weighted average of the three vertices, with the weights given by a random break of the unit interval [0, 1] into three pieces (uniformly over all such breaks). Here x and y represent the places at which we break the unit interval, and s, t and u are the length of the pieces following that break. We then use s, t and u as the barycentric coordinates of the point in the triangle.
Here's a variant of the above that avoids the need to sort, instead making use of an absolute value call:
def point_on_triangle2(pt1, pt2, pt3):
"""
Random point on the triangle with vertices pt1, pt2 and pt3.
"""
x, y = random.random(), random.random()
q = abs(x - y)
s, t, u = q, 0.5 * (x + y - q), 1 - 0.5 * (q + x + y)
return (
s * pt1[0] + t * pt2[0] + u * pt3[0],
s * pt1[1] + t * pt2[1] + u * pt3[1],
)
Here's an example usage that generates 10000 points in a triangle:
pt1 = (1, 1)
pt2 = (2, 4)
pt3 = (5, 2)
points = [point_on_triangle(pt1, pt2, pt3) for _ in range(10000)]
And a plot obtained from the above, demonstrating the uniformity. The plot was generated by this code:
import matplotlib.pyplot as plt
x, y = zip(*points)
plt.scatter(x, y, s=0.1)
plt.show()
Here's the image:
And since you tagged the question with the "numpy" tag, here's a NumPy version that generates multiple samples at once. Note that it uses the matrix multiplication operator #, introduced in Python 3.5 and supported in NumPy >= 1.10. You'll need to replace that with a call to np.dot on older Python or NumPy versions.
import numpy as np
def points_on_triangle(v, n):
"""
Give n random points uniformly on a triangle.
The vertices of the triangle are given by the shape
(2, 3) array *v*: one vertex per row.
"""
x = np.sort(np.random.rand(2, n), axis=0)
return np.column_stack([x[0], x[1]-x[0], 1.0-x[1]]) # v
# Example usage
v = np.array([(1, 1), (2, 4), (5, 2)])
points = points_on_triangle(v, 10000)
Ok, time to add another version, I guess. There is known algorithm to sample uniformly in triangle, see paper, chapter 4.2 for details.
Python code:
import math
import random
import matplotlib.pyplot as plt
def trisample(A, B, C):
"""
Given three vertices A, B, C,
sample point uniformly in the triangle
"""
r1 = random.random()
r2 = random.random()
s1 = math.sqrt(r1)
x = A[0] * (1.0 - s1) + B[0] * (1.0 - r2) * s1 + C[0] * r2 * s1
y = A[1] * (1.0 - s1) + B[1] * (1.0 - r2) * s1 + C[1] * r2 * s1
return (x, y)
random.seed(312345)
A = (1, 1)
B = (2, 4)
C = (5, 2)
points = [trisample(A, B, C) for _ in range(10000)]
xx, yy = zip(*points)
plt.scatter(xx, yy, s=0.2)
plt.show()
And result looks like
Uniform on the triangle?
import numpy as np
N = 10 # number of points to create in one go
rvs = np.random.random((N, 2)) # uniform on the unit square
# Now use the fact that the unit square is tiled by the two triangles
# 0 <= y <= x <= 1 and 0 <= x < y <= 1
# which are mapped onto each other (except for the diagonal which has
# probability 0) by swapping x and y.
# We use this map to send all points of the square to the same of the
# two triangles. Because the map preserves areas this will yield
# uniformly distributed points.
rvs = np.where(rvs[:, 0, None]>rvs[:, 1, None], rvs, rvs[:, ::-1])
Finally, transform the coordinates
xmin, ymin, xmax, ymax = -0.1, 1.1, 2.0, 3.3
rvs = np.array((ymin, xmin)) + rvs*(ymax-ymin, xmax-xmin)
Uniform marginals? The simplest solution would be to uniformly concentrate the mass on the line (ymin, xmin) - (ymax, xmax)
rvs = np.random.random((N,))
rvs = np.c_[ymin + (ymax-ymin)*rvs, xmin + (xmax-xmin)*rvs]
but that is not very interesting, is it?

Python code for Lagrange interpolation - determining the equation of the polynomial

The following code takes in a single value, x, and a list of points, X, and determines the value of the Lagrange polynomial through the list of points at the given x value.
def chunkIt(seq, num):
avg = len(seq) / float(num)
out = []
last = 0.0
while last < len(seq):
out.append(seq[int(last):int(last + avg)])
last += avg
return out
def product(list):
p = 1
for i in list:
p *= i
return p
def Lagrange(x,X):
T = np.zeros((2,len(X)))
list = []
for i in range(len(X)):
for j in range(len(X)):
if i != j:
list.append((x-X[j][0])/(X[i][0]-X[j][0]))
p = []
for i in chunkIt(list,len(X)):
p.append(product(i))
for i in range(len(X)):
T[0][i] = p[i]
T[1][i] = X[i][1]
list2 = []
for i in range(len(X)):
list2.append(T[0][i]*T[1][i])
return sum(list2)
For example:
x, X = 3, [[0,0],[1,1],[2,0.5]]
gives a value of -1.5.
How do I modify this code to determine the equation of the polynomial through the list of points? i.e. if I put x = 'x' as the input, I want it to return -0.75x**2 + 1.75x [for the given example]
import numpy as np
from pypoly import Polynomial
x, X = 3, [[0, 0], [1, 1], [2, 0.5]]
order = len(X)
This is the order of the resulting Lagrange polynomial. For your example, order is 3.
equations = np.array([[point[0] ** i for i in range(order)] for point in X])
values = np.array([point[1] for point in X])
coefficients = np.linalg.solve(equations, values)
This sets up simultaneous equations by substituting the points into a general polynomial. For order 3, the general polynomial is:
a * x ** 2 + b * x ** 1 + c * x ** 0 = y
It solves the system of simultaneous equations to find coefficients. For order 3, we get the values of a, b, c.
print 'coefficients', list(coefficients)
coefficients [0.0, 1.75, -0.75]
p = Polynomial(*coefficients)
Here, the * operator splits the elements of the array-like into individual values to be passed as arguments to Polynomial().
print p
1.75 * X - 0.75 * X**2
print p(x)
-1.5
To install PyPolynomial with pip, use:
for Python 2:
pip install PyPolynomial
for Python 3:
pip3 install PyPolynomial

How can I modify my graph so it displays the proper information for the axes?

I have written a program to solve the Heat Equation (u_t = k * u_xx) numerically by method of Finite Differences.
For my problem, u is function of x and t, where 0 < x < L and t > 0. I have specified L = 1 (the length of the rod) and the terminal time T = 10 seconds for my problem, so I would like for the graph to be displayed on the domain (x,t) \in {(0,1) x (0, 10)}. However, my axes just don't make sense. It is plotting the x-axis from values of 0 - 40 and the t-axis is showing -0.25 - 0.00.
How can I edit my code so that when I plot u which depends on x, t the graph will display for values of x ranging from 0 - 1 and t ranging from 0 - 10 seconds??
Thanks in advance for any and all help. it is very greatly appreciated. Here is the code I am working with:
## This program is to implement a Finite Difference method approximation
## to solve the Heat Equation, u_t = k * u_xx,
## in 1D w/out sources & on a finite interval 0 < x < L. The PDE
## is subject to B.C: u(0,t) = u(L,t) = 0,
## and the I.C: u(x,0) = f(x).
import numpy as np
import matplotlib.pyplot as plt
# Parameters
L = 1 # length of the rod
T = 10 # terminal time
N = 40 # spatial values
M = 1600 # time values/hops; (M ~ N^2)
s = 0.25 # s := k * ( (dt) / (dx)^2 )
# uniform mesh
x_init = 0
x_end = L
dx = float(x_end - x_init) / N
x = np.arange(x_init, x_end, dx)
x[0] = x_init
# time discretization
t_init = 0
t_end = T
dt = float(t_end - t_init) / M
t = np.arange(t_init, t_end, dt)
t[0] = t_init
# time-vector
for m in xrange(0, M):
t[m] = m * dt
# spatial-vector
for j in xrange(0, N):
x[j] = j * dx
# definition of the solution u(x,t) to u_t = k * u_xx
u = np.zeros((N, M+1)) # array to store values of the solution
# Finite Difference Scheme:
u[:,0] = x * (x - 1) #initial condition
for m in xrange(0, M):
for j in xrange(1, N-1):
if j == 1:
u[j-1,m] = 0 # Boundary condition
elif j == N-1:
u[j+1,m] = 0 # Boundary Condition
else:
u[j,m+1] = u[j,m] + s * ( u[j+1,m] -
2 * u[j,m] + u[j-1,m] )
# for graph
print u, x, t
plt.plot(u)
plt.title('Finite Difference Approx. to Heat Equation')
plt.xlabel('x-axis')
plt.ylabel('time (seconds)')
plt.axis()
plt.show()
It appears that whatever displays for the x-axis reflects the number of step sizes in space that I take (N = 40) for my code. I thought np.arange(x_init, x_end, dx) would return evenly spaced values within the interval (x_init, x_end) with step size dx? So what am I doing wrong? Thanks again.
You have some issues with your code as your u turns out to be 40x1601 and not 40x1600. However, I think the plot you may be after (after correcting u) is
corrected_u = u[:,:-1:]
plt.pcolor(t, x, corrected_u)

Fast math operations on an array in python

I have a fairly simple math operation I'd like to perform on a array. Let me write out the example:
A = numpy.ndarray((255, 255, 3), dtype=numpy.single)
# ..
for i in range(A.shape[0]):
for j in range(A.shape[1]):
x = simple_func1(i)
y = simple_func2(j)
A[i, j] = (alpha * x * y + beta * x**2 + gamma * y**2, 1, 0)
So basically, there's a mapping between (i, j) and the 3 values of that value (this is for visualization).
I'd like to roll this up and somehow vectorize this, but I'm not sure how to or if I can. Thanks.
Here is the vectorized version:
i = arange(255)
j = arange(255)
x = simple_func1(i)
y = simple_func2(j)
y = y.reshape(-1,1)
A = alpha * x * y + beta * x**2 + gamma * y**2 # broadcasting is your friend here
If you want to fill the last coordinates with 1 and 0:
B = empty(A.shape+(3,))
B[:,:,0] = A
B[:,:,1] = 1 # broadcasting again
B[:,:,2] = 0
You have to change simple_funcN so that they take arrays as input, and create arrays as output. After that, you could look into the numpy.meshgrid() or the cartesian() function here to build coordinate arrays. After that, you should be able to use the coordinate array(s) to fill A with a one-liner.

Categories

Resources