-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from kamendov-maxim/kr1
kr1
- Loading branch information
Showing
7 changed files
with
301 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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"); | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
// copyright file: https://github.com/kamendov-maxim/semester2/blob/master/LICENSE | ||
namespace Vector; | ||
|
||
/// <summary> | ||
/// Implementation of SparseVector | ||
/// </summary> | ||
public class SparseVector | ||
{ | ||
private readonly SortedDictionary<int, int> 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; | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Check if vector contains only zeros | ||
/// </summary> | ||
/// <returns>True if there are no other numbers than zeros</returns> | ||
public bool IsZeroVector() | ||
{ | ||
return dictionary.Count == 0; | ||
} | ||
|
||
/// <summary> | ||
/// Convert SparseVector to int array | ||
/// </summary> | ||
/// <returns>Uncompressed array containing all the numbers from vector</returns> | ||
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; | ||
} | ||
|
||
/// <summary> | ||
/// Scalar multiplication on two vectors | ||
/// </summary> | ||
/// <param name="secondVector">Vector you want to multiply your vector with</param> | ||
/// <returns>Result of scalat multiplication</returns> | ||
/// <exception cref="DifferentLengthVectorsException">Thrown if lengths of vectors are different</exception> | ||
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; | ||
} | ||
|
||
/// <summary> | ||
/// Substract one vector from another | ||
/// </summary> | ||
/// <param name="secondVector">Vector you want to substract from your vector</param> | ||
/// <exception cref="DifferentLengthVectorsException">Thrown if lengths of vectors are different</exception> | ||
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); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Sum one vector from another | ||
/// </summary> | ||
/// <param name="secondVector">Vector you want to sum with your vector</param> | ||
/// <exception cref="DifferentLengthVectorsException">Thrown if lengths of vectors are different</exception> | ||
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; | ||
} | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// GetNumber located at the given index in vector | ||
/// </summary> | ||
/// <param name="index">index</param> | ||
/// <returns>Number at index</returns> | ||
public int GetNumberAtIndex(int index) | ||
{ | ||
dictionary.TryGetValue(index, out int result); | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
|
||
<IsPackable>false</IsPackable> | ||
<IsTestProject>true</IsTestProject> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="coverlet.collector" Version="6.0.0" /> | ||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.8.0" /> | ||
<PackageReference Include="NUnit" Version="3.14.0" /> | ||
<PackageReference Include="NUnit.Analyzers" Version="3.9.0" /> | ||
<PackageReference Include="NUnit3TestAdapter" Version="4.5.0" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<Using Include="NUnit.Framework" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Vector.Src\Vector.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
Check warning on line 11 in kr1/Vector.Tests/VectorTests.cs GitHub Actions / build-Ubuntu_and_MacOs (ubuntu-latest)
Check warning on line 11 in kr1/Vector.Tests/VectorTests.cs GitHub Actions / build-Windows
Check warning on line 11 in kr1/Vector.Tests/VectorTests.cs GitHub Actions / build-Windows
|
||
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<DifferentLengthVectorsException>(() => vector.Sum(secondVector)); | ||
Assert.Throws<DifferentLengthVectorsException>(() => vector.Substract(secondVector)); | ||
Assert.Throws<DifferentLengthVectorsException>(() => vector.ScalarMultiplicationWith(secondVector)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |