private void button8_Click(object sender, EventArgs e) { if (this.uC_Line1.IsValid()) { var a = this.uC_Line1.A; var b = this.uC_Line1.B; var c = this.uC_Line1.C; //判断方程的参数,是否有效 LinearEquation linear = new LinearEquation() { A = a, B = b, C = c }; if (!linear.IsValid()) { MessageBox.Show("输入的方程参数无效"); return; } if (!this.axisControl1.CheckLineIsValid(linear)) { MessageBox.Show("输入的方程不在坐标轴内"); return; } List <PointF> lstPoints = this.axisControl1.GetLinerPointsFromLinearEquation(linear); foreach (var ele in lstPoints) { axisControl1.GeneratePoint(ele); } } }
public void InitializationString() { string coefficient = "1, 2, 3, 4,5, 5.5, 12"; LinearEquation a1 = new LinearEquation(coefficient); Assert.AreEqual(12, a1[6]); }
public void InitializationZero() { int n = 6; LinearEquation a = new LinearEquation(n); Assert.AreEqual("0x1+0x2+0x3+0x4+0x5+0x6=0", a.ToString()); }
public void Recalculate(List <double> initXs, List <double> initYs, int exp) { var a = new double[exp, exp]; var b = new double[exp]; this.exp = exp; for (var j = 0; j < exp; j++) { var f = 0.0; for (var i = 0; i < initXs.Count; i++) { f += initYs[i] * Math.Pow(initXs[i], j); } b[j] = f; for (var k = 0; k < exp; k++) { var c = 0.0; for (var i = 0; i < initXs.Count; i++) { c += Math.Pow(initXs[i], k + j); } a[j, k] = c; } } eq = new LinearEquation(a, b); eq.Gauss(); }
public void CorrectIndexing() { string coefficient = "1, 2, 3, 4, 5"; LinearEquation a = new LinearEquation(coefficient); Assert.AreEqual(4, a[3]); }
public void IndexingChecking() { string listofcoef = "1,3,5,7,9"; LinearEquation a = new LinearEquation(listofcoef); Assert.AreEqual(9, a[4]); }
static void Main() { while (true) { int n; Random rnd = new Random(); do { Console.Write("Enter N: "); }while (!int.TryParse(Console.ReadLine(), out n) || n < 1); LinearEquation[] array = new LinearEquation[n]; for (int i = 0; i < n; i++) { array[i] = new LinearEquation( rnd.NextDouble() * 20 - 10, rnd.NextDouble() * 20 - 10, rnd.NextDouble() * 20 - 10); } Array.Sort(array); Array.ForEach(array, x => Console.WriteLine(x)); Console.WriteLine(); Console.WriteLine("Press Esc to exit or any other key to continue..."); Console.WriteLine(); ConsoleKeyInfo key = Console.ReadKey(); if (key.Key == ConsoleKey.Escape) { break; } } }
public void FailWithNegativeArgument() { string listofcoef = "1,3,5,7,9"; var a = new LinearEquation(listofcoef); Assert.Equals(typeof(ArgumentException), new LinearEquation(-5)); }
public void FailWithWrongIndexing2() { string listofcoef = "1,3,5,7,9"; var a = new LinearEquation(listofcoef); Assert.Equals(typeof(ArgumentOutOfRangeException), a[5]); }
public static Vector2f MovementLeft(Vector2f _currentMovementVec, Dictionary <int, Vector2f> _movingObjVerts, Dictionary <int, Vector2f> _stationaryObjVerts) { Vector2f returnVec = _currentMovementVec; var movingObjVertsSorted = from entry in _movingObjVerts orderby entry.Value.X ascending select entry; var stationaryObjVertsSorted = from entry in _stationaryObjVerts orderby entry.Value.X descending select entry; //Console.WriteLine(string.Format("{0} {1} {2} {3}", movingObjVertsSorted.ToList()[0], movingObjVertsSorted.ToList()[1], movingObjVertsSorted.ToList()[2], movingObjVertsSorted.ToList()[3])); LinearEquation movingObjEqu = TackMath.GetLinearEquationFromPoints(movingObjVertsSorted.ToList()[0].Value, movingObjVertsSorted.ToList()[1].Value); LinearEquation stationaryObjEqu = TackMath.GetLinearEquationFromPoints(stationaryObjVertsSorted.ToList()[0].Value, stationaryObjVertsSorted.ToList()[1].Value); Console.WriteLine("vert: " + movingObjVertsSorted.ToList()[0].Value); Console.WriteLine(string.Format("{0} | {1}", movingObjEqu.ToString(), stationaryObjEqu.ToString())); if (TackMath.GetLinearIntersectionPoint(movingObjEqu, stationaryObjEqu, out Vector2f interestionPoint)) { Console.WriteLine("1 yes"); if (interestionPoint.X <= stationaryObjVertsSorted.ToList()[0].Value.X && interestionPoint.X >= stationaryObjVertsSorted.ToList()[1].Value.X) { Console.WriteLine("2 yes"); if (interestionPoint.Y <= stationaryObjVertsSorted.ToList()[0].Value.Y && interestionPoint.Y >= stationaryObjVertsSorted.ToList()[1].Value.Y) { Console.WriteLine("Intersection has been achieved: " + interestionPoint.ToString()); } } } Console.WriteLine("Point: " + interestionPoint.ToString()); return(returnVec); }
private void button3_Click(object sender, EventArgs e) { if (this.uC_Line1.IsValid()) { var a = this.uC_Line1.A; var b = this.uC_Line1.B; var c = this.uC_Line1.C; //判断方程的参数,是否有效 LinearEquation linear = new LinearEquation() { A = a, B = b, C = c }; if (!linear.IsValid()) { MessageBox.Show("输入的方程参数无效"); return; } if (!this.axisControl1.CheckLineIsValid(linear)) { MessageBox.Show("输入的方程不在坐标轴内"); return; } bool flag = this.axisControl1.GenerateLinear(linear); if (!flag) { MessageBox.Show("生成直线失败"); return; } } }
public void Mult2()//умножение. число слева { string coeff = "4,6,2,78.9,23"; LinearEquation a = new LinearEquation(coeff); string res = "12,18,6,236.7,69"; Assert.AreEqual(new LinearEquation(res), 3 * a); }
public void InitializationString() { string coeff = "5,6,7,8,9,16.5,45.9"; LinearEquation a1 = new LinearEquation(coeff); Assert.AreEqual(16.5, a1[5]); }
public void ListConstructorWithEmptyCollection() { double[] arr = new double[] { }; LinearEquation le = new LinearEquation(arr); Assert.IsTrue(new double[] { 0, 0 }.SequenceEqual((double[])le)); }
public void ListConstructorWithOneElement() { double[] arr = new double[] { 5 }; LinearEquation le = new LinearEquation(arr); Assert.IsTrue(new double[] { 5, 0 }.SequenceEqual((double[])le)); }
public void StringConstructorWithEmptyString() { string s = ""; LinearEquation le = new LinearEquation(s); Assert.IsTrue(new double[] { 0, 0 }.SequenceEqual((double[])le)); }
public void StringConstructorWithOneElement() { string s = "2"; LinearEquation le = new LinearEquation(s); Assert.IsTrue(new double[] { 2, 0 }.SequenceEqual((double[])le)); }
public void TestSolveForY() { var testStdDev = 2.5D; var testSubject = new RLinearEquation(0.1056, -181.45) { StdDev = testStdDev }; var testCtrl = new LinearEquation(0.1056, -181.45); for (var i = 16; i < 64; i++) { var x = DateTime.Today.AddYears(-1 * i).ToDouble(); var testAvg = testCtrl.SolveForY(x); var testResult = testSubject.SolveForY(DateTime.Today.AddYears(-1 * i).ToDouble()); var expectedFarLeft = (testAvg - testStdDev * 3.25); var expectedFarRight = (testAvg + testStdDev * 3.25); var tr = expectedFarLeft <= testResult && testResult <= expectedFarRight; if (!tr) { Console.WriteLine($"{expectedFarLeft} <= {testResult} <= {expectedFarRight}"); } Assert.IsTrue(tr); } }
public void FillByRandomWithMinMoreThanMaxArguments() { int n = 3; LinearEquation le = new LinearEquation(n); Assert.ThrowsException <ArgumentException>(() => le.FillByRandom(100, 10)); }
void Update() { float x = 0; float y = 0; float z = 0; if (xAxisEquation.equationType == Equation.EquationType.SQUARE) { ExponentialEquation eq = (ExponentialEquation)xAxisEquation; x = (float)(eq.outerCoefficient * Math.Pow(time * eq.innerCoefficient, eq.exponent)); } else if (xAxisEquation.equationType == Equation.EquationType.SIN) { SineEquation eq = (SineEquation)xAxisEquation; x = (float)(eq.outerCoefficient * Math.Sin(time * eq.innerCoefficient)); } else if (xAxisEquation.equationType == Equation.EquationType.LINEAR) { LinearEquation eq = (LinearEquation)xAxisEquation; x = (float)(eq.coefficient * time); } if (yAxisEquation.equationType == Equation.EquationType.SQUARE) { ExponentialEquation eq = (ExponentialEquation)yAxisEquation; y = (float)(eq.outerCoefficient * Math.Pow(time * eq.innerCoefficient, eq.exponent)); } else if (yAxisEquation.equationType == Equation.EquationType.SIN) { SineEquation eq = (SineEquation)yAxisEquation; y = (float)(eq.outerCoefficient * Math.Sin(time * eq.innerCoefficient)); } else if (yAxisEquation.equationType == Equation.EquationType.LINEAR) { LinearEquation eq = (LinearEquation)yAxisEquation; y = (float)(eq.coefficient * time); } if (zAxisEquation.equationType == Equation.EquationType.SQUARE) { ExponentialEquation eq = (ExponentialEquation)zAxisEquation; z = (float)(eq.outerCoefficient * Math.Pow(time * eq.innerCoefficient, eq.exponent)); } else if (zAxisEquation.equationType == Equation.EquationType.SIN) { SineEquation eq = (SineEquation)zAxisEquation; z = (float)(eq.outerCoefficient * Math.Sin(time * eq.innerCoefficient)); } else if (zAxisEquation.equationType == Equation.EquationType.LINEAR) { LinearEquation eq = (LinearEquation)zAxisEquation; z = (float)(eq.coefficient * time); } transform.position = (transform.rotation * new Vector3((float)x, (float)y, (float)z)) + new Vector3(initX, initY, initZ); if (time > lifetime) { Destroy(this.gameObject); } time += Time.deltaTime; }
public void FillByDuplicates() { LinearEquation le = new LinearEquation(5); le.FillByDuplicates(4); Assert.IsTrue(new double[] { 4, 4, 4, 4, 4 }.SequenceEqual((double[])le)); }
public void Equal1()//operator false { string res = "0,0,0,6"; LinearEquation a = new LinearEquation(res); bool check = (a) ? true : false; Assert.AreEqual(false, check); }
public IList <double> Solve() //Returns a list of coefficients for the variables in the same order they were entered { solution = new double[rows[0].Coefficients.Count()]; for (int pivotM = 0; pivotM < rows.Count() - 1; pivotM++) { int pivotN = rows[pivotM].IndexOfFirstNonZero; for (int i = pivotN + 1; i < rows.Count(); i++) { LinearEquation rowToReduce = rows[i]; double pivotFactor = rowToReduce[pivotN] / -rows[pivotM][pivotN]; rowToReduce.AddCoefficients(rows[pivotM], pivotFactor); } } while (rows.Any(r => r.Result != 0)) { LinearEquation row = rows.FirstOrDefault(r => r.NonZeroCount == 1); if (row == null) { break; } int solvedIndex = row.IndexOfFirstNonZero; double newSolution = row.Result / row[solvedIndex]; AddToSolution(solvedIndex, newSolution); } return(solution); }
public void SameIn()//одинаковые значения { LinearEquation a = new LinearEquation(4); a.SameIn(15.2); string res = "15.2,15.2,15.2,15.2,15.2"; Assert.AreEqual(new LinearEquation(res), a); }
public void SetAndGet() { SystemOfLinearEquation sole = new SystemOfLinearEquation(2); sole[0] = new LinearEquation(new double[] { 1, 2, 3 }); Assert.AreEqual("3x2+2x1+1=0", sole[0].ToString()); }
public void Equal2()//operator true { string res = "0,2,0,15.2"; LinearEquation a = new LinearEquation(res); bool check = (a) ? true : false; Assert.AreEqual(true, check); }
public void StringConstructor() { string s = "1 2,3 4 6,2 5,28 0 1"; LinearEquation le = new LinearEquation(s); Assert.IsTrue(new double[] { 1, 2.3, 4, 6.2, 5.28, 0, 1 }.SequenceEqual((double[])le)); }
public void InitializationWithString() { string k = "1 2 3"; LinearEquation a = new LinearEquation(k); Assert.AreEqual(a[0], 2); }
public void IntConstructorWithOne() { int n = 1; LinearEquation le = new LinearEquation(n); Assert.IsTrue(new double[] { 0, 0 }.SequenceEqual((double[])le)); }
public void StringConstructor() { string s = "1 1,5 2,6 -4 89 13,4"; LinearEquation le = new LinearEquation(s); Assert.IsTrue(new double[] { 1, 1.5, 2.6, -4, 89, 13.4 }.SequenceEqual((double[])le)); }
public LinearEquationControl(int variableCount) { InitializeComponent(); char[] chars = new char[] { 'a', 'b', 'c', 'd', 'e' }; equation = new LinearEquation(); coefficients = new double[variableCount]; int x = 10; for (int i = 0; i < variableCount; i++) { int index = i; NumericUpDown nud = new NumericUpDown(); nud.TextAlign = HorizontalAlignment.Right; nud.Width = 70; nud.Minimum = decimal.MinValue; nud.Maximum = decimal.MaxValue; Controls.Add(nud); nud.ValueChanged += (e, sender) => coefficients[index] = (double)(e as NumericUpDown).Value; if (i > 0) { Label lbl = new Label(); lbl.Text = chars[i - 1] + " +"; lbl.AutoSize = false; lbl.Size = new Size(30, nud.Height); lbl.Location = new Point(x, 5); lbl.TextAlign = ContentAlignment.MiddleCenter; Controls.Add(lbl); x += 32; } nud.Location = new Point(x, 5); x += 72; } NumericUpDown nudConstant = new NumericUpDown(); nudConstant.Width = 70; nudConstant.TextAlign = HorizontalAlignment.Right; nudConstant.Minimum = decimal.MinValue; nudConstant.Maximum = decimal.MaxValue; Controls.Add(nudConstant); Label lblEquals = new Label(); lblEquals.Text = chars[variableCount - 1] + " ="; lblEquals.AutoSize = false; lblEquals.Size = new Size(30, nudConstant.Height); lblEquals.Location = new Point(x, 5); lblEquals.TextAlign = ContentAlignment.MiddleCenter; Controls.Add(lblEquals); x += 32; nudConstant.Location = new Point(x, 5); nudConstant.ValueChanged += (e, sender) => equation.Constant = (double)nudConstant.Value; Size = new Size(nudConstant.Location.X + nudConstant.Width + 10, nudConstant.Height + 10); }
/// <summary> /// Parse a string in the format of [intercept],[slope] /// where both intercept and slope may be parsed to doubles /// and are joined by a comma. /// </summary> /// <param name="csv"></param> /// <param name="lq"></param> /// <returns></returns> public static bool TryParse(string csv, out LinearEquation lq) { lq = null; if (string.IsNullOrWhiteSpace(csv) || !csv.Contains(",")) return false; var interceptStr = csv.Split(',')[0]; var slopeStr = csv.Split(',')[1]; double intercept; double slope; if (double.TryParse(interceptStr, out intercept) && double.TryParse(slopeStr, out slope)) { lq = new LinearEquation { Intercept = intercept, Slope = slope }; } return lq != null; }