Esempio n. 1
0
        /// <summary>
        /// Computes the Householder vector.
        /// </summary>
        /// <param name="x"></param>
        /// <returns></returns>
        private static MatrixQ[] HouseholderVector(MatrixQ x)
        {
            //throw new NotImplementedException("Supposingly buggy!");

              //if (!x.IsReal())
              //    throw new ArgumentException("Cannot compute housholder vector of non-real vector.");

              int n = x.VectorLength();

              if (n == 0)
            throw new InvalidOperationException("Expected vector as argument.");

              MatrixQ y = x / x.Norm();
              MatrixQ buf = y.Extract(2, n, 1, 1);
              Complex s = Dot(buf, buf);

              MatrixQ v = Zeros(n, 1);
              v[1] = Complex.One;

              v.Insert(2, 1, buf);

              double beta = 0;

              if (s != 0) {
            Complex mu = Complex.Sqrt(y[1] * y[1] + s);
            if (y[1].Re <= 0)
              v[1] = y[1] - mu;
            else
              v[1] = -s / (y[1] + mu);

            beta = 2 * v[1].Re * v[1].Re / (s.Re + v[1].Re * v[1].Re);
            v = v / v[1];
              }

              return new MatrixQ[] { v, new MatrixQ(beta) };
        }