Доброе утро!
Есть такая задачка рассчитать коэффициент частной корреляции по вот такой формуле:
Сделал рекурсивно, но думаю все таки ошибка в коде:
public static double GetCoefficientPrivateCorrelation(
this Matrix<double> matrix,
int[] dependence1,
int[] dependence2)
{
int[] buf = new int[2];
// проверка dependence2 длина этого массива ноль
if (dependence2.Length != 0)
{
// перестановки
int[] x1Xp = new int[2];
int[] xiXp = new int[2];
x1Xp[0] = dependence1[0];
x1Xp[1] = dependence2[dependence2.Length - 1];
xiXp[0] = dependence1[dependence1.Length - 1];
xiXp[1] = dependence2[dependence2.Length - 1];
// добавляем в буфер
buf[0] = dependence1[0];
buf[1] = dependence1[1];
// xj ... x(p-1)
int[] minusDependence = new int[dependence2.Length - 1];
for (int i = 0; i < minusDependence.Length; i++)
{
minusDependence[i] = dependence2[i];
}
// основная формула
return (GetCoefficientPrivateCorrelation(matrix, dependence1, minusDependence)-
GetCoefficientPrivateCorrelation(matrix, x1Xp, minusDependence) *
GetCoefficientPrivateCorrelation(matrix, xiXp, minusDependence)) /
Math.Sqrt(1 - Math.Pow(GetCoefficientPrivateCorrelation(matrix, x1Xp, minusDependence), 2) *
(1 - Math.Pow(GetCoefficientPrivateCorrelation(matrix, xiXp, minusDependence), 2)));
}
buf[0] = dependence1[0];
buf[1] = dependence1[1];
return matrix[buf[0], buf[1]];
}
Класс матрицы:
[Serializable]
[ComVisible(false)]
public class Matrix<T> :
IEnumerable<T>,
IEquatable<Matrix<T>>
{
private readonly T[] _array;
public Matrix(int rowCount, int columnCount)
{
RowCount = rowCount;
ColumnCount = columnCount;
_array = new T[Count];
}
public int RowCount
{
get; private set;
}
public int ColumnCount
{
get; private set;
}
public int Count
{
get { return RowCount * ColumnCount; }
}
public T this[int row, int column]
{
get { return GetValue(row, column); }
set { SetValue(value, row, column); }
}
public IEnumerator<T> GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this);
}
private int GetIndex(int rowIndex, int columnIndex)
{
return rowIndex*ColumnCount + columnIndex;
}
private void SetValue(T value, int rowIndex, int columnIndex)
{
if (rowIndex < 0 ||
columnIndex < 0 ||
rowIndex > RowCount - 1 ||
columnIndex > ColumnCount - 1)
{
throw new IndexOutOfRangeException();
}
_array[GetIndex(rowIndex, columnIndex)] = value;
}
private T GetValue(int rowIndex, int columnIndex)
{
if (rowIndex < 0 ||
columnIndex < 0 ||
rowIndex > RowCount - 1 ||
columnIndex > ColumnCount - 1)
{
throw new IndexOutOfRangeException();
}
return _array[GetIndex(rowIndex, columnIndex)];
}
public void SetValues(T[] array)
{
if (array.Length != RowCount * ColumnCount)
{
throw new ArgumentOutOfRangeException();
}
int n = default(int);
for (int row = 0; row < RowCount; row++)
{
for (int column = 0; column < ColumnCount; column++)
{
this[row, column] = array[n++];
}
}
}
public T[] GetColumnValues(int columnIndex)
{
int index = default(int);
T[] result = new T[RowCount];
for (int row = 0; row < RowCount; row++)
{
result[index++] = this[row, columnIndex];
}
return result;
}
public T[] GetRowValues(int rowIndex)
{
int index = default(int);
T[] result = new T[ColumnCount];
for (int column = 0; column < ColumnCount; column++)
{
result[index++] = this[rowIndex, column];
}
return result;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder(Count);
foreach (T element in _array)
{
sb.AppendFormat("{0} ", element);
}
return sb.ToString();
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj is Matrix<T> ? Equals(obj as Matrix<T>) : false;
}
public bool Equals(Matrix<T> other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Equals(other._array, _array) &&
other.RowCount == RowCount &&
other.ColumnCount == ColumnCount;
}
public override int GetHashCode()
{
unchecked
{
int result = (_array != null ? _array.GetHashCode() : 0);
result = (result * 0x18D) ^ RowCount;
result = (result * 0x18D) ^ ColumnCount;
return result;
}
}
public static bool operator ==(Matrix<T> left, Matrix<T> right)
{
return Equals(left, right);
}
public static bool operator !=(Matrix<T> left, Matrix<T> right)
{
return !Equals(left, right);
}
[Serializable]
private struct Enumerator : IEnumerator<T>
{
private readonly T[] _array;
private int _position;
internal Enumerator(Matrix<T> matrix)
{
_array = matrix._array;
_position = -1;
}
public void Dispose()
{
_position = -1;
}
public bool MoveNext()
{
_position++;
return (_position < _array.Length);
}
public void Reset()
{
_position = -1;
}
public T Current
{
get
{
if (_position < 0 ||
_position > _array.Length - 1)
{
throw new IndexOutOfRangeException();
}
return _array[_position];
}
}
object IEnumerator.Current
{
get
{
return Current;
}
}
}
}
Коллеги помогите пожалуйста с задачкой, вся разработка проекта в это упирается
Заранее спасибо!