예제 #1
0
		/// <summary>
		/// Implements IClonable
		/// </summary>
		public object Clone()
		{
			AmountVector result = new AmountVector(this.length, this.unit);
			result.values = (decimal?[])this.values.Clone();
			result.amounts = (Amount[])this.amounts.Clone();
			return result;
		}
예제 #2
0
        /// <summary>
        /// Substracts two vectors of equal length and compatible unit.
        /// </summary>
        public static AmountVector operator -(AmountVector left, AmountVector right)
        {
            if (left.length != right.length)
            {
                throw new InvalidOperationException("Can not add or substract AmountVectors of different length.");
            }
            if (left.unit.CompatibleTo(right.unit) == false)
            {
                throw new UnitConversionException("Can not add or substract AmountVectors of incompatible units.");
            }
            AmountVector result = new AmountVector(left.length, left.unit);

            for (int i = 0; i < left.length; i++)
            {
                if (left[i] != null)
                {
                    result[i] = left[i];
                    if (right[i] != null)
                    {
                        result[i] -= right[i];
                    }
                }
                else if (right[i] != null)
                {
                    result[i] = -right[i];
                }
            }
            return(result);
        }
예제 #3
0
        /// <summary>
        /// Divides two vectors of equal length.
        /// </summary>
        public static AmountVector operator /(AmountVector left, AmountVector right)
        {
            if (left.length != right.length)
            {
                throw new InvalidOperationException("Can not multiply or divide AmountVectors of different length.");
            }
            AmountVector result = new AmountVector(left.length, left.unit / right.Unit);

            for (int i = 0; i < left.length; i++)
            {
                if (left[i] != null)
                {
                    if (right[i] != null)
                    {
                        result[i] = new Amount(left.values[i].Value / right.values[i].Value, result.unit);
                    }
                    else
                    {
                        result[i] = null;
                    }
                }
                else
                {
                    result[i] = null;
                }
            }
            return(result);
        }
예제 #4
0
        /// <summary>
        /// Implements IClonable
        /// </summary>
        public object Clone()
        {
            AmountVector result = new AmountVector(this.length, this.unit);

            result.values  = (decimal?[])this.values.Clone();
            result.amounts = (Amount[])this.amounts.Clone();
            return(result);
        }
		public void Sum01Test()
		{
			AmountVector v = new AmountVector(4, LengthUnits.Meter);
			v[0] = new Amount(150m, LengthUnits.Meter);
			v[2] = new Amount(0.2m, LengthUnits.KiloMeter);
			v[3] = new Amount(650m, LengthUnits.Meter);

			Assert.AreEqual(new Amount(1m, LengthUnits.KiloMeter), v.Sum);
		}
예제 #6
0
		/// <summary>
		/// Creates an AmountVector with given length and of given unit,
		/// where all values are zero.
		/// </summary>
		public static AmountVector AllZeros(int length, Unit unit)
		{
			AmountVector v = new AmountVector(length, unit);
			Amount zero = new Amount(0m, unit);
			for (int i = 0; i < length; i++)
			{
				v.values[i] = 0m;
				v.amounts[i] = zero;
			}
			return v;
		}
예제 #7
0
        /// <summary>
        /// Creates an AmountVector with given length and of given unit,
        /// where all values are zero.
        /// </summary>
        public static AmountVector AllZeros(int length, Unit unit)
        {
            AmountVector v    = new AmountVector(length, unit);
            Amount       zero = new Amount(0m, unit);

            for (int i = 0; i < length; i++)
            {
                v.values[i]  = 0m;
                v.amounts[i] = zero;
            }
            return(v);
        }
예제 #8
0
        /// <summary>
        /// Divides each value of a vector with an amount.
        /// </summary>
        public static AmountVector operator /(AmountVector left, Amount right)
        {
            AmountVector result = new AmountVector(left.length, left.unit / right.Unit);

            for (int i = 0; i < left.length; i++)
            {
                if (left[i] != null)
                {
                    result[i] = new Amount(left.values[i].Value / right.Value, result.unit);
                }
            }
            return(result);
        }
예제 #9
0
        /// <summary>
        /// Returns an AmountVector being the current vector with its values rounded
        /// to the given number of decimals.
        /// </summary>
        public AmountVector Round(int decimals)
        {
            AmountVector result = new AmountVector(this.length, this.unit);

            for (int i = 0; i < this.length; i++)
            {
                if (this.values[i].HasValue)
                {
                    result[i] = this[i].ConvertedTo(this.unit, decimals);
                }
            }
            return(result);
        }
