Example #1
0
        ///<summary>Add a <c>FloatVector</c> to another <c>FloatVector</c></summary>
        ///<param name="lhs"><c>FloatVector</c> to add to.</param>
        ///<param name="rhs"><c>FloatVector</c> to add.</param>
        ///<returns><c>FloatVector</c> with results.</returns>
        public static FloatVector operator +(FloatVector lhs, FloatVector rhs)
        {
            FloatVector ret = new FloatVector(lhs);

            Blas.Axpy.Compute(ret.Length, 1, rhs.data, 1, ret.data, 1);
            return(ret);
        }
Example #2
0
        ///<summary>Multiply a <c>float</c> x with a <c>FloatVector</c> y as x*y</summary>
        ///<param name="lhs"><c>float</c> as left hand operand.</param>
        ///<param name="rhs"><c>FloatVector</c> as right hand operand.</param>
        ///<returns><c>FloatVector</c> with results.</returns>
        public static FloatVector operator *(float lhs, FloatVector rhs)
        {
            FloatVector ret = new FloatVector(rhs);

            Blas.Scal.Compute(ret.Length, lhs, ret.data, 1);
            return(ret);
        }
Example #3
0
        ///<summary>Divide a <c>FloatVector</c> x with a <c>float</c> y as x/y</summary>
        ///<param name="lhs"><c>FloatVector</c> as left hand operand.</param>
        ///<param name="rhs"><c>float</c> as right hand operand.</param>
        ///<returns><c>FloatVector</c> with results.</returns>
        public static FloatVector operator /(FloatVector lhs, float rhs)
        {
            FloatVector ret = new FloatVector(lhs);

            Blas.Scal.Compute(ret.Length, 1 / rhs, ret.data, 1);
            return(ret);
        }
Example #4
0
    /// <summary>Performs the QR factorization.</summary>
    protected override void InternalCompute() 
    {
      int m = matrix.RowLength;
      int n = matrix.ColumnLength;

#if MANAGED
      int minmn = m < n ? m : n;
      r_ = new FloatMatrix(matrix); // create a copy
      FloatVector[] u = new FloatVector[minmn];
      for (int i = 0; i < minmn; i++) 
      {
        u[i] = Householder.GenerateColumn(r_, i, m-1, i);
        Householder.UA(u[i], r_, i, m-1, i + 1, n-1);
      }
      q_ = FloatMatrix.CreateIdentity(m);
      for (int i = minmn - 1; i >= 0; i--) 
      {
        Householder.UA(u[i], q_, i, m - 1, i, m - 1);
      }
#else
      qr = new float[matrix.data.Length];
      Array.Copy(matrix.data, qr, matrix.data.Length);
      jpvt = new int[n];
      jpvt[0] = 1;
      Lapack.Geqp3.Compute(m, n, qr, m, jpvt, out tau);
      r_ = new FloatMatrix(m, n);
      // Populate R
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++) {
          if (i <= j) {
            r_.data[j * m + i] = qr[(jpvt[j]-1) * m + i];
          }
          else {
            r_.data[j * m + i] = 0.0f;
          }
        }
      }
      q_ = new FloatMatrix(m, m);
      for (int i = 0; i < m; i++) {
        for (int j = 0; j < m; j++) {
          if (j < n)
            q_.data[j * m + i] = qr[j * m + i];
          else
            q_.data[j * m + i] = 0.0f;
        }
      }

      if( m < n ){
        Lapack.Orgqr.Compute(m, m, m, q_.data, m, tau);
      } else{
        Lapack.Orgqr.Compute(m, m, n, q_.data, m, tau);
      }
#endif
      for (int i = 0; i < m; i++) 
      {
        if (q_[i, i] == 0)
          isFullRank = false;
      }
    }
Example #5
0
 ///<summary>Subtract a <c>FloatVector</c> from this<c>FloatVector</c></summary>
 ///<param name="vector"><c>FloatVector</c> to add.</param>
 ///<exception cref="ArgumentNullException">Exception thrown if null given as argument.</exception>
 public void Subtract(FloatVector vector)
 {
     if (vector == null)
     {
         throw new System.ArgumentNullException("FloatVector cannot be null.");
     }
     Blas.Axpy.Compute(this.Length, -1, vector.data, 1, this.data, 1);
 }
Example #6
0
 private static void dscalVector(FloatVector A, int Start, float z)
 {
     // A part of vector  A from Start to end multiply by z
     for (int i = Start; i < A.Length; i++)
     {
         A[i] = A[i] * z;
     }
 }
Example #7
0
 ///<summary>Negate operator for <c>FloatVector</c></summary>
 ///<returns><c>FloatVector</c> with values to negate.</returns>
 public static FloatVector Negate(FloatVector rhs)
 {
     if (rhs == null)
     {
         throw new ArgumentNullException("rhs", "rhs cannot be null");
     }
     return(-rhs);
 }
 public void CtorDimensions()
 {
   FloatVector test = new FloatVector(2);
   
   Assert.AreEqual(test.Length, 2);
   Assert.AreEqual(test[0],0);
   Assert.AreEqual(test[1],0);
 }
 public void CtorInitialValues()
 {
   FloatVector test = new FloatVector(2,1);
   
   Assert.AreEqual(test.Length, 2);
   Assert.AreEqual(test[0],1);
   Assert.AreEqual(test[1],1);
 }
Example #10
0
 ///<summary>Compute the dot product of this <c>FloatVector</c> x with another <c>FloatVector</c> y and return as <c>float</c></summary>
 ///<param name="alpha">value added to the inner product.</param>
 ///<param name="Y"><c>FloatVector</c> to dot product with this <c>FloatVector</c>.</param>
 ///<returns><c>float</c> results from x dot y.</returns>
 public double GetSDotProduct(float alpha, FloatVector Y)
 {
     if (Y == null)
     {
         throw new System.ArgumentNullException("FloatVector cannot be null.");
     }
     return(Blas.Sdot.Compute(this.Length, alpha, this.data, 1, Y.data, 1));
 }
