/// <summary> /// /// </summary> /// <param name="value"></param> /// <returns></returns> public override QsValue DifferentiateOperation(QsValue vl) { QsValue value; if (vl is QsReference) { value = ((QsReference)vl).ContentValue; } else { value = vl; } if (value is QsScalar) { QsMatrix n = new QsMatrix(); foreach (var row in n.Rows) { n.AddRow(row.DifferentiateScalar((QsScalar)value).ToArray()); } return(n); } else { return(base.DifferentiateOperation(value)); } }
/// <summary> /// Matrix - Matrix /// </summary> /// <param name="matrix"></param> /// <returns></returns> private QsMatrix SubtractMatrix(QsMatrix matrix) { if (this.DimensionEquals(matrix)) { QsMatrix Total = new QsMatrix(); for (int IY = 0; IY < this.RowsCount; IY++) { List <QsScalar> row = new List <QsScalar>(ColumnsCount); for (int IX = 0; IX < this.ColumnsCount; IX++) { row.Add(this[IY, IX] - matrix[IY, IX]); } Total.AddRow(row.ToArray()); } return(Total); } else { throw new QsMatrixException("Matrix 1 [" + this.RowsCount.ToString(CultureInfo.InvariantCulture) + "x" + this.ColumnsCount.ToString(CultureInfo.InvariantCulture) + "] is not dimensional equal with Matrix 2 [" + matrix.RowsCount.ToString(CultureInfo.InvariantCulture) + "x" + matrix.ColumnsCount.ToString(CultureInfo.InvariantCulture) + "]"); } }
/// <summary> /// Transfer the columns of the matrix into rows. /// </summary> /// <returns></returns> public QsMatrix Transpose() { QsMatrix m = new QsMatrix(); for (int IX = 0; IX < ColumnsCount; IX++) { var vec = this.GetColumnVectorMatrix(IX); m.AddRow(vec.ToArray()); } return(m); }
/// <summary> /// Matrix + scalar /// </summary> /// <param name="scalar"></param> /// <returns></returns> private QsMatrix AddScalar(QsScalar scalar) { QsMatrix Total = new QsMatrix(); for (int IY = 0; IY < this.RowsCount; IY++) { List <QsScalar> row = new List <QsScalar>(ColumnsCount); for (int IX = 0; IX < this.ColumnsCount; IX++) { row.Add(this[IY, IX] + scalar); } Total.AddRow(row.ToArray()); } return(Total); }
/// <summary> /// Matrix elements ^. scalar /// </summary> /// <param name="scalarQuantity"></param> /// <returns></returns> public QsMatrix ElementsPowerScalar(QsScalar scalar) { QsMatrix Total = new QsMatrix(); for (int IY = 0; IY < this.RowsCount; IY++) { List <QsScalar> row = new List <QsScalar>(ColumnsCount); for (int IX = 0; IX < this.ColumnsCount; IX++) { row.Add(this[IY, IX].PowerScalar(scalar)); } Total.AddRow(row.ToArray()); } return(Total); }
/// <summary> /// Ordinary multiplicatinon of the matrix. /// Naive implementation :D and I am proud :P /// /// </summary> /// <param name="matrix"></param> /// <returns></returns> public QsMatrix MultiplyMatrix(QsMatrix matrix) { if (this.ColumnsCount == matrix.RowsCount) { QsMatrix Total = new QsMatrix(); //loop through my rows for (int iy = 0; iy < RowsCount; iy++) { var vec = this.Rows[iy]; QsScalar[] tvec = new QsScalar[matrix.ColumnsCount]; //the target row in the Total Matrix. //loop through all co vectors in the target matrix. for (int tix = 0; tix < matrix.ColumnsCount; tix++) { var covec = matrix.GetColumnVectorMatrix(tix).ToArray(); //multiply vec*covec and store it at the Total matrix at iy,ix QsScalar[] snum = new QsScalar[vec.Count]; for (int i = 0; i < vec.Count; i++) { snum[i] = vec[i].MultiplyScalar(covec[i]); } QsScalar tnum = snum[0]; for (int i = 1; i < snum.Length; i++) { tnum = tnum + snum[i]; } tvec[tix] = tnum; } Total.AddRow(tvec); } return(Total); } else { throw new QsMatrixException("Width of the first matrix [" + this.ColumnsCount + "] not equal to the height of the second matrix [" + matrix.RowsCount + "]"); } }