Questions regarding Pi calculating program - c#

I am interested in writing a program that calculates the value of Pi to infinite decimal places. Its something I'm just looking to do to pass time, and have something to work on. So, regarding that, I have a couple of questions:
Benchmarking softwares that calculate Pi, what system do they use? Do they use an infinite function that converges towards the value if pi with every passing iteration? If so, what is the best function?
What would be the best approach to store the calculated value in a variable? Obviously, no existing data type will be able to hold that amount of information so how do I tackle that?

If its for the challenge try building your own datastructure using a list of ArrayLists and maybe just for fun / challenge / really long numbers add virtualization (= storing unused parts of the number to disk).
Sounds like fun :)

See Viete's forumla. Wikipedia also has a PHP example implementation near the bottom of this page.

As far as an arbitrary precision .Net library goes, I've heard good things about W3b.sine

Related

Math.Net and alglib returning different FFT outputs by default

I am developing an application in C# with spectrogram drawing functionality.
For my fist try, I used MathNet.Numerics, and now I am continuing to develop with alglib. When I changed from one to the other, I noticed that the output differs for them. Mathnet uses some kind of correction by default, which alglib seems to omit. I am not really into signal processing, also a newbie to programming, and I could not figure out what the difference exactly comes from.
MathNet default output (raw magnitude) values are ranging from ~0.1 to ~274 in my case.
And with alglib I get values ranging from ~0.2 to ~6220.
I found that MathNet Fourier.Forward uses a default scaling option. Here is says, the FourierOptions.Default is "Universal; Symmetric scaling and common exponent (used in Maple)."
https://numerics.mathdotnet.com/api/MathNet.Numerics.IntegralTransforms/FourierOptions.htm
If I use FourierOptions.NoScaling, the output is the same as what alglib produces.
In MathNet, I used Fourier.Forward function: https://numerics.mathdotnet.com/api/MathNet.Numerics.IntegralTransforms/Fourier.htm#Forward
In case of alglib, I used fftr1d function: https://www.alglib.net/translator/man/manual.csharp.html#sub_fftr1d
What is that difference in their calculation?
What is the function that I could maybe use to convert alglib output magnitude to that of MathNet, or vice versa?
In what cases should I use these different "scalings"? What are they for exactly?
Please share your knowledge. Thanks in advance!
I worked it out by myself, after reading a bunch of posts mentioning different methods of FFT output scaling. I still find this aspect of FFT processing heavily unsdocumented everywhere. I have not yet found any reliable source that explains what is the use of these scalings, which fields of sciences or what processing methods use them.
I have yet found out three different kinds of scalings, regarding the raw FFT output (complexes' magnitudes). This means multiplying them by: 1. 1/numSamples 2. 2/numSamples 3. 1/sqrt(numSamples) 4. (no scaling)
MathNet.IntegralTransforms.Fourier.Forward function (and according to various posts on the net, also possibly Matlab and Maple) by default, uses the third one. Which results in the better distinguishable graphical output when using logarithmic colouring, in my opinion.
I would still be grateful if you know something and share your ideas, or if you can reference a good paper explaining on these.

C# how to find the state someone was born in with their social security number

I am currently working on an assignment in which I am to validate various formats using regular expressions (phone numbers, birth date, email address, Social Security). One of the features our teacher has suggested would be to have a method that returns the state an individual was born using their Social Security Number.
xxx-xx-xxxx
The first 3 digits correspond to a state/area as outlined here:
http://socialsecuritynumerology.com/prefixes.php
If I've isolated the first 3 numbers as an integer already, is there anyway I could quickly match the number with its corresponding area code?
Currently I'm only using if-else statements but its getting pretty tedious.
Example:
if (x > 0 && x<3)
return "New Hampshire";
else if (x <= 7)
return "Maine";
...
You have a few options here:
50 if statements, one for each state, as you are doing.
A switch with 999 conditions, matching each option with a state. It probably looks cleaner and you can generate it with a script and interject the return statements wherever necessary. Maybe worse than option 1 in terms of tediousness.
Import the file as text, parse it into a Dictionary and do a simple lookup. The mapping is most likely not going to change in the near future, so the robustness argument is rather moot, but it is probably "simpler" in terms of amount of effort*. And it's another chance to practice regex to parse lines in the file you linked.
*Where "effort" is measured purely in the amount of tedious gruntwork prone to annoying human error and hand fatigue. Energy consumed within the brain due to engineering and implementing a solution where the computer does the ugly stuff for you is not included. :)
It's hard to tell your level of skill and what your course has taught you so far which is why it's difficult answering these kinds of questions, and also for the most part that's why you will get a negative response from people - they assume that you would have had the answer in your course materials already and will assume that you are being lazy.
I'm going to assume that you are at a basic level and that you already know how to solve the problem the brute force way (your if/else construct) and that you are genuinely interested in how to make your code better and not simply asking for a solution you can copy/paste.
Now, while your if/else idea will work, that is a procedural way of thinking. You are working with an object oriented language, so I would suggest to you to think about how you could use the principles of OO to make this work better. A good starting point would be to make a collection of state objects that contain all the parameters you need. You could then loop through your state collection and use their properties to find the matching one. You could create the state collection by reading from a file or database or even just hard coding it for the purposes of your assignment.
Your intuition that a long chain of if and else if statements might be unwieldy is sound. Not only is it tedious, but the search to find the correct interval is inefficient. However, something needs to be in charge of this tedium. A simple solution would be to use a Dictionary to store key/value pairs that you could build up once, and reuse throughout the application. This has the downside of requiring more space than necessary, as every individual mapping becomes an element of the data structure. You could instead follow the advice from this question and use a data structure more suited to ranged values for look ups.
It's difficult to tell from your question as it's written what your level of expertise is, and going into any real detail here would essentially mean completing your assignment for you. It should be noted though, that there's nothing inherently wrong with your solution, and depending on where you are in your education it may be what's expected.

