Skip to content
Snippets Groups Projects
Commit b792312d authored by cprutean's avatar cprutean
Browse files

Update 3 files

- /CodeExamples/MultiplyByLogsNegativeFixed.ipynb
- /CodeExamples/OneWayConditional.ipynb
- /CodeExamples/PiExample.ipynb
parent 5f6f2b93
No related branches found
No related tags found
No related merge requests found
%% Cell type:markdown id:6838b2de tags:
# Hello
%% Cell type:code id:4200d243 tags:
``` python
"""
Python program to demonstrate multiple and divide by logs.
Python program to demonstrate multiplication and division by logs.
Modified to deal with negative numbers, note that
math.copysign(x,y) gives abs(x) with the sign if y.
math.copysign(x,y) gives abs(x) with the sign of y.
"""
import math # get the maths functions
def main():
# Read in two floats
a = float(input("Value a : "))
b = float(input("Value b : "))
# Calcualte the natural log of abs() each so -ve values dont fail
al = math.log(abs(a))
bl = math.log(abs(b))
# Do a multiply by logs, and put the signs back in with math.copysign()
c = math.copysign(1.0,a)*math.copysign(1.0,b)*math.exp(al + bl)
# Print out the result
print("Multiply by logs : " + str(c) + " and by normal multiply : " + str(a*b))
# Do a divide by logs and put the signs back in with math.copysign()
c = math.copysign(1.0,a)*math.copysign(1.0,b)*math.exp(al - bl)
# Print out tghe result.
print("Divide by logs : " + str(c) + " and by normal divide : " + str(a/b))
# run the main()
main()
# Read in two floats
a = float(input("Value a : "))
b = float(input("Value b : "))
# Calculate the natural log of abs() each so -ve values dont fail
al = math.log(abs(a))
bl = math.log(abs(b))
# Do a multiply by logs, and put the signs back in with math.copysign()
c = math.copysign(1.0,a)*math.copysign(1.0,b)*math.exp(al + bl)
# Print out the result
print("Multiply by logs : " + str(c) + " and by normal multiply : " + str(a*b))
# Do a divide by logs and put the signs back in with math.copysign()
c = math.copysign(1.0,a)*math.copysign(1.0,b)*math.exp(al - bl)
# Print out tghe result.
print("Divide by logs : " + str(c) + " and by normal divide : " + str(a/b))
```
......
%% Cell type:markdown id:781271bc tags:
# $\Pi$ Example
%% Cell type:code id:5417046e tags:
``` python
""" Example of estimating Pi by Monte Carlo Simulation.
"""
import math
import random
import matplotlib.pyplot as plt
def main():
maxPoint = int(input("Number of points : "))
plotInterval = maxPoint/100
xData = []
yData = []
point = 0
inside = 0
while point < maxPoint:
point += 1
x = random.uniform(-1.0,1.0) # Random between -1 -> 1for both x and y
y = random.uniform(-1.0,1.0)
if x*x + y*y <= 1.0: # Test if inside unit circle
inside += 1
if point % plotInterval == 0: # Calcualte estimate for plot
estimate = 4.0*inside/point
xData.append(point)
yData.append(estimate)
print("Estimate for PI after " + str(maxPoint) + " is " + str(estimate))
plt.plot(xData,yData) # Plot the estimate
plt.plot([0,maxPoint],[math.pi,math.pi]) # Plot Pi
plt.ylim(3.0,3.3) # Limit graph range
plt.title("Plot of estimate of Pi again points")
plt.show()
main()
maxPoint = int(input("Number of points : "))
plotInterval = maxPoint/100
xData = []
yData = []
point = 0
inside = 0
while point < maxPoint:
point += 1
x = random.uniform(-1.0,1.0) # Random between -1 -> 1for both x and y
y = random.uniform(-1.0,1.0)
if x*x + y*y <= 1.0: # Test if inside unit circle
inside += 1
if point % plotInterval == 0: # Calcualte estimate for plot
estimate = 4.0*inside/point
xData.append(point)
yData.append(estimate)
print("Estimate for Pi after " + str(maxPoint) + " is " + str(estimate))
plt.plot(xData,yData) # Plot the estimate
plt.plot([0,maxPoint],[math.pi,math.pi]) # Plot Pi
plt.ylim(3.0,3.3) # Limit graph range
plt.title("Plot of estimate of Pi again points")
plt.show()
```
......
%% Cell type:markdown id:1e2beded tags:
# $\pi$ example
%% Cell type:code id:016a7406 tags:
``` python
""" Example of estimating Pi by Monte Carlo Simulation.
"""
import math
import random
import matplotlib.pyplot as plt
def main():
maxPoint = int(input("Number of points : "))
plotInterval = maxPoint/100
xData = []
yData = []
point = 0
inside = 0
while point < maxPoint:
point += 1
x = random.uniform(-1.0,1.0) # Random number between -1 -> 1 for both x and y
y = random.uniform(-1.0,1.0)
if x*x + y*y <= 1.0: # Test if inside unit circle
inside += 1
if point % plotInterval == 0: # Calculate estimate for plot
estimate = 4.0*inside/point
xData.append(point)
yData.append(estimate)
print("Estimate for Pi after " + str(maxPoint) + " is " + str(estimate))
plt.plot(xData,yData) # Plot the estimate
plt.plot([0,maxPoint],[math.pi,math.pi]) # Plot Pi
plt.ylim(3.0,3.3) # Limit graph range
plt.title("Plot of estimate of $\pi$ against number of points")
plt.show()
main()
maxPoint = int(input("Number of points : "))
plotInterval = maxPoint/100
xData = []
yData = []
point = 0
inside = 0
while point < maxPoint:
point += 1
x = random.uniform(-1.0,1.0) # Random number between -1 -> 1 for both x and y
y = random.uniform(-1.0,1.0)
if x*x + y*y <= 1.0: # Test if inside unit circle
inside += 1
if point % plotInterval == 0: # Calculate estimate for plot
estimate = 4.0*inside/point
xData.append(point)
yData.append(estimate)
print("Estimate for Pi after " + str(maxPoint) + " is " + str(estimate))
plt.plot(xData,yData) # Plot the estimate
plt.plot([0,maxPoint],[math.pi,math.pi]) # Plot Pi
plt.ylim(3.0,3.3) # Limit graph range
plt.title("Plot of estimate of $\pi$ against number of points")
plt.show()
```
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment