static void Sample1() { const int C = 3; var data = new Matrix[C]; for (int i = 0; i < C; ++i) { data[i] = File.ReadAllLines(String.Format("data/iris/data_{0}.csv", i), Encoding.UTF8) .Select(line => line.Split(',').Select(s => Double.Parse(s)).ToRow()) .ToMatrix(); } Lda result = Func.Lda(data); // Coefficients of linear discriminants var ldc1 = new ColumnVector(result.Eigenvectors[0]); var ldc2 = new ColumnVector(result.Eigenvectors[1]); var coef = new Matrix(ldc1, ldc2); for (int i = 0; i < C; ++i) { var m = data[i] * coef; File.WriteAllText(String.Format("result_{0}.csv", i), Matrix.ToCsv(m), Encoding.UTF8); } }
public static void CanMultiply(Matrix m, ColumnVector cv) { if (m.ColumnSize != cv.Size) { throw new Exception.CannotMultiplyException( "Matrix.RowSize=" + m.RowSize + ", Matrix.ColumnSize=" + m.ColumnSize + ", ColumnVector.Size=" + cv.Size); } }
public void Test_Combination() { RowVector rv = new RowVector(1, 2, 3); ColumnVector cv = new ColumnVector(1, 2, 3); Assert.That(rv * cv, Is.EqualTo(14).Within(Ep)); Assert.That(cv * rv, Is.EqualTo( new Matrix(new [,] { { 1.0, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 } }) ).Within(Ep) ); }
public void Test_ColumnVector_Operator() { ColumnVector l = new ColumnVector(1, 1, 1); ColumnVector r = new ColumnVector(1, 1, 1); ColumnVector add = l + r; Assert.True(ColumnVector.Equals(add, new ColumnVector(2, 2, 2), Ep), add.ToString()); ColumnVector sub = l - r; Assert.True(ColumnVector.Equals(sub, new ColumnVector(0, 0, 0), Ep)); ColumnVector mul = l * 2; Assert.True(ColumnVector.Equals(mul, new ColumnVector(2, 2, 2), Ep)); ColumnVector mur = 2 * r; Assert.True(ColumnVector.Equals(mur, new ColumnVector(2, 2, 2), Ep)); ColumnVector div = l / 2; Assert.True(ColumnVector.Equals(div, new ColumnVector(0.5, 0.5, 0.5), Ep)); }
public void Test_VectorOperator() { Matrix A; A = new Matrix(new double[,]{ { 5, 3 }, { 2, -1 }, { 4, 2 }, }); ColumnVector cv = new ColumnVector(-1, 1); ColumnVector Yc = A * cv; Assert.AreEqual(new ColumnVector(-2, -3, -2), Yc); RowVector rv = new RowVector(-2, 0, 1); RowVector Yr = rv * A; Assert.AreEqual(new RowVector(-6, -4), Yr); }
public void Test_Properties() { Matrix m = new ColumnVector(1, 2, 3) * new RowVector(1, 2, 3); /* * 1 2 3 * 2 4 6 * 3 6 9 */ Assert.That(m.ColumnSize, Is.EqualTo(new Size(3))); Assert.That(m.ColumnAverages, Is.EqualTo(new Vector(2, 4, 6)).Within(Ep)); Assert.That(m.ColumnUnbiasedVariances, Is.EqualTo(new Vector(1, 4, 9)).Within(Ep)); Assert.That(m.RowSize, Is.EqualTo(new Size(3))); Assert.That(m.RowAverages, Is.EqualTo(new Vector(2, 4, 6)).Within(Ep)); Assert.That(m.RowUnbiasedVariances, Is.EqualTo(new Vector(1, 4, 9)).Within(Ep)); Assert.That(m.Trace, Is.EqualTo(14).Within(Ep)); Assert.That(m.Determinant, Is.EqualTo(0).Within(Ep)); }
public void Test_Vector_ColumnVector_Operator() { // Vector ��������ƁC�S�� Vector �ɂȂ� (���ۂɂȂ�) Vector l = new Vector(1, 1, 1); Vector r = new Vector(1, 1, 1); ColumnVector cv = new ColumnVector(1, 1, 1); Vector add = l + cv; Assert.That(add, Is.Not.InstanceOf(typeof(ColumnVector))); Assert.True(Vector.Equals(add, new Vector(2, 2, 2), Ep)); add = cv + r; Assert.That(add, Is.Not.InstanceOf(typeof(ColumnVector))); Assert.True(Vector.Equals(add, new Vector(2, 2, 2), Ep)); Vector sub = l - cv; Assert.That(sub, Is.Not.InstanceOf(typeof(ColumnVector))); Assert.True(Vector.Equals(sub, new Vector(0, 0, 0), Ep)); sub = cv - r; Assert.That(sub, Is.Not.InstanceOf(typeof(ColumnVector))); Assert.True(Vector.Equals(sub, new Vector(0, 0, 0), Ep)); }
public void Test_Equals() { Vector v = new Vector(1, 2, 3); RowVector rv = new RowVector(1, 2, 3); ColumnVector cv = new ColumnVector(1, 2, 3); // Vector.Equals �̓N���X�̋�ʂ���Ȃ� Assert.True(Vector.Equals(v, v, Ep)); Assert.True(Vector.Equals(v, rv, Ep)); Assert.True(Vector.Equals(v, cv, Ep)); //// RowVector.Equals �� RowVector ���m�̔�r�̂� //Assert.False(RowVector.Equals(rv, v, Ep)); //Assert.True(RowVector.Equals(rv, rv, Ep)); //Assert.False(RowVector.Equals(rv, cv, Ep)); //// ColumnVector.Equals �� ColumnVector ���m�̔�r�̂� //Assert.False(ColumnVector.Equals(cv, v, Ep)); //Assert.False(ColumnVector.Equals(cv, rv, Ep)); //Assert.True(ColumnVector.Equals(cv, cv, Ep)); }
public void Test01() { Matrix m = new ColumnVector(1, 2, 3) * new RowVector(1, 2, 3); // �s�� double d; int n; Vector v; v = m.ColumnAverages; // �e��̕��ϒl Assert.IsTrue(Vector.HaveSameValues(new Vector(2, 4, 6), v)); v = m.ColumnVariances; // �e��̕W�{���U Assert.IsTrue(Vector.HaveSameValues(new Vector(1, 4, 9), v)); n = m.ColumnSize; // �� v = m.RowAverages; // �e�s�̕��ϒl Assert.IsTrue(Vector.HaveSameValues(new Vector(2, 4, 6), v)); v = m.RowVariances; // �e�s�̕W�{���U Assert.IsTrue(Vector.HaveSameValues(new Vector(1, 4, 9), v)); n = m.RowSize; // �� d = m.Trace; // �s��̃g���[�X Assert.AreEqual(14, d, LisysConfig.CalculationLowerLimit); d = m.Determinant; // �s�� Assert.AreEqual(0, d, LisysConfig.CalculationLowerLimit); m = new Matrix( new RowVector(0, 1, -1), new RowVector(-1, 1, 0), new RowVector(2, 1, 0)); // �t�s�� Matrix im; im = Matrix.Inverse(m); Assert.AreEqual(Matrix.Identity(3), m * im);// Matrix.Identity(3) �́C�u3�~3�̒P�ʍs��v�ɂȂ� im = new Matrix(m); im.Inverse(); Assert.AreEqual(Matrix.Identity(3), m * im);// Matrix.Identity(3) �́C�u3�~3�̒P�ʍs��v�ɂȂ� // �]�u Matrix tm = Matrix.Transpose(m); Assert.AreEqual(new Matrix( new ColumnVector(0, 1, -1), new ColumnVector(-1, 1, 0), new ColumnVector(2, 1, 0)), tm); tm = new Matrix(m); tm.Transpose(); Assert.AreEqual(new Matrix( new ColumnVector(0, 1, -1), new ColumnVector(-1, 1, 0), new ColumnVector(2, 1, 0)), tm); // �W���� m[1, 1] = 2; Matrix ms = Function.Standardize(m, Target.EachColumn, VarianceType.DivN); foreach (IVector cv in ms.Columns) { Assert.AreEqual(0, cv.Average, LisysConfig.CalculationLowerLimit); Assert.AreEqual(1, cv.Scatter / cv.Size, LisysConfig.CalculationLowerLimit); } // �e��s���@ m = new Matrix(new ColumnVector(1, 1, 1), new ColumnVector(2, 2, 2), new ColumnVector(3, 3, 3)); Assert.AreEqual(new Matrix(new double[,] { { 1, 2, 3 }, { 1, 2, 3 }, { 1, 2, 3 } }), m); m = new Matrix(new RowVector(1, 1, 1), new RowVector(2, 2, 2), new RowVector(3, 3, 3)); Assert.AreEqual(new Matrix(new double[,] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 } }), m); m = new Matrix(new double[,] { { 1, 1, 1 }, { 2, 2, 2 }, { 3, 3, 3 }, { 4, 4, 4 } }); }
/// <summary> /// 標準化された<see cref="ColumnVector"/>を作成する. /// </summary> /// <param name="vector">標準化される<see cref="ColumnVector"/>(実際は,この引数のコピーが標準化される)</param> /// <param name="varType">標準化に使用する分散の種類</param> /// <returns>標準化された<see cref="ColumnVector"/></returns> /// /// <exception cref="Exception.ValueIsLessThanLimitException"> /// 標準偏差値が設定されている精度の下限値未満の場合にthrowされる. /// </exception> public static ColumnVector Standardize(ColumnVector vector, VarianceType varType) { return(_Standardize_T(new ColumnVector(vector), varType)); }