Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added syncfusion / nmath charting support code and examples

  • Loading branch information...
commit b43713c178baf4f88fdb7d5ef5135b78a9827404 1 parent 5226c12
Paul Shirkey authored

Showing 3 changed files with 7,710 additions and 0 deletions. Show diff stats Hide diff stats

  1. +154 0 Examples.cs
  2. +4,719 0 NMathChart.cs
  3. +2,837 0 NMathStatsChart.cs
154 Examples.cs
... ... @@ -0,0 +1,154 @@
  1 +using System;
  2 +using System.Collections.Generic;
  3 +using System.Drawing;
  4 +using System.Text;
  5 +using System.Windows.Forms;
  6 +
  7 +using CenterSpace.NMath.Analysis;
  8 +using CenterSpace.NMath.Core;
  9 +using CenterSpace.NMath.Stats;
  10 +using CenterSpace.NMath.Charting.Syncfusion;
  11 +
  12 +using Syncfusion.Windows.Forms.Chart;
  13 +
  14 +
  15 +namespace CenterSpace.NMath.Charting.Syncfusion
  16 +{
  17 + class Examples
  18 + {
  19 + static void Main( string[] args )
  20 + {
  21 + // Class NMathChart and NMathStatsChart provide static methods for plotting NMath
  22 + // types using Syncfusion Essential Chart for Windows Forms controls.
  23 +
  24 + // EXAMPLE 1: CURVE FITTING
  25 +
  26 + // This NMath code fits a 4-parameter logistic function to data measuring the evolution
  27 + // of an algal bloom in the Adriatic Sea.
  28 + DoubleVector x = new DoubleVector( 11, 15, 18, 23, 26, 31, 39, 44, 54, 64, 74 );
  29 + 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 );
  30 + DoubleVector start = new DoubleVector( 4, 0.1 );
  31 + OneVariableFunctionFitter<TrustRegionMinimizer> fitter =
  32 + new OneVariableFunctionFitter <TrustRegionMinimizer>( AnalysisFunctions.FourParameterLogistic );
  33 + DoubleVector solution = fitter.Fit( x, y, start );
  34 +
  35 + // For prototyping and debugging console applications, Show() plots common NMath types
  36 + // and displays the chart in a default form.
  37 + int numInterpolatedValues = 100;
  38 + NMathChart.Show( fitter, x, y, solution, numInterpolatedValues );
  39 +
  40 + // The default look of the chart is governed by static properties: DefaultSize,
  41 + // DefaultTitleFont, DefaultAxisTitleFont, DefaultMajorGridLineColor, and DefaultMarker.
  42 +
  43 + // For more control, ToChart() returns an instance of Syncfusion.Windows.Forms.Chart.ChartControl,
  44 + // which can be customized as desired.
  45 + ChartControl chart = NMathChart.ToChart( fitter, x, y, solution, numInterpolatedValues );
  46 + chart.Titles[0].Text = "Algal Bloom in the Adriatic Sea";
  47 + chart.PrimaryXAxis.Title = "Days";
  48 + chart.PrimaryYAxis.Title = "Size (mm2)";
  49 + chart.Series[0].Text = "Observed";
  50 + chart.Series[1].Text = "Fitted 4PL";
  51 + chart.BackColor = Color.Beige;
  52 + NMathChart.Show( chart );
  53 +
  54 + // If you are developing a Windows Forms application using the Designer, add a ChartControl
  55 + // to your form, then update it with an NMath object using the appropriate Update() function
  56 + // after initialization.
  57 +
  58 + // InitializeComponent();
  59 + // NMathChart.Update( ref this.chart1, fitter, x, y, solution, numInterpolatedValues );
  60 +
  61 +
  62 + // EXAMPLE 2: FFT
  63 +
  64 + // This chart shows a complex signal vector with three component sine waves.
  65 + int n = 100;
  66 + DoubleVector t = new DoubleVector( n, 0, 0.1 );
  67 + DoubleVector signal = new DoubleVector( n );
  68 + for( int i = 0; i < n; i++ )
  69 + {
  70 + signal[i] = Math.Sin( 2 * Math.PI * t[i] ) + 2 * Math.Sin( 2 * Math.PI * 2 * t[i] ) + 3 * Math.Sin( 2 * Math.PI * 3 * t[i] );
  71 + }
  72 + chart = NMathChart.ToChart( signal, new NMathChart.Unit( 0, 0.1, "Time (s)" ) );
  73 + chart.Titles[0].Text = "Signal";
  74 + chart.ChartArea.PrimaryYAxis.Title = "Voltage";
  75 + NMathChart.Show( chart );
  76 +
  77 + // We use NMath to compute the forward discrete fourier transform, then plot the power in the frequency domain.
  78 + DoubleForward1DFFT fft = new DoubleForward1DFFT( n );
  79 + fft.FFTInPlace( signal );
  80 + DoubleSymmetricSignalReader reader = fft.GetSignalReader( signal );
  81 + DoubleComplexVector unpacked = reader.UnpackSymmetricHalfToVector();
  82 + chart = NMathChart.ToChart( unpacked, new NMathChart.Unit( 0, 0.1, "Frequency (Hz)" ) );
  83 + chart.Titles[0].Text = "FFT";
  84 + chart.ChartArea.PrimaryYAxis.Title = "Power";
  85 + NMathChart.Show( chart );
  86 +
  87 +
  88 + // EXAMPLE 3: PEAK FINDING
  89 +
  90 + // NMath class PeakFinderSavitzkyGolay uses smooth Savitzky-Golay derivatives to find peaks in data.
  91 + // A peak is defined as a smoothed derivative zero crossing.
  92 + double step_size = 0.1;
  93 + x = new DoubleVector( 1000, 0.01, step_size );
  94 + y = NMathFunctions.Sin( x ) / x;
  95 + int width = 5;
  96 + int polynomial_degree = 4;
  97 + PeakFinderSavitzkyGolay pf = new PeakFinderSavitzkyGolay( y, width, polynomial_degree );
  98 + pf.AbscissaInterval = step_size;
  99 + pf.SlopeSelectivity = 0;
  100 + pf.RootFindingTolerance = 0.0001;
  101 + pf.LocatePeaks();
  102 +
  103 + // Plot the peaks.
  104 + double xmin = 20;
  105 + double xmax = 50;
  106 + NMathChart.Show( pf, xmin, xmax );
  107 +
  108 +
  109 + // EXAMPLE 4: K-MEANS CLUSTERING
  110 +
  111 + // The k-means clustering method assigns data points into k groups such that the sum of squares from points
  112 + // to the computed cluster centers is minimized. Here we cluster 30 points in 3-dimensional space into 5 clusters.
  113 + DoubleMatrix data = new DoubleMatrix( @"30 x 3 [
  114 + 0.62731478808400 0.71654239725005 0.11461282117064
  115 + 0.69908013774534 0.51131144816890 0.66485556714021
  116 + 0.39718395379261 0.77640121193349 0.36537389168912
  117 + 0.41362889533818 0.48934547589850 0.14004445653473
  118 + 0.65521294635567 0.18590445122522 0.56677280030311
  119 + 0.83758509883186 0.70063540514612 0.82300831429067
  120 + 0.37160803224266 0.98270880190626 0.67394863209536
  121 + 0.42525315848265 0.80663774928874 0.99944730494940
  122 + 0.59466337145257 0.70356765500360 0.96163640714857
  123 + 0.56573857208571 0.48496371932457 0.05886216545559
  124 + 1.36031117091978 1.43187338560697 1.73265064912939
  125 + 1.54851281373460 1.63426595631548 1.42222658611939
  126 + 1.26176956987179 1.80302634023193 1.96136999885631
  127 + 1.59734484793384 1.08388100700103 1.07205923855201
  128 + 1.04927799659601 1.94546278791039 1.55340796803039
  129 + 1.57105749438466 1.91594245989412 1.29198392114244
  130 + 1.70085723323733 1.60198742363800 1.85796351308408
  131 + 1.96228825871716 1.25356057873233 1.33575513868621
  132 + 1.75051823194427 1.87345080554039 1.68020385037051
  133 + 1.73999304537847 1.51340070999628 1.05344442131849
  134 + 2.35665553727760 2.67000386489368 2.90898934903532
  135 + 2.49830459603553 2.20087641229516 2.59624713810572
  136 + 2.43444053822029 2.27308816154697 2.32895530216404
  137 + 2.56245841710735 2.62623463865051 2.47819442572535
  138 + 2.61662113016546 2.53685169481751 2.59717077926034
  139 + 2.11333998089856 2.05950405092050 2.16144875489995
  140 + 2.89825174061313 2.08896175947532 2.82947425087386
  141 + 2.75455137523865 2.27130817438170 2.95612240635488
  142 + 2.79112319571067 2.40907231577105 2.59554799520203
  143 + 2.81495206793323 2.47404145037448 2.02874821321149 ]" );
  144 + KMeansClustering km = new KMeansClustering( data );
  145 + ClusterSet clusters = km.Cluster( 5 );
  146 +
  147 + // We have to specify which plane to plot.
  148 + int xColIndex = 0;
  149 + int yColIndex = 1;
  150 + NMathStatsChart.Show( clusters, data, xColIndex, yColIndex );
  151 +
  152 + }
  153 + }
  154 +}
4,719 NMathChart.cs
4,719 additions, 0 deletions not shown
2,837 NMathStatsChart.cs
2,837 additions, 0 deletions not shown

0 comments on commit b43713c

Please sign in to comment.
Something went wrong with that request. Please try again.