public static double PowerMethod(this Matrix <double> matrix) { var lambda = new DyamicList <double>(double.NaN); Console.WriteLine(matrix); var eigenVector = (Vector <double>)DenseVector.Create(matrix.ColumnCount, i => (i) + 3); var index = -1; do { index++; eigenVector = matrix * eigenVector; lambda[index] = eigenVector.HighestValue(); eigenVector /= lambda[index]; } while(lambda[index].Diff(lambda[index - 1]) > Error); Console.WriteLine(lambda.ToString("lambda", true)); Console.WriteLine(eigenVector); string.Format("Result: {0}", Math.Round(lambda[index], 5)).Print(); return(Math.Round(lambda[index], 5)); }
public static DyamicList <double> GaussElimination(this Matrix <double> m) { var equations = DenseMatrix.OfMatrix(m); var x = new DyamicList <double>(double.NaN); Console.WriteLine(equations); var numZeros = 0; for (var row = 0; row < equations.RowCount; row++) { for (var zeros = 0; zeros < numZeros; zeros++) { var topVector = equations.Row(row - (numZeros - zeros)); var currentVector = equations.Row(row); var top = topVector.At(zeros); var bot = currentVector.At(zeros); var normal = bot / top; currentVector -= topVector * normal; equations.SetRow(row, currentVector); } numZeros++; } equations.Round(); Console.WriteLine(equations); for (var row = equations.RowCount - 1; row >= 0; row--) { var rowVector = equations.Row(row); var value = rowVector.At(rowVector.Count - 1); for (var xNum = rowVector.Count - 2; xNum >= row; xNum--) { if (x[xNum].Equals(double.NaN)) { value /= rowVector.At(xNum); break; } value -= rowVector.At(xNum) * x[xNum]; } x[row] = value; } Console.WriteLine("Result: "); Console.WriteLine(x.ToString("x", true)); return(x); }
public static DyamicList <T> Create(T[] array, T defaultReturn) { var list = new DyamicList <T>(defaultReturn); for (var i = 0; i < array.Length; i++) { list[i] = array[i]; } return(list); }
public static double RombergTrapezoid(this Lookup expression, Expression low, Expression high, List <Expression> hList, bool convert = false) { var ts = new DyamicList <DyamicList <Expression> >(); for (var i = 0; i < hList.Count; i++) { ts[i] = new DyamicList <Expression>(); } var round = 0; Console.WriteLine("----------------------- " + round); { var index = 0; foreach (var ex in hList) { var i = convert ? ((high - low) / ex).ToReal() : ex.ToReal(); var value = expression.RombergTrapezoidPart(low, high, (int)i).P(); ("T" + index + "0: Trapezoid(n=" + i + ") = " + value.ToReal()).Print(); ts[round][index++] = value; } } for (var i = 1; i < hList.Count; i++) { round++; Console.WriteLine("----------------------- " + round); for (var j = 0; j < hList.Count - round; j++) { var constant = Expression.Pow(4, i); var t = ts[i - 1]; var value = (constant * t[j + 1] - t[j]) / (constant - 1); string.Format("T{3}{4}: ({0}({1}) - {2}) / ({0} - 1) = {5}", constant.ToReal(), t[j + 1].ToReal(), t[j].ToReal(), i, j, value.ToReal()).Print(); ts[i][j] = value; } } Console.WriteLine("----------------------- End"); ts[hList.Count - 1][0].ToReal().Print(); return(ts[hList.Count - 1][0].ToReal()); }
public static DyamicList <double> GaussJordanElimination(this Matrix <double> m) { var equations = DenseMatrix.OfMatrix(m); Console.WriteLine(equations); var numZeros = 0; for (var row = 0; row < equations.RowCount; row++) { for (var zeros = 0; zeros < numZeros; zeros++) { var topVector = equations.Row(row - (numZeros - zeros)); var currentVector = equations.Row(row); var top = topVector.At(zeros); var bot = currentVector.At(zeros); var normal = bot / top; currentVector -= topVector * normal; equations.SetRow(row, currentVector); } numZeros++; } equations.Round(); Console.WriteLine(equations); var lastRow = equations.RowCount - 1; var lastCol = equations.ColumnCount - 1; for (var row = lastRow; row >= 0; row--) { var rowVector = equations.Row(row); for (var xNum = lastCol - 1; xNum >= row; xNum--) { if (xNum == row) { rowVector /= rowVector.At(xNum); continue; } var helperVector = equations.Row(xNum); var normal = rowVector.At(xNum); rowVector -= helperVector * normal; } equations.SetRow(row, rowVector); } equations.Round(); var values = equations.Column(lastCol).ToArray(); var x = DyamicList <double> .Create(values, double.NaN); Console.WriteLine("Result: "); Console.WriteLine(x.ToString("x", true)); return(x); }
public static Matrix Doolittle(this Matrix a, Matrix b) { Console.WriteLine("a:"); Console.WriteLine(a); Console.WriteLine("b:"); Console.WriteLine(b); var rows = a.RowCount; var cols = a.ColumnCount; Matrix <double> l = DenseMatrix.Create(rows, cols, double.NaN); l.SetUpperTriangle(0); l.SetDiagonal(DenseVector.Create(rows, 1)); Matrix <double> u = DenseMatrix.Create(rows, cols, double.NaN); u.SetLowerTriangle(0); // first row is known u.SetRow(0, a.Row(0)); for (var row = 1; row < rows; row++) { for (var col = 0; col < cols; col++) { if (double.IsNaN(l[row, col])) { l[row, col] = DoolittleMultiplcation(row, col, l, u, a); } else if (double.IsNaN(u[row, col])) { u[row, col] = DoolittleMultiplcation(row, col, l, u, a); } } } Console.WriteLine("l:"); Console.WriteLine(l); Console.WriteLine("u:"); Console.WriteLine(u); Matrix <double> y = DenseMatrix.Create(rows, 1, double.NaN); var yValue = new DyamicList <double>(double.NaN); for (var row = 0; row < rows; row++) { var value = DoolittleMultiplcation(row, 0, l, y, b); yValue[row] = value; y[row, 0] = value; } Console.WriteLine(yValue.ToString("y")); Matrix <double> x = DenseMatrix.Create(rows, 1, double.NaN); var xValue = new DyamicList <double>(double.NaN); for (var row = rows - 1; row >= 0; row--) { var valueType = DoolittleMultiplcation(row, 0, u, x, y); x[row, 0] = valueType; xValue[row] = valueType; } Console.WriteLine(xValue.ToString("x")); return(null); }
public static double Muller(this Func equation, double a, double b) { var x = new DyamicList <double>(double.NaN); x[0] = a; x[1] = (a + b) / 2; x[2] = b; var f = new DyamicList <double>(double.NaN); f[0] = equation.Invoke(x[0]); f[1] = equation.Invoke(x[1]); f[2] = equation.Invoke(x[2]); var d = new DyamicList <double>(double.NaN); d[0] = x[1] - x[0]; d[1] = x[2] - x[1]; var g = new DyamicList <double>(double.NaN); g[0] = (f[1] - f[0]) / d[0]; g[1] = (f[2] - f[1]) / d[1]; var i = 0; var h = new DyamicList <double>(double.NaN); var c = new DyamicList <double>(double.NaN); while (true) { h[i] = (g[i + 1] - g[i]) / (d[i + 1] + d[i]); c[i] = g[i + 1] + (d[i + 1] * h[i]); var sqr = Math.Pow(c[i], 2) - (4 * f[i + 2] * h[i]); if (sqr < 0) { Console.WriteLine("Cannot converge with interval."); return(Double.NaN); } d[i + 2] = (-2 * f[i + 2]) / (c[i] + (Math.Sign(c[i]) * Math.Sqrt(sqr))); x[i + 3] = x[i + 2] + d[i + 2]; f[i + 3] = equation.Invoke(x[i + 3]); if (Equals(f[i + 3], 0d)) { f[i + 3] = 0.0000000000000001; } g[i + 2] = (f[i + 3] - f[i + 2]) / (d[i + 2]); Console.WriteLine(x[i + 1]); if (x[i + 1].Diff(x[i]) < Error) { break; } i++; } Console.WriteLine(x.ToString("x", true)); Console.WriteLine(d.ToString("d", true)); Console.WriteLine(c.ToString("c", true)); Console.WriteLine(g.ToString("g", true)); Console.WriteLine(h.ToString("h", true)); Console.WriteLine("Result: " + x[i + 1]); return(x[i + 1]); }
protected bool Equals(DyamicList <T> other) { return(Equals(Backend, other.Backend)); }