<?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>NMathStats 3.5 Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/tag/nmathstats-3-5/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/tag/nmathstats-3-5</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Wed, 18 Jul 2012 18:58:35 +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>NMath API updated with .NET Func delegates</title>
		<link>https://www.centerspace.net/nmath-api-updated-with-net-func-delegates</link>
					<comments>https://www.centerspace.net/nmath-api-updated-with-net-func-delegates#comments</comments>
		
		<dc:creator><![CDATA[Paul Shirkey]]></dc:creator>
		<pubDate>Thu, 03 May 2012 00:03:16 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Stats]]></category>
		<category><![CDATA[.NET delegates]]></category>
		<category><![CDATA[Action]]></category>
		<category><![CDATA[C# delegates]]></category>
		<category><![CDATA[C# Func]]></category>
		<category><![CDATA[Func]]></category>
		<category><![CDATA[NMath 5.2]]></category>
		<category><![CDATA[NMathStats 3.5]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3711</guid>

					<description><![CDATA[<p>At CenterSpace we are always working hard to keep the core NMath processing kernel start of the art, however changes to our libraries&#8217; API usually lag behind any .NET framework developments in a process of deprecation and introduction. The .NET Func&#60;&#62; and Action&#60;&#62; delegates have been a part of the .NET framework now since .NET [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/nmath-api-updated-with-net-func-delegates">NMath API updated with .NET Func&lt;&gt; delegates</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>At CenterSpace we are always working hard to keep the core NMath processing kernel start of the art, however changes to our libraries&#8217; API usually lag behind any .NET framework developments in a process of deprecation and introduction.  The .NET Func&lt;&gt; and Action&lt;&gt; delegates have been a part of the .NET framework now since .NET 3.5 and VS2008.  Therefore, we are now deprecating all of our custom delegates in the NMath API and replacing them with .NET Func&lt;&gt; generic delegates.  This improves interoperability between NMath and our customers&#8217; code and between our C# API and other .NET languages.  Customers will see this change in the NMath 5.2 and NMathStats 3.5.</p>
<p>For those not familiar with the usage and definition of Func&lt;&gt; types within .NET there is a short primer at the end of this post.</p>
<h2>Compiler Warning for [Obsolete] NMath defined delegates</h2>
<p>All of our NMath delegate types have been deprecated using the .NET <code>[Obsolete()]</code> attribute.  The generated compiler warnings will provide the suggested <code>Func</code> type replacement.</p>
<pre lang="csharp">Warning 52 'CenterSpace.NMath.Core.NMathFunctions.FloatUnaryFunction'
   is obsolete: 'Use Func< float, float >'   ...</pre>
<p>To further clarify, the following table lists the deprecated NMath float delegate types side by side with their replacements.  All NMath delegates should be redefined in your code using the generic <code>Func</code> types.</p>
<pre class="code">
<table style="font-size: 7pt;">
<tbody>
<tr>
<th colspan="2"> List of float-precision deprecated NMath delegate types</th>
</tr>
<tr>
<th>Old NMath delegates</th>
<th>Replacement</th>
</tr>
<tr>
<td>float FloatFunction()</td>
<td>Func&lt; float &gt;</td>
</tr>
<tr>
<td>float FloatUnaryFunction( float x )</td>
<td>Func&lt; float, float &gt;</td>
</tr>
<tr>
<td>float FloatBinaryFunction( float x, float y )</td>
<td>Func&lt; float, float, float &gt;</td>
</tr>
<tr>
<td>float FloatIntFunction( float x, int y )</td>
<td>Func&lt; float, int, float &gt;</td>
</tr>
<tr>
<td>float FloatVectorFloatFunction( FloatVector v )</td>
<td>Func&lt; FloatVector, float &gt;</td>
</tr>
<tr>
<td>FloatVector
  FloatVectorFloatVectorFunction( FloatVector x )</td>
<td>Func&lt; FloatVector, FloatVector &gt;</td>
</tr>
</tbody>
</table>
</pre>
<p>There are many other deprecated NMath delegates that should be similarly replaced in your code.</p>
<h2>Code examples for updating your code</h2>
<p>Where previously you might have created a delegate that takes a <code>double</code> argument and returns a <code>double</code> like this:</p>
<pre lang="csharp">NMathFunctions.DoubleUnaryFunction soln =
   delegate(double x) {return (x*x*x) / 3.0 + y0; };</pre>
<p>you should now do it like this:</p>
<pre lang="csharp">Func soln =
   new Func< double, double >(  x => (x*x*x) / 3.0 + y0 );</pre>
<p>All methods in NMath and NMath Stats now take Func&lt;&gt; type delegates. If you use the old NMath delegates types to call a NMath method, an obsolescence warning will be issued by the compiler.</p>
<p>NMath and NMath Stats contain many functors that have now been obsolesced and redefined using a generic Func&lt;&gt; type.  The new functor naming convention simply drops the &#8220;tion&#8221; from &#8220;Function&#8221; to make the new name.  For example, the delegate <code>NMathFunctions.AtanFunction</code> should now be replaced with <code>NMathFunctions.AtanFunc</code>.  The old delegate has a type of <code>NMathFunctions.DoubleUnaryFunction</code>, and the new delegate has a type of <code>Func&lt; double, double &gt;</code>.  This kind of replacement can be done safely with a global search and replace operation to rid yourself of compiler obsolescence warnings.  The table below list a few functor redefinitions that demonstrate the renaming pattern that was applied across the API.</p>
<pre class="code">
<table style="font-size: 7pt;">
<tbody>
<tr>
<th colspan="2"> Sampling of deprecated NMath functors</th>
</tr>
<tr>
<th>Old NMath functor</th>
<th>Replacement</th>
</tr>
<tr>
<td>DoubleUnaryFunction AbsFunction</td>
<td>Func&lt; double, double &gt; AbsFunc</td>
</tr>
<tr>
<td>DoubleUnaryFunction AcosFunction</td>
<td>Func&lt; double, double &gt; AcosFunc</td>
</tr>
<tr>
<td>DoubleUnaryFunction AsinFunction</td>
<td>Func&lt; double, double &gt; AsinFunc</td>
</tr>
<tr>
<td>DoubleBinaryFunction PowFunction</td>
<td>Func&lt; double, double, double &gt; PowFunc</td>
</tr>
<tr>
<td>DoubleIntFunction RoundFunction</td>
<td>Func&lt; double, int, double &gt; RoundFunc</td>
</tr>
<tr>
<td>DoubleVectorDoubleFunction
  DoubleNaNMedianFunction</td>
<td>Func&lt; DoubleVector, double &gt; DoubleNaNMedianFunc</td>
</tr>
</tbody>
</table>
</pre>
<h2>Potential Issues</h2>
<p>The deprecation process requires us to maintain the old custom delegate defintions in the API methods while introducing the new Func&lt;&gt; based API.  As a result, every method call in our API which previously consumed a custom NMath delgate type, a functionally identical signature was added which consumes a Func&lt;&gt; based delegate.  This duplication can cause an easy-to-fix compiler error in situations when an anonymous delegate was used instead of a NMath delegate type.  Consider the follow line of code which inverts the singular values of a SVD.</p>
<pre lang="csharp">      DoubleVector wInverse =
         svDecomp.SingularValues.Apply(
              delegate( double w )
                 { return w < eps ? 0.0 : 1.0 / w; }
         );</pre>
<p>Written this way will now cause the following compiler error.</p>
<pre class="code">
Error	43
The call is ambiguous between the following methods or properties:
'CenterSpace.NMath.Core.DoubleVector.Apply(
    CenterSpace.NMath.Core.NMathFunctions.DoubleUnaryFunction )'
and
'CenterSpace.NMath.Core.DoubleVector.Apply( System.Func )' ....</pre>
<p>In this error message, the first <code>Apply()</code> is the old method signature and the second <code>Apply()</code> is the new Func&lt;&gt; based signature.  The C# compiler can't choose between the two when provided an anonymous delegate.  Exactly why this ambiguity error is generated by the compiler is carefully explained by Eric Lippert over on <a href="http://stackoverflow.com/questions/2057146/compiler-ambiguous-invocation-error-anonymous-method-and-method-group-with-fun">stackoverflow</a>.  Briefly, the ambiguity error arises because both methods equally satisfy the <code>Apply()</code> method signature and the C# standard does not provide a way to judge which would be 'better', and so reports an error.  The fix is simple, we just specified explicitly that we want to use the method that takes a generic Func&lt;&gt;.</p>
<pre lang="csharp">      DoubleVector wInverse =
         svDecomp.SingularValues.Apply(
              new Func< double, double >
                 ( delegate( double w )
                    { return w < eps ? 0.0 : 1.0 / w; }
                 )
         );</pre>
<p>Or, somewhat more concisely using a lamda expression.</p>
<pre lang="csharp">      DoubleVector wInverse =
         svDecomp.SingularValues.Apply(
              new Func< double, double >
                 ( w => w < eps ? 0.0 : 1.0 / w  )
         );</pre>
<h2>A brief Func&lt;&gt; &amp; Action&lt;&gt;  primer</h2>
<p>The generic delegate types, <code>Func&lt;&gt;</code> and <code>Action&lt;&gt;</code>, were introduced with the .NET 3.5 framework to unify delegate types across various .NET libraries.  This allows a .NET library developer like CenterSpace to create an API that accepts generic delegates that are defined elsewhere in client code.  The <code>Func</code> and <code>Action</code> are differentiated by their return values:  <code>Actions</code> have no return value, <code>Funcs</code> always have a <em>single</em> return value of type <code>TResult</code>.  For each of them, the .NET framework defines a type signature that takes between <code>0</code> and <code>16</code> generically typed arguments.</p>
<pre class="code">
<table style="font-size: 7pt;">
<tbody>
<tr>
<th colspan="2"> .NET Func&lt;&gt; and Action&lt;&gt; definitions</th>
</tr>
<tr>
<th>Func</th>
<th>Action</th>
</tr>
<tr>
<td>TResult Func()</td>
<td>void Action&lt;&gt;()</td>
</tr>
<tr>
<td>TResult Func( T arg )</td>
<td>void Action( T arg )</td>
</tr>
<tr>
<td>...</td>
<td>...</td>
</tr>
<tr>
<td>TResult Func(
	T1 arg1,
	T2 arg2,
	...
	T15 arg15,
	T16 arg16 )</td>
<td>void Action(
	T1 arg1,
	T2 arg2,
	...
	T15 arg15,
	T16 arg16 )</td>
</tr>
</tbody>
</table>
</pre>
<p>This is still all abstract.  Here's how we create a <code>Func</code> using a lambda expression.</p>
<pre lang="csharp">Func mySquaringFunc =
   new Func< double, double> ( x =>  x*x ); 

double xSquared = mySquaringFunc( 45 );  // xSquared = 2025</pre>
<p>This <code>Func</code> delegate takes single double value and returns a double value.  Lamda expressions are commonly used today to generate delegates, however an anonymous delegate could also be used for the same purpose.</p>
<pre lang="csharp">Func mySquaringFunc =
   new Func< double, double> ( delegate( double x ) { return x*x; } ); 

double xSquared = mySquaringFunc( 45 );  // xSquared = 2025</pre>
<p>I find the lambda syntax to be more expressive and succinct.  From the table above, <code>T1 = double</code>, and <code>TResult = double</code>.</p>
<p><code>Action</code> types are not used within the NMath API because we are always dealing with functions and return values.  However, <code>Actions</code> are similarly defined, and may be used for example in queuing code where a queue manager takes work in the form of <code>Action</code> delegates.</p>
<pre lang="csharp">Action< DoubleVector > FFTWorker =
   new Action< DoubleVector >( v =>
      {
         DoubleComplexForward1DFFT fft = new DoubleComplexForward1DFFT( v.Length );
         fft.FFTInPlace( v );
       } );
...
QueueManager.AddWork( FFTWorker, bigDataVector );</pre>
<p>The <code>Action</code> above computes the in-place FFT, and the <code>QueueManager</code> can now generically schedule this work on the queue.</p>
<p>Happy Computing,<br />
Paul Shirkey</p>
<p><!-- NOTES // Error DoubleVector kernel = new DoubleVector(kernelLength, -8.2 * Math.PI, 1).Transform(Math.Sin); //Fix DoubleVector kernel = new DoubleVector(kernelLength, -8.2*Math.PI,1).Transform( new Func&lt;double, double&gt;( Math.Sin ) ); --></p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/nmath-api-updated-with-net-func-delegates">NMath API updated with .NET Func&lt;&gt; delegates</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/nmath-api-updated-with-net-func-delegates/feed</wfw:commentRss>
			<slash:comments>4</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3711</post-id>	</item>
	</channel>
</rss>
