Exemplo n.º 1
0
        static public Matrix operator *(Matrix a, double b)
        {
            Contract.Requires(a != null);
            Contract.Requires(a.NumberOfRows > 0);

            FullMatrix ret = new FullMatrix(a.NumberOfRows, new double[a.NumberOfRows * a.NumberOfColumns]);

            for (int i = 0; i < a.NumberOfRows; i++)
            {
                for (int j = 0; j < a.NumberOfColumns; j++)
                {
                    Contract.Assume(a.NumberOfColumns <= ret.NumberOfColumns); // F: cannot prove it becase it's quadratic

                    ret[i, j] = b * a[i, j];
                }
            }

            return(ret);
        }
Exemplo n.º 2
0
        static public Matrix operator -(Matrix a, Matrix b)
        {
            Contract.Requires(a != null);
            Contract.Requires(b != null);
            Contract.Requires(a.NumberOfRows > 0);
            Contract.Requires(a.NumberOfRows == b.NumberOfRows);
            Contract.Requires(a.NumberOfColumns == b.NumberOfColumns);

            //System.Diagnostics.Debug.Assert(a.NumberOfRows == b.NumberOfRows && a.NumberOfColumns == b.NumberOfColumns);
            FullMatrix ret = new FullMatrix(a.NumberOfRows, new double[a.NumberOfRows * a.NumberOfColumns]);

            for (int i = 0; i < a.NumberOfRows; i++)
            {
                for (int j = 0; j < a.NumberOfColumns; j++)
                {
                    Contract.Assume(a.NumberOfColumns <= ret.NumberOfColumns); // F: cannot prove it becase it's quadratic

                    ret[i, j] = a[i, j] - b[i, j];
                }
            }

            return(ret);
        }
Exemplo n.º 3
0
    static public Matrix operator +(Matrix a, Matrix b)
    {
      //System.Diagnostics.Debug.Assert(a.NumberOfRows==b.NumberOfRows&&a.NumberOfColumns== b.NumberOfColumns);
      Contract.Requires(a != null);
      Contract.Requires(b != null);
      Contract.Requires(a.NumberOfRows > 0);
      Contract.Requires(a.NumberOfRows == b.NumberOfRows);
      Contract.Requires(a.NumberOfColumns == b.NumberOfColumns);

      Contract.Assume(a.NumberOfColumns > 0);

      FullMatrix ret = new FullMatrix(a.NumberOfRows, new double[a.NumberOfRows * a.NumberOfColumns]);
      for (int i = 0; i < a.NumberOfRows; i++)
      {
        for (int j = 0; j < a.NumberOfColumns; j++)
        {
          Contract.Assume(a.NumberOfColumns <= ret.NumberOfColumns); // F: cannot prove it becase it's quadratic

          ret[i, j] = a[i, j] + b[i, j];
        }
      }
      return ret;
    }
Exemplo n.º 4
0
    /// <summary>
    /// If one parameter is a Vector then it can be reused for the answer
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <returns></returns>
    static public Matrix operator *(Matrix a, Matrix b)
    {
      Contract.Requires(a != null);
      Contract.Requires(b != null);

      Contract.Requires(a.NumberOfRows > 0);

      Vector v = b as Vector;
      if (v != null)
      {
        Contract.Assume(a.NumberOfColumns == v.Length);

        TranspositionMatrix transposition = a as TranspositionMatrix;

        if (transposition != null)
          return v * transposition;
        else
        {

          LowerTriangEtaMatrix lm = a as LowerTriangEtaMatrix;
          if (lm != null)
            return lm * v;
          else
          {
            double[] cfs = new double[a.NumberOfRows];
            for (int i = 0; i < a.NumberOfRows; i++)
            {
              double r = 0;
              for (int j = 0; j < v.Length; j++)
                r += a[i, j] * v[j];
              cfs[i] = r;
            }
            return new Vector(cfs);
          }
        }
      }
      else
      {
        v = a as Vector;
        if (v != null)
          return v * b;
        else
        {
          Contract.Assert(a.NumberOfRows > 0);

          Contract.Assume(a.NumberOfColumns == b.NumberOfRows);

          FullMatrix ret = new FullMatrix(a.NumberOfRows, new double[a.NumberOfRows * b.NumberOfColumns]);
          for (int i = 0; i < a.NumberOfRows; i++)
            for (int j = 0; j < b.NumberOfColumns; j++)
            {
              double r = 0;
              for (int k = 0; k < a.NumberOfColumns; k++)
                r += a[i, k] * b[k, j];

              Contract.Assume(b.NumberOfColumns <= ret.NumberOfColumns);  // F: Cannot prove it because quadratic

              ret[i, j] = r;
            }

          return ret;
        }
      }
    }
Exemplo n.º 5
0
    static public Matrix operator *(Matrix a, double b)
    {
      Contract.Requires(a != null);
      Contract.Requires(a.NumberOfRows > 0);

      FullMatrix ret = new FullMatrix(a.NumberOfRows, new double[a.NumberOfRows * a.NumberOfColumns]);
      for (int i = 0; i < a.NumberOfRows; i++)
        for (int j = 0; j < a.NumberOfColumns; j++)
        {
          Contract.Assume(a.NumberOfColumns <= ret.NumberOfColumns); // F: cannot prove it becase it's quadratic

          ret[i, j] = b * a[i, j];
        }

      return ret;
    }
Exemplo n.º 6
0
        /// <summary>
        /// If one parameter is a Vector then it can be reused for the answer
        /// </summary>
        /// <param name="a"></param>
        /// <param name="b"></param>
        /// <returns></returns>
        static public Matrix operator *(Matrix a, Matrix b)
        {
            Contract.Requires(a != null);
            Contract.Requires(b != null);

            Contract.Requires(a.NumberOfRows > 0);

            Vector v = b as Vector;

            if (v != null)
            {
                Contract.Assume(a.NumberOfColumns == v.Length);

                TranspositionMatrix transposition = a as TranspositionMatrix;

                if (transposition != null)
                {
                    return(v * transposition);
                }
                else
                {
                    LowerTriangEtaMatrix lm = a as LowerTriangEtaMatrix;
                    if (lm != null)
                    {
                        return(lm * v);
                    }
                    else
                    {
                        double[] cfs = new double[a.NumberOfRows];
                        for (int i = 0; i < a.NumberOfRows; i++)
                        {
                            double r = 0;
                            for (int j = 0; j < v.Length; j++)
                            {
                                r += a[i, j] * v[j];
                            }
                            cfs[i] = r;
                        }
                        return(new Vector(cfs));
                    }
                }
            }
            else
            {
                v = a as Vector;
                if (v != null)
                {
                    return(v * b);
                }
                else
                {
                    Contract.Assert(a.NumberOfRows > 0);

                    Contract.Assume(a.NumberOfColumns == b.NumberOfRows);

                    FullMatrix ret = new FullMatrix(a.NumberOfRows, new double[a.NumberOfRows * b.NumberOfColumns]);
                    for (int i = 0; i < a.NumberOfRows; i++)
                    {
                        for (int j = 0; j < b.NumberOfColumns; j++)
                        {
                            double r = 0;
                            for (int k = 0; k < a.NumberOfColumns; k++)
                            {
                                r += a[i, k] * b[k, j];
                            }

                            Contract.Assume(b.NumberOfColumns <= ret.NumberOfColumns); // F: Cannot prove it because quadratic

                            ret[i, j] = r;
                        }
                    }

                    return(ret);
                }
            }
        }