static public void Task3Default() { int arrayLength; int startIndex; Console.WriteLine("=== Input vector 1 :"); arrayLength = Verificaton.IntValue("Length of array"); startIndex = Verificaton.IntValue("Start index"); var vector1 = VectorGeneral.GetRandom(arrayLength, startIndex); Console.WriteLine("=== Input vector 2 (with random values :"); var vector2 = new VectorGeneral(Verificaton.DoubleArray(), Verificaton.IntValue("Start index")); Console.WriteLine("\nVector 1 :"); vector1.Output(); Console.WriteLine("\nVector 2 :"); vector2.Output(); try { Console.WriteLine("\nGetting element of vector 1 by index :"); Console.WriteLine(vector1[Verificaton.IntValue("Vector index")]); } catch (Exception e) { message = "Unable to get vector element"; Console.WriteLine(message); logger?.Error(message, e); } try { Console.WriteLine("\nVector 1 + Vector 2 :"); (vector1 + vector2).Output(); Console.WriteLine("\nVector 1 - Vector 2 :"); (vector1 - vector2).Output(); } catch (Exception e) { message = "Unable to add or substract vectors"; Console.WriteLine(message); logger?.Error(message, e); } Console.WriteLine("\nVector 1 * Scalar :"); vector1.MultScalar(Verificaton.DoubleValue("Scalar value")).Output(); Console.WriteLine($"\nVector 1 is greater than Vector 2 ? {VectorGeneral.Compare(vector1, vector2) > 0}"); }
static public double ScalarProduct(VectorGeneral v1, VectorGeneral v2) { if (v1.MinIndex != v2.MinIndex || v1.MaxIndex != v2.MaxIndex) { throw new Exception("Vectors of different size or starting index"); } double sum = 0; for (int i = v1.MinIndex; i <= v1.MaxIndex; i++) { sum += v1[i] * v2[i]; } return(sum); }
static public VectorGeneral Substract(VectorGeneral v1, VectorGeneral v2) { if (v1.MinIndex != v2.MinIndex || v1.MaxIndex != v2.MaxIndex) { throw new Exception("Vectors of different size or starting index"); } double[] newCoordinates = new double[v1.Length]; for (int i = v1.MinIndex; i <= v1.MaxIndex; i++) { newCoordinates[i - v1.MinIndex] = v1[i] - v2[i]; } return(new VectorGeneral(newCoordinates, v1.MinIndex)); }
public static double Compare(VectorGeneral a, VectorGeneral b) { return(a.VectorLength() - b.VectorLength()); }