예제 #10
0
        /// <summary>
        /// Returns an AmountVector being the current vector with its values converted
        /// to the given unit and rounded to the given number of decimals.
        /// </summary>
        public AmountVector ConvertedTo(Unit targetUnit, int decimals)
        {
            AmountVector result = new AmountVector(this.length, targetUnit);

            for (int i = 0; i < this.length; i++)
            {
                if (this.values[i].HasValue)
                {
                    Amount a = this[i].ConvertedTo(targetUnit, decimals);
                    result[i] = a;
                }
            }
            return(result);
        }
		public void Enumeration02Test()
		{
			AmountVector v = new AmountVector(4, LengthUnits.KiloMeter);
			v[0] = new Amount(500, LengthUnits.Meter);
			v[2] = new Amount(200, LengthUnits.Meter);

			// Enumerate:
			decimal sum = 0m;
			int loops = 0;
			foreach (Amount a in v.NonNulls())
			{
				Console.WriteLine((a == null) ? "-" : a.ToString());
				if (a != null) sum += a.Value;
				loops++;
			}

			// Checks:
			Assert.AreEqual(2, loops);
			Assert.AreEqual(0.7m, sum);
		}
예제 #12
0
		/// <summary>
		/// Divides two vectors of equal length.
		/// </summary>
		public static AmountVector operator /(AmountVector left, AmountVector right)
		{
			if (left.length != right.length) throw new InvalidOperationException("Can not multiply or divide AmountVectors of different length.");
			AmountVector result = new AmountVector(left.length, left.unit / right.Unit);
			for (int i = 0; i < left.length; i++)
			{
				if (left[i] != null)
				{
					if (right[i] != null)
					{
						result[i] = new Amount(left.values[i].Value / right.values[i].Value, result.unit);
					}
					else
					{
						result[i] = null;
					}
				}
				else
				{
					result[i] = null;
				}
			}
			return result;
		}
		public void ConvertedTo01Test()
		{
			AmountVector v = new AmountVector(4, LengthUnits.Meter);
			v[0] = new Amount(150m, LengthUnits.Meter);
			v[2] = new Amount(0.2m, LengthUnits.KiloMeter);
			v[3] = new Amount(650m, LengthUnits.Meter);

			AmountVector c = v.ConvertedTo(LengthUnits.KiloMeter);

			Assert.AreEqual(1000m, v.Sum.Value);
			Assert.AreEqual(LengthUnits.Meter, v.Sum.Unit);
			Assert.AreEqual(1m, c.Sum.Value);
			Assert.AreEqual(LengthUnits.KiloMeter, c.Sum.Unit);
		}
		public void Average01Test()
		{
			AmountVector v = new AmountVector(4, LengthUnits.Meter);
			v[1] = new Amount(1m, LengthUnits.KiloMeter);
			v[3] = new Amount(500m, LengthUnits.Meter);

			Assert.AreEqual(new Amount(0.750m, LengthUnits.KiloMeter), v.Average);
		}
예제 #15
0
		/// <summary>
		/// Divides each value of a vector with an amount.
		/// </summary>
		public static AmountVector operator /(AmountVector left, Amount right)
		{
			AmountVector result = new AmountVector(left.length, left.unit / right.Unit);
			for (int i = 0; i < left.length; i++)
			{
				if (left[i] != null)
				{
					result[i] = new Amount(left.values[i].Value / right.Value, result.unit);
				}
			}
			return result;
		}
		public void Length01Test()
		{
			AmountVector v = new AmountVector(new decimal?[] { 10m, 3m, null, 5m }, LengthUnits.Meter);

			Assert.AreEqual(4, v.Length);
			Assert.AreEqual(3, v.LengthNonNulls);
		}
		public void SerializeDeserialize01Test()
		{
			MemoryStream buffer = new MemoryStream();

			// Make some amountvector:
			AmountVector v1before = new AmountVector(new decimal?[] { 83000m, null, 12600m, 99250m }, EnergyUnits.KiloWattHour);
			AmountVector v2before = new AmountVector(new decimal?[] { 4m, null, null, 6m }, TimeUnits.Hour);

			// Serialize the units:
			BinaryFormatter f = new BinaryFormatter();
			f.Serialize(buffer, v1before);
			f.Serialize(buffer, v2before);

			// Reset stream:
			buffer.Seek(0, SeekOrigin.Begin);

			// Deserialize units:
			BinaryFormatter g = new BinaryFormatter();
			AmountVector v1after = (AmountVector)g.Deserialize(buffer);
			AmountVector v2after = (AmountVector)g.Deserialize(buffer);

			buffer.Close();

			Console.WriteLine("{0} => {1}", v1before, v1after);
			Console.WriteLine("{0} => {1}", v2before, v2after);
			Console.WriteLine("{0} => {1}", v1before / v2before, v1after / v2after);

			Assert.AreEqual(v1before.Sum, v1after.Sum);
			Assert.AreEqual(v2before.Sum, v2after.Sum);
		}
		public void OperatorDivide01Test()
		{
			AmountVector energies = new AmountVector(new decimal?[] { 83000m, null, 12600m, 99250m }, EnergyUnits.KiloWattHour);
			Amount ghv = new Amount(6600m, EnergyUnits.KiloWattHour / VolumeUnits.Meter3);

			AmountVector volumes = energies / ghv;

			foreach (Amount a in volumes.All())
			{
				Console.WriteLine(a);
			}
			Console.WriteLine("Sum: {0}", volumes.Sum);

			Assert.AreEqual(4, volumes.Length);
			Assert.AreEqual(VolumeUnits.Meter3, volumes.Unit);
			Assert.AreEqual(new Amount(12.58m, VolumeUnits.Meter3), volumes[0].ConvertedTo(VolumeUnits.Meter3, 2));
			Assert.AreEqual(null, volumes[1]);
			Assert.AreEqual(new Amount(1.91m, VolumeUnits.Meter3), volumes[2].ConvertedTo(VolumeUnits.Meter3, 2));
			Assert.AreEqual(new Amount(15.04m, VolumeUnits.Meter3), volumes[3].ConvertedTo(VolumeUnits.Meter3, 2));
			Assert.AreEqual(new Amount(29.52m, VolumeUnits.Meter3), volumes.Sum.ConvertedTo(VolumeUnits.Meter3, 2));
		}