Example #11
0
 ///<summary>Constructor for <c>FloatVector</c> to deep copy another <c>FloatVector</c></summary>
 ///<param name="src"><c>FloatVector</c> to deep copy into <c>FloatVector</c>.</param>
 ///<exception cref="ArgumentNullException">Exception thrown if null passed as 'src' parameter.</exception>
 public FloatVector(FloatVector src)
 {
     if (src == null)
     {
         throw new ArgumentNullException("FloatVector cannot be null");
     }
     data = new float[src.data.Length];
     Array.Copy(src.data, 0, data, 0, data.Length);
 }
 public void CurrentException2() 
 {
   FloatVector test = new FloatVector(new float[2]{1f,2f});
   IEnumerator enumerator = test.GetEnumerator();
   enumerator.MoveNext();
   enumerator.MoveNext();
   enumerator.MoveNext();
   object value=enumerator.Current;
 }
 public void CtorArray()
 {
   float[] testvector = new float[2]{0,1};
   
   FloatVector test = new FloatVector(testvector);
   Assert.AreEqual(test.Length,testvector.Length);
   Assert.AreEqual(test[0],testvector[0]);
   Assert.AreEqual(test[1],testvector[1]);
 }
Example #14
0
        ///<summary>Swap data in this <c>FloatVector</c> with another <c>FloatVector</c></summary>
        ///<param name="src"><c>FloatVector</c> to swap data with.</param>
        public void Swap(FloatVector src)
        {
            if (src == null)
            {
                throw new System.ArgumentNullException("FloatVector cannot be null.");
            }

            Blas.Swap.Compute(src.Length, src.data, 1, this.data, 1);
        }
Example #15
0
        ///<summary>Compute the sum y = alpha * x + y where y is this <c>FloatVector</c></summary>
        ///<param name="alpha"><c>float</c> value to scale this <c>FloatVector</c></param>
        ///<param name="X"><c>FloatVector</c> to add to alpha * this <c>FloatVector</c></param>
        ///<remarks>Results of computation replace data in this variable</remarks>
        public void Axpy(float alpha, FloatVector X)
        {
            if (X == null)
            {
                throw new System.ArgumentNullException("FloatVector cannot be null.");
            }

            Blas.Axpy.Compute(this.data.Length, alpha, X.data, 1, this.data, 1);
        }
Example #16
0
        ///<summary>Sum the components in this <c>FloatVector</c></summary>
        ///<returns><c>float</c> results from the summary of <c>FloatVector</c> components.</returns>
        public float GetSum()
        {
            float ret = 0;

            for (int i = 0; i < data.Length; ++i)
            {
                ret += data[i];
            }
            return(ret);
        }
Example #17
0
        private FloatVector Pivot(IROFloatVector B)
        {
            FloatVector ret = new FloatVector(B.Length);

            for (int i = 0; i < pivots.Length; i++)
            {
                ret.data[i] = B[pivots[i]];
            }
            return(ret);
        }
Example #18
0
        /// <summary>
        /// Returns the row of a <see cref="IROFloatMatrix" /> as a new <c>FloatVector.</c>
        /// </summary>
        /// <param name="mat">The matrix to copy the column from.</param>
        /// <param name="row">Index of the row to copy from the matrix.</param>
        /// <returns>A new <c>DoubleVector</c> with the same elements as the row of the given matrix.</returns>
        public static FloatVector GetRow(IROFloatMatrix mat, int row)
        {
            FloatVector result = new FloatVector(mat.Columns);

            for (int i = 0; i < result.data.Length; ++i)
            {
                result.data[i] = mat[row, i];
            }

            return(result);
        }
Example #19
0
        private FloatVector Pivot(IReadOnlyList <float> B)
        {
            var ret      = new FloatVector(B.Count);
            var retArray = ret.GetInternalData();

            for (int i = 0; i < pivots.Length; i++)
            {
                retArray[i] = B[pivots[i]];
            }
            return(ret);
        }
Example #20
0
        ///<summary>Implicit cast conversion to <c>ComplexFloatVector</c> from <c>FloatVector</c></summary>
        static public ComplexFloatVector ToComplexFloatVector(FloatVector src)
        {
            float[]            temp = src.ToArray();
            ComplexFloatVector ret  = new ComplexFloatVector(temp.Length);

            for (int i = 0; i < temp.Length; ++i)
            {
                ret.data[i] = temp[i];
            }
            return(ret);
        }
Example #21
0
        ///<summary>Implicit cast conversion to <c>DoubleVector</c> from <c>FloatVector</c></summary>
        public static DoubleVector ToDoubleVector(FloatVector src)
        {
            if (src == null)
            {
                return(null);
            }
            var ret = new DoubleVector(src.Length);

            Array.Copy(src.GetInternalData(), ret._array, src.Length);
            return(ret);
        }
Example #22
0
        ///<summary>Implicit cast conversion to <c>DoubleVector</c> from <c>FloatVector</c></summary>
        static public DoubleVector ToDoubleVector(FloatVector src)
        {
            if (src == null)
            {
                return(null);
            }
            DoubleVector ret = new DoubleVector(src.Length);

            Array.Copy(src.data, ret.data, src.Length);
            return(ret);
        }
Example #23
0
        /// <summary>
        /// Returns the column of a <see cref="IROFloatMatrix" /> as a new <c>FloatVector.</c>
        /// </summary>
        /// <param name="mat">The matrix to copy the column from.</param>
        /// <param name="col">Index of the column to copy from the matrix.</param>
        /// <returns>A new <c>FloatVector</c> with the same elements as the column of the given matrix.</returns>
        public static FloatVector GetColumn(IROFloatMatrix mat, int col)
        {
            FloatVector result = new FloatVector(mat.Rows);

            for (int i = 0; i < result.data.Length; ++i)
            {
                result.data[i] = mat[i, col];
            }

            return(result);
        }
Example #24
0
        ///<summary>Solves a system on linear equations, AX=B, where A is the factored matrixed.</summary>
        ///<param name="B">RHS side of the system.</param>
        ///<returns>the solution vector, X.</returns>
        ///<exception cref="ArgumentNullException">B is null.</exception>
        ///<exception cref="NotPositiveDefiniteException">A is not positive definite.</exception>
        ///<exception cref="ArgumentException">The number of rows of A and the length of B must be the same.</exception>
        public FloatVector Solve(IReadOnlyList <float> B)
        {
            if (B == null)
            {
                throw new System.ArgumentNullException("B cannot be null.");
            }
            Compute();
            if (!ispd)
            {
                throw new NotPositiveDefiniteException();
            }
            else
            {
                if (B.Count != order)
                {
                    throw new System.ArgumentException("The length of B must be the same as the order of the matrix.");
                }
#if MANAGED
                // Copy right hand side.
                var X      = new FloatVector(B);
                var xarray = X.GetInternalData();
                // Solve L*Y = B;
                for (int i = 0; i < order; i++)
                {
                    float sum = B[i];
                    for (int k = i - 1; k >= 0; k--)
                    {
                        sum -= l.data[i][k] * xarray[k];
                    }
                    xarray[i] = sum / l.data[i][i];
                }
                // Solve L'*X = Y;
                for (int i = order - 1; i >= 0; i--)
                {
                    float sum = xarray[i];
                    for (int k = i + 1; k < order; k++)
                    {
                        sum -= l.data[k][i] * xarray[k];
                    }
                    xarray[i] = sum / l.data[i][i];
                }

                return(X);
#else
                float[] rhs = FloatMatrix.ToLinearArray(B);
                Lapack.Potrs.Compute(Lapack.UpLo.Lower, order, 1, l.data, order, rhs, B.Length);
                FloatVector ret = new FloatVector(order, B.Length);
                ret.data = rhs;
                return(ret);
#endif
            }
        }
Example #25
0
        private static float dnrm2Vector(FloatVector A, int Start)
        {
            //  dznrm2Vector returns the euclidean norm of a vector,
            // which is a part of A, beginning from Start to end of vector
            // so that dznrm2Vector := sqrt( conjg( matrix' )*matrix )
            float s = 0;

            for (int i = Start; i < A.Length; i++)
            {
                s += (A[i] * A[i]);
            }
            return((float)System.Math.Sqrt(s));
        }
Example #26
0
        private static float dnrm2Column(FloatMatrix A, int Col, int Start)
        {
            //  dznrm2Column returns the euclidean norm of a vector,
            // which is a part of column Col in matrix A, beginning from Start to end of column
            // so that dznrm2Column := sqrt( conjg( matrix' )*matrix )
            float s = 0;

            for (int i = Start; i < A.RowLength; i++)
            {
                s += (A[i, Col] * A[i, Col]);
            }
            return((float)System.Math.Sqrt(s));
        }
Example #27
0
        ///<summary>Compute the Infinity Norm of this <c>FloatVector</c></summary>
        ///<returns><c>float</c> results from norm.</returns>
        public float GetInfinityNorm()
        {
            float ret = 0;

            for (int i = 0; i < data.Length; i++)
            {
                float tmp = System.Math.Abs(data[i]);
                if (tmp > ret)
                {
                    ret = tmp;
                }
            }
            return(ret);
        }
Example #28
0
        ///<summary>Solves a system on linear equations, AX=B, where A is the factored matrixed.</summary>
        ///<param name="B">RHS side of the system.</param>
        ///<returns>the solution vector, X.</returns>
        ///<exception cref="ArgumentNullException">B is null.</exception>
        ///<exception cref="SingularMatrixException">A is singular.</exception>
        ///<exception cref="ArgumentException">The number of rows of A and the length of B must be the same.</exception>
        public FloatVector Solve(IROFloatVector B)
        {
            if (B == null)
            {
                throw new System.ArgumentNullException("B cannot be null.");
            }
            Compute();
            if (singular)
            {
                throw new SingularMatrixException();
            }
            else
            {
                if (B.Length != order)
                {
                    throw new System.ArgumentException("The length of B must be the same as the order of the matrix.");
                }
#if MANAGED
                // Copy right hand side with pivoting
                FloatVector X = Pivot(B);

                // Solve L*Y = B(piv,:)
                for (int k = 0; k < order; k++)
                {
                    for (int i = k + 1; i < order; i++)
                    {
                        X[i] -= X[k] * factor[i][k];
                    }
                }
                // Solve U*X = Y;
                for (int k = order - 1; k >= 0; k--)
                {
                    X[k] /= factor[k][k];
                    for (int i = 0; i < k; i++)
                    {
                        X[i] -= X[k] * factor[i][k];
                    }
                }
                return(X);
#else
                float[] rhs = FloatMatrix.ToLinearArray(B);
                Lapack.Getrs.Compute(Lapack.Transpose.NoTrans, order, 1, factor, order, pivots, rhs, rhs.Length);
                return(new FloatVector(rhs));
#endif
            }
        }
 public void Current()
 {
   FloatVector test = new FloatVector(new float[2]{1f,2f});
   IEnumerator enumerator = test.GetEnumerator();
   bool movenextresult;
   
   movenextresult=enumerator.MoveNext();
   Assert.IsTrue(movenextresult);
   Assert.AreEqual(enumerator.Current,test[0]);
   
   movenextresult=enumerator.MoveNext();
   Assert.IsTrue(movenextresult);
   Assert.AreEqual(enumerator.Current,test[1]);
   
   movenextresult=enumerator.MoveNext();
   Assert.IsFalse(movenextresult);
 }
