<?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>Best Practices Archives - CenterSpace</title>
	<atom:link href="https://www.centerspace.net/category/best-practices/feed" rel="self" type="application/rss+xml" />
	<link>https://www.centerspace.net/category/best-practices</link>
	<description>.NET numerical class libraries</description>
	<lastBuildDate>Tue, 07 Feb 2023 21:51:52 +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 and Silverlight</title>
		<link>https://www.centerspace.net/nmath-and-silverlight-2</link>
					<comments>https://www.centerspace.net/nmath-and-silverlight-2#comments</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Thu, 09 Feb 2012 20:26:02 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[Silverlight]]></category>
		<category><![CDATA[math and silverlight]]></category>
		<category><![CDATA[nmath and silverlight]]></category>
		<category><![CDATA[silverlight with nmath]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/uncategorized/nmath-and-silverlight-2/</guid>

					<description><![CDATA[<p>From time to time, we’re asked about the best way to use NMath to build Silverlight applications. Unfortunately, like so many answers in software development, the answer is: it depends. Silverlight is a great way to build line of business applications, but at its core, Silverlight runs within a sandboxed environment, typically within a browser, [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/nmath-and-silverlight-2">NMath and Silverlight</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>From time to time, we’re asked about the best way to use NMath to build Silverlight applications. Unfortunately, like so many answers in software development, the answer is: <em>it depends</em>.</p>
<p>Silverlight is a great way to build line of business applications, but at its core, Silverlight runs within a sandboxed environment, typically within a browser, and usually within a networked intranet or Internet environment.  NMath, on the other hand, uses native libraries to provide high-performance math capabilities to managed code.  While it’s possible to build a Silverlight application that calls NMath functions, it’s worth taking a step back to look at what you really want to accomplish with your software project.</p>
<p>Most often, we find that developers are tasked with building dashboard-style business applications that allow the user to initiate some sort of data analysis, and then see a visualization of results.  In these cases, we generally recommend:</p>
<p><strong>1) Use Silverlight (or HTML5) on the client, perform the analysis on one or more servers, and expose web services to initiate the analysis and deliver the results.</strong></p>
<p>This is probably the most favorable approach, and usually the architecture to default to unless there’s a compelling reason to the contrary.  Some of the advantages of this approach:</p>
<ul>
<li>It’s consistent with typical Silverlight application architecture; capabilities in Silverlight and ASP.NET make it easy to build this kind of application.</li>
<li>It retains the deployment advantages of Silverlight: there’s no need to distribute and install native components to every client machine.</li>
<li>It retains the cross-platform advantages of Silverlight: the application will work smoothly regardless of client hardware (including on Mac OS).</li>
<li>Centralizing the analysis on a server makes it easier to scale up with improved server hardware and/or server farms.</li>
<li>It makes efficient use of network bandwidth: usually there’s little need to transfer all of the source data between client and server, just the results of the analysis.</li>
</ul>
<p>The primary disadvantages of this approach include:</p>
<ul>
<li>This might drive an architectural shift if it’s not already working in a client-server environment.</li>
<li>It doesn’t take maximum advantage of available computing resources on the client, at least for the analysis (although the application can potentially use the client’s GPU for the visualization).</li>
</ul>
<p>If you’re certain that you want to perform the analysis on the client using a Silverlight application, you can:</p>
<p><strong>2) Use Silverlight 4+, and use COM interop to invoke COM components that were previously installed on the client.</strong></p>
<p><strong>3) Use Silverlight 5, and use Platform Invoke (P/Invoke) to invoke native components.</strong></p>
<p>There are significant disadvantages to these approaches, in particular:</p>
<ul>
<li>You would need to separately distribute your components and get them preinstalled on the local machine.</li>
<li>The client applications will only run on Windows; you cannot invoke native code on Mac OS from Silverlight.</li>
<li>Users will need to explicitly grant elevated trust to the Silverlight application.</li>
<li>You would also need to build your application with out of browser support (if you’re using COM interop).</li>
</ul>
<p>For the COM interop approach you would use ComAutomationFactory.CreateObject() to instantiate dynamic COM objects, which would also mean that you would require C# 4, Silverlight 4, and Visual Studio 2010.  This approach is less than ideal for new applications; it&#8217;s primarily intended for interaction within controlled environments (e.g. interop with Microsoft Office).  For more about this, see:</p>
<ul>
<li>Silverlight and Desktop .NET Integration</li>
<li><a href="http://justinangel.net/CuttingEdgeSilverlight4ComFeatures" target="_blank" rel="noopener">Cutting Edge Silverlight4 COM+ Features</a></li>
</ul>
<p>For information about P/Invoke in Silverlight 5, see:</p>
<ul>
<li><a href="https://msdn.microsoft.com/en-us/library/hh560563(v=vs.95).aspx" target="_blank" rel="noopener">How to: Call Unmanaged Code from Trusted Applications</a></li>
</ul>
<p>However, if you’re certain that you want to build a rich client application to perform the analysis on Windows, then you should also evaluate whether Silverlight is the best choice for doing so.  Some alternatives include:</p>
<p><strong>4) Create a rich client application using Windows Presentation Foundation, and run in within the browser using XBAP.</strong></p>
<p>XBAP applications are WPF applications that run in partial trust inside a web browser.  You do have access to more local resources than you do in Silverlight, and you can request the user to grant additional trust if needed.  You&#8217;re restricted to using Internet Explorer and Firefox.  For more about this, see:</p>
<ul>
<li><a href="http://msdn.microsoft.com/en-us/library/aa970060.aspx" target="_blank" rel="noopener">WPF XAML Browser Applications Overview</a></li>
<li><a href="https://en.wikipedia.org/wiki/XBAP" target="_blank" rel="noopener">Wikipedia: XAML Browser Applications</a></li>
<li><a href="http://www.xbap.org/" target="_blank" rel="noopener">XBap.org</a></li>
</ul>
<p><strong>5) Create a rich client application using Windows Presentation Foundation or Windows Forms, and distribute it via ClickOnce Deployment.</strong></p>
<p>In this scenario, you&#8217;re running a full client application within Windows, but taking advantage of some of the deployment advantages of the web, including initial distribution and facilities for automatic application updates.  Generally speaking, this is the preferred way to build rich client applications on Windows.  For more about ClickOnce, see:</p>
<ul>
<li><a href="https://msdn.microsoft.com/en-US/library/t71a733d(v=VS.100).aspx" target="_blank" rel="noopener">ClickOnce Security and Deployment</a></li>
<li><a href="https://en.wikipedia.org/wiki/ClickOnce" target="_blank" rel="noopener">Wikipedia: ClickOnce</a></li>
</ul>
<p>Our experienced consulting team is available to help you work through the architectural options that would make the most sense for your specific application; just contact us at <a href="mailto:consulting@centerspace.net">consulting@centerspace.net</a>.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/nmath-and-silverlight-2">NMath and Silverlight</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/nmath-and-silverlight-2/feed</wfw:commentRss>
			<slash:comments>2</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3698</post-id>	</item>
		<item>
		<title>Clearing a vector</title>
		<link>https://www.centerspace.net/clearing-a-vector</link>
					<comments>https://www.centerspace.net/clearing-a-vector#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Wed, 09 Nov 2011 22:28:01 +0000</pubDate>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Performance]]></category>
		<category><![CDATA[clearing a matrix]]></category>
		<category><![CDATA[clearing a vector]]></category>
		<category><![CDATA[NMath matirx]]></category>
		<category><![CDATA[NMath vector]]></category>
		<category><![CDATA[zeroing a matrix]]></category>
		<category><![CDATA[zeroing a vector]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3621</guid>

					<description><![CDATA[<p>A customer recently asked us for the best method to zero out a vector. We decided to run some tests to find out. Here are the five methods we tried followed by performance timing and any drawbacks. The following tests were performed on a DoubleVector of length 10,000,000. 1) Create a new vector. This isn&#8217;t [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/clearing-a-vector">Clearing a vector</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A customer recently asked us for the best method to zero out a vector. We decided to run some tests to find out. Here are the five methods we tried followed by performance timing and any drawbacks.</p>
<p>The following tests were performed on a <code>DoubleVector</code> of length 10,000,000.</p>
<p>1) Create a new vector. This isn&#8217;t really clearing out an existing vector but we thought we should include it for completeness.</p>
<pre lang="csharp" line="1"> DoubleVector v2 = new DoubleVector( v.Length, 0.0 );</pre>
<p>The big drawback here is that you&#8217;re creating new memory. Time: <strong>419.5ms</strong></p>
<p>2) Probably the first thing to come to mind is to simply iterate through the vector and set everything to zero.</p>
<pre lang="csharp" line="1">
for ( int i = 0; i < v.Length; i++ )
{
  v[i] = 0.0;
}</pre>
<p>We have to do some checking in the index operator. No new memory is created. Time: <strong>578.5ms</strong></p>
<p>3) In some cases, you could iterate through the underlying array of data inside the DoubleVector.</p>
<pre lang="csharp" line="1"> 
for ( int i = 0; i &lt; v.DataBlock.Data.Length; i++ )
{
  v.DataBlock.Data[i] = 0.0;
}</pre>
<p>This is a little less intuitive. And, very importantly, it will not work with many views into other data structures. For example, a row slice of a matrix. However, it's easier for the CLR to optimize this loop. Time: <strong>173.5ms</strong></p>
<p>4) We can use the power of Intel's MKL to multiply the vector by zero.</p>
<pre lang="csharp" line="1"> v.Scale( 0.0 );</pre>
<p>Scale() does this in-place. No new memory is created. In this example, we assume that MKL has already been loaded and is ready to go which is true if another MKL-based NMath call was already made or if NMath was <a href="/initializing-nmath/">initialized</a>. This method will work on all views of other data structures. Time: <strong>170ms</strong></p>
<p>5) This surprised us a bit but the best method we could find was to clear out the underlying array using Array.Clear() in .NET</p>
<pre lang="csharp" line="1"> Array.Clear( v.DataBlock.Data, 0, v.DataBlock.Data.Length );</pre>
<p>This creates no new memory. However, this will not work with non-contiguous views. However, this method is very fast. Time: <strong> 85.8ms</strong></p>
<p>To make efficiently clearing a vector simpler for NMath users we have created a <code>Clear()</code> method and a <code>Clear( Slice )</code> method on the vector and matrix classes.  It will do the right thing in the right circumstance. It will be released in NMath 5.2 in 2012.</p>
<h3> Test Code </h3>
<pre lang="csharp" line="1">
using System;
using CenterSpace.NMath.Core;