예제 #19
0
		/// <summary>
		/// Returns an AmountVector being the current vector with its values rounded 
		/// to the given number of decimals.
		/// </summary>
		public AmountVector Round(int decimals)
		{
			AmountVector result = new AmountVector(this.length, this.unit);
			for (int i = 0; i < this.length; i++)
			{
				if (this.values[i].HasValue)
				{
					result[i] = this[i].ConvertedTo(this.unit, decimals);
				}
			}
			return result;
		}
		public void MaxAndMin01Test()
		{
			AmountVector v = new AmountVector(5, LengthUnits.Meter);
			v[0] = new Amount(0.2m, LengthUnits.KiloMeter);
			v[2] = new Amount(150m, LengthUnits.Meter);
			v[3] = new Amount(650m, LengthUnits.Meter);
			v[4] = new Amount(500m, LengthUnits.Meter);

			Assert.AreEqual(new Amount(650m, LengthUnits.Meter), v.Max);
			Assert.AreEqual(new Amount(150m, LengthUnits.Meter), v.Min);
		}
		public void OperatorSubstract01Test()
		{
			AmountVector v = new AmountVector(new decimal?[] { 10m, null, null, 5m }, LengthUnits.Meter);
			AmountVector w = new AmountVector(new decimal?[] { null, null, 12m, 6m }, LengthUnits.Meter);

			AmountVector r = v - w;

			foreach (Amount a in r.All())
			{
				Console.WriteLine(a);
			}

			Assert.AreEqual(4, r.Length);
			Assert.AreEqual(v.Unit, r.Unit);
			Assert.AreEqual(10m, r[0].Value);
			Assert.AreEqual(null, r[1]);
			Assert.AreEqual(-12m, r[2].Value);
			Assert.AreEqual(-1m, r[3].Value);
		}
		public void FirstAndLast01Test()
		{
			AmountVector v = new AmountVector(new decimal?[] { 10m, 3m, null, 5m }, LengthUnits.Meter);
			AmountVector w = new AmountVector(new decimal?[] { null, 3m, null, null }, LengthUnits.Meter);
			AmountVector x = new AmountVector(new decimal?[] { null, null, null, null }, LengthUnits.Meter);

			Assert.AreEqual(new Amount(10m, LengthUnits.Meter), v.FirstNonNull);
			Assert.AreEqual(new Amount(3m, LengthUnits.Meter), w.FirstNonNull);
			Assert.AreEqual(null, x.FirstNonNull);
			Assert.AreEqual(new Amount(5m, LengthUnits.Meter), v.LastNonNull);
			Assert.AreEqual(new Amount(3m, LengthUnits.Meter), w.LastNonNull);
			Assert.AreEqual(null, x.LastNonNull);
			Assert.AreEqual(3, v.LengthNonNulls);
		}
