/// <summary> /// Adds a scaled vector to another: <c>result = y + alpha*x</c>. /// </summary> /// <param name="y">The vector to update.</param> /// <param name="alpha">The value to scale <paramref name="x"/> by.</param> /// <param name="x">The vector to add to <paramref name="y"/>.</param> /// <param name="result">The result of the addition.</param> /// <remarks>This is similar to the AXPY BLAS routine.</remarks> public virtual void AddVectorToScaledVector(Complex[] y, Complex alpha, Complex[] x, Complex[] result) { if (y == null) { throw new ArgumentNullException("y"); } if (x == null) { throw new ArgumentNullException("x"); } if (y.Length != x.Length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength); } if (y.Length != x.Length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength); } if (alpha.IsZero()) { CommonParallel.For(0, y.Length, index => result[index] = y[index]); } else if (alpha.IsOne()) { CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); } else { CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); } }
public void IsOneTest() { Assert.IsFalse(a.IsOne()); Complex one = new Complex(field, BigInt.ONE); Assert.IsTrue(one.IsOne()); }
/// <summary> /// Adds a scaled vector to another: <c>result = y + alpha*x</c>. /// </summary> /// <param name="y">The vector to update.</param> /// <param name="alpha">The value to scale <paramref name="x"/> by.</param> /// <param name="x">The vector to add to <paramref name="y"/>.</param> /// <param name="result">The result of the addition.</param> /// <remarks>This is similar to the AXPY BLAS routine.</remarks> public virtual void AddVectorToScaledVector(Complex[] y, Complex alpha, Complex[] x, Complex[] result) { if (y == null) { throw new ArgumentNullException("y"); } if (x == null) { throw new ArgumentNullException("x"); } if (y.Length != x.Length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength); } if (y.Length != x.Length) { throw new ArgumentException(Resources.ArgumentVectorsSameLength); } if (alpha.IsZero()) { y.Copy(result); } else if (alpha.IsOne()) { if (Control.ParallelizeOperation(x.Length)) { CommonParallel.For(0, y.Length, index => result[index] = y[index] + x[index]); } else { for (var index = 0; index < x.Length; index++) { result[index] = y[index] + x[index]; } } } else { if (Control.ParallelizeOperation(x.Length)) { CommonParallel.For(0, y.Length, index => result[index] = y[index] + (alpha * x[index])); } else { for (var index = 0; index < x.Length; index++) { result[index] = y[index] + (alpha * x[index]); } } } }
/// <summary> /// Scales an array. Can be used to scale a vector and a matrix. /// </summary> /// <param name="alpha">The scalar.</param> /// <param name="x">The values to scale.</param> /// <remarks>This is equivalent to the SCAL BLAS routine.</remarks> public void ScaleArray(Complex alpha, Complex[] x) { if (x == null) { throw new ArgumentNullException("x"); } if (alpha.IsOne()) { return; } SafeNativeMethods.z_scale(x.Length, ref alpha, x); }
public void CanDetermineIfOneValueComplexNumber() { var complex = new Complex(1, 0); Assert.IsTrue(complex.IsOne(), "Complex number with a value of one."); }
/// <summary> /// Multiplies two matrices and updates another with the result. <c>c = alpha*op(a)*op(b) + beta*c</c> /// </summary> /// <param name="transposeA">How to transpose the <paramref name="a"/> matrix.</param> /// <param name="transposeB">How to transpose the <paramref name="b"/> matrix.</param> /// <param name="alpha">The value to scale <paramref name="a"/> matrix.</param> /// <param name="a">The a matrix.</param> /// <param name="rowsA">The number of rows in the <paramref name="a"/> matrix.</param> /// <param name="columnsA">The number of columns in the <paramref name="a"/> matrix.</param> /// <param name="b">The b matrix</param> /// <param name="rowsB">The number of rows in the <paramref name="b"/> matrix.</param> /// <param name="columnsB">The number of columns in the <paramref name="b"/> matrix.</param> /// <param name="beta">The value to scale the <paramref name="c"/> matrix.</param> /// <param name="c">The c matrix.</param> public virtual void MatrixMultiplyWithUpdate(Transpose transposeA, Transpose transposeB, Complex alpha, Complex[] a, int rowsA, int columnsA, Complex[] b, int rowsB, int columnsB, Complex beta, Complex[] c) { int m; // The number of rows of matrix op(A) and of the matrix C. int n; // The number of columns of matrix op(B) and of the matrix C. int k; // The number of columns of matrix op(A) and the rows of the matrix op(B). // First check some basic requirement on the parameters of the matrix multiplication. if (a == null) { throw new ArgumentNullException("a"); } if (b == null) { throw new ArgumentNullException("b"); } if ((int)transposeA > 111 && (int)transposeB > 111) { if (rowsA != columnsB) { throw new ArgumentOutOfRangeException(); } if (columnsA * rowsB != c.Length) { throw new ArgumentOutOfRangeException(); } m = columnsA; n = rowsB; k = rowsA; } else if ((int)transposeA > 111) { if (rowsA != rowsB) { throw new ArgumentOutOfRangeException(); } if (columnsA * columnsB != c.Length) { throw new ArgumentOutOfRangeException(); } m = columnsA; n = columnsB; k = rowsA; } else if ((int)transposeB > 111) { if (columnsA != columnsB) { throw new ArgumentOutOfRangeException(); } if (rowsA * rowsB != c.Length) { throw new ArgumentOutOfRangeException(); } m = rowsA; n = rowsB; k = columnsA; } else { if (columnsA != rowsB) { throw new ArgumentOutOfRangeException(); } if (rowsA * columnsB != c.Length) { throw new ArgumentOutOfRangeException(); } m = rowsA; n = columnsB; k = columnsA; } if (alpha.IsZero() && beta.IsZero()) { Array.Clear(c, 0, c.Length); return; } // Check whether we will be overwriting any of our inputs and make copies if necessary. // TODO - we can don't have to allocate a completely new matrix when x or y point to the same memory // as result, we can do it on a row wise basis. We should investigate this. Complex[] adata; if (ReferenceEquals(a, c)) { adata = (Complex[])a.Clone(); } else { adata = a; } Complex[] bdata; if (ReferenceEquals(b, c)) { bdata = (Complex[])b.Clone(); } else { bdata = b; } if (beta.IsZero()) { Array.Clear(c, 0, c.Length); } else if (!beta.IsOne()) { Control.LinearAlgebraProvider.ScaleArray(beta, c, c); } if (alpha.IsZero()) { return; } CacheObliviousMatrixMultiply(transposeA, transposeB, alpha, adata, 0, 0, bdata, 0, 0, c, 0, 0, m, n, k, m, n, k, true); }
/// <summary> /// Scales an array. Can be used to scale a vector and a matrix. /// </summary> /// <param name="alpha">The scalar.</param> /// <param name="x">The values to scale.</param> /// <param name="result">This result of the scaling.</param> /// <remarks>This is similar to the SCAL BLAS routine.</remarks> public virtual void ScaleArray(Complex alpha, Complex[] x, Complex[] result) { if (x == null) { throw new ArgumentNullException("x"); } if (alpha.IsZero()) { Array.Clear(result, 0, result.Length); } else if (alpha.IsOne()) { x.Copy(result); } else { if (Control.ParallelizeOperation(x.Length)) { CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); } else { for (var index = 0; index < x.Length; index++) { result[index] = alpha * x[index]; } } } }
/// <summary> /// Scales an array. Can be used to scale a vector and a matrix. /// </summary> /// <param name="alpha">The scalar.</param> /// <param name="x">The values to scale.</param> /// <param name="result">This result of the scaling.</param> /// <remarks>This is similar to the SCAL BLAS routine.</remarks> public virtual void ScaleArray(Complex alpha, Complex[] x, Complex[] result) { if (x == null) { throw new ArgumentNullException("x"); } if (alpha.IsZero()) { CommonParallel.For(0, x.Length, index => result[index] = Complex.Zero); } else if (alpha.IsOne()) { CommonParallel.For(0, x.Length, index => result[index] = x[index]); } else { CommonParallel.For(0, x.Length, index => { result[index] = alpha * x[index]; }); } }
/// <summary> /// Multiplies two matrices and updates another with the result. <c>c = alpha*op(a)*op(b) + beta*c</c> /// </summary> /// <param name="transposeA">How to transpose the <paramref name="a"/> matrix.</param> /// <param name="transposeB">How to transpose the <paramref name="b"/> matrix.</param> /// <param name="alpha">The value to scale <paramref name="a"/> matrix.</param> /// <param name="a">The a matrix.</param> /// <param name="rowsA">The number of rows in the <paramref name="a"/> matrix.</param> /// <param name="columnsA">The number of columns in the <paramref name="a"/> matrix.</param> /// <param name="b">The b matrix</param> /// <param name="rowsB">The number of rows in the <paramref name="b"/> matrix.</param> /// <param name="columnsB">The number of columns in the <paramref name="b"/> matrix.</param> /// <param name="beta">The value to scale the <paramref name="c"/> matrix.</param> /// <param name="c">The c matrix.</param> public virtual void MatrixMultiplyWithUpdate(Transpose transposeA, Transpose transposeB, Complex alpha, Complex[] a, int rowsA, int columnsA, Complex[] b, int rowsB, int columnsB, Complex beta, Complex[] c) { // Choose nonsensical values for the number of rows in c; fill them in depending // on the operations on a and b. int rowsC; // First check some basic requirement on the parameters of the matrix multiplication. if (a == null) { throw new ArgumentNullException("a"); } if (b == null) { throw new ArgumentNullException("b"); } if ((int)transposeA > 111 && (int)transposeB > 111) { if (rowsA != columnsB) { throw new ArgumentOutOfRangeException(); } if (columnsA * rowsB != c.Length) { throw new ArgumentOutOfRangeException(); } rowsC = columnsA; } else if ((int)transposeA > 111) { if (rowsA != rowsB) { throw new ArgumentOutOfRangeException(); } if (columnsA * columnsB != c.Length) { throw new ArgumentOutOfRangeException(); } rowsC = columnsA; } else if ((int)transposeB > 111) { if (columnsA != columnsB) { throw new ArgumentOutOfRangeException(); } if (rowsA * rowsB != c.Length) { throw new ArgumentOutOfRangeException(); } rowsC = rowsA; } else { if (columnsA != rowsB) { throw new ArgumentOutOfRangeException(); } if (rowsA * columnsB != c.Length) { throw new ArgumentOutOfRangeException(); } rowsC = rowsA; } if (alpha.IsZero() && beta.IsZero()) { Array.Clear(c, 0, c.Length); return; } // Check whether we will be overwriting any of our inputs and make copies if necessary. // TODO - we can don't have to allocate a completely new matrix when x or y point to the same memory // as result, we can do it on a row wise basis. We should investigate this. Complex[] adata; if (ReferenceEquals(a, c)) { adata = (Complex[])a.Clone(); } else { adata = a; } Complex[] bdata; if (ReferenceEquals(b, c)) { bdata = (Complex[])b.Clone(); } else { bdata = b; } if (alpha.IsOne()) { if (beta.IsZero()) { if ((int)transposeA > 111 && (int)transposeB > 111) { CommonParallel.For( 0, columnsA, j => { var jIndex = j * rowsC; for (var i = 0; i != rowsB; i++) { var iIndex = i * rowsA; Complex s = 0; for (var l = 0; l != columnsB; l++) { s += adata[iIndex + l] * bdata[(l * rowsB) + j]; } c[jIndex + i] = s; } }); } else if ((int)transposeA > 111) { CommonParallel.For( 0, columnsB, j => { var jcIndex = j * rowsC; var jbIndex = j * rowsB; for (var i = 0; i != columnsA; i++) { var iIndex = i * rowsA; Complex s = 0; for (var l = 0; l != rowsA; l++) { s += adata[iIndex + l] * bdata[jbIndex + l]; } c[jcIndex + i] = s; } }); } else if ((int)transposeB > 111) { CommonParallel.For( 0, rowsB, j => { var jIndex = j * rowsC; for (var i = 0; i != rowsA; i++) { Complex s = 0; for (var l = 0; l != columnsA; l++) { s += adata[(l * rowsA) + i] * bdata[(l * rowsB) + j]; } c[jIndex + i] = s; } }); } else { CommonParallel.For( 0, columnsB, j => { var jcIndex = j * rowsC; var jbIndex = j * rowsB; for (var i = 0; i != rowsA; i++) { Complex s = 0; for (var l = 0; l != columnsA; l++) { s += adata[(l * rowsA) + i] * bdata[jbIndex + l]; } c[jcIndex + i] = s; } }); } } else { if ((int)transposeA > 111 && (int)transposeB > 111) { CommonParallel.For( 0, columnsA, j => { var jIndex = j * rowsC; for (var i = 0; i != rowsB; i++) { var iIndex = i * rowsA; Complex s = 0; for (var l = 0; l != columnsB; l++) { s += adata[iIndex + l] * bdata[(l * rowsB) + j]; } c[jIndex + i] = (c[jIndex + i] * beta) + s; } }); } else if ((int)transposeA > 111) { CommonParallel.For( 0, columnsB, j => { var jcIndex = j * rowsC; var jbIndex = j * rowsB; for (var i = 0; i != columnsA; i++) { var iIndex = i * rowsA; Complex s = 0; for (var l = 0; l != rowsA; l++) { s += adata[iIndex + l] * bdata[jbIndex + l]; } c[jcIndex + i] = s + (c[jcIndex + i] * beta); } }); } else if ((int)transposeB > 111) { CommonParallel.For( 0, rowsB, j => { var jIndex = j * rowsC; for (var i = 0; i != rowsA; i++) { Complex s = 0; for (var l = 0; l != columnsA; l++) { s += adata[(l * rowsA) + i] * bdata[(l * rowsB) + j]; } c[jIndex + i] = s + (c[jIndex + i] * beta); } }); } else { CommonParallel.For( 0, columnsB, j => { var jcIndex = j * rowsC; var jbIndex = j * rowsB; for (var i = 0; i != rowsA; i++) { Complex s = 0; for (var l = 0; l != columnsA; l++) { s += adata[(l * rowsA) + i] * bdata[jbIndex + l]; } c[jcIndex + i] = s + (c[jcIndex + i] * beta); } }); } } } else { if ((int)transposeA > 111 && (int)transposeB > 111) { CommonParallel.For( 0, columnsA, j => { var jIndex = j * rowsC; for (var i = 0; i != rowsB; i++) { var iIndex = i * rowsA; Complex s = 0; for (var l = 0; l != columnsB; l++) { s += adata[iIndex + l] * bdata[(l * rowsB) + j]; } c[jIndex + i] = (c[jIndex + i] * beta) + (alpha * s); } }); } else if ((int)transposeA > 111) { CommonParallel.For( 0, columnsB, j => { var jcIndex = j * rowsC; var jbIndex = j * rowsB; for (var i = 0; i != columnsA; i++) { var iIndex = i * rowsA; Complex s = 0; for (var l = 0; l != rowsA; l++) { s += adata[iIndex + l] * bdata[jbIndex + l]; } c[jcIndex + i] = (alpha * s) + (c[jcIndex + i] * beta); } }); } else if ((int)transposeB > 111) { CommonParallel.For( 0, rowsB, j => { var jIndex = j * rowsC; for (var i = 0; i != rowsA; i++) { Complex s = 0; for (var l = 0; l != columnsA; l++) { s += adata[(l * rowsA) + i] * bdata[(l * rowsB) + j]; } c[jIndex + i] = (alpha * s) + (c[jIndex + i] * beta); } }); } else { CommonParallel.For( 0, columnsB, j => { var jcIndex = j * rowsC; var jbIndex = j * rowsB; for (var i = 0; i != rowsA; i++) { Complex s = 0; for (var l = 0; l != columnsA; l++) { s += adata[(l * rowsA) + i] * bdata[jbIndex + l]; } c[jcIndex + i] = (alpha * s) + (c[jcIndex + i] * beta); } }); } } }