public void ExponentationTest() { var m1 = new BaseMeasure("a"); Measures.Instance.Add(m1); var u = new BaseUnit(m1, 1, "s", "pikkus"); Units.Instance.Add(u); var m = u.Exponentiation(4); var s = u.Exponentiation(1); var c = u.Exponentiation(0); var a = u.Exponentiation(-4); Assert.AreEqual("a^4", m.Measure.Name); Assert.AreEqual("s^4", m.Formula()); Assert.AreEqual("s", s.Formula()); Assert.AreEqual("s^-4", a.Formula()); }
public ActionResult Result(ComplexMathViewModel quantity) { var q1 = quantity.FirstQuantity; var q2 = quantity.SecondQuantity; var baseMeasure = BaseMeasure.Random(); Measures.Instance.Add(baseMeasure); var derivedMeasure = DerivedMeasure.Random(); Measures.Instance.Add(derivedMeasure); int i = 0; List <DerivedUnit> derivedUnits = new List <DerivedUnit>(); foreach (string str in quantity.UnitList) { var baseUnit = new BaseUnit(baseMeasure, Unit.SetFactor(str), str, str); Units.Instance.Add(baseUnit); var derivedUnit = baseUnit.Exponentiation(quantity.PowerList[i]) as DerivedUnit; derivedUnit.Measure = derivedMeasure; Units.Instance.Add(derivedUnit); derivedUnits.Add(derivedUnit); i++; if (i == quantity.NumberOfUnits) { break; } } for (int j = 0; j < quantity.NumberOfUnits - 1; j++) { var a = derivedUnits[j].Multiply(derivedUnits[j + 1]) as DerivedUnit; Units.Instance.Add(a); derivedUnits[j + 1] = a; if (j == quantity.NumberOfUnits - 2) { q1.Unit = a.Name; q2.Unit = a.Name; } } quantity = SetResults(quantity, q1, q2); return(View("Index", quantity)); }
public ActionResult Result(FormulaeViewModel formulae) { DerivedMeasure derivedMeasure; var m1 = new BaseMeasure(formulae.MeasureList[0]); var m2 = new BaseMeasure(formulae.MeasureList[1]); Measures.Instance.Add(m1); Measures.Instance.Add(m2); if (formulae.SelectedOperation == "Divide") { derivedMeasure = m1.Divide(m2) as DerivedMeasure; } else { derivedMeasure = m1.Multiply(m2) as DerivedMeasure; } Measures.Instance.Add(derivedMeasure); int i = 0; List <DerivedUnit> derivedUnits = new List <DerivedUnit>(); foreach (string str in formulae.UnitList1) { var baseUnit = new BaseUnit(m1, Unit.SetFactor(str), str, str); Units.Instance.Add(baseUnit); var derivedUnit = baseUnit.Exponentiation(formulae.PowerList1[i]) as DerivedUnit; derivedUnit.Measure = derivedMeasure; Units.Instance.Add(derivedUnit); derivedUnits.Add(derivedUnit); i++; if (i == formulae.NumberOfUnits1) { i = 0; break; } } foreach (string str in formulae.UnitList2) { var baseUnit = new BaseUnit(m2, Unit.SetFactor(str), str, str); Units.Instance.Add(baseUnit); DerivedUnit derivedUnit; if (formulae.SelectedOperation == "Divide") { derivedUnit = baseUnit.Exponentiation(-formulae.PowerList2[i]) as DerivedUnit; } else { derivedUnit = baseUnit.Exponentiation(formulae.PowerList2[i]) as DerivedUnit; } derivedUnit.Measure = derivedMeasure; Units.Instance.Add(derivedUnit); derivedUnits.Add(derivedUnit); i++; if (i == formulae.NumberOfUnits2) { break; } } for (int j = 0; j < formulae.NumberOfUnits1 + formulae.NumberOfUnits2 - 1; j++) { var a = derivedUnits[j].Multiply(derivedUnits[j + 1]) as DerivedUnit; Units.Instance.Add(a); derivedUnits[j + 1] = a; if (a.Name == null) { a.Name = "1"; } if (j == formulae.NumberOfUnits1 + formulae.NumberOfUnits2 - 2) { formulae.Result = "Unit: " + a.Name + " Measure: " + derivedMeasure.Name ?? derivedMeasure.Terms[0].MeasureId; } } return(View("Index", formulae)); }