pi

Python numpy unwrap function

橙三吉。 提交于 2019-12-06 04:15:58
问题 I am hoping to convert a array of radians into range [0, 2*pi) and numpy unwrap function is exactly what I need However, when I run the following code to input a = [pi, 2*pi, 3*pi] : import numpy as np a = np.array([np.pi, 2*np.pi, 3*np.pi]) np.unwrap(a) I expect the results to be close to [pi, 0, pi] . However, the output is still: array([ 3.14159265, 6.28318531, 9.42477796]) It is not unwrapped. However, if I instead run the following without using the numpy.pi a = np.array([3.14159265, 6

Cannot draw pixels, Pi number in a Synesthetic way

不打扰是莪最后的温柔 提交于 2019-12-06 02:26:18
I want to print each digit of pi number as a colored pixel, so, I get an input, with the pi number, then parse it into a list, each node containing a digit (I know, I'll use an array later), but I never get this painted to screen... Can someone help me to see where I'm wrong? import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Image; import java.awt.image.MemoryImageSource; import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.ArrayList; import java.util.List; import javax.swing.JFrame; import javax.swing.JPanel; public class

Computing π to “infinite” binary precision in C#

大憨熊 提交于 2019-12-06 01:49:45
问题 So far it looks like Fabrice Bellard's base 2 equation is the way to go Ironically this will require a BigReal type; do we have this for .Net? .Net 4.0 has BigInteger. Anyone have a Haskell version? 回答1: Since you're asking for a Haskell version, here is a paper by Jerzy Karczmarczuk, called "The Most Unreliable Technique in the World to compute π": This paper is an atypical exercice in lazy functional coding, written for fun and instruction. It can be read and understood by anybody who

Calculate Pi in Python with Polygons

三世轮回 提交于 2019-12-05 19:49:59
I've got a problem in calculating Pi exactly. I'm using a method where I got a circle with radius=1 and inside of it I put polygons with 8, 16, 32, 64, ... corners, doubling them after each step. But the problem is that only the first 15 decimal digits of the result are correct. Here's the program: import math import time from decimal import * n_Ecken = 8 while n_Ecken >=8: time.sleep(0.5) n_Ecken = n_Ecken * 2 def berechneAlpha(): kreis = 360 alphaInsideFunc = Decimal(kreis) / Decimal(n_Ecken) return alphaInsideFunc alphaOutsideFunc = berechneAlpha() def berechneBeta(): betaInsideFunc =

Using basic arithmetics for calculating Pi with arbitary precision

半世苍凉 提交于 2019-12-05 10:04:02
I am looking for a formula/algorithm to calculate PI~3.14 in a given precision. The formula/algorithm must have only very basic arithmetic as +: Addition -: Subtraction *: Multiplication /: Divison because I want to implement these operations in C++ and want to keep the implementation as simple as possible (no bignum library is allowed). I have found that this formula for calculating Pi is pretty simple: Pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... = sum( (-1)^(k+1)/(2*k-1) , k=1..inf ) (note that (-1)^(k+1) can be implemented easily by above operators). But the problem about this formula is the inability

Calculating π using a Monte Carlo Simulation limitations

早过忘川 提交于 2019-12-05 04:50:11
问题 I have asked a question very similar to this so I will mention the previous solutions at the end, I have a website that calculates π with the client's CPU while storing it on a server, so far I've got: '701.766.448.388' points inside the circle, and '893.547.800.000' in total, these numbers are calculated using this code. (working example at: https://jsfiddle.net/d47zwvh5/2/) let inside = 0; let size = 500; for (let i = 0; i < iterations; i++) { var Xpos = Math.random() * size; var Ypos =

Finding PI digits using Monte Carlo

廉价感情. 提交于 2019-12-05 03:17:57
问题 I have tried many algorithms for finding π using Monte Carlo. One of the solutions (in Python) is this: def calc_PI(): n_points = 1000000 hits = 0 for i in range(1, n_points): x, y = uniform(0.0, 1.0), uniform(0.0, 1.0) if (x**2 + y**2) <= 1.0: hits += 1 print "Calc2: PI result", 4.0 * float(hits) / n_points The sad part is that even with 1000000000 the precision is VERY bad ( 3.141... ). Is this the maximum precision this method can offer? The reason I choose Monte Carlo was that it's very

Python large iterations number fail

我们两清 提交于 2019-12-05 01:22:29
I wrote simple monte-carlo π calculation program in Python, using multiprocessing module. It works just fine, but when I pass 1E+10 iterations for each worker, some problem occur, and the result is wrong. I cant understand what is the problem, because everything is fine on 1E+9 iterations! import sys from multiprocessing import Pool from random import random def calculate_pi(iters): """ Worker function """ points = 0 # points inside circle for i in iters: x = random() y = random() if x ** 2 + y ** 2 <= 1: points += 1 return points if __name__ == "__main__": if len(sys.argv) != 3: print "Usage:

Plot the sine and cosine functions

狂风中的少年 提交于 2019-12-04 22:43:32
问题 I'm currently having some problems regarding my homework. Here's the Exercise: (Plot the sine and cosine functions) Write a program that plots the sine function in red and the cosine function in blue. hint: The Unicode for Pi is \u03c0 . To display -2Pi, use g.drawString("-2\u03c0", x, y). For a trigonometric function like sin(x), x is in radians. Use the following loop to add the points to a polygon p for (int x = -170; x <= 170; x++) { p.addPoint(x + 200, 100 - (int)(50 * Math.sin((x / 100

Pi calculation in python [closed]

假如想象 提交于 2019-12-04 19:24:03
Closed. This question is off-topic . It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 6 years ago . n=iterations for some reason this code will need a lot more iterations for more accurate result from other codes, Can anyone explain why this is happening? thanks. n,s,x=1000,1,0 for i in range(0,n,2): x+=s*(1/(1+i))*4 s=-s print(x) As I mentioned in a comment, the only way to speed this is to transform the sequence. Here's a very simple way, related to the Euler transformation (see roippi's link ): for the sum