private static void TransformFeatures(IHost host, ref VBuffer <Float> src, ref VBuffer <Float> dst, TransformInfo transformInfo, AlignedArray featuresAligned, AlignedArray productAligned) { Contracts.AssertValue(host, "host"); host.Check(src.Length == transformInfo.SrcDim, "column does not have the expected dimensionality."); var values = dst.Values; Float scale; if (transformInfo.RotationTerms != null) { if (Utils.Size(values) < transformInfo.NewDim) { values = new Float[transformInfo.NewDim]; } scale = MathUtils.Sqrt((Float)2.0 / transformInfo.NewDim); } else { if (Utils.Size(values) < 2 * transformInfo.NewDim) { values = new Float[2 * transformInfo.NewDim]; } scale = MathUtils.Sqrt((Float)1.0 / transformInfo.NewDim); } if (src.IsDense) { featuresAligned.CopyFrom(src.Values, 0, src.Length); CpuMathUtils.MatTimesSrc(false, false, transformInfo.RndFourierVectors, featuresAligned, productAligned, transformInfo.NewDim); } else { // This overload of MatTimesSrc ignores the values in slots that are not in src.Indices, so there is // no need to zero them out. featuresAligned.CopyFrom(src.Indices, src.Values, 0, 0, src.Count, zeroItems: false); CpuMathUtils.MatTimesSrc(false, false, transformInfo.RndFourierVectors, src.Indices, featuresAligned, 0, 0, src.Count, productAligned, transformInfo.NewDim); } for (int i = 0; i < transformInfo.NewDim; i++) { var dotProduct = productAligned[i]; if (transformInfo.RotationTerms != null) { values[i] = (Float)MathUtils.Cos(dotProduct + transformInfo.RotationTerms[i]) * scale; } else { values[2 * i] = (Float)MathUtils.Cos(dotProduct) * scale; values[2 * i + 1] = (Float)MathUtils.Sin(dotProduct) * scale; } } dst = new VBuffer <Float>(transformInfo.RotationTerms == null ? 2 * transformInfo.NewDim : transformInfo.NewDim, values, dst.Indices); }
/// <summary> /// Matrix multiplication: /// if (add) /// dst = mat * src /// else /// dest += mat * src /// </summary> /// <param name="add">The addition flag</param> /// <param name="mat">The multiplier matrix</param> /// <param name="src">The source vector</param> /// <param name="dst">The destination vector</param> public static void MatTimesSrc(bool add, ICpuFullMatrix mat, ICpuVector src, ICpuVector dst) { bool colMajor = typeof(TMatrix) == typeof(CpuAlignedMatrixCol); AssertCompatible(mat, src, dst); var m = A(mat); CpuMathUtils.MatTimesSrc(colMajor, add, m.Items, A(src).Items, A(dst).Items, m.RunCnt); }
public void MatMulATest(int matTest, int srcTest, int dstTest, float[] expected) { AlignedArray mat = testMatrices[matTest]; AlignedArray src = testSrcVectors[srcTest]; AlignedArray dst = testDstVectors[dstTest]; CpuMathUtils.MatTimesSrc(false, false, mat, src, dst, dst.Size); float[] actual = new float[dst.Size]; dst.CopyTo(actual, 0, dst.Size); Assert.Equal(expected, actual, comparer); }
public void MatMulTranPAAddTest(int matTest, int srcTest, int dstTest, float[] expected) { AlignedArray mat = testMatrices[matTest]; AlignedArray src = testSrcVectors[srcTest]; AlignedArray dst = testDstVectors[dstTest]; int[] idx = testIndexArray; CpuMathUtils.MatTimesSrc(true, true, mat, idx, src, 0, 0, 2 + 2 * srcTest, dst, src.Size); float[] actual = new float[dst.Size]; dst.CopyTo(actual, 0, dst.Size); Assert.Equal(expected, actual, comparer); }
public void MatMulPATest(int matTest, int srcTest, int dstTest, float[] expected) { AlignedArray mat = _testMatrices[matTest]; AlignedArray src = _testSrcVectors[srcTest]; AlignedArray dst = _testDstVectors[dstTest]; int[] idx = _testIndexArray; CpuMathUtils.MatTimesSrc(false, false, mat, idx, src, 0, 0, (srcTest == 0) ? 4 : 9, dst, dst.Size); float[] actual = new float[dst.Size]; dst.CopyTo(actual, 0, dst.Size); Assert.Equal(expected, actual, _matMulComparer); }