public static Vector operator *(Vector v, Matrix m) { Contract.Requires(v != null); Contract.Requires(m != null); LowerTriangEtaMatrix ltr = m as LowerTriangEtaMatrix; if (ltr != null) { return(v * ltr); } TranspositionMatrix tr = m as TranspositionMatrix; if (tr != null) { return(v * tr); } Contract.Assume(v.Length == m.NumberOfRows); Vector r = new Vector(m.NumberOfColumns); for (int i = 0; i < m.NumberOfColumns; i++) { double s = 0; for (int j = 0; j < v.Length; j++) { s += v[j] * m[j, i]; } r[i] = s; } return(r); }
static public double[] operator *(Matrix b, double[] a) { Contract.Requires(b != null); Contract.Requires(a != null); Contract.Ensures(Contract.Result <double[]>() != null); LowerTriangEtaMatrix L = b as LowerTriangEtaMatrix; if (L != null) { return(L * a); } TranspositionMatrix P = b as TranspositionMatrix; if (P != null) { return(P * a); } throw new NotImplementedException(); }
/// <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); } } }