Esempio n. 1
0
        public void CustomFunctionOneVar()
        {
            UncertainNumber x = new UncertainNumber(5, 2);
            x= x.CustomFunction(d => d * d);

            Assert.AreEqual((int)x.Value, 25);

            // NOTE: 20.0000000000002 is too accurate, should be simplified to just 15
            Assert.AreEqual(x.Uncertainty, (decimal)20.0000000000002);
        }
Esempio n. 2
0
        /// <summary>
        /// Formats values according to the precision of the uncertainty. The uncertainty is returned with one significant figure.
        /// </summary>
        /// <remarks>This method does not affect the current object but returns a new object with the new formating.</remarks>
        public static UncertainNumber[] AutoFormat(UncertainNumber[] array)
        {
            var output = new UncertainNumber[array.Length];

            for (var i = 0; i < array.Length; i++)
            {
                output[i] = array[i].AutoFormat();
            }

            return(output);
        }
Esempio n. 3
0
        public void CustomFunctionMultipleVar()
        {
            var x = new UncertainNumber(5, 2);
            var y = new UncertainNumber(5, 1);
            var num = UncertainNumber.CustomFunction(d => d[0]*d[1], x, y);
            
            Assert.AreEqual((int) num.Value, 25);

            // NOTE: 15.0000000000001 is too accurate, should be simplified to just 15
            Assert.AreEqual(num.Uncertainty, (decimal) 15.0000000000001);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns the value obtained by a custom function. Reads values from TSV.
        /// </summary>
        /// <param name="function">The custom function</param>
        /// <param name="tsv">The string that contains TSV data</param>
        /// <returns></returns>
        public static UncertainNumber[] CustomFunction(Func <decimal[], decimal> function, string tsv)
        {
            using (var reader = new System.IO.StringReader(tsv))
            {
                string line;
                var    final = new List <UncertainNumber>();

                while ((line = reader.ReadLine()) != null)
                {
                    var a    = line.Split((char)9);
                    var temp = new UncertainNumber[a.Length / 2];

                    for (var i = 0; i < a.Length; i += 2)
                    {
                        temp[i] = new UncertainNumber(Convert.ToDecimal(a[i]), Convert.ToDecimal(a[i + 1]));
                    }

                    final.Add(CustomFunction(function, temp));
                }

                return(final.ToArray());
            }
        }
Esempio n. 5
0
 /// <summary>
 /// Checks whether the uncertain type is equal to another.
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 protected bool Equals(UncertainNumber other)
 {
     return Value == other.Value && Uncertainty == other.Uncertainty;
 }
Esempio n. 6
0
        /// <summary>
        /// Returns the value obtained by a custom function. Reads values from TSV.
        /// </summary>
        /// <param name="function">The custom function</param>
        /// <param name="tsv">The string that contains TSV data</param>
        /// <returns></returns>
        public static UncertainNumber[] CustomFunction(Func<decimal[],decimal> function, string tsv)
        {
            using (var reader = new System.IO.StringReader(tsv))
            {
                string line;
                var final = new List<UncertainNumber>();
                
                while ((line = reader.ReadLine()) != null)
                {
                    var a = line.Split((char)9);
                    var temp = new UncertainNumber[a.Length/2];

                    for (var i = 0; i < a.Length; i += 2)
                        temp[i] = new UncertainNumber(Convert.ToDecimal(a[i]), Convert.ToDecimal(a[i + 1]));

                    final.Add(CustomFunction(function,temp));
                }

                return final.ToArray();
            }   
        }
Esempio n. 7
0
        /// <summary>
        /// Formats values according to the precision of the uncertainty. The uncertainty is returned with one significant figure.
        /// </summary>
        /// <remarks>This method does not affect the current object but returns a new object with the new formating.</remarks>
        public static UncertainNumber[] AutoFormat(UncertainNumber[] array)
        {
            var output = new UncertainNumber[array.Length];

            for (var i = 0; i < array.Length; i++)
                output[i] = array[i].AutoFormat();

            return output;
        }
Esempio n. 8
0
 /// <summary>
 /// Converts an UncertainNumber array to a TSV string.
 /// </summary>
 /// <param name="numbers">The array with UncertainNumbers</param>
 /// <returns></returns>
 public static string ConvertArrayToTsvString(UncertainNumber[] numbers)
 {
     return numbers.Aggregate("", (current, t) => current + (t.Value + "\t" + t.Uncertainty + "\n"));
 }
Esempio n. 9
0
        /// <summary>
        /// Converts an UncertainNumber array to a decimal array with the values only (uncertainties are not included). 
        /// </summary>
        /// <param name="numbers">The array with UncertainNumbers</param>
        /// <returns></returns>
        public static decimal[] ConvertValuesToDecimalArray (UncertainNumber[] numbers)
        {
            var ret = new decimal[numbers.Length];

            for (var i = 0; i < numbers.Length; i++)
                ret[i] = numbers[i].Value;

            return ret;
        }
Esempio n. 10
0
 /// <summary>
 /// Checks whether the uncertain type is equal to another.
 /// </summary>
 /// <param name="other"></param>
 /// <returns></returns>
 protected bool Equals(UncertainNumber other)
 {
     return(Value == other.Value && Uncertainty == other.Uncertainty);
 }