Need driving distance library/application I can talk to

We need to calculate driving distances for records in a SQL Server database, so I need to find some sort of library or program that will let me do so without connecting to the internet(if it has it's own database, great, if not, I know where to get data). I'm not too worried about calculation types right now, we're probably going to go with Djikstra's, but we just need something offline. Also, I will be dealing with multiple countries, though mostly USA.
So far, I haven't found anything that would work reliably, closest is MapPoint (per Marc Gravell), so I want to ask what offline solutions are available either to plug into, call from, or work next to my code (Delphi and .NET) to calculate driving distances? Thanks.
Options:
For a sensible number of locations, you could obtain (purchase, calculate, etc) a travel matrix between all locations - gets large as you increase the count, though
If you have the lat/long for each, you can do great-arc quite easily; but tends to get messy near lakes, oceans, etc
You could use an offline like MapPoint desktop, perhaps by storing a queue of unknown routes and processing those outside the db
Please check http://www.routeware.dk for RW Net. Developed with Delphi and can use TIGER for off-line calculations. Very fast for large scale matrix calculations.
btw: A better forum for such questions is https://gis.stackexchange.com/
Ok, after sleeping on the problem, I found a solution by using google to search on "vehicle routing software." So far I have found three options that look like they might work, and will be investigating them. Those are ALK Technologies' PCMiler, Telogis' Developer tools, and DNA Evolutions' JOpt.NET. Still plenty more companies to check out for developer tools on that search phrase. I think my main problem was I was using "Driving Distance" and "Route distance" as my search terms yesterday.
Edit: for what I'm looking for, Telogis seems to have the most complete function set.

Tuner application: How do find the frequency?

Greetings,
I am currently developing a tuner application using Silverlight/c# for class project. The problems i am having seem to be asked by quite a few people but not really answered. I have read a lot of forums and googled for hours but still cannot really grasp the code and math. What i have so far is thus:
Mic => audio input => audio samples are wrote to a memory stream => bytes converted to doubles then to complex numbers => FFT() which returns an array of complex numbers...
I have read about FFT/DFT/Autocorrelation/etc. It seems to me that FFT is the way i want to go for speed. I am, essentially, turning a chromatic tuner used in band/orchestra/etc into a online application so everything needs to be done real time. For now im just focusing on trying to understand the entire process.
Questions:
What is the correct method of converting the bytes wrote to the memory stream to complex numbers? This is partially answered here Convert Audio samples from bytes to complex numbers?, but i do not know which is the correct method for each one results in different values.
I understand the basics of FFT, but not exactly sure what the numbers represent at the different stages. For example, what exactly does the array of complex numbers represent when going into the fft algorithm, and what do they represent when leaving?
What other processing is required to find the freqency of the note being played after the FFT has been calculated?
I appreciate all the help, this project has proved to be more complicated than what i orginally researched! :/
Cheers and thanks!
Josh
1) Got nothin'
2) An FFT returns an array of values. Each array member contains the strength of the signal in that frequency band.
3) First, find the array member that has the strongest value. To dial in the exact frequency, you'll probably have to do some interpolation between the array members around the strongest bucket.
EDIT: Found this article. Looks like it breaks it down for you.

Wanted: C# programming library for mathematics

What I need is: plots creation, stuff for interpolation, stuff for counting such things as
and
where L(x) is an interpolation built from some data (points) generated from original known function f(x). meaning we know original function. we have a range (-a, a) - known. We need library to help us calculate data points in range. we need to calculate L(x) a polinom using that data in that range.
I need this library to be free and opensource
Perhaps Math.NET can help you.
Check this other answer https://stackoverflow.com/questions/1387430/recommended-math-library-for-c-net, in particular several people think that MathDotNet is nice.
For plot creation, you may want excel interop (why not ?), or ILNumerics.NET.
But I don't understand the other requirements. You want to measure interpolation errors (in the max and L1 norm) from a function you don't know ? This is not a programming question, it is a math question.
I suggest you look at interpolation libraries (Math.NET contains one for instance, but many others also do) and see if they provide such things as "error estimation".
Otherwise, what you need is a math book which will explain you the assumptions on f that you need to estimate the interpolation error. It depends on what you know about the regularity of f and the interpolation method.
Edit, regarding additional information provided: There are closed form formulas for interpolation errors (here as a starting point). But any numerical integration routine (which Math.NET does not provide) will get what you want. Have a look at libraries other people pointed out, this link will get you started.
Since you seem to have regular functions (since you do polynomial interpolation), I'd go with simple Romberg integration, which is quite simple to implement in case you don't find a library that suits your need (I doubt it). Have a look at Numerical Recipes, 3rd edition for sample code.
What about using Mathematica?
Math.NET and ILNumerics.Net are both open source and will both solve your equations.

Categories