/// <summary> /// Creates a new <paramref name="n"/> x <paramref name="n"/> RealPolynomialMatrix with RealPolynomial <paramref name="f"/> on the leading diagonal and zeros everywhere else /// </summary> public RealPolynomialMatrix(int n, RealPolynomial f) { if (n <= 0) { throw new Exception("Matrices must have positive dimensions"); } else { RealPolynomial[,] _diag = new RealPolynomial[n, n]; for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { if (i == j) { _diag[i, i] = f; } else { _diag[i, j] = RealPolynomial.zeroPolynomial; } } } data = _diag; m = n; this.n = n; } }
/// <summary> /// Returns RealMatrix <paramref name="A"/> right-multiplied by RealPolynomialMatrix <paramref name="B"/> /// </summary> public static RealPolynomialMatrix operator *(RealMatrix A, RealPolynomialMatrix B) { if (A.NumColumns() != B.NumRows()) { throw new Exception("Matrices are not compatiable for multpilication."); } else { RealPolynomial[,] product = new RealPolynomial[A.NumRows(), B.NumColumns()]; for (int i = 0; i < A.NumRows(); i++) { for (int j = 0; j < B.NumColumns(); j++) { RealPolynomial elementIJ = RealPolynomial.zeroPolynomial; for (int k = 0; k < A.NumColumns(); k++) { elementIJ += A.GetIJ(i, k) * B.GetIJ(k, j); } product[i, j] = elementIJ; } } return(new RealPolynomialMatrix(product)); } }
/// <summary> /// Returns the determinant of the RealPolynomialMatrix /// </summary> public RealPolynomial Det() { if (m != n) { throw new Exception("Matrix must be square to have a determinant"); } else { RealPolynomial total = RealPolynomial.zeroPolynomial; RealPolynomial det; if (n == 2) { RealPolynomial leadProd = GetIJ(0, 0) * GetIJ(1, 1); RealPolynomial infProd = GetIJ(0, 1) * GetIJ(1, 0); det = leadProd - infProd; return(det); } else { for (int j = 0; j < n; j++) { RealPolynomialMatrix tempArr = DeleteRowColumn(0, j); RealPolynomial x = tempArr.Det(); RealPolynomial y = GetIJ(0, j) * Math.Pow(-1, j); det = x * y; total += det; } return(total); } } }
/// <summary> /// Returns the product of RealPolynomial <paramref name="p"/> and double <paramref name="scalar"/> /// </summary> public static RealPolynomial operator *(RealPolynomial p, double scalar) { double[] prod = new double[p.coeff.Length]; for (int i = 0; i < p.coeff.Length; i++) { prod[i] = scalar * p.coeff[i]; } RealPolynomial scaled = new RealPolynomial(prod); return(scaled); }
/// <summary> /// Returns the RealPolynomialMatrix obtained by deleting <paramref name="row"/> and <paramref name="column"/> of the PolynomialMatrix /// </summary> public RealPolynomialMatrix DeleteRowColumn(int row, int column) { if (m != n) { throw new Exception("We only define minors for square matrices"); } else //splits matrix into 4 (possibly empty) 'boxes' in order to construct the new matrix { RealPolynomial[,] deleteRowColumn = new RealPolynomial[n - 1, n - 1]; for (int i = 0; i < row; i++) { for (int j = 0; j < column; j++) { deleteRowColumn[i, j] = GetIJ(i, j); } } if (row == n - 1 & column == n - 1) //makes the method more efficient if we delete the last row and last column { return(new RealPolynomialMatrix(deleteRowColumn)); } for (int i = row + 1; i < m; i++) { for (int j = 0; j < column; j++) { deleteRowColumn[i - 1, j] = GetIJ(i, j); } } for (int i = 0; i < row; i++) { for (int j = column + 1; j < n; j++) { deleteRowColumn[i, j - 1] = GetIJ(i, j); } } for (int i = row + 1; i < m; i++) { for (int j = column + 1; j < n; j++) { deleteRowColumn[i - 1, j - 1] = GetIJ(i, j); } } return(new RealPolynomialMatrix(deleteRowColumn)); } }
/// <summary> /// Returns double <paramref name="d"/> subtracted from RealPolynomial <paramref name="p"/> /// </summary> public static RealPolynomial operator -(RealPolynomial p, double d) { double[] diffCoeff = new double[p.coeff.Length]; diffCoeff[0] = p.coeff[0] - d; for (int i = 1; i < diffCoeff.Length; i++) { diffCoeff[i] = p.coeff[i]; } RealPolynomial diff = new RealPolynomial(diffCoeff); return(diff); }
/// <summary> /// Returns the sum of RealPolynomial <paramref name="p"/> and double <paramref name="d"/> /// </summary> public static RealPolynomial operator +(RealPolynomial p, double d) { double[] sumCoeff = new double[p.coeff.Length]; sumCoeff[0] = d + p.coeff[0]; for (int i = 1; i < sumCoeff.Length; i++) { sumCoeff[i] = p.coeff[i]; } RealPolynomial sum = new RealPolynomial(sumCoeff); return(sum); }
/// <summary> /// Returns the transpose of the RealPolynomialMatrix /// </summary> public RealPolynomialMatrix Transpose() { RealPolynomial[,] transpose = new RealPolynomial[n, m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { transpose[i, j] = GetIJ(j, i); } } return(new RealPolynomialMatrix(transpose)); }
/// <summary> /// Returns RealPolynomialMatrix <paramref name="A"/> multiplied by double <paramref name="scalar"/> /// </summary> public static RealPolynomialMatrix operator *(RealPolynomialMatrix A, double scalar) { RealPolynomial[,] product = new RealPolynomial[A.m, A.n]; for (int i = 0; i < A.m; i++) { for (int j = 0; j < A.n; j++) { product[i, j] = A.GetIJ(i, j) * scalar; } } return(new RealPolynomialMatrix(product)); }
/// <summary> /// Returns RealPolynomial <paramref name="q"/> subtracted from RealPolynomial <paramref name="p"/> /// </summary> public static RealPolynomial operator -(RealPolynomial p, RealPolynomial q) { if (p.n == q.n) //case where the degrees are the same { double[] diffCoeff = new double[p.n + 1]; for (int i = 0; i < diffCoeff.Length; i++) { diffCoeff[i] = p.coeff[i] - q.coeff[i]; } RealPolynomial diff = new RealPolynomial(diffCoeff); return(diff); } else if (p.n > q.n) //case where degree of p is greater than degree of q { double[] diffCoeff = new double[p.n + 1]; for (int i = 0; i < q.coeff.Length; i++) { diffCoeff[i] = p.coeff[i] - q.coeff[i]; } for (int i = q.coeff.Length; i < diffCoeff.Length; i++) { diffCoeff[i] = p.coeff[i]; } RealPolynomial diff = new RealPolynomial(diffCoeff); return(diff); } else //case where degree of q is greater than degree of p { double[] diffCoeff = new double[q.n + 1]; for (int i = 0; i < p.coeff.Length; i++) { diffCoeff[i] = p.coeff[i] - q.coeff[i]; } for (int i = p.coeff.Length; i < diffCoeff.Length; i++) { diffCoeff[i] = -q.coeff[i]; } RealPolynomial diff = new RealPolynomial(diffCoeff); return(diff); } }
/// <summary> /// Returns the sum of RealPolynomial <paramref name="p"/> and RealPolynomial <paramref name="q"/> /// </summary> public static RealPolynomial operator +(RealPolynomial p, RealPolynomial q) { if (p.n == q.n) //case where the degrees are the same { double[] sumCoeff = new double[p.n + 1]; for (int i = 0; i < sumCoeff.Length; i++) { sumCoeff[i] = p.coeff[i] + q.coeff[i]; } RealPolynomial sum = new RealPolynomial(sumCoeff); return(sum); } else if (p.n > q.n) //case where degree of p is bigger than degree of q { double[] sumCoeff = new double[p.n + 1]; for (int i = 0; i < q.coeff.Length; i++) { sumCoeff[i] = p.coeff[i] + q.coeff[i]; } for (int i = q.coeff.Length; i < sumCoeff.Length; i++) { sumCoeff[i] = p.coeff[i]; } RealPolynomial sum = new RealPolynomial(sumCoeff); return(sum); } else //case where degree of q is greater than degree of p { double[] sumCoeff = new double[q.n + 1]; for (int i = 0; i < p.coeff.Length; i++) { sumCoeff[i] = p.coeff[i] + q.coeff[i]; } for (int i = p.coeff.Length; i < sumCoeff.Length; i++) { sumCoeff[i] = q.coeff[i]; } RealPolynomial sum = new RealPolynomial(sumCoeff); return(sum); } }
/// <summary> /// Returns true if RealPolynomial <paramref name="p"/> and RealPolynomial <paramref name="q"/> are equal; returns false otherwise /// </summary> public static bool Equals(RealPolynomial p, RealPolynomial q) { if (p.n != q.n) { return(false); } else { for (int i = 0; i < p.coeff.Length; i++) { if (p.coeff[i] != q.coeff[i]) { return(false); } } return(true); } }
/// <summary> /// Returns RealPolynomialMatrix <paramref name="B"/> subtracted from RealMatrix <paramref name="A"/> /// </summary> public static RealPolynomialMatrix operator -(RealMatrix A, RealPolynomialMatrix B) { if ((A.NumRows() != B.NumRows()) | (A.NumColumns() != B.NumColumns())) { throw new Exception("Matrices are not compatable for subtraction"); } else { RealPolynomial[,] diff = new RealPolynomial[A.NumRows(), A.NumColumns()]; for (int i = 0; i < A.NumRows(); i++) { for (int j = 0; j < A.NumColumns(); j++) { diff[i, j] = A.GetIJ(i, j) - B.GetIJ(i, j); } } return(new RealPolynomialMatrix(diff)); } }
/// <summary> /// Returns the sum of RealPolynomialMatrix <paramref name="A"/> RealMatrix <paramref name="B"/> /// </summary> public static RealPolynomialMatrix operator +(RealPolynomialMatrix A, RealMatrix B) { if ((A.m != B.NumRows()) | (A.n != B.NumColumns())) { throw new Exception("Matrices are not compatable for addition"); } else { RealPolynomial[,] sum = new RealPolynomial[A.m, A.n]; for (int i = 0; i < A.m; i++) { for (int j = 0; j < A.n; j++) { sum[i, j] = A.GetIJ(i, j) + B.GetIJ(i, j); } } return(new RealPolynomialMatrix(sum)); } }