Example #30
0
        private static void drotg(ref float da, ref float db, ref float c, ref float s)
        {
            //     construct givens plane rotation.
            //     jack dongarra, linpack, 3/11/78.
            float roe, scale, r, z, Absda, Absdb, sda, sdb;

            roe   = db;
            Absda = System.Math.Abs(da);
            Absdb = System.Math.Abs(db);
            if (Absda > Absdb)
            {
                roe = da;
            }
            scale = Absda + Absdb;
            if (scale == 0.0f)
            {
                c = 1.0f;
                s = 0.0f;
                r = 0.0f;
                z = 0.0f;
            }
            else
            {
                sda = da / scale;
                sdb = db / scale;
                r   = scale * (float)System.Math.Sqrt(sda * sda + sdb * sdb);
                if (roe < 0.0f)
                {
                    r = -r;
                }
                c = da / r;
                s = db / r;
                z = 1.0f;
                if (Absda > Absdb)
                {
                    z = s;
                }
                if (Absdb >= Absda && c != 0.0)
                {
                    z = 1.0f / c;
                }
            }
            da = r;
            db = z;
            return;
        }
Example #31
0
        ///<summary>Compute the P Norm of this <c>FloatVector</c></summary>
        ///<returns><c>float</c> results from norm.</returns>
        ///<remarks>p &gt; 0, if p &lt; 0, ABS(p) is used. If p = 0, the infinity norm is returned.</remarks>
        public float GetNorm(double p)
        {
            if (p == 0)
            {
                return(GetInfinityNorm());
            }
            if (p < 0)
            {
                p = -p;
            }
            double ret = 0;

            for (int i = 0; i < data.Length; i++)
            {
                ret += System.Math.Pow(System.Math.Abs(data[i]), p);
            }
            return((float)System.Math.Pow(ret, 1 / p));
        }
Example #32
0
        public static FloatVector GenerateColumn(IFloatMatrix A, int r1, int r2, int c)
        {
            int         ru = r2 - r1 + 1;
            FloatVector u  = new FloatVector(r2 - r1 + 1);

            for (int i = r1; i <= r2; i++)
            {
                u[i - r1] = A[i, c];
                A[i, c]   = 0.0f;
            }

            float norm = u.GetNorm();

            if (r1 == r2 || norm == 0)
            {
                A[r1, c] = -u[0];
                u[0]     = (float)System.Math.Sqrt(2);
                return(u);
            }

            float scale = 1.0f / norm;

            if (u[0] < 0.0f)
            {
                scale *= -1.0f;
            }

            A[r1, c] = -1.0f / scale;

            for (int i = 0; i < ru; i++)
            {
                u[i] = u[i] * scale;
            }

            u[0] = u[0] + 1.0f;
            float s = (float)System.Math.Sqrt(1 / u[0]);

            for (int i = 0; i < ru; i++)
            {
                u[i] = s * u[i];
            }
            return(u);
        }
Example #33
0
        public static FloatVector GenerateRow(IFloatMatrix A, int r, int c1, int c2)
        {
            int         cu = c2 - c1 + 1;
            FloatVector u  = new FloatVector(cu);

            for (int j = c1; j <= c2; j++)
            {
                u[j - c1] = A[r, j];
                A[r, j]   = 0.0f;
            }

            float norm = u.GetNorm();

            if (c1 == c2 || norm == 0)
            {
                A[r, c1] = -u[0];
                u[0]     = (float)System.Math.Sqrt(2);
                return(u);
            }

            float scale = 1.0f / norm;

            if (u[0] < 0.0f)
            {
                scale *= -1.0f;
            }

            A[r, c1] = -1.0f / scale;

            for (int j = 0; j < cu; j++)
            {
                u[j] *= scale;
            }

            u[0] += 1.0f;
            float s = (float)System.Math.Sqrt(1 / u[0]);

            for (int j = 0; j < cu; j++)
            {
                u[j] *= s;
            }
            return(u);
        }
Example #34
0
        ///<summary>Returns a subvector of the <c>FloatVector</c></summary>
        ///<param name="startElement">Return data starting from this element.</param>
        ///<param name="endElement">Return data ending in this element.</param>
        ///<returns><c>FloatVector</c> a subvector of the reference vector</returns>
        ///<exception cref="ArgumentException">Exception thrown if <paramref>endElement</paramref> is greater than <paramref>startElement</paramref></exception>
        ///<exception cref="ArgumentOutOfRangeException">Exception thrown if input dimensions are out of the range of <c>FloatVector</c> dimensions</exception>
        public FloatVector GetSubVector(int startElement, int endElement)
        {
            if (startElement > endElement)
            {
                throw new ArgumentException("The starting element must be less that the ending element.");
            }

            if (startElement < 0 || endElement < 0 || startElement >= this.Length || endElement >= this.Length)
            {
                throw new ArgumentException("startElement and startElement must be greater than or equal to zero, endElement must be less than Length, and endElement must be less than Length.");
            }

            int         n   = endElement - startElement + 1;
            FloatVector ret = new FloatVector(n);

            for (int i = 0; i < n; i++)
            {
                ret[i] = this[i + startElement];
            }
            return(ret);
        }