namespace Test
{
  class ClearVector
  {
    static int size = 100000000;
    static int runs = 10;
    static int methods = 5;
    
    static void Main( string[] args )
    {
      System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
      DoubleMatrix times = new DoubleMatrix( runs, methods );
      NMathKernel.Init();

      for ( int run = 0; run < runs; run++ )
      {
        Console.WriteLine( "Run {0}...", run );
        DoubleVector v = null;

        // Create a new one
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Start();
        DoubleVector v2 = new DoubleVector( v.Length, 0.0 );
        sw.Stop();
        times[run, 0] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v2 ) );

        // iterate through vector
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        for ( int i = 0; i < v.Length; i++ )
        {
          v[i] = 0.0;
        }
        sw.Stop();
        times[run, 1] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );

        // iterate through array
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        for ( int i = 0; i < v.DataBlock.Data.Length; i++ )
        {
          v.DataBlock.Data[i] = 0.0;
        }
        sw.Stop();
        times[run, 2] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );
        
        // scale
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        v.Scale( 0.0 );
        sw.Stop();
        times[run, 3] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );

        // Array Clear
        v = new DoubleVector( size, 1.0, 2.0 );
        sw.Reset();
        sw.Start();
        Array.Clear( v.DataBlock.Data, 0, v.DataBlock.Data.Length );
        sw.Stop();
        times[run, 4] = sw.ElapsedMilliseconds;
        Console.WriteLine( Assert( v ) );
        Console.WriteLine( times.Row( run ) );
      }
      Console.WriteLine( "Means: " + NMathFunctions.Mean( times ) );
    }

    private static bool Assert( DoubleVector v )
    {
      if ( v.Length != size )
      {
        return false;
      }
      for ( int i = 0; i < v.Length; ++i )
      {
        if ( v[i] != 0.0 )
        {
          return false;
        }
      }
      return true;
    }
  }
}
</pre>
<p>- Trevor</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/clearing-a-vector">Clearing a vector</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/clearing-a-vector/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3621</post-id>	</item>
		<item>
		<title>Initializing NMath</title>
		<link>https://www.centerspace.net/initializing-nmath</link>
					<comments>https://www.centerspace.net/initializing-nmath#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Wed, 09 Nov 2011 22:01:27 +0000</pubDate>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[NMath Tutorial]]></category>
		<category><![CDATA[Performance]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3611</guid>

					<description><![CDATA[<p>NMath uses Intel&#8217;s Math Kernel Library (MKL) internally. This code contains native, optimized code to wring out the best performance possible. There is a one-time delay when the appropriate x86 or x64 native code is loaded. This cost can be easily controlled by the developer by using the NMathKernel.Init() method. Please see Initializing NMath for [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/initializing-nmath">Initializing NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>NMath uses Intel&#8217;s Math Kernel Library (MKL) internally. This code contains native, optimized code to wring out the best performance possible.</p>
<p>There is a one-time delay when the appropriate x86 or x64 native code is loaded. This cost can be easily controlled by the developer by using the NMathKernel.Init() method. Please see <a href="http://centerspace.net/doc/NMath/user/overview-83549.htm">Initializing NMath</a> for more details.</p>
<p>&#8211; Trevor</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/initializing-nmath">Initializing NMath</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/initializing-nmath/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3611</post-id>	</item>
		<item>
		<title>.NET Curve Fitting Applied to Golf</title>
		<link>https://www.centerspace.net/net-curve-fitting</link>
					<comments>https://www.centerspace.net/net-curve-fitting#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Tue, 01 Mar 2011 01:13:23 +0000</pubDate>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[C# differential equations]]></category>
		<category><![CDATA[C# polynomial curve fitting]]></category>
		<category><![CDATA[C# polynomial least squares]]></category>
		<category><![CDATA[C# Runge-Kutta]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=3238</guid>

					<description><![CDATA[<p>Two retired scientists, self-proclaimed duffers, came to us asking for help with their retirement project: to improve their putting using mathematics. Around the office at CenterSpace, we have been wondering if applying the fundamentals of math to the putting green might help us lower our scores this year. We sure could use some help! We will feature the results of their experiments in our quarterly newsletters throughout the year.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/net-curve-fitting">.NET Curve Fitting Applied to Golf</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><em>“I&#8217;ve heard people say putting is 50 percent technique and 50 percent mental. I really believe it is 50 percent technique and 90 percent positive thinking. See… but that adds up to 140 percent, which is why nobody is 100 percent sure how to putt!&#8221; &#8211; Chi Chi Rodriguez<br />
</em></p>
<p>Two retired scientists, self-proclaimed duffers, came to us asking for help with their retirement project: to improve their putting using mathematics. Around the office at CenterSpace, we have been wondering if applying the fundamentals of math to the putting green might help us lower our scores this year. We sure could use some help! We will feature the results of their experiments in our quarterly newsletters throughout the year.</p>
<p>Armed with the NMath <a href="/product-overviews/">.NET math libraries</a> they plan to attack the project in stages.</p>
<p>First, they will model the equations of motion in a simple way with the ball represented as a point, without the complications of the rotational dynamics of a real ball.</p>
<p>Second, they will develop a computer application to serve as a framework for solving the differential equations of motion and displaying this motion with 3d animations. For certain simple models of greens, this will allow the visualization of a putt given an initial direction and speed.</p>
<p>The next step will be to solve the two point boundary value problem: calculate the best initial direction and speed to get the ball into the cup at a reasonable speed. Also more complicated “real life” greens will be modeled, using least squares routines to fit known green elevation data.</p>
<p><em>“These greens are so fast I have to hold my putter over the ball and hit it with the shadow!” -Sam Snead</em></p>
<p>Finally, a more realistic model will be investigated to more precisely capture the rotational motion of a 42.67 mm ball on a grass surface.</p>
<p>Of course we have no illusions that this project will lower anyone’s handicap, because only hours on the golf course, the driving range and practice greens can do that. To our golfing scientist friends it’s a good excuse to both revisit the fun of their college math and physics and explore new and exciting golf courses throughout the world.</p>
<p>And… just maybe… seeing the ball drop in the hole on the computer will help them relieve the frustration of a morning on the real greens that we are all too familiar with!</p>
<p><em>“I would like to deny all allegations by Bob Hope that during my last game of golf, I hit an eagle, a birdie, an elk and a moose.” -Gerald Ford</em></p>
<p>Happy Retirement Guys!</p>
<p>Please check out some of the functionality the duffers used&#8230;</p>
<p><a href="/polynomial-curve-fitting/">C# polynomial least squares</a><br />
<a href="https://www.centerspace.net/doc/NMath/user/index.html">C# differential equations</a> (Chapter 31). A lot more differential equation functionality is coming in June, 2011.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/net-curve-fitting">.NET Curve Fitting Applied to Golf</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/net-curve-fitting/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">3238</post-id>	</item>
		<item>
		<title>Configuring missing values</title>
		<link>https://www.centerspace.net/configuring-missing-values</link>
					<comments>https://www.centerspace.net/configuring-missing-values#respond</comments>
		
		<dc:creator><![CDATA[Trevor Misfeldt]]></dc:creator>
		<pubDate>Wed, 21 Oct 2009 04:23:18 +0000</pubDate>
				<category><![CDATA[Best Practices]]></category>
		<category><![CDATA[NMath]]></category>
		<category><![CDATA[Support]]></category>
		<category><![CDATA[missing values]]></category>
		<category><![CDATA[stats]]></category>
		<guid isPermaLink="false">http://www.centerspace.net/blog/?p=327</guid>

					<description><![CDATA[<p>A customer asked how they could read in a CSV file into a DataFrame (for eventual linear regression) with missing values.</p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/configuring-missing-values">Configuring missing values</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p>A customer asked how they could read in a CSV file into a DataFrame (for eventual linear regression) with missing values. </p>
<p>The post <a rel="nofollow" href="https://www.centerspace.net/configuring-missing-values">Configuring missing values</a> appeared first on <a rel="nofollow" href="https://www.centerspace.net">CenterSpace</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.centerspace.net/configuring-missing-values/feed</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">327</post-id>	</item>
	</channel>
</rss>
