diff --git a/kr1/Vector.Src/DifferentLengthVectorsException.cs b/kr1/Vector.Src/DifferentLengthVectorsException.cs new file mode 100644 index 0000000..4580308 --- /dev/null +++ b/kr1/Vector.Src/DifferentLengthVectorsException.cs @@ -0,0 +1,7 @@ +// copyright file: https://github.com/kamendov-maxim/semester2/blob/master/LICENSE +public class DifferentLengthVectorsException : Exception +{ + public DifferentLengthVectorsException() { } + public DifferentLengthVectorsException(string message) : base(message) { } + public DifferentLengthVectorsException(string message, Exception inner) : base(message, inner) { } +} diff --git a/kr1/Vector.Src/Program.cs b/kr1/Vector.Src/Program.cs new file mode 100644 index 0000000..aea9ced --- /dev/null +++ b/kr1/Vector.Src/Program.cs @@ -0,0 +1,4 @@ +// copyright file: https://github.com/kamendov-maxim/semester2/blob/master/LICENSE + +Console.WriteLine("This program dos not have user interface but it is not a library because when output is not exe tests do not work"); + diff --git a/kr1/Vector.Src/SparseVector.cs b/kr1/Vector.Src/SparseVector.cs new file mode 100644 index 0000000..8a31932 --- /dev/null +++ b/kr1/Vector.Src/SparseVector.cs @@ -0,0 +1,142 @@ +// copyright file: https://github.com/kamendov-maxim/semester2/blob/master/LICENSE +namespace Vector; + +/// +/// Implementation of SparseVector +/// +public class SparseVector +{ + private readonly SortedDictionary dictionary; + public readonly int Length; + public SparseVector(int[] vector) + { + ArgumentNullException.ThrowIfNull(vector); + dictionary = []; + Length = vector.Length; + + int i = 0; + foreach (var number in vector) + { + if (number != 0) + { + dictionary[i] = number; + } + ++i; + } + } + + /// + /// Check if vector contains only zeros + /// + /// True if there are no other numbers than zeros + public bool IsZeroVector() + { + return dictionary.Count == 0; + } + + /// + /// Convert SparseVector to int array + /// + /// Uncompressed array containing all the numbers from vector + public int[] ToArray() + { + var array = new int[Length]; + for (int i = 0; i < Length; ++i) + { + dictionary.TryGetValue(i, out int currentNumber); + array[i] = currentNumber; + } + return array; + } + + /// + /// Scalar multiplication on two vectors + /// + /// Vector you want to multiply your vector with + /// Result of scalat multiplication + /// Thrown if lengths of vectors are different + public int ScalarMultiplicationWith(SparseVector secondVector) + { + if (Length != secondVector.Length) + { + throw new DifferentLengthVectorsException("You can't do any operations with vectors of different length"); + } + if (dictionary.Count == 0 || secondVector.IsZeroVector()) + { + return 0; + } + int result = 0; + for (int i = 0; i < Length; ++i) + { + dictionary.TryGetValue(i, out int firstNumber); + result += secondVector.GetNumberAtIndex(i) * firstNumber; + } + return result; + } + + /// + /// Substract one vector from another + /// + /// Vector you want to substract from your vector + /// Thrown if lengths of vectors are different + public void Substract(SparseVector secondVector) + { + if (Length != secondVector.Length) + { + throw new DifferentLengthVectorsException("You can't do any operations with vectors of different length"); + } + if (!secondVector.IsZeroVector()) + { + SumAndSubstract(secondVector, true); + } + } + + /// + /// Sum one vector from another + /// + /// Vector you want to sum with your vector + /// Thrown if lengths of vectors are different + public void Sum(SparseVector secondVector) + { + if (Length != secondVector.Length) + { + throw new DifferentLengthVectorsException("You can't do any operations with vectors of different length"); + } + if (!secondVector.IsZeroVector()) + { + SumAndSubstract(secondVector, false); + } + } + + private void SumAndSubstract(SparseVector secondVector, bool substract) + { + int multiplier = substract ? -1 : 1; + + for (int i = 0; i < Length; ++i) + { + int newValueAtIndex = 0; + dictionary.TryGetValue(i, out int currentElement); + newValueAtIndex += currentElement; + newValueAtIndex += secondVector.GetNumberAtIndex(i) * multiplier; + if (newValueAtIndex == 0) + { + dictionary.Remove(i); + } + else + { + dictionary[i] = newValueAtIndex; + } + } + } + + /// + /// GetNumber located at the given index in vector + /// + /// index + /// Number at index + public int GetNumberAtIndex(int index) + { + dictionary.TryGetValue(index, out int result); + return result; + } +} diff --git a/kr1/Vector.Src/Vector.csproj b/kr1/Vector.Src/Vector.csproj new file mode 100644 index 0000000..206b89a --- /dev/null +++ b/kr1/Vector.Src/Vector.csproj @@ -0,0 +1,10 @@ + + + + Exe + net8.0 + enable + enable + + + diff --git a/kr1/Vector.Tests/Vector.Tests.csproj b/kr1/Vector.Tests/Vector.Tests.csproj new file mode 100644 index 0000000..77d58b4 --- /dev/null +++ b/kr1/Vector.Tests/Vector.Tests.csproj @@ -0,0 +1,28 @@ + + + + net8.0 + enable + enable + + false + true + + + + + + + + + + + + + + + + + + + diff --git a/kr1/Vector.Tests/VectorTests.cs b/kr1/Vector.Tests/VectorTests.cs new file mode 100644 index 0000000..fa05231 --- /dev/null +++ b/kr1/Vector.Tests/VectorTests.cs @@ -0,0 +1,83 @@ +// copyright file: https://github.com/kamendov-maxim/semester2/blob/master/LICENSE +namespace Vector.Tests; + +using Vector; + +public class Tests +{ + readonly int[] testArray1 = [0, 0, 0, 0, 0, 1, 0, 0, 2, 0, 0, 0, 0, 3, 4, 0, 5]; + readonly int[] testArray2 = [1, 0, 3, 0, 0, 5, 0, 0, 0, 0, 0, 6, 0, 5, 2, 0, 0]; + readonly int[] sub = [-1, 0, -3, 0, 0, -4, 0, 0, 2, 0, 0, -6, 0, -2, 2, 0, 5]; + readonly int MultiplicationAnswer = 28; + readonly int[] testArray3 = [0, 0, 0, 0, 0, 1, 0, 0, 2]; + readonly int[] testZeroArray1 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + readonly int[] testZeroArray2 = [0, 0, 0, 0, 0, 0, 0, 0]; + + [Test] + public void TestCreateInstanceAndGetArrayWithoutChanges() + { + var vector = new SparseVector(testArray1); + var newArray = vector.ToArray(); + Assert.That(newArray, Is.EqualTo(testArray1)); + } + + [Test] + public void TestSummation() + { + var vector = new SparseVector(testArray1); + var secondVector = new SparseVector(testArray2); + vector.Sum(secondVector); + var array = vector.ToArray(); + + int[] answer = [1, 0, 3, 0, 0, 6, 0, 0, 2, 0, 0, 6, 0, 8, 6, 0, 5]; + Assert.That(array, Is.EqualTo(answer)); + } + + [Test] + public void TestSubstraction() + { + var vector = new SparseVector(testArray1); + var secondVector = new SparseVector(testArray2); + vector.Substract(secondVector); + var array = vector.ToArray(); + + int[] answer = [-1, 0, -3, 0, 0, -4, 0, 0, 2, 0, 0, -6, 0, -2, 2, 0, 5]; + Assert.That(array, Is.EqualTo(answer)); + } + + [Test] + public void TestMultiplication() + { + var vector = new SparseVector(testArray1); + var secondVector = new SparseVector(testArray2); + var result = vector.ScalarMultiplicationWith(secondVector); + + int answer = 28; + Assert.That(result, Is.EqualTo(answer)); + } + + [Test] + public void ZeroVectorCases() + { + var vector = new SparseVector(testArray1); + var secondVector = new SparseVector(testZeroArray1); + vector.Sum(secondVector); + var array = vector.ToArray(); + Assert.That(array, Is.EqualTo(testArray1)); + vector.Substract(secondVector); + array = vector.ToArray(); + Assert.That(array, Is.EqualTo(testArray1)); + int MultiplicationResult = vector.ScalarMultiplicationWith(secondVector); + Assert.That(MultiplicationResult, Is.EqualTo(0)); + } + + [Test] + public void TestThatThrowsException() + { + var vector = new SparseVector(testArray1); + var secondVector = new SparseVector(testArray3); + Assert.Throws(() => vector.Sum(secondVector)); + Assert.Throws(() => vector.Substract(secondVector)); + Assert.Throws(() => vector.ScalarMultiplicationWith(secondVector)); + } +} \ No newline at end of file diff --git a/kr1/Vector.sln b/kr1/Vector.sln new file mode 100644 index 0000000..8cc80d3 --- /dev/null +++ b/kr1/Vector.sln @@ -0,0 +1,27 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vector", "Vector.Src\Vector.csproj", "{9CBC1F22-F006-4FF9-80AE-643710600557}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Vector.Tests", "Vector.Tests\Vector.Tests.csproj", "{8767C23E-251E-4CED-8952-54BAF2870521}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9CBC1F22-F006-4FF9-80AE-643710600557}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9CBC1F22-F006-4FF9-80AE-643710600557}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9CBC1F22-F006-4FF9-80AE-643710600557}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9CBC1F22-F006-4FF9-80AE-643710600557}.Release|Any CPU.Build.0 = Release|Any CPU + {8767C23E-251E-4CED-8952-54BAF2870521}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8767C23E-251E-4CED-8952-54BAF2870521}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8767C23E-251E-4CED-8952-54BAF2870521}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8767C23E-251E-4CED-8952-54BAF2870521}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal