This project has moved and is read-only. For the latest updates, please go here.

C# example for complete beginner

Jan 14, 2011 at 10:48 AM

Hi David and all,

 

Thanks very much of the very useful work! I am really a starter to C# and just download MS C# Express wanting to try out this math library. I find myself still at 0 after nearly 2 hours of reading the discussion and documentation. I wonder if you can give me some "Hello World" example for this Meta Numerics library so I could start exploring it?

 

Lots of appreciation!

 

Huan

Jan 16, 2011 at 2:45 AM

Hello Huan,

I'd be happy to help with an example. Here's  a step-by-step guide.

  1. Run the MSI installer to install Meta.Numerics. It should create a Meta.Numerics folder in your Program Files directory with several files it.
  2. In Visual Studio (C# Express or any other flavor), choose File > New > Project and create a new C# Console Application. You could use Meta.Numerics in any other kind of app, too, but a console app will be the fastest for us to write.
  3. In the solution explorer window, right-click on References and choose Add Reference. In the dialog box that appears, there are two ways for you to reference Meta.Numerics. In the .NET tab, if Meta.Numerics appears in the list of assemblies, select it there and click OK. Or in the Browse tab, you can browse to Program Files\Meta.Numerics\Meta.Numerics.dll and select it there. Either way, when you are done you should see Meta.Numerics under References in the solution explorer window.
  4. In the Program.cs file, add using statements for the Meta.Numerics namespaces you want to use. In my example, I will be using the Meta.Numerics.Functions namespace, so I add the line:
    using Meta.Numerics.Functions;
  5. Now let's use Meta.Numerics to find a zero of a Bessel function. Put the following code in the Main method:
    double x0 = FunctionMath.FindZero(delegate(double x) { return AdvancedMath.BesselJ(0, x); }, 1.0);
    Console.WriteLine(x0);
    Console.ReadLine();

    This code uses Meta.Numerics' FindZero method to locate a root. It takes a function and a starting point as arguments. We are using Meta.Numerics' BesselJ method to define the function whose root we seek. The WriteLine and ReadLine invocations are just there to print out the result and ensure that the window doesn't immediately disappear afterward.
  6. Press F5 or choose Debug > Start Debugging to compile and run the program. It should print out a numer starting 2.4048..., which is the zero of J0 that is closest to 1.0. By modifying the code you should be able to reproduce any of the zeros listed here.

Of course, there is a lot more to Meta.Numerics than Bessel-functions and root-finding, but that was the quickest way I could think of to get working code that does something non-trivial. You might also want to look at this article, which has a downloadable WinForms project that uses Meta.Numerics to produce pretty pictures of complex functions.

I hope that was helpful. You could help me, too, by letting us know which of these steps was the crucial missing one, so we can add it to our Documentation pages. Thanks for your interest.

Jan 17, 2011 at 4:18 PM

Thanks very much for this detailed step-by-step tour guide! Much apprecaited really. I have managed to start playing with your library.

As a C# novice, I personally think Step 3,4,5,6 are crucial for me to start using your library.

Once the above steps are done, using your library seems much easier for me.

Hope this is helpful to you. Please feel free to let me know if you need me to contribute more novice questions!

Best wishes,

Huan