public static PolinomRoot QuadraticRoots(this Polinom polinom) { if (polinom.Power != 2) { throw new InvalidOperationException("Polinom power must be 2"); } var roots = new PolinomRoot(polinom); var a = polinom.A; var b = polinom.B; var c = polinom.C; var d2 = b * b - 4 * a * c; if (d2 < 0) { return(roots); } var d = Math.Sqrt(d2); var x1 = (-b - d) / (2 * a); var x2 = (-b + d) / (2 * a); roots.AddRoot(x1); roots.AddRoot(x2); return(roots); }
public void Oduzmi(Polinom umanjilac) { foreach (ClanPolinoma clan in umanjilac.clanovi) { this.Oduzmi(clan); } }
public static PolinomDiv Divide(this Polinom polinom, Polinom divider) { var power = polinom.Power - divider.Power; var result = new Polinom(); var reminder = polinom; (power + 1).ForEachNum(i => { if (reminder.Power + i == polinom.Power) { var c = reminder.Root / divider.Root; var subPolinom = (c * divider).PowerShiftUp(power - i); reminder = reminder - subPolinom; result.PowerUp(c); } else { result.PowerUp(0); } }); return(new PolinomDiv() { Result = result, Remainder = reminder }); }
public void Generate(int minutes) { var timerThread = new Thread(() => MyTimer(minutes)); timerThread.Start(); int membersCount = new Random().Next(0, 11); string result; var polinom = new Polinom(); polinom.items = new List <PolinomItem>(); polinom.freeConstant = new Random().Next(-200, 201); for (int i = 0; i <= membersCount; i++) { Random randConstant = new Random(DateTime.Now.Millisecond + i); Random randDegree = new Random(DateTime.Now.Millisecond + 1 + i); var polinomItem = new PolinomItem( randConstant.Next(-10, 11), randDegree.Next(0, 11) ); polinom.items.Add(polinomItem); } result = "Generated polinom: " + polinom.ToString(); timerThread.Join(); _data.SaveAsTxt(polinom); }
public void TestMethod1() { // arrange Element elem11 = new Element(2, 4); Element elem12 = new Element(3, 4); Element elem21 = new Element(3, 2); Element elem22 = new Element(5, 6); // act Polinom polinom1 = new Polinom(); polinom1.Add(elem11); polinom1.Add(elem12); Polinom polinom2 = new Polinom(); polinom2.Add(elem21); polinom2.Add(elem22); Polinom polinom3 = new Polinom(); polinom3 = Program.Multiplication(polinom1, polinom2, polinom3); bool ok = Program.PolinomNothingCheck(polinom1, polinom2); polinom3.Sorting(); // assert Assert.AreEqual(polinom3.Count, 4); }
public static async void calculateTables(List <Polinom> columns, Order order, Polinom IrreduciblePolinom, Action <Polinom, int, int> summationCallback, Action <Polinom, int, int> multiplicationCallback, Action finalCallback) { int calcSize = (int)Math.Pow(order.Mantissa, order.Exponent); for (int i = 0; i < calcSize; i++) { Polinom rowPolinom = columns[i]; for (int j = 0; j < calcSize; j++) { Polinom colPolinom = columns[j]; Polinom summationRes = await Task.Run <Polinom>(() => { return(Polinom.calcPolinomToZp((rowPolinom + colPolinom) % IrreduciblePolinom, order.Mantissa)); }); summationCallback(summationRes, i, j); Polinom multiplicationRes = await Task.Run <Polinom>(() => { return(Polinom.calcPolinomToZp((rowPolinom * colPolinom) % IrreduciblePolinom, order.Mantissa)); }); multiplicationCallback(multiplicationRes, i, j); } } finalCallback(); }
public void Dodaj(Polinom sabirak) { foreach (ClanPolinoma clan in sabirak.Clanovi) { this.Dodaj(clan); } }
public List <Polinom> GetDataFromFileStorage() { var polinoms = new List <Polinom>(); foreach (var file in Directory.GetFiles(pathDir)) { var tmpPolinom = new Polinom(); string fileContent = File.ReadAllText(file); if (fileContent.Length == 0) { continue; } var splitedData = fileContent.Split(';').ToList(); tmpPolinom.freeConstant = int.Parse(splitedData.Last()); splitedData.Remove(splitedData.Last()); foreach (var strItem in splitedData) { string[] strPair = strItem.Split('X'); tmpPolinom.items.Add(new PolinomItem(int.Parse(strPair[0]), int.Parse(strPair[1]))); } polinoms.Add(tmpPolinom); } return(polinoms); }
static void Main(string[] args) { string str1; string str2; using (var file = new StreamReader(Path.GetFullPath("Inlet.txt"))) { str1 = file.ReadLine(); str2 = file.ReadLine(); file.Close(); } Polinom p1 = new Polinom(str1); Polinom p2 = new Polinom(str2); Console.WriteLine(str1); Console.WriteLine(str2); Console.WriteLine("Sum: " + (p1 + p2)); Console.WriteLine("Diff: " + (p1 - p2)); Console.WriteLine("Multiply: " + p1 * p2); using (var file = new StreamWriter(Path.GetFullPath("Outlet.out"))) { file.WriteLine(str1); file.WriteLine(str2); file.WriteLine("Sum: " + (p1 + p2)); file.WriteLine("Diff: " + (p1 - p2)); file.Write("Multiply: " + p1 * p2); file.Close(); } }
public static Polinom operator +(Polinom one, Polinom two) { var answ = new Polinom(); Transact(one, answ); Transact(two, answ); return(answ); }
public void SaveAsTxt(Polinom polinom) { if (!new DirectoryInfo(pathDir).Exists) { Directory.CreateDirectory(pathDir); } fileName = getFileName(string.Format("{0}\\{1}", pathDir, fileName)); File.WriteAllText(fileName, prepareFileContent(polinom)); }
public double getEvaluationForPolinom(Polinom polinom, double variable) { double result = polinom.freeConstant; foreach (var item in polinom.items) { result += item.Constant * (Math.Pow(variable, item.Degree)); } return(result); }
private string prepareFileContent(Polinom polinom) { string result = string.Empty; foreach (var item in polinom.items) { result += string.Format("{0};", item.ToString()); } return(result += polinom.freeConstant.ToString()); }
public void Mult_1_2_3_and_4_5_returned_4_13_22_15() { double[] kpol = { 1, 2, 3 }; double[] kpol2 = { 4, 5 }; Polinom pol = new Polinom(2, kpol); Polinom pol2 = new Polinom(1, kpol2); double[] kexpected = { 4, 13, 22, 15 }; Polinom expected = new Polinom(3, kexpected); Assert.AreEqual(expected.ToString(), MultPolinom.Multiplication(pol, pol2).ToString()); }
public void FileReadTest() { var test = new Polinom(new List <Tuple <double, int> > { new Tuple <double, int> (5, 4), new Tuple <double, int> (5, 3), new Tuple <double, int> (1, 2), new Tuple <double, int> (-11, 0) }); Assert.AreEqual(true, test.ToString() == test.ToString()); }
public void IndexTest() { var test = new Polinom(new List <Tuple <double, int> > { new Tuple <double, int> (5, 4), new Tuple <double, int> (5, 3), new Tuple <double, int> (1, 2), new Tuple <double, int> (-11, 0) }); Assert.AreEqual(5, test[1]); }
public void ToStringTest() { var test = new Polinom(new List <Tuple <double, int> > { new Tuple <double, int> (5, 4), new Tuple <double, int> (5, 3), new Tuple <double, int> (1, 2), new Tuple <double, int> (-11, 0) }); Assert.AreEqual("5x^4+5x^3+1x^2-11= 0", test.ToString()); }
public void GornerTest() { var test = new Polinom(new List <Tuple <double, int> > { new Tuple <double, int> (5, 4), new Tuple <double, int> (5, 3), new Tuple <double, int> (1, 2), new Tuple <double, int> (-11, 0) }); Assert.AreEqual(0, test.Value(1)); }
public void ToStringTest_Polinom_String() { int variableX = 3; int[] array = { 1, 2, 3, 4 }; string expectedResult = "The example: 1*3^4 + 2*3^3 + 3*3^2 + 4*3 = 0"; Polinom polinom1 = new Polinom(variableX, array); string actualResult = polinom1.ToString(); Assert.AreEqual(expectedResult, actualResult); }
public void PolinomOperations() { Polinom p1 = new Polinom(new double[] { 17.3, 16.2, 6, 3 }); Polinom p2 = new Polinom(new double[] { 5, -4.9, 13.7, 3, 5 }); Polinom sum = new Polinom(new double[] { 5, 12.4, 29.9, 9, 8 }); Polinom dif = new Polinom(new double[] { -5, 22.2, 2.5, 3, -2 }); Polinom mult = new Polinom(new double[] { 5, 39, 140.1, 202.6, 259.44, 187.63, -3.77, 86.5 }); Assert.IsTrue((p1 + p2) == sum); Assert.IsTrue((p1 - p2) == dif); Assert.IsTrue((p1 * p2) == mult); }
static void WriteFile(string path, Polinom poly) { var writer = new StreamWriter(path); var root = poly.Root; while (root != null) { writer.WriteLine(root.Power + " " + root.Coefficient); root = root.next; } writer.Close(); }
public void GornerTest2() { var test = new Polinom(new List <Tuple <double, int> > { new Tuple <double, int> (1, 4), new Tuple <double, int> (3, 3), new Tuple <double, int> (4, 2), new Tuple <double, int> (-5, 1), new Tuple <double, int> (-47, 0) }); Assert.AreEqual(4, test.Value(-3)); }
public void PolinomDivWithEmptyTest3() { var p1 = new Polinom() { Coefficiencts = new Dictionary <int, double>() }; var p2 = new Polinom() { Coefficiencts = new Dictionary <int, double>() }; Assert.ThrowsException <ArgumentException>(() => p1 / p2); }
public void SubTest_3andArray1and2_SubPolinom() { int variableX = 3; int number = 2; int[] array = { 1, 2, 3, 4 }; int[] expectedArrey = { -1, 0, 1, 2 }; Polinom polinom1 = new Polinom(variableX, array); Polinom expectedPolinom = new Polinom(variableX, expectedArrey); Polinom actualPolinom = polinom1 - number; Assert.AreEqual(expectedPolinom.ToString(), actualPolinom.ToString()); }
public void Pomnozi(Polinom other) //za popraviti celu logiku ... { Polinom tmp; Polinom tmp1 = new Polinom(); foreach (ClanPolinoma clan in other.clanovi) { tmp = new Polinom(); tmp.Dodaj(this); tmp.Pomnozi(clan); tmp1.Dodaj(tmp); } this.clanovi = tmp1.clanovi; }
public void GornerTest3() { var test = new Polinom(new List <Tuple <double, int> > { new Tuple <double, int> (3, 6), new Tuple <double, int> (9, 5), new Tuple <double, int> (-28, 4), new Tuple <double, int> (6, 3), new Tuple <double, int> (-30, 2), new Tuple <double, int> (-30, 1), new Tuple <double, int> (100, 0) }); test.Value(2); Assert.AreEqual(0, test.Value(-5)); }
public void PolinomMultWithEmptyTest3() { var p1 = new Polinom() { Coefficiencts = new Dictionary <int, double>() }; var p2 = new Polinom() { Coefficiencts = new Dictionary <int, double>() }; Assert.AreEqual(new Polinom() { Coefficiencts = new Dictionary <int, double>() }, p1 * p2); }
public static List <Polinom> generateIrreduciblePolinoms(int power, int p) { List <Polinom> tmpList = Polinom.getIrreducible(power, p); List <Polinom> result = new List <Polinom>(); // Use only Given exponent polinoms foreach (Polinom pol in tmpList) { if ((int)Polinom.Sorter(pol).List[0].Exponent == power) { result.Add(pol); } } return(result); }
public void PolinomDivTest5() { var p1 = new Polinom() { Coefficiencts = new Dictionary <int, double> { { 0, 0 }, { 1, 0 }, { 2, 0 } } }; var p2 = new Polinom() { Coefficiencts = new Dictionary <int, double> { { 0, 2 }, { 2, 4 }, { 3, 5 } } }; Assert.ThrowsException <ArgumentException>(() => p1 / p2); }
public static void Transact(Polinom from, Polinom to) { var cur = from.Root; Variable next = null; if (cur != null) { next = cur.next; } to.Add(cur); while (next != null) { cur = next; next = next.next; to.Add(cur); } }