Example #35
0
        ///<summary>Check if <c>FloatVector</c> variable is the same as another object</summary>
        ///<param name="obj"><c>obj</c> to compare present <c>FloatVector</c> to.</param>
        ///<returns>Returns true if the variable is the same as the <c>FloatVector</c> variable</returns>
        ///<remarks>The <c>obj</c> parameter is converted into a <c>FloatVector</c> variable before comparing with the current <c>DoubleVector</c>.</remarks>
        public override bool Equals(Object obj)
        {
            FloatVector vector = obj as FloatVector;

            if (vector == null)
            {
                return(false);
            }

            if (this.data.Length != vector.data.Length)
            {
                return(false);
            }

            for (int i = 0; i < this.data.Length; ++i)
            {
                if (this.data[i] != vector.data[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Example #36
0
		///<summary>Implicit cast conversion to <c>DoubleVector</c> from <c>FloatVector</c></summary>
		static public DoubleVector ToDoubleVector(FloatVector src)
		{
			if (src == null)
			{
				return null;
			}
			DoubleVector ret = new DoubleVector(src.Length);
			Array.Copy(src.data, ret.data, src.Length);
			return ret;
		}
Example #37
0
 ///<summary> Constructor </summary>
 public FloatVectorEnumerator(FloatVector vector)
 {
     v      = vector;
     index  = -1;
     length = v.Length;
 }
Example #38
0
 ///<summary>Multiply a <c>FloatVector</c> x with a <c>float</c> y as x*y</summary>
 ///<param name="lhs"><c>FloatVector</c> as left hand operand.</param>
 ///<param name="rhs"><c>float</c> as right hand operand.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector Multiply(FloatVector lhs, float rhs)
 {
   return lhs * rhs;
 }
Example #39
0
 ///<summary>Multiply a <c>FloatVector</c> with another <c>FloatVector</c> as x*y^T</summary>
 ///<param name="lhs"><c>FloatVector</c> as left hand operand.</param>
 ///<param name="rhs"><c>FloatVector</c> as right hand operand.</param>
 ///<returns><c>FloatMatrix</c> with results.</returns>
 public static FloatMatrix Multiply(FloatVector lhs, FloatVector rhs)
 {
   return lhs * rhs;
 }
Example #40
0
 ///<summary>Divide a <c>FloatVector</c> x with a <c>float</c> y as x/y</summary>
 ///<param name="lhs"><c>FloatVector</c> as left hand operand.</param>
 ///<param name="rhs"><c>float</c> as right hand operand.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector Divide(FloatVector lhs, float rhs)
 {
   return lhs / rhs;
 }
    /// <summary>
    /// Constructor with <c>float</c> array parameter.
    /// </summary>
    /// <param name="T">The left-most column of the Toeplitz matrix.</param>
    /// <exception cref="ArgumentNullException">
    /// <B>T</B> is a null reference.
    /// </exception>
    /// <exception cref="RankException">
    /// The length of <B>T</B> is zero.
    /// </exception>
    public FloatSymmetricLevinson(params float[] T)
    {
      // check parameter
      if (T == null)
      {
        throw new System.ArgumentNullException("T");
      }
      else if (T.Length == 0)
      {
        throw new RankException("The length of T is zero.");
      }

      // save the vector
      m_LeftColumn = new FloatVector(T);
      m_Order = m_LeftColumn.Length;

      // allocate memory for lower triangular matrix
      m_LowerTriangle = new float[m_Order][];
      for (int i = 0; i < m_Order; i++)
      {
        m_LowerTriangle[i] = new float[i + 1];
      }

      // allocate memory for diagonal
      m_Diagonal = new float[m_Order];
    }
    /// <summary>
    /// Solve a symmetric square Toeplitz system with a right-side matrix.
    /// </summary>
    /// <param name="T">The left-most column of the Toeplitz matrix.</param>
    /// <param name="Y">The right-side matrix of the system.</param>
    /// <returns>The solution matrix.</returns>
    /// <exception cref="ArgumentNullException">
    /// <B>T</B> and/or <B>Y</B> are null references
    /// </exception>
    /// <exception cref="RankException">
    /// The length of <B>T</B> does not match the number of rows in <B>Y</B>.
    /// </exception>
    /// <exception cref="SingularMatrixException">
    /// The Toeplitz matrix or one of the the leading sub-matrices is singular.
    /// </exception>
    /// <remarks>
    /// This method solves the linear system <B>AX</B> = <B>Y</B>. Where
    /// <B>T</B> is a symmetric square Toeplitz matrix, <B>X</B> is an unknown
    /// matrix and <B>Y</B> is a known matrix.
    /// <para>
    /// This static member combines the <b>UDL</b> decomposition and the calculation of the solution into a
    /// single algorithm. When compared to the non-static member it requires minimal data storage
    /// and suffers from no speed penalty.
    /// </para>
    /// </remarks>
    public static FloatMatrix Solve(IROFloatVector T, IROFloatMatrix Y)
    {

      FloatMatrix X;

      // check parameters
      if (T == null)
      {
        throw new System.ArgumentNullException("T");
      }
      else if (Y == null)
      {
        throw new System.ArgumentNullException("Y");
      }
      else if (T.Length != Y.Columns)
      {
        throw new RankException("The length of T and Y are not equal.");
      }
      else
      {

        // allocate memory
        int N = T.Length;
        int M = Y.Rows;
        X = new FloatMatrix(N, M);                 // solution matrix
        FloatVector Z = new FloatVector(N);       // temporary storage vector
        float e;                                   // prediction error
        int i, j, l, m;

        // setup zero order solution
        e = T[0];
        if (e == 0.0f)
        {
          throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
        }
        for (m = 0; m < M; m++)
        {
          X[0, m] = Y[0, m] / T[0];
        }

        if (N > 1)
        {

          FloatVector a = new FloatVector(N - 1);   // prediction coefficients
          float p;                                   // reflection coefficient
          float inner;                               // inner product
          float k;

          // calculate solution for successive orders
          for (i = 1; i < N; i++)
          {

            // calculate first inner product
            inner = T[i];
            for (j = 0, l = i - 1; j < i - 1; j++, l--)
            {
              inner += a[j] * T[l];
            }

            // update predictor coefficients
            p = -(inner / e);
            for (j = 0, l = i - 2; j < i - 1; j++, l--)
            {
              Z[j] = a[j] + p * a[l];
            }

            // copy vector
            for (j = 0; j < i - 1; j++)
            {
              a[j] = Z[j];
            }

            a[i - 1] = p;
            e *= (1.0f - p * p);

            if (e == 0.0f)
            {
              throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
            }

            // update the solution matrix
            for (m = 0; m < M; m++)
            {

              // retrieve a copy of solution column
              for (j = 0; j < i; j++)
              {
                Z[j] = X[j, m];
              }

              // calculate second inner product
              inner = Y[i, m];
              for (j = 0, l = i; j < i; j++, l--)
              {
                inner -= Z[j] * T[l];
              }

              // update solution vector
              k = inner / e;
              for (j = 0, l = i - 1; j < i; j++, l--)
              {
                Z[j] = Z[j] + k * a[l];
              }
              Z[j] = k;

              // store solution column in matrix
              for (j = 0; j <= i; j++)
              {
                X[j, m] = Z[j];
              }

            }

          }

        }

      }

      return X;
    }
Example #43
0
    /// <summary>
    /// Returns the row of a <see cref="IROFloatMatrix" /> as a new <c>FloatVector.</c>
    /// </summary>
    /// <param name="mat">The matrix to copy the column from.</param>
    /// <param name="row">Index of the row to copy from the matrix.</param>
    /// <returns>A new <c>DoubleVector</c> with the same elements as the row of the given matrix.</returns>
    public static FloatVector GetRow(IROFloatMatrix mat, int row)
    {
      FloatVector result = new FloatVector(mat.Columns);
      for (int i = 0; i < result.data.Length; ++i)
        result.data[i] = mat[row, i];

      return result;
    }
Example #44
0
 ///<summary>Multiply a <c>float</c> x with a <c>FloatVector</c> y as x*y</summary>
 ///<param name="lhs"><c>float</c> as left hand operand.</param>
 ///<param name="rhs"><c>FloatVector</c> as right hand operand.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector Multiply(float lhs, FloatVector rhs)
 {
   return lhs * rhs;
 }
Example #45
0
 ///<summary>Negate operator for <c>FloatVector</c></summary>
 ///<returns><c>FloatVector</c> with values to negate.</returns>
 public static FloatVector Negate(FloatVector rhs)
 {
   if (rhs == null)
   {
     throw new ArgumentNullException("rhs", "rhs cannot be null");
   }
   return -rhs;
 }
Example #46
0
 ///<summary>Subtract a <c>FloatVector</c> from another <c>FloatVector</c></summary>
 ///<param name="lhs"><c>FloatVector</c> to subtract from.</param>
 ///<param name="rhs"><c>FloatVector</c> to subtract.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector Subtract(FloatVector lhs, FloatVector rhs)
 {
   return lhs - rhs;
 }
Example #47
0
 ///<summary>Add a <c>FloatVector</c> to another <c>FloatVector</c></summary>
 ///<param name="lhs"><c>FloatVector</c> to add to.</param>
 ///<param name="rhs"><c>FloatVector</c> to add.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector operator +(FloatVector lhs, FloatVector rhs)
 {
   FloatVector ret = new FloatVector(lhs);
   Blas.Axpy.Compute(ret.Length, 1, rhs.data, 1, ret.data, 1);
   return ret;
 }
Example #48
0
 ///<summary>Subtract a <c>FloatVector</c> from this<c>FloatVector</c></summary>
 ///<param name="vector"><c>FloatVector</c> to add.</param>
 ///<exception cref="ArgumentNullException">Exception thrown if null given as argument.</exception>
 public void Subtract(FloatVector vector)
 {
   if (vector == null)
   {
     throw new System.ArgumentNullException("FloatVector cannot be null.");
   }
   Blas.Axpy.Compute(this.Length, -1, vector.data, 1, this.data, 1);
 }
Example #49
0
 ///<summary>Add a <c>FloatVector</c> to another <c>FloatVector</c></summary>
 ///<param name="lhs"><c>FloatVector</c> to add to.</param>
 ///<param name="rhs"><c>FloatVector</c> to add.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector Add(FloatVector lhs, FloatVector rhs)
 {
   return lhs + rhs;
 }
    public void Equals()
    {
      ComplexDoubleVector a = new ComplexDoubleVector(2,4);
      ComplexDoubleVector b = new ComplexDoubleVector(2,4);
      ComplexDoubleVector c = new ComplexDoubleVector(2);
      c[0] = 4;
      c[1] = 4;

      ComplexDoubleVector d = new ComplexDoubleVector(2,5);
      ComplexDoubleVector e = null;
      FloatVector f = new FloatVector(2,4);
      Assert.IsTrue(a.Equals(b));
      Assert.IsTrue(b.Equals(a));
      Assert.IsTrue(a.Equals(c));
      Assert.IsTrue(b.Equals(c));
      Assert.IsTrue(c.Equals(b));
      Assert.IsTrue(c.Equals(a));
      Assert.IsFalse(a.Equals(d));
      Assert.IsFalse(d.Equals(b));
      Assert.IsFalse(a.Equals(e));
      Assert.IsFalse(a.Equals(f));
    }
    /// <overloads>
    /// Solve a symmetric square Toeplitz system.
    /// </overloads>
    /// <summary>
    /// Solve a symmetric square Toeplitz system with a right-side vector.
    /// </summary>
    /// <param name="T">The left-most column of the Toeplitz matrix.</param>
    /// <param name="Y">The right-side vector of the system.</param>
    /// <returns>The solution vector.</returns>
    /// <exception cref="ArgumentNullException">
    /// <B>T</B> and/or <B>Y</B> are null references
    /// </exception>
    /// <exception cref="RankException">
    /// The length of <B>T</B> does not match the length of <B>Y</B>.
    /// </exception>
    /// <exception cref="SingularMatrixException">
    /// The Toeplitz matrix or one of the the leading sub-matrices is singular.
    /// </exception>
    /// <remarks>
    /// This method solves the linear system <B>AX</B> = <B>Y</B>. Where
    /// <B>T</B> is a symmetric square Toeplitz matrix, <B>X</B> is an unknown
    /// vector and <B>Y</B> is a known vector.
    /// <para>
    /// This static member combines the <b>UDL</b> decomposition and the calculation of the solution into a
    /// single algorithm. When compared to the non-static member it requires minimal data storage
    /// and suffers from no speed penalty.
    /// </para>
    /// </remarks>
    public static FloatVector Solve(IROFloatVector T, IROFloatVector Y)
    {

      FloatVector X;

      // check parameters
      if (T == null)
      {
        throw new System.ArgumentNullException("T");
      }
      else if (Y == null)
      {
        throw new System.ArgumentNullException("Y");
      }
      else if (T.Length != Y.Length)
      {
        throw new RankException("The length of T and Y are not equal.");
      }
      else
      {

        // allocate memory
        int N = T.Length;
        X = new FloatVector(N);                    // solution vector
        float e;                                   // prediction error

        // setup zero order solution
        e = T[0];
        if (e == 0.0f)
        {
          throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
        }
        X[0] = Y[0] / T[0];

        if (N > 1)
        {
          FloatVector a = new FloatVector(N - 1);   // prediction coefficients
          FloatVector Z = new FloatVector(N - 1);   // temporary storage vector
          float g;                                   // reflection coefficient
          float inner;                               // inner product
          float k;
          int i, j, l;

          // calculate solution for successive orders
          for (i = 1; i < N; i++)
          {

            // calculate first inner product
            inner = T[i];
            for (j = 0, l = i - 1; j < i - 1; j++, l--)
            {
              inner += a[j] * T[l];
            }

            // update predictor coefficients
            g = -(inner / e);
            for (j = 0, l = i - 2; j < i - 1; j++, l--)
            {
              Z[j] = a[j] + g * a[l];
            }

            // copy vector
            for (j = 0; j < i - 1; j++)
            {
              a[j] = Z[j];
            }

            a[i - 1] = g;

            e *= (1.0f - g * g);
            if (e == 0.0f)
            {
              throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
            }

            // calculate second inner product
            inner = Y[i];
            for (j = 0, l = i; j < i; j++, l--)
            {
              inner -= X[j] * T[l];
            }

            // update solution vector
            k = inner / e;
            for (j = 0, l = i - 1; j < i; j++, l--)
            {
              X[j] = X[j] + k * a[l];
            }
            X[j] = k;

          }

        }

      }

      return X;

    }
Example #52
0
 ///<summary>Divide a <c>FloatVector</c> x with a <c>float</c> y as x/y</summary>
 ///<param name="lhs"><c>FloatVector</c> as left hand operand.</param>
 ///<param name="rhs"><c>float</c> as right hand operand.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector operator /(FloatVector lhs, float rhs)
 {
   FloatVector ret = new FloatVector(lhs);
   Blas.Scal.Compute(ret.Length, 1 / rhs, ret.data, 1);
   return ret;
 }
    /// <summary>
    /// Solve the Yule-Walker equations for a symmetric square Toeplitz system
    /// </summary>
    /// <param name="R">The left-most column of the Toeplitz matrix.</param>
    /// <returns>The solution vector.</returns>
    /// <exception cref="ArgumentNullException">
    /// <B>R</B> is a null reference.
    /// </exception>
    /// <exception cref="ArgumentOutOfRangeException">
    /// The length of <B>R</B> must be greater than one.
    /// </exception>
    /// <exception cref="SingularMatrixException">
    /// The Toeplitz matrix or one of the the leading sub-matrices is singular.
    /// </exception>
    /// <remarks>
    /// This member is used to solve the Yule-Walker system <B>AX</B> = -<B>a</B>,
    /// where <B>A</B> is a symmetric square Toeplitz matrix, constructed
    /// from the elements <B>R[0]</B>, ..., <B>R[N-2]</B> and
    /// the vector <B>a</B> is constructed from the elements
    /// <B>R[1]</B>, ..., <B>R[N-1]</B>.
    /// <para>
    /// Durbin's algorithm is used to solve the linear system. It requires
    /// approximately the <b>N</b> squared FLOPS to calculate the
    /// solution (<b>N</b> is the matrix order).
    /// </para>
    /// </remarks>
    public static FloatVector YuleWalker(IROFloatVector R)
    {

      FloatVector a;

      // check parameters
      if (R == null)
      {
        throw new System.ArgumentNullException("R");
      }
      else if (R.Length < 2)
      {
        throw new System.ArgumentOutOfRangeException("R", "The length of R must be greater than 1.");
      }
      else
      {

        int N = R.Length - 1;
        a = new FloatVector(N);                    // prediction coefficients
        FloatVector Z = new FloatVector(N);   // temporary storage vector
        float e;                            // predictor error
        float inner;                               // inner product
        float g;                                   // reflection coefficient
        int i, j, l;

        // setup first order solution
        e = R[0];
        if (e == 0.0f)
        {
          throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
        }
        g = -R[1] / R[0];
        a[0] = g;

        // calculate solution for successive orders
        for (i = 1; i < N; i++)
        {

          e *= (1.0f - g * g);
          if (e == 0.0f)
          {
            throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
          }

          // calculate inner product
          inner = R[i + 1];
          for (j = 0, l = i; j < i; j++, l--)
          {
            inner += a[j] * R[l];
          }

          // update prediction coefficients
          g = -(inner / e);
          for (j = 0, l = i - 1; j < i; j++, l--)
          {
            Z[j] = a[j] + g * a[l];
          }

          // copy vector
          for (j = 0; j < i; j++)
          {
            a[j] = Z[j];
          }

          a[i] = g;

        }
      }

      return a;
    }
 public void SolveVector()
 {
   FloatVector b = new FloatVector(3);
   b[0] = 2;
   b[1] = 13;
   b[2] = 25;
   FloatVector x = cd.Solve(b);
   Assert.AreEqual(x[0],-3,TOLERENCE);
   Assert.AreEqual(x[1],8,TOLERENCE);
   Assert.AreEqual(x[2],8.333,TOLERENCE);
 }
    /// <overloads>
    /// Solve a symmetric square Toeplitz system.
    /// </overloads>
    /// <summary>
    /// Solve a symmetric square Toeplitz system with a right-side array.
    /// </summary>
    /// <param name="Y">The right-hand side of the system.</param>
    /// <returns>The solution vector.</returns>
    /// <exception cref="ArgumentNullException">
    /// Parameter <B>Y</B> is a null reference.
    /// </exception>
    /// <exception cref="RankException">
    /// The length of <B>Y</B> is not equal to the number of rows in the Toeplitz matrix.
    /// </exception>
    /// <exception cref="SingularMatrixException">
    /// The Toeplitz matrix or one of the the leading sub-matrices is singular.
    /// </exception>
    /// <remarks>
    /// This member solves the linear system <B>TX</B> = <B>Y</B>, where <B>T</B> is
    /// the symmetric square Toeplitz matrix, <B>X</B> is the unknown solution vector
    /// and <B>Y</B> is a known vector.
    /// <para>
    /// The class implicitly decomposes the inverse Toeplitz matrix into a <b>UDL</b> factorisation
    /// using the Levinson algorithm, and then calculates the solution vector.
    /// </para>
    /// </remarks>
    public FloatVector Solve(params float[] Y)
    {

      FloatVector X;

      // check parameters
      if (Y == null)
      {
        throw new System.ArgumentNullException("Y");
      }
      else if (m_Order != Y.Length)
      {
        throw new RankException("The length of Y is not equal to the number of rows in the Toeplitz matrix.");
      }

      Compute();

      if (m_IsSingular == true)
      {
        throw new SingularMatrixException("The Toeplitz matrix or one of the the leading sub-matrices is singular.");
      }

      int i, j, l;      // index/loop variables
      float Inner;      // inner product
      float G;        // scaling constant
      float[] A;        // reference to current order coefficients

      // allocate memory for solution
      X = new FloatVector(m_Order);

      // setup zero order solution
      X[0] = Y[0] / m_LeftColumn[0];

      // solve systems of increasing order
      for (i = 1; i < m_Order; i++)
      {
        // calculate inner product
        Inner = Y[i];
        for (j = 0, l = i; j < i; j++, l--)
        {
          Inner -= X[j] * m_LeftColumn[l];
        }
        // get the current predictor coefficients row
        A = m_LowerTriangle[i];

        // update the solution vector
        G = Inner * m_Diagonal[i];
        for (j = 0; j <= i; j++)
        {
          X[j] += G * A[j];
        }

      }

      return X;
    }
 public void ForEach()
 {
   FloatVector test = new FloatVector(new float[2]{1f,2f});
   foreach (float f in test) 
     Assert.IsTrue(test.Contains(f));
 }
Example #57
0
		///<summary>Solves a system on linear equations, AX=B, where A is the factored matrixed.</summary>
		///<param name="B">RHS side of the system.</param>
		///<returns>the solution vector, X.</returns>
		///<exception cref="ArgumentNullException">B is null.</exception>
		///<exception cref="NotPositiveDefiniteException">A is not positive definite.</exception>
		///<exception cref="ArgumentException">The number of rows of A and the length of B must be the same.</exception>
		public FloatVector Solve(IROFloatVector B)
		{
			if (B == null)
			{
				throw new System.ArgumentNullException("B cannot be null.");
			}
			Compute();
			if (!ispd)
			{
				throw new NotPositiveDefiniteException();
			}
			else
			{
				if (B.Length != order)
				{
					throw new System.ArgumentException("The length of B must be the same as the order of the matrix.");
				}
#if MANAGED
				// Copy right hand side.
				FloatVector X = new FloatVector(B);
				// Solve L*Y = B;
				for (int i = 0; i < order; i++)
				{
					float sum = B[i];
					for (int k = i - 1; k >= 0; k--)
					{
						sum -= l.data[i][k] * X.data[k];
					}
					X.data[i] = sum / l.data[i][i];
				}
				// Solve L'*X = Y;
				for (int i = order - 1; i >= 0; i--)
				{
					float sum = X.data[i];
					for (int k = i + 1; k < order; k++)
					{
						sum -= l.data[k][i] * X.data[k];
					}
					X.data[i] = sum / l.data[i][i];
				}

				return X;
#else
                float[] rhs = FloatMatrix.ToLinearArray(B);
                Lapack.Potrs.Compute(Lapack.UpLo.Lower,order,1,l.data,order,rhs,B.Length);
                FloatVector ret = new FloatVector(order,B.Length);
                ret.data = rhs;
                return ret;
#endif
			}
		}
Example #58
0
 ///<summary>Multiply a <c>float</c> x with a <c>FloatVector</c> y as x*y</summary>
 ///<param name="lhs"><c>float</c> as left hand operand.</param>
 ///<param name="rhs"><c>FloatVector</c> as right hand operand.</param>
 ///<returns><c>FloatVector</c> with results.</returns>
 public static FloatVector operator *(float lhs, FloatVector rhs)
 {
   FloatVector ret = new FloatVector(rhs);
   Blas.Scal.Compute(ret.Length, lhs, ret.data, 1);
   return ret;
 }
 ///<summary>Implicit cast conversion to <c>ComplexFloatVector</c> from <c>FloatVector</c></summary>
 static public ComplexFloatVector ToComplexFloatVector(FloatVector src)
 {
   float[] temp = src.ToArray();
   ComplexFloatVector ret = new ComplexFloatVector(temp.Length);
   for (int i = 0; i < temp.Length; ++i)
   {
     ret.data[i] = temp[i];
   }
   return ret;
 }
Example #60
0
    /// <summary>
    /// Returns the column of a <see cref="IROFloatMatrix" /> as a new <c>FloatVector.</c>
    /// </summary>
    /// <param name="mat">The matrix to copy the column from.</param>
    /// <param name="col">Index of the column to copy from the matrix.</param>
    /// <returns>A new <c>FloatVector</c> with the same elements as the column of the given matrix.</returns>
    public static FloatVector GetColumn(IROFloatMatrix mat, int col)
    {
      FloatVector result = new FloatVector(mat.Rows);
      for (int i = 0; i < result.data.Length; ++i)
        result.data[i] = mat[i, col];

      return result;
    }