<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>Excel Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/category/excel/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/category/excel</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Tue, 07 Feb 2023 21:54:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.1.1</generator>
<site xmlns="com-wordpress:feed-additions:1">104092929</site>	<item>
		<title>Porting Excel to .NET</title>
		<link>https://www.centerspace.net/porting-excel-to-net</link>
					<comments>https://www.centerspace.net/porting-excel-to-net#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Tue, 18 Feb 2014 19:11:24 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[csharp excel]]></category>
		<category><![CDATA[excel built-in]]></category>
		<category><![CDATA[excel porting]]></category>
		<category><![CDATA[math]]></category>
		<category><![CDATA[port excel to .net]]></category>
		<category><![CDATA[porting excel to .net]]></category>
		<category><![CDATA[stats]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=5254</guid>

					<description><![CDATA[<p>In previous blog posts, we demonstrated calling NMath from within Excel (C#, Visual Basic). Another common use case is replacing an Excel spreadsheet with an equivalent .NET application. Today, we are releasing .NET code to make this task much easier. We have created a library of Excel extensions for NMath that work just like the built-in [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/porting-excel-to-net">Porting Excel to .NET</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In previous blog posts, we demonstrated calling NMath from within Excel (<a href="/using-c-and-exceldna-to-call-net-libraries/">C#</a>, <a href="/calling-external-net-libraries-from-excel/">Visual Basic</a>). Another common use case is replacing an Excel spreadsheet with an equivalent .NET application. Today, we are releasing .NET code to make this task much easier.</p>
<p>We have created a library of Excel extensions for NMath that work just like the built-in Excel mathematical and statistical functions. They work with arrays in a similar fashion to a range of cells in Excel. Errors have been replaced with exceptions. Return values match as closely as possible.</p>
<p>For example, if you were using the <code>ZTest</code> function in Excel like so:</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2014/02/ztest.png"><img decoding="async" loading="lazy" class="aligncenter size-full wp-image-5732" src="https://www.centerspace.net/blog/wp-content/uploads/2014/02/ztest.png" alt="Screenshot of Excel spreadsheet with ZTest function." width="461" height="370" srcset="https://www.centerspace.net/wp-content/uploads/2014/02/ztest.png 461w, https://www.centerspace.net/wp-content/uploads/2014/02/ztest-300x240.png 300w" sizes="(max-width: 461px) 100vw, 461px" /></a></p>
<p>This is how you would do the same thing in .NET with NMath:</p>
<pre lang="csharp"> 
      using CenterSpace.NMath.Excel;

      double[] array = { 4, 5, 6, 7, 8, 3, 4 };
      ExcelStats.ZTest( array, 4.5 );</pre>
<p>That&#8217;s possible because we have code like this to make NMath work just like Excel:</p>
<pre lang="csharp">   
    public static double ZTest( double[] array, double mu, double sigma = Double.NaN )
    {
      if ( array.Length == 0 )
      {
        throw new ExcelNumException( "array must not be empty" );
      }
      sigma = Double.IsNaN( sigma ) ? StatsFunctions.StandardDeviation( array, BiasType.Unbiased ) : sigma;
      var zTest = new OneSampleZTest( array, mu, sigma );
      return zTest.P / 2.0;
    }</pre>
<p>We have tested these functions against Excel directly and verified the results.</p>
<p>Here we provide an equivalent function to the MInverse (matrix inverse) function in Excel:</p>
<pre lang="csharp">   
using CenterSpace.NMath.Excel;

    public static double[,] MInverse( double[,] array )
    {
      if ( array.GetLength( 0 ) != array.GetLength( 1 ) )
      {
        throw new ExcelValueException( "array must be square" );
      }
      var matrix = new DoubleMatrix( array );
      var factorization = new DoubleLUFact( matrix );
      return factorization.Inverse().ToArray();
    }</pre>
<p>In the case where you have a user-defined function that calls other built-in functions, you can replace your code easily. For example, a user-defined function as follows:</p>
<pre lang="vb">Function CV(numbers As Range)
  mean = Application.WorksheetFunction.Average(numbers)
  standarddeviation = Application.WorksheetFunction.StDev(numbers)
  CV = (standarddeviation / mean) * 100
End Function
</pre>
<p>would look as follows:</p>
<pre lang="csharp">    private static double CV( double[] a )
    {
      var mean = ExcelStats.Average( a );
      var stddev = ExcelStats.StDev( a );
      return ( stddev / mean ) * 100.0;
    }
</pre>
<p>To use this functionality:</p>
<ol>
<li>Download the <a href="/trial-version/">free trial versions</a> of our NMath library. Trial versions are distributed in distributed in binary form only for a 30-day evaluation period.</li>
<li>Download the Excel extension library <a title="here" href="https://drive.google.com/file/d/1rD1jzQXiSNaIVYJH7kYgmIiPqB7N923a/view?usp=share_link">here</a>. It comes with source code, so you can see exactly how Excel functions are implement in NMath. (In a future release, this functionality will be built into NMath.)</li>
</ol>
<p><strong>Note:</strong> The Excel extensions are designed as static methods, allowing a one-to-one mapping from Excel to NMath. In some cases, such as within loops, greater performance can be achieved by maintaining NMath state between calls.</p>
<p>&#8211; Trevor</p>
<h2>Porting Excel to .NET</h2>
<p>Below is a complete list of supported Excel functions which are now directly supported in NMath.</p>
<pre class="code">ACos
ACosH
ASin
ASinH
ATan
ATan2
ATanH
Abs
AveDev
Average
BetaDist
BetaInv
BinomDist
Ceiling
ChiDist
ChiInv
Combin
Confidence
Correl
Cos
CosH
Count
CountBlank
CountIf
Covar
CritBinom
Degrees
DevSq
Even
Exp
ExponDist
FDist
FInv
FTest
Fact
FactDouble
Floor
Forecast
Frequency
GCD
GammaDist
GammaInv
GammaLn
GeoMean
Growth
HarMean
Int
Intercept
Kurt
Large
LinEst
Ln
Log
Log10
LogEst
LogInv
LogNormDist
MDeterm
MInverse
MMult
Max
Median
Min
Mod
Mode
NegBinomDist
NormDist
NormInv
NormSDist
NormSInv
Odd
Pearson
PercentRank
Percentile
Permut
Pi
Poisson
Power
Prob
Product
Quartile
Quotient
Radians
Rand
RandBetween
Rank
Round
RoundDown
RoundUp
Sign
Sin
SinH
Skew
Slope
Small
SqRt
SqRtPi
StDev
StDevP
StEYX
Standardize
Sum
SumSq
TDist
TInv
TTest
Tan
TanH
Trend
TrimMean
Var
VarP
Weibull
ZTest</pre>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/porting-excel-to-net">Porting Excel to .NET</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/porting-excel-to-net/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">5254</post-id>	</item>
		<item>
		<title>Advanced Curve Fitting using Excel and NMath</title>
		<link>https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath</link>
					<comments>https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath#respond</comments>
		
		<dc:creator><![CDATA[CenterSpace]]></dc:creator>
		<pubDate>Mon, 14 Mar 2011 22:10:15 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3145</guid>

					<description><![CDATA[<p>In this post, we will demonstrate the advanced curve fitting functions available in the CenterSpace libraries that could be easily be integrated into Excel analysis work. Curve fitting is one of the most practical applications of mathematics as we are often asked to explain or make predictions based on a collection of data points. This example combines the ease of Excel's charting capabilities with CenterSpace's powerful NMath library.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath">Advanced Curve Fitting using Excel and NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In recent blog posts, we have discussed how to call CenterSpace&#8217;s Libraries from Excel.  In his March 2010 blog post, CenterSpace&#8217;s Ken Baldwin demonstrated how to replicate <a href="https://www.centerspace.net">Excel&#8217;s existing Trendline functions using C# and NMath</a>.  In this post, we will demonstrate the advanced curve fitting functions available in the CenterSpace libraries that could be easily be integrated into Excel analysis work.</p>
<p>Curve fitting is one of the most practical applications of mathematics as we are often asked to explain or make predictions based on a collection of data points.  For example, if we collect a series of changing data values over time, we look to explain the relationship that time effects the generated values or in mathematical terms y=f(x).  Here we are using x to represent time values and f(x) to represent the relationship or function that generates the resulting values or y.  So if we can find a function f(x) that represents a good fit of the data we should be able to predict the result at any moment in time.</p>
<p>With this in mind, let us get started with some data points (x, y) that we have collected.  I have entered the following values in an Excel spreadsheet.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit1.gif"><img decoding="async" class="size-full wp-image-3146" title="Curve Fitting Sample data" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit1.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit1.gif 777w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit1-300x279.gif 300w" sizes="(max-width: 777px) 100vw, 777px" /></a></p>
<p>We can now use Excel&#8217;s charting function to create the following XY scatterplot with our data:</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit2.gif"><img decoding="async" class="size-full wp-image-3147" title="Excel XY scatterplot of sample data" width="700" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit2.gif" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit2.gif 1473w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit2-300x184.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit2-1024x629.gif 1024w" sizes="(max-width: 1473px) 100vw, 1473px" /></a></p>
<p>At this point, we can add an Excel trendline to get the best fit it can provide.  By right clicking on a data value in our chart we will get the option to add a trendline. Choose a 2nd order polynomial and these options.</p>
<p>Name the trendline &#8220;2nd Order Polynomial&#8221; and check &#8220;Display equation on chart&#8221; and &#8220;Display R-squared value on chart&#8221;.</p>
<p>Excel calculates and plots the line while returning the equation and the R2 value.  If our R2 equals 1 we would have found a perfect fit.  For a second order polynomial Excel returned a value of 0.8944 which means that roughly 10.6 percent (1-.894) are not on this line.</p>
<p>If we continue increasing our polynomial orders up to the maximum of six we can achieve the best R2 value of 0.9792, but look at the curve we have fitted to these points.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit3.gif"><img decoding="async" class="size-full wp-image-3149" title="Excel Trendline using a polynomial function" width="700" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit3.gif" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit3.gif 1167w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit3-300x188.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit3-1024x642.gif 1024w" sizes="(max-width: 1167px) 100vw, 1167px" /></a></p>
<p>A better fit might be an exponential function so let us try Excel&#8217;s trendline option using exponentials.<br />
<a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit4.gif"><img decoding="async" class="alignnone size-full wp-image-3150" title="Excel exponential trendline fit of sample data" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit4.gif" width="700"  alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit4.gif 1169w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit4-300x203.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit4-1024x695.gif 1024w" sizes="(max-width: 1169px) 100vw, 1169px" /></a></p>
<p>Clearly the results are visually a better fit but the R2 value tells us that over 15% of the data points are not on this line.  This pretty much represents the best we can do with Excel&#8217;s trendline functions for our data.</p>
<p>Looking to CenterSpace&#8217;s NMath and NStat libraries to give us more robust analysis, we can utilize more powerful curve fitting tools quickly and with little effort.</p>
<p>Using the linkage provided by ExcelDNA that we examined in our previous posts, we can create the following C# code for our ExcelDNA text file.</p>
<pre lang="csharp"><![CDATA[

using System;
using ExcelDna.Integration;
using CenterSpace.NMath.Core;
using CenterSpace.NMath.Matrix;
using CenterSpace.NMath.Analysis;
using CenterSpace.NMath.Stats;

public class NMathExcelCurveFit
{

 [ExcelFunction(Description="Four parameterized Fit")]
 public static double[] NOneVarFunctFitFour(double[] xValues, double[] yValues, double[] start)
 {
  DoubleVector TempVx = new DoubleVector(xValues);
  DoubleVector TempVy = new DoubleVector(yValues);
  DoubleVector TempVs = new DoubleVector(start);
  OneVariableFunctionFitter
<TrustRegionMinimizer> fitter = new  OneVariableFunctionFitter
<TrustRegionMinimizer>( AnalysisFunctions.FourParameterLogistic );
  DoubleVector solution = fitter.Fit(TempVx, TempVy, TempVs);
  return solution.ToArray();
 }

 [ExcelFunction(Description="Four parameterized R2")]
 public static double NOneVarFunctFitFourR2(double[] xValues, double[] yValues, double[] start)
 {
  DoubleVector TempVx = new DoubleVector(xValues);
  DoubleVector TempVy = new DoubleVector(yValues);
  DoubleVector TempVs = new DoubleVector(start);
  OneVariableFunctionFitter
<TrustRegionMinimizer> fitter = new  OneVariableFunctionFitter
<TrustRegionMinimizer>( AnalysisFunctions.FourParameterLogistic );
  DoubleVector solution = fitter.Fit(TempVx, TempVy, TempVs);
  GoodnessOfFit gof = new GoodnessOfFit(fitter, TempVx, TempVy, solution);
  return gof.RSquared;
 }
}
]]&gt;</pre>
<p>As you can see by the code, I have chosen to use NMath&#8217;s OneVariableFunctionFitter with the FourParameterLogistic predefined generalized function.</p>
<pre class="code">
<img decoding="async" title="{d} + \frac{(a - d)}{ 1 + ({\frac{x}c})^b}" src="http://latex.codecogs.com/gif.latex?{d} + \frac{(a - d)}{ 1 + ({\frac{x}c})^b}" alt=""/></pre>
<p>From CenterSpace&#8217;s documentation, we get the above equation and need to solve for the model parameters <code> a, b, c, d </code>.</p>
<p>The OneVariableFunction call will require us to provide a starting guess along with the ranges containing the xValues and yValues.  The following screen shows us making the function call from Excel with the necessary parameters.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit6.gif"><img decoding="async" class="size-full wp-image-3156" title="CurveFit6" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit6.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit6.gif 1296w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit6-300x172.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit6-1024x587.gif 1024w" sizes="(max-width: 1296px) 100vw, 1296px" /></a></p>
<p>After computing these values, we can call for the R2 value and generate some points to be plotted.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit7.gif"><img decoding="async" class="size-full wp-image-3157" title="Obtaining the R2 value from a NMath OneVariableFunction call" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit7.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit7.gif 1059w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit7-300x175.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit7-1024x600.gif 1024w" sizes="(max-width: 1059px) 100vw, 1059px" /></a></p>
<p>Note that I choose to hide the rfows from r17 to r107 for display purposes. You will need to have them unhid for the chart to look right.  As you can see we returned the best R2 value so far at 0.9923.</p>
<p>In the next screen, we will have added the series to our chart and drawn a line through our calculated points.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit8.gif"><img decoding="async" class="size-full wp-image-3158" title="OneVariableFunction using FourParameterLogistic function plot" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/CurveFit8.gif" width="700" alt="" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit8.gif 1179w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit8-300x200.gif 300w, https://www.centerspace.net/wp-content/uploads/2011/01/CurveFit8-1024x682.gif 1024w" sizes="(max-width: 1179px) 100vw, 1179px" /></a></p>
<p>This example illustrates the ease that Excel can use the power of NMath curve fitting routines to compute accurate fits to a collection of data.</p>
<p>Mike Magee</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath">Advanced Curve Fitting using Excel and NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/advanced-curve-fitting-using-excel-and-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3145</post-id>	</item>
		<item>
		<title>Using C# and ExcelDNA to call .NET Libraries</title>
		<link>https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries</link>
					<comments>https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries#comments</comments>
		
		<dc:creator><![CDATA[CenterSpace]]></dc:creator>
		<pubDate>Fri, 14 Jan 2011 05:00:21 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[c# math with excel]]></category>
		<category><![CDATA[Calling Excel from NMath]]></category>
		<category><![CDATA[Curve fitting with Excel]]></category>
		<category><![CDATA[ExcelDNA math]]></category>
		<category><![CDATA[ExcelDNA NMath]]></category>
		<category><![CDATA[NMath Excel]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3058</guid>

					<description><![CDATA[<p><img src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic2.gif" alt="Excel and NMath interop"  class="excerpt" /><br />
The outcome of these blog articles should illustrate that Excel can become a powerful tool to quickly access a comprehensive .NET library without needing a large supporting programming environment.  This made possible by Govert van Drimmelen's freeware tool ExcelDNA.  ExcelDNA can interpret VB, C#, and F# instructions stored in a text file as Excel is loaded eliminating the need for a separate compiler.  In our last blog, we named our file <strong>NMathExcel.dna</strong> to hold our VB code.  Below, I have provided C# code that performs the same matrix functions as the VB code in our <a href="https://www.centerspace.net/blog/nmath/calling-external-net-libraries-from-excel/">previous post</a>.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries">Using C# and ExcelDNA to call .NET Libraries</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>In my <a href="/calling-external-net-libraries-from-excel/">last post</a>, I demonstrated calling NMath from Excel using ExcelDNA and Visual Basic (VB) code.  In this blog post, we will duplicate that functionality using C# instead of Visual Basic.  In addition we will use the functionality of NMath to enabled the marshaling of data between Excel and NMath, and provide some additional code examples.</p>
<p>The outcome of these blog articles should illustrate that Excel can become a powerful tool to quickly access a comprehensive .NET library such as CenterSpace&#8217;s NMath without needing a large supporting programming environment.  This is all made possible by Govert van Drimmelen&#8217;s freeware tool ExcelDNA.  ExcelDNA can interpret VB, C#, and F# instructions stored in a text file as Excel is loaded eliminating the need for a separate compiler.  ExcelDNA does require the text file to have the extension <code>.DNA</code>.  I recommend using an editor like <a href="https://notepad-plus-plus.org/">NotePad++</a> to edit this file.  In our last blog post, we named our file <strong>NMathExcel.dna</strong> to hold our VB code.  Below, I have provided C# code that performs the same functions as the VB code in our <a href="/calling-external-net-libraries-from-excel/">previous post</a>.  This code below provides examples on how to use several basic routines in NMath.</p>
<pre lang="csharp">

<DnaLibrary Language="CS">
<Reference Name="CenterSpace.NMath.Core" />
<Reference Path="C:\Program Files\CenterSpace\NMath 4.1\Assemblies\NMath.dll" />
<Reference Path="C:\Program Files\CenterSpace\NMath 4.1\Assemblies\NMathShared.dll" />
<Reference Name="CenterSpace.NMath.Stats" />
<Reference Path="C:\Program Files\CenterSpace\NMath Stats 3.2\Assemblies\NMathStats.dll" />

<![CDATA[
 	 using ExcelDna.Integration;
 	 using CenterSpace.NMath.Core;
  	 using CenterSpace.NMath.Stats;

  	public class NMathExcelFunctions
  	{
  		[ExcelFunction(Description="Returns x raised to the y power")]
  		public static double NPower(double x, double y)
  		{
  			return NMathFunctions.PowFunction(x, y);
  		}
  		[ExcelFunction(IsVolatile=true)]
  		public static double NRand()
  		{
  			RandGenMTwist randomGenMTwist = new RandGenMTwist();
  			return randomGenMTwist.Next53BitRes();
  		}
  		[ExcelFunction(Description="Binomial Distribution: Number of Successes, Trials, Probability, Cumulative is True or False")]
  		public static double NBinomDist(int NSuccess, int NTrials, double Prob, bool Cumul)
  		{
 	 		BinomialDistribution binomialDistribution = new BinomialDistribution();
  			binomialDistribution.N = NTrials;
  			binomialDistribution.P = Prob;
  			if (Cumul)
  				return binomialDistribution.CDF(NSuccess);
  			else
  				return binomialDistribution.PDF(NSuccess);
  		}
  		[ExcelFunction(Description="Create a r by c matrix and fill with Random # between L and B")]
  		public static double[,] NDoubleMatrixRand(int rsize, int csize, int RandLBx, int RandUBx)
  		{
  			RandGenUniform randomGenUniform = new RandGenUniform(RandLBx,RandUBx);
  			randomGenUniform.Reset(0x124);
  			DoubleMatrix TempAr = new DoubleMatrix(rsize,csize,randomGenUniform);
  			return TempAr.ToArray();
  		}
  	}
]]&gt;
</DnaLibrary>
</pre>
<p>In reviewing the differences between the VB and C# versions, the first order of business was to add the language specification to &#8220;CS&#8221; for C# so that ExcelDNA knows to switch from the default language of VB. Aside from the obvious differences between the two languages, we have added a reference to our NMathShared assembly and an explicit reference to using ExcelDNA.Integration.  In general, the C# code is slightly simpler than VB and enables very simple code to call the library.  In our C# sample code function for creating a <code>DoubleMatrix</code> with a random generation, we used the library&#8217;s built in conversion function <code>ToArray()</code> to marshal the data into an array format for Excel.  Using NMath&#8217;s built-in array handling capabilities we can simply copy data in and out in the proper formats.  This illustrates the ease by which we can utilize the NMath math libraries and Excel.</p>
<p>To further demonstrate this capability, we can add the following code to our NMathExcel.dna text file that calls the matrix transpose function in NMath.  We will show how to create array data in Excel, pass it to the library in the proper format, and then return a result for display.</p>
<pre lang="csharp">
   public static double[,] NDoubleMatrixTranspose(double[,] InputAr)
   {
      DoubleMatrix TempAr = new DoubleMatrix(InputAr);
     TempAr = TempAr.Transpose();
      return TempAr.ToArray();
   }
</pre>
<p>Make sure you close Excel and reopen it after saving your code changes to the .DNA file so that ExcelDNA has the opportunity to incorporate the new code.</p>
<p>We can now send our library a range of data cells to be acted on and display the result.  In the following screen shot I show setting up a 5 by 5 range with values to be transposed.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic1.gif"><img decoding="async" loading="lazy" width="829" height="603" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic1.gif" alt="" title="Creating a 5x5 matrix with data" class="alignnone size-full wp-image-3071" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic1.gif 829w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic1-300x218.gif 300w" sizes="(max-width: 829px) 100vw, 829px" /></a></p>
<p>We can now select an available empty cell to insert our transpose computation.  The following screenshot shows selecting the input range for calling our NMath transpose function.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic2.gif"><img decoding="async" loading="lazy" width="993" height="698" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic2.gif" alt="" title="Selecting a range of value to send to the Transpose function  class="alignnone size-full wp-image-3073" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic2.gif 993w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic2-300x210.gif 300w" sizes="(max-width: 993px) 100vw, 993px" /></a></p>
<p>As in our previous blog, the returned result is stored in a single cell and only one value is displayed.  As a quick review to display the full result, first paint (select) the area for the data to be displayed with the function call in the upper left hand corner, then press the F2 key, next press Ctrl-Shift (hold), followed by the Enter key &#8211; this will build the array formula to populate the selected area with the result.  Your result should look like the following screen.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic3.gif"><img decoding="async" loading="lazy" width="773" height="555" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic3.gif" alt="" title="Completed example of the matrix transpose function" class="alignnone size-full wp-image-3074" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic3.gif 773w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic3-300x215.gif 300w" sizes="(max-width: 773px) 100vw, 773px" /></a></p>
<p>As a further example of what is possible, we can tackle an least squares example from the NMath documentation.  The problem is to calculate the residual norm square using the Cholesky method, something that would be awkward to do natively in Excel alone. In this example the inputs are the number of rows and columns of a <code>DoubleMatrix</code> uniformly filled with random numbers. To accomplish this we need to add the following code to our DNA text file.</p>
<pre lang="csharp">
using CenterSpace.NMath.Matrix;
  .
  .
public static object NDoubleCholeskyLeastSqRNS(int rsize, int csize, int RandLBx, int RandUBx)
{
	RandGenUniform randomGenUniform = new RandGenUniform(RandLBx,RandUBx);
	randomGenUniform.Reset(0x124);
	DoubleMatrix TempAr = new DoubleMatrix(rsize,csize,randomGenUniform);
	DoubleCholeskyLeastSq cholLsq = new DoubleCholeskyLeastSq(TempAr);
	if (cholLsq.IsGood)
	{
 		DoubleVector b = new DoubleVector(TempAr.Rows,randomGenUniform);
		DoubleVector x = cholLsq.Solve(b);
		return cholLsq.ResidualNormSqr(b);
	}
	else
		return "The random matrix doesn't have full rank";
}
</pre>
<h3> Summary </h3>
<p>Approaching the complex computations with Excel by performing the necessary computation by calling a C# library with data passed in from Excel, creates a robust approach to solving problems.   The following screenshot shows how providing our function with the input parameters from the example produces the desired result.</p>
<p><a href="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic4.gif"><img decoding="async" loading="lazy" width="870" height="418" src="https://www.centerspace.net/blog/wp-content/uploads/2011/01/Blog2pic4.gif" alt="" title="Using the Cholesky Least Squares method in Excel to solve." class="alignnone size-full wp-image-3077" srcset="https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic4.gif 870w, https://www.centerspace.net/wp-content/uploads/2011/01/Blog2pic4-300x144.gif 300w" sizes="(max-width: 870px) 100vw, 870px" /></a></p>
<p>If we tried to implement the solution line by line in Excel, we would be loosing the power of an object language like C# and powerful third-party .NET libraries, and increasing the generation of tedious Excel code without a strong development environment. </p>
<p>To wrap up, combining Excel, ExcelDNA, and the CenterSpace&#8217;s NMath libraries can be used to quickly generate solutions to complex problems without an extensive programming environment.  In my next blog, I plan to solve a curve fitting example with this approach and using more of Excel features to display the results.</p>
<p>Mike Magee</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries">Using C# and ExcelDNA to call .NET Libraries</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/using-c-and-exceldna-to-call-net-libraries/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3058</post-id>	</item>
		<item>
		<title>Calling External .NET Libraries from Excel</title>
		<link>https://www.centerspace.net/calling-external-net-libraries-from-excel</link>
					<comments>https://www.centerspace.net/calling-external-net-libraries-from-excel#comments</comments>
		
		<dc:creator><![CDATA[CenterSpace]]></dc:creator>
		<pubDate>Thu, 09 Dec 2010 06:10:05 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[Marketing]]></category>
		<category><![CDATA[NMath Stats Tutorial]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[excel interop]]></category>
		<category><![CDATA[NMath and Excel]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=2845</guid>

					<description><![CDATA[<p>There are many circumstances where you may need to access an external library of functions or routines from Excel.  For example, if you need a complex function such as fitting data to a surface, or portfolio optimization, that is not natively available in Excel.  There also may be a need to protect proprietary calculations by [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/calling-external-net-libraries-from-excel">Calling External .NET Libraries from Excel</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>There are many circumstances where you may need to access an external library of functions or routines from Excel.  For example, if you need a complex function such as fitting data to a surface, or portfolio optimization, that is not natively available in Excel.  There also may be a need to protect proprietary calculations by using user defined functions to process algorithms in a black box manner.  I was looking for a way to rapidly prototype some calculations without setting up a complex development environment.</p>
<p>Harking back to the old rule of development projects of  “two out of three”, when the three metrics are fast, cheap, and quality.  On any time limited project you only can plan to achieve two metrics, never all three. Initially I like to dive in and start with fast and cheap and work my way towards quality as necessary.  So, we&#8217;ll start with the quick and dirty approach to calling external libraries from Excel.</p>
<h3>Project Setup</h3>
<p>You must have a version of .NET Framework of 2.0 or greater.  The latest .NET version is free and easily downloaded from Microsoft.</p>
<p>You&#8217;ll also need:</p>
<ul>
<li>Excel 97 or later.</li>
<li>External library assemblies that you need to access from Excel. In our case we will use Centerspace’s <strong>NMath.dll</strong> and <strong>NMathStats.dll</strong>.</li>
<li>A freeware product called ExcelDNA written by Govert van Drimmelen that can be downloaded at <a href="https://excel-dna.net/">Excel-DNA</a> .</li>
</ul>
<p>The first order of business is to unpack the downloaded file, <strong>ExcelDNA.zip</strong>, into a working directory.  For our example, we will use <em>CenterSpaceExcel</em> as our directory name. After unpacking you should have two folders <em>Distribution</em> and <em>Source</em> in our <em>CenterSpaceExcel</em> directory.  Inside the <em>Distribution</em> folder locate the file <strong>ExcelDNA.xll</strong> and rename it to <strong>NMathExcel.xll</strong>.</p>
<p>We now need to locate in the same directory the file <strong>ExcelDna.dna</strong> and rename it to <strong>NMathExcel.dna</strong>.  Then using notepad, or your favorite code editor, and open the file <strong>NMathExcel.dna</strong>.</p>
<p>You should see the following code:</p>
<pre lang="vb">
<DnaLibrary>
<![CDATA[ 
     Public Module Module1   
       Function AddThem(x, y)      
          AddThem = x + y   
       End Function 
    End Module 
]]&gt;
</DnaLibrary></pre>
<div  mce_tmp="1">Assuming CenterSpace NMath and NStat are installed in the standard locations. Change it to read as follows and save:</div>
<pre lang="vb">
<DnaLibrary>

<Reference Name="CenterSpace.NMath.Core" />
<Reference Path="C:\Program Files\CenterSpace\NMath 4.1\Assemblies\NMath.dll" />
<Reference Name="CenterSpace.NMath.Stats" />
<Reference Path="C:\Program Files\CenterSpace\NMath Stats 3.2\Assemblies\NMathStats.dll" />

<![CDATA[
	Imports NMath = CenterSpace.NMath.Core
	Imports Stats = CenterSpace.NMath.Stats

	Public Module NMathExcel

		<ExcelFunction(Description:="Returns x to the y power")> _
	    	Function NPower(x as double, y As double) As double
			NPower = NMath.NMathFunctions.PowFunction(x, y)
		End Function

		<ExcelFunction(IsMacroType:=True, IsVolatile:=True)> _
		Function NRand() As double
			dim rand As New NMath.RandGenMTwist
			NRand = rand.Next53BitRes()
		End Function

		<ExcelFunction(Description:="Binomial Distribution: Number of Successes, Trials, Probability, Cumulative is True or False")> _
		Function NBinomDist(NSuccess As Int32, NTrials As Int32, Prob As double, Cumul As Boolean) As double
			dim nbin As New Stats.BinomialDistribution
			nbin.N = NTrials
			nbin.P = Prob
			IF Cumul
				NBinomDist = nbin.CDF(NSuccess)
			Else
				NBinomDist = nbin.PDF(NSuccess)
			End If
		End Function

		Function NDoubleMatrixRand(rsize As integer, csize As integer, RandLBx As integer, RandUBy As integer) As Object(,)
			dim rng As New NMath.RandGenUniform(RandLBx,RandUBy)
			Rng.Reset(&#038;H124)
			dim TempA As New NMath.DoubleMatrix(rsize, csize, Rng)
			NDoubleMatrixRand = NCopyArray(TempA, rsize, csize)

		End Function

		Function NCopyArray(IMatrix As Object, rsize As integer, csize As integer) As Object(,)
			dim i As Integer
			dim j As Integer
			dim OArray(rsize, csize) As Object
			for i = 0 to rsize - 1
			   for j = 0 to csize - 1
				OArray(i,j) = IMatrix(i,j)
			   next j
			next i
			NCopyArray = OArray
		End Function		

	End Module
]]&gt;</pre>
<p>We now have created the VB code to call our CenterSpace Math and Statistics libraries with the following five functions.</p>
<ol>
<li>The first function shows a simple math library call to the Power function which takes a number x and raises it to the y power and returns the value.</li>
<li>The second function shows a call to obtain a fast random number from the math library.  Since we want a new number each time the spreadsheet is re-calculated we have made the function <code>volatile</code>.</li>
<li>The third function call shows how to set values that need to be accessed by a function in our .NET assemble; in this case, the Binomial Distribution.</li>
<li>The fourth function demonstrates the creation of a <code>DoubleMatrix</code> that is the filled with random uniformly distributed numbers.</li>
<li>The fifth function is a helper sub-routine to transfer data across the com interface.</li>
</ol>
<h3>Test our setup in Excel</h3>
<p>Open Excel and move your cursor the <span style="text-decoration: underline;">Tools</span> menu item.  Usually towards the bottom of the drop down menu you will find the selection <span style="text-decoration: underline;">Add-Ins</span>.  After selecting <span style="text-decoration: underline;">Add-Ins</span>, you see the pop-up window with the option to select Microsoft supplied Add-ins.  Choose the <span style="text-decoration: underline;">Browse</span> option and go to the working directory we created at the beginning.  In our case, this will be the <em>CenterSpaceExcel</em> directory.  Next select the <em>Distribution</em> folder and you should see the renamed file: <strong>NMathExcel.xll</strong>.  Select it and you should now see the following screen.</p>
<figure id="attachment_2899" aria-describedby="caption-attachment-2899" style="width: 360px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/ExcelAddin1.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2899" title="ExcelAddin" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/ExcelAddin1.gif" alt="" width="360" height="422" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/ExcelAddin1.gif 360w, https://www.centerspace.net/wp-content/uploads/2010/12/ExcelAddin1-255x300.gif 255w" sizes="(max-width: 360px) 100vw, 360px" /></a><figcaption id="caption-attachment-2899" class="wp-caption-text">Selecting a user created XLL as an Add-in for Excel</figcaption></figure>
<p>Make sure NMathExcel is checked and click OK. If you get an error and this point it is probably due to a typo in the DNA file, otherwise you will get the expected new sheet ready for entry.</p>
<p>Select an empty cell and then select from the menu bar <span style="text-decoration: underline;">Insert</span> then from the pulldown <span style="text-decoration: underline;">Function</span>.  You should see the following pop-up.</p>
<figure id="attachment_2902" aria-describedby="caption-attachment-2902" style="width: 540px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsertFunction.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2902" title="InsertFunction" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsertFunction.gif" alt="" width="540" height="423" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsertFunction.gif 540w, https://www.centerspace.net/wp-content/uploads/2010/12/InsertFunction-300x235.gif 300w" sizes="(max-width: 540px) 100vw, 540px" /></a><figcaption id="caption-attachment-2902" class="wp-caption-text">Selecting the category containing our NMath functions</figcaption></figure>
<p>At the bottom of the category pull down you should see our NMathExcel Functions;  Select it and you should have these options.:</p>
<figure id="attachment_2905" aria-describedby="caption-attachment-2905" style="width: 540px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsNMathFctn.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2905" title="InsNMathFctn" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsNMathFctn.gif" alt="" width="540" height="427" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsNMathFctn.gif 540w, https://www.centerspace.net/wp-content/uploads/2010/12/InsNMathFctn-300x237.gif 300w" sizes="(max-width: 540px) 100vw, 540px" /></a><figcaption id="caption-attachment-2905" class="wp-caption-text">NMath Excel Function Category</figcaption></figure>
<p>If we choose <code>NPower</code>, we will get the next screen,</p>
<figure id="attachment_2906" aria-describedby="caption-attachment-2906" style="width: 540px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsNMathFctnPwrArg.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2906" title="InsNMathFctnPwrArg" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsNMathFctnPwrArg.gif" alt="" width="540" height="359" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsNMathFctnPwrArg.gif 540w, https://www.centerspace.net/wp-content/uploads/2010/12/InsNMathFctnPwrArg-300x199.gif 300w" sizes="(max-width: 540px) 100vw, 540px" /></a><figcaption id="caption-attachment-2906" class="wp-caption-text">Calling NMath Library Power function in Excel</figcaption></figure>
<p>I arbitrarily typed the value of 3.2 for x and 3.327 for y.  You can see the result of 47.9329301 before selecting OK.</p>
<p>Select OK and Excel will insert the value into the cell.  Select another blank cell and this time choose our <code>NRand()</code> function.  You will notice there is no opportunity to enter values and finish by selecting OK.  At this point you should see a number between 0 and 1 in the cell.  Each time you press F9 (sheet recalc) a new random number will appear.  If we had not made this function volatile the number would not change unless you edit the cell.</p>
<p>To test our Binomial Distribution function, again we will select a new cell and use the insert function option to insert the <code>NBinomDist</code> function with the following values.</p>
<figure id="attachment_2909" aria-describedby="caption-attachment-2909" style="width: 540px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsNMathFctnBinomArg.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2909" title="InsNMathFctnBinomArg" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsNMathFctnBinomArg.gif" alt="" width="540" height="361" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsNMathFctnBinomArg.gif 540w, https://www.centerspace.net/wp-content/uploads/2010/12/InsNMathFctnBinomArg-300x200.gif 300w" sizes="(max-width: 540px) 100vw, 540px" /></a><figcaption id="caption-attachment-2909" class="wp-caption-text">Calling NMath Statistical function Binomial Distribution from Excel</figcaption></figure>
<p>At this point we have made successful calls into both of CenterSpace&#8217;s NMath and NMath Stats .NET math libraries.</p>
<p>In our fourth example, we will see how Excel handles matrices and look at issues passing array arguments across the COM interface.  Excel 2003 was limited to a maximum of 60,000 cells in an array, but Excel 2007 was expanded to handle 2 million.  Excel has some quirky ways of displaying matrices, and I&#8217;ll cover the in&#8217;s and out&#8217;s of these quirks.</p>
<p>We have written the basic code to set up a function called <code>NDoubleMatrixRand</code> for the purpose of creating a matrix with supplied dimensions and filled with uniform Random numbers over a specified distribution.  We will select another blank cell and again go to insert function and this time choose <code>NDoubleMatrixRand</code>.  Suppose we want to create a 6&#215;6 matrix filled with random numbers between -2 and 2.  Our input will look like the following screen.</p>
<figure id="attachment_2910" aria-describedby="caption-attachment-2910" style="width: 600px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsDblMtrxRandArg.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2910" title="InsDblMtrxRandArg" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsDblMtrxRandArg.gif" alt="" width="600" height="421" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsDblMtrxRandArg.gif 600w, https://www.centerspace.net/wp-content/uploads/2010/12/InsDblMtrxRandArg-300x210.gif 300w" sizes="(max-width: 600px) 100vw, 600px" /></a><figcaption id="caption-attachment-2910" class="wp-caption-text">Creating a DoubleMatrix in Excel using NMath</figcaption></figure>
<p>Notice the equal sign in the middle right of the above screen is equal to {-0.994818527251482,-0.08</p>
<p>Values inclosed in curly brackets that are separated by commas indicates that an matrix was actually created, but you can see the Formula result is only displaying a partial value due to display size. At this point when you select OK, you will have a cell with a single value.  Here is where the fun begins.  Start at the cell and drag a 6&#215;6 range as shown in the following screen.</p>
<figure id="attachment_2911" aria-describedby="caption-attachment-2911" style="width: 564px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsDblMtrxRandDsply.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2911" title="InsDblMtrxRandDsply" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsDblMtrxRandDsply.gif" alt="" width="564" height="274" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsDblMtrxRandDsply.gif 564w, https://www.centerspace.net/wp-content/uploads/2010/12/InsDblMtrxRandDsply-300x145.gif 300w" sizes="(max-width: 564px) 100vw, 564px" /></a><figcaption id="caption-attachment-2911" class="wp-caption-text">Selecting the area the matrix is to be displayed in</figcaption></figure>
<p>Now get your fingers limbered. Here is where it gets a bit obscure &#8211; do exactly as follows.</p>
<ul>
<li>Press the F2 key.  (<em>pressing F2 may be  optional but is recommended by Excel as the cell leaves the edit mode</em>)</li>
<li>Press and hold the Ctrl key followed by</li>
<li>pressing and holding the Shift key followed by</li>
<li>pressing the Enter key</li>
</ul>
<p>and presto chango!  You should see a screen like this.</p>
<figure id="attachment_2913" aria-describedby="caption-attachment-2913" style="width: 561px" class="wp-caption alignnone"><a href="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsDblMtrxRandDsplyRslt.gif"><img decoding="async" loading="lazy" class="size-full wp-image-2913" title="InsDblMtrxRandDsplyRslt" src="https://www.centerspace.net/blog/wp-content/uploads/2010/12/InsDblMtrxRandDsplyRslt.gif" alt="" width="561" height="211" srcset="https://www.centerspace.net/wp-content/uploads/2010/12/InsDblMtrxRandDsplyRslt.gif 561w, https://www.centerspace.net/wp-content/uploads/2010/12/InsDblMtrxRandDsplyRslt-300x112.gif 300w" sizes="(max-width: 561px) 100vw, 561px" /></a><figcaption id="caption-attachment-2913" class="wp-caption-text">Displaying a Matrix in Excel </figcaption></figure>
<p>Notice that your cell&#8217;s formula is now enclosed in { }, indicating to Excel that the contained formula is an array function.  This is the only way to get matrices displayed.  Also, if you try to edit this cell you will get an error that changes are not allowed.  If  you want to change the dimensions simply reference the values from another cell when you create the function.</p>
<p>The fifth function <code>NCopyArray</code> copies the library matrix across the COM bridge into an Excel array object.  As I stated in the beginning this would be a quick and dirty approach and would leave room for improvement.</p>
<h3>Summary</h3>
<p>In my next post, I will provide the above code in C# and add more function calls with some matrices with hopefully an improved approach to <code>NCopyArray</code>.  Future posts will include creating a packaged XLL and a more complex example such as curve fitting.</p>
<p>Since time is our most precious asset, being able to quickly access complex math functions with a general purpose tool like Excel should save time and money!</p>
<p>At CenterSpace, we are interested if this blog is helpful and if there is a need for more examples of how our libraries can be accessed by Excel.  Let us know what areas are of interest to you.</p>
<p>Mike  Magee</p>
<p><strong>Thanks and Resources </strong><br />
Also, a special thanks to Govert van Drimmelen for writing a wonderful tool such as ExcelDNA.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/calling-external-net-libraries-from-excel">Calling External .NET Libraries from Excel</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/calling-external-net-libraries-from-excel/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2845</post-id>	</item>
		<item>
		<title>Excel Trendlines</title>
		<link>https://www.centerspace.net/excel-trendlines</link>
					<comments>https://www.centerspace.net/excel-trendlines#comments</comments>
		
		<dc:creator><![CDATA[Ken Baldwin]]></dc:creator>
		<pubDate>Thu, 25 Mar 2010 15:45:35 +0000</pubDate>
				<category><![CDATA[Excel]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[.NET exponential regression]]></category>
		<category><![CDATA[.NET linear regression]]></category>
		<category><![CDATA[.NET logarithmic regression]]></category>
		<category><![CDATA[.NET moving average filter]]></category>
		<category><![CDATA[.NET polynomial regression]]></category>
		<category><![CDATA[.NET power regression]]></category>
		<category><![CDATA[C# exponential regression]]></category>
		<category><![CDATA[C# linear regression]]></category>
		<category><![CDATA[C# logarithmic regression]]></category>
		<category><![CDATA[C# moving average filter]]></category>
		<category><![CDATA[C# polynomial regression]]></category>
		<category><![CDATA[C# power regression]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=1870</guid>

					<description><![CDATA[<p>We are sometimes asked how to reproduce the various Excel Trendline types in NMath: Linear, Logarithmic, Exponential, Power, Polynomial, and Moving Average. In this post, we show you how to compute each trendline using NMath, including printing out the form of the equation and the R2 value (coefficient of determination). </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/excel-trendlines">Excel Trendlines</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We are sometimes asked how to reproduce the various Excel Trendline types in NMath, including printing out the form of the equation and the R2 value (coefficient of determination). Excel offers these trend types:</p>
<ul>
<li>Linear Trendline</li>
<li>Logarithmic Trendline</li>
<li>Exponential Trendline</li>
<li>Power Trendline</li>
<li>Polynomial Trendline</li>
<li>Moving Average Trendline</li>
</ul>
<p>These can all be easily computed using NMath. Let&#8217;s see how. First, let&#8217;s start with a simple data series:</p>
<pre lang="csharp">DoubleVector x = new DoubleVector(11, 15, 18, 23, 26, 31, 39, 44, 54, 64, 74);
DoubleVector y = new DoubleVector(0.00476, 0.0105, 0.0207, 0.0619, 0.337, 0.74, 1.7, 2.45, 3.5, 4.5, 5.09);</pre>
<p>These data represent the evolution of an algal bloom in the Adriatic Sea. The x-values are time expressed in days, and the y-values are the size of the surface of the bloom in mm<sup>2</sup>.</p>
<h3>Linear Trendline</h3>
<p>The Linear trendline fits a line, <code>y = a*x + b</code>, to the data. This is easily computed using the LinearRegression class in NMath Stats, as shown in the following C# code:</p>
<pre lang="csharp">bool addIntercept = true;
LinearRegression lr = new LinearRegression(new DoubleMatrix(x), y, addIntercept);
LinearRegressionAnova lrAnova = new LinearRegressionAnova(lr);
double a = lr.Parameters[0];
double b = lr.Parameters[1];
double r2 = lrAnova.RSquared;

Console.WriteLine("y = {0}*x + {1}", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output</p>
<pre class="code">y = 0.0913403802489977*x -1.63908651994092
r2 = 0.967828167696715</pre>
<p>Note that to compute the coefficient of determination (R2), we construct a LinearRegressionAnova object from the LinearRegression instance. This class tests overall model significance for regressions computed by LinearRegression.</p>
<h3>Logarithmic Trendline</h3>
<p>The Logarithmic trendline fits a line to ln(x), y&#8211;that is, <code>y = a*ln(x) + b</code>. Again, we can use the LinearRegression class for this:</p>
<pre lang="csharp">DoubleVector logX = NMathFunctions.Log(x);
lr = new LinearRegression(new DoubleMatrix(logX), y, addIntercept);
a = lr.Parameters[0];
b = lr.Parameters[1];
r2 = new LinearRegressionAnova(lr).RSquared;

Console.WriteLine("y = {0}*ln(x) + {1}", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = -8.17805531083818*ln(x) + 2.87283105614145
r2= 0.840393353070466</pre>
<h3>Exponential Trendline</h3>
<p>The Exponential trendline fits the function <code>y = a * e ^(b * x)</code>. This can also be computed using LinearRegression by fitting a line to x, ln(y). Taking the log of both sides of the function gives:</p>
<pre>ln(y) = ln(a * e ^(b * x)) = ln(a) + bx</pre>
<p>The following C# code does this:</p>
<pre lang="csharp">DoubleVector logY = NMathFunctions.Log(y);
lr = new LinearRegression(new DoubleMatrix(x), logY, addIntercept);
a = Math.Exp(lr.Parameters[0]);
b = lr.Parameters[1];
r2 = new LinearRegressionAnova(lr).RSquared;

Console.WriteLine("y = {0}*e^{1}*x", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = 0.00552689525130073*e^0.112876522212724*x
r2 = 0.812366433832176</pre>
<p>Note that because the intercept of the fitted line is the natural log of the &#8220;a&#8221; parameter in the exponential function, we need to take the exponential of the found intercept (using <code>Math.Exp</code>) to recover the value of &#8220;a&#8221;.</p>
<h3>Power Trendline</h3>
<p>The Power trendline fits the function <code>y = a * x^b</code>. This can be computed using LinearRegression by fitting a line to ln(x), ln(y). Taking the log of both sides of the equation gives:</p>
<pre>ln(y) = ln(a * x^b) = ln(a) + b * ln(x)</pre>
<pre lang="csharp">lr = new LinearRegression(new DoubleMatrix(logX), logY, addIntercept);
lrAnova = new LinearRegressionAnova(lr);
a = Math.Exp(lr.Parameters[0]);
b = lr.Parameters[1];
r2 = new LinearRegressionAnova(lr).RSquared;

Console.WriteLine("y = {0}*x^{1}", a, b);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = 2.46993343563889E-07*x^4.11443630332377
r2 = 0.947447653331871</pre>
<p>Again, we recover the value of parameter &#8220;a&#8221; by taking the exponential of the found intercept.</p>
<h3>Polynomial Trendline</h3>
<p>NMath provides class PolynomialLeastSquares for fitting a polynomial of the specified degree to paired vectors of x- and y-values. For example, this code fits a cubic to our data:</p>
<pre lang="csharp">int degree = 3;
PolynomialLeastSquares pls = new PolynomialLeastSquares(degree, x, y);

// compute R2
DoubleVector predictions = pls.FittedPolynomial.Evaluate(x);
double regressionSumOfSquares = StatsFunctions.SumOfSquares(predictions - StatsFunctions.Mean(y));
double residualSumOfSquares = pls.LeastSquaresSolution.Residuals.TwoNormSquared();
r2 = regressionSumOfSquares / (regressionSumOfSquares + residualSumOfSquares);

Console.WriteLine("y = " + pls.FittedPolynomial);
Console.WriteLine("r2 = {0}", r2);</pre>
<p>Output:</p>
<pre class="code">y = -4.68278158094222E-05x^3 + 0.00640408381023593x^2 - 
      0.1643720340709x + 1.12703300920657
r2 = 0.998634459376868</pre>
<p>Note that PolynomialLeastSquares does not currently provide the R2 value, so in the code above we compute it directly.</p>
<h3>Moving Average Trendline</h3>
<p>Unlike the trendlines we&#8217;ve examined so far, a moving average does not fit a functional form to data. Rather, it filters data in order to smooth out noise. NMath provides class MovingWindowFilter for this purpose.</p>
<p>Class MovingWindowFilter replaces data points with a linear combination of the data points immediately to the left and right of each point, based on a given set of coefficients to use in the linear combination. Static class methods are provided for generating coefficients to implement a moving average filter and a Savitzky-Golay smoothing filter.</p>
<p>For example, the following C# code filters the data using a window of width 3 (the &#8220;period&#8221; parameter in Excel):</p>
<pre lang="csharp">int numberLeft = 1;
int numberRight = 1;
DoubleVector movingAvgCoefficents = MovingWindowFilter.MovingAverageCoefficients(numberLeft, numberRight);
MovingWindowFilter filter = new MovingWindowFilter(numberLeft, numberRight, movingAvgCoefficents);
DoubleVector yfiltered = filter.Filter(y, MovingWindowFilter.BoundaryOption.DoNotFilterBoundaryPoints);
Console.WriteLine("yfiltered = " + yfiltered);</pre>
<p>Output:</p>
<pre class="code">yfiltered = [ 0.00476 0.0119866666666667 0.0310333333333333
              0.139866666666667 0.379633333333333 0.925666666666667 1.63
              2.55 3.48333333333333 4.36333333333333 5.09 ]</pre>
<h3>Advanced Curve Fitting</h3>
<p>That covers the simple trendlines produced by Excel. For advanced curve fitting, NMath provides class OneVariableFunctionFitter which fits arbitrary one variable functions to a set of points. (You must supply at least as many data points to fit as your function has parameters.) In a future post, we&#8217;ll take a look at this class. In the meantime, see <a href="https://www.centerspace.net/examples/nmath/csharp/one-variable-curve-fitting-example.php">this page</a> for an example demonstrating the use of OneVariableFunctionFitter, including how to define your own function.</p>
<p>Ken</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/excel-trendlines">Excel Trendlines</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/excel-trendlines/feed</wfw:commentRss>
			<slash:comments>1</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1870</post-id>	</item>
		<item>
		<title>Using Excel with NMath</title>
		<link>https://www.centerspace.net/using-excel-with-nmath</link>
					<comments>https://www.centerspace.net/using-excel-with-nmath#respond</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Mon, 15 Feb 2010 12:24:15 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[Support]]></category>
		<category><![CDATA[calling C# from VBA]]></category>
		<category><![CDATA[calling excel from .NET assembly]]></category>
		<category><![CDATA[COM .NET interop]]></category>
		<category><![CDATA[Excel functions]]></category>
		<category><![CDATA[Excel math functions]]></category>
		<category><![CDATA[Excel port c#]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=1043</guid>

					<description><![CDATA[<p><img src="http://s84547.gridserver.com/blog/./blog/wp-content/uploads/2010/02/233px-Microsoft_Excel_Icon.svg_.png" alt="" title="Excel Icon"  class="excerpt" /><br />
We've had several customers ask about porting their Excel model to a .NET language in order to leverage the performance and functionality of NMath or NMath Stats. NMath does have good crossover functionality with Excel making this porting job easier. It is also possible to accelerate your Excel models by calling the NMath .NET assemblies directly from a VBA macro in Excel. This post provides some guidance for porting all or just a portion of your Excel model to C# and NMath.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-excel-with-nmath">Using Excel with NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>We&#8217;ve had several customers ask about porting their Excel model to a .NET language in order to leverage the performance and functionality of NMath or NMath Stats. NMath does have good crossover functionality with Excel making this porting job easier. It is also possible to accelerate your Excel models by calling the NMath .NET assemblies directly from a VBA macro in Excel . This post provides some guidance for porting all or just a portion of your Excel model to C# and NMath.</p>
<p>Excel is designed to interoperate with external assemblies from VBA using COM.  Type libraries built in .NET are not directly COM compatible, however all .NET class libraries including NMath and NMath Stats can be made to present a COM interface making Excel interop possible. This interop is acheived by building the <em>COM type library</em> directly from the assembly &#8211; no recompiling needed &#8211; using a tool shipped with the .NET framework, and then adding this type library as a reference to an Excel sheets&#8217; VBA macro.  Because of the many differences between the C# language and VBA, only a small portion of NMath will be accessable from Excel using this procedure, however a simple remedy will be outlined below that can expand the available functionality to all of NMath.</p>
<ol>
<li>To build the type library interface to the NMath.dll use the <code>regasm.exe</code> utility shipped with the .NET framework <quote>
<pre lang="c"> >regasm.exe NMath.dll /tlb:NMathCom.tlb </pre>
<p> </quote>  The COM compatible type library now resides in the <code> NMathCom.tlb</code> file.  You will see some warning messages regarding incompatibilities between COM and NMath.</p>
<li>Open a spreadsheet, right click on a sheet tab and choose <b>View Code</b>  to open the VBA development environment.
<li>In the <b>Tools</b> menu select <b>References&#8230;</b> and browse to the location of the new NMath type library.
</ol>
<p>Now the COM compatible portions of NMath are now available for use from VBA.  At this point we can code up simple example for generating random numbers in VBA to test out the NMath interoperability</p>
<pre lang="vbnet">
Private Sub TestNMath()
  Dim rand As New NMath.RandGenLogNormal
  rand.Mean = 50
  rand.Variance = 10

  Dim i As Integer
  For i = 1 To 10
    Cells(i, 1) = rand.Next()
  Next
End Sub
</pre>
<p>This simple VBA script populates cells A1:A10 with LogNormal distributed random numbers with a mean of 50 and a variance of 10.  This is not so easily achieved within Excel natively.<br />
<span id="more-1043"></span><br />
Because NMath was not designed from the ground up to interoperate with the aging COM standard, many types will not be useable from VBA.  All types lacking an empty (default) constructor, generic classes, static methods, and all static classes are not directly usable from VBA.  However, any of these incompatible classes or methods can be wrapped in VB and then made available to Excel via COM.  We understand many Excel users are not C#/.NET experts and so we are happy to help wrapping any <a href="/nmath/">NMath classes</a> or <a href="/nmath-stats/">NMath Stats classes</a> you may need for enhancing and accelerating your Excel models.    </p>
<h2> Porting Excel Models to C# using NMath </h2>
<p>As models grow in Excel, they commonly devolve into a  bizantine workbook that becomes slow, opaque, and difficult to version, debug and manage.  Usually, it is at this point that users start looking at porting the complex portions to another compatible platform, typically VB.  These workbooks tend to be business critical applications so the port must be done carefully and in a piecewise fashion.   This conservative strategy can be acheived using the following steps.</p>
<ol>
<li> Identify the computationally demanding and complex portion of the spreadsheet carefully selecting a separation point where the interface is thin between the workbook and the new VB class.
<li> Identify all of the input and output cells of this computation.
<li> Create build a VB wrapper class in a (class library) VB project that wraps the functionality necessary for the port.  This class should include methods for loading and returning results in types compatible with VBA.
<li> Build the library and generate the COM type library and add this new type library to the workbook&#8217;s VBA.
<li> Test the new model in parallel with the existing model.
<li> Remove the duplicated Excel computation and enjoy the new faster model.
</ol>
<p>Porting functionality from an Excel model to a VB type library can be facilitated by NMath due to the large crossover in functionality between Excel and NMath Stats.  Also, due to NMath&#8217;s high performance, once ports are complete, considerable performance gains can be expected.  Below is a table of Excel functions and their supporting classes in either NMath Stats or native .NET.   </p>
<p>Happy Computing,</p>
<p>-Paul</p>
<p><em> Resources </em></p>
<ul>
<li>Microsoft ported the Excel financial functions to F#, making them accessible to any VB or C# project.  Follow this <a href="http://code.msdn.microsoft.com/FinancialFunctions">link</a> to download this library and read the libraries&#8217; documentation and limitations.
<li>Microsoft&#8217;s landing page on the <a href="https://msdn.microsoft.com/en-us/library/tzat5yw6(VS.80).aspx">assembly registration tool</a> <code>regasm.exe</code> for building the COM type libraries.
<li>A little dated but clear and complete <a href="https://richnewman.wordpress.com/2007/04/15/a-beginner%E2%80%99s-guide-to-calling-a-net-library-from-excel/">article</a> on accessing .NET assemblies from Excel.
</ul>
<h2> Excel Functions Supported in NMath and .NET </h2>
<p>Between the NMath and NMath Stats numeric libraries and the .NET framework many of the Excel functions are covered for a port to C#.  If you have a math function that you need which is not covered or is not in this list, let us know and we can probably add it to NMath or NMath Stats.  Note resources above if financial functions are needed.</p>
<table border="1" cellpadding=5 align="center" width="500">
<tr>
<th> Excel Function </th>
<th> Framework/NameSpace </th>
<th> Class/Method</th>
<tr>
<td>ABS </p>
<td> NMath.Core </p>
<td> NMathFunctions.Abs()</p>
<tr>
<td>ACOS </p>
<td> NMath.Core</p>
<td> NMathFunctions.ACos()</p>
<tr>
<td>AND </p>
<td> NMath.Core</p>
<td> NMathFunctions.And()</p>
<tr>
<td>ASIN </p>
<td> NMath.Core</p>
<td> NMathFunctions.ASin()</p>
<tr>
<td>AREAS </p>
<td>No</p>
<td>
<tr>
<td>ASIN </p>
<td> NMath.Core</p>
<td>NMathFunctions.Asin()</p>
<tr>
<td>ASINH </p>
<td>No</p>
<td>
<tr>
<td>ATAN </p>
<td>NMath.Core</p>
<td>NMathFunctions.Atan()</p>
<tr>
<td>ATAN2 </p>
<td>NMath.Core</p>
<td>NMathFunctions.Atan2()</p>
<tr>
<td>ATANH </p>
<td>No</p>
<td>
<tr>
<td>AVEDEV </p>
<td>NMath.Stats</p>
<td>StatsFunctions.MeanDeviation()</p>
<tr>
<td>AVERAGE </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Mean()</p>
<tr>
<td>AVERAGEA </p>
<td>No</p>
<td>
<tr>
<td>BETADIST </p>
<td>NMath.Stats</p>
<td>BetaDistribution class</p>
<tr>
<td>BETAINV </p>
<td>NMath.Stats</p>
<td>InverseCDF method in BetaDistribution class</p>
<tr>
<td>BINOMDIST </p>
<td>NMath.Stats</p>
<td>BinomialDistribution class</p>
<tr>
<td>CALL </p>
<td>.NET</p>
<td>
<tr>
<td>CEILING </p>
<td>NMath.Core</p>
<td>NMathFunctions.Ceil()</p>
<tr>
<td>CELL </p>
<td>No</p>
<td>
<tr>
<td>CHAR </p>
<td>No</p>
<td>
<tr>
<td>CHIDIST </p>
<td>NMath.Stats</p>
<td>ChiSquareDistribution class</p>
<tr>
<td>CHIINV </p>
<td>NMath.Stats</p>
<td>InverseCDF method in ChiSquareDistribution class</p>
<tr>
<td>CHITEST </p>
<td>No</p>
<td>
<tr>
<td>CHOOSE </p>
<td>No</p>
<td>
<tr>
<td>CLEAN </p>
<td>No</p>
<td>
<tr>
<td>CODE </p>
<td>No</p>
<td>
<tr>
<td>COLUMN </p>
<td>No</p>
<td>
<tr>
<td>COLUMNS </p>
<td>NMath.Core</p>
<td>DataFrame class using Cols property </p>
<tr>
<td>COMBIN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Binomial()</p>
<tr>
<td>CONCATENATE </p>
<td>.NET</p>
<td>
<tr>
<td>CONFIDENCE </p>
<td>NMath.Stats</p>
<td>OneSampleZTest class using LowerConfidenceBound or UpperConfidenceBound properties </p>
<tr>
<td>CORREL </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Correlation()</p>
<tr>
<td>COS </p>
<td>NMath.Core</p>
<td>NMathFunctions.Cos()</p>
<tr>
<td>COSH </p>
<td>NMath.Core</p>
<td>NMathFunctions.Cosh()</p>
<tr>
<td>COUNT </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Count()</p>
<tr>
<td>COUNTBLANK </p>
<td>NMath.Stats</p>
<td>If blanks are represented by NaNs then use StatsFunctions.NaNCount()</p>
<tr>
<td>COUNTIF </p>
<td>NMath.Stats</p>
<td>StatsFunctions.CountIf()</p>
<tr>
<td>COVAR </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Covariance()</p>
<tr>
<td>CRITBINOM </p>
<td>NMath.Stats</p>
<td>BinomialDistribution class</p>
<tr>
<td>DATE </p>
<td>.NET</p>
<td>
<tr>
<td>DATEVALUE </p>
<td>.NET</p>
<td>
<tr>
<td>DAVERAGE </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to get Subset then StatsFunctions.Mean()</p>
<tr>
<td>DAY </p>
<td>.NET</p>
<td>
<tr>
<td>DAYS360 </p>
<td>.NET</p>
<td>
<tr>
<td>DB </p>
<td>No</p>
<td>
<tr>
<td>DCOUNT </p>
<td>NMath.Stats</p>
<td>StatsFunctions.CountIf()</p>
<tr>
<td>DCOUNTA </p>
<td>NMath.Stats</p>
<td>If blanks are represented by NaNs then use StatsFunctions.NaNCountIf()</p>
<tr>
<td>DDB </p>
<td>No</p>
<td>
<tr>
<td>DEGREES </p>
<td>No</p>
<td>
<tr>
<td>DEVSQ </p>
<td>NMath.Stats</p>
<td>StatsFunctions.SumOfSquares()</p>
<tr>
<td>DGET </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() and then index property on resulting DataFrame</p>
<tr>
<td>DMAX </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then StatsFunctions.Max()</p>
<tr>
<td>DMIN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then StatsFunctions.Min()</p>
<tr>
<td>DOLLAR </p>
<td>.NET</p>
<td>
<tr>
<td>DPRODUCT </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then to DoubleVector() then NMathFunctions.Product()</p>
<tr>
<td>DSTDEV </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then StatsFunctions.StandardDeviation(BiasType.Unbiased)</p>
<tr>
<td>DSTDEVP </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then StatsFunctions.StandardDeviation()</p>
<tr>
<td>DSUM </p>
<td>NMath.Stats</p>
<td>StatsFunctions.SumIf()</p>
<tr>
<td>DVAR </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then toDoubleVector() then NMathFunctions.Variance(BiasType.Unbiased)</p>
<tr>
<td>DVARP </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If() to create Subset then toDoubleVector() then NMathFunctions.Variance()</p>
<tr>
<td>ERROR.TYPE</p>
<td>No</p>
<td>
<tr>
<td>EVEN </p>
<td>.NET</p>
<td>
<tr>
<td>EXACT </p>
<td>.NET</p>
<td>
<tr>
<td>EXP </p>
<td>NMath.Core</p>
<td>NMathFunctions.Exp()</p>
<tr>
<td>EXPONDIST </p>
<td>NMath.Stats</p>
<td>ExponentialDistribution class</p>
<tr>
<td>FACT </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Factorial()</p>
<tr>
<td>FALSE </p>
<td>.NET</p>
<td>
<tr>
<td>FDIST </p>
<td>NMath.Stats</p>
<td>FDistribution class</p>
<tr>
<td>FIND </p>
<td>.NET</p>
<td>
<tr>
<td>FINV </p>
<td>NMath.Stats</p>
<td>InverseCDF method in FDistribution class</p>
<tr>
<td>FISHER </p>
<td>No</p>
<td>
<tr>
<td>FISHERINV </p>
<td>No</p>
<td>
<tr>
<td>FIXED </p>
<td>.NET</p>
<td>
<tr>
<td>FLOOR </p>
<td>NMath.Core</p>
<td>NMathFunctions.Floor()</p>
<tr>
<td>FORECAST </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>FREQUENCY </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Mode() for frequency, use StatsFunctions.If() to find the indices.</p>
<tr>
<td>FTEST </p>
<td>NMath.Stats</p>
<td>TwoSampleFTest class in Stats</p>
<tr>
<td>FV </p>
<td>No</p>
<td>
<tr>
<td>GAMMADIST </p>
<td>NMath.Stats</p>
<td>GammaDistribution class</p>
<tr>
<td>GAMMAINV </p>
<td>NMath.Stats</p>
<td>InverseCDF method in GammaDistribution class</p>
<tr>
<td>GAMMALN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.GammaLn()</p>
<tr>
<td>GEOMEAN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.GeometricMean()</p>
<tr>
<td>GETPIVOTDATA </p>
<td>No</p>
<td>
<tr>
<td>GROWTH </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>HARMEAN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.HarmonicMean()</p>
<tr>
<td>HLOOKUP </p>
<td>No</p>
<td>
<tr>
<td>HOUR </p>
<td>.NET</p>
<td>
<tr>
<td>HYPERLINK </p>
<td>.NET</p>
<td>
<tr>
<td>HYPGEOMDIST </p>
<td>No</p>
<td>
<tr>
<td>IF </p>
<td>NMath.Stats</p>
<td>StatsFunctions.If()</p>
<tr>
<td>INDEX </p>
<td>NMath.Core</p>
<td>Index properties in vector, matrices, columns and frames</p>
<tr>
<td>INDIRECT </p>
<td>No</p>
<td>
<tr>
<td>INFO </p>
<td>.NET</p>
<td>
<tr>
<td>INT </p>
<td>.NET</p>
<td>
<tr>
<td>INTERCEPT </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>IPMT </p>
<td>No</p>
<td>
<tr>
<td>IRR </p>
<td>No</p>
<td>
<tr>
<td>ISBLANK </p>
<td>NMath.Core</p>
<td>Can use NaN to indicate missing value. Then use Double.IsNaN(cell) to verify.</p>
<tr>
<td>ISERROR </p>
<td>No</p>
<td>
<tr>
<td>ISLOGICAL </p>
<td>No</p>
<td>
<tr>
<td>ISNA </p>
<td>No</p>
<td>
<tr>
<td>ISNONTEXT </p>
<td>No</p>
<td>
<tr>
<td>ISNUMBER </p>
<td>No</p>
<td>
<tr>
<td>ISPMT </p>
<td>No</p>
<td>
<tr>
<td>ISREF </p>
<td>No</p>
<td>
<tr>
<td>ISTEXT </p>
<td>No</p>
<td>
<tr>
<td>KURT </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Kurtosis()</p>
<tr>
<td>LARGE </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Sort() with StatsFunctions.Percentile()</p>
<tr>
<td>LEFT </p>
<td>.NET</p>
<td>
<tr>
<td>LEN </p>
<td>.NET</p>
<td>
<tr>
<td>LINEST </p>
<td>NMath.Stats</p>
<td>LeastSquares class</p>
<tr>
<td>LN </p>
<td>NMath.Core</p>
<td>NMathFunctions.Log()</p>
<tr>
<td>LOG </p>
<td>No</p>
<td>
<tr>
<td>LOG10 </p>
<td>NMath.Core</p>
<td>NMathFunctions.Log10()</p>
<tr>
<td>LOGEST </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>LOGINV </p>
<td>NMath.Stats</p>
<td>InverseCDF method in LognormalDistribution class</p>
<tr>
<td>LOGNORMDIST </p>
<td>NMath.Stats</p>
<td>LognormalDistribution class</p>
<tr>
<td>LOOKUP </p>
<td>NMath.Stats</p>
<td>IndexOf(), IndicesOf() methods in DataFrame class</p>
<tr>
<td>LOWER </p>
<td>.NET</p>
<td>
<tr>
<td>MATCH </p>
<td>No</p>
<td>
<tr>
<td>MAX </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Max()</p>
<tr>
<td>MAXA </p>
<td>No</p>
<td>
<tr>
<td>MDETERM </p>
<td>NMath.Stats</p>
<td>Use LUFact class to factorize then call Determinant() method to compute.</p>
<tr>
<td>MEDIAN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Median()</p>
<tr>
<td>MID </p>
<td>.NET</p>
<td>
<tr>
<td>MIN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Min()</p>
<tr>
<td>MINA </p>
<td>No</p>
<td>
<tr>
<td>MINUTE </p>
<td>.NET</p>
<td>
<tr>
<td>MINVERSE </p>
<td>NMath.Stats</p>
<td>Use LUFact class to factorize then call Inverse() method to compute.</p>
<tr>
<td>MIRR </p>
<td>No</p>
<td>
<tr>
<td>MMULT </p>
<td>NMath.Core</p>
<td>Call Multiply() on a matrix class</p>
<tr>
<td>MOD </p>
<td>.NET</p>
<td>
<tr>
<td>MODE </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Mode()</p>
<tr>
<td>MONTH </p>
<td>.NET</p>
<td>
<tr>
<td>N </p>
<td>No</p>
<td>
<tr>
<td>NA </p>
<td>.NET</p>
<td>
<tr>
<td>NEGBINOMDIST </p>
<td>NMath.Stats</p>
<td>NegativeBinomialDistribution class</p>
<tr>
<td>NORMDIST </p>
<td>NMath.Stats</p>
<td>NormalDistribution class</p>
<tr>
<td>NORMINV </p>
<td>NMath.Stats</p>
<td>InverseCDF() in NormalDistribution class</p>
<tr>
<td>NORMSDIST </p>
<td>NMath.Stats</p>
<td>NormalDistribution class </p>
<tr>
<td>NORMSINV </p>
<td>NMath.Stats</p>
<td>InverseCDF() in NormalDistribution class</p>
<tr>
<td>NOT </p>
<td>.NET</p>
<td>
<tr>
<td>NOW </p>
<td>.NET</p>
<td>
<tr>
<td>NPER </p>
<td>No</p>
<td>
<tr>
<td>NPV </p>
<td>No</p>
<td>
<tr>
<td>ODD </p>
<td>.NET</p>
<td>
<tr>
<td>OFFSET </p>
<td>No</p>
<td>
<tr>
<td>OR </p>
<td>.NET</p>
<td>
<tr>
<td>PEARSON </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Correlation()</p>
<tr>
<td>PERCENTILE </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Percentile()</p>
<tr>
<td>PERCENTRANK </p>
<td>NMath.Stats</p>
<td>StatsFunctions.PercentileRank()</p>
<tr>
<td>PERMUT </p>
<td>NMath.Stats</p>
<td>Factorial() in StatsFunctions</p>
<tr>
<td>PI </p>
<td>.NET System.Math.PI</p>
<td>
<tr>
<td>PMT </p>
<td>No</p>
<td>
<tr>
<td>POISSON </p>
<td>NMath.Stats</p>
<td>PoissonDistribution class</p>
<tr>
<td>POWER </p>
<td>NMath.Stats</p>
<td>NMathFunctions.Pow</p>
<tr>
<td>PPMT </p>
<td>No</p>
<td>
<tr>
<td>PROB </p>
<td>NMath.Stats</p>
<td>PDF methods in all of the distribution classes</p>
<tr>
<td>PRODUCT </p>
<td>NMath.Stats</p>
<td>NMathFunctions.Product()</p>
<tr>
<td>PROPER </p>
<td>.NET</p>
<td>
<tr>
<td>PV </p>
<td>No</p>
<td>
<tr>
<td>QUARTILE </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Quartile()</p>
<tr>
<td>RADIANS </p>
<td>.NET</p>
<td>
<tr>
<td>RAND </p>
<td>NMath.Stats</p>
<td>Many RandomNumberGenerator classes in NMath Core</p>
<tr>
<td>RANK </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Rank()</p>
<tr>
<td>RATE </p>
<td>No</p>
<td>
<tr>
<td>REGISTER.ID</p>
<td>No</p>
<td>
<tr>
<td>REPLACE </p>
<td>.NET</p>
<td>
<tr>
<td>REPT </p>
<td>.NET</p>
<td>
<tr>
<td>RIGHT </p>
<td>.NET</p>
<td>
<tr>
<td>ROMAN </p>
<td>No</p>
<td>
<tr>
<td>ROUND </p>
<td>NMath.Core</p>
<td>NMathFunctions.Round()</p>
<tr>
<td>ROUNDDOWN </p>
<td>NMath.Core</p>
<td>NMathFunctions.Round()</p>
<tr>
<td>ROUNDUP </p>
<td>NMath.Core</p>
<td>NMathFunctions.Round()</p>
<tr>
<td>ROW </p>
<td>NMath.Stats</p>
<td>Row property on DataFrame</p>
<tr>
<td>ROWS </p>
<td>NMath.Stats</p>
<td>Rows property on DataFrame</p>
<tr>
<td>RSQ </p>
<td>No</p>
<td>
<tr>
<td>SEARCH </p>
<td>.NET</p>
<td>
<tr>
<td>SECOND </p>
<td>.NET</p>
<td>
<tr>
<td>SIGN </p>
<td>.NET</p>
<td>
<tr>
<td>SIN </p>
<td>NMath.Core</p>
<td>NMathFunctions.Sin()</p>
<tr>
<td>SINH </p>
<td>NMath.Core</p>
<td>NMathFunctions.Sinh()</p>
<tr>
<td>SKEW </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Skewness() for samples or Skewness properties on Distribution classes.</p>
<tr>
<td>SLN </p>
<td>No</p>
<td>
<tr>
<td>SLOPE </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>SMALL </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Sort() and then index or find percentage of count and use StatsFunctions.Percentile()</p>
<tr>
<td>SQRT </p>
<td>NMath.Core</p>
<td>NMathFunctions.Sqrt()</p>
<tr>
<td>STANDARDIZE </p>
<td>NMath.Stats</p>
<td>NormalDistribuion class</p>
<tr>
<td>STDEV </p>
<td>NMath.Stats</p>
<td>StatsFunctions.StandardDeviation(BiasType.Unbiased)</p>
<tr>
<td>STDEVA </p>
<td>No</p>
<td>
<tr>
<td>STDEVP </p>
<td>NMath.Stats</p>
<td>StatsFunctions.StandardDeviation()</p>
<tr>
<td>STDEVPA </p>
<td>No</p>
<td>
<tr>
<td>STEYX </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>SUBSTITUTE </p>
<td>.NET</p>
<td>
<tr>
<td>SUBTOTAL </p>
<td>NMath.Stats</p>
<td>Use Slice/Subset/Range to get portion of data then StatsFunctions.Sum(), Or, StatsFunctions.SumIf()</p>
<tr>
<td>SUM </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Sum()</p>
<tr>
<td>SUMIF </p>
<td>NMath.Stats</p>
<td>StatsFunctions.SumIf()</p>
<tr>
<td>SUMPRODUCT </p>
<td>NMath.Stats</p>
<td>Use operator* on vectors then NMathFunctions.Sum()</p>
<tr>
<td>SUMSQ </p>
<td>NMath.Stats</p>
<td>StatsFunctions.SumOfSquares()</p>
<tr>
<td>SUMX2MY2 </p>
<td>NMath.Core</p>
<td>Square each vector using NMathFunctions.Pow(2) then us operator- for difference then NMathFunctions.Sum()</p>
<tr>
<td>SUMX2PY2 </p>
<td>NMath.Core</p>
<td>Square each vector using NMathFunctions.Pow(2) then use operator+ for difference then NMathFunctions.Sum()</p>
<tr>
<td>SUMXMY2 </p>
<td>NMath.Stats</p>
<td>use operator- to find difference then Product(2) then NMathFunctions.Sum()</p>
<tr>
<td>SYD </p>
<td>No</p>
<td>
<tr>
<td>T </p>
<td>No</p>
<td>
<tr>
<td>TAN </p>
<td>NMath.Core</p>
<td>NMathFunctions.Tan()</p>
<tr>
<td>TANH </p>
<td>NMath.Core</p>
<td>NMathFunctions.Tanh()</p>
<tr>
<td>TDIST </p>
<td>NMath.Stats</p>
<td>TDistribution class</p>
<tr>
<td>TEXT </p>
<td>No</p>
<td>
<tr>
<td>TIME </p>
<td>.NET</p>
<td>
<tr>
<td>TIMEVALUE </p>
<td>.NET</p>
<td>
<tr>
<td>TINV </p>
<td>NMath.Stats</p>
<td>InverseCDF() in TDistribution class</p>
<tr>
<td>TODAY </p>
<td>.NET</p>
<td>
<tr>
<td>TRANSPOSE </p>
<td>NMath.Core</p>
<td>Transpose() method on a matrix</p>
<tr>
<td>TREND </p>
<td>NMath.Stats</p>
<td>LinearRegression class in Stats</p>
<tr>
<td>TRIM </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Trim()</p>
<tr>
<td>TRIMMEAN </p>
<td>NMath.Stats</p>
<td>StatsFunctions.TrimmedMean()</p>
<tr>
<td>TRUE </p>
<td>.NET</p>
<td>
<tr>
<td>TRUNC </p>
<td>.NET</p>
<td>
<tr>
<td>TTEST </p>
<td>NMath.Stats</p>
<td>TwoSamplePairedTTest or TwoSampleUnpairedTTest classes</p>
<tr>
<td>TYPE </p>
<td>.NET</p>
<td>
<tr>
<td>UPPER </p>
<td>.NET</p>
<td>
<tr>
<td>VALUE </p>
<td>.NET</p>
<td>
<tr>
<td>VAR </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Variance(BiasType.Unbiased)</p>
<tr>
<td>VARA </p>
<td>No</p>
<td>
<tr>
<td>VARP </p>
<td>NMath.Stats</p>
<td>StatsFunctions.Variance()</p>
<tr>
<td>VARPA </p>
<td>No</p>
<td>
<tr>
<td>VDB </p>
<td>No</p>
<td>
<tr>
<td>VLOOKUP </p>
<td>NMath.Stats</p>
<td>Combination of IndexOf() method on DataFrame and index operators</p>
<tr>
<td>WEEKDAY </p>
<td>.NET</p>
<td>
<tr>
<td>WEIBULL </p>
<td>NMath.Stats</p>
<td>WeibullDistribution</p>
<tr>
<td>ZTEST </p>
<td>NMath.Stats</p>
<td>OneSampleZTest</p>
</table>
<p><!--

The following are built into .NET

CALL
CONCATENATE
DATE
DATEVALUE
DAY
DAYS360
DOLLAR
EVEN
EXACT
FALSE
FIND
FIXED
HOUR
HYPERLINK
INFO
INT
LEFT
LEN
LOWER
MID
MINUTE
MOD
MONTH
NA
NOW
ODD
PI
PROPER
RADIANS
REPLACE
REPT
RIGHT
SEARCH
SECOND
SIGN
SUBSTITUTE .NET
TIME
TIMEVALUE
TODAY
TRUE
TRUNC
TYPE
UPPER
VALUE
WEEKDAY
YEAR

These functions are ones we may add to the library. Let us know if you're interested;

AREAS
ASINH
ATANH
AVERAGEA
CELL
CHAR
CHITEST
CHOOSE
CLEAN
CODE
COLUMN
DB
DDB
DEGREES
ERROR.TYPE
FISHER
FISHERINV
FV
GETPIVOTDATA
HLOOKUP
HYPGEOMDIST
INDIRECT
IPMT
IRR
ISERROR
ISLOGICAL
ISNA
ISNONTEXT
ISNUMBER
ISPMT
ISREF
ISTEXT
LOG
MATCH
MAXA
MINA
MIRR
N
NPER
NPV
OFFSET
PMT
PPMT
PV
RATE
REGISTER.ID
ROMAN
RSQ
SLN
STDEVA
STDEVPA
SYD
T
TEXT
VARA
VARPA
VDB

--></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/using-excel-with-nmath">Using Excel with NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/using-excel-with-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">1043</post-id>	</item>
	</channel>
</rss>
