SuanShu in .NET environment

Revision for “SuanShu in .NET environment” created on April 15, 2015 @ 18:39:30

TitleContentExcerptRevision Note
SuanShu in .NET environment
We provide a version of <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> that can be used in Microsoft .NET environment. The following explains how to set up Visual Studio to use <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a>.NET.

Please note that <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> is primarily a Java library, of which the .NET version is a conversion. Hence there are some benefits gained by using the Java version, which will be described at the bottom of this page.
<h1 id="ObtainingtheDistribution">Obtaining the Distribution</h1>
You can download the most recent version of the distribution from SuanShu’s <a class="ext-link" href="http://numericalmethod.com/suanshu/"><span class="icon">​</span>product website</a>.

Included in the distribution are:
<ul>
<li>The converted suanshu.dll</li>
<li>A converted version of the JodaTime jar</li>
<li>The parts of IKVM required to use the converted assemblies.</li>
</ul>
<h1 id="ImportingtheAssemblies">Importing the Assemblies</h1>
In order to use <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a>.NET in your application, you will need to add it and its dependencies as references in Visual Studio. All of the files that you need to add are included in the distribution.
<h2 id="VisualStudio2010">Visual Studio 2010</h2>
These directions are for Visual Studio 2010, although the procedure is very similar for other recent versions.

To add the references, right-click your project in the ‘Solution Explorer’ and click ‘Add Reference’.

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/addreference.jpg"><img class="alignnone size-full wp-image-3513" src="http://numericalmethod.com/wp-content/uploads/2015/04/addreference.jpg" alt="addreference" width="645" height="754" /></a>
<a href="http://numericalmethod.com/wp-content/uploads/2015/04/dialog.jpg"><img class="alignnone size-full wp-image-3514" src="http://numericalmethod.com/wp-content/uploads/2015/04/dialog.jpg" alt="dialog" width="519" height="438" /></a>

Alternatively you can also select ‘Add Reference’ from the ‘Project’ menu. This should open the ‘Add Reference’ dialog. In the dialog, please select the ‘Browse’ tab and navigate to the directory in which you downloaded the <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a>.NET distribution. Select all files and click ‘OK’. You should then see them being listed as References in the ‘Solution Explorer’.

<a href="http://numericalmethod.com/wp-content/uploads/2015/04/references.jpg"><img class="alignnone size-full wp-image-3515" src="http://numericalmethod.com/wp-content/uploads/2015/04/references.jpg" alt="references" width="284" height="535" /></a>

<h1 id="Placingthelicensefile">Placing the license file</h1>
For <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> to run, it has to have access to the license file. In Java it will look for the license file on the classpath of your current project. In .NET it will only check the directory of your executable.

When running your project straight from Visual Studio, this would be the bin\Debug and bin\Release folders inside your project folder. For projects using Office integration features, the license file has to be in your ‘My Documents’ folder. To manually change the license file location, see the tips and tricks section below.
<h1 id="Examples">Examples</h1>
Using <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> with .NET is largely very similar to using it in Java. In the following I will give brief demonstrations on how to do some basic operations in C#.
<h1 id="SimpleExample">Simple Example</h1>
In the blank project there is a simple example that shows how to call <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> code from C# to do matrix multiplication.

After importing the necessary classes (this can be done automatically by Visual Studio):
<pre class="toolbar-overlay:false show-lang:2 lang:c# decode:true">
using com.numericalmethod.suanshu.matrix.doubles;
using com.numericalmethod.suanshu.matrix.doubles.matrixtype.dense;
using com.numericalmethod.suanshu.license;
</pre>
we can do matrix multiplication with the following code:
<pre class="toolbar-overlay:false show-lang:2 lang:xml decode:true">
Matrix A = new DenseMatrix(new double[][] {
new double[] { 1.0, 2.0, 3.0 },
new double[] { 3.0, 5.0, 6.0 },
new double[] { 7.0, 8.0, 9.0 }
});

Matrix B = new DenseMatrix(new double[][] {
new double[] { 2.0, 0.0, 0.0 },
new double[] { 0.0, 2.0, 0.0 },
new double[] { 0.0, 0.0, 2.0 }
});

Matrix AB = A.multiply(B);
Console.Write(AB.ToString());
</pre>
Further tips on how to use <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> with .NET can be found at the bottom of this page.

<h1 id="CodeExamples">Code Examples</h1>
<a href="http://redmine.numericalmethod.com/projects/public/repository/show/SuanShu.NET" target="_blank">http://redmine.numericalmethod.com/projects/public/repository/show/SuanShu.NET</a>
<h1 id="Documentation">Documentation</h1>
Included in the distribution in an XML file containing XML comments, which can be viewed in Visual Studio. However since it is a conversion from Java’s JavaDoc comments, it is possible that documentation for some parts are missing or incomplete. Furthermore documentation for classes from the Java library is not included.

In either of those cases, please check the <a href="http://www.numericalmethod.com/javadoc/suanshu/">latest JavaDoc</a>.
<h1 id="TipsandTricks">Tips and Tricks</h1>
<h2 id="Settingthelocationofthelicensefile">Setting the location of the license file</h2>
You can manually set the location of the license file before calling any <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> code. This can be done by calling:
<pre class="toolbar-overlay:false show-lang:2 lang:xml decode:true">
com.numericalmethod.suanshu.license.License.setLicenseFile()
</pre>
<h2 id="Datatypes">Datatypes</h2>
Since the library uses converted Java datatypes instead of their .NET equivalents. Hence for the <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> code to interact with other .NET code you may need to convert the datatypes. For example for lists, this can be accomplished as follows (in C#):
<pre class="toolbar-overlay:false show-lang:2 lang:c# decode:true">
List<double> cSharpList = new List</double><double>();
java.util.List javaList = java.util.Arrays.asList(cSharpList.ToArray());
List</double></pre>
Unfortunately due to Java’s type erasure at compile time, the converted Java collections are not generic.

<h2 id="Jaggedarrays">Jagged arrays</h2>
In .NET we have multi-dimensional arrays ([,] in C#) and jagged arrays ([][], e.g. arrays of arrays). Since Java only supports the latter, functions for which a multi-dimensional array would have been appropriate, will use jagged arrays. To see how a jagged array is defined, see the matrix example above.
<h1 id="BenefitsofusingtheJavaversion">Benefits of using the Java version</h1>
Even though the .NET version of <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> is fully featured, there are a few benefits of using the Java version:
<ul>
<li>Performance is about 2x better</li>
<li>The JavaDoc documentation is more complete than the XML comments</li>
<li>There is no need to convert between .NET and Java datatypes</li>
<li>Types are generic</li>
<li>The naming conventions of <a href="http://numericalmethod.com/up/suanshu/" target="_blank">SuanShu</a> are consistent with Java conventions</li>
</ul>

&nbsp;



Old New Date Created Author Actions
April 15, 2015 @ 18:39:30 webmaster
April 15, 2015 @ 18:39:29 [Autosave] webmaster
April 15, 2015 @ 18:36:36 webmaster
April 14, 2015 @ 07:13:38 sanjay
April 14, 2015 @ 07:13:33 [Autosave] sanjay
April 13, 2015 @ 10:11:09 sanjay
April 13, 2015 @ 08:27:11 sanjay