public void Solve() { ColumnWiseMatrix A = GetInvertibleMatrix(128); DeviceManager.CheckDeviceSanity(); var _A = A.GetMatrix <float>(); ColumnWiseMatrix B = GetInvertibleMatrix(128, 2345); DeviceManager.CheckDeviceSanity(); var _B = B.GetMatrix <float>(); A.Solve(B); DeviceManager.CheckDeviceSanity(); var _x = B.Get <float>(); var BSanity = A.Multiply(B); var _BSanity = BSanity.GetMatrix <float>(); for (int i = 0; i < A.nRows; ++i) { for (int j = 0; j < A.nCols; ++j) { double expected = _B[i, j]; Assert.IsTrue(Math.Abs(_BSanity[i, j] - expected) <= 5e-5); } } }
public void Invert() { ColumnWiseMatrix A = GetInvertibleMatrix(128); DeviceManager.CheckDeviceSanity(); ColumnWiseMatrix AMinus1 = new ColumnWiseMatrix(A); AMinus1.Invert(); DeviceManager.CheckDeviceSanity(); var eye = A.Multiply(AMinus1); var _eye = eye.GetMatrix <float>(); var _A = A.GetMatrix <float>(); var _AMinus1 = AMinus1.GetMatrix <float>(); for (int i = 0; i < A.nRows; ++i) { for (int j = 0; j < A.nCols; ++j) { double expected = i == j ? 1.0 : 0.0; Assert.IsTrue(Math.Abs(_eye[i, j] - expected) <= 5e-5); } } }
public void Dot() { ColumnWiseMatrix m1 = new ColumnWiseMatrix(10, 10, 1.2345f); DeviceManager.CheckDeviceSanity(); var _m1 = m1.GetMatrix <float>(); Vector v1 = new Vector(10, 9.8765f); DeviceManager.CheckDeviceSanity(); var _v1 = v1.Get <float>(); var v2 = m1 * v1; DeviceManager.CheckDeviceSanity(); var _v2 = v2.Get <float>(); for (int i = 0; i < m1.nRows; ++i) { double m1v1 = 0.0; for (int j = 0; j < m1.nCols; ++j) { m1v1 += _m1[i, j] * _v1[j]; } Assert.IsTrue(Math.Abs(m1v1 - _v2[i]) <= 5e-5); } }
public void Multiply() { ColumnWiseMatrix m1 = new ColumnWiseMatrix(10, 10, 1.2345f); DeviceManager.CheckDeviceSanity(); var _m1 = m1.GetMatrix <float>(); ColumnWiseMatrix m2 = new ColumnWiseMatrix(10, 10, 9.8765f); DeviceManager.CheckDeviceSanity(); var _m2 = m2.GetMatrix <float>(); var m3 = m1 * m2; DeviceManager.CheckDeviceSanity(); var _m3 = m3.GetMatrix <float>(); for (int i = 0; i < m1.nRows; ++i) { for (int j = 0; j < m1.nCols; ++j) { double m1m2 = 0.0; for (int k = 0; k < m1.nCols; ++k) { m1m2 += _m1[i, k] * _m2[k, j]; } Assert.IsTrue(Math.Abs(m1m2 - _m3[i, j]) <= 5e-5); } } }
private void Compress <T>(ColumnWiseMatrix denseMatrix) where T : struct, IEquatable <T>, IFormattable { var hostDenseMatrix = denseMatrix.GetMatrix <T>(); List <T> nonZeroValues = new List <T>(); List <int> nonZeroColumnIndices = new List <int>(); List <int> nNonZeroRows = new List <int>(); int nNonZeros = 0; for (int i = 0; i < denseMatrix.nRows; i++) { for (int j = 0; j < denseMatrix.nCols; j++) { if (Math.Abs(Convert.ToDouble(hostDenseMatrix[i, j])) > 1e-7) { nNonZeros++; nonZeroValues.Add(hostDenseMatrix[i, j]); nonZeroColumnIndices.Add(j); } } nNonZeroRows.Add(nNonZeros); } Buffer.size = (uint)nNonZeros; values = new Vector(nonZeroValues.Count, memorySpace, mathDomain); values.ReadFrom(nonZeroValues, nonZeroValues.Count); this.nonZeroColumnIndices = new Vector(nonZeroColumnIndices.Count, memorySpace, MathDomain.Int); this.nonZeroColumnIndices.ReadFrom(nonZeroColumnIndices, nonZeroColumnIndices.Count); this.nNonZeroRows = new Vector(nNonZeroRows.Count, memorySpace, MathDomain.Int); this.nNonZeroRows.ReadFrom(nNonZeroRows, nNonZeroRows.Count); }
public void ColumnWiseArgAbsoluteMinMax() { ColumnWiseMatrix A = ColumnWiseMatrix.LinSpace(128, 64, -1, 1); var _A = A.GetMatrix <float>(); var min = A.ColumnWiseAbsoluteMinimumIndex().GetRaw <int>(); var max = A.ColumnWiseAbsoluteMaximumIndex().GetRaw <int>(); int[] _min = new int[A.nCols]; int[] _max = new int[A.nCols]; for (int j = 0; j < A.nCols; j++) { _min[j] = _A.Column(j).AbsoluteMinimumIndex(); _max[j] = _A.Column(j).AbsoluteMaximumIndex(); Assert.AreEqual(min[j] - 1, _min[j]); Assert.AreEqual(max[j] - 1, _max[j]); } }
public void AddMatrix() { ColumnWiseMatrix m1 = ColumnWiseMatrix.LinSpace(100, 20, -1.0, 1.0); DeviceManager.CheckDeviceSanity(); var _m1 = m1.GetMatrix <float>(); ColumnWiseMatrix m2 = ColumnWiseMatrix.RandomGaussian(m1.nRows, m1.nCols, 1234); DeviceManager.CheckDeviceSanity(); var _m2 = m2.GetMatrix <float>(); var m3 = m1 + m2; DeviceManager.CheckDeviceSanity(); var _m3 = m3.GetMatrix <float>(); for (int i = 0; i < m1.nRows; ++i) { for (int j = 0; j < m1.nCols; ++j) { Assert.IsTrue(Math.Abs(_m3[i, j] - _m1[i, j] - _m2[i, j]) <= 3e-7, String.Format("i({0}) j({1}) err({2})", i, j, Math.Abs(_m3[i, j] - _m1[i, j] - _m2[i, j]))); } } var m4 = ColumnWiseMatrix.Add(m1, m2, alpha: 2.0); DeviceManager.CheckDeviceSanity(); var _m4 = m4.GetMatrix <float>(); for (int i = 0; i < m1.nRows; ++i) { for (int j = 0; j < m1.nCols; ++j) { Assert.IsTrue(Math.Abs(_m4[i, j] - _m1[i, j] - 2.0 * _m2[i, j]) <= 5e-7, String.Format("i({0}) j({1}) err({2})", i, j, Math.Abs(_m4[i, j] - _m1[i, j] - 2.0 * _m2[i, j]))); } } }
public void Multiply() { int[] _NonZeroCols = new int[] { 0, 1, 1, 3, 2, 3, 4, 5 }; Vector gpuNonZeroCols = new Vector(_NonZeroCols); int[] _NonZeroRows = new int[] { 0, 2, 4, 7, 8 }; Vector gpuNonZeroRows = new Vector(_NonZeroRows); CompressedSparseRowMatrix m1 = new CompressedSparseRowMatrix(4, 6, gpuNonZeroCols, gpuNonZeroRows, 1.2345f, MathDomain.Float); DeviceManager.CheckDeviceSanity(); var _m1 = m1.GetMatrix <float>(); ColumnWiseMatrix m2 = new ColumnWiseMatrix(6, 8, 9.8765f, MemorySpace.Device, MathDomain.Float); DeviceManager.CheckDeviceSanity(); var _m2 = m2.GetMatrix <float>(); var m3 = m1 * m2; DeviceManager.CheckDeviceSanity(); var _m3 = m3.GetMatrix <float>(); for (int i = 0; i < m1.nRows; ++i) { for (int j = 0; j < m2.nCols; ++j) { double m1m2 = 0.0; for (int k = 0; k < m1.nCols; ++k) { m1m2 += _m1[i, k] * _m2[k, j]; } Assert.IsTrue(Math.Abs(m1m2 - _m3[i, j]) <= 5e-5); } } }