public void Drot(DoubleMatrix1D x, DoubleMatrix1D y, double c, double s) { x.CheckSize(y); DoubleMatrix1D tmp = x.Copy(); x.Assign(F1.Mult(c)); x.Assign(y, F2.PlusMult(s)); y.Assign(F1.Mult(c)); y.Assign(tmp, F2.MinusMult(s)); }
public void Dsymv(Boolean isUpperTriangular, double alpha, DoubleMatrix2D A, DoubleMatrix1D x, double beta, DoubleMatrix1D y) { if (isUpperTriangular) { A = A.ViewDice(); } Property.DEFAULT.CheckSquare(A); int size = A.Rows; if (size != x.Size || size != y.Size) { throw new ArgumentException(A.ToStringShort() + ", " + x.ToStringShort() + ", " + y.ToStringShort()); } DoubleMatrix1D tmp = x.Like(); for (int i = 0; i < size; i++) { double sum = 0; for (int j = 0; j <= i; j++) { sum += A[i, j] * x[j]; } for (int j = i + 1; j < size; j++) { sum += A[j, i] * x[j]; } tmp[i] = alpha * sum + beta * y[i]; } y.Assign(tmp); }
public void Dtrmv(Boolean isUpperTriangular, Boolean transposeA, Boolean isUnitTriangular, DoubleMatrix2D A, DoubleMatrix1D x) { if (transposeA) { A = A.ViewDice(); isUpperTriangular = !isUpperTriangular; } Property.DEFAULT.CheckSquare(A); int size = A.Rows; if (size != x.Size) { throw new ArgumentException(A.ToStringShort() + ", " + x.ToStringShort()); } DoubleMatrix1D b = x.Like(); DoubleMatrix1D y = x.Like(); if (isUnitTriangular) { y.Assign(1); } else { for (int i = 0; i < size; i++) { y[i] = A[i, i]; } } for (int i = 0; i < size; i++) { double sum = 0; if (!isUpperTriangular) { for (int j = 0; j < i; j++) { sum += A[i, j] * x[j]; } sum += y[i] * x[i]; } else { sum += y[i] * x[i]; for (int j = i + 1; j < size; j++) { sum += A[i, j] * x[j]; } } b[i] = sum; } x.Assign(b); }
/// <summary> /// Linear algebraic matrix-vector multiplication; <i>z = A * y</i>. /// <i>z[i] = alpha*Sum(A[i,j] * y[j]) + beta*z[i], i=0..A.rows()-1, j=0..y.Count-1</i>. /// Where <i>A == this</i>. /// </summary> /// <param name="y">the source vector.</param> /// <param name="z">the vector where results are to be stored.</param> /// <param name="nonZeroIndexes"></param> /// <param name="allRows"></param> /// <param name="alpha"></param> /// <param name="beta"></param> /// <exception cref="ArgumentException">if <i>A.columns() != y.Count || A.rows() > z.Count)</i>.</exception> protected void ZMult(DoubleMatrix1D y, DoubleMatrix1D z, IntArrayList nonZeroIndexes, DoubleMatrix1D[] allRows, double alpha, double beta) { if (Columns != y.Size || Rows > z.Size) { throw new ArgumentException(String.Format(Cern.LocalizedResources.Instance().Exception_IncompatibleArgs, ToStringShort(), y.ToStringShort(), z.ToStringShort())); } z.Assign(Cern.Jet.Math.Functions.DoubleFunctions.Mult(beta / alpha)); for (int i = indexes.Length; --i >= 0;) { if (indexes[i] != null) { for (int k = indexes[i].Count; --k >= 0;) { int j = indexes[i][k]; double value = values[i][k]; z[i] = z[i] + value * y[j]; } } } z.Assign(Cern.Jet.Math.Functions.DoubleFunctions.Mult(alpha)); }
/// <summary> /// A = A<sup>s</sup> <=> A[i] = System.Math.Pow(A[i], s) /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="s">the scalar; can have any value.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Pow(DoubleMatrix1D A, double s) { return A.Assign(F1.Pow(s)); }
/// <summary> /// A = A + B*s<=> A[i] = A[i] + B[i]*s /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="B">the matrix to stay unaffected.</param> /// <param name="s">the scalar; can have any value.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D PlusMult(DoubleMatrix1D A, DoubleMatrix1D B, double s) { return A.Assign(B, F2.PlusMult(s)); }
/// <summary> /// A = A + B <=> A[i] = A[i] + B[i] /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="B">the matrix to stay unaffected.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Plus(DoubleMatrix1D A, DoubleMatrix1D B) { return A.Assign(B, F2.Plus); }
/// <summary> /// A = -A <=> A[i] = -A[i] for all cells. /// </summary> /// <param name="A"></param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Negate(DoubleMatrix1D A) { return A.Assign(F1.Mult(-1)); }
/// <summary> /// A = A * B <=> A[i] = A[i] * B[i] /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="B">the matrix to stay unaffected.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Mult(DoubleMatrix1D A, DoubleMatrix1D B) { return A.Assign(B, F2.Mult); }
public void Solve(DoubleMatrix2D B) { int CUT_OFF = 10; //algebra.property().checkRectangular(LU); int m = M; int n = N; if (B.Rows != m) { throw new ArgumentException("Matrix row dimensions must agree."); } if (!this.IsNonsingular) { throw new ArgumentException("Matrix is singular."); } // right hand side with pivoting // Matrix Xmat = B.getMatrix(piv,0,nx-1); if (this.work1 == null || this.work1.Length < m) { this.work1 = new int[m]; } //if (this.work2 == null || this.work2.Length < m) this.work2 = new int[m]; Algebra.PermuteRows(B, this.piv, this.work1); if (m * n == 0) { return; // nothing to do } int nx = B.Columns; //precompute and cache some views to avoid regenerating them time and again DoubleMatrix1D[] Brows = new DoubleMatrix1D[n]; for (int k = 0; k < n; k++) { Brows[k] = B.ViewRow(k); } // transformations Cern.Jet.Math.Mult div = Cern.Jet.Math.Mult.Div(0); Cern.Jet.Math.PlusMult minusMult = Cern.Jet.Math.PlusMult.MinusMult(0); IntArrayList nonZeroIndexes = new IntArrayList(); // sparsity DoubleMatrix1D Browk = Cern.Colt.Matrix.DoubleFactory1D.Dense.Make(nx); // blocked row k // Solve L*Y = B(piv,:) for (int k = 0; k < n; k++) { // blocking (make copy of k-th row to localize references) Browk.Assign(Brows[k]); // sparsity detection int maxCardinality = nx / CUT_OFF; // == heuristic depending on speedup Browk.GetNonZeros(nonZeroIndexes, null, maxCardinality); int cardinality = nonZeroIndexes.Count; Boolean sparse = (cardinality < maxCardinality); for (int i = k + 1; i < n; i++) { //for (int j = 0; j < nx; j++) B[i][j] -= B[k][j]*LU[i][k]; //for (int j = 0; j < nx; j++) B.set(i,j, B.Get(i,j) - B.Get(k,j)*LU.Get(i,k)); minusMult.Multiplicator = -LU[i, k]; if (minusMult.Multiplicator != 0) { if (sparse) { Brows[i].Assign(Browk, minusMult, nonZeroIndexes); } else { Brows[i].Assign(Browk, minusMult); } } } } // Solve U*B = Y; for (int k = n - 1; k >= 0; k--) { // for (int j = 0; j < nx; j++) B[k][j] /= LU[k][k]; // for (int j = 0; j < nx; j++) B.set(k,j, B.Get(k,j) / LU.Get(k,k)); div.Multiplicator = 1 / LU[k, k]; Brows[k].Assign(div); // blocking if (Browk == null) { Browk = Cern.Colt.Matrix.DoubleFactory1D.Dense.Make(B.Columns); } Browk.Assign(Brows[k]); // sparsity detection int maxCardinality = nx / CUT_OFF; // == heuristic depending on speedup Browk.GetNonZeros(nonZeroIndexes, null, maxCardinality); int cardinality = nonZeroIndexes.Count; Boolean sparse = (cardinality < maxCardinality); //Browk.GetNonZeros(nonZeroIndexes,null); //Boolean sparse = nonZeroIndexes.Count < nx/10; for (int i = 0; i < k; i++) { // for (int j = 0; j < nx; j++) B[i][j] -= B[k][j]*LU[i][k]; // for (int j = 0; j < nx; j++) B.set(i,j, B.Get(i,j) - B.Get(k,j)*LU.Get(i,k)); minusMult.Multiplicator = -LU[i, k]; if (minusMult.Multiplicator != 0) { if (sparse) { Brows[i].Assign(Browk, minusMult, nonZeroIndexes); } else { Brows[i].Assign(Browk, minusMult); } } } } }
/// <summary> /// A = A / s <=> A[i] = A[i] / s /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="s">the scalar; can have any value.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Div(DoubleMatrix1D A, double s) { return A.Assign(F1.Div(s)); }
/// <summary> /// A = A / B <=> A[i] = A[i] / B[i] /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="B">the matrix to stay unaffected.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Div(DoubleMatrix1D A, DoubleMatrix1D B) { return A.Assign(B, F2.Div); }
public void Dcopy(DoubleMatrix1D x, DoubleMatrix1D y) { y.Assign(x); }
public void Daxpy(double alpha, DoubleMatrix1D x, DoubleMatrix1D y) { y.Assign(x, F2.PlusMult(alpha)); }
public void Dscal(double alpha, DoubleMatrix1D x) { x.Assign(F1.Mult(alpha)); }
/// <summary> /// A = A<sup>B</sup> <=> A[i] = System.Math.Pow(A[i], B[i]) /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="B">the matrix to stay unaffected.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Pow(DoubleMatrix1D A, DoubleMatrix1D B) { return A.Assign(B, F2.Pow); }
/// <summary> /// A[i] = System.Math.Abs(A[i]) /// </summary> /// <param name="A">the matrix to modify.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Abs(DoubleMatrix1D A) { return A.Assign(F1.Abs); }
/// <summary> /// A = A - s <=> A[i] = A[i] - s /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="s">the scalar; can have any value.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Minus(DoubleMatrix1D A, double s) { return A.Assign(F1.Minus(s)); }
/// <summary> /// >A = A * s <=> A[i] = A[i] * s /// </summary> /// <param name="A">the matrix to modify.</param> /// <param name="s">the scalar; can have any value.</param> /// <returns><i>A</i> (for convenience only).</returns> public static DoubleMatrix1D Mult(DoubleMatrix1D A, double s) { return A.Assign(F1.Mult(s)); }
public void Decompose(DoubleMatrix2D A) { int CUT_OFF = 10; // setup LU = A; int m = A.Rows; int n = A.Columns; // setup pivot vector if (this.piv == null || this.piv.Length != m) { this.piv = new int[m]; } for (int i = m; --i >= 0;) { piv[i] = i; } pivsign = 1; if (m * n == 0) { LU = LU; return; // nothing to do } //precompute and cache some views to avoid regenerating them time and again DoubleMatrix1D[] LUrows = new DoubleMatrix1D[m]; for (int i = 0; i < m; i++) { LUrows[i] = LU.ViewRow(i); } IntArrayList nonZeroIndexes = new IntArrayList(); // sparsity DoubleMatrix1D LUcolj = LU.ViewColumn(0).Like(); // blocked column j Cern.Jet.Math.Mult multFunction = Cern.Jet.Math.Mult.CreateInstance(0); // Outer loop. for (int j = 0; j < n; j++) { // blocking (make copy of j-th column to localize references) LUcolj.Assign(LU.ViewColumn(j)); // sparsity detection int maxCardinality = m / CUT_OFF; // == heuristic depending on speedup LUcolj.GetNonZeros(nonZeroIndexes, null, maxCardinality); int cardinality = nonZeroIndexes.Count; Boolean sparse = (cardinality < maxCardinality); // Apply previous transformations. for (int i = 0; i < m; i++) { int kmax = System.Math.Min(i, j); double s; if (sparse) { s = LUrows[i].ZDotProduct(LUcolj, 0, kmax, nonZeroIndexes); } else { s = LUrows[i].ZDotProduct(LUcolj, 0, kmax); } double before = LUcolj[i]; double after = before - s; LUcolj[i] = after; // LUcolj is a copy LU[i, j] = after; // this is the original if (sparse) { if (before == 0 && after != 0) { // nasty bug fixed! int pos = nonZeroIndexes.BinarySearch(i); pos = -pos - 1; nonZeroIndexes.Insert(pos, i); } if (before != 0 && after == 0) { nonZeroIndexes.Remove(nonZeroIndexes.BinarySearch(i)); } } } // Find pivot and exchange if necessary. int p = j; if (p < m) { double max = System.Math.Abs(LUcolj[p]); for (int i = j + 1; i < m; i++) { double v = System.Math.Abs(LUcolj[i]); if (v > max) { p = i; max = v; } } } if (p != j) { LUrows[p].Swap(LUrows[j]); int k = piv[p]; piv[p] = piv[j]; piv[j] = k; pivsign = -pivsign; } // Compute multipliers. double jj; if (j < m && (jj = LU[j, j]) != 0.0) { multFunction.Multiplicator = 1 / jj; LU.ViewColumn(j).ViewPart(j + 1, m - (j + 1)).Assign(multFunction); } } LU = LU; }