public void SparseSquareMatrixAgreement() { int d = 6; SparseSquareMatrix A = new SparseSquareMatrix(d); SquareMatrix B = new SquareMatrix(d); Random rng = new Random(1); for (int i = 0; i < 2 * d; i++) { int r = (int)Math.Floor(rng.NextDouble() * d); int c = (int)Math.Floor(rng.NextDouble() * d); A[r, c] = 2.0 * rng.NextDouble() - 1.0; B[r, c] = A[r, c]; } RowVector u = new RowVector(d); ColumnVector v = new ColumnVector(d); for (int i = 0; i < d; i++) { u[i] = 2.0 * rng.NextDouble() - 1.0; v[i] = 2.0 * rng.NextDouble() - 1.0; } RowVector uA = u * A; RowVector uB = u * B; Assert.IsTrue(TestUtilities.IsNearlyEqual(uA, uB)); ColumnVector Av = A * v; ColumnVector Bv = B * v; Assert.IsTrue(TestUtilities.IsNearlyEqual(Av, Bv)); }
public void ExtendedVectorNorm() { // Check that vector norm is correctly computed even when vector is enormous (square would overflow) or tiny (square woudl underflow) double x = Double.MaxValue / 32.0; // Use the pythagorian quadrouple (2, 3, 6, 7) ColumnVector v = new ColumnVector(2.0, 3.0, 6.0); Assert.IsTrue(TestUtilities.IsNearlyEqual(v.Norm(), 7.0)); ColumnVector bv = x * v; Assert.IsTrue(TestUtilities.IsNearlyEqual(bv.Norm(), 7.0 * x)); ColumnVector sv = v / x; Assert.IsTrue(TestUtilities.IsNearlyEqual(sv.Norm(), 7.0 / x)); // Use the pythagorian 7-tuple (1, 2, 3, 4, 5, 27, 28) RowVector u = new RowVector(1.0, 2.0, 3.0, 4.0, 5.0, 27.0); Assert.IsTrue(TestUtilities.IsNearlyEqual(u.Norm(), 28.0)); RowVector bu = x * u; Assert.IsTrue(TestUtilities.IsNearlyEqual(bu.Norm(), 28.0 * x)); RowVector su = u / x; Assert.IsTrue(TestUtilities.IsNearlyEqual(su.Norm(), 28.0 / x)); }
public void Test_RowVector_Operator() { RowVector l = new RowVector(1, 1, 1); RowVector r = new RowVector(1, 1, 1); RowVector add = l + r; Assert.True(RowVector.Equals(add, new RowVector(2, 2, 2), Ep)); RowVector sub = l - r; Assert.True(RowVector.Equals(sub, new RowVector(0, 0, 0), Ep)); RowVector mul = l * 2; Assert.True(RowVector.Equals(mul, new RowVector(2, 2, 2), Ep)); RowVector mur = 2 * r; Assert.True(RowVector.Equals(mur, new RowVector(2, 2, 2), Ep)); RowVector div = l / 2; Assert.True(RowVector.Equals(div, new RowVector(0.5, 0.5, 0.5), Ep)); }
/// <summary> /// Return row of a matrix /// </summary> /// <param name="rowIndex"></param> /// <returns>Vector</returns> public static RowVector GetAugmentedRow(this RealMatrix matrix, int rowIndex) { var row = matrix.B[rowIndex].GetRange(0, matrix.AugmentedColumnCount).ToList(); var vector = new RowVector(v: row); return(vector); }
/// <summary> /// Replace row with vector that same size /// </summary> /// <param name="vector">Vector</param> /// <param name="rowIndex">Row index</param> public static void WriteOnRow(this RealMatrix matrix, RowVector rowVector, int rowIndex, bool isAugmentedRow = false) { //check that matrix row length (column count) is equal to vector length //and row index is valid if (!isAugmentedRow) { if (rowVector.V.Count != matrix.ColumnCount && rowIndex >= matrix.RowCount) { return; } for (int i = 0; i < rowVector.V.Count; i++) { matrix.M[rowIndex][i] = rowVector.V[i]; } } else { if (rowVector.V.Count != matrix.AugmentedColumnCount && rowIndex >= matrix.AugmentedRowCount) { return; } for (int i = 0; i < rowVector.V.Count; i++) { matrix.B[rowIndex][i] = rowVector.V[i]; } } matrix.MatrixElementsChanged(); }
/// <summary> /// Sets the r'th row of the matrix m to x. /// Requires: x.Dimension == m.ColumnCount /// </summary> /// <param name="m">A matrix whose r'th row will be modified.</param> /// <param name="x">A row vector to copy into m.</param> /// <param name="r">The zero-based index of the row of m to be modified.</param> public static void CopyRowInto(this RectangularMatrix m, RowVector x, int r) { for (int i = 0; i < x.Dimension; ++i) { m[r, i] = x[i]; } }
public void RowVectorArithmetic() { // addition and multiplication by a double RowVector RA = R + R; RowVector R2 = 2.0 * R; Assert.IsTrue(RA == R2); // subtraction and multiplication by a double RowVector RS = R - R; RowVector R0 = 0.0 * R; Assert.IsTrue(RS == R0); // negation and multiplication by -1 RowVector RN = -R; RowVector RD = R / (-1.0); Assert.IsTrue(RN == RD); // multiply a column by a matrix RowVector RM = R * M; Assert.IsTrue(RM.Dimension == M.ColumnCount); // dot multiply ColumnVector RT = R.Transpose(); double dot = R * RT; Assert.IsTrue(dot > 0); }
public void ColumnVectorArithmetic() { // addition and multiplication by a double ColumnVector CA = C + C; ColumnVector C2 = 2.0 * C; Assert.IsTrue(CA == C2); // subtraction and multiplication by a double ColumnVector CS = C - C; ColumnVector C0 = 0.0 * C; Assert.IsTrue(CS == C0); // negation and division by -1 ColumnVector CN = -C; ColumnVector CD = C / (-1.0); Assert.IsTrue(CN == CD); // multiply a column by a matrix ColumnVector MC = M * C; Assert.IsTrue(MC.Dimension == M.RowCount); // dot multiply RowVector CT = C.Transpose(); double dot = CT * C; Assert.IsTrue(dot > 0); }
public void UnitMatrixArithmetic() { SquareMatrix A = new SquareMatrix(new double[, ] { { 1.0, -2.0 }, { -3.0, 4.0 } }); SquareMatrix AI = A * UnitMatrix.OfDimension(A.Dimension); Assert.IsTrue(A == AI); SquareMatrix IA = UnitMatrix.OfDimension(A.Dimension) * A; Assert.IsTrue(IA == A); ColumnVector c = new ColumnVector(1.0, 2.0, 3.0); ColumnVector Ic = UnitMatrix.OfDimension(c.Dimension) * c; Assert.IsTrue(Ic == c); RowVector r = new RowVector(0.0, 1.0); RowVector rI = r * UnitMatrix.OfDimension(r.Dimension); Assert.IsTrue(rI == r); Assert.IsTrue(UnitMatrix.OfDimension(A.Dimension) == UnitMatrix.OfDimension(A.Dimension)); Assert.IsTrue(0.0 * UnitMatrix.OfDimension(3) == UnitMatrix.OfDimension(3) - UnitMatrix.OfDimension(3)); Assert.IsTrue(1.0 * UnitMatrix.OfDimension(3) == UnitMatrix.OfDimension(3)); Assert.IsTrue(2.0 * UnitMatrix.OfDimension(3) == UnitMatrix.OfDimension(3) + UnitMatrix.OfDimension(3)); }
public void MultiplicationRowVectorByColumnVectorExceptionTest() { var v1 = new RowVector(new double[] { 1, 2, 3, 4 }); var v2 = new ColumnVector(new double[] { 5, 6, 7, 8, 9 }); var scalarProduct = v1 * v2; }
public void ScalarProductExceptionTest() { var v1 = new RowVector(new double[] { 1, 2, 3, 4 }); var v2 = new RowVector(new double[] { 5, 6, 7, 8, 9 }); var scalarProduct = v1 * v2; }
/// <summary> /// Return row of a matrix /// </summary> /// <param name="rowIndex"></param> /// <returns>Vector</returns> public static RowVector GetRow(this RealMatrix matrix, int rowIndex) { var row = new List <IR.RealNumber>(); row.AddRange(matrix.M[rowIndex].GetRange(0, matrix.ColumnCount).ToList()); var vector = new RowVector(v: row); return(vector); }
/// <summary> /// Computes the profit-maximizing decision (DECT0), given true costs and selling prices. /// </summary> /// <returns>A [1 x ip.CO] vector of 1's and 0's. 1.0 (0.0) indicates /// that the product is (not)included in the profit-maximizing product mix.</returns> public RowVector CalcOptimalDecision() { RowVector pc_b = CalcTrueProductCosts(); RowVector productProfits = this.sp - pc_b; // Replace each negative (nonnegative) element of productProfits with a 0.0 (1.0) var dect0 = productProfits.Map(x => ((x < 0.0) ? 0.0 : 1.0)); return(new RowVector(dect0)); }
public void VectorNorm() { RowVector nR = R / R.Norm(); Assert.IsTrue(TestUtilities.IsNearlyEqual(nR.Norm(), 1.0)); ColumnVector nC = C / C.Norm(); Assert.IsTrue(TestUtilities.IsNearlyEqual(nC.Norm(), 1.0)); }
public void InstantiateRowVectorWithArray() { var vector = new RowVector(new double[] { 1, 2, 3, 4 }); var expectedValues = new double[] { 1, 2, 3, 4 }; bool isEqual = Helpers.AreEqualVector(expectedValues, vector); Assert.IsTrue(isEqual); }
public void InstantiateRowVectorWithValueTest() { var vector = new RowVector(4); var expectedValues = new double[] { 0, 0, 0, 0 }; bool isEqual = Helpers.AreEqualVector(expectedValues, vector); Assert.IsTrue(isEqual); }
public void InstantiateColumnVectorWithVector() { var v = new RowVector(new double[] { 1, 2, 3, 4 }) as Vector; var vector = new ColumnVector(v); var expectedValues = new double[] { 1, 2, 3, 4 }; bool isEqual = Helpers.AreEqualVector(expectedValues, vector); Assert.IsTrue(isEqual); }
public void MultiplicationRowVectorByColumnVectorTest() { var v1 = new RowVector(new double[] { 1, 2, 3, 4 }); var v2 = new ColumnVector(new double[] { 5, 6, 7, 8 }); var expectedResult = 70; var scalarProduct = v1 * v2; Assert.AreEqual(expectedResult, scalarProduct); }
public void ScalarProductTest() { var v1 = new RowVector(new double[] { 1, 2, 3, 4 }); var v2 = new RowVector(new double[] { 5, 6, 7, 8 }); var expectedScalarProduct = 70; var scalarProduct = v1 * v2; Assert.AreEqual(expectedScalarProduct, scalarProduct); }
public void RectangularMatrixAccess() { // create a matrix via outer product ColumnVector cv = new ColumnVector(new double[] { 1, 2 }); RowVector rv = new RowVector(new double[] { 3, 4, 5 }); RectangularMatrix M = cv * rv; // check dimensions Assert.IsTrue(M.RowCount == cv.Dimension); Assert.IsTrue(M.ColumnCount == rv.Dimension); // check values for (int r = 0; r < M.RowCount; r++) { for (int c = 0; c < M.ColumnCount; c++) { Assert.IsTrue(M[r, c] == cv[r] * rv[c]); } } // extract a column ColumnVector mc = M.Column(1); Assert.IsTrue(mc.Dimension == M.RowCount); for (int i = 0; i < mc.Dimension; i++) { Assert.IsTrue(mc[i] == M[i, 1]); } // extract a row RowVector mr = M.Row(1); Assert.IsTrue(mr.Dimension == M.ColumnCount); for (int i = 0; i < mr.Dimension; i++) { Assert.IsTrue(mr[i] == M[1, i]); } // test clone RectangularMatrix MC = M.Copy(); Assert.IsTrue(MC.RowCount == M.RowCount); Assert.IsTrue(MC.ColumnCount == M.ColumnCount); // test equality of clone Assert.IsTrue(MC == M); Assert.IsFalse(MC != M); // test independence of clone MC[0, 0] += 1.0; Assert.IsFalse(MC == M); Assert.IsTrue(MC != M); }
/// <summary> /// Computes the profit-maximizing production quantities (QT), given true costs and selling prices. /// </summary> /// <returns>A [ip.CO x 1] vector of production quantities.</returns> public ColumnVector CalcOptimalVol() { RowVector dect0 = CalcOptimalDecision(); if (mxq.Dimension != dect0.Dimension) { throw new ApplicationException("MXQ and DECT0 have different lengths."); } var qt = mxq.Zip(dect0, (maxq, decision) => maxq * decision); return(new ColumnVector(qt.ToArray())); }
public void TransposeTest() { var rowVector = new RowVector(new double[] { 1, 2, 3, 4 }); var columnVector = rowVector.Transpose(); var expectedColumnVector = new double[] { 1, 2, 3, 4 }; Assert.IsInstanceOfType(columnVector, typeof(ColumnVector)); bool isEqual = Helpers.AreEqualVector(expectedColumnVector, columnVector); Assert.IsTrue(isEqual); }
public static void VectorsAndMatrices() { ColumnVector v = new ColumnVector(0.0, 1.0, 2.0); ColumnVector w = new ColumnVector(new double[] { 1.0, -0.5, 1.5 }); SquareMatrix A = new SquareMatrix(new double[, ] { { 1, -2, 3 }, { 2, -5, 12 }, { 0, 2, -10 } }); RowVector u = new RowVector(4); for (int i = 0; i < u.Dimension; i++) { u[i] = i; } Random rng = new Random(1); RectangularMatrix B = new RectangularMatrix(4, 3); for (int r = 0; r < B.RowCount; r++) { for (int c = 0; c < B.ColumnCount; c++) { B[r, c] = rng.NextDouble(); } } SquareMatrix AI = A.Inverse(); PrintMatrix("A * AI", A * AI); PrintMatrix("v + 2.0 * w", v + 2.0 * w); PrintMatrix("Av", A * v); PrintMatrix("B A", B * A); PrintMatrix("v^T", v.Transpose); PrintMatrix("B^T", B.Transpose); Console.WriteLine($"|v| = {v.Norm()}"); Console.WriteLine($"sqrt(v^T v) = {Math.Sqrt(v.Transpose * v)}"); UnitMatrix I = UnitMatrix.OfDimension(3); PrintMatrix("IA", I * A); Console.WriteLine(v == w); Console.WriteLine(I * A == A); }
private void Populate(List <TrainingSetRecord> datalist) { for (int i = 0; i < datalist.Count; i++) { RowVector row = new RowVector((double)DataCell[0].Values.Where(v => v.Value == datalist[i].Tempo.ToString()).FirstOrDefault().Key , (double)DataCell[1].Values.Where(v => v.Value == datalist[i].Danceability.ToString()).FirstOrDefault().Key , (double)DataCell[2].Values.Where(v => v.Value == datalist[i].Energy.ToString()).FirstOrDefault().Key , (double)DataCell[3].Values.Where(v => v.Value == datalist[i].Key.ToString()).FirstOrDefault().Key); Dataset.AddRow(row); Target.AddRow(new RowVector(datalist[i].Grouping.ToList()[0].Favourite.Value ? 1f : 0f)); } }
public void VectorAsMatrix() { RowVector r = new RowVector(1.0, 2.0, 3.0); Assert.IsTrue(r[0, 1] == r[1]); r[0, 1] = 0.0; Assert.IsTrue(r[1] == 0.0); ColumnVector c = r.Transpose.Copy(); Assert.IsTrue(c[1, 0] == 0.0); c[1, 0] = 2.0; Assert.IsTrue(c[1] == 2.0); }
internal RowVector GetVector() { var vector = new RowVector(this.RowQuantity * this.ColumnQuantity); for (var idxRow = 0; idxRow < this.RowQuantity; idxRow++) { for (var idxCol = 0; idxCol < this.ColumnQuantity; idxCol++) { vector[(idxRow * this.ColumnQuantity) + idxCol] = this[idxRow, idxCol]; } } return(vector); }
public void RowVectorCopy() { RowVector v = new RowVector(3); // test cloning and equality/inequality testing RowVector vc = v.Copy(); Assert.IsTrue(vc == v); Assert.IsFalse(vc != v); // test independence clone and equality/inequality testing vc[0] += 1.0; Assert.IsFalse(vc == v); Assert.IsTrue(vc != v); }
public void MultiplyVectorByMatrixTest() { var m = new Matrix(new double[, ] { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } }, true); var v = new RowVector(new double[] { 1, 2, 3 }); var expectedVector = new RowVector(new double[] { 38, 44, 50, 56 }); var resVector = v * m; var isEqual = Helpers.AreEqualVector(expectedVector, resVector); Assert.IsTrue(isEqual); }
public void Test09() { RowVector rv = new RowVector(1, 2, 3); ColumnVector cv = new ColumnVector(1, 2, 3); double val = rv * cv; Assert.AreEqual(14, val, LisysConfig.CalculationLowerLimit); Matrix m = cv * rv; Assert.AreEqual(new Matrix(new double[, ] { { 1, 2, 3 }, { 2, 4, 6 }, { 3, 6, 9 } }), m); }
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) ); }