N
Nondisclosure007
Maybe I'm missing something here, but what I want to do is get a value
BACK from a DLL. And I'm not sure how to do it w/ this DLL. Here is
the code:
Note* I retrieved this from Codeproject.com
I get passing 2 arrays via Statistics( double[] x, double[] y ). But
how do I get the value back? I don't see a 'return' of the
correlation.
Note* It's not a C# project that's calling this function, but the
language I'm using is A LOT like 'non-object oriented' C.
my
call in this language would be like: double varCorr=Statistics(X[],Y
[]). But I don't see the 'return' in here.
Michael.
BACK from a DLL. And I'm not sure how to do it w/ this DLL. Here is
the code:
Code:
using System;
namespace MathEx
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public class Statistics
{
/// <summary>
/// This is where we test the code
/// </summary>
public static void Main()
{
double[] x = { 1, -10, 7 };
double[] y = { 22, 15, -1 };
double avgX = Statistics.GetAverage( x );
double varX = Statistics.GetVariance( x );
double stdevX = Statistics.GetStdev( x );
double avgY = Statistics.GetAverage( y );
double varY = Statistics.GetVariance( y );
double stdevY = Statistics.GetStdev( y );
double covXY = 0, pearson = 0;
Statistics.GetCorrelation( x,y, ref covXY, ref pearson );
Console.Read();
}
/// <summary>
/// Constructor
/// </summary>
public Statistics( double[] x, double[] y )
{
}
/// <summary>
/// Get average
/// </summary>
public static double GetAverage( double[] data )
{
int len = data.Length;
if ( len == 0 )
throw new Exception("No data");
double sum = 0;
for ( int i = 0; i < data.Length; i++ )
sum += data[i];
return sum / len;
}
/// <summary>
/// Get variance
/// </summary>
public static double GetVariance( double[] data )
{
int len = data.Length;
// Get average
double avg = GetAverage( data );
double sum = 0;
for ( int i = 0; i < data.Length; i++ )
sum += System.Math.Pow( ( data[i] - avg ), 2 );
return sum / len;
}
/// <summary>
/// Get standard deviation
/// </summary>
public static double GetStdev( double[] data )
{
return Math.Sqrt( GetVariance( data ) );
}
/// <summary>
/// Get correlation
/// </summary>
public static void GetCorrelation( double[] x, double[] y, ref
double covXY, ref double pearson)
{
if ( x.Length != y.Length )
throw new Exception("Length of sources is different");
double avgX = GetAverage( x );
double stdevX = GetStdev( x );
double avgY = GetAverage( y );
double stdevY = GetStdev( y );
int len = x.Length;
for ( int i = 0; i < len; i++ )
covXY += ( x[i] - avgX ) * ( y[i] - avgY );
covXY /= len;
pearson = covXY / ( stdevX * stdevY );
}
}
I get passing 2 arrays via Statistics( double[] x, double[] y ). But
how do I get the value back? I don't see a 'return' of the
correlation.
Note* It's not a C# project that's calling this function, but the
language I'm using is A LOT like 'non-object oriented' C.
![Smile :) :)](/styles/default/custom/smilies/smile.gif)
call in this language would be like: double varCorr=Statistics(X[],Y
[]). But I don't see the 'return' in here.
Michael.