예제 #23
0
		/// <summary>
		/// Substracts two vectors of equal length and compatible unit.
		/// </summary>
		public static AmountVector operator -(AmountVector left, AmountVector right)
		{
			if (left.length != right.length) throw new InvalidOperationException("Can not add or substract AmountVectors of different length.");
			if (left.unit.CompatibleTo(right.unit) == false) throw new UnitConversionException("Can not add or substract AmountVectors of incompatible units.");
			AmountVector result = new AmountVector(left.length, left.unit);
			for (int i = 0; i < left.length; i++)
			{
				if (left[i] != null)
				{
					result[i] = left[i];
					if (right[i] != null) result[i] -= right[i];
				}
				else if (right[i] != null) result[i] = -right[i];
			}
			return result;
		}
		public void OperatorMultiply01Test()
		{
			// Suppose following rooms in a house have following areas:
			AmountVector areas = new AmountVector(new decimal?[] { 13m, 7.5m, 6m, 8m, 2m }, SurfaceUnits.Meter2);

			// Suppose all rooms have following height:
			Amount height = new Amount(2.8m, LengthUnits.Meter);

			// Volumes of rooms:
			AmountVector volumes = areas * height;

			foreach (Amount a in volumes.All())
			{
				Console.WriteLine(a);
			}
			Console.WriteLine("Sum: {0}", volumes.Sum);

			Assert.AreEqual(5, volumes.Length);
			Assert.AreEqual(VolumeUnits.Meter3, volumes.Unit);
			Assert.AreEqual(new Amount(36.4m, VolumeUnits.Meter3), volumes[0]);
			Assert.AreEqual(new Amount(21m, VolumeUnits.Meter3), volumes[1]);
			Assert.AreEqual(new Amount(16.8m, VolumeUnits.Meter3), volumes[2]);
			Assert.AreEqual(new Amount(22.4m, VolumeUnits.Meter3), volumes[3]);
			Assert.AreEqual(new Amount(5.6m, VolumeUnits.Meter3), volumes[4]);
			Assert.AreEqual(new Amount(102.2m, VolumeUnits.Meter3), volumes.Sum);
		}
		public void ToArray01Test()
		{
			AmountVector v = new AmountVector(5, LengthUnits.Meter);
			v[0] = new Amount(0.2m, LengthUnits.KiloMeter);
			v[2] = new Amount(150m, LengthUnits.Meter);
			v[3] = new Amount(650m, LengthUnits.Meter);
			v[4] = new Amount(500m, LengthUnits.Meter);

			Amount[] array = v.ToArray();

			Assert.AreEqual(5, array.Length);
			Assert.AreEqual(v.Length, array.Length);
			Assert.AreEqual(v[0], array[0]);
			Assert.AreEqual(v[1], array[1]);
			Assert.AreEqual(v[2], array[2]);
			Assert.AreEqual(v[3], array[3]);
			Assert.AreEqual(v[4], array[4]);
		}
		public void OperatorMultiply02Test()
		{
			// Suppose I have following length measures for rooms:
			AmountVector lengths = new AmountVector(new decimal?[] { 13m, 7.5m, 6m, 8m, 2m }, LengthUnits.Meter);

			// And following width measures for rooms:
			AmountVector widths = new AmountVector(new decimal?[] { 2m, 2m, 3m, 1m, 4m}, LengthUnits.Meter);

			// What are the area's of the respective rooms ?
			AmountVector areas = lengths * widths;

			// Total area:
			Amount totalArea = areas.Sum.ConvertedTo(SurfaceUnits.Meter2);

			for (int i=0; i<lengths.Length; i++)
			{
				Console.WriteLine("{0}: {1} x {2} = {3}", i, lengths[i], widths[i], areas[i]);
			}
			Console.WriteLine("Total area: {0}", totalArea);

			Assert.AreEqual(5, areas.Length);
			Assert.AreEqual(SurfaceUnits.Meter2, areas.Unit);
			Assert.AreEqual(new Amount(26m, SurfaceUnits.Meter2), areas[0]);
			Assert.AreEqual(new Amount(15m, SurfaceUnits.Meter2), areas[1]);
			Assert.AreEqual(new Amount(18m, SurfaceUnits.Meter2), areas[2]);
			Assert.AreEqual(new Amount(8m, SurfaceUnits.Meter2), areas[3]);
			Assert.AreEqual(new Amount(8m, SurfaceUnits.Meter2), areas[4]);
			Assert.AreEqual(new Amount(75m, SurfaceUnits.Meter2), areas.Sum);
		}
예제 #27
0
		/// <summary>
		/// Returns an AmountVector being the current vector with its values converted
		/// to the given unit and rounded to the given number of decimals.
		/// </summary>
		public AmountVector ConvertedTo(Unit targetUnit, int decimals)
		{
			AmountVector result = new AmountVector(this.length, targetUnit);
			for (int i = 0; i < this.length; i++)
			{
				if (this.values[i].HasValue)
				{
					Amount a = this[i].ConvertedTo(targetUnit, decimals);
					result[i] = a;
				}
			}
			return result;
		}